Slightly improve radio button layout to more closely match the Mac OS 8 HI
[u/mdw/putty] / mac / macctrls.c
1 /* $Id: macctrls.c,v 1.10 2003/03/24 21:55:51 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 <Menus.h>
33 #include <Resources.h>
34 #include <Sound.h>
35 #include <TextUtils.h>
36 #include <Windows.h>
37
38 #include <assert.h>
39 #include <string.h>
40
41 #include "putty.h"
42 #include "mac.h"
43 #include "macresid.h"
44 #include "dialog.h"
45 #include "tree234.h"
46
47 /* Range of menu IDs for popup menus */
48 #define MENU_MIN 1024
49 #define MENU_MAX 2048
50
51
52 union macctrl {
53 struct macctrl_generic {
54 enum {
55 MACCTRL_TEXT,
56 MACCTRL_RADIO,
57 MACCTRL_CHECKBOX,
58 MACCTRL_BUTTON,
59 MACCTRL_POPUP
60 } type;
61 /* Template from which this was generated */
62 union control *ctrl;
63 /* Next control in this panel */
64 union macctrl *next;
65 } generic;
66 struct {
67 struct macctrl_generic generic;
68 ControlRef tbctrl;
69 } text;
70 struct {
71 struct macctrl_generic generic;
72 ControlRef *tbctrls;
73 } radio;
74 struct {
75 struct macctrl_generic generic;
76 ControlRef tbctrl;
77 } checkbox;
78 struct {
79 struct macctrl_generic generic;
80 ControlRef tbctrl;
81 } button;
82 struct {
83 struct macctrl_generic generic;
84 ControlRef tbctrl;
85 MenuRef menu;
86 int menuid;
87 unsigned int nids;
88 int *ids;
89 } popup;
90 };
91
92 struct mac_layoutstate {
93 Point pos;
94 unsigned int width;
95 unsigned int panelnum;
96 };
97
98 #define ctrlevent(mcs, mc, event) do { \
99 if ((mc)->generic.ctrl->generic.handler != NULL) \
100 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
101 (mcs)->data, (event)); \
102 } while (0)
103
104 #define findbyctrl(mcs, ctrl) \
105 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
106
107 static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *,
108 WindowPtr, struct macctrls *);
109 static void macctrl_switchtopanel(struct macctrls *, unsigned int);
110 static void macctrl_text(struct macctrls *, WindowPtr,
111 struct mac_layoutstate *, union control *);
112 static void macctrl_radio(struct macctrls *, WindowPtr,
113 struct mac_layoutstate *, union control *);
114 static void macctrl_checkbox(struct macctrls *, WindowPtr,
115 struct mac_layoutstate *, union control *);
116 static void macctrl_button(struct macctrls *, WindowPtr,
117 struct mac_layoutstate *, union control *);
118 static void macctrl_popup(struct macctrls *, WindowPtr,
119 struct mac_layoutstate *, union control *);
120 #if !TARGET_API_MAC_CARBON
121 static pascal SInt32 macctrl_sys7_text_cdef(SInt16, ControlRef,
122 ControlDefProcMessage, SInt32);
123 static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
124 ControlDefProcMessage, SInt32);
125 #endif
126
127 #if !TARGET_API_MAC_CARBON
128 /*
129 * This trick enables us to keep all the CDEF code in the main
130 * application, which makes life easier. For details, see
131 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
132 */
133
134 #pragma options align=mac68k
135 typedef struct {
136 short jmpabs; /* 4EF9 */
137 ControlDefUPP theUPP;
138 } **PatchCDEF;
139 #pragma options align=reset
140 #endif
141
142 static void macctrl_init()
143 {
144 #if !TARGET_API_MAC_CARBON
145 static int inited = 0;
146 PatchCDEF cdef;
147
148 if (inited) return;
149 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Text);
150 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_text_cdef);
151 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
152 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
153 inited = 1;
154 #endif
155 }
156
157
158 static int macctrl_cmp_byctrl(void *av, void *bv)
159 {
160 union macctrl *a = (union macctrl *)av;
161 union macctrl *b = (union macctrl *)bv;
162
163 if (a->generic.ctrl < b->generic.ctrl)
164 return -1;
165 else if (a->generic.ctrl > b->generic.ctrl)
166 return +1;
167 else
168 return 0;
169 }
170
171 static int macctrl_cmp_byctrl_find(void *av, void *bv)
172 {
173 union control *a = (union control *)av;
174 union macctrl *b = (union macctrl *)bv;
175
176 if (a < b->generic.ctrl)
177 return -1;
178 else if (a > b->generic.ctrl)
179 return +1;
180 else
181 return 0;
182 }
183
184 void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
185 struct macctrls *mcs)
186 {
187 int i;
188 struct mac_layoutstate curstate;
189 ControlRef root;
190 Rect rect;
191
192 macctrl_init();
193 #if TARGET_API_MAC_CARBON
194 GetPortBounds(GetWindowPort(window), &rect);
195 #else
196 rect = window->portRect;
197 #endif
198 curstate.pos.h = rect.left + 13;
199 curstate.pos.v = rect.bottom - 59;
200 curstate.width = rect.right - rect.left - (13 * 2);
201 if (mac_gestalts.apprvers >= 0x100)
202 CreateRootControl(window, &root);
203 mcs->byctrl = newtree234(macctrl_cmp_byctrl);
204 /* Count the number of panels */
205 mcs->npanels = 1;
206 for (i = 1; i < cb->nctrlsets; i++)
207 if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
208 mcs->npanels++;
209 mcs->panels = smalloc(sizeof(*mcs->panels) * mcs->npanels);
210 memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
211 curstate.panelnum = 0;
212 for (i = 0; i < cb->nctrlsets; i++) {
213 if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
214 cb->ctrlsets[i-1]->pathname)) {
215 curstate.pos.v = rect.top + 13;
216 curstate.panelnum++;
217 assert(curstate.panelnum < mcs->npanels);
218 }
219 macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
220 }
221 macctrl_switchtopanel(mcs, 2);
222 }
223
224 static void macctrl_layoutset(struct mac_layoutstate *curstate,
225 struct controlset *s,
226 WindowPtr window, struct macctrls *mcs)
227 {
228 unsigned int i;
229
230 fprintf(stderr, "--- begin set ---\n");
231 fprintf(stderr, "pathname = %s\n", s->pathname);
232 if (s->boxname && *s->boxname)
233 fprintf(stderr, "boxname = %s\n", s->boxname);
234 if (s->boxtitle)
235 fprintf(stderr, "boxtitle = %s\n", s->boxtitle);
236
237
238 for (i = 0; i < s->ncontrols; i++) {
239 union control *ctrl = s->ctrls[i];
240 char const *s;
241
242 switch (ctrl->generic.type) {
243 case CTRL_TEXT: s = "text"; break;
244 case CTRL_EDITBOX: s = "editbox"; break;
245 case CTRL_RADIO: s = "radio"; break;
246 case CTRL_CHECKBOX: s = "checkbox"; break;
247 case CTRL_BUTTON: s = "button"; break;
248 case CTRL_LISTBOX: s = "listbox"; break;
249 case CTRL_COLUMNS: s = "columns"; break;
250 case CTRL_FILESELECT: s = "fileselect"; break;
251 case CTRL_FONTSELECT: s = "fontselect"; break;
252 case CTRL_TABDELAY: s = "tabdelay"; break;
253 default: s = "unknown"; break;
254 }
255 fprintf(stderr, " control: %s\n", s);
256 switch (ctrl->generic.type) {
257 case CTRL_TEXT:
258 macctrl_text(mcs, window, curstate, ctrl);
259 break;
260 case CTRL_RADIO:
261 macctrl_radio(mcs, window, curstate, ctrl);
262 break;
263 case CTRL_CHECKBOX:
264 macctrl_checkbox(mcs, window, curstate, ctrl);
265 break;
266 case CTRL_BUTTON:
267 macctrl_button(mcs, window, curstate, ctrl);
268 break;
269 case CTRL_LISTBOX:
270 if (ctrl->listbox.height == 0)
271 macctrl_popup(mcs, window, curstate, ctrl);
272 break;
273 }
274 }
275 }
276
277 static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
278 {
279 unsigned int i, j;
280 union macctrl *mc;
281
282 /* Panel 0 is special and always visible. */
283 for (i = 1; i < mcs->npanels; i++)
284 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next)
285 switch (mc->generic.type) {
286 case MACCTRL_TEXT:
287 if (i == which)
288 ShowControl(mc->text.tbctrl);
289 else
290 HideControl(mc->text.tbctrl);
291 break;
292 case MACCTRL_RADIO:
293 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
294 if (i == which)
295 ShowControl(mc->radio.tbctrls[j]);
296 else
297 HideControl(mc->radio.tbctrls[j]);
298 break;
299 case MACCTRL_CHECKBOX:
300 if (i == which)
301 ShowControl(mc->checkbox.tbctrl);
302 else
303 HideControl(mc->checkbox.tbctrl);
304 break;
305 case MACCTRL_BUTTON:
306 if (i == which)
307 ShowControl(mc->button.tbctrl);
308 else
309 HideControl(mc->button.tbctrl);
310 break;
311
312 }
313 }
314
315 static void macctrl_text(struct macctrls *mcs, WindowPtr window,
316 struct mac_layoutstate *curstate,
317 union control *ctrl)
318 {
319 union macctrl *mc = smalloc(sizeof *mc);
320 Rect bounds;
321
322 fprintf(stderr, " label = %s\n", ctrl->text.label);
323 mc->generic.type = MACCTRL_TEXT;
324 mc->generic.ctrl = ctrl;
325 bounds.left = curstate->pos.h;
326 bounds.right = bounds.left + curstate->width;
327 bounds.top = curstate->pos.v;
328 bounds.bottom = bounds.top + 16;
329 if (mac_gestalts.apprvers >= 0x100) {
330 SInt16 height;
331 Size olen;
332
333 mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
334 kControlStaticTextProc, (long)mc);
335 SetControlData(mc->text.tbctrl, kControlEntireControl,
336 kControlStaticTextTextTag,
337 strlen(ctrl->text.label), ctrl->text.label);
338 GetControlData(mc->text.tbctrl, kControlEntireControl,
339 kControlStaticTextTextHeightTag,
340 sizeof(height), &height, &olen);
341 fprintf(stderr, " height = %d\n", height);
342 SizeControl(mc->text.tbctrl, curstate->width, height);
343 curstate->pos.v += height + 6;
344 } else {
345 Str255 title;
346
347 c2pstrcpy(title, ctrl->text.label);
348 mc->text.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 0,
349 SYS7_TEXT_PROC, (long)mc);
350 }
351 add234(mcs->byctrl, mc);
352 mc->generic.next = mcs->panels[curstate->panelnum];
353 mcs->panels[curstate->panelnum] = mc;
354 }
355
356 #if !TARGET_API_MAC_CARBON
357 static pascal SInt32 macctrl_sys7_text_cdef(SInt16 variant, ControlRef control,
358 ControlDefProcMessage msg, SInt32 param)
359 {
360 RgnHandle rgn;
361
362 switch (msg) {
363 case drawCntl:
364 if ((*control)->contrlVis)
365 TETextBox((*control)->contrlTitle + 1, (*control)->contrlTitle[0],
366 &(*control)->contrlRect, teFlushDefault);
367 return 0;
368 case calcCRgns:
369 if (param & (1 << 31)) {
370 param &= ~(1 << 31);
371 goto calcthumbrgn;
372 }
373 /* FALLTHROUGH */
374 case calcCntlRgn:
375 rgn = (RgnHandle)param;
376 RectRgn(rgn, &(*control)->contrlRect);
377 return 0;
378 case calcThumbRgn:
379 calcthumbrgn:
380 rgn = (RgnHandle)param;
381 SetEmptyRgn(rgn);
382 return 0;
383 }
384
385 return 0;
386 }
387 #endif
388
389 static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
390 struct mac_layoutstate *curstate,
391 union control *ctrl)
392 {
393 union macctrl *mc = smalloc(sizeof *mc);
394 Rect bounds;
395 Str255 title;
396 unsigned int i, colwidth;
397
398 fprintf(stderr, " label = %s\n", ctrl->radio.label);
399 mc->generic.type = MACCTRL_RADIO;
400 mc->generic.ctrl = ctrl;
401 mc->radio.tbctrls =
402 smalloc(sizeof(*mc->radio.tbctrls) * ctrl->radio.nbuttons);
403 colwidth = (curstate->width + 13) / ctrl->radio.ncolumns;
404 for (i = 0; i < ctrl->radio.nbuttons; i++) {
405 fprintf(stderr, " button = %s\n", ctrl->radio.buttons[i]);
406 bounds.top = curstate->pos.v - 2;
407 bounds.bottom = bounds.top + 18;
408 bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
409 if (i == ctrl->radio.nbuttons - 1 ||
410 i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
411 bounds.right = curstate->pos.h + curstate->width;
412 curstate->pos.v += 18;
413 } else
414 bounds.right = bounds.left + colwidth - 13;
415 c2pstrcpy(title, ctrl->radio.buttons[i]);
416 mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE,
417 0, 0, 1, radioButProc, (long)mc);
418 }
419 curstate->pos.v += 4;
420 add234(mcs->byctrl, mc);
421 mc->generic.next = mcs->panels[curstate->panelnum];
422 mcs->panels[curstate->panelnum] = mc;
423 ctrlevent(mcs, mc, EVENT_REFRESH);
424 }
425
426 static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
427 struct mac_layoutstate *curstate,
428 union control *ctrl)
429 {
430 union macctrl *mc = smalloc(sizeof *mc);
431 Rect bounds;
432 Str255 title;
433
434 fprintf(stderr, " label = %s\n", ctrl->checkbox.label);
435 mc->generic.type = MACCTRL_CHECKBOX;
436 mc->generic.ctrl = ctrl;
437 bounds.left = curstate->pos.h;
438 bounds.right = bounds.left + curstate->width;
439 bounds.top = curstate->pos.v;
440 bounds.bottom = bounds.top + 16;
441 c2pstrcpy(title, ctrl->checkbox.label);
442 mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
443 checkBoxProc, (long)mc);
444 add234(mcs->byctrl, mc);
445 curstate->pos.v += 22;
446 mc->generic.next = mcs->panels[curstate->panelnum];
447 mcs->panels[curstate->panelnum] = mc;
448 ctrlevent(mcs, mc, EVENT_REFRESH);
449 }
450
451 static void macctrl_button(struct macctrls *mcs, WindowPtr window,
452 struct mac_layoutstate *curstate,
453 union control *ctrl)
454 {
455 union macctrl *mc = smalloc(sizeof *mc);
456 Rect bounds;
457 Str255 title;
458
459 fprintf(stderr, " label = %s\n", ctrl->button.label);
460 if (ctrl->button.isdefault)
461 fprintf(stderr, " is default\n");
462 mc->generic.type = MACCTRL_BUTTON;
463 mc->generic.ctrl = ctrl;
464 bounds.left = curstate->pos.h;
465 bounds.right = bounds.left + 100; /* XXX measure string */
466 bounds.top = curstate->pos.v;
467 bounds.bottom = bounds.top + 20;
468 c2pstrcpy(title, ctrl->button.label);
469 mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
470 pushButProc, (long)mc);
471 if (mac_gestalts.apprvers >= 0x100) {
472 Boolean isdefault = ctrl->button.isdefault;
473
474 SetControlData(mc->button.tbctrl, kControlEntireControl,
475 kControlPushButtonDefaultTag,
476 sizeof(isdefault), &isdefault);
477 } else if (ctrl->button.isdefault) {
478 InsetRect(&bounds, -4, -4);
479 NewControl(window, &bounds, title, TRUE, 0, 0, 1,
480 SYS7_DEFAULT_PROC, (long)mc);
481 }
482 if (mac_gestalts.apprvers >= 0x110) {
483 Boolean iscancel = ctrl->button.iscancel;
484
485 SetControlData(mc->button.tbctrl, kControlEntireControl,
486 kControlPushButtonCancelTag,
487 sizeof(iscancel), &iscancel);
488 }
489 add234(mcs->byctrl, mc);
490 mc->generic.next = mcs->panels[curstate->panelnum];
491 mcs->panels[curstate->panelnum] = mc;
492 curstate->pos.v += 26;
493 }
494
495 #if !TARGET_API_MAC_CARBON
496 static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
497 ControlRef control,
498 ControlDefProcMessage msg,
499 SInt32 param)
500 {
501 RgnHandle rgn;
502 Rect rect;
503 int oval;
504
505 switch (msg) {
506 case drawCntl:
507 if ((*control)->contrlVis) {
508 rect = (*control)->contrlRect;
509 PenNormal();
510 PenSize(3, 3);
511 oval = (rect.bottom - rect.top) / 2 + 2;
512 FrameRoundRect(&rect, oval, oval);
513 }
514 return 0;
515 case calcCRgns:
516 if (param & (1 << 31)) {
517 param &= ~(1 << 31);
518 goto calcthumbrgn;
519 }
520 /* FALLTHROUGH */
521 case calcCntlRgn:
522 rgn = (RgnHandle)param;
523 RectRgn(rgn, &(*control)->contrlRect);
524 return 0;
525 case calcThumbRgn:
526 calcthumbrgn:
527 rgn = (RgnHandle)param;
528 SetEmptyRgn(rgn);
529 return 0;
530 }
531
532 return 0;
533 }
534 #endif
535
536 static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
537 struct mac_layoutstate *curstate,
538 union control *ctrl)
539 {
540 union macctrl *mc = smalloc(sizeof *mc);
541 Rect bounds;
542 Str255 title;
543 unsigned int labelwidth;
544 static int nextmenuid;
545 int menuid;
546 MenuRef menu;
547
548 /*
549 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
550 * create a popup menu with dynamic content.
551 */
552 assert(ctrl->listbox.height == 0);
553 assert(!ctrl->listbox.draglist);
554 assert(!ctrl->listbox.multisel);
555
556 fprintf(stderr, " label = %s\n", ctrl->listbox.label);
557 fprintf(stderr, " percentwidth = %d\n", ctrl->listbox.percentwidth);
558
559 mc->generic.type = MACCTRL_POPUP;
560 mc->generic.ctrl = ctrl;
561 c2pstrcpy(title, ctrl->button.label);
562
563 /* Find a spare menu ID and create the menu */
564 while (GetMenuHandle(nextmenuid) != NULL)
565 if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
566 menuid = nextmenuid++;
567 menu = NewMenu(menuid, "\pdummy");
568 if (menu == NULL) return;
569 mc->popup.menu = menu;
570 mc->popup.menuid = menuid;
571 InsertMenu(menu, kInsertHierarchicalMenu);
572
573 /* The menu starts off empty */
574 mc->popup.nids = 0;
575 mc->popup.ids = NULL;
576
577 bounds.left = curstate->pos.h;
578 bounds.right = bounds.left + curstate->width;
579 bounds.top = curstate->pos.v;
580 bounds.bottom = bounds.top + 20;
581 /* XXX handle percentwidth == 100 */
582 labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
583 mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE,
584 popupTitleLeftJust, menuid, labelwidth,
585 popupMenuProc + popupFixedWidth, (long)mc);
586 add234(mcs->byctrl, mc);
587 curstate->pos.v += 26;
588 mc->generic.next = mcs->panels[curstate->panelnum];
589 mcs->panels[curstate->panelnum] = mc;
590 ctrlevent(mcs, mc, EVENT_REFRESH);
591 }
592
593
594 void macctrl_activate(WindowPtr window, EventRecord *event)
595 {
596 Boolean active = (event->modifiers & activeFlag) != 0;
597 GrafPtr saveport;
598 ControlRef root;
599
600 GetPort(&saveport);
601 SetPort((GrafPtr)GetWindowPort(window));
602 if (mac_gestalts.apprvers >= 0x100) {
603 SetThemeWindowBackground(window, active ?
604 kThemeBrushModelessDialogBackgroundActive :
605 kThemeBrushModelessDialogBackgroundInactive,
606 TRUE);
607 GetRootControl(window, &root);
608 if (active)
609 ActivateControl(root);
610 else
611 DeactivateControl(root);
612 } else {
613 /* (De)activate controls one at a time */
614 }
615 SetPort(saveport);
616 }
617
618 void macctrl_click(WindowPtr window, EventRecord *event)
619 {
620 Point mouse;
621 ControlHandle control;
622 int part;
623 GrafPtr saveport;
624 union macctrl *mc;
625 struct macctrls *mcs = mac_winctrls(window);
626 int i;
627
628 GetPort(&saveport);
629 SetPort((GrafPtr)GetWindowPort(window));
630 mouse = event->where;
631 GlobalToLocal(&mouse);
632 part = FindControl(mouse, window, &control);
633 if (control != NULL) {
634 mc = (union macctrl *)GetControlReference(control);
635 switch (mc->generic.type) {
636 case MACCTRL_POPUP:
637 TrackControl(control, mouse, (ControlActionUPP)-1);
638 ctrlevent(mcs, mc, EVENT_SELCHANGE);
639 case MACCTRL_RADIO:
640 if (TrackControl(control, mouse, NULL) != 0) {
641 for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
642 if (mc->radio.tbctrls[i] == control)
643 SetControlValue(mc->radio.tbctrls[i],
644 kControlRadioButtonCheckedValue);
645 else
646 SetControlValue(mc->radio.tbctrls[i],
647 kControlRadioButtonUncheckedValue);
648 ctrlevent(mcs, mc, EVENT_VALCHANGE);
649 }
650 break;
651 case MACCTRL_CHECKBOX:
652 if (TrackControl(control, mouse, NULL) != 0) {
653 SetControlValue(control, !GetControlValue(control));
654 ctrlevent(mcs, mc, EVENT_VALCHANGE);
655 }
656 break;
657 case MACCTRL_BUTTON:
658 if (TrackControl(control, mouse, NULL) != 0)
659 ctrlevent(mcs, mc, EVENT_ACTION);
660 break;
661 }
662 }
663 SetPort(saveport);
664 }
665
666 void macctrl_update(WindowPtr window)
667 {
668 #if TARGET_API_MAC_CARBON
669 RgnHandle visrgn;
670 #endif
671 Rect rect;
672 GrafPtr saveport;
673
674 BeginUpdate(window);
675 GetPort(&saveport);
676 SetPort((GrafPtr)GetWindowPort(window));
677 if (mac_gestalts.apprvers >= 0x101) {
678 #if TARGET_API_MAC_CARBON
679 GetPortBounds(GetWindowPort(window), &rect);
680 #else
681 rect = window->portRect;
682 #endif
683 InsetRect(&rect, -1, -1);
684 DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ?
685 kThemeStateActive : kThemeStateInactive);
686 }
687 #if TARGET_API_MAC_CARBON
688 visrgn = NewRgn();
689 GetPortVisibleRegion(GetWindowPort(window), visrgn);
690 UpdateControls(window, visrgn);
691 DisposeRgn(visrgn);
692 #else
693 UpdateControls(window, window->visRgn);
694 #endif
695 SetPort(saveport);
696 EndUpdate(window);
697 }
698
699 #if TARGET_API_MAC_CARBON
700 #define EnableItem EnableMenuItem
701 #define DisableItem DisableMenuItem
702 #endif
703 void macctrl_adjustmenus(WindowPtr window)
704 {
705 MenuHandle menu;
706
707 menu = GetMenuHandle(mFile);
708 DisableItem(menu, iSave); /* XXX enable if modified */
709 EnableItem(menu, iSaveAs);
710 EnableItem(menu, iDuplicate);
711
712 menu = GetMenuHandle(mEdit);
713 DisableItem(menu, 0);
714 }
715
716 void macctrl_close(WindowPtr window)
717 {
718 struct macctrls *mcs = mac_winctrls(window);
719 union macctrl *mc;
720
721 /*
722 * Mostly, we don't bother disposing of the Toolbox controls,
723 * since that will happen automatically when the window is
724 * disposed of. Popup menus are an exception, because we have to
725 * dispose of the menu ourselves, and doing that while the control
726 * still holds a reference to it seems rude.
727 */
728 while ((mc = index234(mcs->byctrl, 0)) != NULL) {
729 switch (mc->generic.type) {
730 case MACCTRL_POPUP:
731 DisposeControl(mc->popup.tbctrl);
732 DeleteMenu(mc->popup.menuid);
733 DisposeMenu(mc->popup.menu);
734 break;
735 }
736 del234(mcs->byctrl, mc);
737 sfree(mc);
738 }
739
740 freetree234(mcs->byctrl);
741 mcs->byctrl = NULL;
742 sfree(mcs->panels);
743 mcs->panels = NULL;
744 }
745
746 void dlg_update_start(union control *ctrl, void *dlg)
747 {
748
749 /* No-op for now */
750 }
751
752 void dlg_update_done(union control *ctrl, void *dlg)
753 {
754
755 /* No-op for now */
756 }
757
758 void dlg_set_focus(union control *ctrl, void *dlg)
759 {
760
761 if (mac_gestalts.apprvers >= 0x100) {
762 /* Use SetKeyboardFocus() */
763 } else {
764 /* Do our own mucking around */
765 }
766 }
767
768 union control *dlg_last_focused(union control *ctrl, void *dlg)
769 {
770
771 return NULL;
772 }
773
774 void dlg_beep(void *dlg)
775 {
776
777 SysBeep(30);
778 }
779
780 void dlg_error_msg(void *dlg, char *msg)
781 {
782 Str255 pmsg;
783
784 c2pstrcpy(pmsg, msg);
785 ParamText(pmsg, NULL, NULL, NULL);
786 StopAlert(128, NULL);
787 }
788
789 void dlg_end(void *dlg, int value)
790 {
791
792 };
793
794 void dlg_refresh(union control *ctrl, void *dlg)
795 {
796
797 };
798
799 void *dlg_get_privdata(union control *ctrl, void *dlg)
800 {
801
802 return NULL;
803 }
804
805 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
806 {
807
808 fatalbox("dlg_set_privdata");
809 }
810
811 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
812 {
813
814 fatalbox("dlg_alloc_privdata");
815 }
816
817
818 /*
819 * Radio Button control
820 */
821
822 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
823 {
824 struct macctrls *mcs = dlg;
825 union macctrl *mc = findbyctrl(mcs, ctrl);
826 int i;
827
828 assert(mc != NULL);
829 for (i = 0; i < ctrl->radio.nbuttons; i++) {
830 if (i == whichbutton)
831 SetControlValue(mc->radio.tbctrls[i],
832 kControlRadioButtonCheckedValue);
833 else
834 SetControlValue(mc->radio.tbctrls[i],
835 kControlRadioButtonUncheckedValue);
836 }
837
838 };
839
840 int dlg_radiobutton_get(union control *ctrl, void *dlg)
841 {
842 struct macctrls *mcs = dlg;
843 union macctrl *mc = findbyctrl(mcs, ctrl);
844 int i;
845
846 assert(mc != NULL);
847 for (i = 0; i < ctrl->radio.nbuttons; i++) {
848 if (GetControlValue(mc->radio.tbctrls[i]) ==
849 kControlRadioButtonCheckedValue)
850 return i;
851 }
852 return -1;
853 };
854
855
856 /*
857 * Check Box control
858 */
859
860 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
861 {
862 struct macctrls *mcs = dlg;
863 union macctrl *mc = findbyctrl(mcs, ctrl);
864
865 assert(mc != NULL);
866 SetControlValue(mc->checkbox.tbctrl,
867 checked ? kControlCheckBoxCheckedValue :
868 kControlCheckBoxUncheckedValue);
869 }
870
871 int dlg_checkbox_get(union control *ctrl, void *dlg)
872 {
873 struct macctrls *mcs = dlg;
874 union macctrl *mc = findbyctrl(mcs, ctrl);
875
876 assert(mc != NULL);
877 return GetControlValue(mc->checkbox.tbctrl);
878 }
879
880
881 /*
882 * Edit Box control
883 */
884
885 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
886 {
887
888 };
889
890 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
891 {
892
893 };
894
895
896 /*
897 * List Box control
898 */
899
900 static void dlg_macpopup_clear(union control *ctrl, void *dlg)
901 {
902 struct macctrls *mcs = dlg;
903 union macctrl *mc = findbyctrl(mcs, ctrl);
904 MenuRef menu = mc->popup.menu;
905 unsigned int i, n;
906
907 fprintf(stderr, " popup_clear\n");
908 n = CountMItems(menu);
909 for (i = 0; i < n; i++)
910 DeleteMenuItem(menu, n - i);
911 mc->popup.nids = 0;
912 sfree(mc->popup.ids);
913 mc->popup.ids = NULL;
914 SetControlMaximum(mc->popup.tbctrl, CountMItems(menu));
915 }
916
917 void dlg_listbox_clear(union control *ctrl, void *dlg)
918 {
919
920 if (ctrl->listbox.height == 0)
921 dlg_macpopup_clear(ctrl, dlg);
922 }
923
924 static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
925 {
926 struct macctrls *mcs = dlg;
927 union macctrl *mc = findbyctrl(mcs, ctrl);
928 MenuRef menu = mc->popup.menu;
929
930 fprintf(stderr, " popup_del %d\n", index);
931 DeleteMenuItem(menu, index + 1);
932 if (mc->popup.ids != NULL)
933 memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
934 (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
935 SetControlMaximum(mc->popup.tbctrl, CountMItems(menu));
936 }
937
938 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
939 {
940
941 if (ctrl->listbox.height == 0)
942 dlg_macpopup_del(ctrl, dlg, index);
943 }
944
945 static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
946 {
947 struct macctrls *mcs = dlg;
948 union macctrl *mc = findbyctrl(mcs, ctrl);
949 MenuRef menu = mc->popup.menu;
950 Str255 itemstring;
951
952 fprintf(stderr, " popup_add %s\n", text);
953 assert(text[0] != '\0');
954 c2pstrcpy(itemstring, text);
955 AppendMenu(menu, "\pdummy");
956 SetMenuItemText(menu, CountMItems(menu), itemstring);
957 SetControlMaximum(mc->popup.tbctrl, CountMItems(menu));
958 }
959
960 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
961 {
962
963 if (ctrl->listbox.height == 0)
964 dlg_macpopup_add(ctrl, dlg, text);
965 }
966
967 static void dlg_macpopup_addwithindex(union control *ctrl, void *dlg,
968 char const *text, int id)
969 {
970 struct macctrls *mcs = dlg;
971 union macctrl *mc = findbyctrl(mcs, ctrl);
972 MenuRef menu = mc->popup.menu;
973 unsigned int index;
974
975 fprintf(stderr, " popup_addwthindex %s, %d\n", text, id);
976 dlg_macpopup_add(ctrl, dlg, text);
977 index = CountMItems(menu) - 1;
978 if (mc->popup.nids <= index) {
979 mc->popup.nids = index + 1;
980 mc->popup.ids = srealloc(mc->popup.ids,
981 mc->popup.nids * sizeof(*mc->popup.ids));
982 }
983 mc->popup.ids[index] = id;
984 }
985
986 void dlg_listbox_addwithindex(union control *ctrl, void *dlg,
987 char const *text, int id)
988 {
989
990 if (ctrl->listbox.height == 0)
991 dlg_macpopup_addwithindex(ctrl, dlg, text, id);
992 }
993
994 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
995 {
996 struct macctrls *mcs = dlg;
997 union macctrl *mc = findbyctrl(mcs, ctrl);
998
999 if (ctrl->listbox.height == 0) {
1000 assert(mc->popup.ids != NULL && mc->popup.nids > index);
1001 return mc->popup.ids[index];
1002 }
1003 return 0;
1004 }
1005
1006 int dlg_listbox_index(union control *ctrl, void *dlg)
1007 {
1008 struct macctrls *mcs = dlg;
1009 union macctrl *mc = findbyctrl(mcs, ctrl);
1010
1011 if (ctrl->listbox.height == 0)
1012 return GetControlValue(mc->popup.tbctrl) - 1;
1013 return 0;
1014 };
1015
1016 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1017 {
1018 struct macctrls *mcs = dlg;
1019 union macctrl *mc = findbyctrl(mcs, ctrl);
1020
1021 if (ctrl->listbox.height == 0)
1022 return GetControlValue(mc->popup.tbctrl) - 1 == index;
1023 return 0;
1024 };
1025
1026 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1027 {
1028 struct macctrls *mcs = dlg;
1029 union macctrl *mc = findbyctrl(mcs, ctrl);
1030
1031 if (ctrl->listbox.height == 0)
1032 SetControlValue(mc->popup.tbctrl, index + 1);
1033 };
1034
1035
1036 /*
1037 * Text control
1038 */
1039
1040 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1041 {
1042 struct macctrls *mcs = dlg;
1043 union macctrl *mc = findbyctrl(mcs, ctrl);
1044 Str255 title;
1045
1046 assert(mc != NULL);
1047 if (mac_gestalts.apprvers >= 0x100)
1048 SetControlData(mc->text.tbctrl, kControlEntireControl,
1049 kControlStaticTextTextTag,
1050 strlen(ctrl->text.label), ctrl->text.label);
1051 else {
1052 c2pstrcpy(title, text);
1053 SetControlTitle(mc->text.tbctrl, title);
1054 }
1055 }
1056
1057
1058 /*
1059 * File Selector control
1060 */
1061
1062 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1063 {
1064
1065 }
1066
1067 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1068 {
1069
1070 }
1071
1072
1073 /*
1074 * Font Selector control
1075 */
1076
1077 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1078 {
1079
1080 }
1081
1082 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1083 {
1084
1085 }
1086
1087
1088 /*
1089 * Printer enumeration
1090 */
1091
1092 printer_enum *printer_start_enum(int *nprinters)
1093 {
1094
1095 *nprinters = 0;
1096 return NULL;
1097 }
1098
1099 char *printer_get_name(printer_enum *pe, int thing)
1100 {
1101
1102 return "<none>";
1103 }
1104
1105 void printer_finish_enum(printer_enum *pe)
1106 {
1107
1108 }
1109
1110
1111 /*
1112 * Colour selection stuff
1113 */
1114
1115 void dlg_coloursel_start(union control *ctrl, void *dlg,
1116 int r, int g, int b)
1117 {
1118
1119 }
1120
1121 int dlg_coloursel_results(union control *ctrl, void *dlg,
1122 int *r, int *g, int *b)
1123 {
1124
1125 return 0;
1126 }
1127
1128 /*
1129 * Local Variables:
1130 * c-file-style: "simon"
1131 * End:
1132 */