X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/0bd8d76d08b2f222bc624a9b85e482a6e2629710..0e9ce565a659cf20f1d9d83c9f0d4236fd78f8e2:/mac/macctrls.c diff --git a/mac/macctrls.c b/mac/macctrls.c index bc745140..4aa9ce92 100644 --- a/mac/macctrls.c +++ b/mac/macctrls.c @@ -1,4 +1,4 @@ -/* $Id: macctrls.c,v 1.3 2003/03/18 19:06:51 simon Exp $ */ +/* $Id: macctrls.c,v 1.16 2003/03/28 00:06:17 ben Exp $ */ /* * Copyright (c) 2003 Ben Harris * All rights reserved. @@ -29,27 +29,45 @@ #include #include #include +#include #include +#include #include +#include #include +#include #include +#include +#include + #include "putty.h" #include "mac.h" #include "macresid.h" #include "dialog.h" #include "tree234.h" +/* Range of menu IDs for popup menus */ +#define MENU_MIN 1024 +#define MENU_MAX 2048 + + union macctrl { struct macctrl_generic { enum { MACCTRL_TEXT, + MACCTRL_EDITBOX, MACCTRL_RADIO, MACCTRL_CHECKBOX, - MACCTRL_BUTTON + MACCTRL_BUTTON, + MACCTRL_POPUP } type; /* Template from which this was generated */ union control *ctrl; + /* Next control in this panel */ + union macctrl *next; + void *privdata; + int freeprivdata; } generic; struct { struct macctrl_generic generic; @@ -57,6 +75,10 @@ union macctrl { } text; struct { struct macctrl_generic generic; + ControlRef tbctrl; + } editbox; + struct { + struct macctrl_generic generic; ControlRef *tbctrls; } radio; struct { @@ -67,32 +89,53 @@ union macctrl { struct macctrl_generic generic; ControlRef tbctrl; } button; + struct { + struct macctrl_generic generic; + ControlRef tbctrl; + MenuRef menu; + int menuid; + unsigned int nids; + int *ids; + } popup; }; struct mac_layoutstate { Point pos; unsigned int width; + unsigned int panelnum; }; #define ctrlevent(mcs, mc, event) do { \ if ((mc)->generic.ctrl->generic.handler != NULL) \ - (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mc),\ + (*(mc)->generic.ctrl->generic.handler)((mc)->generic.ctrl, (mcs),\ (mcs)->data, (event)); \ } while (0) +#define findbyctrl(mcs, ctrl) \ + find234((mcs)->byctrl, (ctrl), macctrl_cmp_byctrl_find) + static void macctrl_layoutset(struct mac_layoutstate *, struct controlset *, WindowPtr, struct macctrls *); +static void macctrl_switchtopanel(struct macctrls *, unsigned int); static void macctrl_text(struct macctrls *, WindowPtr, struct mac_layoutstate *, union control *); +static void macctrl_editbox(struct macctrls *, WindowPtr, + struct mac_layoutstate *, union control *); static void macctrl_radio(struct macctrls *, WindowPtr, struct mac_layoutstate *, union control *); static void macctrl_checkbox(struct macctrls *, WindowPtr, struct mac_layoutstate *, union control *); static void macctrl_button(struct macctrls *, WindowPtr, struct mac_layoutstate *, union control *); +static void macctrl_popup(struct macctrls *, WindowPtr, + struct mac_layoutstate *, union control *); #if !TARGET_API_MAC_CARBON static pascal SInt32 macctrl_sys7_text_cdef(SInt16, ControlRef, ControlDefProcMessage, SInt32); +static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16, ControlRef, + ControlDefProcMessage, SInt32); +static pascal SInt32 macctrl_sys7_default_cdef(SInt16, ControlRef, + ControlDefProcMessage, SInt32); #endif #if !TARGET_API_MAC_CARBON @@ -119,6 +162,10 @@ static void macctrl_init() if (inited) return; cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Text); (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_text_cdef); + cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_EditBox); + (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_editbox_cdef); + cdef = (PatchCDEF)GetResource(kControlDefProcResourceType, CDEF_Default); + (*cdef)->theUPP = NewControlDefProc(macctrl_sys7_default_cdef); inited = 1; #endif } @@ -137,6 +184,19 @@ static int macctrl_cmp_byctrl(void *av, void *bv) return 0; } +static int macctrl_cmp_byctrl_find(void *av, void *bv) +{ + union control *a = (union control *)av; + union macctrl *b = (union macctrl *)bv; + + if (a < b->generic.ctrl) + return -1; + else if (a > b->generic.ctrl) + return +1; + else + return 0; +} + void macctrl_layoutbox(struct controlbox *cb, WindowPtr window, struct macctrls *mcs) { @@ -152,13 +212,30 @@ void macctrl_layoutbox(struct controlbox *cb, WindowPtr window, rect = window->portRect; #endif curstate.pos.h = rect.left + 13; - curstate.pos.v = rect.top + 13; + curstate.pos.v = rect.bottom - 59; curstate.width = rect.right - rect.left - (13 * 2); if (mac_gestalts.apprvers >= 0x100) CreateRootControl(window, &root); + mcs->window = window; mcs->byctrl = newtree234(macctrl_cmp_byctrl); - for (i = 0; i < cb->nctrlsets; i++) + /* Count the number of panels */ + mcs->npanels = 1; + for (i = 1; i < cb->nctrlsets; i++) + if (strcmp(cb->ctrlsets[i]->pathname, cb->ctrlsets[i-1]->pathname)) + mcs->npanels++; + mcs->panels = smalloc(sizeof(*mcs->panels) * mcs->npanels); + memset(mcs->panels, 0, sizeof(*mcs->panels) * mcs->npanels); + curstate.panelnum = 0; + for (i = 0; i < cb->nctrlsets; i++) { + if (i > 0 && strcmp(cb->ctrlsets[i]->pathname, + cb->ctrlsets[i-1]->pathname)) { + curstate.pos.v = rect.top + 13; + curstate.panelnum++; + assert(curstate.panelnum < mcs->npanels); + } macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs); + } + macctrl_switchtopanel(mcs, 1); } static void macctrl_layoutset(struct mac_layoutstate *curstate, @@ -168,6 +245,7 @@ static void macctrl_layoutset(struct mac_layoutstate *curstate, unsigned int i; fprintf(stderr, "--- begin set ---\n"); + fprintf(stderr, "pathname = %s\n", s->pathname); if (s->boxname && *s->boxname) fprintf(stderr, "boxname = %s\n", s->boxname); if (s->boxtitle) @@ -196,6 +274,9 @@ static void macctrl_layoutset(struct mac_layoutstate *curstate, case CTRL_TEXT: macctrl_text(mcs, window, curstate, ctrl); break; + case CTRL_EDITBOX: + macctrl_editbox(mcs, window, curstate, ctrl); + break; case CTRL_RADIO: macctrl_radio(mcs, window, curstate, ctrl); break; @@ -205,11 +286,50 @@ static void macctrl_layoutset(struct mac_layoutstate *curstate, case CTRL_BUTTON: macctrl_button(mcs, window, curstate, ctrl); break; - + case CTRL_LISTBOX: + if (ctrl->listbox.height == 0) + macctrl_popup(mcs, window, curstate, ctrl); + break; } } } +static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which) +{ + unsigned int i, j; + union macctrl *mc; + +#define hideshow(c) do { \ + if (i == which) ShowControl(c); else HideControl(c); \ +} while (0) + + mcs->curpanel = which; + /* Panel 0 is special and always visible. */ + for (i = 1; i < mcs->npanels; i++) + for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) + switch (mc->generic.type) { + case MACCTRL_TEXT: + hideshow(mc->text.tbctrl); + break; + case MACCTRL_EDITBOX: + hideshow(mc->editbox.tbctrl); + break; + case MACCTRL_RADIO: + for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++) + hideshow(mc->radio.tbctrls[j]); + break; + case MACCTRL_CHECKBOX: + hideshow(mc->checkbox.tbctrl); + break; + case MACCTRL_BUTTON: + hideshow(mc->button.tbctrl); + break; + case MACCTRL_POPUP: + hideshow(mc->popup.tbctrl); + break; + } +} + static void macctrl_text(struct macctrls *mcs, WindowPtr window, struct mac_layoutstate *curstate, union control *ctrl) @@ -220,6 +340,7 @@ static void macctrl_text(struct macctrls *mcs, WindowPtr window, fprintf(stderr, " label = %s\n", ctrl->text.label); mc->generic.type = MACCTRL_TEXT; mc->generic.ctrl = ctrl; + mc->generic.privdata = NULL; bounds.left = curstate->pos.h; bounds.right = bounds.left + curstate->width; bounds.top = curstate->pos.v; @@ -247,6 +368,8 @@ static void macctrl_text(struct macctrls *mcs, WindowPtr window, SYS7_TEXT_PROC, (long)mc); } add234(mcs->byctrl, mc); + mc->generic.next = mcs->panels[curstate->panelnum]; + mcs->panels[curstate->panelnum] = mc; } #if !TARGET_API_MAC_CARBON @@ -282,6 +405,95 @@ static pascal SInt32 macctrl_sys7_text_cdef(SInt16 variant, ControlRef control, } #endif +static void macctrl_editbox(struct macctrls *mcs, WindowPtr window, + struct mac_layoutstate *curstate, + union control *ctrl) +{ + union macctrl *mc = smalloc(sizeof *mc); + Rect bounds; + + fprintf(stderr, " label = %s\n", ctrl->editbox.label); + fprintf(stderr, " percentwidth = %d\n", ctrl->editbox.percentwidth); + if (ctrl->editbox.password) fprintf(stderr, " password\n"); + if (ctrl->editbox.has_list) fprintf(stderr, " has list\n"); + mc->generic.type = MACCTRL_EDITBOX; + mc->generic.ctrl = ctrl; + mc->generic.privdata = NULL; + bounds.left = curstate->pos.h; + bounds.right = bounds.left + curstate->width; + bounds.top = curstate->pos.v; + bounds.bottom = bounds.top + 22; + if (mac_gestalts.apprvers >= 0x100) { + InsetRect(&bounds, 2, 2); + mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0, + ctrl->editbox.password ? + kControlEditTextPasswordProc : + kControlEditTextProc, (long)mc); + } else { + mc->text.tbctrl = NewControl(window, &bounds, NULL, TRUE, 0, 0, 0, + SYS7_EDITBOX_PROC, (long)mc); + } + curstate->pos.v += 28; + add234(mcs->byctrl, mc); + mc->generic.next = mcs->panels[curstate->panelnum]; + mcs->panels[curstate->panelnum] = mc; + ctrlevent(mcs, mc, EVENT_REFRESH); +} + +#if !TARGET_API_MAC_CARBON +static pascal SInt32 macctrl_sys7_editbox_cdef(SInt16 variant, + ControlRef control, + ControlDefProcMessage msg, + SInt32 param) +{ + RgnHandle rgn; + Rect rect; + TEHandle te; + long ssfs; + + switch (msg) { + case initCntl: + rect = (*control)->contrlRect; + InsetRect(&rect, 3, 3); /* 2 if it's 20 pixels high */ + te = TENew(&rect, &rect); + ssfs = GetScriptVariable(smSystemScript, smScriptSysFondSize); + (*te)->txSize = LoWord(ssfs); + (*te)->txFont = HiWord(ssfs); + (*control)->contrlData = (Handle)te; + return noErr; + case dispCntl: + TEDispose((TEHandle)(*control)->contrlData); + return 0; + case drawCntl: + if ((*control)->contrlVis) { + rect = (*control)->contrlRect; + PenNormal(); + FrameRect(&rect); + InsetRect(&rect, 3, 3); + TEUpdate(&rect, (TEHandle)(*control)->contrlData); + } + return 0; + case calcCRgns: + if (param & (1 << 31)) { + param &= ~(1 << 31); + goto calcthumbrgn; + } + /* FALLTHROUGH */ + case calcCntlRgn: + rgn = (RgnHandle)param; + RectRgn(rgn, &(*control)->contrlRect); + return 0; + case calcThumbRgn: + calcthumbrgn: + rgn = (RgnHandle)param; + SetEmptyRgn(rgn); + return 0; + } + + return 0; +} +#endif + static void macctrl_radio(struct macctrls *mcs, WindowPtr window, struct mac_layoutstate *curstate, union control *ctrl) @@ -294,25 +506,29 @@ static void macctrl_radio(struct macctrls *mcs, WindowPtr window, fprintf(stderr, " label = %s\n", ctrl->radio.label); mc->generic.type = MACCTRL_RADIO; mc->generic.ctrl = ctrl; + mc->generic.privdata = NULL; mc->radio.tbctrls = smalloc(sizeof(*mc->radio.tbctrls) * ctrl->radio.nbuttons); colwidth = (curstate->width + 13) / ctrl->radio.ncolumns; for (i = 0; i < ctrl->radio.nbuttons; i++) { fprintf(stderr, " button = %s\n", ctrl->radio.buttons[i]); - bounds.top = curstate->pos.v; - bounds.bottom = bounds.top + 16; + bounds.top = curstate->pos.v - 2; + bounds.bottom = bounds.top + 18; bounds.left = curstate->pos.h + colwidth * (i % ctrl->radio.ncolumns); if (i == ctrl->radio.nbuttons - 1 || i % ctrl->radio.ncolumns == ctrl->radio.ncolumns - 1) { bounds.right = curstate->pos.h + curstate->width; - curstate->pos.v += 22; + curstate->pos.v += 18; } else bounds.right = bounds.left + colwidth - 13; c2pstrcpy(title, ctrl->radio.buttons[i]); mc->radio.tbctrls[i] = NewControl(window, &bounds, title, TRUE, 0, 0, 1, radioButProc, (long)mc); } + curstate->pos.v += 4; add234(mcs->byctrl, mc); + mc->generic.next = mcs->panels[curstate->panelnum]; + mcs->panels[curstate->panelnum] = mc; ctrlevent(mcs, mc, EVENT_REFRESH); } @@ -327,6 +543,7 @@ static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window, fprintf(stderr, " label = %s\n", ctrl->checkbox.label); mc->generic.type = MACCTRL_CHECKBOX; mc->generic.ctrl = ctrl; + mc->generic.privdata = NULL; bounds.left = curstate->pos.h; bounds.right = bounds.left + curstate->width; bounds.top = curstate->pos.v; @@ -336,6 +553,8 @@ static void macctrl_checkbox(struct macctrls *mcs, WindowPtr window, checkBoxProc, (long)mc); add234(mcs->byctrl, mc); curstate->pos.v += 22; + mc->generic.next = mcs->panels[curstate->panelnum]; + mcs->panels[curstate->panelnum] = mc; ctrlevent(mcs, mc, EVENT_REFRESH); } @@ -352,6 +571,7 @@ static void macctrl_button(struct macctrls *mcs, WindowPtr window, fprintf(stderr, " is default\n"); mc->generic.type = MACCTRL_BUTTON; mc->generic.ctrl = ctrl; + mc->generic.privdata = NULL; bounds.left = curstate->pos.h; bounds.right = bounds.left + 100; /* XXX measure string */ bounds.top = curstate->pos.v; @@ -365,33 +585,164 @@ static void macctrl_button(struct macctrls *mcs, WindowPtr window, SetControlData(mc->button.tbctrl, kControlEntireControl, kControlPushButtonDefaultTag, sizeof(isdefault), &isdefault); + } else if (ctrl->button.isdefault) { + InsetRect(&bounds, -4, -4); + NewControl(window, &bounds, title, TRUE, 0, 0, 1, + SYS7_DEFAULT_PROC, (long)mc); + } + if (mac_gestalts.apprvers >= 0x110) { + Boolean iscancel = ctrl->button.iscancel; + + SetControlData(mc->button.tbctrl, kControlEntireControl, + kControlPushButtonCancelTag, + sizeof(iscancel), &iscancel); + } + add234(mcs->byctrl, mc); + mc->generic.next = mcs->panels[curstate->panelnum]; + mcs->panels[curstate->panelnum] = mc; + curstate->pos.v += 26; +} + +#if !TARGET_API_MAC_CARBON +static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant, + ControlRef control, + ControlDefProcMessage msg, + SInt32 param) +{ + RgnHandle rgn; + Rect rect; + int oval; + + switch (msg) { + case drawCntl: + if ((*control)->contrlVis) { + rect = (*control)->contrlRect; + PenNormal(); + PenSize(3, 3); + oval = (rect.bottom - rect.top) / 2 + 2; + FrameRoundRect(&rect, oval, oval); + } + return 0; + case calcCRgns: + if (param & (1 << 31)) { + param &= ~(1 << 31); + goto calcthumbrgn; + } + /* FALLTHROUGH */ + case calcCntlRgn: + rgn = (RgnHandle)param; + RectRgn(rgn, &(*control)->contrlRect); + return 0; + case calcThumbRgn: + calcthumbrgn: + rgn = (RgnHandle)param; + SetEmptyRgn(rgn); + return 0; } + + return 0; +} +#endif + +static void macctrl_popup(struct macctrls *mcs, WindowPtr window, + struct mac_layoutstate *curstate, + union control *ctrl) +{ + union macctrl *mc = smalloc(sizeof *mc); + Rect bounds; + Str255 title; + unsigned int labelwidth; + static int nextmenuid = MENU_MIN; + int menuid; + MenuRef menu; + + /* + * explains how to + * create a popup menu with dynamic content. + */ + assert(ctrl->listbox.height == 0); + assert(!ctrl->listbox.draglist); + assert(!ctrl->listbox.multisel); + + fprintf(stderr, " label = %s\n", ctrl->listbox.label); + fprintf(stderr, " percentwidth = %d\n", ctrl->listbox.percentwidth); + + mc->generic.type = MACCTRL_POPUP; + mc->generic.ctrl = ctrl; + mc->generic.privdata = NULL; + c2pstrcpy(title, ctrl->button.label); + + /* Find a spare menu ID and create the menu */ + while (GetMenuHandle(nextmenuid) != NULL) + if (++nextmenuid >= MENU_MAX) nextmenuid = MENU_MIN; + menuid = nextmenuid++; + menu = NewMenu(menuid, "\pdummy"); + if (menu == NULL) return; + mc->popup.menu = menu; + mc->popup.menuid = menuid; + InsertMenu(menu, kInsertHierarchicalMenu); + + /* The menu starts off empty */ + mc->popup.nids = 0; + mc->popup.ids = NULL; + + bounds.left = curstate->pos.h; + bounds.right = bounds.left + curstate->width; + bounds.top = curstate->pos.v; + bounds.bottom = bounds.top + 20; + /* XXX handle percentwidth == 100 */ + labelwidth = curstate->width * (100 - ctrl->listbox.percentwidth) / 100; + mc->popup.tbctrl = NewControl(window, &bounds, title, TRUE, + popupTitleLeftJust, menuid, labelwidth, + popupMenuProc + popupFixedWidth, (long)mc); add234(mcs->byctrl, mc); curstate->pos.v += 26; + mc->generic.next = mcs->panels[curstate->panelnum]; + mcs->panels[curstate->panelnum] = mc; + ctrlevent(mcs, mc, EVENT_REFRESH); } void macctrl_activate(WindowPtr window, EventRecord *event) { + struct macctrls *mcs = mac_winctrls(window); Boolean active = (event->modifiers & activeFlag) != 0; GrafPtr saveport; - ControlRef root; + int i, j; + ControlPartCode state; + union macctrl *mc; GetPort(&saveport); SetPort((GrafPtr)GetWindowPort(window)); - if (mac_gestalts.apprvers >= 0x100) { + if (mac_gestalts.apprvers >= 0x100) SetThemeWindowBackground(window, active ? kThemeBrushModelessDialogBackgroundActive : kThemeBrushModelessDialogBackgroundInactive, TRUE); - GetRootControl(window, &root); - if (active) - ActivateControl(root); - else - DeactivateControl(root); - } else { - /* (De)activate controls one at a time */ - } + state = active ? kControlNoPart : kControlInactivePart; + for (i = 0; i <= mcs->curpanel; i += mcs->curpanel) + for (mc = mcs->panels[i]; mc != NULL; mc = mc->generic.next) + switch (mc->generic.type) { + case MACCTRL_TEXT: + HiliteControl(mc->text.tbctrl, state); + break; + case MACCTRL_EDITBOX: + HiliteControl(mc->editbox.tbctrl, state); + break; + case MACCTRL_RADIO: + for (j = 0; j < mc->generic.ctrl->radio.nbuttons; j++) + HiliteControl(mc->radio.tbctrls[j], state); + break; + case MACCTRL_CHECKBOX: + HiliteControl(mc->checkbox.tbctrl, state); + break; + case MACCTRL_BUTTON: + HiliteControl(mc->button.tbctrl, state); + break; + case MACCTRL_POPUP: + HiliteControl(mc->popup.tbctrl, state); + break; + } SetPort(saveport); } @@ -399,44 +750,75 @@ void macctrl_click(WindowPtr window, EventRecord *event) { Point mouse; ControlHandle control; - int part; + int part, trackresult; GrafPtr saveport; union macctrl *mc; struct macctrls *mcs = mac_winctrls(window); int i; + UInt32 features; GetPort(&saveport); SetPort((GrafPtr)GetWindowPort(window)); mouse = event->where; GlobalToLocal(&mouse); part = FindControl(mouse, window, &control); - if (control != NULL) - if (TrackControl(control, mouse, NULL) != 0) { - mc = (union macctrl *)GetControlReference(control); - switch (mc->generic.type) { - case MACCTRL_RADIO: - for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++) { + if (control != NULL) { + if (mac_gestalts.apprvers >= 0x100) { + if (GetControlFeatures(control, &features) == noErr && + (features & kControlSupportsFocus) && + (features & kControlGetsFocusOnClick)) + SetKeyboardFocus(window, control, part); + trackresult = HandleControlClick(control, mouse, event->modifiers, + (ControlActionUPP)-1); + } else + trackresult = TrackControl(control, mouse, (ControlActionUPP)-1); + mc = (union macctrl *)GetControlReference(control); + switch (mc->generic.type) { + case MACCTRL_RADIO: + if (trackresult != 0) { + for (i = 0; i < mc->generic.ctrl->radio.nbuttons; i++) if (mc->radio.tbctrls[i] == control) SetControlValue(mc->radio.tbctrls[i], kControlRadioButtonCheckedValue); else SetControlValue(mc->radio.tbctrls[i], kControlRadioButtonUncheckedValue); - } ctrlevent(mcs, mc, EVENT_VALCHANGE); - break; - case MACCTRL_CHECKBOX: + } + break; + case MACCTRL_CHECKBOX: + if (trackresult != 0) { SetControlValue(control, !GetControlValue(control)); ctrlevent(mcs, mc, EVENT_VALCHANGE); - break; - case MACCTRL_BUTTON: - ctrlevent(mcs, mc, EVENT_ACTION); - break; } + break; + case MACCTRL_BUTTON: + if (trackresult != 0) + ctrlevent(mcs, mc, EVENT_ACTION); + break; + case MACCTRL_POPUP: + ctrlevent(mcs, mc, EVENT_SELCHANGE); + break; } + } SetPort(saveport); } +void macctrl_key(WindowPtr window, EventRecord *event) +{ + ControlRef control; + struct macctrls *mcs = mac_winctrls(window); + union macctrl *mc; + + if (mac_gestalts.apprvers >= 0x100 && + GetKeyboardFocus(window, &control) == noErr && control != NULL) { + HandleControlKey(control, (event->message & keyCodeMask) >> 8, + event->message & charCodeMask, event->modifiers); + mc = (union macctrl *)GetControlReference(control); + ctrlevent(mcs, mc, EVENT_VALCHANGE); + } +} + void macctrl_update(WindowPtr window) { #if TARGET_API_MAC_CARBON @@ -492,19 +874,31 @@ void macctrl_close(WindowPtr window) struct macctrls *mcs = mac_winctrls(window); union macctrl *mc; + /* + * Mostly, we don't bother disposing of the Toolbox controls, + * since that will happen automatically when the window is + * disposed of. Popup menus are an exception, because we have to + * dispose of the menu ourselves, and doing that while the control + * still holds a reference to it seems rude. + */ while ((mc = index234(mcs->byctrl, 0)) != NULL) { + if (mc->generic.privdata != NULL && mc->generic.freeprivdata) + sfree(mc->generic.privdata); + switch (mc->generic.type) { + case MACCTRL_POPUP: + DisposeControl(mc->popup.tbctrl); + DeleteMenu(mc->popup.menuid); + DisposeMenu(mc->popup.menu); + break; + } del234(mcs->byctrl, mc); sfree(mc); } freetree234(mcs->byctrl); mcs->byctrl = NULL; - -/* XXX - DisposeWindow(window); - if (s->window == NULL) - sfree(s); -*/ + sfree(mcs->panels); + mcs->panels = NULL; } void dlg_update_start(union control *ctrl, void *dlg) @@ -557,25 +951,44 @@ void dlg_end(void *dlg, int value) void dlg_refresh(union control *ctrl, void *dlg) { + struct macctrls *mcs = dlg; + union macctrl *mc; + if (ctrl == NULL) + return; /* FIXME */ + mc = findbyctrl(mcs, ctrl); + assert(mc != NULL); + ctrlevent(mcs, mc, EVENT_REFRESH); }; void *dlg_get_privdata(union control *ctrl, void *dlg) { + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); - return NULL; + assert(mc != NULL); + return mc->generic.privdata; } void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr) { + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); - fatalbox("dlg_set_privdata"); + assert(mc != NULL); + mc->generic.privdata = ptr; + mc->generic.freeprivdata = FALSE; } void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size) { + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); - fatalbox("dlg_alloc_privdata"); + assert(mc != NULL); + mc->generic.privdata = smalloc(size); + mc->generic.freeprivdata = TRUE; + return mc->generic.privdata; } @@ -585,9 +998,11 @@ void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size) void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton) { - union macctrl *mc = dlg; + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); int i; + assert(mc != NULL); for (i = 0; i < ctrl->radio.nbuttons; i++) { if (i == whichbutton) SetControlValue(mc->radio.tbctrls[i], @@ -601,9 +1016,11 @@ void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton) int dlg_radiobutton_get(union control *ctrl, void *dlg) { - union macctrl *mc = dlg; + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); int i; + assert(mc != NULL); for (i = 0; i < ctrl->radio.nbuttons; i++) { if (GetControlValue(mc->radio.tbctrls[i]) == kControlRadioButtonCheckedValue) @@ -619,8 +1036,10 @@ int dlg_radiobutton_get(union control *ctrl, void *dlg) void dlg_checkbox_set(union control *ctrl, void *dlg, int checked) { - union macctrl *mc = dlg; + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + assert(mc != NULL); SetControlValue(mc->checkbox.tbctrl, checked ? kControlCheckBoxCheckedValue : kControlCheckBoxUncheckedValue); @@ -628,8 +1047,10 @@ void dlg_checkbox_set(union control *ctrl, void *dlg, int checked) int dlg_checkbox_get(union control *ctrl, void *dlg) { - union macctrl *mc = dlg; + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + assert(mc != NULL); return GetControlValue(mc->checkbox.tbctrl); } @@ -640,12 +1061,51 @@ int dlg_checkbox_get(union control *ctrl, void *dlg) void dlg_editbox_set(union control *ctrl, void *dlg, char const *text) { + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + GrafPtr saveport; -}; + assert(mc != NULL); + assert(mc->generic.type == MACCTRL_EDITBOX); + GetPort(&saveport); + SetPort((GrafPtr)(GetWindowPort(mcs->window))); + if (mac_gestalts.apprvers >= 0x100) { + SetControlData(mc->editbox.tbctrl, kControlEntireControl, + ctrl->editbox.password ? + kControlEditTextPasswordTag : + kControlEditTextTextTag, + strlen(text), text); + } else { + TESetText(text, strlen(text), + (TEHandle)(*mc->editbox.tbctrl)->contrlData); + } + DrawOneControl(mc->editbox.tbctrl); + SetPort(saveport); +} void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length) { + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + Size olen; + assert(mc != NULL); + assert(mc->generic.type == MACCTRL_EDITBOX); + if (mac_gestalts.apprvers >= 0x100) { + if (GetControlData(mc->editbox.tbctrl, kControlEntireControl, + ctrl->editbox.password ? + kControlEditTextPasswordTag : + kControlEditTextTextTag, + length - 1, buffer, &olen) != noErr) + olen = 0; + if (olen > length - 1) + buffer[length - 1] = '\0'; + else + buffer[olen] = '\0'; + buffer[olen] = '\0'; + } else + buffer[0] = '\0'; + fprintf(stderr, "dlg_editbox_get: %s\n", buffer); }; @@ -653,48 +1113,139 @@ void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length) * List Box control */ +static void dlg_macpopup_clear(union control *ctrl, void *dlg) +{ + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + MenuRef menu = mc->popup.menu; + unsigned int i, n; + + fprintf(stderr, " popup_clear\n"); + n = CountMenuItems(menu); + for (i = 0; i < n; i++) + DeleteMenuItem(menu, n - i); + mc->popup.nids = 0; + sfree(mc->popup.ids); + mc->popup.ids = NULL; + SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); +} + void dlg_listbox_clear(union control *ctrl, void *dlg) { -}; + if (ctrl->listbox.height == 0) + dlg_macpopup_clear(ctrl, dlg); +} + +static void dlg_macpopup_del(union control *ctrl, void *dlg, int index) +{ + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + MenuRef menu = mc->popup.menu; + + fprintf(stderr, " popup_del %d\n", index); + DeleteMenuItem(menu, index + 1); + if (mc->popup.ids != NULL) + memcpy(mc->popup.ids + index, mc->popup.ids + index + 1, + (mc->popup.nids - index - 1) * sizeof(*mc->popup.ids)); + SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); +} void dlg_listbox_del(union control *ctrl, void *dlg, int index) { -}; + if (ctrl->listbox.height == 0) + dlg_macpopup_del(ctrl, dlg, index); +} + +static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text) +{ + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + MenuRef menu = mc->popup.menu; + Str255 itemstring; + + fprintf(stderr, " popup_add %s\n", text); + assert(text[0] != '\0'); + c2pstrcpy(itemstring, text); + AppendMenu(menu, "\pdummy"); + SetMenuItemText(menu, CountMenuItems(menu), itemstring); + SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); +} void dlg_listbox_add(union control *ctrl, void *dlg, char const *text) { -}; + if (ctrl->listbox.height == 0) + dlg_macpopup_add(ctrl, dlg, text); +} -void dlg_listbox_addwithindex(union control *ctrl, void *dlg, - char const *text, int id) +static void dlg_macpopup_addwithid(union control *ctrl, void *dlg, + char const *text, int id) { + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + MenuRef menu = mc->popup.menu; + unsigned int index; + + fprintf(stderr, " popup_addwthindex %s, %d\n", text, id); + dlg_macpopup_add(ctrl, dlg, text); + index = CountMenuItems(menu) - 1; + if (mc->popup.nids <= index) { + mc->popup.nids = index + 1; + mc->popup.ids = srealloc(mc->popup.ids, + mc->popup.nids * sizeof(*mc->popup.ids)); + } + mc->popup.ids[index] = id; +} -}; +void dlg_listbox_addwithid(union control *ctrl, void *dlg, + char const *text, int id) +{ + + if (ctrl->listbox.height == 0) + dlg_macpopup_addwithid(ctrl, dlg, text, id); +} int dlg_listbox_getid(union control *ctrl, void *dlg, int index) { + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + if (ctrl->listbox.height == 0) { + assert(mc->popup.ids != NULL && mc->popup.nids > index); + return mc->popup.ids[index]; + } return 0; -}; +} int dlg_listbox_index(union control *ctrl, void *dlg) { + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + if (ctrl->listbox.height == 0) + return GetControlValue(mc->popup.tbctrl) - 1; return 0; }; int dlg_listbox_issel(union control *ctrl, void *dlg, int index) { + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + if (ctrl->listbox.height == 0) + return GetControlValue(mc->popup.tbctrl) - 1 == index; return 0; }; void dlg_listbox_select(union control *ctrl, void *dlg, int index) { + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + if (ctrl->listbox.height == 0) + SetControlValue(mc->popup.tbctrl, index + 1); }; @@ -704,12 +1255,18 @@ void dlg_listbox_select(union control *ctrl, void *dlg, int index) void dlg_text_set(union control *ctrl, void *dlg, char const *text) { - union macctrl *mc = dlg; + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + Str255 title; + assert(mc != NULL); if (mac_gestalts.apprvers >= 0x100) SetControlData(mc->text.tbctrl, kControlEntireControl, - kControlStaticTextTextTag, - strlen(ctrl->text.label), ctrl->text.label); + kControlStaticTextTextTag, strlen(text), text); + else { + c2pstrcpy(title, text); + SetControlTitle(mc->text.tbctrl, title); + } }