1 /* $Id: macctrls.c,v 1.40 2003/05/10 11:26:33 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 /* XXX what if there's no selection? */
225 if (event
== EVENT_SELCHANGE
)
226 macctrl_switchtopanel(mcs
, dlg_listbox_index(ctrl
, dlg
) + 1);
229 void macctrl_layoutbox(struct controlbox
*cb
, WindowPtr window
,
230 struct macctrls
*mcs
)
233 struct mac_layoutstate curstate
;
238 if (mac_gestalts
.apprvers
>= 0x100)
239 CreateRootControl(window
, &root
);
240 #if TARGET_API_MAC_CARBON
241 GetPortBounds(GetWindowPort(window
), &rect
);
243 rect
= window
->portRect
;
245 mcs
->window
= window
;
246 mcs
->byctrl
= newtree234(macctrl_cmp_byctrl
);
248 mcs
->defbutton
= NULL
;
249 mcs
->canbutton
= NULL
;
250 /* Count the number of panels */
252 for (i
= 1; i
< cb
->nctrlsets
; i
++)
253 if (strcmp(cb
->ctrlsets
[i
]->pathname
, cb
->ctrlsets
[i
-1]->pathname
))
255 mcs
->panels
= snewn(mcs
->npanels
, union macctrl
*);
256 memset(mcs
->panels
, 0, sizeof(*mcs
->panels
) * mcs
->npanels
);
257 curstate
.panelnum
= 0;
259 curstate
.pos
.h
= rect
.left
+ 13;
260 curstate
.pos
.v
= rect
.top
+ 13;
261 curstate
.width
= 160;
262 panellist
.listbox
.type
= CTRL_LISTBOX
;
263 panellist
.listbox
.handler
= &panellist_handler
;
264 panellist
.listbox
.height
= 20;
265 panellist
.listbox
.percentwidth
= 100;
266 macctrl_listbox(mcs
, window
, &curstate
, &panellist
);
267 /* XXX Start with panel 1 active */
269 curstate
.pos
.h
= rect
.left
+ 13 + 160 + 13;
270 curstate
.pos
.v
= rect
.bottom
- 33;
271 curstate
.width
= rect
.right
- (rect
.left
+ 13 + 160) - (13 * 2);
272 for (i
= 0; i
< cb
->nctrlsets
; i
++) {
273 if (i
> 0 && strcmp(cb
->ctrlsets
[i
]->pathname
,
274 cb
->ctrlsets
[i
-1]->pathname
)) {
275 curstate
.pos
.v
= rect
.top
+ 13;
277 assert(curstate
.panelnum
< mcs
->npanels
);
278 dlg_listbox_add(&panellist
, mcs
, cb
->ctrlsets
[i
]->pathname
);
280 macctrl_layoutset(&curstate
, cb
->ctrlsets
[i
], window
, mcs
);
282 macctrl_switchtopanel(mcs
, 1);
283 /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
288 static void macctrl_layoutset(struct mac_layoutstate
*curstate
,
289 struct controlset
*s
,
290 WindowPtr window
, struct macctrls
*mcs
)
292 unsigned int i
, j
, ncols
, colstart
;
293 struct mac_layoutstate cols
[MAXCOLS
];
298 for (i
= 0; i
< s
->ncontrols
; i
++) {
299 union control
*ctrl
= s
->ctrls
[i
];
301 colstart
= COLUMN_START(ctrl
->generic
.column
);
302 switch (ctrl
->generic
.type
) {
304 if (ctrl
->columns
.ncols
!= 1) {
305 ncols
= ctrl
->columns
.ncols
;
306 assert(ncols
<= MAXCOLS
);
307 for (j
= 0; j
< ncols
; j
++) {
310 cols
[j
].pos
.h
= cols
[j
-1].pos
.h
+ cols
[j
-1].width
+ 6;
312 cols
[j
].width
= curstate
->width
-
313 (cols
[j
].pos
.h
- curstate
->pos
.h
);
315 cols
[j
].width
= (curstate
->width
+ 6) *
316 ctrl
->columns
.percentages
[j
] / 100 - 6;
319 for (j
= 0; j
< ncols
; j
++)
320 if (cols
[j
].pos
.v
> cols
[0].pos
.v
)
321 cols
[0].pos
.v
= cols
[j
].pos
.v
;
322 cols
[0].width
= curstate
->width
;
327 macctrl_text(mcs
, window
, &cols
[colstart
], ctrl
);
330 macctrl_editbox(mcs
, window
, &cols
[colstart
], ctrl
);
333 macctrl_radio(mcs
, window
, &cols
[colstart
], ctrl
);
336 macctrl_checkbox(mcs
, window
, &cols
[colstart
], ctrl
);
339 macctrl_button(mcs
, window
, &cols
[colstart
], ctrl
);
342 if (ctrl
->listbox
.height
== 0)
343 macctrl_popup(mcs
, window
, &cols
[colstart
], ctrl
);
345 macctrl_listbox(mcs
, window
, &cols
[colstart
], ctrl
);
349 for (j
= 0; j
< ncols
; j
++)
350 if (cols
[j
].pos
.v
> curstate
->pos
.v
)
351 curstate
->pos
.v
= cols
[j
].pos
.v
;
354 static void macctrl_switchtopanel(struct macctrls
*mcs
, unsigned int which
)
359 #define hideshow(c) do { \
360 if (i == which) ShowControl(c); else HideControl(c); \
363 mcs
->curpanel
= which
;
364 /* Panel 0 is special and always visible. */
365 for (i
= 1; i
< mcs
->npanels
; i
++)
366 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
367 #if !TARGET_API_MAC_CARBON
368 if (mcs
->focus
== mc
)
369 macctrl_setfocus(mcs
, NULL
);
371 switch (mc
->generic
.type
) {
373 hideshow(mc
->text
.tbctrl
);
375 case MACCTRL_EDITBOX
:
376 hideshow(mc
->editbox
.tbctrl
);
377 if (mc
->editbox
.tblabel
!= NULL
)
378 hideshow(mc
->editbox
.tblabel
);
381 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
382 hideshow(mc
->radio
.tbctrls
[j
]);
383 if (mc
->radio
.tblabel
!= NULL
)
384 hideshow(mc
->radio
.tblabel
);
386 case MACCTRL_CHECKBOX
:
387 hideshow(mc
->checkbox
.tbctrl
);
390 hideshow(mc
->button
.tbctrl
);
392 case MACCTRL_LISTBOX
:
393 hideshow(mc
->listbox
.tbctrl
);
395 * At least under Mac OS 8.1, hiding a list box
396 * doesn't hide its scroll bars.
398 #if TARGET_API_MAC_CARBON
399 hideshow(GetListVerticalScrollBar(mc
->listbox
.list
));
401 hideshow((*mc
->listbox
.list
)->vScroll
);
405 hideshow(mc
->popup
.tbctrl
);
411 #if !TARGET_API_MAC_CARBON
413 * System 7 focus manipulation
415 static void macctrl_defocus(union macctrl
*mc
)
418 assert(mac_gestalts
.apprvers
< 0x100);
419 switch (mc
->generic
.type
) {
420 case MACCTRL_EDITBOX
:
421 TEDeactivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
426 static void macctrl_enfocus(union macctrl
*mc
)
429 assert(mac_gestalts
.apprvers
< 0x100);
430 switch (mc
->generic
.type
) {
431 case MACCTRL_EDITBOX
:
432 TEActivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
437 static void macctrl_setfocus(struct macctrls
*mcs
, union macctrl
*mc
)
440 if (mcs
->focus
== mc
)
442 if (mcs
->focus
!= NULL
)
443 macctrl_defocus(mcs
->focus
);
450 static void macctrl_text(struct macctrls
*mcs
, WindowPtr window
,
451 struct mac_layoutstate
*curstate
,
454 union macctrl
*mc
= snew(union macctrl
);
458 assert(ctrl
->text
.label
!= NULL
);
459 mc
->generic
.type
= MACCTRL_TEXT
;
460 mc
->generic
.ctrl
= ctrl
;
461 mc
->generic
.privdata
= NULL
;
462 bounds
.left
= curstate
->pos
.h
;
463 bounds
.right
= bounds
.left
+ curstate
->width
;
464 bounds
.top
= curstate
->pos
.v
;
465 bounds
.bottom
= bounds
.top
+ 16;
466 if (mac_gestalts
.apprvers
>= 0x100) {
469 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
470 kControlStaticTextProc
, (long)mc
);
471 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
472 kControlStaticTextTextTag
,
473 strlen(ctrl
->text
.label
), ctrl
->text
.label
);
474 GetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
475 kControlStaticTextTextHeightTag
,
476 sizeof(height
), &height
, &olen
);
478 #if !TARGET_API_MAC_CARBON
482 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
483 SYS7_TEXT_PROC
, (long)mc
);
484 te
= (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
;
485 TESetText(ctrl
->text
.label
, strlen(ctrl
->text
.label
), te
);
486 height
= TEGetHeight(1, (*te
)->nLines
, te
);
489 SizeControl(mc
->text
.tbctrl
, curstate
->width
, height
);
490 curstate
->pos
.v
+= height
+ 6;
491 add234(mcs
->byctrl
, mc
);
492 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
493 mcs
->panels
[curstate
->panelnum
] = mc
;
496 static void macctrl_editbox(struct macctrls
*mcs
, WindowPtr window
,
497 struct mac_layoutstate
*curstate
,
500 union macctrl
*mc
= snew(union macctrl
);
501 Rect lbounds
, bounds
;
503 mc
->generic
.type
= MACCTRL_EDITBOX
;
504 mc
->generic
.ctrl
= ctrl
;
505 mc
->generic
.privdata
= NULL
;
506 lbounds
.left
= curstate
->pos
.h
;
507 lbounds
.top
= curstate
->pos
.v
;
508 if (ctrl
->editbox
.percentwidth
== 100) {
509 if (ctrl
->editbox
.label
!= NULL
) {
510 lbounds
.right
= lbounds
.left
+ curstate
->width
;
511 lbounds
.bottom
= lbounds
.top
+ 16;
512 curstate
->pos
.v
+= 18;
514 bounds
.left
= curstate
->pos
.h
;
515 bounds
.right
= bounds
.left
+ curstate
->width
;
517 lbounds
.right
= lbounds
.left
+
518 curstate
->width
* (100 - ctrl
->editbox
.percentwidth
) / 100;
519 lbounds
.bottom
= lbounds
.top
+ 22;
520 bounds
.left
= lbounds
.right
;
521 bounds
.right
= lbounds
.left
+ curstate
->width
;
523 bounds
.top
= curstate
->pos
.v
;
524 bounds
.bottom
= bounds
.top
+ 22;
525 if (mac_gestalts
.apprvers
>= 0x100) {
526 if (ctrl
->editbox
.label
== NULL
)
527 mc
->editbox
.tblabel
= NULL
;
529 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
530 0, 0, 0, kControlStaticTextProc
,
532 SetControlData(mc
->editbox
.tblabel
, kControlEntireControl
,
533 kControlStaticTextTextTag
,
534 strlen(ctrl
->editbox
.label
), ctrl
->editbox
.label
);
536 InsetRect(&bounds
, 3, 3);
537 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
538 ctrl
->editbox
.password ?
539 kControlEditTextPasswordProc
:
540 kControlEditTextProc
, (long)mc
);
542 #if !TARGET_API_MAC_CARBON
544 if (ctrl
->editbox
.label
== NULL
)
545 mc
->editbox
.tblabel
= NULL
;
547 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
548 0, 0, 0, SYS7_TEXT_PROC
,
550 TESetText(ctrl
->editbox
.label
, strlen(ctrl
->editbox
.label
),
551 (TEHandle
)(*mc
->editbox
.tblabel
)->contrlData
);
553 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
554 SYS7_EDITBOX_PROC
, (long)mc
);
557 curstate
->pos
.v
+= 28;
558 add234(mcs
->byctrl
, mc
);
559 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
560 mcs
->panels
[curstate
->panelnum
] = mc
;
561 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
564 #if !TARGET_API_MAC_CARBON
565 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16 variant
,
567 ControlDefProcMessage msg
,
578 rect
= (*control
)->contrlRect
;
579 if (variant
== SYS7_EDITBOX_VARIANT
)
580 InsetRect(&rect
, 3, 3); /* 2 if it's 20 pixels high */
581 te
= TENew(&rect
, &rect
);
582 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
583 (*te
)->txSize
= LoWord(ssfs
);
584 (*te
)->txFont
= HiWord(ssfs
);
585 (*control
)->contrlData
= (Handle
)te
;
588 TEDispose((TEHandle
)(*control
)->contrlData
);
591 if ((*control
)->contrlVis
) {
592 rect
= (*control
)->contrlRect
;
593 if (variant
== SYS7_EDITBOX_VARIANT
) {
596 InsetRect(&rect
, 3, 3);
599 (*(TEHandle
)(*control
)->contrlData
)->viewRect
= rect
;
600 TEUpdate(&rect
, (TEHandle
)(*control
)->contrlData
);
604 if (variant
== SYS7_TEXT_VARIANT
)
605 return kControlNoPart
;
606 mouse
.h
= LoWord(param
);
607 mouse
.v
= HiWord(param
);
608 rect
= (*control
)->contrlRect
;
609 InsetRect(&rect
, 3, 3);
610 return PtInRect(mouse
, &rect
) ? kControlEditTextPart
: kControlNoPart
;
612 if (param
& (1 << 31)) {
618 rgn
= (RgnHandle
)param
;
619 RectRgn(rgn
, &(*control
)->contrlRect
);
623 rgn
= (RgnHandle
)param
;
632 static void macctrl_radio(struct macctrls
*mcs
, WindowPtr window
,
633 struct mac_layoutstate
*curstate
,
636 union macctrl
*mc
= snew(union macctrl
);
639 unsigned int i
, colwidth
;
641 mc
->generic
.type
= MACCTRL_RADIO
;
642 mc
->generic
.ctrl
= ctrl
;
643 mc
->generic
.privdata
= NULL
;
644 mc
->radio
.tbctrls
= snewn(ctrl
->radio
.nbuttons
, ControlRef
);
645 colwidth
= (curstate
->width
+ 13) / ctrl
->radio
.ncolumns
;
646 bounds
.top
= curstate
->pos
.v
;
647 bounds
.bottom
= bounds
.top
+ 16;
648 bounds
.left
= curstate
->pos
.h
;
649 bounds
.right
= bounds
.left
+ curstate
->width
;
650 if (ctrl
->radio
.label
== NULL
)
651 mc
->radio
.tblabel
= NULL
;
653 if (mac_gestalts
.apprvers
>= 0x100) {
654 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
655 0, 0, 0, kControlStaticTextProc
,
657 SetControlData(mc
->radio
.tblabel
, kControlEntireControl
,
658 kControlStaticTextTextTag
,
659 strlen(ctrl
->radio
.label
), ctrl
->radio
.label
);
661 #if !TARGET_API_MAC_CARBON
663 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
664 0, 0, 0, SYS7_TEXT_PROC
, (long)mc
);
665 TESetText(ctrl
->radio
.label
, strlen(ctrl
->radio
.label
),
666 (TEHandle
)(*mc
->radio
.tblabel
)->contrlData
);
669 curstate
->pos
.v
+= 18;
671 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
672 bounds
.top
= curstate
->pos
.v
- 2;
673 bounds
.bottom
= bounds
.top
+ 18;
674 bounds
.left
= curstate
->pos
.h
+ colwidth
* (i
% ctrl
->radio
.ncolumns
);
675 if (i
== ctrl
->radio
.nbuttons
- 1 ||
676 i
% ctrl
->radio
.ncolumns
== ctrl
->radio
.ncolumns
- 1) {
677 bounds
.right
= curstate
->pos
.h
+ curstate
->width
;
678 curstate
->pos
.v
+= 18;
680 bounds
.right
= bounds
.left
+ colwidth
- 13;
681 c2pstrcpy(title
, ctrl
->radio
.buttons
[i
]);
682 mc
->radio
.tbctrls
[i
] = NewControl(window
, &bounds
, title
, TRUE
,
683 0, 0, 1, radioButProc
, (long)mc
);
685 curstate
->pos
.v
+= 4;
686 add234(mcs
->byctrl
, mc
);
687 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
688 mcs
->panels
[curstate
->panelnum
] = mc
;
689 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
692 static void macctrl_checkbox(struct macctrls
*mcs
, WindowPtr window
,
693 struct mac_layoutstate
*curstate
,
696 union macctrl
*mc
= snew(union macctrl
);
700 assert(ctrl
->checkbox
.label
!= NULL
);
701 mc
->generic
.type
= MACCTRL_CHECKBOX
;
702 mc
->generic
.ctrl
= ctrl
;
703 mc
->generic
.privdata
= NULL
;
704 bounds
.left
= curstate
->pos
.h
;
705 bounds
.right
= bounds
.left
+ curstate
->width
;
706 bounds
.top
= curstate
->pos
.v
;
707 bounds
.bottom
= bounds
.top
+ 16;
708 c2pstrcpy(title
, ctrl
->checkbox
.label
);
709 mc
->checkbox
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
710 checkBoxProc
, (long)mc
);
711 add234(mcs
->byctrl
, mc
);
712 curstate
->pos
.v
+= 22;
713 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
714 mcs
->panels
[curstate
->panelnum
] = mc
;
715 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
718 static void macctrl_button(struct macctrls
*mcs
, WindowPtr window
,
719 struct mac_layoutstate
*curstate
,
722 union macctrl
*mc
= snew(union macctrl
);
726 assert(ctrl
->button
.label
!= NULL
);
727 mc
->generic
.type
= MACCTRL_BUTTON
;
728 mc
->generic
.ctrl
= ctrl
;
729 mc
->generic
.privdata
= NULL
;
730 bounds
.left
= curstate
->pos
.h
;
731 bounds
.right
= bounds
.left
+ curstate
->width
;
732 bounds
.top
= curstate
->pos
.v
;
733 bounds
.bottom
= bounds
.top
+ 20;
734 c2pstrcpy(title
, ctrl
->button
.label
);
735 mc
->button
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
736 pushButProc
, (long)mc
);
737 mc
->button
.tbring
= NULL
;
738 if (mac_gestalts
.apprvers
>= 0x100) {
739 Boolean isdefault
= ctrl
->button
.isdefault
;
741 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
742 kControlPushButtonDefaultTag
,
743 sizeof(isdefault
), &isdefault
);
744 } else if (ctrl
->button
.isdefault
) {
745 InsetRect(&bounds
, -4, -4);
746 mc
->button
.tbring
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
747 SYS7_DEFAULT_PROC
, (long)mc
);
749 if (mac_gestalts
.apprvers
>= 0x110) {
750 Boolean iscancel
= ctrl
->button
.iscancel
;
752 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
753 kControlPushButtonCancelTag
,
754 sizeof(iscancel
), &iscancel
);
756 if (ctrl
->button
.isdefault
)
758 if (ctrl
->button
.iscancel
)
760 add234(mcs
->byctrl
, mc
);
761 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
762 mcs
->panels
[curstate
->panelnum
] = mc
;
763 curstate
->pos
.v
+= 26;
766 #if !TARGET_API_MAC_CARBON
767 static pascal SInt32
macctrl_sys7_default_cdef(SInt16 variant
,
769 ControlDefProcMessage msg
,
779 if ((*control
)->contrlVis
) {
780 rect
= (*control
)->contrlRect
;
781 GetPenState(&savestate
);
784 if ((*control
)->contrlHilite
== kControlInactivePart
)
786 oval
= (rect
.bottom
- rect
.top
) / 2 + 2;
787 FrameRoundRect(&rect
, oval
, oval
);
788 SetPenState(&savestate
);
792 if (param
& (1 << 31)) {
798 rgn
= (RgnHandle
)param
;
799 RectRgn(rgn
, &(*control
)->contrlRect
);
803 rgn
= (RgnHandle
)param
;
812 static void macctrl_listbox(struct macctrls
*mcs
, WindowPtr window
,
813 struct mac_layoutstate
*curstate
,
816 union macctrl
*mc
= snew(union macctrl
);
821 assert(ctrl
->listbox
.percentwidth
== 100);
822 mc
->generic
.type
= MACCTRL_LISTBOX
;
823 mc
->generic
.ctrl
= ctrl
;
824 mc
->generic
.privdata
= NULL
;
825 /* The list starts off empty */
826 mc
->listbox
.nids
= 0;
827 mc
->listbox
.ids
= NULL
;
828 bounds
.left
= curstate
->pos
.h
;
829 bounds
.right
= bounds
.left
+ curstate
->width
;
830 bounds
.top
= curstate
->pos
.v
;
831 bounds
.bottom
= bounds
.top
+ 16 * ctrl
->listbox
.height
+ 2;
833 if (mac_gestalts
.apprvers
>= 0x100) {
834 InsetRect(&bounds
, 3, 3);
835 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
837 kControlListBoxProc
, (long)mc
);
838 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
839 kControlListBoxListHandleTag
,
840 sizeof(mc
->listbox
.list
), &mc
->listbox
.list
,
842 DisposeControl(mc
->listbox
.tbctrl
);
847 #if !TARGET_API_MAC_CARBON
849 InsetRect(&bounds
, -3, -3);
850 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
852 SYS7_LISTBOX_PROC
, (long)mc
);
853 mc
->listbox
.list
= (ListHandle
)(*mc
->listbox
.tbctrl
)->contrlData
;
854 (*mc
->listbox
.list
)->refCon
= (long)mc
;
857 if (!ctrl
->listbox
.multisel
) {
858 #if TARGET_API_MAC_CARBON
859 SetListSelectionFlags(mc
->listbox
.list
, lOnlyOne
);
861 (*mc
->listbox
.list
)->selFlags
= lOnlyOne
;
864 add234(mcs
->byctrl
, mc
);
865 curstate
->pos
.v
+= 6 + 16 * ctrl
->listbox
.height
+ 2;
866 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
867 mcs
->panels
[curstate
->panelnum
] = mc
;
868 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
871 #if !TARGET_API_MAC_CARBON
872 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16 variant
,
874 ControlDefProcMessage msg
,
890 rect
= (*control
)->contrlRect
;
891 InsetRect(&rect
, 4, 4);
892 rect
.right
-= 15; /* scroll bar */
893 bounds
.top
= bounds
.bottom
= bounds
.left
= 0;
895 csize
.h
= csize
.v
= 0;
897 savefont
= curport
->txFont
;
898 savesize
= curport
->txSize
;
899 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
900 TextFont(HiWord(ssfs
));
901 TextSize(LoWord(ssfs
));
902 list
= LNew(&rect
, &bounds
, csize
, 0, (*control
)->contrlOwner
,
903 TRUE
, FALSE
, FALSE
, TRUE
);
904 SetControlReference((*list
)->vScroll
, (long)list
);
905 (*control
)->contrlData
= (Handle
)list
;
911 * If the dialogue box is being destroyed, the scroll bar
912 * might have gone already. In our situation, this is the
913 * only time we destroy a control, so NULL out the scroll bar
914 * handle to prevent LDispose trying to free it.
916 list
= (ListHandle
)(*control
)->contrlData
;
917 (*list
)->vScroll
= NULL
;
921 if ((*control
)->contrlVis
) {
922 rect
= (*control
)->contrlRect
;
923 /* XXX input focus highlighting? */
924 InsetRect(&rect
, 3, 3);
927 list
= (ListHandle
)(*control
)->contrlData
;
928 LActivate((*control
)->contrlHilite
!= kControlInactivePart
, list
);
930 LUpdate(curport
->visRgn
, list
);
934 mouse
.h
= LoWord(param
);
935 mouse
.v
= HiWord(param
);
936 rect
= (*control
)->contrlRect
;
937 InsetRect(&rect
, 4, 4);
939 * We deliberately exclude the scrollbar so that LClick() can see it.
942 return PtInRect(mouse
, &rect
) ? kControlListBoxPart
: kControlNoPart
;
944 if (param
& (1 << 31)) {
950 rgn
= (RgnHandle
)param
;
951 RectRgn(rgn
, &(*control
)->contrlRect
);
955 rgn
= (RgnHandle
)param
;
964 static void macctrl_popup(struct macctrls
*mcs
, WindowPtr window
,
965 struct mac_layoutstate
*curstate
,
968 union macctrl
*mc
= snew(union macctrl
);
971 unsigned int labelwidth
;
972 static int nextmenuid
= MENU_MIN
;
977 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
978 * create a popup menu with dynamic content.
980 assert(ctrl
->listbox
.height
== 0);
981 assert(!ctrl
->listbox
.draglist
);
982 assert(!ctrl
->listbox
.multisel
);
984 mc
->generic
.type
= MACCTRL_POPUP
;
985 mc
->generic
.ctrl
= ctrl
;
986 mc
->generic
.privdata
= NULL
;
987 c2pstrcpy(title
, ctrl
->button
.label
== NULL ?
"" : ctrl
->button
.label
);
989 /* Find a spare menu ID and create the menu */
990 while (GetMenuHandle(nextmenuid
) != NULL
)
991 if (++nextmenuid
>= MENU_MAX
) nextmenuid
= MENU_MIN
;
992 menuid
= nextmenuid
++;
993 menu
= NewMenu(menuid
, "\pdummy");
994 if (menu
== NULL
) return;
995 mc
->popup
.menu
= menu
;
996 mc
->popup
.menuid
= menuid
;
997 InsertMenu(menu
, kInsertHierarchicalMenu
);
999 /* The menu starts off empty */
1001 mc
->popup
.ids
= NULL
;
1003 bounds
.left
= curstate
->pos
.h
;
1004 bounds
.right
= bounds
.left
+ curstate
->width
;
1005 bounds
.top
= curstate
->pos
.v
;
1006 bounds
.bottom
= bounds
.top
+ 20;
1007 /* XXX handle percentwidth == 100 */
1008 labelwidth
= curstate
->width
* (100 - ctrl
->listbox
.percentwidth
) / 100;
1009 mc
->popup
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
,
1010 popupTitleLeftJust
, menuid
, labelwidth
,
1011 popupMenuProc
+ popupFixedWidth
, (long)mc
);
1012 add234(mcs
->byctrl
, mc
);
1013 curstate
->pos
.v
+= 26;
1014 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
1015 mcs
->panels
[curstate
->panelnum
] = mc
;
1016 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1020 void macctrl_activate(WindowPtr window
, EventRecord
*event
)
1022 struct macctrls
*mcs
= mac_winctrls(window
);
1023 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1026 ControlPartCode state
;
1030 SetPort((GrafPtr
)GetWindowPort(window
));
1031 if (mac_gestalts
.apprvers
>= 0x100)
1032 SetThemeWindowBackground(window
, active ?
1033 kThemeBrushModelessDialogBackgroundActive
:
1034 kThemeBrushModelessDialogBackgroundInactive
,
1036 state
= active ? kControlNoPart
: kControlInactivePart
;
1037 for (i
= 0; i
<= mcs
->curpanel
; i
+= mcs
->curpanel
)
1038 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
1039 switch (mc
->generic
.type
) {
1041 HiliteControl(mc
->text
.tbctrl
, state
);
1043 case MACCTRL_EDITBOX
:
1044 HiliteControl(mc
->editbox
.tbctrl
, state
);
1045 if (mc
->editbox
.tblabel
!= NULL
)
1046 HiliteControl(mc
->editbox
.tblabel
, state
);
1049 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
1050 HiliteControl(mc
->radio
.tbctrls
[j
], state
);
1051 if (mc
->radio
.tblabel
!= NULL
)
1052 HiliteControl(mc
->radio
.tblabel
, state
);
1054 case MACCTRL_CHECKBOX
:
1055 HiliteControl(mc
->checkbox
.tbctrl
, state
);
1057 case MACCTRL_BUTTON
:
1058 HiliteControl(mc
->button
.tbctrl
, state
);
1059 if (mc
->button
.tbring
!= NULL
)
1060 HiliteControl(mc
->button
.tbring
, state
);
1062 case MACCTRL_LISTBOX
:
1063 HiliteControl(mc
->listbox
.tbctrl
, state
);
1066 HiliteControl(mc
->popup
.tbctrl
, state
);
1069 #if !TARGET_API_MAC_CARBON
1070 if (mcs
->focus
== mc
) {
1072 macctrl_enfocus(mc
);
1074 macctrl_defocus(mc
);
1081 void macctrl_click(WindowPtr window
, EventRecord
*event
)
1084 ControlHandle control
, oldfocus
;
1085 int part
, trackresult
;
1088 struct macctrls
*mcs
= mac_winctrls(window
);
1093 SetPort((GrafPtr
)GetWindowPort(window
));
1094 mouse
= event
->where
;
1095 GlobalToLocal(&mouse
);
1096 part
= FindControl(mouse
, window
, &control
);
1097 if (control
!= NULL
) {
1098 #if !TARGET_API_MAC_CARBON
1100 * Special magic for scroll bars in list boxes, whose refcon
1103 if (part
== kControlUpButtonPart
|| part
== kControlDownButtonPart
||
1104 part
== kControlPageUpPart
|| part
== kControlPageDownPart
||
1105 part
== kControlIndicatorPart
)
1106 mc
= (union macctrl
*)
1107 (*(ListHandle
)GetControlReference(control
))->refCon
;
1110 mc
= (union macctrl
*)GetControlReference(control
);
1111 if (mac_gestalts
.apprvers
>= 0x100) {
1112 if (GetControlFeatures(control
, &features
) == noErr
&&
1113 (features
& kControlSupportsFocus
) &&
1114 (features
& kControlGetsFocusOnClick
) &&
1115 GetKeyboardFocus(window
, &oldfocus
) == noErr
&&
1116 control
!= oldfocus
)
1117 SetKeyboardFocus(window
, control
, part
);
1118 trackresult
= HandleControlClick(control
, mouse
, event
->modifiers
,
1119 (ControlActionUPP
)-1);
1121 #if !TARGET_API_MAC_CARBON
1122 if (mc
->generic
.type
== MACCTRL_EDITBOX
&&
1123 control
== mc
->editbox
.tbctrl
) {
1124 TEHandle te
= (TEHandle
)(*control
)->contrlData
;
1126 macctrl_setfocus(mcs
, mc
);
1127 TEClick(mouse
, !!(event
->modifiers
& shiftKey
), te
);
1130 if (mc
->generic
.type
== MACCTRL_LISTBOX
&&
1131 (control
== mc
->listbox
.tbctrl
||
1132 control
== (*mc
->listbox
.list
)->vScroll
)) {
1134 macctrl_setfocus(mcs
, mc
);
1135 if (LClick(mouse
, event
->modifiers
, mc
->listbox
.list
))
1137 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1139 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1143 trackresult
= TrackControl(control
, mouse
, (ControlActionUPP
)-1);
1145 switch (mc
->generic
.type
) {
1147 if (trackresult
!= 0) {
1148 for (i
= 0; i
< mc
->generic
.ctrl
->radio
.nbuttons
; i
++)
1149 if (mc
->radio
.tbctrls
[i
] == control
)
1150 SetControlValue(mc
->radio
.tbctrls
[i
],
1151 kControlRadioButtonCheckedValue
);
1153 SetControlValue(mc
->radio
.tbctrls
[i
],
1154 kControlRadioButtonUncheckedValue
);
1155 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1158 case MACCTRL_CHECKBOX
:
1159 if (trackresult
!= 0) {
1160 SetControlValue(control
, !GetControlValue(control
));
1161 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1164 case MACCTRL_BUTTON
:
1165 if (trackresult
!= 0)
1166 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1168 case MACCTRL_LISTBOX
:
1169 /* FIXME spot double-click */
1170 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1173 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1181 void macctrl_key(WindowPtr window
, EventRecord
*event
)
1184 struct macctrls
*mcs
= mac_winctrls(window
);
1186 unsigned long dummy
;
1188 switch (event
->message
& charCodeMask
) {
1189 case kEnterCharCode
:
1190 case kReturnCharCode
:
1191 if (mcs
->defbutton
!= NULL
) {
1192 assert(mcs
->defbutton
->generic
.type
== MACCTRL_BUTTON
);
1193 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlButtonPart
);
1195 * I'd like to delay unhilighting the button until after
1196 * the event has been processed, but by them the entire
1197 * dialgue box might have been destroyed.
1200 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1201 ctrlevent(mcs
, mcs
->defbutton
, EVENT_ACTION
);
1204 case kEscapeCharCode
:
1205 if (mcs
->canbutton
!= NULL
) {
1206 assert(mcs
->canbutton
->generic
.type
== MACCTRL_BUTTON
);
1207 HiliteControl(mcs
->canbutton
->button
.tbctrl
, kControlButtonPart
);
1209 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1210 ctrlevent(mcs
, mcs
->canbutton
, EVENT_ACTION
);
1214 if (mac_gestalts
.apprvers
>= 0x100) {
1215 if (GetKeyboardFocus(window
, &control
) == noErr
&& control
!= NULL
) {
1216 HandleControlKey(control
, (event
->message
& keyCodeMask
) >> 8,
1217 event
->message
& charCodeMask
, event
->modifiers
);
1218 mc
= (union macctrl
*)GetControlReference(control
);
1219 switch (mc
->generic
.type
) {
1220 case MACCTRL_LISTBOX
:
1221 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1224 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1229 #if !TARGET_API_MAC_CARBON
1233 if (mcs
->focus
!= NULL
) {
1235 switch (mc
->generic
.type
) {
1236 case MACCTRL_EDITBOX
:
1237 te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1238 TEKey(event
->message
& charCodeMask
, te
);
1239 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1247 void macctrl_update(WindowPtr window
)
1249 #if TARGET_API_MAC_CARBON
1255 BeginUpdate(window
);
1257 SetPort((GrafPtr
)GetWindowPort(window
));
1258 if (mac_gestalts
.apprvers
>= 0x101) {
1259 #if TARGET_API_MAC_CARBON
1260 GetPortBounds(GetWindowPort(window
), &rect
);
1262 rect
= window
->portRect
;
1264 InsetRect(&rect
, -1, -1);
1265 DrawThemeModelessDialogFrame(&rect
, mac_frontwindow() == window ?
1266 kThemeStateActive
: kThemeStateInactive
);
1268 #if TARGET_API_MAC_CARBON
1270 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1271 UpdateControls(window
, visrgn
);
1274 UpdateControls(window
, window
->visRgn
);
1280 #if TARGET_API_MAC_CARBON
1281 #define EnableItem EnableMenuItem
1282 #define DisableItem DisableMenuItem
1284 void macctrl_adjustmenus(WindowPtr window
)
1288 menu
= GetMenuHandle(mFile
);
1289 DisableItem(menu
, iSave
); /* XXX enable if modified */
1290 EnableItem(menu
, iSaveAs
);
1291 EnableItem(menu
, iDuplicate
);
1293 menu
= GetMenuHandle(mEdit
);
1294 DisableItem(menu
, 0);
1297 void macctrl_close(WindowPtr window
)
1299 struct macctrls
*mcs
= mac_winctrls(window
);
1303 * Mostly, we don't bother disposing of the Toolbox controls,
1304 * since that will happen automatically when the window is
1305 * disposed of. Popup menus are an exception, because we have to
1306 * dispose of the menu ourselves, and doing that while the control
1307 * still holds a reference to it seems rude.
1309 while ((mc
= index234(mcs
->byctrl
, 0)) != NULL
) {
1310 if (mc
->generic
.privdata
!= NULL
&& mc
->generic
.freeprivdata
)
1311 sfree(mc
->generic
.privdata
);
1312 switch (mc
->generic
.type
) {
1314 DisposeControl(mc
->popup
.tbctrl
);
1315 DeleteMenu(mc
->popup
.menuid
);
1316 DisposeMenu(mc
->popup
.menu
);
1319 del234(mcs
->byctrl
, mc
);
1323 freetree234(mcs
->byctrl
);
1329 void dlg_update_start(union control
*ctrl
, void *dlg
)
1335 void dlg_update_done(union control
*ctrl
, void *dlg
)
1341 void dlg_set_focus(union control
*ctrl
, void *dlg
)
1344 if (mac_gestalts
.apprvers
>= 0x100) {
1345 /* Use SetKeyboardFocus() */
1347 /* Do our own mucking around */
1351 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1357 void dlg_beep(void *dlg
)
1363 void dlg_error_msg(void *dlg
, char *msg
)
1367 c2pstrcpy(pmsg
, msg
);
1368 ParamText(pmsg
, NULL
, NULL
, NULL
);
1369 StopAlert(128, NULL
);
1372 void dlg_end(void *dlg
, int value
)
1374 struct macctrls
*mcs
= dlg
;
1376 if (mcs
->end
!= NULL
)
1377 (*mcs
->end
)(mcs
->window
, value
);
1380 void dlg_refresh(union control
*ctrl
, void *dlg
)
1382 struct macctrls
*mcs
= dlg
;
1387 mc
= findbyctrl(mcs
, ctrl
);
1389 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1392 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
1394 struct macctrls
*mcs
= dlg
;
1395 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1398 return mc
->generic
.privdata
;
1401 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
1403 struct macctrls
*mcs
= dlg
;
1404 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1407 mc
->generic
.privdata
= ptr
;
1408 mc
->generic
.freeprivdata
= FALSE
;
1411 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
1413 struct macctrls
*mcs
= dlg
;
1414 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1417 mc
->generic
.privdata
= smalloc(size
);
1418 mc
->generic
.freeprivdata
= TRUE
;
1419 return mc
->generic
.privdata
;
1424 * Radio Button control
1427 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
1429 struct macctrls
*mcs
= dlg
;
1430 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1433 if (mc
== NULL
) return;
1434 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1435 if (i
== whichbutton
)
1436 SetControlValue(mc
->radio
.tbctrls
[i
],
1437 kControlRadioButtonCheckedValue
);
1439 SetControlValue(mc
->radio
.tbctrls
[i
],
1440 kControlRadioButtonUncheckedValue
);
1445 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
1447 struct macctrls
*mcs
= dlg
;
1448 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1452 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1453 if (GetControlValue(mc
->radio
.tbctrls
[i
]) ==
1454 kControlRadioButtonCheckedValue
)
1465 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
1467 struct macctrls
*mcs
= dlg
;
1468 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1470 if (mc
== NULL
) return;
1471 SetControlValue(mc
->checkbox
.tbctrl
,
1472 checked ? kControlCheckBoxCheckedValue
:
1473 kControlCheckBoxUncheckedValue
);
1476 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
1478 struct macctrls
*mcs
= dlg
;
1479 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1482 return GetControlValue(mc
->checkbox
.tbctrl
);
1490 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
1492 struct macctrls
*mcs
= dlg
;
1493 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1496 if (mc
== NULL
) return;
1497 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1499 SetPort((GrafPtr
)(GetWindowPort(mcs
->window
)));
1500 if (mac_gestalts
.apprvers
>= 0x100)
1501 SetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1502 ctrl
->editbox
.password ?
1503 kControlEditTextPasswordTag
:
1504 kControlEditTextTextTag
,
1505 strlen(text
), text
);
1506 #if !TARGET_API_MAC_CARBON
1508 TESetText(text
, strlen(text
),
1509 (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
1511 DrawOneControl(mc
->editbox
.tbctrl
);
1515 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
1517 struct macctrls
*mcs
= dlg
;
1518 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1522 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1523 if (mac_gestalts
.apprvers
>= 0x100) {
1524 if (GetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1525 ctrl
->editbox
.password ?
1526 kControlEditTextPasswordTag
:
1527 kControlEditTextTextTag
,
1528 length
- 1, buffer
, &olen
) != noErr
)
1530 if (olen
> length
- 1)
1533 #if !TARGET_API_MAC_CARBON
1535 TEHandle te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1537 olen
= (*te
)->teLength
;
1538 if (olen
> length
- 1)
1540 memcpy(buffer
, *(*te
)->hText
, olen
);
1543 buffer
[olen
] = '\0';
1551 static void dlg_macpopup_clear(union control
*ctrl
, void *dlg
)
1553 struct macctrls
*mcs
= dlg
;
1554 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1555 MenuRef menu
= mc
->popup
.menu
;
1558 if (mc
== NULL
) return;
1559 n
= CountMenuItems(menu
);
1560 for (i
= 0; i
< n
; i
++)
1561 DeleteMenuItem(menu
, n
- i
);
1563 sfree(mc
->popup
.ids
);
1564 mc
->popup
.ids
= NULL
;
1565 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1568 static void dlg_maclist_clear(union control
*ctrl
, void *dlg
)
1570 struct macctrls
*mcs
= dlg
;
1571 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1573 if (mc
== NULL
) return;
1574 LDelRow(0, 0, mc
->listbox
.list
);
1575 mc
->listbox
.nids
= 0;
1576 sfree(mc
->listbox
.ids
);
1577 mc
->listbox
.ids
= NULL
;
1578 DrawOneControl(mc
->listbox
.tbctrl
);
1581 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
1584 switch (ctrl
->generic
.type
) {
1586 if (ctrl
->listbox
.height
== 0)
1587 dlg_macpopup_clear(ctrl
, dlg
);
1589 dlg_maclist_clear(ctrl
, dlg
);
1594 static void dlg_macpopup_del(union control
*ctrl
, void *dlg
, int index
)
1596 struct macctrls
*mcs
= dlg
;
1597 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1598 MenuRef menu
= mc
->popup
.menu
;
1600 if (mc
== NULL
) return;
1601 DeleteMenuItem(menu
, index
+ 1);
1602 if (mc
->popup
.ids
!= NULL
)
1603 memcpy(mc
->popup
.ids
+ index
, mc
->popup
.ids
+ index
+ 1,
1604 (mc
->popup
.nids
- index
- 1) * sizeof(*mc
->popup
.ids
));
1605 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1608 static void dlg_maclist_del(union control
*ctrl
, void *dlg
, int index
)
1610 struct macctrls
*mcs
= dlg
;
1611 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1613 if (mc
== NULL
) return;
1614 LDelRow(1, index
, mc
->listbox
.list
);
1615 if (mc
->listbox
.ids
!= NULL
)
1616 memcpy(mc
->listbox
.ids
+ index
, mc
->listbox
.ids
+ index
+ 1,
1617 (mc
->listbox
.nids
- index
- 1) * sizeof(*mc
->listbox
.ids
));
1618 DrawOneControl(mc
->listbox
.tbctrl
);
1621 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
1624 switch (ctrl
->generic
.type
) {
1626 if (ctrl
->listbox
.height
== 0)
1627 dlg_macpopup_del(ctrl
, dlg
, index
);
1629 dlg_maclist_del(ctrl
, dlg
, index
);
1634 static void dlg_macpopup_add(union control
*ctrl
, void *dlg
, char const *text
)
1636 struct macctrls
*mcs
= dlg
;
1637 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1638 MenuRef menu
= mc
->popup
.menu
;
1641 if (mc
== NULL
) return;
1642 assert(text
[0] != '\0');
1643 c2pstrcpy(itemstring
, text
);
1644 AppendMenu(menu
, "\pdummy");
1645 SetMenuItemText(menu
, CountMenuItems(menu
), itemstring
);
1646 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1650 static void dlg_maclist_add(union control
*ctrl
, void *dlg
, char const *text
)
1652 struct macctrls
*mcs
= dlg
;
1653 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1655 Cell cell
= { 0, 0 };
1657 if (mc
== NULL
) return;
1658 #if TARGET_API_MAC_CARBON
1659 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1661 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1663 cell
.v
= bounds
.bottom
;
1664 LAddRow(1, cell
.v
, mc
->listbox
.list
);
1665 LSetCell(text
, strlen(text
), cell
, mc
->listbox
.list
);
1666 DrawOneControl(mc
->listbox
.tbctrl
);
1669 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
1672 switch (ctrl
->generic
.type
) {
1674 if (ctrl
->listbox
.height
== 0)
1675 dlg_macpopup_add(ctrl
, dlg
, text
);
1677 dlg_maclist_add(ctrl
, dlg
, text
);
1682 static void dlg_macpopup_addwithid(union control
*ctrl
, void *dlg
,
1683 char const *text
, int id
)
1685 struct macctrls
*mcs
= dlg
;
1686 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1687 MenuRef menu
= mc
->popup
.menu
;
1690 if (mc
== NULL
) return;
1691 dlg_macpopup_add(ctrl
, dlg
, text
);
1692 index
= CountMenuItems(menu
) - 1;
1693 if (mc
->popup
.nids
<= index
) {
1694 mc
->popup
.nids
= index
+ 1;
1695 mc
->popup
.ids
= sresize(mc
->popup
.ids
, mc
->popup
.nids
, int);
1697 mc
->popup
.ids
[index
] = id
;
1700 static void dlg_maclist_addwithid(union control
*ctrl
, void *dlg
,
1701 char const *text
, int id
)
1703 struct macctrls
*mcs
= dlg
;
1704 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1708 if (mc
== NULL
) return;
1709 dlg_maclist_add(ctrl
, dlg
, text
);
1710 #if TARGET_API_MAC_CARBON
1711 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1713 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1715 index
= bounds
.bottom
;
1716 if (mc
->listbox
.nids
<= index
) {
1717 mc
->listbox
.nids
= index
+ 1;
1718 mc
->listbox
.ids
= sresize(mc
->listbox
.ids
, mc
->listbox
.nids
, int);
1720 mc
->listbox
.ids
[index
] = id
;
1723 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
1724 char const *text
, int id
)
1727 switch (ctrl
->generic
.type
) {
1729 if (ctrl
->listbox
.height
== 0)
1730 dlg_macpopup_addwithid(ctrl
, dlg
, text
, id
);
1732 dlg_maclist_addwithid(ctrl
, dlg
, text
, id
);
1737 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
1739 struct macctrls
*mcs
= dlg
;
1740 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1743 switch (ctrl
->generic
.type
) {
1745 if (ctrl
->listbox
.height
== 0) {
1746 assert(mc
->popup
.ids
!= NULL
&& mc
->popup
.nids
> index
);
1747 return mc
->popup
.ids
[index
];
1749 assert(mc
->listbox
.ids
!= NULL
&& mc
->listbox
.nids
> index
);
1750 return mc
->listbox
.ids
[index
];
1756 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
1758 struct macctrls
*mcs
= dlg
;
1759 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1760 Cell cell
= { 0, 0 };
1763 switch (ctrl
->generic
.type
) {
1765 if (ctrl
->listbox
.height
== 0)
1766 return GetControlValue(mc
->popup
.tbctrl
) - 1;
1768 if (LGetSelect(TRUE
, &cell
, mc
->listbox
.list
))
1777 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
1779 struct macctrls
*mcs
= dlg
;
1780 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1781 Cell cell
= { 0, 0 };
1784 switch (ctrl
->generic
.type
) {
1786 if (ctrl
->listbox
.height
== 0)
1787 return GetControlValue(mc
->popup
.tbctrl
) - 1 == index
;
1790 return LGetSelect(FALSE
, &cell
, mc
->listbox
.list
);
1796 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
1798 struct macctrls
*mcs
= dlg
;
1799 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1801 if (mc
== NULL
) return;
1802 switch (ctrl
->generic
.type
) {
1804 if (ctrl
->listbox
.height
== 0)
1805 SetControlValue(mc
->popup
.tbctrl
, index
+ 1);
1815 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
1817 struct macctrls
*mcs
= dlg
;
1818 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1820 if (mc
== NULL
) return;
1821 if (mac_gestalts
.apprvers
>= 0x100)
1822 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
1823 kControlStaticTextTextTag
, strlen(text
), text
);
1824 #if !TARGET_API_MAC_CARBON
1826 TESetText(text
, strlen(text
),
1827 (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
);
1833 * File Selector control
1836 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
1841 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
1848 * Font Selector control
1851 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fn
)
1856 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fn
)
1863 * Printer enumeration
1866 printer_enum
*printer_start_enum(int *nprinters
)
1873 char *printer_get_name(printer_enum
*pe
, int thing
)
1879 void printer_finish_enum(printer_enum
*pe
)
1886 * Colour selection stuff
1889 void dlg_coloursel_start(union control
*ctrl
, void *dlg
,
1890 int r
, int g
, int b
)
1892 struct macctrls
*mcs
= dlg
;
1893 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1894 Point where
= {-1, -1}; /* Screen with greatest colour depth */
1897 if (HAVE_COLOR_QD()) {
1898 incolour
.red
= r
* 0x0101;
1899 incolour
.green
= g
* 0x0101;
1900 incolour
.blue
= b
* 0x0101;
1901 mcs
->gotcolour
= GetColor(where
, "\pModify Colour:", &incolour
,
1903 ctrlevent(mcs
, mc
, EVENT_CALLBACK
);
1908 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1909 int *r
, int *g
, int *b
)
1911 struct macctrls
*mcs
= dlg
;
1913 if (mcs
->gotcolour
) {
1914 *r
= mcs
->thecolour
.red
>> 8;
1915 *g
= mcs
->thecolour
.green
>> 8;
1916 *b
= mcs
->thecolour
.blue
>> 8;
1924 * c-file-style: "simon"