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
;
252 /* Count the number of panels */
254 for (i
= 1; i
< cb
->nctrlsets
; i
++)
255 if (strcmp(cb
->ctrlsets
[i
]->pathname
, cb
->ctrlsets
[i
-1]->pathname
))
257 mcs
->panels
= snewn(mcs
->npanels
, union macctrl
*);
258 memset(mcs
->panels
, 0, sizeof(*mcs
->panels
) * mcs
->npanels
);
259 curstate
.panelnum
= 0;
261 curstate
.pos
.h
= rect
.left
+ 13;
262 curstate
.pos
.v
= rect
.top
+ 13;
263 curstate
.width
= 160;
264 panellist
.listbox
.type
= CTRL_LISTBOX
;
265 panellist
.listbox
.handler
= &panellist_handler
;
266 panellist
.listbox
.height
= 20;
267 panellist
.listbox
.percentwidth
= 100;
268 macctrl_listbox(mcs
, window
, &curstate
, &panellist
);
269 /* XXX Start with panel 1 active */
271 curstate
.pos
.h
= rect
.left
+ 13 + 160 + 13;
272 curstate
.pos
.v
= rect
.bottom
- 33;
273 curstate
.width
= rect
.right
- (rect
.left
+ 13 + 160) - (13 * 2);
274 for (i
= 0; i
< cb
->nctrlsets
; i
++) {
275 if (i
> 0 && strcmp(cb
->ctrlsets
[i
]->pathname
,
276 cb
->ctrlsets
[i
-1]->pathname
)) {
277 curstate
.pos
.v
= rect
.top
+ 13;
279 assert(curstate
.panelnum
< mcs
->npanels
);
280 dlg_listbox_add(&panellist
, mcs
, cb
->ctrlsets
[i
]->pathname
);
282 macctrl_layoutset(&curstate
, cb
->ctrlsets
[i
], window
, mcs
);
284 macctrl_switchtopanel(mcs
, 1);
285 macctrl_hideshowpanel(mcs
, 0, TRUE
);
286 /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
291 static void macctrl_layoutset(struct mac_layoutstate
*curstate
,
292 struct controlset
*s
,
293 WindowPtr window
, struct macctrls
*mcs
)
295 unsigned int i
, j
, ncols
, colstart
, colspan
;
296 struct mac_layoutstate cols
[MAXCOLS
], pos
;
301 /* Draw a title, if we have one. */
302 if (!s
->boxname
&& s
->boxtitle
) {
304 c
.text
.label
= dupstr(s
->boxtitle
);
305 macctrl_text(mcs
, window
, &cols
[0], &c
);
306 /* FIXME: should be highlighted, centred or boxed */
309 /* Start a containing box, if we have a boxname. */
310 /* FIXME: draw a box, not just its title */
311 if (s
->boxname
&& *s
->boxname
&& s
->boxtitle
) {
313 c
.text
.label
= dupstr(s
->boxtitle
);
314 macctrl_text(mcs
, window
, &cols
[0], &c
);
317 for (i
= 0; i
< s
->ncontrols
; i
++) {
318 union control
*ctrl
= s
->ctrls
[i
];
320 colstart
= COLUMN_START(ctrl
->generic
.column
);
321 colspan
= COLUMN_SPAN(ctrl
->generic
.column
);
322 if (ctrl
->generic
.type
== CTRL_COLUMNS
) {
323 if (ctrl
->columns
.ncols
!= 1) {
324 ncols
= ctrl
->columns
.ncols
;
325 assert(ncols
<= MAXCOLS
);
326 for (j
= 0; j
< ncols
; j
++) {
329 cols
[j
].pos
.h
= cols
[j
-1].pos
.h
+ cols
[j
-1].width
+ 6;
331 cols
[j
].width
= curstate
->width
-
332 (cols
[j
].pos
.h
- curstate
->pos
.h
);
334 cols
[j
].width
= (curstate
->width
+ 6) *
335 ctrl
->columns
.percentages
[j
] / 100 - 6;
338 for (j
= 0; j
< ncols
; j
++)
339 if (cols
[j
].pos
.v
> cols
[0].pos
.v
)
340 cols
[0].pos
.v
= cols
[j
].pos
.v
;
341 cols
[0].width
= curstate
->width
;
345 pos
= cols
[colstart
];
346 pos
.width
= cols
[colstart
+ colspan
- 1].width
+
347 (cols
[colstart
+ colspan
- 1].pos
.h
- cols
[colstart
].pos
.h
);
349 for (j
= colstart
; j
< colstart
+ colspan
; j
++)
350 if (pos
.pos
.v
< cols
[j
].pos
.v
)
351 pos
.pos
.v
= cols
[j
].pos
.v
;
353 switch (ctrl
->generic
.type
) {
355 macctrl_text(mcs
, window
, &pos
, ctrl
);
358 macctrl_editbox(mcs
, window
, &pos
, ctrl
);
361 macctrl_radio(mcs
, window
, &pos
, ctrl
);
364 macctrl_checkbox(mcs
, window
, &pos
, ctrl
);
367 macctrl_button(mcs
, window
, &pos
, ctrl
);
370 if (ctrl
->listbox
.height
== 0)
371 macctrl_popup(mcs
, window
, &pos
, ctrl
);
373 macctrl_listbox(mcs
, window
, &pos
, ctrl
);
376 for (j
= colstart
; j
< colstart
+ colspan
; j
++)
377 cols
[j
].pos
.v
= pos
.pos
.v
;
380 for (j
= 0; j
< ncols
; j
++)
381 if (cols
[j
].pos
.v
> curstate
->pos
.v
)
382 curstate
->pos
.v
= cols
[j
].pos
.v
;
385 static void macctrl_hideshowpanel(struct macctrls
*mcs
, unsigned int panel
,
391 #define hideshow(c) do { \
392 if (showit) ShowControl(c); else HideControl(c); \
395 for (mc
= mcs
->panels
[panel
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
396 #if !TARGET_API_MAC_CARBON
397 if (mcs
->focus
== mc
)
398 macctrl_setfocus(mcs
, NULL
);
400 switch (mc
->generic
.type
) {
402 hideshow(mc
->text
.tbctrl
);
404 case MACCTRL_EDITBOX
:
405 hideshow(mc
->editbox
.tbctrl
);
406 if (mc
->editbox
.tblabel
!= NULL
)
407 hideshow(mc
->editbox
.tblabel
);
410 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
411 hideshow(mc
->radio
.tbctrls
[j
]);
412 if (mc
->radio
.tblabel
!= NULL
)
413 hideshow(mc
->radio
.tblabel
);
415 case MACCTRL_CHECKBOX
:
416 hideshow(mc
->checkbox
.tbctrl
);
419 hideshow(mc
->button
.tbctrl
);
420 if (mc
->button
.tbring
!= NULL
)
421 hideshow(mc
->button
.tbring
);
423 case MACCTRL_LISTBOX
:
424 hideshow(mc
->listbox
.tbctrl
);
426 * At least under Mac OS 8.1, hiding a list box
427 * doesn't hide its scroll bars.
429 #if TARGET_API_MAC_CARBON
430 hideshow(GetListVerticalScrollBar(mc
->listbox
.list
));
432 hideshow((*mc
->listbox
.list
)->vScroll
);
436 hideshow(mc
->popup
.tbctrl
);
442 static void macctrl_switchtopanel(struct macctrls
*mcs
, unsigned int which
)
445 macctrl_hideshowpanel(mcs
, mcs
->curpanel
, FALSE
);
446 macctrl_hideshowpanel(mcs
, which
, TRUE
);
447 mcs
->curpanel
= which
;
450 #if !TARGET_API_MAC_CARBON
452 * System 7 focus manipulation
454 static void macctrl_defocus(union macctrl
*mc
)
457 assert(mac_gestalts
.apprvers
< 0x100);
458 switch (mc
->generic
.type
) {
459 case MACCTRL_EDITBOX
:
460 TEDeactivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
465 static void macctrl_enfocus(union macctrl
*mc
)
468 assert(mac_gestalts
.apprvers
< 0x100);
469 switch (mc
->generic
.type
) {
470 case MACCTRL_EDITBOX
:
471 TEActivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
476 static void macctrl_setfocus(struct macctrls
*mcs
, union macctrl
*mc
)
479 if (mcs
->focus
== mc
)
481 if (mcs
->focus
!= NULL
)
482 macctrl_defocus(mcs
->focus
);
489 static void macctrl_text(struct macctrls
*mcs
, WindowPtr window
,
490 struct mac_layoutstate
*curstate
,
493 union macctrl
*mc
= snew(union macctrl
);
497 assert(ctrl
->text
.label
!= NULL
);
498 mc
->generic
.type
= MACCTRL_TEXT
;
499 mc
->generic
.ctrl
= ctrl
;
500 mc
->generic
.privdata
= NULL
;
501 bounds
.left
= curstate
->pos
.h
;
502 bounds
.right
= bounds
.left
+ curstate
->width
;
503 bounds
.top
= curstate
->pos
.v
;
504 bounds
.bottom
= bounds
.top
+ 16;
505 if (mac_gestalts
.apprvers
>= 0x100) {
508 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
509 kControlStaticTextProc
, (long)mc
);
510 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
511 kControlStaticTextTextTag
,
512 strlen(ctrl
->text
.label
), ctrl
->text
.label
);
513 GetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
514 kControlStaticTextTextHeightTag
,
515 sizeof(height
), &height
, &olen
);
517 #if !TARGET_API_MAC_CARBON
521 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
522 SYS7_TEXT_PROC
, (long)mc
);
523 te
= (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
;
524 TESetText(ctrl
->text
.label
, strlen(ctrl
->text
.label
), te
);
525 height
= TEGetHeight(1, (*te
)->nLines
, te
);
528 SizeControl(mc
->text
.tbctrl
, curstate
->width
, height
);
529 curstate
->pos
.v
+= height
+ 6;
530 add234(mcs
->byctrl
, mc
);
531 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
532 mcs
->panels
[curstate
->panelnum
] = mc
;
535 static void macctrl_editbox(struct macctrls
*mcs
, WindowPtr window
,
536 struct mac_layoutstate
*curstate
,
539 union macctrl
*mc
= snew(union macctrl
);
540 Rect lbounds
, bounds
;
542 mc
->generic
.type
= MACCTRL_EDITBOX
;
543 mc
->generic
.ctrl
= ctrl
;
544 mc
->generic
.privdata
= NULL
;
545 lbounds
.left
= curstate
->pos
.h
;
546 lbounds
.top
= curstate
->pos
.v
;
547 if (ctrl
->editbox
.percentwidth
== 100) {
548 if (ctrl
->editbox
.label
!= NULL
) {
549 lbounds
.right
= lbounds
.left
+ curstate
->width
;
550 lbounds
.bottom
= lbounds
.top
+ 16;
551 curstate
->pos
.v
+= 18;
553 bounds
.left
= curstate
->pos
.h
;
554 bounds
.right
= bounds
.left
+ curstate
->width
;
556 lbounds
.right
= lbounds
.left
+
557 curstate
->width
* (100 - ctrl
->editbox
.percentwidth
) / 100;
558 lbounds
.bottom
= lbounds
.top
+ 22;
559 bounds
.left
= lbounds
.right
;
560 bounds
.right
= lbounds
.left
+ curstate
->width
;
562 bounds
.top
= curstate
->pos
.v
;
563 bounds
.bottom
= bounds
.top
+ 22;
564 if (mac_gestalts
.apprvers
>= 0x100) {
565 if (ctrl
->editbox
.label
== NULL
)
566 mc
->editbox
.tblabel
= NULL
;
568 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, FALSE
,
569 0, 0, 0, kControlStaticTextProc
,
571 SetControlData(mc
->editbox
.tblabel
, kControlEntireControl
,
572 kControlStaticTextTextTag
,
573 strlen(ctrl
->editbox
.label
), ctrl
->editbox
.label
);
575 InsetRect(&bounds
, 3, 3);
576 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
577 ctrl
->editbox
.password ?
578 kControlEditTextPasswordProc
:
579 kControlEditTextProc
, (long)mc
);
581 #if !TARGET_API_MAC_CARBON
583 if (ctrl
->editbox
.label
== NULL
)
584 mc
->editbox
.tblabel
= NULL
;
586 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, FALSE
,
587 0, 0, 0, SYS7_TEXT_PROC
,
589 TESetText(ctrl
->editbox
.label
, strlen(ctrl
->editbox
.label
),
590 (TEHandle
)(*mc
->editbox
.tblabel
)->contrlData
);
592 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
593 SYS7_EDITBOX_PROC
, (long)mc
);
596 curstate
->pos
.v
+= 28;
597 add234(mcs
->byctrl
, mc
);
598 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
599 mcs
->panels
[curstate
->panelnum
] = mc
;
600 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
603 #if !TARGET_API_MAC_CARBON
604 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16 variant
,
606 ControlDefProcMessage msg
,
617 rect
= (*control
)->contrlRect
;
618 if (variant
== SYS7_EDITBOX_VARIANT
)
619 InsetRect(&rect
, 3, 3); /* 2 if it's 20 pixels high */
620 te
= TENew(&rect
, &rect
);
621 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
622 (*te
)->txSize
= LoWord(ssfs
);
623 (*te
)->txFont
= HiWord(ssfs
);
624 (*control
)->contrlData
= (Handle
)te
;
627 TEDispose((TEHandle
)(*control
)->contrlData
);
630 if ((*control
)->contrlVis
) {
631 rect
= (*control
)->contrlRect
;
632 if (variant
== SYS7_EDITBOX_VARIANT
) {
635 InsetRect(&rect
, 3, 3);
638 (*(TEHandle
)(*control
)->contrlData
)->viewRect
= rect
;
639 TEUpdate(&rect
, (TEHandle
)(*control
)->contrlData
);
643 if (variant
== SYS7_TEXT_VARIANT
)
644 return kControlNoPart
;
645 mouse
.h
= LoWord(param
);
646 mouse
.v
= HiWord(param
);
647 rect
= (*control
)->contrlRect
;
648 InsetRect(&rect
, 3, 3);
649 return PtInRect(mouse
, &rect
) ? kControlEditTextPart
: kControlNoPart
;
651 if (param
& (1 << 31)) {
657 rgn
= (RgnHandle
)param
;
658 RectRgn(rgn
, &(*control
)->contrlRect
);
662 rgn
= (RgnHandle
)param
;
671 static void macctrl_radio(struct macctrls
*mcs
, WindowPtr window
,
672 struct mac_layoutstate
*curstate
,
675 union macctrl
*mc
= snew(union macctrl
);
678 unsigned int i
, colwidth
;
680 mc
->generic
.type
= MACCTRL_RADIO
;
681 mc
->generic
.ctrl
= ctrl
;
682 mc
->generic
.privdata
= NULL
;
683 mc
->radio
.tbctrls
= snewn(ctrl
->radio
.nbuttons
, ControlRef
);
684 colwidth
= (curstate
->width
+ 13) / ctrl
->radio
.ncolumns
;
685 bounds
.top
= curstate
->pos
.v
;
686 bounds
.bottom
= bounds
.top
+ 16;
687 bounds
.left
= curstate
->pos
.h
;
688 bounds
.right
= bounds
.left
+ curstate
->width
;
689 if (ctrl
->radio
.label
== NULL
)
690 mc
->radio
.tblabel
= NULL
;
692 if (mac_gestalts
.apprvers
>= 0x100) {
693 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, FALSE
,
694 0, 0, 0, kControlStaticTextProc
,
696 SetControlData(mc
->radio
.tblabel
, kControlEntireControl
,
697 kControlStaticTextTextTag
,
698 strlen(ctrl
->radio
.label
), ctrl
->radio
.label
);
700 #if !TARGET_API_MAC_CARBON
702 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, FALSE
,
703 0, 0, 0, SYS7_TEXT_PROC
, (long)mc
);
704 TESetText(ctrl
->radio
.label
, strlen(ctrl
->radio
.label
),
705 (TEHandle
)(*mc
->radio
.tblabel
)->contrlData
);
708 curstate
->pos
.v
+= 18;
710 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
711 bounds
.top
= curstate
->pos
.v
- 2;
712 bounds
.bottom
= bounds
.top
+ 18;
713 bounds
.left
= curstate
->pos
.h
+ colwidth
* (i
% ctrl
->radio
.ncolumns
);
714 if (i
== ctrl
->radio
.nbuttons
- 1 ||
715 i
% ctrl
->radio
.ncolumns
== ctrl
->radio
.ncolumns
- 1) {
716 bounds
.right
= curstate
->pos
.h
+ curstate
->width
;
717 curstate
->pos
.v
+= 18;
719 bounds
.right
= bounds
.left
+ colwidth
- 13;
720 c2pstrcpy(title
, ctrl
->radio
.buttons
[i
]);
721 mc
->radio
.tbctrls
[i
] = NewControl(window
, &bounds
, title
, FALSE
,
722 0, 0, 1, radioButProc
, (long)mc
);
724 curstate
->pos
.v
+= 4;
725 add234(mcs
->byctrl
, mc
);
726 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
727 mcs
->panels
[curstate
->panelnum
] = mc
;
728 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
731 static void macctrl_checkbox(struct macctrls
*mcs
, WindowPtr window
,
732 struct mac_layoutstate
*curstate
,
735 union macctrl
*mc
= snew(union macctrl
);
739 assert(ctrl
->checkbox
.label
!= NULL
);
740 mc
->generic
.type
= MACCTRL_CHECKBOX
;
741 mc
->generic
.ctrl
= ctrl
;
742 mc
->generic
.privdata
= NULL
;
743 bounds
.left
= curstate
->pos
.h
;
744 bounds
.right
= bounds
.left
+ curstate
->width
;
745 bounds
.top
= curstate
->pos
.v
;
746 bounds
.bottom
= bounds
.top
+ 16;
747 c2pstrcpy(title
, ctrl
->checkbox
.label
);
748 mc
->checkbox
.tbctrl
= NewControl(window
, &bounds
, title
, FALSE
, 0, 0, 1,
749 checkBoxProc
, (long)mc
);
750 add234(mcs
->byctrl
, mc
);
751 curstate
->pos
.v
+= 22;
752 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
753 mcs
->panels
[curstate
->panelnum
] = mc
;
754 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
757 static void macctrl_button(struct macctrls
*mcs
, WindowPtr window
,
758 struct mac_layoutstate
*curstate
,
761 union macctrl
*mc
= snew(union macctrl
);
765 assert(ctrl
->button
.label
!= NULL
);
766 mc
->generic
.type
= MACCTRL_BUTTON
;
767 mc
->generic
.ctrl
= ctrl
;
768 mc
->generic
.privdata
= NULL
;
769 bounds
.left
= curstate
->pos
.h
;
770 bounds
.right
= bounds
.left
+ curstate
->width
;
771 bounds
.top
= curstate
->pos
.v
;
772 bounds
.bottom
= bounds
.top
+ 20;
773 c2pstrcpy(title
, ctrl
->button
.label
);
774 mc
->button
.tbctrl
= NewControl(window
, &bounds
, title
, FALSE
, 0, 0, 1,
775 pushButProc
, (long)mc
);
776 mc
->button
.tbring
= NULL
;
777 if (mac_gestalts
.apprvers
>= 0x100) {
778 Boolean isdefault
= ctrl
->button
.isdefault
;
780 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
781 kControlPushButtonDefaultTag
,
782 sizeof(isdefault
), &isdefault
);
783 } else if (ctrl
->button
.isdefault
) {
784 InsetRect(&bounds
, -4, -4);
785 mc
->button
.tbring
= NewControl(window
, &bounds
, title
, FALSE
, 0, 0, 1,
786 SYS7_DEFAULT_PROC
, (long)mc
);
788 if (mac_gestalts
.apprvers
>= 0x110) {
789 Boolean iscancel
= ctrl
->button
.iscancel
;
791 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
792 kControlPushButtonCancelTag
,
793 sizeof(iscancel
), &iscancel
);
795 if (ctrl
->button
.isdefault
)
797 if (ctrl
->button
.iscancel
)
799 add234(mcs
->byctrl
, mc
);
800 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
801 mcs
->panels
[curstate
->panelnum
] = mc
;
802 curstate
->pos
.v
+= 26;
805 #if !TARGET_API_MAC_CARBON
806 static pascal SInt32
macctrl_sys7_default_cdef(SInt16 variant
,
808 ControlDefProcMessage msg
,
818 if ((*control
)->contrlVis
) {
819 rect
= (*control
)->contrlRect
;
820 GetPenState(&savestate
);
823 if ((*control
)->contrlHilite
== kControlInactivePart
)
825 oval
= (rect
.bottom
- rect
.top
) / 2 + 2;
826 FrameRoundRect(&rect
, oval
, oval
);
827 SetPenState(&savestate
);
831 if (param
& (1 << 31)) {
837 rgn
= (RgnHandle
)param
;
838 RectRgn(rgn
, &(*control
)->contrlRect
);
842 rgn
= (RgnHandle
)param
;
851 static void macctrl_listbox(struct macctrls
*mcs
, WindowPtr window
,
852 struct mac_layoutstate
*curstate
,
855 union macctrl
*mc
= snew(union macctrl
);
860 assert(ctrl
->listbox
.percentwidth
== 100);
861 mc
->generic
.type
= MACCTRL_LISTBOX
;
862 mc
->generic
.ctrl
= ctrl
;
863 mc
->generic
.privdata
= NULL
;
864 /* The list starts off empty */
865 mc
->listbox
.nids
= 0;
866 mc
->listbox
.ids
= NULL
;
867 bounds
.left
= curstate
->pos
.h
;
868 bounds
.right
= bounds
.left
+ curstate
->width
;
869 bounds
.top
= curstate
->pos
.v
;
870 bounds
.bottom
= bounds
.top
+ 16 * ctrl
->listbox
.height
+ 2;
872 if (mac_gestalts
.apprvers
>= 0x100) {
873 InsetRect(&bounds
, 3, 3);
874 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
,
876 kControlListBoxProc
, (long)mc
);
877 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
878 kControlListBoxListHandleTag
,
879 sizeof(mc
->listbox
.list
), &mc
->listbox
.list
,
881 DisposeControl(mc
->listbox
.tbctrl
);
886 #if !TARGET_API_MAC_CARBON
888 InsetRect(&bounds
, -3, -3);
889 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
,
891 SYS7_LISTBOX_PROC
, (long)mc
);
892 mc
->listbox
.list
= (ListHandle
)(*mc
->listbox
.tbctrl
)->contrlData
;
893 (*mc
->listbox
.list
)->refCon
= (long)mc
;
896 if (!ctrl
->listbox
.multisel
) {
897 #if TARGET_API_MAC_CARBON
898 SetListSelectionFlags(mc
->listbox
.list
, lOnlyOne
);
900 (*mc
->listbox
.list
)->selFlags
= lOnlyOne
;
903 add234(mcs
->byctrl
, mc
);
904 curstate
->pos
.v
+= 6 + 16 * ctrl
->listbox
.height
+ 2;
905 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
906 mcs
->panels
[curstate
->panelnum
] = mc
;
907 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
908 #if TARGET_API_MAC_CARBON
909 HideControl(GetListVerticalScrollBar(mc
->listbox
.list
));
911 HideControl((*mc
->listbox
.list
)->vScroll
);
915 #if !TARGET_API_MAC_CARBON
916 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16 variant
,
918 ControlDefProcMessage msg
,
934 rect
= (*control
)->contrlRect
;
935 InsetRect(&rect
, 4, 4);
936 rect
.right
-= 15; /* scroll bar */
937 bounds
.top
= bounds
.bottom
= bounds
.left
= 0;
939 csize
.h
= csize
.v
= 0;
941 savefont
= curport
->txFont
;
942 savesize
= curport
->txSize
;
943 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
944 TextFont(HiWord(ssfs
));
945 TextSize(LoWord(ssfs
));
946 list
= LNew(&rect
, &bounds
, csize
, 0, (*control
)->contrlOwner
,
947 TRUE
, FALSE
, FALSE
, TRUE
);
948 SetControlReference((*list
)->vScroll
, (long)list
);
949 (*control
)->contrlData
= (Handle
)list
;
955 * If the dialogue box is being destroyed, the scroll bar
956 * might have gone already. In our situation, this is the
957 * only time we destroy a control, so NULL out the scroll bar
958 * handle to prevent LDispose trying to free it.
960 list
= (ListHandle
)(*control
)->contrlData
;
961 (*list
)->vScroll
= NULL
;
965 if ((*control
)->contrlVis
) {
966 rect
= (*control
)->contrlRect
;
967 /* XXX input focus highlighting? */
968 InsetRect(&rect
, 3, 3);
971 list
= (ListHandle
)(*control
)->contrlData
;
972 LActivate((*control
)->contrlHilite
!= kControlInactivePart
, list
);
974 LUpdate(curport
->visRgn
, list
);
978 mouse
.h
= LoWord(param
);
979 mouse
.v
= HiWord(param
);
980 rect
= (*control
)->contrlRect
;
981 InsetRect(&rect
, 4, 4);
983 * We deliberately exclude the scrollbar so that LClick() can see it.
986 return PtInRect(mouse
, &rect
) ? kControlListBoxPart
: kControlNoPart
;
988 if (param
& (1 << 31)) {
994 rgn
= (RgnHandle
)param
;
995 RectRgn(rgn
, &(*control
)->contrlRect
);
999 rgn
= (RgnHandle
)param
;
1008 static void macctrl_popup(struct macctrls
*mcs
, WindowPtr window
,
1009 struct mac_layoutstate
*curstate
,
1010 union control
*ctrl
)
1012 union macctrl
*mc
= snew(union macctrl
);
1015 unsigned int labelwidth
;
1016 static int nextmenuid
= MENU_MIN
;
1021 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
1022 * create a popup menu with dynamic content.
1024 assert(ctrl
->listbox
.height
== 0);
1025 assert(!ctrl
->listbox
.draglist
);
1026 assert(!ctrl
->listbox
.multisel
);
1028 mc
->generic
.type
= MACCTRL_POPUP
;
1029 mc
->generic
.ctrl
= ctrl
;
1030 mc
->generic
.privdata
= NULL
;
1031 c2pstrcpy(title
, ctrl
->button
.label
== NULL ?
"" : ctrl
->button
.label
);
1033 /* Find a spare menu ID and create the menu */
1034 while (GetMenuHandle(nextmenuid
) != NULL
)
1035 if (++nextmenuid
>= MENU_MAX
) nextmenuid
= MENU_MIN
;
1036 menuid
= nextmenuid
++;
1037 menu
= NewMenu(menuid
, "\pdummy");
1038 if (menu
== NULL
) return;
1039 mc
->popup
.menu
= menu
;
1040 mc
->popup
.menuid
= menuid
;
1041 InsertMenu(menu
, kInsertHierarchicalMenu
);
1043 /* The menu starts off empty */
1045 mc
->popup
.ids
= NULL
;
1047 bounds
.left
= curstate
->pos
.h
;
1048 bounds
.right
= bounds
.left
+ curstate
->width
;
1049 bounds
.top
= curstate
->pos
.v
;
1050 bounds
.bottom
= bounds
.top
+ 20;
1051 /* XXX handle percentwidth == 100 */
1052 labelwidth
= curstate
->width
* (100 - ctrl
->listbox
.percentwidth
) / 100;
1053 mc
->popup
.tbctrl
= NewControl(window
, &bounds
, title
, FALSE
,
1054 popupTitleLeftJust
, menuid
, labelwidth
,
1055 popupMenuProc
+ popupFixedWidth
, (long)mc
);
1056 add234(mcs
->byctrl
, mc
);
1057 curstate
->pos
.v
+= 26;
1058 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
1059 mcs
->panels
[curstate
->panelnum
] = mc
;
1060 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1064 void macctrl_activate(WindowPtr window
, EventRecord
*event
)
1066 struct macctrls
*mcs
= mac_winctrls(window
);
1067 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1070 ControlPartCode state
;
1074 SetPort((GrafPtr
)GetWindowPort(window
));
1075 if (mac_gestalts
.apprvers
>= 0x100)
1076 SetThemeWindowBackground(window
, active ?
1077 kThemeBrushModelessDialogBackgroundActive
:
1078 kThemeBrushModelessDialogBackgroundInactive
,
1080 state
= active ? kControlNoPart
: kControlInactivePart
;
1081 for (i
= 0; i
<= mcs
->curpanel
; i
+= mcs
->curpanel
)
1082 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
1083 switch (mc
->generic
.type
) {
1085 HiliteControl(mc
->text
.tbctrl
, state
);
1087 case MACCTRL_EDITBOX
:
1088 HiliteControl(mc
->editbox
.tbctrl
, state
);
1089 if (mc
->editbox
.tblabel
!= NULL
)
1090 HiliteControl(mc
->editbox
.tblabel
, state
);
1093 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
1094 HiliteControl(mc
->radio
.tbctrls
[j
], state
);
1095 if (mc
->radio
.tblabel
!= NULL
)
1096 HiliteControl(mc
->radio
.tblabel
, state
);
1098 case MACCTRL_CHECKBOX
:
1099 HiliteControl(mc
->checkbox
.tbctrl
, state
);
1101 case MACCTRL_BUTTON
:
1102 HiliteControl(mc
->button
.tbctrl
, state
);
1103 if (mc
->button
.tbring
!= NULL
)
1104 HiliteControl(mc
->button
.tbring
, state
);
1106 case MACCTRL_LISTBOX
:
1107 HiliteControl(mc
->listbox
.tbctrl
, state
);
1110 HiliteControl(mc
->popup
.tbctrl
, state
);
1113 #if !TARGET_API_MAC_CARBON
1114 if (mcs
->focus
== mc
) {
1116 macctrl_enfocus(mc
);
1118 macctrl_defocus(mc
);
1125 void macctrl_click(WindowPtr window
, EventRecord
*event
)
1128 ControlHandle control
, oldfocus
;
1129 int part
, trackresult
;
1132 struct macctrls
*mcs
= mac_winctrls(window
);
1137 SetPort((GrafPtr
)GetWindowPort(window
));
1138 mouse
= event
->where
;
1139 GlobalToLocal(&mouse
);
1140 part
= FindControl(mouse
, window
, &control
);
1141 if (control
!= NULL
) {
1142 #if !TARGET_API_MAC_CARBON
1144 * Special magic for scroll bars in list boxes, whose refcon
1147 if (part
== kControlUpButtonPart
|| part
== kControlDownButtonPart
||
1148 part
== kControlPageUpPart
|| part
== kControlPageDownPart
||
1149 part
== kControlIndicatorPart
)
1150 mc
= (union macctrl
*)
1151 (*(ListHandle
)GetControlReference(control
))->refCon
;
1154 mc
= (union macctrl
*)GetControlReference(control
);
1155 if (mac_gestalts
.apprvers
>= 0x100) {
1156 if (GetControlFeatures(control
, &features
) == noErr
&&
1157 (features
& kControlSupportsFocus
) &&
1158 (features
& kControlGetsFocusOnClick
) &&
1159 GetKeyboardFocus(window
, &oldfocus
) == noErr
&&
1160 control
!= oldfocus
)
1161 SetKeyboardFocus(window
, control
, part
);
1162 trackresult
= HandleControlClick(control
, mouse
, event
->modifiers
,
1163 (ControlActionUPP
)-1);
1165 #if !TARGET_API_MAC_CARBON
1166 if (mc
->generic
.type
== MACCTRL_EDITBOX
&&
1167 control
== mc
->editbox
.tbctrl
) {
1168 TEHandle te
= (TEHandle
)(*control
)->contrlData
;
1170 macctrl_setfocus(mcs
, mc
);
1171 TEClick(mouse
, !!(event
->modifiers
& shiftKey
), te
);
1174 if (mc
->generic
.type
== MACCTRL_LISTBOX
&&
1175 (control
== mc
->listbox
.tbctrl
||
1176 control
== (*mc
->listbox
.list
)->vScroll
)) {
1178 macctrl_setfocus(mcs
, mc
);
1179 if (LClick(mouse
, event
->modifiers
, mc
->listbox
.list
))
1181 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1183 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1187 trackresult
= TrackControl(control
, mouse
, (ControlActionUPP
)-1);
1189 switch (mc
->generic
.type
) {
1191 if (trackresult
!= 0) {
1192 for (i
= 0; i
< mc
->generic
.ctrl
->radio
.nbuttons
; i
++)
1193 if (mc
->radio
.tbctrls
[i
] == control
)
1194 SetControlValue(mc
->radio
.tbctrls
[i
],
1195 kControlRadioButtonCheckedValue
);
1197 SetControlValue(mc
->radio
.tbctrls
[i
],
1198 kControlRadioButtonUncheckedValue
);
1199 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1202 case MACCTRL_CHECKBOX
:
1203 if (trackresult
!= 0) {
1204 SetControlValue(control
, !GetControlValue(control
));
1205 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1208 case MACCTRL_BUTTON
:
1209 if (trackresult
!= 0)
1210 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1212 case MACCTRL_LISTBOX
:
1213 /* FIXME spot double-click */
1214 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1217 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1225 void macctrl_key(WindowPtr window
, EventRecord
*event
)
1228 struct macctrls
*mcs
= mac_winctrls(window
);
1230 unsigned long dummy
;
1232 switch (event
->message
& charCodeMask
) {
1233 case kEnterCharCode
:
1234 case kReturnCharCode
:
1235 if (mcs
->defbutton
!= NULL
) {
1236 assert(mcs
->defbutton
->generic
.type
== MACCTRL_BUTTON
);
1237 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlButtonPart
);
1239 * I'd like to delay unhilighting the button until after
1240 * the event has been processed, but by them the entire
1241 * dialgue box might have been destroyed.
1244 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1245 ctrlevent(mcs
, mcs
->defbutton
, EVENT_ACTION
);
1248 case kEscapeCharCode
:
1249 if (mcs
->canbutton
!= NULL
) {
1250 assert(mcs
->canbutton
->generic
.type
== MACCTRL_BUTTON
);
1251 HiliteControl(mcs
->canbutton
->button
.tbctrl
, kControlButtonPart
);
1253 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1254 ctrlevent(mcs
, mcs
->canbutton
, EVENT_ACTION
);
1258 if (mac_gestalts
.apprvers
>= 0x100) {
1259 if (GetKeyboardFocus(window
, &control
) == noErr
&& control
!= NULL
) {
1260 HandleControlKey(control
, (event
->message
& keyCodeMask
) >> 8,
1261 event
->message
& charCodeMask
, event
->modifiers
);
1262 mc
= (union macctrl
*)GetControlReference(control
);
1263 switch (mc
->generic
.type
) {
1264 case MACCTRL_LISTBOX
:
1265 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1268 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1273 #if !TARGET_API_MAC_CARBON
1277 if (mcs
->focus
!= NULL
) {
1279 switch (mc
->generic
.type
) {
1280 case MACCTRL_EDITBOX
:
1281 te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1282 TEKey(event
->message
& charCodeMask
, te
);
1283 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1291 void macctrl_update(WindowPtr window
)
1293 #if TARGET_API_MAC_CARBON
1299 BeginUpdate(window
);
1301 SetPort((GrafPtr
)GetWindowPort(window
));
1302 if (mac_gestalts
.apprvers
>= 0x101) {
1303 #if TARGET_API_MAC_CARBON
1304 GetPortBounds(GetWindowPort(window
), &rect
);
1306 rect
= window
->portRect
;
1308 InsetRect(&rect
, -1, -1);
1309 DrawThemeModelessDialogFrame(&rect
, mac_frontwindow() == window ?
1310 kThemeStateActive
: kThemeStateInactive
);
1312 #if TARGET_API_MAC_CARBON
1314 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1315 UpdateControls(window
, visrgn
);
1318 UpdateControls(window
, window
->visRgn
);
1324 #if TARGET_API_MAC_CARBON
1325 #define EnableItem EnableMenuItem
1326 #define DisableItem DisableMenuItem
1328 void macctrl_adjustmenus(WindowPtr window
)
1332 menu
= GetMenuHandle(mFile
);
1333 DisableItem(menu
, iSave
); /* XXX enable if modified */
1334 EnableItem(menu
, iSaveAs
);
1335 EnableItem(menu
, iDuplicate
);
1337 menu
= GetMenuHandle(mEdit
);
1338 DisableItem(menu
, 0);
1341 void macctrl_close(WindowPtr window
)
1343 struct macctrls
*mcs
= mac_winctrls(window
);
1347 * Mostly, we don't bother disposing of the Toolbox controls,
1348 * since that will happen automatically when the window is
1349 * disposed of. Popup menus are an exception, because we have to
1350 * dispose of the menu ourselves, and doing that while the control
1351 * still holds a reference to it seems rude.
1353 while ((mc
= index234(mcs
->byctrl
, 0)) != NULL
) {
1354 if (mc
->generic
.privdata
!= NULL
&& mc
->generic
.freeprivdata
)
1355 sfree(mc
->generic
.privdata
);
1356 switch (mc
->generic
.type
) {
1358 DisposeControl(mc
->popup
.tbctrl
);
1359 DeleteMenu(mc
->popup
.menuid
);
1360 DisposeMenu(mc
->popup
.menu
);
1363 del234(mcs
->byctrl
, mc
);
1367 freetree234(mcs
->byctrl
);
1373 void dlg_update_start(union control
*ctrl
, void *dlg
)
1379 void dlg_update_done(union control
*ctrl
, void *dlg
)
1385 void dlg_set_focus(union control
*ctrl
, void *dlg
)
1388 if (mac_gestalts
.apprvers
>= 0x100) {
1389 /* Use SetKeyboardFocus() */
1391 /* Do our own mucking around */
1395 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1401 void dlg_beep(void *dlg
)
1407 void dlg_error_msg(void *dlg
, char *msg
)
1411 c2pstrcpy(pmsg
, msg
);
1412 ParamText(pmsg
, NULL
, NULL
, NULL
);
1413 StopAlert(128, NULL
);
1416 void dlg_end(void *dlg
, int value
)
1418 struct macctrls
*mcs
= dlg
;
1420 if (mcs
->end
!= NULL
)
1421 (*mcs
->end
)(mcs
->window
, value
);
1424 void dlg_refresh(union control
*ctrl
, void *dlg
)
1426 struct macctrls
*mcs
= dlg
;
1431 /* NULL means refresh every control */
1432 for (i
= 0 ; i
< mcs
->npanels
; i
++) {
1433 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
1434 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1439 /* Just refresh a specific control */
1440 mc
= findbyctrl(mcs
, ctrl
);
1442 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1445 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
1447 struct macctrls
*mcs
= dlg
;
1448 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1451 return mc
->generic
.privdata
;
1454 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
1456 struct macctrls
*mcs
= dlg
;
1457 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1460 mc
->generic
.privdata
= ptr
;
1461 mc
->generic
.freeprivdata
= FALSE
;
1464 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
1466 struct macctrls
*mcs
= dlg
;
1467 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1470 mc
->generic
.privdata
= smalloc(size
);
1471 mc
->generic
.freeprivdata
= TRUE
;
1472 return mc
->generic
.privdata
;
1477 * Radio Button control
1480 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
1482 struct macctrls
*mcs
= dlg
;
1483 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1486 if (mc
== NULL
) return;
1487 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1488 if (i
== whichbutton
)
1489 SetControlValue(mc
->radio
.tbctrls
[i
],
1490 kControlRadioButtonCheckedValue
);
1492 SetControlValue(mc
->radio
.tbctrls
[i
],
1493 kControlRadioButtonUncheckedValue
);
1498 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
1500 struct macctrls
*mcs
= dlg
;
1501 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1505 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1506 if (GetControlValue(mc
->radio
.tbctrls
[i
]) ==
1507 kControlRadioButtonCheckedValue
)
1518 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
1520 struct macctrls
*mcs
= dlg
;
1521 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1523 if (mc
== NULL
) return;
1524 SetControlValue(mc
->checkbox
.tbctrl
,
1525 checked ? kControlCheckBoxCheckedValue
:
1526 kControlCheckBoxUncheckedValue
);
1529 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
1531 struct macctrls
*mcs
= dlg
;
1532 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1535 return GetControlValue(mc
->checkbox
.tbctrl
);
1543 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
1545 struct macctrls
*mcs
= dlg
;
1546 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1549 if (mc
== NULL
) return;
1550 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1552 SetPort((GrafPtr
)(GetWindowPort(mcs
->window
)));
1553 if (mac_gestalts
.apprvers
>= 0x100)
1554 SetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1555 ctrl
->editbox
.password ?
1556 kControlEditTextPasswordTag
:
1557 kControlEditTextTextTag
,
1558 strlen(text
), text
);
1559 #if !TARGET_API_MAC_CARBON
1561 TESetText(text
, strlen(text
),
1562 (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
1564 DrawOneControl(mc
->editbox
.tbctrl
);
1568 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
1570 struct macctrls
*mcs
= dlg
;
1571 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1575 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1576 if (mac_gestalts
.apprvers
>= 0x100) {
1577 if (GetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1578 ctrl
->editbox
.password ?
1579 kControlEditTextPasswordTag
:
1580 kControlEditTextTextTag
,
1581 length
- 1, buffer
, &olen
) != noErr
)
1583 if (olen
> length
- 1)
1586 #if !TARGET_API_MAC_CARBON
1588 TEHandle te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1590 olen
= (*te
)->teLength
;
1591 if (olen
> length
- 1)
1593 memcpy(buffer
, *(*te
)->hText
, olen
);
1596 buffer
[olen
] = '\0';
1604 static void dlg_macpopup_clear(union control
*ctrl
, void *dlg
)
1606 struct macctrls
*mcs
= dlg
;
1607 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1608 MenuRef menu
= mc
->popup
.menu
;
1611 if (mc
== NULL
) return;
1612 n
= CountMenuItems(menu
);
1613 for (i
= 0; i
< n
; i
++)
1614 DeleteMenuItem(menu
, n
- i
);
1616 sfree(mc
->popup
.ids
);
1617 mc
->popup
.ids
= NULL
;
1618 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1621 static void dlg_maclist_clear(union control
*ctrl
, void *dlg
)
1623 struct macctrls
*mcs
= dlg
;
1624 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1626 if (mc
== NULL
) return;
1627 LDelRow(0, 0, mc
->listbox
.list
);
1628 mc
->listbox
.nids
= 0;
1629 sfree(mc
->listbox
.ids
);
1630 mc
->listbox
.ids
= NULL
;
1631 DrawOneControl(mc
->listbox
.tbctrl
);
1634 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
1637 switch (ctrl
->generic
.type
) {
1639 if (ctrl
->listbox
.height
== 0)
1640 dlg_macpopup_clear(ctrl
, dlg
);
1642 dlg_maclist_clear(ctrl
, dlg
);
1647 static void dlg_macpopup_del(union control
*ctrl
, void *dlg
, int index
)
1649 struct macctrls
*mcs
= dlg
;
1650 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1651 MenuRef menu
= mc
->popup
.menu
;
1653 if (mc
== NULL
) return;
1654 DeleteMenuItem(menu
, index
+ 1);
1655 if (mc
->popup
.ids
!= NULL
)
1656 memcpy(mc
->popup
.ids
+ index
, mc
->popup
.ids
+ index
+ 1,
1657 (mc
->popup
.nids
- index
- 1) * sizeof(*mc
->popup
.ids
));
1658 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1661 static void dlg_maclist_del(union control
*ctrl
, void *dlg
, int index
)
1663 struct macctrls
*mcs
= dlg
;
1664 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1666 if (mc
== NULL
) return;
1667 LDelRow(1, index
, mc
->listbox
.list
);
1668 if (mc
->listbox
.ids
!= NULL
)
1669 memcpy(mc
->listbox
.ids
+ index
, mc
->listbox
.ids
+ index
+ 1,
1670 (mc
->listbox
.nids
- index
- 1) * sizeof(*mc
->listbox
.ids
));
1671 DrawOneControl(mc
->listbox
.tbctrl
);
1674 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
1677 switch (ctrl
->generic
.type
) {
1679 if (ctrl
->listbox
.height
== 0)
1680 dlg_macpopup_del(ctrl
, dlg
, index
);
1682 dlg_maclist_del(ctrl
, dlg
, index
);
1687 static void dlg_macpopup_add(union control
*ctrl
, void *dlg
, char const *text
)
1689 struct macctrls
*mcs
= dlg
;
1690 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1691 MenuRef menu
= mc
->popup
.menu
;
1694 if (mc
== NULL
) return;
1695 assert(text
[0] != '\0');
1696 c2pstrcpy(itemstring
, text
);
1697 AppendMenu(menu
, "\pdummy");
1698 SetMenuItemText(menu
, CountMenuItems(menu
), itemstring
);
1699 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1703 static void dlg_maclist_add(union control
*ctrl
, void *dlg
, char const *text
)
1705 struct macctrls
*mcs
= dlg
;
1706 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1708 Cell cell
= { 0, 0 };
1710 if (mc
== NULL
) return;
1711 #if TARGET_API_MAC_CARBON
1712 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1714 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1716 cell
.v
= bounds
.bottom
;
1717 LAddRow(1, cell
.v
, mc
->listbox
.list
);
1718 LSetCell(text
, strlen(text
), cell
, mc
->listbox
.list
);
1719 DrawOneControl(mc
->listbox
.tbctrl
);
1722 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
1725 switch (ctrl
->generic
.type
) {
1727 if (ctrl
->listbox
.height
== 0)
1728 dlg_macpopup_add(ctrl
, dlg
, text
);
1730 dlg_maclist_add(ctrl
, dlg
, text
);
1735 static void dlg_macpopup_addwithid(union control
*ctrl
, void *dlg
,
1736 char const *text
, int id
)
1738 struct macctrls
*mcs
= dlg
;
1739 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1740 MenuRef menu
= mc
->popup
.menu
;
1743 if (mc
== NULL
) return;
1744 dlg_macpopup_add(ctrl
, dlg
, text
);
1745 index
= CountMenuItems(menu
) - 1;
1746 if (mc
->popup
.nids
<= index
) {
1747 mc
->popup
.nids
= index
+ 1;
1748 mc
->popup
.ids
= sresize(mc
->popup
.ids
, mc
->popup
.nids
, int);
1750 mc
->popup
.ids
[index
] = id
;
1753 static void dlg_maclist_addwithid(union control
*ctrl
, void *dlg
,
1754 char const *text
, int id
)
1756 struct macctrls
*mcs
= dlg
;
1757 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1761 if (mc
== NULL
) return;
1762 dlg_maclist_add(ctrl
, dlg
, text
);
1763 #if TARGET_API_MAC_CARBON
1764 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1766 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1768 index
= bounds
.bottom
;
1769 if (mc
->listbox
.nids
<= index
) {
1770 mc
->listbox
.nids
= index
+ 1;
1771 mc
->listbox
.ids
= sresize(mc
->listbox
.ids
, mc
->listbox
.nids
, int);
1773 mc
->listbox
.ids
[index
] = id
;
1776 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
1777 char const *text
, int id
)
1780 switch (ctrl
->generic
.type
) {
1782 if (ctrl
->listbox
.height
== 0)
1783 dlg_macpopup_addwithid(ctrl
, dlg
, text
, id
);
1785 dlg_maclist_addwithid(ctrl
, dlg
, text
, id
);
1790 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
1792 struct macctrls
*mcs
= dlg
;
1793 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1796 switch (ctrl
->generic
.type
) {
1798 if (ctrl
->listbox
.height
== 0) {
1799 assert(mc
->popup
.ids
!= NULL
&& mc
->popup
.nids
> index
);
1800 return mc
->popup
.ids
[index
];
1802 assert(mc
->listbox
.ids
!= NULL
&& mc
->listbox
.nids
> index
);
1803 return mc
->listbox
.ids
[index
];
1809 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
1811 struct macctrls
*mcs
= dlg
;
1812 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1813 Cell cell
= { 0, 0 };
1816 switch (ctrl
->generic
.type
) {
1818 if (ctrl
->listbox
.height
== 0)
1819 return GetControlValue(mc
->popup
.tbctrl
) - 1;
1821 if (LGetSelect(TRUE
, &cell
, mc
->listbox
.list
))
1830 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
1832 struct macctrls
*mcs
= dlg
;
1833 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1834 Cell cell
= { 0, 0 };
1837 switch (ctrl
->generic
.type
) {
1839 if (ctrl
->listbox
.height
== 0)
1840 return GetControlValue(mc
->popup
.tbctrl
) - 1 == index
;
1843 return LGetSelect(FALSE
, &cell
, mc
->listbox
.list
);
1849 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
1851 struct macctrls
*mcs
= dlg
;
1852 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1854 if (mc
== NULL
) return;
1855 switch (ctrl
->generic
.type
) {
1857 if (ctrl
->listbox
.height
== 0)
1858 SetControlValue(mc
->popup
.tbctrl
, index
+ 1);
1868 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
1870 struct macctrls
*mcs
= dlg
;
1871 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1873 if (mc
== NULL
) return;
1874 if (mac_gestalts
.apprvers
>= 0x100)
1875 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
1876 kControlStaticTextTextTag
, strlen(text
), text
);
1877 #if !TARGET_API_MAC_CARBON
1879 TESetText(text
, strlen(text
),
1880 (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
);
1886 * File Selector control
1889 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
1894 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
1901 * Font Selector control
1904 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fn
)
1909 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fn
)
1916 * Printer enumeration
1919 printer_enum
*printer_start_enum(int *nprinters
)
1926 char *printer_get_name(printer_enum
*pe
, int thing
)
1932 void printer_finish_enum(printer_enum
*pe
)
1939 * Colour selection stuff
1942 void dlg_coloursel_start(union control
*ctrl
, void *dlg
,
1943 int r
, int g
, int b
)
1945 struct macctrls
*mcs
= dlg
;
1946 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1947 Point where
= {-1, -1}; /* Screen with greatest colour depth */
1950 if (HAVE_COLOR_QD()) {
1951 incolour
.red
= r
* 0x0101;
1952 incolour
.green
= g
* 0x0101;
1953 incolour
.blue
= b
* 0x0101;
1954 mcs
->gotcolour
= GetColor(where
, "\pModify Colour:", &incolour
,
1956 ctrlevent(mcs
, mc
, EVENT_CALLBACK
);
1961 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1962 int *r
, int *g
, int *b
)
1964 struct macctrls
*mcs
= dlg
;
1966 if (mcs
->gotcolour
) {
1967 *r
= mcs
->thecolour
.red
>> 8;
1968 *g
= mcs
->thecolour
.green
>> 8;
1969 *b
= mcs
->thecolour
.blue
>> 8;
1977 * c-file-style: "simon"