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 * Some edit boxes. Each one has a static above it. The percentages
152 * of the horizontal space are provided.
154 void multiedit(struct ctlpos *cp, int password, ...)
161 va_start(ap, password);
164 int staticid, editid, pcwidth;
165 text = va_arg(ap, char *);
168 staticid = va_arg(ap, int);
169 editid = va_arg(ap, int);
170 pcwidth = va_arg(ap, int);
172 r.left = xpos + GAPBETWEEN;
174 xpos = (cp->width + GAPBETWEEN) * percent / 100;
175 r.right = xpos - r.left;
178 r.bottom = STATICHEIGHT;
179 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
180 r.top = cp->ypos + 8 + GAPWITHIN;
181 r.bottom = EDITHEIGHT;
183 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL |
184 (password ? ES_PASSWORD : 0),
185 WS_EX_CLIENTEDGE, "", editid);
188 cp->ypos += STATICHEIGHT + GAPWITHIN + EDITHEIGHT + GAPBETWEEN;
192 * A static line, followed by a full-width combo box.
194 void combobox(struct ctlpos *cp, char *text, int staticid, int listid)
202 r.bottom = STATICHEIGHT;
203 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
204 r.top = cp->ypos + 8 + GAPWITHIN;
205 r.bottom = COMBOHEIGHT * 10;
206 doctl(cp, r, "COMBOBOX",
207 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
208 CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
210 cp->ypos += STATICHEIGHT + GAPWITHIN + COMBOHEIGHT + GAPBETWEEN;
213 struct radio { char *text; int id; };
215 static void radioline_common(struct ctlpos *cp, char *text, int id,
216 int nacross, struct radio *buttons, int nbuttons)
227 r.bottom = STATICHEIGHT;
228 cp->ypos += r.bottom + GAPWITHIN;
229 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
234 for (j = 0; j < nbuttons; j++) {
235 char *btext = buttons[j].text;
236 int bid = buttons[j].id;
239 cp->ypos += r.bottom + (nacross > 1 ? GAPBETWEEN : GAPWITHIN);
242 r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
245 (i + 1) * (cp->width + GAPBETWEEN) / nacross - r.left;
247 r.right = cp->width - r.left;
249 r.bottom = RADIOHEIGHT;
250 doctl(cp, r, "BUTTON",
251 BS_NOTIFY | BS_AUTORADIOBUTTON | WS_CHILD |
252 WS_VISIBLE | WS_TABSTOP | group, 0, btext, bid);
256 cp->ypos += r.bottom + GAPBETWEEN;
260 * A set of radio buttons on the same line, with a static above
261 * them. `nacross' dictates how many parts the line is divided into
262 * (you might want this not to equal the number of buttons if you
263 * needed to line up some 2s and some 3s to look good in the same
266 * There's a bit of a hack in here to ensure that if nacross
267 * exceeds the actual number of buttons, the rightmost button
268 * really does get all the space right to the edge of the line, so
269 * you can do things like
271 * (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
273 void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...)
276 struct radio *buttons;
279 va_start(ap, nacross);
282 char *btext = va_arg(ap, char *);
286 bid = va_arg(ap, int);
290 buttons = snewn(nbuttons, struct radio);
291 va_start(ap, nacross);
292 for (i = 0; i < nbuttons; i++) {
293 buttons[i].text = va_arg(ap, char *);
294 buttons[i].id = va_arg(ap, int);
297 radioline_common(cp, text, id, nacross, buttons, nbuttons);
302 * A set of radio buttons on the same line, without a static above
303 * them. Otherwise just like radioline.
305 void bareradioline(struct ctlpos *cp, int nacross, ...)
308 struct radio *buttons;
311 va_start(ap, nacross);
314 char *btext = va_arg(ap, char *);
318 bid = va_arg(ap, int);
321 buttons = snewn(nbuttons, struct radio);
322 va_start(ap, nacross);
323 for (i = 0; i < nbuttons; i++) {
324 buttons[i].text = va_arg(ap, char *);
325 buttons[i].id = va_arg(ap, int);
328 radioline_common(cp, NULL, 0, nacross, buttons, nbuttons);
333 * A set of radio buttons on multiple lines, with a static above
336 void radiobig(struct ctlpos *cp, char *text, int id, ...)
339 struct radio *buttons;
345 char *btext = va_arg(ap, char *);
349 bid = va_arg(ap, int);
352 buttons = snewn(nbuttons, struct radio);
354 for (i = 0; i < nbuttons; i++) {
355 buttons[i].text = va_arg(ap, char *);
356 buttons[i].id = va_arg(ap, int);
359 radioline_common(cp, text, id, 1, buttons, nbuttons);
364 * A single standalone checkbox.
366 void checkbox(struct ctlpos *cp, char *text, int id)
373 r.bottom = CHECKBOXHEIGHT;
374 cp->ypos += r.bottom + GAPBETWEEN;
375 doctl(cp, r, "BUTTON",
376 BS_NOTIFY | BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
381 * Wrap a piece of text for a static text control. Returns the
382 * wrapped text (a malloc'ed string containing \ns), and also
383 * returns the number of lines required.
385 char *staticwrap(struct ctlpos *cp, HWND hwnd, char *text, int *lines)
387 HDC hdc = GetDC(hwnd);
388 int lpx = GetDeviceCaps(hdc, LOGPIXELSX);
389 int width, nlines, j;
394 HFONT oldfont, newfont;
396 ret = snewn(1+strlen(text), char);
399 pwidths = snewn(1+strlen(text), INT);
402 * Work out the width the text will need to fit in, by doing
403 * the same adjustment that the `statictext' function itself
406 SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
407 r.left = r.top = r.bottom = 0;
409 MapDialogRect(hwnd, &r);
415 * We must select the correct font into the HDC before calling
416 * GetTextExtent*, or silly things will happen.
418 newfont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
419 oldfont = SelectObject(hdc, newfont);
422 if (!GetTextExtentExPoint(hdc, p, strlen(p), width,
423 &nfit, pwidths, &size) ||
424 (size_t)nfit >= strlen(p)) {
426 * Either GetTextExtentExPoint returned failure, or the
427 * whole of the rest of the text fits on this line.
428 * Either way, we stop wrapping, copy the remainder of
429 * the input string unchanged to the output, and leave.
436 * Now we search backwards along the string from `nfit',
437 * looking for a space at which to break the line. If we
438 * don't find one at all, that's fine - we'll just break
439 * the line at `nfit'.
441 for (j = nfit; j > 0; j--) {
442 if (isspace((unsigned char)p[j])) {
453 while (*p && isspace((unsigned char)*p))
459 SelectObject(hdc, oldfont);
460 ReleaseDC(cp->hwnd, hdc);
462 if (lines) *lines = nlines;
468 * A single standalone static text control.
470 void statictext(struct ctlpos *cp, char *text, int lines, int id)
477 r.bottom = STATICHEIGHT * lines;
478 cp->ypos += r.bottom + GAPBETWEEN;
479 doctl(cp, r, "STATIC",
480 WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP,
485 * An owner-drawn static text control for a panel title.
487 void paneltitle(struct ctlpos *cp, int id)
494 r.bottom = TITLEHEIGHT;
495 cp->ypos += r.bottom + GAPBETWEEN;
496 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
501 * A button on the right hand side, with a static to its left.
503 void staticbtn(struct ctlpos *cp, char *stext, int sid,
504 char *btext, int bid)
506 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
507 PUSHBTNHEIGHT : STATICHEIGHT);
509 int lwid, rwid, rpos;
511 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
512 lwid = rpos - 2 * GAPBETWEEN;
513 rwid = cp->width + GAPBETWEEN - rpos;
516 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
518 r.bottom = STATICHEIGHT;
519 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
522 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
524 r.bottom = PUSHBTNHEIGHT;
525 doctl(cp, r, "BUTTON",
526 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
529 cp->ypos += height + GAPBETWEEN;
533 * A simple push button.
535 void button(struct ctlpos *cp, char *btext, int bid, int defbtn)
542 r.bottom = PUSHBTNHEIGHT;
544 /* Q67655: the _dialog box_ must know which button is default
545 * as well as the button itself knowing */
546 if (defbtn && cp->hwnd)
547 SendMessage(cp->hwnd, DM_SETDEFID, bid, 0);
549 doctl(cp, r, "BUTTON",
550 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
551 (defbtn ? BS_DEFPUSHBUTTON : 0) | BS_PUSHBUTTON,
554 cp->ypos += PUSHBTNHEIGHT + GAPBETWEEN;
558 * Like staticbtn, but two buttons.
560 void static2btn(struct ctlpos *cp, char *stext, int sid,
561 char *btext1, int bid1, char *btext2, int bid2)
563 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
564 PUSHBTNHEIGHT : STATICHEIGHT);
566 int lwid, rwid1, rwid2, rpos1, rpos2;
568 rpos1 = GAPBETWEEN + (cp->width + GAPBETWEEN) / 2;
569 rpos2 = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
570 lwid = rpos1 - 2 * GAPBETWEEN;
571 rwid1 = rpos2 - rpos1 - GAPBETWEEN;
572 rwid2 = cp->width + GAPBETWEEN - rpos2;
575 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
577 r.bottom = STATICHEIGHT;
578 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
581 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
583 r.bottom = PUSHBTNHEIGHT;
584 doctl(cp, r, "BUTTON",
585 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
589 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
591 r.bottom = PUSHBTNHEIGHT;
592 doctl(cp, r, "BUTTON",
593 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
596 cp->ypos += height + GAPBETWEEN;
600 * An edit control on the right hand side, with a static to its left.
602 static void staticedit_internal(struct ctlpos *cp, char *stext,
603 int sid, int eid, int percentedit,
606 const int height = (EDITHEIGHT > STATICHEIGHT ?
607 EDITHEIGHT : STATICHEIGHT);
609 int lwid, rwid, rpos;
612 GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
613 lwid = rpos - 2 * GAPBETWEEN;
614 rwid = cp->width + GAPBETWEEN - rpos;
617 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
619 r.bottom = STATICHEIGHT;
620 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
623 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
625 r.bottom = EDITHEIGHT;
627 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
628 WS_EX_CLIENTEDGE, "", eid);
630 cp->ypos += height + GAPBETWEEN;
633 void staticedit(struct ctlpos *cp, char *stext,
634 int sid, int eid, int percentedit)
636 staticedit_internal(cp, stext, sid, eid, percentedit, 0);
639 void staticpassedit(struct ctlpos *cp, char *stext,
640 int sid, int eid, int percentedit)
642 staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
646 * A drop-down list box on the right hand side, with a static to
649 void staticddl(struct ctlpos *cp, char *stext,
650 int sid, int lid, int percentlist)
652 const int height = (COMBOHEIGHT > STATICHEIGHT ?
653 COMBOHEIGHT : STATICHEIGHT);
655 int lwid, rwid, rpos;
658 GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
659 lwid = rpos - 2 * GAPBETWEEN;
660 rwid = cp->width + GAPBETWEEN - rpos;
663 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
665 r.bottom = STATICHEIGHT;
666 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
669 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
671 r.bottom = COMBOHEIGHT*4;
672 doctl(cp, r, "COMBOBOX",
673 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
674 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
676 cp->ypos += height + GAPBETWEEN;
680 * A combo box on the right hand side, with a static to its left.
682 void staticcombo(struct ctlpos *cp, char *stext,
683 int sid, int lid, int percentlist)
685 const int height = (COMBOHEIGHT > STATICHEIGHT ?
686 COMBOHEIGHT : STATICHEIGHT);
688 int lwid, rwid, rpos;
691 GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
692 lwid = rpos - 2 * GAPBETWEEN;
693 rwid = cp->width + GAPBETWEEN - rpos;
696 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
698 r.bottom = STATICHEIGHT;
699 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
702 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
704 r.bottom = COMBOHEIGHT*10;
705 doctl(cp, r, "COMBOBOX",
706 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
707 CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
709 cp->ypos += height + GAPBETWEEN;
713 * A static, with a full-width drop-down list box below it.
715 void staticddlbig(struct ctlpos *cp, char *stext,
723 r.bottom = STATICHEIGHT;
724 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
725 cp->ypos += STATICHEIGHT;
730 r.bottom = COMBOHEIGHT*4;
731 doctl(cp, r, "COMBOBOX",
732 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
733 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
734 cp->ypos += COMBOHEIGHT + GAPBETWEEN;
738 * A big multiline edit control with a static labelling it.
740 void bigeditctrl(struct ctlpos *cp, char *stext,
741 int sid, int eid, int lines)
748 r.bottom = STATICHEIGHT;
749 cp->ypos += r.bottom + GAPWITHIN;
750 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
755 r.bottom = EDITHEIGHT + (lines - 1) * STATICHEIGHT;
756 cp->ypos += r.bottom + GAPBETWEEN;
758 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
759 WS_EX_CLIENTEDGE, "", eid);
763 * A list box with a static labelling it.
765 void listbox(struct ctlpos *cp, char *stext,
766 int sid, int lid, int lines, int multi)
774 r.bottom = STATICHEIGHT;
775 cp->ypos += r.bottom + GAPWITHIN;
776 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
782 r.bottom = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
783 cp->ypos += r.bottom + GAPBETWEEN;
784 doctl(cp, r, "LISTBOX",
785 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
786 LBS_NOTIFY | LBS_HASSTRINGS | LBS_USETABSTOPS |
787 (multi ? LBS_MULTIPLESEL : 0),
788 WS_EX_CLIENTEDGE, "", lid);
792 * A tab-control substitute when a real tab control is unavailable.
794 void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id)
796 const int height = (COMBOHEIGHT > STATICHEIGHT ?
797 COMBOHEIGHT : STATICHEIGHT);
799 int bigwid, lwid, rwid, rpos;
800 static const int BIGGAP = 15;
801 static const int MEDGAP = 3;
803 bigwid = cp->width + 2 * GAPBETWEEN - 2 * BIGGAP;
805 rpos = BIGGAP + (bigwid + BIGGAP) / 2;
806 lwid = rpos - 2 * BIGGAP;
807 rwid = bigwid + BIGGAP - rpos;
810 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
812 r.bottom = STATICHEIGHT;
813 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
816 r.top = cp->ypos + (height - COMBOHEIGHT) / 2;
818 r.bottom = COMBOHEIGHT * 10;
819 doctl(cp, r, "COMBOBOX",
820 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
821 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
823 cp->ypos += height + MEDGAP + GAPBETWEEN;
829 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
834 * A static line, followed by an edit control on the left hand side
835 * and a button on the right.
837 void editbutton(struct ctlpos *cp, char *stext, int sid,
838 int eid, char *btext, int bid)
840 const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
841 EDITHEIGHT : PUSHBTNHEIGHT);
843 int lwid, rwid, rpos;
848 r.bottom = STATICHEIGHT;
849 cp->ypos += r.bottom + GAPWITHIN;
850 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
852 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
853 lwid = rpos - 2 * GAPBETWEEN;
854 rwid = cp->width + GAPBETWEEN - rpos;
857 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
859 r.bottom = EDITHEIGHT;
861 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
862 WS_EX_CLIENTEDGE, "", eid);
865 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
867 r.bottom = PUSHBTNHEIGHT;
868 doctl(cp, r, "BUTTON",
869 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
872 cp->ypos += height + GAPBETWEEN;
876 * A special control for manipulating an ordered preference list
877 * (eg. for cipher selection).
878 * XXX: this is a rough hack and could be improved.
880 void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
881 char *stext, int sid, int listid, int upbid, int dnbid)
883 const static int percents[] = { 5, 75, 20 };
885 int xpos, percent = 0, i;
886 int listheight = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
887 const int BTNSHEIGHT = 2*PUSHBTNHEIGHT + GAPBETWEEN;
888 int totalheight, buttonpos;
890 /* Squirrel away IDs. */
891 hdl->listid = listid;
895 /* The static label. */
900 r.bottom = STATICHEIGHT;
901 cp->ypos += r.bottom + GAPWITHIN;
902 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
905 if (listheight > BTNSHEIGHT) {
906 totalheight = listheight;
907 buttonpos = (listheight - BTNSHEIGHT) / 2;
909 totalheight = BTNSHEIGHT;
913 for (i=0; i<3; i++) {
915 xpos = (cp->width + GAPBETWEEN) * percent / 100;
916 left = xpos + GAPBETWEEN;
917 percent += percents[i];
918 xpos = (cp->width + GAPBETWEEN) * percent / 100;
923 /* The drag list box. */
924 r.left = left; r.right = wid;
925 r.top = cp->ypos; r.bottom = listheight;
928 ctl = doctl(cp, r, "LISTBOX",
929 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
930 WS_VSCROLL | LBS_HASSTRINGS | LBS_USETABSTOPS,
938 /* The "Up" and "Down" buttons. */
939 /* XXX worry about accelerators if we have more than one
940 * prefslist on a panel */
941 r.left = left; r.right = wid;
942 r.top = cp->ypos + buttonpos; r.bottom = PUSHBTNHEIGHT;
943 doctl(cp, r, "BUTTON",
944 BS_NOTIFY | WS_CHILD | WS_VISIBLE |
945 WS_TABSTOP | BS_PUSHBUTTON,
948 r.left = left; r.right = wid;
949 r.top = cp->ypos + buttonpos + PUSHBTNHEIGHT + GAPBETWEEN;
950 r.bottom = PUSHBTNHEIGHT;
951 doctl(cp, r, "BUTTON",
952 BS_NOTIFY | WS_CHILD | WS_VISIBLE |
953 WS_TABSTOP | BS_PUSHBUTTON,
961 cp->ypos += totalheight + GAPBETWEEN;
966 * Helper function for prefslist: move item in list box.
968 static void pl_moveitem(HWND hwnd, int listid, int src, int dst)
972 /* Get the item's data. */
973 tlen = SendDlgItemMessage (hwnd, listid, LB_GETTEXTLEN, src, 0);
974 txt = snewn(tlen+1, char);
975 SendDlgItemMessage (hwnd, listid, LB_GETTEXT, src, (LPARAM) txt);
976 val = SendDlgItemMessage (hwnd, listid, LB_GETITEMDATA, src, 0);
977 /* Deselect old location. */
978 SendDlgItemMessage (hwnd, listid, LB_SETSEL, FALSE, src);
979 /* Delete it at the old location. */
980 SendDlgItemMessage (hwnd, listid, LB_DELETESTRING, src, 0);
981 /* Insert it at new location. */
982 SendDlgItemMessage (hwnd, listid, LB_INSERTSTRING, dst,
984 SendDlgItemMessage (hwnd, listid, LB_SETITEMDATA, dst,
987 SendDlgItemMessage (hwnd, listid, LB_SETCURSEL, dst, 0);
991 int pl_itemfrompt(HWND hwnd, POINT cursor, BOOL scroll)
994 POINT uppoint, downpoint;
995 int updist, downdist, upitem, downitem, i;
998 * Ghastly hackery to try to figure out not which
999 * _item_, but which _gap between items_, the user
1000 * is pointing at. We do this by first working out
1001 * which list item is under the cursor, and then
1002 * working out how far the cursor would have to
1003 * move up or down before the answer was different.
1004 * Then we put the insertion point _above_ the
1005 * current item if the upper edge is closer than
1006 * the lower edge, or _below_ it if vice versa.
1008 ret = LBItemFromPt(hwnd, cursor, scroll);
1011 ret = LBItemFromPt(hwnd, cursor, FALSE);
1012 updist = downdist = 0;
1013 for (i = 1; i < 4096 && (!updist || !downdist); i++) {
1014 uppoint = downpoint = cursor;
1017 upitem = LBItemFromPt(hwnd, uppoint, FALSE);
1018 downitem = LBItemFromPt(hwnd, downpoint, FALSE);
1019 if (!updist && upitem != ret)
1021 if (!downdist && downitem != ret)
1024 if (downdist < updist)
1030 * Handler for prefslist above.
1032 * Return value has bit 0 set if the dialog box procedure needs to
1033 * return TRUE from handling this message; it has bit 1 set if a
1034 * change may have been made in the contents of the list.
1036 int handle_prefslist(struct prefslist *hdl,
1037 int *array, int maxmemb,
1038 int is_dlmsg, HWND hwnd,
1039 WPARAM wParam, LPARAM lParam)
1046 if ((int)wParam == hdl->listid) {
1047 DRAGLISTINFO *dlm = (DRAGLISTINFO *)lParam;
1048 int dest = 0; /* initialise to placate gcc */
1049 switch (dlm->uNotification) {
1052 SendDlgItemMessage(hwnd, hdl->listid,
1053 LB_ADDSTRING, 0, (LPARAM) "");
1055 hdl->srcitem = LBItemFromPt(dlm->hWnd, dlm->ptCursor, TRUE);
1057 /* XXX hack Q183115 */
1058 SetWindowLong(hwnd, DWL_MSGRESULT, TRUE);
1061 DrawInsert(hwnd, dlm->hWnd, -1); /* Clear arrow */
1062 SendDlgItemMessage(hwnd, hdl->listid,
1063 LB_DELETESTRING, hdl->dummyitem, 0);
1068 dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, TRUE);
1069 if (dest > hdl->dummyitem) dest = hdl->dummyitem;
1070 DrawInsert (hwnd, dlm->hWnd, dest);
1072 SetWindowLong(hwnd, DWL_MSGRESULT, DL_MOVECURSOR);
1074 SetWindowLong(hwnd, DWL_MSGRESULT, DL_STOPCURSOR);
1077 if (hdl->dragging) {
1078 dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, TRUE);
1079 if (dest > hdl->dummyitem) dest = hdl->dummyitem;
1080 DrawInsert (hwnd, dlm->hWnd, -1);
1082 SendDlgItemMessage(hwnd, hdl->listid,
1083 LB_DELETESTRING, hdl->dummyitem, 0);
1084 if (hdl->dragging) {
1087 /* Correct for "missing" item. */
1088 if (dest > hdl->srcitem) dest--;
1089 pl_moveitem(hwnd, hdl->listid, hdl->srcitem, dest);
1099 if (((LOWORD(wParam) == hdl->upbid) ||
1100 (LOWORD(wParam) == hdl->dnbid)) &&
1101 ((HIWORD(wParam) == BN_CLICKED) ||
1102 (HIWORD(wParam) == BN_DOUBLECLICKED))) {
1103 /* Move an item up or down the list. */
1104 /* Get the current selection, if any. */
1105 int selection = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCURSEL, 0, 0);
1106 if (selection == LB_ERR) {
1110 /* Get the total number of items. */
1111 nitems = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCOUNT, 0, 0);
1112 /* Should we do anything? */
1113 if (LOWORD(wParam) == hdl->upbid && (selection > 0))
1114 pl_moveitem(hwnd, hdl->listid, selection, selection - 1);
1115 else if (LOWORD(wParam) == hdl->dnbid && (selection < nitems - 1))
1116 pl_moveitem(hwnd, hdl->listid, selection, selection + 1);
1125 /* Update array to match the list box. */
1126 for (i=0; i < maxmemb; i++)
1127 array[i] = SendDlgItemMessage (hwnd, hdl->listid, LB_GETITEMDATA,
1135 * A progress bar (from Common Controls). We like our progress bars
1136 * to be smooth and unbroken, without those ugly divisions; some
1137 * older compilers may not support that, but that's life.
1139 void progressbar(struct ctlpos *cp, int id)
1143 r.left = GAPBETWEEN;
1145 r.right = cp->width;
1146 r.bottom = PROGBARHEIGHT;
1147 cp->ypos += r.bottom + GAPBETWEEN;
1149 doctl(cp, r, PROGRESS_CLASS, WS_CHILD | WS_VISIBLE
1153 , WS_EX_CLIENTEDGE, "", id);
1156 /* ----------------------------------------------------------------------
1157 * Platform-specific side of portable dialog-box mechanism.
1161 * This function takes a string, escapes all the ampersands, and
1162 * places a single (unescaped) ampersand in front of the first
1163 * occurrence of the given shortcut character (which may be
1166 * Return value is a malloc'ed copy of the processed version of the
1169 static char *shortcut_escape(char *text, char shortcut)
1175 return NULL; /* sfree won't choke on this */
1177 ret = snewn(2*strlen(text)+1, char); /* size potentially doubles! */
1178 shortcut = tolower((unsigned char)shortcut);
1183 if (shortcut != NO_SHORTCUT &&
1184 tolower((unsigned char)*p) == shortcut) {
1186 shortcut = NO_SHORTCUT; /* stop it happening twice */
1187 } else if (*p == '&') {
1196 void winctrl_add_shortcuts(struct dlgparam *dp, struct winctrl *c)
1199 for (i = 0; i < lenof(c->shortcuts); i++)
1200 if (c->shortcuts[i] != NO_SHORTCUT) {
1201 unsigned char s = tolower((unsigned char)c->shortcuts[i]);
1202 assert(!dp->shortcuts[s]);
1203 dp->shortcuts[s] = TRUE;
1207 void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c)
1210 for (i = 0; i < lenof(c->shortcuts); i++)
1211 if (c->shortcuts[i] != NO_SHORTCUT) {
1212 unsigned char s = tolower((unsigned char)c->shortcuts[i]);
1213 assert(dp->shortcuts[s]);
1214 dp->shortcuts[s] = FALSE;
1218 static int winctrl_cmp_byctrl(void *av, void *bv)
1220 struct winctrl *a = (struct winctrl *)av;
1221 struct winctrl *b = (struct winctrl *)bv;
1222 if (a->ctrl < b->ctrl)
1224 else if (a->ctrl > b->ctrl)
1229 static int winctrl_cmp_byid(void *av, void *bv)
1231 struct winctrl *a = (struct winctrl *)av;
1232 struct winctrl *b = (struct winctrl *)bv;
1233 if (a->base_id < b->base_id)
1235 else if (a->base_id > b->base_id)
1240 static int winctrl_cmp_byctrl_find(void *av, void *bv)
1242 union control *a = (union control *)av;
1243 struct winctrl *b = (struct winctrl *)bv;
1246 else if (a > b->ctrl)
1251 static int winctrl_cmp_byid_find(void *av, void *bv)
1254 struct winctrl *b = (struct winctrl *)bv;
1255 if (*a < b->base_id)
1257 else if (*a >= b->base_id + b->num_ids)
1263 void winctrl_init(struct winctrls *wc)
1265 wc->byctrl = newtree234(winctrl_cmp_byctrl);
1266 wc->byid = newtree234(winctrl_cmp_byid);
1268 void winctrl_cleanup(struct winctrls *wc)
1272 while ((c = index234(wc->byid, 0)) != NULL) {
1273 winctrl_remove(wc, c);
1278 freetree234(wc->byctrl);
1279 freetree234(wc->byid);
1280 wc->byctrl = wc->byid = NULL;
1283 void winctrl_add(struct winctrls *wc, struct winctrl *c)
1285 struct winctrl *ret;
1287 ret = add234(wc->byctrl, c);
1290 ret = add234(wc->byid, c);
1294 void winctrl_remove(struct winctrls *wc, struct winctrl *c)
1296 struct winctrl *ret;
1297 ret = del234(wc->byctrl, c);
1298 ret = del234(wc->byid, c);
1302 struct winctrl *winctrl_findbyctrl(struct winctrls *wc, union control *ctrl)
1304 return find234(wc->byctrl, ctrl, winctrl_cmp_byctrl_find);
1307 struct winctrl *winctrl_findbyid(struct winctrls *wc, int id)
1309 return find234(wc->byid, &id, winctrl_cmp_byid_find);
1312 struct winctrl *winctrl_findbyindex(struct winctrls *wc, int index)
1314 return index234(wc->byid, index);
1317 void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
1318 struct ctlpos *cp, struct controlset *s, int *id)
1320 struct ctlpos columns[16];
1321 int ncols, colstart, colspan;
1323 struct ctlpos tabdelays[16];
1324 union control *tabdelayed[16];
1329 char shortcuts[MAX_SHORTCUTS_PER_CTRL];
1332 int i, actual_base_id, base_id, num_ids;
1337 /* Start a containing box, if we have a boxname. */
1338 if (s->boxname && *s->boxname) {
1339 struct winctrl *c = snew(struct winctrl);
1341 c->base_id = base_id;
1344 memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
1346 beginbox(cp, s->boxtitle, base_id);
1350 /* Draw a title, if we have one. */
1351 if (!s->boxname && s->boxtitle) {
1352 struct winctrl *c = snew(struct winctrl);
1354 c->base_id = base_id;
1356 c->data = dupstr(s->boxtitle);
1357 memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
1359 paneltitle(cp, base_id);
1363 /* Initially we have just one column. */
1365 columns[0] = *cp; /* structure copy */
1367 /* And initially, there are no pending tab-delayed controls. */
1370 /* Loop over each control in the controlset. */
1371 for (i = 0; i < s->ncontrols; i++) {
1372 union control *ctrl = s->ctrls[i];
1375 * Generic processing that pertains to all control types.
1376 * At the end of this if statement, we'll have produced
1377 * `ctrl' (a pointer to the control we have to create, or
1378 * think about creating, in this iteration of the loop),
1379 * `pos' (a suitable ctlpos with which to position it), and
1380 * `c' (a winctrl structure to receive details of the
1381 * dialog IDs). Or we'll have done a `continue', if it was
1382 * CTRL_COLUMNS and doesn't require any control creation at
1385 if (ctrl->generic.type == CTRL_COLUMNS) {
1386 assert((ctrl->columns.ncols == 1) ^ (ncols == 1));
1390 * We're splitting into multiple columns.
1392 int lpercent, rpercent, lx, rx, i;
1394 ncols = ctrl->columns.ncols;
1395 assert(ncols <= lenof(columns));
1396 for (i = 1; i < ncols; i++)
1397 columns[i] = columns[0]; /* structure copy */
1400 for (i = 0; i < ncols; i++) {
1401 rpercent = lpercent + ctrl->columns.percentages[i];
1402 lx = columns[i].xoff + lpercent *
1403 (columns[i].width + GAPBETWEEN) / 100;
1404 rx = columns[i].xoff + rpercent *
1405 (columns[i].width + GAPBETWEEN) / 100;
1406 columns[i].xoff = lx;
1407 columns[i].width = rx - lx - GAPBETWEEN;
1408 lpercent = rpercent;
1412 * We're recombining the various columns into one.
1414 int maxy = columns[0].ypos;
1416 for (i = 1; i < ncols; i++)
1417 if (maxy < columns[i].ypos)
1418 maxy = columns[i].ypos;
1420 columns[0] = *cp; /* structure copy */
1421 columns[0].ypos = maxy;
1425 } else if (ctrl->generic.type == CTRL_TABDELAY) {
1428 assert(!ctrl->generic.tabdelay);
1429 ctrl = ctrl->tabdelay.ctrl;
1431 for (i = 0; i < ntabdelays; i++)
1432 if (tabdelayed[i] == ctrl)
1434 assert(i < ntabdelays); /* we have to have found it */
1436 pos = tabdelays[i]; /* structure copy */
1438 colstart = colspan = -1; /* indicate this was tab-delayed */
1442 * If it wasn't one of those, it's a genuine control;
1443 * so we'll have to compute a position for it now, by
1444 * checking its column span.
1448 colstart = COLUMN_START(ctrl->generic.column);
1449 colspan = COLUMN_SPAN(ctrl->generic.column);
1451 pos = columns[colstart]; /* structure copy */
1452 pos.width = columns[colstart+colspan-1].width +
1453 (columns[colstart+colspan-1].xoff - columns[colstart].xoff);
1455 for (col = colstart; col < colstart+colspan; col++)
1456 if (pos.ypos < columns[col].ypos)
1457 pos.ypos = columns[col].ypos;
1460 * If this control is to be tabdelayed, add it to the
1461 * tabdelay list, and unset pos.hwnd to inhibit actual
1464 if (ctrl->generic.tabdelay) {
1465 assert(ntabdelays < lenof(tabdelays));
1466 tabdelays[ntabdelays] = pos; /* structure copy */
1467 tabdelayed[ntabdelays] = ctrl;
1473 /* Most controls don't need anything in c->data. */
1476 /* And they all start off with no shortcuts registered. */
1477 memset(shortcuts, NO_SHORTCUT, lenof(shortcuts));
1480 /* Almost all controls start at base_id. */
1481 actual_base_id = base_id;
1484 * Now we're ready to actually create the control, by
1485 * switching on its type.
1487 switch (ctrl->generic.type) {
1490 char *wrapped, *escaped;
1493 wrapped = staticwrap(&pos, cp->hwnd,
1494 ctrl->generic.label, &lines);
1495 escaped = shortcut_escape(wrapped, NO_SHORTCUT);
1496 statictext(&pos, escaped, lines, base_id);
1502 num_ids = 2; /* static, edit */
1503 escaped = shortcut_escape(ctrl->editbox.label,
1504 ctrl->editbox.shortcut);
1505 shortcuts[nshortcuts++] = ctrl->editbox.shortcut;
1506 if (ctrl->editbox.percentwidth == 100) {
1507 if (ctrl->editbox.has_list)
1508 combobox(&pos, escaped,
1509 base_id, base_id+1);
1511 multiedit(&pos, ctrl->editbox.password, escaped,
1512 base_id, base_id+1, 100, NULL);
1514 if (ctrl->editbox.has_list) {
1515 staticcombo(&pos, escaped, base_id, base_id+1,
1516 ctrl->editbox.percentwidth);
1518 (ctrl->editbox.password ? staticpassedit : staticedit)
1519 (&pos, escaped, base_id, base_id+1,
1520 ctrl->editbox.percentwidth);
1526 num_ids = ctrl->radio.nbuttons + 1; /* label as well */
1528 struct radio *buttons;
1531 escaped = shortcut_escape(ctrl->radio.label,
1532 ctrl->radio.shortcut);
1533 shortcuts[nshortcuts++] = ctrl->radio.shortcut;
1535 buttons = snewn(ctrl->radio.nbuttons, struct radio);
1537 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1539 shortcut_escape(ctrl->radio.buttons[i],
1540 (char)(ctrl->radio.shortcuts ?
1541 ctrl->radio.shortcuts[i] :
1543 buttons[i].id = base_id + 1 + i;
1544 if (ctrl->radio.shortcuts) {
1545 assert(nshortcuts < MAX_SHORTCUTS_PER_CTRL);
1546 shortcuts[nshortcuts++] = ctrl->radio.shortcuts[i];
1550 radioline_common(&pos, escaped, base_id,
1551 ctrl->radio.ncolumns,
1552 buttons, ctrl->radio.nbuttons);
1554 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1555 sfree(buttons[i].text);
1563 escaped = shortcut_escape(ctrl->checkbox.label,
1564 ctrl->checkbox.shortcut);
1565 shortcuts[nshortcuts++] = ctrl->checkbox.shortcut;
1566 checkbox(&pos, escaped, base_id);
1570 escaped = shortcut_escape(ctrl->button.label,
1571 ctrl->button.shortcut);
1572 shortcuts[nshortcuts++] = ctrl->button.shortcut;
1573 if (ctrl->button.iscancel)
1574 actual_base_id = IDCANCEL;
1576 button(&pos, escaped, actual_base_id, ctrl->button.isdefault);
1581 escaped = shortcut_escape(ctrl->listbox.label,
1582 ctrl->listbox.shortcut);
1583 shortcuts[nshortcuts++] = ctrl->listbox.shortcut;
1584 if (ctrl->listbox.draglist) {
1585 data = snew(struct prefslist);
1587 prefslist(data, &pos, ctrl->listbox.height, escaped,
1588 base_id, base_id+1, base_id+2, base_id+3);
1589 shortcuts[nshortcuts++] = 'u'; /* Up */
1590 shortcuts[nshortcuts++] = 'd'; /* Down */
1591 } else if (ctrl->listbox.height == 0) {
1592 /* Drop-down list. */
1593 if (ctrl->listbox.percentwidth == 100) {
1594 staticddlbig(&pos, escaped,
1595 base_id, base_id+1);
1597 staticddl(&pos, escaped, base_id,
1598 base_id+1, ctrl->listbox.percentwidth);
1601 /* Ordinary list. */
1602 listbox(&pos, escaped, base_id, base_id+1,
1603 ctrl->listbox.height, ctrl->listbox.multisel);
1605 if (ctrl->listbox.ncols) {
1607 * This method of getting the box width is a bit of
1608 * a hack; we'd do better to try to retrieve the
1609 * actual width in dialog units from doctl() just
1610 * before MapDialogRect. But that's going to be no
1611 * fun, and this should be good enough accuracy.
1613 int width = cp->width * ctrl->listbox.percentwidth;
1617 tabarray = snewn(ctrl->listbox.ncols-1, int);
1619 for (i = 0; i < ctrl->listbox.ncols-1; i++) {
1620 percent += ctrl->listbox.percentages[i];
1621 tabarray[i] = width * percent / 10000;
1623 SendDlgItemMessage(cp->hwnd, base_id+1, LB_SETTABSTOPS,
1624 ctrl->listbox.ncols-1, (LPARAM)tabarray);
1629 case CTRL_FILESELECT:
1631 escaped = shortcut_escape(ctrl->fileselect.label,
1632 ctrl->fileselect.shortcut);
1633 shortcuts[nshortcuts++] = ctrl->fileselect.shortcut;
1634 editbutton(&pos, escaped, base_id, base_id+1,
1635 "Bro&wse...", base_id+2);
1636 shortcuts[nshortcuts++] = 'w';
1639 case CTRL_FONTSELECT:
1641 escaped = shortcut_escape(ctrl->fontselect.label,
1642 ctrl->fontselect.shortcut);
1643 shortcuts[nshortcuts++] = ctrl->fontselect.shortcut;
1644 statictext(&pos, escaped, 1, base_id);
1645 staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
1647 data = snew(FontSpec);
1650 assert(!"Can't happen");
1651 num_ids = 0; /* placate gcc */
1656 * Create a `struct winctrl' for this control, and advance
1657 * the dialog ID counter, if it's actually been created
1658 * (and isn't tabdelayed).
1661 struct winctrl *c = snew(struct winctrl);
1664 c->base_id = actual_base_id;
1665 c->num_ids = num_ids;
1667 memcpy(c->shortcuts, shortcuts, sizeof(shortcuts));
1669 winctrl_add_shortcuts(dp, c);
1670 if (actual_base_id == base_id)
1674 if (colstart >= 0) {
1676 * Update the ypos in all columns crossed by this
1680 for (i = colstart; i < colstart+colspan; i++)
1681 columns[i].ypos = pos.ypos;
1686 * We've now finished laying out the controls; so now update
1687 * the ctlpos and control ID that were passed in, terminate
1688 * any containing box, and return.
1690 for (i = 0; i < ncols; i++)
1691 if (cp->ypos < columns[i].ypos)
1692 cp->ypos = columns[i].ypos;
1695 if (s->boxname && *s->boxname)
1699 static void winctrl_set_focus(union control *ctrl, struct dlgparam *dp,
1704 dp->lastfocused = dp->focused;
1706 } else if (!has_focus && dp->focused == ctrl) {
1707 dp->lastfocused = dp->focused;
1712 union control *dlg_last_focused(union control *ctrl, void *dlg)
1714 struct dlgparam *dp = (struct dlgparam *)dlg;
1715 return dp->focused == ctrl ? dp->lastfocused : dp->focused;
1719 * The dialog-box procedure calls this function to handle Windows
1720 * messages on a control we manage.
1722 int winctrl_handle_command(struct dlgparam *dp, UINT msg,
1723 WPARAM wParam, LPARAM lParam)
1726 union control *ctrl;
1728 static UINT draglistmsg = WM_NULL;
1731 * Filter out pointless window messages. Our interest is in
1732 * WM_COMMAND and the drag list message, and nothing else.
1734 if (draglistmsg == WM_NULL)
1735 draglistmsg = RegisterWindowMessage (DRAGLISTMSGSTRING);
1737 if (msg != draglistmsg && msg != WM_COMMAND && msg != WM_DRAWITEM)
1741 * Look up the control ID in our data.
1744 for (i = 0; i < dp->nctrltrees; i++) {
1745 c = winctrl_findbyid(dp->controltrees[i], LOWORD(wParam));
1750 return 0; /* we have nothing to do */
1752 if (msg == WM_DRAWITEM) {
1754 * Owner-draw request for a panel title.
1756 LPDRAWITEMSTRUCT di = (LPDRAWITEMSTRUCT) lParam;
1758 RECT r = di->rcItem;
1761 SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
1763 GetTextExtentPoint32(hdc, (char *)c->data,
1764 strlen((char *)c->data), &s);
1765 DrawEdge(hdc, &r, EDGE_ETCHED, BF_ADJUST | BF_RECT);
1767 r.left + (r.right-r.left-s.cx)/2,
1768 r.top + (r.bottom-r.top-s.cy)/2,
1769 (char *)c->data, strlen((char *)c->data));
1775 id = LOWORD(wParam) - c->base_id;
1777 if (!ctrl || !ctrl->generic.handler)
1778 return 0; /* nothing we can do here */
1781 * From here on we do not issue `return' statements until the
1782 * very end of the dialog box: any event handler is entitled to
1783 * ask for a colour selector, so we _must_ always allow control
1784 * to reach the end of this switch statement so that the
1785 * subsequent code can test dp->coloursel_wanted().
1788 dp->coloursel_wanted = FALSE;
1791 * Now switch on the control type and the message.
1793 switch (ctrl->generic.type) {
1795 if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
1796 (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
1797 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
1798 if (msg == WM_COMMAND && ctrl->editbox.has_list &&
1799 (HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
1800 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
1802 if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
1803 HIWORD(wParam) == EN_CHANGE)
1804 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1805 if (msg == WM_COMMAND &&
1806 ctrl->editbox.has_list) {
1807 if (HIWORD(wParam) == CBN_SELCHANGE) {
1811 index = SendDlgItemMessage(dp->hwnd, c->base_id+1,
1812 CB_GETCURSEL, 0, 0);
1813 len = SendDlgItemMessage(dp->hwnd, c->base_id+1,
1814 CB_GETLBTEXTLEN, index, 0);
1815 text = snewn(len+1, char);
1816 SendDlgItemMessage(dp->hwnd, c->base_id+1, CB_GETLBTEXT,
1817 index, (LPARAM)text);
1818 SetDlgItemText(dp->hwnd, c->base_id+1, text);
1820 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1821 } else if (HIWORD(wParam) == CBN_EDITCHANGE) {
1822 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1823 } else if (HIWORD(wParam) == CBN_KILLFOCUS) {
1824 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
1830 if (msg == WM_COMMAND &&
1831 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1832 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1834 * We sometimes get spurious BN_CLICKED messages for the
1835 * radio button that is just about to _lose_ selection, if
1836 * we're switching using the arrow keys. Therefore we
1837 * double-check that the button in wParam is actually
1838 * checked before generating an event.
1840 if (msg == WM_COMMAND &&
1841 (HIWORD(wParam) == BN_CLICKED ||
1842 HIWORD(wParam) == BN_DOUBLECLICKED) &&
1843 IsDlgButtonChecked(dp->hwnd, LOWORD(wParam))) {
1844 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1848 if (msg == WM_COMMAND &&
1849 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1850 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1851 if (msg == WM_COMMAND &&
1852 (HIWORD(wParam) == BN_CLICKED ||
1853 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1854 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1858 if (msg == WM_COMMAND &&
1859 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1860 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1861 if (msg == WM_COMMAND &&
1862 (HIWORD(wParam) == BN_CLICKED ||
1863 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1864 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_ACTION);
1868 if (msg == WM_COMMAND && ctrl->listbox.height != 0 &&
1869 (HIWORD(wParam)==LBN_SETFOCUS || HIWORD(wParam)==LBN_KILLFOCUS))
1870 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == LBN_SETFOCUS);
1871 if (msg == WM_COMMAND && ctrl->listbox.height == 0 &&
1872 (HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
1873 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
1874 if (msg == WM_COMMAND && id >= 2 &&
1875 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1876 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1877 if (ctrl->listbox.draglist) {
1879 pret = handle_prefslist(c->data, NULL, 0, (msg != WM_COMMAND),
1880 dp->hwnd, wParam, lParam);
1882 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1885 if (msg == WM_COMMAND && HIWORD(wParam) == LBN_DBLCLK) {
1886 SetCapture(dp->hwnd);
1887 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_ACTION);
1888 } else if (msg == WM_COMMAND && HIWORD(wParam) == LBN_SELCHANGE) {
1889 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_SELCHANGE);
1893 case CTRL_FILESELECT:
1894 if (msg == WM_COMMAND && id == 1 &&
1895 (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
1896 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
1897 if (msg == WM_COMMAND && id == 2 &&
1898 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1899 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1900 if (msg == WM_COMMAND && id == 1 && HIWORD(wParam) == EN_CHANGE)
1901 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1903 (msg == WM_COMMAND &&
1904 (HIWORD(wParam) == BN_CLICKED ||
1905 HIWORD(wParam) == BN_DOUBLECLICKED))) {
1907 char filename[FILENAME_MAX];
1909 memset(&of, 0, sizeof(of));
1910 of.hwndOwner = dp->hwnd;
1911 if (ctrl->fileselect.filter)
1912 of.lpstrFilter = ctrl->fileselect.filter;
1914 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
1915 of.lpstrCustomFilter = NULL;
1916 of.nFilterIndex = 1;
1917 of.lpstrFile = filename;
1918 GetDlgItemText(dp->hwnd, c->base_id+1, filename, lenof(filename));
1919 filename[lenof(filename)-1] = '\0';
1920 of.nMaxFile = lenof(filename);
1921 of.lpstrFileTitle = NULL;
1922 of.lpstrTitle = ctrl->fileselect.title;
1924 if (request_file(NULL, &of, FALSE, ctrl->fileselect.for_writing)) {
1925 SetDlgItemText(dp->hwnd, c->base_id + 1, filename);
1926 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1930 case CTRL_FONTSELECT:
1931 if (msg == WM_COMMAND && id == 2 &&
1932 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1933 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1935 (msg == WM_COMMAND &&
1936 (HIWORD(wParam) == BN_CLICKED ||
1937 HIWORD(wParam) == BN_DOUBLECLICKED))) {
1941 FontSpec fs = *(FontSpec *)c->data;
1944 lf.lfHeight = -MulDiv(fs.height,
1945 GetDeviceCaps(hdc, LOGPIXELSY), 72);
1947 lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
1948 lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
1949 lf.lfWeight = (fs.isbold ? FW_BOLD : 0);
1950 lf.lfCharSet = fs.charset;
1951 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
1952 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1953 lf.lfQuality = DEFAULT_QUALITY;
1954 lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
1955 strncpy(lf.lfFaceName, fs.name,
1956 sizeof(lf.lfFaceName) - 1);
1957 lf.lfFaceName[sizeof(lf.lfFaceName) - 1] = '\0';
1959 cf.lStructSize = sizeof(cf);
1960 cf.hwndOwner = dp->hwnd;
1962 cf.Flags = CF_FIXEDPITCHONLY | CF_FORCEFONTEXIST |
1963 CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
1965 if (ChooseFont(&cf)) {
1966 strncpy(fs.name, lf.lfFaceName,
1967 sizeof(fs.name) - 1);
1968 fs.name[sizeof(fs.name) - 1] = '\0';
1969 fs.isbold = (lf.lfWeight == FW_BOLD);
1970 fs.charset = lf.lfCharSet;
1971 fs.height = cf.iPointSize / 10;
1972 dlg_fontsel_set(ctrl, dp, fs);
1973 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1980 * If the above event handler has asked for a colour selector,
1981 * now is the time to generate one.
1983 if (dp->coloursel_wanted) {
1984 static CHOOSECOLOR cc;
1985 static DWORD custom[16] = { 0 }; /* zero initialisers */
1986 cc.lStructSize = sizeof(cc);
1987 cc.hwndOwner = dp->hwnd;
1988 cc.hInstance = (HWND) hinst;
1989 cc.lpCustColors = custom;
1990 cc.rgbResult = RGB(dp->coloursel_result.r,
1991 dp->coloursel_result.g,
1992 dp->coloursel_result.b);
1993 cc.Flags = CC_FULLOPEN | CC_RGBINIT;
1994 if (ChooseColor(&cc)) {
1995 dp->coloursel_result.r =
1996 (unsigned char) (cc.rgbResult & 0xFF);
1997 dp->coloursel_result.g =
1998 (unsigned char) (cc.rgbResult >> 8) & 0xFF;
1999 dp->coloursel_result.b =
2000 (unsigned char) (cc.rgbResult >> 16) & 0xFF;
2001 dp->coloursel_result.ok = TRUE;
2003 dp->coloursel_result.ok = FALSE;
2004 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_CALLBACK);
2011 * This function can be called to produce context help on a
2012 * control. Returns TRUE if it has actually launched WinHelp.
2014 int winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id)
2021 * Look up the control ID in our data.
2024 for (i = 0; i < dp->nctrltrees; i++) {
2025 c = winctrl_findbyid(dp->controltrees[i], id);
2030 return 0; /* we have nothing to do */
2033 * This is the Windows front end, so we're allowed to assume
2034 * `helpctx.p' is a context string.
2036 if (!c->ctrl || !c->ctrl->generic.helpctx.p)
2037 return 0; /* no help available for this ctrl */
2039 cmd = dupprintf("JI(`',`%s')", c->ctrl->generic.helpctx.p);
2040 WinHelp(hwnd, help_path, HELP_COMMAND, (DWORD)cmd);
2046 * Now the various functions that the platform-independent
2047 * mechanism can call to access the dialog box entries.
2050 static struct winctrl *dlg_findbyctrl(struct dlgparam *dp, union control *ctrl)
2054 for (i = 0; i < dp->nctrltrees; i++) {
2055 struct winctrl *c = winctrl_findbyctrl(dp->controltrees[i], ctrl);
2062 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
2064 struct dlgparam *dp = (struct dlgparam *)dlg;
2065 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2066 assert(c && c->ctrl->generic.type == CTRL_RADIO);
2067 CheckRadioButton(dp->hwnd,
2069 c->base_id + c->ctrl->radio.nbuttons,
2070 c->base_id + 1 + whichbutton);
2073 int dlg_radiobutton_get(union control *ctrl, void *dlg)
2075 struct dlgparam *dp = (struct dlgparam *)dlg;
2076 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2078 assert(c && c->ctrl->generic.type == CTRL_RADIO);
2079 for (i = 0; i < c->ctrl->radio.nbuttons; i++)
2080 if (IsDlgButtonChecked(dp->hwnd, c->base_id + 1 + i))
2082 assert(!"No radio button was checked?!");
2086 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
2088 struct dlgparam *dp = (struct dlgparam *)dlg;
2089 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2090 assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
2091 CheckDlgButton(dp->hwnd, c->base_id, (checked != 0));
2094 int dlg_checkbox_get(union control *ctrl, void *dlg)
2096 struct dlgparam *dp = (struct dlgparam *)dlg;
2097 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2098 assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
2099 return 0 != IsDlgButtonChecked(dp->hwnd, c->base_id);
2102 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
2104 struct dlgparam *dp = (struct dlgparam *)dlg;
2105 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2106 assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
2107 SetDlgItemText(dp->hwnd, c->base_id+1, text);
2110 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
2112 struct dlgparam *dp = (struct dlgparam *)dlg;
2113 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2114 assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
2115 GetDlgItemText(dp->hwnd, c->base_id+1, buffer, length);
2116 buffer[length-1] = '\0';
2119 /* The `listbox' functions can also apply to combo boxes. */
2120 void dlg_listbox_clear(union control *ctrl, void *dlg)
2122 struct dlgparam *dp = (struct dlgparam *)dlg;
2123 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2126 (c->ctrl->generic.type == CTRL_LISTBOX ||
2127 (c->ctrl->generic.type == CTRL_EDITBOX &&
2128 c->ctrl->editbox.has_list)));
2129 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2130 LB_RESETCONTENT : CB_RESETCONTENT);
2131 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
2134 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
2136 struct dlgparam *dp = (struct dlgparam *)dlg;
2137 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2140 (c->ctrl->generic.type == CTRL_LISTBOX ||
2141 (c->ctrl->generic.type == CTRL_EDITBOX &&
2142 c->ctrl->editbox.has_list)));
2143 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2144 LB_DELETESTRING : CB_DELETESTRING);
2145 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2148 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
2150 struct dlgparam *dp = (struct dlgparam *)dlg;
2151 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2154 (c->ctrl->generic.type == CTRL_LISTBOX ||
2155 (c->ctrl->generic.type == CTRL_EDITBOX &&
2156 c->ctrl->editbox.has_list)));
2157 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2158 LB_ADDSTRING : CB_ADDSTRING);
2159 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
2163 * Each listbox entry may have a numeric id associated with it.
2164 * Note that some front ends only permit a string to be stored at
2165 * each position, which means that _if_ you put two identical
2166 * strings in any listbox then you MUST not assign them different
2167 * IDs and expect to get meaningful results back.
2169 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
2170 char const *text, int id)
2172 struct dlgparam *dp = (struct dlgparam *)dlg;
2173 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2174 int msg, msg2, index;
2176 (c->ctrl->generic.type == CTRL_LISTBOX ||
2177 (c->ctrl->generic.type == CTRL_EDITBOX &&
2178 c->ctrl->editbox.has_list)));
2179 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2180 LB_ADDSTRING : CB_ADDSTRING);
2181 msg2 = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2182 LB_SETITEMDATA : CB_SETITEMDATA);
2183 index = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
2184 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg2, index, (LPARAM)id);
2187 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
2189 struct dlgparam *dp = (struct dlgparam *)dlg;
2190 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2192 assert(c && c->ctrl->generic.type == CTRL_LISTBOX);
2193 msg = (c->ctrl->listbox.height != 0 ? LB_GETITEMDATA : CB_GETITEMDATA);
2195 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2198 /* dlg_listbox_index returns <0 if no single element is selected. */
2199 int dlg_listbox_index(union control *ctrl, void *dlg)
2201 struct dlgparam *dp = (struct dlgparam *)dlg;
2202 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2204 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2205 !c->ctrl->listbox.multisel);
2206 msg = (c->ctrl->listbox.height != 0 ? LB_GETCURSEL : CB_GETCURSEL);
2207 ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
2214 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
2216 struct dlgparam *dp = (struct dlgparam *)dlg;
2217 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2218 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2219 c->ctrl->listbox.multisel &&
2220 c->ctrl->listbox.height != 0);
2222 SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSEL, index, 0);
2225 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
2227 struct dlgparam *dp = (struct dlgparam *)dlg;
2228 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2230 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2231 !c->ctrl->listbox.multisel);
2232 msg = (c->ctrl->listbox.height != 0 ? LB_SETCURSEL : CB_SETCURSEL);
2233 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2236 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
2238 struct dlgparam *dp = (struct dlgparam *)dlg;
2239 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2240 assert(c && c->ctrl->generic.type == CTRL_TEXT);
2241 SetDlgItemText(dp->hwnd, c->base_id, text);
2244 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
2246 struct dlgparam *dp = (struct dlgparam *)dlg;
2247 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2248 assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
2249 SetDlgItemText(dp->hwnd, c->base_id+1, fn.path);
2252 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
2254 struct dlgparam *dp = (struct dlgparam *)dlg;
2255 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2256 assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
2257 GetDlgItemText(dp->hwnd, c->base_id+1, fn->path, lenof(fn->path));
2258 fn->path[lenof(fn->path)-1] = '\0';
2261 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs)
2263 char *buf, *boldstr;
2264 struct dlgparam *dp = (struct dlgparam *)dlg;
2265 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2266 assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
2268 *(FontSpec *)c->data = fs; /* structure copy */
2270 boldstr = (fs.isbold ? "bold, " : "");
2272 buf = dupprintf("Font: %s, %sdefault height", fs.name, boldstr);
2274 buf = dupprintf("Font: %s, %s%d-%s", fs.name, boldstr,
2275 (fs.height < 0 ? -fs.height : fs.height),
2276 (fs.height < 0 ? "pixel" : "point"));
2277 SetDlgItemText(dp->hwnd, c->base_id+1, buf);
2281 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs)
2283 struct dlgparam *dp = (struct dlgparam *)dlg;
2284 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2285 assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
2286 *fs = *(FontSpec *)c->data; /* structure copy */
2290 * Bracketing a large set of updates in these two functions will
2291 * cause the front end (if possible) to delay updating the screen
2292 * until it's all complete, thus avoiding flicker.
2294 void dlg_update_start(union control *ctrl, void *dlg)
2296 struct dlgparam *dp = (struct dlgparam *)dlg;
2297 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2298 if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
2299 SendDlgItemMessage(dp->hwnd, c->base_id+1, WM_SETREDRAW, FALSE, 0);
2303 void dlg_update_done(union control *ctrl, void *dlg)
2305 struct dlgparam *dp = (struct dlgparam *)dlg;
2306 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2307 if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
2308 HWND hw = GetDlgItem(dp->hwnd, c->base_id+1);
2309 SendMessage(hw, WM_SETREDRAW, TRUE, 0);
2310 InvalidateRect(hw, NULL, TRUE);
2314 void dlg_set_focus(union control *ctrl, void *dlg)
2316 struct dlgparam *dp = (struct dlgparam *)dlg;
2317 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2320 switch (ctrl->generic.type) {
2321 case CTRL_EDITBOX: id = c->base_id + 1; break;
2323 for (id = c->base_id + ctrl->radio.nbuttons; id > 1; id--)
2324 if (IsDlgButtonChecked(dp->hwnd, id))
2327 * In the theoretically-unlikely case that no button was
2328 * selected, id should come out of this as 1, which is a
2329 * reasonable enough choice.
2332 case CTRL_CHECKBOX: id = c->base_id; break;
2333 case CTRL_BUTTON: id = c->base_id; break;
2334 case CTRL_LISTBOX: id = c->base_id + 1; break;
2335 case CTRL_FILESELECT: id = c->base_id + 1; break;
2336 case CTRL_FONTSELECT: id = c->base_id + 2; break;
2337 default: id = c->base_id; break;
2339 ctl = GetDlgItem(dp->hwnd, id);
2344 * During event processing, you might well want to give an error
2345 * indication to the user. dlg_beep() is a quick and easy generic
2346 * error; dlg_error() puts up a message-box or equivalent.
2348 void dlg_beep(void *dlg)
2350 /* struct dlgparam *dp = (struct dlgparam *)dlg; */
2354 void dlg_error_msg(void *dlg, char *msg)
2356 struct dlgparam *dp = (struct dlgparam *)dlg;
2357 MessageBox(dp->hwnd, msg,
2358 dp->errtitle ? dp->errtitle : NULL,
2359 MB_OK | MB_ICONERROR);
2363 * This function signals to the front end that the dialog's
2364 * processing is completed, and passes an integer value (typically
2365 * a success status).
2367 void dlg_end(void *dlg, int value)
2369 struct dlgparam *dp = (struct dlgparam *)dlg;
2371 dp->endresult = value;
2374 void dlg_refresh(union control *ctrl, void *dlg)
2376 struct dlgparam *dp = (struct dlgparam *)dlg;
2382 * Send EVENT_REFRESH to absolutely everything.
2384 for (j = 0; j < dp->nctrltrees; j++) {
2386 (c = winctrl_findbyindex(dp->controltrees[j], i)) != NULL;
2388 if (c->ctrl && c->ctrl->generic.handler != NULL)
2389 c->ctrl->generic.handler(c->ctrl, dp,
2390 dp->data, EVENT_REFRESH);
2395 * Send EVENT_REFRESH to a specific control.
2397 if (ctrl->generic.handler != NULL)
2398 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
2402 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
2404 struct dlgparam *dp = (struct dlgparam *)dlg;
2405 dp->coloursel_wanted = TRUE;
2406 dp->coloursel_result.r = r;
2407 dp->coloursel_result.g = g;
2408 dp->coloursel_result.b = b;
2411 int dlg_coloursel_results(union control *ctrl, void *dlg,
2412 int *r, int *g, int *b)
2414 struct dlgparam *dp = (struct dlgparam *)dlg;
2415 if (dp->coloursel_result.ok) {
2416 *r = dp->coloursel_result.r;
2417 *g = dp->coloursel_result.g;
2418 *b = dp->coloursel_result.b;
2424 struct perctrl_privdata {
2425 union control *ctrl;
2430 static int perctrl_privdata_cmp(void *av, void *bv)
2432 struct perctrl_privdata *a = (struct perctrl_privdata *)av;
2433 struct perctrl_privdata *b = (struct perctrl_privdata *)bv;
2434 if (a->ctrl < b->ctrl)
2436 else if (a->ctrl > b->ctrl)
2441 void dp_init(struct dlgparam *dp)
2446 dp->focused = dp->lastfocused = NULL;
2447 memset(dp->shortcuts, 0, sizeof(dp->shortcuts));
2449 dp->wintitle = dp->errtitle = NULL;
2450 dp->privdata = newtree234(perctrl_privdata_cmp);
2453 void dp_add_tree(struct dlgparam *dp, struct winctrls *wc)
2455 assert(dp->nctrltrees < lenof(dp->controltrees));
2456 dp->controltrees[dp->nctrltrees++] = wc;
2459 void dp_cleanup(struct dlgparam *dp)
2461 struct perctrl_privdata *p;
2464 while ( (p = index234(dp->privdata, 0)) != NULL ) {
2465 del234(dp->privdata, p);
2470 freetree234(dp->privdata);
2471 dp->privdata = NULL;
2473 sfree(dp->wintitle);
2474 sfree(dp->errtitle);
2477 void *dlg_get_privdata(union control *ctrl, void *dlg)
2479 struct dlgparam *dp = (struct dlgparam *)dlg;
2480 struct perctrl_privdata tmp, *p;
2482 p = find234(dp->privdata, &tmp, NULL);
2489 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
2491 struct dlgparam *dp = (struct dlgparam *)dlg;
2492 struct perctrl_privdata tmp, *p;
2494 p = find234(dp->privdata, &tmp, NULL);
2496 p = snew(struct perctrl_privdata);
2498 p->needs_free = FALSE;
2499 add234(dp->privdata, p);
2504 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
2506 struct dlgparam *dp = (struct dlgparam *)dlg;
2507 struct perctrl_privdata tmp, *p;
2509 p = find234(dp->privdata, &tmp, NULL);
2511 p = snew(struct perctrl_privdata);
2513 p->needs_free = FALSE;
2514 add234(dp->privdata, p);
2516 assert(!p->needs_free);
2517 p->needs_free = TRUE;
2519 * This is an internal allocation routine, so it's allowed to
2520 * use smalloc directly.
2522 p->data = smalloc(size);