]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac/macctrls.c
9b51e50c0382eb8128d1ff58a59a3bbe7b742404
[PuTTY.git] / mac / macctrls.c
1 /* $Id: macctrls.c,v 1.37 2003/04/14 23:25:04 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 <ColorPicker.h>
31 #include <Controls.h>
32 #include <ControlDefinitions.h>
33 #include <Events.h>
34 #include <Lists.h>
35 #include <Menus.h>
36 #include <Resources.h>
37 #include <Script.h>
38 #include <Sound.h>
39 #include <TextEdit.h>
40 #include <TextUtils.h>
41 #include <ToolUtils.h>
42 #include <Windows.h>
43
44 #include <assert.h>
45 #include <string.h>
46
47 #include "putty.h"
48 #include "mac.h"
49 #include "macresid.h"
50 #include "dialog.h"
51 #include "tree234.h"
52
53 /* Range of menu IDs for popup menus */
54 #define MENU_MIN        1024
55 #define MENU_MAX        2048
56
57
58 union macctrl {
59     struct macctrl_generic {
60         enum {
61             MACCTRL_TEXT,
62             MACCTRL_EDITBOX,
63             MACCTRL_RADIO,
64             MACCTRL_CHECKBOX,
65             MACCTRL_BUTTON,
66             MACCTRL_LISTBOX,
67             MACCTRL_POPUP
68         } type;
69         /* Template from which this was generated */
70         union control *ctrl;
71         /* Next control in this panel */
72         union macctrl *next;
73         void *privdata;
74         int freeprivdata;
75     } generic;
76     struct {
77         struct macctrl_generic generic;
78         ControlRef tbctrl;
79     } text;
80     struct {
81         struct macctrl_generic generic;
82         ControlRef tbctrl;
83         ControlRef tblabel;
84     } editbox;
85     struct {
86         struct macctrl_generic generic;
87         ControlRef *tbctrls;
88         ControlRef tblabel;
89     } radio;
90     struct {
91         struct macctrl_generic generic;
92         ControlRef tbctrl;
93     } checkbox;
94     struct {
95         struct macctrl_generic generic;
96         ControlRef tbctrl;
97         ControlRef tbring;
98     } button;
99     struct {
100         struct macctrl_generic generic;
101         ControlRef tbctrl;
102         ListHandle list;
103         unsigned int nids;
104         int *ids;
105     } listbox;
106     struct {
107         struct macctrl_generic generic;
108         ControlRef tbctrl;
109         MenuRef menu;
110         int menuid;
111         unsigned int nids;
112         int *ids;
113     } popup;
114 };
115
116 struct mac_layoutstate {
117     Point pos;
118     unsigned int width;
119     unsigned int panelnum;
120 };
121
122 #define ctrlevent(mcs, mc, event) do {                                  \
123     if ((mc)->generic.ctrl->generic.handler != NULL)                    \
124         (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
125                                                (mcs)->data, (event));   \
126 } while (0)
127
128 #define findbyctrl(mcs, ctrl)                                           \
129     find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
130
131 static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *, 
132                               WindowPtr, struct macctrls *);
133 static void macctrl_switchtopanel(struct macctrls *, unsigned int);
134 static void macctrl_setfocus(struct macctrls *, union macctrl *);
135 static void macctrl_text(struct macctrls *, WindowPtr,
136                          struct mac_layoutstate *, union control *);
137 static void macctrl_editbox(struct macctrls *, WindowPtr,
138                             struct mac_layoutstate *, union control *);
139 static void macctrl_radio(struct macctrls *, WindowPtr,
140                           struct mac_layoutstate *, union control *);
141 static void macctrl_checkbox(struct macctrls *, WindowPtr,
142                              struct mac_layoutstate *, union control *);
143 static void macctrl_button(struct macctrls *, WindowPtr,
144                            struct mac_layoutstate *, union control *);
145 static void macctrl_listbox(struct macctrls *, WindowPtr,
146                             struct mac_layoutstate *, union control *);
147 static void macctrl_popup(struct macctrls *, WindowPtr,
148                           struct mac_layoutstate *, union control *);
149 #if !TARGET_API_MAC_CARBON
150 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef,
151                                                ControlDefProcMessage, SInt32);
152 static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
153                                                ControlDefProcMessage, SInt32);
154 static pascal SInt32 macctrl_sys7_listbox_cdef(SInt16, ControlRef,
155                                                ControlDefProcMessage, SInt32);
156 #endif
157
158 #if !TARGET_API_MAC_CARBON
159 /*
160  * This trick enables us to keep all the CDEF code in the main
161  * application, which makes life easier.  For details, see
162  * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
163  */
164
165 #pragma options align=mac68k
166 typedef struct {
167     short               jmpabs; /* 4EF9 */
168     ControlDefUPP       theUPP;
169 } **PatchCDEF;
170 #pragma options align=reset
171 #endif
172
173 static void macctrl_init()
174 {
175 #if !TARGET_API_MAC_CARBON
176     static int inited = 0;
177     PatchCDEF cdef;
178
179     if (inited) return;
180     cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox);
181     (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef);
182     cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
183     (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
184     cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_ListBox);
185     (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_listbox_cdef);
186     inited = 1;
187 #endif
188 }
189
190
191 static int macctrl_cmp_byctrl(void *av, void *bv)
192 {
193     union macctrl *a = (union macctrl *)av;
194     union macctrl *b = (union macctrl *)bv;
195
196     if (a->generic.ctrl < b->generic.ctrl)
197         return -1;
198     else if (a->generic.ctrl > b->generic.ctrl)
199         return +1;
200     else
201         return 0;
202 }
203
204 static int macctrl_cmp_byctrl_find(void *av, void *bv)
205 {
206     union control *a = (union control *)av;
207     union macctrl *b = (union macctrl *)bv;
208
209     if (a < b->generic.ctrl)
210         return -1;
211     else if (a > b->generic.ctrl)
212         return +1;
213     else
214         return 0;
215 }
216
217 static union control panellist;
218
219 static void panellist_handler(union control *ctrl, void *dlg, void *data,
220                               int event)
221 {
222     struct macctrls *mcs = dlg;
223
224     if (event == EVENT_SELCHANGE)
225         macctrl_switchtopanel(mcs, dlg_listbox_index(ctrl, dlg) + 1);
226 }
227
228 void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
229                        struct macctrls *mcs)
230 {
231     int i;
232     struct mac_layoutstate curstate;
233     ControlRef root;
234     Rect rect;
235
236     macctrl_init();
237     if (mac_gestalts.apprvers >= 0x100)
238         CreateRootControl(window, &root);
239 #if TARGET_API_MAC_CARBON
240     GetPortBounds(GetWindowPort(window), &rect);
241 #else
242     rect = window->portRect;
243 #endif
244     mcs->window = window;
245     mcs->byctrl = newtree234(macctrl_cmp_byctrl);
246     mcs->focus = NULL;
247     mcs->defbutton = NULL;
248     mcs->canbutton = NULL;
249     /* Count the number of panels */
250     mcs->npanels = 1;
251     for (i = 1; i < cb->nctrlsets; i++)
252         if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
253             mcs->npanels++;
254     mcs->panels = snewn(mcs->npanels, union macctrl *);
255     memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
256     curstate.panelnum = 0;
257
258     curstate.pos.h = rect.left + 13;
259     curstate.pos.v = rect.top + 13;
260     curstate.width = 160;
261     panellist.listbox.type = CTRL_LISTBOX;
262     panellist.listbox.handler = &panellist_handler;
263     panellist.listbox.height = 20;
264     panellist.listbox.percentwidth = 100;
265     macctrl_listbox(mcs, window, &curstate, &panellist);
266
267     curstate.pos.h = rect.left + 13 + 160 + 13;
268     curstate.pos.v = rect.bottom - 33;
269     curstate.width = rect.right - (rect.left + 13 + 160) - (13 * 2);
270     for (i = 0; i < cb->nctrlsets; i++) {
271         if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
272                             cb->ctrlsets[i-1]->pathname)) {
273             curstate.pos.v = rect.top + 13;
274             curstate.panelnum++;
275             assert(curstate.panelnum < mcs->npanels);
276             dlg_listbox_add(&panellist, mcs, cb->ctrlsets[i]->pathname);
277         }
278         macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
279     }
280     macctrl_switchtopanel(mcs, 1);
281     /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
282 }
283
284 #define MAXCOLS 16
285
286 static void macctrl_layoutset(struct mac_layoutstate *curstate,
287                               struct controlset *s,
288                               WindowPtr window, struct macctrls *mcs)
289 {
290     unsigned int i, j, ncols, colstart;
291     struct mac_layoutstate cols[MAXCOLS];
292
293     fprintf(stderr, "--- begin set ---\n");
294     fprintf(stderr, "pathname = %s\n", s->pathname);
295     if (s->boxname && *s->boxname)
296         fprintf(stderr, "boxname = %s\n", s->boxname);
297     if (s->boxtitle)
298         fprintf(stderr, "boxtitle = %s\n", s->boxtitle);
299
300     cols[0] = *curstate;
301     ncols = 1;
302
303     for (i = 0; i < s->ncontrols; i++) {
304         union control *ctrl = s->ctrls[i];
305         char const *s;
306
307         colstart = COLUMN_START(ctrl->generic.column);
308         switch (ctrl->generic.type) {
309           case CTRL_TEXT: s = "text"; break;
310           case CTRL_EDITBOX: s = "editbox"; break;
311           case CTRL_RADIO: s = "radio"; break;
312           case CTRL_CHECKBOX: s = "checkbox"; break;
313           case CTRL_BUTTON: s = "button"; break;
314           case CTRL_LISTBOX: s = "listbox"; break;
315           case CTRL_COLUMNS: s = "columns"; break;
316           case CTRL_FILESELECT: s = "fileselect"; break;
317           case CTRL_FONTSELECT: s = "fontselect"; break;
318           case CTRL_TABDELAY: s = "tabdelay"; break;
319           default: s = "unknown"; break;
320         }
321         fprintf(stderr, "  control: %s\n", s);
322         switch (ctrl->generic.type) {
323           case CTRL_COLUMNS:
324             if (ctrl->columns.ncols != 1) {
325                 ncols = ctrl->columns.ncols;
326                 fprintf(stderr, "  split to %d\n", ncols);
327                 assert(ncols <= MAXCOLS);
328                 for (j = 0; j < ncols; j++) {
329                     cols[j] = cols[0];
330                     if (j > 0)
331                         cols[j].pos.h = cols[j-1].pos.h + cols[j-1].width + 6;
332                     if (j == ncols - 1)
333                         cols[j].width = curstate->width -
334                             (cols[j].pos.h - curstate->pos.h);
335                     else
336                         cols[j].width = (curstate->width + 6) * 
337                             ctrl->columns.percentages[j] / 100 - 6;
338                 }
339             } else {
340                 fprintf(stderr, "  join\n");
341                 for (j = 0; j < ncols; j++)
342                     if (cols[j].pos.v > cols[0].pos.v)
343                         cols[0].pos.v = cols[j].pos.v;
344                 cols[0].width = curstate->width;
345                 ncols = 1;
346             }
347             break;
348           case CTRL_TEXT:
349             macctrl_text(mcs, window, &cols[colstart], ctrl);
350             break;
351           case CTRL_EDITBOX:
352             macctrl_editbox(mcs, window, &cols[colstart], ctrl);
353             break;
354           case CTRL_RADIO:
355             macctrl_radio(mcs, window, &cols[colstart], ctrl);
356             break;
357           case CTRL_CHECKBOX:
358             macctrl_checkbox(mcs, window, &cols[colstart], ctrl);
359             break;
360           case CTRL_BUTTON:
361             macctrl_button(mcs, window, &cols[colstart], ctrl);
362             break;
363           case CTRL_LISTBOX:
364             if (ctrl->listbox.height == 0)
365                 macctrl_popup(mcs, window, &cols[colstart], ctrl);
366             else
367                 macctrl_listbox(mcs, window, &cols[colstart], ctrl);
368             break;
369         }
370     }
371     for (j = 0; j < ncols; j++)
372         if (cols[j].pos.v > curstate->pos.v)
373             curstate->pos.v = cols[j].pos.v;
374 }
375
376 static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
377 {
378     unsigned int i, j;
379     union macctrl *mc;
380
381 #define hideshow(c) do {                                                \
382     if (i == which) ShowControl(c); else HideControl(c);                \
383 } while (0)
384
385     mcs->curpanel = which;
386     /* Panel 0 is special and always visible. */
387     for (i = 1; i < mcs->npanels; i++)
388         for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
389 #if !TARGET_API_MAC_CARBON
390             if (mcs->focus == mc)
391                 macctrl_setfocus(mcs, NULL);
392 #endif
393             switch (mc->generic.type) {
394               case MACCTRL_TEXT:
395                 hideshow(mc->text.tbctrl);
396                 break;
397               case MACCTRL_EDITBOX:
398                 hideshow(mc->editbox.tbctrl);
399                 if (mc->editbox.tblabel != NULL)
400                     hideshow(mc->editbox.tblabel);
401                 break;
402               case MACCTRL_RADIO:
403                 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
404                     hideshow(mc->radio.tbctrls[j]);
405                 if (mc->radio.tblabel != NULL)
406                     hideshow(mc->radio.tblabel);
407                 break;
408               case MACCTRL_CHECKBOX:
409                 hideshow(mc->checkbox.tbctrl);
410                 break;
411               case MACCTRL_BUTTON:
412                 hideshow(mc->button.tbctrl);
413                 break;
414               case MACCTRL_LISTBOX:
415                 hideshow(mc->listbox.tbctrl);
416                 /*
417                  * At least under Mac OS 8.1, hiding a list box
418                  * doesn't hide its scroll bars.
419                  */
420 #if TARGET_API_MAC_CARBON
421                 hideshow(GetListVerticalScrollBar(mc->listbox.list));
422 #else
423                 hideshow((*mc->listbox.list)->vScroll);
424 #endif
425                 break;
426               case MACCTRL_POPUP:
427                 hideshow(mc->popup.tbctrl);
428                 break;
429             }
430         }
431 }
432
433 #if !TARGET_API_MAC_CARBON
434 /*
435  * System 7 focus manipulation
436  */
437 static void macctrl_defocus(union macctrl *mc)
438 {
439
440     assert(mac_gestalts.apprvers < 0x100);
441     switch (mc->generic.type) {
442       case MACCTRL_EDITBOX:
443         TEDeactivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
444         break;
445     }
446 }
447
448 static void macctrl_enfocus(union macctrl *mc)
449 {
450
451     assert(mac_gestalts.apprvers < 0x100);
452     switch (mc->generic.type) {
453       case MACCTRL_EDITBOX:
454         TEActivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
455         break;
456     }
457 }
458
459 static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc)
460 {
461
462     if (mcs->focus == mc)
463         return;
464     if (mcs->focus != NULL)
465         macctrl_defocus(mcs->focus);
466     mcs->focus = mc;
467     if (mc != NULL)
468         macctrl_enfocus(mc);
469 }
470 #endif
471
472 static void macctrl_text(struct macctrls *mcs, WindowPtr window,
473                          struct mac_layoutstate *curstate,
474                          union control *ctrl)
475 {
476     union macctrl *mc = snew(union macctrl);
477     Rect bounds;
478     SInt16 height;
479
480     assert(ctrl->text.label != NULL);
481     fprintf(stderr, "    label = %s\n", ctrl->text.label);
482     mc->generic.type = MACCTRL_TEXT;
483     mc->generic.ctrl = ctrl;
484     mc->generic.privdata = NULL;
485     bounds.left = curstate->pos.h;
486     bounds.right = bounds.left + curstate->width;
487     bounds.top = curstate->pos.v;
488     bounds.bottom = bounds.top + 16;
489     if (mac_gestalts.apprvers >= 0x100) {
490         Size olen;
491
492         mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
493                                      kControlStaticTextProc, (long)mc);
494         SetControlData(mc->text.tbctrl, kControlEntireControl,
495                        kControlStaticTextTextTag,
496                        strlen(ctrl->text.label), ctrl->text.label);
497         GetControlData(mc->text.tbctrl, kControlEntireControl,
498                        kControlStaticTextTextHeightTag,
499                        sizeof(height), &height, &olen);
500     }
501 #if !TARGET_API_MAC_CARBON
502     else {
503         TEHandle te;
504
505         mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
506                                      SYS7_TEXT_PROC, (long)mc);
507         te = (TEHandle)(*mc->text.tbctrl)->contrlData;
508         TESetText(ctrl->text.label, strlen(ctrl->text.label), te);
509         height = TEGetHeight(1, (*te)->nLines, te);
510     }
511 #endif
512     fprintf(stderr, "    height = %d\n", height);
513     SizeControl(mc->text.tbctrl, curstate->width, height);
514     curstate->pos.v += height + 6;
515     add234(mcs->byctrl, mc);
516     mc->generic.next = mcs->panels[curstate->panelnum];
517     mcs->panels[curstate->panelnum] = mc;
518 }
519
520 static void macctrl_editbox(struct macctrls *mcs, WindowPtr window,
521                             struct mac_layoutstate *curstate,
522                             union control *ctrl)
523 {
524     union macctrl *mc = snew(union macctrl);
525     Rect lbounds, bounds;
526
527     if (ctrl->editbox.label != NULL)
528         fprintf(stderr, "    label = %s\n", ctrl->editbox.label);
529     fprintf(stderr, "    percentwidth = %d\n", ctrl->editbox.percentwidth);
530     if (ctrl->editbox.password) fprintf(stderr, "    password\n");
531     if (ctrl->editbox.has_list) fprintf(stderr, "    has list\n");
532     mc->generic.type = MACCTRL_EDITBOX;
533     mc->generic.ctrl = ctrl;
534     mc->generic.privdata = NULL;
535     lbounds.left = curstate->pos.h;
536     lbounds.top = curstate->pos.v;
537     if (ctrl->editbox.percentwidth == 100) {
538         if (ctrl->editbox.label != NULL) {
539             lbounds.right = lbounds.left + curstate->width;
540             lbounds.bottom = lbounds.top + 16;
541             curstate->pos.v += 18;
542         }
543         bounds.left = curstate->pos.h;
544         bounds.right = bounds.left + curstate->width;
545     } else {
546         lbounds.right = lbounds.left +
547             curstate->width * (100 - ctrl->editbox.percentwidth) / 100;
548         lbounds.bottom = lbounds.top + 22;
549         bounds.left = lbounds.right;
550         bounds.right = lbounds.left + curstate->width;
551     }
552     bounds.top = curstate->pos.v;
553     bounds.bottom = bounds.top + 22;
554     if (mac_gestalts.apprvers >= 0x100) {
555         if (ctrl->editbox.label == NULL)
556             mc->editbox.tblabel = NULL;
557         else {
558             mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE,
559                                              0, 0, 0, kControlStaticTextProc,
560                                              (long)mc);
561             SetControlData(mc->editbox.tblabel, kControlEntireControl,
562                            kControlStaticTextTextTag,
563                            strlen(ctrl->editbox.label), ctrl->editbox.label);
564         }
565         InsetRect(&bounds, 3, 3);
566         mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
567                                         ctrl->editbox.password ?
568                                         kControlEditTextPasswordProc :
569                                         kControlEditTextProc, (long)mc);
570     }
571 #if !TARGET_API_MAC_CARBON
572     else {
573         if (ctrl->editbox.label == NULL)
574             mc->editbox.tblabel = NULL;
575         else {
576             mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE,
577                                              0, 0, 0, SYS7_TEXT_PROC,
578                                              (long)mc);
579             TESetText(ctrl->editbox.label, strlen(ctrl->editbox.label),
580                       (TEHandle)(*mc->editbox.tblabel)->contrlData);
581         }
582         mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
583                                         SYS7_EDITBOX_PROC, (long)mc);
584     }
585 #endif
586     curstate->pos.v += 28;
587     add234(mcs->byctrl, mc);
588     mc->generic.next = mcs->panels[curstate->panelnum];
589     mcs->panels[curstate->panelnum] = mc;
590     ctrlevent(mcs, mc, EVENT_REFRESH);
591 }
592
593 #if !TARGET_API_MAC_CARBON
594 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant,
595                                                ControlRef control,
596                                                ControlDefProcMessage msg,
597                                                SInt32 param)
598 {
599     RgnHandle rgn;
600     Rect rect;
601     TEHandle te;
602     long ssfs;
603     Point mouse;
604
605     switch (msg) {
606       case initCntl:
607         rect = (*control)->contrlRect;
608         if (variant == SYS7_EDITBOX_VARIANT)
609             InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */
610         te = TENew(&rect, &rect);
611         ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
612         (*te)->txSize = LoWord(ssfs);
613         (*te)->txFont = HiWord(ssfs);
614         (*control)->contrlData = (Handle)te;
615         return noErr;
616       case dispCntl:
617         TEDispose((TEHandle)(*control)->contrlData);
618         return 0;
619       case drawCntl:
620         if ((*control)->contrlVis) {
621             rect = (*control)->contrlRect;
622             if (variant == SYS7_EDITBOX_VARIANT) {
623                 PenNormal();
624                 FrameRect(&rect);
625                 InsetRect(&rect, 3, 3);
626             }
627             EraseRect(&rect);
628             (*(TEHandle)(*control)->contrlData)->viewRect = rect;
629             TEUpdate(&rect, (TEHandle)(*control)->contrlData);
630         }
631         return 0;
632       case testCntl:
633         if (variant == SYS7_TEXT_VARIANT)
634             return kControlNoPart;
635         mouse.h = LoWord(param);
636         mouse.v = HiWord(param);
637         rect = (*control)->contrlRect;
638         InsetRect(&rect, 3, 3);
639         return PtInRect(mouse, &rect) ? kControlEditTextPart : kControlNoPart;
640       case calcCRgns:
641         if (param & (1 << 31)) {
642             param &= ~(1 << 31);
643             goto calcthumbrgn;
644         }
645         /* FALLTHROUGH */
646       case calcCntlRgn:
647         rgn = (RgnHandle)param;
648         RectRgn(rgn, &(*control)->contrlRect);
649         return 0;
650       case calcThumbRgn:
651       calcthumbrgn:
652         rgn = (RgnHandle)param;
653         SetEmptyRgn(rgn);
654         return 0;
655     }
656
657     return 0;
658 }
659 #endif
660
661 static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
662                           struct mac_layoutstate *curstate,
663                           union control *ctrl)
664 {
665     union macctrl *mc = snew(union macctrl);
666     Rect bounds;
667     Str255 title;
668     unsigned int i, colwidth;
669
670     if (ctrl->radio.label != NULL)
671         fprintf(stderr, "    label = %s\n", ctrl->radio.label);
672     mc->generic.type = MACCTRL_RADIO;
673     mc->generic.ctrl = ctrl;
674     mc->generic.privdata = NULL;
675     mc->radio.tbctrls = snewn(ctrl->radio.nbuttons, ControlRef);
676     colwidth = (curstate->width + 13) / ctrl->radio.ncolumns;
677     bounds.top = curstate->pos.v;
678     bounds.bottom = bounds.top + 16;
679     bounds.left = curstate->pos.h;
680     bounds.right = bounds.left + curstate->width;
681     if (ctrl->radio.label == NULL)
682         mc->radio.tblabel = NULL;
683     else {
684         if (mac_gestalts.apprvers >= 0x100) {
685             mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE,
686                                            0, 0, 0, kControlStaticTextProc,
687                                            (long)mc);
688             SetControlData(mc->radio.tblabel, kControlEntireControl,
689                            kControlStaticTextTextTag,
690                            strlen(ctrl->radio.label), ctrl->radio.label);
691         }
692 #if !TARGET_API_MAC_CARBON
693         else {
694             mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE,
695                                            0, 0, 0, SYS7_TEXT_PROC, (long)mc);
696             TESetText(ctrl->radio.label, strlen(ctrl->radio.label),
697                       (TEHandle)(*mc->radio.tblabel)->contrlData);
698         }
699 #endif
700         curstate->pos.v += 18;
701     }
702     for (i = 0; i < ctrl->radio.nbuttons; i++) {
703         fprintf(stderr, "    button = %s\n", ctrl->radio.buttons[i]);
704         bounds.top = curstate->pos.v - 2;
705         bounds.bottom = bounds.top + 18;
706         bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
707         if (i == ctrl->radio.nbuttons - 1 ||
708             i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
709             bounds.right = curstate->pos.h + curstate->width;
710             curstate->pos.v += 18;
711         } else
712             bounds.right = bounds.left + colwidth - 13;
713         c2pstrcpy(title, ctrl->radio.buttons[i]);
714         mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE,
715                                           0, 0, 1, radioButProc, (long)mc);
716     }
717     curstate->pos.v += 4;
718     add234(mcs->byctrl, mc);
719     mc->generic.next = mcs->panels[curstate->panelnum];
720     mcs->panels[curstate->panelnum] = mc;
721     ctrlevent(mcs, mc, EVENT_REFRESH);
722 }
723
724 static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
725                              struct mac_layoutstate *curstate,
726                              union control *ctrl)
727 {
728     union macctrl *mc = snew(union macctrl);
729     Rect bounds;
730     Str255 title;
731
732     assert(ctrl->checkbox.label != NULL);
733     fprintf(stderr, "    label = %s\n", ctrl->checkbox.label);
734     mc->generic.type = MACCTRL_CHECKBOX;
735     mc->generic.ctrl = ctrl;
736     mc->generic.privdata = NULL;
737     bounds.left = curstate->pos.h;
738     bounds.right = bounds.left + curstate->width;
739     bounds.top = curstate->pos.v;
740     bounds.bottom = bounds.top + 16;
741     c2pstrcpy(title, ctrl->checkbox.label);
742     mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
743                                      checkBoxProc, (long)mc);
744     add234(mcs->byctrl, mc);
745     curstate->pos.v += 22;
746     mc->generic.next = mcs->panels[curstate->panelnum];
747     mcs->panels[curstate->panelnum] = mc;
748     ctrlevent(mcs, mc, EVENT_REFRESH);
749 }
750
751 static void macctrl_button(struct macctrls *mcs, WindowPtr window,
752                            struct mac_layoutstate *curstate,
753                            union control *ctrl)
754 {
755     union macctrl *mc = snew(union macctrl);
756     Rect bounds;
757     Str255 title;
758
759     assert(ctrl->button.label != NULL);
760     fprintf(stderr, "    label = %s\n", ctrl->button.label);
761     if (ctrl->button.isdefault)
762         fprintf(stderr, "    is default\n");
763     mc->generic.type = MACCTRL_BUTTON;
764     mc->generic.ctrl = ctrl;
765     mc->generic.privdata = NULL;
766     bounds.left = curstate->pos.h;
767     bounds.right = bounds.left + curstate->width;
768     bounds.top = curstate->pos.v;
769     bounds.bottom = bounds.top + 20;
770     c2pstrcpy(title, ctrl->button.label);
771     mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
772                                    pushButProc, (long)mc);
773     mc->button.tbring = NULL;
774     if (mac_gestalts.apprvers >= 0x100) {
775         Boolean isdefault = ctrl->button.isdefault;
776
777         SetControlData(mc->button.tbctrl, kControlEntireControl,
778                        kControlPushButtonDefaultTag,
779                        sizeof(isdefault), &isdefault);
780     } else if (ctrl->button.isdefault) {
781         InsetRect(&bounds, -4, -4);
782         mc->button.tbring = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
783                                        SYS7_DEFAULT_PROC, (long)mc);
784     }
785     if (mac_gestalts.apprvers >= 0x110) {
786         Boolean iscancel = ctrl->button.iscancel;
787
788         SetControlData(mc->button.tbctrl, kControlEntireControl,
789                        kControlPushButtonCancelTag,
790                        sizeof(iscancel), &iscancel);
791     }
792     if (ctrl->button.isdefault)
793         mcs->defbutton = mc;
794     if (ctrl->button.iscancel)
795         mcs->canbutton = mc;
796     add234(mcs->byctrl, mc);
797     mc->generic.next = mcs->panels[curstate->panelnum];
798     mcs->panels[curstate->panelnum] = mc;
799     curstate->pos.v += 26;
800 }
801
802 #if !TARGET_API_MAC_CARBON
803 static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
804                                                ControlRef control,
805                                                ControlDefProcMessage msg,
806                                                SInt32 param)
807 {
808     RgnHandle rgn;
809     Rect rect;
810     int oval;
811     PenState savestate;
812
813     switch (msg) {
814       case drawCntl:
815         if ((*control)->contrlVis) {
816             rect = (*control)->contrlRect;
817             GetPenState(&savestate);
818             PenNormal();
819             PenSize(3, 3);
820             if ((*control)->contrlHilite == kControlInactivePart)
821                 PenPat(&qd.gray);
822             oval = (rect.bottom - rect.top) / 2 + 2;
823             FrameRoundRect(&rect, oval, oval);
824             SetPenState(&savestate);
825         }
826         return 0;
827       case calcCRgns:
828         if (param & (1 << 31)) {
829             param &= ~(1 << 31);
830             goto calcthumbrgn;
831         }
832         /* FALLTHROUGH */
833       case calcCntlRgn:
834         rgn = (RgnHandle)param;
835         RectRgn(rgn, &(*control)->contrlRect);
836         return 0;
837       case calcThumbRgn:
838       calcthumbrgn:
839         rgn = (RgnHandle)param;
840         SetEmptyRgn(rgn);
841         return 0;
842     }
843
844     return 0;
845 }
846 #endif
847
848 static void macctrl_listbox(struct macctrls *mcs, WindowPtr window,
849                             struct mac_layoutstate *curstate,
850                             union control *ctrl)
851 {
852     union macctrl *mc = snew(union macctrl);
853     Rect bounds;
854     Size olen;
855
856     if (ctrl->listbox.label != NULL)
857         fprintf(stderr, "    label = %s\n", ctrl->listbox.label);
858     fprintf(stderr, "    height = %d\n", ctrl->listbox.height);
859     if (ctrl->listbox.draglist)
860         fprintf(stderr, "    draglist\n");
861     if (ctrl->listbox.multisel)
862         fprintf(stderr, "    multisel\n");
863     fprintf(stderr, "    ncols = %d\n", ctrl->listbox.ncols);
864     assert(ctrl->listbox.percentwidth == 100);
865     mc->generic.type = MACCTRL_LISTBOX;
866     mc->generic.ctrl = ctrl;
867     mc->generic.privdata = NULL;
868     /* The list starts off empty */
869     mc->listbox.nids = 0;
870     mc->listbox.ids = NULL;
871     bounds.left = curstate->pos.h;
872     bounds.right = bounds.left + curstate->width;
873     bounds.top = curstate->pos.v;
874     bounds.bottom = bounds.top + 16 * ctrl->listbox.height + 2;
875
876     if (mac_gestalts.apprvers >= 0x100) {
877         InsetRect(&bounds, 3, 3);
878         mc->listbox.tbctrl = NewControl(window, &bounds, NULL, TRUE,
879                                         ldes_Default, 0, 0,
880                                         kControlListBoxProc, (long)mc);
881         if (GetControlData(mc->listbox.tbctrl, kControlEntireControl,
882                            kControlListBoxListHandleTag,
883                            sizeof(mc->listbox.list), &mc->listbox.list,
884                            &olen) != noErr) {
885             DisposeControl(mc->listbox.tbctrl);
886             sfree(mc);
887             return;
888         }
889     }
890 #if !TARGET_API_MAC_CARBON
891     else {
892         InsetRect(&bounds, -3, -3);
893         mc->listbox.tbctrl = NewControl(window, &bounds, NULL, TRUE,
894                                         0, 0, 0,
895                                         SYS7_LISTBOX_PROC, (long)mc);
896         mc->listbox.list = (ListHandle)(*mc->listbox.tbctrl)->contrlData;
897         (*mc->listbox.list)->refCon = (long)mc;
898     }
899 #endif
900     if (!ctrl->listbox.multisel) {
901 #if TARGET_API_MAC_CARBON
902         SetListSelectionFlags(mc->listbox.list, lOnlyOne);
903 #else
904         (*mc->listbox.list)->selFlags = lOnlyOne;
905 #endif
906     }
907     add234(mcs->byctrl, mc);
908     curstate->pos.v += 6 + 20 * ctrl->listbox.height;
909     mc->generic.next = mcs->panels[curstate->panelnum];
910     mcs->panels[curstate->panelnum] = mc;
911     ctrlevent(mcs, mc, EVENT_REFRESH);
912 }
913
914 #if !TARGET_API_MAC_CARBON
915 static pascal SInt32 macctrl_sys7_listbox_cdef(SInt16 variant,
916                                                ControlRef control,
917                                                ControlDefProcMessage msg,
918                                                SInt32 param)
919 {
920     RgnHandle rgn;
921     Rect rect;
922     ListHandle list;
923     long ssfs;
924     Point mouse;
925     ListBounds bounds;
926     Point csize;
927     short savefont;
928     short savesize;
929     GrafPtr curport;
930
931     switch (msg) {
932       case initCntl:
933         rect = (*control)->contrlRect;
934         InsetRect(&rect, 4, 4);
935         rect.right -= 15; /* scroll bar */
936         bounds.top = bounds.bottom = bounds.left = 0;
937         bounds.right = 1;
938         csize.h = csize.v = 0;
939         GetPort(&curport);
940         savefont = curport->txFont;
941         savesize = curport->txSize;
942         ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
943         TextFont(HiWord(ssfs));
944         TextSize(LoWord(ssfs));
945         list = LNew(&rect, &bounds, csize, 0, (*control)->contrlOwner,
946                     TRUE, FALSE, FALSE, TRUE);
947         SetControlReference((*list)->vScroll, (long)list);
948         (*control)->contrlData = (Handle)list;
949         TextFont(savefont);
950         TextSize(savesize);
951         return noErr;
952       case dispCntl:
953         /*
954          * If the dialogue box is being destroyed, the scroll bar
955          * might have gone already.  In our situation, this is the
956          * only time we destroy a control, so NULL out the scroll bar
957          * handle to prevent LDispose trying to free it.
958          */
959         list = (ListHandle)(*control)->contrlData;
960         (*list)->vScroll = NULL;
961         LDispose(list);
962         return 0;
963       case drawCntl:
964         if ((*control)->contrlVis) {
965             rect = (*control)->contrlRect;
966             /* XXX input focus highlighting? */
967             InsetRect(&rect, 3, 3);
968             PenNormal();
969             FrameRect(&rect);
970             list = (ListHandle)(*control)->contrlData;
971             LActivate((*control)->contrlHilite != kControlInactivePart, list);
972             GetPort(&curport);
973             LUpdate(curport->visRgn, list);
974         }
975         return 0;
976       case testCntl:
977         mouse.h = LoWord(param);
978         mouse.v = HiWord(param);
979         rect = (*control)->contrlRect;
980         InsetRect(&rect, 4, 4);
981         /*
982          * We deliberately exclude the scrollbar so that LClick() can see it.
983          */
984         rect.right -= 15;
985         return PtInRect(mouse, &rect) ? kControlListBoxPart : kControlNoPart;
986       case calcCRgns:
987         if (param & (1 << 31)) {
988             param &= ~(1 << 31);
989             goto calcthumbrgn;
990         }
991         /* FALLTHROUGH */
992       case calcCntlRgn:
993         rgn = (RgnHandle)param;
994         RectRgn(rgn, &(*control)->contrlRect);
995         return 0;
996       case calcThumbRgn:
997       calcthumbrgn:
998         rgn = (RgnHandle)param;
999         SetEmptyRgn(rgn);
1000         return 0;
1001     }
1002
1003     return 0;
1004 }
1005 #endif
1006
1007 static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
1008                           struct mac_layoutstate *curstate,
1009                           union control *ctrl)
1010 {
1011     union macctrl *mc = snew(union macctrl);
1012     Rect bounds;
1013     Str255 title;
1014     unsigned int labelwidth;
1015     static int nextmenuid = MENU_MIN;
1016     int menuid;
1017     MenuRef menu;
1018
1019     /* 
1020      * <http://developer.apple.com/qa/tb/tb42.html> explains how to
1021      * create a popup menu with dynamic content.
1022      */
1023     assert(ctrl->listbox.height == 0);
1024     assert(!ctrl->listbox.draglist);
1025     assert(!ctrl->listbox.multisel);
1026
1027     if (ctrl->listbox.label != NULL)
1028         fprintf(stderr, "    label = %s\n", ctrl->listbox.label);
1029     fprintf(stderr, "    percentwidth = %d\n", ctrl->listbox.percentwidth);
1030
1031     mc->generic.type = MACCTRL_POPUP;
1032     mc->generic.ctrl = ctrl;
1033     mc->generic.privdata = NULL;
1034     c2pstrcpy(title, ctrl->button.label == NULL ? "" : ctrl->button.label);
1035
1036     /* Find a spare menu ID and create the menu */
1037     while (GetMenuHandle(nextmenuid) != NULL)
1038         if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
1039     menuid = nextmenuid++;
1040     menu = NewMenu(menuid, "\pdummy");
1041     if (menu == NULL) return;
1042     mc->popup.menu = menu;
1043     mc->popup.menuid = menuid;
1044     InsertMenu(menu, kInsertHierarchicalMenu);
1045
1046     /* The menu starts off empty */
1047     mc->popup.nids = 0;
1048     mc->popup.ids = NULL;
1049
1050     bounds.left = curstate->pos.h;
1051     bounds.right = bounds.left + curstate->width;
1052     bounds.top = curstate->pos.v;
1053     bounds.bottom = bounds.top + 20;
1054     /* XXX handle percentwidth == 100 */
1055     labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
1056     mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE,
1057                                   popupTitleLeftJust, menuid, labelwidth,
1058                                   popupMenuProc + popupFixedWidth, (long)mc);
1059     add234(mcs->byctrl, mc);
1060     curstate->pos.v += 26;
1061     mc->generic.next = mcs->panels[curstate->panelnum];
1062     mcs->panels[curstate->panelnum] = mc;
1063     ctrlevent(mcs, mc, EVENT_REFRESH);
1064 }
1065
1066
1067 void macctrl_activate(WindowPtr window, EventRecord *event)
1068 {
1069     struct macctrls *mcs = mac_winctrls(window);
1070     Boolean active = (event->modifiers & activeFlag) != 0;
1071     GrafPtr saveport;
1072     int i, j;
1073     ControlPartCode state;
1074     union macctrl *mc;
1075
1076     GetPort(&saveport);
1077     SetPort((GrafPtr)GetWindowPort(window));
1078     if (mac_gestalts.apprvers >= 0x100)
1079         SetThemeWindowBackground(window, active ?
1080                                  kThemeBrushModelessDialogBackgroundActive :
1081                                  kThemeBrushModelessDialogBackgroundInactive,
1082                                  TRUE);
1083     state = active ? kControlNoPart : kControlInactivePart;
1084     for (i = 0; i <= mcs->curpanel; i += mcs->curpanel)
1085         for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
1086             switch (mc->generic.type) {
1087               case MACCTRL_TEXT:
1088                 HiliteControl(mc->text.tbctrl, state);
1089                 break;
1090               case MACCTRL_EDITBOX:
1091                 HiliteControl(mc->editbox.tbctrl, state);
1092                 if (mc->editbox.tblabel != NULL)
1093                     HiliteControl(mc->editbox.tblabel, state);
1094                 break;
1095               case MACCTRL_RADIO:
1096                 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
1097                     HiliteControl(mc->radio.tbctrls[j], state);
1098                 if (mc->radio.tblabel != NULL)
1099                     HiliteControl(mc->radio.tblabel, state);
1100                 break;
1101               case MACCTRL_CHECKBOX:
1102                 HiliteControl(mc->checkbox.tbctrl, state);
1103                 break;
1104               case MACCTRL_BUTTON:
1105                 HiliteControl(mc->button.tbctrl, state);
1106                 if (mc->button.tbring != NULL)
1107                     HiliteControl(mc->button.tbring, state);                
1108                 break;
1109               case MACCTRL_LISTBOX:
1110                 HiliteControl(mc->listbox.tbctrl, state);
1111                 break;
1112               case MACCTRL_POPUP:
1113                 HiliteControl(mc->popup.tbctrl, state);
1114                 break;
1115             }
1116 #if !TARGET_API_MAC_CARBON
1117             if (mcs->focus == mc) {
1118                 if (active)
1119                     macctrl_enfocus(mc);
1120                 else
1121                     macctrl_defocus(mc);
1122             }
1123 #endif
1124         }
1125     SetPort(saveport);
1126 }
1127
1128 void macctrl_click(WindowPtr window, EventRecord *event)
1129 {
1130     Point mouse;
1131     ControlHandle control, oldfocus;
1132     int part, trackresult;
1133     GrafPtr saveport;
1134     union macctrl *mc;
1135     struct macctrls *mcs = mac_winctrls(window);
1136     int i;
1137     UInt32 features;
1138
1139     GetPort(&saveport);
1140     SetPort((GrafPtr)GetWindowPort(window));
1141     mouse = event->where;
1142     GlobalToLocal(&mouse);
1143     part = FindControl(mouse, window, &control);
1144     if (control != NULL) {
1145 #if !TARGET_API_MAC_CARBON
1146         /*
1147          * Special magic for scroll bars in list boxes, whose refcon
1148          * is the list.
1149          */
1150         if (part == kControlUpButtonPart || part == kControlDownButtonPart ||
1151             part == kControlPageUpPart || part == kControlPageDownPart ||
1152             part == kControlIndicatorPart)
1153             mc = (union macctrl *)
1154                 (*(ListHandle)GetControlReference(control))->refCon;
1155        else
1156 #endif
1157             mc = (union macctrl *)GetControlReference(control);
1158         fprintf(stderr, "control = %p, part = %d,  mc = %p\n",
1159                 control, part, mc);
1160         if (mac_gestalts.apprvers >= 0x100) {
1161             if (GetControlFeatures(control, &features) == noErr &&
1162                 (features & kControlSupportsFocus) &&
1163                 (features & kControlGetsFocusOnClick) &&
1164                 GetKeyboardFocus(window, &oldfocus) == noErr &&
1165                 control != oldfocus)
1166                 SetKeyboardFocus(window, control, part);
1167             trackresult = HandleControlClick(control, mouse, event->modifiers,
1168                                              (ControlActionUPP)-1);
1169         } else {
1170 #if !TARGET_API_MAC_CARBON
1171             if (mc->generic.type == MACCTRL_EDITBOX &&
1172                 control == mc->editbox.tbctrl) {
1173                 TEHandle te = (TEHandle)(*control)->contrlData;
1174
1175                 macctrl_setfocus(mcs, mc);
1176                 TEClick(mouse, !!(event->modifiers & shiftKey), te);
1177                 goto done;
1178             }
1179             if (mc->generic.type == MACCTRL_LISTBOX &&
1180                 (control == mc->listbox.tbctrl ||
1181                  control == (*mc->listbox.list)->vScroll)) {
1182
1183                 fprintf(stderr, "list = %p\n", mc->listbox.list);
1184                 macctrl_setfocus(mcs, mc);
1185                 if (LClick(mouse, event->modifiers, mc->listbox.list))
1186                     /* double-click */
1187                     ctrlevent(mcs, mc, EVENT_ACTION);
1188                 else
1189                     ctrlevent(mcs, mc, EVENT_SELCHANGE);
1190                 goto done;
1191             }
1192 #endif
1193             trackresult = TrackControl(control, mouse, (ControlActionUPP)-1);
1194         }
1195         switch (mc->generic.type) {
1196           case MACCTRL_RADIO:
1197             if (trackresult != 0) {
1198                 for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
1199                     if (mc->radio.tbctrls[i] == control)
1200                         SetControlValue(mc->radio.tbctrls[i],
1201                                         kControlRadioButtonCheckedValue);
1202                     else
1203                         SetControlValue(mc->radio.tbctrls[i],
1204                                         kControlRadioButtonUncheckedValue);
1205                 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1206             }
1207             break;
1208           case MACCTRL_CHECKBOX:
1209             if (trackresult != 0) {
1210                 SetControlValue(control, !GetControlValue(control));
1211                 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1212             }
1213             break;
1214           case MACCTRL_BUTTON:
1215             if (trackresult != 0)
1216                 ctrlevent(mcs, mc, EVENT_ACTION);
1217             break;
1218           case MACCTRL_LISTBOX:
1219             /* FIXME spot double-click */
1220             ctrlevent(mcs, mc, EVENT_SELCHANGE);
1221             break;
1222           case MACCTRL_POPUP:
1223             ctrlevent(mcs, mc, EVENT_SELCHANGE);
1224             break;
1225         }
1226     }
1227   done:
1228     SetPort(saveport);
1229 }
1230
1231 void macctrl_key(WindowPtr window, EventRecord *event)
1232 {
1233     ControlRef control;
1234     struct macctrls *mcs = mac_winctrls(window);
1235     union macctrl *mc;
1236     unsigned long dummy;
1237
1238     switch (event->message & charCodeMask) {
1239       case kEnterCharCode:
1240       case kReturnCharCode:
1241         if (mcs->defbutton != NULL) {
1242             assert(mcs->defbutton->generic.type == MACCTRL_BUTTON);
1243             HiliteControl(mcs->defbutton->button.tbctrl, kControlButtonPart);
1244             /*
1245              * I'd like to delay unhilighting the button until after
1246              * the event has been processed, but by them the entire
1247              * dialgue box might have been destroyed.
1248              */
1249             Delay(6, &dummy);
1250             HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart);
1251             ctrlevent(mcs, mcs->defbutton, EVENT_ACTION);
1252         }
1253         return;
1254       case kEscapeCharCode:
1255         if (mcs->canbutton != NULL) {
1256             assert(mcs->canbutton->generic.type == MACCTRL_BUTTON);
1257             HiliteControl(mcs->canbutton->button.tbctrl, kControlButtonPart);
1258             Delay(6, &dummy);
1259             HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart);
1260             ctrlevent(mcs, mcs->canbutton, EVENT_ACTION);
1261         }
1262         return;
1263     }
1264     if (mac_gestalts.apprvers >= 0x100) {
1265         if (GetKeyboardFocus(window, &control) == noErr && control != NULL) {
1266             HandleControlKey(control, (event->message & keyCodeMask) >> 8,
1267                              event->message & charCodeMask, event->modifiers);
1268             mc = (union macctrl *)GetControlReference(control);
1269             switch (mc->generic.type) {
1270               case MACCTRL_LISTBOX:
1271                 ctrlevent(mcs, mc, EVENT_SELCHANGE);
1272                 break;
1273               default:
1274                 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1275                 break;
1276             }
1277         }
1278     }
1279 #if !TARGET_API_MAC_CARBON
1280     else {
1281         TEHandle te;
1282
1283         if (mcs->focus != NULL) {
1284             mc = mcs->focus;
1285             switch (mc->generic.type) {
1286               case MACCTRL_EDITBOX:
1287                 te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1288                 TEKey(event->message & charCodeMask, te);
1289                 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1290                 break;
1291             }
1292         }
1293     }
1294 #endif
1295 }
1296
1297 void macctrl_update(WindowPtr window)
1298 {
1299 #if TARGET_API_MAC_CARBON
1300     RgnHandle visrgn;
1301 #endif
1302     Rect rect;
1303     GrafPtr saveport;
1304
1305     BeginUpdate(window);
1306     GetPort(&saveport);
1307     SetPort((GrafPtr)GetWindowPort(window));
1308     if (mac_gestalts.apprvers >= 0x101) {
1309 #if TARGET_API_MAC_CARBON
1310         GetPortBounds(GetWindowPort(window), &rect);
1311 #else
1312         rect = window->portRect;
1313 #endif
1314         InsetRect(&rect, -1, -1);
1315         DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ?
1316                                      kThemeStateActive : kThemeStateInactive);
1317     }
1318 #if TARGET_API_MAC_CARBON
1319     visrgn = NewRgn();
1320     GetPortVisibleRegion(GetWindowPort(window), visrgn);
1321     UpdateControls(window, visrgn);
1322     DisposeRgn(visrgn);
1323 #else
1324     UpdateControls(window, window->visRgn);
1325 #endif
1326     SetPort(saveport);
1327     EndUpdate(window);
1328 }
1329
1330 #if TARGET_API_MAC_CARBON
1331 #define EnableItem EnableMenuItem
1332 #define DisableItem DisableMenuItem
1333 #endif
1334 void macctrl_adjustmenus(WindowPtr window)
1335 {
1336     MenuHandle menu;
1337
1338     menu = GetMenuHandle(mFile);
1339     DisableItem(menu, iSave); /* XXX enable if modified */
1340     EnableItem(menu, iSaveAs);
1341     EnableItem(menu, iDuplicate);
1342
1343     menu = GetMenuHandle(mEdit);
1344     DisableItem(menu, 0);
1345 }
1346
1347 void macctrl_close(WindowPtr window)
1348 {
1349     struct macctrls *mcs = mac_winctrls(window);
1350     union macctrl *mc;
1351
1352     /*
1353      * Mostly, we don't bother disposing of the Toolbox controls,
1354      * since that will happen automatically when the window is
1355      * disposed of.  Popup menus are an exception, because we have to
1356      * dispose of the menu ourselves, and doing that while the control
1357      * still holds a reference to it seems rude.
1358      */
1359     while ((mc = index234(mcs->byctrl, 0)) != NULL) {
1360         if (mc->generic.privdata != NULL && mc->generic.freeprivdata)
1361             sfree(mc->generic.privdata);
1362         switch (mc->generic.type) {
1363           case MACCTRL_POPUP:
1364             DisposeControl(mc->popup.tbctrl);
1365             DeleteMenu(mc->popup.menuid);
1366             DisposeMenu(mc->popup.menu);
1367             break;
1368         }
1369         del234(mcs->byctrl, mc);
1370         sfree(mc);
1371     }
1372
1373     freetree234(mcs->byctrl);
1374     mcs->byctrl = NULL;
1375     sfree(mcs->panels);
1376     mcs->panels = NULL;
1377 }
1378
1379 void dlg_update_start(union control *ctrl, void *dlg)
1380 {
1381
1382     /* No-op for now */
1383 }
1384
1385 void dlg_update_done(union control *ctrl, void *dlg)
1386 {
1387
1388     /* No-op for now */
1389 }
1390
1391 void dlg_set_focus(union control *ctrl, void *dlg)
1392 {
1393
1394     if (mac_gestalts.apprvers >= 0x100) {
1395         /* Use SetKeyboardFocus() */
1396     } else {
1397         /* Do our own mucking around */
1398     }
1399 }
1400
1401 union control *dlg_last_focused(union control *ctrl, void *dlg)
1402 {
1403
1404     return NULL;
1405 }
1406
1407 void dlg_beep(void *dlg)
1408 {
1409
1410     SysBeep(30);
1411 }
1412
1413 void dlg_error_msg(void *dlg, char *msg)
1414 {
1415     Str255 pmsg;
1416
1417     c2pstrcpy(pmsg, msg);
1418     ParamText(pmsg, NULL, NULL, NULL);
1419     StopAlert(128, NULL);
1420 }
1421
1422 void dlg_end(void *dlg, int value)
1423 {
1424     struct macctrls *mcs = dlg;
1425
1426     if (mcs->end != NULL)
1427         (*mcs->end)(mcs->window, value);
1428 };
1429
1430 void dlg_refresh(union control *ctrl, void *dlg)
1431 {
1432     struct macctrls *mcs = dlg;
1433     union macctrl *mc;
1434
1435     if (ctrl == NULL)
1436         return; /* FIXME */
1437     mc = findbyctrl(mcs, ctrl);
1438     assert(mc != NULL);
1439     ctrlevent(mcs, mc, EVENT_REFRESH);
1440 };
1441
1442 void *dlg_get_privdata(union control *ctrl, void *dlg)
1443 {
1444     struct macctrls *mcs = dlg;
1445     union macctrl *mc = findbyctrl(mcs, ctrl);
1446
1447     assert(mc != NULL);
1448     return mc->generic.privdata;
1449 }
1450
1451 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
1452 {
1453     struct macctrls *mcs = dlg;
1454     union macctrl *mc = findbyctrl(mcs, ctrl);
1455
1456     assert(mc != NULL);
1457     mc->generic.privdata = ptr;
1458     mc->generic.freeprivdata = FALSE;
1459 }
1460
1461 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
1462 {
1463     struct macctrls *mcs = dlg;
1464     union macctrl *mc = findbyctrl(mcs, ctrl);
1465
1466     assert(mc != NULL);
1467     mc->generic.privdata = smalloc(size);
1468     mc->generic.freeprivdata = TRUE;
1469     return mc->generic.privdata;
1470 }
1471
1472
1473 /*
1474  * Radio Button control
1475  */
1476
1477 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
1478 {
1479     struct macctrls *mcs = dlg;
1480     union macctrl *mc = findbyctrl(mcs, ctrl);
1481     int i;
1482
1483     if (mc == NULL) return;
1484     for (i = 0; i < ctrl->radio.nbuttons; i++) {
1485         if (i == whichbutton)
1486             SetControlValue(mc->radio.tbctrls[i],
1487                             kControlRadioButtonCheckedValue);
1488         else
1489             SetControlValue(mc->radio.tbctrls[i],
1490                             kControlRadioButtonUncheckedValue);
1491     }
1492
1493 };
1494
1495 int dlg_radiobutton_get(union control *ctrl, void *dlg)
1496 {
1497     struct macctrls *mcs = dlg;
1498     union macctrl *mc = findbyctrl(mcs, ctrl);
1499     int i;
1500
1501     assert(mc != NULL);
1502     for (i = 0; i < ctrl->radio.nbuttons; i++) {
1503         if (GetControlValue(mc->radio.tbctrls[i])  ==
1504             kControlRadioButtonCheckedValue)
1505             return i;
1506     }
1507     return -1;
1508 };
1509
1510
1511 /*
1512  * Check Box control
1513  */
1514
1515 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
1516 {
1517     struct macctrls *mcs = dlg;
1518     union macctrl *mc = findbyctrl(mcs, ctrl);
1519
1520     if (mc == NULL) return;
1521     SetControlValue(mc->checkbox.tbctrl,
1522                     checked ? kControlCheckBoxCheckedValue :
1523                               kControlCheckBoxUncheckedValue);
1524 }
1525
1526 int dlg_checkbox_get(union control *ctrl, void *dlg)
1527 {
1528     struct macctrls *mcs = dlg;
1529     union macctrl *mc = findbyctrl(mcs, ctrl);
1530
1531     assert(mc != NULL);
1532     return GetControlValue(mc->checkbox.tbctrl);
1533 }
1534
1535
1536 /*
1537  * Edit Box control
1538  */
1539
1540 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
1541 {
1542     struct macctrls *mcs = dlg;
1543     union macctrl *mc = findbyctrl(mcs, ctrl);
1544     GrafPtr saveport;
1545
1546     if (mc == NULL) return;
1547     assert(mc->generic.type == MACCTRL_EDITBOX);
1548     GetPort(&saveport);
1549     SetPort((GrafPtr)(GetWindowPort(mcs->window)));
1550     if (mac_gestalts.apprvers >= 0x100)
1551         SetControlData(mc->editbox.tbctrl, kControlEntireControl,
1552                        ctrl->editbox.password ?
1553                        kControlEditTextPasswordTag :
1554                        kControlEditTextTextTag,
1555                        strlen(text), text);
1556 #if !TARGET_API_MAC_CARBON
1557     else
1558         TESetText(text, strlen(text),
1559                   (TEHandle)(*mc->editbox.tbctrl)->contrlData);
1560 #endif
1561     DrawOneControl(mc->editbox.tbctrl);
1562     SetPort(saveport);
1563 }
1564
1565 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
1566 {
1567     struct macctrls *mcs = dlg;
1568     union macctrl *mc = findbyctrl(mcs, ctrl);
1569     Size olen;
1570
1571     assert(mc != NULL);
1572     assert(mc->generic.type == MACCTRL_EDITBOX);
1573     if (mac_gestalts.apprvers >= 0x100) {
1574         if (GetControlData(mc->editbox.tbctrl, kControlEntireControl,
1575                            ctrl->editbox.password ?
1576                            kControlEditTextPasswordTag :
1577                            kControlEditTextTextTag,
1578                            length - 1, buffer, &olen) != noErr)
1579             olen = 0;
1580         if (olen > length - 1)
1581             olen = length - 1;
1582     }
1583 #if !TARGET_API_MAC_CARBON
1584     else {
1585         TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1586
1587         olen = (*te)->teLength;
1588         if (olen > length - 1)
1589             olen = length - 1;
1590         memcpy(buffer, *(*te)->hText, olen);
1591     }
1592 #endif
1593     buffer[olen] = '\0';
1594     fprintf(stderr, "dlg_editbox_get: %s\n", buffer);
1595 }
1596
1597
1598 /*
1599  * List Box control
1600  */
1601
1602 static void dlg_macpopup_clear(union control *ctrl, void *dlg)
1603 {
1604     struct macctrls *mcs = dlg;
1605     union macctrl *mc = findbyctrl(mcs, ctrl);
1606     MenuRef menu = mc->popup.menu;
1607     unsigned int i, n;
1608
1609     if (mc == NULL) return;
1610     fprintf(stderr, "      popup_clear\n");
1611     n = CountMenuItems(menu);
1612     for (i = 0; i < n; i++)
1613         DeleteMenuItem(menu, n - i);
1614     mc->popup.nids = 0;
1615     sfree(mc->popup.ids);
1616     mc->popup.ids = NULL;
1617     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1618 }
1619
1620 static void dlg_maclist_clear(union control *ctrl, void *dlg)
1621 {
1622     struct macctrls *mcs = dlg;
1623     union macctrl *mc = findbyctrl(mcs, ctrl);
1624
1625     if (mc == NULL) return;
1626     fprintf(stderr, "      maclist_clear\n");
1627     LDelRow(0, 0, mc->listbox.list);
1628     mc->listbox.nids = 0;
1629     sfree(mc->listbox.ids);
1630     mc->listbox.ids = NULL;
1631     DrawOneControl(mc->listbox.tbctrl);
1632 }
1633
1634 void dlg_listbox_clear(union control *ctrl, void *dlg)
1635 {
1636
1637     switch (ctrl->generic.type) {
1638       case CTRL_LISTBOX:
1639         if (ctrl->listbox.height == 0)
1640             dlg_macpopup_clear(ctrl, dlg);
1641         else
1642             dlg_maclist_clear(ctrl, dlg);
1643         break;
1644     }
1645 }
1646
1647 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
1648 {
1649     struct macctrls *mcs = dlg;
1650     union macctrl *mc = findbyctrl(mcs, ctrl);
1651     MenuRef menu = mc->popup.menu;
1652
1653     if (mc == NULL) return;
1654     fprintf(stderr, "      popup_del %d\n", index);
1655     DeleteMenuItem(menu, index + 1);
1656     if (mc->popup.ids != NULL)
1657         memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
1658                (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
1659     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1660 }
1661
1662 static void dlg_maclist_del(union control *ctrl, void *dlg, int index)
1663 {
1664     struct macctrls *mcs = dlg;
1665     union macctrl *mc = findbyctrl(mcs, ctrl);
1666
1667     if (mc == NULL) return;
1668     fprintf(stderr, "      maclist_del %d\n", index);
1669     LDelRow(1, index, mc->listbox.list);
1670     if (mc->listbox.ids != NULL)
1671         memcpy(mc->listbox.ids + index, mc->listbox.ids + index + 1,
1672                (mc->listbox.nids - index - 1) * sizeof(*mc->listbox.ids));
1673     DrawOneControl(mc->listbox.tbctrl);
1674 }
1675
1676 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
1677 {
1678
1679     switch (ctrl->generic.type) {
1680       case CTRL_LISTBOX:
1681         if (ctrl->listbox.height == 0)
1682             dlg_macpopup_del(ctrl, dlg, index);
1683         else
1684             dlg_maclist_del(ctrl, dlg, index);
1685         break;
1686     }
1687 }
1688
1689 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
1690 {
1691     struct macctrls *mcs = dlg;
1692     union macctrl *mc = findbyctrl(mcs, ctrl);
1693     MenuRef menu = mc->popup.menu;
1694     Str255 itemstring;
1695
1696     if (mc == NULL) return;
1697     fprintf(stderr, "      popup_add %s\n", text);
1698     assert(text[0] != '\0');
1699     c2pstrcpy(itemstring, text);
1700     AppendMenu(menu, "\pdummy");
1701     SetMenuItemText(menu, CountMenuItems(menu), itemstring);
1702     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1703 }
1704
1705
1706 static void dlg_maclist_add(union control *ctrl, void *dlg, char const *text)
1707 {
1708     struct macctrls *mcs = dlg;
1709     union macctrl *mc = findbyctrl(mcs, ctrl);
1710     ListBounds bounds;
1711     Cell cell = { 0, 0 };
1712
1713     if (mc == NULL) return;
1714     fprintf(stderr, "      maclist_add %s\n", text);
1715 #if TARGET_API_MAC_CARBON
1716     GetListDataBounds(mc->listbox.list, &bounds);
1717 #else
1718     bounds = (*mc->listbox.list)->dataBounds;
1719 #endif
1720     cell.v = bounds.bottom;
1721     LAddRow(1, cell.v, mc->listbox.list);
1722     LSetCell(text, strlen(text), cell, mc->listbox.list);
1723     DrawOneControl(mc->listbox.tbctrl);
1724 }
1725
1726 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
1727 {
1728
1729     switch (ctrl->generic.type) {
1730       case CTRL_LISTBOX:
1731         if (ctrl->listbox.height == 0)
1732             dlg_macpopup_add(ctrl, dlg, text);
1733         else
1734             dlg_maclist_add(ctrl, dlg, text);
1735         break;
1736     }
1737 }
1738
1739 static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
1740                                    char const *text, int id)
1741 {
1742     struct macctrls *mcs = dlg;
1743     union macctrl *mc = findbyctrl(mcs, ctrl);
1744     MenuRef menu = mc->popup.menu;
1745     unsigned int index;
1746
1747     if (mc == NULL) return;
1748     fprintf(stderr, "      popup_addwthid %s, %d\n", text, id);
1749     dlg_macpopup_add(ctrl, dlg, text);
1750     index = CountMenuItems(menu) - 1;
1751     if (mc->popup.nids <= index) {
1752         mc->popup.nids = index + 1;
1753         mc->popup.ids = sresize(mc->popup.ids, mc->popup.nids, int);
1754     }
1755     mc->popup.ids[index] = id;
1756 }
1757
1758 static void dlg_maclist_addwithid(union control *ctrl, void *dlg,
1759                                   char const *text, int id)
1760 {
1761     struct macctrls *mcs = dlg;
1762     union macctrl *mc = findbyctrl(mcs, ctrl);
1763     ListBounds bounds;
1764     int index;
1765
1766     if (mc == NULL) return;
1767     fprintf(stderr, "      maclist_addwithid %s %d\n", text, id);
1768     dlg_maclist_add(ctrl, dlg, text);
1769 #if TARGET_API_MAC_CARBON
1770     GetListDataBounds(mc->listbox.list, &bounds);
1771 #else
1772     bounds = (*mc->listbox.list)->dataBounds;
1773 #endif
1774     index = bounds.bottom;
1775     if (mc->listbox.nids <= index) {
1776         mc->listbox.nids = index + 1;
1777         mc->listbox.ids = sresize(mc->listbox.ids, mc->listbox.nids, int);
1778     }
1779     mc->listbox.ids[index] = id;
1780 }
1781
1782 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
1783                            char const *text, int id)
1784 {
1785
1786     switch (ctrl->generic.type) {
1787       case CTRL_LISTBOX:
1788         if (ctrl->listbox.height == 0)
1789             dlg_macpopup_addwithid(ctrl, dlg, text, id);
1790         else
1791             dlg_maclist_addwithid(ctrl, dlg, text, id);
1792         break;
1793     }
1794 }
1795
1796 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
1797 {
1798     struct macctrls *mcs = dlg;
1799     union macctrl *mc = findbyctrl(mcs, ctrl);
1800
1801     assert(mc != NULL);
1802     switch (ctrl->generic.type) {
1803       case CTRL_LISTBOX:
1804         if (ctrl->listbox.height == 0) {
1805             assert(mc->popup.ids != NULL && mc->popup.nids > index);
1806             return mc->popup.ids[index];
1807         } else {
1808             assert(mc->listbox.ids != NULL && mc->listbox.nids > index);
1809             return mc->listbox.ids[index];
1810         }
1811     }
1812     return -1;
1813 }
1814
1815 int dlg_listbox_index(union control *ctrl, void *dlg)
1816 {
1817     struct macctrls *mcs = dlg;
1818     union macctrl *mc = findbyctrl(mcs, ctrl);
1819     Cell cell = { 0, 0 };
1820
1821     assert(mc != NULL);
1822     switch (ctrl->generic.type) {
1823       case CTRL_LISTBOX:
1824         if (ctrl->listbox.height == 0)
1825             return GetControlValue(mc->popup.tbctrl) - 1;
1826         else {
1827             if (LGetSelect(TRUE, &cell, mc->listbox.list))
1828                 return cell.v;
1829             else
1830                 return -1;
1831         }
1832     }
1833     return -1;
1834 }
1835
1836 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1837 {
1838     struct macctrls *mcs = dlg;
1839     union macctrl *mc = findbyctrl(mcs, ctrl);
1840     Cell cell = { 0, 0 };
1841
1842     assert(mc != NULL);
1843     switch (ctrl->generic.type) {
1844       case CTRL_LISTBOX:
1845         if (ctrl->listbox.height == 0)
1846             return GetControlValue(mc->popup.tbctrl) - 1 == index;
1847         else {
1848             cell.v = index;
1849             return LGetSelect(FALSE, &cell, mc->listbox.list);
1850         }
1851     }
1852     return FALSE;
1853 }
1854
1855 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1856 {
1857     struct macctrls *mcs = dlg;
1858     union macctrl *mc = findbyctrl(mcs, ctrl);
1859
1860     if (mc == NULL) return;
1861     switch (ctrl->generic.type) {
1862       case CTRL_LISTBOX:
1863         if (ctrl->listbox.height == 0)
1864             SetControlValue(mc->popup.tbctrl, index + 1);
1865         break;
1866     }
1867 }
1868
1869
1870 /*
1871  * Text control
1872  */
1873
1874 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1875 {
1876     struct macctrls *mcs = dlg;
1877     union macctrl *mc = findbyctrl(mcs, ctrl);
1878
1879     if (mc == NULL) return;
1880     if (mac_gestalts.apprvers >= 0x100)
1881         SetControlData(mc->text.tbctrl, kControlEntireControl,
1882                        kControlStaticTextTextTag, strlen(text), text);
1883 #if !TARGET_API_MAC_CARBON
1884     else
1885         TESetText(text, strlen(text),
1886                   (TEHandle)(*mc->text.tbctrl)->contrlData);
1887 #endif
1888 }
1889
1890
1891 /*
1892  * File Selector control
1893  */
1894
1895 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1896 {
1897
1898 }
1899
1900 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1901 {
1902
1903 }
1904
1905
1906 /*
1907  * Font Selector control
1908  */
1909
1910 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1911 {
1912
1913 }
1914
1915 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1916 {
1917
1918 }
1919
1920
1921 /*
1922  * Printer enumeration
1923  */
1924
1925 printer_enum *printer_start_enum(int *nprinters)
1926 {
1927
1928     *nprinters = 0;
1929     return NULL;
1930 }
1931
1932 char *printer_get_name(printer_enum *pe, int thing)
1933 {
1934
1935     return "<none>";
1936 }
1937
1938 void printer_finish_enum(printer_enum *pe)
1939 {
1940
1941 }
1942
1943
1944 /*
1945  * Colour selection stuff
1946  */
1947
1948 void dlg_coloursel_start(union control *ctrl, void *dlg,
1949                          int r, int g, int b)
1950 {
1951     struct macctrls *mcs = dlg;
1952     union macctrl *mc = findbyctrl(mcs, ctrl);
1953     Point where = {-1, -1}; /* Screen with greatest colour depth */
1954     RGBColor incolour;
1955
1956     if (HAVE_COLOR_QD()) {
1957         incolour.red = r * 0x0101;
1958         incolour.green = g * 0x0101;
1959         incolour.blue = b * 0x0101;
1960         mcs->gotcolour = GetColor(where, "\pModify Colour:", &incolour,
1961                                   &mcs->thecolour);
1962         ctrlevent(mcs, mc, EVENT_CALLBACK);
1963     } else
1964         dlg_beep(dlg);
1965 }
1966
1967 int dlg_coloursel_results(union control *ctrl, void *dlg,
1968                           int *r, int *g, int *b)
1969 {
1970     struct macctrls *mcs = dlg;
1971
1972     if (mcs->gotcolour) {
1973         *r = mcs->thecolour.red >> 8;
1974         *g = mcs->thecolour.green >> 8;
1975         *b = mcs->thecolour.blue >> 8;
1976         return 1;
1977     } else
1978         return 0;
1979 }
1980
1981 /*
1982  * Local Variables:
1983  * c-file-style: "simon"
1984  * End:
1985  */