1 /* $Id: macctrls.c,v 1.18 2003/03/29 18:32:36 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>
31 #include <ControlDefinitions.h>
33 #include <Resources.h>
37 #include <TextUtils.h>
38 #include <ToolUtils.h>
50 /* Range of menu IDs for popup menus */
56 struct macctrl_generic
{
65 /* Template from which this was generated */
67 /* Next control in this panel */
73 struct macctrl_generic generic
;
77 struct macctrl_generic generic
;
81 struct macctrl_generic generic
;
85 struct macctrl_generic generic
;
89 struct macctrl_generic generic
;
93 struct macctrl_generic generic
;
102 struct mac_layoutstate
{
105 unsigned int panelnum
;
108 #define ctrlevent(mcs, mc, event) do { \
109 if ((mc)->generic.ctrl->generic.handler != NULL) \
110 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
111 (mcs)->data, (event)); \
114 #define findbyctrl(mcs, ctrl) \
115 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
117 static void macctrl_layoutset(struct mac_layoutstate
*, struct controlset
*,
118 WindowPtr
, struct macctrls
*);
119 static void macctrl_switchtopanel(struct macctrls
*, unsigned int);
120 static void macctrl_setfocus(struct macctrls
*, union macctrl
*);
121 static void macctrl_text(struct macctrls
*, WindowPtr
,
122 struct mac_layoutstate
*, union control
*);
123 static void macctrl_editbox(struct macctrls
*, WindowPtr
,
124 struct mac_layoutstate
*, union control
*);
125 static void macctrl_radio(struct macctrls
*, WindowPtr
,
126 struct mac_layoutstate
*, union control
*);
127 static void macctrl_checkbox(struct macctrls
*, WindowPtr
,
128 struct mac_layoutstate
*, union control
*);
129 static void macctrl_button(struct macctrls
*, WindowPtr
,
130 struct mac_layoutstate
*, union control
*);
131 static void macctrl_popup(struct macctrls
*, WindowPtr
,
132 struct mac_layoutstate
*, union control
*);
133 #if !TARGET_API_MAC_CARBON
134 static pascal SInt32
macctrl_sys7_text_cdef(SInt16
, ControlRef
,
135 ControlDefProcMessage
, SInt32
);
136 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16
, ControlRef
,
137 ControlDefProcMessage
, SInt32
);
138 static pascal SInt32
macctrl_sys7_default_cdef(SInt16
, ControlRef
,
139 ControlDefProcMessage
, SInt32
);
142 #if !TARGET_API_MAC_CARBON
144 * This trick enables us to keep all the CDEF code in the main
145 * application, which makes life easier. For details, see
146 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
149 #pragma options align=mac68k
151 short jmpabs
; /* 4EF9 */
152 ControlDefUPP theUPP
;
154 #pragma options align=reset
157 static void macctrl_init()
159 #if !TARGET_API_MAC_CARBON
160 static int inited
= 0;
164 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_Text
);
165 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_text_cdef
);
166 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_EditBox
);
167 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_editbox_cdef
);
168 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_Default
);
169 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_default_cdef
);
175 static int macctrl_cmp_byctrl(void *av
, void *bv
)
177 union macctrl
*a
= (union macctrl
*)av
;
178 union macctrl
*b
= (union macctrl
*)bv
;
180 if (a
->generic
.ctrl
< b
->generic
.ctrl
)
182 else if (a
->generic
.ctrl
> b
->generic
.ctrl
)
188 static int macctrl_cmp_byctrl_find(void *av
, void *bv
)
190 union control
*a
= (union control
*)av
;
191 union macctrl
*b
= (union macctrl
*)bv
;
193 if (a
< b
->generic
.ctrl
)
195 else if (a
> b
->generic
.ctrl
)
201 void macctrl_layoutbox(struct controlbox
*cb
, WindowPtr window
,
202 struct macctrls
*mcs
)
205 struct mac_layoutstate curstate
;
210 #if TARGET_API_MAC_CARBON
211 GetPortBounds(GetWindowPort(window
), &rect
);
213 rect
= window
->portRect
;
215 curstate
.pos
.h
= rect
.left
+ 13;
216 curstate
.pos
.v
= rect
.bottom
- 59;
217 curstate
.width
= rect
.right
- rect
.left
- (13 * 2);
218 if (mac_gestalts
.apprvers
>= 0x100)
219 CreateRootControl(window
, &root
);
220 mcs
->window
= window
;
221 mcs
->byctrl
= newtree234(macctrl_cmp_byctrl
);
223 /* Count the number of panels */
225 for (i
= 1; i
< cb
->nctrlsets
; i
++)
226 if (strcmp(cb
->ctrlsets
[i
]->pathname
, cb
->ctrlsets
[i
-1]->pathname
))
228 mcs
->panels
= smalloc(sizeof(*mcs
->panels
) * mcs
->npanels
);
229 memset(mcs
->panels
, 0, sizeof(*mcs
->panels
) * mcs
->npanels
);
230 curstate
.panelnum
= 0;
231 for (i
= 0; i
< cb
->nctrlsets
; i
++) {
232 if (i
> 0 && strcmp(cb
->ctrlsets
[i
]->pathname
,
233 cb
->ctrlsets
[i
-1]->pathname
)) {
234 curstate
.pos
.v
= rect
.top
+ 13;
236 assert(curstate
.panelnum
< mcs
->npanels
);
238 macctrl_layoutset(&curstate
, cb
->ctrlsets
[i
], window
, mcs
);
240 macctrl_switchtopanel(mcs
, 1);
243 static void macctrl_layoutset(struct mac_layoutstate
*curstate
,
244 struct controlset
*s
,
245 WindowPtr window
, struct macctrls
*mcs
)
249 fprintf(stderr
, "--- begin set ---\n");
250 fprintf(stderr
, "pathname = %s\n", s
->pathname
);
251 if (s
->boxname
&& *s
->boxname
)
252 fprintf(stderr
, "boxname = %s\n", s
->boxname
);
254 fprintf(stderr
, "boxtitle = %s\n", s
->boxtitle
);
257 for (i
= 0; i
< s
->ncontrols
; i
++) {
258 union control
*ctrl
= s
->ctrls
[i
];
261 switch (ctrl
->generic
.type
) {
262 case CTRL_TEXT
: s
= "text"; break;
263 case CTRL_EDITBOX
: s
= "editbox"; break;
264 case CTRL_RADIO
: s
= "radio"; break;
265 case CTRL_CHECKBOX
: s
= "checkbox"; break;
266 case CTRL_BUTTON
: s
= "button"; break;
267 case CTRL_LISTBOX
: s
= "listbox"; break;
268 case CTRL_COLUMNS
: s
= "columns"; break;
269 case CTRL_FILESELECT
: s
= "fileselect"; break;
270 case CTRL_FONTSELECT
: s
= "fontselect"; break;
271 case CTRL_TABDELAY
: s
= "tabdelay"; break;
272 default: s
= "unknown"; break;
274 fprintf(stderr
, " control: %s\n", s
);
275 switch (ctrl
->generic
.type
) {
277 macctrl_text(mcs
, window
, curstate
, ctrl
);
280 macctrl_editbox(mcs
, window
, curstate
, ctrl
);
283 macctrl_radio(mcs
, window
, curstate
, ctrl
);
286 macctrl_checkbox(mcs
, window
, curstate
, ctrl
);
289 macctrl_button(mcs
, window
, curstate
, ctrl
);
292 if (ctrl
->listbox
.height
== 0)
293 macctrl_popup(mcs
, window
, curstate
, ctrl
);
299 static void macctrl_switchtopanel(struct macctrls
*mcs
, unsigned int which
)
304 #define hideshow(c) do { \
305 if (i == which) ShowControl(c); else HideControl(c); \
308 mcs
->curpanel
= which
;
309 /* Panel 0 is special and always visible. */
310 for (i
= 1; i
< mcs
->npanels
; i
++)
311 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
312 #if !TARGET_API_MAC_CARBON
313 if (mcs
->focus
== mc
)
314 macctrl_setfocus(mcs
, NULL
);
316 switch (mc
->generic
.type
) {
318 hideshow(mc
->text
.tbctrl
);
320 case MACCTRL_EDITBOX
:
321 hideshow(mc
->editbox
.tbctrl
);
324 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
325 hideshow(mc
->radio
.tbctrls
[j
]);
327 case MACCTRL_CHECKBOX
:
328 hideshow(mc
->checkbox
.tbctrl
);
331 hideshow(mc
->button
.tbctrl
);
334 hideshow(mc
->popup
.tbctrl
);
340 #if !TARGET_API_MAC_CARBON
342 * System 7 focus manipulation
344 static void macctrl_defocus(union macctrl
*mc
)
347 assert(mac_gestalts
.apprvers
< 0x100);
348 switch (mc
->generic
.type
) {
349 case MACCTRL_EDITBOX
:
350 TEDeactivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
355 static void macctrl_enfocus(union macctrl
*mc
)
358 assert(mac_gestalts
.apprvers
< 0x100);
359 switch (mc
->generic
.type
) {
360 case MACCTRL_EDITBOX
:
361 TEActivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
366 static void macctrl_setfocus(struct macctrls
*mcs
, union macctrl
*mc
)
369 if (mcs
->focus
!= NULL
)
370 macctrl_defocus(mcs
->focus
);
377 static void macctrl_text(struct macctrls
*mcs
, WindowPtr window
,
378 struct mac_layoutstate
*curstate
,
381 union macctrl
*mc
= smalloc(sizeof *mc
);
384 fprintf(stderr
, " label = %s\n", ctrl
->text
.label
);
385 mc
->generic
.type
= MACCTRL_TEXT
;
386 mc
->generic
.ctrl
= ctrl
;
387 mc
->generic
.privdata
= NULL
;
388 bounds
.left
= curstate
->pos
.h
;
389 bounds
.right
= bounds
.left
+ curstate
->width
;
390 bounds
.top
= curstate
->pos
.v
;
391 bounds
.bottom
= bounds
.top
+ 16;
392 if (mac_gestalts
.apprvers
>= 0x100) {
396 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
397 kControlStaticTextProc
, (long)mc
);
398 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
399 kControlStaticTextTextTag
,
400 strlen(ctrl
->text
.label
), ctrl
->text
.label
);
401 GetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
402 kControlStaticTextTextHeightTag
,
403 sizeof(height
), &height
, &olen
);
404 fprintf(stderr
, " height = %d\n", height
);
405 SizeControl(mc
->text
.tbctrl
, curstate
->width
, height
);
406 curstate
->pos
.v
+= height
+ 6;
410 c2pstrcpy(title
, ctrl
->text
.label
);
411 mc
->text
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 0,
412 SYS7_TEXT_PROC
, (long)mc
);
414 add234(mcs
->byctrl
, mc
);
415 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
416 mcs
->panels
[curstate
->panelnum
] = mc
;
419 #if !TARGET_API_MAC_CARBON
420 static pascal SInt32
macctrl_sys7_text_cdef(SInt16 variant
, ControlRef control
,
421 ControlDefProcMessage msg
, SInt32 param
)
427 if ((*control
)->contrlVis
)
428 TETextBox((*control
)->contrlTitle
+ 1, (*control
)->contrlTitle
[0],
429 &(*control
)->contrlRect
, teFlushDefault
);
432 if (param
& (1 << 31)) {
438 rgn
= (RgnHandle
)param
;
439 RectRgn(rgn
, &(*control
)->contrlRect
);
443 rgn
= (RgnHandle
)param
;
452 static void macctrl_editbox(struct macctrls
*mcs
, WindowPtr window
,
453 struct mac_layoutstate
*curstate
,
456 union macctrl
*mc
= smalloc(sizeof *mc
);
459 fprintf(stderr
, " label = %s\n", ctrl
->editbox
.label
);
460 fprintf(stderr
, " percentwidth = %d\n", ctrl
->editbox
.percentwidth
);
461 if (ctrl
->editbox
.password
) fprintf(stderr
, " password\n");
462 if (ctrl
->editbox
.has_list
) fprintf(stderr
, " has list\n");
463 mc
->generic
.type
= MACCTRL_EDITBOX
;
464 mc
->generic
.ctrl
= ctrl
;
465 mc
->generic
.privdata
= NULL
;
466 bounds
.left
= curstate
->pos
.h
;
467 bounds
.right
= bounds
.left
+ curstate
->width
;
468 bounds
.top
= curstate
->pos
.v
;
469 bounds
.bottom
= bounds
.top
+ 22;
470 if (mac_gestalts
.apprvers
>= 0x100) {
471 InsetRect(&bounds
, 2, 2);
472 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
473 ctrl
->editbox
.password ?
474 kControlEditTextPasswordProc
:
475 kControlEditTextProc
, (long)mc
);
477 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
478 SYS7_EDITBOX_PROC
, (long)mc
);
480 curstate
->pos
.v
+= 28;
481 add234(mcs
->byctrl
, mc
);
482 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
483 mcs
->panels
[curstate
->panelnum
] = mc
;
484 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
487 #if !TARGET_API_MAC_CARBON
488 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16 variant
,
490 ControlDefProcMessage msg
,
501 rect
= (*control
)->contrlRect
;
502 InsetRect(&rect
, 3, 3); /* 2 if it's 20 pixels high */
503 te
= TENew(&rect
, &rect
);
504 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
505 (*te
)->txSize
= LoWord(ssfs
);
506 (*te
)->txFont
= HiWord(ssfs
);
507 (*control
)->contrlData
= (Handle
)te
;
510 TEDispose((TEHandle
)(*control
)->contrlData
);
513 if ((*control
)->contrlVis
) {
514 rect
= (*control
)->contrlRect
;
517 InsetRect(&rect
, 3, 3);
518 TEUpdate(&rect
, (TEHandle
)(*control
)->contrlData
);
522 mouse
.h
= LoWord(param
);
523 mouse
.v
= HiWord(param
);
525 PtInRect(mouse
, &(*(TEHandle
)(*control
)->contrlData
)->viewRect
) ?
526 kControlEditTextPart
: kControlNoPart
;
528 if (param
& (1 << 31)) {
534 rgn
= (RgnHandle
)param
;
535 RectRgn(rgn
, &(*control
)->contrlRect
);
539 rgn
= (RgnHandle
)param
;
548 static void macctrl_radio(struct macctrls
*mcs
, WindowPtr window
,
549 struct mac_layoutstate
*curstate
,
552 union macctrl
*mc
= smalloc(sizeof *mc
);
555 unsigned int i
, colwidth
;
557 fprintf(stderr
, " label = %s\n", ctrl
->radio
.label
);
558 mc
->generic
.type
= MACCTRL_RADIO
;
559 mc
->generic
.ctrl
= ctrl
;
560 mc
->generic
.privdata
= NULL
;
562 smalloc(sizeof(*mc
->radio
.tbctrls
) * ctrl
->radio
.nbuttons
);
563 colwidth
= (curstate
->width
+ 13) / ctrl
->radio
.ncolumns
;
564 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
565 fprintf(stderr
, " button = %s\n", ctrl
->radio
.buttons
[i
]);
566 bounds
.top
= curstate
->pos
.v
- 2;
567 bounds
.bottom
= bounds
.top
+ 18;
568 bounds
.left
= curstate
->pos
.h
+ colwidth
* (i
% ctrl
->radio
.ncolumns
);
569 if (i
== ctrl
->radio
.nbuttons
- 1 ||
570 i
% ctrl
->radio
.ncolumns
== ctrl
->radio
.ncolumns
- 1) {
571 bounds
.right
= curstate
->pos
.h
+ curstate
->width
;
572 curstate
->pos
.v
+= 18;
574 bounds
.right
= bounds
.left
+ colwidth
- 13;
575 c2pstrcpy(title
, ctrl
->radio
.buttons
[i
]);
576 mc
->radio
.tbctrls
[i
] = NewControl(window
, &bounds
, title
, TRUE
,
577 0, 0, 1, radioButProc
, (long)mc
);
579 curstate
->pos
.v
+= 4;
580 add234(mcs
->byctrl
, mc
);
581 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
582 mcs
->panels
[curstate
->panelnum
] = mc
;
583 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
586 static void macctrl_checkbox(struct macctrls
*mcs
, WindowPtr window
,
587 struct mac_layoutstate
*curstate
,
590 union macctrl
*mc
= smalloc(sizeof *mc
);
594 fprintf(stderr
, " label = %s\n", ctrl
->checkbox
.label
);
595 mc
->generic
.type
= MACCTRL_CHECKBOX
;
596 mc
->generic
.ctrl
= ctrl
;
597 mc
->generic
.privdata
= NULL
;
598 bounds
.left
= curstate
->pos
.h
;
599 bounds
.right
= bounds
.left
+ curstate
->width
;
600 bounds
.top
= curstate
->pos
.v
;
601 bounds
.bottom
= bounds
.top
+ 16;
602 c2pstrcpy(title
, ctrl
->checkbox
.label
);
603 mc
->checkbox
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
604 checkBoxProc
, (long)mc
);
605 add234(mcs
->byctrl
, mc
);
606 curstate
->pos
.v
+= 22;
607 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
608 mcs
->panels
[curstate
->panelnum
] = mc
;
609 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
612 static void macctrl_button(struct macctrls
*mcs
, WindowPtr window
,
613 struct mac_layoutstate
*curstate
,
616 union macctrl
*mc
= smalloc(sizeof *mc
);
620 fprintf(stderr
, " label = %s\n", ctrl
->button
.label
);
621 if (ctrl
->button
.isdefault
)
622 fprintf(stderr
, " is default\n");
623 mc
->generic
.type
= MACCTRL_BUTTON
;
624 mc
->generic
.ctrl
= ctrl
;
625 mc
->generic
.privdata
= NULL
;
626 bounds
.left
= curstate
->pos
.h
;
627 bounds
.right
= bounds
.left
+ 100; /* XXX measure string */
628 bounds
.top
= curstate
->pos
.v
;
629 bounds
.bottom
= bounds
.top
+ 20;
630 c2pstrcpy(title
, ctrl
->button
.label
);
631 mc
->button
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
632 pushButProc
, (long)mc
);
633 if (mac_gestalts
.apprvers
>= 0x100) {
634 Boolean isdefault
= ctrl
->button
.isdefault
;
636 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
637 kControlPushButtonDefaultTag
,
638 sizeof(isdefault
), &isdefault
);
639 } else if (ctrl
->button
.isdefault
) {
640 InsetRect(&bounds
, -4, -4);
641 NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
642 SYS7_DEFAULT_PROC
, (long)mc
);
644 if (mac_gestalts
.apprvers
>= 0x110) {
645 Boolean iscancel
= ctrl
->button
.iscancel
;
647 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
648 kControlPushButtonCancelTag
,
649 sizeof(iscancel
), &iscancel
);
651 add234(mcs
->byctrl
, mc
);
652 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
653 mcs
->panels
[curstate
->panelnum
] = mc
;
654 curstate
->pos
.v
+= 26;
657 #if !TARGET_API_MAC_CARBON
658 static pascal SInt32
macctrl_sys7_default_cdef(SInt16 variant
,
660 ControlDefProcMessage msg
,
669 if ((*control
)->contrlVis
) {
670 rect
= (*control
)->contrlRect
;
673 oval
= (rect
.bottom
- rect
.top
) / 2 + 2;
674 FrameRoundRect(&rect
, oval
, oval
);
678 if (param
& (1 << 31)) {
684 rgn
= (RgnHandle
)param
;
685 RectRgn(rgn
, &(*control
)->contrlRect
);
689 rgn
= (RgnHandle
)param
;
698 static void macctrl_popup(struct macctrls
*mcs
, WindowPtr window
,
699 struct mac_layoutstate
*curstate
,
702 union macctrl
*mc
= smalloc(sizeof *mc
);
705 unsigned int labelwidth
;
706 static int nextmenuid
= MENU_MIN
;
711 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
712 * create a popup menu with dynamic content.
714 assert(ctrl
->listbox
.height
== 0);
715 assert(!ctrl
->listbox
.draglist
);
716 assert(!ctrl
->listbox
.multisel
);
718 fprintf(stderr
, " label = %s\n", ctrl
->listbox
.label
);
719 fprintf(stderr
, " percentwidth = %d\n", ctrl
->listbox
.percentwidth
);
721 mc
->generic
.type
= MACCTRL_POPUP
;
722 mc
->generic
.ctrl
= ctrl
;
723 mc
->generic
.privdata
= NULL
;
724 c2pstrcpy(title
, ctrl
->button
.label
);
726 /* Find a spare menu ID and create the menu */
727 while (GetMenuHandle(nextmenuid
) != NULL
)
728 if (++nextmenuid
>= MENU_MAX
) nextmenuid
= MENU_MIN
;
729 menuid
= nextmenuid
++;
730 menu
= NewMenu(menuid
, "\pdummy");
731 if (menu
== NULL
) return;
732 mc
->popup
.menu
= menu
;
733 mc
->popup
.menuid
= menuid
;
734 InsertMenu(menu
, kInsertHierarchicalMenu
);
736 /* The menu starts off empty */
738 mc
->popup
.ids
= NULL
;
740 bounds
.left
= curstate
->pos
.h
;
741 bounds
.right
= bounds
.left
+ curstate
->width
;
742 bounds
.top
= curstate
->pos
.v
;
743 bounds
.bottom
= bounds
.top
+ 20;
744 /* XXX handle percentwidth == 100 */
745 labelwidth
= curstate
->width
* (100 - ctrl
->listbox
.percentwidth
) / 100;
746 mc
->popup
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
,
747 popupTitleLeftJust
, menuid
, labelwidth
,
748 popupMenuProc
+ popupFixedWidth
, (long)mc
);
749 add234(mcs
->byctrl
, mc
);
750 curstate
->pos
.v
+= 26;
751 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
752 mcs
->panels
[curstate
->panelnum
] = mc
;
753 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
757 void macctrl_activate(WindowPtr window
, EventRecord
*event
)
759 struct macctrls
*mcs
= mac_winctrls(window
);
760 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
763 ControlPartCode state
;
767 SetPort((GrafPtr
)GetWindowPort(window
));
768 if (mac_gestalts
.apprvers
>= 0x100)
769 SetThemeWindowBackground(window
, active ?
770 kThemeBrushModelessDialogBackgroundActive
:
771 kThemeBrushModelessDialogBackgroundInactive
,
773 state
= active ? kControlNoPart
: kControlInactivePart
;
774 for (i
= 0; i
<= mcs
->curpanel
; i
+= mcs
->curpanel
)
775 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
)
776 switch (mc
->generic
.type
) {
778 HiliteControl(mc
->text
.tbctrl
, state
);
780 case MACCTRL_EDITBOX
:
781 HiliteControl(mc
->editbox
.tbctrl
, state
);
784 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
785 HiliteControl(mc
->radio
.tbctrls
[j
], state
);
787 case MACCTRL_CHECKBOX
:
788 HiliteControl(mc
->checkbox
.tbctrl
, state
);
791 HiliteControl(mc
->button
.tbctrl
, state
);
794 HiliteControl(mc
->popup
.tbctrl
, state
);
800 void macctrl_click(WindowPtr window
, EventRecord
*event
)
803 ControlHandle control
;
804 int part
, trackresult
;
807 struct macctrls
*mcs
= mac_winctrls(window
);
812 SetPort((GrafPtr
)GetWindowPort(window
));
813 mouse
= event
->where
;
814 GlobalToLocal(&mouse
);
815 part
= FindControl(mouse
, window
, &control
);
816 if (control
!= NULL
) {
817 mc
= (union macctrl
*)GetControlReference(control
);
818 if (mac_gestalts
.apprvers
>= 0x100) {
819 if (GetControlFeatures(control
, &features
) == noErr
&&
820 (features
& kControlSupportsFocus
) &&
821 (features
& kControlGetsFocusOnClick
))
822 SetKeyboardFocus(window
, control
, part
);
823 trackresult
= HandleControlClick(control
, mouse
, event
->modifiers
,
824 (ControlActionUPP
)-1);
826 #if !TARGET_API_MAC_CARBON
827 if (mc
->generic
.type
== MACCTRL_EDITBOX
&&
828 control
== mc
->editbox
.tbctrl
) {
829 TEHandle te
= (TEHandle
)(*control
)->contrlData
;
831 macctrl_setfocus(mcs
, mc
);
832 TEClick(mouse
, !!(event
->modifiers
& shiftKey
), te
);
836 trackresult
= TrackControl(control
, mouse
, (ControlActionUPP
)-1);
838 switch (mc
->generic
.type
) {
840 if (trackresult
!= 0) {
841 for (i
= 0; i
< mc
->generic
.ctrl
->radio
.nbuttons
; i
++)
842 if (mc
->radio
.tbctrls
[i
] == control
)
843 SetControlValue(mc
->radio
.tbctrls
[i
],
844 kControlRadioButtonCheckedValue
);
846 SetControlValue(mc
->radio
.tbctrls
[i
],
847 kControlRadioButtonUncheckedValue
);
848 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
851 case MACCTRL_CHECKBOX
:
852 if (trackresult
!= 0) {
853 SetControlValue(control
, !GetControlValue(control
));
854 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
858 if (trackresult
!= 0)
859 ctrlevent(mcs
, mc
, EVENT_ACTION
);
862 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
870 void macctrl_key(WindowPtr window
, EventRecord
*event
)
873 struct macctrls
*mcs
= mac_winctrls(window
);
876 if (mac_gestalts
.apprvers
>= 0x100 &&
877 GetKeyboardFocus(window
, &control
) == noErr
&& control
!= NULL
) {
878 HandleControlKey(control
, (event
->message
& keyCodeMask
) >> 8,
879 event
->message
& charCodeMask
, event
->modifiers
);
880 mc
= (union macctrl
*)GetControlReference(control
);
881 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
885 void macctrl_update(WindowPtr window
)
887 #if TARGET_API_MAC_CARBON
895 SetPort((GrafPtr
)GetWindowPort(window
));
896 if (mac_gestalts
.apprvers
>= 0x101) {
897 #if TARGET_API_MAC_CARBON
898 GetPortBounds(GetWindowPort(window
), &rect
);
900 rect
= window
->portRect
;
902 InsetRect(&rect
, -1, -1);
903 DrawThemeModelessDialogFrame(&rect
, mac_frontwindow() == window ?
904 kThemeStateActive
: kThemeStateInactive
);
906 #if TARGET_API_MAC_CARBON
908 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
909 UpdateControls(window
, visrgn
);
912 UpdateControls(window
, window
->visRgn
);
918 #if TARGET_API_MAC_CARBON
919 #define EnableItem EnableMenuItem
920 #define DisableItem DisableMenuItem
922 void macctrl_adjustmenus(WindowPtr window
)
926 menu
= GetMenuHandle(mFile
);
927 DisableItem(menu
, iSave
); /* XXX enable if modified */
928 EnableItem(menu
, iSaveAs
);
929 EnableItem(menu
, iDuplicate
);
931 menu
= GetMenuHandle(mEdit
);
932 DisableItem(menu
, 0);
935 void macctrl_close(WindowPtr window
)
937 struct macctrls
*mcs
= mac_winctrls(window
);
941 * Mostly, we don't bother disposing of the Toolbox controls,
942 * since that will happen automatically when the window is
943 * disposed of. Popup menus are an exception, because we have to
944 * dispose of the menu ourselves, and doing that while the control
945 * still holds a reference to it seems rude.
947 while ((mc
= index234(mcs
->byctrl
, 0)) != NULL
) {
948 if (mc
->generic
.privdata
!= NULL
&& mc
->generic
.freeprivdata
)
949 sfree(mc
->generic
.privdata
);
950 switch (mc
->generic
.type
) {
952 DisposeControl(mc
->popup
.tbctrl
);
953 DeleteMenu(mc
->popup
.menuid
);
954 DisposeMenu(mc
->popup
.menu
);
957 del234(mcs
->byctrl
, mc
);
961 freetree234(mcs
->byctrl
);
967 void dlg_update_start(union control
*ctrl
, void *dlg
)
973 void dlg_update_done(union control
*ctrl
, void *dlg
)
979 void dlg_set_focus(union control
*ctrl
, void *dlg
)
982 if (mac_gestalts
.apprvers
>= 0x100) {
983 /* Use SetKeyboardFocus() */
985 /* Do our own mucking around */
989 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
995 void dlg_beep(void *dlg
)
1001 void dlg_error_msg(void *dlg
, char *msg
)
1005 c2pstrcpy(pmsg
, msg
);
1006 ParamText(pmsg
, NULL
, NULL
, NULL
);
1007 StopAlert(128, NULL
);
1010 void dlg_end(void *dlg
, int value
)
1015 void dlg_refresh(union control
*ctrl
, void *dlg
)
1017 struct macctrls
*mcs
= dlg
;
1022 mc
= findbyctrl(mcs
, ctrl
);
1024 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1027 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
1029 struct macctrls
*mcs
= dlg
;
1030 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1033 return mc
->generic
.privdata
;
1036 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
1038 struct macctrls
*mcs
= dlg
;
1039 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1042 mc
->generic
.privdata
= ptr
;
1043 mc
->generic
.freeprivdata
= FALSE
;
1046 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
1048 struct macctrls
*mcs
= dlg
;
1049 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1052 mc
->generic
.privdata
= smalloc(size
);
1053 mc
->generic
.freeprivdata
= TRUE
;
1054 return mc
->generic
.privdata
;
1059 * Radio Button control
1062 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
1064 struct macctrls
*mcs
= dlg
;
1065 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1069 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1070 if (i
== whichbutton
)
1071 SetControlValue(mc
->radio
.tbctrls
[i
],
1072 kControlRadioButtonCheckedValue
);
1074 SetControlValue(mc
->radio
.tbctrls
[i
],
1075 kControlRadioButtonUncheckedValue
);
1080 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
1082 struct macctrls
*mcs
= dlg
;
1083 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1087 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1088 if (GetControlValue(mc
->radio
.tbctrls
[i
]) ==
1089 kControlRadioButtonCheckedValue
)
1100 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
1102 struct macctrls
*mcs
= dlg
;
1103 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1106 SetControlValue(mc
->checkbox
.tbctrl
,
1107 checked ? kControlCheckBoxCheckedValue
:
1108 kControlCheckBoxUncheckedValue
);
1111 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
1113 struct macctrls
*mcs
= dlg
;
1114 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1117 return GetControlValue(mc
->checkbox
.tbctrl
);
1125 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
1127 struct macctrls
*mcs
= dlg
;
1128 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1132 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1134 SetPort((GrafPtr
)(GetWindowPort(mcs
->window
)));
1135 if (mac_gestalts
.apprvers
>= 0x100)
1136 SetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1137 ctrl
->editbox
.password ?
1138 kControlEditTextPasswordTag
:
1139 kControlEditTextTextTag
,
1140 strlen(text
), text
);
1141 #if !TARGET_API_MAC_CARBON
1143 TESetText(text
, strlen(text
),
1144 (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
1146 DrawOneControl(mc
->editbox
.tbctrl
);
1150 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
1152 struct macctrls
*mcs
= dlg
;
1153 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1157 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1158 if (mac_gestalts
.apprvers
>= 0x100) {
1159 if (GetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1160 ctrl
->editbox
.password ?
1161 kControlEditTextPasswordTag
:
1162 kControlEditTextTextTag
,
1163 length
- 1, buffer
, &olen
) != noErr
)
1165 if (olen
> length
- 1)
1168 #if !TARGET_API_MAC_CARBON
1170 TEHandle te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1172 olen
= (*te
)->teLength
;
1173 if (olen
> length
- 1)
1175 memcpy(buffer
, *(*te
)->hText
, olen
);
1178 buffer
[olen
] = '\0';
1179 fprintf(stderr
, "dlg_editbox_get: %s\n", buffer
);
1187 static void dlg_macpopup_clear(union control
*ctrl
, void *dlg
)
1189 struct macctrls
*mcs
= dlg
;
1190 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1191 MenuRef menu
= mc
->popup
.menu
;
1194 fprintf(stderr
, " popup_clear\n");
1195 n
= CountMenuItems(menu
);
1196 for (i
= 0; i
< n
; i
++)
1197 DeleteMenuItem(menu
, n
- i
);
1199 sfree(mc
->popup
.ids
);
1200 mc
->popup
.ids
= NULL
;
1201 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1204 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
1207 if (ctrl
->listbox
.height
== 0)
1208 dlg_macpopup_clear(ctrl
, dlg
);
1211 static void dlg_macpopup_del(union control
*ctrl
, void *dlg
, int index
)
1213 struct macctrls
*mcs
= dlg
;
1214 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1215 MenuRef menu
= mc
->popup
.menu
;
1217 fprintf(stderr
, " popup_del %d\n", index
);
1218 DeleteMenuItem(menu
, index
+ 1);
1219 if (mc
->popup
.ids
!= NULL
)
1220 memcpy(mc
->popup
.ids
+ index
, mc
->popup
.ids
+ index
+ 1,
1221 (mc
->popup
.nids
- index
- 1) * sizeof(*mc
->popup
.ids
));
1222 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1225 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
1228 if (ctrl
->listbox
.height
== 0)
1229 dlg_macpopup_del(ctrl
, dlg
, index
);
1232 static void dlg_macpopup_add(union control
*ctrl
, void *dlg
, char const *text
)
1234 struct macctrls
*mcs
= dlg
;
1235 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1236 MenuRef menu
= mc
->popup
.menu
;
1239 fprintf(stderr
, " popup_add %s\n", text
);
1240 assert(text
[0] != '\0');
1241 c2pstrcpy(itemstring
, text
);
1242 AppendMenu(menu
, "\pdummy");
1243 SetMenuItemText(menu
, CountMenuItems(menu
), itemstring
);
1244 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1247 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
1250 if (ctrl
->listbox
.height
== 0)
1251 dlg_macpopup_add(ctrl
, dlg
, text
);
1254 static void dlg_macpopup_addwithid(union control
*ctrl
, void *dlg
,
1255 char const *text
, int id
)
1257 struct macctrls
*mcs
= dlg
;
1258 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1259 MenuRef menu
= mc
->popup
.menu
;
1262 fprintf(stderr
, " popup_addwthindex %s, %d\n", text
, id
);
1263 dlg_macpopup_add(ctrl
, dlg
, text
);
1264 index
= CountMenuItems(menu
) - 1;
1265 if (mc
->popup
.nids
<= index
) {
1266 mc
->popup
.nids
= index
+ 1;
1267 mc
->popup
.ids
= srealloc(mc
->popup
.ids
,
1268 mc
->popup
.nids
* sizeof(*mc
->popup
.ids
));
1270 mc
->popup
.ids
[index
] = id
;
1273 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
1274 char const *text
, int id
)
1277 if (ctrl
->listbox
.height
== 0)
1278 dlg_macpopup_addwithid(ctrl
, dlg
, text
, id
);
1281 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
1283 struct macctrls
*mcs
= dlg
;
1284 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1286 if (ctrl
->listbox
.height
== 0) {
1287 assert(mc
->popup
.ids
!= NULL
&& mc
->popup
.nids
> index
);
1288 return mc
->popup
.ids
[index
];
1293 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
1295 struct macctrls
*mcs
= dlg
;
1296 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1298 if (ctrl
->listbox
.height
== 0)
1299 return GetControlValue(mc
->popup
.tbctrl
) - 1;
1303 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
1305 struct macctrls
*mcs
= dlg
;
1306 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1308 if (ctrl
->listbox
.height
== 0)
1309 return GetControlValue(mc
->popup
.tbctrl
) - 1 == index
;
1313 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
1315 struct macctrls
*mcs
= dlg
;
1316 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1318 if (ctrl
->listbox
.height
== 0)
1319 SetControlValue(mc
->popup
.tbctrl
, index
+ 1);
1327 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
1329 struct macctrls
*mcs
= dlg
;
1330 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1334 if (mac_gestalts
.apprvers
>= 0x100)
1335 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
1336 kControlStaticTextTextTag
, strlen(text
), text
);
1338 c2pstrcpy(title
, text
);
1339 SetControlTitle(mc
->text
.tbctrl
, title
);
1345 * File Selector control
1348 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
1353 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
1360 * Font Selector control
1363 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fn
)
1368 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fn
)
1375 * Printer enumeration
1378 printer_enum
*printer_start_enum(int *nprinters
)
1385 char *printer_get_name(printer_enum
*pe
, int thing
)
1391 void printer_finish_enum(printer_enum
*pe
)
1398 * Colour selection stuff
1401 void dlg_coloursel_start(union control
*ctrl
, void *dlg
,
1402 int r
, int g
, int b
)
1407 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1408 int *r
, int *g
, int *b
)
1416 * c-file-style: "simon"