1 /* $Id: macctrls.c,v 1.33 2003/04/13 14:37:07 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>
35 #include <Resources.h>
39 #include <TextUtils.h>
40 #include <ToolUtils.h>
52 /* Range of menu IDs for popup menus */
58 struct macctrl_generic {
68 /* Template from which this was generated */
70 /* Next control in this panel */
76 struct macctrl_generic generic;
80 struct macctrl_generic generic;
85 struct macctrl_generic generic;
90 struct macctrl_generic generic;
94 struct macctrl_generic generic;
99 struct macctrl_generic generic;
106 struct macctrl_generic generic;
115 struct mac_layoutstate {
118 unsigned int panelnum;
121 #define ctrlevent(mcs, mc, event) do { \
122 if ((mc)->generic.ctrl->generic.handler != NULL) \
123 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
124 (mcs)->data, (event)); \
127 #define findbyctrl(mcs, ctrl) \
128 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
130 static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *,
131 WindowPtr, struct macctrls *);
132 static void macctrl_switchtopanel(struct macctrls *, unsigned int);
133 static void macctrl_setfocus(struct macctrls *, union macctrl *);
134 static void macctrl_text(struct macctrls *, WindowPtr,
135 struct mac_layoutstate *, union control *);
136 static void macctrl_editbox(struct macctrls *, WindowPtr,
137 struct mac_layoutstate *, union control *);
138 static void macctrl_radio(struct macctrls *, WindowPtr,
139 struct mac_layoutstate *, union control *);
140 static void macctrl_checkbox(struct macctrls *, WindowPtr,
141 struct mac_layoutstate *, union control *);
142 static void macctrl_button(struct macctrls *, WindowPtr,
143 struct mac_layoutstate *, union control *);
144 static void macctrl_listbox(struct macctrls *, WindowPtr,
145 struct mac_layoutstate *, union control *);
146 static void macctrl_popup(struct macctrls *, WindowPtr,
147 struct mac_layoutstate *, union control *);
148 #if !TARGET_API_MAC_CARBON
149 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef,
150 ControlDefProcMessage, SInt32);
151 static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
152 ControlDefProcMessage, SInt32);
153 static pascal SInt32 macctrl_sys7_listbox_cdef(SInt16, ControlRef,
154 ControlDefProcMessage, SInt32);
157 #if !TARGET_API_MAC_CARBON
159 * This trick enables us to keep all the CDEF code in the main
160 * application, which makes life easier. For details, see
161 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
164 #pragma options align=mac68k
166 short jmpabs; /* 4EF9 */
167 ControlDefUPP theUPP;
169 #pragma options align=reset
172 static void macctrl_init()
174 #if !TARGET_API_MAC_CARBON
175 static int inited = 0;
179 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox);
180 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef);
181 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
182 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
183 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_ListBox);
184 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_listbox_cdef);
190 static int macctrl_cmp_byctrl(void *av, void *bv)
192 union macctrl *a = (union macctrl *)av;
193 union macctrl *b = (union macctrl *)bv;
195 if (a->generic.ctrl < b->generic.ctrl)
197 else if (a->generic.ctrl > b->generic.ctrl)
203 static int macctrl_cmp_byctrl_find(void *av, void *bv)
205 union control *a = (union control *)av;
206 union macctrl *b = (union macctrl *)bv;
208 if (a < b->generic.ctrl)
210 else if (a > b->generic.ctrl)
216 static union control panellist;
218 static void panellist_handler(union control *ctrl, void *dlg, void *data,
221 struct macctrls *mcs = dlg;
223 if (event == EVENT_SELCHANGE)
224 macctrl_switchtopanel(mcs, dlg_listbox_index(ctrl, dlg) + 1);
227 void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
228 struct macctrls *mcs)
231 struct mac_layoutstate curstate;
236 if (mac_gestalts.apprvers >= 0x100)
237 CreateRootControl(window, &root);
238 #if TARGET_API_MAC_CARBON
239 GetPortBounds(GetWindowPort(window), &rect);
241 rect = window->portRect;
243 mcs->window = window;
244 mcs->byctrl = newtree234(macctrl_cmp_byctrl);
246 mcs->defbutton = NULL;
247 mcs->canbutton = NULL;
248 /* Count the number of panels */
250 for (i = 1; i < cb->nctrlsets; i++)
251 if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
253 mcs->panels = snewn(mcs->npanels, union macctrl *);
254 memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
255 curstate.panelnum = 0;
257 curstate.pos.h = rect.left + 13;
258 curstate.pos.v = rect.top + 13;
259 curstate.width = 160;
260 panellist.listbox.type = CTRL_LISTBOX;
261 panellist.listbox.handler = &panellist_handler;
262 panellist.listbox.height = 15;
263 panellist.listbox.percentwidth = 100;
264 macctrl_listbox(mcs, window, &curstate, &panellist);
266 curstate.pos.h = rect.left + 13 + 160 + 13;
267 curstate.pos.v = rect.bottom - 33;
268 curstate.width = rect.right - (rect.left + 13 + 160) - (13 * 2);
269 for (i = 0; i < cb->nctrlsets; i++) {
270 if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
271 cb->ctrlsets[i-1]->pathname)) {
272 curstate.pos.v = rect.top + 13;
274 assert(curstate.panelnum < mcs->npanels);
275 dlg_listbox_add(&panellist, mcs, cb->ctrlsets[i]->pathname);
277 macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
279 macctrl_switchtopanel(mcs, 1);
280 /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
285 static void macctrl_layoutset(struct mac_layoutstate *curstate,
286 struct controlset *s,
287 WindowPtr window, struct macctrls *mcs)
289 unsigned int i, j, ncols, colstart;
290 struct mac_layoutstate cols[MAXCOLS];
292 fprintf(stderr, "--- begin set ---\n");
293 fprintf(stderr, "pathname = %s\n", s->pathname);
294 if (s->boxname && *s->boxname)
295 fprintf(stderr, "boxname = %s\n", s->boxname);
297 fprintf(stderr, "boxtitle = %s\n", s->boxtitle);
302 for (i = 0; i < s->ncontrols; i++) {
303 union control *ctrl = s->ctrls[i];
306 colstart = COLUMN_START(ctrl->generic.column);
307 switch (ctrl->generic.type) {
308 case CTRL_TEXT: s = "text"; break;
309 case CTRL_EDITBOX: s = "editbox"; break;
310 case CTRL_RADIO: s = "radio"; break;
311 case CTRL_CHECKBOX: s = "checkbox"; break;
312 case CTRL_BUTTON: s = "button"; break;
313 case CTRL_LISTBOX: s = "listbox"; break;
314 case CTRL_COLUMNS: s = "columns"; break;
315 case CTRL_FILESELECT: s = "fileselect"; break;
316 case CTRL_FONTSELECT: s = "fontselect"; break;
317 case CTRL_TABDELAY: s = "tabdelay"; break;
318 default: s = "unknown"; break;
320 fprintf(stderr, " control: %s\n", s);
321 switch (ctrl->generic.type) {
323 if (ctrl->columns.ncols != 1) {
324 ncols = ctrl->columns.ncols;
325 fprintf(stderr, " split to %d\n", ncols);
326 assert(ncols <= MAXCOLS);
327 for (j = 0; j < ncols; j++) {
330 cols[j].pos.h = cols[j-1].pos.h + cols[j-1].width + 6;
332 cols[j].width = curstate->width -
333 (cols[j].pos.h - curstate->pos.h);
335 cols[j].width = (curstate->width + 6) *
336 ctrl->columns.percentages[j] / 100 - 6;
339 fprintf(stderr, " join\n");
340 for (j = 0; j < ncols; j++)
341 if (cols[j].pos.v > cols[0].pos.v)
342 cols[0].pos.v = cols[j].pos.v;
343 cols[0].width = curstate->width;
348 macctrl_text(mcs, window, &cols[colstart], ctrl);
351 macctrl_editbox(mcs, window, &cols[colstart], ctrl);
354 macctrl_radio(mcs, window, &cols[colstart], ctrl);
357 macctrl_checkbox(mcs, window, &cols[colstart], ctrl);
360 macctrl_button(mcs, window, &cols[colstart], ctrl);
363 if (ctrl->listbox.height == 0)
364 macctrl_popup(mcs, window, &cols[colstart], ctrl);
366 macctrl_listbox(mcs, window, &cols[colstart], ctrl);
370 for (j = 0; j < ncols; j++)
371 if (cols[j].pos.v > curstate->pos.v)
372 curstate->pos.v = cols[j].pos.v;
375 static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
380 #define hideshow(c) do { \
381 if (i == which) ShowControl(c); else HideControl(c); \
384 mcs->curpanel = which;
385 /* Panel 0 is special and always visible. */
386 for (i = 1; i < mcs->npanels; i++)
387 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
388 #if !TARGET_API_MAC_CARBON
389 if (mcs->focus == mc)
390 macctrl_setfocus(mcs, NULL);
392 switch (mc->generic.type) {
394 hideshow(mc->text.tbctrl);
396 case MACCTRL_EDITBOX:
397 hideshow(mc->editbox.tbctrl);
398 if (mc->editbox.tblabel != NULL)
399 hideshow(mc->editbox.tblabel);
402 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
403 hideshow(mc->radio.tbctrls[j]);
404 if (mc->radio.tblabel != NULL)
405 hideshow(mc->radio.tblabel);
407 case MACCTRL_CHECKBOX:
408 hideshow(mc->checkbox.tbctrl);
411 hideshow(mc->button.tbctrl);
413 case MACCTRL_LISTBOX:
414 hideshow(mc->listbox.tbctrl);
416 * At least under Mac OS 8.1, hiding a list box
417 * doesn't hide its scroll bars.
419 #if TARGET_API_MAC_CARBON
420 hideshow(GetListVerticalScrollBar(mc->listbox.list));
422 hideshow((*mc->listbox.list)->vScroll);
426 hideshow(mc->popup.tbctrl);
432 #if !TARGET_API_MAC_CARBON
434 * System 7 focus manipulation
436 static void macctrl_defocus(union macctrl *mc)
439 assert(mac_gestalts.apprvers < 0x100);
440 switch (mc->generic.type) {
441 case MACCTRL_EDITBOX:
442 TEDeactivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
447 static void macctrl_enfocus(union macctrl *mc)
450 assert(mac_gestalts.apprvers < 0x100);
451 switch (mc->generic.type) {
452 case MACCTRL_EDITBOX:
453 TEActivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
458 static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc)
461 if (mcs->focus == mc)
463 if (mcs->focus != NULL)
464 macctrl_defocus(mcs->focus);
471 static void macctrl_text(struct macctrls *mcs, WindowPtr window,
472 struct mac_layoutstate *curstate,
475 union macctrl *mc = snew(union macctrl);
479 assert(ctrl->text.label != NULL);
480 fprintf(stderr, " label = %s\n", ctrl->text.label);
481 mc->generic.type = MACCTRL_TEXT;
482 mc->generic.ctrl = ctrl;
483 mc->generic.privdata = NULL;
484 bounds.left = curstate->pos.h;
485 bounds.right = bounds.left + curstate->width;
486 bounds.top = curstate->pos.v;
487 bounds.bottom = bounds.top + 16;
488 if (mac_gestalts.apprvers >= 0x100) {
491 mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
492 kControlStaticTextProc, (long)mc);
493 SetControlData(mc->text.tbctrl, kControlEntireControl,
494 kControlStaticTextTextTag,
495 strlen(ctrl->text.label), ctrl->text.label);
496 GetControlData(mc->text.tbctrl, kControlEntireControl,
497 kControlStaticTextTextHeightTag,
498 sizeof(height), &height, &olen);
500 #if !TARGET_API_MAC_CARBON
504 mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
505 SYS7_TEXT_PROC, (long)mc);
506 te = (TEHandle)(*mc->text.tbctrl)->contrlData;
507 TESetText(ctrl->text.label, strlen(ctrl->text.label), te);
508 height = TEGetHeight(1, (*te)->nLines, te);
511 fprintf(stderr, " height = %d\n", height);
512 SizeControl(mc->text.tbctrl, curstate->width, height);
513 curstate->pos.v += height + 6;
514 add234(mcs->byctrl, mc);
515 mc->generic.next = mcs->panels[curstate->panelnum];
516 mcs->panels[curstate->panelnum] = mc;
519 static void macctrl_editbox(struct macctrls *mcs, WindowPtr window,
520 struct mac_layoutstate *curstate,
523 union macctrl *mc = snew(union macctrl);
524 Rect lbounds, bounds;
526 if (ctrl->editbox.label != NULL)
527 fprintf(stderr, " label = %s\n", ctrl->editbox.label);
528 fprintf(stderr, " percentwidth = %d\n", ctrl->editbox.percentwidth);
529 if (ctrl->editbox.password) fprintf(stderr, " password\n");
530 if (ctrl->editbox.has_list) fprintf(stderr, " has list\n");
531 mc->generic.type = MACCTRL_EDITBOX;
532 mc->generic.ctrl = ctrl;
533 mc->generic.privdata = NULL;
534 lbounds.left = curstate->pos.h;
535 lbounds.top = curstate->pos.v;
536 if (ctrl->editbox.percentwidth == 100) {
537 if (ctrl->editbox.label != NULL) {
538 lbounds.right = lbounds.left + curstate->width;
539 lbounds.bottom = lbounds.top + 16;
540 curstate->pos.v += 18;
542 bounds.left = curstate->pos.h;
543 bounds.right = bounds.left + curstate->width;
545 lbounds.right = lbounds.left +
546 curstate->width * (100 - ctrl->editbox.percentwidth) / 100;
547 lbounds.bottom = lbounds.top + 22;
548 bounds.left = lbounds.right;
549 bounds.right = lbounds.left + curstate->width;
551 bounds.top = curstate->pos.v;
552 bounds.bottom = bounds.top + 22;
553 if (mac_gestalts.apprvers >= 0x100) {
554 if (ctrl->editbox.label == NULL)
555 mc->editbox.tblabel = NULL;
557 mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE,
558 0, 0, 0, kControlStaticTextProc,
560 SetControlData(mc->editbox.tblabel, kControlEntireControl,
561 kControlStaticTextTextTag,
562 strlen(ctrl->editbox.label), ctrl->editbox.label);
564 InsetRect(&bounds, 3, 3);
565 mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
566 ctrl->editbox.password ?
567 kControlEditTextPasswordProc :
568 kControlEditTextProc, (long)mc);
570 #if !TARGET_API_MAC_CARBON
572 if (ctrl->editbox.label == NULL)
573 mc->editbox.tblabel = NULL;
575 mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE,
576 0, 0, 0, SYS7_TEXT_PROC,
578 TESetText(ctrl->editbox.label, strlen(ctrl->editbox.label),
579 (TEHandle)(*mc->editbox.tblabel)->contrlData);
581 mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
582 SYS7_EDITBOX_PROC, (long)mc);
585 curstate->pos.v += 28;
586 add234(mcs->byctrl, mc);
587 mc->generic.next = mcs->panels[curstate->panelnum];
588 mcs->panels[curstate->panelnum] = mc;
589 ctrlevent(mcs, mc, EVENT_REFRESH);
592 #if !TARGET_API_MAC_CARBON
593 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant,
595 ControlDefProcMessage msg,
606 rect = (*control)->contrlRect;
607 if (variant == SYS7_EDITBOX_VARIANT)
608 InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */
609 te = TENew(&rect, &rect);
610 ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
611 (*te)->txSize = LoWord(ssfs);
612 (*te)->txFont = HiWord(ssfs);
613 (*control)->contrlData = (Handle)te;
616 TEDispose((TEHandle)(*control)->contrlData);
619 if ((*control)->contrlVis) {
620 rect = (*control)->contrlRect;
621 if (variant == SYS7_EDITBOX_VARIANT) {
624 InsetRect(&rect, 3, 3);
626 (*(TEHandle)(*control)->contrlData)->viewRect = rect;
627 TEUpdate(&rect, (TEHandle)(*control)->contrlData);
631 if (variant == SYS7_TEXT_VARIANT)
632 return kControlNoPart;
633 mouse.h = LoWord(param);
634 mouse.v = HiWord(param);
635 rect = (*control)->contrlRect;
636 InsetRect(&rect, 3, 3);
637 return PtInRect(mouse, &rect) ? kControlEditTextPart : kControlNoPart;
639 if (param & (1 << 31)) {
645 rgn = (RgnHandle)param;
646 RectRgn(rgn, &(*control)->contrlRect);
650 rgn = (RgnHandle)param;
659 static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
660 struct mac_layoutstate *curstate,
663 union macctrl *mc = snew(union macctrl);
666 unsigned int i, colwidth;
668 if (ctrl->radio.label != NULL)
669 fprintf(stderr, " label = %s\n", ctrl->radio.label);
670 mc->generic.type = MACCTRL_RADIO;
671 mc->generic.ctrl = ctrl;
672 mc->generic.privdata = NULL;
673 mc->radio.tbctrls = snewn(ctrl->radio.nbuttons, ControlRef);
674 colwidth = (curstate->width + 13) / ctrl->radio.ncolumns;
675 bounds.top = curstate->pos.v;
676 bounds.bottom = bounds.top + 16;
677 bounds.left = curstate->pos.h;
678 bounds.right = bounds.left + curstate->width;
679 if (ctrl->radio.label == NULL)
680 mc->radio.tblabel = NULL;
682 if (mac_gestalts.apprvers >= 0x100) {
683 mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE,
684 0, 0, 0, kControlStaticTextProc,
686 SetControlData(mc->radio.tblabel, kControlEntireControl,
687 kControlStaticTextTextTag,
688 strlen(ctrl->radio.label), ctrl->radio.label);
690 #if !TARGET_API_MAC_CARBON
692 mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE,
693 0, 0, 0, SYS7_TEXT_PROC, (long)mc);
694 TESetText(ctrl->radio.label, strlen(ctrl->radio.label),
695 (TEHandle)(*mc->radio.tblabel)->contrlData);
698 curstate->pos.v += 18;
700 for (i = 0; i < ctrl->radio.nbuttons; i++) {
701 fprintf(stderr, " button = %s\n", ctrl->radio.buttons[i]);
702 bounds.top = curstate->pos.v - 2;
703 bounds.bottom = bounds.top + 18;
704 bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
705 if (i == ctrl->radio.nbuttons - 1 ||
706 i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
707 bounds.right = curstate->pos.h + curstate->width;
708 curstate->pos.v += 18;
710 bounds.right = bounds.left + colwidth - 13;
711 c2pstrcpy(title, ctrl->radio.buttons[i]);
712 mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE,
713 0, 0, 1, radioButProc, (long)mc);
715 curstate->pos.v += 4;
716 add234(mcs->byctrl, mc);
717 mc->generic.next = mcs->panels[curstate->panelnum];
718 mcs->panels[curstate->panelnum] = mc;
719 ctrlevent(mcs, mc, EVENT_REFRESH);
722 static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
723 struct mac_layoutstate *curstate,
726 union macctrl *mc = snew(union macctrl);
730 assert(ctrl->checkbox.label != NULL);
731 fprintf(stderr, " label = %s\n", ctrl->checkbox.label);
732 mc->generic.type = MACCTRL_CHECKBOX;
733 mc->generic.ctrl = ctrl;
734 mc->generic.privdata = NULL;
735 bounds.left = curstate->pos.h;
736 bounds.right = bounds.left + curstate->width;
737 bounds.top = curstate->pos.v;
738 bounds.bottom = bounds.top + 16;
739 c2pstrcpy(title, ctrl->checkbox.label);
740 mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
741 checkBoxProc, (long)mc);
742 add234(mcs->byctrl, mc);
743 curstate->pos.v += 22;
744 mc->generic.next = mcs->panels[curstate->panelnum];
745 mcs->panels[curstate->panelnum] = mc;
746 ctrlevent(mcs, mc, EVENT_REFRESH);
749 static void macctrl_button(struct macctrls *mcs, WindowPtr window,
750 struct mac_layoutstate *curstate,
753 union macctrl *mc = snew(union macctrl);
757 assert(ctrl->button.label != NULL);
758 fprintf(stderr, " label = %s\n", ctrl->button.label);
759 if (ctrl->button.isdefault)
760 fprintf(stderr, " is default\n");
761 mc->generic.type = MACCTRL_BUTTON;
762 mc->generic.ctrl = ctrl;
763 mc->generic.privdata = NULL;
764 bounds.left = curstate->pos.h;
765 bounds.right = bounds.left + curstate->width;
766 bounds.top = curstate->pos.v;
767 bounds.bottom = bounds.top + 20;
768 c2pstrcpy(title, ctrl->button.label);
769 mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
770 pushButProc, (long)mc);
771 mc->button.tbring = NULL;
772 if (mac_gestalts.apprvers >= 0x100) {
773 Boolean isdefault = ctrl->button.isdefault;
775 SetControlData(mc->button.tbctrl, kControlEntireControl,
776 kControlPushButtonDefaultTag,
777 sizeof(isdefault), &isdefault);
778 } else if (ctrl->button.isdefault) {
779 InsetRect(&bounds, -4, -4);
780 mc->button.tbring = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
781 SYS7_DEFAULT_PROC, (long)mc);
783 if (mac_gestalts.apprvers >= 0x110) {
784 Boolean iscancel = ctrl->button.iscancel;
786 SetControlData(mc->button.tbctrl, kControlEntireControl,
787 kControlPushButtonCancelTag,
788 sizeof(iscancel), &iscancel);
790 if (ctrl->button.isdefault)
792 if (ctrl->button.iscancel)
794 add234(mcs->byctrl, mc);
795 mc->generic.next = mcs->panels[curstate->panelnum];
796 mcs->panels[curstate->panelnum] = mc;
797 curstate->pos.v += 26;
800 #if !TARGET_API_MAC_CARBON
801 static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
803 ControlDefProcMessage msg,
813 if ((*control)->contrlVis) {
814 rect = (*control)->contrlRect;
815 GetPenState(&savestate);
818 if ((*control)->contrlHilite == kControlInactivePart)
820 oval = (rect.bottom - rect.top) / 2 + 2;
821 FrameRoundRect(&rect, oval, oval);
822 SetPenState(&savestate);
826 if (param & (1 << 31)) {
832 rgn = (RgnHandle)param;
833 RectRgn(rgn, &(*control)->contrlRect);
837 rgn = (RgnHandle)param;
846 static void macctrl_listbox(struct macctrls *mcs, WindowPtr window,
847 struct mac_layoutstate *curstate,
850 union macctrl *mc = snew(union macctrl);
854 if (ctrl->listbox.label != NULL)
855 fprintf(stderr, " label = %s\n", ctrl->listbox.label);
856 fprintf(stderr, " height = %d\n", ctrl->listbox.height);
857 if (ctrl->listbox.draglist)
858 fprintf(stderr, " draglist\n");
859 if (ctrl->listbox.multisel)
860 fprintf(stderr, " multisel\n");
861 fprintf(stderr, " ncols = %d\n", ctrl->listbox.ncols);
862 assert(ctrl->listbox.percentwidth == 100);
863 mc->generic.type = MACCTRL_LISTBOX;
864 mc->generic.ctrl = ctrl;
865 mc->generic.privdata = NULL;
866 /* The list starts off empty */
867 mc->listbox.nids = 0;
868 mc->listbox.ids = NULL;
869 bounds.left = curstate->pos.h;
870 bounds.right = bounds.left + curstate->width;
871 bounds.top = curstate->pos.v;
872 bounds.bottom = bounds.top + 20 * ctrl->listbox.height;
874 if (mac_gestalts.apprvers >= 0x100) {
875 InsetRect(&bounds, 3, 3);
876 mc->listbox.tbctrl = NewControl(window, &bounds, NULL, TRUE,
878 kControlListBoxProc, (long)mc);
879 if (GetControlData(mc->listbox.tbctrl, kControlEntireControl,
880 kControlListBoxListHandleTag,
881 sizeof(mc->listbox.list), &mc->listbox.list,
883 DisposeControl(mc->listbox.tbctrl);
888 #if !TARGET_API_MAC_CARBON
890 InsetRect(&bounds, -3, -3);
891 mc->listbox.tbctrl = NewControl(window, &bounds, NULL, TRUE,
893 SYS7_LISTBOX_PROC, (long)mc);
894 mc->listbox.list = (ListHandle)(*mc->listbox.tbctrl)->contrlData;
895 (*mc->listbox.list)->refCon = (long)mc;
898 if (!ctrl->listbox.multisel) {
899 #if TARGET_API_MAC_CARBON
900 SetListSelectionFlags(mc->listbox.list, lOnlyOne);
902 (*mc->listbox.list)->selFlags = lOnlyOne;
905 add234(mcs->byctrl, mc);
906 curstate->pos.v += 6 + 20 * ctrl->listbox.height;
907 mc->generic.next = mcs->panels[curstate->panelnum];
908 mcs->panels[curstate->panelnum] = mc;
909 ctrlevent(mcs, mc, EVENT_REFRESH);
912 #if !TARGET_API_MAC_CARBON
913 static pascal SInt32 macctrl_sys7_listbox_cdef(SInt16 variant,
915 ControlDefProcMessage msg,
931 rect = (*control)->contrlRect;
932 InsetRect(&rect, 4, 4);
933 rect.right -= 15; /* scroll bar */
934 bounds.top = bounds.bottom = bounds.left = 0;
936 csize.h = csize.v = 0;
938 savefont = curport->txFont;
939 savesize = curport->txSize;
940 ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
941 TextFont(HiWord(ssfs));
942 TextSize(LoWord(ssfs));
943 list = LNew(&rect, &bounds, csize, 0, (*control)->contrlOwner,
944 TRUE, FALSE, FALSE, TRUE);
945 SetControlReference((*list)->vScroll, (long)list);
946 (*control)->contrlData = (Handle)list;
952 * If the dialogue box is being destroyed, the scroll bar
953 * might have gone already. In our situation, this is the
954 * only time we destroy a control, so NULL out the scroll bar
955 * handle to prevent LDispose trying to free it.
957 list = (ListHandle)(*control)->contrlData;
958 (*list)->vScroll = NULL;
962 if ((*control)->contrlVis) {
963 rect = (*control)->contrlRect;
964 /* XXX input focus highlighting? */
965 InsetRect(&rect, 3, 3);
968 list = (ListHandle)(*control)->contrlData;
969 LActivate((*control)->contrlHilite != kControlInactivePart, list);
971 LUpdate(curport->visRgn, list);
975 mouse.h = LoWord(param);
976 mouse.v = HiWord(param);
977 rect = (*control)->contrlRect;
978 InsetRect(&rect, 4, 4);
980 * We deliberately exclude the scrollbar so that LClick() can see it.
983 return PtInRect(mouse, &rect) ? kControlListBoxPart : kControlNoPart;
985 if (param & (1 << 31)) {
991 rgn = (RgnHandle)param;
992 RectRgn(rgn, &(*control)->contrlRect);
996 rgn = (RgnHandle)param;
1005 static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
1006 struct mac_layoutstate *curstate,
1007 union control *ctrl)
1009 union macctrl *mc = snew(union macctrl);
1012 unsigned int labelwidth;
1013 static int nextmenuid = MENU_MIN;
1018 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
1019 * create a popup menu with dynamic content.
1021 assert(ctrl->listbox.height == 0);
1022 assert(!ctrl->listbox.draglist);
1023 assert(!ctrl->listbox.multisel);
1025 if (ctrl->listbox.label != NULL)
1026 fprintf(stderr, " label = %s\n", ctrl->listbox.label);
1027 fprintf(stderr, " percentwidth = %d\n", ctrl->listbox.percentwidth);
1029 mc->generic.type = MACCTRL_POPUP;
1030 mc->generic.ctrl = ctrl;
1031 mc->generic.privdata = NULL;
1032 c2pstrcpy(title, ctrl->button.label == NULL ? "" : ctrl->button.label);
1034 /* Find a spare menu ID and create the menu */
1035 while (GetMenuHandle(nextmenuid) != NULL)
1036 if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
1037 menuid = nextmenuid++;
1038 menu = NewMenu(menuid, "\pdummy");
1039 if (menu == NULL) return;
1040 mc->popup.menu = menu;
1041 mc->popup.menuid = menuid;
1042 InsertMenu(menu, kInsertHierarchicalMenu);
1044 /* The menu starts off empty */
1046 mc->popup.ids = NULL;
1048 bounds.left = curstate->pos.h;
1049 bounds.right = bounds.left + curstate->width;
1050 bounds.top = curstate->pos.v;
1051 bounds.bottom = bounds.top + 20;
1052 /* XXX handle percentwidth == 100 */
1053 labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
1054 mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE,
1055 popupTitleLeftJust, menuid, labelwidth,
1056 popupMenuProc + popupFixedWidth, (long)mc);
1057 add234(mcs->byctrl, mc);
1058 curstate->pos.v += 26;
1059 mc->generic.next = mcs->panels[curstate->panelnum];
1060 mcs->panels[curstate->panelnum] = mc;
1061 ctrlevent(mcs, mc, EVENT_REFRESH);
1065 void macctrl_activate(WindowPtr window, EventRecord *event)
1067 struct macctrls *mcs = mac_winctrls(window);
1068 Boolean active = (event->modifiers & activeFlag) != 0;
1071 ControlPartCode state;
1075 SetPort((GrafPtr)GetWindowPort(window));
1076 if (mac_gestalts.apprvers >= 0x100)
1077 SetThemeWindowBackground(window, active ?
1078 kThemeBrushModelessDialogBackgroundActive :
1079 kThemeBrushModelessDialogBackgroundInactive,
1081 state = active ? kControlNoPart : kControlInactivePart;
1082 for (i = 0; i <= mcs->curpanel; i += mcs->curpanel)
1083 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
1084 switch (mc->generic.type) {
1086 HiliteControl(mc->text.tbctrl, state);
1088 case MACCTRL_EDITBOX:
1089 HiliteControl(mc->editbox.tbctrl, state);
1090 if (mc->editbox.tblabel != NULL)
1091 HiliteControl(mc->editbox.tblabel, state);
1094 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
1095 HiliteControl(mc->radio.tbctrls[j], state);
1096 if (mc->radio.tblabel != NULL)
1097 HiliteControl(mc->radio.tblabel, state);
1099 case MACCTRL_CHECKBOX:
1100 HiliteControl(mc->checkbox.tbctrl, state);
1102 case MACCTRL_BUTTON:
1103 HiliteControl(mc->button.tbctrl, state);
1104 if (mc->button.tbring != NULL)
1105 HiliteControl(mc->button.tbring, state);
1107 case MACCTRL_LISTBOX:
1108 HiliteControl(mc->listbox.tbctrl, state);
1111 HiliteControl(mc->popup.tbctrl, state);
1114 #if !TARGET_API_MAC_CARBON
1115 if (mcs->focus == mc) {
1117 macctrl_enfocus(mc);
1119 macctrl_defocus(mc);
1126 void macctrl_click(WindowPtr window, EventRecord *event)
1129 ControlHandle control, oldfocus;
1130 int part, trackresult;
1133 struct macctrls *mcs = mac_winctrls(window);
1138 SetPort((GrafPtr)GetWindowPort(window));
1139 mouse = event->where;
1140 GlobalToLocal(&mouse);
1141 part = FindControl(mouse, window, &control);
1142 if (control != NULL) {
1143 #if !TARGET_API_MAC_CARBON
1145 * Special magic for scroll bars in list boxes, whose refcon
1148 if (part == kControlUpButtonPart || part == kControlDownButtonPart ||
1149 part == kControlPageUpPart || part == kControlPageDownPart ||
1150 part == kControlIndicatorPart)
1151 mc = (union macctrl *)
1152 (*(ListHandle)GetControlReference(control))->refCon;
1155 mc = (union macctrl *)GetControlReference(control);
1156 fprintf(stderr, "control = %p, part = %d, mc = %p\n",
1158 if (mac_gestalts.apprvers >= 0x100) {
1159 if (GetControlFeatures(control, &features) == noErr &&
1160 (features & kControlSupportsFocus) &&
1161 (features & kControlGetsFocusOnClick) &&
1162 GetKeyboardFocus(window, &oldfocus) == noErr &&
1163 control != oldfocus)
1164 SetKeyboardFocus(window, control, part);
1165 trackresult = HandleControlClick(control, mouse, event->modifiers,
1166 (ControlActionUPP)-1);
1168 #if !TARGET_API_MAC_CARBON
1169 if (mc->generic.type == MACCTRL_EDITBOX &&
1170 control == mc->editbox.tbctrl) {
1171 TEHandle te = (TEHandle)(*control)->contrlData;
1173 macctrl_setfocus(mcs, mc);
1174 TEClick(mouse, !!(event->modifiers & shiftKey), te);
1177 if (mc->generic.type == MACCTRL_LISTBOX &&
1178 (control == mc->listbox.tbctrl ||
1179 control == (*mc->listbox.list)->vScroll)) {
1181 fprintf(stderr, "list = %p\n", mc->listbox.list);
1182 macctrl_setfocus(mcs, mc);
1183 if (LClick(mouse, event->modifiers, mc->listbox.list))
1185 ctrlevent(mcs, mc, EVENT_ACTION);
1187 ctrlevent(mcs, mc, EVENT_SELCHANGE);
1191 trackresult = TrackControl(control, mouse, (ControlActionUPP)-1);
1193 switch (mc->generic.type) {
1195 if (trackresult != 0) {
1196 for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
1197 if (mc->radio.tbctrls[i] == control)
1198 SetControlValue(mc->radio.tbctrls[i],
1199 kControlRadioButtonCheckedValue);
1201 SetControlValue(mc->radio.tbctrls[i],
1202 kControlRadioButtonUncheckedValue);
1203 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1206 case MACCTRL_CHECKBOX:
1207 if (trackresult != 0) {
1208 SetControlValue(control, !GetControlValue(control));
1209 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1212 case MACCTRL_BUTTON:
1213 if (trackresult != 0)
1214 ctrlevent(mcs, mc, EVENT_ACTION);
1216 case MACCTRL_LISTBOX:
1217 /* FIXME spot double-click */
1218 ctrlevent(mcs, mc, EVENT_SELCHANGE);
1221 ctrlevent(mcs, mc, EVENT_SELCHANGE);
1229 void macctrl_key(WindowPtr window, EventRecord *event)
1232 struct macctrls *mcs = mac_winctrls(window);
1234 unsigned long dummy;
1236 switch (event->message & charCodeMask) {
1237 case kEnterCharCode:
1238 case kReturnCharCode:
1239 if (mcs->defbutton != NULL) {
1240 assert(mcs->defbutton->generic.type == MACCTRL_BUTTON);
1241 HiliteControl(mcs->defbutton->button.tbctrl, kControlButtonPart);
1243 * I'd like to delay unhilighting the button until after
1244 * the event has been processed, but by them the entire
1245 * dialgue box might have been destroyed.
1248 HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart);
1249 ctrlevent(mcs, mcs->defbutton, EVENT_ACTION);
1252 case kEscapeCharCode:
1253 if (mcs->canbutton != NULL) {
1254 assert(mcs->canbutton->generic.type == MACCTRL_BUTTON);
1255 HiliteControl(mcs->canbutton->button.tbctrl, kControlButtonPart);
1257 HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart);
1258 ctrlevent(mcs, mcs->canbutton, EVENT_ACTION);
1262 if (mac_gestalts.apprvers >= 0x100) {
1263 if (GetKeyboardFocus(window, &control) == noErr && control != NULL) {
1264 HandleControlKey(control, (event->message & keyCodeMask) >> 8,
1265 event->message & charCodeMask, event->modifiers);
1266 mc = (union macctrl *)GetControlReference(control);
1267 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1270 #if !TARGET_API_MAC_CARBON
1274 if (mcs->focus != NULL) {
1276 switch (mc->generic.type) {
1277 case MACCTRL_EDITBOX:
1278 te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1279 TEKey(event->message & charCodeMask, te);
1280 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1288 void macctrl_update(WindowPtr window)
1290 #if TARGET_API_MAC_CARBON
1296 BeginUpdate(window);
1298 SetPort((GrafPtr)GetWindowPort(window));
1299 if (mac_gestalts.apprvers >= 0x101) {
1300 #if TARGET_API_MAC_CARBON
1301 GetPortBounds(GetWindowPort(window), &rect);
1303 rect = window->portRect;
1305 InsetRect(&rect, -1, -1);
1306 DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ?
1307 kThemeStateActive : kThemeStateInactive);
1309 #if TARGET_API_MAC_CARBON
1311 GetPortVisibleRegion(GetWindowPort(window), visrgn);
1312 UpdateControls(window, visrgn);
1315 UpdateControls(window, window->visRgn);
1321 #if TARGET_API_MAC_CARBON
1322 #define EnableItem EnableMenuItem
1323 #define DisableItem DisableMenuItem
1325 void macctrl_adjustmenus(WindowPtr window)
1329 menu = GetMenuHandle(mFile);
1330 DisableItem(menu, iSave); /* XXX enable if modified */
1331 EnableItem(menu, iSaveAs);
1332 EnableItem(menu, iDuplicate);
1334 menu = GetMenuHandle(mEdit);
1335 DisableItem(menu, 0);
1338 void macctrl_close(WindowPtr window)
1340 struct macctrls *mcs = mac_winctrls(window);
1344 * Mostly, we don't bother disposing of the Toolbox controls,
1345 * since that will happen automatically when the window is
1346 * disposed of. Popup menus are an exception, because we have to
1347 * dispose of the menu ourselves, and doing that while the control
1348 * still holds a reference to it seems rude.
1350 while ((mc = index234(mcs->byctrl, 0)) != NULL) {
1351 if (mc->generic.privdata != NULL && mc->generic.freeprivdata)
1352 sfree(mc->generic.privdata);
1353 switch (mc->generic.type) {
1355 DisposeControl(mc->popup.tbctrl);
1356 DeleteMenu(mc->popup.menuid);
1357 DisposeMenu(mc->popup.menu);
1360 del234(mcs->byctrl, mc);
1364 freetree234(mcs->byctrl);
1370 void dlg_update_start(union control *ctrl, void *dlg)
1376 void dlg_update_done(union control *ctrl, void *dlg)
1382 void dlg_set_focus(union control *ctrl, void *dlg)
1385 if (mac_gestalts.apprvers >= 0x100) {
1386 /* Use SetKeyboardFocus() */
1388 /* Do our own mucking around */
1392 union control *dlg_last_focused(union control *ctrl, void *dlg)
1398 void dlg_beep(void *dlg)
1404 void dlg_error_msg(void *dlg, char *msg)
1408 c2pstrcpy(pmsg, msg);
1409 ParamText(pmsg, NULL, NULL, NULL);
1410 StopAlert(128, NULL);
1413 void dlg_end(void *dlg, int value)
1415 struct macctrls *mcs = dlg;
1417 if (mcs->end != NULL)
1418 (*mcs->end)(mcs->window, value);
1421 void dlg_refresh(union control *ctrl, void *dlg)
1423 struct macctrls *mcs = dlg;
1428 mc = findbyctrl(mcs, ctrl);
1430 ctrlevent(mcs, mc, EVENT_REFRESH);
1433 void *dlg_get_privdata(union control *ctrl, void *dlg)
1435 struct macctrls *mcs = dlg;
1436 union macctrl *mc = findbyctrl(mcs, ctrl);
1439 return mc->generic.privdata;
1442 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
1444 struct macctrls *mcs = dlg;
1445 union macctrl *mc = findbyctrl(mcs, ctrl);
1448 mc->generic.privdata = ptr;
1449 mc->generic.freeprivdata = FALSE;
1452 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
1454 struct macctrls *mcs = dlg;
1455 union macctrl *mc = findbyctrl(mcs, ctrl);
1458 mc->generic.privdata = smalloc(size);
1459 mc->generic.freeprivdata = TRUE;
1460 return mc->generic.privdata;
1465 * Radio Button control
1468 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
1470 struct macctrls *mcs = dlg;
1471 union macctrl *mc = findbyctrl(mcs, ctrl);
1474 if (mc == NULL) return;
1475 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1476 if (i == whichbutton)
1477 SetControlValue(mc->radio.tbctrls[i],
1478 kControlRadioButtonCheckedValue);
1480 SetControlValue(mc->radio.tbctrls[i],
1481 kControlRadioButtonUncheckedValue);
1486 int dlg_radiobutton_get(union control *ctrl, void *dlg)
1488 struct macctrls *mcs = dlg;
1489 union macctrl *mc = findbyctrl(mcs, ctrl);
1493 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1494 if (GetControlValue(mc->radio.tbctrls[i]) ==
1495 kControlRadioButtonCheckedValue)
1506 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
1508 struct macctrls *mcs = dlg;
1509 union macctrl *mc = findbyctrl(mcs, ctrl);
1511 if (mc == NULL) return;
1512 SetControlValue(mc->checkbox.tbctrl,
1513 checked ? kControlCheckBoxCheckedValue :
1514 kControlCheckBoxUncheckedValue);
1517 int dlg_checkbox_get(union control *ctrl, void *dlg)
1519 struct macctrls *mcs = dlg;
1520 union macctrl *mc = findbyctrl(mcs, ctrl);
1523 return GetControlValue(mc->checkbox.tbctrl);
1531 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
1533 struct macctrls *mcs = dlg;
1534 union macctrl *mc = findbyctrl(mcs, ctrl);
1537 if (mc == NULL) return;
1538 assert(mc->generic.type == MACCTRL_EDITBOX);
1540 SetPort((GrafPtr)(GetWindowPort(mcs->window)));
1541 if (mac_gestalts.apprvers >= 0x100)
1542 SetControlData(mc->editbox.tbctrl, kControlEntireControl,
1543 ctrl->editbox.password ?
1544 kControlEditTextPasswordTag :
1545 kControlEditTextTextTag,
1546 strlen(text), text);
1547 #if !TARGET_API_MAC_CARBON
1549 TESetText(text, strlen(text),
1550 (TEHandle)(*mc->editbox.tbctrl)->contrlData);
1552 DrawOneControl(mc->editbox.tbctrl);
1556 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
1558 struct macctrls *mcs = dlg;
1559 union macctrl *mc = findbyctrl(mcs, ctrl);
1563 assert(mc->generic.type == MACCTRL_EDITBOX);
1564 if (mac_gestalts.apprvers >= 0x100) {
1565 if (GetControlData(mc->editbox.tbctrl, kControlEntireControl,
1566 ctrl->editbox.password ?
1567 kControlEditTextPasswordTag :
1568 kControlEditTextTextTag,
1569 length - 1, buffer, &olen) != noErr)
1571 if (olen > length - 1)
1574 #if !TARGET_API_MAC_CARBON
1576 TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1578 olen = (*te)->teLength;
1579 if (olen > length - 1)
1581 memcpy(buffer, *(*te)->hText, olen);
1584 buffer[olen] = '\0';
1585 fprintf(stderr, "dlg_editbox_get: %s\n", buffer);
1593 static void dlg_macpopup_clear(union control *ctrl, void *dlg)
1595 struct macctrls *mcs = dlg;
1596 union macctrl *mc = findbyctrl(mcs, ctrl);
1597 MenuRef menu = mc->popup.menu;
1600 if (mc == NULL) return;
1601 fprintf(stderr, " popup_clear\n");
1602 n = CountMenuItems(menu);
1603 for (i = 0; i < n; i++)
1604 DeleteMenuItem(menu, n - i);
1606 sfree(mc->popup.ids);
1607 mc->popup.ids = NULL;
1608 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1611 static void dlg_maclist_clear(union control *ctrl, void *dlg)
1613 struct macctrls *mcs = dlg;
1614 union macctrl *mc = findbyctrl(mcs, ctrl);
1616 if (mc == NULL) return;
1617 fprintf(stderr, " maclist_clear\n");
1618 LDelRow(0, 0, mc->listbox.list);
1619 mc->listbox.nids = 0;
1620 sfree(mc->listbox.ids);
1621 mc->listbox.ids = NULL;
1622 DrawOneControl(mc->listbox.tbctrl);
1625 void dlg_listbox_clear(union control *ctrl, void *dlg)
1628 switch (ctrl->generic.type) {
1630 if (ctrl->listbox.height == 0)
1631 dlg_macpopup_clear(ctrl, dlg);
1633 dlg_maclist_clear(ctrl, dlg);
1638 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
1640 struct macctrls *mcs = dlg;
1641 union macctrl *mc = findbyctrl(mcs, ctrl);
1642 MenuRef menu = mc->popup.menu;
1644 if (mc == NULL) return;
1645 fprintf(stderr, " popup_del %d\n", index);
1646 DeleteMenuItem(menu, index + 1);
1647 if (mc->popup.ids != NULL)
1648 memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
1649 (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
1650 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1653 static void dlg_maclist_del(union control *ctrl, void *dlg, int index)
1655 struct macctrls *mcs = dlg;
1656 union macctrl *mc = findbyctrl(mcs, ctrl);
1658 if (mc == NULL) return;
1659 fprintf(stderr, " maclist_del %d\n", index);
1660 LDelRow(1, index, mc->listbox.list);
1661 if (mc->listbox.ids != NULL)
1662 memcpy(mc->listbox.ids + index, mc->listbox.ids + index + 1,
1663 (mc->listbox.nids - index - 1) * sizeof(*mc->listbox.ids));
1664 DrawOneControl(mc->listbox.tbctrl);
1667 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
1670 switch (ctrl->generic.type) {
1672 if (ctrl->listbox.height == 0)
1673 dlg_macpopup_del(ctrl, dlg, index);
1675 dlg_maclist_del(ctrl, dlg, index);
1680 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
1682 struct macctrls *mcs = dlg;
1683 union macctrl *mc = findbyctrl(mcs, ctrl);
1684 MenuRef menu = mc->popup.menu;
1687 if (mc == NULL) return;
1688 fprintf(stderr, " popup_add %s\n", text);
1689 assert(text[0] != '\0');
1690 c2pstrcpy(itemstring, text);
1691 AppendMenu(menu, "\pdummy");
1692 SetMenuItemText(menu, CountMenuItems(menu), itemstring);
1693 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1697 static void dlg_maclist_add(union control *ctrl, void *dlg, char const *text)
1699 struct macctrls *mcs = dlg;
1700 union macctrl *mc = findbyctrl(mcs, ctrl);
1702 Cell cell = { 0, 0 };
1704 if (mc == NULL) return;
1705 fprintf(stderr, " maclist_add %s\n", text);
1706 #if TARGET_API_MAC_CARBON
1707 GetListDataBounds(mc->listbox.list, &bounds);
1709 bounds = (*mc->listbox.list)->dataBounds;
1711 cell.v = bounds.bottom;
1712 LAddRow(1, cell.v, mc->listbox.list);
1713 LSetCell(text, strlen(text), cell, mc->listbox.list);
1714 DrawOneControl(mc->listbox.tbctrl);
1717 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
1720 switch (ctrl->generic.type) {
1722 if (ctrl->listbox.height == 0)
1723 dlg_macpopup_add(ctrl, dlg, text);
1725 dlg_maclist_add(ctrl, dlg, text);
1730 static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
1731 char const *text, int id)
1733 struct macctrls *mcs = dlg;
1734 union macctrl *mc = findbyctrl(mcs, ctrl);
1735 MenuRef menu = mc->popup.menu;
1738 if (mc == NULL) return;
1739 fprintf(stderr, " popup_addwthid %s, %d\n", text, id);
1740 dlg_macpopup_add(ctrl, dlg, text);
1741 index = CountMenuItems(menu) - 1;
1742 if (mc->popup.nids <= index) {
1743 mc->popup.nids = index + 1;
1744 mc->popup.ids = sresize(mc->popup.ids, mc->popup.nids, int);
1746 mc->popup.ids[index] = id;
1749 static void dlg_maclist_addwithid(union control *ctrl, void *dlg,
1750 char const *text, int id)
1752 struct macctrls *mcs = dlg;
1753 union macctrl *mc = findbyctrl(mcs, ctrl);
1757 if (mc == NULL) return;
1758 fprintf(stderr, " maclist_addwithid %s %d\n", text, id);
1759 dlg_maclist_add(ctrl, dlg, text);
1760 #if TARGET_API_MAC_CARBON
1761 GetListDataBounds(mc->listbox.list, &bounds);
1763 bounds = (*mc->listbox.list)->dataBounds;
1765 index = bounds.bottom;
1766 if (mc->listbox.nids <= index) {
1767 mc->listbox.nids = index + 1;
1768 mc->listbox.ids = sresize(mc->listbox.ids, mc->listbox.nids, int);
1770 mc->listbox.ids[index] = id;
1773 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
1774 char const *text, int id)
1777 switch (ctrl->generic.type) {
1779 if (ctrl->listbox.height == 0)
1780 dlg_macpopup_addwithid(ctrl, dlg, text, id);
1782 dlg_maclist_addwithid(ctrl, dlg, text, id);
1787 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
1789 struct macctrls *mcs = dlg;
1790 union macctrl *mc = findbyctrl(mcs, ctrl);
1793 switch (ctrl->generic.type) {
1795 if (ctrl->listbox.height == 0) {
1796 assert(mc->popup.ids != NULL && mc->popup.nids > index);
1797 return mc->popup.ids[index];
1799 assert(mc->listbox.ids != NULL && mc->listbox.nids > index);
1800 return mc->listbox.ids[index];
1806 int dlg_listbox_index(union control *ctrl, void *dlg)
1808 struct macctrls *mcs = dlg;
1809 union macctrl *mc = findbyctrl(mcs, ctrl);
1810 Cell cell = { 0, 0 };
1813 switch (ctrl->generic.type) {
1815 if (ctrl->listbox.height == 0)
1816 return GetControlValue(mc->popup.tbctrl) - 1;
1818 if (LGetSelect(TRUE, &cell, mc->listbox.list))
1827 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1829 struct macctrls *mcs = dlg;
1830 union macctrl *mc = findbyctrl(mcs, ctrl);
1831 Cell cell = { 0, 0 };
1834 switch (ctrl->generic.type) {
1836 if (ctrl->listbox.height == 0)
1837 return GetControlValue(mc->popup.tbctrl) - 1 == index;
1840 return LGetSelect(FALSE, &cell, mc->listbox.list);
1846 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1848 struct macctrls *mcs = dlg;
1849 union macctrl *mc = findbyctrl(mcs, ctrl);
1851 if (mc == NULL) return;
1852 switch (ctrl->generic.type) {
1854 if (ctrl->listbox.height == 0)
1855 SetControlValue(mc->popup.tbctrl, index + 1);
1865 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1867 struct macctrls *mcs = dlg;
1868 union macctrl *mc = findbyctrl(mcs, ctrl);
1870 if (mc == NULL) return;
1871 if (mac_gestalts.apprvers >= 0x100)
1872 SetControlData(mc->text.tbctrl, kControlEntireControl,
1873 kControlStaticTextTextTag, strlen(text), text);
1874 #if !TARGET_API_MAC_CARBON
1876 TESetText(text, strlen(text),
1877 (TEHandle)(*mc->text.tbctrl)->contrlData);
1883 * File Selector control
1886 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1891 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1898 * Font Selector control
1901 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1906 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1913 * Printer enumeration
1916 printer_enum *printer_start_enum(int *nprinters)
1923 char *printer_get_name(printer_enum *pe, int thing)
1929 void printer_finish_enum(printer_enum *pe)
1936 * Colour selection stuff
1939 void dlg_coloursel_start(union control *ctrl, void *dlg,
1940 int r, int g, int b)
1945 int dlg_coloursel_results(union control *ctrl, void *dlg,
1946 int *r, int *g, int *b)
1954 * c-file-style: "simon"