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