+/*
+ * 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);
+}
+
+/*
+ * During event processing, you might well want to give an error
+ * indication to the user. dlg_beep() is a quick and easy generic
+ * error; dlg_error() puts up a message-box or equivalent.
+ */
+void dlg_beep(void *dlg)
+{
+ /* struct dlgparam *dp = (struct dlgparam *)dlg; */
+ MessageBeep(0);
+}
+
+void dlg_error_msg(void *dlg, char *msg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ MessageBox(dp->hwnd, msg,
+ dp->errtitle ? dp->errtitle : NULL,
+ MB_OK | MB_ICONERROR);
+}
+
+/*
+ * This function signals to the front end that the dialog's
+ * processing is completed, and passes an integer value (typically
+ * a success status).
+ */
+void dlg_end(void *dlg, int value)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ dp->ended = TRUE;
+ dp->endresult = value;
+}
+
+void dlg_refresh(union control *ctrl, void *dlg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ int i, j;
+ struct winctrl *c;
+
+ if (!ctrl) {
+ /*
+ * Send EVENT_REFRESH to absolutely everything.
+ */
+ for (j = 0; j < dp->nctrltrees; j++) {
+ for (i = 0;
+ (c = winctrl_findbyindex(dp->controltrees[j], i)) != NULL;
+ i++) {
+ if (c->ctrl && c->ctrl->generic.handler != NULL)
+ c->ctrl->generic.handler(c->ctrl, dp,
+ dp->data, EVENT_REFRESH);
+ }
+ }
+ } else {
+ /*
+ * Send EVENT_REFRESH to a specific control.
+ */
+ if (ctrl->generic.handler != NULL)
+ ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
+ }
+}
+
+void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ dp->coloursel_wanted = TRUE;
+ dp->coloursel_result.r = r;
+ dp->coloursel_result.g = g;
+ dp->coloursel_result.b = b;
+}
+
+int dlg_coloursel_results(union control *ctrl, void *dlg,
+ int *r, int *g, int *b)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ if (dp->coloursel_result.ok) {
+ *r = dp->coloursel_result.r;
+ *g = dp->coloursel_result.g;
+ *b = dp->coloursel_result.b;
+ return 1;
+ } else
+ return 0;
+}
+
+struct perctrl_privdata {
+ union control *ctrl;
+ void *data;
+ int needs_free;
+};
+
+static int perctrl_privdata_cmp(void *av, void *bv)
+{
+ struct perctrl_privdata *a = (struct perctrl_privdata *)av;
+ struct perctrl_privdata *b = (struct perctrl_privdata *)bv;
+ if (a->ctrl < b->ctrl)
+ return -1;
+ else if (a->ctrl > b->ctrl)
+ return +1;
+ return 0;
+}
+
+void dp_init(struct dlgparam *dp)
+{
+ dp->nctrltrees = 0;
+ dp->data = NULL;
+ dp->ended = FALSE;
+ dp->focused = dp->lastfocused = NULL;
+ memset(dp->shortcuts, 0, sizeof(dp->shortcuts));
+ dp->hwnd = NULL;
+ dp->wintitle = dp->errtitle = NULL;
+ dp->privdata = newtree234(perctrl_privdata_cmp);
+}
+
+void dp_add_tree(struct dlgparam *dp, struct winctrls *wc)
+{
+ assert(dp->nctrltrees < lenof(dp->controltrees));
+ dp->controltrees[dp->nctrltrees++] = wc;
+}
+
+void dp_cleanup(struct dlgparam *dp)
+{
+ struct perctrl_privdata *p;
+
+ if (dp->privdata) {
+ while ( (p = index234(dp->privdata, 0)) != NULL ) {
+ del234(dp->privdata, p);
+ if (p->needs_free)
+ sfree(p->data);
+ sfree(p);
+ }
+ freetree234(dp->privdata);
+ dp->privdata = NULL;
+ }
+ sfree(dp->wintitle);
+ sfree(dp->errtitle);
+}
+
+void *dlg_get_privdata(union control *ctrl, void *dlg)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct perctrl_privdata tmp, *p;
+ tmp.ctrl = ctrl;
+ p = find234(dp->privdata, &tmp, NULL);
+ if (p)
+ return p->data;
+ else
+ return NULL;
+}
+
+void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct perctrl_privdata tmp, *p;
+ tmp.ctrl = ctrl;
+ p = find234(dp->privdata, &tmp, NULL);
+ if (!p) {
+ p = snew(struct perctrl_privdata);
+ p->ctrl = ctrl;
+ p->needs_free = FALSE;
+ add234(dp->privdata, p);
+ }
+ p->data = ptr;
+}
+
+void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
+{
+ struct dlgparam *dp = (struct dlgparam *)dlg;
+ struct perctrl_privdata tmp, *p;
+ tmp.ctrl = ctrl;
+ p = find234(dp->privdata, &tmp, NULL);
+ if (!p) {
+ p = snew(struct perctrl_privdata);
+ p->ctrl = ctrl;
+ p->needs_free = FALSE;
+ add234(dp->privdata, p);
+ }
+ assert(!p->needs_free);
+ p->needs_free = TRUE;
+ /*
+ * This is an internal allocation routine, so it's allowed to
+ * use smalloc directly.
+ */
+ p->data = smalloc(size);
+ return p->data;
+}