2 * winctrls.c: routines to self-manage the controls in a dialog
7 * Possible TODO in new cross-platform config box stuff:
9 * - When lining up two controls alongside each other, I wonder if
10 * we could conveniently arrange to centre them vertically?
11 * Particularly ugly in the current setup is the `Add new
12 * forwarded port:' static next to the rather taller `Remove'
30 #define STATICHEIGHT 8
31 #define TITLEHEIGHT 12
32 #define CHECKBOXHEIGHT 8
36 #define LISTINCREMENT 8
37 #define COMBOHEIGHT 12
38 #define PUSHBTNHEIGHT 14
39 #define PROGBARHEIGHT 14
41 void ctlposinit(struct ctlpos *cp, HWND hwnd,
42 int leftborder, int rightborder, int topborder)
46 cp->font = SendMessage(hwnd, WM_GETFONT, 0, 0);
48 GetClientRect(hwnd, &r);
52 MapDialogRect(hwnd, &r2);
53 cp->dlu4inpix = r2.right;
54 cp->width = (r.right * 4) / (r2.right) - 2 * GAPBETWEEN;
55 cp->xoff = leftborder;
56 cp->width -= leftborder + rightborder;
59 HWND doctl(struct ctlpos *cp, RECT r,
60 char *wclass, int wstyle, int exstyle, char *wtext, int wid)
64 * Note nonstandard use of RECT. This is deliberate: by
65 * transforming the width and height directly we arrange to
66 * have all supposedly same-sized controls really same-sized.
70 MapDialogRect(cp->hwnd, &r);
73 * We can pass in cp->hwnd == NULL, to indicate a dry run
74 * without creating any actual controls.
77 ctl = CreateWindowEx(exstyle, wclass, wtext, wstyle,
78 r.left, r.top, r.right, r.bottom,
79 cp->hwnd, (HMENU) wid, hinst, NULL);
80 SendMessage(ctl, WM_SETFONT, cp->font, MAKELPARAM(TRUE, 0));
82 if (!strcmp(wclass, "LISTBOX")) {
84 * Bizarre Windows bug: the list box calculates its
85 * number of lines based on the font it has at creation
86 * time, but sending it WM_SETFONT doesn't cause it to
87 * recalculate. So now, _after_ we've sent it
88 * WM_SETFONT, we explicitly resize it (to the same
89 * size it was already!) to force it to reconsider.
91 SetWindowPos(ctl, NULL, 0, 0, r.right, r.bottom,
92 SWP_NOACTIVATE | SWP_NOCOPYBITS |
93 SWP_NOMOVE | SWP_NOZORDER);
101 * A title bar across the top of a sub-dialog.
103 void bartitle(struct ctlpos *cp, char *name, int id)
110 r.bottom = STATICHEIGHT;
111 cp->ypos += r.bottom + GAPBETWEEN;
112 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, name, id);
116 * Begin a grouping box, with or without a group title.
118 void beginbox(struct ctlpos *cp, char *name, int idbox)
120 cp->boxystart = cp->ypos;
122 cp->boxystart -= STATICHEIGHT / 2;
124 cp->ypos += STATICHEIGHT;
126 cp->width -= 2 * GAPXBOX;
133 * End a grouping box.
135 void endbox(struct ctlpos *cp)
139 cp->width += 2 * GAPXBOX;
140 cp->ypos += GAPYBOX - GAPBETWEEN;
143 r.top = cp->boxystart;
144 r.bottom = cp->ypos - cp->boxystart;
145 doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0,
146 cp->boxtext ? cp->boxtext : "", cp->boxid);
151 * A static line, followed by a full-width edit box.
153 void editboxfw(struct ctlpos *cp, int password, char *text,
154 int staticid, int editid)
163 r.bottom = STATICHEIGHT;
164 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
165 cp->ypos += STATICHEIGHT + GAPWITHIN;
168 r.bottom = EDITHEIGHT;
170 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL |
171 (password ? ES_PASSWORD : 0),
172 WS_EX_CLIENTEDGE, "", editid);
173 cp->ypos += EDITHEIGHT + GAPBETWEEN;
177 * A static line, followed by a full-width combo box.
179 void combobox(struct ctlpos *cp, char *text, int staticid, int listid)
188 r.bottom = STATICHEIGHT;
189 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
190 cp->ypos += STATICHEIGHT + GAPWITHIN;
193 r.bottom = COMBOHEIGHT * 10;
194 doctl(cp, r, "COMBOBOX",
195 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
196 CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
197 cp->ypos += COMBOHEIGHT + GAPBETWEEN;
200 struct radio { char *text; int id; };
202 static void radioline_common(struct ctlpos *cp, char *text, int id,
203 int nacross, struct radio *buttons, int nbuttons)
214 r.bottom = STATICHEIGHT;
215 cp->ypos += r.bottom + GAPWITHIN;
216 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
221 for (j = 0; j < nbuttons; j++) {
222 char *btext = buttons[j].text;
223 int bid = buttons[j].id;
226 cp->ypos += r.bottom + (nacross > 1 ? GAPBETWEEN : GAPWITHIN);
229 r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
232 (i + 1) * (cp->width + GAPBETWEEN) / nacross - r.left;
234 r.right = cp->width - r.left;
236 r.bottom = RADIOHEIGHT;
237 doctl(cp, r, "BUTTON",
238 BS_NOTIFY | BS_AUTORADIOBUTTON | WS_CHILD |
239 WS_VISIBLE | WS_TABSTOP | group, 0, btext, bid);
243 cp->ypos += r.bottom + GAPBETWEEN;
247 * A set of radio buttons on the same line, with a static above
248 * them. `nacross' dictates how many parts the line is divided into
249 * (you might want this not to equal the number of buttons if you
250 * needed to line up some 2s and some 3s to look good in the same
253 * There's a bit of a hack in here to ensure that if nacross
254 * exceeds the actual number of buttons, the rightmost button
255 * really does get all the space right to the edge of the line, so
256 * you can do things like
258 * (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
260 void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...)
263 struct radio *buttons;
266 va_start(ap, nacross);
269 char *btext = va_arg(ap, char *);
272 (void) va_arg(ap, int); /* id */
276 buttons = snewn(nbuttons, struct radio);
277 va_start(ap, nacross);
278 for (i = 0; i < nbuttons; i++) {
279 buttons[i].text = va_arg(ap, char *);
280 buttons[i].id = va_arg(ap, int);
283 radioline_common(cp, text, id, nacross, buttons, nbuttons);
288 * A set of radio buttons on the same line, without a static above
289 * them. Otherwise just like radioline.
291 void bareradioline(struct ctlpos *cp, int nacross, ...)
294 struct radio *buttons;
297 va_start(ap, nacross);
300 char *btext = va_arg(ap, char *);
303 (void) va_arg(ap, int); /* id */
306 buttons = snewn(nbuttons, struct radio);
307 va_start(ap, nacross);
308 for (i = 0; i < nbuttons; i++) {
309 buttons[i].text = va_arg(ap, char *);
310 buttons[i].id = va_arg(ap, int);
313 radioline_common(cp, NULL, 0, nacross, buttons, nbuttons);
318 * A set of radio buttons on multiple lines, with a static above
321 void radiobig(struct ctlpos *cp, char *text, int id, ...)
324 struct radio *buttons;
330 char *btext = va_arg(ap, char *);
333 (void) va_arg(ap, int); /* id */
336 buttons = snewn(nbuttons, struct radio);
338 for (i = 0; i < nbuttons; i++) {
339 buttons[i].text = va_arg(ap, char *);
340 buttons[i].id = va_arg(ap, int);
343 radioline_common(cp, text, id, 1, buttons, nbuttons);
348 * A single standalone checkbox.
350 void checkbox(struct ctlpos *cp, char *text, int id)
357 r.bottom = CHECKBOXHEIGHT;
358 cp->ypos += r.bottom + GAPBETWEEN;
359 doctl(cp, r, "BUTTON",
360 BS_NOTIFY | BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
365 * Wrap a piece of text for a static text control. Returns the
366 * wrapped text (a malloc'ed string containing \ns), and also
367 * returns the number of lines required.
369 char *staticwrap(struct ctlpos *cp, HWND hwnd, char *text, int *lines)
371 HDC hdc = GetDC(hwnd);
372 int width, nlines, j;
377 HFONT oldfont, newfont;
379 ret = snewn(1+strlen(text), char);
382 pwidths = snewn(1+strlen(text), INT);
385 * Work out the width the text will need to fit in, by doing
386 * the same adjustment that the `statictext' function itself
389 SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
390 r.left = r.top = r.bottom = 0;
392 MapDialogRect(hwnd, &r);
398 * We must select the correct font into the HDC before calling
399 * GetTextExtent*, or silly things will happen.
401 newfont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
402 oldfont = SelectObject(hdc, newfont);
405 if (!GetTextExtentExPoint(hdc, p, strlen(p), width,
406 &nfit, pwidths, &size) ||
407 (size_t)nfit >= strlen(p)) {
409 * Either GetTextExtentExPoint returned failure, or the
410 * whole of the rest of the text fits on this line.
411 * Either way, we stop wrapping, copy the remainder of
412 * the input string unchanged to the output, and leave.
419 * Now we search backwards along the string from `nfit',
420 * looking for a space at which to break the line. If we
421 * don't find one at all, that's fine - we'll just break
422 * the line at `nfit'.
424 for (j = nfit; j > 0; j--) {
425 if (isspace((unsigned char)p[j])) {
436 while (*p && isspace((unsigned char)*p))
442 SelectObject(hdc, oldfont);
443 ReleaseDC(cp->hwnd, hdc);
445 if (lines) *lines = nlines;
453 * A single standalone static text control.
455 void statictext(struct ctlpos *cp, char *text, int lines, int id)
462 r.bottom = STATICHEIGHT * lines;
463 cp->ypos += r.bottom + GAPBETWEEN;
464 doctl(cp, r, "STATIC",
465 WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP,
470 * An owner-drawn static text control for a panel title.
472 void paneltitle(struct ctlpos *cp, int id)
479 r.bottom = TITLEHEIGHT;
480 cp->ypos += r.bottom + GAPBETWEEN;
481 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
486 * A button on the right hand side, with a static to its left.
488 void staticbtn(struct ctlpos *cp, char *stext, int sid,
489 char *btext, int bid)
491 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
492 PUSHBTNHEIGHT : STATICHEIGHT);
494 int lwid, rwid, rpos;
496 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
497 lwid = rpos - 2 * GAPBETWEEN;
498 rwid = cp->width + GAPBETWEEN - rpos;
501 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
503 r.bottom = STATICHEIGHT;
504 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
507 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
509 r.bottom = PUSHBTNHEIGHT;
510 doctl(cp, r, "BUTTON",
511 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
514 cp->ypos += height + GAPBETWEEN;
518 * A simple push button.
520 void button(struct ctlpos *cp, char *btext, int bid, int defbtn)
527 r.bottom = PUSHBTNHEIGHT;
529 /* Q67655: the _dialog box_ must know which button is default
530 * as well as the button itself knowing */
531 if (defbtn && cp->hwnd)
532 SendMessage(cp->hwnd, DM_SETDEFID, bid, 0);
534 doctl(cp, r, "BUTTON",
535 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
536 (defbtn ? BS_DEFPUSHBUTTON : 0) | BS_PUSHBUTTON,
539 cp->ypos += PUSHBTNHEIGHT + GAPBETWEEN;
543 * Like staticbtn, but two buttons.
545 void static2btn(struct ctlpos *cp, char *stext, int sid,
546 char *btext1, int bid1, char *btext2, int bid2)
548 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
549 PUSHBTNHEIGHT : STATICHEIGHT);
551 int lwid, rwid1, rwid2, rpos1, rpos2;
553 rpos1 = GAPBETWEEN + (cp->width + GAPBETWEEN) / 2;
554 rpos2 = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
555 lwid = rpos1 - 2 * GAPBETWEEN;
556 rwid1 = rpos2 - rpos1 - GAPBETWEEN;
557 rwid2 = cp->width + GAPBETWEEN - rpos2;
560 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
562 r.bottom = STATICHEIGHT;
563 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
566 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
568 r.bottom = PUSHBTNHEIGHT;
569 doctl(cp, r, "BUTTON",
570 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
574 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
576 r.bottom = PUSHBTNHEIGHT;
577 doctl(cp, r, "BUTTON",
578 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
581 cp->ypos += height + GAPBETWEEN;
585 * An edit control on the right hand side, with a static to its left.
587 static void staticedit_internal(struct ctlpos *cp, char *stext,
588 int sid, int eid, int percentedit,
591 const int height = (EDITHEIGHT > STATICHEIGHT ?
592 EDITHEIGHT : STATICHEIGHT);
594 int lwid, rwid, rpos;
597 GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
598 lwid = rpos - 2 * GAPBETWEEN;
599 rwid = cp->width + GAPBETWEEN - rpos;
602 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
604 r.bottom = STATICHEIGHT;
605 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
608 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
610 r.bottom = EDITHEIGHT;
612 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
613 WS_EX_CLIENTEDGE, "", eid);
615 cp->ypos += height + GAPBETWEEN;
618 void staticedit(struct ctlpos *cp, char *stext,
619 int sid, int eid, int percentedit)
621 staticedit_internal(cp, stext, sid, eid, percentedit, 0);
624 void staticpassedit(struct ctlpos *cp, char *stext,
625 int sid, int eid, int percentedit)
627 staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
631 * A drop-down list box on the right hand side, with a static to
634 void staticddl(struct ctlpos *cp, char *stext,
635 int sid, int lid, int percentlist)
637 const int height = (COMBOHEIGHT > STATICHEIGHT ?
638 COMBOHEIGHT : STATICHEIGHT);
640 int lwid, rwid, rpos;
643 GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
644 lwid = rpos - 2 * GAPBETWEEN;
645 rwid = cp->width + GAPBETWEEN - rpos;
648 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
650 r.bottom = STATICHEIGHT;
651 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
654 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
656 r.bottom = COMBOHEIGHT*4;
657 doctl(cp, r, "COMBOBOX",
658 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
659 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
661 cp->ypos += height + GAPBETWEEN;
665 * A combo box on the right hand side, with a static to its left.
667 void staticcombo(struct ctlpos *cp, char *stext,
668 int sid, int lid, int percentlist)
670 const int height = (COMBOHEIGHT > STATICHEIGHT ?
671 COMBOHEIGHT : STATICHEIGHT);
673 int lwid, rwid, rpos;
676 GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
677 lwid = rpos - 2 * GAPBETWEEN;
678 rwid = cp->width + GAPBETWEEN - rpos;
681 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
683 r.bottom = STATICHEIGHT;
684 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
687 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
689 r.bottom = COMBOHEIGHT*10;
690 doctl(cp, r, "COMBOBOX",
691 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
692 CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
694 cp->ypos += height + GAPBETWEEN;
698 * A static, with a full-width drop-down list box below it.
700 void staticddlbig(struct ctlpos *cp, char *stext,
709 r.bottom = STATICHEIGHT;
710 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
711 cp->ypos += STATICHEIGHT;
717 r.bottom = COMBOHEIGHT*4;
718 doctl(cp, r, "COMBOBOX",
719 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
720 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
721 cp->ypos += COMBOHEIGHT + GAPBETWEEN;
725 * A big multiline edit control with a static labelling it.
727 void bigeditctrl(struct ctlpos *cp, char *stext,
728 int sid, int eid, int lines)
736 r.bottom = STATICHEIGHT;
737 cp->ypos += r.bottom + GAPWITHIN;
738 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
744 r.bottom = EDITHEIGHT + (lines - 1) * STATICHEIGHT;
745 cp->ypos += r.bottom + GAPBETWEEN;
747 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
748 WS_EX_CLIENTEDGE, "", eid);
752 * A list box with a static labelling it.
754 void listbox(struct ctlpos *cp, char *stext,
755 int sid, int lid, int lines, int multi)
763 r.bottom = STATICHEIGHT;
764 cp->ypos += r.bottom + GAPWITHIN;
765 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
771 r.bottom = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
772 cp->ypos += r.bottom + GAPBETWEEN;
773 doctl(cp, r, "LISTBOX",
774 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
775 LBS_NOTIFY | LBS_HASSTRINGS | LBS_USETABSTOPS |
776 (multi ? LBS_MULTIPLESEL : 0),
777 WS_EX_CLIENTEDGE, "", lid);
781 * A tab-control substitute when a real tab control is unavailable.
783 void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id)
785 const int height = (COMBOHEIGHT > STATICHEIGHT ?
786 COMBOHEIGHT : STATICHEIGHT);
788 int bigwid, lwid, rwid, rpos;
789 static const int BIGGAP = 15;
790 static const int MEDGAP = 3;
792 bigwid = cp->width + 2 * GAPBETWEEN - 2 * BIGGAP;
794 rpos = BIGGAP + (bigwid + BIGGAP) / 2;
795 lwid = rpos - 2 * BIGGAP;
796 rwid = bigwid + BIGGAP - rpos;
799 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
801 r.bottom = STATICHEIGHT;
802 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
805 r.top = cp->ypos + (height - COMBOHEIGHT) / 2;
807 r.bottom = COMBOHEIGHT * 10;
808 doctl(cp, r, "COMBOBOX",
809 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
810 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
812 cp->ypos += height + MEDGAP + GAPBETWEEN;
818 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
823 * A static line, followed by an edit control on the left hand side
824 * and a button on the right.
826 void editbutton(struct ctlpos *cp, char *stext, int sid,
827 int eid, char *btext, int bid)
829 const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
830 EDITHEIGHT : PUSHBTNHEIGHT);
832 int lwid, rwid, rpos;
837 r.bottom = STATICHEIGHT;
838 cp->ypos += r.bottom + GAPWITHIN;
839 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
841 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
842 lwid = rpos - 2 * GAPBETWEEN;
843 rwid = cp->width + GAPBETWEEN - rpos;
846 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
848 r.bottom = EDITHEIGHT;
850 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
851 WS_EX_CLIENTEDGE, "", eid);
854 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
856 r.bottom = PUSHBTNHEIGHT;
857 doctl(cp, r, "BUTTON",
858 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
861 cp->ypos += height + GAPBETWEEN;
865 * A special control for manipulating an ordered preference list
866 * (eg. for cipher selection).
867 * XXX: this is a rough hack and could be improved.
869 void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
870 char *stext, int sid, int listid, int upbid, int dnbid)
872 const static int percents[] = { 5, 75, 20 };
874 int xpos, percent = 0, i;
875 int listheight = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
876 const int BTNSHEIGHT = 2*PUSHBTNHEIGHT + GAPBETWEEN;
877 int totalheight, buttonpos;
879 /* Squirrel away IDs. */
880 hdl->listid = listid;
884 /* The static label. */
889 r.bottom = STATICHEIGHT;
890 cp->ypos += r.bottom + GAPWITHIN;
891 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
894 if (listheight > BTNSHEIGHT) {
895 totalheight = listheight;
896 buttonpos = (listheight - BTNSHEIGHT) / 2;
898 totalheight = BTNSHEIGHT;
902 for (i=0; i<3; i++) {
904 xpos = (cp->width + GAPBETWEEN) * percent / 100;
905 left = xpos + GAPBETWEEN;
906 percent += percents[i];
907 xpos = (cp->width + GAPBETWEEN) * percent / 100;
912 /* The drag list box. */
913 r.left = left; r.right = wid;
914 r.top = cp->ypos; r.bottom = listheight;
917 ctl = doctl(cp, r, "LISTBOX",
918 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
919 WS_VSCROLL | LBS_HASSTRINGS | LBS_USETABSTOPS,
927 /* The "Up" and "Down" buttons. */
928 /* XXX worry about accelerators if we have more than one
929 * prefslist on a panel */
930 r.left = left; r.right = wid;
931 r.top = cp->ypos + buttonpos; r.bottom = PUSHBTNHEIGHT;
932 doctl(cp, r, "BUTTON",
933 BS_NOTIFY | WS_CHILD | WS_VISIBLE |
934 WS_TABSTOP | BS_PUSHBUTTON,
937 r.left = left; r.right = wid;
938 r.top = cp->ypos + buttonpos + PUSHBTNHEIGHT + GAPBETWEEN;
939 r.bottom = PUSHBTNHEIGHT;
940 doctl(cp, r, "BUTTON",
941 BS_NOTIFY | WS_CHILD | WS_VISIBLE |
942 WS_TABSTOP | BS_PUSHBUTTON,
950 cp->ypos += totalheight + GAPBETWEEN;
955 * Helper function for prefslist: move item in list box.
957 static void pl_moveitem(HWND hwnd, int listid, int src, int dst)
961 /* Get the item's data. */
962 tlen = SendDlgItemMessage (hwnd, listid, LB_GETTEXTLEN, src, 0);
963 txt = snewn(tlen+1, char);
964 SendDlgItemMessage (hwnd, listid, LB_GETTEXT, src, (LPARAM) txt);
965 val = SendDlgItemMessage (hwnd, listid, LB_GETITEMDATA, src, 0);
966 /* Deselect old location. */
967 SendDlgItemMessage (hwnd, listid, LB_SETSEL, FALSE, src);
968 /* Delete it at the old location. */
969 SendDlgItemMessage (hwnd, listid, LB_DELETESTRING, src, 0);
970 /* Insert it at new location. */
971 SendDlgItemMessage (hwnd, listid, LB_INSERTSTRING, dst,
973 SendDlgItemMessage (hwnd, listid, LB_SETITEMDATA, dst,
976 SendDlgItemMessage (hwnd, listid, LB_SETCURSEL, dst, 0);
980 int pl_itemfrompt(HWND hwnd, POINT cursor, BOOL scroll)
983 POINT uppoint, downpoint;
984 int updist, downdist, upitem, downitem, i;
987 * Ghastly hackery to try to figure out not which
988 * _item_, but which _gap between items_, the user
989 * is pointing at. We do this by first working out
990 * which list item is under the cursor, and then
991 * working out how far the cursor would have to
992 * move up or down before the answer was different.
993 * Then we put the insertion point _above_ the
994 * current item if the upper edge is closer than
995 * the lower edge, or _below_ it if vice versa.
997 ret = LBItemFromPt(hwnd, cursor, scroll);
1000 ret = LBItemFromPt(hwnd, cursor, FALSE);
1001 updist = downdist = 0;
1002 for (i = 1; i < 4096 && (!updist || !downdist); i++) {
1003 uppoint = downpoint = cursor;
1006 upitem = LBItemFromPt(hwnd, uppoint, FALSE);
1007 downitem = LBItemFromPt(hwnd, downpoint, FALSE);
1008 if (!updist && upitem != ret)
1010 if (!downdist && downitem != ret)
1013 if (downdist < updist)
1019 * Handler for prefslist above.
1021 * Return value has bit 0 set if the dialog box procedure needs to
1022 * return TRUE from handling this message; it has bit 1 set if a
1023 * change may have been made in the contents of the list.
1025 int handle_prefslist(struct prefslist *hdl,
1026 int *array, int maxmemb,
1027 int is_dlmsg, HWND hwnd,
1028 WPARAM wParam, LPARAM lParam)
1035 if ((int)wParam == hdl->listid) {
1036 DRAGLISTINFO *dlm = (DRAGLISTINFO *)lParam;
1037 int dest = 0; /* initialise to placate gcc */
1038 switch (dlm->uNotification) {
1040 /* Add a dummy item to make pl_itemfrompt() work
1042 * FIXME: this causes scrollbar glitches if the count of
1043 * listbox contains >= its height. */
1045 SendDlgItemMessage(hwnd, hdl->listid,
1046 LB_ADDSTRING, 0, (LPARAM) "");
1048 hdl->srcitem = LBItemFromPt(dlm->hWnd, dlm->ptCursor, TRUE);
1050 /* XXX hack Q183115 */
1051 SetWindowLongPtr(hwnd, DWLP_MSGRESULT, TRUE);
1054 DrawInsert(hwnd, dlm->hWnd, -1); /* Clear arrow */
1055 SendDlgItemMessage(hwnd, hdl->listid,
1056 LB_DELETESTRING, hdl->dummyitem, 0);
1061 dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, TRUE);
1062 if (dest > hdl->dummyitem) dest = hdl->dummyitem;
1063 DrawInsert (hwnd, dlm->hWnd, dest);
1065 SetWindowLongPtr(hwnd, DWLP_MSGRESULT, DL_MOVECURSOR);
1067 SetWindowLongPtr(hwnd, DWLP_MSGRESULT, DL_STOPCURSOR);
1070 if (hdl->dragging) {
1071 dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, TRUE);
1072 if (dest > hdl->dummyitem) dest = hdl->dummyitem;
1073 DrawInsert (hwnd, dlm->hWnd, -1);
1075 SendDlgItemMessage(hwnd, hdl->listid,
1076 LB_DELETESTRING, hdl->dummyitem, 0);
1077 if (hdl->dragging) {
1080 /* Correct for "missing" item. */
1081 if (dest > hdl->srcitem) dest--;
1082 pl_moveitem(hwnd, hdl->listid, hdl->srcitem, dest);
1092 if (((LOWORD(wParam) == hdl->upbid) ||
1093 (LOWORD(wParam) == hdl->dnbid)) &&
1094 ((HIWORD(wParam) == BN_CLICKED) ||
1095 (HIWORD(wParam) == BN_DOUBLECLICKED))) {
1096 /* Move an item up or down the list. */
1097 /* Get the current selection, if any. */
1098 int selection = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCURSEL, 0, 0);
1099 if (selection == LB_ERR) {
1103 /* Get the total number of items. */
1104 nitems = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCOUNT, 0, 0);
1105 /* Should we do anything? */
1106 if (LOWORD(wParam) == hdl->upbid && (selection > 0))
1107 pl_moveitem(hwnd, hdl->listid, selection, selection - 1);
1108 else if (LOWORD(wParam) == hdl->dnbid && (selection < nitems - 1))
1109 pl_moveitem(hwnd, hdl->listid, selection, selection + 1);
1118 /* Update array to match the list box. */
1119 for (i=0; i < maxmemb; i++)
1120 array[i] = SendDlgItemMessage (hwnd, hdl->listid, LB_GETITEMDATA,
1128 * A progress bar (from Common Controls). We like our progress bars
1129 * to be smooth and unbroken, without those ugly divisions; some
1130 * older compilers may not support that, but that's life.
1132 void progressbar(struct ctlpos *cp, int id)
1136 r.left = GAPBETWEEN;
1138 r.right = cp->width;
1139 r.bottom = PROGBARHEIGHT;
1140 cp->ypos += r.bottom + GAPBETWEEN;
1142 doctl(cp, r, PROGRESS_CLASS, WS_CHILD | WS_VISIBLE
1146 , WS_EX_CLIENTEDGE, "", id);
1149 /* ----------------------------------------------------------------------
1150 * Platform-specific side of portable dialog-box mechanism.
1154 * This function takes a string, escapes all the ampersands, and
1155 * places a single (unescaped) ampersand in front of the first
1156 * occurrence of the given shortcut character (which may be
1159 * Return value is a malloc'ed copy of the processed version of the
1162 static char *shortcut_escape(const char *text, char shortcut)
1169 return NULL; /* sfree won't choke on this */
1171 ret = snewn(2*strlen(text)+1, char); /* size potentially doubles! */
1172 shortcut = tolower((unsigned char)shortcut);
1177 if (shortcut != NO_SHORTCUT &&
1178 tolower((unsigned char)*p) == shortcut) {
1180 shortcut = NO_SHORTCUT; /* stop it happening twice */
1181 } else if (*p == '&') {
1190 void winctrl_add_shortcuts(struct dlgparam *dp, struct winctrl *c)
1193 for (i = 0; i < lenof(c->shortcuts); i++)
1194 if (c->shortcuts[i] != NO_SHORTCUT) {
1195 unsigned char s = tolower((unsigned char)c->shortcuts[i]);
1196 assert(!dp->shortcuts[s]);
1197 dp->shortcuts[s] = TRUE;
1201 void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c)
1204 for (i = 0; i < lenof(c->shortcuts); i++)
1205 if (c->shortcuts[i] != NO_SHORTCUT) {
1206 unsigned char s = tolower((unsigned char)c->shortcuts[i]);
1207 assert(dp->shortcuts[s]);
1208 dp->shortcuts[s] = FALSE;
1212 static int winctrl_cmp_byctrl(void *av, void *bv)
1214 struct winctrl *a = (struct winctrl *)av;
1215 struct winctrl *b = (struct winctrl *)bv;
1216 if (a->ctrl < b->ctrl)
1218 else if (a->ctrl > b->ctrl)
1223 static int winctrl_cmp_byid(void *av, void *bv)
1225 struct winctrl *a = (struct winctrl *)av;
1226 struct winctrl *b = (struct winctrl *)bv;
1227 if (a->base_id < b->base_id)
1229 else if (a->base_id > b->base_id)
1234 static int winctrl_cmp_byctrl_find(void *av, void *bv)
1236 union control *a = (union control *)av;
1237 struct winctrl *b = (struct winctrl *)bv;
1240 else if (a > b->ctrl)
1245 static int winctrl_cmp_byid_find(void *av, void *bv)
1248 struct winctrl *b = (struct winctrl *)bv;
1249 if (*a < b->base_id)
1251 else if (*a >= b->base_id + b->num_ids)
1257 void winctrl_init(struct winctrls *wc)
1259 wc->byctrl = newtree234(winctrl_cmp_byctrl);
1260 wc->byid = newtree234(winctrl_cmp_byid);
1262 void winctrl_cleanup(struct winctrls *wc)
1266 while ((c = index234(wc->byid, 0)) != NULL) {
1267 winctrl_remove(wc, c);
1272 freetree234(wc->byctrl);
1273 freetree234(wc->byid);
1274 wc->byctrl = wc->byid = NULL;
1277 void winctrl_add(struct winctrls *wc, struct winctrl *c)
1279 struct winctrl *ret;
1281 ret = add234(wc->byctrl, c);
1284 ret = add234(wc->byid, c);
1288 void winctrl_remove(struct winctrls *wc, struct winctrl *c)
1290 struct winctrl *ret;
1291 ret = del234(wc->byctrl, c);
1292 ret = del234(wc->byid, c);
1296 struct winctrl *winctrl_findbyctrl(struct winctrls *wc, union control *ctrl)
1298 return find234(wc->byctrl, ctrl, winctrl_cmp_byctrl_find);
1301 struct winctrl *winctrl_findbyid(struct winctrls *wc, int id)
1303 return find234(wc->byid, &id, winctrl_cmp_byid_find);
1306 struct winctrl *winctrl_findbyindex(struct winctrls *wc, int index)
1308 return index234(wc->byid, index);
1311 void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
1312 struct ctlpos *cp, struct controlset *s, int *id)
1314 struct ctlpos columns[16];
1315 int ncols, colstart, colspan;
1317 struct ctlpos tabdelays[16];
1318 union control *tabdelayed[16];
1323 char shortcuts[MAX_SHORTCUTS_PER_CTRL];
1326 int i, actual_base_id, base_id, num_ids;
1331 /* Start a containing box, if we have a boxname. */
1332 if (s->boxname && *s->boxname) {
1333 struct winctrl *c = snew(struct winctrl);
1335 c->base_id = base_id;
1338 memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
1340 beginbox(cp, s->boxtitle, base_id);
1344 /* Draw a title, if we have one. */
1345 if (!s->boxname && s->boxtitle) {
1346 struct winctrl *c = snew(struct winctrl);
1348 c->base_id = base_id;
1350 c->data = dupstr(s->boxtitle);
1351 memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
1353 paneltitle(cp, base_id);
1357 /* Initially we have just one column. */
1359 columns[0] = *cp; /* structure copy */
1361 /* And initially, there are no pending tab-delayed controls. */
1364 /* Loop over each control in the controlset. */
1365 for (i = 0; i < s->ncontrols; i++) {
1366 union control *ctrl = s->ctrls[i];
1369 * Generic processing that pertains to all control types.
1370 * At the end of this if statement, we'll have produced
1371 * `ctrl' (a pointer to the control we have to create, or
1372 * think about creating, in this iteration of the loop),
1373 * `pos' (a suitable ctlpos with which to position it), and
1374 * `c' (a winctrl structure to receive details of the
1375 * dialog IDs). Or we'll have done a `continue', if it was
1376 * CTRL_COLUMNS and doesn't require any control creation at
1379 if (ctrl->generic.type == CTRL_COLUMNS) {
1380 assert((ctrl->columns.ncols == 1) ^ (ncols == 1));
1384 * We're splitting into multiple columns.
1386 int lpercent, rpercent, lx, rx, i;
1388 ncols = ctrl->columns.ncols;
1389 assert(ncols <= lenof(columns));
1390 for (i = 1; i < ncols; i++)
1391 columns[i] = columns[0]; /* structure copy */
1394 for (i = 0; i < ncols; i++) {
1395 rpercent = lpercent + ctrl->columns.percentages[i];
1396 lx = columns[i].xoff + lpercent *
1397 (columns[i].width + GAPBETWEEN) / 100;
1398 rx = columns[i].xoff + rpercent *
1399 (columns[i].width + GAPBETWEEN) / 100;
1400 columns[i].xoff = lx;
1401 columns[i].width = rx - lx - GAPBETWEEN;
1402 lpercent = rpercent;
1406 * We're recombining the various columns into one.
1408 int maxy = columns[0].ypos;
1410 for (i = 1; i < ncols; i++)
1411 if (maxy < columns[i].ypos)
1412 maxy = columns[i].ypos;
1414 columns[0] = *cp; /* structure copy */
1415 columns[0].ypos = maxy;
1419 } else if (ctrl->generic.type == CTRL_TABDELAY) {
1422 assert(!ctrl->generic.tabdelay);
1423 ctrl = ctrl->tabdelay.ctrl;
1425 for (i = 0; i < ntabdelays; i++)
1426 if (tabdelayed[i] == ctrl)
1428 assert(i < ntabdelays); /* we have to have found it */
1430 pos = tabdelays[i]; /* structure copy */
1432 colstart = colspan = -1; /* indicate this was tab-delayed */
1436 * If it wasn't one of those, it's a genuine control;
1437 * so we'll have to compute a position for it now, by
1438 * checking its column span.
1442 colstart = COLUMN_START(ctrl->generic.column);
1443 colspan = COLUMN_SPAN(ctrl->generic.column);
1445 pos = columns[colstart]; /* structure copy */
1446 pos.width = columns[colstart+colspan-1].width +
1447 (columns[colstart+colspan-1].xoff - columns[colstart].xoff);
1449 for (col = colstart; col < colstart+colspan; col++)
1450 if (pos.ypos < columns[col].ypos)
1451 pos.ypos = columns[col].ypos;
1454 * If this control is to be tabdelayed, add it to the
1455 * tabdelay list, and unset pos.hwnd to inhibit actual
1458 if (ctrl->generic.tabdelay) {
1459 assert(ntabdelays < lenof(tabdelays));
1460 tabdelays[ntabdelays] = pos; /* structure copy */
1461 tabdelayed[ntabdelays] = ctrl;
1467 /* Most controls don't need anything in c->data. */
1470 /* And they all start off with no shortcuts registered. */
1471 memset(shortcuts, NO_SHORTCUT, lenof(shortcuts));
1474 /* Almost all controls start at base_id. */
1475 actual_base_id = base_id;
1478 * Now we're ready to actually create the control, by
1479 * switching on its type.
1481 switch (ctrl->generic.type) {
1484 char *wrapped, *escaped;
1487 wrapped = staticwrap(&pos, cp->hwnd,
1488 ctrl->generic.label, &lines);
1489 escaped = shortcut_escape(wrapped, NO_SHORTCUT);
1490 statictext(&pos, escaped, lines, base_id);
1496 num_ids = 2; /* static, edit */
1497 escaped = shortcut_escape(ctrl->editbox.label,
1498 ctrl->editbox.shortcut);
1499 shortcuts[nshortcuts++] = ctrl->editbox.shortcut;
1500 if (ctrl->editbox.percentwidth == 100) {
1501 if (ctrl->editbox.has_list)
1502 combobox(&pos, escaped,
1503 base_id, base_id+1);
1505 editboxfw(&pos, ctrl->editbox.password, escaped,
1506 base_id, base_id+1);
1508 if (ctrl->editbox.has_list) {
1509 staticcombo(&pos, escaped, base_id, base_id+1,
1510 ctrl->editbox.percentwidth);
1512 (ctrl->editbox.password ? staticpassedit : staticedit)
1513 (&pos, escaped, base_id, base_id+1,
1514 ctrl->editbox.percentwidth);
1520 num_ids = ctrl->radio.nbuttons + 1; /* label as well */
1522 struct radio *buttons;
1525 escaped = shortcut_escape(ctrl->radio.label,
1526 ctrl->radio.shortcut);
1527 shortcuts[nshortcuts++] = ctrl->radio.shortcut;
1529 buttons = snewn(ctrl->radio.nbuttons, struct radio);
1531 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1533 shortcut_escape(ctrl->radio.buttons[i],
1534 (char)(ctrl->radio.shortcuts ?
1535 ctrl->radio.shortcuts[i] :
1537 buttons[i].id = base_id + 1 + i;
1538 if (ctrl->radio.shortcuts) {
1539 assert(nshortcuts < MAX_SHORTCUTS_PER_CTRL);
1540 shortcuts[nshortcuts++] = ctrl->radio.shortcuts[i];
1544 radioline_common(&pos, escaped, base_id,
1545 ctrl->radio.ncolumns,
1546 buttons, ctrl->radio.nbuttons);
1548 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1549 sfree(buttons[i].text);
1557 escaped = shortcut_escape(ctrl->checkbox.label,
1558 ctrl->checkbox.shortcut);
1559 shortcuts[nshortcuts++] = ctrl->checkbox.shortcut;
1560 checkbox(&pos, escaped, base_id);
1564 escaped = shortcut_escape(ctrl->button.label,
1565 ctrl->button.shortcut);
1566 shortcuts[nshortcuts++] = ctrl->button.shortcut;
1567 if (ctrl->button.iscancel)
1568 actual_base_id = IDCANCEL;
1570 button(&pos, escaped, actual_base_id, ctrl->button.isdefault);
1575 escaped = shortcut_escape(ctrl->listbox.label,
1576 ctrl->listbox.shortcut);
1577 shortcuts[nshortcuts++] = ctrl->listbox.shortcut;
1578 if (ctrl->listbox.draglist) {
1579 data = snew(struct prefslist);
1581 prefslist(data, &pos, ctrl->listbox.height, escaped,
1582 base_id, base_id+1, base_id+2, base_id+3);
1583 shortcuts[nshortcuts++] = 'u'; /* Up */
1584 shortcuts[nshortcuts++] = 'd'; /* Down */
1585 } else if (ctrl->listbox.height == 0) {
1586 /* Drop-down list. */
1587 if (ctrl->listbox.percentwidth == 100) {
1588 staticddlbig(&pos, escaped,
1589 base_id, base_id+1);
1591 staticddl(&pos, escaped, base_id,
1592 base_id+1, ctrl->listbox.percentwidth);
1595 /* Ordinary list. */
1596 listbox(&pos, escaped, base_id, base_id+1,
1597 ctrl->listbox.height, ctrl->listbox.multisel);
1599 if (ctrl->listbox.ncols) {
1601 * This method of getting the box width is a bit of
1602 * a hack; we'd do better to try to retrieve the
1603 * actual width in dialog units from doctl() just
1604 * before MapDialogRect. But that's going to be no
1605 * fun, and this should be good enough accuracy.
1607 int width = cp->width * ctrl->listbox.percentwidth;
1611 tabarray = snewn(ctrl->listbox.ncols-1, int);
1613 for (i = 0; i < ctrl->listbox.ncols-1; i++) {
1614 percent += ctrl->listbox.percentages[i];
1615 tabarray[i] = width * percent / 10000;
1617 SendDlgItemMessage(cp->hwnd, base_id+1, LB_SETTABSTOPS,
1618 ctrl->listbox.ncols-1, (LPARAM)tabarray);
1623 case CTRL_FILESELECT:
1625 escaped = shortcut_escape(ctrl->fileselect.label,
1626 ctrl->fileselect.shortcut);
1627 shortcuts[nshortcuts++] = ctrl->fileselect.shortcut;
1628 editbutton(&pos, escaped, base_id, base_id+1,
1629 "Bro&wse...", base_id+2);
1630 shortcuts[nshortcuts++] = 'w';
1633 case CTRL_FONTSELECT:
1635 escaped = shortcut_escape(ctrl->fontselect.label,
1636 ctrl->fontselect.shortcut);
1637 shortcuts[nshortcuts++] = ctrl->fontselect.shortcut;
1638 statictext(&pos, escaped, 1, base_id);
1639 staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
1640 data = fontspec_new("", 0, 0, 0);
1644 assert(!"Can't happen");
1645 num_ids = 0; /* placate gcc */
1650 * Create a `struct winctrl' for this control, and advance
1651 * the dialog ID counter, if it's actually been created
1652 * (and isn't tabdelayed).
1655 struct winctrl *c = snew(struct winctrl);
1658 c->base_id = actual_base_id;
1659 c->num_ids = num_ids;
1661 memcpy(c->shortcuts, shortcuts, sizeof(shortcuts));
1663 winctrl_add_shortcuts(dp, c);
1664 if (actual_base_id == base_id)
1670 if (colstart >= 0) {
1672 * Update the ypos in all columns crossed by this
1676 for (i = colstart; i < colstart+colspan; i++)
1677 columns[i].ypos = pos.ypos;
1682 * We've now finished laying out the controls; so now update
1683 * the ctlpos and control ID that were passed in, terminate
1684 * any containing box, and return.
1686 for (i = 0; i < ncols; i++)
1687 if (cp->ypos < columns[i].ypos)
1688 cp->ypos = columns[i].ypos;
1691 if (s->boxname && *s->boxname)
1695 static void winctrl_set_focus(union control *ctrl, struct dlgparam *dp,
1700 dp->lastfocused = dp->focused;
1702 } else if (!has_focus && dp->focused == ctrl) {
1703 dp->lastfocused = dp->focused;
1708 union control *dlg_last_focused(union control *ctrl, void *dlg)
1710 struct dlgparam *dp = (struct dlgparam *)dlg;
1711 return dp->focused == ctrl ? dp->lastfocused : dp->focused;
1715 * The dialog-box procedure calls this function to handle Windows
1716 * messages on a control we manage.
1718 int winctrl_handle_command(struct dlgparam *dp, UINT msg,
1719 WPARAM wParam, LPARAM lParam)
1722 union control *ctrl;
1724 static UINT draglistmsg = WM_NULL;
1727 * Filter out pointless window messages. Our interest is in
1728 * WM_COMMAND and the drag list message, and nothing else.
1730 if (draglistmsg == WM_NULL)
1731 draglistmsg = RegisterWindowMessage (DRAGLISTMSGSTRING);
1733 if (msg != draglistmsg && msg != WM_COMMAND && msg != WM_DRAWITEM)
1737 * Look up the control ID in our data.
1740 for (i = 0; i < dp->nctrltrees; i++) {
1741 c = winctrl_findbyid(dp->controltrees[i], LOWORD(wParam));
1746 return 0; /* we have nothing to do */
1748 if (msg == WM_DRAWITEM) {
1750 * Owner-draw request for a panel title.
1752 LPDRAWITEMSTRUCT di = (LPDRAWITEMSTRUCT) lParam;
1754 RECT r = di->rcItem;
1757 SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
1759 GetTextExtentPoint32(hdc, (char *)c->data,
1760 strlen((char *)c->data), &s);
1761 DrawEdge(hdc, &r, EDGE_ETCHED, BF_ADJUST | BF_RECT);
1763 r.left + (r.right-r.left-s.cx)/2,
1764 r.top + (r.bottom-r.top-s.cy)/2,
1765 (char *)c->data, strlen((char *)c->data));
1771 id = LOWORD(wParam) - c->base_id;
1773 if (!ctrl || !ctrl->generic.handler)
1774 return 0; /* nothing we can do here */
1777 * From here on we do not issue `return' statements until the
1778 * very end of the dialog box: any event handler is entitled to
1779 * ask for a colour selector, so we _must_ always allow control
1780 * to reach the end of this switch statement so that the
1781 * subsequent code can test dp->coloursel_wanted().
1784 dp->coloursel_wanted = FALSE;
1787 * Now switch on the control type and the message.
1789 switch (ctrl->generic.type) {
1791 if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
1792 (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
1793 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
1794 if (msg == WM_COMMAND && ctrl->editbox.has_list &&
1795 (HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
1796 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
1798 if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
1799 HIWORD(wParam) == EN_CHANGE)
1800 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1801 if (msg == WM_COMMAND &&
1802 ctrl->editbox.has_list) {
1803 if (HIWORD(wParam) == CBN_SELCHANGE) {
1807 index = SendDlgItemMessage(dp->hwnd, c->base_id+1,
1808 CB_GETCURSEL, 0, 0);
1809 len = SendDlgItemMessage(dp->hwnd, c->base_id+1,
1810 CB_GETLBTEXTLEN, index, 0);
1811 text = snewn(len+1, char);
1812 SendDlgItemMessage(dp->hwnd, c->base_id+1, CB_GETLBTEXT,
1813 index, (LPARAM)text);
1814 SetDlgItemText(dp->hwnd, c->base_id+1, text);
1816 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1817 } else if (HIWORD(wParam) == CBN_EDITCHANGE) {
1818 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1819 } else if (HIWORD(wParam) == CBN_KILLFOCUS) {
1820 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
1826 if (msg == WM_COMMAND &&
1827 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1828 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1830 * We sometimes get spurious BN_CLICKED messages for the
1831 * radio button that is just about to _lose_ selection, if
1832 * we're switching using the arrow keys. Therefore we
1833 * double-check that the button in wParam is actually
1834 * checked before generating an event.
1836 if (msg == WM_COMMAND &&
1837 (HIWORD(wParam) == BN_CLICKED ||
1838 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1839 IsDlgButtonChecked(dp->hwnd, LOWORD(wParam))) {
1840 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1844 if (msg == WM_COMMAND &&
1845 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1846 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1847 if (msg == WM_COMMAND &&
1848 (HIWORD(wParam) == BN_CLICKED ||
1849 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1850 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1854 if (msg == WM_COMMAND &&
1855 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1856 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1857 if (msg == WM_COMMAND &&
1858 (HIWORD(wParam) == BN_CLICKED ||
1859 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1860 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_ACTION);
1864 if (msg == WM_COMMAND && ctrl->listbox.height != 0 &&
1865 (HIWORD(wParam)==LBN_SETFOCUS || HIWORD(wParam)==LBN_KILLFOCUS))
1866 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == LBN_SETFOCUS);
1867 if (msg == WM_COMMAND && ctrl->listbox.height == 0 &&
1868 (HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
1869 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
1870 if (msg == WM_COMMAND && id >= 2 &&
1871 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1872 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1873 if (ctrl->listbox.draglist) {
1875 pret = handle_prefslist(c->data, NULL, 0, (msg != WM_COMMAND),
1876 dp->hwnd, wParam, lParam);
1878 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1881 if (msg == WM_COMMAND && HIWORD(wParam) == LBN_DBLCLK) {
1882 SetCapture(dp->hwnd);
1883 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_ACTION);
1884 } else if (msg == WM_COMMAND && HIWORD(wParam) == LBN_SELCHANGE) {
1885 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_SELCHANGE);
1889 case CTRL_FILESELECT:
1890 if (msg == WM_COMMAND && id == 1 &&
1891 (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
1892 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
1893 if (msg == WM_COMMAND && id == 2 &&
1894 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1895 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1896 if (msg == WM_COMMAND && id == 1 && HIWORD(wParam) == EN_CHANGE)
1897 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1899 (msg == WM_COMMAND &&
1900 (HIWORD(wParam) == BN_CLICKED ||
1901 HIWORD(wParam) == BN_DOUBLECLICKED))) {
1903 char filename[FILENAME_MAX];
1905 memset(&of, 0, sizeof(of));
1906 of.hwndOwner = dp->hwnd;
1907 if (ctrl->fileselect.filter)
1908 of.lpstrFilter = ctrl->fileselect.filter;
1910 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
1911 of.lpstrCustomFilter = NULL;
1912 of.nFilterIndex = 1;
1913 of.lpstrFile = filename;
1914 GetDlgItemText(dp->hwnd, c->base_id+1, filename, lenof(filename));
1915 filename[lenof(filename)-1] = '\0';
1916 of.nMaxFile = lenof(filename);
1917 of.lpstrFileTitle = NULL;
1918 of.lpstrTitle = ctrl->fileselect.title;
1920 if (request_file(NULL, &of, FALSE, ctrl->fileselect.for_writing)) {
1921 SetDlgItemText(dp->hwnd, c->base_id + 1, filename);
1922 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1926 case CTRL_FONTSELECT:
1927 if (msg == WM_COMMAND && id == 2 &&
1928 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1929 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1931 (msg == WM_COMMAND &&
1932 (HIWORD(wParam) == BN_CLICKED ||
1933 HIWORD(wParam) == BN_DOUBLECLICKED))) {
1937 FontSpec *fs = (FontSpec *)c->data;
1940 lf.lfHeight = -MulDiv(fs->height,
1941 GetDeviceCaps(hdc, LOGPIXELSY), 72);
1943 lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
1944 lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
1945 lf.lfWeight = (fs->isbold ? FW_BOLD : 0);
1946 lf.lfCharSet = fs->charset;
1947 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
1948 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1949 lf.lfQuality = DEFAULT_QUALITY;
1950 lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
1951 strncpy(lf.lfFaceName, fs->name,
1952 sizeof(lf.lfFaceName) - 1);
1953 lf.lfFaceName[sizeof(lf.lfFaceName) - 1] = '\0';
1955 cf.lStructSize = sizeof(cf);
1956 cf.hwndOwner = dp->hwnd;
1958 cf.Flags = (dp->fixed_pitch_fonts ? CF_FIXEDPITCHONLY : 0) |
1959 CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
1961 if (ChooseFont(&cf)) {
1962 fs = fontspec_new(lf.lfFaceName, (lf.lfWeight == FW_BOLD),
1963 cf.iPointSize / 10, lf.lfCharSet);
1964 dlg_fontsel_set(ctrl, dp, fs);
1967 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1974 * If the above event handler has asked for a colour selector,
1975 * now is the time to generate one.
1977 if (dp->coloursel_wanted) {
1978 static CHOOSECOLOR cc;
1979 static DWORD custom[16] = { 0 }; /* zero initialisers */
1980 cc.lStructSize = sizeof(cc);
1981 cc.hwndOwner = dp->hwnd;
1982 cc.hInstance = (HWND) hinst;
1983 cc.lpCustColors = custom;
1984 cc.rgbResult = RGB(dp->coloursel_result.r,
1985 dp->coloursel_result.g,
1986 dp->coloursel_result.b);
1987 cc.Flags = CC_FULLOPEN | CC_RGBINIT;
1988 if (ChooseColor(&cc)) {
1989 dp->coloursel_result.r =
1990 (unsigned char) (cc.rgbResult & 0xFF);
1991 dp->coloursel_result.g =
1992 (unsigned char) (cc.rgbResult >> 8) & 0xFF;
1993 dp->coloursel_result.b =
1994 (unsigned char) (cc.rgbResult >> 16) & 0xFF;
1995 dp->coloursel_result.ok = TRUE;
1997 dp->coloursel_result.ok = FALSE;
1998 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_CALLBACK);
2005 * This function can be called to produce context help on a
2006 * control. Returns TRUE if it has actually launched some help.
2008 int winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id)
2014 * Look up the control ID in our data.
2017 for (i = 0; i < dp->nctrltrees; i++) {
2018 c = winctrl_findbyid(dp->controltrees[i], id);
2023 return 0; /* we have nothing to do */
2026 * This is the Windows front end, so we're allowed to assume
2027 * `helpctx.p' is a context string.
2029 if (!c->ctrl || !c->ctrl->generic.helpctx.p)
2030 return 0; /* no help available for this ctrl */
2032 launch_help(hwnd, c->ctrl->generic.helpctx.p);
2037 * Now the various functions that the platform-independent
2038 * mechanism can call to access the dialog box entries.
2041 static struct winctrl *dlg_findbyctrl(struct dlgparam *dp, union control *ctrl)
2045 for (i = 0; i < dp->nctrltrees; i++) {
2046 struct winctrl *c = winctrl_findbyctrl(dp->controltrees[i], ctrl);
2053 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
2055 struct dlgparam *dp = (struct dlgparam *)dlg;
2056 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2057 assert(c && c->ctrl->generic.type == CTRL_RADIO);
2058 CheckRadioButton(dp->hwnd,
2060 c->base_id + c->ctrl->radio.nbuttons,
2061 c->base_id + 1 + whichbutton);
2064 int dlg_radiobutton_get(union control *ctrl, void *dlg)
2066 struct dlgparam *dp = (struct dlgparam *)dlg;
2067 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2069 assert(c && c->ctrl->generic.type == CTRL_RADIO);
2070 for (i = 0; i < c->ctrl->radio.nbuttons; i++)
2071 if (IsDlgButtonChecked(dp->hwnd, c->base_id + 1 + i))
2073 assert(!"No radio button was checked?!");
2077 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
2079 struct dlgparam *dp = (struct dlgparam *)dlg;
2080 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2081 assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
2082 CheckDlgButton(dp->hwnd, c->base_id, (checked != 0));
2085 int dlg_checkbox_get(union control *ctrl, void *dlg)
2087 struct dlgparam *dp = (struct dlgparam *)dlg;
2088 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2089 assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
2090 return 0 != IsDlgButtonChecked(dp->hwnd, c->base_id);
2093 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
2095 struct dlgparam *dp = (struct dlgparam *)dlg;
2096 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2097 assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
2098 SetDlgItemText(dp->hwnd, c->base_id+1, text);
2101 char *dlg_editbox_get(union control *ctrl, void *dlg)
2103 struct dlgparam *dp = (struct dlgparam *)dlg;
2104 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2105 assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
2106 return GetDlgItemText_alloc(dp->hwnd, c->base_id+1);
2109 /* The `listbox' functions can also apply to combo boxes. */
2110 void dlg_listbox_clear(union control *ctrl, void *dlg)
2112 struct dlgparam *dp = (struct dlgparam *)dlg;
2113 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2116 (c->ctrl->generic.type == CTRL_LISTBOX ||
2117 (c->ctrl->generic.type == CTRL_EDITBOX &&
2118 c->ctrl->editbox.has_list)));
2119 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2120 LB_RESETCONTENT : CB_RESETCONTENT);
2121 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
2124 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
2126 struct dlgparam *dp = (struct dlgparam *)dlg;
2127 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2130 (c->ctrl->generic.type == CTRL_LISTBOX ||
2131 (c->ctrl->generic.type == CTRL_EDITBOX &&
2132 c->ctrl->editbox.has_list)));
2133 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2134 LB_DELETESTRING : CB_DELETESTRING);
2135 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2138 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
2140 struct dlgparam *dp = (struct dlgparam *)dlg;
2141 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2144 (c->ctrl->generic.type == CTRL_LISTBOX ||
2145 (c->ctrl->generic.type == CTRL_EDITBOX &&
2146 c->ctrl->editbox.has_list)));
2147 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2148 LB_ADDSTRING : CB_ADDSTRING);
2149 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
2153 * Each listbox entry may have a numeric id associated with it.
2154 * Note that some front ends only permit a string to be stored at
2155 * each position, which means that _if_ you put two identical
2156 * strings in any listbox then you MUST not assign them different
2157 * IDs and expect to get meaningful results back.
2159 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
2160 char const *text, int id)
2162 struct dlgparam *dp = (struct dlgparam *)dlg;
2163 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2164 int msg, msg2, index;
2166 (c->ctrl->generic.type == CTRL_LISTBOX ||
2167 (c->ctrl->generic.type == CTRL_EDITBOX &&
2168 c->ctrl->editbox.has_list)));
2169 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2170 LB_ADDSTRING : CB_ADDSTRING);
2171 msg2 = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2172 LB_SETITEMDATA : CB_SETITEMDATA);
2173 index = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
2174 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg2, index, (LPARAM)id);
2177 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
2179 struct dlgparam *dp = (struct dlgparam *)dlg;
2180 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2182 assert(c && c->ctrl->generic.type == CTRL_LISTBOX);
2183 msg = (c->ctrl->listbox.height != 0 ? LB_GETITEMDATA : CB_GETITEMDATA);
2185 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2188 /* dlg_listbox_index returns <0 if no single element is selected. */
2189 int dlg_listbox_index(union control *ctrl, void *dlg)
2191 struct dlgparam *dp = (struct dlgparam *)dlg;
2192 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2194 assert(c && c->ctrl->generic.type == CTRL_LISTBOX);
2195 if (c->ctrl->listbox.multisel) {
2196 assert(c->ctrl->listbox.height != 0); /* not combo box */
2197 ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSELCOUNT, 0, 0);
2198 if (ret == LB_ERR || ret > 1)
2201 msg = (c->ctrl->listbox.height != 0 ? LB_GETCURSEL : CB_GETCURSEL);
2202 ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
2209 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
2211 struct dlgparam *dp = (struct dlgparam *)dlg;
2212 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2213 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2214 c->ctrl->listbox.multisel &&
2215 c->ctrl->listbox.height != 0);
2217 SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSEL, index, 0);
2220 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
2222 struct dlgparam *dp = (struct dlgparam *)dlg;
2223 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2225 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2226 !c->ctrl->listbox.multisel);
2227 msg = (c->ctrl->listbox.height != 0 ? LB_SETCURSEL : CB_SETCURSEL);
2228 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2231 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
2233 struct dlgparam *dp = (struct dlgparam *)dlg;
2234 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2235 assert(c && c->ctrl->generic.type == CTRL_TEXT);
2236 SetDlgItemText(dp->hwnd, c->base_id, text);
2239 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
2241 struct dlgparam *dp = (struct dlgparam *)dlg;
2242 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2243 char *escaped = NULL;
2247 switch (c->ctrl->generic.type) {
2249 escaped = shortcut_escape(text, c->ctrl->editbox.shortcut);
2253 escaped = shortcut_escape(text, c->ctrl->radio.shortcut);
2257 escaped = shortcut_escape(text, ctrl->checkbox.shortcut);
2261 escaped = shortcut_escape(text, ctrl->button.shortcut);
2265 escaped = shortcut_escape(text, ctrl->listbox.shortcut);
2268 case CTRL_FILESELECT:
2269 escaped = shortcut_escape(text, ctrl->fileselect.shortcut);
2272 case CTRL_FONTSELECT:
2273 escaped = shortcut_escape(text, ctrl->fontselect.shortcut);
2277 assert(!"Can't happen");
2281 SetDlgItemText(dp->hwnd, id, escaped);
2286 void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn)
2288 struct dlgparam *dp = (struct dlgparam *)dlg;
2289 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2290 assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
2291 SetDlgItemText(dp->hwnd, c->base_id+1, fn->path);
2294 Filename *dlg_filesel_get(union control *ctrl, void *dlg)
2296 struct dlgparam *dp = (struct dlgparam *)dlg;
2297 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2300 assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
2301 tmp = GetDlgItemText_alloc(dp->hwnd, c->base_id+1);
2302 ret = filename_from_str(tmp);
2307 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
2309 char *buf, *boldstr;
2310 struct dlgparam *dp = (struct dlgparam *)dlg;
2311 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2312 assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
2314 fontspec_free((FontSpec *)c->data);
2315 c->data = fontspec_copy(fs);
2317 boldstr = (fs->isbold ? "bold, " : "");
2318 if (fs->height == 0)
2319 buf = dupprintf("Font: %s, %sdefault height", fs->name, boldstr);
2321 buf = dupprintf("Font: %s, %s%d-%s", fs->name, boldstr,
2322 (fs->height < 0 ? -fs->height : fs->height),
2323 (fs->height < 0 ? "pixel" : "point"));
2324 SetDlgItemText(dp->hwnd, c->base_id+1, buf);
2327 dlg_auto_set_fixed_pitch_flag(dp);
2330 FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
2332 struct dlgparam *dp = (struct dlgparam *)dlg;
2333 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2334 assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
2335 return fontspec_copy((FontSpec *)c->data);
2339 * Bracketing a large set of updates in these two functions will
2340 * cause the front end (if possible) to delay updating the screen
2341 * until it's all complete, thus avoiding flicker.
2343 void dlg_update_start(union control *ctrl, void *dlg)
2345 struct dlgparam *dp = (struct dlgparam *)dlg;
2346 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2347 if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
2348 SendDlgItemMessage(dp->hwnd, c->base_id+1, WM_SETREDRAW, FALSE, 0);
2352 void dlg_update_done(union control *ctrl, void *dlg)
2354 struct dlgparam *dp = (struct dlgparam *)dlg;
2355 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2356 if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
2357 HWND hw = GetDlgItem(dp->hwnd, c->base_id+1);
2358 SendMessage(hw, WM_SETREDRAW, TRUE, 0);
2359 InvalidateRect(hw, NULL, TRUE);
2363 void dlg_set_focus(union control *ctrl, void *dlg)
2365 struct dlgparam *dp = (struct dlgparam *)dlg;
2366 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2371 switch (ctrl->generic.type) {
2372 case CTRL_EDITBOX: id = c->base_id + 1; break;
2374 for (id = c->base_id + ctrl->radio.nbuttons; id > 1; id--)
2375 if (IsDlgButtonChecked(dp->hwnd, id))
2378 * In the theoretically-unlikely case that no button was
2379 * selected, id should come out of this as 1, which is a
2380 * reasonable enough choice.
2383 case CTRL_CHECKBOX: id = c->base_id; break;
2384 case CTRL_BUTTON: id = c->base_id; break;
2385 case CTRL_LISTBOX: id = c->base_id + 1; break;
2386 case CTRL_FILESELECT: id = c->base_id + 1; break;
2387 case CTRL_FONTSELECT: id = c->base_id + 2; break;
2388 default: id = c->base_id; break;
2390 ctl = GetDlgItem(dp->hwnd, id);
2395 * During event processing, you might well want to give an error
2396 * indication to the user. dlg_beep() is a quick and easy generic
2397 * error; dlg_error() puts up a message-box or equivalent.
2399 void dlg_beep(void *dlg)
2401 /* struct dlgparam *dp = (struct dlgparam *)dlg; */
2405 void dlg_error_msg(void *dlg, const char *msg)
2407 struct dlgparam *dp = (struct dlgparam *)dlg;
2408 MessageBox(dp->hwnd, msg,
2409 dp->errtitle ? dp->errtitle : NULL,
2410 MB_OK | MB_ICONERROR);
2414 * This function signals to the front end that the dialog's
2415 * processing is completed, and passes an integer value (typically
2416 * a success status).
2418 void dlg_end(void *dlg, int value)
2420 struct dlgparam *dp = (struct dlgparam *)dlg;
2422 dp->endresult = value;
2425 void dlg_refresh(union control *ctrl, void *dlg)
2427 struct dlgparam *dp = (struct dlgparam *)dlg;
2433 * Send EVENT_REFRESH to absolutely everything.
2435 for (j = 0; j < dp->nctrltrees; j++) {
2437 (c = winctrl_findbyindex(dp->controltrees[j], i)) != NULL;
2439 if (c->ctrl && c->ctrl->generic.handler != NULL)
2440 c->ctrl->generic.handler(c->ctrl, dp,
2441 dp->data, EVENT_REFRESH);
2446 * Send EVENT_REFRESH to a specific control.
2448 if (ctrl->generic.handler != NULL)
2449 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
2453 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
2455 struct dlgparam *dp = (struct dlgparam *)dlg;
2456 dp->coloursel_wanted = TRUE;
2457 dp->coloursel_result.r = r;
2458 dp->coloursel_result.g = g;
2459 dp->coloursel_result.b = b;
2462 int dlg_coloursel_results(union control *ctrl, void *dlg,
2463 int *r, int *g, int *b)
2465 struct dlgparam *dp = (struct dlgparam *)dlg;
2466 if (dp->coloursel_result.ok) {
2467 *r = dp->coloursel_result.r;
2468 *g = dp->coloursel_result.g;
2469 *b = dp->coloursel_result.b;
2475 void dlg_auto_set_fixed_pitch_flag(void *dlg)
2477 struct dlgparam *dp = (struct dlgparam *)dlg;
2478 Conf *conf = (Conf *)dp->data;
2487 * Attempt to load the current font, and see if it's
2488 * variable-pitch. If so, start off the fixed-pitch flag for the
2489 * dialog box as false.
2491 * We assume here that any client of the dlg_* mechanism which is
2492 * using font selectors at all is also using a normal 'Conf *'
2496 quality = conf_get_int(conf, CONF_font_quality);
2497 fs = conf_get_fontspec(conf, CONF_font);
2499 hfont = CreateFont(0, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE,
2500 DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
2501 CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality),
2502 FIXED_PITCH | FF_DONTCARE, fs->name);
2504 if (hdc && SelectObject(hdc, hfont) && GetTextMetrics(hdc, &tm)) {
2505 /* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
2506 is_var = (tm.tmPitchAndFamily & TMPF_FIXED_PITCH);
2508 is_var = FALSE; /* assume it's basically normal */
2511 ReleaseDC(NULL, hdc);
2513 DeleteObject(hfont);
2516 dp->fixed_pitch_fonts = FALSE;
2519 int dlg_get_fixed_pitch_flag(void *dlg)
2521 struct dlgparam *dp = (struct dlgparam *)dlg;
2522 return dp->fixed_pitch_fonts;
2525 void dlg_set_fixed_pitch_flag(void *dlg, int flag)
2527 struct dlgparam *dp = (struct dlgparam *)dlg;
2528 dp->fixed_pitch_fonts = flag;
2531 void dp_init(struct dlgparam *dp)
2536 dp->focused = dp->lastfocused = NULL;
2537 memset(dp->shortcuts, 0, sizeof(dp->shortcuts));
2539 dp->wintitle = dp->errtitle = NULL;
2540 dp->fixed_pitch_fonts = TRUE;
2543 void dp_add_tree(struct dlgparam *dp, struct winctrls *wc)
2545 assert(dp->nctrltrees < lenof(dp->controltrees));
2546 dp->controltrees[dp->nctrltrees++] = wc;
2549 void dp_cleanup(struct dlgparam *dp)
2551 sfree(dp->wintitle);
2552 sfree(dp->errtitle);