In a couple of places, snewn() was being asked for an array of char which was
[u/mdw/putty] / mac / macctrls.c
CommitLineData
f28d33e8 1/* $Id: macctrls.c,v 1.20 2003/03/29 22:04:21 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>
30#include <Controls.h>
31#include <ControlDefinitions.h>
56c01970 32#include <Menus.h>
fa4942e7 33#include <Resources.h>
0e9ce565 34#include <Script.h>
8a7e67ec 35#include <Sound.h>
0e9ce565 36#include <TextEdit.h>
8a7e67ec 37#include <TextUtils.h>
0e9ce565 38#include <ToolUtils.h>
8a7e67ec 39#include <Windows.h>
40
93ba25f8 41#include <assert.h>
ee10bc56 42#include <string.h>
93ba25f8 43
8a7e67ec 44#include "putty.h"
45#include "mac.h"
46#include "macresid.h"
47#include "dialog.h"
48#include "tree234.h"
49
56c01970 50/* Range of menu IDs for popup menus */
51#define MENU_MIN 1024
52#define MENU_MAX 2048
53
54
8a7e67ec 55union macctrl {
56 struct macctrl_generic {
57 enum {
58 MACCTRL_TEXT,
1f1ec0e5 59 MACCTRL_EDITBOX,
8a7e67ec 60 MACCTRL_RADIO,
61 MACCTRL_CHECKBOX,
56c01970 62 MACCTRL_BUTTON,
63 MACCTRL_POPUP
8a7e67ec 64 } type;
65 /* Template from which this was generated */
66 union control *ctrl;
ee10bc56 67 /* Next control in this panel */
68 union macctrl *next;
1f1ec0e5 69 void *privdata;
70 int freeprivdata;
8a7e67ec 71 } generic;
72 struct {
73 struct macctrl_generic generic;
74 ControlRef tbctrl;
75 } text;
76 struct {
77 struct macctrl_generic generic;
1f1ec0e5 78 ControlRef tbctrl;
cd3e71e8 79 ControlRef tblabel;
1f1ec0e5 80 } editbox;
81 struct {
82 struct macctrl_generic generic;
8a7e67ec 83 ControlRef *tbctrls;
cd3e71e8 84 ControlRef tblabel;
8a7e67ec 85 } radio;
86 struct {
87 struct macctrl_generic generic;
88 ControlRef tbctrl;
89 } checkbox;
90 struct {
91 struct macctrl_generic generic;
92 ControlRef tbctrl;
93 } button;
56c01970 94 struct {
95 struct macctrl_generic generic;
96 ControlRef tbctrl;
97 MenuRef menu;
98 int menuid;
99 unsigned int nids;
100 int *ids;
101 } popup;
8a7e67ec 102};
103
104struct mac_layoutstate {
105 Point pos;
106 unsigned int width;
ee10bc56 107 unsigned int panelnum;
8a7e67ec 108};
109
110#define ctrlevent(mcs, mc, event) do { \
111 if ((mc)->generic.ctrl->generic.handler != NULL) \
93ba25f8 112 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
8a7e67ec 113 (mcs)->data, (event)); \
114} while (0)
115
93ba25f8 116#define findbyctrl(mcs, ctrl) \
117 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
118
8a7e67ec 119static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *,
120 WindowPtr, struct macctrls *);
ee10bc56 121static void macctrl_switchtopanel(struct macctrls *, unsigned int);
a460b361 122static void macctrl_setfocus(struct macctrls *, union macctrl *);
8a7e67ec 123static void macctrl_text(struct macctrls *, WindowPtr,
124 struct mac_layoutstate *, union control *);
1f1ec0e5 125static void macctrl_editbox(struct macctrls *, WindowPtr,
126 struct mac_layoutstate *, union control *);
8a7e67ec 127static void macctrl_radio(struct macctrls *, WindowPtr,
128 struct mac_layoutstate *, union control *);
129static void macctrl_checkbox(struct macctrls *, WindowPtr,
130 struct mac_layoutstate *, union control *);
131static void macctrl_button(struct macctrls *, WindowPtr,
132 struct mac_layoutstate *, union control *);
56c01970 133static void macctrl_popup(struct macctrls *, WindowPtr,
134 struct mac_layoutstate *, union control *);
fa4942e7 135#if !TARGET_API_MAC_CARBON
0e9ce565 136static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef,
137 ControlDefProcMessage, SInt32);
5537f572 138static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef,
139 ControlDefProcMessage, SInt32);
fa4942e7 140#endif
141
142#if !TARGET_API_MAC_CARBON
143/*
144 * This trick enables us to keep all the CDEF code in the main
145 * application, which makes life easier. For details, see
146 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
147 */
148
149#pragma options align=mac68k
150typedef struct {
151 short jmpabs; /* 4EF9 */
152 ControlDefUPP theUPP;
153} **PatchCDEF;
154#pragma options align=reset
155#endif
156
157static void macctrl_init()
158{
159#if !TARGET_API_MAC_CARBON
160 static int inited = 0;
161 PatchCDEF cdef;
162
163 if (inited) return;
0e9ce565 164 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox);
165 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef);
5537f572 166 cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default);
167 (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef);
fa4942e7 168 inited = 1;
169#endif
170}
171
8a7e67ec 172
173static int macctrl_cmp_byctrl(void *av, void *bv)
174{
175 union macctrl *a = (union macctrl *)av;
176 union macctrl *b = (union macctrl *)bv;
177
178 if (a->generic.ctrl < b->generic.ctrl)
179 return -1;
180 else if (a->generic.ctrl > b->generic.ctrl)
181 return +1;
182 else
183 return 0;
184}
185
93ba25f8 186static int macctrl_cmp_byctrl_find(void *av, void *bv)
187{
188 union control *a = (union control *)av;
189 union macctrl *b = (union macctrl *)bv;
190
191 if (a < b->generic.ctrl)
192 return -1;
193 else if (a > b->generic.ctrl)
194 return +1;
195 else
196 return 0;
197}
198
8a7e67ec 199void macctrl_layoutbox(struct controlbox *cb, WindowPtr window,
200 struct macctrls *mcs)
201{
202 int i;
203 struct mac_layoutstate curstate;
204 ControlRef root;
205 Rect rect;
fa4942e7 206
207 macctrl_init();
8a7e67ec 208#if TARGET_API_MAC_CARBON
209 GetPortBounds(GetWindowPort(window), &rect);
210#else
211 rect = window->portRect;
212#endif
213 curstate.pos.h = rect.left + 13;
ee10bc56 214 curstate.pos.v = rect.bottom - 59;
8a7e67ec 215 curstate.width = rect.right - rect.left - (13 * 2);
216 if (mac_gestalts.apprvers >= 0x100)
217 CreateRootControl(window, &root);
1f1ec0e5 218 mcs->window = window;
8a7e67ec 219 mcs->byctrl = newtree234(macctrl_cmp_byctrl);
a460b361 220 mcs->focus = NULL;
ee10bc56 221 /* Count the number of panels */
222 mcs->npanels = 1;
223 for (i = 1; i < cb->nctrlsets; i++)
224 if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname))
225 mcs->npanels++;
226 mcs->panels = smalloc(sizeof(*mcs->panels) * mcs->npanels);
227 memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels);
228 curstate.panelnum = 0;
229 for (i = 0; i < cb->nctrlsets; i++) {
230 if (i > 0 && strcmp(cb->ctrlsets[i]->pathname,
231 cb->ctrlsets[i-1]->pathname)) {
232 curstate.pos.v = rect.top + 13;
233 curstate.panelnum++;
234 assert(curstate.panelnum < mcs->npanels);
235 }
8a7e67ec 236 macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs);
ee10bc56 237 }
f28d33e8 238 macctrl_switchtopanel(mcs, 2);
239 /* 14 = proxies, 20 = SSH bugs */
8a7e67ec 240}
241
242static void macctrl_layoutset(struct mac_layoutstate *curstate,
243 struct controlset *s,
244 WindowPtr window, struct macctrls *mcs)
245{
246 unsigned int i;
247
248 fprintf(stderr, "--- begin set ---\n");
93ba25f8 249 fprintf(stderr, "pathname = %s\n", s->pathname);
8a7e67ec 250 if (s->boxname && *s->boxname)
251 fprintf(stderr, "boxname = %s\n", s->boxname);
252 if (s->boxtitle)
253 fprintf(stderr, "boxtitle = %s\n", s->boxtitle);
254
255
256 for (i = 0; i < s->ncontrols; i++) {
257 union control *ctrl = s->ctrls[i];
258 char const *s;
259
260 switch (ctrl->generic.type) {
261 case CTRL_TEXT: s = "text"; break;
262 case CTRL_EDITBOX: s = "editbox"; break;
263 case CTRL_RADIO: s = "radio"; break;
264 case CTRL_CHECKBOX: s = "checkbox"; break;
265 case CTRL_BUTTON: s = "button"; break;
266 case CTRL_LISTBOX: s = "listbox"; break;
267 case CTRL_COLUMNS: s = "columns"; break;
268 case CTRL_FILESELECT: s = "fileselect"; break;
269 case CTRL_FONTSELECT: s = "fontselect"; break;
270 case CTRL_TABDELAY: s = "tabdelay"; break;
271 default: s = "unknown"; break;
272 }
273 fprintf(stderr, " control: %s\n", s);
274 switch (ctrl->generic.type) {
275 case CTRL_TEXT:
276 macctrl_text(mcs, window, curstate, ctrl);
277 break;
1f1ec0e5 278 case CTRL_EDITBOX:
279 macctrl_editbox(mcs, window, curstate, ctrl);
280 break;
8a7e67ec 281 case CTRL_RADIO:
282 macctrl_radio(mcs, window, curstate, ctrl);
283 break;
284 case CTRL_CHECKBOX:
285 macctrl_checkbox(mcs, window, curstate, ctrl);
286 break;
287 case CTRL_BUTTON:
288 macctrl_button(mcs, window, curstate, ctrl);
289 break;
56c01970 290 case CTRL_LISTBOX:
291 if (ctrl->listbox.height == 0)
292 macctrl_popup(mcs, window, curstate, ctrl);
293 break;
8a7e67ec 294 }
295 }
296}
297
ee10bc56 298static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which)
299{
300 unsigned int i, j;
301 union macctrl *mc;
302
0a33efe1 303#define hideshow(c) do { \
304 if (i == which) ShowControl(c); else HideControl(c); \
305} while (0)
306
307 mcs->curpanel = which;
ee10bc56 308 /* Panel 0 is special and always visible. */
309 for (i = 1; i < mcs->npanels; i++)
a460b361 310 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
311#if !TARGET_API_MAC_CARBON
312 if (mcs->focus == mc)
313 macctrl_setfocus(mcs, NULL);
314#endif
ee10bc56 315 switch (mc->generic.type) {
316 case MACCTRL_TEXT:
0a33efe1 317 hideshow(mc->text.tbctrl);
ee10bc56 318 break;
1f1ec0e5 319 case MACCTRL_EDITBOX:
320 hideshow(mc->editbox.tbctrl);
cd3e71e8 321 hideshow(mc->editbox.tblabel);
1f1ec0e5 322 break;
ee10bc56 323 case MACCTRL_RADIO:
324 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
0a33efe1 325 hideshow(mc->radio.tbctrls[j]);
cd3e71e8 326 hideshow(mc->radio.tblabel);
ee10bc56 327 break;
328 case MACCTRL_CHECKBOX:
0a33efe1 329 hideshow(mc->checkbox.tbctrl);
ee10bc56 330 break;
331 case MACCTRL_BUTTON:
0a33efe1 332 hideshow(mc->button.tbctrl);
333 break;
334 case MACCTRL_POPUP:
335 hideshow(mc->popup.tbctrl);
ee10bc56 336 break;
ee10bc56 337 }
a460b361 338 }
339}
340
341#if !TARGET_API_MAC_CARBON
342/*
343 * System 7 focus manipulation
344 */
345static void macctrl_defocus(union macctrl *mc)
346{
347
348 assert(mac_gestalts.apprvers < 0x100);
349 switch (mc->generic.type) {
350 case MACCTRL_EDITBOX:
351 TEDeactivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
352 break;
353 }
354}
355
356static void macctrl_enfocus(union macctrl *mc)
357{
358
359 assert(mac_gestalts.apprvers < 0x100);
360 switch (mc->generic.type) {
361 case MACCTRL_EDITBOX:
362 TEActivate((TEHandle)(*mc->editbox.tbctrl)->contrlData);
363 break;
364 }
ee10bc56 365}
366
a460b361 367static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc)
368{
369
370 if (mcs->focus != NULL)
371 macctrl_defocus(mcs->focus);
372 mcs->focus = mc;
373 if (mc != NULL)
374 macctrl_enfocus(mc);
375}
376#endif
377
8a7e67ec 378static void macctrl_text(struct macctrls *mcs, WindowPtr window,
379 struct mac_layoutstate *curstate,
380 union control *ctrl)
381{
382 union macctrl *mc = smalloc(sizeof *mc);
383 Rect bounds;
f28d33e8 384 SInt16 height;
8a7e67ec 385
386 fprintf(stderr, " label = %s\n", ctrl->text.label);
387 mc->generic.type = MACCTRL_TEXT;
388 mc->generic.ctrl = ctrl;
1f1ec0e5 389 mc->generic.privdata = NULL;
8a7e67ec 390 bounds.left = curstate->pos.h;
391 bounds.right = bounds.left + curstate->width;
392 bounds.top = curstate->pos.v;
393 bounds.bottom = bounds.top + 16;
394 if (mac_gestalts.apprvers >= 0x100) {
8a7e67ec 395 Size olen;
396
397 mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
398 kControlStaticTextProc, (long)mc);
399 SetControlData(mc->text.tbctrl, kControlEntireControl,
400 kControlStaticTextTextTag,
401 strlen(ctrl->text.label), ctrl->text.label);
402 GetControlData(mc->text.tbctrl, kControlEntireControl,
403 kControlStaticTextTextHeightTag,
404 sizeof(height), &height, &olen);
f28d33e8 405 }
406#if !TARGET_API_MAC_CARBON
407 else {
408 TEHandle te;
fa4942e7 409
f28d33e8 410 mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
fa4942e7 411 SYS7_TEXT_PROC, (long)mc);
f28d33e8 412 te = (TEHandle)(*mc->text.tbctrl)->contrlData;
413 TESetText(ctrl->text.label, strlen(ctrl->text.label), te);
414 height = TEGetHeight(1, (*te)->nLines, te);
8a7e67ec 415 }
f28d33e8 416#endif
417 fprintf(stderr, " height = %d\n", height);
418 SizeControl(mc->text.tbctrl, curstate->width, height);
419 curstate->pos.v += height + 6;
8a7e67ec 420 add234(mcs->byctrl, mc);
ee10bc56 421 mc->generic.next = mcs->panels[curstate->panelnum];
422 mcs->panels[curstate->panelnum] = mc;
8a7e67ec 423}
424
1f1ec0e5 425static void macctrl_editbox(struct macctrls *mcs, WindowPtr window,
426 struct mac_layoutstate *curstate,
427 union control *ctrl)
428{
429 union macctrl *mc = smalloc(sizeof *mc);
cd3e71e8 430 Rect lbounds, bounds;
1f1ec0e5 431
432 fprintf(stderr, " label = %s\n", ctrl->editbox.label);
433 fprintf(stderr, " percentwidth = %d\n", ctrl->editbox.percentwidth);
434 if (ctrl->editbox.password) fprintf(stderr, " password\n");
435 if (ctrl->editbox.has_list) fprintf(stderr, " has list\n");
436 mc->generic.type = MACCTRL_EDITBOX;
437 mc->generic.ctrl = ctrl;
438 mc->generic.privdata = NULL;
cd3e71e8 439 lbounds.left = curstate->pos.h;
440 lbounds.top = curstate->pos.v;
441 if (ctrl->editbox.percentwidth == 100) {
442 lbounds.right = lbounds.left + curstate->width;
443 lbounds.bottom = lbounds.top + 16;
444 bounds.left = curstate->pos.h;
445 bounds.right = bounds.left + curstate->width;
446 curstate->pos.v += 18;
447 } else {
448 lbounds.right = lbounds.left +
449 curstate->width * (100 - ctrl->editbox.percentwidth) / 100;
450 lbounds.bottom = lbounds.top + 22;
451 bounds.left = lbounds.right;
452 bounds.right = lbounds.left + curstate->width;
453 }
0e9ce565 454 bounds.top = curstate->pos.v;
455 bounds.bottom = bounds.top + 22;
1f1ec0e5 456 if (mac_gestalts.apprvers >= 0x100) {
cd3e71e8 457 mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE, 0, 0, 0,
458 kControlStaticTextProc, (long)mc);
459 SetControlData(mc->editbox.tblabel, kControlEntireControl,
460 kControlStaticTextTextTag,
461 strlen(ctrl->editbox.label), ctrl->editbox.label);
0e9ce565 462 InsetRect(&bounds, 2, 2);
cd3e71e8 463 mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
464 ctrl->editbox.password ?
465 kControlEditTextPasswordProc :
466 kControlEditTextProc, (long)mc);
f28d33e8 467 }
468#if !TARGET_API_MAC_CARBON
469 else {
470 mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE,
cd3e71e8 471 0, 0, 0, SYS7_TEXT_PROC, (long)mc);
f28d33e8 472 TESetText(ctrl->editbox.label, strlen(ctrl->editbox.label),
473 (TEHandle)(*mc->editbox.tblabel)->contrlData);
cd3e71e8 474 mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
475 SYS7_EDITBOX_PROC, (long)mc);
1f1ec0e5 476 }
f28d33e8 477#endif
0e9ce565 478 curstate->pos.v += 28;
479 add234(mcs->byctrl, mc);
480 mc->generic.next = mcs->panels[curstate->panelnum];
481 mcs->panels[curstate->panelnum] = mc;
482 ctrlevent(mcs, mc, EVENT_REFRESH);
1f1ec0e5 483}
484
0e9ce565 485#if !TARGET_API_MAC_CARBON
486static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant,
487 ControlRef control,
488 ControlDefProcMessage msg,
489 SInt32 param)
490{
491 RgnHandle rgn;
492 Rect rect;
493 TEHandle te;
494 long ssfs;
a460b361 495 Point mouse;
0e9ce565 496
497 switch (msg) {
498 case initCntl:
499 rect = (*control)->contrlRect;
f28d33e8 500 if (variant == SYS7_EDITBOX_VARIANT)
501 InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */
0e9ce565 502 te = TENew(&rect, &rect);
503 ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize);
504 (*te)->txSize = LoWord(ssfs);
505 (*te)->txFont = HiWord(ssfs);
506 (*control)->contrlData = (Handle)te;
507 return noErr;
508 case dispCntl:
509 TEDispose((TEHandle)(*control)->contrlData);
510 return 0;
511 case drawCntl:
512 if ((*control)->contrlVis) {
513 rect = (*control)->contrlRect;
f28d33e8 514 if (variant == SYS7_EDITBOX_VARIANT) {
515 PenNormal();
516 FrameRect(&rect);
517 InsetRect(&rect, 3, 3);
518 }
519 (*(TEHandle)(*control)->contrlData)->viewRect = rect;
0e9ce565 520 TEUpdate(&rect, (TEHandle)(*control)->contrlData);
521 }
522 return 0;
a460b361 523 case testCntl:
f28d33e8 524 if (variant == SYS7_TEXT_VARIANT)
525 return kControlNoPart;
a460b361 526 mouse.h = LoWord(param);
527 mouse.v = HiWord(param);
f28d33e8 528 rect = (*control)->contrlRect;
529 InsetRect(&rect, 3, 3);
530 return PtInRect(mouse, &rect) ? kControlEditTextPart : kControlNoPart;
0e9ce565 531 case calcCRgns:
532 if (param & (1 << 31)) {
533 param &= ~(1 << 31);
534 goto calcthumbrgn;
535 }
536 /* FALLTHROUGH */
537 case calcCntlRgn:
538 rgn = (RgnHandle)param;
539 RectRgn(rgn, &(*control)->contrlRect);
540 return 0;
541 case calcThumbRgn:
542 calcthumbrgn:
543 rgn = (RgnHandle)param;
544 SetEmptyRgn(rgn);
545 return 0;
546 }
547
548 return 0;
549}
550#endif
551
8a7e67ec 552static void macctrl_radio(struct macctrls *mcs, WindowPtr window,
553 struct mac_layoutstate *curstate,
554 union control *ctrl)
555{
556 union macctrl *mc = smalloc(sizeof *mc);
557 Rect bounds;
558 Str255 title;
559 unsigned int i, colwidth;
560
561 fprintf(stderr, " label = %s\n", ctrl->radio.label);
562 mc->generic.type = MACCTRL_RADIO;
563 mc->generic.ctrl = ctrl;
1f1ec0e5 564 mc->generic.privdata = NULL;
8a7e67ec 565 mc->radio.tbctrls =
566 smalloc(sizeof(*mc->radio.tbctrls) * ctrl->radio.nbuttons);
567 colwidth = (curstate->width + 13) / ctrl->radio.ncolumns;
cd3e71e8 568 bounds.top = curstate->pos.v;
569 bounds.bottom = bounds.top + 16;
570 bounds.left = curstate->pos.h;
571 bounds.right = bounds.left + curstate->width;
572 if (mac_gestalts.apprvers >= 0x100) {
573 mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0,
574 kControlStaticTextProc, (long)mc);
575 SetControlData(mc->radio.tblabel, kControlEntireControl,
576 kControlStaticTextTextTag,
577 strlen(ctrl->radio.label), ctrl->radio.label);
f28d33e8 578 }
579 #if !TARGET_API_MAC_CARBON
580 else {
581 mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE,
cd3e71e8 582 0, 0, 0, SYS7_TEXT_PROC, (long)mc);
f28d33e8 583 TESetText(ctrl->radio.label, strlen(ctrl->radio.label),
584 (TEHandle)(*mc->radio.tblabel)->contrlData);
cd3e71e8 585 }
f28d33e8 586#endif
cd3e71e8 587 curstate->pos.v += 18;
8a7e67ec 588 for (i = 0; i < ctrl->radio.nbuttons; i++) {
589 fprintf(stderr, " button = %s\n", ctrl->radio.buttons[i]);
35790008 590 bounds.top = curstate->pos.v - 2;
591 bounds.bottom = bounds.top + 18;
8a7e67ec 592 bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns);
593 if (i == ctrl->radio.nbuttons - 1 ||
594 i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) {
595 bounds.right = curstate->pos.h + curstate->width;
35790008 596 curstate->pos.v += 18;
8a7e67ec 597 } else
598 bounds.right = bounds.left + colwidth - 13;
599 c2pstrcpy(title, ctrl->radio.buttons[i]);
600 mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE,
601 0, 0, 1, radioButProc, (long)mc);
602 }
35790008 603 curstate->pos.v += 4;
8a7e67ec 604 add234(mcs->byctrl, mc);
ee10bc56 605 mc->generic.next = mcs->panels[curstate->panelnum];
606 mcs->panels[curstate->panelnum] = mc;
8a7e67ec 607 ctrlevent(mcs, mc, EVENT_REFRESH);
608}
609
610static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window,
611 struct mac_layoutstate *curstate,
612 union control *ctrl)
613{
614 union macctrl *mc = smalloc(sizeof *mc);
615 Rect bounds;
616 Str255 title;
617
618 fprintf(stderr, " label = %s\n", ctrl->checkbox.label);
619 mc->generic.type = MACCTRL_CHECKBOX;
620 mc->generic.ctrl = ctrl;
1f1ec0e5 621 mc->generic.privdata = NULL;
8a7e67ec 622 bounds.left = curstate->pos.h;
623 bounds.right = bounds.left + curstate->width;
624 bounds.top = curstate->pos.v;
625 bounds.bottom = bounds.top + 16;
626 c2pstrcpy(title, ctrl->checkbox.label);
627 mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
628 checkBoxProc, (long)mc);
629 add234(mcs->byctrl, mc);
630 curstate->pos.v += 22;
ee10bc56 631 mc->generic.next = mcs->panels[curstate->panelnum];
632 mcs->panels[curstate->panelnum] = mc;
8a7e67ec 633 ctrlevent(mcs, mc, EVENT_REFRESH);
634}
635
636static void macctrl_button(struct macctrls *mcs, WindowPtr window,
637 struct mac_layoutstate *curstate,
638 union control *ctrl)
639{
640 union macctrl *mc = smalloc(sizeof *mc);
641 Rect bounds;
642 Str255 title;
643
644 fprintf(stderr, " label = %s\n", ctrl->button.label);
645 if (ctrl->button.isdefault)
646 fprintf(stderr, " is default\n");
647 mc->generic.type = MACCTRL_BUTTON;
648 mc->generic.ctrl = ctrl;
1f1ec0e5 649 mc->generic.privdata = NULL;
8a7e67ec 650 bounds.left = curstate->pos.h;
651 bounds.right = bounds.left + 100; /* XXX measure string */
652 bounds.top = curstate->pos.v;
653 bounds.bottom = bounds.top + 20;
654 c2pstrcpy(title, ctrl->button.label);
655 mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1,
656 pushButProc, (long)mc);
657 if (mac_gestalts.apprvers >= 0x100) {
658 Boolean isdefault = ctrl->button.isdefault;
659
660 SetControlData(mc->button.tbctrl, kControlEntireControl,
661 kControlPushButtonDefaultTag,
662 sizeof(isdefault), &isdefault);
5537f572 663 } else if (ctrl->button.isdefault) {
664 InsetRect(&bounds, -4, -4);
665 NewControl(window, &bounds, title, TRUE, 0, 0, 1,
666 SYS7_DEFAULT_PROC, (long)mc);
8a7e67ec 667 }
855d05c4 668 if (mac_gestalts.apprvers >= 0x110) {
669 Boolean iscancel = ctrl->button.iscancel;
670
671 SetControlData(mc->button.tbctrl, kControlEntireControl,
672 kControlPushButtonCancelTag,
673 sizeof(iscancel), &iscancel);
674 }
8a7e67ec 675 add234(mcs->byctrl, mc);
ee10bc56 676 mc->generic.next = mcs->panels[curstate->panelnum];
677 mcs->panels[curstate->panelnum] = mc;
8a7e67ec 678 curstate->pos.v += 26;
679}
680
5537f572 681#if !TARGET_API_MAC_CARBON
682static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant,
683 ControlRef control,
684 ControlDefProcMessage msg,
685 SInt32 param)
686{
687 RgnHandle rgn;
688 Rect rect;
689 int oval;
690
691 switch (msg) {
692 case drawCntl:
693 if ((*control)->contrlVis) {
694 rect = (*control)->contrlRect;
695 PenNormal();
696 PenSize(3, 3);
697 oval = (rect.bottom - rect.top) / 2 + 2;
698 FrameRoundRect(&rect, oval, oval);
699 }
700 return 0;
701 case calcCRgns:
702 if (param & (1 << 31)) {
703 param &= ~(1 << 31);
704 goto calcthumbrgn;
705 }
706 /* FALLTHROUGH */
707 case calcCntlRgn:
708 rgn = (RgnHandle)param;
709 RectRgn(rgn, &(*control)->contrlRect);
710 return 0;
711 case calcThumbRgn:
712 calcthumbrgn:
713 rgn = (RgnHandle)param;
714 SetEmptyRgn(rgn);
715 return 0;
716 }
717
718 return 0;
719}
720#endif
721
56c01970 722static void macctrl_popup(struct macctrls *mcs, WindowPtr window,
723 struct mac_layoutstate *curstate,
724 union control *ctrl)
725{
726 union macctrl *mc = smalloc(sizeof *mc);
727 Rect bounds;
728 Str255 title;
729 unsigned int labelwidth;
0a33efe1 730 static int nextmenuid = MENU_MIN;
56c01970 731 int menuid;
732 MenuRef menu;
733
734 /*
735 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
736 * create a popup menu with dynamic content.
737 */
738 assert(ctrl->listbox.height == 0);
739 assert(!ctrl->listbox.draglist);
740 assert(!ctrl->listbox.multisel);
741
742 fprintf(stderr, " label = %s\n", ctrl->listbox.label);
743 fprintf(stderr, " percentwidth = %d\n", ctrl->listbox.percentwidth);
744
745 mc->generic.type = MACCTRL_POPUP;
746 mc->generic.ctrl = ctrl;
1f1ec0e5 747 mc->generic.privdata = NULL;
56c01970 748 c2pstrcpy(title, ctrl->button.label);
749
750 /* Find a spare menu ID and create the menu */
751 while (GetMenuHandle(nextmenuid) != NULL)
752 if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN;
753 menuid = nextmenuid++;
754 menu = NewMenu(menuid, "\pdummy");
755 if (menu == NULL) return;
756 mc->popup.menu = menu;
757 mc->popup.menuid = menuid;
758 InsertMenu(menu, kInsertHierarchicalMenu);
759
760 /* The menu starts off empty */
761 mc->popup.nids = 0;
762 mc->popup.ids = NULL;
763
764 bounds.left = curstate->pos.h;
765 bounds.right = bounds.left + curstate->width;
766 bounds.top = curstate->pos.v;
767 bounds.bottom = bounds.top + 20;
768 /* XXX handle percentwidth == 100 */
769 labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100;
770 mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE,
771 popupTitleLeftJust, menuid, labelwidth,
772 popupMenuProc + popupFixedWidth, (long)mc);
773 add234(mcs->byctrl, mc);
774 curstate->pos.v += 26;
775 mc->generic.next = mcs->panels[curstate->panelnum];
776 mcs->panels[curstate->panelnum] = mc;
777 ctrlevent(mcs, mc, EVENT_REFRESH);
778}
779
8a7e67ec 780
781void macctrl_activate(WindowPtr window, EventRecord *event)
782{
0a33efe1 783 struct macctrls *mcs = mac_winctrls(window);
8a7e67ec 784 Boolean active = (event->modifiers & activeFlag) != 0;
785 GrafPtr saveport;
0a33efe1 786 int i, j;
787 ControlPartCode state;
788 union macctrl *mc;
8a7e67ec 789
790 GetPort(&saveport);
791 SetPort((GrafPtr)GetWindowPort(window));
78f015b8 792 if (mac_gestalts.apprvers >= 0x100)
8a7e67ec 793 SetThemeWindowBackground(window, active ?
794 kThemeBrushModelessDialogBackgroundActive :
795 kThemeBrushModelessDialogBackgroundInactive,
796 TRUE);
0a33efe1 797 state = active ? kControlNoPart : kControlInactivePart;
798 for (i = 0; i <= mcs->curpanel; i += mcs->curpanel)
f28d33e8 799 for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) {
0a33efe1 800 switch (mc->generic.type) {
801 case MACCTRL_TEXT:
802 HiliteControl(mc->text.tbctrl, state);
803 break;
1f1ec0e5 804 case MACCTRL_EDITBOX:
805 HiliteControl(mc->editbox.tbctrl, state);
cd3e71e8 806 HiliteControl(mc->editbox.tblabel, state);
1f1ec0e5 807 break;
0a33efe1 808 case MACCTRL_RADIO:
809 for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++)
810 HiliteControl(mc->radio.tbctrls[j], state);
cd3e71e8 811 HiliteControl(mc->radio.tblabel, state);
0a33efe1 812 break;
813 case MACCTRL_CHECKBOX:
814 HiliteControl(mc->checkbox.tbctrl, state);
815 break;
816 case MACCTRL_BUTTON:
817 HiliteControl(mc->button.tbctrl, state);
818 break;
819 case MACCTRL_POPUP:
820 HiliteControl(mc->popup.tbctrl, state);
821 break;
822 }
f28d33e8 823#if !TARGET_API_MAC_CARBON
824 if (mcs->focus == mc) {
825 if (active)
826 macctrl_enfocus(mc);
827 else
828 macctrl_defocus(mc);
829 }
830#endif
831 }
8a7e67ec 832 SetPort(saveport);
833}
834
835void macctrl_click(WindowPtr window, EventRecord *event)
836{
837 Point mouse;
838 ControlHandle control;
1f1ec0e5 839 int part, trackresult;
8a7e67ec 840 GrafPtr saveport;
841 union macctrl *mc;
842 struct macctrls *mcs = mac_winctrls(window);
843 int i;
1f1ec0e5 844 UInt32 features;
8a7e67ec 845
846 GetPort(&saveport);
847 SetPort((GrafPtr)GetWindowPort(window));
848 mouse = event->where;
849 GlobalToLocal(&mouse);
850 part = FindControl(mouse, window, &control);
56c01970 851 if (control != NULL) {
a460b361 852 mc = (union macctrl *)GetControlReference(control);
1f1ec0e5 853 if (mac_gestalts.apprvers >= 0x100) {
854 if (GetControlFeatures(control, &features) == noErr &&
855 (features & kControlSupportsFocus) &&
856 (features & kControlGetsFocusOnClick))
857 SetKeyboardFocus(window, control, part);
858 trackresult = HandleControlClick(control, mouse, event->modifiers,
859 (ControlActionUPP)-1);
a460b361 860 } else {
861#if !TARGET_API_MAC_CARBON
862 if (mc->generic.type == MACCTRL_EDITBOX &&
863 control == mc->editbox.tbctrl) {
864 TEHandle te = (TEHandle)(*control)->contrlData;
865
866 macctrl_setfocus(mcs, mc);
867 TEClick(mouse, !!(event->modifiers & shiftKey), te);
868 goto done;
869 }
870#endif
1f1ec0e5 871 trackresult = TrackControl(control, mouse, (ControlActionUPP)-1);
a460b361 872 }
56c01970 873 switch (mc->generic.type) {
56c01970 874 case MACCTRL_RADIO:
1f1ec0e5 875 if (trackresult != 0) {
56c01970 876 for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++)
8a7e67ec 877 if (mc->radio.tbctrls[i] == control)
878 SetControlValue(mc->radio.tbctrls[i],
879 kControlRadioButtonCheckedValue);
880 else
881 SetControlValue(mc->radio.tbctrls[i],
882 kControlRadioButtonUncheckedValue);
8a7e67ec 883 ctrlevent(mcs, mc, EVENT_VALCHANGE);
56c01970 884 }
885 break;
886 case MACCTRL_CHECKBOX:
1f1ec0e5 887 if (trackresult != 0) {
8a7e67ec 888 SetControlValue(control, !GetControlValue(control));
889 ctrlevent(mcs, mc, EVENT_VALCHANGE);
8a7e67ec 890 }
56c01970 891 break;
892 case MACCTRL_BUTTON:
1f1ec0e5 893 if (trackresult != 0)
56c01970 894 ctrlevent(mcs, mc, EVENT_ACTION);
895 break;
1f1ec0e5 896 case MACCTRL_POPUP:
897 ctrlevent(mcs, mc, EVENT_SELCHANGE);
898 break;
8a7e67ec 899 }
56c01970 900 }
a460b361 901 done:
8a7e67ec 902 SetPort(saveport);
903}
904
1f1ec0e5 905void macctrl_key(WindowPtr window, EventRecord *event)
906{
907 ControlRef control;
908 struct macctrls *mcs = mac_winctrls(window);
909 union macctrl *mc;
910
911 if (mac_gestalts.apprvers >= 0x100 &&
912 GetKeyboardFocus(window, &control) == noErr && control != NULL) {
913 HandleControlKey(control, (event->message & keyCodeMask) >> 8,
914 event->message & charCodeMask, event->modifiers);
915 mc = (union macctrl *)GetControlReference(control);
916 ctrlevent(mcs, mc, EVENT_VALCHANGE);
917 }
918}
919
8a7e67ec 920void macctrl_update(WindowPtr window)
921{
922#if TARGET_API_MAC_CARBON
923 RgnHandle visrgn;
924#endif
925 Rect rect;
926 GrafPtr saveport;
927
928 BeginUpdate(window);
929 GetPort(&saveport);
930 SetPort((GrafPtr)GetWindowPort(window));
931 if (mac_gestalts.apprvers >= 0x101) {
932#if TARGET_API_MAC_CARBON
933 GetPortBounds(GetWindowPort(window), &rect);
934#else
935 rect = window->portRect;
936#endif
937 InsetRect(&rect, -1, -1);
938 DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ?
939 kThemeStateActive : kThemeStateInactive);
940 }
941#if TARGET_API_MAC_CARBON
942 visrgn = NewRgn();
943 GetPortVisibleRegion(GetWindowPort(window), visrgn);
944 UpdateControls(window, visrgn);
945 DisposeRgn(visrgn);
946#else
947 UpdateControls(window, window->visRgn);
948#endif
949 SetPort(saveport);
950 EndUpdate(window);
951}
952
953#if TARGET_API_MAC_CARBON
954#define EnableItem EnableMenuItem
955#define DisableItem DisableMenuItem
956#endif
957void macctrl_adjustmenus(WindowPtr window)
958{
959 MenuHandle menu;
960
961 menu = GetMenuHandle(mFile);
962 DisableItem(menu, iSave); /* XXX enable if modified */
963 EnableItem(menu, iSaveAs);
964 EnableItem(menu, iDuplicate);
965
966 menu = GetMenuHandle(mEdit);
967 DisableItem(menu, 0);
968}
969
970void macctrl_close(WindowPtr window)
971{
972 struct macctrls *mcs = mac_winctrls(window);
973 union macctrl *mc;
974
56c01970 975 /*
976 * Mostly, we don't bother disposing of the Toolbox controls,
977 * since that will happen automatically when the window is
978 * disposed of. Popup menus are an exception, because we have to
979 * dispose of the menu ourselves, and doing that while the control
980 * still holds a reference to it seems rude.
981 */
8a7e67ec 982 while ((mc = index234(mcs->byctrl, 0)) != NULL) {
1f1ec0e5 983 if (mc->generic.privdata != NULL && mc->generic.freeprivdata)
984 sfree(mc->generic.privdata);
56c01970 985 switch (mc->generic.type) {
986 case MACCTRL_POPUP:
987 DisposeControl(mc->popup.tbctrl);
988 DeleteMenu(mc->popup.menuid);
989 DisposeMenu(mc->popup.menu);
990 break;
991 }
8a7e67ec 992 del234(mcs->byctrl, mc);
993 sfree(mc);
994 }
995
996 freetree234(mcs->byctrl);
997 mcs->byctrl = NULL;
ee10bc56 998 sfree(mcs->panels);
999 mcs->panels = NULL;
8a7e67ec 1000}
1001
1002void dlg_update_start(union control *ctrl, void *dlg)
1003{
1004
1005 /* No-op for now */
1006}
1007
1008void dlg_update_done(union control *ctrl, void *dlg)
1009{
1010
1011 /* No-op for now */
1012}
1013
1014void dlg_set_focus(union control *ctrl, void *dlg)
1015{
1016
1017 if (mac_gestalts.apprvers >= 0x100) {
1018 /* Use SetKeyboardFocus() */
1019 } else {
1020 /* Do our own mucking around */
1021 }
1022}
1023
0bd8d76d 1024union control *dlg_last_focused(union control *ctrl, void *dlg)
8a7e67ec 1025{
1026
1027 return NULL;
1028}
1029
1030void dlg_beep(void *dlg)
1031{
1032
1033 SysBeep(30);
1034}
1035
1036void dlg_error_msg(void *dlg, char *msg)
1037{
1038 Str255 pmsg;
1039
1040 c2pstrcpy(pmsg, msg);
1041 ParamText(pmsg, NULL, NULL, NULL);
1042 StopAlert(128, NULL);
1043}
1044
1045void dlg_end(void *dlg, int value)
1046{
1047
1048};
1049
1050void dlg_refresh(union control *ctrl, void *dlg)
1051{
1f1ec0e5 1052 struct macctrls *mcs = dlg;
1053 union macctrl *mc;
8a7e67ec 1054
1f1ec0e5 1055 if (ctrl == NULL)
1056 return; /* FIXME */
1057 mc = findbyctrl(mcs, ctrl);
1058 assert(mc != NULL);
1059 ctrlevent(mcs, mc, EVENT_REFRESH);
8a7e67ec 1060};
1061
1062void *dlg_get_privdata(union control *ctrl, void *dlg)
1063{
1f1ec0e5 1064 struct macctrls *mcs = dlg;
1065 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1066
1f1ec0e5 1067 assert(mc != NULL);
1068 return mc->generic.privdata;
8a7e67ec 1069}
1070
1071void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
1072{
1f1ec0e5 1073 struct macctrls *mcs = dlg;
1074 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1075
1f1ec0e5 1076 assert(mc != NULL);
1077 mc->generic.privdata = ptr;
1078 mc->generic.freeprivdata = FALSE;
8a7e67ec 1079}
1080
1081void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
1082{
1f1ec0e5 1083 struct macctrls *mcs = dlg;
1084 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1085
1f1ec0e5 1086 assert(mc != NULL);
1087 mc->generic.privdata = smalloc(size);
1088 mc->generic.freeprivdata = TRUE;
1089 return mc->generic.privdata;
8a7e67ec 1090}
1091
1092
1093/*
1094 * Radio Button control
1095 */
1096
1097void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
1098{
93ba25f8 1099 struct macctrls *mcs = dlg;
1100 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1101 int i;
1102
93ba25f8 1103 assert(mc != NULL);
8a7e67ec 1104 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1105 if (i == whichbutton)
1106 SetControlValue(mc->radio.tbctrls[i],
1107 kControlRadioButtonCheckedValue);
1108 else
1109 SetControlValue(mc->radio.tbctrls[i],
1110 kControlRadioButtonUncheckedValue);
1111 }
1112
1113};
1114
1115int dlg_radiobutton_get(union control *ctrl, void *dlg)
1116{
93ba25f8 1117 struct macctrls *mcs = dlg;
1118 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1119 int i;
1120
93ba25f8 1121 assert(mc != NULL);
8a7e67ec 1122 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1123 if (GetControlValue(mc->radio.tbctrls[i]) ==
1124 kControlRadioButtonCheckedValue)
1125 return i;
1126 }
1127 return -1;
1128};
1129
1130
1131/*
1132 * Check Box control
1133 */
1134
1135void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
1136{
93ba25f8 1137 struct macctrls *mcs = dlg;
1138 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1139
93ba25f8 1140 assert(mc != NULL);
8a7e67ec 1141 SetControlValue(mc->checkbox.tbctrl,
1142 checked ? kControlCheckBoxCheckedValue :
1143 kControlCheckBoxUncheckedValue);
1144}
1145
1146int dlg_checkbox_get(union control *ctrl, void *dlg)
1147{
93ba25f8 1148 struct macctrls *mcs = dlg;
1149 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1150
93ba25f8 1151 assert(mc != NULL);
8a7e67ec 1152 return GetControlValue(mc->checkbox.tbctrl);
1153}
1154
1155
1156/*
1157 * Edit Box control
1158 */
1159
1160void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
1161{
1f1ec0e5 1162 struct macctrls *mcs = dlg;
1163 union macctrl *mc = findbyctrl(mcs, ctrl);
1164 GrafPtr saveport;
8a7e67ec 1165
1f1ec0e5 1166 assert(mc != NULL);
1167 assert(mc->generic.type == MACCTRL_EDITBOX);
0e9ce565 1168 GetPort(&saveport);
1169 SetPort((GrafPtr)(GetWindowPort(mcs->window)));
d9158b5e 1170 if (mac_gestalts.apprvers >= 0x100)
1f1ec0e5 1171 SetControlData(mc->editbox.tbctrl, kControlEntireControl,
1172 ctrl->editbox.password ?
1173 kControlEditTextPasswordTag :
1174 kControlEditTextTextTag,
1175 strlen(text), text);
a460b361 1176#if !TARGET_API_MAC_CARBON
d9158b5e 1177 else
0e9ce565 1178 TESetText(text, strlen(text),
1179 (TEHandle)(*mc->editbox.tbctrl)->contrlData);
a460b361 1180#endif
d9158b5e 1181 DrawOneControl(mc->editbox.tbctrl);
0e9ce565 1182 SetPort(saveport);
1183}
8a7e67ec 1184
1185void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
1186{
1f1ec0e5 1187 struct macctrls *mcs = dlg;
1188 union macctrl *mc = findbyctrl(mcs, ctrl);
1189 Size olen;
8a7e67ec 1190
1f1ec0e5 1191 assert(mc != NULL);
1192 assert(mc->generic.type == MACCTRL_EDITBOX);
1193 if (mac_gestalts.apprvers >= 0x100) {
1194 if (GetControlData(mc->editbox.tbctrl, kControlEntireControl,
1195 ctrl->editbox.password ?
1196 kControlEditTextPasswordTag :
1197 kControlEditTextTextTag,
1198 length - 1, buffer, &olen) != noErr)
1199 olen = 0;
1200 if (olen > length - 1)
d9158b5e 1201 olen = length - 1;
a460b361 1202 }
1203#if !TARGET_API_MAC_CARBON
1204 else {
1205 TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData;
1206
d9158b5e 1207 olen = (*te)->teLength;
1208 if (olen > length - 1)
1209 olen = length - 1;
1210 memcpy(buffer, *(*te)->hText, olen);
1211 }
a460b361 1212#endif
d9158b5e 1213 buffer[olen] = '\0';
1f1ec0e5 1214 fprintf(stderr, "dlg_editbox_get: %s\n", buffer);
d9158b5e 1215}
8a7e67ec 1216
1217
1218/*
1219 * List Box control
1220 */
1221
56c01970 1222static void dlg_macpopup_clear(union control *ctrl, void *dlg)
1223{
1224 struct macctrls *mcs = dlg;
1225 union macctrl *mc = findbyctrl(mcs, ctrl);
1226 MenuRef menu = mc->popup.menu;
1227 unsigned int i, n;
1228
1229 fprintf(stderr, " popup_clear\n");
fd703c0a 1230 n = CountMenuItems(menu);
56c01970 1231 for (i = 0; i < n; i++)
1232 DeleteMenuItem(menu, n - i);
1233 mc->popup.nids = 0;
1234 sfree(mc->popup.ids);
1235 mc->popup.ids = NULL;
fd703c0a 1236 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
56c01970 1237}
1238
8a7e67ec 1239void dlg_listbox_clear(union control *ctrl, void *dlg)
1240{
1241
56c01970 1242 if (ctrl->listbox.height == 0)
1243 dlg_macpopup_clear(ctrl, dlg);
1244}
1245
1246static void dlg_macpopup_del(union control *ctrl, void *dlg, int index)
1247{
1248 struct macctrls *mcs = dlg;
1249 union macctrl *mc = findbyctrl(mcs, ctrl);
1250 MenuRef menu = mc->popup.menu;
1251
1252 fprintf(stderr, " popup_del %d\n", index);
1253 DeleteMenuItem(menu, index + 1);
1254 if (mc->popup.ids != NULL)
1255 memcpy(mc->popup.ids + index, mc->popup.ids + index + 1,
1256 (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids));
fd703c0a 1257 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
56c01970 1258}
8a7e67ec 1259
1260void dlg_listbox_del(union control *ctrl, void *dlg, int index)
1261{
1262
56c01970 1263 if (ctrl->listbox.height == 0)
1264 dlg_macpopup_del(ctrl, dlg, index);
1265}
1266
1267static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text)
1268{
1269 struct macctrls *mcs = dlg;
1270 union macctrl *mc = findbyctrl(mcs, ctrl);
1271 MenuRef menu = mc->popup.menu;
1272 Str255 itemstring;
1273
1274 fprintf(stderr, " popup_add %s\n", text);
1275 assert(text[0] != '\0');
1276 c2pstrcpy(itemstring, text);
1277 AppendMenu(menu, "\pdummy");
fd703c0a 1278 SetMenuItemText(menu, CountMenuItems(menu), itemstring);
1279 SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu));
56c01970 1280}
8a7e67ec 1281
1282void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
1283{
1284
56c01970 1285 if (ctrl->listbox.height == 0)
1286 dlg_macpopup_add(ctrl, dlg, text);
1287}
1288
4eaff8d4 1289static void dlg_macpopup_addwithid(union control *ctrl, void *dlg,
1290 char const *text, int id)
56c01970 1291{
1292 struct macctrls *mcs = dlg;
1293 union macctrl *mc = findbyctrl(mcs, ctrl);
1294 MenuRef menu = mc->popup.menu;
1295 unsigned int index;
1296
1297 fprintf(stderr, " popup_addwthindex %s, %d\n", text, id);
1298 dlg_macpopup_add(ctrl, dlg, text);
fd703c0a 1299 index = CountMenuItems(menu) - 1;
56c01970 1300 if (mc->popup.nids <= index) {
1301 mc->popup.nids = index + 1;
1302 mc->popup.ids = srealloc(mc->popup.ids,
1303 mc->popup.nids * sizeof(*mc->popup.ids));
1304 }
1305 mc->popup.ids[index] = id;
1306}
8a7e67ec 1307
4eaff8d4 1308void dlg_listbox_addwithid(union control *ctrl, void *dlg,
1309 char const *text, int id)
8a7e67ec 1310{
1311
56c01970 1312 if (ctrl->listbox.height == 0)
4eaff8d4 1313 dlg_macpopup_addwithid(ctrl, dlg, text, id);
56c01970 1314}
8a7e67ec 1315
1316int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
1317{
56c01970 1318 struct macctrls *mcs = dlg;
1319 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1320
56c01970 1321 if (ctrl->listbox.height == 0) {
1322 assert(mc->popup.ids != NULL && mc->popup.nids > index);
1323 return mc->popup.ids[index];
1324 }
8a7e67ec 1325 return 0;
56c01970 1326}
8a7e67ec 1327
1328int dlg_listbox_index(union control *ctrl, void *dlg)
1329{
56c01970 1330 struct macctrls *mcs = dlg;
1331 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1332
56c01970 1333 if (ctrl->listbox.height == 0)
1334 return GetControlValue(mc->popup.tbctrl) - 1;
8a7e67ec 1335 return 0;
1336};
1337
1338int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
1339{
56c01970 1340 struct macctrls *mcs = dlg;
1341 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1342
56c01970 1343 if (ctrl->listbox.height == 0)
1344 return GetControlValue(mc->popup.tbctrl) - 1 == index;
8a7e67ec 1345 return 0;
1346};
1347
1348void dlg_listbox_select(union control *ctrl, void *dlg, int index)
1349{
56c01970 1350 struct macctrls *mcs = dlg;
1351 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1352
56c01970 1353 if (ctrl->listbox.height == 0)
1354 SetControlValue(mc->popup.tbctrl, index + 1);
8a7e67ec 1355};
1356
1357
1358/*
1359 * Text control
1360 */
1361
1362void dlg_text_set(union control *ctrl, void *dlg, char const *text)
1363{
93ba25f8 1364 struct macctrls *mcs = dlg;
1365 union macctrl *mc = findbyctrl(mcs, ctrl);
8a7e67ec 1366
93ba25f8 1367 assert(mc != NULL);
8a7e67ec 1368 if (mac_gestalts.apprvers >= 0x100)
1369 SetControlData(mc->text.tbctrl, kControlEntireControl,
1f1ec0e5 1370 kControlStaticTextTextTag, strlen(text), text);
f28d33e8 1371#if !TARGET_API_MAC_CARBON
1372 else
1373 TESetText(text, strlen(text),
1374 (TEHandle)(*mc->text.tbctrl)->contrlData);
1375#endif
8a7e67ec 1376}
1377
1378
1379/*
1380 * File Selector control
1381 */
1382
1383void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
1384{
1385
1386}
1387
1388void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
1389{
1390
1391}
1392
1393
1394/*
1395 * Font Selector control
1396 */
1397
1398void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn)
1399{
1400
1401}
1402
1403void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn)
1404{
1405
1406}
1407
1408
1409/*
1410 * Printer enumeration
1411 */
1412
1413printer_enum *printer_start_enum(int *nprinters)
1414{
1415
1416 *nprinters = 0;
1417 return NULL;
1418}
1419
1420char *printer_get_name(printer_enum *pe, int thing)
1421{
1422
1423 return "<none>";
1424}
1425
1426void printer_finish_enum(printer_enum *pe)
1427{
1428
1429}
1430
1431
1432/*
1433 * Colour selection stuff
1434 */
1435
1436void dlg_coloursel_start(union control *ctrl, void *dlg,
1437 int r, int g, int b)
1438{
1439
1440}
1441
1442int dlg_coloursel_results(union control *ctrl, void *dlg,
1443 int *r, int *g, int *b)
1444{
1445
1446 return 0;
1447}
1448
1449/*
1450 * Local Variables:
1451 * c-file-style: "simon"
1452 * End:
1453 */