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 for (i
= 0; i
< s
->ncontrols
; i
++) {
302 union control
*ctrl
= s
->ctrls
[i
];
304 colstart
= COLUMN_START(ctrl
->generic
.column
);
305 colspan
= COLUMN_SPAN(ctrl
->generic
.column
);
306 if (ctrl
->generic
.type
== CTRL_COLUMNS
) {
307 if (ctrl
->columns
.ncols
!= 1) {
308 ncols
= ctrl
->columns
.ncols
;
309 assert(ncols
<= MAXCOLS
);
310 for (j
= 0; j
< ncols
; j
++) {
313 cols
[j
].pos
.h
= cols
[j
-1].pos
.h
+ cols
[j
-1].width
+ 6;
315 cols
[j
].width
= curstate
->width
-
316 (cols
[j
].pos
.h
- curstate
->pos
.h
);
318 cols
[j
].width
= (curstate
->width
+ 6) *
319 ctrl
->columns
.percentages
[j
] / 100 - 6;
322 for (j
= 0; j
< ncols
; j
++)
323 if (cols
[j
].pos
.v
> cols
[0].pos
.v
)
324 cols
[0].pos
.v
= cols
[j
].pos
.v
;
325 cols
[0].width
= curstate
->width
;
329 pos
= cols
[colstart
];
330 pos
.width
= cols
[colstart
+ colspan
- 1].width
+
331 (cols
[colstart
+ colspan
- 1].pos
.h
- cols
[colstart
].pos
.h
);
333 for (j
= colstart
; j
< colstart
+ colspan
; j
++)
334 if (pos
.pos
.v
< cols
[j
].pos
.v
)
335 pos
.pos
.v
= cols
[j
].pos
.v
;
337 switch (ctrl
->generic
.type
) {
339 macctrl_text(mcs
, window
, &pos
, ctrl
);
342 macctrl_editbox(mcs
, window
, &pos
, ctrl
);
345 macctrl_radio(mcs
, window
, &pos
, ctrl
);
348 macctrl_checkbox(mcs
, window
, &pos
, ctrl
);
351 macctrl_button(mcs
, window
, &pos
, ctrl
);
354 if (ctrl
->listbox
.height
== 0)
355 macctrl_popup(mcs
, window
, &pos
, ctrl
);
357 macctrl_listbox(mcs
, window
, &pos
, ctrl
);
360 for (j
= colstart
; j
< colstart
+ colspan
; j
++)
361 cols
[j
].pos
.v
= pos
.pos
.v
;
364 for (j
= 0; j
< ncols
; j
++)
365 if (cols
[j
].pos
.v
> curstate
->pos
.v
)
366 curstate
->pos
.v
= cols
[j
].pos
.v
;
369 static void macctrl_hideshowpanel(struct macctrls
*mcs
, unsigned int panel
,
375 #define hideshow(c) do { \
376 if (showit) ShowControl(c); else HideControl(c); \
379 for (mc
= mcs
->panels
[panel
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
380 #if !TARGET_API_MAC_CARBON
381 if (mcs
->focus
== mc
)
382 macctrl_setfocus(mcs
, NULL
);
384 switch (mc
->generic
.type
) {
386 hideshow(mc
->text
.tbctrl
);
388 case MACCTRL_EDITBOX
:
389 hideshow(mc
->editbox
.tbctrl
);
390 if (mc
->editbox
.tblabel
!= NULL
)
391 hideshow(mc
->editbox
.tblabel
);
394 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
395 hideshow(mc
->radio
.tbctrls
[j
]);
396 if (mc
->radio
.tblabel
!= NULL
)
397 hideshow(mc
->radio
.tblabel
);
399 case MACCTRL_CHECKBOX
:
400 hideshow(mc
->checkbox
.tbctrl
);
403 hideshow(mc
->button
.tbctrl
);
404 if (mc
->button
.tbring
!= NULL
)
405 hideshow(mc
->button
.tbring
);
407 case MACCTRL_LISTBOX
:
408 hideshow(mc
->listbox
.tbctrl
);
410 * At least under Mac OS 8.1, hiding a list box
411 * doesn't hide its scroll bars.
413 #if TARGET_API_MAC_CARBON
414 hideshow(GetListVerticalScrollBar(mc
->listbox
.list
));
416 hideshow((*mc
->listbox
.list
)->vScroll
);
420 hideshow(mc
->popup
.tbctrl
);
426 static void macctrl_switchtopanel(struct macctrls
*mcs
, unsigned int which
)
429 macctrl_hideshowpanel(mcs
, mcs
->curpanel
, FALSE
);
430 macctrl_hideshowpanel(mcs
, which
, TRUE
);
431 mcs
->curpanel
= which
;
434 #if !TARGET_API_MAC_CARBON
436 * System 7 focus manipulation
438 static void macctrl_defocus(union macctrl
*mc
)
441 assert(mac_gestalts
.apprvers
< 0x100);
442 switch (mc
->generic
.type
) {
443 case MACCTRL_EDITBOX
:
444 TEDeactivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
449 static void macctrl_enfocus(union macctrl
*mc
)
452 assert(mac_gestalts
.apprvers
< 0x100);
453 switch (mc
->generic
.type
) {
454 case MACCTRL_EDITBOX
:
455 TEActivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
460 static void macctrl_setfocus(struct macctrls
*mcs
, union macctrl
*mc
)
463 if (mcs
->focus
== mc
)
465 if (mcs
->focus
!= NULL
)
466 macctrl_defocus(mcs
->focus
);
473 static void macctrl_text(struct macctrls
*mcs
, WindowPtr window
,
474 struct mac_layoutstate
*curstate
,
477 union macctrl
*mc
= snew(union macctrl
);
481 assert(ctrl
->text
.label
!= NULL
);
482 mc
->generic
.type
= MACCTRL_TEXT
;
483 mc
->generic
.ctrl
= ctrl
;
484 mc
->generic
.privdata
= NULL
;
485 bounds
.left
= curstate
->pos
.h
;
486 bounds
.right
= bounds
.left
+ curstate
->width
;
487 bounds
.top
= curstate
->pos
.v
;
488 bounds
.bottom
= bounds
.top
+ 16;
489 if (mac_gestalts
.apprvers
>= 0x100) {
492 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
493 kControlStaticTextProc
, (long)mc
);
494 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
495 kControlStaticTextTextTag
,
496 strlen(ctrl
->text
.label
), ctrl
->text
.label
);
497 GetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
498 kControlStaticTextTextHeightTag
,
499 sizeof(height
), &height
, &olen
);
501 #if !TARGET_API_MAC_CARBON
505 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
506 SYS7_TEXT_PROC
, (long)mc
);
507 te
= (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
;
508 TESetText(ctrl
->text
.label
, strlen(ctrl
->text
.label
), te
);
509 height
= TEGetHeight(1, (*te
)->nLines
, te
);
512 SizeControl(mc
->text
.tbctrl
, curstate
->width
, height
);
513 curstate
->pos
.v
+= height
+ 6;
514 add234(mcs
->byctrl
, mc
);
515 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
516 mcs
->panels
[curstate
->panelnum
] = mc
;
519 static void macctrl_editbox(struct macctrls
*mcs
, WindowPtr window
,
520 struct mac_layoutstate
*curstate
,
523 union macctrl
*mc
= snew(union macctrl
);
524 Rect lbounds
, bounds
;
526 mc
->generic
.type
= MACCTRL_EDITBOX
;
527 mc
->generic
.ctrl
= ctrl
;
528 mc
->generic
.privdata
= NULL
;
529 lbounds
.left
= curstate
->pos
.h
;
530 lbounds
.top
= curstate
->pos
.v
;
531 if (ctrl
->editbox
.percentwidth
== 100) {
532 if (ctrl
->editbox
.label
!= NULL
) {
533 lbounds
.right
= lbounds
.left
+ curstate
->width
;
534 lbounds
.bottom
= lbounds
.top
+ 16;
535 curstate
->pos
.v
+= 18;
537 bounds
.left
= curstate
->pos
.h
;
538 bounds
.right
= bounds
.left
+ curstate
->width
;
540 lbounds
.right
= lbounds
.left
+
541 curstate
->width
* (100 - ctrl
->editbox
.percentwidth
) / 100;
542 lbounds
.bottom
= lbounds
.top
+ 22;
543 bounds
.left
= lbounds
.right
;
544 bounds
.right
= lbounds
.left
+ curstate
->width
;
546 bounds
.top
= curstate
->pos
.v
;
547 bounds
.bottom
= bounds
.top
+ 22;
548 if (mac_gestalts
.apprvers
>= 0x100) {
549 if (ctrl
->editbox
.label
== NULL
)
550 mc
->editbox
.tblabel
= NULL
;
552 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, FALSE
,
553 0, 0, 0, kControlStaticTextProc
,
555 SetControlData(mc
->editbox
.tblabel
, kControlEntireControl
,
556 kControlStaticTextTextTag
,
557 strlen(ctrl
->editbox
.label
), ctrl
->editbox
.label
);
559 InsetRect(&bounds
, 3, 3);
560 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
561 ctrl
->editbox
.password ?
562 kControlEditTextPasswordProc
:
563 kControlEditTextProc
, (long)mc
);
565 #if !TARGET_API_MAC_CARBON
567 if (ctrl
->editbox
.label
== NULL
)
568 mc
->editbox
.tblabel
= NULL
;
570 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, FALSE
,
571 0, 0, 0, SYS7_TEXT_PROC
,
573 TESetText(ctrl
->editbox
.label
, strlen(ctrl
->editbox
.label
),
574 (TEHandle
)(*mc
->editbox
.tblabel
)->contrlData
);
576 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
, 0, 0, 0,
577 SYS7_EDITBOX_PROC
, (long)mc
);
580 curstate
->pos
.v
+= 28;
581 add234(mcs
->byctrl
, mc
);
582 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
583 mcs
->panels
[curstate
->panelnum
] = mc
;
584 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
587 #if !TARGET_API_MAC_CARBON
588 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16 variant
,
590 ControlDefProcMessage msg
,
601 rect
= (*control
)->contrlRect
;
602 if (variant
== SYS7_EDITBOX_VARIANT
)
603 InsetRect(&rect
, 3, 3); /* 2 if it's 20 pixels high */
604 te
= TENew(&rect
, &rect
);
605 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
606 (*te
)->txSize
= LoWord(ssfs
);
607 (*te
)->txFont
= HiWord(ssfs
);
608 (*control
)->contrlData
= (Handle
)te
;
611 TEDispose((TEHandle
)(*control
)->contrlData
);
614 if ((*control
)->contrlVis
) {
615 rect
= (*control
)->contrlRect
;
616 if (variant
== SYS7_EDITBOX_VARIANT
) {
619 InsetRect(&rect
, 3, 3);
622 (*(TEHandle
)(*control
)->contrlData
)->viewRect
= rect
;
623 TEUpdate(&rect
, (TEHandle
)(*control
)->contrlData
);
627 if (variant
== SYS7_TEXT_VARIANT
)
628 return kControlNoPart
;
629 mouse
.h
= LoWord(param
);
630 mouse
.v
= HiWord(param
);
631 rect
= (*control
)->contrlRect
;
632 InsetRect(&rect
, 3, 3);
633 return PtInRect(mouse
, &rect
) ? kControlEditTextPart
: kControlNoPart
;
635 if (param
& (1 << 31)) {
641 rgn
= (RgnHandle
)param
;
642 RectRgn(rgn
, &(*control
)->contrlRect
);
646 rgn
= (RgnHandle
)param
;
655 static void macctrl_radio(struct macctrls
*mcs
, WindowPtr window
,
656 struct mac_layoutstate
*curstate
,
659 union macctrl
*mc
= snew(union macctrl
);
662 unsigned int i
, colwidth
;
664 mc
->generic
.type
= MACCTRL_RADIO
;
665 mc
->generic
.ctrl
= ctrl
;
666 mc
->generic
.privdata
= NULL
;
667 mc
->radio
.tbctrls
= snewn(ctrl
->radio
.nbuttons
, ControlRef
);
668 colwidth
= (curstate
->width
+ 13) / ctrl
->radio
.ncolumns
;
669 bounds
.top
= curstate
->pos
.v
;
670 bounds
.bottom
= bounds
.top
+ 16;
671 bounds
.left
= curstate
->pos
.h
;
672 bounds
.right
= bounds
.left
+ curstate
->width
;
673 if (ctrl
->radio
.label
== NULL
)
674 mc
->radio
.tblabel
= NULL
;
676 if (mac_gestalts
.apprvers
>= 0x100) {
677 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, FALSE
,
678 0, 0, 0, kControlStaticTextProc
,
680 SetControlData(mc
->radio
.tblabel
, kControlEntireControl
,
681 kControlStaticTextTextTag
,
682 strlen(ctrl
->radio
.label
), ctrl
->radio
.label
);
684 #if !TARGET_API_MAC_CARBON
686 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, FALSE
,
687 0, 0, 0, SYS7_TEXT_PROC
, (long)mc
);
688 TESetText(ctrl
->radio
.label
, strlen(ctrl
->radio
.label
),
689 (TEHandle
)(*mc
->radio
.tblabel
)->contrlData
);
692 curstate
->pos
.v
+= 18;
694 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
695 bounds
.top
= curstate
->pos
.v
- 2;
696 bounds
.bottom
= bounds
.top
+ 18;
697 bounds
.left
= curstate
->pos
.h
+ colwidth
* (i
% ctrl
->radio
.ncolumns
);
698 if (i
== ctrl
->radio
.nbuttons
- 1 ||
699 i
% ctrl
->radio
.ncolumns
== ctrl
->radio
.ncolumns
- 1) {
700 bounds
.right
= curstate
->pos
.h
+ curstate
->width
;
701 curstate
->pos
.v
+= 18;
703 bounds
.right
= bounds
.left
+ colwidth
- 13;
704 c2pstrcpy(title
, ctrl
->radio
.buttons
[i
]);
705 mc
->radio
.tbctrls
[i
] = NewControl(window
, &bounds
, title
, FALSE
,
706 0, 0, 1, radioButProc
, (long)mc
);
708 curstate
->pos
.v
+= 4;
709 add234(mcs
->byctrl
, mc
);
710 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
711 mcs
->panels
[curstate
->panelnum
] = mc
;
712 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
715 static void macctrl_checkbox(struct macctrls
*mcs
, WindowPtr window
,
716 struct mac_layoutstate
*curstate
,
719 union macctrl
*mc
= snew(union macctrl
);
723 assert(ctrl
->checkbox
.label
!= NULL
);
724 mc
->generic
.type
= MACCTRL_CHECKBOX
;
725 mc
->generic
.ctrl
= ctrl
;
726 mc
->generic
.privdata
= NULL
;
727 bounds
.left
= curstate
->pos
.h
;
728 bounds
.right
= bounds
.left
+ curstate
->width
;
729 bounds
.top
= curstate
->pos
.v
;
730 bounds
.bottom
= bounds
.top
+ 16;
731 c2pstrcpy(title
, ctrl
->checkbox
.label
);
732 mc
->checkbox
.tbctrl
= NewControl(window
, &bounds
, title
, FALSE
, 0, 0, 1,
733 checkBoxProc
, (long)mc
);
734 add234(mcs
->byctrl
, mc
);
735 curstate
->pos
.v
+= 22;
736 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
737 mcs
->panels
[curstate
->panelnum
] = mc
;
738 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
741 static void macctrl_button(struct macctrls
*mcs
, WindowPtr window
,
742 struct mac_layoutstate
*curstate
,
745 union macctrl
*mc
= snew(union macctrl
);
749 assert(ctrl
->button
.label
!= NULL
);
750 mc
->generic
.type
= MACCTRL_BUTTON
;
751 mc
->generic
.ctrl
= ctrl
;
752 mc
->generic
.privdata
= NULL
;
753 bounds
.left
= curstate
->pos
.h
;
754 bounds
.right
= bounds
.left
+ curstate
->width
;
755 bounds
.top
= curstate
->pos
.v
;
756 bounds
.bottom
= bounds
.top
+ 20;
757 c2pstrcpy(title
, ctrl
->button
.label
);
758 mc
->button
.tbctrl
= NewControl(window
, &bounds
, title
, FALSE
, 0, 0, 1,
759 pushButProc
, (long)mc
);
760 mc
->button
.tbring
= NULL
;
761 if (mac_gestalts
.apprvers
>= 0x100) {
762 Boolean isdefault
= ctrl
->button
.isdefault
;
764 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
765 kControlPushButtonDefaultTag
,
766 sizeof(isdefault
), &isdefault
);
767 } else if (ctrl
->button
.isdefault
) {
768 InsetRect(&bounds
, -4, -4);
769 mc
->button
.tbring
= NewControl(window
, &bounds
, title
, FALSE
, 0, 0, 1,
770 SYS7_DEFAULT_PROC
, (long)mc
);
772 if (mac_gestalts
.apprvers
>= 0x110) {
773 Boolean iscancel
= ctrl
->button
.iscancel
;
775 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
776 kControlPushButtonCancelTag
,
777 sizeof(iscancel
), &iscancel
);
779 if (ctrl
->button
.isdefault
)
781 if (ctrl
->button
.iscancel
)
783 add234(mcs
->byctrl
, mc
);
784 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
785 mcs
->panels
[curstate
->panelnum
] = mc
;
786 curstate
->pos
.v
+= 26;
789 #if !TARGET_API_MAC_CARBON
790 static pascal SInt32
macctrl_sys7_default_cdef(SInt16 variant
,
792 ControlDefProcMessage msg
,
802 if ((*control
)->contrlVis
) {
803 rect
= (*control
)->contrlRect
;
804 GetPenState(&savestate
);
807 if ((*control
)->contrlHilite
== kControlInactivePart
)
809 oval
= (rect
.bottom
- rect
.top
) / 2 + 2;
810 FrameRoundRect(&rect
, oval
, oval
);
811 SetPenState(&savestate
);
815 if (param
& (1 << 31)) {
821 rgn
= (RgnHandle
)param
;
822 RectRgn(rgn
, &(*control
)->contrlRect
);
826 rgn
= (RgnHandle
)param
;
835 static void macctrl_listbox(struct macctrls
*mcs
, WindowPtr window
,
836 struct mac_layoutstate
*curstate
,
839 union macctrl
*mc
= snew(union macctrl
);
844 assert(ctrl
->listbox
.percentwidth
== 100);
845 mc
->generic
.type
= MACCTRL_LISTBOX
;
846 mc
->generic
.ctrl
= ctrl
;
847 mc
->generic
.privdata
= NULL
;
848 /* The list starts off empty */
849 mc
->listbox
.nids
= 0;
850 mc
->listbox
.ids
= NULL
;
851 bounds
.left
= curstate
->pos
.h
;
852 bounds
.right
= bounds
.left
+ curstate
->width
;
853 bounds
.top
= curstate
->pos
.v
;
854 bounds
.bottom
= bounds
.top
+ 16 * ctrl
->listbox
.height
+ 2;
856 if (mac_gestalts
.apprvers
>= 0x100) {
857 InsetRect(&bounds
, 3, 3);
858 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
,
860 kControlListBoxProc
, (long)mc
);
861 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
862 kControlListBoxListHandleTag
,
863 sizeof(mc
->listbox
.list
), &mc
->listbox
.list
,
865 DisposeControl(mc
->listbox
.tbctrl
);
870 #if !TARGET_API_MAC_CARBON
872 InsetRect(&bounds
, -3, -3);
873 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, FALSE
,
875 SYS7_LISTBOX_PROC
, (long)mc
);
876 mc
->listbox
.list
= (ListHandle
)(*mc
->listbox
.tbctrl
)->contrlData
;
877 (*mc
->listbox
.list
)->refCon
= (long)mc
;
880 if (!ctrl
->listbox
.multisel
) {
881 #if TARGET_API_MAC_CARBON
882 SetListSelectionFlags(mc
->listbox
.list
, lOnlyOne
);
884 (*mc
->listbox
.list
)->selFlags
= lOnlyOne
;
887 add234(mcs
->byctrl
, mc
);
888 curstate
->pos
.v
+= 6 + 16 * ctrl
->listbox
.height
+ 2;
889 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
890 mcs
->panels
[curstate
->panelnum
] = mc
;
891 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
892 #if TARGET_API_MAC_CARBON
893 HideControl(GetListVerticalScrollBar(mc
->listbox
.list
));
895 HideControl((*mc
->listbox
.list
)->vScroll
);
899 #if !TARGET_API_MAC_CARBON
900 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16 variant
,
902 ControlDefProcMessage msg
,
918 rect
= (*control
)->contrlRect
;
919 InsetRect(&rect
, 4, 4);
920 rect
.right
-= 15; /* scroll bar */
921 bounds
.top
= bounds
.bottom
= bounds
.left
= 0;
923 csize
.h
= csize
.v
= 0;
925 savefont
= curport
->txFont
;
926 savesize
= curport
->txSize
;
927 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
928 TextFont(HiWord(ssfs
));
929 TextSize(LoWord(ssfs
));
930 list
= LNew(&rect
, &bounds
, csize
, 0, (*control
)->contrlOwner
,
931 TRUE
, FALSE
, FALSE
, TRUE
);
932 SetControlReference((*list
)->vScroll
, (long)list
);
933 (*control
)->contrlData
= (Handle
)list
;
939 * If the dialogue box is being destroyed, the scroll bar
940 * might have gone already. In our situation, this is the
941 * only time we destroy a control, so NULL out the scroll bar
942 * handle to prevent LDispose trying to free it.
944 list
= (ListHandle
)(*control
)->contrlData
;
945 (*list
)->vScroll
= NULL
;
949 if ((*control
)->contrlVis
) {
950 rect
= (*control
)->contrlRect
;
951 /* XXX input focus highlighting? */
952 InsetRect(&rect
, 3, 3);
955 list
= (ListHandle
)(*control
)->contrlData
;
956 LActivate((*control
)->contrlHilite
!= kControlInactivePart
, list
);
958 LUpdate(curport
->visRgn
, list
);
962 mouse
.h
= LoWord(param
);
963 mouse
.v
= HiWord(param
);
964 rect
= (*control
)->contrlRect
;
965 InsetRect(&rect
, 4, 4);
967 * We deliberately exclude the scrollbar so that LClick() can see it.
970 return PtInRect(mouse
, &rect
) ? kControlListBoxPart
: kControlNoPart
;
972 if (param
& (1 << 31)) {
978 rgn
= (RgnHandle
)param
;
979 RectRgn(rgn
, &(*control
)->contrlRect
);
983 rgn
= (RgnHandle
)param
;
992 static void macctrl_popup(struct macctrls
*mcs
, WindowPtr window
,
993 struct mac_layoutstate
*curstate
,
996 union macctrl
*mc
= snew(union macctrl
);
999 unsigned int labelwidth
;
1000 static int nextmenuid
= MENU_MIN
;
1005 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
1006 * create a popup menu with dynamic content.
1008 assert(ctrl
->listbox
.height
== 0);
1009 assert(!ctrl
->listbox
.draglist
);
1010 assert(!ctrl
->listbox
.multisel
);
1012 mc
->generic
.type
= MACCTRL_POPUP
;
1013 mc
->generic
.ctrl
= ctrl
;
1014 mc
->generic
.privdata
= NULL
;
1015 c2pstrcpy(title
, ctrl
->button
.label
== NULL ?
"" : ctrl
->button
.label
);
1017 /* Find a spare menu ID and create the menu */
1018 while (GetMenuHandle(nextmenuid
) != NULL
)
1019 if (++nextmenuid
>= MENU_MAX
) nextmenuid
= MENU_MIN
;
1020 menuid
= nextmenuid
++;
1021 menu
= NewMenu(menuid
, "\pdummy");
1022 if (menu
== NULL
) return;
1023 mc
->popup
.menu
= menu
;
1024 mc
->popup
.menuid
= menuid
;
1025 InsertMenu(menu
, kInsertHierarchicalMenu
);
1027 /* The menu starts off empty */
1029 mc
->popup
.ids
= NULL
;
1031 bounds
.left
= curstate
->pos
.h
;
1032 bounds
.right
= bounds
.left
+ curstate
->width
;
1033 bounds
.top
= curstate
->pos
.v
;
1034 bounds
.bottom
= bounds
.top
+ 20;
1035 /* XXX handle percentwidth == 100 */
1036 labelwidth
= curstate
->width
* (100 - ctrl
->listbox
.percentwidth
) / 100;
1037 mc
->popup
.tbctrl
= NewControl(window
, &bounds
, title
, FALSE
,
1038 popupTitleLeftJust
, menuid
, labelwidth
,
1039 popupMenuProc
+ popupFixedWidth
, (long)mc
);
1040 add234(mcs
->byctrl
, mc
);
1041 curstate
->pos
.v
+= 26;
1042 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
1043 mcs
->panels
[curstate
->panelnum
] = mc
;
1044 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1048 void macctrl_activate(WindowPtr window
, EventRecord
*event
)
1050 struct macctrls
*mcs
= mac_winctrls(window
);
1051 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1054 ControlPartCode state
;
1058 SetPort((GrafPtr
)GetWindowPort(window
));
1059 if (mac_gestalts
.apprvers
>= 0x100)
1060 SetThemeWindowBackground(window
, active ?
1061 kThemeBrushModelessDialogBackgroundActive
:
1062 kThemeBrushModelessDialogBackgroundInactive
,
1064 state
= active ? kControlNoPart
: kControlInactivePart
;
1065 for (i
= 0; i
<= mcs
->curpanel
; i
+= mcs
->curpanel
)
1066 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
1067 switch (mc
->generic
.type
) {
1069 HiliteControl(mc
->text
.tbctrl
, state
);
1071 case MACCTRL_EDITBOX
:
1072 HiliteControl(mc
->editbox
.tbctrl
, state
);
1073 if (mc
->editbox
.tblabel
!= NULL
)
1074 HiliteControl(mc
->editbox
.tblabel
, state
);
1077 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
1078 HiliteControl(mc
->radio
.tbctrls
[j
], state
);
1079 if (mc
->radio
.tblabel
!= NULL
)
1080 HiliteControl(mc
->radio
.tblabel
, state
);
1082 case MACCTRL_CHECKBOX
:
1083 HiliteControl(mc
->checkbox
.tbctrl
, state
);
1085 case MACCTRL_BUTTON
:
1086 HiliteControl(mc
->button
.tbctrl
, state
);
1087 if (mc
->button
.tbring
!= NULL
)
1088 HiliteControl(mc
->button
.tbring
, state
);
1090 case MACCTRL_LISTBOX
:
1091 HiliteControl(mc
->listbox
.tbctrl
, state
);
1094 HiliteControl(mc
->popup
.tbctrl
, state
);
1097 #if !TARGET_API_MAC_CARBON
1098 if (mcs
->focus
== mc
) {
1100 macctrl_enfocus(mc
);
1102 macctrl_defocus(mc
);
1109 void macctrl_click(WindowPtr window
, EventRecord
*event
)
1112 ControlHandle control
, oldfocus
;
1113 int part
, trackresult
;
1116 struct macctrls
*mcs
= mac_winctrls(window
);
1121 SetPort((GrafPtr
)GetWindowPort(window
));
1122 mouse
= event
->where
;
1123 GlobalToLocal(&mouse
);
1124 part
= FindControl(mouse
, window
, &control
);
1125 if (control
!= NULL
) {
1126 #if !TARGET_API_MAC_CARBON
1128 * Special magic for scroll bars in list boxes, whose refcon
1131 if (part
== kControlUpButtonPart
|| part
== kControlDownButtonPart
||
1132 part
== kControlPageUpPart
|| part
== kControlPageDownPart
||
1133 part
== kControlIndicatorPart
)
1134 mc
= (union macctrl
*)
1135 (*(ListHandle
)GetControlReference(control
))->refCon
;
1138 mc
= (union macctrl
*)GetControlReference(control
);
1139 if (mac_gestalts
.apprvers
>= 0x100) {
1140 if (GetControlFeatures(control
, &features
) == noErr
&&
1141 (features
& kControlSupportsFocus
) &&
1142 (features
& kControlGetsFocusOnClick
) &&
1143 GetKeyboardFocus(window
, &oldfocus
) == noErr
&&
1144 control
!= oldfocus
)
1145 SetKeyboardFocus(window
, control
, part
);
1146 trackresult
= HandleControlClick(control
, mouse
, event
->modifiers
,
1147 (ControlActionUPP
)-1);
1149 #if !TARGET_API_MAC_CARBON
1150 if (mc
->generic
.type
== MACCTRL_EDITBOX
&&
1151 control
== mc
->editbox
.tbctrl
) {
1152 TEHandle te
= (TEHandle
)(*control
)->contrlData
;
1154 macctrl_setfocus(mcs
, mc
);
1155 TEClick(mouse
, !!(event
->modifiers
& shiftKey
), te
);
1158 if (mc
->generic
.type
== MACCTRL_LISTBOX
&&
1159 (control
== mc
->listbox
.tbctrl
||
1160 control
== (*mc
->listbox
.list
)->vScroll
)) {
1162 macctrl_setfocus(mcs
, mc
);
1163 if (LClick(mouse
, event
->modifiers
, mc
->listbox
.list
))
1165 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1167 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1171 trackresult
= TrackControl(control
, mouse
, (ControlActionUPP
)-1);
1173 switch (mc
->generic
.type
) {
1175 if (trackresult
!= 0) {
1176 for (i
= 0; i
< mc
->generic
.ctrl
->radio
.nbuttons
; i
++)
1177 if (mc
->radio
.tbctrls
[i
] == control
)
1178 SetControlValue(mc
->radio
.tbctrls
[i
],
1179 kControlRadioButtonCheckedValue
);
1181 SetControlValue(mc
->radio
.tbctrls
[i
],
1182 kControlRadioButtonUncheckedValue
);
1183 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1186 case MACCTRL_CHECKBOX
:
1187 if (trackresult
!= 0) {
1188 SetControlValue(control
, !GetControlValue(control
));
1189 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1192 case MACCTRL_BUTTON
:
1193 if (trackresult
!= 0)
1194 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1196 case MACCTRL_LISTBOX
:
1197 /* FIXME spot double-click */
1198 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1201 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1209 void macctrl_key(WindowPtr window
, EventRecord
*event
)
1212 struct macctrls
*mcs
= mac_winctrls(window
);
1214 unsigned long dummy
;
1216 switch (event
->message
& charCodeMask
) {
1217 case kEnterCharCode
:
1218 case kReturnCharCode
:
1219 if (mcs
->defbutton
!= NULL
) {
1220 assert(mcs
->defbutton
->generic
.type
== MACCTRL_BUTTON
);
1221 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlButtonPart
);
1223 * I'd like to delay unhilighting the button until after
1224 * the event has been processed, but by them the entire
1225 * dialgue box might have been destroyed.
1228 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1229 ctrlevent(mcs
, mcs
->defbutton
, EVENT_ACTION
);
1232 case kEscapeCharCode
:
1233 if (mcs
->canbutton
!= NULL
) {
1234 assert(mcs
->canbutton
->generic
.type
== MACCTRL_BUTTON
);
1235 HiliteControl(mcs
->canbutton
->button
.tbctrl
, kControlButtonPart
);
1237 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1238 ctrlevent(mcs
, mcs
->canbutton
, EVENT_ACTION
);
1242 if (mac_gestalts
.apprvers
>= 0x100) {
1243 if (GetKeyboardFocus(window
, &control
) == noErr
&& control
!= NULL
) {
1244 HandleControlKey(control
, (event
->message
& keyCodeMask
) >> 8,
1245 event
->message
& charCodeMask
, event
->modifiers
);
1246 mc
= (union macctrl
*)GetControlReference(control
);
1247 switch (mc
->generic
.type
) {
1248 case MACCTRL_LISTBOX
:
1249 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1252 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1257 #if !TARGET_API_MAC_CARBON
1261 if (mcs
->focus
!= NULL
) {
1263 switch (mc
->generic
.type
) {
1264 case MACCTRL_EDITBOX
:
1265 te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1266 TEKey(event
->message
& charCodeMask
, te
);
1267 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1275 void macctrl_update(WindowPtr window
)
1277 #if TARGET_API_MAC_CARBON
1283 BeginUpdate(window
);
1285 SetPort((GrafPtr
)GetWindowPort(window
));
1286 if (mac_gestalts
.apprvers
>= 0x101) {
1287 #if TARGET_API_MAC_CARBON
1288 GetPortBounds(GetWindowPort(window
), &rect
);
1290 rect
= window
->portRect
;
1292 InsetRect(&rect
, -1, -1);
1293 DrawThemeModelessDialogFrame(&rect
, mac_frontwindow() == window ?
1294 kThemeStateActive
: kThemeStateInactive
);
1296 #if TARGET_API_MAC_CARBON
1298 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1299 UpdateControls(window
, visrgn
);
1302 UpdateControls(window
, window
->visRgn
);
1308 #if TARGET_API_MAC_CARBON
1309 #define EnableItem EnableMenuItem
1310 #define DisableItem DisableMenuItem
1312 void macctrl_adjustmenus(WindowPtr window
)
1316 menu
= GetMenuHandle(mFile
);
1317 DisableItem(menu
, iSave
); /* XXX enable if modified */
1318 EnableItem(menu
, iSaveAs
);
1319 EnableItem(menu
, iDuplicate
);
1321 menu
= GetMenuHandle(mEdit
);
1322 DisableItem(menu
, 0);
1325 void macctrl_close(WindowPtr window
)
1327 struct macctrls
*mcs
= mac_winctrls(window
);
1331 * Mostly, we don't bother disposing of the Toolbox controls,
1332 * since that will happen automatically when the window is
1333 * disposed of. Popup menus are an exception, because we have to
1334 * dispose of the menu ourselves, and doing that while the control
1335 * still holds a reference to it seems rude.
1337 while ((mc
= index234(mcs
->byctrl
, 0)) != NULL
) {
1338 if (mc
->generic
.privdata
!= NULL
&& mc
->generic
.freeprivdata
)
1339 sfree(mc
->generic
.privdata
);
1340 switch (mc
->generic
.type
) {
1342 DisposeControl(mc
->popup
.tbctrl
);
1343 DeleteMenu(mc
->popup
.menuid
);
1344 DisposeMenu(mc
->popup
.menu
);
1347 del234(mcs
->byctrl
, mc
);
1351 freetree234(mcs
->byctrl
);
1357 void dlg_update_start(union control
*ctrl
, void *dlg
)
1363 void dlg_update_done(union control
*ctrl
, void *dlg
)
1369 void dlg_set_focus(union control
*ctrl
, void *dlg
)
1372 if (mac_gestalts
.apprvers
>= 0x100) {
1373 /* Use SetKeyboardFocus() */
1375 /* Do our own mucking around */
1379 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1385 void dlg_beep(void *dlg
)
1391 void dlg_error_msg(void *dlg
, char *msg
)
1395 c2pstrcpy(pmsg
, msg
);
1396 ParamText(pmsg
, NULL
, NULL
, NULL
);
1397 StopAlert(128, NULL
);
1400 void dlg_end(void *dlg
, int value
)
1402 struct macctrls
*mcs
= dlg
;
1404 if (mcs
->end
!= NULL
)
1405 (*mcs
->end
)(mcs
->window
, value
);
1408 void dlg_refresh(union control
*ctrl
, void *dlg
)
1410 struct macctrls
*mcs
= dlg
;
1415 /* NULL means refresh every control */
1416 for (i
= 0 ; i
< mcs
->npanels
; i
++) {
1417 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
1418 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1423 /* Just refresh a specific control */
1424 mc
= findbyctrl(mcs
, ctrl
);
1426 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1429 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
1431 struct macctrls
*mcs
= dlg
;
1432 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1435 return mc
->generic
.privdata
;
1438 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
1440 struct macctrls
*mcs
= dlg
;
1441 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1444 mc
->generic
.privdata
= ptr
;
1445 mc
->generic
.freeprivdata
= FALSE
;
1448 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
1450 struct macctrls
*mcs
= dlg
;
1451 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1454 mc
->generic
.privdata
= smalloc(size
);
1455 mc
->generic
.freeprivdata
= TRUE
;
1456 return mc
->generic
.privdata
;
1461 * Radio Button control
1464 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
1466 struct macctrls
*mcs
= dlg
;
1467 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1470 if (mc
== NULL
) return;
1471 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1472 if (i
== whichbutton
)
1473 SetControlValue(mc
->radio
.tbctrls
[i
],
1474 kControlRadioButtonCheckedValue
);
1476 SetControlValue(mc
->radio
.tbctrls
[i
],
1477 kControlRadioButtonUncheckedValue
);
1482 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
1484 struct macctrls
*mcs
= dlg
;
1485 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1489 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1490 if (GetControlValue(mc
->radio
.tbctrls
[i
]) ==
1491 kControlRadioButtonCheckedValue
)
1502 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
1504 struct macctrls
*mcs
= dlg
;
1505 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1507 if (mc
== NULL
) return;
1508 SetControlValue(mc
->checkbox
.tbctrl
,
1509 checked ? kControlCheckBoxCheckedValue
:
1510 kControlCheckBoxUncheckedValue
);
1513 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
1515 struct macctrls
*mcs
= dlg
;
1516 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1519 return GetControlValue(mc
->checkbox
.tbctrl
);
1527 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
1529 struct macctrls
*mcs
= dlg
;
1530 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1533 if (mc
== NULL
) return;
1534 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1536 SetPort((GrafPtr
)(GetWindowPort(mcs
->window
)));
1537 if (mac_gestalts
.apprvers
>= 0x100)
1538 SetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1539 ctrl
->editbox
.password ?
1540 kControlEditTextPasswordTag
:
1541 kControlEditTextTextTag
,
1542 strlen(text
), text
);
1543 #if !TARGET_API_MAC_CARBON
1545 TESetText(text
, strlen(text
),
1546 (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
1548 DrawOneControl(mc
->editbox
.tbctrl
);
1552 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
1554 struct macctrls
*mcs
= dlg
;
1555 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1559 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1560 if (mac_gestalts
.apprvers
>= 0x100) {
1561 if (GetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1562 ctrl
->editbox
.password ?
1563 kControlEditTextPasswordTag
:
1564 kControlEditTextTextTag
,
1565 length
- 1, buffer
, &olen
) != noErr
)
1567 if (olen
> length
- 1)
1570 #if !TARGET_API_MAC_CARBON
1572 TEHandle te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1574 olen
= (*te
)->teLength
;
1575 if (olen
> length
- 1)
1577 memcpy(buffer
, *(*te
)->hText
, olen
);
1580 buffer
[olen
] = '\0';
1588 static void dlg_macpopup_clear(union control
*ctrl
, void *dlg
)
1590 struct macctrls
*mcs
= dlg
;
1591 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1592 MenuRef menu
= mc
->popup
.menu
;
1595 if (mc
== NULL
) return;
1596 n
= CountMenuItems(menu
);
1597 for (i
= 0; i
< n
; i
++)
1598 DeleteMenuItem(menu
, n
- i
);
1600 sfree(mc
->popup
.ids
);
1601 mc
->popup
.ids
= NULL
;
1602 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1605 static void dlg_maclist_clear(union control
*ctrl
, void *dlg
)
1607 struct macctrls
*mcs
= dlg
;
1608 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1610 if (mc
== NULL
) return;
1611 LDelRow(0, 0, mc
->listbox
.list
);
1612 mc
->listbox
.nids
= 0;
1613 sfree(mc
->listbox
.ids
);
1614 mc
->listbox
.ids
= NULL
;
1615 DrawOneControl(mc
->listbox
.tbctrl
);
1618 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
1621 switch (ctrl
->generic
.type
) {
1623 if (ctrl
->listbox
.height
== 0)
1624 dlg_macpopup_clear(ctrl
, dlg
);
1626 dlg_maclist_clear(ctrl
, dlg
);
1631 static void dlg_macpopup_del(union control
*ctrl
, void *dlg
, int index
)
1633 struct macctrls
*mcs
= dlg
;
1634 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1635 MenuRef menu
= mc
->popup
.menu
;
1637 if (mc
== NULL
) return;
1638 DeleteMenuItem(menu
, index
+ 1);
1639 if (mc
->popup
.ids
!= NULL
)
1640 memcpy(mc
->popup
.ids
+ index
, mc
->popup
.ids
+ index
+ 1,
1641 (mc
->popup
.nids
- index
- 1) * sizeof(*mc
->popup
.ids
));
1642 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1645 static void dlg_maclist_del(union control
*ctrl
, void *dlg
, int index
)
1647 struct macctrls
*mcs
= dlg
;
1648 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1650 if (mc
== NULL
) return;
1651 LDelRow(1, index
, mc
->listbox
.list
);
1652 if (mc
->listbox
.ids
!= NULL
)
1653 memcpy(mc
->listbox
.ids
+ index
, mc
->listbox
.ids
+ index
+ 1,
1654 (mc
->listbox
.nids
- index
- 1) * sizeof(*mc
->listbox
.ids
));
1655 DrawOneControl(mc
->listbox
.tbctrl
);
1658 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
1661 switch (ctrl
->generic
.type
) {
1663 if (ctrl
->listbox
.height
== 0)
1664 dlg_macpopup_del(ctrl
, dlg
, index
);
1666 dlg_maclist_del(ctrl
, dlg
, index
);
1671 static void dlg_macpopup_add(union control
*ctrl
, void *dlg
, char const *text
)
1673 struct macctrls
*mcs
= dlg
;
1674 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1675 MenuRef menu
= mc
->popup
.menu
;
1678 if (mc
== NULL
) return;
1679 assert(text
[0] != '\0');
1680 c2pstrcpy(itemstring
, text
);
1681 AppendMenu(menu
, "\pdummy");
1682 SetMenuItemText(menu
, CountMenuItems(menu
), itemstring
);
1683 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1687 static void dlg_maclist_add(union control
*ctrl
, void *dlg
, char const *text
)
1689 struct macctrls
*mcs
= dlg
;
1690 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1692 Cell cell
= { 0, 0 };
1694 if (mc
== NULL
) return;
1695 #if TARGET_API_MAC_CARBON
1696 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1698 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1700 cell
.v
= bounds
.bottom
;
1701 LAddRow(1, cell
.v
, mc
->listbox
.list
);
1702 LSetCell(text
, strlen(text
), cell
, mc
->listbox
.list
);
1703 DrawOneControl(mc
->listbox
.tbctrl
);
1706 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
1709 switch (ctrl
->generic
.type
) {
1711 if (ctrl
->listbox
.height
== 0)
1712 dlg_macpopup_add(ctrl
, dlg
, text
);
1714 dlg_maclist_add(ctrl
, dlg
, text
);
1719 static void dlg_macpopup_addwithid(union control
*ctrl
, void *dlg
,
1720 char const *text
, int id
)
1722 struct macctrls
*mcs
= dlg
;
1723 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1724 MenuRef menu
= mc
->popup
.menu
;
1727 if (mc
== NULL
) return;
1728 dlg_macpopup_add(ctrl
, dlg
, text
);
1729 index
= CountMenuItems(menu
) - 1;
1730 if (mc
->popup
.nids
<= index
) {
1731 mc
->popup
.nids
= index
+ 1;
1732 mc
->popup
.ids
= sresize(mc
->popup
.ids
, mc
->popup
.nids
, int);
1734 mc
->popup
.ids
[index
] = id
;
1737 static void dlg_maclist_addwithid(union control
*ctrl
, void *dlg
,
1738 char const *text
, int id
)
1740 struct macctrls
*mcs
= dlg
;
1741 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1745 if (mc
== NULL
) return;
1746 dlg_maclist_add(ctrl
, dlg
, text
);
1747 #if TARGET_API_MAC_CARBON
1748 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1750 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1752 index
= bounds
.bottom
;
1753 if (mc
->listbox
.nids
<= index
) {
1754 mc
->listbox
.nids
= index
+ 1;
1755 mc
->listbox
.ids
= sresize(mc
->listbox
.ids
, mc
->listbox
.nids
, int);
1757 mc
->listbox
.ids
[index
] = id
;
1760 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
1761 char const *text
, int id
)
1764 switch (ctrl
->generic
.type
) {
1766 if (ctrl
->listbox
.height
== 0)
1767 dlg_macpopup_addwithid(ctrl
, dlg
, text
, id
);
1769 dlg_maclist_addwithid(ctrl
, dlg
, text
, id
);
1774 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
1776 struct macctrls
*mcs
= dlg
;
1777 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1780 switch (ctrl
->generic
.type
) {
1782 if (ctrl
->listbox
.height
== 0) {
1783 assert(mc
->popup
.ids
!= NULL
&& mc
->popup
.nids
> index
);
1784 return mc
->popup
.ids
[index
];
1786 assert(mc
->listbox
.ids
!= NULL
&& mc
->listbox
.nids
> index
);
1787 return mc
->listbox
.ids
[index
];
1793 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
1795 struct macctrls
*mcs
= dlg
;
1796 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1797 Cell cell
= { 0, 0 };
1800 switch (ctrl
->generic
.type
) {
1802 if (ctrl
->listbox
.height
== 0)
1803 return GetControlValue(mc
->popup
.tbctrl
) - 1;
1805 if (LGetSelect(TRUE
, &cell
, mc
->listbox
.list
))
1814 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
1816 struct macctrls
*mcs
= dlg
;
1817 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1818 Cell cell
= { 0, 0 };
1821 switch (ctrl
->generic
.type
) {
1823 if (ctrl
->listbox
.height
== 0)
1824 return GetControlValue(mc
->popup
.tbctrl
) - 1 == index
;
1827 return LGetSelect(FALSE
, &cell
, mc
->listbox
.list
);
1833 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
1835 struct macctrls
*mcs
= dlg
;
1836 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1838 if (mc
== NULL
) return;
1839 switch (ctrl
->generic
.type
) {
1841 if (ctrl
->listbox
.height
== 0)
1842 SetControlValue(mc
->popup
.tbctrl
, index
+ 1);
1852 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
1854 struct macctrls
*mcs
= dlg
;
1855 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1857 if (mc
== NULL
) return;
1858 if (mac_gestalts
.apprvers
>= 0x100)
1859 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
1860 kControlStaticTextTextTag
, strlen(text
), text
);
1861 #if !TARGET_API_MAC_CARBON
1863 TESetText(text
, strlen(text
),
1864 (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
);
1870 * File Selector control
1873 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
1878 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
1885 * Font Selector control
1888 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fn
)
1893 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fn
)
1900 * Printer enumeration
1903 printer_enum
*printer_start_enum(int *nprinters
)
1910 char *printer_get_name(printer_enum
*pe
, int thing
)
1916 void printer_finish_enum(printer_enum
*pe
)
1923 * Colour selection stuff
1926 void dlg_coloursel_start(union control
*ctrl
, void *dlg
,
1927 int r
, int g
, int b
)
1929 struct macctrls
*mcs
= dlg
;
1930 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1931 Point where
= {-1, -1}; /* Screen with greatest colour depth */
1934 if (HAVE_COLOR_QD()) {
1935 incolour
.red
= r
* 0x0101;
1936 incolour
.green
= g
* 0x0101;
1937 incolour
.blue
= b
* 0x0101;
1938 mcs
->gotcolour
= GetColor(where
, "\pModify Colour:", &incolour
,
1940 ctrlevent(mcs
, mc
, EVENT_CALLBACK
);
1945 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1946 int *r
, int *g
, int *b
)
1948 struct macctrls
*mcs
= dlg
;
1950 if (mcs
->gotcolour
) {
1951 *r
= mcs
->thecolour
.red
>> 8;
1952 *g
= mcs
->thecolour
.green
>> 8;
1953 *b
= mcs
->thecolour
.blue
>> 8;
1961 * c-file-style: "simon"