]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac/macctrls.c
Make Change Settings do something.
[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
1413     if (ctrl == NULL)
1414         return; /* FIXME */
1415     mc = findbyctrl(mcs, ctrl);
1416     assert(mc != NULL);
1417     ctrlevent(mcs, mc, EVENT_REFRESH);
1418 };
1419
1420 void *dlg_get_privdata(union control *ctrl, void *dlg)
1421 {
1422     struct macctrls *mcs = dlg;
1423     union macctrl *mc = findbyctrl(mcs, ctrl);
1424
1425     assert(mc != NULL);
1426     return mc->generic.privdata;
1427 }
1428
1429 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
1430 {
1431     struct macctrls *mcs = dlg;
1432     union macctrl *mc = findbyctrl(mcs, ctrl);
1433
1434     assert(mc != NULL);
1435     mc->generic.privdata = ptr;
1436     mc->generic.freeprivdata = FALSE;
1437 }
1438
1439 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
1440 {
1441     struct macctrls *mcs = dlg;
1442     union macctrl *mc = findbyctrl(mcs, ctrl);
1443
1444     assert(mc != NULL);
1445     mc->generic.privdata = smalloc(size);
1446     mc->generic.freeprivdata = TRUE;
1447     return mc->generic.privdata;
1448 }
1449
1450
1451 /*
1452  * Radio Button control
1453  */
1454
1455 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
1456 {
1457     struct macctrls *mcs = dlg;
1458     union macctrl *mc = findbyctrl(mcs, ctrl);
1459     int i;
1460
1461     if (mc == NULL) return;
1462     for (i = 0; i < ctrl->radio.nbuttons; i++) {
1463         if (i == whichbutton)
1464             SetControlValue(mc->radio.tbctrls[i],
1465                             kControlRadioButtonCheckedValue);
1466         else
1467             SetControlValue(mc->radio.tbctrls[i],
1468                             kControlRadioButtonUncheckedValue);
1469     }
1470
1471 };
1472
1473 int dlg_radiobutton_get(union control *ctrl, void *dlg)
1474 {
1475     struct macctrls *mcs = dlg;
1476     union macctrl *mc = findbyctrl(mcs, ctrl);
1477     int i;
1478
1479     assert(mc != NULL);
1480     for (i = 0; i < ctrl->radio.nbuttons; i++) {
1481         if (GetControlValue(mc->radio.tbctrls[i])  ==
1482             kControlRadioButtonCheckedValue)
1483             return i;
1484     }
1485     return -1;
1486 };
1487
1488
1489 /*
1490  * Check Box control
1491  */
1492
1493 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
1494 {
1495     struct macctrls *mcs = dlg;
1496     union macctrl *mc = findbyctrl(mcs, ctrl);
1497
1498     if (mc == NULL) return;
1499     SetControlValue(mc->checkbox.tbctrl,
1500                     checked ? kControlCheckBoxCheckedValue :
1501                               kControlCheckBoxUncheckedValue);
1502 }
1503
1504 int dlg_checkbox_get(union control *ctrl, void *dlg)
1505 {
1506     struct macctrls *mcs = dlg;
1507     union macctrl *mc = findbyctrl(mcs, ctrl);
1508
1509     assert(mc != NULL);
1510     return GetControlValue(mc->checkbox.tbctrl);
1511 }
1512
1513
1514 /*
1515  * Edit Box control
1516  */
1517
1518 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
1519 {
1520     struct macctrls *mcs = dlg;
1521     union macctrl *mc = findbyctrl(mcs, ctrl);
1522     GrafPtr saveport;
1523
1524     if (mc == NULL) return;
1525     assert(mc->generic.type == MACCTRL_EDITBOX);
1526     GetPort(&saveport);
1527     SetPort((GrafPtr)(GetWindowPort(mcs->window)));
1528     if (mac_gestalts.apprvers >= 0x100)
1529         SetControlData(mc->editbox.tbctrl, kControlEntireControl,
1530                        ctrl->editbox.password ?
1531                        kControlEditTextPasswordTag :
1532                        kControlEditTextTextTag,
1533                        strlen(text), text);
1534 #if !TARGET_API_MAC_CARBON
1535     else
1536         TESetText(text, strlen(text),
1537                   (TEHandle)(*mc->editbox.tbctrl)->contrlData);
1538 #endif
1539     DrawOneControl(mc->editbox.tbctrl);
1540     SetPort(saveport);
1541 }
1542
1543 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
1544 {
1545     struct macctrls *mcs = dlg;
1546     union macctrl *mc = findbyctrl(mcs, ctrl);
1547     Size olen;
1548
1549     assert(mc != NULL);
1550     assert(mc->generic.type == MACCTRL_EDITBOX);
1551     if (mac_gestalts.apprvers >= 0x100) {
1552         if (GetControlData(mc->editbox.tbctrl, kControlEntireControl,
1553                            ctrl->editbox.password ?
1554                            kControlEditTextPasswordTag :
1555                            kControlEditTextTextTag,
1556                            length - 1, buffer, &olen) != noErr)
1557             olen = 0;
1558         if (olen > length - 1)
1559             olen = length - 1;
1560     }
1561 #if !TARGET_API_MAC_CARBON
1562     else {
1563         TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1564
1565         olen = (*te)->teLength;
1566         if (olen > length - 1)
1567             olen = length - 1;
1568         memcpy(buffer, *(*te)->hText, olen);
1569     }
1570 #endif
1571     buffer[olen] = '\0';
1572 }
1573
1574
1575 /*
1576  * List Box control
1577  */
1578
1579 static void dlg_macpopup_clear(union control *ctrl, void *dlg)
1580 {
1581     struct macctrls *mcs = dlg;
1582     union macctrl *mc = findbyctrl(mcs, ctrl);
1583     MenuRef menu = mc->popup.menu;
1584     unsigned int i, n;
1585
1586     if (mc == NULL) return;
1587     n = CountMenuItems(menu);
1588     for (i = 0; i < n; i++)
1589         DeleteMenuItem(menu, n - i);
1590     mc->popup.nids = 0;
1591     sfree(mc->popup.ids);
1592     mc->popup.ids = NULL;
1593     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1594 }
1595
1596 static void dlg_maclist_clear(union control *ctrl, void *dlg)
1597 {
1598     struct macctrls *mcs = dlg;
1599     union macctrl *mc = findbyctrl(mcs, ctrl);
1600
1601     if (mc == NULL) return;
1602     LDelRow(0, 0, mc->listbox.list);
1603     mc->listbox.nids = 0;
1604     sfree(mc->listbox.ids);
1605     mc->listbox.ids = NULL;
1606     DrawOneControl(mc->listbox.tbctrl);
1607 }
1608
1609 void dlg_listbox_clear(union control *ctrl, void *dlg)
1610 {
1611
1612     switch (ctrl->generic.type) {
1613       case CTRL_LISTBOX:
1614         if (ctrl->listbox.height == 0)
1615             dlg_macpopup_clear(ctrl, dlg);
1616         else
1617             dlg_maclist_clear(ctrl, dlg);
1618         break;
1619     }
1620 }
1621
1622 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
1623 {
1624     struct macctrls *mcs = dlg;
1625     union macctrl *mc = findbyctrl(mcs, ctrl);
1626     MenuRef menu = mc->popup.menu;
1627
1628     if (mc == NULL) return;
1629     DeleteMenuItem(menu, index + 1);
1630     if (mc->popup.ids != NULL)
1631         memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
1632                (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
1633     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1634 }
1635
1636 static void dlg_maclist_del(union control *ctrl, void *dlg, int index)
1637 {
1638     struct macctrls *mcs = dlg;
1639     union macctrl *mc = findbyctrl(mcs, ctrl);
1640
1641     if (mc == NULL) return;
1642     LDelRow(1, index, mc->listbox.list);
1643     if (mc->listbox.ids != NULL)
1644         memcpy(mc->listbox.ids + index, mc->listbox.ids + index + 1,
1645                (mc->listbox.nids - index - 1) * sizeof(*mc->listbox.ids));
1646     DrawOneControl(mc->listbox.tbctrl);
1647 }
1648
1649 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
1650 {
1651
1652     switch (ctrl->generic.type) {
1653       case CTRL_LISTBOX:
1654         if (ctrl->listbox.height == 0)
1655             dlg_macpopup_del(ctrl, dlg, index);
1656         else
1657             dlg_maclist_del(ctrl, dlg, index);
1658         break;
1659     }
1660 }
1661
1662 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
1663 {
1664     struct macctrls *mcs = dlg;
1665     union macctrl *mc = findbyctrl(mcs, ctrl);
1666     MenuRef menu = mc->popup.menu;
1667     Str255 itemstring;
1668
1669     if (mc == NULL) return;
1670     assert(text[0] != '\0');
1671     c2pstrcpy(itemstring, text);
1672     AppendMenu(menu, "\pdummy");
1673     SetMenuItemText(menu, CountMenuItems(menu), itemstring);
1674     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1675 }
1676
1677
1678 static void dlg_maclist_add(union control *ctrl, void *dlg, char const *text)
1679 {
1680     struct macctrls *mcs = dlg;
1681     union macctrl *mc = findbyctrl(mcs, ctrl);
1682     ListBounds bounds;
1683     Cell cell = { 0, 0 };
1684
1685     if (mc == NULL) return;
1686 #if TARGET_API_MAC_CARBON
1687     GetListDataBounds(mc->listbox.list, &bounds);
1688 #else
1689     bounds = (*mc->listbox.list)->dataBounds;
1690 #endif
1691     cell.v = bounds.bottom;
1692     LAddRow(1, cell.v, mc->listbox.list);
1693     LSetCell(text, strlen(text), cell, mc->listbox.list);
1694     DrawOneControl(mc->listbox.tbctrl);
1695 }
1696
1697 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
1698 {
1699
1700     switch (ctrl->generic.type) {
1701       case CTRL_LISTBOX:
1702         if (ctrl->listbox.height == 0)
1703             dlg_macpopup_add(ctrl, dlg, text);
1704         else
1705             dlg_maclist_add(ctrl, dlg, text);
1706         break;
1707     }
1708 }
1709
1710 static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
1711                                    char const *text, int id)
1712 {
1713     struct macctrls *mcs = dlg;
1714     union macctrl *mc = findbyctrl(mcs, ctrl);
1715     MenuRef menu = mc->popup.menu;
1716     unsigned int index;
1717
1718     if (mc == NULL) return;
1719     dlg_macpopup_add(ctrl, dlg, text);
1720     index = CountMenuItems(menu) - 1;
1721     if (mc->popup.nids <= index) {
1722         mc->popup.nids = index + 1;
1723         mc->popup.ids = sresize(mc->popup.ids, mc->popup.nids, int);
1724     }
1725     mc->popup.ids[index] = id;
1726 }
1727
1728 static void dlg_maclist_addwithid(union control *ctrl, void *dlg,
1729                                   char const *text, int id)
1730 {
1731     struct macctrls *mcs = dlg;
1732     union macctrl *mc = findbyctrl(mcs, ctrl);
1733     ListBounds bounds;
1734     int index;
1735
1736     if (mc == NULL) return;
1737     dlg_maclist_add(ctrl, dlg, text);
1738 #if TARGET_API_MAC_CARBON
1739     GetListDataBounds(mc->listbox.list, &bounds);
1740 #else
1741     bounds = (*mc->listbox.list)->dataBounds;
1742 #endif
1743     index = bounds.bottom;
1744     if (mc->listbox.nids <= index) {
1745         mc->listbox.nids = index + 1;
1746         mc->listbox.ids = sresize(mc->listbox.ids, mc->listbox.nids, int);
1747     }
1748     mc->listbox.ids[index] = id;
1749 }
1750
1751 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
1752                            char const *text, int id)
1753 {
1754
1755     switch (ctrl->generic.type) {
1756       case CTRL_LISTBOX:
1757         if (ctrl->listbox.height == 0)
1758             dlg_macpopup_addwithid(ctrl, dlg, text, id);
1759         else
1760             dlg_maclist_addwithid(ctrl, dlg, text, id);
1761         break;
1762     }
1763 }
1764
1765 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
1766 {
1767     struct macctrls *mcs = dlg;
1768     union macctrl *mc = findbyctrl(mcs, ctrl);
1769
1770     assert(mc != NULL);
1771     switch (ctrl->generic.type) {
1772       case CTRL_LISTBOX:
1773         if (ctrl->listbox.height == 0) {
1774             assert(mc->popup.ids != NULL && mc->popup.nids > index);
1775             return mc->popup.ids[index];
1776         } else {
1777             assert(mc->listbox.ids != NULL && mc->listbox.nids > index);
1778             return mc->listbox.ids[index];
1779         }
1780     }
1781     return -1;
1782 }
1783
1784 int dlg_listbox_index(union control *ctrl, void *dlg)
1785 {
1786     struct macctrls *mcs = dlg;
1787     union macctrl *mc = findbyctrl(mcs, ctrl);
1788     Cell cell = { 0, 0 };
1789
1790     assert(mc != NULL);
1791     switch (ctrl->generic.type) {
1792       case CTRL_LISTBOX:
1793         if (ctrl->listbox.height == 0)
1794             return GetControlValue(mc->popup.tbctrl) - 1;
1795         else {
1796             if (LGetSelect(TRUE, &cell, mc->listbox.list))
1797                 return cell.v;
1798             else
1799                 return -1;
1800         }
1801     }
1802     return -1;
1803 }
1804
1805 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1806 {
1807     struct macctrls *mcs = dlg;
1808     union macctrl *mc = findbyctrl(mcs, ctrl);
1809     Cell cell = { 0, 0 };
1810
1811     assert(mc != NULL);
1812     switch (ctrl->generic.type) {
1813       case CTRL_LISTBOX:
1814         if (ctrl->listbox.height == 0)
1815             return GetControlValue(mc->popup.tbctrl) - 1 == index;
1816         else {
1817             cell.v = index;
1818             return LGetSelect(FALSE, &cell, mc->listbox.list);
1819         }
1820     }
1821     return FALSE;
1822 }
1823
1824 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1825 {
1826     struct macctrls *mcs = dlg;
1827     union macctrl *mc = findbyctrl(mcs, ctrl);
1828
1829     if (mc == NULL) return;
1830     switch (ctrl->generic.type) {
1831       case CTRL_LISTBOX:
1832         if (ctrl->listbox.height == 0)
1833             SetControlValue(mc->popup.tbctrl, index + 1);
1834         break;
1835     }
1836 }
1837
1838
1839 /*
1840  * Text control
1841  */
1842
1843 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1844 {
1845     struct macctrls *mcs = dlg;
1846     union macctrl *mc = findbyctrl(mcs, ctrl);
1847
1848     if (mc == NULL) return;
1849     if (mac_gestalts.apprvers >= 0x100)
1850         SetControlData(mc->text.tbctrl, kControlEntireControl,
1851                        kControlStaticTextTextTag, strlen(text), text);
1852 #if !TARGET_API_MAC_CARBON
1853     else
1854         TESetText(text, strlen(text),
1855                   (TEHandle)(*mc->text.tbctrl)->contrlData);
1856 #endif
1857 }
1858
1859
1860 /*
1861  * File Selector control
1862  */
1863
1864 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1865 {
1866
1867 }
1868
1869 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1870 {
1871
1872 }
1873
1874
1875 /*
1876  * Font Selector control
1877  */
1878
1879 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1880 {
1881
1882 }
1883
1884 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1885 {
1886
1887 }
1888
1889
1890 /*
1891  * Printer enumeration
1892  */
1893
1894 printer_enum *printer_start_enum(int *nprinters)
1895 {
1896
1897     *nprinters = 0;
1898     return NULL;
1899 }
1900
1901 char *printer_get_name(printer_enum *pe, int thing)
1902 {
1903
1904     return "<none>";
1905 }
1906
1907 void printer_finish_enum(printer_enum *pe)
1908 {
1909
1910 }
1911
1912
1913 /*
1914  * Colour selection stuff
1915  */
1916
1917 void dlg_coloursel_start(union control *ctrl, void *dlg,
1918                          int r, int g, int b)
1919 {
1920     struct macctrls *mcs = dlg;
1921     union macctrl *mc = findbyctrl(mcs, ctrl);
1922     Point where = {-1, -1}; /* Screen with greatest colour depth */
1923     RGBColor incolour;
1924
1925     if (HAVE_COLOR_QD()) {
1926         incolour.red = r * 0x0101;
1927         incolour.green = g * 0x0101;
1928         incolour.blue = b * 0x0101;
1929         mcs->gotcolour = GetColor(where, "\pModify Colour:", &incolour,
1930                                   &mcs->thecolour);
1931         ctrlevent(mcs, mc, EVENT_CALLBACK);
1932     } else
1933         dlg_beep(dlg);
1934 }
1935
1936 int dlg_coloursel_results(union control *ctrl, void *dlg,
1937                           int *r, int *g, int *b)
1938 {
1939     struct macctrls *mcs = dlg;
1940
1941     if (mcs->gotcolour) {
1942         *r = mcs->thecolour.red >> 8;
1943         *g = mcs->thecolour.green >> 8;
1944         *b = mcs->thecolour.blue >> 8;
1945         return 1;
1946     } else
1947         return 0;
1948 }
1949
1950 /*
1951  * Local Variables:
1952  * c-file-style: "simon"
1953  * End:
1954  */