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