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