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