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