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