1 /* $Id: macctrls.c,v 1.19 2003/03/29 20:16:51 ben Exp $ */
3 * Copyright (c) 2003 Ben Harris
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <Appearance.h>
31 #include <ControlDefinitions.h>
33 #include <Resources.h>
37 #include <TextUtils.h>
38 #include <ToolUtils.h>
50 /* Range of menu IDs for popup menus */
56 struct macctrl_generic {
65 /* Template from which this was generated */
67 /* Next control in this panel */
73 struct macctrl_generic generic;
77 struct macctrl_generic generic;
82 struct macctrl_generic generic;
87 struct macctrl_generic generic;
91 struct macctrl_generic generic;
95 struct macctrl_generic generic;
104 struct mac_layoutstate {
107 unsigned int panelnum;
110 #define ctrlevent(mcs, mc, event) do { \
111 if ((mc)->generic.ctrl->generic.handler != NULL) \
112 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
113 (mcs)->data, (event)); \
116 #define findbyctrl(mcs, ctrl) \
117 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
119 static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *,
120 WindowPtr, struct macctrls *);
121 static void macctrl_switchtopanel(struct macctrls *, unsigned int);
122 static void macctrl_setfocus(struct macctrls *, union macctrl *);
123 static void macctrl_text(struct macctrls *, WindowPtr,
124 struct mac_layoutstate *, union control *);
125 static void macctrl_editbox(struct macctrls *, WindowPtr,
126 struct mac_layoutstate *, union control *);
127 static void macctrl_radio(struct macctrls *, WindowPtr,
128 struct mac_layoutstate *, union control *);
129 static void macctrl_checkbox(struct macctrls *, WindowPtr,
130 struct mac_layoutstate *, union control *);
131 static void macctrl_button(struct macctrls *, WindowPtr,
132 struct mac_layoutstate *, union control *);
133 static void macctrl_popup(struct macctrls *, WindowPtr,
134 struct mac_layoutstate *, union control *);
135 #if !TARGET_API_MAC_CARBON
136 static pascal SInt32 macctrl_sys7_text_cdef(SInt16, ControlRef,
137 ControlDefProcMessage, SInt32);
138 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef,
139 ControlDefProcMessage, SInt32);
140 static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
141 ControlDefProcMessage, SInt32);
144 #if !TARGET_API_MAC_CARBON
146 * This trick enables us to keep all the CDEF code in the main
147 * application, which makes life easier. For details, see
148 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
151 #pragma options align=mac68k
153 short jmpabs; /* 4EF9 */
154 ControlDefUPP theUPP;
156 #pragma options align=reset
159 static void macctrl_init()
161 #if !TARGET_API_MAC_CARBON
162 static int inited = 0;
166 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Text);
167 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_text_cdef);
168 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox);
169 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef);
170 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
171 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
177 static int macctrl_cmp_byctrl(void *av, void *bv)
179 union macctrl *a = (union macctrl *)av;
180 union macctrl *b = (union macctrl *)bv;
182 if (a->generic.ctrl < b->generic.ctrl)
184 else if (a->generic.ctrl > b->generic.ctrl)
190 static int macctrl_cmp_byctrl_find(void *av, void *bv)
192 union control *a = (union control *)av;
193 union macctrl *b = (union macctrl *)bv;
195 if (a < b->generic.ctrl)
197 else if (a > b->generic.ctrl)
203 void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
204 struct macctrls *mcs)
207 struct mac_layoutstate curstate;
212 #if TARGET_API_MAC_CARBON
213 GetPortBounds(GetWindowPort(window), &rect);
215 rect = window->portRect;
217 curstate.pos.h = rect.left + 13;
218 curstate.pos.v = rect.bottom - 59;
219 curstate.width = rect.right - rect.left - (13 * 2);
220 if (mac_gestalts.apprvers >= 0x100)
221 CreateRootControl(window, &root);
222 mcs->window = window;
223 mcs->byctrl = newtree234(macctrl_cmp_byctrl);
225 /* Count the number of panels */
227 for (i = 1; i < cb->nctrlsets; i++)
228 if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
230 mcs->panels = smalloc(sizeof(*mcs->panels) * mcs->npanels);
231 memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
232 curstate.panelnum = 0;
233 for (i = 0; i < cb->nctrlsets; i++) {
234 if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
235 cb->ctrlsets[i-1]->pathname)) {
236 curstate.pos.v = rect.top + 13;
238 assert(curstate.panelnum < mcs->npanels);
240 macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
242 macctrl_switchtopanel(mcs, 14);
245 static void macctrl_layoutset(struct mac_layoutstate *curstate,
246 struct controlset *s,
247 WindowPtr window, struct macctrls *mcs)
251 fprintf(stderr, "--- begin set ---\n");
252 fprintf(stderr, "pathname = %s\n", s->pathname);
253 if (s->boxname && *s->boxname)
254 fprintf(stderr, "boxname = %s\n", s->boxname);
256 fprintf(stderr, "boxtitle = %s\n", s->boxtitle);
259 for (i = 0; i < s->ncontrols; i++) {
260 union control *ctrl = s->ctrls[i];
263 switch (ctrl->generic.type) {
264 case CTRL_TEXT: s = "text"; break;
265 case CTRL_EDITBOX: s = "editbox"; break;
266 case CTRL_RADIO: s = "radio"; break;
267 case CTRL_CHECKBOX: s = "checkbox"; break;
268 case CTRL_BUTTON: s = "button"; break;
269 case CTRL_LISTBOX: s = "listbox"; break;
270 case CTRL_COLUMNS: s = "columns"; break;
271 case CTRL_FILESELECT: s = "fileselect"; break;
272 case CTRL_FONTSELECT: s = "fontselect"; break;
273 case CTRL_TABDELAY: s = "tabdelay"; break;
274 default: s = "unknown"; break;
276 fprintf(stderr, " control: %s\n", s);
277 switch (ctrl->generic.type) {
279 macctrl_text(mcs, window, curstate, ctrl);
282 macctrl_editbox(mcs, window, curstate, ctrl);
285 macctrl_radio(mcs, window, curstate, ctrl);
288 macctrl_checkbox(mcs, window, curstate, ctrl);
291 macctrl_button(mcs, window, curstate, ctrl);
294 if (ctrl->listbox.height == 0)
295 macctrl_popup(mcs, window, curstate, ctrl);
301 static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
306 #define hideshow(c) do { \
307 if (i == which) ShowControl(c); else HideControl(c); \
310 mcs->curpanel = which;
311 /* Panel 0 is special and always visible. */
312 for (i = 1; i < mcs->npanels; i++)
313 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
314 #if !TARGET_API_MAC_CARBON
315 if (mcs->focus == mc)
316 macctrl_setfocus(mcs, NULL);
318 switch (mc->generic.type) {
320 hideshow(mc->text.tbctrl);
322 case MACCTRL_EDITBOX:
323 hideshow(mc->editbox.tbctrl);
324 hideshow(mc->editbox.tblabel);
327 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
328 hideshow(mc->radio.tbctrls[j]);
329 hideshow(mc->radio.tblabel);
331 case MACCTRL_CHECKBOX:
332 hideshow(mc->checkbox.tbctrl);
335 hideshow(mc->button.tbctrl);
338 hideshow(mc->popup.tbctrl);
344 #if !TARGET_API_MAC_CARBON
346 * System 7 focus manipulation
348 static void macctrl_defocus(union macctrl *mc)
351 assert(mac_gestalts.apprvers < 0x100);
352 switch (mc->generic.type) {
353 case MACCTRL_EDITBOX:
354 TEDeactivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
359 static void macctrl_enfocus(union macctrl *mc)
362 assert(mac_gestalts.apprvers < 0x100);
363 switch (mc->generic.type) {
364 case MACCTRL_EDITBOX:
365 TEActivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
370 static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc)
373 if (mcs->focus != NULL)
374 macctrl_defocus(mcs->focus);
381 static void macctrl_text(struct macctrls *mcs, WindowPtr window,
382 struct mac_layoutstate *curstate,
385 union macctrl *mc = smalloc(sizeof *mc);
388 fprintf(stderr, " label = %s\n", ctrl->text.label);
389 mc->generic.type = MACCTRL_TEXT;
390 mc->generic.ctrl = ctrl;
391 mc->generic.privdata = NULL;
392 bounds.left = curstate->pos.h;
393 bounds.right = bounds.left + curstate->width;
394 bounds.top = curstate->pos.v;
395 bounds.bottom = bounds.top + 16;
396 if (mac_gestalts.apprvers >= 0x100) {
400 mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
401 kControlStaticTextProc, (long)mc);
402 SetControlData(mc->text.tbctrl, kControlEntireControl,
403 kControlStaticTextTextTag,
404 strlen(ctrl->text.label), ctrl->text.label);
405 GetControlData(mc->text.tbctrl, kControlEntireControl,
406 kControlStaticTextTextHeightTag,
407 sizeof(height), &height, &olen);
408 fprintf(stderr, " height = %d\n", height);
409 SizeControl(mc->text.tbctrl, curstate->width, height);
410 curstate->pos.v += height + 6;
414 c2pstrcpy(title, ctrl->text.label);
415 mc->text.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 0,
416 SYS7_TEXT_PROC, (long)mc);
418 add234(mcs->byctrl, mc);
419 mc->generic.next = mcs->panels[curstate->panelnum];
420 mcs->panels[curstate->panelnum] = mc;
423 #if !TARGET_API_MAC_CARBON
424 static pascal SInt32 macctrl_sys7_text_cdef(SInt16 variant, ControlRef control,
425 ControlDefProcMessage msg, SInt32 param)
431 if ((*control)->contrlVis)
432 TETextBox((*control)->contrlTitle + 1, (*control)->contrlTitle[0],
433 &(*control)->contrlRect, teFlushDefault);
436 if (param & (1 << 31)) {
442 rgn = (RgnHandle)param;
443 RectRgn(rgn, &(*control)->contrlRect);
447 rgn = (RgnHandle)param;
456 static void macctrl_editbox(struct macctrls *mcs, WindowPtr window,
457 struct mac_layoutstate *curstate,
460 union macctrl *mc = smalloc(sizeof *mc);
461 Rect lbounds, bounds;
463 fprintf(stderr, " label = %s\n", ctrl->editbox.label);
464 fprintf(stderr, " percentwidth = %d\n", ctrl->editbox.percentwidth);
465 if (ctrl->editbox.password) fprintf(stderr, " password\n");
466 if (ctrl->editbox.has_list) fprintf(stderr, " has list\n");
467 mc->generic.type = MACCTRL_EDITBOX;
468 mc->generic.ctrl = ctrl;
469 mc->generic.privdata = NULL;
470 lbounds.left = curstate->pos.h;
471 lbounds.top = curstate->pos.v;
472 if (ctrl->editbox.percentwidth == 100) {
473 lbounds.right = lbounds.left + curstate->width;
474 lbounds.bottom = lbounds.top + 16;
475 bounds.left = curstate->pos.h;
476 bounds.right = bounds.left + curstate->width;
477 curstate->pos.v += 18;
479 lbounds.right = lbounds.left +
480 curstate->width * (100 - ctrl->editbox.percentwidth) / 100;
481 lbounds.bottom = lbounds.top + 22;
482 bounds.left = lbounds.right;
483 bounds.right = lbounds.left + curstate->width;
485 bounds.top = curstate->pos.v;
486 bounds.bottom = bounds.top + 22;
487 if (mac_gestalts.apprvers >= 0x100) {
488 mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE, 0, 0, 0,
489 kControlStaticTextProc, (long)mc);
490 SetControlData(mc->editbox.tblabel, kControlEntireControl,
491 kControlStaticTextTextTag,
492 strlen(ctrl->editbox.label), ctrl->editbox.label);
493 InsetRect(&bounds, 2, 2);
494 mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
495 ctrl->editbox.password ?
496 kControlEditTextPasswordProc :
497 kControlEditTextProc, (long)mc);
501 c2pstrcpy(title, ctrl->editbox.label);
502 mc->editbox.tblabel = NewControl(window, &lbounds, title, TRUE,
503 0, 0, 0, SYS7_TEXT_PROC, (long)mc);
504 mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
505 SYS7_EDITBOX_PROC, (long)mc);
507 curstate->pos.v += 28;
508 add234(mcs->byctrl, mc);
509 mc->generic.next = mcs->panels[curstate->panelnum];
510 mcs->panels[curstate->panelnum] = mc;
511 ctrlevent(mcs, mc, EVENT_REFRESH);
514 #if !TARGET_API_MAC_CARBON
515 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant,
517 ControlDefProcMessage msg,
528 rect = (*control)->contrlRect;
529 InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */
530 te = TENew(&rect, &rect);
531 ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
532 (*te)->txSize = LoWord(ssfs);
533 (*te)->txFont = HiWord(ssfs);
534 (*control)->contrlData = (Handle)te;
537 TEDispose((TEHandle)(*control)->contrlData);
540 if ((*control)->contrlVis) {
541 rect = (*control)->contrlRect;
544 InsetRect(&rect, 3, 3);
545 TEUpdate(&rect, (TEHandle)(*control)->contrlData);
549 mouse.h = LoWord(param);
550 mouse.v = HiWord(param);
552 PtInRect(mouse, &(*(TEHandle)(*control)->contrlData)->viewRect) ?
553 kControlEditTextPart : kControlNoPart;
555 if (param & (1 << 31)) {
561 rgn = (RgnHandle)param;
562 RectRgn(rgn, &(*control)->contrlRect);
566 rgn = (RgnHandle)param;
575 static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
576 struct mac_layoutstate *curstate,
579 union macctrl *mc = smalloc(sizeof *mc);
582 unsigned int i, colwidth;
584 fprintf(stderr, " label = %s\n", ctrl->radio.label);
585 mc->generic.type = MACCTRL_RADIO;
586 mc->generic.ctrl = ctrl;
587 mc->generic.privdata = NULL;
589 smalloc(sizeof(*mc->radio.tbctrls) * ctrl->radio.nbuttons);
590 colwidth = (curstate->width + 13) / ctrl->radio.ncolumns;
591 bounds.top = curstate->pos.v;
592 bounds.bottom = bounds.top + 16;
593 bounds.left = curstate->pos.h;
594 bounds.right = bounds.left + curstate->width;
595 if (mac_gestalts.apprvers >= 0x100) {
596 mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
597 kControlStaticTextProc, (long)mc);
598 SetControlData(mc->radio.tblabel, kControlEntireControl,
599 kControlStaticTextTextTag,
600 strlen(ctrl->radio.label), ctrl->radio.label);
604 c2pstrcpy(title, ctrl->radio.label);
605 mc->editbox.tblabel = NewControl(window, &bounds, title, TRUE,
606 0, 0, 0, SYS7_TEXT_PROC, (long)mc);
608 curstate->pos.v += 18;
609 for (i = 0; i < ctrl->radio.nbuttons; i++) {
610 fprintf(stderr, " button = %s\n", ctrl->radio.buttons[i]);
611 bounds.top = curstate->pos.v - 2;
612 bounds.bottom = bounds.top + 18;
613 bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
614 if (i == ctrl->radio.nbuttons - 1 ||
615 i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
616 bounds.right = curstate->pos.h + curstate->width;
617 curstate->pos.v += 18;
619 bounds.right = bounds.left + colwidth - 13;
620 c2pstrcpy(title, ctrl->radio.buttons[i]);
621 mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE,
622 0, 0, 1, radioButProc, (long)mc);
624 curstate->pos.v += 4;
625 add234(mcs->byctrl, mc);
626 mc->generic.next = mcs->panels[curstate->panelnum];
627 mcs->panels[curstate->panelnum] = mc;
628 ctrlevent(mcs, mc, EVENT_REFRESH);
631 static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
632 struct mac_layoutstate *curstate,
635 union macctrl *mc = smalloc(sizeof *mc);
639 fprintf(stderr, " label = %s\n", ctrl->checkbox.label);
640 mc->generic.type = MACCTRL_CHECKBOX;
641 mc->generic.ctrl = ctrl;
642 mc->generic.privdata = NULL;
643 bounds.left = curstate->pos.h;
644 bounds.right = bounds.left + curstate->width;
645 bounds.top = curstate->pos.v;
646 bounds.bottom = bounds.top + 16;
647 c2pstrcpy(title, ctrl->checkbox.label);
648 mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
649 checkBoxProc, (long)mc);
650 add234(mcs->byctrl, mc);
651 curstate->pos.v += 22;
652 mc->generic.next = mcs->panels[curstate->panelnum];
653 mcs->panels[curstate->panelnum] = mc;
654 ctrlevent(mcs, mc, EVENT_REFRESH);
657 static void macctrl_button(struct macctrls *mcs, WindowPtr window,
658 struct mac_layoutstate *curstate,
661 union macctrl *mc = smalloc(sizeof *mc);
665 fprintf(stderr, " label = %s\n", ctrl->button.label);
666 if (ctrl->button.isdefault)
667 fprintf(stderr, " is default\n");
668 mc->generic.type = MACCTRL_BUTTON;
669 mc->generic.ctrl = ctrl;
670 mc->generic.privdata = NULL;
671 bounds.left = curstate->pos.h;
672 bounds.right = bounds.left + 100; /* XXX measure string */
673 bounds.top = curstate->pos.v;
674 bounds.bottom = bounds.top + 20;
675 c2pstrcpy(title, ctrl->button.label);
676 mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
677 pushButProc, (long)mc);
678 if (mac_gestalts.apprvers >= 0x100) {
679 Boolean isdefault = ctrl->button.isdefault;
681 SetControlData(mc->button.tbctrl, kControlEntireControl,
682 kControlPushButtonDefaultTag,
683 sizeof(isdefault), &isdefault);
684 } else if (ctrl->button.isdefault) {
685 InsetRect(&bounds, -4, -4);
686 NewControl(window, &bounds, title, TRUE, 0, 0, 1,
687 SYS7_DEFAULT_PROC, (long)mc);
689 if (mac_gestalts.apprvers >= 0x110) {
690 Boolean iscancel = ctrl->button.iscancel;
692 SetControlData(mc->button.tbctrl, kControlEntireControl,
693 kControlPushButtonCancelTag,
694 sizeof(iscancel), &iscancel);
696 add234(mcs->byctrl, mc);
697 mc->generic.next = mcs->panels[curstate->panelnum];
698 mcs->panels[curstate->panelnum] = mc;
699 curstate->pos.v += 26;
702 #if !TARGET_API_MAC_CARBON
703 static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
705 ControlDefProcMessage msg,
714 if ((*control)->contrlVis) {
715 rect = (*control)->contrlRect;
718 oval = (rect.bottom - rect.top) / 2 + 2;
719 FrameRoundRect(&rect, oval, oval);
723 if (param & (1 << 31)) {
729 rgn = (RgnHandle)param;
730 RectRgn(rgn, &(*control)->contrlRect);
734 rgn = (RgnHandle)param;
743 static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
744 struct mac_layoutstate *curstate,
747 union macctrl *mc = smalloc(sizeof *mc);
750 unsigned int labelwidth;
751 static int nextmenuid = MENU_MIN;
756 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
757 * create a popup menu with dynamic content.
759 assert(ctrl->listbox.height == 0);
760 assert(!ctrl->listbox.draglist);
761 assert(!ctrl->listbox.multisel);
763 fprintf(stderr, " label = %s\n", ctrl->listbox.label);
764 fprintf(stderr, " percentwidth = %d\n", ctrl->listbox.percentwidth);
766 mc->generic.type = MACCTRL_POPUP;
767 mc->generic.ctrl = ctrl;
768 mc->generic.privdata = NULL;
769 c2pstrcpy(title, ctrl->button.label);
771 /* Find a spare menu ID and create the menu */
772 while (GetMenuHandle(nextmenuid) != NULL)
773 if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
774 menuid = nextmenuid++;
775 menu = NewMenu(menuid, "\pdummy");
776 if (menu == NULL) return;
777 mc->popup.menu = menu;
778 mc->popup.menuid = menuid;
779 InsertMenu(menu, kInsertHierarchicalMenu);
781 /* The menu starts off empty */
783 mc->popup.ids = NULL;
785 bounds.left = curstate->pos.h;
786 bounds.right = bounds.left + curstate->width;
787 bounds.top = curstate->pos.v;
788 bounds.bottom = bounds.top + 20;
789 /* XXX handle percentwidth == 100 */
790 labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
791 mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE,
792 popupTitleLeftJust, menuid, labelwidth,
793 popupMenuProc + popupFixedWidth, (long)mc);
794 add234(mcs->byctrl, mc);
795 curstate->pos.v += 26;
796 mc->generic.next = mcs->panels[curstate->panelnum];
797 mcs->panels[curstate->panelnum] = mc;
798 ctrlevent(mcs, mc, EVENT_REFRESH);
802 void macctrl_activate(WindowPtr window, EventRecord *event)
804 struct macctrls *mcs = mac_winctrls(window);
805 Boolean active = (event->modifiers & activeFlag) != 0;
808 ControlPartCode state;
812 SetPort((GrafPtr)GetWindowPort(window));
813 if (mac_gestalts.apprvers >= 0x100)
814 SetThemeWindowBackground(window, active ?
815 kThemeBrushModelessDialogBackgroundActive :
816 kThemeBrushModelessDialogBackgroundInactive,
818 state = active ? kControlNoPart : kControlInactivePart;
819 for (i = 0; i <= mcs->curpanel; i += mcs->curpanel)
820 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next)
821 switch (mc->generic.type) {
823 HiliteControl(mc->text.tbctrl, state);
825 case MACCTRL_EDITBOX:
826 HiliteControl(mc->editbox.tbctrl, state);
827 HiliteControl(mc->editbox.tblabel, state);
830 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
831 HiliteControl(mc->radio.tbctrls[j], state);
832 HiliteControl(mc->radio.tblabel, state);
834 case MACCTRL_CHECKBOX:
835 HiliteControl(mc->checkbox.tbctrl, state);
838 HiliteControl(mc->button.tbctrl, state);
841 HiliteControl(mc->popup.tbctrl, state);
847 void macctrl_click(WindowPtr window, EventRecord *event)
850 ControlHandle control;
851 int part, trackresult;
854 struct macctrls *mcs = mac_winctrls(window);
859 SetPort((GrafPtr)GetWindowPort(window));
860 mouse = event->where;
861 GlobalToLocal(&mouse);
862 part = FindControl(mouse, window, &control);
863 if (control != NULL) {
864 mc = (union macctrl *)GetControlReference(control);
865 if (mac_gestalts.apprvers >= 0x100) {
866 if (GetControlFeatures(control, &features) == noErr &&
867 (features & kControlSupportsFocus) &&
868 (features & kControlGetsFocusOnClick))
869 SetKeyboardFocus(window, control, part);
870 trackresult = HandleControlClick(control, mouse, event->modifiers,
871 (ControlActionUPP)-1);
873 #if !TARGET_API_MAC_CARBON
874 if (mc->generic.type == MACCTRL_EDITBOX &&
875 control == mc->editbox.tbctrl) {
876 TEHandle te = (TEHandle)(*control)->contrlData;
878 macctrl_setfocus(mcs, mc);
879 TEClick(mouse, !!(event->modifiers & shiftKey), te);
883 trackresult = TrackControl(control, mouse, (ControlActionUPP)-1);
885 switch (mc->generic.type) {
887 if (trackresult != 0) {
888 for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
889 if (mc->radio.tbctrls[i] == control)
890 SetControlValue(mc->radio.tbctrls[i],
891 kControlRadioButtonCheckedValue);
893 SetControlValue(mc->radio.tbctrls[i],
894 kControlRadioButtonUncheckedValue);
895 ctrlevent(mcs, mc, EVENT_VALCHANGE);
898 case MACCTRL_CHECKBOX:
899 if (trackresult != 0) {
900 SetControlValue(control, !GetControlValue(control));
901 ctrlevent(mcs, mc, EVENT_VALCHANGE);
905 if (trackresult != 0)
906 ctrlevent(mcs, mc, EVENT_ACTION);
909 ctrlevent(mcs, mc, EVENT_SELCHANGE);
917 void macctrl_key(WindowPtr window, EventRecord *event)
920 struct macctrls *mcs = mac_winctrls(window);
923 if (mac_gestalts.apprvers >= 0x100 &&
924 GetKeyboardFocus(window, &control) == noErr && control != NULL) {
925 HandleControlKey(control, (event->message & keyCodeMask) >> 8,
926 event->message & charCodeMask, event->modifiers);
927 mc = (union macctrl *)GetControlReference(control);
928 ctrlevent(mcs, mc, EVENT_VALCHANGE);
932 void macctrl_update(WindowPtr window)
934 #if TARGET_API_MAC_CARBON
942 SetPort((GrafPtr)GetWindowPort(window));
943 if (mac_gestalts.apprvers >= 0x101) {
944 #if TARGET_API_MAC_CARBON
945 GetPortBounds(GetWindowPort(window), &rect);
947 rect = window->portRect;
949 InsetRect(&rect, -1, -1);
950 DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ?
951 kThemeStateActive : kThemeStateInactive);
953 #if TARGET_API_MAC_CARBON
955 GetPortVisibleRegion(GetWindowPort(window), visrgn);
956 UpdateControls(window, visrgn);
959 UpdateControls(window, window->visRgn);
965 #if TARGET_API_MAC_CARBON
966 #define EnableItem EnableMenuItem
967 #define DisableItem DisableMenuItem
969 void macctrl_adjustmenus(WindowPtr window)
973 menu = GetMenuHandle(mFile);
974 DisableItem(menu, iSave); /* XXX enable if modified */
975 EnableItem(menu, iSaveAs);
976 EnableItem(menu, iDuplicate);
978 menu = GetMenuHandle(mEdit);
979 DisableItem(menu, 0);
982 void macctrl_close(WindowPtr window)
984 struct macctrls *mcs = mac_winctrls(window);
988 * Mostly, we don't bother disposing of the Toolbox controls,
989 * since that will happen automatically when the window is
990 * disposed of. Popup menus are an exception, because we have to
991 * dispose of the menu ourselves, and doing that while the control
992 * still holds a reference to it seems rude.
994 while ((mc = index234(mcs->byctrl, 0)) != NULL) {
995 if (mc->generic.privdata != NULL && mc->generic.freeprivdata)
996 sfree(mc->generic.privdata);
997 switch (mc->generic.type) {
999 DisposeControl(mc->popup.tbctrl);
1000 DeleteMenu(mc->popup.menuid);
1001 DisposeMenu(mc->popup.menu);
1004 del234(mcs->byctrl, mc);
1008 freetree234(mcs->byctrl);
1014 void dlg_update_start(union control *ctrl, void *dlg)
1020 void dlg_update_done(union control *ctrl, void *dlg)
1026 void dlg_set_focus(union control *ctrl, void *dlg)
1029 if (mac_gestalts.apprvers >= 0x100) {
1030 /* Use SetKeyboardFocus() */
1032 /* Do our own mucking around */
1036 union control *dlg_last_focused(union control *ctrl, void *dlg)
1042 void dlg_beep(void *dlg)
1048 void dlg_error_msg(void *dlg, char *msg)
1052 c2pstrcpy(pmsg, msg);
1053 ParamText(pmsg, NULL, NULL, NULL);
1054 StopAlert(128, NULL);
1057 void dlg_end(void *dlg, int value)
1062 void dlg_refresh(union control *ctrl, void *dlg)
1064 struct macctrls *mcs = dlg;
1069 mc = findbyctrl(mcs, ctrl);
1071 ctrlevent(mcs, mc, EVENT_REFRESH);
1074 void *dlg_get_privdata(union control *ctrl, void *dlg)
1076 struct macctrls *mcs = dlg;
1077 union macctrl *mc = findbyctrl(mcs, ctrl);
1080 return mc->generic.privdata;
1083 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
1085 struct macctrls *mcs = dlg;
1086 union macctrl *mc = findbyctrl(mcs, ctrl);
1089 mc->generic.privdata = ptr;
1090 mc->generic.freeprivdata = FALSE;
1093 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
1095 struct macctrls *mcs = dlg;
1096 union macctrl *mc = findbyctrl(mcs, ctrl);
1099 mc->generic.privdata = smalloc(size);
1100 mc->generic.freeprivdata = TRUE;
1101 return mc->generic.privdata;
1106 * Radio Button control
1109 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
1111 struct macctrls *mcs = dlg;
1112 union macctrl *mc = findbyctrl(mcs, ctrl);
1116 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1117 if (i == whichbutton)
1118 SetControlValue(mc->radio.tbctrls[i],
1119 kControlRadioButtonCheckedValue);
1121 SetControlValue(mc->radio.tbctrls[i],
1122 kControlRadioButtonUncheckedValue);
1127 int dlg_radiobutton_get(union control *ctrl, void *dlg)
1129 struct macctrls *mcs = dlg;
1130 union macctrl *mc = findbyctrl(mcs, ctrl);
1134 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1135 if (GetControlValue(mc->radio.tbctrls[i]) ==
1136 kControlRadioButtonCheckedValue)
1147 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
1149 struct macctrls *mcs = dlg;
1150 union macctrl *mc = findbyctrl(mcs, ctrl);
1153 SetControlValue(mc->checkbox.tbctrl,
1154 checked ? kControlCheckBoxCheckedValue :
1155 kControlCheckBoxUncheckedValue);
1158 int dlg_checkbox_get(union control *ctrl, void *dlg)
1160 struct macctrls *mcs = dlg;
1161 union macctrl *mc = findbyctrl(mcs, ctrl);
1164 return GetControlValue(mc->checkbox.tbctrl);
1172 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
1174 struct macctrls *mcs = dlg;
1175 union macctrl *mc = findbyctrl(mcs, ctrl);
1179 assert(mc->generic.type == MACCTRL_EDITBOX);
1181 SetPort((GrafPtr)(GetWindowPort(mcs->window)));
1182 if (mac_gestalts.apprvers >= 0x100)
1183 SetControlData(mc->editbox.tbctrl, kControlEntireControl,
1184 ctrl->editbox.password ?
1185 kControlEditTextPasswordTag :
1186 kControlEditTextTextTag,
1187 strlen(text), text);
1188 #if !TARGET_API_MAC_CARBON
1190 TESetText(text, strlen(text),
1191 (TEHandle)(*mc->editbox.tbctrl)->contrlData);
1193 DrawOneControl(mc->editbox.tbctrl);
1197 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
1199 struct macctrls *mcs = dlg;
1200 union macctrl *mc = findbyctrl(mcs, ctrl);
1204 assert(mc->generic.type == MACCTRL_EDITBOX);
1205 if (mac_gestalts.apprvers >= 0x100) {
1206 if (GetControlData(mc->editbox.tbctrl, kControlEntireControl,
1207 ctrl->editbox.password ?
1208 kControlEditTextPasswordTag :
1209 kControlEditTextTextTag,
1210 length - 1, buffer, &olen) != noErr)
1212 if (olen > length - 1)
1215 #if !TARGET_API_MAC_CARBON
1217 TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1219 olen = (*te)->teLength;
1220 if (olen > length - 1)
1222 memcpy(buffer, *(*te)->hText, olen);
1225 buffer[olen] = '\0';
1226 fprintf(stderr, "dlg_editbox_get: %s\n", buffer);
1234 static void dlg_macpopup_clear(union control *ctrl, void *dlg)
1236 struct macctrls *mcs = dlg;
1237 union macctrl *mc = findbyctrl(mcs, ctrl);
1238 MenuRef menu = mc->popup.menu;
1241 fprintf(stderr, " popup_clear\n");
1242 n = CountMenuItems(menu);
1243 for (i = 0; i < n; i++)
1244 DeleteMenuItem(menu, n - i);
1246 sfree(mc->popup.ids);
1247 mc->popup.ids = NULL;
1248 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1251 void dlg_listbox_clear(union control *ctrl, void *dlg)
1254 if (ctrl->listbox.height == 0)
1255 dlg_macpopup_clear(ctrl, dlg);
1258 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
1260 struct macctrls *mcs = dlg;
1261 union macctrl *mc = findbyctrl(mcs, ctrl);
1262 MenuRef menu = mc->popup.menu;
1264 fprintf(stderr, " popup_del %d\n", index);
1265 DeleteMenuItem(menu, index + 1);
1266 if (mc->popup.ids != NULL)
1267 memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
1268 (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
1269 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1272 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
1275 if (ctrl->listbox.height == 0)
1276 dlg_macpopup_del(ctrl, dlg, index);
1279 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
1281 struct macctrls *mcs = dlg;
1282 union macctrl *mc = findbyctrl(mcs, ctrl);
1283 MenuRef menu = mc->popup.menu;
1286 fprintf(stderr, " popup_add %s\n", text);
1287 assert(text[0] != '\0');
1288 c2pstrcpy(itemstring, text);
1289 AppendMenu(menu, "\pdummy");
1290 SetMenuItemText(menu, CountMenuItems(menu), itemstring);
1291 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1294 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
1297 if (ctrl->listbox.height == 0)
1298 dlg_macpopup_add(ctrl, dlg, text);
1301 static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
1302 char const *text, int id)
1304 struct macctrls *mcs = dlg;
1305 union macctrl *mc = findbyctrl(mcs, ctrl);
1306 MenuRef menu = mc->popup.menu;
1309 fprintf(stderr, " popup_addwthindex %s, %d\n", text, id);
1310 dlg_macpopup_add(ctrl, dlg, text);
1311 index = CountMenuItems(menu) - 1;
1312 if (mc->popup.nids <= index) {
1313 mc->popup.nids = index + 1;
1314 mc->popup.ids = srealloc(mc->popup.ids,
1315 mc->popup.nids * sizeof(*mc->popup.ids));
1317 mc->popup.ids[index] = id;
1320 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
1321 char const *text, int id)
1324 if (ctrl->listbox.height == 0)
1325 dlg_macpopup_addwithid(ctrl, dlg, text, id);
1328 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
1330 struct macctrls *mcs = dlg;
1331 union macctrl *mc = findbyctrl(mcs, ctrl);
1333 if (ctrl->listbox.height == 0) {
1334 assert(mc->popup.ids != NULL && mc->popup.nids > index);
1335 return mc->popup.ids[index];
1340 int dlg_listbox_index(union control *ctrl, void *dlg)
1342 struct macctrls *mcs = dlg;
1343 union macctrl *mc = findbyctrl(mcs, ctrl);
1345 if (ctrl->listbox.height == 0)
1346 return GetControlValue(mc->popup.tbctrl) - 1;
1350 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1352 struct macctrls *mcs = dlg;
1353 union macctrl *mc = findbyctrl(mcs, ctrl);
1355 if (ctrl->listbox.height == 0)
1356 return GetControlValue(mc->popup.tbctrl) - 1 == index;
1360 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1362 struct macctrls *mcs = dlg;
1363 union macctrl *mc = findbyctrl(mcs, ctrl);
1365 if (ctrl->listbox.height == 0)
1366 SetControlValue(mc->popup.tbctrl, index + 1);
1374 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1376 struct macctrls *mcs = dlg;
1377 union macctrl *mc = findbyctrl(mcs, ctrl);
1381 if (mac_gestalts.apprvers >= 0x100)
1382 SetControlData(mc->text.tbctrl, kControlEntireControl,
1383 kControlStaticTextTextTag, strlen(text), text);
1385 c2pstrcpy(title, text);
1386 SetControlTitle(mc->text.tbctrl, title);
1392 * File Selector control
1395 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1400 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1407 * Font Selector control
1410 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1415 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1422 * Printer enumeration
1425 printer_enum *printer_start_enum(int *nprinters)
1432 char *printer_get_name(printer_enum *pe, int thing)
1438 void printer_finish_enum(printer_enum *pe)
1445 * Colour selection stuff
1448 void dlg_coloursel_start(union control *ctrl, void *dlg,
1449 int r, int g, int b)
1454 int dlg_coloursel_results(union control *ctrl, void *dlg,
1455 int *r, int *g, int *b)
1463 * c-file-style: "simon"