1 /* $Id: macctrls.c,v 1.42 2003/05/10 20:51:39 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_hideshowpanel(struct macctrls
*, unsigned int, int);
134 static void macctrl_switchtopanel(struct macctrls
*, unsigned int);
135 static void macctrl_setfocus(struct macctrls
*, union macctrl
*);
136 static void macctrl_text(struct macctrls
*, WindowPtr
,
137 struct mac_layoutstate
*, union control
*);
138 static void macctrl_editbox(struct macctrls
*, WindowPtr
,
139 struct mac_layoutstate
*, union control
*);
140 static void macctrl_radio(struct macctrls
*, WindowPtr
,
141 struct mac_layoutstate
*, union control
*);
142 static void macctrl_checkbox(struct macctrls
*, WindowPtr
,
143 struct mac_layoutstate
*, union control
*);
144 static void macctrl_button(struct macctrls
*, WindowPtr
,
145 struct mac_layoutstate
*, union control
*);
146 static void macctrl_listbox(struct macctrls
*, WindowPtr
,
147 struct mac_layoutstate
*, union control
*);
148 static void macctrl_popup(struct macctrls
*, WindowPtr
,
149 struct mac_layoutstate
*, union control
*);
150 #if !TARGET_API_MAC_CARBON
151 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16
, ControlRef
,
152 ControlDefProcMessage
, SInt32
);
153 static pascal SInt32
macctrl_sys7_default_cdef(SInt16
, ControlRef
,
154 ControlDefProcMessage
, SInt32
);
155 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16
, ControlRef
,
156 ControlDefProcMessage
, SInt32
);
159 #if !TARGET_API_MAC_CARBON
161 * This trick enables us to keep all the CDEF code in the main
162 * application, which makes life easier. For details, see
163 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
166 #pragma options align=mac68k
168 short jmpabs
; /* 4EF9 */
169 ControlDefUPP theUPP
;
171 #pragma options align=reset
174 static void macctrl_init()
176 #if !TARGET_API_MAC_CARBON
177 static int inited
= 0;
181 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_EditBox
);
182 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_editbox_cdef
);
183 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_Default
);
184 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_default_cdef
);
185 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_ListBox
);
186 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_listbox_cdef
);
192 static int macctrl_cmp_byctrl(void *av
, void *bv
)
194 union macctrl
*a
= (union macctrl
*)av
;
195 union macctrl
*b
= (union macctrl
*)bv
;
197 if (a
->generic
.ctrl
< b
->generic
.ctrl
)
199 else if (a
->generic
.ctrl
> b
->generic
.ctrl
)
205 static int macctrl_cmp_byctrl_find(void *av
, void *bv
)
207 union control
*a
= (union control
*)av
;
208 union macctrl
*b
= (union macctrl
*)bv
;
210 if (a
< b
->generic
.ctrl
)
212 else if (a
> b
->generic
.ctrl
)
218 static union control panellist
;
220 static void panellist_handler(union control
*ctrl
, void *dlg
, void *data
,
223 struct macctrls
*mcs
= dlg
;
225 /* XXX what if there's no selection? */
226 if (event
== EVENT_SELCHANGE
)
227 macctrl_switchtopanel(mcs
, dlg_listbox_index(ctrl
, dlg
) + 1);
230 void macctrl_layoutbox(struct controlbox
*cb
, WindowPtr window
,
231 struct macctrls
*mcs
)
234 struct mac_layoutstate curstate
;
239 if (mac_gestalts
.apprvers
>= 0x100)
240 CreateRootControl(window
, &root
);
241 #if TARGET_API_MAC_CARBON
242 GetPortBounds(GetWindowPort(window
), &rect
);
244 rect
= window
->portRect
;
246 mcs
->window
= window
;
247 mcs
->byctrl
= newtree234(macctrl_cmp_byctrl
);
249 mcs
->defbutton
= NULL
;
250 mcs
->canbutton
= NULL
;
251 /* Count the number of panels */
253 for (i
= 1; i
< cb
->nctrlsets
; i
++)
254 if (strcmp(cb
->ctrlsets
[i
]->pathname
, cb
->ctrlsets
[i
-1]->pathname
))
256 mcs
->panels
= snewn(mcs
->npanels
, union macctrl
*);
257 memset(mcs
->panels
, 0, sizeof(*mcs
->panels
) * mcs
->npanels
);
258 curstate
.panelnum
= 0;
260 curstate
.pos
.h
= rect
.left
+ 13;
261 curstate
.pos
.v
= rect
.top
+ 13;
262 curstate
.width
= 160;
263 panellist
.listbox
.type
= CTRL_LISTBOX
;
264 panellist
.listbox
.handler
= &panellist_handler
;
265 panellist
.listbox
.height
= 20;
266 panellist
.listbox
.percentwidth
= 100;
267 macctrl_listbox(mcs
, window
, &curstate
, &panellist
);
268 /* XXX Start with panel 1 active */
270 curstate
.pos
.h
= rect
.left
+ 13 + 160 + 13;
271 curstate
.pos
.v
= rect
.bottom
- 33;
272 curstate
.width
= rect
.right
- (rect
.left
+ 13 + 160) - (13 * 2);
273 for (i
= 0; i
< cb
->nctrlsets
; i
++) {
274 if (i
> 0 && strcmp(cb
->ctrlsets
[i
]->pathname
,
275 cb
->ctrlsets
[i
-1]->pathname
)) {
276 curstate
.pos
.v
= rect
.top
+ 13;
278 assert(curstate
.panelnum
< mcs
->npanels
);
279 dlg_listbox_add(&panellist
, mcs
, cb
->ctrlsets
[i
]->pathname
);
281 macctrl_layoutset(&curstate
, cb
->ctrlsets
[i
], window
, mcs
);
283 macctrl_switchtopanel(mcs
, 1);
284 macctrl_hideshowpanel(mcs
, 0, TRUE
);
285 /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
290 static void macctrl_layoutset(struct mac_layoutstate
*curstate
,
291 struct controlset
*s
,
292 WindowPtr window
, struct macctrls
*mcs
)
294 unsigned int i
, j
, ncols
, colstart
, colspan
;
295 struct mac_layoutstate cols
[MAXCOLS
], pos
;
300 for (i
= 0; i
< s
->ncontrols
; i
++) {
301 union control
*ctrl
= s
->ctrls
[i
];
303 colstart
= COLUMN_START(ctrl
->generic
.column
);
304 colspan
= COLUMN_SPAN(ctrl
->generic
.column
);
305 if (ctrl
->generic
.type
== CTRL_COLUMNS
) {
306 if (ctrl
->columns
.ncols
!= 1) {
307 ncols
= ctrl
->columns
.ncols
;
308 assert(ncols
<= MAXCOLS
);
309 for (j
= 0; j
< ncols
; j
++) {
312 cols
[j
].pos
.h
= cols
[j
-1].pos
.h
+ cols
[j
-1].width
+ 6;
314 cols
[j
].width
= curstate
->width
-
315 (cols
[j
].pos
.h
- curstate
->pos
.h
);
317 cols
[j
].width
= (curstate
->width
+ 6) *
318 ctrl
->columns
.percentages
[j
] / 100 - 6;
321 for (j
= 0; j
< ncols
; j
++)
322 if (cols
[j
].pos
.v
> cols
[0].pos
.v
)
323 cols
[0].pos
.v
= cols
[j
].pos
.v
;
324 cols
[0].width
= curstate
->width
;
328 pos
= cols
[colstart
];
329 pos
.width
= cols
[colstart
+ colspan
- 1].width
+
330 (cols
[colstart
+ colspan
- 1].pos
.h
- cols
[colstart
].pos
.h
);
332 for (j
= colstart
; j
< colstart
+ colspan
; j
++)
333 if (pos
.pos
.v
< cols
[j
].pos
.v
)
334 pos
.pos
.v
= cols
[j
].pos
.v
;
336 switch (ctrl
->generic
.type
) {
338 macctrl_text(mcs
, window
, &pos
, ctrl
);
341 macctrl_editbox(mcs
, window
, &pos
, ctrl
);
344 macctrl_radio(mcs
, window
, &pos
, ctrl
);
347 macctrl_checkbox(mcs
, window
, &pos
, ctrl
);
350 macctrl_button(mcs
, window
, &pos
, ctrl
);
353 if (ctrl
->listbox
.height
== 0)
354 macctrl_popup(mcs
, window
, &pos
, ctrl
);
356 macctrl_listbox(mcs
, window
, &pos
, ctrl
);
359 for (j
= colstart
; j
< colstart
+ colspan
; j
++)
360 cols
[j
].pos
.v
= pos
.pos
.v
;
363 for (j
= 0; j
< ncols
; j
++)
364 if (cols
[j
].pos
.v
> curstate
->pos
.v
)
365 curstate
->pos
.v
= cols
[j
].pos
.v
;
368 static void macctrl_hideshowpanel(struct macctrls
*mcs
, unsigned int panel
,
374 #define hideshow(c) do { \
375 if (showit) ShowControl(c); else HideControl(c); \
378 for (mc
= mcs
->panels
[panel
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
379 #if !TARGET_API_MAC_CARBON
380 if (mcs
->focus
== mc
)
381 macctrl_setfocus(mcs
, NULL
);
383 switch (mc
->generic
.type
) {
385 hideshow(mc
->text
.tbctrl
);
387 case MACCTRL_EDITBOX
:
388 hideshow(mc
->editbox
.tbctrl
);
389 if (mc
->editbox
.tblabel
!= NULL
)
390 hideshow(mc
->editbox
.tblabel
);
393 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
394 hideshow(mc
->radio
.tbctrls
[j
]);
395 if (mc
->radio
.tblabel
!= NULL
)
396 hideshow(mc
->radio
.tblabel
);
398 case MACCTRL_CHECKBOX
:
399 hideshow(mc
->checkbox
.tbctrl
);
402 hideshow(mc
->button
.tbctrl
);
403 if (mc
->button
.tbring
!= NULL
)
404 hideshow(mc
->button
.tbring
);
406 case MACCTRL_LISTBOX
:
407 hideshow(mc
->listbox
.tbctrl
);
409 * At least under Mac OS 8.1, hiding a list box
410 * doesn't hide its scroll bars.
412 #if TARGET_API_MAC_CARBON
413 hideshow(GetListVerticalScrollBar(mc
->listbox
.list
));
415 hideshow((*mc
->listbox
.list
)->vScroll
);
419 hideshow(mc
->popup
.tbctrl
);
425 static void macctrl_switchtopanel(struct macctrls
*mcs
, unsigned int which
)
428 macctrl_hideshowpanel(mcs
, mcs
->curpanel
, FALSE
);
429 macctrl_hideshowpanel(mcs
, which
, TRUE
);
430 mcs
->curpanel
= which
;
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 mc
->generic
.type
= MACCTRL_TEXT
;
482 mc
->generic
.ctrl
= ctrl
;
483 mc
->generic
.privdata
= NULL
;
484 bounds
.left
= curstate
->pos
.h
;
485 bounds
.right
= bounds
.left
+ curstate
->width
;
486 bounds
.top
= curstate
->pos
.v
;
487 bounds
.bottom
= bounds
.top
+ 16;
488 if (mac_gestalts
.apprvers
>= 0x100) {
491 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
492 kControlStaticTextProc
, (long)mc
);
493 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
494 kControlStaticTextTextTag
,
495 strlen(ctrl
->text
.label
), ctrl
->text
.label
);
496 GetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
497 kControlStaticTextTextHeightTag
,
498 sizeof(height
), &height
, &olen
);
500 #if !TARGET_API_MAC_CARBON
504 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
505 SYS7_TEXT_PROC
, (long)mc
);
506 te
= (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
;
507 TESetText(ctrl
->text
.label
, strlen(ctrl
->text
.label
), te
);
508 height
= TEGetHeight(1, (*te
)->nLines
, te
);
511 SizeControl(mc
->text
.tbctrl
, curstate
->width
, height
);
512 curstate
->pos
.v
+= height
+ 6;
513 add234(mcs
->byctrl
, mc
);
514 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
515 mcs
->panels
[curstate
->panelnum
] = mc
;
518 static void macctrl_editbox(struct macctrls
*mcs
, WindowPtr window
,
519 struct mac_layoutstate
*curstate
,
522 union macctrl
*mc
= snew(union macctrl
);
523 Rect lbounds
, bounds
;
525 mc
->generic
.type
= MACCTRL_EDITBOX
;
526 mc
->generic
.ctrl
= ctrl
;
527 mc
->generic
.privdata
= NULL
;
528 lbounds
.left
= curstate
->pos
.h
;
529 lbounds
.top
= curstate
->pos
.v
;
530 if (ctrl
->editbox
.percentwidth
== 100) {
531 if (ctrl
->editbox
.label
!= NULL
) {
532 lbounds
.right
= lbounds
.left
+ curstate
->width
;
533 lbounds
.bottom
= lbounds
.top
+ 16;
534 curstate
->pos
.v
+= 18;
536 bounds
.left
= curstate
->pos
.h
;
537 bounds
.right
= bounds
.left
+ curstate
->width
;
539 lbounds
.right
= lbounds
.left
+
540 curstate
->width
* (100 - ctrl
->editbox
.percentwidth
) / 100;
541 lbounds
.bottom
= lbounds
.top
+ 22;
542 bounds
.left
= lbounds
.right
;
543 bounds
.right
= lbounds
.left
+ curstate
->width
;
545 bounds
.top
= curstate
->pos
.v
;
546 bounds
.bottom
= bounds
.top
+ 22;
547 if (mac_gestalts
.apprvers
>= 0x100) {
548 if (ctrl
->editbox
.label
== NULL
)
549 mc
->editbox
.tblabel
= NULL
;
551 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, FALSE
,
552 0, 0, 0, kControlStaticTextProc
,
554 SetControlData(mc
->editbox
.tblabel
, kControlEntireControl
,
555 kControlStaticTextTextTag
,
556 strlen(ctrl
->editbox
.label
), ctrl
->editbox
.label
);
558 InsetRect(&bounds
, 3, 3);
559 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
560 ctrl
->editbox
.password ?
561 kControlEditTextPasswordProc
:
562 kControlEditTextProc
, (long)mc
);
564 #if !TARGET_API_MAC_CARBON
566 if (ctrl
->editbox
.label
== NULL
)
567 mc
->editbox
.tblabel
= NULL
;
569 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, FALSE
,
570 0, 0, 0, SYS7_TEXT_PROC
,
572 TESetText(ctrl
->editbox
.label
, strlen(ctrl
->editbox
.label
),
573 (TEHandle
)(*mc
->editbox
.tblabel
)->contrlData
);
575 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
576 SYS7_EDITBOX_PROC
, (long)mc
);
579 curstate
->pos
.v
+= 28;
580 add234(mcs
->byctrl
, mc
);
581 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
582 mcs
->panels
[curstate
->panelnum
] = mc
;
583 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
586 #if !TARGET_API_MAC_CARBON
587 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16 variant
,
589 ControlDefProcMessage msg
,
600 rect
= (*control
)->contrlRect
;
601 if (variant
== SYS7_EDITBOX_VARIANT
)
602 InsetRect(&rect
, 3, 3); /* 2 if it's 20 pixels high */
603 te
= TENew(&rect
, &rect
);
604 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
605 (*te
)->txSize
= LoWord(ssfs
);
606 (*te
)->txFont
= HiWord(ssfs
);
607 (*control
)->contrlData
= (Handle
)te
;
610 TEDispose((TEHandle
)(*control
)->contrlData
);
613 if ((*control
)->contrlVis
) {
614 rect
= (*control
)->contrlRect
;
615 if (variant
== SYS7_EDITBOX_VARIANT
) {
618 InsetRect(&rect
, 3, 3);
621 (*(TEHandle
)(*control
)->contrlData
)->viewRect
= rect
;
622 TEUpdate(&rect
, (TEHandle
)(*control
)->contrlData
);
626 if (variant
== SYS7_TEXT_VARIANT
)
627 return kControlNoPart
;
628 mouse
.h
= LoWord(param
);
629 mouse
.v
= HiWord(param
);
630 rect
= (*control
)->contrlRect
;
631 InsetRect(&rect
, 3, 3);
632 return PtInRect(mouse
, &rect
) ? kControlEditTextPart
: kControlNoPart
;
634 if (param
& (1 << 31)) {
640 rgn
= (RgnHandle
)param
;
641 RectRgn(rgn
, &(*control
)->contrlRect
);
645 rgn
= (RgnHandle
)param
;
654 static void macctrl_radio(struct macctrls
*mcs
, WindowPtr window
,
655 struct mac_layoutstate
*curstate
,
658 union macctrl
*mc
= snew(union macctrl
);
661 unsigned int i
, colwidth
;
663 mc
->generic
.type
= MACCTRL_RADIO
;
664 mc
->generic
.ctrl
= ctrl
;
665 mc
->generic
.privdata
= NULL
;
666 mc
->radio
.tbctrls
= snewn(ctrl
->radio
.nbuttons
, ControlRef
);
667 colwidth
= (curstate
->width
+ 13) / ctrl
->radio
.ncolumns
;
668 bounds
.top
= curstate
->pos
.v
;
669 bounds
.bottom
= bounds
.top
+ 16;
670 bounds
.left
= curstate
->pos
.h
;
671 bounds
.right
= bounds
.left
+ curstate
->width
;
672 if (ctrl
->radio
.label
== NULL
)
673 mc
->radio
.tblabel
= NULL
;
675 if (mac_gestalts
.apprvers
>= 0x100) {
676 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, FALSE
,
677 0, 0, 0, kControlStaticTextProc
,
679 SetControlData(mc
->radio
.tblabel
, kControlEntireControl
,
680 kControlStaticTextTextTag
,
681 strlen(ctrl
->radio
.label
), ctrl
->radio
.label
);
683 #if !TARGET_API_MAC_CARBON
685 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, FALSE
,
686 0, 0, 0, SYS7_TEXT_PROC
, (long)mc
);
687 TESetText(ctrl
->radio
.label
, strlen(ctrl
->radio
.label
),
688 (TEHandle
)(*mc
->radio
.tblabel
)->contrlData
);
691 curstate
->pos
.v
+= 18;
693 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
694 bounds
.top
= curstate
->pos
.v
- 2;
695 bounds
.bottom
= bounds
.top
+ 18;
696 bounds
.left
= curstate
->pos
.h
+ colwidth
* (i
% ctrl
->radio
.ncolumns
);
697 if (i
== ctrl
->radio
.nbuttons
- 1 ||
698 i
% ctrl
->radio
.ncolumns
== ctrl
->radio
.ncolumns
- 1) {
699 bounds
.right
= curstate
->pos
.h
+ curstate
->width
;
700 curstate
->pos
.v
+= 18;
702 bounds
.right
= bounds
.left
+ colwidth
- 13;
703 c2pstrcpy(title
, ctrl
->radio
.buttons
[i
]);
704 mc
->radio
.tbctrls
[i
] = NewControl(window
, &bounds
, title
, FALSE
,
705 0, 0, 1, radioButProc
, (long)mc
);
707 curstate
->pos
.v
+= 4;
708 add234(mcs
->byctrl
, mc
);
709 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
710 mcs
->panels
[curstate
->panelnum
] = mc
;
711 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
714 static void macctrl_checkbox(struct macctrls
*mcs
, WindowPtr window
,
715 struct mac_layoutstate
*curstate
,
718 union macctrl
*mc
= snew(union macctrl
);
722 assert(ctrl
->checkbox
.label
!= NULL
);
723 mc
->generic
.type
= MACCTRL_CHECKBOX
;
724 mc
->generic
.ctrl
= ctrl
;
725 mc
->generic
.privdata
= NULL
;
726 bounds
.left
= curstate
->pos
.h
;
727 bounds
.right
= bounds
.left
+ curstate
->width
;
728 bounds
.top
= curstate
->pos
.v
;
729 bounds
.bottom
= bounds
.top
+ 16;
730 c2pstrcpy(title
, ctrl
->checkbox
.label
);
731 mc
->checkbox
.tbctrl
= NewControl(window
, &bounds
, title
, FALSE
, 0, 0, 1,
732 checkBoxProc
, (long)mc
);
733 add234(mcs
->byctrl
, mc
);
734 curstate
->pos
.v
+= 22;
735 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
736 mcs
->panels
[curstate
->panelnum
] = mc
;
737 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
740 static void macctrl_button(struct macctrls
*mcs
, WindowPtr window
,
741 struct mac_layoutstate
*curstate
,
744 union macctrl
*mc
= snew(union macctrl
);
748 assert(ctrl
->button
.label
!= NULL
);
749 mc
->generic
.type
= MACCTRL_BUTTON
;
750 mc
->generic
.ctrl
= ctrl
;
751 mc
->generic
.privdata
= NULL
;
752 bounds
.left
= curstate
->pos
.h
;
753 bounds
.right
= bounds
.left
+ curstate
->width
;
754 bounds
.top
= curstate
->pos
.v
;
755 bounds
.bottom
= bounds
.top
+ 20;
756 c2pstrcpy(title
, ctrl
->button
.label
);
757 mc
->button
.tbctrl
= NewControl(window
, &bounds
, title
, FALSE
, 0, 0, 1,
758 pushButProc
, (long)mc
);
759 mc
->button
.tbring
= NULL
;
760 if (mac_gestalts
.apprvers
>= 0x100) {
761 Boolean isdefault
= ctrl
->button
.isdefault
;
763 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
764 kControlPushButtonDefaultTag
,
765 sizeof(isdefault
), &isdefault
);
766 } else if (ctrl
->button
.isdefault
) {
767 InsetRect(&bounds
, -4, -4);
768 mc
->button
.tbring
= NewControl(window
, &bounds
, title
, FALSE
, 0, 0, 1,
769 SYS7_DEFAULT_PROC
, (long)mc
);
771 if (mac_gestalts
.apprvers
>= 0x110) {
772 Boolean iscancel
= ctrl
->button
.iscancel
;
774 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
775 kControlPushButtonCancelTag
,
776 sizeof(iscancel
), &iscancel
);
778 if (ctrl
->button
.isdefault
)
780 if (ctrl
->button
.iscancel
)
782 add234(mcs
->byctrl
, mc
);
783 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
784 mcs
->panels
[curstate
->panelnum
] = mc
;
785 curstate
->pos
.v
+= 26;
788 #if !TARGET_API_MAC_CARBON
789 static pascal SInt32
macctrl_sys7_default_cdef(SInt16 variant
,
791 ControlDefProcMessage msg
,
801 if ((*control
)->contrlVis
) {
802 rect
= (*control
)->contrlRect
;
803 GetPenState(&savestate
);
806 if ((*control
)->contrlHilite
== kControlInactivePart
)
808 oval
= (rect
.bottom
- rect
.top
) / 2 + 2;
809 FrameRoundRect(&rect
, oval
, oval
);
810 SetPenState(&savestate
);
814 if (param
& (1 << 31)) {
820 rgn
= (RgnHandle
)param
;
821 RectRgn(rgn
, &(*control
)->contrlRect
);
825 rgn
= (RgnHandle
)param
;
834 static void macctrl_listbox(struct macctrls
*mcs
, WindowPtr window
,
835 struct mac_layoutstate
*curstate
,
838 union macctrl
*mc
= snew(union macctrl
);
843 assert(ctrl
->listbox
.percentwidth
== 100);
844 mc
->generic
.type
= MACCTRL_LISTBOX
;
845 mc
->generic
.ctrl
= ctrl
;
846 mc
->generic
.privdata
= NULL
;
847 /* The list starts off empty */
848 mc
->listbox
.nids
= 0;
849 mc
->listbox
.ids
= NULL
;
850 bounds
.left
= curstate
->pos
.h
;
851 bounds
.right
= bounds
.left
+ curstate
->width
;
852 bounds
.top
= curstate
->pos
.v
;
853 bounds
.bottom
= bounds
.top
+ 16 * ctrl
->listbox
.height
+ 2;
855 if (mac_gestalts
.apprvers
>= 0x100) {
856 InsetRect(&bounds
, 3, 3);
857 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
,
859 kControlListBoxProc
, (long)mc
);
860 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
861 kControlListBoxListHandleTag
,
862 sizeof(mc
->listbox
.list
), &mc
->listbox
.list
,
864 DisposeControl(mc
->listbox
.tbctrl
);
869 #if !TARGET_API_MAC_CARBON
871 InsetRect(&bounds
, -3, -3);
872 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
,
874 SYS7_LISTBOX_PROC
, (long)mc
);
875 mc
->listbox
.list
= (ListHandle
)(*mc
->listbox
.tbctrl
)->contrlData
;
876 (*mc
->listbox
.list
)->refCon
= (long)mc
;
879 if (!ctrl
->listbox
.multisel
) {
880 #if TARGET_API_MAC_CARBON
881 SetListSelectionFlags(mc
->listbox
.list
, lOnlyOne
);
883 (*mc
->listbox
.list
)->selFlags
= lOnlyOne
;
886 add234(mcs
->byctrl
, mc
);
887 curstate
->pos
.v
+= 6 + 16 * ctrl
->listbox
.height
+ 2;
888 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
889 mcs
->panels
[curstate
->panelnum
] = mc
;
890 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
891 #if TARGET_API_MAC_CARBON
892 HideControl(GetListVerticalScrollBar(mc
->listbox
.list
));
894 HideControl((*mc
->listbox
.list
)->vScroll
);
898 #if !TARGET_API_MAC_CARBON
899 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16 variant
,
901 ControlDefProcMessage msg
,
917 rect
= (*control
)->contrlRect
;
918 InsetRect(&rect
, 4, 4);
919 rect
.right
-= 15; /* scroll bar */
920 bounds
.top
= bounds
.bottom
= bounds
.left
= 0;
922 csize
.h
= csize
.v
= 0;
924 savefont
= curport
->txFont
;
925 savesize
= curport
->txSize
;
926 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
927 TextFont(HiWord(ssfs
));
928 TextSize(LoWord(ssfs
));
929 list
= LNew(&rect
, &bounds
, csize
, 0, (*control
)->contrlOwner
,
930 TRUE
, FALSE
, FALSE
, TRUE
);
931 SetControlReference((*list
)->vScroll
, (long)list
);
932 (*control
)->contrlData
= (Handle
)list
;
938 * If the dialogue box is being destroyed, the scroll bar
939 * might have gone already. In our situation, this is the
940 * only time we destroy a control, so NULL out the scroll bar
941 * handle to prevent LDispose trying to free it.
943 list
= (ListHandle
)(*control
)->contrlData
;
944 (*list
)->vScroll
= NULL
;
948 if ((*control
)->contrlVis
) {
949 rect
= (*control
)->contrlRect
;
950 /* XXX input focus highlighting? */
951 InsetRect(&rect
, 3, 3);
954 list
= (ListHandle
)(*control
)->contrlData
;
955 LActivate((*control
)->contrlHilite
!= kControlInactivePart
, list
);
957 LUpdate(curport
->visRgn
, list
);
961 mouse
.h
= LoWord(param
);
962 mouse
.v
= HiWord(param
);
963 rect
= (*control
)->contrlRect
;
964 InsetRect(&rect
, 4, 4);
966 * We deliberately exclude the scrollbar so that LClick() can see it.
969 return PtInRect(mouse
, &rect
) ? kControlListBoxPart
: kControlNoPart
;
971 if (param
& (1 << 31)) {
977 rgn
= (RgnHandle
)param
;
978 RectRgn(rgn
, &(*control
)->contrlRect
);
982 rgn
= (RgnHandle
)param
;
991 static void macctrl_popup(struct macctrls
*mcs
, WindowPtr window
,
992 struct mac_layoutstate
*curstate
,
995 union macctrl
*mc
= snew(union macctrl
);
998 unsigned int labelwidth
;
999 static int nextmenuid
= MENU_MIN
;
1004 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
1005 * create a popup menu with dynamic content.
1007 assert(ctrl
->listbox
.height
== 0);
1008 assert(!ctrl
->listbox
.draglist
);
1009 assert(!ctrl
->listbox
.multisel
);
1011 mc
->generic
.type
= MACCTRL_POPUP
;
1012 mc
->generic
.ctrl
= ctrl
;
1013 mc
->generic
.privdata
= NULL
;
1014 c2pstrcpy(title
, ctrl
->button
.label
== NULL ?
"" : ctrl
->button
.label
);
1016 /* Find a spare menu ID and create the menu */
1017 while (GetMenuHandle(nextmenuid
) != NULL
)
1018 if (++nextmenuid
>= MENU_MAX
) nextmenuid
= MENU_MIN
;
1019 menuid
= nextmenuid
++;
1020 menu
= NewMenu(menuid
, "\pdummy");
1021 if (menu
== NULL
) return;
1022 mc
->popup
.menu
= menu
;
1023 mc
->popup
.menuid
= menuid
;
1024 InsertMenu(menu
, kInsertHierarchicalMenu
);
1026 /* The menu starts off empty */
1028 mc
->popup
.ids
= NULL
;
1030 bounds
.left
= curstate
->pos
.h
;
1031 bounds
.right
= bounds
.left
+ curstate
->width
;
1032 bounds
.top
= curstate
->pos
.v
;
1033 bounds
.bottom
= bounds
.top
+ 20;
1034 /* XXX handle percentwidth == 100 */
1035 labelwidth
= curstate
->width
* (100 - ctrl
->listbox
.percentwidth
) / 100;
1036 mc
->popup
.tbctrl
= NewControl(window
, &bounds
, title
, FALSE
,
1037 popupTitleLeftJust
, menuid
, labelwidth
,
1038 popupMenuProc
+ popupFixedWidth
, (long)mc
);
1039 add234(mcs
->byctrl
, mc
);
1040 curstate
->pos
.v
+= 26;
1041 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
1042 mcs
->panels
[curstate
->panelnum
] = mc
;
1043 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1047 void macctrl_activate(WindowPtr window
, EventRecord
*event
)
1049 struct macctrls
*mcs
= mac_winctrls(window
);
1050 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1053 ControlPartCode state
;
1057 SetPort((GrafPtr
)GetWindowPort(window
));
1058 if (mac_gestalts
.apprvers
>= 0x100)
1059 SetThemeWindowBackground(window
, active ?
1060 kThemeBrushModelessDialogBackgroundActive
:
1061 kThemeBrushModelessDialogBackgroundInactive
,
1063 state
= active ? kControlNoPart
: kControlInactivePart
;
1064 for (i
= 0; i
<= mcs
->curpanel
; i
+= mcs
->curpanel
)
1065 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
1066 switch (mc
->generic
.type
) {
1068 HiliteControl(mc
->text
.tbctrl
, state
);
1070 case MACCTRL_EDITBOX
:
1071 HiliteControl(mc
->editbox
.tbctrl
, state
);
1072 if (mc
->editbox
.tblabel
!= NULL
)
1073 HiliteControl(mc
->editbox
.tblabel
, state
);
1076 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
1077 HiliteControl(mc
->radio
.tbctrls
[j
], state
);
1078 if (mc
->radio
.tblabel
!= NULL
)
1079 HiliteControl(mc
->radio
.tblabel
, state
);
1081 case MACCTRL_CHECKBOX
:
1082 HiliteControl(mc
->checkbox
.tbctrl
, state
);
1084 case MACCTRL_BUTTON
:
1085 HiliteControl(mc
->button
.tbctrl
, state
);
1086 if (mc
->button
.tbring
!= NULL
)
1087 HiliteControl(mc
->button
.tbring
, state
);
1089 case MACCTRL_LISTBOX
:
1090 HiliteControl(mc
->listbox
.tbctrl
, state
);
1093 HiliteControl(mc
->popup
.tbctrl
, state
);
1096 #if !TARGET_API_MAC_CARBON
1097 if (mcs
->focus
== mc
) {
1099 macctrl_enfocus(mc
);
1101 macctrl_defocus(mc
);
1108 void macctrl_click(WindowPtr window
, EventRecord
*event
)
1111 ControlHandle control
, oldfocus
;
1112 int part
, trackresult
;
1115 struct macctrls
*mcs
= mac_winctrls(window
);
1120 SetPort((GrafPtr
)GetWindowPort(window
));
1121 mouse
= event
->where
;
1122 GlobalToLocal(&mouse
);
1123 part
= FindControl(mouse
, window
, &control
);
1124 if (control
!= NULL
) {
1125 #if !TARGET_API_MAC_CARBON
1127 * Special magic for scroll bars in list boxes, whose refcon
1130 if (part
== kControlUpButtonPart
|| part
== kControlDownButtonPart
||
1131 part
== kControlPageUpPart
|| part
== kControlPageDownPart
||
1132 part
== kControlIndicatorPart
)
1133 mc
= (union macctrl
*)
1134 (*(ListHandle
)GetControlReference(control
))->refCon
;
1137 mc
= (union macctrl
*)GetControlReference(control
);
1138 if (mac_gestalts
.apprvers
>= 0x100) {
1139 if (GetControlFeatures(control
, &features
) == noErr
&&
1140 (features
& kControlSupportsFocus
) &&
1141 (features
& kControlGetsFocusOnClick
) &&
1142 GetKeyboardFocus(window
, &oldfocus
) == noErr
&&
1143 control
!= oldfocus
)
1144 SetKeyboardFocus(window
, control
, part
);
1145 trackresult
= HandleControlClick(control
, mouse
, event
->modifiers
,
1146 (ControlActionUPP
)-1);
1148 #if !TARGET_API_MAC_CARBON
1149 if (mc
->generic
.type
== MACCTRL_EDITBOX
&&
1150 control
== mc
->editbox
.tbctrl
) {
1151 TEHandle te
= (TEHandle
)(*control
)->contrlData
;
1153 macctrl_setfocus(mcs
, mc
);
1154 TEClick(mouse
, !!(event
->modifiers
& shiftKey
), te
);
1157 if (mc
->generic
.type
== MACCTRL_LISTBOX
&&
1158 (control
== mc
->listbox
.tbctrl
||
1159 control
== (*mc
->listbox
.list
)->vScroll
)) {
1161 macctrl_setfocus(mcs
, mc
);
1162 if (LClick(mouse
, event
->modifiers
, mc
->listbox
.list
))
1164 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1166 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1170 trackresult
= TrackControl(control
, mouse
, (ControlActionUPP
)-1);
1172 switch (mc
->generic
.type
) {
1174 if (trackresult
!= 0) {
1175 for (i
= 0; i
< mc
->generic
.ctrl
->radio
.nbuttons
; i
++)
1176 if (mc
->radio
.tbctrls
[i
] == control
)
1177 SetControlValue(mc
->radio
.tbctrls
[i
],
1178 kControlRadioButtonCheckedValue
);
1180 SetControlValue(mc
->radio
.tbctrls
[i
],
1181 kControlRadioButtonUncheckedValue
);
1182 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1185 case MACCTRL_CHECKBOX
:
1186 if (trackresult
!= 0) {
1187 SetControlValue(control
, !GetControlValue(control
));
1188 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1191 case MACCTRL_BUTTON
:
1192 if (trackresult
!= 0)
1193 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1195 case MACCTRL_LISTBOX
:
1196 /* FIXME spot double-click */
1197 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1200 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1208 void macctrl_key(WindowPtr window
, EventRecord
*event
)
1211 struct macctrls
*mcs
= mac_winctrls(window
);
1213 unsigned long dummy
;
1215 switch (event
->message
& charCodeMask
) {
1216 case kEnterCharCode
:
1217 case kReturnCharCode
:
1218 if (mcs
->defbutton
!= NULL
) {
1219 assert(mcs
->defbutton
->generic
.type
== MACCTRL_BUTTON
);
1220 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlButtonPart
);
1222 * I'd like to delay unhilighting the button until after
1223 * the event has been processed, but by them the entire
1224 * dialgue box might have been destroyed.
1227 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1228 ctrlevent(mcs
, mcs
->defbutton
, EVENT_ACTION
);
1231 case kEscapeCharCode
:
1232 if (mcs
->canbutton
!= NULL
) {
1233 assert(mcs
->canbutton
->generic
.type
== MACCTRL_BUTTON
);
1234 HiliteControl(mcs
->canbutton
->button
.tbctrl
, kControlButtonPart
);
1236 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1237 ctrlevent(mcs
, mcs
->canbutton
, EVENT_ACTION
);
1241 if (mac_gestalts
.apprvers
>= 0x100) {
1242 if (GetKeyboardFocus(window
, &control
) == noErr
&& control
!= NULL
) {
1243 HandleControlKey(control
, (event
->message
& keyCodeMask
) >> 8,
1244 event
->message
& charCodeMask
, event
->modifiers
);
1245 mc
= (union macctrl
*)GetControlReference(control
);
1246 switch (mc
->generic
.type
) {
1247 case MACCTRL_LISTBOX
:
1248 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1251 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1256 #if !TARGET_API_MAC_CARBON
1260 if (mcs
->focus
!= NULL
) {
1262 switch (mc
->generic
.type
) {
1263 case MACCTRL_EDITBOX
:
1264 te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1265 TEKey(event
->message
& charCodeMask
, te
);
1266 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1274 void macctrl_update(WindowPtr window
)
1276 #if TARGET_API_MAC_CARBON
1282 BeginUpdate(window
);
1284 SetPort((GrafPtr
)GetWindowPort(window
));
1285 if (mac_gestalts
.apprvers
>= 0x101) {
1286 #if TARGET_API_MAC_CARBON
1287 GetPortBounds(GetWindowPort(window
), &rect
);
1289 rect
= window
->portRect
;
1291 InsetRect(&rect
, -1, -1);
1292 DrawThemeModelessDialogFrame(&rect
, mac_frontwindow() == window ?
1293 kThemeStateActive
: kThemeStateInactive
);
1295 #if TARGET_API_MAC_CARBON
1297 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1298 UpdateControls(window
, visrgn
);
1301 UpdateControls(window
, window
->visRgn
);
1307 #if TARGET_API_MAC_CARBON
1308 #define EnableItem EnableMenuItem
1309 #define DisableItem DisableMenuItem
1311 void macctrl_adjustmenus(WindowPtr window
)
1315 menu
= GetMenuHandle(mFile
);
1316 DisableItem(menu
, iSave
); /* XXX enable if modified */
1317 EnableItem(menu
, iSaveAs
);
1318 EnableItem(menu
, iDuplicate
);
1320 menu
= GetMenuHandle(mEdit
);
1321 DisableItem(menu
, 0);
1324 void macctrl_close(WindowPtr window
)
1326 struct macctrls
*mcs
= mac_winctrls(window
);
1330 * Mostly, we don't bother disposing of the Toolbox controls,
1331 * since that will happen automatically when the window is
1332 * disposed of. Popup menus are an exception, because we have to
1333 * dispose of the menu ourselves, and doing that while the control
1334 * still holds a reference to it seems rude.
1336 while ((mc
= index234(mcs
->byctrl
, 0)) != NULL
) {
1337 if (mc
->generic
.privdata
!= NULL
&& mc
->generic
.freeprivdata
)
1338 sfree(mc
->generic
.privdata
);
1339 switch (mc
->generic
.type
) {
1341 DisposeControl(mc
->popup
.tbctrl
);
1342 DeleteMenu(mc
->popup
.menuid
);
1343 DisposeMenu(mc
->popup
.menu
);
1346 del234(mcs
->byctrl
, mc
);
1350 freetree234(mcs
->byctrl
);
1356 void dlg_update_start(union control
*ctrl
, void *dlg
)
1362 void dlg_update_done(union control
*ctrl
, void *dlg
)
1368 void dlg_set_focus(union control
*ctrl
, void *dlg
)
1371 if (mac_gestalts
.apprvers
>= 0x100) {
1372 /* Use SetKeyboardFocus() */
1374 /* Do our own mucking around */
1378 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1384 void dlg_beep(void *dlg
)
1390 void dlg_error_msg(void *dlg
, char *msg
)
1394 c2pstrcpy(pmsg
, msg
);
1395 ParamText(pmsg
, NULL
, NULL
, NULL
);
1396 StopAlert(128, NULL
);
1399 void dlg_end(void *dlg
, int value
)
1401 struct macctrls
*mcs
= dlg
;
1403 if (mcs
->end
!= NULL
)
1404 (*mcs
->end
)(mcs
->window
, value
);
1407 void dlg_refresh(union control
*ctrl
, void *dlg
)
1409 struct macctrls
*mcs
= dlg
;
1414 mc
= findbyctrl(mcs
, ctrl
);
1416 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1419 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
1421 struct macctrls
*mcs
= dlg
;
1422 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1425 return mc
->generic
.privdata
;
1428 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
1430 struct macctrls
*mcs
= dlg
;
1431 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1434 mc
->generic
.privdata
= ptr
;
1435 mc
->generic
.freeprivdata
= FALSE
;
1438 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
1440 struct macctrls
*mcs
= dlg
;
1441 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1444 mc
->generic
.privdata
= smalloc(size
);
1445 mc
->generic
.freeprivdata
= TRUE
;
1446 return mc
->generic
.privdata
;
1451 * Radio Button control
1454 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
1456 struct macctrls
*mcs
= dlg
;
1457 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1460 if (mc
== NULL
) return;
1461 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1462 if (i
== whichbutton
)
1463 SetControlValue(mc
->radio
.tbctrls
[i
],
1464 kControlRadioButtonCheckedValue
);
1466 SetControlValue(mc
->radio
.tbctrls
[i
],
1467 kControlRadioButtonUncheckedValue
);
1472 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
1474 struct macctrls
*mcs
= dlg
;
1475 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1479 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1480 if (GetControlValue(mc
->radio
.tbctrls
[i
]) ==
1481 kControlRadioButtonCheckedValue
)
1492 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
1494 struct macctrls
*mcs
= dlg
;
1495 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1497 if (mc
== NULL
) return;
1498 SetControlValue(mc
->checkbox
.tbctrl
,
1499 checked ? kControlCheckBoxCheckedValue
:
1500 kControlCheckBoxUncheckedValue
);
1503 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
1505 struct macctrls
*mcs
= dlg
;
1506 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1509 return GetControlValue(mc
->checkbox
.tbctrl
);
1517 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
1519 struct macctrls
*mcs
= dlg
;
1520 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1523 if (mc
== NULL
) return;
1524 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1526 SetPort((GrafPtr
)(GetWindowPort(mcs
->window
)));
1527 if (mac_gestalts
.apprvers
>= 0x100)
1528 SetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1529 ctrl
->editbox
.password ?
1530 kControlEditTextPasswordTag
:
1531 kControlEditTextTextTag
,
1532 strlen(text
), text
);
1533 #if !TARGET_API_MAC_CARBON
1535 TESetText(text
, strlen(text
),
1536 (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
1538 DrawOneControl(mc
->editbox
.tbctrl
);
1542 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
1544 struct macctrls
*mcs
= dlg
;
1545 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1549 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1550 if (mac_gestalts
.apprvers
>= 0x100) {
1551 if (GetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1552 ctrl
->editbox
.password ?
1553 kControlEditTextPasswordTag
:
1554 kControlEditTextTextTag
,
1555 length
- 1, buffer
, &olen
) != noErr
)
1557 if (olen
> length
- 1)
1560 #if !TARGET_API_MAC_CARBON
1562 TEHandle te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1564 olen
= (*te
)->teLength
;
1565 if (olen
> length
- 1)
1567 memcpy(buffer
, *(*te
)->hText
, olen
);
1570 buffer
[olen
] = '\0';
1578 static void dlg_macpopup_clear(union control
*ctrl
, void *dlg
)
1580 struct macctrls
*mcs
= dlg
;
1581 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1582 MenuRef menu
= mc
->popup
.menu
;
1585 if (mc
== NULL
) return;
1586 n
= CountMenuItems(menu
);
1587 for (i
= 0; i
< n
; i
++)
1588 DeleteMenuItem(menu
, n
- i
);
1590 sfree(mc
->popup
.ids
);
1591 mc
->popup
.ids
= NULL
;
1592 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1595 static void dlg_maclist_clear(union control
*ctrl
, void *dlg
)
1597 struct macctrls
*mcs
= dlg
;
1598 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1600 if (mc
== NULL
) return;
1601 LDelRow(0, 0, mc
->listbox
.list
);
1602 mc
->listbox
.nids
= 0;
1603 sfree(mc
->listbox
.ids
);
1604 mc
->listbox
.ids
= NULL
;
1605 DrawOneControl(mc
->listbox
.tbctrl
);
1608 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
1611 switch (ctrl
->generic
.type
) {
1613 if (ctrl
->listbox
.height
== 0)
1614 dlg_macpopup_clear(ctrl
, dlg
);
1616 dlg_maclist_clear(ctrl
, dlg
);
1621 static void dlg_macpopup_del(union control
*ctrl
, void *dlg
, int index
)
1623 struct macctrls
*mcs
= dlg
;
1624 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1625 MenuRef menu
= mc
->popup
.menu
;
1627 if (mc
== NULL
) return;
1628 DeleteMenuItem(menu
, index
+ 1);
1629 if (mc
->popup
.ids
!= NULL
)
1630 memcpy(mc
->popup
.ids
+ index
, mc
->popup
.ids
+ index
+ 1,
1631 (mc
->popup
.nids
- index
- 1) * sizeof(*mc
->popup
.ids
));
1632 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1635 static void dlg_maclist_del(union control
*ctrl
, void *dlg
, int index
)
1637 struct macctrls
*mcs
= dlg
;
1638 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1640 if (mc
== NULL
) return;
1641 LDelRow(1, index
, mc
->listbox
.list
);
1642 if (mc
->listbox
.ids
!= NULL
)
1643 memcpy(mc
->listbox
.ids
+ index
, mc
->listbox
.ids
+ index
+ 1,
1644 (mc
->listbox
.nids
- index
- 1) * sizeof(*mc
->listbox
.ids
));
1645 DrawOneControl(mc
->listbox
.tbctrl
);
1648 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
1651 switch (ctrl
->generic
.type
) {
1653 if (ctrl
->listbox
.height
== 0)
1654 dlg_macpopup_del(ctrl
, dlg
, index
);
1656 dlg_maclist_del(ctrl
, dlg
, index
);
1661 static void dlg_macpopup_add(union control
*ctrl
, void *dlg
, char const *text
)
1663 struct macctrls
*mcs
= dlg
;
1664 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1665 MenuRef menu
= mc
->popup
.menu
;
1668 if (mc
== NULL
) return;
1669 assert(text
[0] != '\0');
1670 c2pstrcpy(itemstring
, text
);
1671 AppendMenu(menu
, "\pdummy");
1672 SetMenuItemText(menu
, CountMenuItems(menu
), itemstring
);
1673 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1677 static void dlg_maclist_add(union control
*ctrl
, void *dlg
, char const *text
)
1679 struct macctrls
*mcs
= dlg
;
1680 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1682 Cell cell
= { 0, 0 };
1684 if (mc
== NULL
) return;
1685 #if TARGET_API_MAC_CARBON
1686 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1688 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1690 cell
.v
= bounds
.bottom
;
1691 LAddRow(1, cell
.v
, mc
->listbox
.list
);
1692 LSetCell(text
, strlen(text
), cell
, mc
->listbox
.list
);
1693 DrawOneControl(mc
->listbox
.tbctrl
);
1696 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
1699 switch (ctrl
->generic
.type
) {
1701 if (ctrl
->listbox
.height
== 0)
1702 dlg_macpopup_add(ctrl
, dlg
, text
);
1704 dlg_maclist_add(ctrl
, dlg
, text
);
1709 static void dlg_macpopup_addwithid(union control
*ctrl
, void *dlg
,
1710 char const *text
, int id
)
1712 struct macctrls
*mcs
= dlg
;
1713 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1714 MenuRef menu
= mc
->popup
.menu
;
1717 if (mc
== NULL
) return;
1718 dlg_macpopup_add(ctrl
, dlg
, text
);
1719 index
= CountMenuItems(menu
) - 1;
1720 if (mc
->popup
.nids
<= index
) {
1721 mc
->popup
.nids
= index
+ 1;
1722 mc
->popup
.ids
= sresize(mc
->popup
.ids
, mc
->popup
.nids
, int);
1724 mc
->popup
.ids
[index
] = id
;
1727 static void dlg_maclist_addwithid(union control
*ctrl
, void *dlg
,
1728 char const *text
, int id
)
1730 struct macctrls
*mcs
= dlg
;
1731 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1735 if (mc
== NULL
) return;
1736 dlg_maclist_add(ctrl
, dlg
, text
);
1737 #if TARGET_API_MAC_CARBON
1738 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1740 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1742 index
= bounds
.bottom
;
1743 if (mc
->listbox
.nids
<= index
) {
1744 mc
->listbox
.nids
= index
+ 1;
1745 mc
->listbox
.ids
= sresize(mc
->listbox
.ids
, mc
->listbox
.nids
, int);
1747 mc
->listbox
.ids
[index
] = id
;
1750 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
1751 char const *text
, int id
)
1754 switch (ctrl
->generic
.type
) {
1756 if (ctrl
->listbox
.height
== 0)
1757 dlg_macpopup_addwithid(ctrl
, dlg
, text
, id
);
1759 dlg_maclist_addwithid(ctrl
, dlg
, text
, id
);
1764 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
1766 struct macctrls
*mcs
= dlg
;
1767 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1770 switch (ctrl
->generic
.type
) {
1772 if (ctrl
->listbox
.height
== 0) {
1773 assert(mc
->popup
.ids
!= NULL
&& mc
->popup
.nids
> index
);
1774 return mc
->popup
.ids
[index
];
1776 assert(mc
->listbox
.ids
!= NULL
&& mc
->listbox
.nids
> index
);
1777 return mc
->listbox
.ids
[index
];
1783 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
1785 struct macctrls
*mcs
= dlg
;
1786 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1787 Cell cell
= { 0, 0 };
1790 switch (ctrl
->generic
.type
) {
1792 if (ctrl
->listbox
.height
== 0)
1793 return GetControlValue(mc
->popup
.tbctrl
) - 1;
1795 if (LGetSelect(TRUE
, &cell
, mc
->listbox
.list
))
1804 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
1806 struct macctrls
*mcs
= dlg
;
1807 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1808 Cell cell
= { 0, 0 };
1811 switch (ctrl
->generic
.type
) {
1813 if (ctrl
->listbox
.height
== 0)
1814 return GetControlValue(mc
->popup
.tbctrl
) - 1 == index
;
1817 return LGetSelect(FALSE
, &cell
, mc
->listbox
.list
);
1823 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
1825 struct macctrls
*mcs
= dlg
;
1826 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1828 if (mc
== NULL
) return;
1829 switch (ctrl
->generic
.type
) {
1831 if (ctrl
->listbox
.height
== 0)
1832 SetControlValue(mc
->popup
.tbctrl
, index
+ 1);
1842 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
1844 struct macctrls
*mcs
= dlg
;
1845 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1847 if (mc
== NULL
) return;
1848 if (mac_gestalts
.apprvers
>= 0x100)
1849 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
1850 kControlStaticTextTextTag
, strlen(text
), text
);
1851 #if !TARGET_API_MAC_CARBON
1853 TESetText(text
, strlen(text
),
1854 (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
);
1860 * File Selector control
1863 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
1868 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
1875 * Font Selector control
1878 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fn
)
1883 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fn
)
1890 * Printer enumeration
1893 printer_enum
*printer_start_enum(int *nprinters
)
1900 char *printer_get_name(printer_enum
*pe
, int thing
)
1906 void printer_finish_enum(printer_enum
*pe
)
1913 * Colour selection stuff
1916 void dlg_coloursel_start(union control
*ctrl
, void *dlg
,
1917 int r
, int g
, int b
)
1919 struct macctrls
*mcs
= dlg
;
1920 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1921 Point where
= {-1, -1}; /* Screen with greatest colour depth */
1924 if (HAVE_COLOR_QD()) {
1925 incolour
.red
= r
* 0x0101;
1926 incolour
.green
= g
* 0x0101;
1927 incolour
.blue
= b
* 0x0101;
1928 mcs
->gotcolour
= GetColor(where
, "\pModify Colour:", &incolour
,
1930 ctrlevent(mcs
, mc
, EVENT_CALLBACK
);
1935 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1936 int *r
, int *g
, int *b
)
1938 struct macctrls
*mcs
= dlg
;
1940 if (mcs
->gotcolour
) {
1941 *r
= mcs
->thecolour
.red
>> 8;
1942 *g
= mcs
->thecolour
.green
>> 8;
1943 *b
= mcs
->thecolour
.blue
>> 8;
1951 * c-file-style: "simon"