1 /* $Id: macctrls.c,v 1.32 2003/04/13 13:52:44 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>
35 #include <Resources.h>
39 #include <TextUtils.h>
40 #include <ToolUtils.h>
52 /* Range of menu IDs for popup menus */
58 struct macctrl_generic
{
68 /* Template from which this was generated */
70 /* Next control in this panel */
76 struct macctrl_generic generic
;
80 struct macctrl_generic generic
;
85 struct macctrl_generic generic
;
90 struct macctrl_generic generic
;
94 struct macctrl_generic generic
;
99 struct macctrl_generic generic
;
106 struct macctrl_generic generic
;
115 struct mac_layoutstate
{
118 unsigned int panelnum
;
121 #define ctrlevent(mcs, mc, event) do { \
122 if ((mc)->generic.ctrl->generic.handler != NULL) \
123 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
124 (mcs)->data, (event)); \
127 #define findbyctrl(mcs, ctrl) \
128 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
130 static void macctrl_layoutset(struct mac_layoutstate
*, struct controlset
*,
131 WindowPtr
, struct macctrls
*);
132 static void macctrl_switchtopanel(struct macctrls
*, unsigned int);
133 static void macctrl_setfocus(struct macctrls
*, union macctrl
*);
134 static void macctrl_text(struct macctrls
*, WindowPtr
,
135 struct mac_layoutstate
*, union control
*);
136 static void macctrl_editbox(struct macctrls
*, WindowPtr
,
137 struct mac_layoutstate
*, union control
*);
138 static void macctrl_radio(struct macctrls
*, WindowPtr
,
139 struct mac_layoutstate
*, union control
*);
140 static void macctrl_checkbox(struct macctrls
*, WindowPtr
,
141 struct mac_layoutstate
*, union control
*);
142 static void macctrl_button(struct macctrls
*, WindowPtr
,
143 struct mac_layoutstate
*, union control
*);
144 static void macctrl_listbox(struct macctrls
*, WindowPtr
,
145 struct mac_layoutstate
*, union control
*);
146 static void macctrl_popup(struct macctrls
*, WindowPtr
,
147 struct mac_layoutstate
*, union control
*);
148 #if !TARGET_API_MAC_CARBON
149 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16
, ControlRef
,
150 ControlDefProcMessage
, SInt32
);
151 static pascal SInt32
macctrl_sys7_default_cdef(SInt16
, ControlRef
,
152 ControlDefProcMessage
, SInt32
);
153 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16
, ControlRef
,
154 ControlDefProcMessage
, SInt32
);
157 #if !TARGET_API_MAC_CARBON
159 * This trick enables us to keep all the CDEF code in the main
160 * application, which makes life easier. For details, see
161 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
164 #pragma options align=mac68k
166 short jmpabs
; /* 4EF9 */
167 ControlDefUPP theUPP
;
169 #pragma options align=reset
172 static void macctrl_init()
174 #if !TARGET_API_MAC_CARBON
175 static int inited
= 0;
179 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_EditBox
);
180 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_editbox_cdef
);
181 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_Default
);
182 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_default_cdef
);
183 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_ListBox
);
184 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_listbox_cdef
);
190 static int macctrl_cmp_byctrl(void *av
, void *bv
)
192 union macctrl
*a
= (union macctrl
*)av
;
193 union macctrl
*b
= (union macctrl
*)bv
;
195 if (a
->generic
.ctrl
< b
->generic
.ctrl
)
197 else if (a
->generic
.ctrl
> b
->generic
.ctrl
)
203 static int macctrl_cmp_byctrl_find(void *av
, void *bv
)
205 union control
*a
= (union control
*)av
;
206 union macctrl
*b
= (union macctrl
*)bv
;
208 if (a
< b
->generic
.ctrl
)
210 else if (a
> b
->generic
.ctrl
)
216 void macctrl_layoutbox(struct controlbox
*cb
, WindowPtr window
,
217 struct macctrls
*mcs
)
220 struct mac_layoutstate curstate
;
225 #if TARGET_API_MAC_CARBON
226 GetPortBounds(GetWindowPort(window
), &rect
);
228 rect
= window
->portRect
;
230 curstate
.pos
.h
= rect
.left
+ 13;
231 curstate
.pos
.v
= rect
.bottom
- 33;
232 curstate
.width
= rect
.right
- rect
.left
- (13 * 2);
233 if (mac_gestalts
.apprvers
>= 0x100)
234 CreateRootControl(window
, &root
);
235 mcs
->window
= window
;
236 mcs
->byctrl
= newtree234(macctrl_cmp_byctrl
);
238 mcs
->defbutton
= NULL
;
239 mcs
->canbutton
= NULL
;
240 /* Count the number of panels */
242 for (i
= 1; i
< cb
->nctrlsets
; i
++)
243 if (strcmp(cb
->ctrlsets
[i
]->pathname
, cb
->ctrlsets
[i
-1]->pathname
))
245 mcs
->panels
= snewn(mcs
->npanels
, union macctrl
*);
246 memset(mcs
->panels
, 0, sizeof(*mcs
->panels
) * mcs
->npanels
);
247 curstate
.panelnum
= 0;
248 for (i
= 0; i
< cb
->nctrlsets
; i
++) {
249 if (i
> 0 && strcmp(cb
->ctrlsets
[i
]->pathname
,
250 cb
->ctrlsets
[i
-1]->pathname
)) {
251 curstate
.pos
.v
= rect
.top
+ 13;
253 assert(curstate
.panelnum
< mcs
->npanels
);
255 macctrl_layoutset(&curstate
, cb
->ctrlsets
[i
], window
, mcs
);
257 macctrl_switchtopanel(mcs
, 1);
258 /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */
263 static void macctrl_layoutset(struct mac_layoutstate
*curstate
,
264 struct controlset
*s
,
265 WindowPtr window
, struct macctrls
*mcs
)
267 unsigned int i
, j
, ncols
, colstart
;
268 struct mac_layoutstate cols
[MAXCOLS
];
270 fprintf(stderr
, "--- begin set ---\n");
271 fprintf(stderr
, "pathname = %s\n", s
->pathname
);
272 if (s
->boxname
&& *s
->boxname
)
273 fprintf(stderr
, "boxname = %s\n", s
->boxname
);
275 fprintf(stderr
, "boxtitle = %s\n", s
->boxtitle
);
280 for (i
= 0; i
< s
->ncontrols
; i
++) {
281 union control
*ctrl
= s
->ctrls
[i
];
284 colstart
= COLUMN_START(ctrl
->generic
.column
);
285 switch (ctrl
->generic
.type
) {
286 case CTRL_TEXT
: s
= "text"; break;
287 case CTRL_EDITBOX
: s
= "editbox"; break;
288 case CTRL_RADIO
: s
= "radio"; break;
289 case CTRL_CHECKBOX
: s
= "checkbox"; break;
290 case CTRL_BUTTON
: s
= "button"; break;
291 case CTRL_LISTBOX
: s
= "listbox"; break;
292 case CTRL_COLUMNS
: s
= "columns"; break;
293 case CTRL_FILESELECT
: s
= "fileselect"; break;
294 case CTRL_FONTSELECT
: s
= "fontselect"; break;
295 case CTRL_TABDELAY
: s
= "tabdelay"; break;
296 default: s
= "unknown"; break;
298 fprintf(stderr
, " control: %s\n", s
);
299 switch (ctrl
->generic
.type
) {
301 if (ctrl
->columns
.ncols
!= 1) {
302 ncols
= ctrl
->columns
.ncols
;
303 fprintf(stderr
, " split to %d\n", ncols
);
304 assert(ncols
<= MAXCOLS
);
305 for (j
= 0; j
< ncols
; j
++) {
308 cols
[j
].pos
.h
= cols
[j
-1].pos
.h
+ cols
[j
-1].width
+ 6;
310 cols
[j
].width
= curstate
->width
-
311 (cols
[j
].pos
.h
- curstate
->pos
.h
);
313 cols
[j
].width
= (curstate
->width
+ 6) *
314 ctrl
->columns
.percentages
[j
] / 100 - 6;
317 fprintf(stderr
, " join\n");
318 for (j
= 0; j
< ncols
; j
++)
319 if (cols
[j
].pos
.v
> cols
[0].pos
.v
)
320 cols
[0].pos
.v
= cols
[j
].pos
.v
;
321 cols
[0].width
= curstate
->width
;
326 macctrl_text(mcs
, window
, &cols
[colstart
], ctrl
);
329 macctrl_editbox(mcs
, window
, &cols
[colstart
], ctrl
);
332 macctrl_radio(mcs
, window
, &cols
[colstart
], ctrl
);
335 macctrl_checkbox(mcs
, window
, &cols
[colstart
], ctrl
);
338 macctrl_button(mcs
, window
, &cols
[colstart
], ctrl
);
341 if (ctrl
->listbox
.height
== 0)
342 macctrl_popup(mcs
, window
, &cols
[colstart
], ctrl
);
344 macctrl_listbox(mcs
, window
, &cols
[colstart
], ctrl
);
348 for (j
= 0; j
< ncols
; j
++)
349 if (cols
[j
].pos
.v
> curstate
->pos
.v
)
350 curstate
->pos
.v
= cols
[j
].pos
.v
;
353 static void macctrl_switchtopanel(struct macctrls
*mcs
, unsigned int which
)
358 #define hideshow(c) do { \
359 if (i == which) ShowControl(c); else HideControl(c); \
362 mcs
->curpanel
= which
;
363 /* Panel 0 is special and always visible. */
364 for (i
= 1; i
< mcs
->npanels
; i
++)
365 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
366 #if !TARGET_API_MAC_CARBON
367 if (mcs
->focus
== mc
)
368 macctrl_setfocus(mcs
, NULL
);
370 switch (mc
->generic
.type
) {
372 hideshow(mc
->text
.tbctrl
);
374 case MACCTRL_EDITBOX
:
375 hideshow(mc
->editbox
.tbctrl
);
376 if (mc
->editbox
.tblabel
!= NULL
)
377 hideshow(mc
->editbox
.tblabel
);
380 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
381 hideshow(mc
->radio
.tbctrls
[j
]);
382 if (mc
->radio
.tblabel
!= NULL
)
383 hideshow(mc
->radio
.tblabel
);
385 case MACCTRL_CHECKBOX
:
386 hideshow(mc
->checkbox
.tbctrl
);
389 hideshow(mc
->button
.tbctrl
);
391 case MACCTRL_LISTBOX
:
392 hideshow(mc
->listbox
.tbctrl
);
394 * At least under Mac OS 8.1, hiding a list box
395 * doesn't hide its scroll bars.
397 #if TARGET_API_MAC_CARBON
398 hideshow(GetListVerticalScrollBar(mc
->listbox
.list
));
400 hideshow((*mc
->listbox
.list
)->vScroll
);
404 hideshow(mc
->popup
.tbctrl
);
410 #if !TARGET_API_MAC_CARBON
412 * System 7 focus manipulation
414 static void macctrl_defocus(union macctrl
*mc
)
417 assert(mac_gestalts
.apprvers
< 0x100);
418 switch (mc
->generic
.type
) {
419 case MACCTRL_EDITBOX
:
420 TEDeactivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
425 static void macctrl_enfocus(union macctrl
*mc
)
428 assert(mac_gestalts
.apprvers
< 0x100);
429 switch (mc
->generic
.type
) {
430 case MACCTRL_EDITBOX
:
431 TEActivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
436 static void macctrl_setfocus(struct macctrls
*mcs
, union macctrl
*mc
)
439 if (mcs
->focus
== mc
)
441 if (mcs
->focus
!= NULL
)
442 macctrl_defocus(mcs
->focus
);
449 static void macctrl_text(struct macctrls
*mcs
, WindowPtr window
,
450 struct mac_layoutstate
*curstate
,
453 union macctrl
*mc
= snew(union macctrl
);
457 assert(ctrl
->text
.label
!= NULL
);
458 fprintf(stderr
, " label = %s\n", ctrl
->text
.label
);
459 mc
->generic
.type
= MACCTRL_TEXT
;
460 mc
->generic
.ctrl
= ctrl
;
461 mc
->generic
.privdata
= NULL
;
462 bounds
.left
= curstate
->pos
.h
;
463 bounds
.right
= bounds
.left
+ curstate
->width
;
464 bounds
.top
= curstate
->pos
.v
;
465 bounds
.bottom
= bounds
.top
+ 16;
466 if (mac_gestalts
.apprvers
>= 0x100) {
469 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
470 kControlStaticTextProc
, (long)mc
);
471 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
472 kControlStaticTextTextTag
,
473 strlen(ctrl
->text
.label
), ctrl
->text
.label
);
474 GetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
475 kControlStaticTextTextHeightTag
,
476 sizeof(height
), &height
, &olen
);
478 #if !TARGET_API_MAC_CARBON
482 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
483 SYS7_TEXT_PROC
, (long)mc
);
484 te
= (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
;
485 TESetText(ctrl
->text
.label
, strlen(ctrl
->text
.label
), te
);
486 height
= TEGetHeight(1, (*te
)->nLines
, te
);
489 fprintf(stderr
, " height = %d\n", height
);
490 SizeControl(mc
->text
.tbctrl
, curstate
->width
, height
);
491 curstate
->pos
.v
+= height
+ 6;
492 add234(mcs
->byctrl
, mc
);
493 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
494 mcs
->panels
[curstate
->panelnum
] = mc
;
497 static void macctrl_editbox(struct macctrls
*mcs
, WindowPtr window
,
498 struct mac_layoutstate
*curstate
,
501 union macctrl
*mc
= snew(union macctrl
);
502 Rect lbounds
, bounds
;
504 if (ctrl
->editbox
.label
!= NULL
)
505 fprintf(stderr
, " label = %s\n", ctrl
->editbox
.label
);
506 fprintf(stderr
, " percentwidth = %d\n", ctrl
->editbox
.percentwidth
);
507 if (ctrl
->editbox
.password
) fprintf(stderr
, " password\n");
508 if (ctrl
->editbox
.has_list
) fprintf(stderr
, " has list\n");
509 mc
->generic
.type
= MACCTRL_EDITBOX
;
510 mc
->generic
.ctrl
= ctrl
;
511 mc
->generic
.privdata
= NULL
;
512 lbounds
.left
= curstate
->pos
.h
;
513 lbounds
.top
= curstate
->pos
.v
;
514 if (ctrl
->editbox
.percentwidth
== 100) {
515 if (ctrl
->editbox
.label
!= NULL
) {
516 lbounds
.right
= lbounds
.left
+ curstate
->width
;
517 lbounds
.bottom
= lbounds
.top
+ 16;
518 curstate
->pos
.v
+= 18;
520 bounds
.left
= curstate
->pos
.h
;
521 bounds
.right
= bounds
.left
+ curstate
->width
;
523 lbounds
.right
= lbounds
.left
+
524 curstate
->width
* (100 - ctrl
->editbox
.percentwidth
) / 100;
525 lbounds
.bottom
= lbounds
.top
+ 22;
526 bounds
.left
= lbounds
.right
;
527 bounds
.right
= lbounds
.left
+ curstate
->width
;
529 bounds
.top
= curstate
->pos
.v
;
530 bounds
.bottom
= bounds
.top
+ 22;
531 if (mac_gestalts
.apprvers
>= 0x100) {
532 if (ctrl
->editbox
.label
== NULL
)
533 mc
->editbox
.tblabel
= NULL
;
535 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
536 0, 0, 0, kControlStaticTextProc
,
538 SetControlData(mc
->editbox
.tblabel
, kControlEntireControl
,
539 kControlStaticTextTextTag
,
540 strlen(ctrl
->editbox
.label
), ctrl
->editbox
.label
);
542 InsetRect(&bounds
, 3, 3);
543 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
544 ctrl
->editbox
.password ?
545 kControlEditTextPasswordProc
:
546 kControlEditTextProc
, (long)mc
);
548 #if !TARGET_API_MAC_CARBON
550 if (ctrl
->editbox
.label
== NULL
)
551 mc
->editbox
.tblabel
= NULL
;
553 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
554 0, 0, 0, SYS7_TEXT_PROC
,
556 TESetText(ctrl
->editbox
.label
, strlen(ctrl
->editbox
.label
),
557 (TEHandle
)(*mc
->editbox
.tblabel
)->contrlData
);
559 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
560 SYS7_EDITBOX_PROC
, (long)mc
);
563 curstate
->pos
.v
+= 28;
564 add234(mcs
->byctrl
, mc
);
565 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
566 mcs
->panels
[curstate
->panelnum
] = mc
;
567 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
570 #if !TARGET_API_MAC_CARBON
571 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16 variant
,
573 ControlDefProcMessage msg
,
584 rect
= (*control
)->contrlRect
;
585 if (variant
== SYS7_EDITBOX_VARIANT
)
586 InsetRect(&rect
, 3, 3); /* 2 if it's 20 pixels high */
587 te
= TENew(&rect
, &rect
);
588 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
589 (*te
)->txSize
= LoWord(ssfs
);
590 (*te
)->txFont
= HiWord(ssfs
);
591 (*control
)->contrlData
= (Handle
)te
;
594 TEDispose((TEHandle
)(*control
)->contrlData
);
597 if ((*control
)->contrlVis
) {
598 rect
= (*control
)->contrlRect
;
599 if (variant
== SYS7_EDITBOX_VARIANT
) {
602 InsetRect(&rect
, 3, 3);
604 (*(TEHandle
)(*control
)->contrlData
)->viewRect
= rect
;
605 TEUpdate(&rect
, (TEHandle
)(*control
)->contrlData
);
609 if (variant
== SYS7_TEXT_VARIANT
)
610 return kControlNoPart
;
611 mouse
.h
= LoWord(param
);
612 mouse
.v
= HiWord(param
);
613 rect
= (*control
)->contrlRect
;
614 InsetRect(&rect
, 3, 3);
615 return PtInRect(mouse
, &rect
) ? kControlEditTextPart
: kControlNoPart
;
617 if (param
& (1 << 31)) {
623 rgn
= (RgnHandle
)param
;
624 RectRgn(rgn
, &(*control
)->contrlRect
);
628 rgn
= (RgnHandle
)param
;
637 static void macctrl_radio(struct macctrls
*mcs
, WindowPtr window
,
638 struct mac_layoutstate
*curstate
,
641 union macctrl
*mc
= snew(union macctrl
);
644 unsigned int i
, colwidth
;
646 if (ctrl
->radio
.label
!= NULL
)
647 fprintf(stderr
, " label = %s\n", ctrl
->radio
.label
);
648 mc
->generic
.type
= MACCTRL_RADIO
;
649 mc
->generic
.ctrl
= ctrl
;
650 mc
->generic
.privdata
= NULL
;
651 mc
->radio
.tbctrls
= snewn(ctrl
->radio
.nbuttons
, ControlRef
);
652 colwidth
= (curstate
->width
+ 13) / ctrl
->radio
.ncolumns
;
653 bounds
.top
= curstate
->pos
.v
;
654 bounds
.bottom
= bounds
.top
+ 16;
655 bounds
.left
= curstate
->pos
.h
;
656 bounds
.right
= bounds
.left
+ curstate
->width
;
657 if (ctrl
->radio
.label
== NULL
)
658 mc
->radio
.tblabel
= NULL
;
660 if (mac_gestalts
.apprvers
>= 0x100) {
661 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
662 0, 0, 0, kControlStaticTextProc
,
664 SetControlData(mc
->radio
.tblabel
, kControlEntireControl
,
665 kControlStaticTextTextTag
,
666 strlen(ctrl
->radio
.label
), ctrl
->radio
.label
);
668 #if !TARGET_API_MAC_CARBON
670 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
671 0, 0, 0, SYS7_TEXT_PROC
, (long)mc
);
672 TESetText(ctrl
->radio
.label
, strlen(ctrl
->radio
.label
),
673 (TEHandle
)(*mc
->radio
.tblabel
)->contrlData
);
676 curstate
->pos
.v
+= 18;
678 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
679 fprintf(stderr
, " button = %s\n", ctrl
->radio
.buttons
[i
]);
680 bounds
.top
= curstate
->pos
.v
- 2;
681 bounds
.bottom
= bounds
.top
+ 18;
682 bounds
.left
= curstate
->pos
.h
+ colwidth
* (i
% ctrl
->radio
.ncolumns
);
683 if (i
== ctrl
->radio
.nbuttons
- 1 ||
684 i
% ctrl
->radio
.ncolumns
== ctrl
->radio
.ncolumns
- 1) {
685 bounds
.right
= curstate
->pos
.h
+ curstate
->width
;
686 curstate
->pos
.v
+= 18;
688 bounds
.right
= bounds
.left
+ colwidth
- 13;
689 c2pstrcpy(title
, ctrl
->radio
.buttons
[i
]);
690 mc
->radio
.tbctrls
[i
] = NewControl(window
, &bounds
, title
, TRUE
,
691 0, 0, 1, radioButProc
, (long)mc
);
693 curstate
->pos
.v
+= 4;
694 add234(mcs
->byctrl
, mc
);
695 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
696 mcs
->panels
[curstate
->panelnum
] = mc
;
697 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
700 static void macctrl_checkbox(struct macctrls
*mcs
, WindowPtr window
,
701 struct mac_layoutstate
*curstate
,
704 union macctrl
*mc
= snew(union macctrl
);
708 assert(ctrl
->checkbox
.label
!= NULL
);
709 fprintf(stderr
, " label = %s\n", ctrl
->checkbox
.label
);
710 mc
->generic
.type
= MACCTRL_CHECKBOX
;
711 mc
->generic
.ctrl
= ctrl
;
712 mc
->generic
.privdata
= NULL
;
713 bounds
.left
= curstate
->pos
.h
;
714 bounds
.right
= bounds
.left
+ curstate
->width
;
715 bounds
.top
= curstate
->pos
.v
;
716 bounds
.bottom
= bounds
.top
+ 16;
717 c2pstrcpy(title
, ctrl
->checkbox
.label
);
718 mc
->checkbox
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
719 checkBoxProc
, (long)mc
);
720 add234(mcs
->byctrl
, mc
);
721 curstate
->pos
.v
+= 22;
722 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
723 mcs
->panels
[curstate
->panelnum
] = mc
;
724 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
727 static void macctrl_button(struct macctrls
*mcs
, WindowPtr window
,
728 struct mac_layoutstate
*curstate
,
731 union macctrl
*mc
= snew(union macctrl
);
735 assert(ctrl
->button
.label
!= NULL
);
736 fprintf(stderr
, " label = %s\n", ctrl
->button
.label
);
737 if (ctrl
->button
.isdefault
)
738 fprintf(stderr
, " is default\n");
739 mc
->generic
.type
= MACCTRL_BUTTON
;
740 mc
->generic
.ctrl
= ctrl
;
741 mc
->generic
.privdata
= NULL
;
742 bounds
.left
= curstate
->pos
.h
;
743 bounds
.right
= bounds
.left
+ curstate
->width
;
744 bounds
.top
= curstate
->pos
.v
;
745 bounds
.bottom
= bounds
.top
+ 20;
746 c2pstrcpy(title
, ctrl
->button
.label
);
747 mc
->button
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
748 pushButProc
, (long)mc
);
749 mc
->button
.tbring
= NULL
;
750 if (mac_gestalts
.apprvers
>= 0x100) {
751 Boolean isdefault
= ctrl
->button
.isdefault
;
753 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
754 kControlPushButtonDefaultTag
,
755 sizeof(isdefault
), &isdefault
);
756 } else if (ctrl
->button
.isdefault
) {
757 InsetRect(&bounds
, -4, -4);
758 mc
->button
.tbring
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
759 SYS7_DEFAULT_PROC
, (long)mc
);
761 if (mac_gestalts
.apprvers
>= 0x110) {
762 Boolean iscancel
= ctrl
->button
.iscancel
;
764 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
765 kControlPushButtonCancelTag
,
766 sizeof(iscancel
), &iscancel
);
768 if (ctrl
->button
.isdefault
)
770 if (ctrl
->button
.iscancel
)
772 add234(mcs
->byctrl
, mc
);
773 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
774 mcs
->panels
[curstate
->panelnum
] = mc
;
775 curstate
->pos
.v
+= 26;
778 #if !TARGET_API_MAC_CARBON
779 static pascal SInt32
macctrl_sys7_default_cdef(SInt16 variant
,
781 ControlDefProcMessage msg
,
791 if ((*control
)->contrlVis
) {
792 rect
= (*control
)->contrlRect
;
793 GetPenState(&savestate
);
796 if ((*control
)->contrlHilite
== kControlInactivePart
)
798 oval
= (rect
.bottom
- rect
.top
) / 2 + 2;
799 FrameRoundRect(&rect
, oval
, oval
);
800 SetPenState(&savestate
);
804 if (param
& (1 << 31)) {
810 rgn
= (RgnHandle
)param
;
811 RectRgn(rgn
, &(*control
)->contrlRect
);
815 rgn
= (RgnHandle
)param
;
824 static void macctrl_listbox(struct macctrls
*mcs
, WindowPtr window
,
825 struct mac_layoutstate
*curstate
,
828 union macctrl
*mc
= snew(union macctrl
);
832 if (ctrl
->listbox
.label
!= NULL
)
833 fprintf(stderr
, " label = %s\n", ctrl
->listbox
.label
);
834 fprintf(stderr
, " height = %d\n", ctrl
->listbox
.height
);
835 if (ctrl
->listbox
.draglist
)
836 fprintf(stderr
, " draglist\n");
837 if (ctrl
->listbox
.multisel
)
838 fprintf(stderr
, " multisel\n");
839 fprintf(stderr
, " ncols = %d\n", ctrl
->listbox
.ncols
);
840 assert(ctrl
->listbox
.percentwidth
== 100);
841 mc
->generic
.type
= MACCTRL_LISTBOX
;
842 mc
->generic
.ctrl
= ctrl
;
843 mc
->generic
.privdata
= NULL
;
844 /* The list starts off empty */
845 mc
->listbox
.nids
= 0;
846 mc
->listbox
.ids
= NULL
;
847 bounds
.left
= curstate
->pos
.h
;
848 bounds
.right
= bounds
.left
+ curstate
->width
;
849 bounds
.top
= curstate
->pos
.v
;
850 bounds
.bottom
= bounds
.top
+ 20 * ctrl
->listbox
.height
;
852 if (mac_gestalts
.apprvers
>= 0x100) {
853 InsetRect(&bounds
, 3, 3);
854 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
856 kControlListBoxProc
, (long)mc
);
857 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
858 kControlListBoxListHandleTag
,
859 sizeof(mc
->listbox
.list
), &mc
->listbox
.list
,
861 DisposeControl(mc
->listbox
.tbctrl
);
866 #if !TARGET_API_MAC_CARBON
868 InsetRect(&bounds
, -3, -3);
869 mc
->listbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
871 SYS7_LISTBOX_PROC
, (long)mc
);
872 mc
->listbox
.list
= (ListHandle
)(*mc
->listbox
.tbctrl
)->contrlData
;
873 (*mc
->listbox
.list
)->refCon
= (long)mc
;
876 if (!ctrl
->listbox
.multisel
) {
877 #if TARGET_API_MAC_CARBON
878 SetListSelectionFlags(mc
->listbox
.list
, lOnlyOne
);
880 (*mc
->listbox
.list
)->selFlags
= lOnlyOne
;
883 add234(mcs
->byctrl
, mc
);
884 curstate
->pos
.v
+= 6 + 20 * ctrl
->listbox
.height
;
885 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
886 mcs
->panels
[curstate
->panelnum
] = mc
;
887 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
890 #if !TARGET_API_MAC_CARBON
891 static pascal SInt32
macctrl_sys7_listbox_cdef(SInt16 variant
,
893 ControlDefProcMessage msg
,
909 rect
= (*control
)->contrlRect
;
910 InsetRect(&rect
, 4, 4);
911 rect
.right
-= 15; /* scroll bar */
912 bounds
.top
= bounds
.bottom
= bounds
.left
= 0;
914 csize
.h
= csize
.v
= 0;
916 savefont
= curport
->txFont
;
917 savesize
= curport
->txSize
;
918 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
919 TextFont(HiWord(ssfs
));
920 TextSize(LoWord(ssfs
));
921 list
= LNew(&rect
, &bounds
, csize
, 0, (*control
)->contrlOwner
,
922 TRUE
, FALSE
, FALSE
, TRUE
);
923 SetControlReference((*list
)->vScroll
, (long)list
);
924 (*control
)->contrlData
= (Handle
)list
;
930 * If the dialogue box is being destroyed, the scroll bar
931 * might have gone already. In our situation, this is the
932 * only time we destroy a control, so NULL out the scroll bar
933 * handle to prevent LDispose trying to free it.
935 list
= (ListHandle
)(*control
)->contrlData
;
936 (*list
)->vScroll
= NULL
;
940 if ((*control
)->contrlVis
) {
941 rect
= (*control
)->contrlRect
;
942 /* XXX input focus highlighting? */
943 InsetRect(&rect
, 3, 3);
946 list
= (ListHandle
)(*control
)->contrlData
;
947 LActivate((*control
)->contrlHilite
!= kControlInactivePart
, list
);
949 LUpdate(curport
->visRgn
, list
);
953 mouse
.h
= LoWord(param
);
954 mouse
.v
= HiWord(param
);
955 rect
= (*control
)->contrlRect
;
956 InsetRect(&rect
, 4, 4);
958 * We deliberately exclude the scrollbar so that LClick() can see it.
961 return PtInRect(mouse
, &rect
) ? kControlListBoxPart
: kControlNoPart
;
963 if (param
& (1 << 31)) {
969 rgn
= (RgnHandle
)param
;
970 RectRgn(rgn
, &(*control
)->contrlRect
);
974 rgn
= (RgnHandle
)param
;
983 static void macctrl_popup(struct macctrls
*mcs
, WindowPtr window
,
984 struct mac_layoutstate
*curstate
,
987 union macctrl
*mc
= snew(union macctrl
);
990 unsigned int labelwidth
;
991 static int nextmenuid
= MENU_MIN
;
996 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
997 * create a popup menu with dynamic content.
999 assert(ctrl
->listbox
.height
== 0);
1000 assert(!ctrl
->listbox
.draglist
);
1001 assert(!ctrl
->listbox
.multisel
);
1003 if (ctrl
->listbox
.label
!= NULL
)
1004 fprintf(stderr
, " label = %s\n", ctrl
->listbox
.label
);
1005 fprintf(stderr
, " percentwidth = %d\n", ctrl
->listbox
.percentwidth
);
1007 mc
->generic
.type
= MACCTRL_POPUP
;
1008 mc
->generic
.ctrl
= ctrl
;
1009 mc
->generic
.privdata
= NULL
;
1010 c2pstrcpy(title
, ctrl
->button
.label
== NULL ?
"" : ctrl
->button
.label
);
1012 /* Find a spare menu ID and create the menu */
1013 while (GetMenuHandle(nextmenuid
) != NULL
)
1014 if (++nextmenuid
>= MENU_MAX
) nextmenuid
= MENU_MIN
;
1015 menuid
= nextmenuid
++;
1016 menu
= NewMenu(menuid
, "\pdummy");
1017 if (menu
== NULL
) return;
1018 mc
->popup
.menu
= menu
;
1019 mc
->popup
.menuid
= menuid
;
1020 InsertMenu(menu
, kInsertHierarchicalMenu
);
1022 /* The menu starts off empty */
1024 mc
->popup
.ids
= NULL
;
1026 bounds
.left
= curstate
->pos
.h
;
1027 bounds
.right
= bounds
.left
+ curstate
->width
;
1028 bounds
.top
= curstate
->pos
.v
;
1029 bounds
.bottom
= bounds
.top
+ 20;
1030 /* XXX handle percentwidth == 100 */
1031 labelwidth
= curstate
->width
* (100 - ctrl
->listbox
.percentwidth
) / 100;
1032 mc
->popup
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
,
1033 popupTitleLeftJust
, menuid
, labelwidth
,
1034 popupMenuProc
+ popupFixedWidth
, (long)mc
);
1035 add234(mcs
->byctrl
, mc
);
1036 curstate
->pos
.v
+= 26;
1037 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
1038 mcs
->panels
[curstate
->panelnum
] = mc
;
1039 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1043 void macctrl_activate(WindowPtr window
, EventRecord
*event
)
1045 struct macctrls
*mcs
= mac_winctrls(window
);
1046 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
1049 ControlPartCode state
;
1053 SetPort((GrafPtr
)GetWindowPort(window
));
1054 if (mac_gestalts
.apprvers
>= 0x100)
1055 SetThemeWindowBackground(window
, active ?
1056 kThemeBrushModelessDialogBackgroundActive
:
1057 kThemeBrushModelessDialogBackgroundInactive
,
1059 state
= active ? kControlNoPart
: kControlInactivePart
;
1060 for (i
= 0; i
<= mcs
->curpanel
; i
+= mcs
->curpanel
)
1061 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
1062 switch (mc
->generic
.type
) {
1064 HiliteControl(mc
->text
.tbctrl
, state
);
1066 case MACCTRL_EDITBOX
:
1067 HiliteControl(mc
->editbox
.tbctrl
, state
);
1068 if (mc
->editbox
.tblabel
!= NULL
)
1069 HiliteControl(mc
->editbox
.tblabel
, state
);
1072 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
1073 HiliteControl(mc
->radio
.tbctrls
[j
], state
);
1074 if (mc
->radio
.tblabel
!= NULL
)
1075 HiliteControl(mc
->radio
.tblabel
, state
);
1077 case MACCTRL_CHECKBOX
:
1078 HiliteControl(mc
->checkbox
.tbctrl
, state
);
1080 case MACCTRL_BUTTON
:
1081 HiliteControl(mc
->button
.tbctrl
, state
);
1082 if (mc
->button
.tbring
!= NULL
)
1083 HiliteControl(mc
->button
.tbring
, state
);
1085 case MACCTRL_LISTBOX
:
1086 HiliteControl(mc
->listbox
.tbctrl
, state
);
1089 HiliteControl(mc
->popup
.tbctrl
, state
);
1092 #if !TARGET_API_MAC_CARBON
1093 if (mcs
->focus
== mc
) {
1095 macctrl_enfocus(mc
);
1097 macctrl_defocus(mc
);
1104 void macctrl_click(WindowPtr window
, EventRecord
*event
)
1107 ControlHandle control
, oldfocus
;
1108 int part
, trackresult
;
1111 struct macctrls
*mcs
= mac_winctrls(window
);
1116 SetPort((GrafPtr
)GetWindowPort(window
));
1117 mouse
= event
->where
;
1118 GlobalToLocal(&mouse
);
1119 part
= FindControl(mouse
, window
, &control
);
1120 if (control
!= NULL
) {
1121 #if !TARGET_API_MAC_CARBON
1123 * Special magic for scroll bars in list boxes, whose refcon
1126 if (part
== kControlUpButtonPart
|| part
== kControlDownButtonPart
||
1127 part
== kControlPageUpPart
|| part
== kControlPageDownPart
||
1128 part
== kControlIndicatorPart
)
1129 mc
= (union macctrl
*)
1130 (*(ListHandle
)GetControlReference(control
))->refCon
;
1133 mc
= (union macctrl
*)GetControlReference(control
);
1134 fprintf(stderr
, "control = %p, part = %d, mc = %p\n",
1136 if (mac_gestalts
.apprvers
>= 0x100) {
1137 if (GetControlFeatures(control
, &features
) == noErr
&&
1138 (features
& kControlSupportsFocus
) &&
1139 (features
& kControlGetsFocusOnClick
) &&
1140 GetKeyboardFocus(window
, &oldfocus
) == noErr
&&
1141 control
!= oldfocus
)
1142 SetKeyboardFocus(window
, control
, part
);
1143 trackresult
= HandleControlClick(control
, mouse
, event
->modifiers
,
1144 (ControlActionUPP
)-1);
1146 #if !TARGET_API_MAC_CARBON
1147 if (mc
->generic
.type
== MACCTRL_EDITBOX
&&
1148 control
== mc
->editbox
.tbctrl
) {
1149 TEHandle te
= (TEHandle
)(*control
)->contrlData
;
1151 macctrl_setfocus(mcs
, mc
);
1152 TEClick(mouse
, !!(event
->modifiers
& shiftKey
), te
);
1155 if (mc
->generic
.type
== MACCTRL_LISTBOX
&&
1156 (control
== mc
->listbox
.tbctrl
||
1157 control
== (*mc
->listbox
.list
)->vScroll
)) {
1159 fprintf(stderr
, "list = %p\n", mc
->listbox
.list
);
1160 macctrl_setfocus(mcs
, mc
);
1161 if (LClick(mouse
, event
->modifiers
, mc
->listbox
.list
))
1163 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1165 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1169 trackresult
= TrackControl(control
, mouse
, (ControlActionUPP
)-1);
1171 switch (mc
->generic
.type
) {
1173 if (trackresult
!= 0) {
1174 for (i
= 0; i
< mc
->generic
.ctrl
->radio
.nbuttons
; i
++)
1175 if (mc
->radio
.tbctrls
[i
] == control
)
1176 SetControlValue(mc
->radio
.tbctrls
[i
],
1177 kControlRadioButtonCheckedValue
);
1179 SetControlValue(mc
->radio
.tbctrls
[i
],
1180 kControlRadioButtonUncheckedValue
);
1181 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1184 case MACCTRL_CHECKBOX
:
1185 if (trackresult
!= 0) {
1186 SetControlValue(control
, !GetControlValue(control
));
1187 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1190 case MACCTRL_BUTTON
:
1191 if (trackresult
!= 0)
1192 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1194 case MACCTRL_LISTBOX
:
1195 /* FIXME spot double-click */
1196 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1199 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1207 void macctrl_key(WindowPtr window
, EventRecord
*event
)
1210 struct macctrls
*mcs
= mac_winctrls(window
);
1212 unsigned long dummy
;
1214 switch (event
->message
& charCodeMask
) {
1215 case kEnterCharCode
:
1216 case kReturnCharCode
:
1217 if (mcs
->defbutton
!= NULL
) {
1218 assert(mcs
->defbutton
->generic
.type
== MACCTRL_BUTTON
);
1219 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlButtonPart
);
1221 * I'd like to delay unhilighting the button until after
1222 * the event has been processed, but by them the entire
1223 * dialgue box might have been destroyed.
1226 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1227 ctrlevent(mcs
, mcs
->defbutton
, EVENT_ACTION
);
1230 case kEscapeCharCode
:
1231 if (mcs
->canbutton
!= NULL
) {
1232 assert(mcs
->canbutton
->generic
.type
== MACCTRL_BUTTON
);
1233 HiliteControl(mcs
->canbutton
->button
.tbctrl
, kControlButtonPart
);
1235 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1236 ctrlevent(mcs
, mcs
->canbutton
, EVENT_ACTION
);
1240 if (mac_gestalts
.apprvers
>= 0x100) {
1241 if (GetKeyboardFocus(window
, &control
) == noErr
&& control
!= NULL
) {
1242 HandleControlKey(control
, (event
->message
& keyCodeMask
) >> 8,
1243 event
->message
& charCodeMask
, event
->modifiers
);
1244 mc
= (union macctrl
*)GetControlReference(control
);
1245 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1248 #if !TARGET_API_MAC_CARBON
1252 if (mcs
->focus
!= NULL
) {
1254 switch (mc
->generic
.type
) {
1255 case MACCTRL_EDITBOX
:
1256 te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1257 TEKey(event
->message
& charCodeMask
, te
);
1258 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1266 void macctrl_update(WindowPtr window
)
1268 #if TARGET_API_MAC_CARBON
1274 BeginUpdate(window
);
1276 SetPort((GrafPtr
)GetWindowPort(window
));
1277 if (mac_gestalts
.apprvers
>= 0x101) {
1278 #if TARGET_API_MAC_CARBON
1279 GetPortBounds(GetWindowPort(window
), &rect
);
1281 rect
= window
->portRect
;
1283 InsetRect(&rect
, -1, -1);
1284 DrawThemeModelessDialogFrame(&rect
, mac_frontwindow() == window ?
1285 kThemeStateActive
: kThemeStateInactive
);
1287 #if TARGET_API_MAC_CARBON
1289 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1290 UpdateControls(window
, visrgn
);
1293 UpdateControls(window
, window
->visRgn
);
1299 #if TARGET_API_MAC_CARBON
1300 #define EnableItem EnableMenuItem
1301 #define DisableItem DisableMenuItem
1303 void macctrl_adjustmenus(WindowPtr window
)
1307 menu
= GetMenuHandle(mFile
);
1308 DisableItem(menu
, iSave
); /* XXX enable if modified */
1309 EnableItem(menu
, iSaveAs
);
1310 EnableItem(menu
, iDuplicate
);
1312 menu
= GetMenuHandle(mEdit
);
1313 DisableItem(menu
, 0);
1316 void macctrl_close(WindowPtr window
)
1318 struct macctrls
*mcs
= mac_winctrls(window
);
1322 * Mostly, we don't bother disposing of the Toolbox controls,
1323 * since that will happen automatically when the window is
1324 * disposed of. Popup menus are an exception, because we have to
1325 * dispose of the menu ourselves, and doing that while the control
1326 * still holds a reference to it seems rude.
1328 while ((mc
= index234(mcs
->byctrl
, 0)) != NULL
) {
1329 if (mc
->generic
.privdata
!= NULL
&& mc
->generic
.freeprivdata
)
1330 sfree(mc
->generic
.privdata
);
1331 switch (mc
->generic
.type
) {
1333 DisposeControl(mc
->popup
.tbctrl
);
1334 DeleteMenu(mc
->popup
.menuid
);
1335 DisposeMenu(mc
->popup
.menu
);
1338 del234(mcs
->byctrl
, mc
);
1342 freetree234(mcs
->byctrl
);
1348 void dlg_update_start(union control
*ctrl
, void *dlg
)
1354 void dlg_update_done(union control
*ctrl
, void *dlg
)
1360 void dlg_set_focus(union control
*ctrl
, void *dlg
)
1363 if (mac_gestalts
.apprvers
>= 0x100) {
1364 /* Use SetKeyboardFocus() */
1366 /* Do our own mucking around */
1370 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1376 void dlg_beep(void *dlg
)
1382 void dlg_error_msg(void *dlg
, char *msg
)
1386 c2pstrcpy(pmsg
, msg
);
1387 ParamText(pmsg
, NULL
, NULL
, NULL
);
1388 StopAlert(128, NULL
);
1391 void dlg_end(void *dlg
, int value
)
1393 struct macctrls
*mcs
= dlg
;
1395 if (mcs
->end
!= NULL
)
1396 (*mcs
->end
)(mcs
->window
, value
);
1399 void dlg_refresh(union control
*ctrl
, void *dlg
)
1401 struct macctrls
*mcs
= dlg
;
1406 mc
= findbyctrl(mcs
, ctrl
);
1408 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1411 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
1413 struct macctrls
*mcs
= dlg
;
1414 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1417 return mc
->generic
.privdata
;
1420 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
1422 struct macctrls
*mcs
= dlg
;
1423 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1426 mc
->generic
.privdata
= ptr
;
1427 mc
->generic
.freeprivdata
= FALSE
;
1430 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
1432 struct macctrls
*mcs
= dlg
;
1433 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1436 mc
->generic
.privdata
= smalloc(size
);
1437 mc
->generic
.freeprivdata
= TRUE
;
1438 return mc
->generic
.privdata
;
1443 * Radio Button control
1446 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
1448 struct macctrls
*mcs
= dlg
;
1449 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1452 if (mc
== NULL
) return;
1453 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1454 if (i
== whichbutton
)
1455 SetControlValue(mc
->radio
.tbctrls
[i
],
1456 kControlRadioButtonCheckedValue
);
1458 SetControlValue(mc
->radio
.tbctrls
[i
],
1459 kControlRadioButtonUncheckedValue
);
1464 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
1466 struct macctrls
*mcs
= dlg
;
1467 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1471 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1472 if (GetControlValue(mc
->radio
.tbctrls
[i
]) ==
1473 kControlRadioButtonCheckedValue
)
1484 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
1486 struct macctrls
*mcs
= dlg
;
1487 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1489 if (mc
== NULL
) return;
1490 SetControlValue(mc
->checkbox
.tbctrl
,
1491 checked ? kControlCheckBoxCheckedValue
:
1492 kControlCheckBoxUncheckedValue
);
1495 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
1497 struct macctrls
*mcs
= dlg
;
1498 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1501 return GetControlValue(mc
->checkbox
.tbctrl
);
1509 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
1511 struct macctrls
*mcs
= dlg
;
1512 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1515 if (mc
== NULL
) return;
1516 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1518 SetPort((GrafPtr
)(GetWindowPort(mcs
->window
)));
1519 if (mac_gestalts
.apprvers
>= 0x100)
1520 SetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1521 ctrl
->editbox
.password ?
1522 kControlEditTextPasswordTag
:
1523 kControlEditTextTextTag
,
1524 strlen(text
), text
);
1525 #if !TARGET_API_MAC_CARBON
1527 TESetText(text
, strlen(text
),
1528 (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
1530 DrawOneControl(mc
->editbox
.tbctrl
);
1534 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
1536 struct macctrls
*mcs
= dlg
;
1537 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1541 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1542 if (mac_gestalts
.apprvers
>= 0x100) {
1543 if (GetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1544 ctrl
->editbox
.password ?
1545 kControlEditTextPasswordTag
:
1546 kControlEditTextTextTag
,
1547 length
- 1, buffer
, &olen
) != noErr
)
1549 if (olen
> length
- 1)
1552 #if !TARGET_API_MAC_CARBON
1554 TEHandle te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1556 olen
= (*te
)->teLength
;
1557 if (olen
> length
- 1)
1559 memcpy(buffer
, *(*te
)->hText
, olen
);
1562 buffer
[olen
] = '\0';
1563 fprintf(stderr
, "dlg_editbox_get: %s\n", buffer
);
1571 static void dlg_macpopup_clear(union control
*ctrl
, void *dlg
)
1573 struct macctrls
*mcs
= dlg
;
1574 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1575 MenuRef menu
= mc
->popup
.menu
;
1578 if (mc
== NULL
) return;
1579 fprintf(stderr
, " popup_clear\n");
1580 n
= CountMenuItems(menu
);
1581 for (i
= 0; i
< n
; i
++)
1582 DeleteMenuItem(menu
, n
- i
);
1584 sfree(mc
->popup
.ids
);
1585 mc
->popup
.ids
= NULL
;
1586 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1589 static void dlg_maclist_clear(union control
*ctrl
, void *dlg
)
1591 struct macctrls
*mcs
= dlg
;
1592 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1594 if (mc
== NULL
) return;
1595 fprintf(stderr
, " maclist_clear\n");
1596 LDelRow(0, 0, mc
->listbox
.list
);
1597 mc
->listbox
.nids
= 0;
1598 sfree(mc
->listbox
.ids
);
1599 mc
->listbox
.ids
= NULL
;
1600 DrawOneControl(mc
->listbox
.tbctrl
);
1603 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
1606 switch (ctrl
->generic
.type
) {
1608 if (ctrl
->listbox
.height
== 0)
1609 dlg_macpopup_clear(ctrl
, dlg
);
1611 dlg_maclist_clear(ctrl
, dlg
);
1616 static void dlg_macpopup_del(union control
*ctrl
, void *dlg
, int index
)
1618 struct macctrls
*mcs
= dlg
;
1619 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1620 MenuRef menu
= mc
->popup
.menu
;
1622 if (mc
== NULL
) return;
1623 fprintf(stderr
, " popup_del %d\n", index
);
1624 DeleteMenuItem(menu
, index
+ 1);
1625 if (mc
->popup
.ids
!= NULL
)
1626 memcpy(mc
->popup
.ids
+ index
, mc
->popup
.ids
+ index
+ 1,
1627 (mc
->popup
.nids
- index
- 1) * sizeof(*mc
->popup
.ids
));
1628 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1631 static void dlg_maclist_del(union control
*ctrl
, void *dlg
, int index
)
1633 struct macctrls
*mcs
= dlg
;
1634 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1636 if (mc
== NULL
) return;
1637 fprintf(stderr
, " maclist_del %d\n", index
);
1638 LDelRow(1, index
, mc
->listbox
.list
);
1639 if (mc
->listbox
.ids
!= NULL
)
1640 memcpy(mc
->listbox
.ids
+ index
, mc
->listbox
.ids
+ index
+ 1,
1641 (mc
->listbox
.nids
- index
- 1) * sizeof(*mc
->listbox
.ids
));
1642 DrawOneControl(mc
->listbox
.tbctrl
);
1645 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
1648 switch (ctrl
->generic
.type
) {
1650 if (ctrl
->listbox
.height
== 0)
1651 dlg_macpopup_del(ctrl
, dlg
, index
);
1653 dlg_maclist_del(ctrl
, dlg
, index
);
1658 static void dlg_macpopup_add(union control
*ctrl
, void *dlg
, char const *text
)
1660 struct macctrls
*mcs
= dlg
;
1661 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1662 MenuRef menu
= mc
->popup
.menu
;
1665 if (mc
== NULL
) return;
1666 fprintf(stderr
, " popup_add %s\n", text
);
1667 assert(text
[0] != '\0');
1668 c2pstrcpy(itemstring
, text
);
1669 AppendMenu(menu
, "\pdummy");
1670 SetMenuItemText(menu
, CountMenuItems(menu
), itemstring
);
1671 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1675 static void dlg_maclist_add(union control
*ctrl
, void *dlg
, char const *text
)
1677 struct macctrls
*mcs
= dlg
;
1678 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1680 Cell cell
= { 0, 0 };
1682 if (mc
== NULL
) return;
1683 fprintf(stderr
, " maclist_add %s\n", text
);
1684 #if TARGET_API_MAC_CARBON
1685 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1687 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1689 cell
.v
= bounds
.bottom
;
1690 LAddRow(1, cell
.v
, mc
->listbox
.list
);
1691 LSetCell(text
, strlen(text
), cell
, mc
->listbox
.list
);
1692 DrawOneControl(mc
->listbox
.tbctrl
);
1695 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
1698 switch (ctrl
->generic
.type
) {
1700 if (ctrl
->listbox
.height
== 0)
1701 dlg_macpopup_add(ctrl
, dlg
, text
);
1703 dlg_maclist_add(ctrl
, dlg
, text
);
1708 static void dlg_macpopup_addwithid(union control
*ctrl
, void *dlg
,
1709 char const *text
, int id
)
1711 struct macctrls
*mcs
= dlg
;
1712 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1713 MenuRef menu
= mc
->popup
.menu
;
1716 if (mc
== NULL
) return;
1717 fprintf(stderr
, " popup_addwthid %s, %d\n", text
, id
);
1718 dlg_macpopup_add(ctrl
, dlg
, text
);
1719 index
= CountMenuItems(menu
) - 1;
1720 if (mc
->popup
.nids
<= index
) {
1721 mc
->popup
.nids
= index
+ 1;
1722 mc
->popup
.ids
= sresize(mc
->popup
.ids
, mc
->popup
.nids
, int);
1724 mc
->popup
.ids
[index
] = id
;
1727 static void dlg_maclist_addwithid(union control
*ctrl
, void *dlg
,
1728 char const *text
, int id
)
1730 struct macctrls
*mcs
= dlg
;
1731 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1735 if (mc
== NULL
) return;
1736 fprintf(stderr
, " maclist_addwithid %s %d\n", text
, id
);
1737 dlg_maclist_add(ctrl
, dlg
, text
);
1738 #if TARGET_API_MAC_CARBON
1739 GetListDataBounds(mc
->listbox
.list
, &bounds
);
1741 bounds
= (*mc
->listbox
.list
)->dataBounds
;
1743 index
= bounds
.bottom
;
1744 if (mc
->listbox
.nids
<= index
) {
1745 mc
->listbox
.nids
= index
+ 1;
1746 mc
->listbox
.ids
= sresize(mc
->listbox
.ids
, mc
->listbox
.nids
, int);
1748 mc
->listbox
.ids
[index
] = id
;
1751 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
1752 char const *text
, int id
)
1755 switch (ctrl
->generic
.type
) {
1757 if (ctrl
->listbox
.height
== 0)
1758 dlg_macpopup_addwithid(ctrl
, dlg
, text
, id
);
1760 dlg_maclist_addwithid(ctrl
, dlg
, text
, id
);
1765 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
1767 struct macctrls
*mcs
= dlg
;
1768 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1771 switch (ctrl
->generic
.type
) {
1773 if (ctrl
->listbox
.height
== 0) {
1774 assert(mc
->popup
.ids
!= NULL
&& mc
->popup
.nids
> index
);
1775 return mc
->popup
.ids
[index
];
1777 assert(mc
->listbox
.ids
!= NULL
&& mc
->listbox
.nids
> index
);
1778 return mc
->listbox
.ids
[index
];
1784 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
1786 struct macctrls
*mcs
= dlg
;
1787 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1788 Cell cell
= { 0, 0 };
1791 switch (ctrl
->generic
.type
) {
1793 if (ctrl
->listbox
.height
== 0)
1794 return GetControlValue(mc
->popup
.tbctrl
) - 1;
1796 if (LGetSelect(TRUE
, &cell
, mc
->listbox
.list
))
1805 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
1807 struct macctrls
*mcs
= dlg
;
1808 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1809 Cell cell
= { 0, 0 };
1812 switch (ctrl
->generic
.type
) {
1814 if (ctrl
->listbox
.height
== 0)
1815 return GetControlValue(mc
->popup
.tbctrl
) - 1 == index
;
1818 return LGetSelect(FALSE
, &cell
, mc
->listbox
.list
);
1824 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
1826 struct macctrls
*mcs
= dlg
;
1827 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1829 if (mc
== NULL
) return;
1830 switch (ctrl
->generic
.type
) {
1832 if (ctrl
->listbox
.height
== 0)
1833 SetControlValue(mc
->popup
.tbctrl
, index
+ 1);
1843 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
1845 struct macctrls
*mcs
= dlg
;
1846 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1848 if (mc
== NULL
) return;
1849 if (mac_gestalts
.apprvers
>= 0x100)
1850 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
1851 kControlStaticTextTextTag
, strlen(text
), text
);
1852 #if !TARGET_API_MAC_CARBON
1854 TESetText(text
, strlen(text
),
1855 (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
);
1861 * File Selector control
1864 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
1869 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
1876 * Font Selector control
1879 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fn
)
1884 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fn
)
1891 * Printer enumeration
1894 printer_enum
*printer_start_enum(int *nprinters
)
1901 char *printer_get_name(printer_enum
*pe
, int thing
)
1907 void printer_finish_enum(printer_enum
*pe
)
1914 * Colour selection stuff
1917 void dlg_coloursel_start(union control
*ctrl
, void *dlg
,
1918 int r
, int g
, int b
)
1923 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1924 int *r
, int *g
, int *b
)
1932 * c-file-style: "simon"