1 /* $Id: macctrls.c,v 1.36 2003/04/14 22:55:12 ben Exp $ */
3 * Copyright (c) 2003 Ben Harris
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
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
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
29 #include <Appearance.h>
30 #include <ColorPicker.h>
32 #include <ControlDefinitions.h>
36 #include <Resources.h>
40 #include <TextUtils.h>
41 #include <ToolUtils.h>
53 /* Range of menu IDs for popup menus */
59 struct macctrl_generic
{
69 /* Template from which this was generated */
71 /* Next control in this panel */
77 struct macctrl_generic generic
;
81 struct macctrl_generic generic
;
86 struct macctrl_generic generic
;
91 struct macctrl_generic generic
;
95 struct macctrl_generic generic
;
100 struct macctrl_generic generic
;
107 struct macctrl_generic generic
;
116 struct mac_layoutstate
{
119 unsigned int panelnum
;
122 #define ctrlevent(mcs, mc, event) do { \
123 if ((mc)->generic.ctrl->generic.handler != NULL) \
124 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
125 (mcs)->data, (event)); \
128 #define findbyctrl(mcs, ctrl) \
129 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
131 static void macctrl_layoutset(struct mac_layoutstate
*, struct controlset
*,
132 WindowPtr
, struct macctrls
*);
133 static void macctrl_switchtopanel(struct macctrls
*, unsigned int);
134 static void macctrl_setfocus(struct macctrls
*, union macctrl
*);
135 static void macctrl_text(struct macctrls
*, WindowPtr
,
136 struct mac_layoutstate
*, union control
*);
137 static void macctrl_editbox(struct macctrls
*, WindowPtr
,
138 struct mac_layoutstate
*, union control
*);
139 static void macctrl_radio(struct macctrls
*, WindowPtr
,
140 struct mac_layoutstate
*, union control
*);
141 static void macctrl_checkbox(struct macctrls
*, WindowPtr
,
142 struct mac_layoutstate
*, union control
*);
143 static void macctrl_button(struct macctrls
*, WindowPtr
,
144 struct mac_layoutstate
*, union control
*);
145 static void macctrl_listbox(struct macctrls
*, WindowPtr
,
146 struct mac_layoutstate
*, union control
*);
147 static void macctrl_popup(struct macctrls
*, WindowPtr
,
148 struct mac_layoutstate
*, union control
*);
149 #if !TARGET_API_MAC_CARBON
150 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16
, ControlRef
,
151 ControlDefProcMessage
, SInt32
);
152 static pascal SInt32
macctrl_sys7_default_cdef(SInt16
, ControlRef
,
153 ControlDefProcMessage
, SInt32
);
154 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16
, ControlRef
,
155 ControlDefProcMessage
, SInt32
);
158 #if !TARGET_API_MAC_CARBON
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>.
165 #pragma options align=mac68k
167 short jmpabs
; /* 4EF9 */
168 ControlDefUPP theUPP
;
170 #pragma options align=reset
173 static void macctrl_init()
175 #if !TARGET_API_MAC_CARBON
176 static int inited
= 0;
180 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_EditBox
);
181 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_editbox_cdef
);
182 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_Default
);
183 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_default_cdef
);
184 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_ListBox
);
185 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_listbox_cdef
);
191 static int macctrl_cmp_byctrl(void *av
, void *bv
)
193 union macctrl
*a
= (union macctrl
*)av
;
194 union macctrl
*b
= (union macctrl
*)bv
;
196 if (a
->generic
.ctrl
< b
->generic
.ctrl
)
198 else if (a
->generic
.ctrl
> b
->generic
.ctrl
)
204 static int macctrl_cmp_byctrl_find(void *av
, void *bv
)
206 union control
*a
= (union control
*)av
;
207 union macctrl
*b
= (union macctrl
*)bv
;
209 if (a
< b
->generic
.ctrl
)
211 else if (a
> b
->generic
.ctrl
)
217 static union control panellist
;
219 static void panellist_handler(union control
*ctrl
, void *dlg
, void *data
,
222 struct macctrls
*mcs
= dlg
;
224 if (event
== EVENT_SELCHANGE
)
225 macctrl_switchtopanel(mcs
, dlg_listbox_index(ctrl
, dlg
) + 1);
228 void macctrl_layoutbox(struct controlbox
*cb
, WindowPtr window
,
229 struct macctrls
*mcs
)
232 struct mac_layoutstate curstate
;
237 if (mac_gestalts
.apprvers
>= 0x100)
238 CreateRootControl(window
, &root
);
239 #if TARGET_API_MAC_CARBON
240 GetPortBounds(GetWindowPort(window
), &rect
);
242 rect
= window
->portRect
;
244 mcs
->window
= window
;
245 mcs
->byctrl
= newtree234(macctrl_cmp_byctrl
);
247 mcs
->defbutton
= NULL
;
248 mcs
->canbutton
= NULL
;
249 /* Count the number of panels */
251 for (i
= 1; i
< cb
->nctrlsets
; i
++)
252 if (strcmp(cb
->ctrlsets
[i
]->pathname
, cb
->ctrlsets
[i
-1]->pathname
))
254 mcs
->panels
= snewn(mcs
->npanels
, union macctrl
*);
255 memset(mcs
->panels
, 0, sizeof(*mcs
->panels
) * mcs
->npanels
);
256 curstate
.panelnum
= 0;
258 curstate
.pos
.h
= rect
.left
+ 13;
259 curstate
.pos
.v
= rect
.top
+ 13;
260 curstate
.width
= 160;
261 panellist
.listbox
.type
= CTRL_LISTBOX
;
262 panellist
.listbox
.handler
= &panellist_handler
;
263 panellist
.listbox
.height
= 15;
264 panellist
.listbox
.percentwidth
= 100;
265 macctrl_listbox(mcs
, window
, &curstate
, &panellist
);
267 curstate
.pos
.h
= rect
.left
+ 13 + 160 + 13;
268 curstate
.pos
.v
= rect
.bottom
- 33;
269 curstate
.width
= rect
.right
- (rect
.left
+ 13 + 160) - (13 * 2);
270 for (i
= 0; i
< cb
->nctrlsets
; i
++) {
271 if (i
> 0 && strcmp(cb
->ctrlsets
[i
]->pathname
,
272 cb
->ctrlsets
[i
-1]->pathname
)) {
273 curstate
.pos
.v
= rect
.top
+ 13;
275 assert(curstate
.panelnum
< mcs
->npanels
);
276 dlg_listbox_add(&panellist
, mcs
, cb
->ctrlsets
[i
]->pathname
);
278 macctrl_layoutset(&curstate
, cb
->ctrlsets
[i
], window
, mcs
);
280 macctrl_switchtopanel(mcs
, 1);
281 /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
286 static void macctrl_layoutset(struct mac_layoutstate
*curstate
,
287 struct controlset
*s
,
288 WindowPtr window
, struct macctrls
*mcs
)
290 unsigned int i
, j
, ncols
, colstart
;
291 struct mac_layoutstate cols
[MAXCOLS
];
293 fprintf(stderr
, "--- begin set ---\n");
294 fprintf(stderr
, "pathname = %s\n", s
->pathname
);
295 if (s
->boxname
&& *s
->boxname
)
296 fprintf(stderr
, "boxname = %s\n", s
->boxname
);
298 fprintf(stderr
, "boxtitle = %s\n", s
->boxtitle
);
303 for (i
= 0; i
< s
->ncontrols
; i
++) {
304 union control
*ctrl
= s
->ctrls
[i
];
307 colstart
= COLUMN_START(ctrl
->generic
.column
);
308 switch (ctrl
->generic
.type
) {
309 case CTRL_TEXT
: s
= "text"; break;
310 case CTRL_EDITBOX
: s
= "editbox"; break;
311 case CTRL_RADIO
: s
= "radio"; break;
312 case CTRL_CHECKBOX
: s
= "checkbox"; break;
313 case CTRL_BUTTON
: s
= "button"; break;
314 case CTRL_LISTBOX
: s
= "listbox"; break;
315 case CTRL_COLUMNS
: s
= "columns"; break;
316 case CTRL_FILESELECT
: s
= "fileselect"; break;
317 case CTRL_FONTSELECT
: s
= "fontselect"; break;
318 case CTRL_TABDELAY
: s
= "tabdelay"; break;
319 default: s
= "unknown"; break;
321 fprintf(stderr
, " control: %s\n", s
);
322 switch (ctrl
->generic
.type
) {
324 if (ctrl
->columns
.ncols
!= 1) {
325 ncols
= ctrl
->columns
.ncols
;
326 fprintf(stderr
, " split to %d\n", ncols
);
327 assert(ncols
<= MAXCOLS
);
328 for (j
= 0; j
< ncols
; j
++) {
331 cols
[j
].pos
.h
= cols
[j
-1].pos
.h
+ cols
[j
-1].width
+ 6;
333 cols
[j
].width
= curstate
->width
-
334 (cols
[j
].pos
.h
- curstate
->pos
.h
);
336 cols
[j
].width
= (curstate
->width
+ 6) *
337 ctrl
->columns
.percentages
[j
] / 100 - 6;
340 fprintf(stderr
, " join\n");
341 for (j
= 0; j
< ncols
; j
++)
342 if (cols
[j
].pos
.v
> cols
[0].pos
.v
)
343 cols
[0].pos
.v
= cols
[j
].pos
.v
;
344 cols
[0].width
= curstate
->width
;
349 macctrl_text(mcs
, window
, &cols
[colstart
], ctrl
);
352 macctrl_editbox(mcs
, window
, &cols
[colstart
], ctrl
);
355 macctrl_radio(mcs
, window
, &cols
[colstart
], ctrl
);
358 macctrl_checkbox(mcs
, window
, &cols
[colstart
], ctrl
);
361 macctrl_button(mcs
, window
, &cols
[colstart
], ctrl
);
364 if (ctrl
->listbox
.height
== 0)
365 macctrl_popup(mcs
, window
, &cols
[colstart
], ctrl
);
367 macctrl_listbox(mcs
, window
, &cols
[colstart
], ctrl
);
371 for (j
= 0; j
< ncols
; j
++)
372 if (cols
[j
].pos
.v
> curstate
->pos
.v
)
373 curstate
->pos
.v
= cols
[j
].pos
.v
;
376 static void macctrl_switchtopanel(struct macctrls
*mcs
, unsigned int which
)
381 #define hideshow(c) do { \
382 if (i == which) ShowControl(c); else HideControl(c); \
385 mcs
->curpanel
= which
;
386 /* Panel 0 is special and always visible. */
387 for (i
= 1; i
< mcs
->npanels
; i
++)
388 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
389 #if !TARGET_API_MAC_CARBON
390 if (mcs
->focus
== mc
)
391 macctrl_setfocus(mcs
, NULL
);
393 switch (mc
->generic
.type
) {
395 hideshow(mc
->text
.tbctrl
);
397 case MACCTRL_EDITBOX
:
398 hideshow(mc
->editbox
.tbctrl
);
399 if (mc
->editbox
.tblabel
!= NULL
)
400 hideshow(mc
->editbox
.tblabel
);
403 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
404 hideshow(mc
->radio
.tbctrls
[j
]);
405 if (mc
->radio
.tblabel
!= NULL
)
406 hideshow(mc
->radio
.tblabel
);
408 case MACCTRL_CHECKBOX
:
409 hideshow(mc
->checkbox
.tbctrl
);
412 hideshow(mc
->button
.tbctrl
);
414 case MACCTRL_LISTBOX
:
415 hideshow(mc
->listbox
.tbctrl
);
417 * At least under Mac OS 8.1, hiding a list box
418 * doesn't hide its scroll bars.
420 #if TARGET_API_MAC_CARBON
421 hideshow(GetListVerticalScrollBar(mc
->listbox
.list
));
423 hideshow((*mc
->listbox
.list
)->vScroll
);
427 hideshow(mc
->popup
.tbctrl
);
433 #if !TARGET_API_MAC_CARBON
435 * System 7 focus manipulation
437 static void macctrl_defocus(union macctrl
*mc
)
440 assert(mac_gestalts
.apprvers
< 0x100);
441 switch (mc
->generic
.type
) {
442 case MACCTRL_EDITBOX
:
443 TEDeactivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
448 static void macctrl_enfocus(union macctrl
*mc
)
451 assert(mac_gestalts
.apprvers
< 0x100);
452 switch (mc
->generic
.type
) {
453 case MACCTRL_EDITBOX
:
454 TEActivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
459 static void macctrl_setfocus(struct macctrls
*mcs
, union macctrl
*mc
)
462 if (mcs
->focus
== mc
)
464 if (mcs
->focus
!= NULL
)
465 macctrl_defocus(mcs
->focus
);
472 static void macctrl_text(struct macctrls
*mcs
, WindowPtr window
,
473 struct mac_layoutstate
*curstate
,
476 union macctrl
*mc
= snew(union macctrl
);
480 assert(ctrl
->text
.label
!= NULL
);
481 fprintf(stderr
, " label = %s\n", ctrl
->text
.label
);
482 mc
->generic
.type
= MACCTRL_TEXT
;
483 mc
->generic
.ctrl
= ctrl
;
484 mc
->generic
.privdata
= NULL
;
485 bounds
.left
= curstate
->pos
.h
;
486 bounds
.right
= bounds
.left
+ curstate
->width
;
487 bounds
.top
= curstate
->pos
.v
;
488 bounds
.bottom
= bounds
.top
+ 16;
489 if (mac_gestalts
.apprvers
>= 0x100) {
492 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
493 kControlStaticTextProc
, (long)mc
);
494 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
495 kControlStaticTextTextTag
,
496 strlen(ctrl
->text
.label
), ctrl
->text
.label
);
497 GetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
498 kControlStaticTextTextHeightTag
,
499 sizeof(height
), &height
, &olen
);
501 #if !TARGET_API_MAC_CARBON
505 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
506 SYS7_TEXT_PROC
, (long)mc
);
507 te
= (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
;
508 TESetText(ctrl
->text
.label
, strlen(ctrl
->text
.label
), te
);
509 height
= TEGetHeight(1, (*te
)->nLines
, te
);
512 fprintf(stderr
, " height = %d\n", height
);
513 SizeControl(mc
->text
.tbctrl
, curstate
->width
, height
);
514 curstate
->pos
.v
+= height
+ 6;
515 add234(mcs
->byctrl
, mc
);
516 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
517 mcs
->panels
[curstate
->panelnum
] = mc
;
520 static void macctrl_editbox(struct macctrls
*mcs
, WindowPtr window
,
521 struct mac_layoutstate
*curstate
,
524 union macctrl
*mc
= snew(union macctrl
);
525 Rect lbounds
, bounds
;
527 if (ctrl
->editbox
.label
!= NULL
)
528 fprintf(stderr
, " label = %s\n", ctrl
->editbox
.label
);
529 fprintf(stderr
, " percentwidth = %d\n", ctrl
->editbox
.percentwidth
);
530 if (ctrl
->editbox
.password
) fprintf(stderr
, " password\n");
531 if (ctrl
->editbox
.has_list
) fprintf(stderr
, " has list\n");
532 mc
->generic
.type
= MACCTRL_EDITBOX
;
533 mc
->generic
.ctrl
= ctrl
;
534 mc
->generic
.privdata
= NULL
;
535 lbounds
.left
= curstate
->pos
.h
;
536 lbounds
.top
= curstate
->pos
.v
;
537 if (ctrl
->editbox
.percentwidth
== 100) {
538 if (ctrl
->editbox
.label
!= NULL
) {
539 lbounds
.right
= lbounds
.left
+ curstate
->width
;
540 lbounds
.bottom
= lbounds
.top
+ 16;
541 curstate
->pos
.v
+= 18;
543 bounds
.left
= curstate
->pos
.h
;
544 bounds
.right
= bounds
.left
+ curstate
->width
;
546 lbounds
.right
= lbounds
.left
+
547 curstate
->width
* (100 - ctrl
->editbox
.percentwidth
) / 100;
548 lbounds
.bottom
= lbounds
.top
+ 22;
549 bounds
.left
= lbounds
.right
;
550 bounds
.right
= lbounds
.left
+ curstate
->width
;
552 bounds
.top
= curstate
->pos
.v
;
553 bounds
.bottom
= bounds
.top
+ 22;
554 if (mac_gestalts
.apprvers
>= 0x100) {
555 if (ctrl
->editbox
.label
== NULL
)
556 mc
->editbox
.tblabel
= NULL
;
558 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
559 0, 0, 0, kControlStaticTextProc
,
561 SetControlData(mc
->editbox
.tblabel
, kControlEntireControl
,
562 kControlStaticTextTextTag
,
563 strlen(ctrl
->editbox
.label
), ctrl
->editbox
.label
);
565 InsetRect(&bounds
, 3, 3);
566 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
567 ctrl
->editbox
.password ?
568 kControlEditTextPasswordProc
:
569 kControlEditTextProc
, (long)mc
);
571 #if !TARGET_API_MAC_CARBON
573 if (ctrl
->editbox
.label
== NULL
)
574 mc
->editbox
.tblabel
= NULL
;
576 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
577 0, 0, 0, SYS7_TEXT_PROC
,
579 TESetText(ctrl
->editbox
.label
, strlen(ctrl
->editbox
.label
),
580 (TEHandle
)(*mc
->editbox
.tblabel
)->contrlData
);
582 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
583 SYS7_EDITBOX_PROC
, (long)mc
);
586 curstate
->pos
.v
+= 28;
587 add234(mcs
->byctrl
, mc
);
588 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
589 mcs
->panels
[curstate
->panelnum
] = mc
;
590 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
593 #if !TARGET_API_MAC_CARBON
594 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16 variant
,
596 ControlDefProcMessage msg
,
607 rect
= (*control
)->contrlRect
;
608 if (variant
== SYS7_EDITBOX_VARIANT
)
609 InsetRect(&rect
, 3, 3); /* 2 if it's 20 pixels high */
610 te
= TENew(&rect
, &rect
);
611 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
612 (*te
)->txSize
= LoWord(ssfs
);
613 (*te
)->txFont
= HiWord(ssfs
);
614 (*control
)->contrlData
= (Handle
)te
;
617 TEDispose((TEHandle
)(*control
)->contrlData
);
620 if ((*control
)->contrlVis
) {
621 rect
= (*control
)->contrlRect
;
622 if (variant
== SYS7_EDITBOX_VARIANT
) {
625 InsetRect(&rect
, 3, 3);
627 (*(TEHandle
)(*control
)->contrlData
)->viewRect
= rect
;
628 TEUpdate(&rect
, (TEHandle
)(*control
)->contrlData
);
632 if (variant
== SYS7_TEXT_VARIANT
)
633 return kControlNoPart
;
634 mouse
.h
= LoWord(param
);
635 mouse
.v
= HiWord(param
);
636 rect
= (*control
)->contrlRect
;
637 InsetRect(&rect
, 3, 3);
638 return PtInRect(mouse
, &rect
) ? kControlEditTextPart
: kControlNoPart
;
640 if (param
& (1 << 31)) {
646 rgn
= (RgnHandle
)param
;
647 RectRgn(rgn
, &(*control
)->contrlRect
);
651 rgn
= (RgnHandle
)param
;
660 static void macctrl_radio(struct macctrls
*mcs
, WindowPtr window
,
661 struct mac_layoutstate
*curstate
,
664 union macctrl
*mc
= snew(union macctrl
);
667 unsigned int i
, colwidth
;
669 if (ctrl
->radio
.label
!= NULL
)
670 fprintf(stderr
, " label = %s\n", ctrl
->radio
.label
);
671 mc
->generic
.type
= MACCTRL_RADIO
;
672 mc
->generic
.ctrl
= ctrl
;
673 mc
->generic
.privdata
= NULL
;
674 mc
->radio
.tbctrls
= snewn(ctrl
->radio
.nbuttons
, ControlRef
);
675 colwidth
= (curstate
->width
+ 13) / ctrl
->radio
.ncolumns
;
676 bounds
.top
= curstate
->pos
.v
;
677 bounds
.bottom
= bounds
.top
+ 16;
678 bounds
.left
= curstate
->pos
.h
;
679 bounds
.right
= bounds
.left
+ curstate
->width
;
680 if (ctrl
->radio
.label
== NULL
)
681 mc
->radio
.tblabel
= NULL
;
683 if (mac_gestalts
.apprvers
>= 0x100) {
684 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
685 0, 0, 0, kControlStaticTextProc
,
687 SetControlData(mc
->radio
.tblabel
, kControlEntireControl
,
688 kControlStaticTextTextTag
,
689 strlen(ctrl
->radio
.label
), ctrl
->radio
.label
);
691 #if !TARGET_API_MAC_CARBON
693 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
694 0, 0, 0, SYS7_TEXT_PROC
, (long)mc
);
695 TESetText(ctrl
->radio
.label
, strlen(ctrl
->radio
.label
),
696 (TEHandle
)(*mc
->radio
.tblabel
)->contrlData
);
699 curstate
->pos
.v
+= 18;
701 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
702 fprintf(stderr
, " button = %s\n", ctrl
->radio
.buttons
[i
]);
703 bounds
.top
= curstate
->pos
.v
- 2;
704 bounds
.bottom
= bounds
.top
+ 18;
705 bounds
.left
= curstate
->pos
.h
+ colwidth
* (i
% ctrl
->radio
.ncolumns
);
706 if (i
== ctrl
->radio
.nbuttons
- 1 ||
707 i
% ctrl
->radio
.ncolumns
== ctrl
->radio
.ncolumns
- 1) {
708 bounds
.right
= curstate
->pos
.h
+ curstate
->width
;
709 curstate
->pos
.v
+= 18;
711 bounds
.right
= bounds
.left
+ colwidth
- 13;
712 c2pstrcpy(title
, ctrl
->radio
.buttons
[i
]);
713 mc
->radio
.tbctrls
[i
] = NewControl(window
, &bounds
, title
, TRUE
,
714 0, 0, 1, radioButProc
, (long)mc
);
716 curstate
->pos
.v
+= 4;
717 add234(mcs
->byctrl
, mc
);
718 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
719 mcs
->panels
[curstate
->panelnum
] = mc
;
720 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
723 static void macctrl_checkbox(struct macctrls
*mcs
, WindowPtr window
,
724 struct mac_layoutstate
*curstate
,
727 union macctrl
*mc
= snew(union macctrl
);
731 assert(ctrl
->checkbox
.label
!= NULL
);
732 fprintf(stderr
, " label = %s\n", ctrl
->checkbox
.label
);
733 mc
->generic
.type
= MACCTRL_CHECKBOX
;
734 mc
->generic
.ctrl
= ctrl
;
735 mc
->generic
.privdata
= NULL
;
736 bounds
.left
= curstate
->pos
.h
;
737 bounds
.right
= bounds
.left
+ curstate
->width
;
738 bounds
.top
= curstate
->pos
.v
;
739 bounds
.bottom
= bounds
.top
+ 16;
740 c2pstrcpy(title
, ctrl
->checkbox
.label
);
741 mc
->checkbox
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
742 checkBoxProc
, (long)mc
);
743 add234(mcs
->byctrl
, mc
);
744 curstate
->pos
.v
+= 22;
745 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
746 mcs
->panels
[curstate
->panelnum
] = mc
;
747 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
750 static void macctrl_button(struct macctrls
*mcs
, WindowPtr window
,
751 struct mac_layoutstate
*curstate
,
754 union macctrl
*mc
= snew(union macctrl
);
758 assert(ctrl
->button
.label
!= NULL
);
759 fprintf(stderr
, " label = %s\n", ctrl
->button
.label
);
760 if (ctrl
->button
.isdefault
)
761 fprintf(stderr
, " is default\n");
762 mc
->generic
.type
= MACCTRL_BUTTON
;
763 mc
->generic
.ctrl
= ctrl
;
764 mc
->generic
.privdata
= NULL
;
765 bounds
.left
= curstate
->pos
.h
;
766 bounds
.right
= bounds
.left
+ curstate
->width
;
767 bounds
.top
= curstate
->pos
.v
;
768 bounds
.bottom
= bounds
.top
+ 20;
769 c2pstrcpy(title
, ctrl
->button
.label
);
770 mc
->button
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
771 pushButProc
, (long)mc
);
772 mc
->button
.tbring
= NULL
;
773 if (mac_gestalts
.apprvers
>= 0x100) {
774 Boolean isdefault
= ctrl
->button
.isdefault
;
776 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
777 kControlPushButtonDefaultTag
,
778 sizeof(isdefault
), &isdefault
);
779 } else if (ctrl
->button
.isdefault
) {
780 InsetRect(&bounds
, -4, -4);
781 mc
->button
.tbring
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
782 SYS7_DEFAULT_PROC
, (long)mc
);
784 if (mac_gestalts
.apprvers
>= 0x110) {
785 Boolean iscancel
= ctrl
->button
.iscancel
;
787 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
788 kControlPushButtonCancelTag
,
789 sizeof(iscancel
), &iscancel
);
791 if (ctrl
->button
.isdefault
)
793 if (ctrl
->button
.iscancel
)
795 add234(mcs
->byctrl
, mc
);
796 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
797 mcs
->panels
[curstate
->panelnum
] = mc
;
798 curstate
->pos
.v
+= 26;
801 #if !TARGET_API_MAC_CARBON
802 static pascal SInt32
macctrl_sys7_default_cdef(SInt16 variant
,
804 ControlDefProcMessage msg
,
814 if ((*control
)->contrlVis
) {
815 rect
= (*control
)->contrlRect
;
816 GetPenState(&savestate
);
819 if ((*control
)->contrlHilite
== kControlInactivePart
)
821 oval
= (rect
.bottom
- rect
.top
) / 2 + 2;
822 FrameRoundRect(&rect
, oval
, oval
);
823 SetPenState(&savestate
);
827 if (param
& (1 << 31)) {
833 rgn
= (RgnHandle
)param
;
834 RectRgn(rgn
, &(*control
)->contrlRect
);
838 rgn
= (RgnHandle
)param
;
847 static void macctrl_listbox(struct macctrls
*mcs
, WindowPtr window
,
848 struct mac_layoutstate
*curstate
,
851 union macctrl
*mc
= snew(union macctrl
);
855 if (ctrl
->listbox
.label
!= NULL
)
856 fprintf(stderr
, " label = %s\n", ctrl
->listbox
.label
);
857 fprintf(stderr
, " height = %d\n", ctrl
->listbox
.height
);
858 if (ctrl
->listbox
.draglist
)
859 fprintf(stderr
, " draglist\n");
860 if (ctrl
->listbox
.multisel
)
861 fprintf(stderr
, " multisel\n");
862 fprintf(stderr
, " ncols = %d\n", ctrl
->listbox
.ncols
);
863 assert(ctrl
->listbox
.percentwidth
== 100);
864 mc
->generic
.type
= MACCTRL_LISTBOX
;
865 mc
->generic
.ctrl
= ctrl
;
866 mc
->generic
.privdata
= NULL
;
867 /* The list starts off empty */
868 mc
->listbox
.nids
= 0;
869 mc
->listbox
.ids
= NULL
;
870 bounds
.left
= curstate
->pos
.h
;
871 bounds
.right
= bounds
.left
+ curstate
->width
;
872 bounds
.top
= curstate
->pos
.v
;
873 bounds
.bottom
= bounds
.top
+ 20 * ctrl
->listbox
.height
;
875 if (mac_gestalts
.apprvers
>= 0x100) {
876 InsetRect(&bounds
, 3, 3);
877 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
879 kControlListBoxProc
, (long)mc
);
880 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
881 kControlListBoxListHandleTag
,
882 sizeof(mc
->listbox
.list
), &mc
->listbox
.list
,
884 DisposeControl(mc
->listbox
.tbctrl
);
889 #if !TARGET_API_MAC_CARBON
891 InsetRect(&bounds
, -3, -3);
892 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
894 SYS7_LISTBOX_PROC
, (long)mc
);
895 mc
->listbox
.list
= (ListHandle
)(*mc
->listbox
.tbctrl
)->contrlData
;
896 (*mc
->listbox
.list
)->refCon
= (long)mc
;
899 if (!ctrl
->listbox
.multisel
) {
900 #if TARGET_API_MAC_CARBON
901 SetListSelectionFlags(mc
->listbox
.list
, lOnlyOne
);
903 (*mc
->listbox
.list
)->selFlags
= lOnlyOne
;
906 add234(mcs
->byctrl
, mc
);
907 curstate
->pos
.v
+= 6 + 20 * ctrl
->listbox
.height
;
908 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
909 mcs
->panels
[curstate
->panelnum
] = mc
;
910 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
913 #if !TARGET_API_MAC_CARBON
914 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16 variant
,
916 ControlDefProcMessage msg
,
932 rect
= (*control
)->contrlRect
;
933 InsetRect(&rect
, 4, 4);
934 rect
.right
-= 15; /* scroll bar */
935 bounds
.top
= bounds
.bottom
= bounds
.left
= 0;
937 csize
.h
= csize
.v
= 0;
939 savefont
= curport
->txFont
;
940 savesize
= curport
->txSize
;
941 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
942 TextFont(HiWord(ssfs
));
943 TextSize(LoWord(ssfs
));
944 list
= LNew(&rect
, &bounds
, csize
, 0, (*control
)->contrlOwner
,
945 TRUE
, FALSE
, FALSE
, TRUE
);
946 SetControlReference((*list
)->vScroll
, (long)list
);
947 (*control
)->contrlData
= (Handle
)list
;
953 * If the dialogue box is being destroyed, the scroll bar
954 * might have gone already. In our situation, this is the
955 * only time we destroy a control, so NULL out the scroll bar
956 * handle to prevent LDispose trying to free it.
958 list
= (ListHandle
)(*control
)->contrlData
;
959 (*list
)->vScroll
= NULL
;
963 if ((*control
)->contrlVis
) {
964 rect
= (*control
)->contrlRect
;
965 /* XXX input focus highlighting? */
966 InsetRect(&rect
, 3, 3);
969 list
= (ListHandle
)(*control
)->contrlData
;
970 LActivate((*control
)->contrlHilite
!= kControlInactivePart
, list
);
972 LUpdate(curport
->visRgn
, list
);
976 mouse
.h
= LoWord(param
);
977 mouse
.v
= HiWord(param
);
978 rect
= (*control
)->contrlRect
;
979 InsetRect(&rect
, 4, 4);
981 * We deliberately exclude the scrollbar so that LClick() can see it.
984 return PtInRect(mouse
, &rect
) ? kControlListBoxPart
: kControlNoPart
;
986 if (param
& (1 << 31)) {
992 rgn
= (RgnHandle
)param
;
993 RectRgn(rgn
, &(*control
)->contrlRect
);
997 rgn
= (RgnHandle
)param
;
1006 static void macctrl_popup(struct macctrls
*mcs
, WindowPtr window
,
1007 struct mac_layoutstate
*curstate
,
1008 union control
*ctrl
)
1010 union macctrl
*mc
= snew(union macctrl
);
1013 unsigned int labelwidth
;
1014 static int nextmenuid
= MENU_MIN
;
1019 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
1020 * create a popup menu with dynamic content.
1022 assert(ctrl
->listbox
.height
== 0);
1023 assert(!ctrl
->listbox
.draglist
);
1024 assert(!ctrl
->listbox
.multisel
);
1026 if (ctrl
->listbox
.label
!= NULL
)
1027 fprintf(stderr
, " label = %s\n", ctrl
->listbox
.label
);
1028 fprintf(stderr
, " percentwidth = %d\n", ctrl
->listbox
.percentwidth
);
1030 mc
->generic
.type
= MACCTRL_POPUP
;
1031 mc
->generic
.ctrl
= ctrl
;
1032 mc
->generic
.privdata
= NULL
;
1033 c2pstrcpy(title
, ctrl
->button
.label
== NULL ?
"" : ctrl
->button
.label
);
1035 /* Find a spare menu ID and create the menu */
1036 while (GetMenuHandle(nextmenuid
) != NULL
)
1037 if (++nextmenuid
>= MENU_MAX
) nextmenuid
= MENU_MIN
;
1038 menuid
= nextmenuid
++;
1039 menu
= NewMenu(menuid
, "\pdummy");
1040 if (menu
== NULL
) return;
1041 mc
->popup
.menu
= menu
;
1042 mc
->popup
.menuid
= menuid
;
1043 InsertMenu(menu
, kInsertHierarchicalMenu
);
1045 /* The menu starts off empty */
1047 mc
->popup
.ids
= NULL
;
1049 bounds
.left
= curstate
->pos
.h
;
1050 bounds
.right
= bounds
.left
+ curstate
->width
;
1051 bounds
.top
= curstate
->pos
.v
;
1052 bounds
.bottom
= bounds
.top
+ 20;
1053 /* XXX handle percentwidth == 100 */
1054 labelwidth
= curstate
->width
* (100 - ctrl
->listbox
.percentwidth
) / 100;
1055 mc
->popup
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
,
1056 popupTitleLeftJust
, menuid
, labelwidth
,
1057 popupMenuProc
+ popupFixedWidth
, (long)mc
);
1058 add234(mcs
->byctrl
, mc
);
1059 curstate
->pos
.v
+= 26;
1060 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
1061 mcs
->panels
[curstate
->panelnum
] = mc
;
1062 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1066 void macctrl_activate(WindowPtr window
, EventRecord
*event
)
1068 struct macctrls
*mcs
= mac_winctrls(window
);
1069 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1072 ControlPartCode state
;
1076 SetPort((GrafPtr
)GetWindowPort(window
));
1077 if (mac_gestalts
.apprvers
>= 0x100)
1078 SetThemeWindowBackground(window
, active ?
1079 kThemeBrushModelessDialogBackgroundActive
:
1080 kThemeBrushModelessDialogBackgroundInactive
,
1082 state
= active ? kControlNoPart
: kControlInactivePart
;
1083 for (i
= 0; i
<= mcs
->curpanel
; i
+= mcs
->curpanel
)
1084 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
1085 switch (mc
->generic
.type
) {
1087 HiliteControl(mc
->text
.tbctrl
, state
);
1089 case MACCTRL_EDITBOX
:
1090 HiliteControl(mc
->editbox
.tbctrl
, state
);
1091 if (mc
->editbox
.tblabel
!= NULL
)
1092 HiliteControl(mc
->editbox
.tblabel
, state
);
1095 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
1096 HiliteControl(mc
->radio
.tbctrls
[j
], state
);
1097 if (mc
->radio
.tblabel
!= NULL
)
1098 HiliteControl(mc
->radio
.tblabel
, state
);
1100 case MACCTRL_CHECKBOX
:
1101 HiliteControl(mc
->checkbox
.tbctrl
, state
);
1103 case MACCTRL_BUTTON
:
1104 HiliteControl(mc
->button
.tbctrl
, state
);
1105 if (mc
->button
.tbring
!= NULL
)
1106 HiliteControl(mc
->button
.tbring
, state
);
1108 case MACCTRL_LISTBOX
:
1109 HiliteControl(mc
->listbox
.tbctrl
, state
);
1112 HiliteControl(mc
->popup
.tbctrl
, state
);
1115 #if !TARGET_API_MAC_CARBON
1116 if (mcs
->focus
== mc
) {
1118 macctrl_enfocus(mc
);
1120 macctrl_defocus(mc
);
1127 void macctrl_click(WindowPtr window
, EventRecord
*event
)
1130 ControlHandle control
, oldfocus
;
1131 int part
, trackresult
;
1134 struct macctrls
*mcs
= mac_winctrls(window
);
1139 SetPort((GrafPtr
)GetWindowPort(window
));
1140 mouse
= event
->where
;
1141 GlobalToLocal(&mouse
);
1142 part
= FindControl(mouse
, window
, &control
);
1143 if (control
!= NULL
) {
1144 #if !TARGET_API_MAC_CARBON
1146 * Special magic for scroll bars in list boxes, whose refcon
1149 if (part
== kControlUpButtonPart
|| part
== kControlDownButtonPart
||
1150 part
== kControlPageUpPart
|| part
== kControlPageDownPart
||
1151 part
== kControlIndicatorPart
)
1152 mc
= (union macctrl
*)
1153 (*(ListHandle
)GetControlReference(control
))->refCon
;
1156 mc
= (union macctrl
*)GetControlReference(control
);
1157 fprintf(stderr
, "control = %p, part = %d, mc = %p\n",
1159 if (mac_gestalts
.apprvers
>= 0x100) {
1160 if (GetControlFeatures(control
, &features
) == noErr
&&
1161 (features
& kControlSupportsFocus
) &&
1162 (features
& kControlGetsFocusOnClick
) &&
1163 GetKeyboardFocus(window
, &oldfocus
) == noErr
&&
1164 control
!= oldfocus
)
1165 SetKeyboardFocus(window
, control
, part
);
1166 trackresult
= HandleControlClick(control
, mouse
, event
->modifiers
,
1167 (ControlActionUPP
)-1);
1169 #if !TARGET_API_MAC_CARBON
1170 if (mc
->generic
.type
== MACCTRL_EDITBOX
&&
1171 control
== mc
->editbox
.tbctrl
) {
1172 TEHandle te
= (TEHandle
)(*control
)->contrlData
;
1174 macctrl_setfocus(mcs
, mc
);
1175 TEClick(mouse
, !!(event
->modifiers
& shiftKey
), te
);
1178 if (mc
->generic
.type
== MACCTRL_LISTBOX
&&
1179 (control
== mc
->listbox
.tbctrl
||
1180 control
== (*mc
->listbox
.list
)->vScroll
)) {
1182 fprintf(stderr
, "list = %p\n", mc
->listbox
.list
);
1183 macctrl_setfocus(mcs
, mc
);
1184 if (LClick(mouse
, event
->modifiers
, mc
->listbox
.list
))
1186 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1188 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1192 trackresult
= TrackControl(control
, mouse
, (ControlActionUPP
)-1);
1194 switch (mc
->generic
.type
) {
1196 if (trackresult
!= 0) {
1197 for (i
= 0; i
< mc
->generic
.ctrl
->radio
.nbuttons
; i
++)
1198 if (mc
->radio
.tbctrls
[i
] == control
)
1199 SetControlValue(mc
->radio
.tbctrls
[i
],
1200 kControlRadioButtonCheckedValue
);
1202 SetControlValue(mc
->radio
.tbctrls
[i
],
1203 kControlRadioButtonUncheckedValue
);
1204 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1207 case MACCTRL_CHECKBOX
:
1208 if (trackresult
!= 0) {
1209 SetControlValue(control
, !GetControlValue(control
));
1210 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1213 case MACCTRL_BUTTON
:
1214 if (trackresult
!= 0)
1215 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1217 case MACCTRL_LISTBOX
:
1218 /* FIXME spot double-click */
1219 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1222 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1230 void macctrl_key(WindowPtr window
, EventRecord
*event
)
1233 struct macctrls
*mcs
= mac_winctrls(window
);
1235 unsigned long dummy
;
1237 switch (event
->message
& charCodeMask
) {
1238 case kEnterCharCode
:
1239 case kReturnCharCode
:
1240 if (mcs
->defbutton
!= NULL
) {
1241 assert(mcs
->defbutton
->generic
.type
== MACCTRL_BUTTON
);
1242 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlButtonPart
);
1244 * I'd like to delay unhilighting the button until after
1245 * the event has been processed, but by them the entire
1246 * dialgue box might have been destroyed.
1249 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1250 ctrlevent(mcs
, mcs
->defbutton
, EVENT_ACTION
);
1253 case kEscapeCharCode
:
1254 if (mcs
->canbutton
!= NULL
) {
1255 assert(mcs
->canbutton
->generic
.type
== MACCTRL_BUTTON
);
1256 HiliteControl(mcs
->canbutton
->button
.tbctrl
, kControlButtonPart
);
1258 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1259 ctrlevent(mcs
, mcs
->canbutton
, EVENT_ACTION
);
1263 if (mac_gestalts
.apprvers
>= 0x100) {
1264 if (GetKeyboardFocus(window
, &control
) == noErr
&& control
!= NULL
) {
1265 HandleControlKey(control
, (event
->message
& keyCodeMask
) >> 8,
1266 event
->message
& charCodeMask
, event
->modifiers
);
1267 mc
= (union macctrl
*)GetControlReference(control
);
1268 switch (mc
->generic
.type
) {
1269 case MACCTRL_LISTBOX
:
1270 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1273 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1278 #if !TARGET_API_MAC_CARBON
1282 if (mcs
->focus
!= NULL
) {
1284 switch (mc
->generic
.type
) {
1285 case MACCTRL_EDITBOX
:
1286 te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1287 TEKey(event
->message
& charCodeMask
, te
);
1288 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1296 void macctrl_update(WindowPtr window
)
1298 #if TARGET_API_MAC_CARBON
1304 BeginUpdate(window
);
1306 SetPort((GrafPtr
)GetWindowPort(window
));
1307 if (mac_gestalts
.apprvers
>= 0x101) {
1308 #if TARGET_API_MAC_CARBON
1309 GetPortBounds(GetWindowPort(window
), &rect
);
1311 rect
= window
->portRect
;
1313 InsetRect(&rect
, -1, -1);
1314 DrawThemeModelessDialogFrame(&rect
, mac_frontwindow() == window ?
1315 kThemeStateActive
: kThemeStateInactive
);
1317 #if TARGET_API_MAC_CARBON
1319 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1320 UpdateControls(window
, visrgn
);
1323 UpdateControls(window
, window
->visRgn
);
1329 #if TARGET_API_MAC_CARBON
1330 #define EnableItem EnableMenuItem
1331 #define DisableItem DisableMenuItem
1333 void macctrl_adjustmenus(WindowPtr window
)
1337 menu
= GetMenuHandle(mFile
);
1338 DisableItem(menu
, iSave
); /* XXX enable if modified */
1339 EnableItem(menu
, iSaveAs
);
1340 EnableItem(menu
, iDuplicate
);
1342 menu
= GetMenuHandle(mEdit
);
1343 DisableItem(menu
, 0);
1346 void macctrl_close(WindowPtr window
)
1348 struct macctrls
*mcs
= mac_winctrls(window
);
1352 * Mostly, we don't bother disposing of the Toolbox controls,
1353 * since that will happen automatically when the window is
1354 * disposed of. Popup menus are an exception, because we have to
1355 * dispose of the menu ourselves, and doing that while the control
1356 * still holds a reference to it seems rude.
1358 while ((mc
= index234(mcs
->byctrl
, 0)) != NULL
) {
1359 if (mc
->generic
.privdata
!= NULL
&& mc
->generic
.freeprivdata
)
1360 sfree(mc
->generic
.privdata
);
1361 switch (mc
->generic
.type
) {
1363 DisposeControl(mc
->popup
.tbctrl
);
1364 DeleteMenu(mc
->popup
.menuid
);
1365 DisposeMenu(mc
->popup
.menu
);
1368 del234(mcs
->byctrl
, mc
);
1372 freetree234(mcs
->byctrl
);
1378 void dlg_update_start(union control
*ctrl
, void *dlg
)
1384 void dlg_update_done(union control
*ctrl
, void *dlg
)
1390 void dlg_set_focus(union control
*ctrl
, void *dlg
)
1393 if (mac_gestalts
.apprvers
>= 0x100) {
1394 /* Use SetKeyboardFocus() */
1396 /* Do our own mucking around */
1400 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1406 void dlg_beep(void *dlg
)
1412 void dlg_error_msg(void *dlg
, char *msg
)
1416 c2pstrcpy(pmsg
, msg
);
1417 ParamText(pmsg
, NULL
, NULL
, NULL
);
1418 StopAlert(128, NULL
);
1421 void dlg_end(void *dlg
, int value
)
1423 struct macctrls
*mcs
= dlg
;
1425 if (mcs
->end
!= NULL
)
1426 (*mcs
->end
)(mcs
->window
, value
);
1429 void dlg_refresh(union control
*ctrl
, void *dlg
)
1431 struct macctrls
*mcs
= dlg
;
1436 mc
= findbyctrl(mcs
, ctrl
);
1438 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1441 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
1443 struct macctrls
*mcs
= dlg
;
1444 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1447 return mc
->generic
.privdata
;
1450 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
1452 struct macctrls
*mcs
= dlg
;
1453 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1456 mc
->generic
.privdata
= ptr
;
1457 mc
->generic
.freeprivdata
= FALSE
;
1460 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
1462 struct macctrls
*mcs
= dlg
;
1463 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1466 mc
->generic
.privdata
= smalloc(size
);
1467 mc
->generic
.freeprivdata
= TRUE
;
1468 return mc
->generic
.privdata
;
1473 * Radio Button control
1476 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
1478 struct macctrls
*mcs
= dlg
;
1479 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1482 if (mc
== NULL
) return;
1483 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1484 if (i
== whichbutton
)
1485 SetControlValue(mc
->radio
.tbctrls
[i
],
1486 kControlRadioButtonCheckedValue
);
1488 SetControlValue(mc
->radio
.tbctrls
[i
],
1489 kControlRadioButtonUncheckedValue
);
1494 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
1496 struct macctrls
*mcs
= dlg
;
1497 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1501 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1502 if (GetControlValue(mc
->radio
.tbctrls
[i
]) ==
1503 kControlRadioButtonCheckedValue
)
1514 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
1516 struct macctrls
*mcs
= dlg
;
1517 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1519 if (mc
== NULL
) return;
1520 SetControlValue(mc
->checkbox
.tbctrl
,
1521 checked ? kControlCheckBoxCheckedValue
:
1522 kControlCheckBoxUncheckedValue
);
1525 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
1527 struct macctrls
*mcs
= dlg
;
1528 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1531 return GetControlValue(mc
->checkbox
.tbctrl
);
1539 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
1541 struct macctrls
*mcs
= dlg
;
1542 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1545 if (mc
== NULL
) return;
1546 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1548 SetPort((GrafPtr
)(GetWindowPort(mcs
->window
)));
1549 if (mac_gestalts
.apprvers
>= 0x100)
1550 SetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1551 ctrl
->editbox
.password ?
1552 kControlEditTextPasswordTag
:
1553 kControlEditTextTextTag
,
1554 strlen(text
), text
);
1555 #if !TARGET_API_MAC_CARBON
1557 TESetText(text
, strlen(text
),
1558 (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
1560 DrawOneControl(mc
->editbox
.tbctrl
);
1564 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
1566 struct macctrls
*mcs
= dlg
;
1567 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1571 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1572 if (mac_gestalts
.apprvers
>= 0x100) {
1573 if (GetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1574 ctrl
->editbox
.password ?
1575 kControlEditTextPasswordTag
:
1576 kControlEditTextTextTag
,
1577 length
- 1, buffer
, &olen
) != noErr
)
1579 if (olen
> length
- 1)
1582 #if !TARGET_API_MAC_CARBON
1584 TEHandle te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1586 olen
= (*te
)->teLength
;
1587 if (olen
> length
- 1)
1589 memcpy(buffer
, *(*te
)->hText
, olen
);
1592 buffer
[olen
] = '\0';
1593 fprintf(stderr
, "dlg_editbox_get: %s\n", buffer
);
1601 static void dlg_macpopup_clear(union control
*ctrl
, void *dlg
)
1603 struct macctrls
*mcs
= dlg
;
1604 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1605 MenuRef menu
= mc
->popup
.menu
;
1608 if (mc
== NULL
) return;
1609 fprintf(stderr
, " popup_clear\n");
1610 n
= CountMenuItems(menu
);
1611 for (i
= 0; i
< n
; i
++)
1612 DeleteMenuItem(menu
, n
- i
);
1614 sfree(mc
->popup
.ids
);
1615 mc
->popup
.ids
= NULL
;
1616 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1619 static void dlg_maclist_clear(union control
*ctrl
, void *dlg
)
1621 struct macctrls
*mcs
= dlg
;
1622 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1624 if (mc
== NULL
) return;
1625 fprintf(stderr
, " maclist_clear\n");
1626 LDelRow(0, 0, mc
->listbox
.list
);
1627 mc
->listbox
.nids
= 0;
1628 sfree(mc
->listbox
.ids
);
1629 mc
->listbox
.ids
= NULL
;
1630 DrawOneControl(mc
->listbox
.tbctrl
);
1633 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
1636 switch (ctrl
->generic
.type
) {
1638 if (ctrl
->listbox
.height
== 0)
1639 dlg_macpopup_clear(ctrl
, dlg
);
1641 dlg_maclist_clear(ctrl
, dlg
);
1646 static void dlg_macpopup_del(union control
*ctrl
, void *dlg
, int index
)
1648 struct macctrls
*mcs
= dlg
;
1649 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1650 MenuRef menu
= mc
->popup
.menu
;
1652 if (mc
== NULL
) return;
1653 fprintf(stderr
, " popup_del %d\n", index
);
1654 DeleteMenuItem(menu
, index
+ 1);
1655 if (mc
->popup
.ids
!= NULL
)
1656 memcpy(mc
->popup
.ids
+ index
, mc
->popup
.ids
+ index
+ 1,
1657 (mc
->popup
.nids
- index
- 1) * sizeof(*mc
->popup
.ids
));
1658 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1661 static void dlg_maclist_del(union control
*ctrl
, void *dlg
, int index
)
1663 struct macctrls
*mcs
= dlg
;
1664 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1666 if (mc
== NULL
) return;
1667 fprintf(stderr
, " maclist_del %d\n", index
);
1668 LDelRow(1, index
, mc
->listbox
.list
);
1669 if (mc
->listbox
.ids
!= NULL
)
1670 memcpy(mc
->listbox
.ids
+ index
, mc
->listbox
.ids
+ index
+ 1,
1671 (mc
->listbox
.nids
- index
- 1) * sizeof(*mc
->listbox
.ids
));
1672 DrawOneControl(mc
->listbox
.tbctrl
);
1675 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
1678 switch (ctrl
->generic
.type
) {
1680 if (ctrl
->listbox
.height
== 0)
1681 dlg_macpopup_del(ctrl
, dlg
, index
);
1683 dlg_maclist_del(ctrl
, dlg
, index
);
1688 static void dlg_macpopup_add(union control
*ctrl
, void *dlg
, char const *text
)
1690 struct macctrls
*mcs
= dlg
;
1691 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1692 MenuRef menu
= mc
->popup
.menu
;
1695 if (mc
== NULL
) return;
1696 fprintf(stderr
, " popup_add %s\n", text
);
1697 assert(text
[0] != '\0');
1698 c2pstrcpy(itemstring
, text
);
1699 AppendMenu(menu
, "\pdummy");
1700 SetMenuItemText(menu
, CountMenuItems(menu
), itemstring
);
1701 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1705 static void dlg_maclist_add(union control
*ctrl
, void *dlg
, char const *text
)
1707 struct macctrls
*mcs
= dlg
;
1708 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1710 Cell cell
= { 0, 0 };
1712 if (mc
== NULL
) return;
1713 fprintf(stderr
, " maclist_add %s\n", text
);
1714 #if TARGET_API_MAC_CARBON
1715 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1717 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1719 cell
.v
= bounds
.bottom
;
1720 LAddRow(1, cell
.v
, mc
->listbox
.list
);
1721 LSetCell(text
, strlen(text
), cell
, mc
->listbox
.list
);
1722 DrawOneControl(mc
->listbox
.tbctrl
);
1725 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
1728 switch (ctrl
->generic
.type
) {
1730 if (ctrl
->listbox
.height
== 0)
1731 dlg_macpopup_add(ctrl
, dlg
, text
);
1733 dlg_maclist_add(ctrl
, dlg
, text
);
1738 static void dlg_macpopup_addwithid(union control
*ctrl
, void *dlg
,
1739 char const *text
, int id
)
1741 struct macctrls
*mcs
= dlg
;
1742 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1743 MenuRef menu
= mc
->popup
.menu
;
1746 if (mc
== NULL
) return;
1747 fprintf(stderr
, " popup_addwthid %s, %d\n", text
, id
);
1748 dlg_macpopup_add(ctrl
, dlg
, text
);
1749 index
= CountMenuItems(menu
) - 1;
1750 if (mc
->popup
.nids
<= index
) {
1751 mc
->popup
.nids
= index
+ 1;
1752 mc
->popup
.ids
= sresize(mc
->popup
.ids
, mc
->popup
.nids
, int);
1754 mc
->popup
.ids
[index
] = id
;
1757 static void dlg_maclist_addwithid(union control
*ctrl
, void *dlg
,
1758 char const *text
, int id
)
1760 struct macctrls
*mcs
= dlg
;
1761 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1765 if (mc
== NULL
) return;
1766 fprintf(stderr
, " maclist_addwithid %s %d\n", text
, id
);
1767 dlg_maclist_add(ctrl
, dlg
, text
);
1768 #if TARGET_API_MAC_CARBON
1769 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1771 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1773 index
= bounds
.bottom
;
1774 if (mc
->listbox
.nids
<= index
) {
1775 mc
->listbox
.nids
= index
+ 1;
1776 mc
->listbox
.ids
= sresize(mc
->listbox
.ids
, mc
->listbox
.nids
, int);
1778 mc
->listbox
.ids
[index
] = id
;
1781 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
1782 char const *text
, int id
)
1785 switch (ctrl
->generic
.type
) {
1787 if (ctrl
->listbox
.height
== 0)
1788 dlg_macpopup_addwithid(ctrl
, dlg
, text
, id
);
1790 dlg_maclist_addwithid(ctrl
, dlg
, text
, id
);
1795 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
1797 struct macctrls
*mcs
= dlg
;
1798 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1801 switch (ctrl
->generic
.type
) {
1803 if (ctrl
->listbox
.height
== 0) {
1804 assert(mc
->popup
.ids
!= NULL
&& mc
->popup
.nids
> index
);
1805 return mc
->popup
.ids
[index
];
1807 assert(mc
->listbox
.ids
!= NULL
&& mc
->listbox
.nids
> index
);
1808 return mc
->listbox
.ids
[index
];
1814 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
1816 struct macctrls
*mcs
= dlg
;
1817 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1818 Cell cell
= { 0, 0 };
1821 switch (ctrl
->generic
.type
) {
1823 if (ctrl
->listbox
.height
== 0)
1824 return GetControlValue(mc
->popup
.tbctrl
) - 1;
1826 if (LGetSelect(TRUE
, &cell
, mc
->listbox
.list
))
1835 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
1837 struct macctrls
*mcs
= dlg
;
1838 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1839 Cell cell
= { 0, 0 };
1842 switch (ctrl
->generic
.type
) {
1844 if (ctrl
->listbox
.height
== 0)
1845 return GetControlValue(mc
->popup
.tbctrl
) - 1 == index
;
1848 return LGetSelect(FALSE
, &cell
, mc
->listbox
.list
);
1854 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
1856 struct macctrls
*mcs
= dlg
;
1857 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1859 if (mc
== NULL
) return;
1860 switch (ctrl
->generic
.type
) {
1862 if (ctrl
->listbox
.height
== 0)
1863 SetControlValue(mc
->popup
.tbctrl
, index
+ 1);
1873 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
1875 struct macctrls
*mcs
= dlg
;
1876 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1878 if (mc
== NULL
) return;
1879 if (mac_gestalts
.apprvers
>= 0x100)
1880 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
1881 kControlStaticTextTextTag
, strlen(text
), text
);
1882 #if !TARGET_API_MAC_CARBON
1884 TESetText(text
, strlen(text
),
1885 (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
);
1891 * File Selector control
1894 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
1899 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
1906 * Font Selector control
1909 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fn
)
1914 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fn
)
1921 * Printer enumeration
1924 printer_enum
*printer_start_enum(int *nprinters
)
1931 char *printer_get_name(printer_enum
*pe
, int thing
)
1937 void printer_finish_enum(printer_enum
*pe
)
1944 * Colour selection stuff
1947 void dlg_coloursel_start(union control
*ctrl
, void *dlg
,
1948 int r
, int g
, int b
)
1950 struct macctrls
*mcs
= dlg
;
1951 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1952 Point where
= {-1, -1}; /* Screen with greatest colour depth */
1955 if (HAVE_COLOR_QD()) {
1956 incolour
.red
= r
* 0x0101;
1957 incolour
.green
= g
* 0x0101;
1958 incolour
.blue
= b
* 0x0101;
1959 mcs
->gotcolour
= GetColor(where
, "\pModify Colour:", &incolour
,
1961 ctrlevent(mcs
, mc
, EVENT_CALLBACK
);
1966 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1967 int *r
, int *g
, int *b
)
1969 struct macctrls
*mcs
= dlg
;
1971 if (mcs
->gotcolour
) {
1972 *r
= mcs
->thecolour
.red
>> 8;
1973 *g
= mcs
->thecolour
.green
>> 8;
1974 *b
= mcs
->thecolour
.blue
>> 8;
1982 * c-file-style: "simon"