e8e5e30a |
1 | /* $Id: macctrls.c,v 1.39 2003/04/14 23:47:07 ben Exp $ */ |
8a7e67ec |
2 | /* |
3 | * Copyright (c) 2003 Ben Harris |
4 | * All rights reserved. |
5 | * |
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 |
13 | * conditions: |
14 | * |
15 | * The above copyright notice and this permission notice shall be |
16 | * included in all copies or substantial portions of the Software. |
17 | * |
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 |
25 | * SOFTWARE. |
26 | */ |
27 | |
28 | #include <MacTypes.h> |
29 | #include <Appearance.h> |
621bfeaa |
30 | #include <ColorPicker.h> |
8a7e67ec |
31 | #include <Controls.h> |
32 | #include <ControlDefinitions.h> |
135c0f1a |
33 | #include <Events.h> |
4bd083f7 |
34 | #include <Lists.h> |
56c01970 |
35 | #include <Menus.h> |
fa4942e7 |
36 | #include <Resources.h> |
0e9ce565 |
37 | #include <Script.h> |
8a7e67ec |
38 | #include <Sound.h> |
0e9ce565 |
39 | #include <TextEdit.h> |
8a7e67ec |
40 | #include <TextUtils.h> |
0e9ce565 |
41 | #include <ToolUtils.h> |
8a7e67ec |
42 | #include <Windows.h> |
43 | |
93ba25f8 |
44 | #include <assert.h> |
ee10bc56 |
45 | #include <string.h> |
93ba25f8 |
46 | |
8a7e67ec |
47 | #include "putty.h" |
48 | #include "mac.h" |
49 | #include "macresid.h" |
50 | #include "dialog.h" |
51 | #include "tree234.h" |
52 | |
56c01970 |
53 | /* Range of menu IDs for popup menus */ |
54 | #define MENU_MIN 1024 |
55 | #define MENU_MAX 2048 |
56 | |
57 | |
8a7e67ec |
58 | union macctrl { |
59 | struct macctrl_generic { |
60 | enum { |
61 | MACCTRL_TEXT, |
1f1ec0e5 |
62 | MACCTRL_EDITBOX, |
8a7e67ec |
63 | MACCTRL_RADIO, |
64 | MACCTRL_CHECKBOX, |
56c01970 |
65 | MACCTRL_BUTTON, |
7d77d56d |
66 | MACCTRL_LISTBOX, |
56c01970 |
67 | MACCTRL_POPUP |
8a7e67ec |
68 | } type; |
69 | /* Template from which this was generated */ |
70 | union control *ctrl; |
ee10bc56 |
71 | /* Next control in this panel */ |
72 | union macctrl *next; |
1f1ec0e5 |
73 | void *privdata; |
74 | int freeprivdata; |
8a7e67ec |
75 | } generic; |
76 | struct { |
77 | struct macctrl_generic generic; |
78 | ControlRef tbctrl; |
79 | } text; |
80 | struct { |
81 | struct macctrl_generic generic; |
1f1ec0e5 |
82 | ControlRef tbctrl; |
cd3e71e8 |
83 | ControlRef tblabel; |
1f1ec0e5 |
84 | } editbox; |
85 | struct { |
86 | struct macctrl_generic generic; |
8a7e67ec |
87 | ControlRef *tbctrls; |
cd3e71e8 |
88 | ControlRef tblabel; |
8a7e67ec |
89 | } radio; |
90 | struct { |
91 | struct macctrl_generic generic; |
92 | ControlRef tbctrl; |
93 | } checkbox; |
94 | struct { |
95 | struct macctrl_generic generic; |
96 | ControlRef tbctrl; |
457f0b8f |
97 | ControlRef tbring; |
8a7e67ec |
98 | } button; |
56c01970 |
99 | struct { |
100 | struct macctrl_generic generic; |
101 | ControlRef tbctrl; |
4bd083f7 |
102 | ListHandle list; |
7d77d56d |
103 | unsigned int nids; |
104 | int *ids; |
105 | } listbox; |
106 | struct { |
107 | struct macctrl_generic generic; |
108 | ControlRef tbctrl; |
56c01970 |
109 | MenuRef menu; |
110 | int menuid; |
111 | unsigned int nids; |
112 | int *ids; |
113 | } popup; |
8a7e67ec |
114 | }; |
115 | |
116 | struct mac_layoutstate { |
117 | Point pos; |
118 | unsigned int width; |
ee10bc56 |
119 | unsigned int panelnum; |
8a7e67ec |
120 | }; |
121 | |
122 | #define ctrlevent(mcs, mc, event) do { \ |
123 | if ((mc)->generic.ctrl->generic.handler != NULL) \ |
93ba25f8 |
124 | (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\ |
8a7e67ec |
125 | (mcs)->data, (event)); \ |
126 | } while (0) |
127 | |
93ba25f8 |
128 | #define findbyctrl(mcs, ctrl) \ |
129 | find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find) |
130 | |
8a7e67ec |
131 | static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *, |
132 | WindowPtr, struct macctrls *); |
ee10bc56 |
133 | static void macctrl_switchtopanel(struct macctrls *, unsigned int); |
a460b361 |
134 | static void macctrl_setfocus(struct macctrls *, union macctrl *); |
8a7e67ec |
135 | static void macctrl_text(struct macctrls *, WindowPtr, |
136 | struct mac_layoutstate *, union control *); |
1f1ec0e5 |
137 | static void macctrl_editbox(struct macctrls *, WindowPtr, |
138 | struct mac_layoutstate *, union control *); |
8a7e67ec |
139 | static void macctrl_radio(struct macctrls *, WindowPtr, |
140 | struct mac_layoutstate *, union control *); |
141 | static void macctrl_checkbox(struct macctrls *, WindowPtr, |
142 | struct mac_layoutstate *, union control *); |
143 | static void macctrl_button(struct macctrls *, WindowPtr, |
144 | struct mac_layoutstate *, union control *); |
7d77d56d |
145 | static void macctrl_listbox(struct macctrls *, WindowPtr, |
146 | struct mac_layoutstate *, union control *); |
56c01970 |
147 | static void macctrl_popup(struct macctrls *, WindowPtr, |
148 | struct mac_layoutstate *, union control *); |
fa4942e7 |
149 | #if !TARGET_API_MAC_CARBON |
0e9ce565 |
150 | static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef, |
151 | ControlDefProcMessage, SInt32); |
5537f572 |
152 | static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef, |
153 | ControlDefProcMessage, SInt32); |
4bd083f7 |
154 | static pascal SInt32 macctrl_sys7_listbox_cdef(SInt16, ControlRef, |
155 | ControlDefProcMessage, SInt32); |
fa4942e7 |
156 | #endif |
157 | |
158 | #if !TARGET_API_MAC_CARBON |
159 | /* |
160 | * This trick enables us to keep all the CDEF code in the main |
161 | * application, which makes life easier. For details, see |
162 | * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>. |
163 | */ |
164 | |
165 | #pragma options align=mac68k |
166 | typedef struct { |
167 | short jmpabs; /* 4EF9 */ |
168 | ControlDefUPP theUPP; |
169 | } **PatchCDEF; |
170 | #pragma options align=reset |
171 | #endif |
172 | |
173 | static void macctrl_init() |
174 | { |
175 | #if !TARGET_API_MAC_CARBON |
176 | static int inited = 0; |
177 | PatchCDEF cdef; |
178 | |
179 | if (inited) return; |
0e9ce565 |
180 | cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox); |
181 | (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef); |
5537f572 |
182 | cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default); |
183 | (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef); |
4bd083f7 |
184 | cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_ListBox); |
185 | (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_listbox_cdef); |
fa4942e7 |
186 | inited = 1; |
187 | #endif |
188 | } |
189 | |
8a7e67ec |
190 | |
191 | static int macctrl_cmp_byctrl(void *av, void *bv) |
192 | { |
193 | union macctrl *a = (union macctrl *)av; |
194 | union macctrl *b = (union macctrl *)bv; |
195 | |
196 | if (a->generic.ctrl < b->generic.ctrl) |
197 | return -1; |
198 | else if (a->generic.ctrl > b->generic.ctrl) |
199 | return +1; |
200 | else |
201 | return 0; |
202 | } |
203 | |
93ba25f8 |
204 | static int macctrl_cmp_byctrl_find(void *av, void *bv) |
205 | { |
206 | union control *a = (union control *)av; |
207 | union macctrl *b = (union macctrl *)bv; |
208 | |
209 | if (a < b->generic.ctrl) |
210 | return -1; |
211 | else if (a > b->generic.ctrl) |
212 | return +1; |
213 | else |
214 | return 0; |
215 | } |
216 | |
d50c2df3 |
217 | static union control panellist; |
218 | |
219 | static void panellist_handler(union control *ctrl, void *dlg, void *data, |
220 | int event) |
221 | { |
222 | struct macctrls *mcs = dlg; |
223 | |
224 | if (event == EVENT_SELCHANGE) |
225 | macctrl_switchtopanel(mcs, dlg_listbox_index(ctrl, dlg) + 1); |
226 | } |
227 | |
8a7e67ec |
228 | void macctrl_layoutbox(struct controlbox *cb, WindowPtr window, |
229 | struct macctrls *mcs) |
230 | { |
231 | int i; |
232 | struct mac_layoutstate curstate; |
233 | ControlRef root; |
234 | Rect rect; |
fa4942e7 |
235 | |
236 | macctrl_init(); |
d50c2df3 |
237 | if (mac_gestalts.apprvers >= 0x100) |
238 | CreateRootControl(window, &root); |
8a7e67ec |
239 | #if TARGET_API_MAC_CARBON |
240 | GetPortBounds(GetWindowPort(window), &rect); |
241 | #else |
242 | rect = window->portRect; |
243 | #endif |
1f1ec0e5 |
244 | mcs->window = window; |
8a7e67ec |
245 | mcs->byctrl = newtree234(macctrl_cmp_byctrl); |
a460b361 |
246 | mcs->focus = NULL; |
135c0f1a |
247 | mcs->defbutton = NULL; |
248 | mcs->canbutton = NULL; |
ee10bc56 |
249 | /* Count the number of panels */ |
250 | mcs->npanels = 1; |
251 | for (i = 1; i < cb->nctrlsets; i++) |
252 | if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname)) |
253 | mcs->npanels++; |
34773273 |
254 | mcs->panels = snewn(mcs->npanels, union macctrl *); |
ee10bc56 |
255 | memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels); |
256 | curstate.panelnum = 0; |
d50c2df3 |
257 | |
258 | curstate.pos.h = rect.left + 13; |
259 | curstate.pos.v = rect.top + 13; |
260 | curstate.width = 160; |
261 | panellist.listbox.type = CTRL_LISTBOX; |
262 | panellist.listbox.handler = &panellist_handler; |
8b1065b5 |
263 | panellist.listbox.height = 20; |
d50c2df3 |
264 | panellist.listbox.percentwidth = 100; |
265 | macctrl_listbox(mcs, window, &curstate, &panellist); |
266 | |
267 | curstate.pos.h = rect.left + 13 + 160 + 13; |
268 | curstate.pos.v = rect.bottom - 33; |
269 | curstate.width = rect.right - (rect.left + 13 + 160) - (13 * 2); |
ee10bc56 |
270 | for (i = 0; i < cb->nctrlsets; i++) { |
271 | if (i > 0 && strcmp(cb->ctrlsets[i]->pathname, |
272 | cb->ctrlsets[i-1]->pathname)) { |
273 | curstate.pos.v = rect.top + 13; |
274 | curstate.panelnum++; |
275 | assert(curstate.panelnum < mcs->npanels); |
d50c2df3 |
276 | dlg_listbox_add(&panellist, mcs, cb->ctrlsets[i]->pathname); |
ee10bc56 |
277 | } |
8a7e67ec |
278 | macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs); |
ee10bc56 |
279 | } |
4bd083f7 |
280 | macctrl_switchtopanel(mcs, 1); |
281 | /* 14 = proxies, 19 = portfwd, 20 = SSH bugs */ |
8a7e67ec |
282 | } |
283 | |
47c65db4 |
284 | #define MAXCOLS 16 |
285 | |
8a7e67ec |
286 | static void macctrl_layoutset(struct mac_layoutstate *curstate, |
287 | struct controlset *s, |
288 | WindowPtr window, struct macctrls *mcs) |
289 | { |
47c65db4 |
290 | unsigned int i, j, ncols, colstart; |
291 | struct mac_layoutstate cols[MAXCOLS]; |
8a7e67ec |
292 | |
47c65db4 |
293 | cols[0] = *curstate; |
294 | ncols = 1; |
8a7e67ec |
295 | |
296 | for (i = 0; i < s->ncontrols; i++) { |
297 | union control *ctrl = s->ctrls[i]; |
8a7e67ec |
298 | |
47c65db4 |
299 | colstart = COLUMN_START(ctrl->generic.column); |
8a7e67ec |
300 | switch (ctrl->generic.type) { |
47c65db4 |
301 | case CTRL_COLUMNS: |
302 | if (ctrl->columns.ncols != 1) { |
303 | ncols = ctrl->columns.ncols; |
47c65db4 |
304 | assert(ncols <= MAXCOLS); |
305 | for (j = 0; j < ncols; j++) { |
306 | cols[j] = cols[0]; |
307 | if (j > 0) |
308 | cols[j].pos.h = cols[j-1].pos.h + cols[j-1].width + 6; |
309 | if (j == ncols - 1) |
310 | cols[j].width = curstate->width - |
311 | (cols[j].pos.h - curstate->pos.h); |
312 | else |
313 | cols[j].width = (curstate->width + 6) * |
314 | ctrl->columns.percentages[j] / 100 - 6; |
315 | } |
316 | } else { |
47c65db4 |
317 | for (j = 0; j < ncols; j++) |
318 | if (cols[j].pos.v > cols[0].pos.v) |
319 | cols[0].pos.v = cols[j].pos.v; |
320 | cols[0].width = curstate->width; |
321 | ncols = 1; |
322 | } |
323 | break; |
8a7e67ec |
324 | case CTRL_TEXT: |
47c65db4 |
325 | macctrl_text(mcs, window, &cols[colstart], ctrl); |
8a7e67ec |
326 | break; |
1f1ec0e5 |
327 | case CTRL_EDITBOX: |
47c65db4 |
328 | macctrl_editbox(mcs, window, &cols[colstart], ctrl); |
1f1ec0e5 |
329 | break; |
8a7e67ec |
330 | case CTRL_RADIO: |
47c65db4 |
331 | macctrl_radio(mcs, window, &cols[colstart], ctrl); |
8a7e67ec |
332 | break; |
333 | case CTRL_CHECKBOX: |
47c65db4 |
334 | macctrl_checkbox(mcs, window, &cols[colstart], ctrl); |
8a7e67ec |
335 | break; |
336 | case CTRL_BUTTON: |
47c65db4 |
337 | macctrl_button(mcs, window, &cols[colstart], ctrl); |
8a7e67ec |
338 | break; |
56c01970 |
339 | case CTRL_LISTBOX: |
340 | if (ctrl->listbox.height == 0) |
47c65db4 |
341 | macctrl_popup(mcs, window, &cols[colstart], ctrl); |
7d77d56d |
342 | else |
343 | macctrl_listbox(mcs, window, &cols[colstart], ctrl); |
56c01970 |
344 | break; |
8a7e67ec |
345 | } |
346 | } |
47c65db4 |
347 | for (j = 0; j < ncols; j++) |
348 | if (cols[j].pos.v > curstate->pos.v) |
349 | curstate->pos.v = cols[j].pos.v; |
8a7e67ec |
350 | } |
351 | |
ee10bc56 |
352 | static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which) |
353 | { |
354 | unsigned int i, j; |
355 | union macctrl *mc; |
356 | |
0a33efe1 |
357 | #define hideshow(c) do { \ |
358 | if (i == which) ShowControl(c); else HideControl(c); \ |
359 | } while (0) |
360 | |
361 | mcs->curpanel = which; |
ee10bc56 |
362 | /* Panel 0 is special and always visible. */ |
363 | for (i = 1; i < mcs->npanels; i++) |
a460b361 |
364 | for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) { |
365 | #if !TARGET_API_MAC_CARBON |
366 | if (mcs->focus == mc) |
367 | macctrl_setfocus(mcs, NULL); |
368 | #endif |
ee10bc56 |
369 | switch (mc->generic.type) { |
370 | case MACCTRL_TEXT: |
0a33efe1 |
371 | hideshow(mc->text.tbctrl); |
ee10bc56 |
372 | break; |
1f1ec0e5 |
373 | case MACCTRL_EDITBOX: |
374 | hideshow(mc->editbox.tbctrl); |
0b89e7b2 |
375 | if (mc->editbox.tblabel != NULL) |
376 | hideshow(mc->editbox.tblabel); |
1f1ec0e5 |
377 | break; |
ee10bc56 |
378 | case MACCTRL_RADIO: |
379 | for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++) |
0a33efe1 |
380 | hideshow(mc->radio.tbctrls[j]); |
0b89e7b2 |
381 | if (mc->radio.tblabel != NULL) |
382 | hideshow(mc->radio.tblabel); |
ee10bc56 |
383 | break; |
384 | case MACCTRL_CHECKBOX: |
0a33efe1 |
385 | hideshow(mc->checkbox.tbctrl); |
ee10bc56 |
386 | break; |
387 | case MACCTRL_BUTTON: |
0a33efe1 |
388 | hideshow(mc->button.tbctrl); |
389 | break; |
7d77d56d |
390 | case MACCTRL_LISTBOX: |
391 | hideshow(mc->listbox.tbctrl); |
392 | /* |
393 | * At least under Mac OS 8.1, hiding a list box |
394 | * doesn't hide its scroll bars. |
395 | */ |
7d77d56d |
396 | #if TARGET_API_MAC_CARBON |
4bd083f7 |
397 | hideshow(GetListVerticalScrollBar(mc->listbox.list)); |
7d77d56d |
398 | #else |
4bd083f7 |
399 | hideshow((*mc->listbox.list)->vScroll); |
7d77d56d |
400 | #endif |
7d77d56d |
401 | break; |
0a33efe1 |
402 | case MACCTRL_POPUP: |
403 | hideshow(mc->popup.tbctrl); |
ee10bc56 |
404 | break; |
ee10bc56 |
405 | } |
a460b361 |
406 | } |
407 | } |
408 | |
409 | #if !TARGET_API_MAC_CARBON |
410 | /* |
411 | * System 7 focus manipulation |
412 | */ |
413 | static void macctrl_defocus(union macctrl *mc) |
414 | { |
415 | |
416 | assert(mac_gestalts.apprvers < 0x100); |
417 | switch (mc->generic.type) { |
418 | case MACCTRL_EDITBOX: |
419 | TEDeactivate((TEHandle)(*mc->editbox.tbctrl)->contrlData); |
420 | break; |
421 | } |
422 | } |
423 | |
424 | static void macctrl_enfocus(union macctrl *mc) |
425 | { |
426 | |
427 | assert(mac_gestalts.apprvers < 0x100); |
428 | switch (mc->generic.type) { |
429 | case MACCTRL_EDITBOX: |
430 | TEActivate((TEHandle)(*mc->editbox.tbctrl)->contrlData); |
431 | break; |
432 | } |
ee10bc56 |
433 | } |
434 | |
a460b361 |
435 | static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc) |
436 | { |
437 | |
84e1c2a9 |
438 | if (mcs->focus == mc) |
439 | return; |
a460b361 |
440 | if (mcs->focus != NULL) |
441 | macctrl_defocus(mcs->focus); |
442 | mcs->focus = mc; |
443 | if (mc != NULL) |
444 | macctrl_enfocus(mc); |
445 | } |
446 | #endif |
447 | |
8a7e67ec |
448 | static void macctrl_text(struct macctrls *mcs, WindowPtr window, |
449 | struct mac_layoutstate *curstate, |
450 | union control *ctrl) |
451 | { |
34773273 |
452 | union macctrl *mc = snew(union macctrl); |
8a7e67ec |
453 | Rect bounds; |
f28d33e8 |
454 | SInt16 height; |
8a7e67ec |
455 | |
0b89e7b2 |
456 | assert(ctrl->text.label != NULL); |
8a7e67ec |
457 | mc->generic.type = MACCTRL_TEXT; |
458 | mc->generic.ctrl = ctrl; |
1f1ec0e5 |
459 | mc->generic.privdata = NULL; |
8a7e67ec |
460 | bounds.left = curstate->pos.h; |
461 | bounds.right = bounds.left + curstate->width; |
462 | bounds.top = curstate->pos.v; |
463 | bounds.bottom = bounds.top + 16; |
464 | if (mac_gestalts.apprvers >= 0x100) { |
8a7e67ec |
465 | Size olen; |
466 | |
467 | mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0, |
468 | kControlStaticTextProc, (long)mc); |
469 | SetControlData(mc->text.tbctrl, kControlEntireControl, |
470 | kControlStaticTextTextTag, |
471 | strlen(ctrl->text.label), ctrl->text.label); |
472 | GetControlData(mc->text.tbctrl, kControlEntireControl, |
473 | kControlStaticTextTextHeightTag, |
474 | sizeof(height), &height, &olen); |
f28d33e8 |
475 | } |
476 | #if !TARGET_API_MAC_CARBON |
477 | else { |
478 | TEHandle te; |
fa4942e7 |
479 | |
f28d33e8 |
480 | mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0, |
fa4942e7 |
481 | SYS7_TEXT_PROC, (long)mc); |
f28d33e8 |
482 | te = (TEHandle)(*mc->text.tbctrl)->contrlData; |
483 | TESetText(ctrl->text.label, strlen(ctrl->text.label), te); |
484 | height = TEGetHeight(1, (*te)->nLines, te); |
8a7e67ec |
485 | } |
f28d33e8 |
486 | #endif |
f28d33e8 |
487 | SizeControl(mc->text.tbctrl, curstate->width, height); |
488 | curstate->pos.v += height + 6; |
8a7e67ec |
489 | add234(mcs->byctrl, mc); |
ee10bc56 |
490 | mc->generic.next = mcs->panels[curstate->panelnum]; |
491 | mcs->panels[curstate->panelnum] = mc; |
8a7e67ec |
492 | } |
493 | |
1f1ec0e5 |
494 | static void macctrl_editbox(struct macctrls *mcs, WindowPtr window, |
495 | struct mac_layoutstate *curstate, |
496 | union control *ctrl) |
497 | { |
34773273 |
498 | union macctrl *mc = snew(union macctrl); |
cd3e71e8 |
499 | Rect lbounds, bounds; |
1f1ec0e5 |
500 | |
1f1ec0e5 |
501 | mc->generic.type = MACCTRL_EDITBOX; |
502 | mc->generic.ctrl = ctrl; |
503 | mc->generic.privdata = NULL; |
cd3e71e8 |
504 | lbounds.left = curstate->pos.h; |
505 | lbounds.top = curstate->pos.v; |
506 | if (ctrl->editbox.percentwidth == 100) { |
0b89e7b2 |
507 | if (ctrl->editbox.label != NULL) { |
508 | lbounds.right = lbounds.left + curstate->width; |
509 | lbounds.bottom = lbounds.top + 16; |
510 | curstate->pos.v += 18; |
511 | } |
cd3e71e8 |
512 | bounds.left = curstate->pos.h; |
513 | bounds.right = bounds.left + curstate->width; |
cd3e71e8 |
514 | } else { |
515 | lbounds.right = lbounds.left + |
516 | curstate->width * (100 - ctrl->editbox.percentwidth) / 100; |
517 | lbounds.bottom = lbounds.top + 22; |
518 | bounds.left = lbounds.right; |
519 | bounds.right = lbounds.left + curstate->width; |
520 | } |
0e9ce565 |
521 | bounds.top = curstate->pos.v; |
522 | bounds.bottom = bounds.top + 22; |
1f1ec0e5 |
523 | if (mac_gestalts.apprvers >= 0x100) { |
0b89e7b2 |
524 | if (ctrl->editbox.label == NULL) |
525 | mc->editbox.tblabel = NULL; |
526 | else { |
527 | mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE, |
528 | 0, 0, 0, kControlStaticTextProc, |
529 | (long)mc); |
530 | SetControlData(mc->editbox.tblabel, kControlEntireControl, |
531 | kControlStaticTextTextTag, |
532 | strlen(ctrl->editbox.label), ctrl->editbox.label); |
533 | } |
95b1a3e4 |
534 | InsetRect(&bounds, 3, 3); |
cd3e71e8 |
535 | mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0, |
536 | ctrl->editbox.password ? |
537 | kControlEditTextPasswordProc : |
538 | kControlEditTextProc, (long)mc); |
f28d33e8 |
539 | } |
540 | #if !TARGET_API_MAC_CARBON |
541 | else { |
0b89e7b2 |
542 | if (ctrl->editbox.label == NULL) |
543 | mc->editbox.tblabel = NULL; |
544 | else { |
545 | mc->editbox.tblabel = NewControl(window, &lbounds, NULL, TRUE, |
546 | 0, 0, 0, SYS7_TEXT_PROC, |
547 | (long)mc); |
548 | TESetText(ctrl->editbox.label, strlen(ctrl->editbox.label), |
549 | (TEHandle)(*mc->editbox.tblabel)->contrlData); |
550 | } |
cd3e71e8 |
551 | mc->editbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0, |
552 | SYS7_EDITBOX_PROC, (long)mc); |
1f1ec0e5 |
553 | } |
f28d33e8 |
554 | #endif |
0e9ce565 |
555 | curstate->pos.v += 28; |
556 | add234(mcs->byctrl, mc); |
557 | mc->generic.next = mcs->panels[curstate->panelnum]; |
558 | mcs->panels[curstate->panelnum] = mc; |
559 | ctrlevent(mcs, mc, EVENT_REFRESH); |
1f1ec0e5 |
560 | } |
561 | |
0e9ce565 |
562 | #if !TARGET_API_MAC_CARBON |
563 | static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant, |
564 | ControlRef control, |
565 | ControlDefProcMessage msg, |
566 | SInt32 param) |
567 | { |
568 | RgnHandle rgn; |
569 | Rect rect; |
570 | TEHandle te; |
571 | long ssfs; |
a460b361 |
572 | Point mouse; |
0e9ce565 |
573 | |
574 | switch (msg) { |
575 | case initCntl: |
576 | rect = (*control)->contrlRect; |
f28d33e8 |
577 | if (variant == SYS7_EDITBOX_VARIANT) |
578 | InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */ |
0e9ce565 |
579 | te = TENew(&rect, &rect); |
580 | ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize); |
581 | (*te)->txSize = LoWord(ssfs); |
582 | (*te)->txFont = HiWord(ssfs); |
583 | (*control)->contrlData = (Handle)te; |
584 | return noErr; |
585 | case dispCntl: |
586 | TEDispose((TEHandle)(*control)->contrlData); |
587 | return 0; |
588 | case drawCntl: |
589 | if ((*control)->contrlVis) { |
590 | rect = (*control)->contrlRect; |
f28d33e8 |
591 | if (variant == SYS7_EDITBOX_VARIANT) { |
592 | PenNormal(); |
593 | FrameRect(&rect); |
594 | InsetRect(&rect, 3, 3); |
595 | } |
8b1065b5 |
596 | EraseRect(&rect); |
f28d33e8 |
597 | (*(TEHandle)(*control)->contrlData)->viewRect = rect; |
0e9ce565 |
598 | TEUpdate(&rect, (TEHandle)(*control)->contrlData); |
599 | } |
600 | return 0; |
a460b361 |
601 | case testCntl: |
f28d33e8 |
602 | if (variant == SYS7_TEXT_VARIANT) |
603 | return kControlNoPart; |
a460b361 |
604 | mouse.h = LoWord(param); |
605 | mouse.v = HiWord(param); |
f28d33e8 |
606 | rect = (*control)->contrlRect; |
607 | InsetRect(&rect, 3, 3); |
608 | return PtInRect(mouse, &rect) ? kControlEditTextPart : kControlNoPart; |
0e9ce565 |
609 | case calcCRgns: |
610 | if (param & (1 << 31)) { |
611 | param &= ~(1 << 31); |
612 | goto calcthumbrgn; |
613 | } |
614 | /* FALLTHROUGH */ |
615 | case calcCntlRgn: |
616 | rgn = (RgnHandle)param; |
617 | RectRgn(rgn, &(*control)->contrlRect); |
618 | return 0; |
619 | case calcThumbRgn: |
620 | calcthumbrgn: |
621 | rgn = (RgnHandle)param; |
622 | SetEmptyRgn(rgn); |
623 | return 0; |
624 | } |
625 | |
626 | return 0; |
627 | } |
628 | #endif |
629 | |
8a7e67ec |
630 | static void macctrl_radio(struct macctrls *mcs, WindowPtr window, |
631 | struct mac_layoutstate *curstate, |
632 | union control *ctrl) |
633 | { |
34773273 |
634 | union macctrl *mc = snew(union macctrl); |
8a7e67ec |
635 | Rect bounds; |
636 | Str255 title; |
637 | unsigned int i, colwidth; |
638 | |
8a7e67ec |
639 | mc->generic.type = MACCTRL_RADIO; |
640 | mc->generic.ctrl = ctrl; |
1f1ec0e5 |
641 | mc->generic.privdata = NULL; |
34773273 |
642 | mc->radio.tbctrls = snewn(ctrl->radio.nbuttons, ControlRef); |
8a7e67ec |
643 | colwidth = (curstate->width + 13) / ctrl->radio.ncolumns; |
cd3e71e8 |
644 | bounds.top = curstate->pos.v; |
645 | bounds.bottom = bounds.top + 16; |
646 | bounds.left = curstate->pos.h; |
647 | bounds.right = bounds.left + curstate->width; |
0b89e7b2 |
648 | if (ctrl->radio.label == NULL) |
649 | mc->radio.tblabel = NULL; |
f28d33e8 |
650 | else { |
0b89e7b2 |
651 | if (mac_gestalts.apprvers >= 0x100) { |
652 | mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE, |
653 | 0, 0, 0, kControlStaticTextProc, |
654 | (long)mc); |
655 | SetControlData(mc->radio.tblabel, kControlEntireControl, |
656 | kControlStaticTextTextTag, |
657 | strlen(ctrl->radio.label), ctrl->radio.label); |
658 | } |
659 | #if !TARGET_API_MAC_CARBON |
660 | else { |
661 | mc->radio.tblabel = NewControl(window, &bounds, NULL, TRUE, |
662 | 0, 0, 0, SYS7_TEXT_PROC, (long)mc); |
663 | TESetText(ctrl->radio.label, strlen(ctrl->radio.label), |
664 | (TEHandle)(*mc->radio.tblabel)->contrlData); |
665 | } |
f28d33e8 |
666 | #endif |
0b89e7b2 |
667 | curstate->pos.v += 18; |
668 | } |
8a7e67ec |
669 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
35790008 |
670 | bounds.top = curstate->pos.v - 2; |
671 | bounds.bottom = bounds.top + 18; |
8a7e67ec |
672 | bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns); |
673 | if (i == ctrl->radio.nbuttons - 1 || |
674 | i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) { |
675 | bounds.right = curstate->pos.h + curstate->width; |
35790008 |
676 | curstate->pos.v += 18; |
8a7e67ec |
677 | } else |
678 | bounds.right = bounds.left + colwidth - 13; |
679 | c2pstrcpy(title, ctrl->radio.buttons[i]); |
680 | mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE, |
681 | 0, 0, 1, radioButProc, (long)mc); |
682 | } |
35790008 |
683 | curstate->pos.v += 4; |
8a7e67ec |
684 | add234(mcs->byctrl, mc); |
ee10bc56 |
685 | mc->generic.next = mcs->panels[curstate->panelnum]; |
686 | mcs->panels[curstate->panelnum] = mc; |
8a7e67ec |
687 | ctrlevent(mcs, mc, EVENT_REFRESH); |
688 | } |
689 | |
690 | static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window, |
691 | struct mac_layoutstate *curstate, |
692 | union control *ctrl) |
693 | { |
34773273 |
694 | union macctrl *mc = snew(union macctrl); |
8a7e67ec |
695 | Rect bounds; |
696 | Str255 title; |
697 | |
0b89e7b2 |
698 | assert(ctrl->checkbox.label != NULL); |
8a7e67ec |
699 | mc->generic.type = MACCTRL_CHECKBOX; |
700 | mc->generic.ctrl = ctrl; |
1f1ec0e5 |
701 | mc->generic.privdata = NULL; |
8a7e67ec |
702 | bounds.left = curstate->pos.h; |
703 | bounds.right = bounds.left + curstate->width; |
704 | bounds.top = curstate->pos.v; |
705 | bounds.bottom = bounds.top + 16; |
706 | c2pstrcpy(title, ctrl->checkbox.label); |
707 | mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1, |
708 | checkBoxProc, (long)mc); |
709 | add234(mcs->byctrl, mc); |
710 | curstate->pos.v += 22; |
ee10bc56 |
711 | mc->generic.next = mcs->panels[curstate->panelnum]; |
712 | mcs->panels[curstate->panelnum] = mc; |
8a7e67ec |
713 | ctrlevent(mcs, mc, EVENT_REFRESH); |
714 | } |
715 | |
716 | static void macctrl_button(struct macctrls *mcs, WindowPtr window, |
717 | struct mac_layoutstate *curstate, |
718 | union control *ctrl) |
719 | { |
34773273 |
720 | union macctrl *mc = snew(union macctrl); |
8a7e67ec |
721 | Rect bounds; |
722 | Str255 title; |
723 | |
0b89e7b2 |
724 | assert(ctrl->button.label != NULL); |
8a7e67ec |
725 | mc->generic.type = MACCTRL_BUTTON; |
726 | mc->generic.ctrl = ctrl; |
1f1ec0e5 |
727 | mc->generic.privdata = NULL; |
8a7e67ec |
728 | bounds.left = curstate->pos.h; |
47c65db4 |
729 | bounds.right = bounds.left + curstate->width; |
8a7e67ec |
730 | bounds.top = curstate->pos.v; |
731 | bounds.bottom = bounds.top + 20; |
732 | c2pstrcpy(title, ctrl->button.label); |
733 | mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1, |
734 | pushButProc, (long)mc); |
457f0b8f |
735 | mc->button.tbring = NULL; |
8a7e67ec |
736 | if (mac_gestalts.apprvers >= 0x100) { |
737 | Boolean isdefault = ctrl->button.isdefault; |
738 | |
739 | SetControlData(mc->button.tbctrl, kControlEntireControl, |
740 | kControlPushButtonDefaultTag, |
741 | sizeof(isdefault), &isdefault); |
5537f572 |
742 | } else if (ctrl->button.isdefault) { |
743 | InsetRect(&bounds, -4, -4); |
457f0b8f |
744 | mc->button.tbring = NewControl(window, &bounds, title, TRUE, 0, 0, 1, |
745 | SYS7_DEFAULT_PROC, (long)mc); |
8a7e67ec |
746 | } |
855d05c4 |
747 | if (mac_gestalts.apprvers >= 0x110) { |
748 | Boolean iscancel = ctrl->button.iscancel; |
749 | |
750 | SetControlData(mc->button.tbctrl, kControlEntireControl, |
751 | kControlPushButtonCancelTag, |
752 | sizeof(iscancel), &iscancel); |
753 | } |
135c0f1a |
754 | if (ctrl->button.isdefault) |
755 | mcs->defbutton = mc; |
756 | if (ctrl->button.iscancel) |
757 | mcs->canbutton = mc; |
8a7e67ec |
758 | add234(mcs->byctrl, mc); |
ee10bc56 |
759 | mc->generic.next = mcs->panels[curstate->panelnum]; |
760 | mcs->panels[curstate->panelnum] = mc; |
8a7e67ec |
761 | curstate->pos.v += 26; |
762 | } |
763 | |
5537f572 |
764 | #if !TARGET_API_MAC_CARBON |
765 | static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant, |
766 | ControlRef control, |
767 | ControlDefProcMessage msg, |
768 | SInt32 param) |
769 | { |
770 | RgnHandle rgn; |
771 | Rect rect; |
772 | int oval; |
457f0b8f |
773 | PenState savestate; |
5537f572 |
774 | |
775 | switch (msg) { |
776 | case drawCntl: |
777 | if ((*control)->contrlVis) { |
778 | rect = (*control)->contrlRect; |
457f0b8f |
779 | GetPenState(&savestate); |
5537f572 |
780 | PenNormal(); |
781 | PenSize(3, 3); |
457f0b8f |
782 | if ((*control)->contrlHilite == kControlInactivePart) |
783 | PenPat(&qd.gray); |
5537f572 |
784 | oval = (rect.bottom - rect.top) / 2 + 2; |
785 | FrameRoundRect(&rect, oval, oval); |
457f0b8f |
786 | SetPenState(&savestate); |
5537f572 |
787 | } |
788 | return 0; |
789 | case calcCRgns: |
790 | if (param & (1 << 31)) { |
791 | param &= ~(1 << 31); |
792 | goto calcthumbrgn; |
793 | } |
794 | /* FALLTHROUGH */ |
795 | case calcCntlRgn: |
796 | rgn = (RgnHandle)param; |
797 | RectRgn(rgn, &(*control)->contrlRect); |
798 | return 0; |
799 | case calcThumbRgn: |
800 | calcthumbrgn: |
801 | rgn = (RgnHandle)param; |
802 | SetEmptyRgn(rgn); |
803 | return 0; |
804 | } |
805 | |
806 | return 0; |
807 | } |
808 | #endif |
809 | |
7d77d56d |
810 | static void macctrl_listbox(struct macctrls *mcs, WindowPtr window, |
811 | struct mac_layoutstate *curstate, |
812 | union control *ctrl) |
813 | { |
814 | union macctrl *mc = snew(union macctrl); |
815 | Rect bounds; |
7d77d56d |
816 | Size olen; |
817 | |
7d77d56d |
818 | assert(ctrl->listbox.percentwidth == 100); |
819 | mc->generic.type = MACCTRL_LISTBOX; |
820 | mc->generic.ctrl = ctrl; |
821 | mc->generic.privdata = NULL; |
822 | /* The list starts off empty */ |
823 | mc->listbox.nids = 0; |
824 | mc->listbox.ids = NULL; |
825 | bounds.left = curstate->pos.h; |
826 | bounds.right = bounds.left + curstate->width; |
827 | bounds.top = curstate->pos.v; |
8b1065b5 |
828 | bounds.bottom = bounds.top + 16 * ctrl->listbox.height + 2; |
7d77d56d |
829 | |
4bd083f7 |
830 | if (mac_gestalts.apprvers >= 0x100) { |
831 | InsetRect(&bounds, 3, 3); |
832 | mc->listbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, |
833 | ldes_Default, 0, 0, |
834 | kControlListBoxProc, (long)mc); |
7d77d56d |
835 | if (GetControlData(mc->listbox.tbctrl, kControlEntireControl, |
4bd083f7 |
836 | kControlListBoxListHandleTag, |
837 | sizeof(mc->listbox.list), &mc->listbox.list, |
838 | &olen) != noErr) { |
839 | DisposeControl(mc->listbox.tbctrl); |
840 | sfree(mc); |
7d77d56d |
841 | return; |
4bd083f7 |
842 | } |
843 | } |
844 | #if !TARGET_API_MAC_CARBON |
845 | else { |
846 | InsetRect(&bounds, -3, -3); |
847 | mc->listbox.tbctrl = NewControl(window, &bounds, NULL, TRUE, |
848 | 0, 0, 0, |
849 | SYS7_LISTBOX_PROC, (long)mc); |
850 | mc->listbox.list = (ListHandle)(*mc->listbox.tbctrl)->contrlData; |
851 | (*mc->listbox.list)->refCon = (long)mc; |
852 | } |
853 | #endif |
854 | if (!ctrl->listbox.multisel) { |
7d77d56d |
855 | #if TARGET_API_MAC_CARBON |
4bd083f7 |
856 | SetListSelectionFlags(mc->listbox.list, lOnlyOne); |
7d77d56d |
857 | #else |
4bd083f7 |
858 | (*mc->listbox.list)->selFlags = lOnlyOne; |
7d77d56d |
859 | #endif |
860 | } |
861 | add234(mcs->byctrl, mc); |
08a58231 |
862 | curstate->pos.v += 6 + 16 * ctrl->listbox.height + 2; |
7d77d56d |
863 | mc->generic.next = mcs->panels[curstate->panelnum]; |
864 | mcs->panels[curstate->panelnum] = mc; |
865 | ctrlevent(mcs, mc, EVENT_REFRESH); |
866 | } |
867 | |
4bd083f7 |
868 | #if !TARGET_API_MAC_CARBON |
869 | static pascal SInt32 macctrl_sys7_listbox_cdef(SInt16 variant, |
870 | ControlRef control, |
871 | ControlDefProcMessage msg, |
872 | SInt32 param) |
873 | { |
874 | RgnHandle rgn; |
875 | Rect rect; |
876 | ListHandle list; |
877 | long ssfs; |
878 | Point mouse; |
879 | ListBounds bounds; |
880 | Point csize; |
881 | short savefont; |
882 | short savesize; |
883 | GrafPtr curport; |
884 | |
885 | switch (msg) { |
886 | case initCntl: |
887 | rect = (*control)->contrlRect; |
888 | InsetRect(&rect, 4, 4); |
889 | rect.right -= 15; /* scroll bar */ |
890 | bounds.top = bounds.bottom = bounds.left = 0; |
891 | bounds.right = 1; |
892 | csize.h = csize.v = 0; |
893 | GetPort(&curport); |
894 | savefont = curport->txFont; |
895 | savesize = curport->txSize; |
896 | ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize); |
897 | TextFont(HiWord(ssfs)); |
898 | TextSize(LoWord(ssfs)); |
899 | list = LNew(&rect, &bounds, csize, 0, (*control)->contrlOwner, |
900 | TRUE, FALSE, FALSE, TRUE); |
901 | SetControlReference((*list)->vScroll, (long)list); |
902 | (*control)->contrlData = (Handle)list; |
903 | TextFont(savefont); |
904 | TextSize(savesize); |
905 | return noErr; |
906 | case dispCntl: |
907 | /* |
908 | * If the dialogue box is being destroyed, the scroll bar |
909 | * might have gone already. In our situation, this is the |
910 | * only time we destroy a control, so NULL out the scroll bar |
911 | * handle to prevent LDispose trying to free it. |
912 | */ |
913 | list = (ListHandle)(*control)->contrlData; |
914 | (*list)->vScroll = NULL; |
915 | LDispose(list); |
916 | return 0; |
917 | case drawCntl: |
918 | if ((*control)->contrlVis) { |
919 | rect = (*control)->contrlRect; |
920 | /* XXX input focus highlighting? */ |
921 | InsetRect(&rect, 3, 3); |
922 | PenNormal(); |
923 | FrameRect(&rect); |
924 | list = (ListHandle)(*control)->contrlData; |
925 | LActivate((*control)->contrlHilite != kControlInactivePart, list); |
926 | GetPort(&curport); |
927 | LUpdate(curport->visRgn, list); |
928 | } |
929 | return 0; |
930 | case testCntl: |
931 | mouse.h = LoWord(param); |
932 | mouse.v = HiWord(param); |
933 | rect = (*control)->contrlRect; |
934 | InsetRect(&rect, 4, 4); |
935 | /* |
936 | * We deliberately exclude the scrollbar so that LClick() can see it. |
937 | */ |
938 | rect.right -= 15; |
939 | return PtInRect(mouse, &rect) ? kControlListBoxPart : kControlNoPart; |
940 | case calcCRgns: |
941 | if (param & (1 << 31)) { |
942 | param &= ~(1 << 31); |
943 | goto calcthumbrgn; |
944 | } |
945 | /* FALLTHROUGH */ |
946 | case calcCntlRgn: |
947 | rgn = (RgnHandle)param; |
948 | RectRgn(rgn, &(*control)->contrlRect); |
949 | return 0; |
950 | case calcThumbRgn: |
951 | calcthumbrgn: |
952 | rgn = (RgnHandle)param; |
953 | SetEmptyRgn(rgn); |
954 | return 0; |
955 | } |
956 | |
957 | return 0; |
958 | } |
959 | #endif |
7d77d56d |
960 | |
56c01970 |
961 | static void macctrl_popup(struct macctrls *mcs, WindowPtr window, |
962 | struct mac_layoutstate *curstate, |
963 | union control *ctrl) |
964 | { |
34773273 |
965 | union macctrl *mc = snew(union macctrl); |
56c01970 |
966 | Rect bounds; |
967 | Str255 title; |
968 | unsigned int labelwidth; |
0a33efe1 |
969 | static int nextmenuid = MENU_MIN; |
56c01970 |
970 | int menuid; |
971 | MenuRef menu; |
972 | |
973 | /* |
974 | * <http://developer.apple.com/qa/tb/tb42.html> explains how to |
975 | * create a popup menu with dynamic content. |
976 | */ |
977 | assert(ctrl->listbox.height == 0); |
978 | assert(!ctrl->listbox.draglist); |
979 | assert(!ctrl->listbox.multisel); |
980 | |
56c01970 |
981 | mc->generic.type = MACCTRL_POPUP; |
982 | mc->generic.ctrl = ctrl; |
1f1ec0e5 |
983 | mc->generic.privdata = NULL; |
0b89e7b2 |
984 | c2pstrcpy(title, ctrl->button.label == NULL ? "" : ctrl->button.label); |
56c01970 |
985 | |
986 | /* Find a spare menu ID and create the menu */ |
987 | while (GetMenuHandle(nextmenuid) != NULL) |
988 | if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN; |
989 | menuid = nextmenuid++; |
990 | menu = NewMenu(menuid, "\pdummy"); |
991 | if (menu == NULL) return; |
992 | mc->popup.menu = menu; |
993 | mc->popup.menuid = menuid; |
994 | InsertMenu(menu, kInsertHierarchicalMenu); |
995 | |
996 | /* The menu starts off empty */ |
997 | mc->popup.nids = 0; |
998 | mc->popup.ids = NULL; |
999 | |
1000 | bounds.left = curstate->pos.h; |
1001 | bounds.right = bounds.left + curstate->width; |
1002 | bounds.top = curstate->pos.v; |
1003 | bounds.bottom = bounds.top + 20; |
1004 | /* XXX handle percentwidth == 100 */ |
1005 | labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100; |
1006 | mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE, |
1007 | popupTitleLeftJust, menuid, labelwidth, |
1008 | popupMenuProc + popupFixedWidth, (long)mc); |
1009 | add234(mcs->byctrl, mc); |
1010 | curstate->pos.v += 26; |
1011 | mc->generic.next = mcs->panels[curstate->panelnum]; |
1012 | mcs->panels[curstate->panelnum] = mc; |
1013 | ctrlevent(mcs, mc, EVENT_REFRESH); |
1014 | } |
1015 | |
8a7e67ec |
1016 | |
1017 | void macctrl_activate(WindowPtr window, EventRecord *event) |
1018 | { |
0a33efe1 |
1019 | struct macctrls *mcs = mac_winctrls(window); |
8a7e67ec |
1020 | Boolean active = (event->modifiers & activeFlag) != 0; |
1021 | GrafPtr saveport; |
0a33efe1 |
1022 | int i, j; |
1023 | ControlPartCode state; |
1024 | union macctrl *mc; |
8a7e67ec |
1025 | |
1026 | GetPort(&saveport); |
1027 | SetPort((GrafPtr)GetWindowPort(window)); |
78f015b8 |
1028 | if (mac_gestalts.apprvers >= 0x100) |
8a7e67ec |
1029 | SetThemeWindowBackground(window, active ? |
1030 | kThemeBrushModelessDialogBackgroundActive : |
1031 | kThemeBrushModelessDialogBackgroundInactive, |
1032 | TRUE); |
0a33efe1 |
1033 | state = active ? kControlNoPart : kControlInactivePart; |
1034 | for (i = 0; i <= mcs->curpanel; i += mcs->curpanel) |
f28d33e8 |
1035 | for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) { |
0a33efe1 |
1036 | switch (mc->generic.type) { |
1037 | case MACCTRL_TEXT: |
1038 | HiliteControl(mc->text.tbctrl, state); |
1039 | break; |
1f1ec0e5 |
1040 | case MACCTRL_EDITBOX: |
1041 | HiliteControl(mc->editbox.tbctrl, state); |
0b89e7b2 |
1042 | if (mc->editbox.tblabel != NULL) |
1043 | HiliteControl(mc->editbox.tblabel, state); |
1f1ec0e5 |
1044 | break; |
0a33efe1 |
1045 | case MACCTRL_RADIO: |
1046 | for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++) |
1047 | HiliteControl(mc->radio.tbctrls[j], state); |
0b89e7b2 |
1048 | if (mc->radio.tblabel != NULL) |
1049 | HiliteControl(mc->radio.tblabel, state); |
0a33efe1 |
1050 | break; |
1051 | case MACCTRL_CHECKBOX: |
1052 | HiliteControl(mc->checkbox.tbctrl, state); |
1053 | break; |
1054 | case MACCTRL_BUTTON: |
1055 | HiliteControl(mc->button.tbctrl, state); |
457f0b8f |
1056 | if (mc->button.tbring != NULL) |
1057 | HiliteControl(mc->button.tbring, state); |
0a33efe1 |
1058 | break; |
7d77d56d |
1059 | case MACCTRL_LISTBOX: |
1060 | HiliteControl(mc->listbox.tbctrl, state); |
1061 | break; |
0a33efe1 |
1062 | case MACCTRL_POPUP: |
1063 | HiliteControl(mc->popup.tbctrl, state); |
1064 | break; |
1065 | } |
f28d33e8 |
1066 | #if !TARGET_API_MAC_CARBON |
1067 | if (mcs->focus == mc) { |
1068 | if (active) |
1069 | macctrl_enfocus(mc); |
1070 | else |
1071 | macctrl_defocus(mc); |
1072 | } |
1073 | #endif |
1074 | } |
8a7e67ec |
1075 | SetPort(saveport); |
1076 | } |
1077 | |
1078 | void macctrl_click(WindowPtr window, EventRecord *event) |
1079 | { |
1080 | Point mouse; |
84e1c2a9 |
1081 | ControlHandle control, oldfocus; |
1f1ec0e5 |
1082 | int part, trackresult; |
8a7e67ec |
1083 | GrafPtr saveport; |
1084 | union macctrl *mc; |
1085 | struct macctrls *mcs = mac_winctrls(window); |
1086 | int i; |
1f1ec0e5 |
1087 | UInt32 features; |
8a7e67ec |
1088 | |
1089 | GetPort(&saveport); |
1090 | SetPort((GrafPtr)GetWindowPort(window)); |
1091 | mouse = event->where; |
1092 | GlobalToLocal(&mouse); |
1093 | part = FindControl(mouse, window, &control); |
56c01970 |
1094 | if (control != NULL) { |
4bd083f7 |
1095 | #if !TARGET_API_MAC_CARBON |
1096 | /* |
1097 | * Special magic for scroll bars in list boxes, whose refcon |
1098 | * is the list. |
1099 | */ |
1100 | if (part == kControlUpButtonPart || part == kControlDownButtonPart || |
1101 | part == kControlPageUpPart || part == kControlPageDownPart || |
1102 | part == kControlIndicatorPart) |
1103 | mc = (union macctrl *) |
1104 | (*(ListHandle)GetControlReference(control))->refCon; |
1105 | else |
1106 | #endif |
1107 | mc = (union macctrl *)GetControlReference(control); |
1f1ec0e5 |
1108 | if (mac_gestalts.apprvers >= 0x100) { |
1109 | if (GetControlFeatures(control, &features) == noErr && |
1110 | (features & kControlSupportsFocus) && |
84e1c2a9 |
1111 | (features & kControlGetsFocusOnClick) && |
1112 | GetKeyboardFocus(window, &oldfocus) == noErr && |
1113 | control != oldfocus) |
1f1ec0e5 |
1114 | SetKeyboardFocus(window, control, part); |
1115 | trackresult = HandleControlClick(control, mouse, event->modifiers, |
1116 | (ControlActionUPP)-1); |
a460b361 |
1117 | } else { |
1118 | #if !TARGET_API_MAC_CARBON |
1119 | if (mc->generic.type == MACCTRL_EDITBOX && |
1120 | control == mc->editbox.tbctrl) { |
1121 | TEHandle te = (TEHandle)(*control)->contrlData; |
1122 | |
1123 | macctrl_setfocus(mcs, mc); |
1124 | TEClick(mouse, !!(event->modifiers & shiftKey), te); |
1125 | goto done; |
1126 | } |
4bd083f7 |
1127 | if (mc->generic.type == MACCTRL_LISTBOX && |
1128 | (control == mc->listbox.tbctrl || |
1129 | control == (*mc->listbox.list)->vScroll)) { |
1130 | |
4bd083f7 |
1131 | macctrl_setfocus(mcs, mc); |
1132 | if (LClick(mouse, event->modifiers, mc->listbox.list)) |
1133 | /* double-click */ |
1134 | ctrlevent(mcs, mc, EVENT_ACTION); |
1135 | else |
1136 | ctrlevent(mcs, mc, EVENT_SELCHANGE); |
1137 | goto done; |
1138 | } |
a460b361 |
1139 | #endif |
1f1ec0e5 |
1140 | trackresult = TrackControl(control, mouse, (ControlActionUPP)-1); |
a460b361 |
1141 | } |
56c01970 |
1142 | switch (mc->generic.type) { |
56c01970 |
1143 | case MACCTRL_RADIO: |
1f1ec0e5 |
1144 | if (trackresult != 0) { |
56c01970 |
1145 | for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++) |
8a7e67ec |
1146 | if (mc->radio.tbctrls[i] == control) |
1147 | SetControlValue(mc->radio.tbctrls[i], |
1148 | kControlRadioButtonCheckedValue); |
1149 | else |
1150 | SetControlValue(mc->radio.tbctrls[i], |
1151 | kControlRadioButtonUncheckedValue); |
8a7e67ec |
1152 | ctrlevent(mcs, mc, EVENT_VALCHANGE); |
56c01970 |
1153 | } |
1154 | break; |
1155 | case MACCTRL_CHECKBOX: |
1f1ec0e5 |
1156 | if (trackresult != 0) { |
8a7e67ec |
1157 | SetControlValue(control, !GetControlValue(control)); |
1158 | ctrlevent(mcs, mc, EVENT_VALCHANGE); |
8a7e67ec |
1159 | } |
56c01970 |
1160 | break; |
1161 | case MACCTRL_BUTTON: |
1f1ec0e5 |
1162 | if (trackresult != 0) |
56c01970 |
1163 | ctrlevent(mcs, mc, EVENT_ACTION); |
1164 | break; |
7d77d56d |
1165 | case MACCTRL_LISTBOX: |
1166 | /* FIXME spot double-click */ |
1167 | ctrlevent(mcs, mc, EVENT_SELCHANGE); |
1168 | break; |
1f1ec0e5 |
1169 | case MACCTRL_POPUP: |
1170 | ctrlevent(mcs, mc, EVENT_SELCHANGE); |
1171 | break; |
8a7e67ec |
1172 | } |
56c01970 |
1173 | } |
a460b361 |
1174 | done: |
8a7e67ec |
1175 | SetPort(saveport); |
1176 | } |
1177 | |
1f1ec0e5 |
1178 | void macctrl_key(WindowPtr window, EventRecord *event) |
1179 | { |
1180 | ControlRef control; |
1181 | struct macctrls *mcs = mac_winctrls(window); |
1182 | union macctrl *mc; |
135c0f1a |
1183 | unsigned long dummy; |
1184 | |
1185 | switch (event->message & charCodeMask) { |
1186 | case kEnterCharCode: |
1187 | case kReturnCharCode: |
1188 | if (mcs->defbutton != NULL) { |
1189 | assert(mcs->defbutton->generic.type == MACCTRL_BUTTON); |
1190 | HiliteControl(mcs->defbutton->button.tbctrl, kControlButtonPart); |
1191 | /* |
1192 | * I'd like to delay unhilighting the button until after |
1193 | * the event has been processed, but by them the entire |
1194 | * dialgue box might have been destroyed. |
1195 | */ |
1196 | Delay(6, &dummy); |
1197 | HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart); |
1198 | ctrlevent(mcs, mcs->defbutton, EVENT_ACTION); |
1199 | } |
1200 | return; |
1201 | case kEscapeCharCode: |
1202 | if (mcs->canbutton != NULL) { |
1203 | assert(mcs->canbutton->generic.type == MACCTRL_BUTTON); |
1204 | HiliteControl(mcs->canbutton->button.tbctrl, kControlButtonPart); |
1205 | Delay(6, &dummy); |
1206 | HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart); |
1207 | ctrlevent(mcs, mcs->canbutton, EVENT_ACTION); |
1208 | } |
1209 | return; |
1210 | } |
2dfd068b |
1211 | if (mac_gestalts.apprvers >= 0x100) { |
1212 | if (GetKeyboardFocus(window, &control) == noErr && control != NULL) { |
1213 | HandleControlKey(control, (event->message & keyCodeMask) >> 8, |
1214 | event->message & charCodeMask, event->modifiers); |
1215 | mc = (union macctrl *)GetControlReference(control); |
576d534c |
1216 | switch (mc->generic.type) { |
1217 | case MACCTRL_LISTBOX: |
1218 | ctrlevent(mcs, mc, EVENT_SELCHANGE); |
1219 | break; |
1220 | default: |
1221 | ctrlevent(mcs, mc, EVENT_VALCHANGE); |
1222 | break; |
1223 | } |
2dfd068b |
1224 | } |
47c65db4 |
1225 | } |
1226 | #if !TARGET_API_MAC_CARBON |
1227 | else { |
1228 | TEHandle te; |
1229 | |
2dfd068b |
1230 | if (mcs->focus != NULL) { |
91156e95 |
1231 | mc = mcs->focus; |
1232 | switch (mc->generic.type) { |
2dfd068b |
1233 | case MACCTRL_EDITBOX: |
91156e95 |
1234 | te = (TEHandle)(*mc->editbox.tbctrl)->contrlData; |
2dfd068b |
1235 | TEKey(event->message & charCodeMask, te); |
91156e95 |
1236 | ctrlevent(mcs, mc, EVENT_VALCHANGE); |
2dfd068b |
1237 | break; |
1238 | } |
1239 | } |
1f1ec0e5 |
1240 | } |
47c65db4 |
1241 | #endif |
1f1ec0e5 |
1242 | } |
1243 | |
8a7e67ec |
1244 | void macctrl_update(WindowPtr window) |
1245 | { |
1246 | #if TARGET_API_MAC_CARBON |
1247 | RgnHandle visrgn; |
1248 | #endif |
1249 | Rect rect; |
1250 | GrafPtr saveport; |
1251 | |
1252 | BeginUpdate(window); |
1253 | GetPort(&saveport); |
1254 | SetPort((GrafPtr)GetWindowPort(window)); |
1255 | if (mac_gestalts.apprvers >= 0x101) { |
1256 | #if TARGET_API_MAC_CARBON |
1257 | GetPortBounds(GetWindowPort(window), &rect); |
1258 | #else |
1259 | rect = window->portRect; |
1260 | #endif |
1261 | InsetRect(&rect, -1, -1); |
1262 | DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ? |
1263 | kThemeStateActive : kThemeStateInactive); |
1264 | } |
1265 | #if TARGET_API_MAC_CARBON |
1266 | visrgn = NewRgn(); |
1267 | GetPortVisibleRegion(GetWindowPort(window), visrgn); |
1268 | UpdateControls(window, visrgn); |
1269 | DisposeRgn(visrgn); |
1270 | #else |
1271 | UpdateControls(window, window->visRgn); |
1272 | #endif |
1273 | SetPort(saveport); |
1274 | EndUpdate(window); |
1275 | } |
1276 | |
1277 | #if TARGET_API_MAC_CARBON |
1278 | #define EnableItem EnableMenuItem |
1279 | #define DisableItem DisableMenuItem |
1280 | #endif |
1281 | void macctrl_adjustmenus(WindowPtr window) |
1282 | { |
1283 | MenuHandle menu; |
1284 | |
1285 | menu = GetMenuHandle(mFile); |
1286 | DisableItem(menu, iSave); /* XXX enable if modified */ |
1287 | EnableItem(menu, iSaveAs); |
1288 | EnableItem(menu, iDuplicate); |
1289 | |
1290 | menu = GetMenuHandle(mEdit); |
1291 | DisableItem(menu, 0); |
1292 | } |
1293 | |
1294 | void macctrl_close(WindowPtr window) |
1295 | { |
1296 | struct macctrls *mcs = mac_winctrls(window); |
1297 | union macctrl *mc; |
1298 | |
56c01970 |
1299 | /* |
1300 | * Mostly, we don't bother disposing of the Toolbox controls, |
1301 | * since that will happen automatically when the window is |
1302 | * disposed of. Popup menus are an exception, because we have to |
1303 | * dispose of the menu ourselves, and doing that while the control |
1304 | * still holds a reference to it seems rude. |
1305 | */ |
8a7e67ec |
1306 | while ((mc = index234(mcs->byctrl, 0)) != NULL) { |
1f1ec0e5 |
1307 | if (mc->generic.privdata != NULL && mc->generic.freeprivdata) |
1308 | sfree(mc->generic.privdata); |
56c01970 |
1309 | switch (mc->generic.type) { |
1310 | case MACCTRL_POPUP: |
1311 | DisposeControl(mc->popup.tbctrl); |
1312 | DeleteMenu(mc->popup.menuid); |
1313 | DisposeMenu(mc->popup.menu); |
1314 | break; |
1315 | } |
8a7e67ec |
1316 | del234(mcs->byctrl, mc); |
1317 | sfree(mc); |
1318 | } |
1319 | |
1320 | freetree234(mcs->byctrl); |
1321 | mcs->byctrl = NULL; |
ee10bc56 |
1322 | sfree(mcs->panels); |
1323 | mcs->panels = NULL; |
8a7e67ec |
1324 | } |
1325 | |
1326 | void dlg_update_start(union control *ctrl, void *dlg) |
1327 | { |
1328 | |
1329 | /* No-op for now */ |
1330 | } |
1331 | |
1332 | void dlg_update_done(union control *ctrl, void *dlg) |
1333 | { |
1334 | |
1335 | /* No-op for now */ |
1336 | } |
1337 | |
1338 | void dlg_set_focus(union control *ctrl, void *dlg) |
1339 | { |
1340 | |
1341 | if (mac_gestalts.apprvers >= 0x100) { |
1342 | /* Use SetKeyboardFocus() */ |
1343 | } else { |
1344 | /* Do our own mucking around */ |
1345 | } |
1346 | } |
1347 | |
0bd8d76d |
1348 | union control *dlg_last_focused(union control *ctrl, void *dlg) |
8a7e67ec |
1349 | { |
1350 | |
1351 | return NULL; |
1352 | } |
1353 | |
1354 | void dlg_beep(void *dlg) |
1355 | { |
1356 | |
1357 | SysBeep(30); |
1358 | } |
1359 | |
1360 | void dlg_error_msg(void *dlg, char *msg) |
1361 | { |
1362 | Str255 pmsg; |
1363 | |
1364 | c2pstrcpy(pmsg, msg); |
1365 | ParamText(pmsg, NULL, NULL, NULL); |
1366 | StopAlert(128, NULL); |
1367 | } |
1368 | |
1369 | void dlg_end(void *dlg, int value) |
1370 | { |
f73f2f31 |
1371 | struct macctrls *mcs = dlg; |
8a7e67ec |
1372 | |
f73f2f31 |
1373 | if (mcs->end != NULL) |
1374 | (*mcs->end)(mcs->window, value); |
8a7e67ec |
1375 | }; |
1376 | |
1377 | void dlg_refresh(union control *ctrl, void *dlg) |
1378 | { |
1f1ec0e5 |
1379 | struct macctrls *mcs = dlg; |
1380 | union macctrl *mc; |
8a7e67ec |
1381 | |
1f1ec0e5 |
1382 | if (ctrl == NULL) |
1383 | return; /* FIXME */ |
1384 | mc = findbyctrl(mcs, ctrl); |
1385 | assert(mc != NULL); |
1386 | ctrlevent(mcs, mc, EVENT_REFRESH); |
8a7e67ec |
1387 | }; |
1388 | |
1389 | void *dlg_get_privdata(union control *ctrl, void *dlg) |
1390 | { |
1f1ec0e5 |
1391 | struct macctrls *mcs = dlg; |
1392 | union macctrl *mc = findbyctrl(mcs, ctrl); |
8a7e67ec |
1393 | |
1f1ec0e5 |
1394 | assert(mc != NULL); |
1395 | return mc->generic.privdata; |
8a7e67ec |
1396 | } |
1397 | |
1398 | void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr) |
1399 | { |
1f1ec0e5 |
1400 | struct macctrls *mcs = dlg; |
1401 | union macctrl *mc = findbyctrl(mcs, ctrl); |
8a7e67ec |
1402 | |
1f1ec0e5 |
1403 | assert(mc != NULL); |
1404 | mc->generic.privdata = ptr; |
1405 | mc->generic.freeprivdata = FALSE; |
8a7e67ec |
1406 | } |
1407 | |
1408 | void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size) |
1409 | { |
1f1ec0e5 |
1410 | struct macctrls *mcs = dlg; |
1411 | union macctrl *mc = findbyctrl(mcs, ctrl); |
8a7e67ec |
1412 | |
1f1ec0e5 |
1413 | assert(mc != NULL); |
1414 | mc->generic.privdata = smalloc(size); |
1415 | mc->generic.freeprivdata = TRUE; |
1416 | return mc->generic.privdata; |
8a7e67ec |
1417 | } |
1418 | |
1419 | |
1420 | /* |
1421 | * Radio Button control |
1422 | */ |
1423 | |
1424 | void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton) |
1425 | { |
93ba25f8 |
1426 | struct macctrls *mcs = dlg; |
1427 | union macctrl *mc = findbyctrl(mcs, ctrl); |
8a7e67ec |
1428 | int i; |
1429 | |
7d77d56d |
1430 | if (mc == NULL) return; |
8a7e67ec |
1431 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
1432 | if (i == whichbutton) |
1433 | SetControlValue(mc->radio.tbctrls[i], |
1434 | kControlRadioButtonCheckedValue); |
1435 | else |
1436 | SetControlValue(mc->radio.tbctrls[i], |
1437 | kControlRadioButtonUncheckedValue); |
1438 | } |
1439 | |
1440 | }; |
1441 | |
1442 | int dlg_radiobutton_get(union control *ctrl, void *dlg) |
1443 | { |
93ba25f8 |
1444 | struct macctrls *mcs = dlg; |
1445 | union macctrl *mc = findbyctrl(mcs, ctrl); |
8a7e67ec |
1446 | int i; |
1447 | |
93ba25f8 |
1448 | assert(mc != NULL); |
8a7e67ec |
1449 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
1450 | if (GetControlValue(mc->radio.tbctrls[i]) == |
1451 | kControlRadioButtonCheckedValue) |
1452 | return i; |
1453 | } |
1454 | return -1; |
1455 | }; |
1456 | |
1457 | |
1458 | /* |
1459 | * Check Box control |
1460 | */ |
1461 | |
1462 | void dlg_checkbox_set(union control *ctrl, void *dlg, int checked) |
1463 | { |
93ba25f8 |
1464 | struct macctrls *mcs = dlg; |
1465 | union macctrl *mc = findbyctrl(mcs, ctrl); |
8a7e67ec |
1466 | |
7d77d56d |
1467 | if (mc == NULL) return; |
8a7e67ec |
1468 | SetControlValue(mc->checkbox.tbctrl, |
1469 | checked ? kControlCheckBoxCheckedValue : |
1470 | kControlCheckBoxUncheckedValue); |
1471 | } |
1472 | |
1473 | int dlg_checkbox_get(union control *ctrl, void *dlg) |
1474 | { |
93ba25f8 |
1475 | struct macctrls *mcs = dlg; |
1476 | union macctrl *mc = findbyctrl(mcs, ctrl); |
8a7e67ec |
1477 | |
93ba25f8 |
1478 | assert(mc != NULL); |
8a7e67ec |
1479 | return GetControlValue(mc->checkbox.tbctrl); |
1480 | } |
1481 | |
1482 | |
1483 | /* |
1484 | * Edit Box control |
1485 | */ |
1486 | |
1487 | void dlg_editbox_set(union control *ctrl, void *dlg, char const *text) |
1488 | { |
1f1ec0e5 |
1489 | struct macctrls *mcs = dlg; |
1490 | union macctrl *mc = findbyctrl(mcs, ctrl); |
1491 | GrafPtr saveport; |
8a7e67ec |
1492 | |
7d77d56d |
1493 | if (mc == NULL) return; |
1f1ec0e5 |
1494 | assert(mc->generic.type == MACCTRL_EDITBOX); |
0e9ce565 |
1495 | GetPort(&saveport); |
1496 | SetPort((GrafPtr)(GetWindowPort(mcs->window))); |
d9158b5e |
1497 | if (mac_gestalts.apprvers >= 0x100) |
1f1ec0e5 |
1498 | SetControlData(mc->editbox.tbctrl, kControlEntireControl, |
1499 | ctrl->editbox.password ? |
1500 | kControlEditTextPasswordTag : |
1501 | kControlEditTextTextTag, |
1502 | strlen(text), text); |
a460b361 |
1503 | #if !TARGET_API_MAC_CARBON |
d9158b5e |
1504 | else |
0e9ce565 |
1505 | TESetText(text, strlen(text), |
1506 | (TEHandle)(*mc->editbox.tbctrl)->contrlData); |
a460b361 |
1507 | #endif |
d9158b5e |
1508 | DrawOneControl(mc->editbox.tbctrl); |
0e9ce565 |
1509 | SetPort(saveport); |
1510 | } |
8a7e67ec |
1511 | |
1512 | void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length) |
1513 | { |
1f1ec0e5 |
1514 | struct macctrls *mcs = dlg; |
1515 | union macctrl *mc = findbyctrl(mcs, ctrl); |
1516 | Size olen; |
8a7e67ec |
1517 | |
1f1ec0e5 |
1518 | assert(mc != NULL); |
1519 | assert(mc->generic.type == MACCTRL_EDITBOX); |
1520 | if (mac_gestalts.apprvers >= 0x100) { |
1521 | if (GetControlData(mc->editbox.tbctrl, kControlEntireControl, |
1522 | ctrl->editbox.password ? |
1523 | kControlEditTextPasswordTag : |
1524 | kControlEditTextTextTag, |
1525 | length - 1, buffer, &olen) != noErr) |
1526 | olen = 0; |
1527 | if (olen > length - 1) |
d9158b5e |
1528 | olen = length - 1; |
a460b361 |
1529 | } |
1530 | #if !TARGET_API_MAC_CARBON |
1531 | else { |
1532 | TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData; |
1533 | |
d9158b5e |
1534 | olen = (*te)->teLength; |
1535 | if (olen > length - 1) |
1536 | olen = length - 1; |
1537 | memcpy(buffer, *(*te)->hText, olen); |
1538 | } |
a460b361 |
1539 | #endif |
d9158b5e |
1540 | buffer[olen] = '\0'; |
d9158b5e |
1541 | } |
8a7e67ec |
1542 | |
1543 | |
1544 | /* |
1545 | * List Box control |
1546 | */ |
1547 | |
56c01970 |
1548 | static void dlg_macpopup_clear(union control *ctrl, void *dlg) |
1549 | { |
1550 | struct macctrls *mcs = dlg; |
1551 | union macctrl *mc = findbyctrl(mcs, ctrl); |
1552 | MenuRef menu = mc->popup.menu; |
1553 | unsigned int i, n; |
1554 | |
7d77d56d |
1555 | if (mc == NULL) return; |
fd703c0a |
1556 | n = CountMenuItems(menu); |
56c01970 |
1557 | for (i = 0; i < n; i++) |
1558 | DeleteMenuItem(menu, n - i); |
1559 | mc->popup.nids = 0; |
1560 | sfree(mc->popup.ids); |
1561 | mc->popup.ids = NULL; |
fd703c0a |
1562 | SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); |
56c01970 |
1563 | } |
1564 | |
7d77d56d |
1565 | static void dlg_maclist_clear(union control *ctrl, void *dlg) |
1566 | { |
1567 | struct macctrls *mcs = dlg; |
1568 | union macctrl *mc = findbyctrl(mcs, ctrl); |
7d77d56d |
1569 | |
1570 | if (mc == NULL) return; |
4bd083f7 |
1571 | LDelRow(0, 0, mc->listbox.list); |
7d77d56d |
1572 | mc->listbox.nids = 0; |
1573 | sfree(mc->listbox.ids); |
1574 | mc->listbox.ids = NULL; |
1575 | DrawOneControl(mc->listbox.tbctrl); |
1576 | } |
1577 | |
8a7e67ec |
1578 | void dlg_listbox_clear(union control *ctrl, void *dlg) |
1579 | { |
1580 | |
4bd083f7 |
1581 | switch (ctrl->generic.type) { |
1582 | case CTRL_LISTBOX: |
1583 | if (ctrl->listbox.height == 0) |
1584 | dlg_macpopup_clear(ctrl, dlg); |
1585 | else |
1586 | dlg_maclist_clear(ctrl, dlg); |
1587 | break; |
1588 | } |
56c01970 |
1589 | } |
1590 | |
1591 | static void dlg_macpopup_del(union control *ctrl, void *dlg, int index) |
1592 | { |
1593 | struct macctrls *mcs = dlg; |
1594 | union macctrl *mc = findbyctrl(mcs, ctrl); |
1595 | MenuRef menu = mc->popup.menu; |
1596 | |
7d77d56d |
1597 | if (mc == NULL) return; |
56c01970 |
1598 | DeleteMenuItem(menu, index + 1); |
1599 | if (mc->popup.ids != NULL) |
1600 | memcpy(mc->popup.ids + index, mc->popup.ids + index + 1, |
1601 | (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids)); |
fd703c0a |
1602 | SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); |
56c01970 |
1603 | } |
8a7e67ec |
1604 | |
7d77d56d |
1605 | static void dlg_maclist_del(union control *ctrl, void *dlg, int index) |
1606 | { |
1607 | struct macctrls *mcs = dlg; |
1608 | union macctrl *mc = findbyctrl(mcs, ctrl); |
7d77d56d |
1609 | |
1610 | if (mc == NULL) return; |
4bd083f7 |
1611 | LDelRow(1, index, mc->listbox.list); |
7d77d56d |
1612 | if (mc->listbox.ids != NULL) |
1613 | memcpy(mc->listbox.ids + index, mc->listbox.ids + index + 1, |
1614 | (mc->listbox.nids - index - 1) * sizeof(*mc->listbox.ids)); |
1615 | DrawOneControl(mc->listbox.tbctrl); |
1616 | } |
1617 | |
8a7e67ec |
1618 | void dlg_listbox_del(union control *ctrl, void *dlg, int index) |
1619 | { |
1620 | |
4bd083f7 |
1621 | switch (ctrl->generic.type) { |
1622 | case CTRL_LISTBOX: |
1623 | if (ctrl->listbox.height == 0) |
1624 | dlg_macpopup_del(ctrl, dlg, index); |
1625 | else |
1626 | dlg_maclist_del(ctrl, dlg, index); |
1627 | break; |
1628 | } |
56c01970 |
1629 | } |
1630 | |
1631 | static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text) |
1632 | { |
1633 | struct macctrls *mcs = dlg; |
1634 | union macctrl *mc = findbyctrl(mcs, ctrl); |
1635 | MenuRef menu = mc->popup.menu; |
1636 | Str255 itemstring; |
1637 | |
7d77d56d |
1638 | if (mc == NULL) return; |
56c01970 |
1639 | assert(text[0] != '\0'); |
1640 | c2pstrcpy(itemstring, text); |
1641 | AppendMenu(menu, "\pdummy"); |
fd703c0a |
1642 | SetMenuItemText(menu, CountMenuItems(menu), itemstring); |
1643 | SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); |
56c01970 |
1644 | } |
8a7e67ec |
1645 | |
7d77d56d |
1646 | |
1647 | static void dlg_maclist_add(union control *ctrl, void *dlg, char const *text) |
1648 | { |
1649 | struct macctrls *mcs = dlg; |
1650 | union macctrl *mc = findbyctrl(mcs, ctrl); |
7d77d56d |
1651 | ListBounds bounds; |
1652 | Cell cell = { 0, 0 }; |
1653 | |
1654 | if (mc == NULL) return; |
7d77d56d |
1655 | #if TARGET_API_MAC_CARBON |
4bd083f7 |
1656 | GetListDataBounds(mc->listbox.list, &bounds); |
7d77d56d |
1657 | #else |
4bd083f7 |
1658 | bounds = (*mc->listbox.list)->dataBounds; |
7d77d56d |
1659 | #endif |
1660 | cell.v = bounds.bottom; |
4bd083f7 |
1661 | LAddRow(1, cell.v, mc->listbox.list); |
1662 | LSetCell(text, strlen(text), cell, mc->listbox.list); |
7d77d56d |
1663 | DrawOneControl(mc->listbox.tbctrl); |
1664 | } |
1665 | |
8a7e67ec |
1666 | void dlg_listbox_add(union control *ctrl, void *dlg, char const *text) |
1667 | { |
1668 | |
4bd083f7 |
1669 | switch (ctrl->generic.type) { |
1670 | case CTRL_LISTBOX: |
1671 | if (ctrl->listbox.height == 0) |
1672 | dlg_macpopup_add(ctrl, dlg, text); |
1673 | else |
1674 | dlg_maclist_add(ctrl, dlg, text); |
1675 | break; |
1676 | } |
56c01970 |
1677 | } |
1678 | |
4eaff8d4 |
1679 | static void dlg_macpopup_addwithid(union control *ctrl, void *dlg, |
1680 | char const *text, int id) |
56c01970 |
1681 | { |
1682 | struct macctrls *mcs = dlg; |
1683 | union macctrl *mc = findbyctrl(mcs, ctrl); |
1684 | MenuRef menu = mc->popup.menu; |
1685 | unsigned int index; |
1686 | |
7d77d56d |
1687 | if (mc == NULL) return; |
56c01970 |
1688 | dlg_macpopup_add(ctrl, dlg, text); |
fd703c0a |
1689 | index = CountMenuItems(menu) - 1; |
56c01970 |
1690 | if (mc->popup.nids <= index) { |
1691 | mc->popup.nids = index + 1; |
34773273 |
1692 | mc->popup.ids = sresize(mc->popup.ids, mc->popup.nids, int); |
56c01970 |
1693 | } |
1694 | mc->popup.ids[index] = id; |
1695 | } |
8a7e67ec |
1696 | |
7d77d56d |
1697 | static void dlg_maclist_addwithid(union control *ctrl, void *dlg, |
1698 | char const *text, int id) |
1699 | { |
1700 | struct macctrls *mcs = dlg; |
1701 | union macctrl *mc = findbyctrl(mcs, ctrl); |
7d77d56d |
1702 | ListBounds bounds; |
1703 | int index; |
1704 | |
1705 | if (mc == NULL) return; |
7d77d56d |
1706 | dlg_maclist_add(ctrl, dlg, text); |
7d77d56d |
1707 | #if TARGET_API_MAC_CARBON |
4bd083f7 |
1708 | GetListDataBounds(mc->listbox.list, &bounds); |
7d77d56d |
1709 | #else |
4bd083f7 |
1710 | bounds = (*mc->listbox.list)->dataBounds; |
7d77d56d |
1711 | #endif |
1712 | index = bounds.bottom; |
1713 | if (mc->listbox.nids <= index) { |
1714 | mc->listbox.nids = index + 1; |
1715 | mc->listbox.ids = sresize(mc->listbox.ids, mc->listbox.nids, int); |
1716 | } |
1717 | mc->listbox.ids[index] = id; |
1718 | } |
1719 | |
4eaff8d4 |
1720 | void dlg_listbox_addwithid(union control *ctrl, void *dlg, |
1721 | char const *text, int id) |
8a7e67ec |
1722 | { |
1723 | |
4bd083f7 |
1724 | switch (ctrl->generic.type) { |
1725 | case CTRL_LISTBOX: |
1726 | if (ctrl->listbox.height == 0) |
1727 | dlg_macpopup_addwithid(ctrl, dlg, text, id); |
1728 | else |
1729 | dlg_maclist_addwithid(ctrl, dlg, text, id); |
1730 | break; |
1731 | } |
56c01970 |
1732 | } |
8a7e67ec |
1733 | |
1734 | int dlg_listbox_getid(union control *ctrl, void *dlg, int index) |
1735 | { |
56c01970 |
1736 | struct macctrls *mcs = dlg; |
1737 | union macctrl *mc = findbyctrl(mcs, ctrl); |
8a7e67ec |
1738 | |
7d77d56d |
1739 | assert(mc != NULL); |
4bd083f7 |
1740 | switch (ctrl->generic.type) { |
1741 | case CTRL_LISTBOX: |
1742 | if (ctrl->listbox.height == 0) { |
1743 | assert(mc->popup.ids != NULL && mc->popup.nids > index); |
1744 | return mc->popup.ids[index]; |
1745 | } else { |
1746 | assert(mc->listbox.ids != NULL && mc->listbox.nids > index); |
1747 | return mc->listbox.ids[index]; |
1748 | } |
56c01970 |
1749 | } |
4bd083f7 |
1750 | return -1; |
7d77d56d |
1751 | } |
1752 | |
1753 | int dlg_listbox_index(union control *ctrl, void *dlg) |
1754 | { |
1755 | struct macctrls *mcs = dlg; |
7d77d56d |
1756 | union macctrl *mc = findbyctrl(mcs, ctrl); |
7d77d56d |
1757 | Cell cell = { 0, 0 }; |
1758 | |
4bd083f7 |
1759 | assert(mc != NULL); |
1760 | switch (ctrl->generic.type) { |
1761 | case CTRL_LISTBOX: |
1762 | if (ctrl->listbox.height == 0) |
1763 | return GetControlValue(mc->popup.tbctrl) - 1; |
1764 | else { |
1765 | if (LGetSelect(TRUE, &cell, mc->listbox.list)) |
1766 | return cell.v; |
1767 | else |
1768 | return -1; |
1769 | } |
1770 | } |
1771 | return -1; |
7d77d56d |
1772 | } |
8a7e67ec |
1773 | |
1774 | int dlg_listbox_issel(union control *ctrl, void *dlg, int index) |
1775 | { |
56c01970 |
1776 | struct macctrls *mcs = dlg; |
1777 | union macctrl *mc = findbyctrl(mcs, ctrl); |
4bd083f7 |
1778 | Cell cell = { 0, 0 }; |
8a7e67ec |
1779 | |
4bd083f7 |
1780 | assert(mc != NULL); |
1781 | switch (ctrl->generic.type) { |
1782 | case CTRL_LISTBOX: |
1783 | if (ctrl->listbox.height == 0) |
1784 | return GetControlValue(mc->popup.tbctrl) - 1 == index; |
1785 | else { |
1786 | cell.v = index; |
1787 | return LGetSelect(FALSE, &cell, mc->listbox.list); |
1788 | } |
1789 | } |
1790 | return FALSE; |
7d77d56d |
1791 | } |
8a7e67ec |
1792 | |
1793 | void dlg_listbox_select(union control *ctrl, void *dlg, int index) |
1794 | { |
56c01970 |
1795 | struct macctrls *mcs = dlg; |
1796 | union macctrl *mc = findbyctrl(mcs, ctrl); |
8a7e67ec |
1797 | |
7d77d56d |
1798 | if (mc == NULL) return; |
4bd083f7 |
1799 | switch (ctrl->generic.type) { |
1800 | case CTRL_LISTBOX: |
1801 | if (ctrl->listbox.height == 0) |
1802 | SetControlValue(mc->popup.tbctrl, index + 1); |
1803 | break; |
1804 | } |
1805 | } |
8a7e67ec |
1806 | |
1807 | |
1808 | /* |
1809 | * Text control |
1810 | */ |
1811 | |
1812 | void dlg_text_set(union control *ctrl, void *dlg, char const *text) |
1813 | { |
93ba25f8 |
1814 | struct macctrls *mcs = dlg; |
1815 | union macctrl *mc = findbyctrl(mcs, ctrl); |
8a7e67ec |
1816 | |
7d77d56d |
1817 | if (mc == NULL) return; |
8a7e67ec |
1818 | if (mac_gestalts.apprvers >= 0x100) |
1819 | SetControlData(mc->text.tbctrl, kControlEntireControl, |
1f1ec0e5 |
1820 | kControlStaticTextTextTag, strlen(text), text); |
f28d33e8 |
1821 | #if !TARGET_API_MAC_CARBON |
1822 | else |
1823 | TESetText(text, strlen(text), |
1824 | (TEHandle)(*mc->text.tbctrl)->contrlData); |
1825 | #endif |
8a7e67ec |
1826 | } |
1827 | |
1828 | |
1829 | /* |
1830 | * File Selector control |
1831 | */ |
1832 | |
1833 | void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn) |
1834 | { |
1835 | |
1836 | } |
1837 | |
1838 | void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn) |
1839 | { |
1840 | |
1841 | } |
1842 | |
1843 | |
1844 | /* |
1845 | * Font Selector control |
1846 | */ |
1847 | |
1848 | void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn) |
1849 | { |
1850 | |
1851 | } |
1852 | |
1853 | void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn) |
1854 | { |
1855 | |
1856 | } |
1857 | |
1858 | |
1859 | /* |
1860 | * Printer enumeration |
1861 | */ |
1862 | |
1863 | printer_enum *printer_start_enum(int *nprinters) |
1864 | { |
1865 | |
1866 | *nprinters = 0; |
1867 | return NULL; |
1868 | } |
1869 | |
1870 | char *printer_get_name(printer_enum *pe, int thing) |
1871 | { |
1872 | |
1873 | return "<none>"; |
1874 | } |
1875 | |
1876 | void printer_finish_enum(printer_enum *pe) |
1877 | { |
1878 | |
1879 | } |
1880 | |
1881 | |
1882 | /* |
1883 | * Colour selection stuff |
1884 | */ |
1885 | |
1886 | void dlg_coloursel_start(union control *ctrl, void *dlg, |
1887 | int r, int g, int b) |
1888 | { |
621bfeaa |
1889 | struct macctrls *mcs = dlg; |
1890 | union macctrl *mc = findbyctrl(mcs, ctrl); |
1891 | Point where = {-1, -1}; /* Screen with greatest colour depth */ |
1892 | RGBColor incolour; |
8a7e67ec |
1893 | |
37404b15 |
1894 | if (HAVE_COLOR_QD()) { |
1895 | incolour.red = r * 0x0101; |
1896 | incolour.green = g * 0x0101; |
1897 | incolour.blue = b * 0x0101; |
1898 | mcs->gotcolour = GetColor(where, "\pModify Colour:", &incolour, |
1899 | &mcs->thecolour); |
1900 | ctrlevent(mcs, mc, EVENT_CALLBACK); |
1901 | } else |
1902 | dlg_beep(dlg); |
8a7e67ec |
1903 | } |
1904 | |
1905 | int dlg_coloursel_results(union control *ctrl, void *dlg, |
1906 | int *r, int *g, int *b) |
1907 | { |
621bfeaa |
1908 | struct macctrls *mcs = dlg; |
8a7e67ec |
1909 | |
621bfeaa |
1910 | if (mcs->gotcolour) { |
1911 | *r = mcs->thecolour.red >> 8; |
1912 | *g = mcs->thecolour.green >> 8; |
1913 | *b = mcs->thecolour.blue >> 8; |
1914 | return 1; |
1915 | } else |
1916 | return 0; |
8a7e67ec |
1917 | } |
1918 | |
1919 | /* |
1920 | * Local Variables: |
1921 | * c-file-style: "simon" |
1922 | * End: |
1923 | */ |