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