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