1 /* $Id: macctrls.c,v 1.28 2003/04/05 17:19:38 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>
34 #include <Resources.h>
38 #include <TextUtils.h>
39 #include <ToolUtils.h>
51 /* Range of menu IDs for popup menus */
57 struct macctrl_generic {
66 /* Template from which this was generated */
68 /* Next control in this panel */
74 struct macctrl_generic generic;
78 struct macctrl_generic generic;
83 struct macctrl_generic generic;
88 struct macctrl_generic generic;
92 struct macctrl_generic generic;
96 struct macctrl_generic generic;
105 struct mac_layoutstate {
108 unsigned int panelnum;
111 #define ctrlevent(mcs, mc, event) do { \
112 if ((mc)->generic.ctrl->generic.handler != NULL) \
113 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
114 (mcs)->data, (event)); \
117 #define findbyctrl(mcs, ctrl) \
118 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
120 static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *,
121 WindowPtr, struct macctrls *);
122 static void macctrl_switchtopanel(struct macctrls *, unsigned int);
123 static void macctrl_setfocus(struct macctrls *, union macctrl *);
124 static void macctrl_text(struct macctrls *, WindowPtr,
125 struct mac_layoutstate *, union control *);
126 static void macctrl_editbox(struct macctrls *, WindowPtr,
127 struct mac_layoutstate *, union control *);
128 static void macctrl_radio(struct macctrls *, WindowPtr,
129 struct mac_layoutstate *, union control *);
130 static void macctrl_checkbox(struct macctrls *, WindowPtr,
131 struct mac_layoutstate *, union control *);
132 static void macctrl_button(struct macctrls *, WindowPtr,
133 struct mac_layoutstate *, union control *);
134 static void macctrl_popup(struct macctrls *, WindowPtr,
135 struct mac_layoutstate *, union control *);
136 #if !TARGET_API_MAC_CARBON
137 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef,
138 ControlDefProcMessage, SInt32);
139 static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
140 ControlDefProcMessage, SInt32);
143 #if !TARGET_API_MAC_CARBON
145 * This trick enables us to keep all the CDEF code in the main
146 * application, which makes life easier. For details, see
147 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
150 #pragma options align=mac68k
152 short jmpabs; /* 4EF9 */
153 ControlDefUPP theUPP;
155 #pragma options align=reset
158 static void macctrl_init()
160 #if !TARGET_API_MAC_CARBON
161 static int inited = 0;
165 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox);
166 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef);
167 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
168 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
174 static int macctrl_cmp_byctrl(void *av, void *bv)
176 union macctrl *a = (union macctrl *)av;
177 union macctrl *b = (union macctrl *)bv;
179 if (a->generic.ctrl < b->generic.ctrl)
181 else if (a->generic.ctrl > b->generic.ctrl)
187 static int macctrl_cmp_byctrl_find(void *av, void *bv)
189 union control *a = (union control *)av;
190 union macctrl *b = (union macctrl *)bv;
192 if (a < b->generic.ctrl)
194 else if (a > b->generic.ctrl)
200 void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
201 struct macctrls *mcs)
204 struct mac_layoutstate curstate;
209 #if TARGET_API_MAC_CARBON
210 GetPortBounds(GetWindowPort(window), &rect);
212 rect = window->portRect;
214 curstate.pos.h = rect.left + 13;
215 curstate.pos.v = rect.bottom - 33;
216 curstate.width = rect.right - rect.left - (13 * 2);
217 if (mac_gestalts.apprvers >= 0x100)
218 CreateRootControl(window, &root);
219 mcs->window = window;
220 mcs->byctrl = newtree234(macctrl_cmp_byctrl);
222 mcs->defbutton = NULL;
223 mcs->canbutton = NULL;
224 /* Count the number of panels */
226 for (i = 1; i < cb->nctrlsets; i++)
227 if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
229 mcs->panels = snewn(mcs->npanels, union macctrl *);
230 memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
231 curstate.panelnum = 0;
232 for (i = 0; i < cb->nctrlsets; i++) {
233 if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
234 cb->ctrlsets[i-1]->pathname)) {
235 curstate.pos.v = rect.top + 13;
237 assert(curstate.panelnum < mcs->npanels);
239 macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
241 macctrl_switchtopanel(mcs, 1);
242 /* 14 = proxies, 20 = SSH bugs */
247 static void macctrl_layoutset(struct mac_layoutstate *curstate,
248 struct controlset *s,
249 WindowPtr window, struct macctrls *mcs)
251 unsigned int i, j, ncols, colstart;
252 struct mac_layoutstate cols[MAXCOLS];
254 fprintf(stderr, "--- begin set ---\n");
255 fprintf(stderr, "pathname = %s\n", s->pathname);
256 if (s->boxname && *s->boxname)
257 fprintf(stderr, "boxname = %s\n", s->boxname);
259 fprintf(stderr, "boxtitle = %s\n", s->boxtitle);
264 for (i = 0; i < s->ncontrols; i++) {
265 union control *ctrl = s->ctrls[i];
268 colstart = COLUMN_START(ctrl->generic.column);
269 switch (ctrl->generic.type) {
270 case CTRL_TEXT: s = "text"; break;
271 case CTRL_EDITBOX: s = "editbox"; break;
272 case CTRL_RADIO: s = "radio"; break;
273 case CTRL_CHECKBOX: s = "checkbox"; break;
274 case CTRL_BUTTON: s = "button"; break;
275 case CTRL_LISTBOX: s = "listbox"; break;
276 case CTRL_COLUMNS: s = "columns"; break;
277 case CTRL_FILESELECT: s = "fileselect"; break;
278 case CTRL_FONTSELECT: s = "fontselect"; break;
279 case CTRL_TABDELAY: s = "tabdelay"; break;
280 default: s = "unknown"; break;
282 fprintf(stderr, " control: %s\n", s);
283 switch (ctrl->generic.type) {
285 if (ctrl->columns.ncols != 1) {
286 ncols = ctrl->columns.ncols;
287 fprintf(stderr, " split to %d\n", ncols);
288 assert(ncols <= MAXCOLS);
289 for (j = 0; j < ncols; j++) {
292 cols[j].pos.h = cols[j-1].pos.h + cols[j-1].width + 6;
294 cols[j].width = curstate->width -
295 (cols[j].pos.h - curstate->pos.h);
297 cols[j].width = (curstate->width + 6) *
298 ctrl->columns.percentages[j] / 100 - 6;
301 fprintf(stderr, " join\n");
302 for (j = 0; j < ncols; j++)
303 if (cols[j].pos.v > cols[0].pos.v)
304 cols[0].pos.v = cols[j].pos.v;
305 cols[0].width = curstate->width;
310 macctrl_text(mcs, window, &cols[colstart], ctrl);
313 macctrl_editbox(mcs, window, &cols[colstart], ctrl);
316 macctrl_radio(mcs, window, &cols[colstart], ctrl);
319 macctrl_checkbox(mcs, window, &cols[colstart], ctrl);
322 macctrl_button(mcs, window, &cols[colstart], ctrl);
325 if (ctrl->listbox.height == 0)
326 macctrl_popup(mcs, window, &cols[colstart], ctrl);
330 for (j = 0; j < ncols; j++)
331 if (cols[j].pos.v > curstate->pos.v)
332 curstate->pos.v = cols[j].pos.v;
335 static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
340 #define hideshow(c) do { \
341 if (i == which) ShowControl(c); else HideControl(c); \
344 mcs->curpanel = which;
345 /* Panel 0 is special and always visible. */
346 for (i = 1; i < mcs->npanels; i++)
347 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
348 #if !TARGET_API_MAC_CARBON
349 if (mcs->focus == mc)
350 macctrl_setfocus(mcs, NULL);
352 switch (mc->generic.type) {
354 hideshow(mc->text.tbctrl);
356 case MACCTRL_EDITBOX:
357 hideshow(mc->editbox.tbctrl);
358 if (mc->editbox.tblabel != NULL)
359 hideshow(mc->editbox.tblabel);
362 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
363 hideshow(mc->radio.tbctrls[j]);
364 if (mc->radio.tblabel != NULL)
365 hideshow(mc->radio.tblabel);
367 case MACCTRL_CHECKBOX:
368 hideshow(mc->checkbox.tbctrl);
371 hideshow(mc->button.tbctrl);
374 hideshow(mc->popup.tbctrl);
380 #if !TARGET_API_MAC_CARBON
382 * System 7 focus manipulation
384 static void macctrl_defocus(union macctrl *mc)
387 assert(mac_gestalts.apprvers < 0x100);
388 switch (mc->generic.type) {
389 case MACCTRL_EDITBOX:
390 TEDeactivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
395 static void macctrl_enfocus(union macctrl *mc)
398 assert(mac_gestalts.apprvers < 0x100);
399 switch (mc->generic.type) {
400 case MACCTRL_EDITBOX:
401 TEActivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
406 static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc)
409 if (mcs->focus != NULL)
410 macctrl_defocus(mcs->focus);
417 static void macctrl_text(struct macctrls *mcs, WindowPtr window,
418 struct mac_layoutstate *curstate,
421 union macctrl *mc = snew(union macctrl);
425 assert(ctrl->text.label != NULL);
426 fprintf(stderr, " label = %s\n", ctrl->text.label);
427 mc->generic.type = MACCTRL_TEXT;
428 mc->generic.ctrl = ctrl;
429 mc->generic.privdata = NULL;
430 bounds.left = curstate->pos.h;
431 bounds.right = bounds.left + curstate->width;
432 bounds.top = curstate->pos.v;
433 bounds.bottom = bounds.top + 16;
434 if (mac_gestalts.apprvers >= 0x100) {
437 mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
438 kControlStaticTextProc, (long)mc);
439 SetControlData(mc->text.tbctrl, kControlEntireControl,
440 kControlStaticTextTextTag,
441 strlen(ctrl->text.label), ctrl->text.label);
442 GetControlData(mc->text.tbctrl, kControlEntireControl,
443 kControlStaticTextTextHeightTag,
444 sizeof(height), &height, &olen);
446 #if !TARGET_API_MAC_CARBON
450 mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
451 SYS7_TEXT_PROC, (long)mc);
452 te = (TEHandle)(*mc->text.tbctrl)->contrlData;
453 TESetText(ctrl->text.label, strlen(ctrl->text.label), te);
454 height = TEGetHeight(1, (*te)->nLines, te);
457 fprintf(stderr, " height = %d\n", height);
458 SizeControl(mc->text.tbctrl, curstate->width, height);
459 curstate->pos.v += height + 6;
460 add234(mcs->byctrl, mc);
461 mc->generic.next = mcs->panels[curstate->panelnum];
462 mcs->panels[curstate->panelnum] = mc;
465 static void macctrl_editbox(struct macctrls *mcs, WindowPtr window,
466 struct mac_layoutstate *curstate,
469 union macctrl *mc = snew(union macctrl);
470 Rect lbounds, bounds;
472 if (ctrl->editbox.label != NULL)
473 fprintf(stderr, " label = %s\n", ctrl->editbox.label);
474 fprintf(stderr, " percentwidth = %d\n", ctrl->editbox.percentwidth);
475 if (ctrl->editbox.password) fprintf(stderr, " password\n");
476 if (ctrl->editbox.has_list) fprintf(stderr, " has list\n");
477 mc->generic.type = MACCTRL_EDITBOX;
478 mc->generic.ctrl = ctrl;
479 mc->generic.privdata = NULL;
480 lbounds.left = curstate->pos.h;
481 lbounds.top = curstate->pos.v;
482 if (ctrl->editbox.percentwidth == 100) {
483 if (ctrl->editbox.label != NULL) {
484 lbounds.right = lbounds.left + curstate->width;
485 lbounds.bottom = lbounds.top + 16;
486 curstate->pos.v += 18;
488 bounds.left = curstate->pos.h;
489 bounds.right = bounds.left + curstate->width;
491 lbounds.right = lbounds.left +
492 curstate->width * (100 - ctrl->editbox.percentwidth) / 100;
493 lbounds.bottom = lbounds.top + 22;
494 bounds.left = lbounds.right;
495 bounds.right = lbounds.left + curstate->width;
497 bounds.top = curstate->pos.v;
498 bounds.bottom = bounds.top + 22;
499 if (mac_gestalts.apprvers >= 0x100) {
500 if (ctrl->editbox.label == NULL)
501 mc->editbox.tblabel = NULL;
503 mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE,
504 0, 0, 0, kControlStaticTextProc,
506 SetControlData(mc->editbox.tblabel, kControlEntireControl,
507 kControlStaticTextTextTag,
508 strlen(ctrl->editbox.label), ctrl->editbox.label);
510 InsetRect(&bounds, 3, 3);
511 mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
512 ctrl->editbox.password ?
513 kControlEditTextPasswordProc :
514 kControlEditTextProc, (long)mc);
516 #if !TARGET_API_MAC_CARBON
518 if (ctrl->editbox.label == NULL)
519 mc->editbox.tblabel = NULL;
521 mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE,
522 0, 0, 0, SYS7_TEXT_PROC,
524 TESetText(ctrl->editbox.label, strlen(ctrl->editbox.label),
525 (TEHandle)(*mc->editbox.tblabel)->contrlData);
527 mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
528 SYS7_EDITBOX_PROC, (long)mc);
531 curstate->pos.v += 28;
532 add234(mcs->byctrl, mc);
533 mc->generic.next = mcs->panels[curstate->panelnum];
534 mcs->panels[curstate->panelnum] = mc;
535 ctrlevent(mcs, mc, EVENT_REFRESH);
538 #if !TARGET_API_MAC_CARBON
539 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant,
541 ControlDefProcMessage msg,
552 rect = (*control)->contrlRect;
553 if (variant == SYS7_EDITBOX_VARIANT)
554 InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */
555 te = TENew(&rect, &rect);
556 ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
557 (*te)->txSize = LoWord(ssfs);
558 (*te)->txFont = HiWord(ssfs);
559 (*control)->contrlData = (Handle)te;
562 TEDispose((TEHandle)(*control)->contrlData);
565 if ((*control)->contrlVis) {
566 rect = (*control)->contrlRect;
567 if (variant == SYS7_EDITBOX_VARIANT) {
570 InsetRect(&rect, 3, 3);
572 (*(TEHandle)(*control)->contrlData)->viewRect = rect;
573 TEUpdate(&rect, (TEHandle)(*control)->contrlData);
577 if (variant == SYS7_TEXT_VARIANT)
578 return kControlNoPart;
579 mouse.h = LoWord(param);
580 mouse.v = HiWord(param);
581 rect = (*control)->contrlRect;
582 InsetRect(&rect, 3, 3);
583 return PtInRect(mouse, &rect) ? kControlEditTextPart : kControlNoPart;
585 if (param & (1 << 31)) {
591 rgn = (RgnHandle)param;
592 RectRgn(rgn, &(*control)->contrlRect);
596 rgn = (RgnHandle)param;
605 static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
606 struct mac_layoutstate *curstate,
609 union macctrl *mc = snew(union macctrl);
612 unsigned int i, colwidth;
614 if (ctrl->radio.label != NULL)
615 fprintf(stderr, " label = %s\n", ctrl->radio.label);
616 mc->generic.type = MACCTRL_RADIO;
617 mc->generic.ctrl = ctrl;
618 mc->generic.privdata = NULL;
619 mc->radio.tbctrls = snewn(ctrl->radio.nbuttons, ControlRef);
620 colwidth = (curstate->width + 13) / ctrl->radio.ncolumns;
621 bounds.top = curstate->pos.v;
622 bounds.bottom = bounds.top + 16;
623 bounds.left = curstate->pos.h;
624 bounds.right = bounds.left + curstate->width;
625 if (ctrl->radio.label == NULL)
626 mc->radio.tblabel = NULL;
628 if (mac_gestalts.apprvers >= 0x100) {
629 mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE,
630 0, 0, 0, kControlStaticTextProc,
632 SetControlData(mc->radio.tblabel, kControlEntireControl,
633 kControlStaticTextTextTag,
634 strlen(ctrl->radio.label), ctrl->radio.label);
636 #if !TARGET_API_MAC_CARBON
638 mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE,
639 0, 0, 0, SYS7_TEXT_PROC, (long)mc);
640 TESetText(ctrl->radio.label, strlen(ctrl->radio.label),
641 (TEHandle)(*mc->radio.tblabel)->contrlData);
644 curstate->pos.v += 18;
646 for (i = 0; i < ctrl->radio.nbuttons; i++) {
647 fprintf(stderr, " button = %s\n", ctrl->radio.buttons[i]);
648 bounds.top = curstate->pos.v - 2;
649 bounds.bottom = bounds.top + 18;
650 bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
651 if (i == ctrl->radio.nbuttons - 1 ||
652 i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
653 bounds.right = curstate->pos.h + curstate->width;
654 curstate->pos.v += 18;
656 bounds.right = bounds.left + colwidth - 13;
657 c2pstrcpy(title, ctrl->radio.buttons[i]);
658 mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE,
659 0, 0, 1, radioButProc, (long)mc);
661 curstate->pos.v += 4;
662 add234(mcs->byctrl, mc);
663 mc->generic.next = mcs->panels[curstate->panelnum];
664 mcs->panels[curstate->panelnum] = mc;
665 ctrlevent(mcs, mc, EVENT_REFRESH);
668 static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
669 struct mac_layoutstate *curstate,
672 union macctrl *mc = snew(union macctrl);
676 assert(ctrl->checkbox.label != NULL);
677 fprintf(stderr, " label = %s\n", ctrl->checkbox.label);
678 mc->generic.type = MACCTRL_CHECKBOX;
679 mc->generic.ctrl = ctrl;
680 mc->generic.privdata = NULL;
681 bounds.left = curstate->pos.h;
682 bounds.right = bounds.left + curstate->width;
683 bounds.top = curstate->pos.v;
684 bounds.bottom = bounds.top + 16;
685 c2pstrcpy(title, ctrl->checkbox.label);
686 mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
687 checkBoxProc, (long)mc);
688 add234(mcs->byctrl, mc);
689 curstate->pos.v += 22;
690 mc->generic.next = mcs->panels[curstate->panelnum];
691 mcs->panels[curstate->panelnum] = mc;
692 ctrlevent(mcs, mc, EVENT_REFRESH);
695 static void macctrl_button(struct macctrls *mcs, WindowPtr window,
696 struct mac_layoutstate *curstate,
699 union macctrl *mc = snew(union macctrl);
703 assert(ctrl->button.label != NULL);
704 fprintf(stderr, " label = %s\n", ctrl->button.label);
705 if (ctrl->button.isdefault)
706 fprintf(stderr, " is default\n");
707 mc->generic.type = MACCTRL_BUTTON;
708 mc->generic.ctrl = ctrl;
709 mc->generic.privdata = NULL;
710 bounds.left = curstate->pos.h;
711 bounds.right = bounds.left + curstate->width;
712 bounds.top = curstate->pos.v;
713 bounds.bottom = bounds.top + 20;
714 c2pstrcpy(title, ctrl->button.label);
715 mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
716 pushButProc, (long)mc);
717 if (mac_gestalts.apprvers >= 0x100) {
718 Boolean isdefault = ctrl->button.isdefault;
720 SetControlData(mc->button.tbctrl, kControlEntireControl,
721 kControlPushButtonDefaultTag,
722 sizeof(isdefault), &isdefault);
723 } else if (ctrl->button.isdefault) {
724 InsetRect(&bounds, -4, -4);
725 NewControl(window, &bounds, title, TRUE, 0, 0, 1,
726 SYS7_DEFAULT_PROC, (long)mc);
728 if (mac_gestalts.apprvers >= 0x110) {
729 Boolean iscancel = ctrl->button.iscancel;
731 SetControlData(mc->button.tbctrl, kControlEntireControl,
732 kControlPushButtonCancelTag,
733 sizeof(iscancel), &iscancel);
735 if (ctrl->button.isdefault)
737 if (ctrl->button.iscancel)
739 add234(mcs->byctrl, mc);
740 mc->generic.next = mcs->panels[curstate->panelnum];
741 mcs->panels[curstate->panelnum] = mc;
742 curstate->pos.v += 26;
745 #if !TARGET_API_MAC_CARBON
746 static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
748 ControlDefProcMessage msg,
757 if ((*control)->contrlVis) {
758 rect = (*control)->contrlRect;
761 oval = (rect.bottom - rect.top) / 2 + 2;
762 FrameRoundRect(&rect, oval, oval);
766 if (param & (1 << 31)) {
772 rgn = (RgnHandle)param;
773 RectRgn(rgn, &(*control)->contrlRect);
777 rgn = (RgnHandle)param;
786 static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
787 struct mac_layoutstate *curstate,
790 union macctrl *mc = snew(union macctrl);
793 unsigned int labelwidth;
794 static int nextmenuid = MENU_MIN;
799 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
800 * create a popup menu with dynamic content.
802 assert(ctrl->listbox.height == 0);
803 assert(!ctrl->listbox.draglist);
804 assert(!ctrl->listbox.multisel);
806 if (ctrl->listbox.label != NULL)
807 fprintf(stderr, " label = %s\n", ctrl->listbox.label);
808 fprintf(stderr, " percentwidth = %d\n", ctrl->listbox.percentwidth);
810 mc->generic.type = MACCTRL_POPUP;
811 mc->generic.ctrl = ctrl;
812 mc->generic.privdata = NULL;
813 c2pstrcpy(title, ctrl->button.label == NULL ? "" : ctrl->button.label);
815 /* Find a spare menu ID and create the menu */
816 while (GetMenuHandle(nextmenuid) != NULL)
817 if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
818 menuid = nextmenuid++;
819 menu = NewMenu(menuid, "\pdummy");
820 if (menu == NULL) return;
821 mc->popup.menu = menu;
822 mc->popup.menuid = menuid;
823 InsertMenu(menu, kInsertHierarchicalMenu);
825 /* The menu starts off empty */
827 mc->popup.ids = NULL;
829 bounds.left = curstate->pos.h;
830 bounds.right = bounds.left + curstate->width;
831 bounds.top = curstate->pos.v;
832 bounds.bottom = bounds.top + 20;
833 /* XXX handle percentwidth == 100 */
834 labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
835 mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE,
836 popupTitleLeftJust, menuid, labelwidth,
837 popupMenuProc + popupFixedWidth, (long)mc);
838 add234(mcs->byctrl, mc);
839 curstate->pos.v += 26;
840 mc->generic.next = mcs->panels[curstate->panelnum];
841 mcs->panels[curstate->panelnum] = mc;
842 ctrlevent(mcs, mc, EVENT_REFRESH);
846 void macctrl_activate(WindowPtr window, EventRecord *event)
848 struct macctrls *mcs = mac_winctrls(window);
849 Boolean active = (event->modifiers & activeFlag) != 0;
852 ControlPartCode state;
856 SetPort((GrafPtr)GetWindowPort(window));
857 if (mac_gestalts.apprvers >= 0x100)
858 SetThemeWindowBackground(window, active ?
859 kThemeBrushModelessDialogBackgroundActive :
860 kThemeBrushModelessDialogBackgroundInactive,
862 state = active ? kControlNoPart : kControlInactivePart;
863 for (i = 0; i <= mcs->curpanel; i += mcs->curpanel)
864 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
865 switch (mc->generic.type) {
867 HiliteControl(mc->text.tbctrl, state);
869 case MACCTRL_EDITBOX:
870 HiliteControl(mc->editbox.tbctrl, state);
871 if (mc->editbox.tblabel != NULL)
872 HiliteControl(mc->editbox.tblabel, state);
875 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
876 HiliteControl(mc->radio.tbctrls[j], state);
877 if (mc->radio.tblabel != NULL)
878 HiliteControl(mc->radio.tblabel, state);
880 case MACCTRL_CHECKBOX:
881 HiliteControl(mc->checkbox.tbctrl, state);
884 HiliteControl(mc->button.tbctrl, state);
887 HiliteControl(mc->popup.tbctrl, state);
890 #if !TARGET_API_MAC_CARBON
891 if (mcs->focus == mc) {
902 void macctrl_click(WindowPtr window, EventRecord *event)
905 ControlHandle control;
906 int part, trackresult;
909 struct macctrls *mcs = mac_winctrls(window);
914 SetPort((GrafPtr)GetWindowPort(window));
915 mouse = event->where;
916 GlobalToLocal(&mouse);
917 part = FindControl(mouse, window, &control);
918 if (control != NULL) {
919 mc = (union macctrl *)GetControlReference(control);
920 if (mac_gestalts.apprvers >= 0x100) {
921 if (GetControlFeatures(control, &features) == noErr &&
922 (features & kControlSupportsFocus) &&
923 (features & kControlGetsFocusOnClick))
924 SetKeyboardFocus(window, control, part);
925 trackresult = HandleControlClick(control, mouse, event->modifiers,
926 (ControlActionUPP)-1);
928 #if !TARGET_API_MAC_CARBON
929 if (mc->generic.type == MACCTRL_EDITBOX &&
930 control == mc->editbox.tbctrl) {
931 TEHandle te = (TEHandle)(*control)->contrlData;
933 macctrl_setfocus(mcs, mc);
934 TEClick(mouse, !!(event->modifiers & shiftKey), te);
938 trackresult = TrackControl(control, mouse, (ControlActionUPP)-1);
940 switch (mc->generic.type) {
942 if (trackresult != 0) {
943 for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
944 if (mc->radio.tbctrls[i] == control)
945 SetControlValue(mc->radio.tbctrls[i],
946 kControlRadioButtonCheckedValue);
948 SetControlValue(mc->radio.tbctrls[i],
949 kControlRadioButtonUncheckedValue);
950 ctrlevent(mcs, mc, EVENT_VALCHANGE);
953 case MACCTRL_CHECKBOX:
954 if (trackresult != 0) {
955 SetControlValue(control, !GetControlValue(control));
956 ctrlevent(mcs, mc, EVENT_VALCHANGE);
960 if (trackresult != 0)
961 ctrlevent(mcs, mc, EVENT_ACTION);
964 ctrlevent(mcs, mc, EVENT_SELCHANGE);
972 void macctrl_key(WindowPtr window, EventRecord *event)
975 struct macctrls *mcs = mac_winctrls(window);
979 switch (event->message & charCodeMask) {
981 case kReturnCharCode:
982 if (mcs->defbutton != NULL) {
983 assert(mcs->defbutton->generic.type == MACCTRL_BUTTON);
984 HiliteControl(mcs->defbutton->button.tbctrl, kControlButtonPart);
986 * I'd like to delay unhilighting the button until after
987 * the event has been processed, but by them the entire
988 * dialgue box might have been destroyed.
991 HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart);
992 ctrlevent(mcs, mcs->defbutton, EVENT_ACTION);
995 case kEscapeCharCode:
996 if (mcs->canbutton != NULL) {
997 assert(mcs->canbutton->generic.type == MACCTRL_BUTTON);
998 HiliteControl(mcs->canbutton->button.tbctrl, kControlButtonPart);
1000 HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart);
1001 ctrlevent(mcs, mcs->canbutton, EVENT_ACTION);
1005 if (mac_gestalts.apprvers >= 0x100) {
1006 if (GetKeyboardFocus(window, &control) == noErr && control != NULL) {
1007 HandleControlKey(control, (event->message & keyCodeMask) >> 8,
1008 event->message & charCodeMask, event->modifiers);
1009 mc = (union macctrl *)GetControlReference(control);
1010 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1013 #if !TARGET_API_MAC_CARBON
1017 if (mcs->focus != NULL) {
1019 switch (mc->generic.type) {
1020 case MACCTRL_EDITBOX:
1021 te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1022 TEKey(event->message & charCodeMask, te);
1023 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1031 void macctrl_update(WindowPtr window)
1033 #if TARGET_API_MAC_CARBON
1039 BeginUpdate(window);
1041 SetPort((GrafPtr)GetWindowPort(window));
1042 if (mac_gestalts.apprvers >= 0x101) {
1043 #if TARGET_API_MAC_CARBON
1044 GetPortBounds(GetWindowPort(window), &rect);
1046 rect = window->portRect;
1048 InsetRect(&rect, -1, -1);
1049 DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ?
1050 kThemeStateActive : kThemeStateInactive);
1052 #if TARGET_API_MAC_CARBON
1054 GetPortVisibleRegion(GetWindowPort(window), visrgn);
1055 UpdateControls(window, visrgn);
1058 UpdateControls(window, window->visRgn);
1064 #if TARGET_API_MAC_CARBON
1065 #define EnableItem EnableMenuItem
1066 #define DisableItem DisableMenuItem
1068 void macctrl_adjustmenus(WindowPtr window)
1072 menu = GetMenuHandle(mFile);
1073 DisableItem(menu, iSave); /* XXX enable if modified */
1074 EnableItem(menu, iSaveAs);
1075 EnableItem(menu, iDuplicate);
1077 menu = GetMenuHandle(mEdit);
1078 DisableItem(menu, 0);
1081 void macctrl_close(WindowPtr window)
1083 struct macctrls *mcs = mac_winctrls(window);
1087 * Mostly, we don't bother disposing of the Toolbox controls,
1088 * since that will happen automatically when the window is
1089 * disposed of. Popup menus are an exception, because we have to
1090 * dispose of the menu ourselves, and doing that while the control
1091 * still holds a reference to it seems rude.
1093 while ((mc = index234(mcs->byctrl, 0)) != NULL) {
1094 if (mc->generic.privdata != NULL && mc->generic.freeprivdata)
1095 sfree(mc->generic.privdata);
1096 switch (mc->generic.type) {
1098 DisposeControl(mc->popup.tbctrl);
1099 DeleteMenu(mc->popup.menuid);
1100 DisposeMenu(mc->popup.menu);
1103 del234(mcs->byctrl, mc);
1107 freetree234(mcs->byctrl);
1113 void dlg_update_start(union control *ctrl, void *dlg)
1119 void dlg_update_done(union control *ctrl, void *dlg)
1125 void dlg_set_focus(union control *ctrl, void *dlg)
1128 if (mac_gestalts.apprvers >= 0x100) {
1129 /* Use SetKeyboardFocus() */
1131 /* Do our own mucking around */
1135 union control *dlg_last_focused(union control *ctrl, void *dlg)
1141 void dlg_beep(void *dlg)
1147 void dlg_error_msg(void *dlg, char *msg)
1151 c2pstrcpy(pmsg, msg);
1152 ParamText(pmsg, NULL, NULL, NULL);
1153 StopAlert(128, NULL);
1156 void dlg_end(void *dlg, int value)
1158 struct macctrls *mcs = dlg;
1160 if (mcs->end != NULL)
1161 (*mcs->end)(mcs->window, value);
1164 void dlg_refresh(union control *ctrl, void *dlg)
1166 struct macctrls *mcs = dlg;
1171 mc = findbyctrl(mcs, ctrl);
1173 ctrlevent(mcs, mc, EVENT_REFRESH);
1176 void *dlg_get_privdata(union control *ctrl, void *dlg)
1178 struct macctrls *mcs = dlg;
1179 union macctrl *mc = findbyctrl(mcs, ctrl);
1182 return mc->generic.privdata;
1185 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
1187 struct macctrls *mcs = dlg;
1188 union macctrl *mc = findbyctrl(mcs, ctrl);
1191 mc->generic.privdata = ptr;
1192 mc->generic.freeprivdata = FALSE;
1195 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
1197 struct macctrls *mcs = dlg;
1198 union macctrl *mc = findbyctrl(mcs, ctrl);
1201 mc->generic.privdata = smalloc(size);
1202 mc->generic.freeprivdata = TRUE;
1203 return mc->generic.privdata;
1208 * Radio Button control
1211 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
1213 struct macctrls *mcs = dlg;
1214 union macctrl *mc = findbyctrl(mcs, ctrl);
1218 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1219 if (i == whichbutton)
1220 SetControlValue(mc->radio.tbctrls[i],
1221 kControlRadioButtonCheckedValue);
1223 SetControlValue(mc->radio.tbctrls[i],
1224 kControlRadioButtonUncheckedValue);
1229 int dlg_radiobutton_get(union control *ctrl, void *dlg)
1231 struct macctrls *mcs = dlg;
1232 union macctrl *mc = findbyctrl(mcs, ctrl);
1236 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1237 if (GetControlValue(mc->radio.tbctrls[i]) ==
1238 kControlRadioButtonCheckedValue)
1249 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
1251 struct macctrls *mcs = dlg;
1252 union macctrl *mc = findbyctrl(mcs, ctrl);
1255 SetControlValue(mc->checkbox.tbctrl,
1256 checked ? kControlCheckBoxCheckedValue :
1257 kControlCheckBoxUncheckedValue);
1260 int dlg_checkbox_get(union control *ctrl, void *dlg)
1262 struct macctrls *mcs = dlg;
1263 union macctrl *mc = findbyctrl(mcs, ctrl);
1266 return GetControlValue(mc->checkbox.tbctrl);
1274 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
1276 struct macctrls *mcs = dlg;
1277 union macctrl *mc = findbyctrl(mcs, ctrl);
1281 assert(mc->generic.type == MACCTRL_EDITBOX);
1283 SetPort((GrafPtr)(GetWindowPort(mcs->window)));
1284 if (mac_gestalts.apprvers >= 0x100)
1285 SetControlData(mc->editbox.tbctrl, kControlEntireControl,
1286 ctrl->editbox.password ?
1287 kControlEditTextPasswordTag :
1288 kControlEditTextTextTag,
1289 strlen(text), text);
1290 #if !TARGET_API_MAC_CARBON
1292 TESetText(text, strlen(text),
1293 (TEHandle)(*mc->editbox.tbctrl)->contrlData);
1295 DrawOneControl(mc->editbox.tbctrl);
1299 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
1301 struct macctrls *mcs = dlg;
1302 union macctrl *mc = findbyctrl(mcs, ctrl);
1306 assert(mc->generic.type == MACCTRL_EDITBOX);
1307 if (mac_gestalts.apprvers >= 0x100) {
1308 if (GetControlData(mc->editbox.tbctrl, kControlEntireControl,
1309 ctrl->editbox.password ?
1310 kControlEditTextPasswordTag :
1311 kControlEditTextTextTag,
1312 length - 1, buffer, &olen) != noErr)
1314 if (olen > length - 1)
1317 #if !TARGET_API_MAC_CARBON
1319 TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1321 olen = (*te)->teLength;
1322 if (olen > length - 1)
1324 memcpy(buffer, *(*te)->hText, olen);
1327 buffer[olen] = '\0';
1328 fprintf(stderr, "dlg_editbox_get: %s\n", buffer);
1336 static void dlg_macpopup_clear(union control *ctrl, void *dlg)
1338 struct macctrls *mcs = dlg;
1339 union macctrl *mc = findbyctrl(mcs, ctrl);
1340 MenuRef menu = mc->popup.menu;
1343 fprintf(stderr, " popup_clear\n");
1344 n = CountMenuItems(menu);
1345 for (i = 0; i < n; i++)
1346 DeleteMenuItem(menu, n - i);
1348 sfree(mc->popup.ids);
1349 mc->popup.ids = NULL;
1350 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1353 void dlg_listbox_clear(union control *ctrl, void *dlg)
1356 if (ctrl->listbox.height == 0)
1357 dlg_macpopup_clear(ctrl, dlg);
1360 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
1362 struct macctrls *mcs = dlg;
1363 union macctrl *mc = findbyctrl(mcs, ctrl);
1364 MenuRef menu = mc->popup.menu;
1366 fprintf(stderr, " popup_del %d\n", index);
1367 DeleteMenuItem(menu, index + 1);
1368 if (mc->popup.ids != NULL)
1369 memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
1370 (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
1371 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1374 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
1377 if (ctrl->listbox.height == 0)
1378 dlg_macpopup_del(ctrl, dlg, index);
1381 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
1383 struct macctrls *mcs = dlg;
1384 union macctrl *mc = findbyctrl(mcs, ctrl);
1385 MenuRef menu = mc->popup.menu;
1388 fprintf(stderr, " popup_add %s\n", text);
1389 assert(text[0] != '\0');
1390 c2pstrcpy(itemstring, text);
1391 AppendMenu(menu, "\pdummy");
1392 SetMenuItemText(menu, CountMenuItems(menu), itemstring);
1393 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1396 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
1399 if (ctrl->listbox.height == 0)
1400 dlg_macpopup_add(ctrl, dlg, text);
1403 static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
1404 char const *text, int id)
1406 struct macctrls *mcs = dlg;
1407 union macctrl *mc = findbyctrl(mcs, ctrl);
1408 MenuRef menu = mc->popup.menu;
1411 fprintf(stderr, " popup_addwthindex %s, %d\n", text, id);
1412 dlg_macpopup_add(ctrl, dlg, text);
1413 index = CountMenuItems(menu) - 1;
1414 if (mc->popup.nids <= index) {
1415 mc->popup.nids = index + 1;
1416 mc->popup.ids = sresize(mc->popup.ids, mc->popup.nids, int);
1418 mc->popup.ids[index] = id;
1421 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
1422 char const *text, int id)
1425 if (ctrl->listbox.height == 0)
1426 dlg_macpopup_addwithid(ctrl, dlg, text, id);
1429 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
1431 struct macctrls *mcs = dlg;
1432 union macctrl *mc = findbyctrl(mcs, ctrl);
1434 if (ctrl->listbox.height == 0) {
1435 assert(mc->popup.ids != NULL && mc->popup.nids > index);
1436 return mc->popup.ids[index];
1441 int dlg_listbox_index(union control *ctrl, void *dlg)
1443 struct macctrls *mcs = dlg;
1444 union macctrl *mc = findbyctrl(mcs, ctrl);
1446 if (ctrl->listbox.height == 0)
1447 return GetControlValue(mc->popup.tbctrl) - 1;
1451 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1453 struct macctrls *mcs = dlg;
1454 union macctrl *mc = findbyctrl(mcs, ctrl);
1456 if (ctrl->listbox.height == 0)
1457 return GetControlValue(mc->popup.tbctrl) - 1 == index;
1461 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1463 struct macctrls *mcs = dlg;
1464 union macctrl *mc = findbyctrl(mcs, ctrl);
1466 if (ctrl->listbox.height == 0)
1467 SetControlValue(mc->popup.tbctrl, index + 1);
1475 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1477 struct macctrls *mcs = dlg;
1478 union macctrl *mc = findbyctrl(mcs, ctrl);
1481 if (mac_gestalts.apprvers >= 0x100)
1482 SetControlData(mc->text.tbctrl, kControlEntireControl,
1483 kControlStaticTextTextTag, strlen(text), text);
1484 #if !TARGET_API_MAC_CARBON
1486 TESetText(text, strlen(text),
1487 (TEHandle)(*mc->text.tbctrl)->contrlData);
1493 * File Selector control
1496 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1501 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1508 * Font Selector control
1511 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1516 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1523 * Printer enumeration
1526 printer_enum *printer_start_enum(int *nprinters)
1533 char *printer_get_name(printer_enum *pe, int thing)
1539 void printer_finish_enum(printer_enum *pe)
1546 * Colour selection stuff
1549 void dlg_coloursel_start(union control *ctrl, void *dlg,
1550 int r, int g, int b)
1555 int dlg_coloursel_results(union control *ctrl, void *dlg,
1556 int *r, int *g, int *b)
1564 * c-file-style: "simon"