+void winctrl_add_shortcuts(struct dlgparam *dp, struct winctrl *c)
+{
+ int i;
+ for (i = 0; i < lenof(c->shortcuts); i++)
+ if (c->shortcuts[i] != NO_SHORTCUT) {
+ unsigned char s = tolower((unsigned char)c->shortcuts[i]);
+ assert(!dp->shortcuts[s]);
+ dp->shortcuts[s] = TRUE;
+ }
+}
+
+void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c)
+{
+ int i;
+ for (i = 0; i < lenof(c->shortcuts); i++)
+ if (c->shortcuts[i] != NO_SHORTCUT) {
+ unsigned char s = tolower((unsigned char)c->shortcuts[i]);
+ assert(dp->shortcuts[s]);
+ dp->shortcuts[s] = FALSE;
+ }
+}
+
+static int winctrl_cmp_byctrl(void *av, void *bv)
+{
+ struct winctrl *a = (struct winctrl *)av;
+ struct winctrl *b = (struct winctrl *)bv;
+ if (a->ctrl < b->ctrl)
+ return -1;
+ else if (a->ctrl > b->ctrl)
+ return +1;
+ else
+ return 0;
+}
+static int winctrl_cmp_byid(void *av, void *bv)
+{
+ struct winctrl *a = (struct winctrl *)av;
+ struct winctrl *b = (struct winctrl *)bv;
+ if (a->base_id < b->base_id)
+ return -1;
+ else if (a->base_id > b->base_id)
+ return +1;
+ else
+ return 0;
+}
+static int winctrl_cmp_byctrl_find(void *av, void *bv)
+{
+ union control *a = (union control *)av;
+ struct winctrl *b = (struct winctrl *)bv;
+ if (a < b->ctrl)
+ return -1;
+ else if (a > b->ctrl)
+ return +1;
+ else
+ return 0;
+}
+static int winctrl_cmp_byid_find(void *av, void *bv)
+{
+ int *a = (int *)av;
+ struct winctrl *b = (struct winctrl *)bv;
+ if (*a < b->base_id)
+ return -1;
+ else if (*a >= b->base_id + b->num_ids)
+ return +1;
+ else
+ return 0;
+}
+
+void winctrl_init(struct winctrls *wc)
+{
+ wc->byctrl = newtree234(winctrl_cmp_byctrl);
+ wc->byid = newtree234(winctrl_cmp_byid);
+}
+void winctrl_cleanup(struct winctrls *wc)
+{
+ struct winctrl *c;
+
+ while ((c = index234(wc->byid, 0)) != NULL) {
+ winctrl_remove(wc, c);
+ sfree(c->data);
+ sfree(c);
+ }
+
+ freetree234(wc->byctrl);
+ freetree234(wc->byid);
+ wc->byctrl = wc->byid = NULL;
+}
+
+void winctrl_add(struct winctrls *wc, struct winctrl *c)
+{
+ struct winctrl *ret;
+ if (c->ctrl) {
+ ret = add234(wc->byctrl, c);
+ assert(ret == c);
+ }
+ ret = add234(wc->byid, c);
+ assert(ret == c);
+}
+
+void winctrl_remove(struct winctrls *wc, struct winctrl *c)
+{
+ struct winctrl *ret;
+ ret = del234(wc->byctrl, c);
+ ret = del234(wc->byid, c);
+ assert(ret == c);
+}
+
+struct winctrl *winctrl_findbyctrl(struct winctrls *wc, union control *ctrl)
+{
+ return find234(wc->byctrl, ctrl, winctrl_cmp_byctrl_find);
+}
+
+struct winctrl *winctrl_findbyid(struct winctrls *wc, int id)
+{
+ return find234(wc->byid, &id, winctrl_cmp_byid_find);
+}
+
+struct winctrl *winctrl_findbyindex(struct winctrls *wc, int index)
+{
+ return index234(wc->byid, index);
+}
+
+void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
+ struct ctlpos *cp, struct controlset *s, int *id)
+{
+ struct ctlpos columns[16];
+ int ncols, colstart, colspan;
+
+ struct ctlpos tabdelays[16];
+ union control *tabdelayed[16];
+ int ntabdelays;
+
+ struct ctlpos pos;
+
+ char shortcuts[MAX_SHORTCUTS_PER_CTRL];
+ int nshortcuts;
+ char *escaped;
+ int i, actual_base_id, base_id, num_ids;
+ void *data;
+
+ base_id = *id;
+
+ /* Start a containing box, if we have a boxname. */
+ if (s->boxname && *s->boxname) {
+ struct winctrl *c = snew(struct winctrl);
+ c->ctrl = NULL;
+ c->base_id = base_id;
+ c->num_ids = 1;
+ c->data = NULL;
+ memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
+ winctrl_add(wc, c);
+ beginbox(cp, s->boxtitle, base_id);
+ base_id++;
+ }
+
+ /* Draw a title, if we have one. */
+ if (!s->boxname && s->boxtitle) {
+ struct winctrl *c = snew(struct winctrl);
+ c->ctrl = NULL;
+ c->base_id = base_id;
+ c->num_ids = 1;
+ c->data = dupstr(s->boxtitle);
+ memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
+ winctrl_add(wc, c);
+ paneltitle(cp, base_id);
+ base_id++;
+ }
+
+ /* Initially we have just one column. */
+ ncols = 1;
+ columns[0] = *cp; /* structure copy */
+
+ /* And initially, there are no pending tab-delayed controls. */
+ ntabdelays = 0;
+
+ /* Loop over each control in the controlset. */
+ for (i = 0; i < s->ncontrols; i++) {
+ union control *ctrl = s->ctrls[i];
+
+ /*
+ * Generic processing that pertains to all control types.
+ * At the end of this if statement, we'll have produced
+ * `ctrl' (a pointer to the control we have to create, or
+ * think about creating, in this iteration of the loop),
+ * `pos' (a suitable ctlpos with which to position it), and
+ * `c' (a winctrl structure to receive details of the
+ * dialog IDs). Or we'll have done a `continue', if it was
+ * CTRL_COLUMNS and doesn't require any control creation at
+ * all.
+ */
+ if (ctrl->generic.type == CTRL_COLUMNS) {
+ assert((ctrl->columns.ncols == 1) ^ (ncols == 1));
+
+ if (ncols == 1) {
+ /*
+ * We're splitting into multiple columns.
+ */
+ int lpercent, rpercent, lx, rx, i;
+
+ ncols = ctrl->columns.ncols;
+ assert(ncols <= lenof(columns));
+ for (i = 1; i < ncols; i++)
+ columns[i] = columns[0]; /* structure copy */
+
+ lpercent = 0;
+ for (i = 0; i < ncols; i++) {
+ rpercent = lpercent + ctrl->columns.percentages[i];
+ lx = columns[i].xoff + lpercent *
+ (columns[i].width + GAPBETWEEN) / 100;
+ rx = columns[i].xoff + rpercent *
+ (columns[i].width + GAPBETWEEN) / 100;
+ columns[i].xoff = lx;
+ columns[i].width = rx - lx - GAPBETWEEN;
+ lpercent = rpercent;
+ }
+ } else {
+ /*
+ * We're recombining the various columns into one.
+ */
+ int maxy = columns[0].ypos;
+ int i;
+ for (i = 1; i < ncols; i++)
+ if (maxy < columns[i].ypos)
+ maxy = columns[i].ypos;
+ ncols = 1;
+ columns[0] = *cp; /* structure copy */
+ columns[0].ypos = maxy;
+ }
+
+ continue;
+ } else if (ctrl->generic.type == CTRL_TABDELAY) {
+ int i;
+
+ assert(!ctrl->generic.tabdelay);
+ ctrl = ctrl->tabdelay.ctrl;
+
+ for (i = 0; i < ntabdelays; i++)
+ if (tabdelayed[i] == ctrl)
+ break;
+ assert(i < ntabdelays); /* we have to have found it */
+
+ pos = tabdelays[i]; /* structure copy */
+
+ colstart = colspan = -1; /* indicate this was tab-delayed */
+
+ } else {
+ /*
+ * If it wasn't one of those, it's a genuine control;
+ * so we'll have to compute a position for it now, by
+ * checking its column span.
+ */
+ int col;
+
+ colstart = COLUMN_START(ctrl->generic.column);
+ colspan = COLUMN_SPAN(ctrl->generic.column);
+
+ pos = columns[colstart]; /* structure copy */
+ pos.width = columns[colstart+colspan-1].width +
+ (columns[colstart+colspan-1].xoff - columns[colstart].xoff);
+
+ for (col = colstart; col < colstart+colspan; col++)
+ if (pos.ypos < columns[col].ypos)
+ pos.ypos = columns[col].ypos;
+
+ /*
+ * If this control is to be tabdelayed, add it to the
+ * tabdelay list, and unset pos.hwnd to inhibit actual
+ * control creation.
+ */
+ if (ctrl->generic.tabdelay) {
+ assert(ntabdelays < lenof(tabdelays));
+ tabdelays[ntabdelays] = pos; /* structure copy */
+ tabdelayed[ntabdelays] = ctrl;
+ ntabdelays++;
+ pos.hwnd = NULL;
+ }
+ }
+
+ /* Most controls don't need anything in c->data. */
+ data = NULL;
+
+ /* And they all start off with no shortcuts registered. */
+ memset(shortcuts, NO_SHORTCUT, lenof(shortcuts));
+ nshortcuts = 0;
+
+ /* Almost all controls start at base_id. */
+ actual_base_id = base_id;
+
+ /*
+ * Now we're ready to actually create the control, by
+ * switching on its type.
+ */
+ switch (ctrl->generic.type) {
+ case CTRL_TEXT:
+ {
+ char *wrapped, *escaped;
+ int lines;
+ num_ids = 1;
+ wrapped = staticwrap(&pos, cp->hwnd,
+ ctrl->generic.label, &lines);
+ escaped = shortcut_escape(wrapped, NO_SHORTCUT);
+ statictext(&pos, escaped, lines, base_id);
+ sfree(escaped);
+ sfree(wrapped);
+ }
+ break;
+ case CTRL_EDITBOX:
+ num_ids = 2; /* static, edit */
+ escaped = shortcut_escape(ctrl->editbox.label,
+ ctrl->editbox.shortcut);
+ shortcuts[nshortcuts++] = ctrl->editbox.shortcut;
+ if (ctrl->editbox.percentwidth == 100) {
+ if (ctrl->editbox.has_list)
+ combobox(&pos, escaped,
+ base_id, base_id+1);
+ else
+ multiedit(&pos, ctrl->editbox.password, escaped,
+ base_id, base_id+1, 100, NULL);
+ } else {
+ if (ctrl->editbox.has_list) {
+ staticcombo(&pos, escaped, base_id, base_id+1,
+ ctrl->editbox.percentwidth);
+ } else {
+ (ctrl->editbox.password ? staticpassedit : staticedit)
+ (&pos, escaped, base_id, base_id+1,
+ ctrl->editbox.percentwidth);
+ }
+ }
+ sfree(escaped);
+ break;
+ case CTRL_RADIO:
+ num_ids = ctrl->radio.nbuttons + 1; /* label as well */
+ {
+ struct radio *buttons;
+ int i;
+
+ escaped = shortcut_escape(ctrl->radio.label,
+ ctrl->radio.shortcut);
+ shortcuts[nshortcuts++] = ctrl->radio.shortcut;
+
+ buttons = snewn(ctrl->radio.nbuttons, struct radio);
+
+ for (i = 0; i < ctrl->radio.nbuttons; i++) {
+ buttons[i].text =
+ shortcut_escape(ctrl->radio.buttons[i],
+ (char)(ctrl->radio.shortcuts ?
+ ctrl->radio.shortcuts[i] :
+ NO_SHORTCUT));
+ buttons[i].id = base_id + 1 + i;
+ if (ctrl->radio.shortcuts) {
+ assert(nshortcuts < MAX_SHORTCUTS_PER_CTRL);
+ shortcuts[nshortcuts++] = ctrl->radio.shortcuts[i];
+ }
+ }
+
+ radioline_common(&pos, escaped, base_id,
+ ctrl->radio.ncolumns,
+ buttons, ctrl->radio.nbuttons);
+
+ for (i = 0; i < ctrl->radio.nbuttons; i++) {
+ sfree(buttons[i].text);
+ }
+ sfree(buttons);
+ sfree(escaped);
+ }
+ break;
+ case CTRL_CHECKBOX:
+ num_ids = 1;
+ escaped = shortcut_escape(ctrl->checkbox.label,
+ ctrl->checkbox.shortcut);
+ shortcuts[nshortcuts++] = ctrl->checkbox.shortcut;
+ checkbox(&pos, escaped, base_id);
+ sfree(escaped);
+ break;
+ case CTRL_BUTTON:
+ escaped = shortcut_escape(ctrl->button.label,
+ ctrl->button.shortcut);
+ shortcuts[nshortcuts++] = ctrl->button.shortcut;
+ if (ctrl->button.iscancel)
+ actual_base_id = IDCANCEL;
+ num_ids = 1;
+ button(&pos, escaped, actual_base_id, ctrl->button.isdefault);
+ sfree(escaped);
+ break;
+ case CTRL_LISTBOX:
+ num_ids = 2;
+ escaped = shortcut_escape(ctrl->listbox.label,
+ ctrl->listbox.shortcut);
+ shortcuts[nshortcuts++] = ctrl->listbox.shortcut;
+ if (ctrl->listbox.draglist) {
+ data = snew(struct prefslist);
+ num_ids = 4;
+ prefslist(data, &pos, ctrl->listbox.height, escaped,
+ base_id, base_id+1, base_id+2, base_id+3);
+ shortcuts[nshortcuts++] = 'u'; /* Up */
+ shortcuts[nshortcuts++] = 'd'; /* Down */
+ } else if (ctrl->listbox.height == 0) {
+ /* Drop-down list. */
+ if (ctrl->listbox.percentwidth == 100) {
+ staticddlbig(&pos, escaped,
+ base_id, base_id+1);
+ } else {
+ staticddl(&pos, escaped, base_id,
+ base_id+1, ctrl->listbox.percentwidth);
+ }
+ } else {
+ /* Ordinary list. */
+ listbox(&pos, escaped, base_id, base_id+1,
+ ctrl->listbox.height, ctrl->listbox.multisel);
+ }
+ if (ctrl->listbox.ncols) {
+ /*
+ * This method of getting the box width is a bit of
+ * a hack; we'd do better to try to retrieve the
+ * actual width in dialog units from doctl() just
+ * before MapDialogRect. But that's going to be no
+ * fun, and this should be good enough accuracy.
+ */
+ int width = cp->width * ctrl->listbox.percentwidth;
+ int *tabarray;
+ int i, percent;
+
+ tabarray = snewn(ctrl->listbox.ncols-1, int);
+ percent = 0;
+ for (i = 0; i < ctrl->listbox.ncols-1; i++) {
+ percent += ctrl->listbox.percentages[i];
+ tabarray[i] = width * percent / 10000;
+ }
+ SendDlgItemMessage(cp->hwnd, base_id+1, LB_SETTABSTOPS,
+ ctrl->listbox.ncols-1, (LPARAM)tabarray);
+ sfree(tabarray);
+ }
+ sfree(escaped);
+ break;
+ case CTRL_FILESELECT:
+ num_ids = 3;
+ escaped = shortcut_escape(ctrl->fileselect.label,
+ ctrl->fileselect.shortcut);
+ shortcuts[nshortcuts++] = ctrl->fileselect.shortcut;
+ editbutton(&pos, escaped, base_id, base_id+1,
+ "Bro&wse...", base_id+2);
+ shortcuts[nshortcuts++] = 'w';
+ sfree(escaped);
+ break;
+ case CTRL_FONTSELECT:
+ num_ids = 3;
+ escaped = shortcut_escape(ctrl->fontselect.label,
+ ctrl->fontselect.shortcut);
+ shortcuts[nshortcuts++] = ctrl->fontselect.shortcut;
+ statictext(&pos, escaped, 1, base_id);
+ staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
+ sfree(escaped);
+ data = snew(FontSpec);
+ break;
+ default:
+ assert(!"Can't happen");
+ num_ids = 0; /* placate gcc */
+ break;
+ }
+
+ /*
+ * Create a `struct winctrl' for this control, and advance
+ * the dialog ID counter, if it's actually been created
+ * (and isn't tabdelayed).
+ */
+ if (pos.hwnd) {
+ struct winctrl *c = snew(struct winctrl);
+
+ c->ctrl = ctrl;
+ c->base_id = actual_base_id;
+ c->num_ids = num_ids;
+ c->data = data;
+ memcpy(c->shortcuts, shortcuts, sizeof(shortcuts));
+ winctrl_add(wc, c);
+ winctrl_add_shortcuts(dp, c);
+ if (actual_base_id == base_id)
+ base_id += num_ids;
+ }
+
+ if (colstart >= 0) {
+ /*
+ * Update the ypos in all columns crossed by this
+ * control.
+ */
+ int i;
+ for (i = colstart; i < colstart+colspan; i++)
+ columns[i].ypos = pos.ypos;
+ }
+ }
+
+ /*
+ * We've now finished laying out the controls; so now update
+ * the ctlpos and control ID that were passed in, terminate
+ * any containing box, and return.
+ */
+ for (i = 0; i < ncols; i++)
+ if (cp->ypos < columns[i].ypos)
+ cp->ypos = columns[i].ypos;
+ *id = base_id;
+
+ if (s->boxname && *s->boxname)
+ endbox(cp);
+}
+
+static void winctrl_set_focus(union control *ctrl, struct dlgparam *dp,
+ int has_focus)
+{
+ if (has_focus) {
+ if (dp->focused)
+ dp->lastfocused = dp->focused;
+ dp->focused = ctrl;
+ } else if (!has_focus && dp->focused == ctrl) {
+ dp->lastfocused = dp->focused;
+ dp->focused = NULL;
+ }
+}
+
+union control *dlg_last_focused(union control *ctrl, void *dlg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ return dp->focused == ctrl ? dp->lastfocused : dp->focused;
+}
+
+/*
+ * The dialog-box procedure calls this function to handle Windows
+ * messages on a control we manage.
+ */
+int winctrl_handle_command(struct dlgparam *dp, UINT msg,
+ WPARAM wParam, LPARAM lParam)
+{
+ struct winctrl *c;
+ union control *ctrl;
+ int i, id, ret;
+ static UINT draglistmsg = WM_NULL;
+
+ /*
+ * Filter out pointless window messages. Our interest is in
+ * WM_COMMAND and the drag list message, and nothing else.
+ */
+ if (draglistmsg == WM_NULL)
+ draglistmsg = RegisterWindowMessage (DRAGLISTMSGSTRING);
+
+ if (msg != draglistmsg && msg != WM_COMMAND && msg != WM_DRAWITEM)
+ return 0;
+
+ /*
+ * Look up the control ID in our data.
+ */
+ c = NULL;
+ for (i = 0; i < dp->nctrltrees; i++) {
+ c = winctrl_findbyid(dp->controltrees[i], LOWORD(wParam));
+ if (c)
+ break;
+ }
+ if (!c)
+ return 0; /* we have nothing to do */
+
+ if (msg == WM_DRAWITEM) {
+ /*
+ * Owner-draw request for a panel title.
+ */
+ LPDRAWITEMSTRUCT di = (LPDRAWITEMSTRUCT) lParam;
+ HDC hdc = di->hDC;
+ RECT r = di->rcItem;
+ SIZE s;
+
+ SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
+
+ GetTextExtentPoint32(hdc, (char *)c->data,
+ strlen((char *)c->data), &s);
+ DrawEdge(hdc, &r, EDGE_ETCHED, BF_ADJUST | BF_RECT);
+ TextOut(hdc,
+ r.left + (r.right-r.left-s.cx)/2,
+ r.top + (r.bottom-r.top-s.cy)/2,
+ (char *)c->data, strlen((char *)c->data));
+
+ return TRUE;
+ }
+
+ ctrl = c->ctrl;
+ id = LOWORD(wParam) - c->base_id;
+
+ if (!ctrl || !ctrl->generic.handler)
+ return 0; /* nothing we can do here */
+
+ /*
+ * From here on we do not issue `return' statements until the
+ * very end of the dialog box: any event handler is entitled to
+ * ask for a colour selector, so we _must_ always allow control
+ * to reach the end of this switch statement so that the
+ * subsequent code can test dp->coloursel_wanted().
+ */
+ ret = 0;
+ dp->coloursel_wanted = FALSE;
+
+ /*
+ * Now switch on the control type and the message.
+ */
+ switch (ctrl->generic.type) {
+ case CTRL_EDITBOX:
+ if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
+ (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
+ if (msg == WM_COMMAND && ctrl->editbox.has_list &&
+ (HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
+
+ if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
+ HIWORD(wParam) == EN_CHANGE)
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
+ if (msg == WM_COMMAND &&
+ ctrl->editbox.has_list) {
+ if (HIWORD(wParam) == CBN_SELCHANGE) {
+ int index, len;
+ char *text;
+
+ index = SendDlgItemMessage(dp->hwnd, c->base_id+1,
+ CB_GETCURSEL, 0, 0);
+ len = SendDlgItemMessage(dp->hwnd, c->base_id+1,
+ CB_GETLBTEXTLEN, index, 0);
+ text = snewn(len+1, char);
+ SendDlgItemMessage(dp->hwnd, c->base_id+1, CB_GETLBTEXT,
+ index, (LPARAM)text);
+ SetDlgItemText(dp->hwnd, c->base_id+1, text);
+ sfree(text);
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
+ } else if (HIWORD(wParam) == CBN_EDITCHANGE) {
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
+ } else if (HIWORD(wParam) == CBN_KILLFOCUS) {
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
+ }
+
+ }
+ break;
+ case CTRL_RADIO:
+ if (msg == WM_COMMAND &&
+ (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
+ /*
+ * We sometimes get spurious BN_CLICKED messages for the
+ * radio button that is just about to _lose_ selection, if
+ * we're switching using the arrow keys. Therefore we
+ * double-check that the button in wParam is actually
+ * checked before generating an event.
+ */
+ if (msg == WM_COMMAND &&
+ (HIWORD(wParam) == BN_CLICKED ||
+ HIWORD(wParam) == BN_DOUBLECLICKED) &&
+ IsDlgButtonChecked(dp->hwnd, LOWORD(wParam))) {
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
+ }
+ break;
+ case CTRL_CHECKBOX:
+ if (msg == WM_COMMAND &&
+ (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
+ if (msg == WM_COMMAND &&
+ (HIWORD(wParam) == BN_CLICKED ||
+ HIWORD(wParam) == BN_DOUBLECLICKED)) {
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
+ }
+ break;
+ case CTRL_BUTTON:
+ if (msg == WM_COMMAND &&
+ (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
+ if (msg == WM_COMMAND &&
+ (HIWORD(wParam) == BN_CLICKED ||
+ HIWORD(wParam) == BN_DOUBLECLICKED)) {
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_ACTION);
+ }
+ break;
+ case CTRL_LISTBOX:
+ if (msg == WM_COMMAND && ctrl->listbox.height != 0 &&
+ (HIWORD(wParam)==LBN_SETFOCUS || HIWORD(wParam)==LBN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == LBN_SETFOCUS);
+ if (msg == WM_COMMAND && ctrl->listbox.height == 0 &&
+ (HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
+ if (msg == WM_COMMAND && id >= 2 &&
+ (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
+ if (ctrl->listbox.draglist) {
+ int pret;
+ pret = handle_prefslist(c->data, NULL, 0, (msg != WM_COMMAND),
+ dp->hwnd, wParam, lParam);
+ if (pret & 2)
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
+ ret = pret & 1;
+ } else {
+ if (msg == WM_COMMAND && HIWORD(wParam) == LBN_DBLCLK) {
+ SetCapture(dp->hwnd);
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_ACTION);
+ } else if (msg == WM_COMMAND && HIWORD(wParam) == LBN_SELCHANGE) {
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_SELCHANGE);
+ }
+ }
+ break;
+ case CTRL_FILESELECT:
+ if (msg == WM_COMMAND && id == 1 &&
+ (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
+ if (msg == WM_COMMAND && id == 2 &&
+ (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
+ if (msg == WM_COMMAND && id == 1 && HIWORD(wParam) == EN_CHANGE)
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
+ if (id == 2 &&
+ (msg == WM_COMMAND &&
+ (HIWORD(wParam) == BN_CLICKED ||
+ HIWORD(wParam) == BN_DOUBLECLICKED))) {
+ OPENFILENAME of;
+ char filename[FILENAME_MAX];
+ int ret;
+
+ memset(&of, 0, sizeof(of));
+#ifdef OPENFILENAME_SIZE_VERSION_400
+ of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
+#else
+ of.lStructSize = sizeof(of);
+#endif
+ of.hwndOwner = dp->hwnd;
+ if (ctrl->fileselect.filter)
+ of.lpstrFilter = ctrl->fileselect.filter;
+ else
+ of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
+ of.lpstrCustomFilter = NULL;
+ of.nFilterIndex = 1;
+ of.lpstrFile = filename;
+ GetDlgItemText(dp->hwnd, c->base_id+1, filename, lenof(filename));
+ filename[lenof(filename)-1] = '\0';
+ of.nMaxFile = lenof(filename);
+ of.lpstrFileTitle = NULL;
+ of.lpstrInitialDir = NULL;
+ of.lpstrTitle = ctrl->fileselect.title;
+ of.Flags = 0;
+ if (ctrl->fileselect.for_writing)
+ ret = GetSaveFileName(&of);
+ else
+ ret = GetOpenFileName(&of);
+ if (ret) {
+ SetDlgItemText(dp->hwnd, c->base_id + 1, filename);
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
+ }
+ }
+ break;
+ case CTRL_FONTSELECT:
+ if (msg == WM_COMMAND && id == 2 &&
+ (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
+ winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
+ if (id == 2 &&
+ (msg == WM_COMMAND &&
+ (HIWORD(wParam) == BN_CLICKED ||
+ HIWORD(wParam) == BN_DOUBLECLICKED))) {
+ CHOOSEFONT cf;
+ LOGFONT lf;
+ HDC hdc;
+ FontSpec fs = *(FontSpec *)c->data;
+
+ hdc = GetDC(0);
+ lf.lfHeight = -MulDiv(fs.height,
+ GetDeviceCaps(hdc, LOGPIXELSY), 72);
+ ReleaseDC(0, hdc);
+ lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
+ lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
+ lf.lfWeight = (fs.isbold ? FW_BOLD : 0);
+ lf.lfCharSet = fs.charset;
+ lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
+ lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
+ lf.lfQuality = DEFAULT_QUALITY;
+ lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
+ strncpy(lf.lfFaceName, fs.name,
+ sizeof(lf.lfFaceName) - 1);
+ lf.lfFaceName[sizeof(lf.lfFaceName) - 1] = '\0';
+
+ cf.lStructSize = sizeof(cf);
+ cf.hwndOwner = dp->hwnd;
+ cf.lpLogFont = &lf;
+ cf.Flags = CF_FIXEDPITCHONLY | CF_FORCEFONTEXIST |
+ CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
+
+ if (ChooseFont(&cf)) {
+ strncpy(fs.name, lf.lfFaceName,
+ sizeof(fs.name) - 1);
+ fs.name[sizeof(fs.name) - 1] = '\0';
+ fs.isbold = (lf.lfWeight == FW_BOLD);
+ fs.charset = lf.lfCharSet;
+ fs.height = cf.iPointSize / 10;
+ dlg_fontsel_set(ctrl, dp, fs);
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
+ }
+ }
+ break;
+ }
+
+ /*
+ * If the above event handler has asked for a colour selector,
+ * now is the time to generate one.
+ */
+ if (dp->coloursel_wanted) {
+ static CHOOSECOLOR cc;
+ static DWORD custom[16] = { 0 }; /* zero initialisers */
+ cc.lStructSize = sizeof(cc);
+ cc.hwndOwner = dp->hwnd;
+ cc.hInstance = (HWND) hinst;
+ cc.lpCustColors = custom;
+ cc.rgbResult = RGB(dp->coloursel_result.r,
+ dp->coloursel_result.g,
+ dp->coloursel_result.b);
+ cc.Flags = CC_FULLOPEN | CC_RGBINIT;
+ if (ChooseColor(&cc)) {
+ dp->coloursel_result.r =
+ (unsigned char) (cc.rgbResult & 0xFF);
+ dp->coloursel_result.g =
+ (unsigned char) (cc.rgbResult >> 8) & 0xFF;
+ dp->coloursel_result.b =
+ (unsigned char) (cc.rgbResult >> 16) & 0xFF;
+ dp->coloursel_result.ok = TRUE;
+ } else
+ dp->coloursel_result.ok = FALSE;
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_CALLBACK);
+ }
+
+ return ret;
+}
+
+/*
+ * This function can be called to produce context help on a
+ * control. Returns TRUE if it has actually launched WinHelp.
+ */
+int winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id)
+{
+ int i;
+ struct winctrl *c;
+ char *cmd;
+
+ /*
+ * Look up the control ID in our data.
+ */
+ c = NULL;
+ for (i = 0; i < dp->nctrltrees; i++) {
+ c = winctrl_findbyid(dp->controltrees[i], id);
+ if (c)
+ break;
+ }
+ if (!c)
+ return 0; /* we have nothing to do */
+
+ /*
+ * This is the Windows front end, so we're allowed to assume
+ * `helpctx.p' is a context string.
+ */
+ if (!c->ctrl || !c->ctrl->generic.helpctx.p)
+ return 0; /* no help available for this ctrl */
+
+ cmd = dupprintf("JI(`',`%s')", c->ctrl->generic.helpctx.p);
+ WinHelp(hwnd, help_path, HELP_COMMAND, (DWORD)cmd);
+ sfree(cmd);
+ return 1;
+}
+
+/*
+ * Now the various functions that the platform-independent
+ * mechanism can call to access the dialog box entries.
+ */
+
+static struct winctrl *dlg_findbyctrl(struct dlgparam *dp, union control *ctrl)
+{
+ int i;
+
+ for (i = 0; i < dp->nctrltrees; i++) {
+ struct winctrl *c = winctrl_findbyctrl(dp->controltrees[i], ctrl);
+ if (c)
+ return c;
+ }
+ return NULL;
+}
+
+void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_RADIO);
+ CheckRadioButton(dp->hwnd,
+ c->base_id + 1,
+ c->base_id + c->ctrl->radio.nbuttons,
+ c->base_id + 1 + whichbutton);
+}
+
+int dlg_radiobutton_get(union control *ctrl, void *dlg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ int i;
+ assert(c && c->ctrl->generic.type == CTRL_RADIO);
+ for (i = 0; i < c->ctrl->radio.nbuttons; i++)
+ if (IsDlgButtonChecked(dp->hwnd, c->base_id + 1 + i))
+ return i;
+ assert(!"No radio button was checked?!");
+ return 0;
+}
+
+void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
+ CheckDlgButton(dp->hwnd, c->base_id, (checked != 0));
+}
+
+int dlg_checkbox_get(union control *ctrl, void *dlg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
+ return 0 != IsDlgButtonChecked(dp->hwnd, c->base_id);
+}
+
+void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
+ SetDlgItemText(dp->hwnd, c->base_id+1, text);
+}
+
+void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
+ GetDlgItemText(dp->hwnd, c->base_id+1, buffer, length);
+ buffer[length-1] = '\0';
+}
+
+/* The `listbox' functions can also apply to combo boxes. */
+void dlg_listbox_clear(union control *ctrl, void *dlg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ int msg;
+ assert(c &&
+ (c->ctrl->generic.type == CTRL_LISTBOX ||
+ (c->ctrl->generic.type == CTRL_EDITBOX &&
+ c->ctrl->editbox.has_list)));
+ msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
+ LB_RESETCONTENT : CB_RESETCONTENT);
+ SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
+}
+
+void dlg_listbox_del(union control *ctrl, void *dlg, int index)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ int msg;
+ assert(c &&
+ (c->ctrl->generic.type == CTRL_LISTBOX ||
+ (c->ctrl->generic.type == CTRL_EDITBOX &&
+ c->ctrl->editbox.has_list)));
+ msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
+ LB_DELETESTRING : CB_DELETESTRING);
+ SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
+}
+
+void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ int msg;
+ assert(c &&
+ (c->ctrl->generic.type == CTRL_LISTBOX ||
+ (c->ctrl->generic.type == CTRL_EDITBOX &&
+ c->ctrl->editbox.has_list)));
+ msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
+ LB_ADDSTRING : CB_ADDSTRING);
+ SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
+}
+
+/*
+ * Each listbox entry may have a numeric id associated with it.
+ * Note that some front ends only permit a string to be stored at
+ * each position, which means that _if_ you put two identical
+ * strings in any listbox then you MUST not assign them different
+ * IDs and expect to get meaningful results back.
+ */
+void dlg_listbox_addwithid(union control *ctrl, void *dlg,
+ char const *text, int id)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ int msg, msg2, index;
+ assert(c &&
+ (c->ctrl->generic.type == CTRL_LISTBOX ||
+ (c->ctrl->generic.type == CTRL_EDITBOX &&
+ c->ctrl->editbox.has_list)));
+ msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
+ LB_ADDSTRING : CB_ADDSTRING);
+ msg2 = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
+ LB_SETITEMDATA : CB_SETITEMDATA);
+ index = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
+ SendDlgItemMessage(dp->hwnd, c->base_id+1, msg2, index, (LPARAM)id);
+}
+
+int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ int msg;
+ assert(c && c->ctrl->generic.type == CTRL_LISTBOX);
+ msg = (c->ctrl->listbox.height != 0 ? LB_GETITEMDATA : CB_GETITEMDATA);
+ return
+ SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
+}
+
+/* dlg_listbox_index returns <0 if no single element is selected. */
+int dlg_listbox_index(union control *ctrl, void *dlg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ int msg, ret;
+ assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
+ !c->ctrl->listbox.multisel);
+ msg = (c->ctrl->listbox.height != 0 ? LB_GETCURSEL : CB_GETCURSEL);
+ ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
+ if (ret == LB_ERR)
+ return -1;
+ else
+ return ret;
+}
+
+int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
+ c->ctrl->listbox.multisel &&
+ c->ctrl->listbox.height != 0);
+ return
+ SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSEL, index, 0);
+}
+
+void dlg_listbox_select(union control *ctrl, void *dlg, int index)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ int msg;
+ assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
+ !c->ctrl->listbox.multisel);
+ msg = (c->ctrl->listbox.height != 0 ? LB_SETCURSEL : CB_SETCURSEL);
+ SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
+}
+
+void dlg_text_set(union control *ctrl, void *dlg, char const *text)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_TEXT);
+ SetDlgItemText(dp->hwnd, c->base_id, text);
+}
+
+void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
+ SetDlgItemText(dp->hwnd, c->base_id+1, fn.path);
+}
+
+void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
+ GetDlgItemText(dp->hwnd, c->base_id+1, fn->path, lenof(fn->path));
+ fn->path[lenof(fn->path)-1] = '\0';
+}
+
+void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs)
+{
+ char *buf, *boldstr;
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
+
+ *(FontSpec *)c->data = fs; /* structure copy */
+
+ boldstr = (fs.isbold ? "bold, " : "");
+ if (fs.height == 0)
+ buf = dupprintf("Font: %s, %sdefault height", fs.name, boldstr);
+ else
+ buf = dupprintf("Font: %s, %s%d-point", fs.name, boldstr,
+ (fs.height < 0 ? -fs.height : fs.height));
+ SetDlgItemText(dp->hwnd, c->base_id+1, buf);
+ sfree(buf);
+}
+
+void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
+ *fs = *(FontSpec *)c->data; /* structure copy */
+}
+
+/*
+ * Bracketing a large set of updates in these two functions will
+ * cause the front end (if possible) to delay updating the screen
+ * until it's all complete, thus avoiding flicker.
+ */
+void dlg_update_start(union control *ctrl, void *dlg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
+ SendDlgItemMessage(dp->hwnd, c->base_id+1, WM_SETREDRAW, FALSE, 0);
+ }
+}
+
+void dlg_update_done(union control *ctrl, void *dlg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
+ HWND hw = GetDlgItem(dp->hwnd, c->base_id+1);
+ SendMessage(hw, WM_SETREDRAW, TRUE, 0);
+ InvalidateRect(hw, NULL, TRUE);
+ }
+}
+
+void dlg_set_focus(union control *ctrl, void *dlg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct winctrl *c = dlg_findbyctrl(dp, ctrl);
+ int id;
+ HWND ctl;
+ switch (ctrl->generic.type) {
+ case CTRL_EDITBOX: id = c->base_id + 1; break;
+ case CTRL_RADIO:
+ for (id = c->base_id + ctrl->radio.nbuttons; id > 1; id--)
+ if (IsDlgButtonChecked(dp->hwnd, id))
+ break;
+ /*
+ * In the theoretically-unlikely case that no button was
+ * selected, id should come out of this as 1, which is a
+ * reasonable enough choice.
+ */
+ break;
+ case CTRL_CHECKBOX: id = c->base_id; break;
+ case CTRL_BUTTON: id = c->base_id; break;
+ case CTRL_LISTBOX: id = c->base_id + 1; break;
+ case CTRL_FILESELECT: id = c->base_id + 1; break;
+ case CTRL_FONTSELECT: id = c->base_id + 2; break;
+ default: id = c->base_id; break;
+ }
+ ctl = GetDlgItem(dp->hwnd, id);
+ SetFocus(ctl);
+}