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