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