]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac/macctrls.c
4b6c382efecaf87fcd0a941eec06938a1d6f6de1
[PuTTY.git] / mac / macctrls.c
1 /* $Id: macctrls.c,v 1.19 2003/03/29 20:16:51 ben Exp $ */
2 /*
3  * Copyright (c) 2003 Ben Harris
4  * All rights reserved.
5  *
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
13  * conditions:
14  * 
15  * The above copyright notice and this permission notice shall be
16  * included in all copies or substantial portions of the Software.
17  * 
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
25  * SOFTWARE.
26  */
27
28 #include <MacTypes.h>
29 #include <Appearance.h>
30 #include <Controls.h>
31 #include <ControlDefinitions.h>
32 #include <Menus.h>
33 #include <Resources.h>
34 #include <Script.h>
35 #include <Sound.h>
36 #include <TextEdit.h>
37 #include <TextUtils.h>
38 #include <ToolUtils.h>
39 #include <Windows.h>
40
41 #include <assert.h>
42 #include <string.h>
43
44 #include "putty.h"
45 #include "mac.h"
46 #include "macresid.h"
47 #include "dialog.h"
48 #include "tree234.h"
49
50 /* Range of menu IDs for popup menus */
51 #define MENU_MIN        1024
52 #define MENU_MAX        2048
53
54
55 union macctrl {
56     struct macctrl_generic {
57         enum {
58             MACCTRL_TEXT,
59             MACCTRL_EDITBOX,
60             MACCTRL_RADIO,
61             MACCTRL_CHECKBOX,
62             MACCTRL_BUTTON,
63             MACCTRL_POPUP
64         } type;
65         /* Template from which this was generated */
66         union control *ctrl;
67         /* Next control in this panel */
68         union macctrl *next;
69         void *privdata;
70         int freeprivdata;
71     } generic;
72     struct {
73         struct macctrl_generic generic;
74         ControlRef tbctrl;
75     } text;
76     struct {
77         struct macctrl_generic generic;
78         ControlRef tbctrl;
79         ControlRef tblabel;
80     } editbox;
81     struct {
82         struct macctrl_generic generic;
83         ControlRef *tbctrls;
84         ControlRef tblabel;
85     } radio;
86     struct {
87         struct macctrl_generic generic;
88         ControlRef tbctrl;
89     } checkbox;
90     struct {
91         struct macctrl_generic generic;
92         ControlRef tbctrl;
93     } button;
94     struct {
95         struct macctrl_generic generic;
96         ControlRef tbctrl;
97         MenuRef menu;
98         int menuid;
99         unsigned int nids;
100         int *ids;
101     } popup;
102 };
103
104 struct mac_layoutstate {
105     Point pos;
106     unsigned int width;
107     unsigned int panelnum;
108 };
109
110 #define ctrlevent(mcs, mc, event) do {                                  \
111     if ((mc)->generic.ctrl->generic.handler != NULL)                    \
112         (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
113                                                (mcs)->data, (event));   \
114 } while (0)
115
116 #define findbyctrl(mcs, ctrl)                                           \
117     find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
118
119 static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *, 
120                               WindowPtr, struct macctrls *);
121 static void macctrl_switchtopanel(struct macctrls *, unsigned int);
122 static void macctrl_setfocus(struct macctrls *, union macctrl *);
123 static void macctrl_text(struct macctrls *, WindowPtr,
124                          struct mac_layoutstate *, union control *);
125 static void macctrl_editbox(struct macctrls *, WindowPtr,
126                             struct mac_layoutstate *, union control *);
127 static void macctrl_radio(struct macctrls *, WindowPtr,
128                           struct mac_layoutstate *, union control *);
129 static void macctrl_checkbox(struct macctrls *, WindowPtr,
130                              struct mac_layoutstate *, union control *);
131 static void macctrl_button(struct macctrls *, WindowPtr,
132                            struct mac_layoutstate *, union control *);
133 static void macctrl_popup(struct macctrls *, WindowPtr,
134                           struct mac_layoutstate *, union control *);
135 #if !TARGET_API_MAC_CARBON
136 static pascal SInt32 macctrl_sys7_text_cdef(SInt16, ControlRef,
137                                             ControlDefProcMessage, SInt32);
138 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef,
139                                                ControlDefProcMessage, SInt32);
140 static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
141                                                ControlDefProcMessage, SInt32);
142 #endif
143
144 #if !TARGET_API_MAC_CARBON
145 /*
146  * This trick enables us to keep all the CDEF code in the main
147  * application, which makes life easier.  For details, see
148  * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
149  */
150
151 #pragma options align=mac68k
152 typedef struct {
153     short               jmpabs; /* 4EF9 */
154     ControlDefUPP       theUPP;
155 } **PatchCDEF;
156 #pragma options align=reset
157 #endif
158
159 static void macctrl_init()
160 {
161 #if !TARGET_API_MAC_CARBON
162     static int inited = 0;
163     PatchCDEF cdef;
164
165     if (inited) return;
166     cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Text);
167     (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_text_cdef);
168     cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox);
169     (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef);
170     cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
171     (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
172     inited = 1;
173 #endif
174 }
175
176
177 static int macctrl_cmp_byctrl(void *av, void *bv)
178 {
179     union macctrl *a = (union macctrl *)av;
180     union macctrl *b = (union macctrl *)bv;
181
182     if (a->generic.ctrl < b->generic.ctrl)
183         return -1;
184     else if (a->generic.ctrl > b->generic.ctrl)
185         return +1;
186     else
187         return 0;
188 }
189
190 static int macctrl_cmp_byctrl_find(void *av, void *bv)
191 {
192     union control *a = (union control *)av;
193     union macctrl *b = (union macctrl *)bv;
194
195     if (a < b->generic.ctrl)
196         return -1;
197     else if (a > b->generic.ctrl)
198         return +1;
199     else
200         return 0;
201 }
202
203 void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
204                        struct macctrls *mcs)
205 {
206     int i;
207     struct mac_layoutstate curstate;
208     ControlRef root;
209     Rect rect;
210
211     macctrl_init();
212 #if TARGET_API_MAC_CARBON
213     GetPortBounds(GetWindowPort(window), &rect);
214 #else
215     rect = window->portRect;
216 #endif
217     curstate.pos.h = rect.left + 13;
218     curstate.pos.v = rect.bottom - 59;
219     curstate.width = rect.right - rect.left - (13 * 2);
220     if (mac_gestalts.apprvers >= 0x100)
221         CreateRootControl(window, &root);
222     mcs->window = window;
223     mcs->byctrl = newtree234(macctrl_cmp_byctrl);
224     mcs->focus = NULL;
225     /* Count the number of panels */
226     mcs->npanels = 1;
227     for (i = 1; i < cb->nctrlsets; i++)
228         if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
229             mcs->npanels++;
230     mcs->panels = smalloc(sizeof(*mcs->panels) * mcs->npanels);
231     memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
232     curstate.panelnum = 0;
233     for (i = 0; i < cb->nctrlsets; i++) {
234         if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
235                             cb->ctrlsets[i-1]->pathname)) {
236             curstate.pos.v = rect.top + 13;
237             curstate.panelnum++;
238             assert(curstate.panelnum < mcs->npanels);
239         }
240         macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
241     }
242     macctrl_switchtopanel(mcs, 14);
243 }
244
245 static void macctrl_layoutset(struct mac_layoutstate *curstate,
246                               struct controlset *s,
247                               WindowPtr window, struct macctrls *mcs)
248 {
249     unsigned int i;
250
251     fprintf(stderr, "--- begin set ---\n");
252     fprintf(stderr, "pathname = %s\n", s->pathname);
253     if (s->boxname && *s->boxname)
254         fprintf(stderr, "boxname = %s\n", s->boxname);
255     if (s->boxtitle)
256         fprintf(stderr, "boxtitle = %s\n", s->boxtitle);
257
258
259     for (i = 0; i < s->ncontrols; i++) {
260         union control *ctrl = s->ctrls[i];
261         char const *s;
262
263         switch (ctrl->generic.type) {
264           case CTRL_TEXT: s = "text"; break;
265           case CTRL_EDITBOX: s = "editbox"; break;
266           case CTRL_RADIO: s = "radio"; break;
267           case CTRL_CHECKBOX: s = "checkbox"; break;
268           case CTRL_BUTTON: s = "button"; break;
269           case CTRL_LISTBOX: s = "listbox"; break;
270           case CTRL_COLUMNS: s = "columns"; break;
271           case CTRL_FILESELECT: s = "fileselect"; break;
272           case CTRL_FONTSELECT: s = "fontselect"; break;
273           case CTRL_TABDELAY: s = "tabdelay"; break;
274           default: s = "unknown"; break;
275         }
276         fprintf(stderr, "  control: %s\n", s);
277         switch (ctrl->generic.type) {
278           case CTRL_TEXT:
279             macctrl_text(mcs, window, curstate, ctrl);
280             break;
281           case CTRL_EDITBOX:
282             macctrl_editbox(mcs, window, curstate, ctrl);
283             break;
284           case CTRL_RADIO:
285             macctrl_radio(mcs, window, curstate, ctrl);
286             break;
287           case CTRL_CHECKBOX:
288             macctrl_checkbox(mcs, window, curstate, ctrl);
289             break;
290           case CTRL_BUTTON:
291             macctrl_button(mcs, window, curstate, ctrl);
292             break;
293           case CTRL_LISTBOX:
294             if (ctrl->listbox.height == 0)
295                 macctrl_popup(mcs, window, curstate, ctrl);
296             break;
297         }
298     }
299 }
300
301 static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
302 {
303     unsigned int i, j;
304     union macctrl *mc;
305
306 #define hideshow(c) do {                                                \
307     if (i == which) ShowControl(c); else HideControl(c);                \
308 } while (0)
309
310     mcs->curpanel = which;
311     /* Panel 0 is special and always visible. */
312     for (i = 1; i < mcs->npanels; i++)
313         for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
314 #if !TARGET_API_MAC_CARBON
315             if (mcs->focus == mc)
316                 macctrl_setfocus(mcs, NULL);
317 #endif
318             switch (mc->generic.type) {
319               case MACCTRL_TEXT:
320                 hideshow(mc->text.tbctrl);
321                 break;
322               case MACCTRL_EDITBOX:
323                 hideshow(mc->editbox.tbctrl);
324                 hideshow(mc->editbox.tblabel);
325                 break;
326               case MACCTRL_RADIO:
327                 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
328                     hideshow(mc->radio.tbctrls[j]);
329                 hideshow(mc->radio.tblabel);
330                 break;
331               case MACCTRL_CHECKBOX:
332                 hideshow(mc->checkbox.tbctrl);
333                 break;
334               case MACCTRL_BUTTON:
335                 hideshow(mc->button.tbctrl);
336                 break;
337               case MACCTRL_POPUP:
338                 hideshow(mc->popup.tbctrl);
339                 break;
340             }
341         }
342 }
343
344 #if !TARGET_API_MAC_CARBON
345 /*
346  * System 7 focus manipulation
347  */
348 static void macctrl_defocus(union macctrl *mc)
349 {
350
351     assert(mac_gestalts.apprvers < 0x100);
352     switch (mc->generic.type) {
353       case MACCTRL_EDITBOX:
354         TEDeactivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
355         break;
356     }
357 }
358
359 static void macctrl_enfocus(union macctrl *mc)
360 {
361
362     assert(mac_gestalts.apprvers < 0x100);
363     switch (mc->generic.type) {
364       case MACCTRL_EDITBOX:
365         TEActivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
366         break;
367     }
368 }
369
370 static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc)
371 {
372
373     if (mcs->focus != NULL)
374         macctrl_defocus(mcs->focus);
375     mcs->focus = mc;
376     if (mc != NULL)
377         macctrl_enfocus(mc);
378 }
379 #endif
380
381 static void macctrl_text(struct macctrls *mcs, WindowPtr window,
382                          struct mac_layoutstate *curstate,
383                          union control *ctrl)
384 {
385     union macctrl *mc = smalloc(sizeof *mc);
386     Rect bounds;
387
388     fprintf(stderr, "    label = %s\n", ctrl->text.label);
389     mc->generic.type = MACCTRL_TEXT;
390     mc->generic.ctrl = ctrl;
391     mc->generic.privdata = NULL;
392     bounds.left = curstate->pos.h;
393     bounds.right = bounds.left + curstate->width;
394     bounds.top = curstate->pos.v;
395     bounds.bottom = bounds.top + 16;
396     if (mac_gestalts.apprvers >= 0x100) {
397         SInt16 height;
398         Size olen;
399
400         mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
401                                      kControlStaticTextProc, (long)mc);
402         SetControlData(mc->text.tbctrl, kControlEntireControl,
403                        kControlStaticTextTextTag,
404                        strlen(ctrl->text.label), ctrl->text.label);
405         GetControlData(mc->text.tbctrl, kControlEntireControl,
406                        kControlStaticTextTextHeightTag,
407                        sizeof(height), &height, &olen);
408         fprintf(stderr, "    height = %d\n", height);
409         SizeControl(mc->text.tbctrl, curstate->width, height);
410         curstate->pos.v += height + 6;
411     } else {
412         Str255 title;
413
414         c2pstrcpy(title, ctrl->text.label);
415         mc->text.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 0,
416                                      SYS7_TEXT_PROC, (long)mc);
417     }
418     add234(mcs->byctrl, mc);
419     mc->generic.next = mcs->panels[curstate->panelnum];
420     mcs->panels[curstate->panelnum] = mc;
421 }
422
423 #if !TARGET_API_MAC_CARBON
424 static pascal SInt32 macctrl_sys7_text_cdef(SInt16 variant, ControlRef control,
425                                      ControlDefProcMessage msg, SInt32 param)
426 {
427     RgnHandle rgn;
428
429     switch (msg) {
430       case drawCntl:
431         if ((*control)->contrlVis)
432             TETextBox((*control)->contrlTitle + 1, (*control)->contrlTitle[0],
433                       &(*control)->contrlRect, teFlushDefault);
434         return 0;
435       case calcCRgns:
436         if (param & (1 << 31)) {
437             param &= ~(1 << 31);
438             goto calcthumbrgn;
439         }
440         /* FALLTHROUGH */
441       case calcCntlRgn:
442         rgn = (RgnHandle)param;
443         RectRgn(rgn, &(*control)->contrlRect);
444         return 0;
445       case calcThumbRgn:
446       calcthumbrgn:
447         rgn = (RgnHandle)param;
448         SetEmptyRgn(rgn);
449         return 0;
450     }
451
452     return 0;
453 }
454 #endif
455
456 static void macctrl_editbox(struct macctrls *mcs, WindowPtr window,
457                             struct mac_layoutstate *curstate,
458                             union control *ctrl)
459 {
460     union macctrl *mc = smalloc(sizeof *mc);
461     Rect lbounds, bounds;
462
463     fprintf(stderr, "    label = %s\n", ctrl->editbox.label);
464     fprintf(stderr, "    percentwidth = %d\n", ctrl->editbox.percentwidth);
465     if (ctrl->editbox.password) fprintf(stderr, "    password\n");
466     if (ctrl->editbox.has_list) fprintf(stderr, "    has list\n");
467     mc->generic.type = MACCTRL_EDITBOX;
468     mc->generic.ctrl = ctrl;
469     mc->generic.privdata = NULL;
470     lbounds.left = curstate->pos.h;
471     lbounds.top = curstate->pos.v;
472     if (ctrl->editbox.percentwidth == 100) {
473         lbounds.right = lbounds.left + curstate->width;
474         lbounds.bottom = lbounds.top + 16;
475         bounds.left = curstate->pos.h;
476         bounds.right = bounds.left + curstate->width;
477         curstate->pos.v += 18;
478     } else {
479         lbounds.right = lbounds.left +
480             curstate->width * (100 - ctrl->editbox.percentwidth) / 100;
481         lbounds.bottom = lbounds.top + 22;
482         bounds.left = lbounds.right;
483         bounds.right = lbounds.left + curstate->width;
484     }
485     bounds.top = curstate->pos.v;
486     bounds.bottom = bounds.top + 22;
487     if (mac_gestalts.apprvers >= 0x100) {
488         mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE, 0, 0, 0,
489                                          kControlStaticTextProc, (long)mc);
490         SetControlData(mc->editbox.tblabel, kControlEntireControl,
491                        kControlStaticTextTextTag,
492                        strlen(ctrl->editbox.label), ctrl->editbox.label);
493         InsetRect(&bounds, 2, 2);
494         mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
495                                         ctrl->editbox.password ?
496                                         kControlEditTextPasswordProc :
497                                         kControlEditTextProc, (long)mc);
498     } else {
499         Str255 title;
500
501         c2pstrcpy(title, ctrl->editbox.label);
502         mc->editbox.tblabel = NewControl(window, &lbounds, title, TRUE,
503                                          0, 0, 0, SYS7_TEXT_PROC, (long)mc);
504         mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
505                                         SYS7_EDITBOX_PROC, (long)mc);
506     }
507     curstate->pos.v += 28;
508     add234(mcs->byctrl, mc);
509     mc->generic.next = mcs->panels[curstate->panelnum];
510     mcs->panels[curstate->panelnum] = mc;
511     ctrlevent(mcs, mc, EVENT_REFRESH);
512 }
513
514 #if !TARGET_API_MAC_CARBON
515 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant,
516                                                ControlRef control,
517                                                ControlDefProcMessage msg,
518                                                SInt32 param)
519 {
520     RgnHandle rgn;
521     Rect rect;
522     TEHandle te;
523     long ssfs;
524     Point mouse;
525
526     switch (msg) {
527       case initCntl:
528         rect = (*control)->contrlRect;
529         InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */
530         te = TENew(&rect, &rect);
531         ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
532         (*te)->txSize = LoWord(ssfs);
533         (*te)->txFont = HiWord(ssfs);
534         (*control)->contrlData = (Handle)te;
535         return noErr;
536       case dispCntl:
537         TEDispose((TEHandle)(*control)->contrlData);
538         return 0;
539       case drawCntl:
540         if ((*control)->contrlVis) {
541             rect = (*control)->contrlRect;
542             PenNormal();
543             FrameRect(&rect);
544             InsetRect(&rect, 3, 3);
545             TEUpdate(&rect, (TEHandle)(*control)->contrlData);
546         }
547         return 0;
548       case testCntl:
549         mouse.h = LoWord(param);
550         mouse.v = HiWord(param);
551         return
552             PtInRect(mouse, &(*(TEHandle)(*control)->contrlData)->viewRect) ?
553             kControlEditTextPart : kControlNoPart;
554       case calcCRgns:
555         if (param & (1 << 31)) {
556             param &= ~(1 << 31);
557             goto calcthumbrgn;
558         }
559         /* FALLTHROUGH */
560       case calcCntlRgn:
561         rgn = (RgnHandle)param;
562         RectRgn(rgn, &(*control)->contrlRect);
563         return 0;
564       case calcThumbRgn:
565       calcthumbrgn:
566         rgn = (RgnHandle)param;
567         SetEmptyRgn(rgn);
568         return 0;
569     }
570
571     return 0;
572 }
573 #endif
574
575 static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
576                           struct mac_layoutstate *curstate,
577                           union control *ctrl)
578 {
579     union macctrl *mc = smalloc(sizeof *mc);
580     Rect bounds;
581     Str255 title;
582     unsigned int i, colwidth;
583
584     fprintf(stderr, "    label = %s\n", ctrl->radio.label);
585     mc->generic.type = MACCTRL_RADIO;
586     mc->generic.ctrl = ctrl;
587     mc->generic.privdata = NULL;
588     mc->radio.tbctrls =
589         smalloc(sizeof(*mc->radio.tbctrls) * ctrl->radio.nbuttons);
590     colwidth = (curstate->width + 13) / ctrl->radio.ncolumns;
591     bounds.top = curstate->pos.v;
592     bounds.bottom = bounds.top + 16;
593     bounds.left = curstate->pos.h;
594     bounds.right = bounds.left + curstate->width;
595     if (mac_gestalts.apprvers >= 0x100) {
596         mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
597                                        kControlStaticTextProc, (long)mc);
598         SetControlData(mc->radio.tblabel, kControlEntireControl,
599                        kControlStaticTextTextTag,
600                        strlen(ctrl->radio.label), ctrl->radio.label);
601     } else {
602         Str255 title;
603
604         c2pstrcpy(title, ctrl->radio.label);
605         mc->editbox.tblabel = NewControl(window, &bounds, title, TRUE,
606                                          0, 0, 0, SYS7_TEXT_PROC, (long)mc);
607     }
608     curstate->pos.v += 18;
609     for (i = 0; i < ctrl->radio.nbuttons; i++) {
610         fprintf(stderr, "    button = %s\n", ctrl->radio.buttons[i]);
611         bounds.top = curstate->pos.v - 2;
612         bounds.bottom = bounds.top + 18;
613         bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
614         if (i == ctrl->radio.nbuttons - 1 ||
615             i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
616             bounds.right = curstate->pos.h + curstate->width;
617             curstate->pos.v += 18;
618         } else
619             bounds.right = bounds.left + colwidth - 13;
620         c2pstrcpy(title, ctrl->radio.buttons[i]);
621         mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE,
622                                           0, 0, 1, radioButProc, (long)mc);
623     }
624     curstate->pos.v += 4;
625     add234(mcs->byctrl, mc);
626     mc->generic.next = mcs->panels[curstate->panelnum];
627     mcs->panels[curstate->panelnum] = mc;
628     ctrlevent(mcs, mc, EVENT_REFRESH);
629 }
630
631 static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
632                              struct mac_layoutstate *curstate,
633                              union control *ctrl)
634 {
635     union macctrl *mc = smalloc(sizeof *mc);
636     Rect bounds;
637     Str255 title;
638
639     fprintf(stderr, "    label = %s\n", ctrl->checkbox.label);
640     mc->generic.type = MACCTRL_CHECKBOX;
641     mc->generic.ctrl = ctrl;
642     mc->generic.privdata = NULL;
643     bounds.left = curstate->pos.h;
644     bounds.right = bounds.left + curstate->width;
645     bounds.top = curstate->pos.v;
646     bounds.bottom = bounds.top + 16;
647     c2pstrcpy(title, ctrl->checkbox.label);
648     mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
649                                      checkBoxProc, (long)mc);
650     add234(mcs->byctrl, mc);
651     curstate->pos.v += 22;
652     mc->generic.next = mcs->panels[curstate->panelnum];
653     mcs->panels[curstate->panelnum] = mc;
654     ctrlevent(mcs, mc, EVENT_REFRESH);
655 }
656
657 static void macctrl_button(struct macctrls *mcs, WindowPtr window,
658                            struct mac_layoutstate *curstate,
659                            union control *ctrl)
660 {
661     union macctrl *mc = smalloc(sizeof *mc);
662     Rect bounds;
663     Str255 title;
664
665     fprintf(stderr, "    label = %s\n", ctrl->button.label);
666     if (ctrl->button.isdefault)
667         fprintf(stderr, "    is default\n");
668     mc->generic.type = MACCTRL_BUTTON;
669     mc->generic.ctrl = ctrl;
670     mc->generic.privdata = NULL;
671     bounds.left = curstate->pos.h;
672     bounds.right = bounds.left + 100; /* XXX measure string */
673     bounds.top = curstate->pos.v;
674     bounds.bottom = bounds.top + 20;
675     c2pstrcpy(title, ctrl->button.label);
676     mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
677                                    pushButProc, (long)mc);
678     if (mac_gestalts.apprvers >= 0x100) {
679         Boolean isdefault = ctrl->button.isdefault;
680
681         SetControlData(mc->button.tbctrl, kControlEntireControl,
682                        kControlPushButtonDefaultTag,
683                        sizeof(isdefault), &isdefault);
684     } else if (ctrl->button.isdefault) {
685         InsetRect(&bounds, -4, -4);
686         NewControl(window, &bounds, title, TRUE, 0, 0, 1,
687                    SYS7_DEFAULT_PROC, (long)mc);
688     }
689     if (mac_gestalts.apprvers >= 0x110) {
690         Boolean iscancel = ctrl->button.iscancel;
691
692         SetControlData(mc->button.tbctrl, kControlEntireControl,
693                        kControlPushButtonCancelTag,
694                        sizeof(iscancel), &iscancel);
695     }
696     add234(mcs->byctrl, mc);
697     mc->generic.next = mcs->panels[curstate->panelnum];
698     mcs->panels[curstate->panelnum] = mc;
699     curstate->pos.v += 26;
700 }
701
702 #if !TARGET_API_MAC_CARBON
703 static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
704                                                ControlRef control,
705                                                ControlDefProcMessage msg,
706                                                SInt32 param)
707 {
708     RgnHandle rgn;
709     Rect rect;
710     int oval;
711
712     switch (msg) {
713       case drawCntl:
714         if ((*control)->contrlVis) {
715             rect = (*control)->contrlRect;
716             PenNormal();
717             PenSize(3, 3);
718             oval = (rect.bottom - rect.top) / 2 + 2;
719             FrameRoundRect(&rect, oval, oval);
720         }
721         return 0;
722       case calcCRgns:
723         if (param & (1 << 31)) {
724             param &= ~(1 << 31);
725             goto calcthumbrgn;
726         }
727         /* FALLTHROUGH */
728       case calcCntlRgn:
729         rgn = (RgnHandle)param;
730         RectRgn(rgn, &(*control)->contrlRect);
731         return 0;
732       case calcThumbRgn:
733       calcthumbrgn:
734         rgn = (RgnHandle)param;
735         SetEmptyRgn(rgn);
736         return 0;
737     }
738
739     return 0;
740 }
741 #endif
742
743 static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
744                           struct mac_layoutstate *curstate,
745                           union control *ctrl)
746 {
747     union macctrl *mc = smalloc(sizeof *mc);
748     Rect bounds;
749     Str255 title;
750     unsigned int labelwidth;
751     static int nextmenuid = MENU_MIN;
752     int menuid;
753     MenuRef menu;
754
755     /* 
756      * <http://developer.apple.com/qa/tb/tb42.html> explains how to
757      * create a popup menu with dynamic content.
758      */
759     assert(ctrl->listbox.height == 0);
760     assert(!ctrl->listbox.draglist);
761     assert(!ctrl->listbox.multisel);
762
763     fprintf(stderr, "    label = %s\n", ctrl->listbox.label);
764     fprintf(stderr, "    percentwidth = %d\n", ctrl->listbox.percentwidth);
765
766     mc->generic.type = MACCTRL_POPUP;
767     mc->generic.ctrl = ctrl;
768     mc->generic.privdata = NULL;
769     c2pstrcpy(title, ctrl->button.label);
770
771     /* Find a spare menu ID and create the menu */
772     while (GetMenuHandle(nextmenuid) != NULL)
773         if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
774     menuid = nextmenuid++;
775     menu = NewMenu(menuid, "\pdummy");
776     if (menu == NULL) return;
777     mc->popup.menu = menu;
778     mc->popup.menuid = menuid;
779     InsertMenu(menu, kInsertHierarchicalMenu);
780
781     /* The menu starts off empty */
782     mc->popup.nids = 0;
783     mc->popup.ids = NULL;
784
785     bounds.left = curstate->pos.h;
786     bounds.right = bounds.left + curstate->width;
787     bounds.top = curstate->pos.v;
788     bounds.bottom = bounds.top + 20;
789     /* XXX handle percentwidth == 100 */
790     labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
791     mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE,
792                                   popupTitleLeftJust, menuid, labelwidth,
793                                   popupMenuProc + popupFixedWidth, (long)mc);
794     add234(mcs->byctrl, mc);
795     curstate->pos.v += 26;
796     mc->generic.next = mcs->panels[curstate->panelnum];
797     mcs->panels[curstate->panelnum] = mc;
798     ctrlevent(mcs, mc, EVENT_REFRESH);
799 }
800
801
802 void macctrl_activate(WindowPtr window, EventRecord *event)
803 {
804     struct macctrls *mcs = mac_winctrls(window);
805     Boolean active = (event->modifiers & activeFlag) != 0;
806     GrafPtr saveport;
807     int i, j;
808     ControlPartCode state;
809     union macctrl *mc;
810
811     GetPort(&saveport);
812     SetPort((GrafPtr)GetWindowPort(window));
813     if (mac_gestalts.apprvers >= 0x100)
814         SetThemeWindowBackground(window, active ?
815                                  kThemeBrushModelessDialogBackgroundActive :
816                                  kThemeBrushModelessDialogBackgroundInactive,
817                                  TRUE);
818     state = active ? kControlNoPart : kControlInactivePart;
819     for (i = 0; i <= mcs->curpanel; i += mcs->curpanel)
820         for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next)
821             switch (mc->generic.type) {
822               case MACCTRL_TEXT:
823                 HiliteControl(mc->text.tbctrl, state);
824                 break;
825               case MACCTRL_EDITBOX:
826                 HiliteControl(mc->editbox.tbctrl, state);
827                 HiliteControl(mc->editbox.tblabel, state);
828                 break;
829               case MACCTRL_RADIO:
830                 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
831                     HiliteControl(mc->radio.tbctrls[j], state);
832                 HiliteControl(mc->radio.tblabel, state);
833                 break;
834               case MACCTRL_CHECKBOX:
835                 HiliteControl(mc->checkbox.tbctrl, state);
836                 break;
837               case MACCTRL_BUTTON:
838                 HiliteControl(mc->button.tbctrl, state);
839                 break;
840               case MACCTRL_POPUP:
841                 HiliteControl(mc->popup.tbctrl, state);
842                 break;
843             }
844     SetPort(saveport);
845 }
846
847 void macctrl_click(WindowPtr window, EventRecord *event)
848 {
849     Point mouse;
850     ControlHandle control;
851     int part, trackresult;
852     GrafPtr saveport;
853     union macctrl *mc;
854     struct macctrls *mcs = mac_winctrls(window);
855     int i;
856     UInt32 features;
857
858     GetPort(&saveport);
859     SetPort((GrafPtr)GetWindowPort(window));
860     mouse = event->where;
861     GlobalToLocal(&mouse);
862     part = FindControl(mouse, window, &control);
863     if (control != NULL) {
864         mc = (union macctrl *)GetControlReference(control);
865         if (mac_gestalts.apprvers >= 0x100) {
866             if (GetControlFeatures(control, &features) == noErr &&
867                 (features & kControlSupportsFocus) &&
868                 (features & kControlGetsFocusOnClick))
869                 SetKeyboardFocus(window, control, part);
870             trackresult = HandleControlClick(control, mouse, event->modifiers,
871                                              (ControlActionUPP)-1);
872         } else {
873 #if !TARGET_API_MAC_CARBON
874             if (mc->generic.type == MACCTRL_EDITBOX &&
875                 control == mc->editbox.tbctrl) {
876                 TEHandle te = (TEHandle)(*control)->contrlData;
877
878                 macctrl_setfocus(mcs, mc);
879                 TEClick(mouse, !!(event->modifiers & shiftKey), te);
880                 goto done;
881             }
882 #endif
883             trackresult = TrackControl(control, mouse, (ControlActionUPP)-1);
884         }
885         switch (mc->generic.type) {
886           case MACCTRL_RADIO:
887             if (trackresult != 0) {
888                 for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
889                     if (mc->radio.tbctrls[i] == control)
890                         SetControlValue(mc->radio.tbctrls[i],
891                                         kControlRadioButtonCheckedValue);
892                     else
893                         SetControlValue(mc->radio.tbctrls[i],
894                                         kControlRadioButtonUncheckedValue);
895                 ctrlevent(mcs, mc, EVENT_VALCHANGE);
896             }
897             break;
898           case MACCTRL_CHECKBOX:
899             if (trackresult != 0) {
900                 SetControlValue(control, !GetControlValue(control));
901                 ctrlevent(mcs, mc, EVENT_VALCHANGE);
902             }
903             break;
904           case MACCTRL_BUTTON:
905             if (trackresult != 0)
906                 ctrlevent(mcs, mc, EVENT_ACTION);
907             break;
908           case MACCTRL_POPUP:
909             ctrlevent(mcs, mc, EVENT_SELCHANGE);
910             break;
911         }
912     }
913   done:
914     SetPort(saveport);
915 }
916
917 void macctrl_key(WindowPtr window, EventRecord *event)
918 {
919     ControlRef control;
920     struct macctrls *mcs = mac_winctrls(window);
921     union macctrl *mc;
922
923     if (mac_gestalts.apprvers >= 0x100 &&
924         GetKeyboardFocus(window, &control) == noErr && control != NULL) {
925         HandleControlKey(control, (event->message & keyCodeMask) >> 8,
926                          event->message & charCodeMask, event->modifiers);
927         mc =  (union macctrl *)GetControlReference(control);
928         ctrlevent(mcs, mc, EVENT_VALCHANGE);
929     }
930 }
931
932 void macctrl_update(WindowPtr window)
933 {
934 #if TARGET_API_MAC_CARBON
935     RgnHandle visrgn;
936 #endif
937     Rect rect;
938     GrafPtr saveport;
939
940     BeginUpdate(window);
941     GetPort(&saveport);
942     SetPort((GrafPtr)GetWindowPort(window));
943     if (mac_gestalts.apprvers >= 0x101) {
944 #if TARGET_API_MAC_CARBON
945         GetPortBounds(GetWindowPort(window), &rect);
946 #else
947         rect = window->portRect;
948 #endif
949         InsetRect(&rect, -1, -1);
950         DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ?
951                                      kThemeStateActive : kThemeStateInactive);
952     }
953 #if TARGET_API_MAC_CARBON
954     visrgn = NewRgn();
955     GetPortVisibleRegion(GetWindowPort(window), visrgn);
956     UpdateControls(window, visrgn);
957     DisposeRgn(visrgn);
958 #else
959     UpdateControls(window, window->visRgn);
960 #endif
961     SetPort(saveport);
962     EndUpdate(window);
963 }
964
965 #if TARGET_API_MAC_CARBON
966 #define EnableItem EnableMenuItem
967 #define DisableItem DisableMenuItem
968 #endif
969 void macctrl_adjustmenus(WindowPtr window)
970 {
971     MenuHandle menu;
972
973     menu = GetMenuHandle(mFile);
974     DisableItem(menu, iSave); /* XXX enable if modified */
975     EnableItem(menu, iSaveAs);
976     EnableItem(menu, iDuplicate);
977
978     menu = GetMenuHandle(mEdit);
979     DisableItem(menu, 0);
980 }
981
982 void macctrl_close(WindowPtr window)
983 {
984     struct macctrls *mcs = mac_winctrls(window);
985     union macctrl *mc;
986
987     /*
988      * Mostly, we don't bother disposing of the Toolbox controls,
989      * since that will happen automatically when the window is
990      * disposed of.  Popup menus are an exception, because we have to
991      * dispose of the menu ourselves, and doing that while the control
992      * still holds a reference to it seems rude.
993      */
994     while ((mc = index234(mcs->byctrl, 0)) != NULL) {
995         if (mc->generic.privdata != NULL && mc->generic.freeprivdata)
996             sfree(mc->generic.privdata);
997         switch (mc->generic.type) {
998           case MACCTRL_POPUP:
999             DisposeControl(mc->popup.tbctrl);
1000             DeleteMenu(mc->popup.menuid);
1001             DisposeMenu(mc->popup.menu);
1002             break;
1003         }
1004         del234(mcs->byctrl, mc);
1005         sfree(mc);
1006     }
1007
1008     freetree234(mcs->byctrl);
1009     mcs->byctrl = NULL;
1010     sfree(mcs->panels);
1011     mcs->panels = NULL;
1012 }
1013
1014 void dlg_update_start(union control *ctrl, void *dlg)
1015 {
1016
1017     /* No-op for now */
1018 }
1019
1020 void dlg_update_done(union control *ctrl, void *dlg)
1021 {
1022
1023     /* No-op for now */
1024 }
1025
1026 void dlg_set_focus(union control *ctrl, void *dlg)
1027 {
1028
1029     if (mac_gestalts.apprvers >= 0x100) {
1030         /* Use SetKeyboardFocus() */
1031     } else {
1032         /* Do our own mucking around */
1033     }
1034 }
1035
1036 union control *dlg_last_focused(union control *ctrl, void *dlg)
1037 {
1038
1039     return NULL;
1040 }
1041
1042 void dlg_beep(void *dlg)
1043 {
1044
1045     SysBeep(30);
1046 }
1047
1048 void dlg_error_msg(void *dlg, char *msg)
1049 {
1050     Str255 pmsg;
1051
1052     c2pstrcpy(pmsg, msg);
1053     ParamText(pmsg, NULL, NULL, NULL);
1054     StopAlert(128, NULL);
1055 }
1056
1057 void dlg_end(void *dlg, int value)
1058 {
1059
1060 };
1061
1062 void dlg_refresh(union control *ctrl, void *dlg)
1063 {
1064     struct macctrls *mcs = dlg;
1065     union macctrl *mc;
1066
1067     if (ctrl == NULL)
1068         return; /* FIXME */
1069     mc = findbyctrl(mcs, ctrl);
1070     assert(mc != NULL);
1071     ctrlevent(mcs, mc, EVENT_REFRESH);
1072 };
1073
1074 void *dlg_get_privdata(union control *ctrl, void *dlg)
1075 {
1076     struct macctrls *mcs = dlg;
1077     union macctrl *mc = findbyctrl(mcs, ctrl);
1078
1079     assert(mc != NULL);
1080     return mc->generic.privdata;
1081 }
1082
1083 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
1084 {
1085     struct macctrls *mcs = dlg;
1086     union macctrl *mc = findbyctrl(mcs, ctrl);
1087
1088     assert(mc != NULL);
1089     mc->generic.privdata = ptr;
1090     mc->generic.freeprivdata = FALSE;
1091 }
1092
1093 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
1094 {
1095     struct macctrls *mcs = dlg;
1096     union macctrl *mc = findbyctrl(mcs, ctrl);
1097
1098     assert(mc != NULL);
1099     mc->generic.privdata = smalloc(size);
1100     mc->generic.freeprivdata = TRUE;
1101     return mc->generic.privdata;
1102 }
1103
1104
1105 /*
1106  * Radio Button control
1107  */
1108
1109 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
1110 {
1111     struct macctrls *mcs = dlg;
1112     union macctrl *mc = findbyctrl(mcs, ctrl);
1113     int i;
1114
1115     assert(mc != NULL);
1116     for (i = 0; i < ctrl->radio.nbuttons; i++) {
1117         if (i == whichbutton)
1118             SetControlValue(mc->radio.tbctrls[i],
1119                             kControlRadioButtonCheckedValue);
1120         else
1121             SetControlValue(mc->radio.tbctrls[i],
1122                             kControlRadioButtonUncheckedValue);
1123     }
1124
1125 };
1126
1127 int dlg_radiobutton_get(union control *ctrl, void *dlg)
1128 {
1129     struct macctrls *mcs = dlg;
1130     union macctrl *mc = findbyctrl(mcs, ctrl);
1131     int i;
1132
1133     assert(mc != NULL);
1134     for (i = 0; i < ctrl->radio.nbuttons; i++) {
1135         if (GetControlValue(mc->radio.tbctrls[i])  ==
1136             kControlRadioButtonCheckedValue)
1137             return i;
1138     }
1139     return -1;
1140 };
1141
1142
1143 /*
1144  * Check Box control
1145  */
1146
1147 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
1148 {
1149     struct macctrls *mcs = dlg;
1150     union macctrl *mc = findbyctrl(mcs, ctrl);
1151
1152     assert(mc != NULL);
1153     SetControlValue(mc->checkbox.tbctrl,
1154                     checked ? kControlCheckBoxCheckedValue :
1155                               kControlCheckBoxUncheckedValue);
1156 }
1157
1158 int dlg_checkbox_get(union control *ctrl, void *dlg)
1159 {
1160     struct macctrls *mcs = dlg;
1161     union macctrl *mc = findbyctrl(mcs, ctrl);
1162
1163     assert(mc != NULL);
1164     return GetControlValue(mc->checkbox.tbctrl);
1165 }
1166
1167
1168 /*
1169  * Edit Box control
1170  */
1171
1172 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
1173 {
1174     struct macctrls *mcs = dlg;
1175     union macctrl *mc = findbyctrl(mcs, ctrl);
1176     GrafPtr saveport;
1177
1178     assert(mc != NULL);
1179     assert(mc->generic.type == MACCTRL_EDITBOX);
1180     GetPort(&saveport);
1181     SetPort((GrafPtr)(GetWindowPort(mcs->window)));
1182     if (mac_gestalts.apprvers >= 0x100)
1183         SetControlData(mc->editbox.tbctrl, kControlEntireControl,
1184                        ctrl->editbox.password ?
1185                        kControlEditTextPasswordTag :
1186                        kControlEditTextTextTag,
1187                        strlen(text), text);
1188 #if !TARGET_API_MAC_CARBON
1189     else
1190         TESetText(text, strlen(text),
1191                   (TEHandle)(*mc->editbox.tbctrl)->contrlData);
1192 #endif
1193     DrawOneControl(mc->editbox.tbctrl);
1194     SetPort(saveport);
1195 }
1196
1197 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
1198 {
1199     struct macctrls *mcs = dlg;
1200     union macctrl *mc = findbyctrl(mcs, ctrl);
1201     Size olen;
1202
1203     assert(mc != NULL);
1204     assert(mc->generic.type == MACCTRL_EDITBOX);
1205     if (mac_gestalts.apprvers >= 0x100) {
1206         if (GetControlData(mc->editbox.tbctrl, kControlEntireControl,
1207                            ctrl->editbox.password ?
1208                            kControlEditTextPasswordTag :
1209                            kControlEditTextTextTag,
1210                            length - 1, buffer, &olen) != noErr)
1211             olen = 0;
1212         if (olen > length - 1)
1213             olen = length - 1;
1214     }
1215 #if !TARGET_API_MAC_CARBON
1216     else {
1217         TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1218
1219         olen = (*te)->teLength;
1220         if (olen > length - 1)
1221             olen = length - 1;
1222         memcpy(buffer, *(*te)->hText, olen);
1223     }
1224 #endif
1225     buffer[olen] = '\0';
1226     fprintf(stderr, "dlg_editbox_get: %s\n", buffer);
1227 }
1228
1229
1230 /*
1231  * List Box control
1232  */
1233
1234 static void dlg_macpopup_clear(union control *ctrl, void *dlg)
1235 {
1236     struct macctrls *mcs = dlg;
1237     union macctrl *mc = findbyctrl(mcs, ctrl);
1238     MenuRef menu = mc->popup.menu;
1239     unsigned int i, n;
1240
1241     fprintf(stderr, "      popup_clear\n");
1242     n = CountMenuItems(menu);
1243     for (i = 0; i < n; i++)
1244         DeleteMenuItem(menu, n - i);
1245     mc->popup.nids = 0;
1246     sfree(mc->popup.ids);
1247     mc->popup.ids = NULL;
1248     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1249 }
1250
1251 void dlg_listbox_clear(union control *ctrl, void *dlg)
1252 {
1253
1254     if (ctrl->listbox.height == 0)
1255         dlg_macpopup_clear(ctrl, dlg);
1256 }
1257
1258 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
1259 {
1260     struct macctrls *mcs = dlg;
1261     union macctrl *mc = findbyctrl(mcs, ctrl);
1262     MenuRef menu = mc->popup.menu;
1263
1264     fprintf(stderr, "      popup_del %d\n", index);
1265     DeleteMenuItem(menu, index + 1);
1266     if (mc->popup.ids != NULL)
1267         memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
1268                (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
1269     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1270 }
1271
1272 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
1273 {
1274
1275     if (ctrl->listbox.height == 0)
1276         dlg_macpopup_del(ctrl, dlg, index);
1277 }
1278
1279 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
1280 {
1281     struct macctrls *mcs = dlg;
1282     union macctrl *mc = findbyctrl(mcs, ctrl);
1283     MenuRef menu = mc->popup.menu;
1284     Str255 itemstring;
1285
1286     fprintf(stderr, "      popup_add %s\n", text);
1287     assert(text[0] != '\0');
1288     c2pstrcpy(itemstring, text);
1289     AppendMenu(menu, "\pdummy");
1290     SetMenuItemText(menu, CountMenuItems(menu), itemstring);
1291     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1292 }
1293
1294 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
1295 {
1296
1297     if (ctrl->listbox.height == 0)
1298         dlg_macpopup_add(ctrl, dlg, text);
1299 }
1300
1301 static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
1302                                    char const *text, int id)
1303 {
1304     struct macctrls *mcs = dlg;
1305     union macctrl *mc = findbyctrl(mcs, ctrl);
1306     MenuRef menu = mc->popup.menu;
1307     unsigned int index;
1308
1309     fprintf(stderr, "      popup_addwthindex %s, %d\n", text, id);
1310     dlg_macpopup_add(ctrl, dlg, text);
1311     index = CountMenuItems(menu) - 1;
1312     if (mc->popup.nids <= index) {
1313         mc->popup.nids = index + 1;
1314         mc->popup.ids = srealloc(mc->popup.ids,
1315                                  mc->popup.nids * sizeof(*mc->popup.ids));
1316     }
1317     mc->popup.ids[index] = id;
1318 }
1319
1320 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
1321                            char const *text, int id)
1322 {
1323
1324     if (ctrl->listbox.height == 0)
1325         dlg_macpopup_addwithid(ctrl, dlg, text, id);
1326 }
1327
1328 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
1329 {
1330     struct macctrls *mcs = dlg;
1331     union macctrl *mc = findbyctrl(mcs, ctrl);
1332
1333     if (ctrl->listbox.height == 0) {
1334         assert(mc->popup.ids != NULL && mc->popup.nids > index);
1335         return mc->popup.ids[index];
1336     }
1337     return 0;
1338 }
1339
1340 int dlg_listbox_index(union control *ctrl, void *dlg)
1341 {
1342     struct macctrls *mcs = dlg;
1343     union macctrl *mc = findbyctrl(mcs, ctrl);
1344
1345     if (ctrl->listbox.height == 0)
1346         return GetControlValue(mc->popup.tbctrl) - 1;
1347     return 0;
1348 };
1349
1350 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1351 {
1352     struct macctrls *mcs = dlg;
1353     union macctrl *mc = findbyctrl(mcs, ctrl);
1354
1355     if (ctrl->listbox.height == 0)
1356         return GetControlValue(mc->popup.tbctrl) - 1 == index;
1357     return 0;
1358 };
1359
1360 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1361 {
1362     struct macctrls *mcs = dlg;
1363     union macctrl *mc = findbyctrl(mcs, ctrl);
1364
1365     if (ctrl->listbox.height == 0)
1366         SetControlValue(mc->popup.tbctrl, index + 1);
1367 };
1368
1369
1370 /*
1371  * Text control
1372  */
1373
1374 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1375 {
1376     struct macctrls *mcs = dlg;
1377     union macctrl *mc = findbyctrl(mcs, ctrl);
1378     Str255 title;
1379
1380     assert(mc != NULL);
1381     if (mac_gestalts.apprvers >= 0x100)
1382         SetControlData(mc->text.tbctrl, kControlEntireControl,
1383                        kControlStaticTextTextTag, strlen(text), text);
1384     else {
1385         c2pstrcpy(title, text);
1386         SetControlTitle(mc->text.tbctrl, title);
1387     }
1388 }
1389
1390
1391 /*
1392  * File Selector control
1393  */
1394
1395 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1396 {
1397
1398 }
1399
1400 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1401 {
1402
1403 }
1404
1405
1406 /*
1407  * Font Selector control
1408  */
1409
1410 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1411 {
1412
1413 }
1414
1415 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1416 {
1417
1418 }
1419
1420
1421 /*
1422  * Printer enumeration
1423  */
1424
1425 printer_enum *printer_start_enum(int *nprinters)
1426 {
1427
1428     *nprinters = 0;
1429     return NULL;
1430 }
1431
1432 char *printer_get_name(printer_enum *pe, int thing)
1433 {
1434
1435     return "<none>";
1436 }
1437
1438 void printer_finish_enum(printer_enum *pe)
1439 {
1440
1441 }
1442
1443
1444 /*
1445  * Colour selection stuff
1446  */
1447
1448 void dlg_coloursel_start(union control *ctrl, void *dlg,
1449                          int r, int g, int b)
1450 {
1451
1452 }
1453
1454 int dlg_coloursel_results(union control *ctrl, void *dlg,
1455                           int *r, int *g, int *b)
1456 {
1457
1458     return 0;
1459 }
1460
1461 /*
1462  * Local Variables:
1463  * c-file-style: "simon"
1464  * End:
1465  */