1 /* $Id: macctrls.c,v 1.41 2003/05/10 20:23:23 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>
30 #include <ColorPicker.h>
32 #include <ControlDefinitions.h>
36 #include <Resources.h>
40 #include <TextUtils.h>
41 #include <ToolUtils.h>
53 /* Range of menu IDs for popup menus */
59 struct macctrl_generic {
69 /* Template from which this was generated */
71 /* Next control in this panel */
77 struct macctrl_generic generic;
81 struct macctrl_generic generic;
86 struct macctrl_generic generic;
91 struct macctrl_generic generic;
95 struct macctrl_generic generic;
100 struct macctrl_generic generic;
107 struct macctrl_generic generic;
116 struct mac_layoutstate {
119 unsigned int panelnum;
122 #define ctrlevent(mcs, mc, event) do { \
123 if ((mc)->generic.ctrl->generic.handler != NULL) \
124 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
125 (mcs)->data, (event)); \
128 #define findbyctrl(mcs, ctrl) \
129 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
131 static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *,
132 WindowPtr, struct macctrls *);
133 static void macctrl_hideshowpanel(struct macctrls *, unsigned int, int);
134 static void macctrl_switchtopanel(struct macctrls *, unsigned int);
135 static void macctrl_setfocus(struct macctrls *, union macctrl *);
136 static void macctrl_text(struct macctrls *, WindowPtr,
137 struct mac_layoutstate *, union control *);
138 static void macctrl_editbox(struct macctrls *, WindowPtr,
139 struct mac_layoutstate *, union control *);
140 static void macctrl_radio(struct macctrls *, WindowPtr,
141 struct mac_layoutstate *, union control *);
142 static void macctrl_checkbox(struct macctrls *, WindowPtr,
143 struct mac_layoutstate *, union control *);
144 static void macctrl_button(struct macctrls *, WindowPtr,
145 struct mac_layoutstate *, union control *);
146 static void macctrl_listbox(struct macctrls *, WindowPtr,
147 struct mac_layoutstate *, union control *);
148 static void macctrl_popup(struct macctrls *, WindowPtr,
149 struct mac_layoutstate *, union control *);
150 #if !TARGET_API_MAC_CARBON
151 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef,
152 ControlDefProcMessage, SInt32);
153 static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
154 ControlDefProcMessage, SInt32);
155 static pascal SInt32 macctrl_sys7_listbox_cdef(SInt16, ControlRef,
156 ControlDefProcMessage, SInt32);
159 #if !TARGET_API_MAC_CARBON
161 * This trick enables us to keep all the CDEF code in the main
162 * application, which makes life easier. For details, see
163 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
166 #pragma options align=mac68k
168 short jmpabs; /* 4EF9 */
169 ControlDefUPP theUPP;
171 #pragma options align=reset
174 static void macctrl_init()
176 #if !TARGET_API_MAC_CARBON
177 static int inited = 0;
181 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox);
182 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef);
183 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
184 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
185 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_ListBox);
186 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_listbox_cdef);
192 static int macctrl_cmp_byctrl(void *av, void *bv)
194 union macctrl *a = (union macctrl *)av;
195 union macctrl *b = (union macctrl *)bv;
197 if (a->generic.ctrl < b->generic.ctrl)
199 else if (a->generic.ctrl > b->generic.ctrl)
205 static int macctrl_cmp_byctrl_find(void *av, void *bv)
207 union control *a = (union control *)av;
208 union macctrl *b = (union macctrl *)bv;
210 if (a < b->generic.ctrl)
212 else if (a > b->generic.ctrl)
218 static union control panellist;
220 static void panellist_handler(union control *ctrl, void *dlg, void *data,
223 struct macctrls *mcs = dlg;
225 /* XXX what if there's no selection? */
226 if (event == EVENT_SELCHANGE)
227 macctrl_switchtopanel(mcs, dlg_listbox_index(ctrl, dlg) + 1);
230 void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
231 struct macctrls *mcs)
234 struct mac_layoutstate curstate;
239 if (mac_gestalts.apprvers >= 0x100)
240 CreateRootControl(window, &root);
241 #if TARGET_API_MAC_CARBON
242 GetPortBounds(GetWindowPort(window), &rect);
244 rect = window->portRect;
246 mcs->window = window;
247 mcs->byctrl = newtree234(macctrl_cmp_byctrl);
249 mcs->defbutton = NULL;
250 mcs->canbutton = NULL;
251 /* Count the number of panels */
253 for (i = 1; i < cb->nctrlsets; i++)
254 if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
256 mcs->panels = snewn(mcs->npanels, union macctrl *);
257 memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
258 curstate.panelnum = 0;
260 curstate.pos.h = rect.left + 13;
261 curstate.pos.v = rect.top + 13;
262 curstate.width = 160;
263 panellist.listbox.type = CTRL_LISTBOX;
264 panellist.listbox.handler = &panellist_handler;
265 panellist.listbox.height = 20;
266 panellist.listbox.percentwidth = 100;
267 macctrl_listbox(mcs, window, &curstate, &panellist);
268 /* XXX Start with panel 1 active */
270 curstate.pos.h = rect.left + 13 + 160 + 13;
271 curstate.pos.v = rect.bottom - 33;
272 curstate.width = rect.right - (rect.left + 13 + 160) - (13 * 2);
273 for (i = 0; i < cb->nctrlsets; i++) {
274 if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
275 cb->ctrlsets[i-1]->pathname)) {
276 curstate.pos.v = rect.top + 13;
278 assert(curstate.panelnum < mcs->npanels);
279 dlg_listbox_add(&panellist, mcs, cb->ctrlsets[i]->pathname);
281 macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
283 macctrl_switchtopanel(mcs, 1);
284 macctrl_hideshowpanel(mcs, 0, TRUE);
285 /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
290 static void macctrl_layoutset(struct mac_layoutstate *curstate,
291 struct controlset *s,
292 WindowPtr window, struct macctrls *mcs)
294 unsigned int i, j, ncols, colstart;
295 struct mac_layoutstate cols[MAXCOLS];
300 for (i = 0; i < s->ncontrols; i++) {
301 union control *ctrl = s->ctrls[i];
303 colstart = COLUMN_START(ctrl->generic.column);
304 switch (ctrl->generic.type) {
306 if (ctrl->columns.ncols != 1) {
307 ncols = ctrl->columns.ncols;
308 assert(ncols <= MAXCOLS);
309 for (j = 0; j < ncols; j++) {
312 cols[j].pos.h = cols[j-1].pos.h + cols[j-1].width + 6;
314 cols[j].width = curstate->width -
315 (cols[j].pos.h - curstate->pos.h);
317 cols[j].width = (curstate->width + 6) *
318 ctrl->columns.percentages[j] / 100 - 6;
321 for (j = 0; j < ncols; j++)
322 if (cols[j].pos.v > cols[0].pos.v)
323 cols[0].pos.v = cols[j].pos.v;
324 cols[0].width = curstate->width;
329 macctrl_text(mcs, window, &cols[colstart], ctrl);
332 macctrl_editbox(mcs, window, &cols[colstart], ctrl);
335 macctrl_radio(mcs, window, &cols[colstart], ctrl);
338 macctrl_checkbox(mcs, window, &cols[colstart], ctrl);
341 macctrl_button(mcs, window, &cols[colstart], ctrl);
344 if (ctrl->listbox.height == 0)
345 macctrl_popup(mcs, window, &cols[colstart], ctrl);
347 macctrl_listbox(mcs, window, &cols[colstart], ctrl);
351 for (j = 0; j < ncols; j++)
352 if (cols[j].pos.v > curstate->pos.v)
353 curstate->pos.v = cols[j].pos.v;
356 static void macctrl_hideshowpanel(struct macctrls *mcs, unsigned int panel,
362 #define hideshow(c) do { \
363 if (showit) ShowControl(c); else HideControl(c); \
366 for (mc = mcs->panels[panel]; mc != NULL; mc = mc->generic.next) {
367 #if !TARGET_API_MAC_CARBON
368 if (mcs->focus == mc)
369 macctrl_setfocus(mcs, NULL);
371 switch (mc->generic.type) {
373 hideshow(mc->text.tbctrl);
375 case MACCTRL_EDITBOX:
376 hideshow(mc->editbox.tbctrl);
377 if (mc->editbox.tblabel != NULL)
378 hideshow(mc->editbox.tblabel);
381 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
382 hideshow(mc->radio.tbctrls[j]);
383 if (mc->radio.tblabel != NULL)
384 hideshow(mc->radio.tblabel);
386 case MACCTRL_CHECKBOX:
387 hideshow(mc->checkbox.tbctrl);
390 hideshow(mc->button.tbctrl);
391 if (mc->button.tbring != NULL)
392 hideshow(mc->button.tbring);
394 case MACCTRL_LISTBOX:
395 hideshow(mc->listbox.tbctrl);
397 * At least under Mac OS 8.1, hiding a list box
398 * doesn't hide its scroll bars.
400 #if TARGET_API_MAC_CARBON
401 hideshow(GetListVerticalScrollBar(mc->listbox.list));
403 hideshow((*mc->listbox.list)->vScroll);
407 hideshow(mc->popup.tbctrl);
413 static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
416 macctrl_hideshowpanel(mcs, mcs->curpanel, FALSE);
417 macctrl_hideshowpanel(mcs, which, TRUE);
418 mcs->curpanel = which;
421 #if !TARGET_API_MAC_CARBON
423 * System 7 focus manipulation
425 static void macctrl_defocus(union macctrl *mc)
428 assert(mac_gestalts.apprvers < 0x100);
429 switch (mc->generic.type) {
430 case MACCTRL_EDITBOX:
431 TEDeactivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
436 static void macctrl_enfocus(union macctrl *mc)
439 assert(mac_gestalts.apprvers < 0x100);
440 switch (mc->generic.type) {
441 case MACCTRL_EDITBOX:
442 TEActivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
447 static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc)
450 if (mcs->focus == mc)
452 if (mcs->focus != NULL)
453 macctrl_defocus(mcs->focus);
460 static void macctrl_text(struct macctrls *mcs, WindowPtr window,
461 struct mac_layoutstate *curstate,
464 union macctrl *mc = snew(union macctrl);
468 assert(ctrl->text.label != NULL);
469 mc->generic.type = MACCTRL_TEXT;
470 mc->generic.ctrl = ctrl;
471 mc->generic.privdata = NULL;
472 bounds.left = curstate->pos.h;
473 bounds.right = bounds.left + curstate->width;
474 bounds.top = curstate->pos.v;
475 bounds.bottom = bounds.top + 16;
476 if (mac_gestalts.apprvers >= 0x100) {
479 mc->text.tbctrl = NewControl(window, &bounds, NULL, FALSE, 0, 0, 0,
480 kControlStaticTextProc, (long)mc);
481 SetControlData(mc->text.tbctrl, kControlEntireControl,
482 kControlStaticTextTextTag,
483 strlen(ctrl->text.label), ctrl->text.label);
484 GetControlData(mc->text.tbctrl, kControlEntireControl,
485 kControlStaticTextTextHeightTag,
486 sizeof(height), &height, &olen);
488 #if !TARGET_API_MAC_CARBON
492 mc->text.tbctrl = NewControl(window, &bounds, NULL, FALSE, 0, 0, 0,
493 SYS7_TEXT_PROC, (long)mc);
494 te = (TEHandle)(*mc->text.tbctrl)->contrlData;
495 TESetText(ctrl->text.label, strlen(ctrl->text.label), te);
496 height = TEGetHeight(1, (*te)->nLines, te);
499 SizeControl(mc->text.tbctrl, curstate->width, height);
500 curstate->pos.v += height + 6;
501 add234(mcs->byctrl, mc);
502 mc->generic.next = mcs->panels[curstate->panelnum];
503 mcs->panels[curstate->panelnum] = mc;
506 static void macctrl_editbox(struct macctrls *mcs, WindowPtr window,
507 struct mac_layoutstate *curstate,
510 union macctrl *mc = snew(union macctrl);
511 Rect lbounds, bounds;
513 mc->generic.type = MACCTRL_EDITBOX;
514 mc->generic.ctrl = ctrl;
515 mc->generic.privdata = NULL;
516 lbounds.left = curstate->pos.h;
517 lbounds.top = curstate->pos.v;
518 if (ctrl->editbox.percentwidth == 100) {
519 if (ctrl->editbox.label != NULL) {
520 lbounds.right = lbounds.left + curstate->width;
521 lbounds.bottom = lbounds.top + 16;
522 curstate->pos.v += 18;
524 bounds.left = curstate->pos.h;
525 bounds.right = bounds.left + curstate->width;
527 lbounds.right = lbounds.left +
528 curstate->width * (100 - ctrl->editbox.percentwidth) / 100;
529 lbounds.bottom = lbounds.top + 22;
530 bounds.left = lbounds.right;
531 bounds.right = lbounds.left + curstate->width;
533 bounds.top = curstate->pos.v;
534 bounds.bottom = bounds.top + 22;
535 if (mac_gestalts.apprvers >= 0x100) {
536 if (ctrl->editbox.label == NULL)
537 mc->editbox.tblabel = NULL;
539 mc->editbox.tblabel = NewControl(window, &lbounds, NULL, FALSE,
540 0, 0, 0, kControlStaticTextProc,
542 SetControlData(mc->editbox.tblabel, kControlEntireControl,
543 kControlStaticTextTextTag,
544 strlen(ctrl->editbox.label), ctrl->editbox.label);
546 InsetRect(&bounds, 3, 3);
547 mc->editbox.tbctrl = NewControl(window, &bounds, NULL, FALSE, 0, 0, 0,
548 ctrl->editbox.password ?
549 kControlEditTextPasswordProc :
550 kControlEditTextProc, (long)mc);
552 #if !TARGET_API_MAC_CARBON
554 if (ctrl->editbox.label == NULL)
555 mc->editbox.tblabel = NULL;
557 mc->editbox.tblabel = NewControl(window, &lbounds, NULL, FALSE,
558 0, 0, 0, SYS7_TEXT_PROC,
560 TESetText(ctrl->editbox.label, strlen(ctrl->editbox.label),
561 (TEHandle)(*mc->editbox.tblabel)->contrlData);
563 mc->editbox.tbctrl = NewControl(window, &bounds, NULL, FALSE, 0, 0, 0,
564 SYS7_EDITBOX_PROC, (long)mc);
567 curstate->pos.v += 28;
568 add234(mcs->byctrl, mc);
569 mc->generic.next = mcs->panels[curstate->panelnum];
570 mcs->panels[curstate->panelnum] = mc;
571 ctrlevent(mcs, mc, EVENT_REFRESH);
574 #if !TARGET_API_MAC_CARBON
575 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant,
577 ControlDefProcMessage msg,
588 rect = (*control)->contrlRect;
589 if (variant == SYS7_EDITBOX_VARIANT)
590 InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */
591 te = TENew(&rect, &rect);
592 ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
593 (*te)->txSize = LoWord(ssfs);
594 (*te)->txFont = HiWord(ssfs);
595 (*control)->contrlData = (Handle)te;
598 TEDispose((TEHandle)(*control)->contrlData);
601 if ((*control)->contrlVis) {
602 rect = (*control)->contrlRect;
603 if (variant == SYS7_EDITBOX_VARIANT) {
606 InsetRect(&rect, 3, 3);
609 (*(TEHandle)(*control)->contrlData)->viewRect = rect;
610 TEUpdate(&rect, (TEHandle)(*control)->contrlData);
614 if (variant == SYS7_TEXT_VARIANT)
615 return kControlNoPart;
616 mouse.h = LoWord(param);
617 mouse.v = HiWord(param);
618 rect = (*control)->contrlRect;
619 InsetRect(&rect, 3, 3);
620 return PtInRect(mouse, &rect) ? kControlEditTextPart : kControlNoPart;
622 if (param & (1 << 31)) {
628 rgn = (RgnHandle)param;
629 RectRgn(rgn, &(*control)->contrlRect);
633 rgn = (RgnHandle)param;
642 static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
643 struct mac_layoutstate *curstate,
646 union macctrl *mc = snew(union macctrl);
649 unsigned int i, colwidth;
651 mc->generic.type = MACCTRL_RADIO;
652 mc->generic.ctrl = ctrl;
653 mc->generic.privdata = NULL;
654 mc->radio.tbctrls = snewn(ctrl->radio.nbuttons, ControlRef);
655 colwidth = (curstate->width + 13) / ctrl->radio.ncolumns;
656 bounds.top = curstate->pos.v;
657 bounds.bottom = bounds.top + 16;
658 bounds.left = curstate->pos.h;
659 bounds.right = bounds.left + curstate->width;
660 if (ctrl->radio.label == NULL)
661 mc->radio.tblabel = NULL;
663 if (mac_gestalts.apprvers >= 0x100) {
664 mc->radio.tblabel = NewControl(window, &bounds, NULL, FALSE,
665 0, 0, 0, kControlStaticTextProc,
667 SetControlData(mc->radio.tblabel, kControlEntireControl,
668 kControlStaticTextTextTag,
669 strlen(ctrl->radio.label), ctrl->radio.label);
671 #if !TARGET_API_MAC_CARBON
673 mc->radio.tblabel = NewControl(window, &bounds, NULL, FALSE,
674 0, 0, 0, SYS7_TEXT_PROC, (long)mc);
675 TESetText(ctrl->radio.label, strlen(ctrl->radio.label),
676 (TEHandle)(*mc->radio.tblabel)->contrlData);
679 curstate->pos.v += 18;
681 for (i = 0; i < ctrl->radio.nbuttons; i++) {
682 bounds.top = curstate->pos.v - 2;
683 bounds.bottom = bounds.top + 18;
684 bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
685 if (i == ctrl->radio.nbuttons - 1 ||
686 i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
687 bounds.right = curstate->pos.h + curstate->width;
688 curstate->pos.v += 18;
690 bounds.right = bounds.left + colwidth - 13;
691 c2pstrcpy(title, ctrl->radio.buttons[i]);
692 mc->radio.tbctrls[i] = NewControl(window, &bounds, title, FALSE,
693 0, 0, 1, radioButProc, (long)mc);
695 curstate->pos.v += 4;
696 add234(mcs->byctrl, mc);
697 mc->generic.next = mcs->panels[curstate->panelnum];
698 mcs->panels[curstate->panelnum] = mc;
699 ctrlevent(mcs, mc, EVENT_REFRESH);
702 static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
703 struct mac_layoutstate *curstate,
706 union macctrl *mc = snew(union macctrl);
710 assert(ctrl->checkbox.label != NULL);
711 mc->generic.type = MACCTRL_CHECKBOX;
712 mc->generic.ctrl = ctrl;
713 mc->generic.privdata = NULL;
714 bounds.left = curstate->pos.h;
715 bounds.right = bounds.left + curstate->width;
716 bounds.top = curstate->pos.v;
717 bounds.bottom = bounds.top + 16;
718 c2pstrcpy(title, ctrl->checkbox.label);
719 mc->checkbox.tbctrl = NewControl(window, &bounds, title, FALSE, 0, 0, 1,
720 checkBoxProc, (long)mc);
721 add234(mcs->byctrl, mc);
722 curstate->pos.v += 22;
723 mc->generic.next = mcs->panels[curstate->panelnum];
724 mcs->panels[curstate->panelnum] = mc;
725 ctrlevent(mcs, mc, EVENT_REFRESH);
728 static void macctrl_button(struct macctrls *mcs, WindowPtr window,
729 struct mac_layoutstate *curstate,
732 union macctrl *mc = snew(union macctrl);
736 assert(ctrl->button.label != NULL);
737 mc->generic.type = MACCTRL_BUTTON;
738 mc->generic.ctrl = ctrl;
739 mc->generic.privdata = NULL;
740 bounds.left = curstate->pos.h;
741 bounds.right = bounds.left + curstate->width;
742 bounds.top = curstate->pos.v;
743 bounds.bottom = bounds.top + 20;
744 c2pstrcpy(title, ctrl->button.label);
745 mc->button.tbctrl = NewControl(window, &bounds, title, FALSE, 0, 0, 1,
746 pushButProc, (long)mc);
747 mc->button.tbring = NULL;
748 if (mac_gestalts.apprvers >= 0x100) {
749 Boolean isdefault = ctrl->button.isdefault;
751 SetControlData(mc->button.tbctrl, kControlEntireControl,
752 kControlPushButtonDefaultTag,
753 sizeof(isdefault), &isdefault);
754 } else if (ctrl->button.isdefault) {
755 InsetRect(&bounds, -4, -4);
756 mc->button.tbring = NewControl(window, &bounds, title, FALSE, 0, 0, 1,
757 SYS7_DEFAULT_PROC, (long)mc);
759 if (mac_gestalts.apprvers >= 0x110) {
760 Boolean iscancel = ctrl->button.iscancel;
762 SetControlData(mc->button.tbctrl, kControlEntireControl,
763 kControlPushButtonCancelTag,
764 sizeof(iscancel), &iscancel);
766 if (ctrl->button.isdefault)
768 if (ctrl->button.iscancel)
770 add234(mcs->byctrl, mc);
771 mc->generic.next = mcs->panels[curstate->panelnum];
772 mcs->panels[curstate->panelnum] = mc;
773 curstate->pos.v += 26;
776 #if !TARGET_API_MAC_CARBON
777 static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
779 ControlDefProcMessage msg,
789 if ((*control)->contrlVis) {
790 rect = (*control)->contrlRect;
791 GetPenState(&savestate);
794 if ((*control)->contrlHilite == kControlInactivePart)
796 oval = (rect.bottom - rect.top) / 2 + 2;
797 FrameRoundRect(&rect, oval, oval);
798 SetPenState(&savestate);
802 if (param & (1 << 31)) {
808 rgn = (RgnHandle)param;
809 RectRgn(rgn, &(*control)->contrlRect);
813 rgn = (RgnHandle)param;
822 static void macctrl_listbox(struct macctrls *mcs, WindowPtr window,
823 struct mac_layoutstate *curstate,
826 union macctrl *mc = snew(union macctrl);
831 assert(ctrl->listbox.percentwidth == 100);
832 mc->generic.type = MACCTRL_LISTBOX;
833 mc->generic.ctrl = ctrl;
834 mc->generic.privdata = NULL;
835 /* The list starts off empty */
836 mc->listbox.nids = 0;
837 mc->listbox.ids = NULL;
838 bounds.left = curstate->pos.h;
839 bounds.right = bounds.left + curstate->width;
840 bounds.top = curstate->pos.v;
841 bounds.bottom = bounds.top + 16 * ctrl->listbox.height + 2;
843 if (mac_gestalts.apprvers >= 0x100) {
844 InsetRect(&bounds, 3, 3);
845 mc->listbox.tbctrl = NewControl(window, &bounds, NULL, FALSE,
847 kControlListBoxProc, (long)mc);
848 if (GetControlData(mc->listbox.tbctrl, kControlEntireControl,
849 kControlListBoxListHandleTag,
850 sizeof(mc->listbox.list), &mc->listbox.list,
852 DisposeControl(mc->listbox.tbctrl);
857 #if !TARGET_API_MAC_CARBON
859 InsetRect(&bounds, -3, -3);
860 mc->listbox.tbctrl = NewControl(window, &bounds, NULL, FALSE,
862 SYS7_LISTBOX_PROC, (long)mc);
863 mc->listbox.list = (ListHandle)(*mc->listbox.tbctrl)->contrlData;
864 (*mc->listbox.list)->refCon = (long)mc;
867 if (!ctrl->listbox.multisel) {
868 #if TARGET_API_MAC_CARBON
869 SetListSelectionFlags(mc->listbox.list, lOnlyOne);
871 (*mc->listbox.list)->selFlags = lOnlyOne;
874 add234(mcs->byctrl, mc);
875 curstate->pos.v += 6 + 16 * ctrl->listbox.height + 2;
876 mc->generic.next = mcs->panels[curstate->panelnum];
877 mcs->panels[curstate->panelnum] = mc;
878 ctrlevent(mcs, mc, EVENT_REFRESH);
879 #if TARGET_API_MAC_CARBON
880 HideControl(GetListVerticalScrollBar(mc->listbox.list));
882 HideControl((*mc->listbox.list)->vScroll);
886 #if !TARGET_API_MAC_CARBON
887 static pascal SInt32 macctrl_sys7_listbox_cdef(SInt16 variant,
889 ControlDefProcMessage msg,
905 rect = (*control)->contrlRect;
906 InsetRect(&rect, 4, 4);
907 rect.right -= 15; /* scroll bar */
908 bounds.top = bounds.bottom = bounds.left = 0;
910 csize.h = csize.v = 0;
912 savefont = curport->txFont;
913 savesize = curport->txSize;
914 ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
915 TextFont(HiWord(ssfs));
916 TextSize(LoWord(ssfs));
917 list = LNew(&rect, &bounds, csize, 0, (*control)->contrlOwner,
918 TRUE, FALSE, FALSE, TRUE);
919 SetControlReference((*list)->vScroll, (long)list);
920 (*control)->contrlData = (Handle)list;
926 * If the dialogue box is being destroyed, the scroll bar
927 * might have gone already. In our situation, this is the
928 * only time we destroy a control, so NULL out the scroll bar
929 * handle to prevent LDispose trying to free it.
931 list = (ListHandle)(*control)->contrlData;
932 (*list)->vScroll = NULL;
936 if ((*control)->contrlVis) {
937 rect = (*control)->contrlRect;
938 /* XXX input focus highlighting? */
939 InsetRect(&rect, 3, 3);
942 list = (ListHandle)(*control)->contrlData;
943 LActivate((*control)->contrlHilite != kControlInactivePart, list);
945 LUpdate(curport->visRgn, list);
949 mouse.h = LoWord(param);
950 mouse.v = HiWord(param);
951 rect = (*control)->contrlRect;
952 InsetRect(&rect, 4, 4);
954 * We deliberately exclude the scrollbar so that LClick() can see it.
957 return PtInRect(mouse, &rect) ? kControlListBoxPart : kControlNoPart;
959 if (param & (1 << 31)) {
965 rgn = (RgnHandle)param;
966 RectRgn(rgn, &(*control)->contrlRect);
970 rgn = (RgnHandle)param;
979 static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
980 struct mac_layoutstate *curstate,
983 union macctrl *mc = snew(union macctrl);
986 unsigned int labelwidth;
987 static int nextmenuid = MENU_MIN;
992 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
993 * create a popup menu with dynamic content.
995 assert(ctrl->listbox.height == 0);
996 assert(!ctrl->listbox.draglist);
997 assert(!ctrl->listbox.multisel);
999 mc->generic.type = MACCTRL_POPUP;
1000 mc->generic.ctrl = ctrl;
1001 mc->generic.privdata = NULL;
1002 c2pstrcpy(title, ctrl->button.label == NULL ? "" : ctrl->button.label);
1004 /* Find a spare menu ID and create the menu */
1005 while (GetMenuHandle(nextmenuid) != NULL)
1006 if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
1007 menuid = nextmenuid++;
1008 menu = NewMenu(menuid, "\pdummy");
1009 if (menu == NULL) return;
1010 mc->popup.menu = menu;
1011 mc->popup.menuid = menuid;
1012 InsertMenu(menu, kInsertHierarchicalMenu);
1014 /* The menu starts off empty */
1016 mc->popup.ids = NULL;
1018 bounds.left = curstate->pos.h;
1019 bounds.right = bounds.left + curstate->width;
1020 bounds.top = curstate->pos.v;
1021 bounds.bottom = bounds.top + 20;
1022 /* XXX handle percentwidth == 100 */
1023 labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
1024 mc->popup.tbctrl = NewControl(window, &bounds, title, FALSE,
1025 popupTitleLeftJust, menuid, labelwidth,
1026 popupMenuProc + popupFixedWidth, (long)mc);
1027 add234(mcs->byctrl, mc);
1028 curstate->pos.v += 26;
1029 mc->generic.next = mcs->panels[curstate->panelnum];
1030 mcs->panels[curstate->panelnum] = mc;
1031 ctrlevent(mcs, mc, EVENT_REFRESH);
1035 void macctrl_activate(WindowPtr window, EventRecord *event)
1037 struct macctrls *mcs = mac_winctrls(window);
1038 Boolean active = (event->modifiers & activeFlag) != 0;
1041 ControlPartCode state;
1045 SetPort((GrafPtr)GetWindowPort(window));
1046 if (mac_gestalts.apprvers >= 0x100)
1047 SetThemeWindowBackground(window, active ?
1048 kThemeBrushModelessDialogBackgroundActive :
1049 kThemeBrushModelessDialogBackgroundInactive,
1051 state = active ? kControlNoPart : kControlInactivePart;
1052 for (i = 0; i <= mcs->curpanel; i += mcs->curpanel)
1053 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
1054 switch (mc->generic.type) {
1056 HiliteControl(mc->text.tbctrl, state);
1058 case MACCTRL_EDITBOX:
1059 HiliteControl(mc->editbox.tbctrl, state);
1060 if (mc->editbox.tblabel != NULL)
1061 HiliteControl(mc->editbox.tblabel, state);
1064 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
1065 HiliteControl(mc->radio.tbctrls[j], state);
1066 if (mc->radio.tblabel != NULL)
1067 HiliteControl(mc->radio.tblabel, state);
1069 case MACCTRL_CHECKBOX:
1070 HiliteControl(mc->checkbox.tbctrl, state);
1072 case MACCTRL_BUTTON:
1073 HiliteControl(mc->button.tbctrl, state);
1074 if (mc->button.tbring != NULL)
1075 HiliteControl(mc->button.tbring, state);
1077 case MACCTRL_LISTBOX:
1078 HiliteControl(mc->listbox.tbctrl, state);
1081 HiliteControl(mc->popup.tbctrl, state);
1084 #if !TARGET_API_MAC_CARBON
1085 if (mcs->focus == mc) {
1087 macctrl_enfocus(mc);
1089 macctrl_defocus(mc);
1096 void macctrl_click(WindowPtr window, EventRecord *event)
1099 ControlHandle control, oldfocus;
1100 int part, trackresult;
1103 struct macctrls *mcs = mac_winctrls(window);
1108 SetPort((GrafPtr)GetWindowPort(window));
1109 mouse = event->where;
1110 GlobalToLocal(&mouse);
1111 part = FindControl(mouse, window, &control);
1112 if (control != NULL) {
1113 #if !TARGET_API_MAC_CARBON
1115 * Special magic for scroll bars in list boxes, whose refcon
1118 if (part == kControlUpButtonPart || part == kControlDownButtonPart ||
1119 part == kControlPageUpPart || part == kControlPageDownPart ||
1120 part == kControlIndicatorPart)
1121 mc = (union macctrl *)
1122 (*(ListHandle)GetControlReference(control))->refCon;
1125 mc = (union macctrl *)GetControlReference(control);
1126 if (mac_gestalts.apprvers >= 0x100) {
1127 if (GetControlFeatures(control, &features) == noErr &&
1128 (features & kControlSupportsFocus) &&
1129 (features & kControlGetsFocusOnClick) &&
1130 GetKeyboardFocus(window, &oldfocus) == noErr &&
1131 control != oldfocus)
1132 SetKeyboardFocus(window, control, part);
1133 trackresult = HandleControlClick(control, mouse, event->modifiers,
1134 (ControlActionUPP)-1);
1136 #if !TARGET_API_MAC_CARBON
1137 if (mc->generic.type == MACCTRL_EDITBOX &&
1138 control == mc->editbox.tbctrl) {
1139 TEHandle te = (TEHandle)(*control)->contrlData;
1141 macctrl_setfocus(mcs, mc);
1142 TEClick(mouse, !!(event->modifiers & shiftKey), te);
1145 if (mc->generic.type == MACCTRL_LISTBOX &&
1146 (control == mc->listbox.tbctrl ||
1147 control == (*mc->listbox.list)->vScroll)) {
1149 macctrl_setfocus(mcs, mc);
1150 if (LClick(mouse, event->modifiers, mc->listbox.list))
1152 ctrlevent(mcs, mc, EVENT_ACTION);
1154 ctrlevent(mcs, mc, EVENT_SELCHANGE);
1158 trackresult = TrackControl(control, mouse, (ControlActionUPP)-1);
1160 switch (mc->generic.type) {
1162 if (trackresult != 0) {
1163 for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
1164 if (mc->radio.tbctrls[i] == control)
1165 SetControlValue(mc->radio.tbctrls[i],
1166 kControlRadioButtonCheckedValue);
1168 SetControlValue(mc->radio.tbctrls[i],
1169 kControlRadioButtonUncheckedValue);
1170 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1173 case MACCTRL_CHECKBOX:
1174 if (trackresult != 0) {
1175 SetControlValue(control, !GetControlValue(control));
1176 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1179 case MACCTRL_BUTTON:
1180 if (trackresult != 0)
1181 ctrlevent(mcs, mc, EVENT_ACTION);
1183 case MACCTRL_LISTBOX:
1184 /* FIXME spot double-click */
1185 ctrlevent(mcs, mc, EVENT_SELCHANGE);
1188 ctrlevent(mcs, mc, EVENT_SELCHANGE);
1196 void macctrl_key(WindowPtr window, EventRecord *event)
1199 struct macctrls *mcs = mac_winctrls(window);
1201 unsigned long dummy;
1203 switch (event->message & charCodeMask) {
1204 case kEnterCharCode:
1205 case kReturnCharCode:
1206 if (mcs->defbutton != NULL) {
1207 assert(mcs->defbutton->generic.type == MACCTRL_BUTTON);
1208 HiliteControl(mcs->defbutton->button.tbctrl, kControlButtonPart);
1210 * I'd like to delay unhilighting the button until after
1211 * the event has been processed, but by them the entire
1212 * dialgue box might have been destroyed.
1215 HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart);
1216 ctrlevent(mcs, mcs->defbutton, EVENT_ACTION);
1219 case kEscapeCharCode:
1220 if (mcs->canbutton != NULL) {
1221 assert(mcs->canbutton->generic.type == MACCTRL_BUTTON);
1222 HiliteControl(mcs->canbutton->button.tbctrl, kControlButtonPart);
1224 HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart);
1225 ctrlevent(mcs, mcs->canbutton, EVENT_ACTION);
1229 if (mac_gestalts.apprvers >= 0x100) {
1230 if (GetKeyboardFocus(window, &control) == noErr && control != NULL) {
1231 HandleControlKey(control, (event->message & keyCodeMask) >> 8,
1232 event->message & charCodeMask, event->modifiers);
1233 mc = (union macctrl *)GetControlReference(control);
1234 switch (mc->generic.type) {
1235 case MACCTRL_LISTBOX:
1236 ctrlevent(mcs, mc, EVENT_SELCHANGE);
1239 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1244 #if !TARGET_API_MAC_CARBON
1248 if (mcs->focus != NULL) {
1250 switch (mc->generic.type) {
1251 case MACCTRL_EDITBOX:
1252 te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1253 TEKey(event->message & charCodeMask, te);
1254 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1262 void macctrl_update(WindowPtr window)
1264 #if TARGET_API_MAC_CARBON
1270 BeginUpdate(window);
1272 SetPort((GrafPtr)GetWindowPort(window));
1273 if (mac_gestalts.apprvers >= 0x101) {
1274 #if TARGET_API_MAC_CARBON
1275 GetPortBounds(GetWindowPort(window), &rect);
1277 rect = window->portRect;
1279 InsetRect(&rect, -1, -1);
1280 DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ?
1281 kThemeStateActive : kThemeStateInactive);
1283 #if TARGET_API_MAC_CARBON
1285 GetPortVisibleRegion(GetWindowPort(window), visrgn);
1286 UpdateControls(window, visrgn);
1289 UpdateControls(window, window->visRgn);
1295 #if TARGET_API_MAC_CARBON
1296 #define EnableItem EnableMenuItem
1297 #define DisableItem DisableMenuItem
1299 void macctrl_adjustmenus(WindowPtr window)
1303 menu = GetMenuHandle(mFile);
1304 DisableItem(menu, iSave); /* XXX enable if modified */
1305 EnableItem(menu, iSaveAs);
1306 EnableItem(menu, iDuplicate);
1308 menu = GetMenuHandle(mEdit);
1309 DisableItem(menu, 0);
1312 void macctrl_close(WindowPtr window)
1314 struct macctrls *mcs = mac_winctrls(window);
1318 * Mostly, we don't bother disposing of the Toolbox controls,
1319 * since that will happen automatically when the window is
1320 * disposed of. Popup menus are an exception, because we have to
1321 * dispose of the menu ourselves, and doing that while the control
1322 * still holds a reference to it seems rude.
1324 while ((mc = index234(mcs->byctrl, 0)) != NULL) {
1325 if (mc->generic.privdata != NULL && mc->generic.freeprivdata)
1326 sfree(mc->generic.privdata);
1327 switch (mc->generic.type) {
1329 DisposeControl(mc->popup.tbctrl);
1330 DeleteMenu(mc->popup.menuid);
1331 DisposeMenu(mc->popup.menu);
1334 del234(mcs->byctrl, mc);
1338 freetree234(mcs->byctrl);
1344 void dlg_update_start(union control *ctrl, void *dlg)
1350 void dlg_update_done(union control *ctrl, void *dlg)
1356 void dlg_set_focus(union control *ctrl, void *dlg)
1359 if (mac_gestalts.apprvers >= 0x100) {
1360 /* Use SetKeyboardFocus() */
1362 /* Do our own mucking around */
1366 union control *dlg_last_focused(union control *ctrl, void *dlg)
1372 void dlg_beep(void *dlg)
1378 void dlg_error_msg(void *dlg, char *msg)
1382 c2pstrcpy(pmsg, msg);
1383 ParamText(pmsg, NULL, NULL, NULL);
1384 StopAlert(128, NULL);
1387 void dlg_end(void *dlg, int value)
1389 struct macctrls *mcs = dlg;
1391 if (mcs->end != NULL)
1392 (*mcs->end)(mcs->window, value);
1395 void dlg_refresh(union control *ctrl, void *dlg)
1397 struct macctrls *mcs = dlg;
1402 mc = findbyctrl(mcs, ctrl);
1404 ctrlevent(mcs, mc, EVENT_REFRESH);
1407 void *dlg_get_privdata(union control *ctrl, void *dlg)
1409 struct macctrls *mcs = dlg;
1410 union macctrl *mc = findbyctrl(mcs, ctrl);
1413 return mc->generic.privdata;
1416 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
1418 struct macctrls *mcs = dlg;
1419 union macctrl *mc = findbyctrl(mcs, ctrl);
1422 mc->generic.privdata = ptr;
1423 mc->generic.freeprivdata = FALSE;
1426 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
1428 struct macctrls *mcs = dlg;
1429 union macctrl *mc = findbyctrl(mcs, ctrl);
1432 mc->generic.privdata = smalloc(size);
1433 mc->generic.freeprivdata = TRUE;
1434 return mc->generic.privdata;
1439 * Radio Button control
1442 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
1444 struct macctrls *mcs = dlg;
1445 union macctrl *mc = findbyctrl(mcs, ctrl);
1448 if (mc == NULL) return;
1449 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1450 if (i == whichbutton)
1451 SetControlValue(mc->radio.tbctrls[i],
1452 kControlRadioButtonCheckedValue);
1454 SetControlValue(mc->radio.tbctrls[i],
1455 kControlRadioButtonUncheckedValue);
1460 int dlg_radiobutton_get(union control *ctrl, void *dlg)
1462 struct macctrls *mcs = dlg;
1463 union macctrl *mc = findbyctrl(mcs, ctrl);
1467 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1468 if (GetControlValue(mc->radio.tbctrls[i]) ==
1469 kControlRadioButtonCheckedValue)
1480 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
1482 struct macctrls *mcs = dlg;
1483 union macctrl *mc = findbyctrl(mcs, ctrl);
1485 if (mc == NULL) return;
1486 SetControlValue(mc->checkbox.tbctrl,
1487 checked ? kControlCheckBoxCheckedValue :
1488 kControlCheckBoxUncheckedValue);
1491 int dlg_checkbox_get(union control *ctrl, void *dlg)
1493 struct macctrls *mcs = dlg;
1494 union macctrl *mc = findbyctrl(mcs, ctrl);
1497 return GetControlValue(mc->checkbox.tbctrl);
1505 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
1507 struct macctrls *mcs = dlg;
1508 union macctrl *mc = findbyctrl(mcs, ctrl);
1511 if (mc == NULL) return;
1512 assert(mc->generic.type == MACCTRL_EDITBOX);
1514 SetPort((GrafPtr)(GetWindowPort(mcs->window)));
1515 if (mac_gestalts.apprvers >= 0x100)
1516 SetControlData(mc->editbox.tbctrl, kControlEntireControl,
1517 ctrl->editbox.password ?
1518 kControlEditTextPasswordTag :
1519 kControlEditTextTextTag,
1520 strlen(text), text);
1521 #if !TARGET_API_MAC_CARBON
1523 TESetText(text, strlen(text),
1524 (TEHandle)(*mc->editbox.tbctrl)->contrlData);
1526 DrawOneControl(mc->editbox.tbctrl);
1530 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
1532 struct macctrls *mcs = dlg;
1533 union macctrl *mc = findbyctrl(mcs, ctrl);
1537 assert(mc->generic.type == MACCTRL_EDITBOX);
1538 if (mac_gestalts.apprvers >= 0x100) {
1539 if (GetControlData(mc->editbox.tbctrl, kControlEntireControl,
1540 ctrl->editbox.password ?
1541 kControlEditTextPasswordTag :
1542 kControlEditTextTextTag,
1543 length - 1, buffer, &olen) != noErr)
1545 if (olen > length - 1)
1548 #if !TARGET_API_MAC_CARBON
1550 TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1552 olen = (*te)->teLength;
1553 if (olen > length - 1)
1555 memcpy(buffer, *(*te)->hText, olen);
1558 buffer[olen] = '\0';
1566 static void dlg_macpopup_clear(union control *ctrl, void *dlg)
1568 struct macctrls *mcs = dlg;
1569 union macctrl *mc = findbyctrl(mcs, ctrl);
1570 MenuRef menu = mc->popup.menu;
1573 if (mc == NULL) return;
1574 n = CountMenuItems(menu);
1575 for (i = 0; i < n; i++)
1576 DeleteMenuItem(menu, n - i);
1578 sfree(mc->popup.ids);
1579 mc->popup.ids = NULL;
1580 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1583 static void dlg_maclist_clear(union control *ctrl, void *dlg)
1585 struct macctrls *mcs = dlg;
1586 union macctrl *mc = findbyctrl(mcs, ctrl);
1588 if (mc == NULL) return;
1589 LDelRow(0, 0, mc->listbox.list);
1590 mc->listbox.nids = 0;
1591 sfree(mc->listbox.ids);
1592 mc->listbox.ids = NULL;
1593 DrawOneControl(mc->listbox.tbctrl);
1596 void dlg_listbox_clear(union control *ctrl, void *dlg)
1599 switch (ctrl->generic.type) {
1601 if (ctrl->listbox.height == 0)
1602 dlg_macpopup_clear(ctrl, dlg);
1604 dlg_maclist_clear(ctrl, dlg);
1609 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
1611 struct macctrls *mcs = dlg;
1612 union macctrl *mc = findbyctrl(mcs, ctrl);
1613 MenuRef menu = mc->popup.menu;
1615 if (mc == NULL) return;
1616 DeleteMenuItem(menu, index + 1);
1617 if (mc->popup.ids != NULL)
1618 memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
1619 (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
1620 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1623 static void dlg_maclist_del(union control *ctrl, void *dlg, int index)
1625 struct macctrls *mcs = dlg;
1626 union macctrl *mc = findbyctrl(mcs, ctrl);
1628 if (mc == NULL) return;
1629 LDelRow(1, index, mc->listbox.list);
1630 if (mc->listbox.ids != NULL)
1631 memcpy(mc->listbox.ids + index, mc->listbox.ids + index + 1,
1632 (mc->listbox.nids - index - 1) * sizeof(*mc->listbox.ids));
1633 DrawOneControl(mc->listbox.tbctrl);
1636 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
1639 switch (ctrl->generic.type) {
1641 if (ctrl->listbox.height == 0)
1642 dlg_macpopup_del(ctrl, dlg, index);
1644 dlg_maclist_del(ctrl, dlg, index);
1649 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
1651 struct macctrls *mcs = dlg;
1652 union macctrl *mc = findbyctrl(mcs, ctrl);
1653 MenuRef menu = mc->popup.menu;
1656 if (mc == NULL) return;
1657 assert(text[0] != '\0');
1658 c2pstrcpy(itemstring, text);
1659 AppendMenu(menu, "\pdummy");
1660 SetMenuItemText(menu, CountMenuItems(menu), itemstring);
1661 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1665 static void dlg_maclist_add(union control *ctrl, void *dlg, char const *text)
1667 struct macctrls *mcs = dlg;
1668 union macctrl *mc = findbyctrl(mcs, ctrl);
1670 Cell cell = { 0, 0 };
1672 if (mc == NULL) return;
1673 #if TARGET_API_MAC_CARBON
1674 GetListDataBounds(mc->listbox.list, &bounds);
1676 bounds = (*mc->listbox.list)->dataBounds;
1678 cell.v = bounds.bottom;
1679 LAddRow(1, cell.v, mc->listbox.list);
1680 LSetCell(text, strlen(text), cell, mc->listbox.list);
1681 DrawOneControl(mc->listbox.tbctrl);
1684 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
1687 switch (ctrl->generic.type) {
1689 if (ctrl->listbox.height == 0)
1690 dlg_macpopup_add(ctrl, dlg, text);
1692 dlg_maclist_add(ctrl, dlg, text);
1697 static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
1698 char const *text, int id)
1700 struct macctrls *mcs = dlg;
1701 union macctrl *mc = findbyctrl(mcs, ctrl);
1702 MenuRef menu = mc->popup.menu;
1705 if (mc == NULL) return;
1706 dlg_macpopup_add(ctrl, dlg, text);
1707 index = CountMenuItems(menu) - 1;
1708 if (mc->popup.nids <= index) {
1709 mc->popup.nids = index + 1;
1710 mc->popup.ids = sresize(mc->popup.ids, mc->popup.nids, int);
1712 mc->popup.ids[index] = id;
1715 static void dlg_maclist_addwithid(union control *ctrl, void *dlg,
1716 char const *text, int id)
1718 struct macctrls *mcs = dlg;
1719 union macctrl *mc = findbyctrl(mcs, ctrl);
1723 if (mc == NULL) return;
1724 dlg_maclist_add(ctrl, dlg, text);
1725 #if TARGET_API_MAC_CARBON
1726 GetListDataBounds(mc->listbox.list, &bounds);
1728 bounds = (*mc->listbox.list)->dataBounds;
1730 index = bounds.bottom;
1731 if (mc->listbox.nids <= index) {
1732 mc->listbox.nids = index + 1;
1733 mc->listbox.ids = sresize(mc->listbox.ids, mc->listbox.nids, int);
1735 mc->listbox.ids[index] = id;
1738 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
1739 char const *text, int id)
1742 switch (ctrl->generic.type) {
1744 if (ctrl->listbox.height == 0)
1745 dlg_macpopup_addwithid(ctrl, dlg, text, id);
1747 dlg_maclist_addwithid(ctrl, dlg, text, id);
1752 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
1754 struct macctrls *mcs = dlg;
1755 union macctrl *mc = findbyctrl(mcs, ctrl);
1758 switch (ctrl->generic.type) {
1760 if (ctrl->listbox.height == 0) {
1761 assert(mc->popup.ids != NULL && mc->popup.nids > index);
1762 return mc->popup.ids[index];
1764 assert(mc->listbox.ids != NULL && mc->listbox.nids > index);
1765 return mc->listbox.ids[index];
1771 int dlg_listbox_index(union control *ctrl, void *dlg)
1773 struct macctrls *mcs = dlg;
1774 union macctrl *mc = findbyctrl(mcs, ctrl);
1775 Cell cell = { 0, 0 };
1778 switch (ctrl->generic.type) {
1780 if (ctrl->listbox.height == 0)
1781 return GetControlValue(mc->popup.tbctrl) - 1;
1783 if (LGetSelect(TRUE, &cell, mc->listbox.list))
1792 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1794 struct macctrls *mcs = dlg;
1795 union macctrl *mc = findbyctrl(mcs, ctrl);
1796 Cell cell = { 0, 0 };
1799 switch (ctrl->generic.type) {
1801 if (ctrl->listbox.height == 0)
1802 return GetControlValue(mc->popup.tbctrl) - 1 == index;
1805 return LGetSelect(FALSE, &cell, mc->listbox.list);
1811 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1813 struct macctrls *mcs = dlg;
1814 union macctrl *mc = findbyctrl(mcs, ctrl);
1816 if (mc == NULL) return;
1817 switch (ctrl->generic.type) {
1819 if (ctrl->listbox.height == 0)
1820 SetControlValue(mc->popup.tbctrl, index + 1);
1830 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1832 struct macctrls *mcs = dlg;
1833 union macctrl *mc = findbyctrl(mcs, ctrl);
1835 if (mc == NULL) return;
1836 if (mac_gestalts.apprvers >= 0x100)
1837 SetControlData(mc->text.tbctrl, kControlEntireControl,
1838 kControlStaticTextTextTag, strlen(text), text);
1839 #if !TARGET_API_MAC_CARBON
1841 TESetText(text, strlen(text),
1842 (TEHandle)(*mc->text.tbctrl)->contrlData);
1848 * File Selector control
1851 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1856 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1863 * Font Selector control
1866 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1871 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1878 * Printer enumeration
1881 printer_enum *printer_start_enum(int *nprinters)
1888 char *printer_get_name(printer_enum *pe, int thing)
1894 void printer_finish_enum(printer_enum *pe)
1901 * Colour selection stuff
1904 void dlg_coloursel_start(union control *ctrl, void *dlg,
1905 int r, int g, int b)
1907 struct macctrls *mcs = dlg;
1908 union macctrl *mc = findbyctrl(mcs, ctrl);
1909 Point where = {-1, -1}; /* Screen with greatest colour depth */
1912 if (HAVE_COLOR_QD()) {
1913 incolour.red = r * 0x0101;
1914 incolour.green = g * 0x0101;
1915 incolour.blue = b * 0x0101;
1916 mcs->gotcolour = GetColor(where, "\pModify Colour:", &incolour,
1918 ctrlevent(mcs, mc, EVENT_CALLBACK);
1923 int dlg_coloursel_results(union control *ctrl, void *dlg,
1924 int *r, int *g, int *b)
1926 struct macctrls *mcs = dlg;
1928 if (mcs->gotcolour) {
1929 *r = mcs->thecolour.red >> 8;
1930 *g = mcs->thecolour.green >> 8;
1931 *b = mcs->thecolour.blue >> 8;
1939 * c-file-style: "simon"