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