1 /* $Id: macctrls.c,v 1.31 2003/04/06 13:27:40 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>
34 #include <Resources.h>
38 #include <TextUtils.h>
39 #include <ToolUtils.h>
51 /* Range of menu IDs for popup menus */
57 struct macctrl_generic
{
67 /* Template from which this was generated */
69 /* Next control in this panel */
75 struct macctrl_generic generic
;
79 struct macctrl_generic generic
;
84 struct macctrl_generic generic
;
89 struct macctrl_generic generic
;
93 struct macctrl_generic generic
;
98 struct macctrl_generic generic
;
104 struct macctrl_generic generic
;
113 struct mac_layoutstate
{
116 unsigned int panelnum
;
119 #define ctrlevent(mcs, mc, event) do { \
120 if ((mc)->generic.ctrl->generic.handler != NULL) \
121 (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\
122 (mcs)->data, (event)); \
125 #define findbyctrl(mcs, ctrl) \
126 find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find)
128 static void macctrl_layoutset(struct mac_layoutstate
*, struct controlset
*,
129 WindowPtr
, struct macctrls
*);
130 static void macctrl_switchtopanel(struct macctrls
*, unsigned int);
131 static void macctrl_setfocus(struct macctrls
*, union macctrl
*);
132 static void macctrl_text(struct macctrls
*, WindowPtr
,
133 struct mac_layoutstate
*, union control
*);
134 static void macctrl_editbox(struct macctrls
*, WindowPtr
,
135 struct mac_layoutstate
*, union control
*);
136 static void macctrl_radio(struct macctrls
*, WindowPtr
,
137 struct mac_layoutstate
*, union control
*);
138 static void macctrl_checkbox(struct macctrls
*, WindowPtr
,
139 struct mac_layoutstate
*, union control
*);
140 static void macctrl_button(struct macctrls
*, WindowPtr
,
141 struct mac_layoutstate
*, union control
*);
142 static void macctrl_listbox(struct macctrls
*, WindowPtr
,
143 struct mac_layoutstate
*, union control
*);
144 static void macctrl_popup(struct macctrls
*, WindowPtr
,
145 struct mac_layoutstate
*, union control
*);
146 #if !TARGET_API_MAC_CARBON
147 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16
, ControlRef
,
148 ControlDefProcMessage
, SInt32
);
149 static pascal SInt32
macctrl_sys7_default_cdef(SInt16
, ControlRef
,
150 ControlDefProcMessage
, SInt32
);
153 #if !TARGET_API_MAC_CARBON
155 * This trick enables us to keep all the CDEF code in the main
156 * application, which makes life easier. For details, see
157 * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>.
160 #pragma options align=mac68k
162 short jmpabs
; /* 4EF9 */
163 ControlDefUPP theUPP
;
165 #pragma options align=reset
168 static void macctrl_init()
170 #if !TARGET_API_MAC_CARBON
171 static int inited
= 0;
175 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_EditBox
);
176 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_editbox_cdef
);
177 cdef
= (PatchCDEF
)GetResource(kControlDefProcResourceType
, CDEF_Default
);
178 (*cdef
)->theUPP
= NewControlDefProc(macctrl_sys7_default_cdef
);
184 static int macctrl_cmp_byctrl(void *av
, void *bv
)
186 union macctrl
*a
= (union macctrl
*)av
;
187 union macctrl
*b
= (union macctrl
*)bv
;
189 if (a
->generic
.ctrl
< b
->generic
.ctrl
)
191 else if (a
->generic
.ctrl
> b
->generic
.ctrl
)
197 static int macctrl_cmp_byctrl_find(void *av
, void *bv
)
199 union control
*a
= (union control
*)av
;
200 union macctrl
*b
= (union macctrl
*)bv
;
202 if (a
< b
->generic
.ctrl
)
204 else if (a
> b
->generic
.ctrl
)
210 void macctrl_layoutbox(struct controlbox
*cb
, WindowPtr window
,
211 struct macctrls
*mcs
)
214 struct mac_layoutstate curstate
;
219 #if TARGET_API_MAC_CARBON
220 GetPortBounds(GetWindowPort(window
), &rect
);
222 rect
= window
->portRect
;
224 curstate
.pos
.h
= rect
.left
+ 13;
225 curstate
.pos
.v
= rect
.bottom
- 33;
226 curstate
.width
= rect
.right
- rect
.left
- (13 * 2);
227 if (mac_gestalts
.apprvers
>= 0x100)
228 CreateRootControl(window
, &root
);
229 mcs
->window
= window
;
230 mcs
->byctrl
= newtree234(macctrl_cmp_byctrl
);
232 mcs
->defbutton
= NULL
;
233 mcs
->canbutton
= NULL
;
234 /* Count the number of panels */
236 for (i
= 1; i
< cb
->nctrlsets
; i
++)
237 if (strcmp(cb
->ctrlsets
[i
]->pathname
, cb
->ctrlsets
[i
-1]->pathname
))
239 mcs
->panels
= snewn(mcs
->npanels
, union macctrl
*);
240 memset(mcs
->panels
, 0, sizeof(*mcs
->panels
) * mcs
->npanels
);
241 curstate
.panelnum
= 0;
242 for (i
= 0; i
< cb
->nctrlsets
; i
++) {
243 if (i
> 0 && strcmp(cb
->ctrlsets
[i
]->pathname
,
244 cb
->ctrlsets
[i
-1]->pathname
)) {
245 curstate
.pos
.v
= rect
.top
+ 13;
247 assert(curstate
.panelnum
< mcs
->npanels
);
249 macctrl_layoutset(&curstate
, cb
->ctrlsets
[i
], window
, mcs
);
251 macctrl_switchtopanel(mcs
, 19);
252 /* 14 = proxies, 20 = SSH bugs */
257 static void macctrl_layoutset(struct mac_layoutstate
*curstate
,
258 struct controlset
*s
,
259 WindowPtr window
, struct macctrls
*mcs
)
261 unsigned int i
, j
, ncols
, colstart
;
262 struct mac_layoutstate cols
[MAXCOLS
];
264 fprintf(stderr
, "--- begin set ---\n");
265 fprintf(stderr
, "pathname = %s\n", s
->pathname
);
266 if (s
->boxname
&& *s
->boxname
)
267 fprintf(stderr
, "boxname = %s\n", s
->boxname
);
269 fprintf(stderr
, "boxtitle = %s\n", s
->boxtitle
);
274 for (i
= 0; i
< s
->ncontrols
; i
++) {
275 union control
*ctrl
= s
->ctrls
[i
];
278 colstart
= COLUMN_START(ctrl
->generic
.column
);
279 switch (ctrl
->generic
.type
) {
280 case CTRL_TEXT
: s
= "text"; break;
281 case CTRL_EDITBOX
: s
= "editbox"; break;
282 case CTRL_RADIO
: s
= "radio"; break;
283 case CTRL_CHECKBOX
: s
= "checkbox"; break;
284 case CTRL_BUTTON
: s
= "button"; break;
285 case CTRL_LISTBOX
: s
= "listbox"; break;
286 case CTRL_COLUMNS
: s
= "columns"; break;
287 case CTRL_FILESELECT
: s
= "fileselect"; break;
288 case CTRL_FONTSELECT
: s
= "fontselect"; break;
289 case CTRL_TABDELAY
: s
= "tabdelay"; break;
290 default: s
= "unknown"; break;
292 fprintf(stderr
, " control: %s\n", s
);
293 switch (ctrl
->generic
.type
) {
295 if (ctrl
->columns
.ncols
!= 1) {
296 ncols
= ctrl
->columns
.ncols
;
297 fprintf(stderr
, " split to %d\n", ncols
);
298 assert(ncols
<= MAXCOLS
);
299 for (j
= 0; j
< ncols
; j
++) {
302 cols
[j
].pos
.h
= cols
[j
-1].pos
.h
+ cols
[j
-1].width
+ 6;
304 cols
[j
].width
= curstate
->width
-
305 (cols
[j
].pos
.h
- curstate
->pos
.h
);
307 cols
[j
].width
= (curstate
->width
+ 6) *
308 ctrl
->columns
.percentages
[j
] / 100 - 6;
311 fprintf(stderr
, " join\n");
312 for (j
= 0; j
< ncols
; j
++)
313 if (cols
[j
].pos
.v
> cols
[0].pos
.v
)
314 cols
[0].pos
.v
= cols
[j
].pos
.v
;
315 cols
[0].width
= curstate
->width
;
320 macctrl_text(mcs
, window
, &cols
[colstart
], ctrl
);
323 macctrl_editbox(mcs
, window
, &cols
[colstart
], ctrl
);
326 macctrl_radio(mcs
, window
, &cols
[colstart
], ctrl
);
329 macctrl_checkbox(mcs
, window
, &cols
[colstart
], ctrl
);
332 macctrl_button(mcs
, window
, &cols
[colstart
], ctrl
);
335 if (ctrl
->listbox
.height
== 0)
336 macctrl_popup(mcs
, window
, &cols
[colstart
], ctrl
);
338 macctrl_listbox(mcs
, window
, &cols
[colstart
], ctrl
);
342 for (j
= 0; j
< ncols
; j
++)
343 if (cols
[j
].pos
.v
> curstate
->pos
.v
)
344 curstate
->pos
.v
= cols
[j
].pos
.v
;
347 static void macctrl_switchtopanel(struct macctrls
*mcs
, unsigned int which
)
355 #define hideshow(c) do { \
356 if (i == which) ShowControl(c); else HideControl(c); \
359 mcs
->curpanel
= which
;
360 /* Panel 0 is special and always visible. */
361 for (i
= 1; i
< mcs
->npanels
; i
++)
362 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
363 #if !TARGET_API_MAC_CARBON
364 if (mcs
->focus
== mc
)
365 macctrl_setfocus(mcs
, NULL
);
367 switch (mc
->generic
.type
) {
369 hideshow(mc
->text
.tbctrl
);
371 case MACCTRL_EDITBOX
:
372 hideshow(mc
->editbox
.tbctrl
);
373 if (mc
->editbox
.tblabel
!= NULL
)
374 hideshow(mc
->editbox
.tblabel
);
377 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
378 hideshow(mc
->radio
.tbctrls
[j
]);
379 if (mc
->radio
.tblabel
!= NULL
)
380 hideshow(mc
->radio
.tblabel
);
382 case MACCTRL_CHECKBOX
:
383 hideshow(mc
->checkbox
.tbctrl
);
386 hideshow(mc
->button
.tbctrl
);
388 case MACCTRL_LISTBOX
:
389 hideshow(mc
->listbox
.tbctrl
);
391 * At least under Mac OS 8.1, hiding a list box
392 * doesn't hide its scroll bars.
394 if (mac_gestalts
.apprvers
>= 0x100) {
395 if (GetControlData(mc
->listbox
.tbctrl
,
396 kControlEntireControl
,
397 kControlListBoxListHandleTag
,
398 sizeof(list
), &list
, &olen
) == noErr
)
399 #if TARGET_API_MAC_CARBON
400 hideshow(GetListVerticalScrollBar(list
));
402 hideshow((*list
)->vScroll
);
407 hideshow(mc
->popup
.tbctrl
);
413 #if !TARGET_API_MAC_CARBON
415 * System 7 focus manipulation
417 static void macctrl_defocus(union macctrl
*mc
)
420 assert(mac_gestalts
.apprvers
< 0x100);
421 switch (mc
->generic
.type
) {
422 case MACCTRL_EDITBOX
:
423 TEDeactivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
428 static void macctrl_enfocus(union macctrl
*mc
)
431 assert(mac_gestalts
.apprvers
< 0x100);
432 switch (mc
->generic
.type
) {
433 case MACCTRL_EDITBOX
:
434 TEActivate((TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
439 static void macctrl_setfocus(struct macctrls
*mcs
, union macctrl
*mc
)
442 if (mcs
->focus
== mc
)
444 if (mcs
->focus
!= NULL
)
445 macctrl_defocus(mcs
->focus
);
452 static void macctrl_text(struct macctrls
*mcs
, WindowPtr window
,
453 struct mac_layoutstate
*curstate
,
456 union macctrl
*mc
= snew(union macctrl
);
460 assert(ctrl
->text
.label
!= NULL
);
461 fprintf(stderr
, " label = %s\n", ctrl
->text
.label
);
462 mc
->generic
.type
= MACCTRL_TEXT
;
463 mc
->generic
.ctrl
= ctrl
;
464 mc
->generic
.privdata
= NULL
;
465 bounds
.left
= curstate
->pos
.h
;
466 bounds
.right
= bounds
.left
+ curstate
->width
;
467 bounds
.top
= curstate
->pos
.v
;
468 bounds
.bottom
= bounds
.top
+ 16;
469 if (mac_gestalts
.apprvers
>= 0x100) {
472 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
473 kControlStaticTextProc
, (long)mc
);
474 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
475 kControlStaticTextTextTag
,
476 strlen(ctrl
->text
.label
), ctrl
->text
.label
);
477 GetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
478 kControlStaticTextTextHeightTag
,
479 sizeof(height
), &height
, &olen
);
481 #if !TARGET_API_MAC_CARBON
485 mc
->text
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
486 SYS7_TEXT_PROC
, (long)mc
);
487 te
= (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
;
488 TESetText(ctrl
->text
.label
, strlen(ctrl
->text
.label
), te
);
489 height
= TEGetHeight(1, (*te
)->nLines
, te
);
492 fprintf(stderr
, " height = %d\n", height
);
493 SizeControl(mc
->text
.tbctrl
, curstate
->width
, height
);
494 curstate
->pos
.v
+= height
+ 6;
495 add234(mcs
->byctrl
, mc
);
496 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
497 mcs
->panels
[curstate
->panelnum
] = mc
;
500 static void macctrl_editbox(struct macctrls
*mcs
, WindowPtr window
,
501 struct mac_layoutstate
*curstate
,
504 union macctrl
*mc
= snew(union macctrl
);
505 Rect lbounds
, bounds
;
507 if (ctrl
->editbox
.label
!= NULL
)
508 fprintf(stderr
, " label = %s\n", ctrl
->editbox
.label
);
509 fprintf(stderr
, " percentwidth = %d\n", ctrl
->editbox
.percentwidth
);
510 if (ctrl
->editbox
.password
) fprintf(stderr
, " password\n");
511 if (ctrl
->editbox
.has_list
) fprintf(stderr
, " has list\n");
512 mc
->generic
.type
= MACCTRL_EDITBOX
;
513 mc
->generic
.ctrl
= ctrl
;
514 mc
->generic
.privdata
= NULL
;
515 lbounds
.left
= curstate
->pos
.h
;
516 lbounds
.top
= curstate
->pos
.v
;
517 if (ctrl
->editbox
.percentwidth
== 100) {
518 if (ctrl
->editbox
.label
!= NULL
) {
519 lbounds
.right
= lbounds
.left
+ curstate
->width
;
520 lbounds
.bottom
= lbounds
.top
+ 16;
521 curstate
->pos
.v
+= 18;
523 bounds
.left
= curstate
->pos
.h
;
524 bounds
.right
= bounds
.left
+ curstate
->width
;
526 lbounds
.right
= lbounds
.left
+
527 curstate
->width
* (100 - ctrl
->editbox
.percentwidth
) / 100;
528 lbounds
.bottom
= lbounds
.top
+ 22;
529 bounds
.left
= lbounds
.right
;
530 bounds
.right
= lbounds
.left
+ curstate
->width
;
532 bounds
.top
= curstate
->pos
.v
;
533 bounds
.bottom
= bounds
.top
+ 22;
534 if (mac_gestalts
.apprvers
>= 0x100) {
535 if (ctrl
->editbox
.label
== NULL
)
536 mc
->editbox
.tblabel
= NULL
;
538 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
539 0, 0, 0, kControlStaticTextProc
,
541 SetControlData(mc
->editbox
.tblabel
, kControlEntireControl
,
542 kControlStaticTextTextTag
,
543 strlen(ctrl
->editbox
.label
), ctrl
->editbox
.label
);
545 InsetRect(&bounds
, 3, 3);
546 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
547 ctrl
->editbox
.password ?
548 kControlEditTextPasswordProc
:
549 kControlEditTextProc
, (long)mc
);
551 #if !TARGET_API_MAC_CARBON
553 if (ctrl
->editbox
.label
== NULL
)
554 mc
->editbox
.tblabel
= NULL
;
556 mc
->editbox
.tblabel
= NewControl(window
, &lbounds
, NULL
, TRUE
,
557 0, 0, 0, SYS7_TEXT_PROC
,
559 TESetText(ctrl
->editbox
.label
, strlen(ctrl
->editbox
.label
),
560 (TEHandle
)(*mc
->editbox
.tblabel
)->contrlData
);
562 mc
->editbox
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
, 0, 0, 0,
563 SYS7_EDITBOX_PROC
, (long)mc
);
566 curstate
->pos
.v
+= 28;
567 add234(mcs
->byctrl
, mc
);
568 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
569 mcs
->panels
[curstate
->panelnum
] = mc
;
570 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
573 #if !TARGET_API_MAC_CARBON
574 static pascal SInt32
macctrl_sys7_editbox_cdef(SInt16 variant
,
576 ControlDefProcMessage msg
,
587 rect
= (*control
)->contrlRect
;
588 if (variant
== SYS7_EDITBOX_VARIANT
)
589 InsetRect(&rect
, 3, 3); /* 2 if it's 20 pixels high */
590 te
= TENew(&rect
, &rect
);
591 ssfs
= GetScriptVariable(smSystemScript
, smScriptSysFondSize
);
592 (*te
)->txSize
= LoWord(ssfs
);
593 (*te
)->txFont
= HiWord(ssfs
);
594 (*control
)->contrlData
= (Handle
)te
;
597 TEDispose((TEHandle
)(*control
)->contrlData
);
600 if ((*control
)->contrlVis
) {
601 rect
= (*control
)->contrlRect
;
602 if (variant
== SYS7_EDITBOX_VARIANT
) {
605 InsetRect(&rect
, 3, 3);
607 (*(TEHandle
)(*control
)->contrlData
)->viewRect
= rect
;
608 TEUpdate(&rect
, (TEHandle
)(*control
)->contrlData
);
612 if (variant
== SYS7_TEXT_VARIANT
)
613 return kControlNoPart
;
614 mouse
.h
= LoWord(param
);
615 mouse
.v
= HiWord(param
);
616 rect
= (*control
)->contrlRect
;
617 InsetRect(&rect
, 3, 3);
618 return PtInRect(mouse
, &rect
) ? kControlEditTextPart
: kControlNoPart
;
620 if (param
& (1 << 31)) {
626 rgn
= (RgnHandle
)param
;
627 RectRgn(rgn
, &(*control
)->contrlRect
);
631 rgn
= (RgnHandle
)param
;
640 static void macctrl_radio(struct macctrls
*mcs
, WindowPtr window
,
641 struct mac_layoutstate
*curstate
,
644 union macctrl
*mc
= snew(union macctrl
);
647 unsigned int i
, colwidth
;
649 if (ctrl
->radio
.label
!= NULL
)
650 fprintf(stderr
, " label = %s\n", ctrl
->radio
.label
);
651 mc
->generic
.type
= MACCTRL_RADIO
;
652 mc
->generic
.ctrl
= ctrl
;
653 mc
->generic
.privdata
= NULL
;
654 mc
->radio
.tbctrls
= snewn(ctrl
->radio
.nbuttons
, ControlRef
);
655 colwidth
= (curstate
->width
+ 13) / ctrl
->radio
.ncolumns
;
656 bounds
.top
= curstate
->pos
.v
;
657 bounds
.bottom
= bounds
.top
+ 16;
658 bounds
.left
= curstate
->pos
.h
;
659 bounds
.right
= bounds
.left
+ curstate
->width
;
660 if (ctrl
->radio
.label
== NULL
)
661 mc
->radio
.tblabel
= NULL
;
663 if (mac_gestalts
.apprvers
>= 0x100) {
664 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
665 0, 0, 0, kControlStaticTextProc
,
667 SetControlData(mc
->radio
.tblabel
, kControlEntireControl
,
668 kControlStaticTextTextTag
,
669 strlen(ctrl
->radio
.label
), ctrl
->radio
.label
);
671 #if !TARGET_API_MAC_CARBON
673 mc
->radio
.tblabel
= NewControl(window
, &bounds
, NULL
, TRUE
,
674 0, 0, 0, SYS7_TEXT_PROC
, (long)mc
);
675 TESetText(ctrl
->radio
.label
, strlen(ctrl
->radio
.label
),
676 (TEHandle
)(*mc
->radio
.tblabel
)->contrlData
);
679 curstate
->pos
.v
+= 18;
681 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
682 fprintf(stderr
, " button = %s\n", ctrl
->radio
.buttons
[i
]);
683 bounds
.top
= curstate
->pos
.v
- 2;
684 bounds
.bottom
= bounds
.top
+ 18;
685 bounds
.left
= curstate
->pos
.h
+ colwidth
* (i
% ctrl
->radio
.ncolumns
);
686 if (i
== ctrl
->radio
.nbuttons
- 1 ||
687 i
% ctrl
->radio
.ncolumns
== ctrl
->radio
.ncolumns
- 1) {
688 bounds
.right
= curstate
->pos
.h
+ curstate
->width
;
689 curstate
->pos
.v
+= 18;
691 bounds
.right
= bounds
.left
+ colwidth
- 13;
692 c2pstrcpy(title
, ctrl
->radio
.buttons
[i
]);
693 mc
->radio
.tbctrls
[i
] = NewControl(window
, &bounds
, title
, TRUE
,
694 0, 0, 1, radioButProc
, (long)mc
);
696 curstate
->pos
.v
+= 4;
697 add234(mcs
->byctrl
, mc
);
698 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
699 mcs
->panels
[curstate
->panelnum
] = mc
;
700 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
703 static void macctrl_checkbox(struct macctrls
*mcs
, WindowPtr window
,
704 struct mac_layoutstate
*curstate
,
707 union macctrl
*mc
= snew(union macctrl
);
711 assert(ctrl
->checkbox
.label
!= NULL
);
712 fprintf(stderr
, " label = %s\n", ctrl
->checkbox
.label
);
713 mc
->generic
.type
= MACCTRL_CHECKBOX
;
714 mc
->generic
.ctrl
= ctrl
;
715 mc
->generic
.privdata
= NULL
;
716 bounds
.left
= curstate
->pos
.h
;
717 bounds
.right
= bounds
.left
+ curstate
->width
;
718 bounds
.top
= curstate
->pos
.v
;
719 bounds
.bottom
= bounds
.top
+ 16;
720 c2pstrcpy(title
, ctrl
->checkbox
.label
);
721 mc
->checkbox
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
722 checkBoxProc
, (long)mc
);
723 add234(mcs
->byctrl
, mc
);
724 curstate
->pos
.v
+= 22;
725 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
726 mcs
->panels
[curstate
->panelnum
] = mc
;
727 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
730 static void macctrl_button(struct macctrls
*mcs
, WindowPtr window
,
731 struct mac_layoutstate
*curstate
,
734 union macctrl
*mc
= snew(union macctrl
);
738 assert(ctrl
->button
.label
!= NULL
);
739 fprintf(stderr
, " label = %s\n", ctrl
->button
.label
);
740 if (ctrl
->button
.isdefault
)
741 fprintf(stderr
, " is default\n");
742 mc
->generic
.type
= MACCTRL_BUTTON
;
743 mc
->generic
.ctrl
= ctrl
;
744 mc
->generic
.privdata
= NULL
;
745 bounds
.left
= curstate
->pos
.h
;
746 bounds
.right
= bounds
.left
+ curstate
->width
;
747 bounds
.top
= curstate
->pos
.v
;
748 bounds
.bottom
= bounds
.top
+ 20;
749 c2pstrcpy(title
, ctrl
->button
.label
);
750 mc
->button
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
751 pushButProc
, (long)mc
);
752 mc
->button
.tbring
= NULL
;
753 if (mac_gestalts
.apprvers
>= 0x100) {
754 Boolean isdefault
= ctrl
->button
.isdefault
;
756 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
757 kControlPushButtonDefaultTag
,
758 sizeof(isdefault
), &isdefault
);
759 } else if (ctrl
->button
.isdefault
) {
760 InsetRect(&bounds
, -4, -4);
761 mc
->button
.tbring
= NewControl(window
, &bounds
, title
, TRUE
, 0, 0, 1,
762 SYS7_DEFAULT_PROC
, (long)mc
);
764 if (mac_gestalts
.apprvers
>= 0x110) {
765 Boolean iscancel
= ctrl
->button
.iscancel
;
767 SetControlData(mc
->button
.tbctrl
, kControlEntireControl
,
768 kControlPushButtonCancelTag
,
769 sizeof(iscancel
), &iscancel
);
771 if (ctrl
->button
.isdefault
)
773 if (ctrl
->button
.iscancel
)
775 add234(mcs
->byctrl
, mc
);
776 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
777 mcs
->panels
[curstate
->panelnum
] = mc
;
778 curstate
->pos
.v
+= 26;
781 #if !TARGET_API_MAC_CARBON
782 static pascal SInt32
macctrl_sys7_default_cdef(SInt16 variant
,
784 ControlDefProcMessage msg
,
794 if ((*control
)->contrlVis
) {
795 rect
= (*control
)->contrlRect
;
796 GetPenState(&savestate
);
799 if ((*control
)->contrlHilite
== kControlInactivePart
)
801 oval
= (rect
.bottom
- rect
.top
) / 2 + 2;
802 FrameRoundRect(&rect
, oval
, oval
);
803 SetPenState(&savestate
);
807 if (param
& (1 << 31)) {
813 rgn
= (RgnHandle
)param
;
814 RectRgn(rgn
, &(*control
)->contrlRect
);
818 rgn
= (RgnHandle
)param
;
827 static void macctrl_listbox(struct macctrls
*mcs
, WindowPtr window
,
828 struct mac_layoutstate
*curstate
,
831 union macctrl
*mc
= snew(union macctrl
);
836 if (mac_gestalts
.apprvers
< 0x100)
838 if (ctrl
->listbox
.label
!= NULL
)
839 fprintf(stderr
, " label = %s\n", ctrl
->listbox
.label
);
840 fprintf(stderr
, " height = %d\n", ctrl
->listbox
.height
);
841 if (ctrl
->listbox
.draglist
)
842 fprintf(stderr
, " draglist\n");
843 if (ctrl
->listbox
.multisel
)
844 fprintf(stderr
, " multisel\n");
845 fprintf(stderr
, " ncols = %d\n", ctrl
->listbox
.ncols
);
846 assert(ctrl
->listbox
.percentwidth
== 100);
847 mc
->generic
.type
= MACCTRL_LISTBOX
;
848 mc
->generic
.ctrl
= ctrl
;
849 mc
->generic
.privdata
= NULL
;
850 /* The list starts off empty */
851 mc
->listbox
.nids
= 0;
852 mc
->listbox
.ids
= NULL
;
853 bounds
.left
= curstate
->pos
.h
;
854 bounds
.right
= bounds
.left
+ curstate
->width
;
855 bounds
.top
= curstate
->pos
.v
;
856 bounds
.bottom
= bounds
.top
+ 20 * ctrl
->listbox
.height
;
858 mc
->button
.tbctrl
= NewControl(window
, &bounds
, NULL
, TRUE
,
860 kControlListBoxProc
, (long)mc
);
861 if (!ctrl
->listbox
.multisel
) {
862 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
863 kControlListBoxListHandleTag
, sizeof(list
), &list
,
866 #if TARGET_API_MAC_CARBON
867 SetListSelectionFlags(list
, lOnlyOne
);
869 (*list
)->selFlags
= lOnlyOne
;
872 add234(mcs
->byctrl
, mc
);
873 curstate
->pos
.v
+= 6 + 20 * ctrl
->listbox
.height
;
874 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
875 mcs
->panels
[curstate
->panelnum
] = mc
;
876 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
880 static void macctrl_popup(struct macctrls
*mcs
, WindowPtr window
,
881 struct mac_layoutstate
*curstate
,
884 union macctrl
*mc
= snew(union macctrl
);
887 unsigned int labelwidth
;
888 static int nextmenuid
= MENU_MIN
;
893 * <http://developer.apple.com/qa/tb/tb42.html> explains how to
894 * create a popup menu with dynamic content.
896 assert(ctrl
->listbox
.height
== 0);
897 assert(!ctrl
->listbox
.draglist
);
898 assert(!ctrl
->listbox
.multisel
);
900 if (ctrl
->listbox
.label
!= NULL
)
901 fprintf(stderr
, " label = %s\n", ctrl
->listbox
.label
);
902 fprintf(stderr
, " percentwidth = %d\n", ctrl
->listbox
.percentwidth
);
904 mc
->generic
.type
= MACCTRL_POPUP
;
905 mc
->generic
.ctrl
= ctrl
;
906 mc
->generic
.privdata
= NULL
;
907 c2pstrcpy(title
, ctrl
->button
.label
== NULL ?
"" : ctrl
->button
.label
);
909 /* Find a spare menu ID and create the menu */
910 while (GetMenuHandle(nextmenuid
) != NULL
)
911 if (++nextmenuid
>= MENU_MAX
) nextmenuid
= MENU_MIN
;
912 menuid
= nextmenuid
++;
913 menu
= NewMenu(menuid
, "\pdummy");
914 if (menu
== NULL
) return;
915 mc
->popup
.menu
= menu
;
916 mc
->popup
.menuid
= menuid
;
917 InsertMenu(menu
, kInsertHierarchicalMenu
);
919 /* The menu starts off empty */
921 mc
->popup
.ids
= NULL
;
923 bounds
.left
= curstate
->pos
.h
;
924 bounds
.right
= bounds
.left
+ curstate
->width
;
925 bounds
.top
= curstate
->pos
.v
;
926 bounds
.bottom
= bounds
.top
+ 20;
927 /* XXX handle percentwidth == 100 */
928 labelwidth
= curstate
->width
* (100 - ctrl
->listbox
.percentwidth
) / 100;
929 mc
->popup
.tbctrl
= NewControl(window
, &bounds
, title
, TRUE
,
930 popupTitleLeftJust
, menuid
, labelwidth
,
931 popupMenuProc
+ popupFixedWidth
, (long)mc
);
932 add234(mcs
->byctrl
, mc
);
933 curstate
->pos
.v
+= 26;
934 mc
->generic
.next
= mcs
->panels
[curstate
->panelnum
];
935 mcs
->panels
[curstate
->panelnum
] = mc
;
936 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
940 void macctrl_activate(WindowPtr window
, EventRecord
*event
)
942 struct macctrls
*mcs
= mac_winctrls(window
);
943 Boolean active
= (event
->modifiers
& activeFlag
) != 0;
946 ControlPartCode state
;
950 SetPort((GrafPtr
)GetWindowPort(window
));
951 if (mac_gestalts
.apprvers
>= 0x100)
952 SetThemeWindowBackground(window
, active ?
953 kThemeBrushModelessDialogBackgroundActive
:
954 kThemeBrushModelessDialogBackgroundInactive
,
956 state
= active ? kControlNoPart
: kControlInactivePart
;
957 for (i
= 0; i
<= mcs
->curpanel
; i
+= mcs
->curpanel
)
958 for (mc
= mcs
->panels
[i
]; mc
!= NULL
; mc
= mc
->generic
.next
) {
959 switch (mc
->generic
.type
) {
961 HiliteControl(mc
->text
.tbctrl
, state
);
963 case MACCTRL_EDITBOX
:
964 HiliteControl(mc
->editbox
.tbctrl
, state
);
965 if (mc
->editbox
.tblabel
!= NULL
)
966 HiliteControl(mc
->editbox
.tblabel
, state
);
969 for (j
= 0; j
< mc
->generic
.ctrl
->radio
.nbuttons
; j
++)
970 HiliteControl(mc
->radio
.tbctrls
[j
], state
);
971 if (mc
->radio
.tblabel
!= NULL
)
972 HiliteControl(mc
->radio
.tblabel
, state
);
974 case MACCTRL_CHECKBOX
:
975 HiliteControl(mc
->checkbox
.tbctrl
, state
);
978 HiliteControl(mc
->button
.tbctrl
, state
);
979 if (mc
->button
.tbring
!= NULL
)
980 HiliteControl(mc
->button
.tbring
, state
);
982 case MACCTRL_LISTBOX
:
983 HiliteControl(mc
->listbox
.tbctrl
, state
);
986 HiliteControl(mc
->popup
.tbctrl
, state
);
989 #if !TARGET_API_MAC_CARBON
990 if (mcs
->focus
== mc
) {
1001 void macctrl_click(WindowPtr window
, EventRecord
*event
)
1004 ControlHandle control
, oldfocus
;
1005 int part
, trackresult
;
1008 struct macctrls
*mcs
= mac_winctrls(window
);
1013 SetPort((GrafPtr
)GetWindowPort(window
));
1014 mouse
= event
->where
;
1015 GlobalToLocal(&mouse
);
1016 part
= FindControl(mouse
, window
, &control
);
1017 if (control
!= NULL
) {
1018 mc
= (union macctrl
*)GetControlReference(control
);
1019 if (mac_gestalts
.apprvers
>= 0x100) {
1020 if (GetControlFeatures(control
, &features
) == noErr
&&
1021 (features
& kControlSupportsFocus
) &&
1022 (features
& kControlGetsFocusOnClick
) &&
1023 GetKeyboardFocus(window
, &oldfocus
) == noErr
&&
1024 control
!= oldfocus
)
1025 SetKeyboardFocus(window
, control
, part
);
1026 trackresult
= HandleControlClick(control
, mouse
, event
->modifiers
,
1027 (ControlActionUPP
)-1);
1029 #if !TARGET_API_MAC_CARBON
1030 if (mc
->generic
.type
== MACCTRL_EDITBOX
&&
1031 control
== mc
->editbox
.tbctrl
) {
1032 TEHandle te
= (TEHandle
)(*control
)->contrlData
;
1034 macctrl_setfocus(mcs
, mc
);
1035 TEClick(mouse
, !!(event
->modifiers
& shiftKey
), te
);
1039 trackresult
= TrackControl(control
, mouse
, (ControlActionUPP
)-1);
1041 switch (mc
->generic
.type
) {
1043 if (trackresult
!= 0) {
1044 for (i
= 0; i
< mc
->generic
.ctrl
->radio
.nbuttons
; i
++)
1045 if (mc
->radio
.tbctrls
[i
] == control
)
1046 SetControlValue(mc
->radio
.tbctrls
[i
],
1047 kControlRadioButtonCheckedValue
);
1049 SetControlValue(mc
->radio
.tbctrls
[i
],
1050 kControlRadioButtonUncheckedValue
);
1051 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1054 case MACCTRL_CHECKBOX
:
1055 if (trackresult
!= 0) {
1056 SetControlValue(control
, !GetControlValue(control
));
1057 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1060 case MACCTRL_BUTTON
:
1061 if (trackresult
!= 0)
1062 ctrlevent(mcs
, mc
, EVENT_ACTION
);
1064 case MACCTRL_LISTBOX
:
1065 /* FIXME spot double-click */
1066 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1069 ctrlevent(mcs
, mc
, EVENT_SELCHANGE
);
1077 void macctrl_key(WindowPtr window
, EventRecord
*event
)
1080 struct macctrls
*mcs
= mac_winctrls(window
);
1082 unsigned long dummy
;
1084 switch (event
->message
& charCodeMask
) {
1085 case kEnterCharCode
:
1086 case kReturnCharCode
:
1087 if (mcs
->defbutton
!= NULL
) {
1088 assert(mcs
->defbutton
->generic
.type
== MACCTRL_BUTTON
);
1089 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlButtonPart
);
1091 * I'd like to delay unhilighting the button until after
1092 * the event has been processed, but by them the entire
1093 * dialgue box might have been destroyed.
1096 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1097 ctrlevent(mcs
, mcs
->defbutton
, EVENT_ACTION
);
1100 case kEscapeCharCode
:
1101 if (mcs
->canbutton
!= NULL
) {
1102 assert(mcs
->canbutton
->generic
.type
== MACCTRL_BUTTON
);
1103 HiliteControl(mcs
->canbutton
->button
.tbctrl
, kControlButtonPart
);
1105 HiliteControl(mcs
->defbutton
->button
.tbctrl
, kControlNoPart
);
1106 ctrlevent(mcs
, mcs
->canbutton
, EVENT_ACTION
);
1110 if (mac_gestalts
.apprvers
>= 0x100) {
1111 if (GetKeyboardFocus(window
, &control
) == noErr
&& control
!= NULL
) {
1112 HandleControlKey(control
, (event
->message
& keyCodeMask
) >> 8,
1113 event
->message
& charCodeMask
, event
->modifiers
);
1114 mc
= (union macctrl
*)GetControlReference(control
);
1115 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1118 #if !TARGET_API_MAC_CARBON
1122 if (mcs
->focus
!= NULL
) {
1124 switch (mc
->generic
.type
) {
1125 case MACCTRL_EDITBOX
:
1126 te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1127 TEKey(event
->message
& charCodeMask
, te
);
1128 ctrlevent(mcs
, mc
, EVENT_VALCHANGE
);
1136 void macctrl_update(WindowPtr window
)
1138 #if TARGET_API_MAC_CARBON
1144 BeginUpdate(window
);
1146 SetPort((GrafPtr
)GetWindowPort(window
));
1147 if (mac_gestalts
.apprvers
>= 0x101) {
1148 #if TARGET_API_MAC_CARBON
1149 GetPortBounds(GetWindowPort(window
), &rect
);
1151 rect
= window
->portRect
;
1153 InsetRect(&rect
, -1, -1);
1154 DrawThemeModelessDialogFrame(&rect
, mac_frontwindow() == window ?
1155 kThemeStateActive
: kThemeStateInactive
);
1157 #if TARGET_API_MAC_CARBON
1159 GetPortVisibleRegion(GetWindowPort(window
), visrgn
);
1160 UpdateControls(window
, visrgn
);
1163 UpdateControls(window
, window
->visRgn
);
1169 #if TARGET_API_MAC_CARBON
1170 #define EnableItem EnableMenuItem
1171 #define DisableItem DisableMenuItem
1173 void macctrl_adjustmenus(WindowPtr window
)
1177 menu
= GetMenuHandle(mFile
);
1178 DisableItem(menu
, iSave
); /* XXX enable if modified */
1179 EnableItem(menu
, iSaveAs
);
1180 EnableItem(menu
, iDuplicate
);
1182 menu
= GetMenuHandle(mEdit
);
1183 DisableItem(menu
, 0);
1186 void macctrl_close(WindowPtr window
)
1188 struct macctrls
*mcs
= mac_winctrls(window
);
1192 * Mostly, we don't bother disposing of the Toolbox controls,
1193 * since that will happen automatically when the window is
1194 * disposed of. Popup menus are an exception, because we have to
1195 * dispose of the menu ourselves, and doing that while the control
1196 * still holds a reference to it seems rude.
1198 while ((mc
= index234(mcs
->byctrl
, 0)) != NULL
) {
1199 if (mc
->generic
.privdata
!= NULL
&& mc
->generic
.freeprivdata
)
1200 sfree(mc
->generic
.privdata
);
1201 switch (mc
->generic
.type
) {
1203 DisposeControl(mc
->popup
.tbctrl
);
1204 DeleteMenu(mc
->popup
.menuid
);
1205 DisposeMenu(mc
->popup
.menu
);
1208 del234(mcs
->byctrl
, mc
);
1212 freetree234(mcs
->byctrl
);
1218 void dlg_update_start(union control
*ctrl
, void *dlg
)
1224 void dlg_update_done(union control
*ctrl
, void *dlg
)
1230 void dlg_set_focus(union control
*ctrl
, void *dlg
)
1233 if (mac_gestalts
.apprvers
>= 0x100) {
1234 /* Use SetKeyboardFocus() */
1236 /* Do our own mucking around */
1240 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1246 void dlg_beep(void *dlg
)
1252 void dlg_error_msg(void *dlg
, char *msg
)
1256 c2pstrcpy(pmsg
, msg
);
1257 ParamText(pmsg
, NULL
, NULL
, NULL
);
1258 StopAlert(128, NULL
);
1261 void dlg_end(void *dlg
, int value
)
1263 struct macctrls
*mcs
= dlg
;
1265 if (mcs
->end
!= NULL
)
1266 (*mcs
->end
)(mcs
->window
, value
);
1269 void dlg_refresh(union control
*ctrl
, void *dlg
)
1271 struct macctrls
*mcs
= dlg
;
1276 mc
= findbyctrl(mcs
, ctrl
);
1278 ctrlevent(mcs
, mc
, EVENT_REFRESH
);
1281 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
1283 struct macctrls
*mcs
= dlg
;
1284 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1287 return mc
->generic
.privdata
;
1290 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
1292 struct macctrls
*mcs
= dlg
;
1293 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1296 mc
->generic
.privdata
= ptr
;
1297 mc
->generic
.freeprivdata
= FALSE
;
1300 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
1302 struct macctrls
*mcs
= dlg
;
1303 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1306 mc
->generic
.privdata
= smalloc(size
);
1307 mc
->generic
.freeprivdata
= TRUE
;
1308 return mc
->generic
.privdata
;
1313 * Radio Button control
1316 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
1318 struct macctrls
*mcs
= dlg
;
1319 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1322 if (mc
== NULL
) return;
1323 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1324 if (i
== whichbutton
)
1325 SetControlValue(mc
->radio
.tbctrls
[i
],
1326 kControlRadioButtonCheckedValue
);
1328 SetControlValue(mc
->radio
.tbctrls
[i
],
1329 kControlRadioButtonUncheckedValue
);
1334 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
1336 struct macctrls
*mcs
= dlg
;
1337 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1341 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1342 if (GetControlValue(mc
->radio
.tbctrls
[i
]) ==
1343 kControlRadioButtonCheckedValue
)
1354 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
1356 struct macctrls
*mcs
= dlg
;
1357 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1359 if (mc
== NULL
) return;
1360 SetControlValue(mc
->checkbox
.tbctrl
,
1361 checked ? kControlCheckBoxCheckedValue
:
1362 kControlCheckBoxUncheckedValue
);
1365 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
1367 struct macctrls
*mcs
= dlg
;
1368 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1371 return GetControlValue(mc
->checkbox
.tbctrl
);
1379 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
1381 struct macctrls
*mcs
= dlg
;
1382 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1385 if (mc
== NULL
) return;
1386 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1388 SetPort((GrafPtr
)(GetWindowPort(mcs
->window
)));
1389 if (mac_gestalts
.apprvers
>= 0x100)
1390 SetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1391 ctrl
->editbox
.password ?
1392 kControlEditTextPasswordTag
:
1393 kControlEditTextTextTag
,
1394 strlen(text
), text
);
1395 #if !TARGET_API_MAC_CARBON
1397 TESetText(text
, strlen(text
),
1398 (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
);
1400 DrawOneControl(mc
->editbox
.tbctrl
);
1404 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
1406 struct macctrls
*mcs
= dlg
;
1407 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1411 assert(mc
->generic
.type
== MACCTRL_EDITBOX
);
1412 if (mac_gestalts
.apprvers
>= 0x100) {
1413 if (GetControlData(mc
->editbox
.tbctrl
, kControlEntireControl
,
1414 ctrl
->editbox
.password ?
1415 kControlEditTextPasswordTag
:
1416 kControlEditTextTextTag
,
1417 length
- 1, buffer
, &olen
) != noErr
)
1419 if (olen
> length
- 1)
1422 #if !TARGET_API_MAC_CARBON
1424 TEHandle te
= (TEHandle
)(*mc
->editbox
.tbctrl
)->contrlData
;
1426 olen
= (*te
)->teLength
;
1427 if (olen
> length
- 1)
1429 memcpy(buffer
, *(*te
)->hText
, olen
);
1432 buffer
[olen
] = '\0';
1433 fprintf(stderr
, "dlg_editbox_get: %s\n", buffer
);
1441 static void dlg_macpopup_clear(union control
*ctrl
, void *dlg
)
1443 struct macctrls
*mcs
= dlg
;
1444 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1445 MenuRef menu
= mc
->popup
.menu
;
1448 if (mc
== NULL
) return;
1449 fprintf(stderr
, " popup_clear\n");
1450 n
= CountMenuItems(menu
);
1451 for (i
= 0; i
< n
; i
++)
1452 DeleteMenuItem(menu
, n
- i
);
1454 sfree(mc
->popup
.ids
);
1455 mc
->popup
.ids
= NULL
;
1456 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1459 static void dlg_maclist_clear(union control
*ctrl
, void *dlg
)
1461 struct macctrls
*mcs
= dlg
;
1462 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1466 if (mc
== NULL
) return;
1467 fprintf(stderr
, " maclist_clear\n");
1468 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
1469 kControlListBoxListHandleTag
, sizeof(list
), &list
,
1472 LDelRow(0, 0, list
);
1473 mc
->listbox
.nids
= 0;
1474 sfree(mc
->listbox
.ids
);
1475 mc
->listbox
.ids
= NULL
;
1476 DrawOneControl(mc
->listbox
.tbctrl
);
1479 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
1482 if (ctrl
->listbox
.height
== 0)
1483 dlg_macpopup_clear(ctrl
, dlg
);
1485 dlg_maclist_clear(ctrl
, dlg
);
1488 static void dlg_macpopup_del(union control
*ctrl
, void *dlg
, int index
)
1490 struct macctrls
*mcs
= dlg
;
1491 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1492 MenuRef menu
= mc
->popup
.menu
;
1494 if (mc
== NULL
) return;
1495 fprintf(stderr
, " popup_del %d\n", index
);
1496 DeleteMenuItem(menu
, index
+ 1);
1497 if (mc
->popup
.ids
!= NULL
)
1498 memcpy(mc
->popup
.ids
+ index
, mc
->popup
.ids
+ index
+ 1,
1499 (mc
->popup
.nids
- index
- 1) * sizeof(*mc
->popup
.ids
));
1500 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1503 static void dlg_maclist_del(union control
*ctrl
, void *dlg
, int index
)
1505 struct macctrls
*mcs
= dlg
;
1506 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1510 if (mc
== NULL
) return;
1511 fprintf(stderr
, " maclist_del %d\n", index
);
1512 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
1513 kControlListBoxListHandleTag
, sizeof(list
), &list
,
1516 LDelRow(1, index
, list
);
1517 if (mc
->listbox
.ids
!= NULL
)
1518 memcpy(mc
->listbox
.ids
+ index
, mc
->listbox
.ids
+ index
+ 1,
1519 (mc
->listbox
.nids
- index
- 1) * sizeof(*mc
->listbox
.ids
));
1520 DrawOneControl(mc
->listbox
.tbctrl
);
1523 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
1526 if (ctrl
->listbox
.height
== 0)
1527 dlg_macpopup_del(ctrl
, dlg
, index
);
1529 dlg_maclist_del(ctrl
, dlg
, index
);
1532 static void dlg_macpopup_add(union control
*ctrl
, void *dlg
, char const *text
)
1534 struct macctrls
*mcs
= dlg
;
1535 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1536 MenuRef menu
= mc
->popup
.menu
;
1539 if (mc
== NULL
) return;
1540 fprintf(stderr
, " popup_add %s\n", text
);
1541 assert(text
[0] != '\0');
1542 c2pstrcpy(itemstring
, text
);
1543 AppendMenu(menu
, "\pdummy");
1544 SetMenuItemText(menu
, CountMenuItems(menu
), itemstring
);
1545 SetControlMaximum(mc
->popup
.tbctrl
, CountMenuItems(menu
));
1549 static void dlg_maclist_add(union control
*ctrl
, void *dlg
, char const *text
)
1551 struct macctrls
*mcs
= dlg
;
1552 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1556 Cell cell
= { 0, 0 };
1558 if (mc
== NULL
) return;
1559 fprintf(stderr
, " maclist_add %s\n", text
);
1560 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
1561 kControlListBoxListHandleTag
, sizeof(list
), &list
,
1564 #if TARGET_API_MAC_CARBON
1565 GetListDataBounds(list
, &bounds
);
1567 bounds
= (*list
)->dataBounds
;
1569 cell
.v
= bounds
.bottom
;
1570 LAddRow(1, cell
.v
, list
);
1571 LSetCell(text
, strlen(text
), cell
, list
);
1572 DrawOneControl(mc
->listbox
.tbctrl
);
1575 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
1578 if (ctrl
->listbox
.height
== 0)
1579 dlg_macpopup_add(ctrl
, dlg
, text
);
1581 dlg_maclist_add(ctrl
, dlg
, text
);
1584 static void dlg_macpopup_addwithid(union control
*ctrl
, void *dlg
,
1585 char const *text
, int id
)
1587 struct macctrls
*mcs
= dlg
;
1588 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1589 MenuRef menu
= mc
->popup
.menu
;
1592 if (mc
== NULL
) return;
1593 fprintf(stderr
, " popup_addwthid %s, %d\n", text
, id
);
1594 dlg_macpopup_add(ctrl
, dlg
, text
);
1595 index
= CountMenuItems(menu
) - 1;
1596 if (mc
->popup
.nids
<= index
) {
1597 mc
->popup
.nids
= index
+ 1;
1598 mc
->popup
.ids
= sresize(mc
->popup
.ids
, mc
->popup
.nids
, int);
1600 mc
->popup
.ids
[index
] = id
;
1603 static void dlg_maclist_addwithid(union control
*ctrl
, void *dlg
,
1604 char const *text
, int id
)
1606 struct macctrls
*mcs
= dlg
;
1607 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1613 if (mc
== NULL
) return;
1614 fprintf(stderr
, " maclist_addwithid %s %d\n", text
, id
);
1615 dlg_maclist_add(ctrl
, dlg
, text
);
1616 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
1617 kControlListBoxListHandleTag
, sizeof(list
), &list
,
1620 #if TARGET_API_MAC_CARBON
1621 GetListDataBounds(list
, &bounds
);
1623 bounds
= (*list
)->dataBounds
;
1625 index
= bounds
.bottom
;
1626 if (mc
->listbox
.nids
<= index
) {
1627 mc
->listbox
.nids
= index
+ 1;
1628 mc
->listbox
.ids
= sresize(mc
->listbox
.ids
, mc
->listbox
.nids
, int);
1630 mc
->listbox
.ids
[index
] = id
;
1633 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
1634 char const *text
, int id
)
1637 if (ctrl
->listbox
.height
== 0)
1638 dlg_macpopup_addwithid(ctrl
, dlg
, text
, id
);
1640 dlg_maclist_addwithid(ctrl
, dlg
, text
, id
);
1643 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
1645 struct macctrls
*mcs
= dlg
;
1646 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1649 if (ctrl
->listbox
.height
== 0) {
1650 assert(mc
->popup
.ids
!= NULL
&& mc
->popup
.nids
> index
);
1651 return mc
->popup
.ids
[index
];
1656 static int dlg_maclist_index(union control
*ctrl
, void *dlg
)
1658 struct macctrls
*mcs
= dlg
;
1659 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1662 Cell cell
= { 0, 0 };
1664 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
1665 kControlListBoxListHandleTag
, sizeof(list
), &list
,
1668 if (LGetSelect(TRUE
, &cell
, list
))
1674 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
1676 struct macctrls
*mcs
= dlg
;
1679 if (ctrl
->listbox
.height
== 0) {
1680 mc
= findbyctrl(mcs
, ctrl
);
1682 return GetControlValue(mc
->popup
.tbctrl
) - 1;
1684 return dlg_maclist_index(ctrl
, dlg
);
1687 static int dlg_maclist_issel(union control
*ctrl
, void *dlg
, int index
)
1689 struct macctrls
*mcs
= dlg
;
1690 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1693 Cell cell
= { 0, 0 };
1695 if (GetControlData(mc
->listbox
.tbctrl
, kControlEntireControl
,
1696 kControlListBoxListHandleTag
, sizeof(list
), &list
,
1700 return LGetSelect(FALSE
, &cell
, list
);
1703 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
1705 struct macctrls
*mcs
= dlg
;
1706 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1708 if (ctrl
->listbox
.height
== 0)
1709 return GetControlValue(mc
->popup
.tbctrl
) - 1 == index
;
1711 return dlg_maclist_issel(ctrl
, dlg
, index
);
1714 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
1716 struct macctrls
*mcs
= dlg
;
1717 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1719 if (mc
== NULL
) return;
1720 if (ctrl
->listbox
.height
== 0)
1721 SetControlValue(mc
->popup
.tbctrl
, index
+ 1);
1729 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
1731 struct macctrls
*mcs
= dlg
;
1732 union macctrl
*mc
= findbyctrl(mcs
, ctrl
);
1734 if (mc
== NULL
) return;
1735 if (mac_gestalts
.apprvers
>= 0x100)
1736 SetControlData(mc
->text
.tbctrl
, kControlEntireControl
,
1737 kControlStaticTextTextTag
, strlen(text
), text
);
1738 #if !TARGET_API_MAC_CARBON
1740 TESetText(text
, strlen(text
),
1741 (TEHandle
)(*mc
->text
.tbctrl
)->contrlData
);
1747 * File Selector control
1750 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
1755 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
1762 * Font Selector control
1765 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fn
)
1770 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fn
)
1777 * Printer enumeration
1780 printer_enum
*printer_start_enum(int *nprinters
)
1787 char *printer_get_name(printer_enum
*pe
, int thing
)
1793 void printer_finish_enum(printer_enum
*pe
)
1800 * Colour selection stuff
1803 void dlg_coloursel_start(union control
*ctrl
, void *dlg
,
1804 int r
, int g
, int b
)
1809 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
1810 int *r
, int *g
, int *b
)
1818 * c-file-style: "simon"