]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - mac/macctrls.c
ce3ac75b7d6c23dd12fe1ad292980411b35a05a3
[PuTTY.git] / mac / macctrls.c
1 /* $Id: macctrls.c,v 1.29 2003/04/05 18:00:46 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 <Controls.h>
31 #include <ControlDefinitions.h>
32 #include <Events.h>
33 #include <Menus.h>
34 #include <Resources.h>
35 #include <Script.h>
36 #include <Sound.h>
37 #include <TextEdit.h>
38 #include <TextUtils.h>
39 #include <ToolUtils.h>
40 #include <Windows.h>
41
42 #include <assert.h>
43 #include <string.h>
44
45 #include "putty.h"
46 #include "mac.h"
47 #include "macresid.h"
48 #include "dialog.h"
49 #include "tree234.h"
50
51 /* Range of menu IDs for popup menus */
52 #define MENU_MIN        1024
53 #define MENU_MAX        2048
54
55
56 union macctrl {
57     struct macctrl_generic {
58         enum {
59             MACCTRL_TEXT,
60             MACCTRL_EDITBOX,
61             MACCTRL_RADIO,
62             MACCTRL_CHECKBOX,
63             MACCTRL_BUTTON,
64             MACCTRL_POPUP
65         } type;
66         /* Template from which this was generated */
67         union control *ctrl;
68         /* Next control in this panel */
69         union macctrl *next;
70         void *privdata;
71         int freeprivdata;
72     } generic;
73     struct {
74         struct macctrl_generic generic;
75         ControlRef tbctrl;
76     } text;
77     struct {
78         struct macctrl_generic generic;
79         ControlRef tbctrl;
80         ControlRef tblabel;
81     } editbox;
82     struct {
83         struct macctrl_generic generic;
84         ControlRef *tbctrls;
85         ControlRef tblabel;
86     } radio;
87     struct {
88         struct macctrl_generic generic;
89         ControlRef tbctrl;
90     } checkbox;
91     struct {
92         struct macctrl_generic generic;
93         ControlRef tbctrl;
94         ControlRef tbring;
95     } button;
96     struct {
97         struct macctrl_generic generic;
98         ControlRef tbctrl;
99         MenuRef menu;
100         int menuid;
101         unsigned int nids;
102         int *ids;
103     } popup;
104 };
105
106 struct mac_layoutstate {
107     Point pos;
108     unsigned int width;
109     unsigned int panelnum;
110 };
111
112 #define ctrlevent(mcs, mc, event) do {                                  \
113     if ((mc)->generic.ctrl->generic.handler != NULL)                    \
114         (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
115                                                (mcs)->data, (event));   \
116 } while (0)
117
118 #define findbyctrl(mcs, ctrl)                                           \
119     find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
120
121 static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *, 
122                               WindowPtr, struct macctrls *);
123 static void macctrl_switchtopanel(struct macctrls *, unsigned int);
124 static void macctrl_setfocus(struct macctrls *, union macctrl *);
125 static void macctrl_text(struct macctrls *, WindowPtr,
126                          struct mac_layoutstate *, union control *);
127 static void macctrl_editbox(struct macctrls *, WindowPtr,
128                             struct mac_layoutstate *, union control *);
129 static void macctrl_radio(struct macctrls *, WindowPtr,
130                           struct mac_layoutstate *, union control *);
131 static void macctrl_checkbox(struct macctrls *, WindowPtr,
132                              struct mac_layoutstate *, union control *);
133 static void macctrl_button(struct macctrls *, WindowPtr,
134                            struct mac_layoutstate *, union control *);
135 static void macctrl_popup(struct macctrls *, WindowPtr,
136                           struct mac_layoutstate *, union control *);
137 #if !TARGET_API_MAC_CARBON
138 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef,
139                                                ControlDefProcMessage, SInt32);
140 static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
141                                                ControlDefProcMessage, SInt32);
142 #endif
143
144 #if !TARGET_API_MAC_CARBON
145 /*
146  * This trick enables us to keep all the CDEF code in the main
147  * application, which makes life easier.  For details, see
148  * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
149  */
150
151 #pragma options align=mac68k
152 typedef struct {
153     short               jmpabs; /* 4EF9 */
154     ControlDefUPP       theUPP;
155 } **PatchCDEF;
156 #pragma options align=reset
157 #endif
158
159 static void macctrl_init()
160 {
161 #if !TARGET_API_MAC_CARBON
162     static int inited = 0;
163     PatchCDEF cdef;
164
165     if (inited) return;
166     cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox);
167     (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef);
168     cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
169     (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
170     inited = 1;
171 #endif
172 }
173
174
175 static int macctrl_cmp_byctrl(void *av, void *bv)
176 {
177     union macctrl *a = (union macctrl *)av;
178     union macctrl *b = (union macctrl *)bv;
179
180     if (a->generic.ctrl < b->generic.ctrl)
181         return -1;
182     else if (a->generic.ctrl > b->generic.ctrl)
183         return +1;
184     else
185         return 0;
186 }
187
188 static int macctrl_cmp_byctrl_find(void *av, void *bv)
189 {
190     union control *a = (union control *)av;
191     union macctrl *b = (union macctrl *)bv;
192
193     if (a < b->generic.ctrl)
194         return -1;
195     else if (a > b->generic.ctrl)
196         return +1;
197     else
198         return 0;
199 }
200
201 void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
202                        struct macctrls *mcs)
203 {
204     int i;
205     struct mac_layoutstate curstate;
206     ControlRef root;
207     Rect rect;
208
209     macctrl_init();
210 #if TARGET_API_MAC_CARBON
211     GetPortBounds(GetWindowPort(window), &rect);
212 #else
213     rect = window->portRect;
214 #endif
215     curstate.pos.h = rect.left + 13;
216     curstate.pos.v = rect.bottom - 33;
217     curstate.width = rect.right - rect.left - (13 * 2);
218     if (mac_gestalts.apprvers >= 0x100)
219         CreateRootControl(window, &root);
220     mcs->window = window;
221     mcs->byctrl = newtree234(macctrl_cmp_byctrl);
222     mcs->focus = NULL;
223     mcs->defbutton = NULL;
224     mcs->canbutton = NULL;
225     /* Count the number of panels */
226     mcs->npanels = 1;
227     for (i = 1; i < cb->nctrlsets; i++)
228         if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
229             mcs->npanels++;
230     mcs->panels = snewn(mcs->npanels, union macctrl *);
231     memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
232     curstate.panelnum = 0;
233     for (i = 0; i < cb->nctrlsets; i++) {
234         if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
235                             cb->ctrlsets[i-1]->pathname)) {
236             curstate.pos.v = rect.top + 13;
237             curstate.panelnum++;
238             assert(curstate.panelnum < mcs->npanels);
239         }
240         macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
241     }
242     macctrl_switchtopanel(mcs, 1);
243     /* 14 = proxies, 20 = SSH bugs */
244 }
245
246 #define MAXCOLS 16
247
248 static void macctrl_layoutset(struct mac_layoutstate *curstate,
249                               struct controlset *s,
250                               WindowPtr window, struct macctrls *mcs)
251 {
252     unsigned int i, j, ncols, colstart;
253     struct mac_layoutstate cols[MAXCOLS];
254
255     fprintf(stderr, "--- begin set ---\n");
256     fprintf(stderr, "pathname = %s\n", s->pathname);
257     if (s->boxname && *s->boxname)
258         fprintf(stderr, "boxname = %s\n", s->boxname);
259     if (s->boxtitle)
260         fprintf(stderr, "boxtitle = %s\n", s->boxtitle);
261
262     cols[0] = *curstate;
263     ncols = 1;
264
265     for (i = 0; i < s->ncontrols; i++) {
266         union control *ctrl = s->ctrls[i];
267         char const *s;
268
269         colstart = COLUMN_START(ctrl->generic.column);
270         switch (ctrl->generic.type) {
271           case CTRL_TEXT: s = "text"; break;
272           case CTRL_EDITBOX: s = "editbox"; break;
273           case CTRL_RADIO: s = "radio"; break;
274           case CTRL_CHECKBOX: s = "checkbox"; break;
275           case CTRL_BUTTON: s = "button"; break;
276           case CTRL_LISTBOX: s = "listbox"; break;
277           case CTRL_COLUMNS: s = "columns"; break;
278           case CTRL_FILESELECT: s = "fileselect"; break;
279           case CTRL_FONTSELECT: s = "fontselect"; break;
280           case CTRL_TABDELAY: s = "tabdelay"; break;
281           default: s = "unknown"; break;
282         }
283         fprintf(stderr, "  control: %s\n", s);
284         switch (ctrl->generic.type) {
285           case CTRL_COLUMNS:
286             if (ctrl->columns.ncols != 1) {
287                 ncols = ctrl->columns.ncols;
288                 fprintf(stderr, "  split to %d\n", ncols);
289                 assert(ncols <= MAXCOLS);
290                 for (j = 0; j < ncols; j++) {
291                     cols[j] = cols[0];
292                     if (j > 0)
293                         cols[j].pos.h = cols[j-1].pos.h + cols[j-1].width + 6;
294                     if (j == ncols - 1)
295                         cols[j].width = curstate->width -
296                             (cols[j].pos.h - curstate->pos.h);
297                     else
298                         cols[j].width = (curstate->width + 6) * 
299                             ctrl->columns.percentages[j] / 100 - 6;
300                 }
301             } else {
302                 fprintf(stderr, "  join\n");
303                 for (j = 0; j < ncols; j++)
304                     if (cols[j].pos.v > cols[0].pos.v)
305                         cols[0].pos.v = cols[j].pos.v;
306                 cols[0].width = curstate->width;
307                 ncols = 1;
308             }
309             break;
310           case CTRL_TEXT:
311             macctrl_text(mcs, window, &cols[colstart], ctrl);
312             break;
313           case CTRL_EDITBOX:
314             macctrl_editbox(mcs, window, &cols[colstart], ctrl);
315             break;
316           case CTRL_RADIO:
317             macctrl_radio(mcs, window, &cols[colstart], ctrl);
318             break;
319           case CTRL_CHECKBOX:
320             macctrl_checkbox(mcs, window, &cols[colstart], ctrl);
321             break;
322           case CTRL_BUTTON:
323             macctrl_button(mcs, window, &cols[colstart], ctrl);
324             break;
325           case CTRL_LISTBOX:
326             if (ctrl->listbox.height == 0)
327                 macctrl_popup(mcs, window, &cols[colstart], ctrl);
328             break;
329         }
330     }
331     for (j = 0; j < ncols; j++)
332         if (cols[j].pos.v > curstate->pos.v)
333             curstate->pos.v = cols[j].pos.v;
334 }
335
336 static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
337 {
338     unsigned int i, j;
339     union macctrl *mc;
340
341 #define hideshow(c) do {                                                \
342     if (i == which) ShowControl(c); else HideControl(c);                \
343 } while (0)
344
345     mcs->curpanel = which;
346     /* Panel 0 is special and always visible. */
347     for (i = 1; i < mcs->npanels; i++)
348         for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
349 #if !TARGET_API_MAC_CARBON
350             if (mcs->focus == mc)
351                 macctrl_setfocus(mcs, NULL);
352 #endif
353             switch (mc->generic.type) {
354               case MACCTRL_TEXT:
355                 hideshow(mc->text.tbctrl);
356                 break;
357               case MACCTRL_EDITBOX:
358                 hideshow(mc->editbox.tbctrl);
359                 if (mc->editbox.tblabel != NULL)
360                     hideshow(mc->editbox.tblabel);
361                 break;
362               case MACCTRL_RADIO:
363                 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
364                     hideshow(mc->radio.tbctrls[j]);
365                 if (mc->radio.tblabel != NULL)
366                     hideshow(mc->radio.tblabel);
367                 break;
368               case MACCTRL_CHECKBOX:
369                 hideshow(mc->checkbox.tbctrl);
370                 break;
371               case MACCTRL_BUTTON:
372                 hideshow(mc->button.tbctrl);
373                 break;
374               case MACCTRL_POPUP:
375                 hideshow(mc->popup.tbctrl);
376                 break;
377             }
378         }
379 }
380
381 #if !TARGET_API_MAC_CARBON
382 /*
383  * System 7 focus manipulation
384  */
385 static void macctrl_defocus(union macctrl *mc)
386 {
387
388     assert(mac_gestalts.apprvers < 0x100);
389     switch (mc->generic.type) {
390       case MACCTRL_EDITBOX:
391         TEDeactivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
392         break;
393     }
394 }
395
396 static void macctrl_enfocus(union macctrl *mc)
397 {
398
399     assert(mac_gestalts.apprvers < 0x100);
400     switch (mc->generic.type) {
401       case MACCTRL_EDITBOX:
402         TEActivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
403         break;
404     }
405 }
406
407 static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc)
408 {
409
410     if (mcs->focus != NULL)
411         macctrl_defocus(mcs->focus);
412     mcs->focus = mc;
413     if (mc != NULL)
414         macctrl_enfocus(mc);
415 }
416 #endif
417
418 static void macctrl_text(struct macctrls *mcs, WindowPtr window,
419                          struct mac_layoutstate *curstate,
420                          union control *ctrl)
421 {
422     union macctrl *mc = snew(union macctrl);
423     Rect bounds;
424     SInt16 height;
425
426     assert(ctrl->text.label != NULL);
427     fprintf(stderr, "    label = %s\n", ctrl->text.label);
428     mc->generic.type = MACCTRL_TEXT;
429     mc->generic.ctrl = ctrl;
430     mc->generic.privdata = NULL;
431     bounds.left = curstate->pos.h;
432     bounds.right = bounds.left + curstate->width;
433     bounds.top = curstate->pos.v;
434     bounds.bottom = bounds.top + 16;
435     if (mac_gestalts.apprvers >= 0x100) {
436         Size olen;
437
438         mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
439                                      kControlStaticTextProc, (long)mc);
440         SetControlData(mc->text.tbctrl, kControlEntireControl,
441                        kControlStaticTextTextTag,
442                        strlen(ctrl->text.label), ctrl->text.label);
443         GetControlData(mc->text.tbctrl, kControlEntireControl,
444                        kControlStaticTextTextHeightTag,
445                        sizeof(height), &height, &olen);
446     }
447 #if !TARGET_API_MAC_CARBON
448     else {
449         TEHandle te;
450
451         mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
452                                      SYS7_TEXT_PROC, (long)mc);
453         te = (TEHandle)(*mc->text.tbctrl)->contrlData;
454         TESetText(ctrl->text.label, strlen(ctrl->text.label), te);
455         height = TEGetHeight(1, (*te)->nLines, te);
456     }
457 #endif
458     fprintf(stderr, "    height = %d\n", height);
459     SizeControl(mc->text.tbctrl, curstate->width, height);
460     curstate->pos.v += height + 6;
461     add234(mcs->byctrl, mc);
462     mc->generic.next = mcs->panels[curstate->panelnum];
463     mcs->panels[curstate->panelnum] = mc;
464 }
465
466 static void macctrl_editbox(struct macctrls *mcs, WindowPtr window,
467                             struct mac_layoutstate *curstate,
468                             union control *ctrl)
469 {
470     union macctrl *mc = snew(union macctrl);
471     Rect lbounds, bounds;
472
473     if (ctrl->editbox.label != NULL)
474         fprintf(stderr, "    label = %s\n", ctrl->editbox.label);
475     fprintf(stderr, "    percentwidth = %d\n", ctrl->editbox.percentwidth);
476     if (ctrl->editbox.password) fprintf(stderr, "    password\n");
477     if (ctrl->editbox.has_list) fprintf(stderr, "    has list\n");
478     mc->generic.type = MACCTRL_EDITBOX;
479     mc->generic.ctrl = ctrl;
480     mc->generic.privdata = NULL;
481     lbounds.left = curstate->pos.h;
482     lbounds.top = curstate->pos.v;
483     if (ctrl->editbox.percentwidth == 100) {
484         if (ctrl->editbox.label != NULL) {
485             lbounds.right = lbounds.left + curstate->width;
486             lbounds.bottom = lbounds.top + 16;
487             curstate->pos.v += 18;
488         }
489         bounds.left = curstate->pos.h;
490         bounds.right = bounds.left + curstate->width;
491     } else {
492         lbounds.right = lbounds.left +
493             curstate->width * (100 - ctrl->editbox.percentwidth) / 100;
494         lbounds.bottom = lbounds.top + 22;
495         bounds.left = lbounds.right;
496         bounds.right = lbounds.left + curstate->width;
497     }
498     bounds.top = curstate->pos.v;
499     bounds.bottom = bounds.top + 22;
500     if (mac_gestalts.apprvers >= 0x100) {
501         if (ctrl->editbox.label == NULL)
502             mc->editbox.tblabel = NULL;
503         else {
504             mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE,
505                                              0, 0, 0, kControlStaticTextProc,
506                                              (long)mc);
507             SetControlData(mc->editbox.tblabel, kControlEntireControl,
508                            kControlStaticTextTextTag,
509                            strlen(ctrl->editbox.label), ctrl->editbox.label);
510         }
511         InsetRect(&bounds, 3, 3);
512         mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
513                                         ctrl->editbox.password ?
514                                         kControlEditTextPasswordProc :
515                                         kControlEditTextProc, (long)mc);
516     }
517 #if !TARGET_API_MAC_CARBON
518     else {
519         if (ctrl->editbox.label == NULL)
520             mc->editbox.tblabel = NULL;
521         else {
522             mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE,
523                                              0, 0, 0, SYS7_TEXT_PROC,
524                                              (long)mc);
525             TESetText(ctrl->editbox.label, strlen(ctrl->editbox.label),
526                       (TEHandle)(*mc->editbox.tblabel)->contrlData);
527         }
528         mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
529                                         SYS7_EDITBOX_PROC, (long)mc);
530     }
531 #endif
532     curstate->pos.v += 28;
533     add234(mcs->byctrl, mc);
534     mc->generic.next = mcs->panels[curstate->panelnum];
535     mcs->panels[curstate->panelnum] = mc;
536     ctrlevent(mcs, mc, EVENT_REFRESH);
537 }
538
539 #if !TARGET_API_MAC_CARBON
540 static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant,
541                                                ControlRef control,
542                                                ControlDefProcMessage msg,
543                                                SInt32 param)
544 {
545     RgnHandle rgn;
546     Rect rect;
547     TEHandle te;
548     long ssfs;
549     Point mouse;
550
551     switch (msg) {
552       case initCntl:
553         rect = (*control)->contrlRect;
554         if (variant == SYS7_EDITBOX_VARIANT)
555             InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */
556         te = TENew(&rect, &rect);
557         ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
558         (*te)->txSize = LoWord(ssfs);
559         (*te)->txFont = HiWord(ssfs);
560         (*control)->contrlData = (Handle)te;
561         return noErr;
562       case dispCntl:
563         TEDispose((TEHandle)(*control)->contrlData);
564         return 0;
565       case drawCntl:
566         if ((*control)->contrlVis) {
567             rect = (*control)->contrlRect;
568             if (variant == SYS7_EDITBOX_VARIANT) {
569                 PenNormal();
570                 FrameRect(&rect);
571                 InsetRect(&rect, 3, 3);
572             }
573             (*(TEHandle)(*control)->contrlData)->viewRect = rect;
574             TEUpdate(&rect, (TEHandle)(*control)->contrlData);
575         }
576         return 0;
577       case testCntl:
578         if (variant == SYS7_TEXT_VARIANT)
579             return kControlNoPart;
580         mouse.h = LoWord(param);
581         mouse.v = HiWord(param);
582         rect = (*control)->contrlRect;
583         InsetRect(&rect, 3, 3);
584         return PtInRect(mouse, &rect) ? kControlEditTextPart : kControlNoPart;
585       case calcCRgns:
586         if (param & (1 << 31)) {
587             param &= ~(1 << 31);
588             goto calcthumbrgn;
589         }
590         /* FALLTHROUGH */
591       case calcCntlRgn:
592         rgn = (RgnHandle)param;
593         RectRgn(rgn, &(*control)->contrlRect);
594         return 0;
595       case calcThumbRgn:
596       calcthumbrgn:
597         rgn = (RgnHandle)param;
598         SetEmptyRgn(rgn);
599         return 0;
600     }
601
602     return 0;
603 }
604 #endif
605
606 static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
607                           struct mac_layoutstate *curstate,
608                           union control *ctrl)
609 {
610     union macctrl *mc = snew(union macctrl);
611     Rect bounds;
612     Str255 title;
613     unsigned int i, colwidth;
614
615     if (ctrl->radio.label != NULL)
616         fprintf(stderr, "    label = %s\n", ctrl->radio.label);
617     mc->generic.type = MACCTRL_RADIO;
618     mc->generic.ctrl = ctrl;
619     mc->generic.privdata = NULL;
620     mc->radio.tbctrls = snewn(ctrl->radio.nbuttons, ControlRef);
621     colwidth = (curstate->width + 13) / ctrl->radio.ncolumns;
622     bounds.top = curstate->pos.v;
623     bounds.bottom = bounds.top + 16;
624     bounds.left = curstate->pos.h;
625     bounds.right = bounds.left + curstate->width;
626     if (ctrl->radio.label == NULL)
627         mc->radio.tblabel = NULL;
628     else {
629         if (mac_gestalts.apprvers >= 0x100) {
630             mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE,
631                                            0, 0, 0, kControlStaticTextProc,
632                                            (long)mc);
633             SetControlData(mc->radio.tblabel, kControlEntireControl,
634                            kControlStaticTextTextTag,
635                            strlen(ctrl->radio.label), ctrl->radio.label);
636         }
637 #if !TARGET_API_MAC_CARBON
638         else {
639             mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE,
640                                            0, 0, 0, SYS7_TEXT_PROC, (long)mc);
641             TESetText(ctrl->radio.label, strlen(ctrl->radio.label),
642                       (TEHandle)(*mc->radio.tblabel)->contrlData);
643         }
644 #endif
645         curstate->pos.v += 18;
646     }
647     for (i = 0; i < ctrl->radio.nbuttons; i++) {
648         fprintf(stderr, "    button = %s\n", ctrl->radio.buttons[i]);
649         bounds.top = curstate->pos.v - 2;
650         bounds.bottom = bounds.top + 18;
651         bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
652         if (i == ctrl->radio.nbuttons - 1 ||
653             i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
654             bounds.right = curstate->pos.h + curstate->width;
655             curstate->pos.v += 18;
656         } else
657             bounds.right = bounds.left + colwidth - 13;
658         c2pstrcpy(title, ctrl->radio.buttons[i]);
659         mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE,
660                                           0, 0, 1, radioButProc, (long)mc);
661     }
662     curstate->pos.v += 4;
663     add234(mcs->byctrl, mc);
664     mc->generic.next = mcs->panels[curstate->panelnum];
665     mcs->panels[curstate->panelnum] = mc;
666     ctrlevent(mcs, mc, EVENT_REFRESH);
667 }
668
669 static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
670                              struct mac_layoutstate *curstate,
671                              union control *ctrl)
672 {
673     union macctrl *mc = snew(union macctrl);
674     Rect bounds;
675     Str255 title;
676
677     assert(ctrl->checkbox.label != NULL);
678     fprintf(stderr, "    label = %s\n", ctrl->checkbox.label);
679     mc->generic.type = MACCTRL_CHECKBOX;
680     mc->generic.ctrl = ctrl;
681     mc->generic.privdata = NULL;
682     bounds.left = curstate->pos.h;
683     bounds.right = bounds.left + curstate->width;
684     bounds.top = curstate->pos.v;
685     bounds.bottom = bounds.top + 16;
686     c2pstrcpy(title, ctrl->checkbox.label);
687     mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
688                                      checkBoxProc, (long)mc);
689     add234(mcs->byctrl, mc);
690     curstate->pos.v += 22;
691     mc->generic.next = mcs->panels[curstate->panelnum];
692     mcs->panels[curstate->panelnum] = mc;
693     ctrlevent(mcs, mc, EVENT_REFRESH);
694 }
695
696 static void macctrl_button(struct macctrls *mcs, WindowPtr window,
697                            struct mac_layoutstate *curstate,
698                            union control *ctrl)
699 {
700     union macctrl *mc = snew(union macctrl);
701     Rect bounds;
702     Str255 title;
703
704     assert(ctrl->button.label != NULL);
705     fprintf(stderr, "    label = %s\n", ctrl->button.label);
706     if (ctrl->button.isdefault)
707         fprintf(stderr, "    is default\n");
708     mc->generic.type = MACCTRL_BUTTON;
709     mc->generic.ctrl = ctrl;
710     mc->generic.privdata = NULL;
711     bounds.left = curstate->pos.h;
712     bounds.right = bounds.left + curstate->width;
713     bounds.top = curstate->pos.v;
714     bounds.bottom = bounds.top + 20;
715     c2pstrcpy(title, ctrl->button.label);
716     mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
717                                    pushButProc, (long)mc);
718     mc->button.tbring = NULL;
719     if (mac_gestalts.apprvers >= 0x100) {
720         Boolean isdefault = ctrl->button.isdefault;
721
722         SetControlData(mc->button.tbctrl, kControlEntireControl,
723                        kControlPushButtonDefaultTag,
724                        sizeof(isdefault), &isdefault);
725     } else if (ctrl->button.isdefault) {
726         InsetRect(&bounds, -4, -4);
727         mc->button.tbring = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
728                                        SYS7_DEFAULT_PROC, (long)mc);
729     }
730     if (mac_gestalts.apprvers >= 0x110) {
731         Boolean iscancel = ctrl->button.iscancel;
732
733         SetControlData(mc->button.tbctrl, kControlEntireControl,
734                        kControlPushButtonCancelTag,
735                        sizeof(iscancel), &iscancel);
736     }
737     if (ctrl->button.isdefault)
738         mcs->defbutton = mc;
739     if (ctrl->button.iscancel)
740         mcs->canbutton = mc;
741     add234(mcs->byctrl, mc);
742     mc->generic.next = mcs->panels[curstate->panelnum];
743     mcs->panels[curstate->panelnum] = mc;
744     curstate->pos.v += 26;
745 }
746
747 #if !TARGET_API_MAC_CARBON
748 static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
749                                                ControlRef control,
750                                                ControlDefProcMessage msg,
751                                                SInt32 param)
752 {
753     RgnHandle rgn;
754     Rect rect;
755     int oval;
756     PenState savestate;
757
758     switch (msg) {
759       case drawCntl:
760         if ((*control)->contrlVis) {
761             rect = (*control)->contrlRect;
762             GetPenState(&savestate);
763             PenNormal();
764             PenSize(3, 3);
765             if ((*control)->contrlHilite == kControlInactivePart)
766                 PenPat(&qd.gray);
767             oval = (rect.bottom - rect.top) / 2 + 2;
768             FrameRoundRect(&rect, oval, oval);
769             SetPenState(&savestate);
770         }
771         return 0;
772       case calcCRgns:
773         if (param & (1 << 31)) {
774             param &= ~(1 << 31);
775             goto calcthumbrgn;
776         }
777         /* FALLTHROUGH */
778       case calcCntlRgn:
779         rgn = (RgnHandle)param;
780         RectRgn(rgn, &(*control)->contrlRect);
781         return 0;
782       case calcThumbRgn:
783       calcthumbrgn:
784         rgn = (RgnHandle)param;
785         SetEmptyRgn(rgn);
786         return 0;
787     }
788
789     return 0;
790 }
791 #endif
792
793 static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
794                           struct mac_layoutstate *curstate,
795                           union control *ctrl)
796 {
797     union macctrl *mc = snew(union macctrl);
798     Rect bounds;
799     Str255 title;
800     unsigned int labelwidth;
801     static int nextmenuid = MENU_MIN;
802     int menuid;
803     MenuRef menu;
804
805     /* 
806      * <http://developer.apple.com/qa/tb/tb42.html> explains how to
807      * create a popup menu with dynamic content.
808      */
809     assert(ctrl->listbox.height == 0);
810     assert(!ctrl->listbox.draglist);
811     assert(!ctrl->listbox.multisel);
812
813     if (ctrl->listbox.label != NULL)
814         fprintf(stderr, "    label = %s\n", ctrl->listbox.label);
815     fprintf(stderr, "    percentwidth = %d\n", ctrl->listbox.percentwidth);
816
817     mc->generic.type = MACCTRL_POPUP;
818     mc->generic.ctrl = ctrl;
819     mc->generic.privdata = NULL;
820     c2pstrcpy(title, ctrl->button.label == NULL ? "" : ctrl->button.label);
821
822     /* Find a spare menu ID and create the menu */
823     while (GetMenuHandle(nextmenuid) != NULL)
824         if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
825     menuid = nextmenuid++;
826     menu = NewMenu(menuid, "\pdummy");
827     if (menu == NULL) return;
828     mc->popup.menu = menu;
829     mc->popup.menuid = menuid;
830     InsertMenu(menu, kInsertHierarchicalMenu);
831
832     /* The menu starts off empty */
833     mc->popup.nids = 0;
834     mc->popup.ids = NULL;
835
836     bounds.left = curstate->pos.h;
837     bounds.right = bounds.left + curstate->width;
838     bounds.top = curstate->pos.v;
839     bounds.bottom = bounds.top + 20;
840     /* XXX handle percentwidth == 100 */
841     labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
842     mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE,
843                                   popupTitleLeftJust, menuid, labelwidth,
844                                   popupMenuProc + popupFixedWidth, (long)mc);
845     add234(mcs->byctrl, mc);
846     curstate->pos.v += 26;
847     mc->generic.next = mcs->panels[curstate->panelnum];
848     mcs->panels[curstate->panelnum] = mc;
849     ctrlevent(mcs, mc, EVENT_REFRESH);
850 }
851
852
853 void macctrl_activate(WindowPtr window, EventRecord *event)
854 {
855     struct macctrls *mcs = mac_winctrls(window);
856     Boolean active = (event->modifiers & activeFlag) != 0;
857     GrafPtr saveport;
858     int i, j;
859     ControlPartCode state;
860     union macctrl *mc;
861
862     GetPort(&saveport);
863     SetPort((GrafPtr)GetWindowPort(window));
864     if (mac_gestalts.apprvers >= 0x100)
865         SetThemeWindowBackground(window, active ?
866                                  kThemeBrushModelessDialogBackgroundActive :
867                                  kThemeBrushModelessDialogBackgroundInactive,
868                                  TRUE);
869     state = active ? kControlNoPart : kControlInactivePart;
870     for (i = 0; i <= mcs->curpanel; i += mcs->curpanel)
871         for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
872             switch (mc->generic.type) {
873               case MACCTRL_TEXT:
874                 HiliteControl(mc->text.tbctrl, state);
875                 break;
876               case MACCTRL_EDITBOX:
877                 HiliteControl(mc->editbox.tbctrl, state);
878                 if (mc->editbox.tblabel != NULL)
879                     HiliteControl(mc->editbox.tblabel, state);
880                 break;
881               case MACCTRL_RADIO:
882                 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
883                     HiliteControl(mc->radio.tbctrls[j], state);
884                 if (mc->radio.tblabel != NULL)
885                     HiliteControl(mc->radio.tblabel, state);
886                 break;
887               case MACCTRL_CHECKBOX:
888                 HiliteControl(mc->checkbox.tbctrl, state);
889                 break;
890               case MACCTRL_BUTTON:
891                 HiliteControl(mc->button.tbctrl, state);
892                 if (mc->button.tbring != NULL)
893                     HiliteControl(mc->button.tbring, state);                
894                 break;
895               case MACCTRL_POPUP:
896                 HiliteControl(mc->popup.tbctrl, state);
897                 break;
898             }
899 #if !TARGET_API_MAC_CARBON
900             if (mcs->focus == mc) {
901                 if (active)
902                     macctrl_enfocus(mc);
903                 else
904                     macctrl_defocus(mc);
905             }
906 #endif
907         }
908     SetPort(saveport);
909 }
910
911 void macctrl_click(WindowPtr window, EventRecord *event)
912 {
913     Point mouse;
914     ControlHandle control;
915     int part, trackresult;
916     GrafPtr saveport;
917     union macctrl *mc;
918     struct macctrls *mcs = mac_winctrls(window);
919     int i;
920     UInt32 features;
921
922     GetPort(&saveport);
923     SetPort((GrafPtr)GetWindowPort(window));
924     mouse = event->where;
925     GlobalToLocal(&mouse);
926     part = FindControl(mouse, window, &control);
927     if (control != NULL) {
928         mc = (union macctrl *)GetControlReference(control);
929         if (mac_gestalts.apprvers >= 0x100) {
930             if (GetControlFeatures(control, &features) == noErr &&
931                 (features & kControlSupportsFocus) &&
932                 (features & kControlGetsFocusOnClick))
933                 SetKeyboardFocus(window, control, part);
934             trackresult = HandleControlClick(control, mouse, event->modifiers,
935                                              (ControlActionUPP)-1);
936         } else {
937 #if !TARGET_API_MAC_CARBON
938             if (mc->generic.type == MACCTRL_EDITBOX &&
939                 control == mc->editbox.tbctrl) {
940                 TEHandle te = (TEHandle)(*control)->contrlData;
941
942                 macctrl_setfocus(mcs, mc);
943                 TEClick(mouse, !!(event->modifiers & shiftKey), te);
944                 goto done;
945             }
946 #endif
947             trackresult = TrackControl(control, mouse, (ControlActionUPP)-1);
948         }
949         switch (mc->generic.type) {
950           case MACCTRL_RADIO:
951             if (trackresult != 0) {
952                 for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
953                     if (mc->radio.tbctrls[i] == control)
954                         SetControlValue(mc->radio.tbctrls[i],
955                                         kControlRadioButtonCheckedValue);
956                     else
957                         SetControlValue(mc->radio.tbctrls[i],
958                                         kControlRadioButtonUncheckedValue);
959                 ctrlevent(mcs, mc, EVENT_VALCHANGE);
960             }
961             break;
962           case MACCTRL_CHECKBOX:
963             if (trackresult != 0) {
964                 SetControlValue(control, !GetControlValue(control));
965                 ctrlevent(mcs, mc, EVENT_VALCHANGE);
966             }
967             break;
968           case MACCTRL_BUTTON:
969             if (trackresult != 0)
970                 ctrlevent(mcs, mc, EVENT_ACTION);
971             break;
972           case MACCTRL_POPUP:
973             ctrlevent(mcs, mc, EVENT_SELCHANGE);
974             break;
975         }
976     }
977   done:
978     SetPort(saveport);
979 }
980
981 void macctrl_key(WindowPtr window, EventRecord *event)
982 {
983     ControlRef control;
984     struct macctrls *mcs = mac_winctrls(window);
985     union macctrl *mc;
986     unsigned long dummy;
987
988     switch (event->message & charCodeMask) {
989       case kEnterCharCode:
990       case kReturnCharCode:
991         if (mcs->defbutton != NULL) {
992             assert(mcs->defbutton->generic.type == MACCTRL_BUTTON);
993             HiliteControl(mcs->defbutton->button.tbctrl, kControlButtonPart);
994             /*
995              * I'd like to delay unhilighting the button until after
996              * the event has been processed, but by them the entire
997              * dialgue box might have been destroyed.
998              */
999             Delay(6, &dummy);
1000             HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart);
1001             ctrlevent(mcs, mcs->defbutton, EVENT_ACTION);
1002         }
1003         return;
1004       case kEscapeCharCode:
1005         if (mcs->canbutton != NULL) {
1006             assert(mcs->canbutton->generic.type == MACCTRL_BUTTON);
1007             HiliteControl(mcs->canbutton->button.tbctrl, kControlButtonPart);
1008             Delay(6, &dummy);
1009             HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart);
1010             ctrlevent(mcs, mcs->canbutton, EVENT_ACTION);
1011         }
1012         return;
1013     }
1014     if (mac_gestalts.apprvers >= 0x100) {
1015         if (GetKeyboardFocus(window, &control) == noErr && control != NULL) {
1016             HandleControlKey(control, (event->message & keyCodeMask) >> 8,
1017                              event->message & charCodeMask, event->modifiers);
1018             mc = (union macctrl *)GetControlReference(control);
1019             ctrlevent(mcs, mc, EVENT_VALCHANGE);
1020         }
1021     }
1022 #if !TARGET_API_MAC_CARBON
1023     else {
1024         TEHandle te;
1025
1026         if (mcs->focus != NULL) {
1027             mc = mcs->focus;
1028             switch (mc->generic.type) {
1029               case MACCTRL_EDITBOX:
1030                 te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1031                 TEKey(event->message & charCodeMask, te);
1032                 ctrlevent(mcs, mc, EVENT_VALCHANGE);
1033                 break;
1034             }
1035         }
1036     }
1037 #endif
1038 }
1039
1040 void macctrl_update(WindowPtr window)
1041 {
1042 #if TARGET_API_MAC_CARBON
1043     RgnHandle visrgn;
1044 #endif
1045     Rect rect;
1046     GrafPtr saveport;
1047
1048     BeginUpdate(window);
1049     GetPort(&saveport);
1050     SetPort((GrafPtr)GetWindowPort(window));
1051     if (mac_gestalts.apprvers >= 0x101) {
1052 #if TARGET_API_MAC_CARBON
1053         GetPortBounds(GetWindowPort(window), &rect);
1054 #else
1055         rect = window->portRect;
1056 #endif
1057         InsetRect(&rect, -1, -1);
1058         DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ?
1059                                      kThemeStateActive : kThemeStateInactive);
1060     }
1061 #if TARGET_API_MAC_CARBON
1062     visrgn = NewRgn();
1063     GetPortVisibleRegion(GetWindowPort(window), visrgn);
1064     UpdateControls(window, visrgn);
1065     DisposeRgn(visrgn);
1066 #else
1067     UpdateControls(window, window->visRgn);
1068 #endif
1069     SetPort(saveport);
1070     EndUpdate(window);
1071 }
1072
1073 #if TARGET_API_MAC_CARBON
1074 #define EnableItem EnableMenuItem
1075 #define DisableItem DisableMenuItem
1076 #endif
1077 void macctrl_adjustmenus(WindowPtr window)
1078 {
1079     MenuHandle menu;
1080
1081     menu = GetMenuHandle(mFile);
1082     DisableItem(menu, iSave); /* XXX enable if modified */
1083     EnableItem(menu, iSaveAs);
1084     EnableItem(menu, iDuplicate);
1085
1086     menu = GetMenuHandle(mEdit);
1087     DisableItem(menu, 0);
1088 }
1089
1090 void macctrl_close(WindowPtr window)
1091 {
1092     struct macctrls *mcs = mac_winctrls(window);
1093     union macctrl *mc;
1094
1095     /*
1096      * Mostly, we don't bother disposing of the Toolbox controls,
1097      * since that will happen automatically when the window is
1098      * disposed of.  Popup menus are an exception, because we have to
1099      * dispose of the menu ourselves, and doing that while the control
1100      * still holds a reference to it seems rude.
1101      */
1102     while ((mc = index234(mcs->byctrl, 0)) != NULL) {
1103         if (mc->generic.privdata != NULL && mc->generic.freeprivdata)
1104             sfree(mc->generic.privdata);
1105         switch (mc->generic.type) {
1106           case MACCTRL_POPUP:
1107             DisposeControl(mc->popup.tbctrl);
1108             DeleteMenu(mc->popup.menuid);
1109             DisposeMenu(mc->popup.menu);
1110             break;
1111         }
1112         del234(mcs->byctrl, mc);
1113         sfree(mc);
1114     }
1115
1116     freetree234(mcs->byctrl);
1117     mcs->byctrl = NULL;
1118     sfree(mcs->panels);
1119     mcs->panels = NULL;
1120 }
1121
1122 void dlg_update_start(union control *ctrl, void *dlg)
1123 {
1124
1125     /* No-op for now */
1126 }
1127
1128 void dlg_update_done(union control *ctrl, void *dlg)
1129 {
1130
1131     /* No-op for now */
1132 }
1133
1134 void dlg_set_focus(union control *ctrl, void *dlg)
1135 {
1136
1137     if (mac_gestalts.apprvers >= 0x100) {
1138         /* Use SetKeyboardFocus() */
1139     } else {
1140         /* Do our own mucking around */
1141     }
1142 }
1143
1144 union control *dlg_last_focused(union control *ctrl, void *dlg)
1145 {
1146
1147     return NULL;
1148 }
1149
1150 void dlg_beep(void *dlg)
1151 {
1152
1153     SysBeep(30);
1154 }
1155
1156 void dlg_error_msg(void *dlg, char *msg)
1157 {
1158     Str255 pmsg;
1159
1160     c2pstrcpy(pmsg, msg);
1161     ParamText(pmsg, NULL, NULL, NULL);
1162     StopAlert(128, NULL);
1163 }
1164
1165 void dlg_end(void *dlg, int value)
1166 {
1167     struct macctrls *mcs = dlg;
1168
1169     if (mcs->end != NULL)
1170         (*mcs->end)(mcs->window, value);
1171 };
1172
1173 void dlg_refresh(union control *ctrl, void *dlg)
1174 {
1175     struct macctrls *mcs = dlg;
1176     union macctrl *mc;
1177
1178     if (ctrl == NULL)
1179         return; /* FIXME */
1180     mc = findbyctrl(mcs, ctrl);
1181     assert(mc != NULL);
1182     ctrlevent(mcs, mc, EVENT_REFRESH);
1183 };
1184
1185 void *dlg_get_privdata(union control *ctrl, void *dlg)
1186 {
1187     struct macctrls *mcs = dlg;
1188     union macctrl *mc = findbyctrl(mcs, ctrl);
1189
1190     assert(mc != NULL);
1191     return mc->generic.privdata;
1192 }
1193
1194 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
1195 {
1196     struct macctrls *mcs = dlg;
1197     union macctrl *mc = findbyctrl(mcs, ctrl);
1198
1199     assert(mc != NULL);
1200     mc->generic.privdata = ptr;
1201     mc->generic.freeprivdata = FALSE;
1202 }
1203
1204 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
1205 {
1206     struct macctrls *mcs = dlg;
1207     union macctrl *mc = findbyctrl(mcs, ctrl);
1208
1209     assert(mc != NULL);
1210     mc->generic.privdata = smalloc(size);
1211     mc->generic.freeprivdata = TRUE;
1212     return mc->generic.privdata;
1213 }
1214
1215
1216 /*
1217  * Radio Button control
1218  */
1219
1220 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
1221 {
1222     struct macctrls *mcs = dlg;
1223     union macctrl *mc = findbyctrl(mcs, ctrl);
1224     int i;
1225
1226     assert(mc != NULL);
1227     for (i = 0; i < ctrl->radio.nbuttons; i++) {
1228         if (i == whichbutton)
1229             SetControlValue(mc->radio.tbctrls[i],
1230                             kControlRadioButtonCheckedValue);
1231         else
1232             SetControlValue(mc->radio.tbctrls[i],
1233                             kControlRadioButtonUncheckedValue);
1234     }
1235
1236 };
1237
1238 int dlg_radiobutton_get(union control *ctrl, void *dlg)
1239 {
1240     struct macctrls *mcs = dlg;
1241     union macctrl *mc = findbyctrl(mcs, ctrl);
1242     int i;
1243
1244     assert(mc != NULL);
1245     for (i = 0; i < ctrl->radio.nbuttons; i++) {
1246         if (GetControlValue(mc->radio.tbctrls[i])  ==
1247             kControlRadioButtonCheckedValue)
1248             return i;
1249     }
1250     return -1;
1251 };
1252
1253
1254 /*
1255  * Check Box control
1256  */
1257
1258 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
1259 {
1260     struct macctrls *mcs = dlg;
1261     union macctrl *mc = findbyctrl(mcs, ctrl);
1262
1263     assert(mc != NULL);
1264     SetControlValue(mc->checkbox.tbctrl,
1265                     checked ? kControlCheckBoxCheckedValue :
1266                               kControlCheckBoxUncheckedValue);
1267 }
1268
1269 int dlg_checkbox_get(union control *ctrl, void *dlg)
1270 {
1271     struct macctrls *mcs = dlg;
1272     union macctrl *mc = findbyctrl(mcs, ctrl);
1273
1274     assert(mc != NULL);
1275     return GetControlValue(mc->checkbox.tbctrl);
1276 }
1277
1278
1279 /*
1280  * Edit Box control
1281  */
1282
1283 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
1284 {
1285     struct macctrls *mcs = dlg;
1286     union macctrl *mc = findbyctrl(mcs, ctrl);
1287     GrafPtr saveport;
1288
1289     assert(mc != NULL);
1290     assert(mc->generic.type == MACCTRL_EDITBOX);
1291     GetPort(&saveport);
1292     SetPort((GrafPtr)(GetWindowPort(mcs->window)));
1293     if (mac_gestalts.apprvers >= 0x100)
1294         SetControlData(mc->editbox.tbctrl, kControlEntireControl,
1295                        ctrl->editbox.password ?
1296                        kControlEditTextPasswordTag :
1297                        kControlEditTextTextTag,
1298                        strlen(text), text);
1299 #if !TARGET_API_MAC_CARBON
1300     else
1301         TESetText(text, strlen(text),
1302                   (TEHandle)(*mc->editbox.tbctrl)->contrlData);
1303 #endif
1304     DrawOneControl(mc->editbox.tbctrl);
1305     SetPort(saveport);
1306 }
1307
1308 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
1309 {
1310     struct macctrls *mcs = dlg;
1311     union macctrl *mc = findbyctrl(mcs, ctrl);
1312     Size olen;
1313
1314     assert(mc != NULL);
1315     assert(mc->generic.type == MACCTRL_EDITBOX);
1316     if (mac_gestalts.apprvers >= 0x100) {
1317         if (GetControlData(mc->editbox.tbctrl, kControlEntireControl,
1318                            ctrl->editbox.password ?
1319                            kControlEditTextPasswordTag :
1320                            kControlEditTextTextTag,
1321                            length - 1, buffer, &olen) != noErr)
1322             olen = 0;
1323         if (olen > length - 1)
1324             olen = length - 1;
1325     }
1326 #if !TARGET_API_MAC_CARBON
1327     else {
1328         TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1329
1330         olen = (*te)->teLength;
1331         if (olen > length - 1)
1332             olen = length - 1;
1333         memcpy(buffer, *(*te)->hText, olen);
1334     }
1335 #endif
1336     buffer[olen] = '\0';
1337     fprintf(stderr, "dlg_editbox_get: %s\n", buffer);
1338 }
1339
1340
1341 /*
1342  * List Box control
1343  */
1344
1345 static void dlg_macpopup_clear(union control *ctrl, void *dlg)
1346 {
1347     struct macctrls *mcs = dlg;
1348     union macctrl *mc = findbyctrl(mcs, ctrl);
1349     MenuRef menu = mc->popup.menu;
1350     unsigned int i, n;
1351
1352     fprintf(stderr, "      popup_clear\n");
1353     n = CountMenuItems(menu);
1354     for (i = 0; i < n; i++)
1355         DeleteMenuItem(menu, n - i);
1356     mc->popup.nids = 0;
1357     sfree(mc->popup.ids);
1358     mc->popup.ids = NULL;
1359     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1360 }
1361
1362 void dlg_listbox_clear(union control *ctrl, void *dlg)
1363 {
1364
1365     if (ctrl->listbox.height == 0)
1366         dlg_macpopup_clear(ctrl, dlg);
1367 }
1368
1369 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
1370 {
1371     struct macctrls *mcs = dlg;
1372     union macctrl *mc = findbyctrl(mcs, ctrl);
1373     MenuRef menu = mc->popup.menu;
1374
1375     fprintf(stderr, "      popup_del %d\n", index);
1376     DeleteMenuItem(menu, index + 1);
1377     if (mc->popup.ids != NULL)
1378         memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
1379                (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
1380     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1381 }
1382
1383 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
1384 {
1385
1386     if (ctrl->listbox.height == 0)
1387         dlg_macpopup_del(ctrl, dlg, index);
1388 }
1389
1390 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
1391 {
1392     struct macctrls *mcs = dlg;
1393     union macctrl *mc = findbyctrl(mcs, ctrl);
1394     MenuRef menu = mc->popup.menu;
1395     Str255 itemstring;
1396
1397     fprintf(stderr, "      popup_add %s\n", text);
1398     assert(text[0] != '\0');
1399     c2pstrcpy(itemstring, text);
1400     AppendMenu(menu, "\pdummy");
1401     SetMenuItemText(menu, CountMenuItems(menu), itemstring);
1402     SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
1403 }
1404
1405 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
1406 {
1407
1408     if (ctrl->listbox.height == 0)
1409         dlg_macpopup_add(ctrl, dlg, text);
1410 }
1411
1412 static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
1413                                    char const *text, int id)
1414 {
1415     struct macctrls *mcs = dlg;
1416     union macctrl *mc = findbyctrl(mcs, ctrl);
1417     MenuRef menu = mc->popup.menu;
1418     unsigned int index;
1419
1420     fprintf(stderr, "      popup_addwthindex %s, %d\n", text, id);
1421     dlg_macpopup_add(ctrl, dlg, text);
1422     index = CountMenuItems(menu) - 1;
1423     if (mc->popup.nids <= index) {
1424         mc->popup.nids = index + 1;
1425         mc->popup.ids = sresize(mc->popup.ids, mc->popup.nids, int);
1426     }
1427     mc->popup.ids[index] = id;
1428 }
1429
1430 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
1431                            char const *text, int id)
1432 {
1433
1434     if (ctrl->listbox.height == 0)
1435         dlg_macpopup_addwithid(ctrl, dlg, text, id);
1436 }
1437
1438 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
1439 {
1440     struct macctrls *mcs = dlg;
1441     union macctrl *mc = findbyctrl(mcs, ctrl);
1442
1443     if (ctrl->listbox.height == 0) {
1444         assert(mc->popup.ids != NULL && mc->popup.nids > index);
1445         return mc->popup.ids[index];
1446     }
1447     return 0;
1448 }
1449
1450 int dlg_listbox_index(union control *ctrl, void *dlg)
1451 {
1452     struct macctrls *mcs = dlg;
1453     union macctrl *mc = findbyctrl(mcs, ctrl);
1454
1455     if (ctrl->listbox.height == 0)
1456         return GetControlValue(mc->popup.tbctrl) - 1;
1457     return 0;
1458 };
1459
1460 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1461 {
1462     struct macctrls *mcs = dlg;
1463     union macctrl *mc = findbyctrl(mcs, ctrl);
1464
1465     if (ctrl->listbox.height == 0)
1466         return GetControlValue(mc->popup.tbctrl) - 1 == index;
1467     return 0;
1468 };
1469
1470 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1471 {
1472     struct macctrls *mcs = dlg;
1473     union macctrl *mc = findbyctrl(mcs, ctrl);
1474
1475     if (ctrl->listbox.height == 0)
1476         SetControlValue(mc->popup.tbctrl, index + 1);
1477 };
1478
1479
1480 /*
1481  * Text control
1482  */
1483
1484 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1485 {
1486     struct macctrls *mcs = dlg;
1487     union macctrl *mc = findbyctrl(mcs, ctrl);
1488
1489     assert(mc != NULL);
1490     if (mac_gestalts.apprvers >= 0x100)
1491         SetControlData(mc->text.tbctrl, kControlEntireControl,
1492                        kControlStaticTextTextTag, strlen(text), text);
1493 #if !TARGET_API_MAC_CARBON
1494     else
1495         TESetText(text, strlen(text),
1496                   (TEHandle)(*mc->text.tbctrl)->contrlData);
1497 #endif
1498 }
1499
1500
1501 /*
1502  * File Selector control
1503  */
1504
1505 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1506 {
1507
1508 }
1509
1510 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1511 {
1512
1513 }
1514
1515
1516 /*
1517  * Font Selector control
1518  */
1519
1520 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1521 {
1522
1523 }
1524
1525 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1526 {
1527
1528 }
1529
1530
1531 /*
1532  * Printer enumeration
1533  */
1534
1535 printer_enum *printer_start_enum(int *nprinters)
1536 {
1537
1538     *nprinters = 0;
1539     return NULL;
1540 }
1541
1542 char *printer_get_name(printer_enum *pe, int thing)
1543 {
1544
1545     return "<none>";
1546 }
1547
1548 void printer_finish_enum(printer_enum *pe)
1549 {
1550
1551 }
1552
1553
1554 /*
1555  * Colour selection stuff
1556  */
1557
1558 void dlg_coloursel_start(union control *ctrl, void *dlg,
1559                          int r, int g, int b)
1560 {
1561
1562 }
1563
1564 int dlg_coloursel_results(union control *ctrl, void *dlg,
1565                           int *r, int *g, int *b)
1566 {
1567
1568     return 0;
1569 }
1570
1571 /*
1572  * Local Variables:
1573  * c-file-style: "simon"
1574  * End:
1575  */