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