1 /* $Id: macctrls.c,v 1.38 2003/04/14 23:28:58 ben Exp $ */
3 * Copyright (c) 2003 Ben Harris
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
23 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <Appearance.h>
30 #include <ColorPicker.h>
32 #include <ControlDefinitions.h>
36 #include <Resources.h>
40 #include <TextUtils.h>
41 #include <ToolUtils.h>
53 /* Range of menu IDs for popup menus */
59 struct macctrl_generic
{
69 /* Template from which this was generated */
71 /* Next control in this panel */
77 struct macctrl_generic generic
;
81 struct macctrl_generic generic
;
86 struct macctrl_generic generic
;
91 struct macctrl_generic generic
;
95 struct macctrl_generic generic
;
100 struct macctrl_generic generic
;
107 struct macctrl_generic generic
;
116 struct mac_layoutstate
{
119 unsigned int panelnum
;
122 #define ctrlevent(mcs, mc, event) do { \
123 if ((mc)->generic.ctrl->generic.handler != NULL) \
124 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
125 (mcs)->data, (event)); \
128 #define findbyctrl(mcs, ctrl) \
129 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
131 static void macctrl_layoutset(struct mac_layoutstate
*, struct controlset
*,
132 WindowPtr
, struct macctrls
*);
133 static void macctrl_switchtopanel(struct macctrls
*, unsigned int);
134 static void macctrl_setfocus(struct macctrls
*, union macctrl
*);
135 static void macctrl_text(struct macctrls
*, WindowPtr
,
136 struct mac_layoutstate
*, union control
*);
137 static void macctrl_editbox(struct macctrls
*, WindowPtr
,
138 struct mac_layoutstate
*, union control
*);
139 static void macctrl_radio(struct macctrls
*, WindowPtr
,
140 struct mac_layoutstate
*, union control
*);
141 static void macctrl_checkbox(struct macctrls
*, WindowPtr
,
142 struct mac_layoutstate
*, union control
*);
143 static void macctrl_button(struct macctrls
*, WindowPtr
,
144 struct mac_layoutstate
*, union control
*);
145 static void macctrl_listbox(struct macctrls
*, WindowPtr
,
146 struct mac_layoutstate
*, union control
*);
147 static void macctrl_popup(struct macctrls
*, WindowPtr
,
148 struct mac_layoutstate
*, union control
*);
149 #if !TARGET_API_MAC_CARBON
150 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16
, ControlRef
,
151 ControlDefProcMessage
, SInt32
);
152 static pascal SInt32
macctrl_sys7_default_cdef(SInt16
, ControlRef
,
153 ControlDefProcMessage
, SInt32
);
154 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16
, ControlRef
,
155 ControlDefProcMessage
, SInt32
);
158 #if !TARGET_API_MAC_CARBON
160 * This trick enables us to keep all the CDEF code in the main
161 * application, which makes life easier. For details, see
162 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
165 #pragma options align=mac68k
167 short jmpabs
; /* 4EF9 */
168 ControlDefUPP theUPP
;
170 #pragma options align=reset
173 static void macctrl_init()
175 #if !TARGET_API_MAC_CARBON
176 static int inited
= 0;
180 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_EditBox
);
181 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_editbox_cdef
);
182 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_Default
);
183 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_default_cdef
);
184 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_ListBox
);
185 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_listbox_cdef
);
191 static int macctrl_cmp_byctrl(void *av
, void *bv
)
193 union macctrl
*a
= (union macctrl
*)av
;
194 union macctrl
*b
= (union macctrl
*)bv
;
196 if (a
->generic
.ctrl
< b
->generic
.ctrl
)
198 else if (a
->generic
.ctrl
> b
->generic
.ctrl
)
204 static int macctrl_cmp_byctrl_find(void *av
, void *bv
)
206 union control
*a
= (union control
*)av
;
207 union macctrl
*b
= (union macctrl
*)bv
;
209 if (a
< b
->generic
.ctrl
)
211 else if (a
> b
->generic
.ctrl
)
217 static union control panellist
;
219 static void panellist_handler(union control
*ctrl
, void *dlg
, void *data
,
222 struct macctrls
*mcs
= dlg
;
224 if (event
== EVENT_SELCHANGE
)
225 macctrl_switchtopanel(mcs
, dlg_listbox_index(ctrl
, dlg
) + 1);
228 void macctrl_layoutbox(struct controlbox
*cb
, WindowPtr window
,
229 struct macctrls
*mcs
)
232 struct mac_layoutstate curstate
;
237 if (mac_gestalts
.apprvers
>= 0x100)
238 CreateRootControl(window
, &root
);
239 #if TARGET_API_MAC_CARBON
240 GetPortBounds(GetWindowPort(window
), &rect
);
242 rect
= window
->portRect
;
244 mcs
->window
= window
;
245 mcs
->byctrl
= newtree234(macctrl_cmp_byctrl
);
247 mcs
->defbutton
= NULL
;
248 mcs
->canbutton
= NULL
;
249 /* Count the number of panels */
251 for (i
= 1; i
< cb
->nctrlsets
; i
++)
252 if (strcmp(cb
->ctrlsets
[i
]->pathname
, cb
->ctrlsets
[i
-1]->pathname
))
254 mcs
->panels
= snewn(mcs
->npanels
, union macctrl
*);
255 memset(mcs
->panels
, 0, sizeof(*mcs
->panels
) * mcs
->npanels
);
256 curstate
.panelnum
= 0;
258 curstate
.pos
.h
= rect
.left
+ 13;
259 curstate
.pos
.v
= rect
.top
+ 13;
260 curstate
.width
= 160;
261 panellist
.listbox
.type
= CTRL_LISTBOX
;
262 panellist
.listbox
.handler
= &panellist_handler
;
263 panellist
.listbox
.height
= 20;
264 panellist
.listbox
.percentwidth
= 100;
265 macctrl_listbox(mcs
, window
, &curstate
, &panellist
);
267 curstate
.pos
.h
= rect
.left
+ 13 + 160 + 13;
268 curstate
.pos
.v
= rect
.bottom
- 33;
269 curstate
.width
= rect
.right
- (rect
.left
+ 13 + 160) - (13 * 2);
270 for (i
= 0; i
< cb
->nctrlsets
; i
++) {
271 if (i
> 0 && strcmp(cb
->ctrlsets
[i
]->pathname
,
272 cb
->ctrlsets
[i
-1]->pathname
)) {
273 curstate
.pos
.v
= rect
.top
+ 13;
275 assert(curstate
.panelnum
< mcs
->npanels
);
276 dlg_listbox_add(&panellist
, mcs
, cb
->ctrlsets
[i
]->pathname
);
278 macctrl_layoutset(&curstate
, cb
->ctrlsets
[i
], window
, mcs
);
280 macctrl_switchtopanel(mcs
, 1);
281 /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
286 static void macctrl_layoutset(struct mac_layoutstate
*curstate
,
287 struct controlset
*s
,
288 WindowPtr window
, struct macctrls
*mcs
)
290 unsigned int i
, j
, ncols
, colstart
;
291 struct mac_layoutstate cols
[MAXCOLS
];
293 fprintf(stderr
, "--- begin set ---\n");
294 fprintf(stderr
, "pathname = %s\n", s
->pathname
);
295 if (s
->boxname
&& *s
->boxname
)
296 fprintf(stderr
, "boxname = %s\n", s
->boxname
);
298 fprintf(stderr
, "boxtitle = %s\n", s
->boxtitle
);
303 for (i
= 0; i
< s
->ncontrols
; i
++) {
304 union control
*ctrl
= s
->ctrls
[i
];
307 colstart
= COLUMN_START(ctrl
->generic
.column
);
308 switch (ctrl
->generic
.type
) {
309 case CTRL_TEXT
: s
= "text"; break;
310 case CTRL_EDITBOX
: s
= "editbox"; break;
311 case CTRL_RADIO
: s
= "radio"; break;
312 case CTRL_CHECKBOX
: s
= "checkbox"; break;
313 case CTRL_BUTTON
: s
= "button"; break;
314 case CTRL_LISTBOX
: s
= "listbox"; break;
315 case CTRL_COLUMNS
: s
= "columns"; break;
316 case CTRL_FILESELECT
: s
= "fileselect"; break;
317 case CTRL_FONTSELECT
: s
= "fontselect"; break;
318 case CTRL_TABDELAY
: s
= "tabdelay"; break;
319 default: s
= "unknown"; break;
321 fprintf(stderr
, " control: %s\n", s
);
322 switch (ctrl
->generic
.type
) {
324 if (ctrl
->columns
.ncols
!= 1) {
325 ncols
= ctrl
->columns
.ncols
;
326 fprintf(stderr
, " split to %d\n", ncols
);
327 assert(ncols
<= MAXCOLS
);
328 for (j
= 0; j
< ncols
; j
++) {
331 cols
[j
].pos
.h
= cols
[j
-1].pos
.h
+ cols
[j
-1].width
+ 6;
333 cols
[j
].width
= curstate
->width
-
334 (cols
[j
].pos
.h
- curstate
->pos
.h
);
336 cols
[j
].width
= (curstate
->width
+ 6) *
337 ctrl
->columns
.percentages
[j
] / 100 - 6;
340 fprintf(stderr
, " join\n");
341 for (j
= 0; j
< ncols
; j
++)
342 if (cols
[j
].pos
.v
> cols
[0].pos
.v
)
343 cols
[0].pos
.v
= cols
[j
].pos
.v
;
344 cols
[0].width
= curstate
->width
;
349 macctrl_text(mcs
, window
, &cols
[colstart
], ctrl
);
352 macctrl_editbox(mcs
, window
, &cols
[colstart
], ctrl
);
355 macctrl_radio(mcs
, window
, &cols
[colstart
], ctrl
);
358 macctrl_checkbox(mcs
, window
, &cols
[colstart
], ctrl
);
361 macctrl_button(mcs
, window
, &cols
[colstart
], ctrl
);
364 if (ctrl
->listbox
.height
== 0)
365 macctrl_popup(mcs
, window
, &cols
[colstart
], ctrl
);
367 macctrl_listbox(mcs
, window
, &cols
[colstart
], ctrl
);
371 for (j
= 0; j
< ncols
; j
++)
372 if (cols
[j
].pos
.v
> curstate
->pos
.v
)
373 curstate
->pos
.v
= cols
[j
].pos
.v
;
376 static void macctrl_switchtopanel(struct macctrls
*mcs
, unsigned int which
)
381 #define hideshow(c) do { \
382 if (i == which) ShowControl(c); else HideControl(c); \
385 mcs
->curpanel
= which
;
386 /* Panel 0 is special and always visible. */
387 for (i
= 1; i
< mcs
->npanels
; i
++)
388 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
389 #if !TARGET_API_MAC_CARBON
390 if (mcs
->focus
== mc
)
391 macctrl_setfocus(mcs
, NULL
);
393 switch (mc
->generic
.type
) {
395 hideshow(mc
->text
.tbctrl
);
397 case MACCTRL_EDITBOX
:
398 hideshow(mc
->editbox
.tbctrl
);
399 if (mc
->editbox
.tblabel
!= NULL
)
400 hideshow(mc
->editbox
.tblabel
);
403 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
404 hideshow(mc
->radio
.tbctrls
[j
]);
405 if (mc
->radio
.tblabel
!= NULL
)
406 hideshow(mc
->radio
.tblabel
);
408 case MACCTRL_CHECKBOX
:
409 hideshow(mc
->checkbox
.tbctrl
);
412 hideshow(mc
->button
.tbctrl
);
414 case MACCTRL_LISTBOX
:
415 hideshow(mc
->listbox
.tbctrl
);
417 * At least under Mac OS 8.1, hiding a list box
418 * doesn't hide its scroll bars.
420 #if TARGET_API_MAC_CARBON
421 hideshow(GetListVerticalScrollBar(mc
->listbox
.list
));
423 hideshow((*mc
->listbox
.list
)->vScroll
);
427 hideshow(mc
->popup
.tbctrl
);
433 #if !TARGET_API_MAC_CARBON
435 * System 7 focus manipulation
437 static void macctrl_defocus(union macctrl
*mc
)
440 assert(mac_gestalts
.apprvers
< 0x100);
441 switch (mc
->generic
.type
) {
442 case MACCTRL_EDITBOX
:
443 TEDeactivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
448 static void macctrl_enfocus(union macctrl
*mc
)
451 assert(mac_gestalts
.apprvers
< 0x100);
452 switch (mc
->generic
.type
) {
453 case MACCTRL_EDITBOX
:
454 TEActivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
459 static void macctrl_setfocus(struct macctrls
*mcs
, union macctrl
*mc
)
462 if (mcs
->focus
== mc
)
464 if (mcs
->focus
!= NULL
)
465 macctrl_defocus(mcs
->focus
);
472 static void macctrl_text(struct macctrls
*mcs
, WindowPtr window
,
473 struct mac_layoutstate
*curstate
,
476 union macctrl
*mc
= snew(union macctrl
);
480 assert(ctrl
->text
.label
!= NULL
);
481 fprintf(stderr
, " label = %s\n", ctrl
->text
.label
);
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
, TRUE
, 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
, TRUE
, 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 fprintf(stderr
, " height = %d\n", height
);
513 SizeControl(mc
->text
.tbctrl
, curstate
->width
, height
);
514 curstate
->pos
.v
+= height
+ 6;
515 add234(mcs
->byctrl
, mc
);
516 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
517 mcs
->panels
[curstate
->panelnum
] = mc
;
520 static void macctrl_editbox(struct macctrls
*mcs
, WindowPtr window
,
521 struct mac_layoutstate
*curstate
,
524 union macctrl
*mc
= snew(union macctrl
);
525 Rect lbounds
, bounds
;
527 if (ctrl
->editbox
.label
!= NULL
)
528 fprintf(stderr
, " label = %s\n", ctrl
->editbox
.label
);
529 fprintf(stderr
, " percentwidth = %d\n", ctrl
->editbox
.percentwidth
);
530 if (ctrl
->editbox
.password
) fprintf(stderr
, " password\n");
531 if (ctrl
->editbox
.has_list
) fprintf(stderr
, " has list\n");
532 mc
->generic
.type
= MACCTRL_EDITBOX
;
533 mc
->generic
.ctrl
= ctrl
;
534 mc
->generic
.privdata
= NULL
;
535 lbounds
.left
= curstate
->pos
.h
;
536 lbounds
.top
= curstate
->pos
.v
;
537 if (ctrl
->editbox
.percentwidth
== 100) {
538 if (ctrl
->editbox
.label
!= NULL
) {
539 lbounds
.right
= lbounds
.left
+ curstate
->width
;
540 lbounds
.bottom
= lbounds
.top
+ 16;
541 curstate
->pos
.v
+= 18;
543 bounds
.left
= curstate
->pos
.h
;
544 bounds
.right
= bounds
.left
+ curstate
->width
;
546 lbounds
.right
= lbounds
.left
+
547 curstate
->width
* (100 - ctrl
->editbox
.percentwidth
) / 100;
548 lbounds
.bottom
= lbounds
.top
+ 22;
549 bounds
.left
= lbounds
.right
;
550 bounds
.right
= lbounds
.left
+ curstate
->width
;
552 bounds
.top
= curstate
->pos
.v
;
553 bounds
.bottom
= bounds
.top
+ 22;
554 if (mac_gestalts
.apprvers
>= 0x100) {
555 if (ctrl
->editbox
.label
== NULL
)
556 mc
->editbox
.tblabel
= NULL
;
558 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
559 0, 0, 0, kControlStaticTextProc
,
561 SetControlData(mc
->editbox
.tblabel
, kControlEntireControl
,
562 kControlStaticTextTextTag
,
563 strlen(ctrl
->editbox
.label
), ctrl
->editbox
.label
);
565 InsetRect(&bounds
, 3, 3);
566 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
567 ctrl
->editbox
.password ?
568 kControlEditTextPasswordProc
:
569 kControlEditTextProc
, (long)mc
);
571 #if !TARGET_API_MAC_CARBON
573 if (ctrl
->editbox
.label
== NULL
)
574 mc
->editbox
.tblabel
= NULL
;
576 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
577 0, 0, 0, SYS7_TEXT_PROC
,
579 TESetText(ctrl
->editbox
.label
, strlen(ctrl
->editbox
.label
),
580 (TEHandle
)(*mc
->editbox
.tblabel
)->contrlData
);
582 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
583 SYS7_EDITBOX_PROC
, (long)mc
);
586 curstate
->pos
.v
+= 28;
587 add234(mcs
->byctrl
, mc
);
588 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
589 mcs
->panels
[curstate
->panelnum
] = mc
;
590 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
593 #if !TARGET_API_MAC_CARBON
594 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16 variant
,
596 ControlDefProcMessage msg
,
607 rect
= (*control
)->contrlRect
;
608 if (variant
== SYS7_EDITBOX_VARIANT
)
609 InsetRect(&rect
, 3, 3); /* 2 if it's 20 pixels high */
610 te
= TENew(&rect
, &rect
);
611 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
612 (*te
)->txSize
= LoWord(ssfs
);
613 (*te
)->txFont
= HiWord(ssfs
);
614 (*control
)->contrlData
= (Handle
)te
;
617 TEDispose((TEHandle
)(*control
)->contrlData
);
620 if ((*control
)->contrlVis
) {
621 rect
= (*control
)->contrlRect
;
622 if (variant
== SYS7_EDITBOX_VARIANT
) {
625 InsetRect(&rect
, 3, 3);
628 (*(TEHandle
)(*control
)->contrlData
)->viewRect
= rect
;
629 TEUpdate(&rect
, (TEHandle
)(*control
)->contrlData
);
633 if (variant
== SYS7_TEXT_VARIANT
)
634 return kControlNoPart
;
635 mouse
.h
= LoWord(param
);
636 mouse
.v
= HiWord(param
);
637 rect
= (*control
)->contrlRect
;
638 InsetRect(&rect
, 3, 3);
639 return PtInRect(mouse
, &rect
) ? kControlEditTextPart
: kControlNoPart
;
641 if (param
& (1 << 31)) {
647 rgn
= (RgnHandle
)param
;
648 RectRgn(rgn
, &(*control
)->contrlRect
);
652 rgn
= (RgnHandle
)param
;
661 static void macctrl_radio(struct macctrls
*mcs
, WindowPtr window
,
662 struct mac_layoutstate
*curstate
,
665 union macctrl
*mc
= snew(union macctrl
);
668 unsigned int i
, colwidth
;
670 if (ctrl
->radio
.label
!= NULL
)
671 fprintf(stderr
, " label = %s\n", ctrl
->radio
.label
);
672 mc
->generic
.type
= MACCTRL_RADIO
;
673 mc
->generic
.ctrl
= ctrl
;
674 mc
->generic
.privdata
= NULL
;
675 mc
->radio
.tbctrls
= snewn(ctrl
->radio
.nbuttons
, ControlRef
);
676 colwidth
= (curstate
->width
+ 13) / ctrl
->radio
.ncolumns
;
677 bounds
.top
= curstate
->pos
.v
;
678 bounds
.bottom
= bounds
.top
+ 16;
679 bounds
.left
= curstate
->pos
.h
;
680 bounds
.right
= bounds
.left
+ curstate
->width
;
681 if (ctrl
->radio
.label
== NULL
)
682 mc
->radio
.tblabel
= NULL
;
684 if (mac_gestalts
.apprvers
>= 0x100) {
685 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
686 0, 0, 0, kControlStaticTextProc
,
688 SetControlData(mc
->radio
.tblabel
, kControlEntireControl
,
689 kControlStaticTextTextTag
,
690 strlen(ctrl
->radio
.label
), ctrl
->radio
.label
);
692 #if !TARGET_API_MAC_CARBON
694 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
695 0, 0, 0, SYS7_TEXT_PROC
, (long)mc
);
696 TESetText(ctrl
->radio
.label
, strlen(ctrl
->radio
.label
),
697 (TEHandle
)(*mc
->radio
.tblabel
)->contrlData
);
700 curstate
->pos
.v
+= 18;
702 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
703 fprintf(stderr
, " button = %s\n", ctrl
->radio
.buttons
[i
]);
704 bounds
.top
= curstate
->pos
.v
- 2;
705 bounds
.bottom
= bounds
.top
+ 18;
706 bounds
.left
= curstate
->pos
.h
+ colwidth
* (i
% ctrl
->radio
.ncolumns
);
707 if (i
== ctrl
->radio
.nbuttons
- 1 ||
708 i
% ctrl
->radio
.ncolumns
== ctrl
->radio
.ncolumns
- 1) {
709 bounds
.right
= curstate
->pos
.h
+ curstate
->width
;
710 curstate
->pos
.v
+= 18;
712 bounds
.right
= bounds
.left
+ colwidth
- 13;
713 c2pstrcpy(title
, ctrl
->radio
.buttons
[i
]);
714 mc
->radio
.tbctrls
[i
] = NewControl(window
, &bounds
, title
, TRUE
,
715 0, 0, 1, radioButProc
, (long)mc
);
717 curstate
->pos
.v
+= 4;
718 add234(mcs
->byctrl
, mc
);
719 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
720 mcs
->panels
[curstate
->panelnum
] = mc
;
721 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
724 static void macctrl_checkbox(struct macctrls
*mcs
, WindowPtr window
,
725 struct mac_layoutstate
*curstate
,
728 union macctrl
*mc
= snew(union macctrl
);
732 assert(ctrl
->checkbox
.label
!= NULL
);
733 fprintf(stderr
, " label = %s\n", ctrl
->checkbox
.label
);
734 mc
->generic
.type
= MACCTRL_CHECKBOX
;
735 mc
->generic
.ctrl
= ctrl
;
736 mc
->generic
.privdata
= NULL
;
737 bounds
.left
= curstate
->pos
.h
;
738 bounds
.right
= bounds
.left
+ curstate
->width
;
739 bounds
.top
= curstate
->pos
.v
;
740 bounds
.bottom
= bounds
.top
+ 16;
741 c2pstrcpy(title
, ctrl
->checkbox
.label
);
742 mc
->checkbox
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
743 checkBoxProc
, (long)mc
);
744 add234(mcs
->byctrl
, mc
);
745 curstate
->pos
.v
+= 22;
746 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
747 mcs
->panels
[curstate
->panelnum
] = mc
;
748 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
751 static void macctrl_button(struct macctrls
*mcs
, WindowPtr window
,
752 struct mac_layoutstate
*curstate
,
755 union macctrl
*mc
= snew(union macctrl
);
759 assert(ctrl
->button
.label
!= NULL
);
760 fprintf(stderr
, " label = %s\n", ctrl
->button
.label
);
761 if (ctrl
->button
.isdefault
)
762 fprintf(stderr
, " is default\n");
763 mc
->generic
.type
= MACCTRL_BUTTON
;
764 mc
->generic
.ctrl
= ctrl
;
765 mc
->generic
.privdata
= NULL
;
766 bounds
.left
= curstate
->pos
.h
;
767 bounds
.right
= bounds
.left
+ curstate
->width
;
768 bounds
.top
= curstate
->pos
.v
;
769 bounds
.bottom
= bounds
.top
+ 20;
770 c2pstrcpy(title
, ctrl
->button
.label
);
771 mc
->button
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
772 pushButProc
, (long)mc
);
773 mc
->button
.tbring
= NULL
;
774 if (mac_gestalts
.apprvers
>= 0x100) {
775 Boolean isdefault
= ctrl
->button
.isdefault
;
777 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
778 kControlPushButtonDefaultTag
,
779 sizeof(isdefault
), &isdefault
);
780 } else if (ctrl
->button
.isdefault
) {
781 InsetRect(&bounds
, -4, -4);
782 mc
->button
.tbring
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
783 SYS7_DEFAULT_PROC
, (long)mc
);
785 if (mac_gestalts
.apprvers
>= 0x110) {
786 Boolean iscancel
= ctrl
->button
.iscancel
;
788 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
789 kControlPushButtonCancelTag
,
790 sizeof(iscancel
), &iscancel
);
792 if (ctrl
->button
.isdefault
)
794 if (ctrl
->button
.iscancel
)
796 add234(mcs
->byctrl
, mc
);
797 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
798 mcs
->panels
[curstate
->panelnum
] = mc
;
799 curstate
->pos
.v
+= 26;
802 #if !TARGET_API_MAC_CARBON
803 static pascal SInt32
macctrl_sys7_default_cdef(SInt16 variant
,
805 ControlDefProcMessage msg
,
815 if ((*control
)->contrlVis
) {
816 rect
= (*control
)->contrlRect
;
817 GetPenState(&savestate
);
820 if ((*control
)->contrlHilite
== kControlInactivePart
)
822 oval
= (rect
.bottom
- rect
.top
) / 2 + 2;
823 FrameRoundRect(&rect
, oval
, oval
);
824 SetPenState(&savestate
);
828 if (param
& (1 << 31)) {
834 rgn
= (RgnHandle
)param
;
835 RectRgn(rgn
, &(*control
)->contrlRect
);
839 rgn
= (RgnHandle
)param
;
848 static void macctrl_listbox(struct macctrls
*mcs
, WindowPtr window
,
849 struct mac_layoutstate
*curstate
,
852 union macctrl
*mc
= snew(union macctrl
);
856 if (ctrl
->listbox
.label
!= NULL
)
857 fprintf(stderr
, " label = %s\n", ctrl
->listbox
.label
);
858 fprintf(stderr
, " height = %d\n", ctrl
->listbox
.height
);
859 if (ctrl
->listbox
.draglist
)
860 fprintf(stderr
, " draglist\n");
861 if (ctrl
->listbox
.multisel
)
862 fprintf(stderr
, " multisel\n");
863 fprintf(stderr
, " ncols = %d\n", ctrl
->listbox
.ncols
);
864 assert(ctrl
->listbox
.percentwidth
== 100);
865 mc
->generic
.type
= MACCTRL_LISTBOX
;
866 mc
->generic
.ctrl
= ctrl
;
867 mc
->generic
.privdata
= NULL
;
868 /* The list starts off empty */
869 mc
->listbox
.nids
= 0;
870 mc
->listbox
.ids
= NULL
;
871 bounds
.left
= curstate
->pos
.h
;
872 bounds
.right
= bounds
.left
+ curstate
->width
;
873 bounds
.top
= curstate
->pos
.v
;
874 bounds
.bottom
= bounds
.top
+ 16 * ctrl
->listbox
.height
+ 2;
876 if (mac_gestalts
.apprvers
>= 0x100) {
877 InsetRect(&bounds
, 3, 3);
878 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
880 kControlListBoxProc
, (long)mc
);
881 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
882 kControlListBoxListHandleTag
,
883 sizeof(mc
->listbox
.list
), &mc
->listbox
.list
,
885 DisposeControl(mc
->listbox
.tbctrl
);
890 #if !TARGET_API_MAC_CARBON
892 InsetRect(&bounds
, -3, -3);
893 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
895 SYS7_LISTBOX_PROC
, (long)mc
);
896 mc
->listbox
.list
= (ListHandle
)(*mc
->listbox
.tbctrl
)->contrlData
;
897 (*mc
->listbox
.list
)->refCon
= (long)mc
;
900 if (!ctrl
->listbox
.multisel
) {
901 #if TARGET_API_MAC_CARBON
902 SetListSelectionFlags(mc
->listbox
.list
, lOnlyOne
);
904 (*mc
->listbox
.list
)->selFlags
= lOnlyOne
;
907 add234(mcs
->byctrl
, mc
);
908 curstate
->pos
.v
+= 6 + 16 * ctrl
->listbox
.height
+ 2;
909 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
910 mcs
->panels
[curstate
->panelnum
] = mc
;
911 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
914 #if !TARGET_API_MAC_CARBON
915 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16 variant
,
917 ControlDefProcMessage msg
,
933 rect
= (*control
)->contrlRect
;
934 InsetRect(&rect
, 4, 4);
935 rect
.right
-= 15; /* scroll bar */
936 bounds
.top
= bounds
.bottom
= bounds
.left
= 0;
938 csize
.h
= csize
.v
= 0;
940 savefont
= curport
->txFont
;
941 savesize
= curport
->txSize
;
942 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
943 TextFont(HiWord(ssfs
));
944 TextSize(LoWord(ssfs
));
945 list
= LNew(&rect
, &bounds
, csize
, 0, (*control
)->contrlOwner
,
946 TRUE
, FALSE
, FALSE
, TRUE
);
947 SetControlReference((*list
)->vScroll
, (long)list
);
948 (*control
)->contrlData
= (Handle
)list
;
954 * If the dialogue box is being destroyed, the scroll bar
955 * might have gone already. In our situation, this is the
956 * only time we destroy a control, so NULL out the scroll bar
957 * handle to prevent LDispose trying to free it.
959 list
= (ListHandle
)(*control
)->contrlData
;
960 (*list
)->vScroll
= NULL
;
964 if ((*control
)->contrlVis
) {
965 rect
= (*control
)->contrlRect
;
966 /* XXX input focus highlighting? */
967 InsetRect(&rect
, 3, 3);
970 list
= (ListHandle
)(*control
)->contrlData
;
971 LActivate((*control
)->contrlHilite
!= kControlInactivePart
, list
);
973 LUpdate(curport
->visRgn
, list
);
977 mouse
.h
= LoWord(param
);
978 mouse
.v
= HiWord(param
);
979 rect
= (*control
)->contrlRect
;
980 InsetRect(&rect
, 4, 4);
982 * We deliberately exclude the scrollbar so that LClick() can see it.
985 return PtInRect(mouse
, &rect
) ? kControlListBoxPart
: kControlNoPart
;
987 if (param
& (1 << 31)) {
993 rgn
= (RgnHandle
)param
;
994 RectRgn(rgn
, &(*control
)->contrlRect
);
998 rgn
= (RgnHandle
)param
;
1007 static void macctrl_popup(struct macctrls
*mcs
, WindowPtr window
,
1008 struct mac_layoutstate
*curstate
,
1009 union control
*ctrl
)
1011 union macctrl
*mc
= snew(union macctrl
);
1014 unsigned int labelwidth
;
1015 static int nextmenuid
= MENU_MIN
;
1020 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
1021 * create a popup menu with dynamic content.
1023 assert(ctrl
->listbox
.height
== 0);
1024 assert(!ctrl
->listbox
.draglist
);
1025 assert(!ctrl
->listbox
.multisel
);
1027 if (ctrl
->listbox
.label
!= NULL
)
1028 fprintf(stderr
, " label = %s\n", ctrl
->listbox
.label
);
1029 fprintf(stderr
, " percentwidth = %d\n", ctrl
->listbox
.percentwidth
);
1031 mc
->generic
.type
= MACCTRL_POPUP
;
1032 mc
->generic
.ctrl
= ctrl
;
1033 mc
->generic
.privdata
= NULL
;
1034 c2pstrcpy(title
, ctrl
->button
.label
== NULL ?
"" : ctrl
->button
.label
);
1036 /* Find a spare menu ID and create the menu */
1037 while (GetMenuHandle(nextmenuid
) != NULL
)
1038 if (++nextmenuid
>= MENU_MAX
) nextmenuid
= MENU_MIN
;
1039 menuid
= nextmenuid
++;
1040 menu
= NewMenu(menuid
, "\pdummy");
1041 if (menu
== NULL
) return;
1042 mc
->popup
.menu
= menu
;
1043 mc
->popup
.menuid
= menuid
;
1044 InsertMenu(menu
, kInsertHierarchicalMenu
);
1046 /* The menu starts off empty */
1048 mc
->popup
.ids
= NULL
;
1050 bounds
.left
= curstate
->pos
.h
;
1051 bounds
.right
= bounds
.left
+ curstate
->width
;
1052 bounds
.top
= curstate
->pos
.v
;
1053 bounds
.bottom
= bounds
.top
+ 20;
1054 /* XXX handle percentwidth == 100 */
1055 labelwidth
= curstate
->width
* (100 - ctrl
->listbox
.percentwidth
) / 100;
1056 mc
->popup
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
,
1057 popupTitleLeftJust
, menuid
, labelwidth
,
1058 popupMenuProc
+ popupFixedWidth
, (long)mc
);
1059 add234(mcs
->byctrl
, mc
);
1060 curstate
->pos
.v
+= 26;
1061 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
1062 mcs
->panels
[curstate
->panelnum
] = mc
;
1063 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1067 void macctrl_activate(WindowPtr window
, EventRecord
*event
)
1069 struct macctrls
*mcs
= mac_winctrls(window
);
1070 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1073 ControlPartCode state
;
1077 SetPort((GrafPtr
)GetWindowPort(window
));
1078 if (mac_gestalts
.apprvers
>= 0x100)
1079 SetThemeWindowBackground(window
, active ?
1080 kThemeBrushModelessDialogBackgroundActive
:
1081 kThemeBrushModelessDialogBackgroundInactive
,
1083 state
= active ? kControlNoPart
: kControlInactivePart
;
1084 for (i
= 0; i
<= mcs
->curpanel
; i
+= mcs
->curpanel
)
1085 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
1086 switch (mc
->generic
.type
) {
1088 HiliteControl(mc
->text
.tbctrl
, state
);
1090 case MACCTRL_EDITBOX
:
1091 HiliteControl(mc
->editbox
.tbctrl
, state
);
1092 if (mc
->editbox
.tblabel
!= NULL
)
1093 HiliteControl(mc
->editbox
.tblabel
, state
);
1096 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
1097 HiliteControl(mc
->radio
.tbctrls
[j
], state
);
1098 if (mc
->radio
.tblabel
!= NULL
)
1099 HiliteControl(mc
->radio
.tblabel
, state
);
1101 case MACCTRL_CHECKBOX
:
1102 HiliteControl(mc
->checkbox
.tbctrl
, state
);
1104 case MACCTRL_BUTTON
:
1105 HiliteControl(mc
->button
.tbctrl
, state
);
1106 if (mc
->button
.tbring
!= NULL
)
1107 HiliteControl(mc
->button
.tbring
, state
);
1109 case MACCTRL_LISTBOX
:
1110 HiliteControl(mc
->listbox
.tbctrl
, state
);
1113 HiliteControl(mc
->popup
.tbctrl
, state
);
1116 #if !TARGET_API_MAC_CARBON
1117 if (mcs
->focus
== mc
) {
1119 macctrl_enfocus(mc
);
1121 macctrl_defocus(mc
);
1128 void macctrl_click(WindowPtr window
, EventRecord
*event
)
1131 ControlHandle control
, oldfocus
;
1132 int part
, trackresult
;
1135 struct macctrls
*mcs
= mac_winctrls(window
);
1140 SetPort((GrafPtr
)GetWindowPort(window
));
1141 mouse
= event
->where
;
1142 GlobalToLocal(&mouse
);
1143 part
= FindControl(mouse
, window
, &control
);
1144 if (control
!= NULL
) {
1145 #if !TARGET_API_MAC_CARBON
1147 * Special magic for scroll bars in list boxes, whose refcon
1150 if (part
== kControlUpButtonPart
|| part
== kControlDownButtonPart
||
1151 part
== kControlPageUpPart
|| part
== kControlPageDownPart
||
1152 part
== kControlIndicatorPart
)
1153 mc
= (union macctrl
*)
1154 (*(ListHandle
)GetControlReference(control
))->refCon
;
1157 mc
= (union macctrl
*)GetControlReference(control
);
1158 fprintf(stderr
, "control = %p, part = %d, mc = %p\n",
1160 if (mac_gestalts
.apprvers
>= 0x100) {
1161 if (GetControlFeatures(control
, &features
) == noErr
&&
1162 (features
& kControlSupportsFocus
) &&
1163 (features
& kControlGetsFocusOnClick
) &&
1164 GetKeyboardFocus(window
, &oldfocus
) == noErr
&&
1165 control
!= oldfocus
)
1166 SetKeyboardFocus(window
, control
, part
);
1167 trackresult
= HandleControlClick(control
, mouse
, event
->modifiers
,
1168 (ControlActionUPP
)-1);
1170 #if !TARGET_API_MAC_CARBON
1171 if (mc
->generic
.type
== MACCTRL_EDITBOX
&&
1172 control
== mc
->editbox
.tbctrl
) {
1173 TEHandle te
= (TEHandle
)(*control
)->contrlData
;
1175 macctrl_setfocus(mcs
, mc
);
1176 TEClick(mouse
, !!(event
->modifiers
& shiftKey
), te
);
1179 if (mc
->generic
.type
== MACCTRL_LISTBOX
&&
1180 (control
== mc
->listbox
.tbctrl
||
1181 control
== (*mc
->listbox
.list
)->vScroll
)) {
1183 fprintf(stderr
, "list = %p\n", mc
->listbox
.list
);
1184 macctrl_setfocus(mcs
, mc
);
1185 if (LClick(mouse
, event
->modifiers
, mc
->listbox
.list
))
1187 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1189 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1193 trackresult
= TrackControl(control
, mouse
, (ControlActionUPP
)-1);
1195 switch (mc
->generic
.type
) {
1197 if (trackresult
!= 0) {
1198 for (i
= 0; i
< mc
->generic
.ctrl
->radio
.nbuttons
; i
++)
1199 if (mc
->radio
.tbctrls
[i
] == control
)
1200 SetControlValue(mc
->radio
.tbctrls
[i
],
1201 kControlRadioButtonCheckedValue
);
1203 SetControlValue(mc
->radio
.tbctrls
[i
],
1204 kControlRadioButtonUncheckedValue
);
1205 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1208 case MACCTRL_CHECKBOX
:
1209 if (trackresult
!= 0) {
1210 SetControlValue(control
, !GetControlValue(control
));
1211 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1214 case MACCTRL_BUTTON
:
1215 if (trackresult
!= 0)
1216 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1218 case MACCTRL_LISTBOX
:
1219 /* FIXME spot double-click */
1220 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1223 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1231 void macctrl_key(WindowPtr window
, EventRecord
*event
)
1234 struct macctrls
*mcs
= mac_winctrls(window
);
1236 unsigned long dummy
;
1238 switch (event
->message
& charCodeMask
) {
1239 case kEnterCharCode
:
1240 case kReturnCharCode
:
1241 if (mcs
->defbutton
!= NULL
) {
1242 assert(mcs
->defbutton
->generic
.type
== MACCTRL_BUTTON
);
1243 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlButtonPart
);
1245 * I'd like to delay unhilighting the button until after
1246 * the event has been processed, but by them the entire
1247 * dialgue box might have been destroyed.
1250 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1251 ctrlevent(mcs
, mcs
->defbutton
, EVENT_ACTION
);
1254 case kEscapeCharCode
:
1255 if (mcs
->canbutton
!= NULL
) {
1256 assert(mcs
->canbutton
->generic
.type
== MACCTRL_BUTTON
);
1257 HiliteControl(mcs
->canbutton
->button
.tbctrl
, kControlButtonPart
);
1259 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1260 ctrlevent(mcs
, mcs
->canbutton
, EVENT_ACTION
);
1264 if (mac_gestalts
.apprvers
>= 0x100) {
1265 if (GetKeyboardFocus(window
, &control
) == noErr
&& control
!= NULL
) {
1266 HandleControlKey(control
, (event
->message
& keyCodeMask
) >> 8,
1267 event
->message
& charCodeMask
, event
->modifiers
);
1268 mc
= (union macctrl
*)GetControlReference(control
);
1269 switch (mc
->generic
.type
) {
1270 case MACCTRL_LISTBOX
:
1271 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1274 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1279 #if !TARGET_API_MAC_CARBON
1283 if (mcs
->focus
!= NULL
) {
1285 switch (mc
->generic
.type
) {
1286 case MACCTRL_EDITBOX
:
1287 te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1288 TEKey(event
->message
& charCodeMask
, te
);
1289 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1297 void macctrl_update(WindowPtr window
)
1299 #if TARGET_API_MAC_CARBON
1305 BeginUpdate(window
);
1307 SetPort((GrafPtr
)GetWindowPort(window
));
1308 if (mac_gestalts
.apprvers
>= 0x101) {
1309 #if TARGET_API_MAC_CARBON
1310 GetPortBounds(GetWindowPort(window
), &rect
);
1312 rect
= window
->portRect
;
1314 InsetRect(&rect
, -1, -1);
1315 DrawThemeModelessDialogFrame(&rect
, mac_frontwindow() == window ?
1316 kThemeStateActive
: kThemeStateInactive
);
1318 #if TARGET_API_MAC_CARBON
1320 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1321 UpdateControls(window
, visrgn
);
1324 UpdateControls(window
, window
->visRgn
);
1330 #if TARGET_API_MAC_CARBON
1331 #define EnableItem EnableMenuItem
1332 #define DisableItem DisableMenuItem
1334 void macctrl_adjustmenus(WindowPtr window
)
1338 menu
= GetMenuHandle(mFile
);
1339 DisableItem(menu
, iSave
); /* XXX enable if modified */
1340 EnableItem(menu
, iSaveAs
);
1341 EnableItem(menu
, iDuplicate
);
1343 menu
= GetMenuHandle(mEdit
);
1344 DisableItem(menu
, 0);
1347 void macctrl_close(WindowPtr window
)
1349 struct macctrls
*mcs
= mac_winctrls(window
);
1353 * Mostly, we don't bother disposing of the Toolbox controls,
1354 * since that will happen automatically when the window is
1355 * disposed of. Popup menus are an exception, because we have to
1356 * dispose of the menu ourselves, and doing that while the control
1357 * still holds a reference to it seems rude.
1359 while ((mc
= index234(mcs
->byctrl
, 0)) != NULL
) {
1360 if (mc
->generic
.privdata
!= NULL
&& mc
->generic
.freeprivdata
)
1361 sfree(mc
->generic
.privdata
);
1362 switch (mc
->generic
.type
) {
1364 DisposeControl(mc
->popup
.tbctrl
);
1365 DeleteMenu(mc
->popup
.menuid
);
1366 DisposeMenu(mc
->popup
.menu
);
1369 del234(mcs
->byctrl
, mc
);
1373 freetree234(mcs
->byctrl
);
1379 void dlg_update_start(union control
*ctrl
, void *dlg
)
1385 void dlg_update_done(union control
*ctrl
, void *dlg
)
1391 void dlg_set_focus(union control
*ctrl
, void *dlg
)
1394 if (mac_gestalts
.apprvers
>= 0x100) {
1395 /* Use SetKeyboardFocus() */
1397 /* Do our own mucking around */
1401 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1407 void dlg_beep(void *dlg
)
1413 void dlg_error_msg(void *dlg
, char *msg
)
1417 c2pstrcpy(pmsg
, msg
);
1418 ParamText(pmsg
, NULL
, NULL
, NULL
);
1419 StopAlert(128, NULL
);
1422 void dlg_end(void *dlg
, int value
)
1424 struct macctrls
*mcs
= dlg
;
1426 if (mcs
->end
!= NULL
)
1427 (*mcs
->end
)(mcs
->window
, value
);
1430 void dlg_refresh(union control
*ctrl
, void *dlg
)
1432 struct macctrls
*mcs
= dlg
;
1437 mc
= findbyctrl(mcs
, ctrl
);
1439 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1442 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
1444 struct macctrls
*mcs
= dlg
;
1445 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1448 return mc
->generic
.privdata
;
1451 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
1453 struct macctrls
*mcs
= dlg
;
1454 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1457 mc
->generic
.privdata
= ptr
;
1458 mc
->generic
.freeprivdata
= FALSE
;
1461 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
1463 struct macctrls
*mcs
= dlg
;
1464 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1467 mc
->generic
.privdata
= smalloc(size
);
1468 mc
->generic
.freeprivdata
= TRUE
;
1469 return mc
->generic
.privdata
;
1474 * Radio Button control
1477 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
1479 struct macctrls
*mcs
= dlg
;
1480 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1483 if (mc
== NULL
) return;
1484 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1485 if (i
== whichbutton
)
1486 SetControlValue(mc
->radio
.tbctrls
[i
],
1487 kControlRadioButtonCheckedValue
);
1489 SetControlValue(mc
->radio
.tbctrls
[i
],
1490 kControlRadioButtonUncheckedValue
);
1495 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
1497 struct macctrls
*mcs
= dlg
;
1498 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1502 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1503 if (GetControlValue(mc
->radio
.tbctrls
[i
]) ==
1504 kControlRadioButtonCheckedValue
)
1515 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
1517 struct macctrls
*mcs
= dlg
;
1518 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1520 if (mc
== NULL
) return;
1521 SetControlValue(mc
->checkbox
.tbctrl
,
1522 checked ? kControlCheckBoxCheckedValue
:
1523 kControlCheckBoxUncheckedValue
);
1526 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
1528 struct macctrls
*mcs
= dlg
;
1529 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1532 return GetControlValue(mc
->checkbox
.tbctrl
);
1540 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
1542 struct macctrls
*mcs
= dlg
;
1543 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1546 if (mc
== NULL
) return;
1547 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1549 SetPort((GrafPtr
)(GetWindowPort(mcs
->window
)));
1550 if (mac_gestalts
.apprvers
>= 0x100)
1551 SetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1552 ctrl
->editbox
.password ?
1553 kControlEditTextPasswordTag
:
1554 kControlEditTextTextTag
,
1555 strlen(text
), text
);
1556 #if !TARGET_API_MAC_CARBON
1558 TESetText(text
, strlen(text
),
1559 (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
1561 DrawOneControl(mc
->editbox
.tbctrl
);
1565 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
1567 struct macctrls
*mcs
= dlg
;
1568 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1572 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1573 if (mac_gestalts
.apprvers
>= 0x100) {
1574 if (GetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1575 ctrl
->editbox
.password ?
1576 kControlEditTextPasswordTag
:
1577 kControlEditTextTextTag
,
1578 length
- 1, buffer
, &olen
) != noErr
)
1580 if (olen
> length
- 1)
1583 #if !TARGET_API_MAC_CARBON
1585 TEHandle te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1587 olen
= (*te
)->teLength
;
1588 if (olen
> length
- 1)
1590 memcpy(buffer
, *(*te
)->hText
, olen
);
1593 buffer
[olen
] = '\0';
1594 fprintf(stderr
, "dlg_editbox_get: %s\n", buffer
);
1602 static void dlg_macpopup_clear(union control
*ctrl
, void *dlg
)
1604 struct macctrls
*mcs
= dlg
;
1605 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1606 MenuRef menu
= mc
->popup
.menu
;
1609 if (mc
== NULL
) return;
1610 fprintf(stderr
, " popup_clear\n");
1611 n
= CountMenuItems(menu
);
1612 for (i
= 0; i
< n
; i
++)
1613 DeleteMenuItem(menu
, n
- i
);
1615 sfree(mc
->popup
.ids
);
1616 mc
->popup
.ids
= NULL
;
1617 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1620 static void dlg_maclist_clear(union control
*ctrl
, void *dlg
)
1622 struct macctrls
*mcs
= dlg
;
1623 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1625 if (mc
== NULL
) return;
1626 fprintf(stderr
, " maclist_clear\n");
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 fprintf(stderr
, " popup_del %d\n", index
);
1655 DeleteMenuItem(menu
, index
+ 1);
1656 if (mc
->popup
.ids
!= NULL
)
1657 memcpy(mc
->popup
.ids
+ index
, mc
->popup
.ids
+ index
+ 1,
1658 (mc
->popup
.nids
- index
- 1) * sizeof(*mc
->popup
.ids
));
1659 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1662 static void dlg_maclist_del(union control
*ctrl
, void *dlg
, int index
)
1664 struct macctrls
*mcs
= dlg
;
1665 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1667 if (mc
== NULL
) return;
1668 fprintf(stderr
, " maclist_del %d\n", index
);
1669 LDelRow(1, index
, mc
->listbox
.list
);
1670 if (mc
->listbox
.ids
!= NULL
)
1671 memcpy(mc
->listbox
.ids
+ index
, mc
->listbox
.ids
+ index
+ 1,
1672 (mc
->listbox
.nids
- index
- 1) * sizeof(*mc
->listbox
.ids
));
1673 DrawOneControl(mc
->listbox
.tbctrl
);
1676 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
1679 switch (ctrl
->generic
.type
) {
1681 if (ctrl
->listbox
.height
== 0)
1682 dlg_macpopup_del(ctrl
, dlg
, index
);
1684 dlg_maclist_del(ctrl
, dlg
, index
);
1689 static void dlg_macpopup_add(union control
*ctrl
, void *dlg
, char const *text
)
1691 struct macctrls
*mcs
= dlg
;
1692 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1693 MenuRef menu
= mc
->popup
.menu
;
1696 if (mc
== NULL
) return;
1697 fprintf(stderr
, " popup_add %s\n", text
);
1698 assert(text
[0] != '\0');
1699 c2pstrcpy(itemstring
, text
);
1700 AppendMenu(menu
, "\pdummy");
1701 SetMenuItemText(menu
, CountMenuItems(menu
), itemstring
);
1702 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1706 static void dlg_maclist_add(union control
*ctrl
, void *dlg
, char const *text
)
1708 struct macctrls
*mcs
= dlg
;
1709 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1711 Cell cell
= { 0, 0 };
1713 if (mc
== NULL
) return;
1714 fprintf(stderr
, " maclist_add %s\n", text
);
1715 #if TARGET_API_MAC_CARBON
1716 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1718 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1720 cell
.v
= bounds
.bottom
;
1721 LAddRow(1, cell
.v
, mc
->listbox
.list
);
1722 LSetCell(text
, strlen(text
), cell
, mc
->listbox
.list
);
1723 DrawOneControl(mc
->listbox
.tbctrl
);
1726 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
1729 switch (ctrl
->generic
.type
) {
1731 if (ctrl
->listbox
.height
== 0)
1732 dlg_macpopup_add(ctrl
, dlg
, text
);
1734 dlg_maclist_add(ctrl
, dlg
, text
);
1739 static void dlg_macpopup_addwithid(union control
*ctrl
, void *dlg
,
1740 char const *text
, int id
)
1742 struct macctrls
*mcs
= dlg
;
1743 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1744 MenuRef menu
= mc
->popup
.menu
;
1747 if (mc
== NULL
) return;
1748 fprintf(stderr
, " popup_addwthid %s, %d\n", text
, id
);
1749 dlg_macpopup_add(ctrl
, dlg
, text
);
1750 index
= CountMenuItems(menu
) - 1;
1751 if (mc
->popup
.nids
<= index
) {
1752 mc
->popup
.nids
= index
+ 1;
1753 mc
->popup
.ids
= sresize(mc
->popup
.ids
, mc
->popup
.nids
, int);
1755 mc
->popup
.ids
[index
] = id
;
1758 static void dlg_maclist_addwithid(union control
*ctrl
, void *dlg
,
1759 char const *text
, int id
)
1761 struct macctrls
*mcs
= dlg
;
1762 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1766 if (mc
== NULL
) return;
1767 fprintf(stderr
, " maclist_addwithid %s %d\n", text
, id
);
1768 dlg_maclist_add(ctrl
, dlg
, text
);
1769 #if TARGET_API_MAC_CARBON
1770 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1772 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1774 index
= bounds
.bottom
;
1775 if (mc
->listbox
.nids
<= index
) {
1776 mc
->listbox
.nids
= index
+ 1;
1777 mc
->listbox
.ids
= sresize(mc
->listbox
.ids
, mc
->listbox
.nids
, int);
1779 mc
->listbox
.ids
[index
] = id
;
1782 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
1783 char const *text
, int id
)
1786 switch (ctrl
->generic
.type
) {
1788 if (ctrl
->listbox
.height
== 0)
1789 dlg_macpopup_addwithid(ctrl
, dlg
, text
, id
);
1791 dlg_maclist_addwithid(ctrl
, dlg
, text
, id
);
1796 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
1798 struct macctrls
*mcs
= dlg
;
1799 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1802 switch (ctrl
->generic
.type
) {
1804 if (ctrl
->listbox
.height
== 0) {
1805 assert(mc
->popup
.ids
!= NULL
&& mc
->popup
.nids
> index
);
1806 return mc
->popup
.ids
[index
];
1808 assert(mc
->listbox
.ids
!= NULL
&& mc
->listbox
.nids
> index
);
1809 return mc
->listbox
.ids
[index
];
1815 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
1817 struct macctrls
*mcs
= dlg
;
1818 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1819 Cell cell
= { 0, 0 };
1822 switch (ctrl
->generic
.type
) {
1824 if (ctrl
->listbox
.height
== 0)
1825 return GetControlValue(mc
->popup
.tbctrl
) - 1;
1827 if (LGetSelect(TRUE
, &cell
, mc
->listbox
.list
))
1836 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
1838 struct macctrls
*mcs
= dlg
;
1839 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1840 Cell cell
= { 0, 0 };
1843 switch (ctrl
->generic
.type
) {
1845 if (ctrl
->listbox
.height
== 0)
1846 return GetControlValue(mc
->popup
.tbctrl
) - 1 == index
;
1849 return LGetSelect(FALSE
, &cell
, mc
->listbox
.list
);
1855 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
1857 struct macctrls
*mcs
= dlg
;
1858 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1860 if (mc
== NULL
) return;
1861 switch (ctrl
->generic
.type
) {
1863 if (ctrl
->listbox
.height
== 0)
1864 SetControlValue(mc
->popup
.tbctrl
, index
+ 1);
1874 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
1876 struct macctrls
*mcs
= dlg
;
1877 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1879 if (mc
== NULL
) return;
1880 if (mac_gestalts
.apprvers
>= 0x100)
1881 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
1882 kControlStaticTextTextTag
, strlen(text
), text
);
1883 #if !TARGET_API_MAC_CARBON
1885 TESetText(text
, strlen(text
),
1886 (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
);
1892 * File Selector control
1895 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
1900 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
1907 * Font Selector control
1910 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fn
)
1915 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fn
)
1922 * Printer enumeration
1925 printer_enum
*printer_start_enum(int *nprinters
)
1932 char *printer_get_name(printer_enum
*pe
, int thing
)
1938 void printer_finish_enum(printer_enum
*pe
)
1945 * Colour selection stuff
1948 void dlg_coloursel_start(union control
*ctrl
, void *dlg
,
1949 int r
, int g
, int b
)
1951 struct macctrls
*mcs
= dlg
;
1952 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1953 Point where
= {-1, -1}; /* Screen with greatest colour depth */
1956 if (HAVE_COLOR_QD()) {
1957 incolour
.red
= r
* 0x0101;
1958 incolour
.green
= g
* 0x0101;
1959 incolour
.blue
= b
* 0x0101;
1960 mcs
->gotcolour
= GetColor(where
, "\pModify Colour:", &incolour
,
1962 ctrlevent(mcs
, mc
, EVENT_CALLBACK
);
1967 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1968 int *r
, int *g
, int *b
)
1970 struct macctrls
*mcs
= dlg
;
1972 if (mcs
->gotcolour
) {
1973 *r
= mcs
->thecolour
.red
>> 8;
1974 *g
= mcs
->thecolour
.green
>> 8;
1975 *b
= mcs
->thecolour
.blue
>> 8;
1983 * c-file-style: "simon"