| 1 | /* $Id: macctrls.c,v 1.18 2003/03/29 18:32:36 ben Exp $ */ |
| 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> |
| 32 | #include <Menus.h> |
| 33 | #include <Resources.h> |
| 34 | #include <Script.h> |
| 35 | #include <Sound.h> |
| 36 | #include <TextEdit.h> |
| 37 | #include <TextUtils.h> |
| 38 | #include <ToolUtils.h> |
| 39 | #include <Windows.h> |
| 40 | |
| 41 | #include <assert.h> |
| 42 | #include <string.h> |
| 43 | |
| 44 | #include "putty.h" |
| 45 | #include "mac.h" |
| 46 | #include "macresid.h" |
| 47 | #include "dialog.h" |
| 48 | #include "tree234.h" |
| 49 | |
| 50 | /* Range of menu IDs for popup menus */ |
| 51 | #define MENU_MIN 1024 |
| 52 | #define MENU_MAX 2048 |
| 53 | |
| 54 | |
| 55 | union macctrl { |
| 56 | struct macctrl_generic { |
| 57 | enum { |
| 58 | MACCTRL_TEXT, |
| 59 | MACCTRL_EDITBOX, |
| 60 | MACCTRL_RADIO, |
| 61 | MACCTRL_CHECKBOX, |
| 62 | MACCTRL_BUTTON, |
| 63 | MACCTRL_POPUP |
| 64 | } type; |
| 65 | /* Template from which this was generated */ |
| 66 | union control *ctrl; |
| 67 | /* Next control in this panel */ |
| 68 | union macctrl *next; |
| 69 | void *privdata; |
| 70 | int freeprivdata; |
| 71 | } generic; |
| 72 | struct { |
| 73 | struct macctrl_generic generic; |
| 74 | ControlRef tbctrl; |
| 75 | } text; |
| 76 | struct { |
| 77 | struct macctrl_generic generic; |
| 78 | ControlRef tbctrl; |
| 79 | } editbox; |
| 80 | struct { |
| 81 | struct macctrl_generic generic; |
| 82 | ControlRef *tbctrls; |
| 83 | } radio; |
| 84 | struct { |
| 85 | struct macctrl_generic generic; |
| 86 | ControlRef tbctrl; |
| 87 | } checkbox; |
| 88 | struct { |
| 89 | struct macctrl_generic generic; |
| 90 | ControlRef tbctrl; |
| 91 | } button; |
| 92 | struct { |
| 93 | struct macctrl_generic generic; |
| 94 | ControlRef tbctrl; |
| 95 | MenuRef menu; |
| 96 | int menuid; |
| 97 | unsigned int nids; |
| 98 | int *ids; |
| 99 | } popup; |
| 100 | }; |
| 101 | |
| 102 | struct mac_layoutstate { |
| 103 | Point pos; |
| 104 | unsigned int width; |
| 105 | unsigned int panelnum; |
| 106 | }; |
| 107 | |
| 108 | #define ctrlevent(mcs, mc, event) do { \ |
| 109 | if ((mc)->generic.ctrl->generic.handler != NULL) \ |
| 110 | (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\ |
| 111 | (mcs)->data, (event)); \ |
| 112 | } while (0) |
| 113 | |
| 114 | #define findbyctrl(mcs, ctrl) \ |
| 115 | find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find) |
| 116 | |
| 117 | static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *, |
| 118 | WindowPtr, struct macctrls *); |
| 119 | static void macctrl_switchtopanel(struct macctrls *, unsigned int); |
| 120 | static void macctrl_setfocus(struct macctrls *, union macctrl *); |
| 121 | static void macctrl_text(struct macctrls *, WindowPtr, |
| 122 | struct mac_layoutstate *, union control *); |
| 123 | static void macctrl_editbox(struct macctrls *, WindowPtr, |
| 124 | struct mac_layoutstate *, union control *); |
| 125 | static void macctrl_radio(struct macctrls *, WindowPtr, |
| 126 | struct mac_layoutstate *, union control *); |
| 127 | static void macctrl_checkbox(struct macctrls *, WindowPtr, |
| 128 | struct mac_layoutstate *, union control *); |
| 129 | static void macctrl_button(struct macctrls *, WindowPtr, |
| 130 | struct mac_layoutstate *, union control *); |
| 131 | static void macctrl_popup(struct macctrls *, WindowPtr, |
| 132 | struct mac_layoutstate *, union control *); |
| 133 | #if !TARGET_API_MAC_CARBON |
| 134 | static pascal SInt32 macctrl_sys7_text_cdef(SInt16, ControlRef, |
| 135 | ControlDefProcMessage, SInt32); |
| 136 | static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef, |
| 137 | ControlDefProcMessage, SInt32); |
| 138 | static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef, |
| 139 | ControlDefProcMessage, SInt32); |
| 140 | #endif |
| 141 | |
| 142 | #if !TARGET_API_MAC_CARBON |
| 143 | /* |
| 144 | * This trick enables us to keep all the CDEF code in the main |
| 145 | * application, which makes life easier. For details, see |
| 146 | * <http://developer.apple.com/technotes/tn/tn2003.html#custom_code_base>. |
| 147 | */ |
| 148 | |
| 149 | #pragma options align=mac68k |
| 150 | typedef struct { |
| 151 | short jmpabs; /* 4EF9 */ |
| 152 | ControlDefUPP theUPP; |
| 153 | } **PatchCDEF; |
| 154 | #pragma options align=reset |
| 155 | #endif |
| 156 | |
| 157 | static void macctrl_init() |
| 158 | { |
| 159 | #if !TARGET_API_MAC_CARBON |
| 160 | static int inited = 0; |
| 161 | PatchCDEF cdef; |
| 162 | |
| 163 | if (inited) return; |
| 164 | cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Text); |
| 165 | (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_text_cdef); |
| 166 | cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox); |
| 167 | (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef); |
| 168 | cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default); |
| 169 | (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef); |
| 170 | inited = 1; |
| 171 | #endif |
| 172 | } |
| 173 | |
| 174 | |
| 175 | static int macctrl_cmp_byctrl(void *av, void *bv) |
| 176 | { |
| 177 | union macctrl *a = (union macctrl *)av; |
| 178 | union macctrl *b = (union macctrl *)bv; |
| 179 | |
| 180 | if (a->generic.ctrl < b->generic.ctrl) |
| 181 | return -1; |
| 182 | else if (a->generic.ctrl > b->generic.ctrl) |
| 183 | return +1; |
| 184 | else |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | static int macctrl_cmp_byctrl_find(void *av, void *bv) |
| 189 | { |
| 190 | union control *a = (union control *)av; |
| 191 | union macctrl *b = (union macctrl *)bv; |
| 192 | |
| 193 | if (a < b->generic.ctrl) |
| 194 | return -1; |
| 195 | else if (a > b->generic.ctrl) |
| 196 | return +1; |
| 197 | else |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | void macctrl_layoutbox(struct controlbox *cb, WindowPtr window, |
| 202 | struct macctrls *mcs) |
| 203 | { |
| 204 | int i; |
| 205 | struct mac_layoutstate curstate; |
| 206 | ControlRef root; |
| 207 | Rect rect; |
| 208 | |
| 209 | macctrl_init(); |
| 210 | #if TARGET_API_MAC_CARBON |
| 211 | GetPortBounds(GetWindowPort(window), &rect); |
| 212 | #else |
| 213 | rect = window->portRect; |
| 214 | #endif |
| 215 | curstate.pos.h = rect.left + 13; |
| 216 | curstate.pos.v = rect.bottom - 59; |
| 217 | curstate.width = rect.right - rect.left - (13 * 2); |
| 218 | if (mac_gestalts.apprvers >= 0x100) |
| 219 | CreateRootControl(window, &root); |
| 220 | mcs->window = window; |
| 221 | mcs->byctrl = newtree234(macctrl_cmp_byctrl); |
| 222 | mcs->focus = NULL; |
| 223 | /* Count the number of panels */ |
| 224 | mcs->npanels = 1; |
| 225 | for (i = 1; i < cb->nctrlsets; i++) |
| 226 | if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname)) |
| 227 | mcs->npanels++; |
| 228 | mcs->panels = smalloc(sizeof(*mcs->panels) * mcs->npanels); |
| 229 | memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels); |
| 230 | curstate.panelnum = 0; |
| 231 | for (i = 0; i < cb->nctrlsets; i++) { |
| 232 | if (i > 0 && strcmp(cb->ctrlsets[i]->pathname, |
| 233 | cb->ctrlsets[i-1]->pathname)) { |
| 234 | curstate.pos.v = rect.top + 13; |
| 235 | curstate.panelnum++; |
| 236 | assert(curstate.panelnum < mcs->npanels); |
| 237 | } |
| 238 | macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs); |
| 239 | } |
| 240 | macctrl_switchtopanel(mcs, 1); |
| 241 | } |
| 242 | |
| 243 | static void macctrl_layoutset(struct mac_layoutstate *curstate, |
| 244 | struct controlset *s, |
| 245 | WindowPtr window, struct macctrls *mcs) |
| 246 | { |
| 247 | unsigned int i; |
| 248 | |
| 249 | fprintf(stderr, "--- begin set ---\n"); |
| 250 | fprintf(stderr, "pathname = %s\n", s->pathname); |
| 251 | if (s->boxname && *s->boxname) |
| 252 | fprintf(stderr, "boxname = %s\n", s->boxname); |
| 253 | if (s->boxtitle) |
| 254 | fprintf(stderr, "boxtitle = %s\n", s->boxtitle); |
| 255 | |
| 256 | |
| 257 | for (i = 0; i < s->ncontrols; i++) { |
| 258 | union control *ctrl = s->ctrls[i]; |
| 259 | char const *s; |
| 260 | |
| 261 | switch (ctrl->generic.type) { |
| 262 | case CTRL_TEXT: s = "text"; break; |
| 263 | case CTRL_EDITBOX: s = "editbox"; break; |
| 264 | case CTRL_RADIO: s = "radio"; break; |
| 265 | case CTRL_CHECKBOX: s = "checkbox"; break; |
| 266 | case CTRL_BUTTON: s = "button"; break; |
| 267 | case CTRL_LISTBOX: s = "listbox"; break; |
| 268 | case CTRL_COLUMNS: s = "columns"; break; |
| 269 | case CTRL_FILESELECT: s = "fileselect"; break; |
| 270 | case CTRL_FONTSELECT: s = "fontselect"; break; |
| 271 | case CTRL_TABDELAY: s = "tabdelay"; break; |
| 272 | default: s = "unknown"; break; |
| 273 | } |
| 274 | fprintf(stderr, " control: %s\n", s); |
| 275 | switch (ctrl->generic.type) { |
| 276 | case CTRL_TEXT: |
| 277 | macctrl_text(mcs, window, curstate, ctrl); |
| 278 | break; |
| 279 | case CTRL_EDITBOX: |
| 280 | macctrl_editbox(mcs, window, curstate, ctrl); |
| 281 | break; |
| 282 | case CTRL_RADIO: |
| 283 | macctrl_radio(mcs, window, curstate, ctrl); |
| 284 | break; |
| 285 | case CTRL_CHECKBOX: |
| 286 | macctrl_checkbox(mcs, window, curstate, ctrl); |
| 287 | break; |
| 288 | case CTRL_BUTTON: |
| 289 | macctrl_button(mcs, window, curstate, ctrl); |
| 290 | break; |
| 291 | case CTRL_LISTBOX: |
| 292 | if (ctrl->listbox.height == 0) |
| 293 | macctrl_popup(mcs, window, curstate, ctrl); |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which) |
| 300 | { |
| 301 | unsigned int i, j; |
| 302 | union macctrl *mc; |
| 303 | |
| 304 | #define hideshow(c) do { \ |
| 305 | if (i == which) ShowControl(c); else HideControl(c); \ |
| 306 | } while (0) |
| 307 | |
| 308 | mcs->curpanel = which; |
| 309 | /* Panel 0 is special and always visible. */ |
| 310 | for (i = 1; i < mcs->npanels; i++) |
| 311 | for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) { |
| 312 | #if !TARGET_API_MAC_CARBON |
| 313 | if (mcs->focus == mc) |
| 314 | macctrl_setfocus(mcs, NULL); |
| 315 | #endif |
| 316 | switch (mc->generic.type) { |
| 317 | case MACCTRL_TEXT: |
| 318 | hideshow(mc->text.tbctrl); |
| 319 | break; |
| 320 | case MACCTRL_EDITBOX: |
| 321 | hideshow(mc->editbox.tbctrl); |
| 322 | break; |
| 323 | case MACCTRL_RADIO: |
| 324 | for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++) |
| 325 | hideshow(mc->radio.tbctrls[j]); |
| 326 | break; |
| 327 | case MACCTRL_CHECKBOX: |
| 328 | hideshow(mc->checkbox.tbctrl); |
| 329 | break; |
| 330 | case MACCTRL_BUTTON: |
| 331 | hideshow(mc->button.tbctrl); |
| 332 | break; |
| 333 | case MACCTRL_POPUP: |
| 334 | hideshow(mc->popup.tbctrl); |
| 335 | break; |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | #if !TARGET_API_MAC_CARBON |
| 341 | /* |
| 342 | * System 7 focus manipulation |
| 343 | */ |
| 344 | static void macctrl_defocus(union macctrl *mc) |
| 345 | { |
| 346 | |
| 347 | assert(mac_gestalts.apprvers < 0x100); |
| 348 | switch (mc->generic.type) { |
| 349 | case MACCTRL_EDITBOX: |
| 350 | TEDeactivate((TEHandle)(*mc->editbox.tbctrl)->contrlData); |
| 351 | break; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static void macctrl_enfocus(union macctrl *mc) |
| 356 | { |
| 357 | |
| 358 | assert(mac_gestalts.apprvers < 0x100); |
| 359 | switch (mc->generic.type) { |
| 360 | case MACCTRL_EDITBOX: |
| 361 | TEActivate((TEHandle)(*mc->editbox.tbctrl)->contrlData); |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | static void macctrl_setfocus(struct macctrls *mcs, union macctrl *mc) |
| 367 | { |
| 368 | |
| 369 | if (mcs->focus != NULL) |
| 370 | macctrl_defocus(mcs->focus); |
| 371 | mcs->focus = mc; |
| 372 | if (mc != NULL) |
| 373 | macctrl_enfocus(mc); |
| 374 | } |
| 375 | #endif |
| 376 | |
| 377 | static void macctrl_text(struct macctrls *mcs, WindowPtr window, |
| 378 | struct mac_layoutstate *curstate, |
| 379 | union control *ctrl) |
| 380 | { |
| 381 | union macctrl *mc = smalloc(sizeof *mc); |
| 382 | Rect bounds; |
| 383 | |
| 384 | fprintf(stderr, " label = %s\n", ctrl->text.label); |
| 385 | mc->generic.type = MACCTRL_TEXT; |
| 386 | mc->generic.ctrl = ctrl; |
| 387 | mc->generic.privdata = NULL; |
| 388 | bounds.left = curstate->pos.h; |
| 389 | bounds.right = bounds.left + curstate->width; |
| 390 | bounds.top = curstate->pos.v; |
| 391 | bounds.bottom = bounds.top + 16; |
| 392 | if (mac_gestalts.apprvers >= 0x100) { |
| 393 | SInt16 height; |
| 394 | Size olen; |
| 395 | |
| 396 | mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0, |
| 397 | kControlStaticTextProc, (long)mc); |
| 398 | SetControlData(mc->text.tbctrl, kControlEntireControl, |
| 399 | kControlStaticTextTextTag, |
| 400 | strlen(ctrl->text.label), ctrl->text.label); |
| 401 | GetControlData(mc->text.tbctrl, kControlEntireControl, |
| 402 | kControlStaticTextTextHeightTag, |
| 403 | sizeof(height), &height, &olen); |
| 404 | fprintf(stderr, " height = %d\n", height); |
| 405 | SizeControl(mc->text.tbctrl, curstate->width, height); |
| 406 | curstate->pos.v += height + 6; |
| 407 | } else { |
| 408 | Str255 title; |
| 409 | |
| 410 | c2pstrcpy(title, ctrl->text.label); |
| 411 | mc->text.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 0, |
| 412 | SYS7_TEXT_PROC, (long)mc); |
| 413 | } |
| 414 | add234(mcs->byctrl, mc); |
| 415 | mc->generic.next = mcs->panels[curstate->panelnum]; |
| 416 | mcs->panels[curstate->panelnum] = mc; |
| 417 | } |
| 418 | |
| 419 | #if !TARGET_API_MAC_CARBON |
| 420 | static pascal SInt32 macctrl_sys7_text_cdef(SInt16 variant, ControlRef control, |
| 421 | ControlDefProcMessage msg, SInt32 param) |
| 422 | { |
| 423 | RgnHandle rgn; |
| 424 | |
| 425 | switch (msg) { |
| 426 | case drawCntl: |
| 427 | if ((*control)->contrlVis) |
| 428 | TETextBox((*control)->contrlTitle + 1, (*control)->contrlTitle[0], |
| 429 | &(*control)->contrlRect, teFlushDefault); |
| 430 | return 0; |
| 431 | case calcCRgns: |
| 432 | if (param & (1 << 31)) { |
| 433 | param &= ~(1 << 31); |
| 434 | goto calcthumbrgn; |
| 435 | } |
| 436 | /* FALLTHROUGH */ |
| 437 | case calcCntlRgn: |
| 438 | rgn = (RgnHandle)param; |
| 439 | RectRgn(rgn, &(*control)->contrlRect); |
| 440 | return 0; |
| 441 | case calcThumbRgn: |
| 442 | calcthumbrgn: |
| 443 | rgn = (RgnHandle)param; |
| 444 | SetEmptyRgn(rgn); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | return 0; |
| 449 | } |
| 450 | #endif |
| 451 | |
| 452 | static void macctrl_editbox(struct macctrls *mcs, WindowPtr window, |
| 453 | struct mac_layoutstate *curstate, |
| 454 | union control *ctrl) |
| 455 | { |
| 456 | union macctrl *mc = smalloc(sizeof *mc); |
| 457 | Rect bounds; |
| 458 | |
| 459 | fprintf(stderr, " label = %s\n", ctrl->editbox.label); |
| 460 | fprintf(stderr, " percentwidth = %d\n", ctrl->editbox.percentwidth); |
| 461 | if (ctrl->editbox.password) fprintf(stderr, " password\n"); |
| 462 | if (ctrl->editbox.has_list) fprintf(stderr, " has list\n"); |
| 463 | mc->generic.type = MACCTRL_EDITBOX; |
| 464 | mc->generic.ctrl = ctrl; |
| 465 | mc->generic.privdata = NULL; |
| 466 | bounds.left = curstate->pos.h; |
| 467 | bounds.right = bounds.left + curstate->width; |
| 468 | bounds.top = curstate->pos.v; |
| 469 | bounds.bottom = bounds.top + 22; |
| 470 | if (mac_gestalts.apprvers >= 0x100) { |
| 471 | InsetRect(&bounds, 2, 2); |
| 472 | mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0, |
| 473 | ctrl->editbox.password ? |
| 474 | kControlEditTextPasswordProc : |
| 475 | kControlEditTextProc, (long)mc); |
| 476 | } else { |
| 477 | mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0, |
| 478 | SYS7_EDITBOX_PROC, (long)mc); |
| 479 | } |
| 480 | curstate->pos.v += 28; |
| 481 | add234(mcs->byctrl, mc); |
| 482 | mc->generic.next = mcs->panels[curstate->panelnum]; |
| 483 | mcs->panels[curstate->panelnum] = mc; |
| 484 | ctrlevent(mcs, mc, EVENT_REFRESH); |
| 485 | } |
| 486 | |
| 487 | #if !TARGET_API_MAC_CARBON |
| 488 | static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant, |
| 489 | ControlRef control, |
| 490 | ControlDefProcMessage msg, |
| 491 | SInt32 param) |
| 492 | { |
| 493 | RgnHandle rgn; |
| 494 | Rect rect; |
| 495 | TEHandle te; |
| 496 | long ssfs; |
| 497 | Point mouse; |
| 498 | |
| 499 | switch (msg) { |
| 500 | case initCntl: |
| 501 | rect = (*control)->contrlRect; |
| 502 | InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */ |
| 503 | te = TENew(&rect, &rect); |
| 504 | ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize); |
| 505 | (*te)->txSize = LoWord(ssfs); |
| 506 | (*te)->txFont = HiWord(ssfs); |
| 507 | (*control)->contrlData = (Handle)te; |
| 508 | return noErr; |
| 509 | case dispCntl: |
| 510 | TEDispose((TEHandle)(*control)->contrlData); |
| 511 | return 0; |
| 512 | case drawCntl: |
| 513 | if ((*control)->contrlVis) { |
| 514 | rect = (*control)->contrlRect; |
| 515 | PenNormal(); |
| 516 | FrameRect(&rect); |
| 517 | InsetRect(&rect, 3, 3); |
| 518 | TEUpdate(&rect, (TEHandle)(*control)->contrlData); |
| 519 | } |
| 520 | return 0; |
| 521 | case testCntl: |
| 522 | mouse.h = LoWord(param); |
| 523 | mouse.v = HiWord(param); |
| 524 | return |
| 525 | PtInRect(mouse, &(*(TEHandle)(*control)->contrlData)->viewRect) ? |
| 526 | kControlEditTextPart : kControlNoPart; |
| 527 | case calcCRgns: |
| 528 | if (param & (1 << 31)) { |
| 529 | param &= ~(1 << 31); |
| 530 | goto calcthumbrgn; |
| 531 | } |
| 532 | /* FALLTHROUGH */ |
| 533 | case calcCntlRgn: |
| 534 | rgn = (RgnHandle)param; |
| 535 | RectRgn(rgn, &(*control)->contrlRect); |
| 536 | return 0; |
| 537 | case calcThumbRgn: |
| 538 | calcthumbrgn: |
| 539 | rgn = (RgnHandle)param; |
| 540 | SetEmptyRgn(rgn); |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | #endif |
| 547 | |
| 548 | static void macctrl_radio(struct macctrls *mcs, WindowPtr window, |
| 549 | struct mac_layoutstate *curstate, |
| 550 | union control *ctrl) |
| 551 | { |
| 552 | union macctrl *mc = smalloc(sizeof *mc); |
| 553 | Rect bounds; |
| 554 | Str255 title; |
| 555 | unsigned int i, colwidth; |
| 556 | |
| 557 | fprintf(stderr, " label = %s\n", ctrl->radio.label); |
| 558 | mc->generic.type = MACCTRL_RADIO; |
| 559 | mc->generic.ctrl = ctrl; |
| 560 | mc->generic.privdata = NULL; |
| 561 | mc->radio.tbctrls = |
| 562 | smalloc(sizeof(*mc->radio.tbctrls) * ctrl->radio.nbuttons); |
| 563 | colwidth = (curstate->width + 13) / ctrl->radio.ncolumns; |
| 564 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
| 565 | fprintf(stderr, " button = %s\n", ctrl->radio.buttons[i]); |
| 566 | bounds.top = curstate->pos.v - 2; |
| 567 | bounds.bottom = bounds.top + 18; |
| 568 | bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns); |
| 569 | if (i == ctrl->radio.nbuttons - 1 || |
| 570 | i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) { |
| 571 | bounds.right = curstate->pos.h + curstate->width; |
| 572 | curstate->pos.v += 18; |
| 573 | } else |
| 574 | bounds.right = bounds.left + colwidth - 13; |
| 575 | c2pstrcpy(title, ctrl->radio.buttons[i]); |
| 576 | mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE, |
| 577 | 0, 0, 1, radioButProc, (long)mc); |
| 578 | } |
| 579 | curstate->pos.v += 4; |
| 580 | add234(mcs->byctrl, mc); |
| 581 | mc->generic.next = mcs->panels[curstate->panelnum]; |
| 582 | mcs->panels[curstate->panelnum] = mc; |
| 583 | ctrlevent(mcs, mc, EVENT_REFRESH); |
| 584 | } |
| 585 | |
| 586 | static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window, |
| 587 | struct mac_layoutstate *curstate, |
| 588 | union control *ctrl) |
| 589 | { |
| 590 | union macctrl *mc = smalloc(sizeof *mc); |
| 591 | Rect bounds; |
| 592 | Str255 title; |
| 593 | |
| 594 | fprintf(stderr, " label = %s\n", ctrl->checkbox.label); |
| 595 | mc->generic.type = MACCTRL_CHECKBOX; |
| 596 | mc->generic.ctrl = ctrl; |
| 597 | mc->generic.privdata = NULL; |
| 598 | bounds.left = curstate->pos.h; |
| 599 | bounds.right = bounds.left + curstate->width; |
| 600 | bounds.top = curstate->pos.v; |
| 601 | bounds.bottom = bounds.top + 16; |
| 602 | c2pstrcpy(title, ctrl->checkbox.label); |
| 603 | mc->checkbox.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1, |
| 604 | checkBoxProc, (long)mc); |
| 605 | add234(mcs->byctrl, mc); |
| 606 | curstate->pos.v += 22; |
| 607 | mc->generic.next = mcs->panels[curstate->panelnum]; |
| 608 | mcs->panels[curstate->panelnum] = mc; |
| 609 | ctrlevent(mcs, mc, EVENT_REFRESH); |
| 610 | } |
| 611 | |
| 612 | static void macctrl_button(struct macctrls *mcs, WindowPtr window, |
| 613 | struct mac_layoutstate *curstate, |
| 614 | union control *ctrl) |
| 615 | { |
| 616 | union macctrl *mc = smalloc(sizeof *mc); |
| 617 | Rect bounds; |
| 618 | Str255 title; |
| 619 | |
| 620 | fprintf(stderr, " label = %s\n", ctrl->button.label); |
| 621 | if (ctrl->button.isdefault) |
| 622 | fprintf(stderr, " is default\n"); |
| 623 | mc->generic.type = MACCTRL_BUTTON; |
| 624 | mc->generic.ctrl = ctrl; |
| 625 | mc->generic.privdata = NULL; |
| 626 | bounds.left = curstate->pos.h; |
| 627 | bounds.right = bounds.left + 100; /* XXX measure string */ |
| 628 | bounds.top = curstate->pos.v; |
| 629 | bounds.bottom = bounds.top + 20; |
| 630 | c2pstrcpy(title, ctrl->button.label); |
| 631 | mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1, |
| 632 | pushButProc, (long)mc); |
| 633 | if (mac_gestalts.apprvers >= 0x100) { |
| 634 | Boolean isdefault = ctrl->button.isdefault; |
| 635 | |
| 636 | SetControlData(mc->button.tbctrl, kControlEntireControl, |
| 637 | kControlPushButtonDefaultTag, |
| 638 | sizeof(isdefault), &isdefault); |
| 639 | } else if (ctrl->button.isdefault) { |
| 640 | InsetRect(&bounds, -4, -4); |
| 641 | NewControl(window, &bounds, title, TRUE, 0, 0, 1, |
| 642 | SYS7_DEFAULT_PROC, (long)mc); |
| 643 | } |
| 644 | if (mac_gestalts.apprvers >= 0x110) { |
| 645 | Boolean iscancel = ctrl->button.iscancel; |
| 646 | |
| 647 | SetControlData(mc->button.tbctrl, kControlEntireControl, |
| 648 | kControlPushButtonCancelTag, |
| 649 | sizeof(iscancel), &iscancel); |
| 650 | } |
| 651 | add234(mcs->byctrl, mc); |
| 652 | mc->generic.next = mcs->panels[curstate->panelnum]; |
| 653 | mcs->panels[curstate->panelnum] = mc; |
| 654 | curstate->pos.v += 26; |
| 655 | } |
| 656 | |
| 657 | #if !TARGET_API_MAC_CARBON |
| 658 | static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant, |
| 659 | ControlRef control, |
| 660 | ControlDefProcMessage msg, |
| 661 | SInt32 param) |
| 662 | { |
| 663 | RgnHandle rgn; |
| 664 | Rect rect; |
| 665 | int oval; |
| 666 | |
| 667 | switch (msg) { |
| 668 | case drawCntl: |
| 669 | if ((*control)->contrlVis) { |
| 670 | rect = (*control)->contrlRect; |
| 671 | PenNormal(); |
| 672 | PenSize(3, 3); |
| 673 | oval = (rect.bottom - rect.top) / 2 + 2; |
| 674 | FrameRoundRect(&rect, oval, oval); |
| 675 | } |
| 676 | return 0; |
| 677 | case calcCRgns: |
| 678 | if (param & (1 << 31)) { |
| 679 | param &= ~(1 << 31); |
| 680 | goto calcthumbrgn; |
| 681 | } |
| 682 | /* FALLTHROUGH */ |
| 683 | case calcCntlRgn: |
| 684 | rgn = (RgnHandle)param; |
| 685 | RectRgn(rgn, &(*control)->contrlRect); |
| 686 | return 0; |
| 687 | case calcThumbRgn: |
| 688 | calcthumbrgn: |
| 689 | rgn = (RgnHandle)param; |
| 690 | SetEmptyRgn(rgn); |
| 691 | return 0; |
| 692 | } |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | #endif |
| 697 | |
| 698 | static void macctrl_popup(struct macctrls *mcs, WindowPtr window, |
| 699 | struct mac_layoutstate *curstate, |
| 700 | union control *ctrl) |
| 701 | { |
| 702 | union macctrl *mc = smalloc(sizeof *mc); |
| 703 | Rect bounds; |
| 704 | Str255 title; |
| 705 | unsigned int labelwidth; |
| 706 | static int nextmenuid = MENU_MIN; |
| 707 | int menuid; |
| 708 | MenuRef menu; |
| 709 | |
| 710 | /* |
| 711 | * <http://developer.apple.com/qa/tb/tb42.html> explains how to |
| 712 | * create a popup menu with dynamic content. |
| 713 | */ |
| 714 | assert(ctrl->listbox.height == 0); |
| 715 | assert(!ctrl->listbox.draglist); |
| 716 | assert(!ctrl->listbox.multisel); |
| 717 | |
| 718 | fprintf(stderr, " label = %s\n", ctrl->listbox.label); |
| 719 | fprintf(stderr, " percentwidth = %d\n", ctrl->listbox.percentwidth); |
| 720 | |
| 721 | mc->generic.type = MACCTRL_POPUP; |
| 722 | mc->generic.ctrl = ctrl; |
| 723 | mc->generic.privdata = NULL; |
| 724 | c2pstrcpy(title, ctrl->button.label); |
| 725 | |
| 726 | /* Find a spare menu ID and create the menu */ |
| 727 | while (GetMenuHandle(nextmenuid) != NULL) |
| 728 | if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN; |
| 729 | menuid = nextmenuid++; |
| 730 | menu = NewMenu(menuid, "\pdummy"); |
| 731 | if (menu == NULL) return; |
| 732 | mc->popup.menu = menu; |
| 733 | mc->popup.menuid = menuid; |
| 734 | InsertMenu(menu, kInsertHierarchicalMenu); |
| 735 | |
| 736 | /* The menu starts off empty */ |
| 737 | mc->popup.nids = 0; |
| 738 | mc->popup.ids = NULL; |
| 739 | |
| 740 | bounds.left = curstate->pos.h; |
| 741 | bounds.right = bounds.left + curstate->width; |
| 742 | bounds.top = curstate->pos.v; |
| 743 | bounds.bottom = bounds.top + 20; |
| 744 | /* XXX handle percentwidth == 100 */ |
| 745 | labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100; |
| 746 | mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE, |
| 747 | popupTitleLeftJust, menuid, labelwidth, |
| 748 | popupMenuProc + popupFixedWidth, (long)mc); |
| 749 | add234(mcs->byctrl, mc); |
| 750 | curstate->pos.v += 26; |
| 751 | mc->generic.next = mcs->panels[curstate->panelnum]; |
| 752 | mcs->panels[curstate->panelnum] = mc; |
| 753 | ctrlevent(mcs, mc, EVENT_REFRESH); |
| 754 | } |
| 755 | |
| 756 | |
| 757 | void macctrl_activate(WindowPtr window, EventRecord *event) |
| 758 | { |
| 759 | struct macctrls *mcs = mac_winctrls(window); |
| 760 | Boolean active = (event->modifiers & activeFlag) != 0; |
| 761 | GrafPtr saveport; |
| 762 | int i, j; |
| 763 | ControlPartCode state; |
| 764 | union macctrl *mc; |
| 765 | |
| 766 | GetPort(&saveport); |
| 767 | SetPort((GrafPtr)GetWindowPort(window)); |
| 768 | if (mac_gestalts.apprvers >= 0x100) |
| 769 | SetThemeWindowBackground(window, active ? |
| 770 | kThemeBrushModelessDialogBackgroundActive : |
| 771 | kThemeBrushModelessDialogBackgroundInactive, |
| 772 | TRUE); |
| 773 | state = active ? kControlNoPart : kControlInactivePart; |
| 774 | for (i = 0; i <= mcs->curpanel; i += mcs->curpanel) |
| 775 | for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) |
| 776 | switch (mc->generic.type) { |
| 777 | case MACCTRL_TEXT: |
| 778 | HiliteControl(mc->text.tbctrl, state); |
| 779 | break; |
| 780 | case MACCTRL_EDITBOX: |
| 781 | HiliteControl(mc->editbox.tbctrl, state); |
| 782 | break; |
| 783 | case MACCTRL_RADIO: |
| 784 | for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++) |
| 785 | HiliteControl(mc->radio.tbctrls[j], state); |
| 786 | break; |
| 787 | case MACCTRL_CHECKBOX: |
| 788 | HiliteControl(mc->checkbox.tbctrl, state); |
| 789 | break; |
| 790 | case MACCTRL_BUTTON: |
| 791 | HiliteControl(mc->button.tbctrl, state); |
| 792 | break; |
| 793 | case MACCTRL_POPUP: |
| 794 | HiliteControl(mc->popup.tbctrl, state); |
| 795 | break; |
| 796 | } |
| 797 | SetPort(saveport); |
| 798 | } |
| 799 | |
| 800 | void macctrl_click(WindowPtr window, EventRecord *event) |
| 801 | { |
| 802 | Point mouse; |
| 803 | ControlHandle control; |
| 804 | int part, trackresult; |
| 805 | GrafPtr saveport; |
| 806 | union macctrl *mc; |
| 807 | struct macctrls *mcs = mac_winctrls(window); |
| 808 | int i; |
| 809 | UInt32 features; |
| 810 | |
| 811 | GetPort(&saveport); |
| 812 | SetPort((GrafPtr)GetWindowPort(window)); |
| 813 | mouse = event->where; |
| 814 | GlobalToLocal(&mouse); |
| 815 | part = FindControl(mouse, window, &control); |
| 816 | if (control != NULL) { |
| 817 | mc = (union macctrl *)GetControlReference(control); |
| 818 | if (mac_gestalts.apprvers >= 0x100) { |
| 819 | if (GetControlFeatures(control, &features) == noErr && |
| 820 | (features & kControlSupportsFocus) && |
| 821 | (features & kControlGetsFocusOnClick)) |
| 822 | SetKeyboardFocus(window, control, part); |
| 823 | trackresult = HandleControlClick(control, mouse, event->modifiers, |
| 824 | (ControlActionUPP)-1); |
| 825 | } else { |
| 826 | #if !TARGET_API_MAC_CARBON |
| 827 | if (mc->generic.type == MACCTRL_EDITBOX && |
| 828 | control == mc->editbox.tbctrl) { |
| 829 | TEHandle te = (TEHandle)(*control)->contrlData; |
| 830 | |
| 831 | macctrl_setfocus(mcs, mc); |
| 832 | TEClick(mouse, !!(event->modifiers & shiftKey), te); |
| 833 | goto done; |
| 834 | } |
| 835 | #endif |
| 836 | trackresult = TrackControl(control, mouse, (ControlActionUPP)-1); |
| 837 | } |
| 838 | switch (mc->generic.type) { |
| 839 | case MACCTRL_RADIO: |
| 840 | if (trackresult != 0) { |
| 841 | for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++) |
| 842 | if (mc->radio.tbctrls[i] == control) |
| 843 | SetControlValue(mc->radio.tbctrls[i], |
| 844 | kControlRadioButtonCheckedValue); |
| 845 | else |
| 846 | SetControlValue(mc->radio.tbctrls[i], |
| 847 | kControlRadioButtonUncheckedValue); |
| 848 | ctrlevent(mcs, mc, EVENT_VALCHANGE); |
| 849 | } |
| 850 | break; |
| 851 | case MACCTRL_CHECKBOX: |
| 852 | if (trackresult != 0) { |
| 853 | SetControlValue(control, !GetControlValue(control)); |
| 854 | ctrlevent(mcs, mc, EVENT_VALCHANGE); |
| 855 | } |
| 856 | break; |
| 857 | case MACCTRL_BUTTON: |
| 858 | if (trackresult != 0) |
| 859 | ctrlevent(mcs, mc, EVENT_ACTION); |
| 860 | break; |
| 861 | case MACCTRL_POPUP: |
| 862 | ctrlevent(mcs, mc, EVENT_SELCHANGE); |
| 863 | break; |
| 864 | } |
| 865 | } |
| 866 | done: |
| 867 | SetPort(saveport); |
| 868 | } |
| 869 | |
| 870 | void macctrl_key(WindowPtr window, EventRecord *event) |
| 871 | { |
| 872 | ControlRef control; |
| 873 | struct macctrls *mcs = mac_winctrls(window); |
| 874 | union macctrl *mc; |
| 875 | |
| 876 | if (mac_gestalts.apprvers >= 0x100 && |
| 877 | GetKeyboardFocus(window, &control) == noErr && control != NULL) { |
| 878 | HandleControlKey(control, (event->message & keyCodeMask) >> 8, |
| 879 | event->message & charCodeMask, event->modifiers); |
| 880 | mc = (union macctrl *)GetControlReference(control); |
| 881 | ctrlevent(mcs, mc, EVENT_VALCHANGE); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | void macctrl_update(WindowPtr window) |
| 886 | { |
| 887 | #if TARGET_API_MAC_CARBON |
| 888 | RgnHandle visrgn; |
| 889 | #endif |
| 890 | Rect rect; |
| 891 | GrafPtr saveport; |
| 892 | |
| 893 | BeginUpdate(window); |
| 894 | GetPort(&saveport); |
| 895 | SetPort((GrafPtr)GetWindowPort(window)); |
| 896 | if (mac_gestalts.apprvers >= 0x101) { |
| 897 | #if TARGET_API_MAC_CARBON |
| 898 | GetPortBounds(GetWindowPort(window), &rect); |
| 899 | #else |
| 900 | rect = window->portRect; |
| 901 | #endif |
| 902 | InsetRect(&rect, -1, -1); |
| 903 | DrawThemeModelessDialogFrame(&rect, mac_frontwindow() == window ? |
| 904 | kThemeStateActive : kThemeStateInactive); |
| 905 | } |
| 906 | #if TARGET_API_MAC_CARBON |
| 907 | visrgn = NewRgn(); |
| 908 | GetPortVisibleRegion(GetWindowPort(window), visrgn); |
| 909 | UpdateControls(window, visrgn); |
| 910 | DisposeRgn(visrgn); |
| 911 | #else |
| 912 | UpdateControls(window, window->visRgn); |
| 913 | #endif |
| 914 | SetPort(saveport); |
| 915 | EndUpdate(window); |
| 916 | } |
| 917 | |
| 918 | #if TARGET_API_MAC_CARBON |
| 919 | #define EnableItem EnableMenuItem |
| 920 | #define DisableItem DisableMenuItem |
| 921 | #endif |
| 922 | void macctrl_adjustmenus(WindowPtr window) |
| 923 | { |
| 924 | MenuHandle menu; |
| 925 | |
| 926 | menu = GetMenuHandle(mFile); |
| 927 | DisableItem(menu, iSave); /* XXX enable if modified */ |
| 928 | EnableItem(menu, iSaveAs); |
| 929 | EnableItem(menu, iDuplicate); |
| 930 | |
| 931 | menu = GetMenuHandle(mEdit); |
| 932 | DisableItem(menu, 0); |
| 933 | } |
| 934 | |
| 935 | void macctrl_close(WindowPtr window) |
| 936 | { |
| 937 | struct macctrls *mcs = mac_winctrls(window); |
| 938 | union macctrl *mc; |
| 939 | |
| 940 | /* |
| 941 | * Mostly, we don't bother disposing of the Toolbox controls, |
| 942 | * since that will happen automatically when the window is |
| 943 | * disposed of. Popup menus are an exception, because we have to |
| 944 | * dispose of the menu ourselves, and doing that while the control |
| 945 | * still holds a reference to it seems rude. |
| 946 | */ |
| 947 | while ((mc = index234(mcs->byctrl, 0)) != NULL) { |
| 948 | if (mc->generic.privdata != NULL && mc->generic.freeprivdata) |
| 949 | sfree(mc->generic.privdata); |
| 950 | switch (mc->generic.type) { |
| 951 | case MACCTRL_POPUP: |
| 952 | DisposeControl(mc->popup.tbctrl); |
| 953 | DeleteMenu(mc->popup.menuid); |
| 954 | DisposeMenu(mc->popup.menu); |
| 955 | break; |
| 956 | } |
| 957 | del234(mcs->byctrl, mc); |
| 958 | sfree(mc); |
| 959 | } |
| 960 | |
| 961 | freetree234(mcs->byctrl); |
| 962 | mcs->byctrl = NULL; |
| 963 | sfree(mcs->panels); |
| 964 | mcs->panels = NULL; |
| 965 | } |
| 966 | |
| 967 | void dlg_update_start(union control *ctrl, void *dlg) |
| 968 | { |
| 969 | |
| 970 | /* No-op for now */ |
| 971 | } |
| 972 | |
| 973 | void dlg_update_done(union control *ctrl, void *dlg) |
| 974 | { |
| 975 | |
| 976 | /* No-op for now */ |
| 977 | } |
| 978 | |
| 979 | void dlg_set_focus(union control *ctrl, void *dlg) |
| 980 | { |
| 981 | |
| 982 | if (mac_gestalts.apprvers >= 0x100) { |
| 983 | /* Use SetKeyboardFocus() */ |
| 984 | } else { |
| 985 | /* Do our own mucking around */ |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | union control *dlg_last_focused(union control *ctrl, void *dlg) |
| 990 | { |
| 991 | |
| 992 | return NULL; |
| 993 | } |
| 994 | |
| 995 | void dlg_beep(void *dlg) |
| 996 | { |
| 997 | |
| 998 | SysBeep(30); |
| 999 | } |
| 1000 | |
| 1001 | void dlg_error_msg(void *dlg, char *msg) |
| 1002 | { |
| 1003 | Str255 pmsg; |
| 1004 | |
| 1005 | c2pstrcpy(pmsg, msg); |
| 1006 | ParamText(pmsg, NULL, NULL, NULL); |
| 1007 | StopAlert(128, NULL); |
| 1008 | } |
| 1009 | |
| 1010 | void dlg_end(void *dlg, int value) |
| 1011 | { |
| 1012 | |
| 1013 | }; |
| 1014 | |
| 1015 | void dlg_refresh(union control *ctrl, void *dlg) |
| 1016 | { |
| 1017 | struct macctrls *mcs = dlg; |
| 1018 | union macctrl *mc; |
| 1019 | |
| 1020 | if (ctrl == NULL) |
| 1021 | return; /* FIXME */ |
| 1022 | mc = findbyctrl(mcs, ctrl); |
| 1023 | assert(mc != NULL); |
| 1024 | ctrlevent(mcs, mc, EVENT_REFRESH); |
| 1025 | }; |
| 1026 | |
| 1027 | void *dlg_get_privdata(union control *ctrl, void *dlg) |
| 1028 | { |
| 1029 | struct macctrls *mcs = dlg; |
| 1030 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1031 | |
| 1032 | assert(mc != NULL); |
| 1033 | return mc->generic.privdata; |
| 1034 | } |
| 1035 | |
| 1036 | void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr) |
| 1037 | { |
| 1038 | struct macctrls *mcs = dlg; |
| 1039 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1040 | |
| 1041 | assert(mc != NULL); |
| 1042 | mc->generic.privdata = ptr; |
| 1043 | mc->generic.freeprivdata = FALSE; |
| 1044 | } |
| 1045 | |
| 1046 | void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size) |
| 1047 | { |
| 1048 | struct macctrls *mcs = dlg; |
| 1049 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1050 | |
| 1051 | assert(mc != NULL); |
| 1052 | mc->generic.privdata = smalloc(size); |
| 1053 | mc->generic.freeprivdata = TRUE; |
| 1054 | return mc->generic.privdata; |
| 1055 | } |
| 1056 | |
| 1057 | |
| 1058 | /* |
| 1059 | * Radio Button control |
| 1060 | */ |
| 1061 | |
| 1062 | void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton) |
| 1063 | { |
| 1064 | struct macctrls *mcs = dlg; |
| 1065 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1066 | int i; |
| 1067 | |
| 1068 | assert(mc != NULL); |
| 1069 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
| 1070 | if (i == whichbutton) |
| 1071 | SetControlValue(mc->radio.tbctrls[i], |
| 1072 | kControlRadioButtonCheckedValue); |
| 1073 | else |
| 1074 | SetControlValue(mc->radio.tbctrls[i], |
| 1075 | kControlRadioButtonUncheckedValue); |
| 1076 | } |
| 1077 | |
| 1078 | }; |
| 1079 | |
| 1080 | int dlg_radiobutton_get(union control *ctrl, void *dlg) |
| 1081 | { |
| 1082 | struct macctrls *mcs = dlg; |
| 1083 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1084 | int i; |
| 1085 | |
| 1086 | assert(mc != NULL); |
| 1087 | for (i = 0; i < ctrl->radio.nbuttons; i++) { |
| 1088 | if (GetControlValue(mc->radio.tbctrls[i]) == |
| 1089 | kControlRadioButtonCheckedValue) |
| 1090 | return i; |
| 1091 | } |
| 1092 | return -1; |
| 1093 | }; |
| 1094 | |
| 1095 | |
| 1096 | /* |
| 1097 | * Check Box control |
| 1098 | */ |
| 1099 | |
| 1100 | void dlg_checkbox_set(union control *ctrl, void *dlg, int checked) |
| 1101 | { |
| 1102 | struct macctrls *mcs = dlg; |
| 1103 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1104 | |
| 1105 | assert(mc != NULL); |
| 1106 | SetControlValue(mc->checkbox.tbctrl, |
| 1107 | checked ? kControlCheckBoxCheckedValue : |
| 1108 | kControlCheckBoxUncheckedValue); |
| 1109 | } |
| 1110 | |
| 1111 | int dlg_checkbox_get(union control *ctrl, void *dlg) |
| 1112 | { |
| 1113 | struct macctrls *mcs = dlg; |
| 1114 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1115 | |
| 1116 | assert(mc != NULL); |
| 1117 | return GetControlValue(mc->checkbox.tbctrl); |
| 1118 | } |
| 1119 | |
| 1120 | |
| 1121 | /* |
| 1122 | * Edit Box control |
| 1123 | */ |
| 1124 | |
| 1125 | void dlg_editbox_set(union control *ctrl, void *dlg, char const *text) |
| 1126 | { |
| 1127 | struct macctrls *mcs = dlg; |
| 1128 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1129 | GrafPtr saveport; |
| 1130 | |
| 1131 | assert(mc != NULL); |
| 1132 | assert(mc->generic.type == MACCTRL_EDITBOX); |
| 1133 | GetPort(&saveport); |
| 1134 | SetPort((GrafPtr)(GetWindowPort(mcs->window))); |
| 1135 | if (mac_gestalts.apprvers >= 0x100) |
| 1136 | SetControlData(mc->editbox.tbctrl, kControlEntireControl, |
| 1137 | ctrl->editbox.password ? |
| 1138 | kControlEditTextPasswordTag : |
| 1139 | kControlEditTextTextTag, |
| 1140 | strlen(text), text); |
| 1141 | #if !TARGET_API_MAC_CARBON |
| 1142 | else |
| 1143 | TESetText(text, strlen(text), |
| 1144 | (TEHandle)(*mc->editbox.tbctrl)->contrlData); |
| 1145 | #endif |
| 1146 | DrawOneControl(mc->editbox.tbctrl); |
| 1147 | SetPort(saveport); |
| 1148 | } |
| 1149 | |
| 1150 | void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length) |
| 1151 | { |
| 1152 | struct macctrls *mcs = dlg; |
| 1153 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1154 | Size olen; |
| 1155 | |
| 1156 | assert(mc != NULL); |
| 1157 | assert(mc->generic.type == MACCTRL_EDITBOX); |
| 1158 | if (mac_gestalts.apprvers >= 0x100) { |
| 1159 | if (GetControlData(mc->editbox.tbctrl, kControlEntireControl, |
| 1160 | ctrl->editbox.password ? |
| 1161 | kControlEditTextPasswordTag : |
| 1162 | kControlEditTextTextTag, |
| 1163 | length - 1, buffer, &olen) != noErr) |
| 1164 | olen = 0; |
| 1165 | if (olen > length - 1) |
| 1166 | olen = length - 1; |
| 1167 | } |
| 1168 | #if !TARGET_API_MAC_CARBON |
| 1169 | else { |
| 1170 | TEHandle te = (TEHandle)(*mc->editbox.tbctrl)->contrlData; |
| 1171 | |
| 1172 | olen = (*te)->teLength; |
| 1173 | if (olen > length - 1) |
| 1174 | olen = length - 1; |
| 1175 | memcpy(buffer, *(*te)->hText, olen); |
| 1176 | } |
| 1177 | #endif |
| 1178 | buffer[olen] = '\0'; |
| 1179 | fprintf(stderr, "dlg_editbox_get: %s\n", buffer); |
| 1180 | } |
| 1181 | |
| 1182 | |
| 1183 | /* |
| 1184 | * List Box control |
| 1185 | */ |
| 1186 | |
| 1187 | static void dlg_macpopup_clear(union control *ctrl, void *dlg) |
| 1188 | { |
| 1189 | struct macctrls *mcs = dlg; |
| 1190 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1191 | MenuRef menu = mc->popup.menu; |
| 1192 | unsigned int i, n; |
| 1193 | |
| 1194 | fprintf(stderr, " popup_clear\n"); |
| 1195 | n = CountMenuItems(menu); |
| 1196 | for (i = 0; i < n; i++) |
| 1197 | DeleteMenuItem(menu, n - i); |
| 1198 | mc->popup.nids = 0; |
| 1199 | sfree(mc->popup.ids); |
| 1200 | mc->popup.ids = NULL; |
| 1201 | SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); |
| 1202 | } |
| 1203 | |
| 1204 | void dlg_listbox_clear(union control *ctrl, void *dlg) |
| 1205 | { |
| 1206 | |
| 1207 | if (ctrl->listbox.height == 0) |
| 1208 | dlg_macpopup_clear(ctrl, dlg); |
| 1209 | } |
| 1210 | |
| 1211 | static void dlg_macpopup_del(union control *ctrl, void *dlg, int index) |
| 1212 | { |
| 1213 | struct macctrls *mcs = dlg; |
| 1214 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1215 | MenuRef menu = mc->popup.menu; |
| 1216 | |
| 1217 | fprintf(stderr, " popup_del %d\n", index); |
| 1218 | DeleteMenuItem(menu, index + 1); |
| 1219 | if (mc->popup.ids != NULL) |
| 1220 | memcpy(mc->popup.ids + index, mc->popup.ids + index + 1, |
| 1221 | (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids)); |
| 1222 | SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); |
| 1223 | } |
| 1224 | |
| 1225 | void dlg_listbox_del(union control *ctrl, void *dlg, int index) |
| 1226 | { |
| 1227 | |
| 1228 | if (ctrl->listbox.height == 0) |
| 1229 | dlg_macpopup_del(ctrl, dlg, index); |
| 1230 | } |
| 1231 | |
| 1232 | static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text) |
| 1233 | { |
| 1234 | struct macctrls *mcs = dlg; |
| 1235 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1236 | MenuRef menu = mc->popup.menu; |
| 1237 | Str255 itemstring; |
| 1238 | |
| 1239 | fprintf(stderr, " popup_add %s\n", text); |
| 1240 | assert(text[0] != '\0'); |
| 1241 | c2pstrcpy(itemstring, text); |
| 1242 | AppendMenu(menu, "\pdummy"); |
| 1243 | SetMenuItemText(menu, CountMenuItems(menu), itemstring); |
| 1244 | SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); |
| 1245 | } |
| 1246 | |
| 1247 | void dlg_listbox_add(union control *ctrl, void *dlg, char const *text) |
| 1248 | { |
| 1249 | |
| 1250 | if (ctrl->listbox.height == 0) |
| 1251 | dlg_macpopup_add(ctrl, dlg, text); |
| 1252 | } |
| 1253 | |
| 1254 | static void dlg_macpopup_addwithid(union control *ctrl, void *dlg, |
| 1255 | char const *text, int id) |
| 1256 | { |
| 1257 | struct macctrls *mcs = dlg; |
| 1258 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1259 | MenuRef menu = mc->popup.menu; |
| 1260 | unsigned int index; |
| 1261 | |
| 1262 | fprintf(stderr, " popup_addwthindex %s, %d\n", text, id); |
| 1263 | dlg_macpopup_add(ctrl, dlg, text); |
| 1264 | index = CountMenuItems(menu) - 1; |
| 1265 | if (mc->popup.nids <= index) { |
| 1266 | mc->popup.nids = index + 1; |
| 1267 | mc->popup.ids = srealloc(mc->popup.ids, |
| 1268 | mc->popup.nids * sizeof(*mc->popup.ids)); |
| 1269 | } |
| 1270 | mc->popup.ids[index] = id; |
| 1271 | } |
| 1272 | |
| 1273 | void dlg_listbox_addwithid(union control *ctrl, void *dlg, |
| 1274 | char const *text, int id) |
| 1275 | { |
| 1276 | |
| 1277 | if (ctrl->listbox.height == 0) |
| 1278 | dlg_macpopup_addwithid(ctrl, dlg, text, id); |
| 1279 | } |
| 1280 | |
| 1281 | int dlg_listbox_getid(union control *ctrl, void *dlg, int index) |
| 1282 | { |
| 1283 | struct macctrls *mcs = dlg; |
| 1284 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1285 | |
| 1286 | if (ctrl->listbox.height == 0) { |
| 1287 | assert(mc->popup.ids != NULL && mc->popup.nids > index); |
| 1288 | return mc->popup.ids[index]; |
| 1289 | } |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | int dlg_listbox_index(union control *ctrl, void *dlg) |
| 1294 | { |
| 1295 | struct macctrls *mcs = dlg; |
| 1296 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1297 | |
| 1298 | if (ctrl->listbox.height == 0) |
| 1299 | return GetControlValue(mc->popup.tbctrl) - 1; |
| 1300 | return 0; |
| 1301 | }; |
| 1302 | |
| 1303 | int dlg_listbox_issel(union control *ctrl, void *dlg, int index) |
| 1304 | { |
| 1305 | struct macctrls *mcs = dlg; |
| 1306 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1307 | |
| 1308 | if (ctrl->listbox.height == 0) |
| 1309 | return GetControlValue(mc->popup.tbctrl) - 1 == index; |
| 1310 | return 0; |
| 1311 | }; |
| 1312 | |
| 1313 | void dlg_listbox_select(union control *ctrl, void *dlg, int index) |
| 1314 | { |
| 1315 | struct macctrls *mcs = dlg; |
| 1316 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1317 | |
| 1318 | if (ctrl->listbox.height == 0) |
| 1319 | SetControlValue(mc->popup.tbctrl, index + 1); |
| 1320 | }; |
| 1321 | |
| 1322 | |
| 1323 | /* |
| 1324 | * Text control |
| 1325 | */ |
| 1326 | |
| 1327 | void dlg_text_set(union control *ctrl, void *dlg, char const *text) |
| 1328 | { |
| 1329 | struct macctrls *mcs = dlg; |
| 1330 | union macctrl *mc = findbyctrl(mcs, ctrl); |
| 1331 | Str255 title; |
| 1332 | |
| 1333 | assert(mc != NULL); |
| 1334 | if (mac_gestalts.apprvers >= 0x100) |
| 1335 | SetControlData(mc->text.tbctrl, kControlEntireControl, |
| 1336 | kControlStaticTextTextTag, strlen(text), text); |
| 1337 | else { |
| 1338 | c2pstrcpy(title, text); |
| 1339 | SetControlTitle(mc->text.tbctrl, title); |
| 1340 | } |
| 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | /* |
| 1345 | * File Selector control |
| 1346 | */ |
| 1347 | |
| 1348 | void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn) |
| 1349 | { |
| 1350 | |
| 1351 | } |
| 1352 | |
| 1353 | void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn) |
| 1354 | { |
| 1355 | |
| 1356 | } |
| 1357 | |
| 1358 | |
| 1359 | /* |
| 1360 | * Font Selector control |
| 1361 | */ |
| 1362 | |
| 1363 | void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fn) |
| 1364 | { |
| 1365 | |
| 1366 | } |
| 1367 | |
| 1368 | void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fn) |
| 1369 | { |
| 1370 | |
| 1371 | } |
| 1372 | |
| 1373 | |
| 1374 | /* |
| 1375 | * Printer enumeration |
| 1376 | */ |
| 1377 | |
| 1378 | printer_enum *printer_start_enum(int *nprinters) |
| 1379 | { |
| 1380 | |
| 1381 | *nprinters = 0; |
| 1382 | return NULL; |
| 1383 | } |
| 1384 | |
| 1385 | char *printer_get_name(printer_enum *pe, int thing) |
| 1386 | { |
| 1387 | |
| 1388 | return "<none>"; |
| 1389 | } |
| 1390 | |
| 1391 | void printer_finish_enum(printer_enum *pe) |
| 1392 | { |
| 1393 | |
| 1394 | } |
| 1395 | |
| 1396 | |
| 1397 | /* |
| 1398 | * Colour selection stuff |
| 1399 | */ |
| 1400 | |
| 1401 | void dlg_coloursel_start(union control *ctrl, void *dlg, |
| 1402 | int r, int g, int b) |
| 1403 | { |
| 1404 | |
| 1405 | } |
| 1406 | |
| 1407 | int dlg_coloursel_results(union control *ctrl, void *dlg, |
| 1408 | int *r, int *g, int *b) |
| 1409 | { |
| 1410 | |
| 1411 | return 0; |
| 1412 | } |
| 1413 | |
| 1414 | /* |
| 1415 | * Local Variables: |
| 1416 | * c-file-style: "simon" |
| 1417 | * End: |
| 1418 | */ |