1 /* $Id: macctrls.c,v 1.12 2003/03/24 22:46:11 ben Exp $ */
3 * Copyright (c) 2003 Ben Harris
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <Appearance.h>
31 #include <ControlDefinitions.h>
33 #include <Resources.h>
35 #include <TextUtils.h>
47 /* Range of menu IDs for popup menus */
53 struct macctrl_generic {
61 /* Template from which this was generated */
63 /* Next control in this panel */
67 struct macctrl_generic generic;
71 struct macctrl_generic generic;
75 struct macctrl_generic generic;
79 struct macctrl_generic generic;
83 struct macctrl_generic generic;
92 struct mac_layoutstate {
95 unsigned int panelnum;
98 #define ctrlevent(mcs, mc, event) do { \
99 if ((mc)->generic.ctrl->generic.handler != NULL) \
100 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
101 (mcs)->data, (event)); \
104 #define findbyctrl(mcs, ctrl) \
105 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
107 static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *,
108 WindowPtr, struct macctrls *);
109 static void macctrl_switchtopanel(struct macctrls *, unsigned int);
110 static void macctrl_text(struct macctrls *, WindowPtr,
111 struct mac_layoutstate *, union control *);
112 static void macctrl_radio(struct macctrls *, WindowPtr,
113 struct mac_layoutstate *, union control *);
114 static void macctrl_checkbox(struct macctrls *, WindowPtr,
115 struct mac_layoutstate *, union control *);
116 static void macctrl_button(struct macctrls *, WindowPtr,
117 struct mac_layoutstate *, union control *);
118 static void macctrl_popup(struct macctrls *, WindowPtr,
119 struct mac_layoutstate *, union control *);
120 #if !TARGET_API_MAC_CARBON
121 static pascal SInt32 macctrl_sys7_text_cdef(SInt16, ControlRef,
122 ControlDefProcMessage, SInt32);
123 static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
124 ControlDefProcMessage, SInt32);
127 #if !TARGET_API_MAC_CARBON
129 * This trick enables us to keep all the CDEF code in the main
130 * application, which makes life easier. For details, see
131 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
134 #pragma options align=mac68k
136 short jmpabs; /* 4EF9 */
137 ControlDefUPP theUPP;
139 #pragma options align=reset
142 static void macctrl_init()
144 #if !TARGET_API_MAC_CARBON
145 static int inited = 0;
149 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Text);
150 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_text_cdef);
151 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
152 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
158 static int macctrl_cmp_byctrl(void *av, void *bv)
160 union macctrl *a = (union macctrl *)av;
161 union macctrl *b = (union macctrl *)bv;
163 if (a->generic.ctrl < b->generic.ctrl)
165 else if (a->generic.ctrl > b->generic.ctrl)
171 static int macctrl_cmp_byctrl_find(void *av, void *bv)
173 union control *a = (union control *)av;
174 union macctrl *b = (union macctrl *)bv;
176 if (a < b->generic.ctrl)
178 else if (a > b->generic.ctrl)
184 void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
185 struct macctrls *mcs)
188 struct mac_layoutstate curstate;
193 #if TARGET_API_MAC_CARBON
194 GetPortBounds(GetWindowPort(window), &rect);
196 rect = window->portRect;
198 curstate.pos.h = rect.left + 13;
199 curstate.pos.v = rect.bottom - 59;
200 curstate.width = rect.right - rect.left - (13 * 2);
201 if (mac_gestalts.apprvers >= 0x100)
202 CreateRootControl(window, &root);
203 mcs->byctrl = newtree234(macctrl_cmp_byctrl);
204 /* Count the number of panels */
206 for (i = 1; i < cb->nctrlsets; i++)
207 if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
209 mcs->panels = smalloc(sizeof(*mcs->panels) * mcs->npanels);
210 memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
211 curstate.panelnum = 0;
212 for (i = 0; i < cb->nctrlsets; i++) {
213 if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
214 cb->ctrlsets[i-1]->pathname)) {
215 curstate.pos.v = rect.top + 13;
217 assert(curstate.panelnum < mcs->npanels);
219 macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
221 macctrl_switchtopanel(mcs, 20);
224 static void macctrl_layoutset(struct mac_layoutstate *curstate,
225 struct controlset *s,
226 WindowPtr window, struct macctrls *mcs)
230 fprintf(stderr, "--- begin set ---\n");
231 fprintf(stderr, "pathname = %s\n", s->pathname);
232 if (s->boxname && *s->boxname)
233 fprintf(stderr, "boxname = %s\n", s->boxname);
235 fprintf(stderr, "boxtitle = %s\n", s->boxtitle);
238 for (i = 0; i < s->ncontrols; i++) {
239 union control *ctrl = s->ctrls[i];
242 switch (ctrl->generic.type) {
243 case CTRL_TEXT: s = "text"; break;
244 case CTRL_EDITBOX: s = "editbox"; break;
245 case CTRL_RADIO: s = "radio"; break;
246 case CTRL_CHECKBOX: s = "checkbox"; break;
247 case CTRL_BUTTON: s = "button"; break;
248 case CTRL_LISTBOX: s = "listbox"; break;
249 case CTRL_COLUMNS: s = "columns"; break;
250 case CTRL_FILESELECT: s = "fileselect"; break;
251 case CTRL_FONTSELECT: s = "fontselect"; break;
252 case CTRL_TABDELAY: s = "tabdelay"; break;
253 default: s = "unknown"; break;
255 fprintf(stderr, " control: %s\n", s);
256 switch (ctrl->generic.type) {
258 macctrl_text(mcs, window, curstate, ctrl);
261 macctrl_radio(mcs, window, curstate, ctrl);
264 macctrl_checkbox(mcs, window, curstate, ctrl);
267 macctrl_button(mcs, window, curstate, ctrl);
270 if (ctrl->listbox.height == 0)
271 macctrl_popup(mcs, window, curstate, ctrl);
277 static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
282 #define hideshow(c) do { \
283 if (i == which) ShowControl(c); else HideControl(c); \
286 mcs->curpanel = which;
287 /* Panel 0 is special and always visible. */
288 for (i = 1; i < mcs->npanels; i++)
289 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next)
290 switch (mc->generic.type) {
292 hideshow(mc->text.tbctrl);
295 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
296 hideshow(mc->radio.tbctrls[j]);
298 case MACCTRL_CHECKBOX:
299 hideshow(mc->checkbox.tbctrl);
302 hideshow(mc->button.tbctrl);
305 hideshow(mc->popup.tbctrl);
310 static void macctrl_text(struct macctrls *mcs, WindowPtr window,
311 struct mac_layoutstate *curstate,
314 union macctrl *mc = smalloc(sizeof *mc);
317 fprintf(stderr, " label = %s\n", ctrl->text.label);
318 mc->generic.type = MACCTRL_TEXT;
319 mc->generic.ctrl = ctrl;
320 bounds.left = curstate->pos.h;
321 bounds.right = bounds.left + curstate->width;
322 bounds.top = curstate->pos.v;
323 bounds.bottom = bounds.top + 16;
324 if (mac_gestalts.apprvers >= 0x100) {
328 mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
329 kControlStaticTextProc, (long)mc);
330 SetControlData(mc->text.tbctrl, kControlEntireControl,
331 kControlStaticTextTextTag,
332 strlen(ctrl->text.label), ctrl->text.label);
333 GetControlData(mc->text.tbctrl, kControlEntireControl,
334 kControlStaticTextTextHeightTag,
335 sizeof(height), &height, &olen);
336 fprintf(stderr, " height = %d\n", height);
337 SizeControl(mc->text.tbctrl, curstate->width, height);
338 curstate->pos.v += height + 6;
342 c2pstrcpy(title, ctrl->text.label);
343 mc->text.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 0,
344 SYS7_TEXT_PROC, (long)mc);
346 add234(mcs->byctrl, mc);
347 mc->generic.next = mcs->panels[curstate->panelnum];
348 mcs->panels[curstate->panelnum] = mc;
351 #if !TARGET_API_MAC_CARBON
352 static pascal SInt32 macctrl_sys7_text_cdef(SInt16 variant, ControlRef control,
353 ControlDefProcMessage msg, SInt32 param)
359 if ((*control)->contrlVis)
360 TETextBox((*control)->contrlTitle + 1, (*control)->contrlTitle[0],
361 &(*control)->contrlRect, teFlushDefault);
364 if (param & (1 << 31)) {
370 rgn = (RgnHandle)param;
371 RectRgn(rgn, &(*control)->contrlRect);
375 rgn = (RgnHandle)param;
384 static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
385 struct mac_layoutstate *curstate,
388 union macctrl *mc = smalloc(sizeof *mc);
391 unsigned int i, colwidth;
393 fprintf(stderr, " label = %s\n", ctrl->radio.label);
394 mc->generic.type = MACCTRL_RADIO;
395 mc->generic.ctrl = ctrl;
397 smalloc(sizeof(*mc->radio.tbctrls) * ctrl->radio.nbuttons);
398 colwidth = (curstate->width + 13) / ctrl->radio.ncolumns;
399 for (i = 0; i < ctrl->radio.nbuttons; i++) {
400 fprintf(stderr, " button = %s\n", ctrl->radio.buttons[i]);
401 bounds.top = curstate->pos.v - 2;
402 bounds.bottom = bounds.top + 18;
403 bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
404 if (i == ctrl->radio.nbuttons - 1 ||
405 i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
406 bounds.right = curstate->pos.h + curstate->width;
407 curstate->pos.v += 18;
409 bounds.right = bounds.left + colwidth - 13;
410 c2pstrcpy(title, ctrl->radio.buttons[i]);
411 mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE,
412 0, 0, 1, radioButProc, (long)mc);
414 curstate->pos.v += 4;
415 add234(mcs->byctrl, mc);
416 mc->generic.next = mcs->panels[curstate->panelnum];
417 mcs->panels[curstate->panelnum] = mc;
418 ctrlevent(mcs, mc, EVENT_REFRESH);
421 static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
422 struct mac_layoutstate *curstate,
425 union macctrl *mc = smalloc(sizeof *mc);
429 fprintf(stderr, " label = %s\n", ctrl->checkbox.label);
430 mc->generic.type = MACCTRL_CHECKBOX;
431 mc->generic.ctrl = ctrl;
432 bounds.left = curstate->pos.h;
433 bounds.right = bounds.left + curstate->width;
434 bounds.top = curstate->pos.v;
435 bounds.bottom = bounds.top + 16;
436 c2pstrcpy(title, ctrl->checkbox.label);
437 mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
438 checkBoxProc, (long)mc);
439 add234(mcs->byctrl, mc);
440 curstate->pos.v += 22;
441 mc->generic.next = mcs->panels[curstate->panelnum];
442 mcs->panels[curstate->panelnum] = mc;
443 ctrlevent(mcs, mc, EVENT_REFRESH);
446 static void macctrl_button(struct macctrls *mcs, WindowPtr window,
447 struct mac_layoutstate *curstate,
450 union macctrl *mc = smalloc(sizeof *mc);
454 fprintf(stderr, " label = %s\n", ctrl->button.label);
455 if (ctrl->button.isdefault)
456 fprintf(stderr, " is default\n");
457 mc->generic.type = MACCTRL_BUTTON;
458 mc->generic.ctrl = ctrl;
459 bounds.left = curstate->pos.h;
460 bounds.right = bounds.left + 100; /* XXX measure string */
461 bounds.top = curstate->pos.v;
462 bounds.bottom = bounds.top + 20;
463 c2pstrcpy(title, ctrl->button.label);
464 mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
465 pushButProc, (long)mc);
466 if (mac_gestalts.apprvers >= 0x100) {
467 Boolean isdefault = ctrl->button.isdefault;
469 SetControlData(mc->button.tbctrl, kControlEntireControl,
470 kControlPushButtonDefaultTag,
471 sizeof(isdefault), &isdefault);
472 } else if (ctrl->button.isdefault) {
473 InsetRect(&bounds, -4, -4);
474 NewControl(window, &bounds, title, TRUE, 0, 0, 1,
475 SYS7_DEFAULT_PROC, (long)mc);
477 if (mac_gestalts.apprvers >= 0x110) {
478 Boolean iscancel = ctrl->button.iscancel;
480 SetControlData(mc->button.tbctrl, kControlEntireControl,
481 kControlPushButtonCancelTag,
482 sizeof(iscancel), &iscancel);
484 add234(mcs->byctrl, mc);
485 mc->generic.next = mcs->panels[curstate->panelnum];
486 mcs->panels[curstate->panelnum] = mc;
487 curstate->pos.v += 26;
490 #if !TARGET_API_MAC_CARBON
491 static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
493 ControlDefProcMessage msg,
502 if ((*control)->contrlVis) {
503 rect = (*control)->contrlRect;
506 oval = (rect.bottom - rect.top) / 2 + 2;
507 FrameRoundRect(&rect, oval, oval);
511 if (param & (1 << 31)) {
517 rgn = (RgnHandle)param;
518 RectRgn(rgn, &(*control)->contrlRect);
522 rgn = (RgnHandle)param;
531 static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
532 struct mac_layoutstate *curstate,
535 union macctrl *mc = smalloc(sizeof *mc);
538 unsigned int labelwidth;
539 static int nextmenuid = MENU_MIN;
544 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
545 * create a popup menu with dynamic content.
547 assert(ctrl->listbox.height == 0);
548 assert(!ctrl->listbox.draglist);
549 assert(!ctrl->listbox.multisel);
551 fprintf(stderr, " label = %s\n", ctrl->listbox.label);
552 fprintf(stderr, " percentwidth = %d\n", ctrl->listbox.percentwidth);
554 mc->generic.type = MACCTRL_POPUP;
555 mc->generic.ctrl = ctrl;
556 c2pstrcpy(title, ctrl->button.label);
558 /* Find a spare menu ID and create the menu */
559 while (GetMenuHandle(nextmenuid) != NULL)
560 if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
561 menuid = nextmenuid++;
562 menu = NewMenu(menuid, "\pdummy");
563 if (menu == NULL) return;
564 mc->popup.menu = menu;
565 mc->popup.menuid = menuid;
566 InsertMenu(menu, kInsertHierarchicalMenu);
568 /* The menu starts off empty */
570 mc->popup.ids = NULL;
572 bounds.left = curstate->pos.h;
573 bounds.right = bounds.left + curstate->width;
574 bounds.top = curstate->pos.v;
575 bounds.bottom = bounds.top + 20;
576 /* XXX handle percentwidth == 100 */
577 labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
578 mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE,
579 popupTitleLeftJust, menuid, labelwidth,
580 popupMenuProc + popupFixedWidth, (long)mc);
581 add234(mcs->byctrl, mc);
582 curstate->pos.v += 26;
583 mc->generic.next = mcs->panels[curstate->panelnum];
584 mcs->panels[curstate->panelnum] = mc;
585 ctrlevent(mcs, mc, EVENT_REFRESH);
589 void macctrl_activate(WindowPtr window, EventRecord *event)
591 struct macctrls *mcs = mac_winctrls(window);
592 Boolean active = (event->modifiers & activeFlag) != 0;
595 ControlPartCode state;
599 SetPort((GrafPtr)GetWindowPort(window));
600 if (mac_gestalts.apprvers >= 0x100)
601 SetThemeWindowBackground(window, active ?
602 kThemeBrushModelessDialogBackgroundActive :
603 kThemeBrushModelessDialogBackgroundInactive,
605 state = active ? kControlNoPart : kControlInactivePart;
606 for (i = 0; i <= mcs->curpanel; i += mcs->curpanel)
607 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next)
608 switch (mc->generic.type) {
610 HiliteControl(mc->text.tbctrl, state);
613 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
614 HiliteControl(mc->radio.tbctrls[j], state);
616 case MACCTRL_CHECKBOX:
617 HiliteControl(mc->checkbox.tbctrl, state);
620 HiliteControl(mc->button.tbctrl, state);
623 HiliteControl(mc->popup.tbctrl, state);
629 void macctrl_click(WindowPtr window, EventRecord *event)
632 ControlHandle control;
636 struct macctrls *mcs = mac_winctrls(window);
640 SetPort((GrafPtr)GetWindowPort(window));
641 mouse = event->where;
642 GlobalToLocal(&mouse);
643 part = FindControl(mouse, window, &control);
644 if (control != NULL) {
645 mc = (union macctrl *)GetControlReference(control);
646 switch (mc->generic.type) {
648 TrackControl(control, mouse, (ControlActionUPP)-1);
649 ctrlevent(mcs, mc, EVENT_SELCHANGE);
651 if (TrackControl(control, mouse, NULL) != 0) {
652 for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
653 if (mc->radio.tbctrls[i] == control)
654 SetControlValue(mc->radio.tbctrls[i],
655 kControlRadioButtonCheckedValue);
657 SetControlValue(mc->radio.tbctrls[i],
658 kControlRadioButtonUncheckedValue);
659 ctrlevent(mcs, mc, EVENT_VALCHANGE);
662 case MACCTRL_CHECKBOX:
663 if (TrackControl(control, mouse, NULL) != 0) {
664 SetControlValue(control, !GetControlValue(control));
665 ctrlevent(mcs, mc, EVENT_VALCHANGE);
669 if (TrackControl(control, mouse, NULL) != 0)
670 ctrlevent(mcs, mc, EVENT_ACTION);
677 void macctrl_update(WindowPtr window)
679 #if TARGET_API_MAC_CARBON
687 SetPort((GrafPtr)GetWindowPort(window));
688 if (mac_gestalts.apprvers >= 0x101) {
689 #if TARGET_API_MAC_CARBON
690 GetPortBounds(GetWindowPort(window), &rect);
692 rect = window->portRect;
694 InsetRect(&rect, -1, -1);
695 DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ?
696 kThemeStateActive : kThemeStateInactive);
698 #if TARGET_API_MAC_CARBON
700 GetPortVisibleRegion(GetWindowPort(window), visrgn);
701 UpdateControls(window, visrgn);
704 UpdateControls(window, window->visRgn);
710 #if TARGET_API_MAC_CARBON
711 #define EnableItem EnableMenuItem
712 #define DisableItem DisableMenuItem
714 void macctrl_adjustmenus(WindowPtr window)
718 menu = GetMenuHandle(mFile);
719 DisableItem(menu, iSave); /* XXX enable if modified */
720 EnableItem(menu, iSaveAs);
721 EnableItem(menu, iDuplicate);
723 menu = GetMenuHandle(mEdit);
724 DisableItem(menu, 0);
727 void macctrl_close(WindowPtr window)
729 struct macctrls *mcs = mac_winctrls(window);
733 * Mostly, we don't bother disposing of the Toolbox controls,
734 * since that will happen automatically when the window is
735 * disposed of. Popup menus are an exception, because we have to
736 * dispose of the menu ourselves, and doing that while the control
737 * still holds a reference to it seems rude.
739 while ((mc = index234(mcs->byctrl, 0)) != NULL) {
740 switch (mc->generic.type) {
742 DisposeControl(mc->popup.tbctrl);
743 DeleteMenu(mc->popup.menuid);
744 DisposeMenu(mc->popup.menu);
747 del234(mcs->byctrl, mc);
751 freetree234(mcs->byctrl);
757 void dlg_update_start(union control *ctrl, void *dlg)
763 void dlg_update_done(union control *ctrl, void *dlg)
769 void dlg_set_focus(union control *ctrl, void *dlg)
772 if (mac_gestalts.apprvers >= 0x100) {
773 /* Use SetKeyboardFocus() */
775 /* Do our own mucking around */
779 union control *dlg_last_focused(union control *ctrl, void *dlg)
785 void dlg_beep(void *dlg)
791 void dlg_error_msg(void *dlg, char *msg)
795 c2pstrcpy(pmsg, msg);
796 ParamText(pmsg, NULL, NULL, NULL);
797 StopAlert(128, NULL);
800 void dlg_end(void *dlg, int value)
805 void dlg_refresh(union control *ctrl, void *dlg)
810 void *dlg_get_privdata(union control *ctrl, void *dlg)
816 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
819 fatalbox("dlg_set_privdata");
822 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
825 fatalbox("dlg_alloc_privdata");
830 * Radio Button control
833 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
835 struct macctrls *mcs = dlg;
836 union macctrl *mc = findbyctrl(mcs, ctrl);
840 for (i = 0; i < ctrl->radio.nbuttons; i++) {
841 if (i == whichbutton)
842 SetControlValue(mc->radio.tbctrls[i],
843 kControlRadioButtonCheckedValue);
845 SetControlValue(mc->radio.tbctrls[i],
846 kControlRadioButtonUncheckedValue);
851 int dlg_radiobutton_get(union control *ctrl, void *dlg)
853 struct macctrls *mcs = dlg;
854 union macctrl *mc = findbyctrl(mcs, ctrl);
858 for (i = 0; i < ctrl->radio.nbuttons; i++) {
859 if (GetControlValue(mc->radio.tbctrls[i]) ==
860 kControlRadioButtonCheckedValue)
871 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
873 struct macctrls *mcs = dlg;
874 union macctrl *mc = findbyctrl(mcs, ctrl);
877 SetControlValue(mc->checkbox.tbctrl,
878 checked ? kControlCheckBoxCheckedValue :
879 kControlCheckBoxUncheckedValue);
882 int dlg_checkbox_get(union control *ctrl, void *dlg)
884 struct macctrls *mcs = dlg;
885 union macctrl *mc = findbyctrl(mcs, ctrl);
888 return GetControlValue(mc->checkbox.tbctrl);
896 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
901 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
911 static void dlg_macpopup_clear(union control *ctrl, void *dlg)
913 struct macctrls *mcs = dlg;
914 union macctrl *mc = findbyctrl(mcs, ctrl);
915 MenuRef menu = mc->popup.menu;
918 fprintf(stderr, " popup_clear\n");
919 n = CountMItems(menu);
920 for (i = 0; i < n; i++)
921 DeleteMenuItem(menu, n - i);
923 sfree(mc->popup.ids);
924 mc->popup.ids = NULL;
925 SetControlMaximum(mc->popup.tbctrl, CountMItems(menu));
928 void dlg_listbox_clear(union control *ctrl, void *dlg)
931 if (ctrl->listbox.height == 0)
932 dlg_macpopup_clear(ctrl, dlg);
935 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
937 struct macctrls *mcs = dlg;
938 union macctrl *mc = findbyctrl(mcs, ctrl);
939 MenuRef menu = mc->popup.menu;
941 fprintf(stderr, " popup_del %d\n", index);
942 DeleteMenuItem(menu, index + 1);
943 if (mc->popup.ids != NULL)
944 memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
945 (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
946 SetControlMaximum(mc->popup.tbctrl, CountMItems(menu));
949 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
952 if (ctrl->listbox.height == 0)
953 dlg_macpopup_del(ctrl, dlg, index);
956 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
958 struct macctrls *mcs = dlg;
959 union macctrl *mc = findbyctrl(mcs, ctrl);
960 MenuRef menu = mc->popup.menu;
963 fprintf(stderr, " popup_add %s\n", text);
964 assert(text[0] != '\0');
965 c2pstrcpy(itemstring, text);
966 AppendMenu(menu, "\pdummy");
967 SetMenuItemText(menu, CountMItems(menu), itemstring);
968 SetControlMaximum(mc->popup.tbctrl, CountMItems(menu));
971 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
974 if (ctrl->listbox.height == 0)
975 dlg_macpopup_add(ctrl, dlg, text);
978 static void dlg_macpopup_addwithindex(union control *ctrl, void *dlg,
979 char const *text, int id)
981 struct macctrls *mcs = dlg;
982 union macctrl *mc = findbyctrl(mcs, ctrl);
983 MenuRef menu = mc->popup.menu;
986 fprintf(stderr, " popup_addwthindex %s, %d\n", text, id);
987 dlg_macpopup_add(ctrl, dlg, text);
988 index = CountMItems(menu) - 1;
989 if (mc->popup.nids <= index) {
990 mc->popup.nids = index + 1;
991 mc->popup.ids = srealloc(mc->popup.ids,
992 mc->popup.nids * sizeof(*mc->popup.ids));
994 mc->popup.ids[index] = id;
997 void dlg_listbox_addwithindex(union control *ctrl, void *dlg,
998 char const *text, int id)
1001 if (ctrl->listbox.height == 0)
1002 dlg_macpopup_addwithindex(ctrl, dlg, text, id);
1005 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
1007 struct macctrls *mcs = dlg;
1008 union macctrl *mc = findbyctrl(mcs, ctrl);
1010 if (ctrl->listbox.height == 0) {
1011 assert(mc->popup.ids != NULL && mc->popup.nids > index);
1012 return mc->popup.ids[index];
1017 int dlg_listbox_index(union control *ctrl, void *dlg)
1019 struct macctrls *mcs = dlg;
1020 union macctrl *mc = findbyctrl(mcs, ctrl);
1022 if (ctrl->listbox.height == 0)
1023 return GetControlValue(mc->popup.tbctrl) - 1;
1027 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1029 struct macctrls *mcs = dlg;
1030 union macctrl *mc = findbyctrl(mcs, ctrl);
1032 if (ctrl->listbox.height == 0)
1033 return GetControlValue(mc->popup.tbctrl) - 1 == index;
1037 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1039 struct macctrls *mcs = dlg;
1040 union macctrl *mc = findbyctrl(mcs, ctrl);
1042 if (ctrl->listbox.height == 0)
1043 SetControlValue(mc->popup.tbctrl, index + 1);
1051 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1053 struct macctrls *mcs = dlg;
1054 union macctrl *mc = findbyctrl(mcs, ctrl);
1058 if (mac_gestalts.apprvers >= 0x100)
1059 SetControlData(mc->text.tbctrl, kControlEntireControl,
1060 kControlStaticTextTextTag,
1061 strlen(ctrl->text.label), ctrl->text.label);
1063 c2pstrcpy(title, text);
1064 SetControlTitle(mc->text.tbctrl, title);
1070 * File Selector control
1073 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1078 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1085 * Font Selector control
1088 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1093 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1100 * Printer enumeration
1103 printer_enum *printer_start_enum(int *nprinters)
1110 char *printer_get_name(printer_enum *pe, int thing)
1116 void printer_finish_enum(printer_enum *pe)
1123 * Colour selection stuff
1126 void dlg_coloursel_start(union control *ctrl, void *dlg,
1127 int r, int g, int b)
1132 int dlg_coloursel_results(union control *ctrl, void *dlg,
1133 int *r, int *g, int *b)
1141 * c-file-style: "simon"