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