1 /* $Id: macctrls.c,v 1.39 2003/04/14 23:47:07 ben Exp $ */
3 * Copyright (c) 2003 Ben Harris
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <Appearance.h>
30 #include <ColorPicker.h>
32 #include <ControlDefinitions.h>
36 #include <Resources.h>
40 #include <TextUtils.h>
41 #include <ToolUtils.h>
53 /* Range of menu IDs for popup menus */
59 struct macctrl_generic
{
69 /* Template from which this was generated */
71 /* Next control in this panel */
77 struct macctrl_generic generic
;
81 struct macctrl_generic generic
;
86 struct macctrl_generic generic
;
91 struct macctrl_generic generic
;
95 struct macctrl_generic generic
;
100 struct macctrl_generic generic
;
107 struct macctrl_generic generic
;
116 struct mac_layoutstate
{
119 unsigned int panelnum
;
122 #define ctrlevent(mcs, mc, event) do { \
123 if ((mc)->generic.ctrl->generic.handler != NULL) \
124 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
125 (mcs)->data, (event)); \
128 #define findbyctrl(mcs, ctrl) \
129 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
131 static void macctrl_layoutset(struct mac_layoutstate
*, struct controlset
*,
132 WindowPtr
, struct macctrls
*);
133 static void macctrl_switchtopanel(struct macctrls
*, unsigned int);
134 static void macctrl_setfocus(struct macctrls
*, union macctrl
*);
135 static void macctrl_text(struct macctrls
*, WindowPtr
,
136 struct mac_layoutstate
*, union control
*);
137 static void macctrl_editbox(struct macctrls
*, WindowPtr
,
138 struct mac_layoutstate
*, union control
*);
139 static void macctrl_radio(struct macctrls
*, WindowPtr
,
140 struct mac_layoutstate
*, union control
*);
141 static void macctrl_checkbox(struct macctrls
*, WindowPtr
,
142 struct mac_layoutstate
*, union control
*);
143 static void macctrl_button(struct macctrls
*, WindowPtr
,
144 struct mac_layoutstate
*, union control
*);
145 static void macctrl_listbox(struct macctrls
*, WindowPtr
,
146 struct mac_layoutstate
*, union control
*);
147 static void macctrl_popup(struct macctrls
*, WindowPtr
,
148 struct mac_layoutstate
*, union control
*);
149 #if !TARGET_API_MAC_CARBON
150 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16
, ControlRef
,
151 ControlDefProcMessage
, SInt32
);
152 static pascal SInt32
macctrl_sys7_default_cdef(SInt16
, ControlRef
,
153 ControlDefProcMessage
, SInt32
);
154 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16
, ControlRef
,
155 ControlDefProcMessage
, SInt32
);
158 #if !TARGET_API_MAC_CARBON
160 * This trick enables us to keep all the CDEF code in the main
161 * application, which makes life easier. For details, see
162 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
165 #pragma options align=mac68k
167 short jmpabs
; /* 4EF9 */
168 ControlDefUPP theUPP
;
170 #pragma options align=reset
173 static void macctrl_init()
175 #if !TARGET_API_MAC_CARBON
176 static int inited
= 0;
180 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_EditBox
);
181 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_editbox_cdef
);
182 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_Default
);
183 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_default_cdef
);
184 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_ListBox
);
185 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_listbox_cdef
);
191 static int macctrl_cmp_byctrl(void *av
, void *bv
)
193 union macctrl
*a
= (union macctrl
*)av
;
194 union macctrl
*b
= (union macctrl
*)bv
;
196 if (a
->generic
.ctrl
< b
->generic
.ctrl
)
198 else if (a
->generic
.ctrl
> b
->generic
.ctrl
)
204 static int macctrl_cmp_byctrl_find(void *av
, void *bv
)
206 union control
*a
= (union control
*)av
;
207 union macctrl
*b
= (union macctrl
*)bv
;
209 if (a
< b
->generic
.ctrl
)
211 else if (a
> b
->generic
.ctrl
)
217 static union control panellist
;
219 static void panellist_handler(union control
*ctrl
, void *dlg
, void *data
,
222 struct macctrls
*mcs
= dlg
;
224 if (event
== EVENT_SELCHANGE
)
225 macctrl_switchtopanel(mcs
, dlg_listbox_index(ctrl
, dlg
) + 1);
228 void macctrl_layoutbox(struct controlbox
*cb
, WindowPtr window
,
229 struct macctrls
*mcs
)
232 struct mac_layoutstate curstate
;
237 if (mac_gestalts
.apprvers
>= 0x100)
238 CreateRootControl(window
, &root
);
239 #if TARGET_API_MAC_CARBON
240 GetPortBounds(GetWindowPort(window
), &rect
);
242 rect
= window
->portRect
;
244 mcs
->window
= window
;
245 mcs
->byctrl
= newtree234(macctrl_cmp_byctrl
);
247 mcs
->defbutton
= NULL
;
248 mcs
->canbutton
= NULL
;
249 /* Count the number of panels */
251 for (i
= 1; i
< cb
->nctrlsets
; i
++)
252 if (strcmp(cb
->ctrlsets
[i
]->pathname
, cb
->ctrlsets
[i
-1]->pathname
))
254 mcs
->panels
= snewn(mcs
->npanels
, union macctrl
*);
255 memset(mcs
->panels
, 0, sizeof(*mcs
->panels
) * mcs
->npanels
);
256 curstate
.panelnum
= 0;
258 curstate
.pos
.h
= rect
.left
+ 13;
259 curstate
.pos
.v
= rect
.top
+ 13;
260 curstate
.width
= 160;
261 panellist
.listbox
.type
= CTRL_LISTBOX
;
262 panellist
.listbox
.handler
= &panellist_handler
;
263 panellist
.listbox
.height
= 20;
264 panellist
.listbox
.percentwidth
= 100;
265 macctrl_listbox(mcs
, window
, &curstate
, &panellist
);
267 curstate
.pos
.h
= rect
.left
+ 13 + 160 + 13;
268 curstate
.pos
.v
= rect
.bottom
- 33;
269 curstate
.width
= rect
.right
- (rect
.left
+ 13 + 160) - (13 * 2);
270 for (i
= 0; i
< cb
->nctrlsets
; i
++) {
271 if (i
> 0 && strcmp(cb
->ctrlsets
[i
]->pathname
,
272 cb
->ctrlsets
[i
-1]->pathname
)) {
273 curstate
.pos
.v
= rect
.top
+ 13;
275 assert(curstate
.panelnum
< mcs
->npanels
);
276 dlg_listbox_add(&panellist
, mcs
, cb
->ctrlsets
[i
]->pathname
);
278 macctrl_layoutset(&curstate
, cb
->ctrlsets
[i
], window
, mcs
);
280 macctrl_switchtopanel(mcs
, 1);
281 /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
286 static void macctrl_layoutset(struct mac_layoutstate
*curstate
,
287 struct controlset
*s
,
288 WindowPtr window
, struct macctrls
*mcs
)
290 unsigned int i
, j
, ncols
, colstart
;
291 struct mac_layoutstate cols
[MAXCOLS
];
296 for (i
= 0; i
< s
->ncontrols
; i
++) {
297 union control
*ctrl
= s
->ctrls
[i
];
299 colstart
= COLUMN_START(ctrl
->generic
.column
);
300 switch (ctrl
->generic
.type
) {
302 if (ctrl
->columns
.ncols
!= 1) {
303 ncols
= ctrl
->columns
.ncols
;
304 assert(ncols
<= MAXCOLS
);
305 for (j
= 0; j
< ncols
; j
++) {
308 cols
[j
].pos
.h
= cols
[j
-1].pos
.h
+ cols
[j
-1].width
+ 6;
310 cols
[j
].width
= curstate
->width
-
311 (cols
[j
].pos
.h
- curstate
->pos
.h
);
313 cols
[j
].width
= (curstate
->width
+ 6) *
314 ctrl
->columns
.percentages
[j
] / 100 - 6;
317 for (j
= 0; j
< ncols
; j
++)
318 if (cols
[j
].pos
.v
> cols
[0].pos
.v
)
319 cols
[0].pos
.v
= cols
[j
].pos
.v
;
320 cols
[0].width
= curstate
->width
;
325 macctrl_text(mcs
, window
, &cols
[colstart
], ctrl
);
328 macctrl_editbox(mcs
, window
, &cols
[colstart
], ctrl
);
331 macctrl_radio(mcs
, window
, &cols
[colstart
], ctrl
);
334 macctrl_checkbox(mcs
, window
, &cols
[colstart
], ctrl
);
337 macctrl_button(mcs
, window
, &cols
[colstart
], ctrl
);
340 if (ctrl
->listbox
.height
== 0)
341 macctrl_popup(mcs
, window
, &cols
[colstart
], ctrl
);
343 macctrl_listbox(mcs
, window
, &cols
[colstart
], ctrl
);
347 for (j
= 0; j
< ncols
; j
++)
348 if (cols
[j
].pos
.v
> curstate
->pos
.v
)
349 curstate
->pos
.v
= cols
[j
].pos
.v
;
352 static void macctrl_switchtopanel(struct macctrls
*mcs
, unsigned int which
)
357 #define hideshow(c) do { \
358 if (i == which) ShowControl(c); else HideControl(c); \
361 mcs
->curpanel
= which
;
362 /* Panel 0 is special and always visible. */
363 for (i
= 1; i
< mcs
->npanels
; i
++)
364 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
365 #if !TARGET_API_MAC_CARBON
366 if (mcs
->focus
== mc
)
367 macctrl_setfocus(mcs
, NULL
);
369 switch (mc
->generic
.type
) {
371 hideshow(mc
->text
.tbctrl
);
373 case MACCTRL_EDITBOX
:
374 hideshow(mc
->editbox
.tbctrl
);
375 if (mc
->editbox
.tblabel
!= NULL
)
376 hideshow(mc
->editbox
.tblabel
);
379 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
380 hideshow(mc
->radio
.tbctrls
[j
]);
381 if (mc
->radio
.tblabel
!= NULL
)
382 hideshow(mc
->radio
.tblabel
);
384 case MACCTRL_CHECKBOX
:
385 hideshow(mc
->checkbox
.tbctrl
);
388 hideshow(mc
->button
.tbctrl
);
390 case MACCTRL_LISTBOX
:
391 hideshow(mc
->listbox
.tbctrl
);
393 * At least under Mac OS 8.1, hiding a list box
394 * doesn't hide its scroll bars.
396 #if TARGET_API_MAC_CARBON
397 hideshow(GetListVerticalScrollBar(mc
->listbox
.list
));
399 hideshow((*mc
->listbox
.list
)->vScroll
);
403 hideshow(mc
->popup
.tbctrl
);
409 #if !TARGET_API_MAC_CARBON
411 * System 7 focus manipulation
413 static void macctrl_defocus(union macctrl
*mc
)
416 assert(mac_gestalts
.apprvers
< 0x100);
417 switch (mc
->generic
.type
) {
418 case MACCTRL_EDITBOX
:
419 TEDeactivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
424 static void macctrl_enfocus(union macctrl
*mc
)
427 assert(mac_gestalts
.apprvers
< 0x100);
428 switch (mc
->generic
.type
) {
429 case MACCTRL_EDITBOX
:
430 TEActivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
435 static void macctrl_setfocus(struct macctrls
*mcs
, union macctrl
*mc
)
438 if (mcs
->focus
== mc
)
440 if (mcs
->focus
!= NULL
)
441 macctrl_defocus(mcs
->focus
);
448 static void macctrl_text(struct macctrls
*mcs
, WindowPtr window
,
449 struct mac_layoutstate
*curstate
,
452 union macctrl
*mc
= snew(union macctrl
);
456 assert(ctrl
->text
.label
!= NULL
);
457 mc
->generic
.type
= MACCTRL_TEXT
;
458 mc
->generic
.ctrl
= ctrl
;
459 mc
->generic
.privdata
= NULL
;
460 bounds
.left
= curstate
->pos
.h
;
461 bounds
.right
= bounds
.left
+ curstate
->width
;
462 bounds
.top
= curstate
->pos
.v
;
463 bounds
.bottom
= bounds
.top
+ 16;
464 if (mac_gestalts
.apprvers
>= 0x100) {
467 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
468 kControlStaticTextProc
, (long)mc
);
469 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
470 kControlStaticTextTextTag
,
471 strlen(ctrl
->text
.label
), ctrl
->text
.label
);
472 GetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
473 kControlStaticTextTextHeightTag
,
474 sizeof(height
), &height
, &olen
);
476 #if !TARGET_API_MAC_CARBON
480 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
481 SYS7_TEXT_PROC
, (long)mc
);
482 te
= (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
;
483 TESetText(ctrl
->text
.label
, strlen(ctrl
->text
.label
), te
);
484 height
= TEGetHeight(1, (*te
)->nLines
, te
);
487 SizeControl(mc
->text
.tbctrl
, curstate
->width
, height
);
488 curstate
->pos
.v
+= height
+ 6;
489 add234(mcs
->byctrl
, mc
);
490 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
491 mcs
->panels
[curstate
->panelnum
] = mc
;
494 static void macctrl_editbox(struct macctrls
*mcs
, WindowPtr window
,
495 struct mac_layoutstate
*curstate
,
498 union macctrl
*mc
= snew(union macctrl
);
499 Rect lbounds
, bounds
;
501 mc
->generic
.type
= MACCTRL_EDITBOX
;
502 mc
->generic
.ctrl
= ctrl
;
503 mc
->generic
.privdata
= NULL
;
504 lbounds
.left
= curstate
->pos
.h
;
505 lbounds
.top
= curstate
->pos
.v
;
506 if (ctrl
->editbox
.percentwidth
== 100) {
507 if (ctrl
->editbox
.label
!= NULL
) {
508 lbounds
.right
= lbounds
.left
+ curstate
->width
;
509 lbounds
.bottom
= lbounds
.top
+ 16;
510 curstate
->pos
.v
+= 18;
512 bounds
.left
= curstate
->pos
.h
;
513 bounds
.right
= bounds
.left
+ curstate
->width
;
515 lbounds
.right
= lbounds
.left
+
516 curstate
->width
* (100 - ctrl
->editbox
.percentwidth
) / 100;
517 lbounds
.bottom
= lbounds
.top
+ 22;
518 bounds
.left
= lbounds
.right
;
519 bounds
.right
= lbounds
.left
+ curstate
->width
;
521 bounds
.top
= curstate
->pos
.v
;
522 bounds
.bottom
= bounds
.top
+ 22;
523 if (mac_gestalts
.apprvers
>= 0x100) {
524 if (ctrl
->editbox
.label
== NULL
)
525 mc
->editbox
.tblabel
= NULL
;
527 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
528 0, 0, 0, kControlStaticTextProc
,
530 SetControlData(mc
->editbox
.tblabel
, kControlEntireControl
,
531 kControlStaticTextTextTag
,
532 strlen(ctrl
->editbox
.label
), ctrl
->editbox
.label
);
534 InsetRect(&bounds
, 3, 3);
535 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
536 ctrl
->editbox
.password ?
537 kControlEditTextPasswordProc
:
538 kControlEditTextProc
, (long)mc
);
540 #if !TARGET_API_MAC_CARBON
542 if (ctrl
->editbox
.label
== NULL
)
543 mc
->editbox
.tblabel
= NULL
;
545 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
546 0, 0, 0, SYS7_TEXT_PROC
,
548 TESetText(ctrl
->editbox
.label
, strlen(ctrl
->editbox
.label
),
549 (TEHandle
)(*mc
->editbox
.tblabel
)->contrlData
);
551 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
552 SYS7_EDITBOX_PROC
, (long)mc
);
555 curstate
->pos
.v
+= 28;
556 add234(mcs
->byctrl
, mc
);
557 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
558 mcs
->panels
[curstate
->panelnum
] = mc
;
559 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
562 #if !TARGET_API_MAC_CARBON
563 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16 variant
,
565 ControlDefProcMessage msg
,
576 rect
= (*control
)->contrlRect
;
577 if (variant
== SYS7_EDITBOX_VARIANT
)
578 InsetRect(&rect
, 3, 3); /* 2 if it's 20 pixels high */
579 te
= TENew(&rect
, &rect
);
580 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
581 (*te
)->txSize
= LoWord(ssfs
);
582 (*te
)->txFont
= HiWord(ssfs
);
583 (*control
)->contrlData
= (Handle
)te
;
586 TEDispose((TEHandle
)(*control
)->contrlData
);
589 if ((*control
)->contrlVis
) {
590 rect
= (*control
)->contrlRect
;
591 if (variant
== SYS7_EDITBOX_VARIANT
) {
594 InsetRect(&rect
, 3, 3);
597 (*(TEHandle
)(*control
)->contrlData
)->viewRect
= rect
;
598 TEUpdate(&rect
, (TEHandle
)(*control
)->contrlData
);
602 if (variant
== SYS7_TEXT_VARIANT
)
603 return kControlNoPart
;
604 mouse
.h
= LoWord(param
);
605 mouse
.v
= HiWord(param
);
606 rect
= (*control
)->contrlRect
;
607 InsetRect(&rect
, 3, 3);
608 return PtInRect(mouse
, &rect
) ? kControlEditTextPart
: kControlNoPart
;
610 if (param
& (1 << 31)) {
616 rgn
= (RgnHandle
)param
;
617 RectRgn(rgn
, &(*control
)->contrlRect
);
621 rgn
= (RgnHandle
)param
;
630 static void macctrl_radio(struct macctrls
*mcs
, WindowPtr window
,
631 struct mac_layoutstate
*curstate
,
634 union macctrl
*mc
= snew(union macctrl
);
637 unsigned int i
, colwidth
;
639 mc
->generic
.type
= MACCTRL_RADIO
;
640 mc
->generic
.ctrl
= ctrl
;
641 mc
->generic
.privdata
= NULL
;
642 mc
->radio
.tbctrls
= snewn(ctrl
->radio
.nbuttons
, ControlRef
);
643 colwidth
= (curstate
->width
+ 13) / ctrl
->radio
.ncolumns
;
644 bounds
.top
= curstate
->pos
.v
;
645 bounds
.bottom
= bounds
.top
+ 16;
646 bounds
.left
= curstate
->pos
.h
;
647 bounds
.right
= bounds
.left
+ curstate
->width
;
648 if (ctrl
->radio
.label
== NULL
)
649 mc
->radio
.tblabel
= NULL
;
651 if (mac_gestalts
.apprvers
>= 0x100) {
652 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
653 0, 0, 0, kControlStaticTextProc
,
655 SetControlData(mc
->radio
.tblabel
, kControlEntireControl
,
656 kControlStaticTextTextTag
,
657 strlen(ctrl
->radio
.label
), ctrl
->radio
.label
);
659 #if !TARGET_API_MAC_CARBON
661 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
662 0, 0, 0, SYS7_TEXT_PROC
, (long)mc
);
663 TESetText(ctrl
->radio
.label
, strlen(ctrl
->radio
.label
),
664 (TEHandle
)(*mc
->radio
.tblabel
)->contrlData
);
667 curstate
->pos
.v
+= 18;
669 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
670 bounds
.top
= curstate
->pos
.v
- 2;
671 bounds
.bottom
= bounds
.top
+ 18;
672 bounds
.left
= curstate
->pos
.h
+ colwidth
* (i
% ctrl
->radio
.ncolumns
);
673 if (i
== ctrl
->radio
.nbuttons
- 1 ||
674 i
% ctrl
->radio
.ncolumns
== ctrl
->radio
.ncolumns
- 1) {
675 bounds
.right
= curstate
->pos
.h
+ curstate
->width
;
676 curstate
->pos
.v
+= 18;
678 bounds
.right
= bounds
.left
+ colwidth
- 13;
679 c2pstrcpy(title
, ctrl
->radio
.buttons
[i
]);
680 mc
->radio
.tbctrls
[i
] = NewControl(window
, &bounds
, title
, TRUE
,
681 0, 0, 1, radioButProc
, (long)mc
);
683 curstate
->pos
.v
+= 4;
684 add234(mcs
->byctrl
, mc
);
685 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
686 mcs
->panels
[curstate
->panelnum
] = mc
;
687 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
690 static void macctrl_checkbox(struct macctrls
*mcs
, WindowPtr window
,
691 struct mac_layoutstate
*curstate
,
694 union macctrl
*mc
= snew(union macctrl
);
698 assert(ctrl
->checkbox
.label
!= NULL
);
699 mc
->generic
.type
= MACCTRL_CHECKBOX
;
700 mc
->generic
.ctrl
= ctrl
;
701 mc
->generic
.privdata
= NULL
;
702 bounds
.left
= curstate
->pos
.h
;
703 bounds
.right
= bounds
.left
+ curstate
->width
;
704 bounds
.top
= curstate
->pos
.v
;
705 bounds
.bottom
= bounds
.top
+ 16;
706 c2pstrcpy(title
, ctrl
->checkbox
.label
);
707 mc
->checkbox
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
708 checkBoxProc
, (long)mc
);
709 add234(mcs
->byctrl
, mc
);
710 curstate
->pos
.v
+= 22;
711 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
712 mcs
->panels
[curstate
->panelnum
] = mc
;
713 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
716 static void macctrl_button(struct macctrls
*mcs
, WindowPtr window
,
717 struct mac_layoutstate
*curstate
,
720 union macctrl
*mc
= snew(union macctrl
);
724 assert(ctrl
->button
.label
!= NULL
);
725 mc
->generic
.type
= MACCTRL_BUTTON
;
726 mc
->generic
.ctrl
= ctrl
;
727 mc
->generic
.privdata
= NULL
;
728 bounds
.left
= curstate
->pos
.h
;
729 bounds
.right
= bounds
.left
+ curstate
->width
;
730 bounds
.top
= curstate
->pos
.v
;
731 bounds
.bottom
= bounds
.top
+ 20;
732 c2pstrcpy(title
, ctrl
->button
.label
);
733 mc
->button
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
734 pushButProc
, (long)mc
);
735 mc
->button
.tbring
= NULL
;
736 if (mac_gestalts
.apprvers
>= 0x100) {
737 Boolean isdefault
= ctrl
->button
.isdefault
;
739 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
740 kControlPushButtonDefaultTag
,
741 sizeof(isdefault
), &isdefault
);
742 } else if (ctrl
->button
.isdefault
) {
743 InsetRect(&bounds
, -4, -4);
744 mc
->button
.tbring
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
745 SYS7_DEFAULT_PROC
, (long)mc
);
747 if (mac_gestalts
.apprvers
>= 0x110) {
748 Boolean iscancel
= ctrl
->button
.iscancel
;
750 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
751 kControlPushButtonCancelTag
,
752 sizeof(iscancel
), &iscancel
);
754 if (ctrl
->button
.isdefault
)
756 if (ctrl
->button
.iscancel
)
758 add234(mcs
->byctrl
, mc
);
759 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
760 mcs
->panels
[curstate
->panelnum
] = mc
;
761 curstate
->pos
.v
+= 26;
764 #if !TARGET_API_MAC_CARBON
765 static pascal SInt32
macctrl_sys7_default_cdef(SInt16 variant
,
767 ControlDefProcMessage msg
,
777 if ((*control
)->contrlVis
) {
778 rect
= (*control
)->contrlRect
;
779 GetPenState(&savestate
);
782 if ((*control
)->contrlHilite
== kControlInactivePart
)
784 oval
= (rect
.bottom
- rect
.top
) / 2 + 2;
785 FrameRoundRect(&rect
, oval
, oval
);
786 SetPenState(&savestate
);
790 if (param
& (1 << 31)) {
796 rgn
= (RgnHandle
)param
;
797 RectRgn(rgn
, &(*control
)->contrlRect
);
801 rgn
= (RgnHandle
)param
;
810 static void macctrl_listbox(struct macctrls
*mcs
, WindowPtr window
,
811 struct mac_layoutstate
*curstate
,
814 union macctrl
*mc
= snew(union macctrl
);
818 assert(ctrl
->listbox
.percentwidth
== 100);
819 mc
->generic
.type
= MACCTRL_LISTBOX
;
820 mc
->generic
.ctrl
= ctrl
;
821 mc
->generic
.privdata
= NULL
;
822 /* The list starts off empty */
823 mc
->listbox
.nids
= 0;
824 mc
->listbox
.ids
= NULL
;
825 bounds
.left
= curstate
->pos
.h
;
826 bounds
.right
= bounds
.left
+ curstate
->width
;
827 bounds
.top
= curstate
->pos
.v
;
828 bounds
.bottom
= bounds
.top
+ 16 * ctrl
->listbox
.height
+ 2;
830 if (mac_gestalts
.apprvers
>= 0x100) {
831 InsetRect(&bounds
, 3, 3);
832 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
834 kControlListBoxProc
, (long)mc
);
835 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
836 kControlListBoxListHandleTag
,
837 sizeof(mc
->listbox
.list
), &mc
->listbox
.list
,
839 DisposeControl(mc
->listbox
.tbctrl
);
844 #if !TARGET_API_MAC_CARBON
846 InsetRect(&bounds
, -3, -3);
847 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
849 SYS7_LISTBOX_PROC
, (long)mc
);
850 mc
->listbox
.list
= (ListHandle
)(*mc
->listbox
.tbctrl
)->contrlData
;
851 (*mc
->listbox
.list
)->refCon
= (long)mc
;
854 if (!ctrl
->listbox
.multisel
) {
855 #if TARGET_API_MAC_CARBON
856 SetListSelectionFlags(mc
->listbox
.list
, lOnlyOne
);
858 (*mc
->listbox
.list
)->selFlags
= lOnlyOne
;
861 add234(mcs
->byctrl
, mc
);
862 curstate
->pos
.v
+= 6 + 16 * ctrl
->listbox
.height
+ 2;
863 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
864 mcs
->panels
[curstate
->panelnum
] = mc
;
865 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
868 #if !TARGET_API_MAC_CARBON
869 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16 variant
,
871 ControlDefProcMessage msg
,
887 rect
= (*control
)->contrlRect
;
888 InsetRect(&rect
, 4, 4);
889 rect
.right
-= 15; /* scroll bar */
890 bounds
.top
= bounds
.bottom
= bounds
.left
= 0;
892 csize
.h
= csize
.v
= 0;
894 savefont
= curport
->txFont
;
895 savesize
= curport
->txSize
;
896 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
897 TextFont(HiWord(ssfs
));
898 TextSize(LoWord(ssfs
));
899 list
= LNew(&rect
, &bounds
, csize
, 0, (*control
)->contrlOwner
,
900 TRUE
, FALSE
, FALSE
, TRUE
);
901 SetControlReference((*list
)->vScroll
, (long)list
);
902 (*control
)->contrlData
= (Handle
)list
;
908 * If the dialogue box is being destroyed, the scroll bar
909 * might have gone already. In our situation, this is the
910 * only time we destroy a control, so NULL out the scroll bar
911 * handle to prevent LDispose trying to free it.
913 list
= (ListHandle
)(*control
)->contrlData
;
914 (*list
)->vScroll
= NULL
;
918 if ((*control
)->contrlVis
) {
919 rect
= (*control
)->contrlRect
;
920 /* XXX input focus highlighting? */
921 InsetRect(&rect
, 3, 3);
924 list
= (ListHandle
)(*control
)->contrlData
;
925 LActivate((*control
)->contrlHilite
!= kControlInactivePart
, list
);
927 LUpdate(curport
->visRgn
, list
);
931 mouse
.h
= LoWord(param
);
932 mouse
.v
= HiWord(param
);
933 rect
= (*control
)->contrlRect
;
934 InsetRect(&rect
, 4, 4);
936 * We deliberately exclude the scrollbar so that LClick() can see it.
939 return PtInRect(mouse
, &rect
) ? kControlListBoxPart
: kControlNoPart
;
941 if (param
& (1 << 31)) {
947 rgn
= (RgnHandle
)param
;
948 RectRgn(rgn
, &(*control
)->contrlRect
);
952 rgn
= (RgnHandle
)param
;
961 static void macctrl_popup(struct macctrls
*mcs
, WindowPtr window
,
962 struct mac_layoutstate
*curstate
,
965 union macctrl
*mc
= snew(union macctrl
);
968 unsigned int labelwidth
;
969 static int nextmenuid
= MENU_MIN
;
974 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
975 * create a popup menu with dynamic content.
977 assert(ctrl
->listbox
.height
== 0);
978 assert(!ctrl
->listbox
.draglist
);
979 assert(!ctrl
->listbox
.multisel
);
981 mc
->generic
.type
= MACCTRL_POPUP
;
982 mc
->generic
.ctrl
= ctrl
;
983 mc
->generic
.privdata
= NULL
;
984 c2pstrcpy(title
, ctrl
->button
.label
== NULL ?
"" : ctrl
->button
.label
);
986 /* Find a spare menu ID and create the menu */
987 while (GetMenuHandle(nextmenuid
) != NULL
)
988 if (++nextmenuid
>= MENU_MAX
) nextmenuid
= MENU_MIN
;
989 menuid
= nextmenuid
++;
990 menu
= NewMenu(menuid
, "\pdummy");
991 if (menu
== NULL
) return;
992 mc
->popup
.menu
= menu
;
993 mc
->popup
.menuid
= menuid
;
994 InsertMenu(menu
, kInsertHierarchicalMenu
);
996 /* The menu starts off empty */
998 mc
->popup
.ids
= NULL
;
1000 bounds
.left
= curstate
->pos
.h
;
1001 bounds
.right
= bounds
.left
+ curstate
->width
;
1002 bounds
.top
= curstate
->pos
.v
;
1003 bounds
.bottom
= bounds
.top
+ 20;
1004 /* XXX handle percentwidth == 100 */
1005 labelwidth
= curstate
->width
* (100 - ctrl
->listbox
.percentwidth
) / 100;
1006 mc
->popup
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
,
1007 popupTitleLeftJust
, menuid
, labelwidth
,
1008 popupMenuProc
+ popupFixedWidth
, (long)mc
);
1009 add234(mcs
->byctrl
, mc
);
1010 curstate
->pos
.v
+= 26;
1011 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
1012 mcs
->panels
[curstate
->panelnum
] = mc
;
1013 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1017 void macctrl_activate(WindowPtr window
, EventRecord
*event
)
1019 struct macctrls
*mcs
= mac_winctrls(window
);
1020 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1023 ControlPartCode state
;
1027 SetPort((GrafPtr
)GetWindowPort(window
));
1028 if (mac_gestalts
.apprvers
>= 0x100)
1029 SetThemeWindowBackground(window
, active ?
1030 kThemeBrushModelessDialogBackgroundActive
:
1031 kThemeBrushModelessDialogBackgroundInactive
,
1033 state
= active ? kControlNoPart
: kControlInactivePart
;
1034 for (i
= 0; i
<= mcs
->curpanel
; i
+= mcs
->curpanel
)
1035 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
1036 switch (mc
->generic
.type
) {
1038 HiliteControl(mc
->text
.tbctrl
, state
);
1040 case MACCTRL_EDITBOX
:
1041 HiliteControl(mc
->editbox
.tbctrl
, state
);
1042 if (mc
->editbox
.tblabel
!= NULL
)
1043 HiliteControl(mc
->editbox
.tblabel
, state
);
1046 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
1047 HiliteControl(mc
->radio
.tbctrls
[j
], state
);
1048 if (mc
->radio
.tblabel
!= NULL
)
1049 HiliteControl(mc
->radio
.tblabel
, state
);
1051 case MACCTRL_CHECKBOX
:
1052 HiliteControl(mc
->checkbox
.tbctrl
, state
);
1054 case MACCTRL_BUTTON
:
1055 HiliteControl(mc
->button
.tbctrl
, state
);
1056 if (mc
->button
.tbring
!= NULL
)
1057 HiliteControl(mc
->button
.tbring
, state
);
1059 case MACCTRL_LISTBOX
:
1060 HiliteControl(mc
->listbox
.tbctrl
, state
);
1063 HiliteControl(mc
->popup
.tbctrl
, state
);
1066 #if !TARGET_API_MAC_CARBON
1067 if (mcs
->focus
== mc
) {
1069 macctrl_enfocus(mc
);
1071 macctrl_defocus(mc
);
1078 void macctrl_click(WindowPtr window
, EventRecord
*event
)
1081 ControlHandle control
, oldfocus
;
1082 int part
, trackresult
;
1085 struct macctrls
*mcs
= mac_winctrls(window
);
1090 SetPort((GrafPtr
)GetWindowPort(window
));
1091 mouse
= event
->where
;
1092 GlobalToLocal(&mouse
);
1093 part
= FindControl(mouse
, window
, &control
);
1094 if (control
!= NULL
) {
1095 #if !TARGET_API_MAC_CARBON
1097 * Special magic for scroll bars in list boxes, whose refcon
1100 if (part
== kControlUpButtonPart
|| part
== kControlDownButtonPart
||
1101 part
== kControlPageUpPart
|| part
== kControlPageDownPart
||
1102 part
== kControlIndicatorPart
)
1103 mc
= (union macctrl
*)
1104 (*(ListHandle
)GetControlReference(control
))->refCon
;
1107 mc
= (union macctrl
*)GetControlReference(control
);
1108 if (mac_gestalts
.apprvers
>= 0x100) {
1109 if (GetControlFeatures(control
, &features
) == noErr
&&
1110 (features
& kControlSupportsFocus
) &&
1111 (features
& kControlGetsFocusOnClick
) &&
1112 GetKeyboardFocus(window
, &oldfocus
) == noErr
&&
1113 control
!= oldfocus
)
1114 SetKeyboardFocus(window
, control
, part
);
1115 trackresult
= HandleControlClick(control
, mouse
, event
->modifiers
,
1116 (ControlActionUPP
)-1);
1118 #if !TARGET_API_MAC_CARBON
1119 if (mc
->generic
.type
== MACCTRL_EDITBOX
&&
1120 control
== mc
->editbox
.tbctrl
) {
1121 TEHandle te
= (TEHandle
)(*control
)->contrlData
;
1123 macctrl_setfocus(mcs
, mc
);
1124 TEClick(mouse
, !!(event
->modifiers
& shiftKey
), te
);
1127 if (mc
->generic
.type
== MACCTRL_LISTBOX
&&
1128 (control
== mc
->listbox
.tbctrl
||
1129 control
== (*mc
->listbox
.list
)->vScroll
)) {
1131 macctrl_setfocus(mcs
, mc
);
1132 if (LClick(mouse
, event
->modifiers
, mc
->listbox
.list
))
1134 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1136 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1140 trackresult
= TrackControl(control
, mouse
, (ControlActionUPP
)-1);
1142 switch (mc
->generic
.type
) {
1144 if (trackresult
!= 0) {
1145 for (i
= 0; i
< mc
->generic
.ctrl
->radio
.nbuttons
; i
++)
1146 if (mc
->radio
.tbctrls
[i
] == control
)
1147 SetControlValue(mc
->radio
.tbctrls
[i
],
1148 kControlRadioButtonCheckedValue
);
1150 SetControlValue(mc
->radio
.tbctrls
[i
],
1151 kControlRadioButtonUncheckedValue
);
1152 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1155 case MACCTRL_CHECKBOX
:
1156 if (trackresult
!= 0) {
1157 SetControlValue(control
, !GetControlValue(control
));
1158 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1161 case MACCTRL_BUTTON
:
1162 if (trackresult
!= 0)
1163 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1165 case MACCTRL_LISTBOX
:
1166 /* FIXME spot double-click */
1167 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1170 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1178 void macctrl_key(WindowPtr window
, EventRecord
*event
)
1181 struct macctrls
*mcs
= mac_winctrls(window
);
1183 unsigned long dummy
;
1185 switch (event
->message
& charCodeMask
) {
1186 case kEnterCharCode
:
1187 case kReturnCharCode
:
1188 if (mcs
->defbutton
!= NULL
) {
1189 assert(mcs
->defbutton
->generic
.type
== MACCTRL_BUTTON
);
1190 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlButtonPart
);
1192 * I'd like to delay unhilighting the button until after
1193 * the event has been processed, but by them the entire
1194 * dialgue box might have been destroyed.
1197 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1198 ctrlevent(mcs
, mcs
->defbutton
, EVENT_ACTION
);
1201 case kEscapeCharCode
:
1202 if (mcs
->canbutton
!= NULL
) {
1203 assert(mcs
->canbutton
->generic
.type
== MACCTRL_BUTTON
);
1204 HiliteControl(mcs
->canbutton
->button
.tbctrl
, kControlButtonPart
);
1206 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1207 ctrlevent(mcs
, mcs
->canbutton
, EVENT_ACTION
);
1211 if (mac_gestalts
.apprvers
>= 0x100) {
1212 if (GetKeyboardFocus(window
, &control
) == noErr
&& control
!= NULL
) {
1213 HandleControlKey(control
, (event
->message
& keyCodeMask
) >> 8,
1214 event
->message
& charCodeMask
, event
->modifiers
);
1215 mc
= (union macctrl
*)GetControlReference(control
);
1216 switch (mc
->generic
.type
) {
1217 case MACCTRL_LISTBOX
:
1218 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1221 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1226 #if !TARGET_API_MAC_CARBON
1230 if (mcs
->focus
!= NULL
) {
1232 switch (mc
->generic
.type
) {
1233 case MACCTRL_EDITBOX
:
1234 te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1235 TEKey(event
->message
& charCodeMask
, te
);
1236 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1244 void macctrl_update(WindowPtr window
)
1246 #if TARGET_API_MAC_CARBON
1252 BeginUpdate(window
);
1254 SetPort((GrafPtr
)GetWindowPort(window
));
1255 if (mac_gestalts
.apprvers
>= 0x101) {
1256 #if TARGET_API_MAC_CARBON
1257 GetPortBounds(GetWindowPort(window
), &rect
);
1259 rect
= window
->portRect
;
1261 InsetRect(&rect
, -1, -1);
1262 DrawThemeModelessDialogFrame(&rect
, mac_frontwindow() == window ?
1263 kThemeStateActive
: kThemeStateInactive
);
1265 #if TARGET_API_MAC_CARBON
1267 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1268 UpdateControls(window
, visrgn
);
1271 UpdateControls(window
, window
->visRgn
);
1277 #if TARGET_API_MAC_CARBON
1278 #define EnableItem EnableMenuItem
1279 #define DisableItem DisableMenuItem
1281 void macctrl_adjustmenus(WindowPtr window
)
1285 menu
= GetMenuHandle(mFile
);
1286 DisableItem(menu
, iSave
); /* XXX enable if modified */
1287 EnableItem(menu
, iSaveAs
);
1288 EnableItem(menu
, iDuplicate
);
1290 menu
= GetMenuHandle(mEdit
);
1291 DisableItem(menu
, 0);
1294 void macctrl_close(WindowPtr window
)
1296 struct macctrls
*mcs
= mac_winctrls(window
);
1300 * Mostly, we don't bother disposing of the Toolbox controls,
1301 * since that will happen automatically when the window is
1302 * disposed of. Popup menus are an exception, because we have to
1303 * dispose of the menu ourselves, and doing that while the control
1304 * still holds a reference to it seems rude.
1306 while ((mc
= index234(mcs
->byctrl
, 0)) != NULL
) {
1307 if (mc
->generic
.privdata
!= NULL
&& mc
->generic
.freeprivdata
)
1308 sfree(mc
->generic
.privdata
);
1309 switch (mc
->generic
.type
) {
1311 DisposeControl(mc
->popup
.tbctrl
);
1312 DeleteMenu(mc
->popup
.menuid
);
1313 DisposeMenu(mc
->popup
.menu
);
1316 del234(mcs
->byctrl
, mc
);
1320 freetree234(mcs
->byctrl
);
1326 void dlg_update_start(union control
*ctrl
, void *dlg
)
1332 void dlg_update_done(union control
*ctrl
, void *dlg
)
1338 void dlg_set_focus(union control
*ctrl
, void *dlg
)
1341 if (mac_gestalts
.apprvers
>= 0x100) {
1342 /* Use SetKeyboardFocus() */
1344 /* Do our own mucking around */
1348 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1354 void dlg_beep(void *dlg
)
1360 void dlg_error_msg(void *dlg
, char *msg
)
1364 c2pstrcpy(pmsg
, msg
);
1365 ParamText(pmsg
, NULL
, NULL
, NULL
);
1366 StopAlert(128, NULL
);
1369 void dlg_end(void *dlg
, int value
)
1371 struct macctrls
*mcs
= dlg
;
1373 if (mcs
->end
!= NULL
)
1374 (*mcs
->end
)(mcs
->window
, value
);
1377 void dlg_refresh(union control
*ctrl
, void *dlg
)
1379 struct macctrls
*mcs
= dlg
;
1384 mc
= findbyctrl(mcs
, ctrl
);
1386 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1389 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
1391 struct macctrls
*mcs
= dlg
;
1392 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1395 return mc
->generic
.privdata
;
1398 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
1400 struct macctrls
*mcs
= dlg
;
1401 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1404 mc
->generic
.privdata
= ptr
;
1405 mc
->generic
.freeprivdata
= FALSE
;
1408 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
1410 struct macctrls
*mcs
= dlg
;
1411 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1414 mc
->generic
.privdata
= smalloc(size
);
1415 mc
->generic
.freeprivdata
= TRUE
;
1416 return mc
->generic
.privdata
;
1421 * Radio Button control
1424 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
1426 struct macctrls
*mcs
= dlg
;
1427 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1430 if (mc
== NULL
) return;
1431 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1432 if (i
== whichbutton
)
1433 SetControlValue(mc
->radio
.tbctrls
[i
],
1434 kControlRadioButtonCheckedValue
);
1436 SetControlValue(mc
->radio
.tbctrls
[i
],
1437 kControlRadioButtonUncheckedValue
);
1442 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
1444 struct macctrls
*mcs
= dlg
;
1445 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1449 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1450 if (GetControlValue(mc
->radio
.tbctrls
[i
]) ==
1451 kControlRadioButtonCheckedValue
)
1462 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
1464 struct macctrls
*mcs
= dlg
;
1465 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1467 if (mc
== NULL
) return;
1468 SetControlValue(mc
->checkbox
.tbctrl
,
1469 checked ? kControlCheckBoxCheckedValue
:
1470 kControlCheckBoxUncheckedValue
);
1473 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
1475 struct macctrls
*mcs
= dlg
;
1476 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1479 return GetControlValue(mc
->checkbox
.tbctrl
);
1487 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
1489 struct macctrls
*mcs
= dlg
;
1490 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1493 if (mc
== NULL
) return;
1494 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1496 SetPort((GrafPtr
)(GetWindowPort(mcs
->window
)));
1497 if (mac_gestalts
.apprvers
>= 0x100)
1498 SetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1499 ctrl
->editbox
.password ?
1500 kControlEditTextPasswordTag
:
1501 kControlEditTextTextTag
,
1502 strlen(text
), text
);
1503 #if !TARGET_API_MAC_CARBON
1505 TESetText(text
, strlen(text
),
1506 (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
1508 DrawOneControl(mc
->editbox
.tbctrl
);
1512 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
1514 struct macctrls
*mcs
= dlg
;
1515 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1519 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1520 if (mac_gestalts
.apprvers
>= 0x100) {
1521 if (GetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1522 ctrl
->editbox
.password ?
1523 kControlEditTextPasswordTag
:
1524 kControlEditTextTextTag
,
1525 length
- 1, buffer
, &olen
) != noErr
)
1527 if (olen
> length
- 1)
1530 #if !TARGET_API_MAC_CARBON
1532 TEHandle te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1534 olen
= (*te
)->teLength
;
1535 if (olen
> length
- 1)
1537 memcpy(buffer
, *(*te
)->hText
, olen
);
1540 buffer
[olen
] = '\0';
1548 static void dlg_macpopup_clear(union control
*ctrl
, void *dlg
)
1550 struct macctrls
*mcs
= dlg
;
1551 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1552 MenuRef menu
= mc
->popup
.menu
;
1555 if (mc
== NULL
) return;
1556 n
= CountMenuItems(menu
);
1557 for (i
= 0; i
< n
; i
++)
1558 DeleteMenuItem(menu
, n
- i
);
1560 sfree(mc
->popup
.ids
);
1561 mc
->popup
.ids
= NULL
;
1562 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1565 static void dlg_maclist_clear(union control
*ctrl
, void *dlg
)
1567 struct macctrls
*mcs
= dlg
;
1568 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1570 if (mc
== NULL
) return;
1571 LDelRow(0, 0, mc
->listbox
.list
);
1572 mc
->listbox
.nids
= 0;
1573 sfree(mc
->listbox
.ids
);
1574 mc
->listbox
.ids
= NULL
;
1575 DrawOneControl(mc
->listbox
.tbctrl
);
1578 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
1581 switch (ctrl
->generic
.type
) {
1583 if (ctrl
->listbox
.height
== 0)
1584 dlg_macpopup_clear(ctrl
, dlg
);
1586 dlg_maclist_clear(ctrl
, dlg
);
1591 static void dlg_macpopup_del(union control
*ctrl
, void *dlg
, int index
)
1593 struct macctrls
*mcs
= dlg
;
1594 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1595 MenuRef menu
= mc
->popup
.menu
;
1597 if (mc
== NULL
) return;
1598 DeleteMenuItem(menu
, index
+ 1);
1599 if (mc
->popup
.ids
!= NULL
)
1600 memcpy(mc
->popup
.ids
+ index
, mc
->popup
.ids
+ index
+ 1,
1601 (mc
->popup
.nids
- index
- 1) * sizeof(*mc
->popup
.ids
));
1602 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1605 static void dlg_maclist_del(union control
*ctrl
, void *dlg
, int index
)
1607 struct macctrls
*mcs
= dlg
;
1608 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1610 if (mc
== NULL
) return;
1611 LDelRow(1, index
, mc
->listbox
.list
);
1612 if (mc
->listbox
.ids
!= NULL
)
1613 memcpy(mc
->listbox
.ids
+ index
, mc
->listbox
.ids
+ index
+ 1,
1614 (mc
->listbox
.nids
- index
- 1) * sizeof(*mc
->listbox
.ids
));
1615 DrawOneControl(mc
->listbox
.tbctrl
);
1618 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
1621 switch (ctrl
->generic
.type
) {
1623 if (ctrl
->listbox
.height
== 0)
1624 dlg_macpopup_del(ctrl
, dlg
, index
);
1626 dlg_maclist_del(ctrl
, dlg
, index
);
1631 static void dlg_macpopup_add(union control
*ctrl
, void *dlg
, char const *text
)
1633 struct macctrls
*mcs
= dlg
;
1634 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1635 MenuRef menu
= mc
->popup
.menu
;
1638 if (mc
== NULL
) return;
1639 assert(text
[0] != '\0');
1640 c2pstrcpy(itemstring
, text
);
1641 AppendMenu(menu
, "\pdummy");
1642 SetMenuItemText(menu
, CountMenuItems(menu
), itemstring
);
1643 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1647 static void dlg_maclist_add(union control
*ctrl
, void *dlg
, char const *text
)
1649 struct macctrls
*mcs
= dlg
;
1650 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1652 Cell cell
= { 0, 0 };
1654 if (mc
== NULL
) return;
1655 #if TARGET_API_MAC_CARBON
1656 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1658 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1660 cell
.v
= bounds
.bottom
;
1661 LAddRow(1, cell
.v
, mc
->listbox
.list
);
1662 LSetCell(text
, strlen(text
), cell
, mc
->listbox
.list
);
1663 DrawOneControl(mc
->listbox
.tbctrl
);
1666 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
1669 switch (ctrl
->generic
.type
) {
1671 if (ctrl
->listbox
.height
== 0)
1672 dlg_macpopup_add(ctrl
, dlg
, text
);
1674 dlg_maclist_add(ctrl
, dlg
, text
);
1679 static void dlg_macpopup_addwithid(union control
*ctrl
, void *dlg
,
1680 char const *text
, int id
)
1682 struct macctrls
*mcs
= dlg
;
1683 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1684 MenuRef menu
= mc
->popup
.menu
;
1687 if (mc
== NULL
) return;
1688 dlg_macpopup_add(ctrl
, dlg
, text
);
1689 index
= CountMenuItems(menu
) - 1;
1690 if (mc
->popup
.nids
<= index
) {
1691 mc
->popup
.nids
= index
+ 1;
1692 mc
->popup
.ids
= sresize(mc
->popup
.ids
, mc
->popup
.nids
, int);
1694 mc
->popup
.ids
[index
] = id
;
1697 static void dlg_maclist_addwithid(union control
*ctrl
, void *dlg
,
1698 char const *text
, int id
)
1700 struct macctrls
*mcs
= dlg
;
1701 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1705 if (mc
== NULL
) return;
1706 dlg_maclist_add(ctrl
, dlg
, text
);
1707 #if TARGET_API_MAC_CARBON
1708 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1710 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1712 index
= bounds
.bottom
;
1713 if (mc
->listbox
.nids
<= index
) {
1714 mc
->listbox
.nids
= index
+ 1;
1715 mc
->listbox
.ids
= sresize(mc
->listbox
.ids
, mc
->listbox
.nids
, int);
1717 mc
->listbox
.ids
[index
] = id
;
1720 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
1721 char const *text
, int id
)
1724 switch (ctrl
->generic
.type
) {
1726 if (ctrl
->listbox
.height
== 0)
1727 dlg_macpopup_addwithid(ctrl
, dlg
, text
, id
);
1729 dlg_maclist_addwithid(ctrl
, dlg
, text
, id
);
1734 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
1736 struct macctrls
*mcs
= dlg
;
1737 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1740 switch (ctrl
->generic
.type
) {
1742 if (ctrl
->listbox
.height
== 0) {
1743 assert(mc
->popup
.ids
!= NULL
&& mc
->popup
.nids
> index
);
1744 return mc
->popup
.ids
[index
];
1746 assert(mc
->listbox
.ids
!= NULL
&& mc
->listbox
.nids
> index
);
1747 return mc
->listbox
.ids
[index
];
1753 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
1755 struct macctrls
*mcs
= dlg
;
1756 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1757 Cell cell
= { 0, 0 };
1760 switch (ctrl
->generic
.type
) {
1762 if (ctrl
->listbox
.height
== 0)
1763 return GetControlValue(mc
->popup
.tbctrl
) - 1;
1765 if (LGetSelect(TRUE
, &cell
, mc
->listbox
.list
))
1774 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
1776 struct macctrls
*mcs
= dlg
;
1777 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1778 Cell cell
= { 0, 0 };
1781 switch (ctrl
->generic
.type
) {
1783 if (ctrl
->listbox
.height
== 0)
1784 return GetControlValue(mc
->popup
.tbctrl
) - 1 == index
;
1787 return LGetSelect(FALSE
, &cell
, mc
->listbox
.list
);
1793 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
1795 struct macctrls
*mcs
= dlg
;
1796 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1798 if (mc
== NULL
) return;
1799 switch (ctrl
->generic
.type
) {
1801 if (ctrl
->listbox
.height
== 0)
1802 SetControlValue(mc
->popup
.tbctrl
, index
+ 1);
1812 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
1814 struct macctrls
*mcs
= dlg
;
1815 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1817 if (mc
== NULL
) return;
1818 if (mac_gestalts
.apprvers
>= 0x100)
1819 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
1820 kControlStaticTextTextTag
, strlen(text
), text
);
1821 #if !TARGET_API_MAC_CARBON
1823 TESetText(text
, strlen(text
),
1824 (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
);
1830 * File Selector control
1833 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
1838 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
1845 * Font Selector control
1848 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fn
)
1853 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fn
)
1860 * Printer enumeration
1863 printer_enum
*printer_start_enum(int *nprinters
)
1870 char *printer_get_name(printer_enum
*pe
, int thing
)
1876 void printer_finish_enum(printer_enum
*pe
)
1883 * Colour selection stuff
1886 void dlg_coloursel_start(union control
*ctrl
, void *dlg
,
1887 int r
, int g
, int b
)
1889 struct macctrls
*mcs
= dlg
;
1890 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1891 Point where
= {-1, -1}; /* Screen with greatest colour depth */
1894 if (HAVE_COLOR_QD()) {
1895 incolour
.red
= r
* 0x0101;
1896 incolour
.green
= g
* 0x0101;
1897 incolour
.blue
= b
* 0x0101;
1898 mcs
->gotcolour
= GetColor(where
, "\pModify Colour:", &incolour
,
1900 ctrlevent(mcs
, mc
, EVENT_CALLBACK
);
1905 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1906 int *r
, int *g
, int *b
)
1908 struct macctrls
*mcs
= dlg
;
1910 if (mcs
->gotcolour
) {
1911 *r
= mcs
->thecolour
.red
>> 8;
1912 *g
= mcs
->thecolour
.green
>> 8;
1913 *b
= mcs
->thecolour
.blue
>> 8;
1921 * c-file-style: "simon"