X-Git-Url: https://git.distorted.org.uk/u/mdw/putty/blobdiff_plain/0b89e7b2ddbde72ef39bf1ef041655a8f5cd291b..06567a99c718a3f451c07bba69933c34d5e9111f:/mac/macctrls.c diff --git a/mac/macctrls.c b/mac/macctrls.c index 09ffb69a..4cf32861 100644 --- a/mac/macctrls.c +++ b/mac/macctrls.c @@ -1,4 +1,4 @@ -/* $Id: macctrls.c,v 1.27 2003/04/05 15:55:22 ben Exp $ */ +/* $Id: macctrls.c,v 1.30 2003/04/05 22:00:57 ben Exp $ */ /* * Copyright (c) 2003 Ben Harris * All rights reserved. @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -60,6 +61,7 @@ union macctrl { MACCTRL_RADIO, MACCTRL_CHECKBOX, MACCTRL_BUTTON, + MACCTRL_LISTBOX, MACCTRL_POPUP } type; /* Template from which this was generated */ @@ -90,10 +92,17 @@ union macctrl { struct { struct macctrl_generic generic; ControlRef tbctrl; + ControlRef tbring; } button; struct { struct macctrl_generic generic; ControlRef tbctrl; + unsigned int nids; + int *ids; + } listbox; + struct { + struct macctrl_generic generic; + ControlRef tbctrl; MenuRef menu; int menuid; unsigned int nids; @@ -130,6 +139,8 @@ 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_listbox(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 @@ -218,6 +229,8 @@ void macctrl_layoutbox(struct controlbox *cb, WindowPtr window, mcs->window = window; mcs->byctrl = newtree234(macctrl_cmp_byctrl); mcs->focus = NULL; + mcs->defbutton = NULL; + mcs->canbutton = NULL; /* Count the number of panels */ mcs->npanels = 1; for (i = 1; i < cb->nctrlsets; i++) @@ -235,7 +248,7 @@ void macctrl_layoutbox(struct controlbox *cb, WindowPtr window, } macctrl_layoutset(&curstate, cb->ctrlsets[i], window, mcs); } - macctrl_switchtopanel(mcs, 1); + macctrl_switchtopanel(mcs, 19); /* 14 = proxies, 20 = SSH bugs */ } @@ -321,6 +334,8 @@ static void macctrl_layoutset(struct mac_layoutstate *curstate, case CTRL_LISTBOX: if (ctrl->listbox.height == 0) macctrl_popup(mcs, window, &cols[colstart], ctrl); + else + macctrl_listbox(mcs, window, &cols[colstart], ctrl); break; } } @@ -333,6 +348,9 @@ static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which) { unsigned int i, j; union macctrl *mc; + ListHandle list; + Size olen; + #define hideshow(c) do { \ if (i == which) ShowControl(c); else HideControl(c); \ @@ -367,6 +385,24 @@ static void macctrl_switchtopanel(struct macctrls *mcs, unsigned int which) case MACCTRL_BUTTON: hideshow(mc->button.tbctrl); break; + case MACCTRL_LISTBOX: + hideshow(mc->listbox.tbctrl); + /* + * At least under Mac OS 8.1, hiding a list box + * doesn't hide its scroll bars. + */ + if (mac_gestalts.apprvers >= 0x100) { + if (GetControlData(mc->listbox.tbctrl, + kControlEntireControl, + kControlListBoxListHandleTag, + sizeof(list), &list, &olen) == noErr) +#if TARGET_API_MAC_CARBON + hideshow(GetListVerticalScrollBar(list)); +#else + hideshow((*list)->vScroll); +#endif + } + break; case MACCTRL_POPUP: hideshow(mc->popup.tbctrl); break; @@ -711,6 +747,7 @@ static void macctrl_button(struct macctrls *mcs, WindowPtr window, c2pstrcpy(title, ctrl->button.label); mc->button.tbctrl = NewControl(window, &bounds, title, TRUE, 0, 0, 1, pushButProc, (long)mc); + mc->button.tbring = NULL; if (mac_gestalts.apprvers >= 0x100) { Boolean isdefault = ctrl->button.isdefault; @@ -719,8 +756,8 @@ static void macctrl_button(struct macctrls *mcs, WindowPtr window, 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); + mc->button.tbring = NewControl(window, &bounds, title, TRUE, 0, 0, 1, + SYS7_DEFAULT_PROC, (long)mc); } if (mac_gestalts.apprvers >= 0x110) { Boolean iscancel = ctrl->button.iscancel; @@ -729,6 +766,10 @@ static void macctrl_button(struct macctrls *mcs, WindowPtr window, kControlPushButtonCancelTag, sizeof(iscancel), &iscancel); } + if (ctrl->button.isdefault) + mcs->defbutton = mc; + if (ctrl->button.iscancel) + mcs->canbutton = mc; add234(mcs->byctrl, mc); mc->generic.next = mcs->panels[curstate->panelnum]; mcs->panels[curstate->panelnum] = mc; @@ -744,15 +785,20 @@ static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant, RgnHandle rgn; Rect rect; int oval; + PenState savestate; switch (msg) { case drawCntl: if ((*control)->contrlVis) { rect = (*control)->contrlRect; + GetPenState(&savestate); PenNormal(); PenSize(3, 3); + if ((*control)->contrlHilite == kControlInactivePart) + PenPat(&qd.gray); oval = (rect.bottom - rect.top) / 2 + 2; FrameRoundRect(&rect, oval, oval); + SetPenState(&savestate); } return 0; case calcCRgns: @@ -776,6 +822,59 @@ static pascal SInt32 macctrl_sys7_default_cdef(SInt16 variant, } #endif +static void macctrl_listbox(struct macctrls *mcs, WindowPtr window, + struct mac_layoutstate *curstate, + union control *ctrl) +{ + union macctrl *mc = snew(union macctrl); + Rect bounds; + ListHandle list; + Size olen; + + if (mac_gestalts.apprvers < 0x100) + return; + if (ctrl->listbox.label != NULL) + fprintf(stderr, " label = %s\n", ctrl->listbox.label); + fprintf(stderr, " height = %d\n", ctrl->listbox.height); + if (ctrl->listbox.draglist) + fprintf(stderr, " draglist\n"); + if (ctrl->listbox.multisel) + fprintf(stderr, " multisel\n"); + fprintf(stderr, " ncols = %d\n", ctrl->listbox.ncols); + assert(ctrl->listbox.percentwidth == 100); + mc->generic.type = MACCTRL_LISTBOX; + mc->generic.ctrl = ctrl; + mc->generic.privdata = NULL; + /* The list starts off empty */ + mc->listbox.nids = 0; + mc->listbox.ids = NULL; + bounds.left = curstate->pos.h; + bounds.right = bounds.left + curstate->width; + bounds.top = curstate->pos.v; + bounds.bottom = bounds.top + 20 * ctrl->listbox.height; + + mc->button.tbctrl = NewControl(window, &bounds, NULL, TRUE, + ldes_Default, 0, 0, + kControlListBoxProc, (long)mc); + if (!ctrl->listbox.multisel) { + if (GetControlData(mc->listbox.tbctrl, kControlEntireControl, + kControlListBoxListHandleTag, sizeof(list), &list, + &olen) != noErr) + return; +#if TARGET_API_MAC_CARBON + SetListSelectionFlags(list, lOnlyOne); +#else + (*list)->selFlags = lOnlyOne; +#endif + } + add234(mcs->byctrl, mc); + curstate->pos.v += 6 + 20 * ctrl->listbox.height; + mc->generic.next = mcs->panels[curstate->panelnum]; + mcs->panels[curstate->panelnum] = mc; + ctrlevent(mcs, mc, EVENT_REFRESH); +} + + static void macctrl_popup(struct macctrls *mcs, WindowPtr window, struct mac_layoutstate *curstate, union control *ctrl) @@ -875,6 +974,11 @@ void macctrl_activate(WindowPtr window, EventRecord *event) break; case MACCTRL_BUTTON: HiliteControl(mc->button.tbctrl, state); + if (mc->button.tbring != NULL) + HiliteControl(mc->button.tbring, state); + break; + case MACCTRL_LISTBOX: + HiliteControl(mc->listbox.tbctrl, state); break; case MACCTRL_POPUP: HiliteControl(mc->popup.tbctrl, state); @@ -953,6 +1057,10 @@ void macctrl_click(WindowPtr window, EventRecord *event) if (trackresult != 0) ctrlevent(mcs, mc, EVENT_ACTION); break; + case MACCTRL_LISTBOX: + /* FIXME spot double-click */ + ctrlevent(mcs, mc, EVENT_SELCHANGE); + break; case MACCTRL_POPUP: ctrlevent(mcs, mc, EVENT_SELCHANGE); break; @@ -967,7 +1075,34 @@ void macctrl_key(WindowPtr window, EventRecord *event) ControlRef control; struct macctrls *mcs = mac_winctrls(window); union macctrl *mc; - + unsigned long dummy; + + switch (event->message & charCodeMask) { + case kEnterCharCode: + case kReturnCharCode: + if (mcs->defbutton != NULL) { + assert(mcs->defbutton->generic.type == MACCTRL_BUTTON); + HiliteControl(mcs->defbutton->button.tbctrl, kControlButtonPart); + /* + * I'd like to delay unhilighting the button until after + * the event has been processed, but by them the entire + * dialgue box might have been destroyed. + */ + Delay(6, &dummy); + HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart); + ctrlevent(mcs, mcs->defbutton, EVENT_ACTION); + } + return; + case kEscapeCharCode: + if (mcs->canbutton != NULL) { + assert(mcs->canbutton->generic.type == MACCTRL_BUTTON); + HiliteControl(mcs->canbutton->button.tbctrl, kControlButtonPart); + Delay(6, &dummy); + HiliteControl(mcs->defbutton->button.tbctrl, kControlNoPart); + ctrlevent(mcs, mcs->canbutton, EVENT_ACTION); + } + return; + } if (mac_gestalts.apprvers >= 0x100) { if (GetKeyboardFocus(window, &control) == noErr && control != NULL) { HandleControlKey(control, (event->message & keyCodeMask) >> 8, @@ -1180,7 +1315,7 @@ void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton) union macctrl *mc = findbyctrl(mcs, ctrl); int i; - assert(mc != NULL); + if (mc == NULL) return; for (i = 0; i < ctrl->radio.nbuttons; i++) { if (i == whichbutton) SetControlValue(mc->radio.tbctrls[i], @@ -1217,7 +1352,7 @@ void dlg_checkbox_set(union control *ctrl, void *dlg, int checked) struct macctrls *mcs = dlg; union macctrl *mc = findbyctrl(mcs, ctrl); - assert(mc != NULL); + if (mc == NULL) return; SetControlValue(mc->checkbox.tbctrl, checked ? kControlCheckBoxCheckedValue : kControlCheckBoxUncheckedValue); @@ -1243,7 +1378,7 @@ void dlg_editbox_set(union control *ctrl, void *dlg, char const *text) union macctrl *mc = findbyctrl(mcs, ctrl); GrafPtr saveport; - assert(mc != NULL); + if (mc == NULL) return; assert(mc->generic.type == MACCTRL_EDITBOX); GetPort(&saveport); SetPort((GrafPtr)(GetWindowPort(mcs->window))); @@ -1306,6 +1441,7 @@ static void dlg_macpopup_clear(union control *ctrl, void *dlg) MenuRef menu = mc->popup.menu; unsigned int i, n; + if (mc == NULL) return; fprintf(stderr, " popup_clear\n"); n = CountMenuItems(menu); for (i = 0; i < n; i++) @@ -1316,11 +1452,33 @@ static void dlg_macpopup_clear(union control *ctrl, void *dlg) SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); } +static void dlg_maclist_clear(union control *ctrl, void *dlg) +{ + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + ListHandle list; + Size olen; + + if (mc == NULL) return; + fprintf(stderr, " maclist_clear\n"); + if (GetControlData(mc->listbox.tbctrl, kControlEntireControl, + kControlListBoxListHandleTag, sizeof(list), &list, + &olen) != noErr) + return; + LDelRow(0, 0, list); + mc->listbox.nids = 0; + sfree(mc->listbox.ids); + mc->listbox.ids = NULL; + DrawOneControl(mc->listbox.tbctrl); +} + void dlg_listbox_clear(union control *ctrl, void *dlg) { if (ctrl->listbox.height == 0) dlg_macpopup_clear(ctrl, dlg); + else + dlg_maclist_clear(ctrl, dlg); } static void dlg_macpopup_del(union control *ctrl, void *dlg, int index) @@ -1329,6 +1487,7 @@ static void dlg_macpopup_del(union control *ctrl, void *dlg, int index) union macctrl *mc = findbyctrl(mcs, ctrl); MenuRef menu = mc->popup.menu; + if (mc == NULL) return; fprintf(stderr, " popup_del %d\n", index); DeleteMenuItem(menu, index + 1); if (mc->popup.ids != NULL) @@ -1337,11 +1496,33 @@ static void dlg_macpopup_del(union control *ctrl, void *dlg, int index) SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); } +static void dlg_maclist_del(union control *ctrl, void *dlg, int index) +{ + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + ListHandle list; + Size olen; + + if (mc == NULL) return; + fprintf(stderr, " maclist_del %d\n", index); + if (GetControlData(mc->listbox.tbctrl, kControlEntireControl, + kControlListBoxListHandleTag, sizeof(list), &list, + &olen) != noErr) + return; + LDelRow(1, index, list); + if (mc->listbox.ids != NULL) + memcpy(mc->listbox.ids + index, mc->listbox.ids + index + 1, + (mc->listbox.nids - index - 1) * sizeof(*mc->listbox.ids)); + DrawOneControl(mc->listbox.tbctrl); +} + void dlg_listbox_del(union control *ctrl, void *dlg, int index) { if (ctrl->listbox.height == 0) dlg_macpopup_del(ctrl, dlg, index); + else + dlg_maclist_del(ctrl, dlg, index); } static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text) @@ -1351,6 +1532,7 @@ static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text) MenuRef menu = mc->popup.menu; Str255 itemstring; + if (mc == NULL) return; fprintf(stderr, " popup_add %s\n", text); assert(text[0] != '\0'); c2pstrcpy(itemstring, text); @@ -1359,11 +1541,40 @@ static void dlg_macpopup_add(union control *ctrl, void *dlg, char const *text) SetControlMaximum(mc->popup.tbctrl, CountMenuItems(menu)); } + +static void dlg_maclist_add(union control *ctrl, void *dlg, char const *text) +{ + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + ListHandle list; + Size olen; + ListBounds bounds; + Cell cell = { 0, 0 }; + + if (mc == NULL) return; + fprintf(stderr, " maclist_add %s\n", text); + if (GetControlData(mc->listbox.tbctrl, kControlEntireControl, + kControlListBoxListHandleTag, sizeof(list), &list, + &olen) != noErr) + return; +#if TARGET_API_MAC_CARBON + GetListDataBounds(list, &bounds); +#else + bounds = (*list)->dataBounds; +#endif + cell.v = bounds.bottom; + LAddRow(1, cell.v, list); + LSetCell(text, strlen(text), cell, list); + DrawOneControl(mc->listbox.tbctrl); +} + void dlg_listbox_add(union control *ctrl, void *dlg, char const *text) { if (ctrl->listbox.height == 0) dlg_macpopup_add(ctrl, dlg, text); + else + dlg_maclist_add(ctrl, dlg, text); } static void dlg_macpopup_addwithid(union control *ctrl, void *dlg, @@ -1374,7 +1585,8 @@ static void dlg_macpopup_addwithid(union control *ctrl, void *dlg, MenuRef menu = mc->popup.menu; unsigned int index; - fprintf(stderr, " popup_addwthindex %s, %d\n", text, id); + if (mc == NULL) return; + fprintf(stderr, " popup_addwthid %s, %d\n", text, id); dlg_macpopup_add(ctrl, dlg, text); index = CountMenuItems(menu) - 1; if (mc->popup.nids <= index) { @@ -1384,12 +1596,44 @@ static void dlg_macpopup_addwithid(union control *ctrl, void *dlg, mc->popup.ids[index] = id; } +static void dlg_maclist_addwithid(union control *ctrl, void *dlg, + char const *text, int id) +{ + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + ListHandle list; + Size olen; + ListBounds bounds; + int index; + + if (mc == NULL) return; + fprintf(stderr, " maclist_addwithid %s %d\n", text, id); + dlg_maclist_add(ctrl, dlg, text); + if (GetControlData(mc->listbox.tbctrl, kControlEntireControl, + kControlListBoxListHandleTag, sizeof(list), &list, + &olen) != noErr) + return; +#if TARGET_API_MAC_CARBON + GetListDataBounds(list, &bounds); +#else + bounds = (*list)->dataBounds; +#endif + index = bounds.bottom; + if (mc->listbox.nids <= index) { + mc->listbox.nids = index + 1; + mc->listbox.ids = sresize(mc->listbox.ids, mc->listbox.nids, int); + } + mc->listbox.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); + else + dlg_maclist_addwithid(ctrl, dlg, text, id); } int dlg_listbox_getid(union control *ctrl, void *dlg, int index) @@ -1397,6 +1641,7 @@ int dlg_listbox_getid(union control *ctrl, void *dlg, int index) struct macctrls *mcs = dlg; union macctrl *mc = findbyctrl(mcs, ctrl); + assert(mc != NULL); if (ctrl->listbox.height == 0) { assert(mc->popup.ids != NULL && mc->popup.nids > index); return mc->popup.ids[index]; @@ -1404,15 +1649,52 @@ int dlg_listbox_getid(union control *ctrl, void *dlg, int index) return 0; } -int dlg_listbox_index(union control *ctrl, void *dlg) +static int dlg_maclist_index(union control *ctrl, void *dlg) { struct macctrls *mcs = dlg; union macctrl *mc = findbyctrl(mcs, ctrl); + ListHandle list; + Size olen; + Cell cell = { 0, 0 }; - if (ctrl->listbox.height == 0) + if (GetControlData(mc->listbox.tbctrl, kControlEntireControl, + kControlListBoxListHandleTag, sizeof(list), &list, + &olen) != noErr) + return -1; + if (LGetSelect(TRUE, &cell, list)) + return cell.v; + else + return -1; +} + +int dlg_listbox_index(union control *ctrl, void *dlg) +{ + struct macctrls *mcs = dlg; + union macctrl *mc; + + if (ctrl->listbox.height == 0) { + mc = findbyctrl(mcs, ctrl); + assert(mc != NULL); return GetControlValue(mc->popup.tbctrl) - 1; - return 0; -}; + } else + return dlg_maclist_index(ctrl, dlg); +} + +static int dlg_maclist_issel(union control *ctrl, void *dlg, int index) +{ + struct macctrls *mcs = dlg; + union macctrl *mc = findbyctrl(mcs, ctrl); + ListHandle list; + Size olen; + Cell cell = { 0, 0 }; + + if (GetControlData(mc->listbox.tbctrl, kControlEntireControl, + kControlListBoxListHandleTag, sizeof(list), &list, + &olen) != noErr) + return FALSE; + cell.v = index; + return LGetSelect(FALSE, &cell, list); +} int dlg_listbox_issel(union control *ctrl, void *dlg, int index) { @@ -1421,14 +1703,16 @@ int dlg_listbox_issel(union control *ctrl, void *dlg, int index) if (ctrl->listbox.height == 0) return GetControlValue(mc->popup.tbctrl) - 1 == index; - return 0; -}; + else + return dlg_maclist_issel(ctrl, dlg, index); +} void dlg_listbox_select(union control *ctrl, void *dlg, int index) { struct macctrls *mcs = dlg; union macctrl *mc = findbyctrl(mcs, ctrl); + if (mc == NULL) return; if (ctrl->listbox.height == 0) SetControlValue(mc->popup.tbctrl, index + 1); }; @@ -1443,7 +1727,7 @@ void dlg_text_set(union control *ctrl, void *dlg, char const *text) struct macctrls *mcs = dlg; union macctrl *mc = findbyctrl(mcs, ctrl); - assert(mc != NULL); + if (mc == NULL) return; if (mac_gestalts.apprvers >= 0x100) SetControlData(mc->text.tbctrl, kControlEntireControl, kControlStaticTextTextTag, strlen(text), text);