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