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