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