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'
33 #define STATICHEIGHT 8
34 #define TITLEHEIGHT 12
35 #define CHECKBOXHEIGHT 8
39 #define LISTINCREMENT 8
40 #define COMBOHEIGHT 12
41 #define PUSHBTNHEIGHT 14
42 #define PROGBARHEIGHT 14
44 void ctlposinit(struct ctlpos *cp, HWND hwnd,
45 int leftborder, int rightborder, int topborder)
49 cp->font = SendMessage(hwnd, WM_GETFONT, 0, 0);
51 GetClientRect(hwnd, &r);
55 MapDialogRect(hwnd, &r2);
56 cp->dlu4inpix = r2.right;
57 cp->width = (r.right * 4) / (r2.right) - 2 * GAPBETWEEN;
58 cp->xoff = leftborder;
59 cp->width -= leftborder + rightborder;
62 HWND doctl(struct ctlpos *cp, RECT r,
63 char *wclass, int wstyle, int exstyle, char *wtext, int wid)
67 * Note nonstandard use of RECT. This is deliberate: by
68 * transforming the width and height directly we arrange to
69 * have all supposedly same-sized controls really same-sized.
73 MapDialogRect(cp->hwnd, &r);
76 * We can pass in cp->hwnd == NULL, to indicate a dry run
77 * without creating any actual controls.
80 ctl = CreateWindowEx(exstyle, wclass, wtext, wstyle,
81 r.left, r.top, r.right, r.bottom,
82 cp->hwnd, (HMENU) wid, hinst, NULL);
83 SendMessage(ctl, WM_SETFONT, cp->font, MAKELPARAM(TRUE, 0));
85 if (!strcmp(wclass, "LISTBOX")) {
87 * Bizarre Windows bug: the list box calculates its
88 * number of lines based on the font it has at creation
89 * time, but sending it WM_SETFONT doesn't cause it to
90 * recalculate. So now, _after_ we've sent it
91 * WM_SETFONT, we explicitly resize it (to the same
92 * size it was already!) to force it to reconsider.
94 SetWindowPos(ctl, NULL, 0, 0, r.right, r.bottom,
95 SWP_NOACTIVATE | SWP_NOCOPYBITS |
96 SWP_NOMOVE | SWP_NOZORDER);
104 * A title bar across the top of a sub-dialog.
106 void bartitle(struct ctlpos *cp, char *name, int id)
113 r.bottom = STATICHEIGHT;
114 cp->ypos += r.bottom + GAPBETWEEN;
115 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, name, id);
119 * Begin a grouping box, with or without a group title.
121 void beginbox(struct ctlpos *cp, char *name, int idbox)
123 cp->boxystart = cp->ypos;
125 cp->boxystart -= STATICHEIGHT / 2;
127 cp->ypos += STATICHEIGHT;
129 cp->width -= 2 * GAPXBOX;
136 * End a grouping box.
138 void endbox(struct ctlpos *cp)
142 cp->width += 2 * GAPXBOX;
143 cp->ypos += GAPYBOX - GAPBETWEEN;
146 r.top = cp->boxystart;
147 r.bottom = cp->ypos - cp->boxystart;
148 doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0,
149 cp->boxtext ? cp->boxtext : "", cp->boxid);
154 * Some edit boxes. Each one has a static above it. The percentages
155 * of the horizontal space are provided.
157 void multiedit(struct ctlpos *cp, int password, ...)
164 va_start(ap, password);
167 int staticid, editid, pcwidth;
168 text = va_arg(ap, char *);
171 staticid = va_arg(ap, int);
172 editid = va_arg(ap, int);
173 pcwidth = va_arg(ap, int);
175 r.left = xpos + GAPBETWEEN;
177 xpos = (cp->width + GAPBETWEEN) * percent / 100;
178 r.right = xpos - r.left;
181 r.bottom = STATICHEIGHT;
182 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
183 r.top = cp->ypos + 8 + GAPWITHIN;
184 r.bottom = EDITHEIGHT;
186 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL |
187 (password ? ES_PASSWORD : 0),
188 WS_EX_CLIENTEDGE, "", editid);
191 cp->ypos += STATICHEIGHT + GAPWITHIN + EDITHEIGHT + GAPBETWEEN;
195 * A static line, followed by a full-width combo box.
197 void combobox(struct ctlpos *cp, char *text, int staticid, int listid)
205 r.bottom = STATICHEIGHT;
206 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
207 r.top = cp->ypos + 8 + GAPWITHIN;
208 r.bottom = COMBOHEIGHT * 10;
209 doctl(cp, r, "COMBOBOX",
210 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
211 CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
213 cp->ypos += STATICHEIGHT + GAPWITHIN + COMBOHEIGHT + GAPBETWEEN;
216 struct radio { char *text; int id; };
218 static void radioline_common(struct ctlpos *cp, char *text, int id,
219 int nacross, struct radio *buttons, int nbuttons)
230 r.bottom = STATICHEIGHT;
231 cp->ypos += r.bottom + GAPWITHIN;
232 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
237 for (j = 0; j < nbuttons; j++) {
238 char *btext = buttons[j].text;
239 int bid = buttons[j].id;
242 cp->ypos += r.bottom + (nacross > 1 ? GAPBETWEEN : GAPWITHIN);
245 r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
248 (i + 1) * (cp->width + GAPBETWEEN) / nacross - r.left;
250 r.right = cp->width - r.left;
252 r.bottom = RADIOHEIGHT;
253 doctl(cp, r, "BUTTON",
254 BS_NOTIFY | BS_AUTORADIOBUTTON | WS_CHILD |
255 WS_VISIBLE | WS_TABSTOP | group, 0, btext, bid);
259 cp->ypos += r.bottom + GAPBETWEEN;
263 * A set of radio buttons on the same line, with a static above
264 * them. `nacross' dictates how many parts the line is divided into
265 * (you might want this not to equal the number of buttons if you
266 * needed to line up some 2s and some 3s to look good in the same
269 * There's a bit of a hack in here to ensure that if nacross
270 * exceeds the actual number of buttons, the rightmost button
271 * really does get all the space right to the edge of the line, so
272 * you can do things like
274 * (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
276 void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...)
279 struct radio *buttons;
282 va_start(ap, nacross);
285 char *btext = va_arg(ap, char *);
289 bid = va_arg(ap, int);
292 buttons = smalloc(nbuttons * sizeof(struct radio));
293 va_start(ap, nacross);
294 for (i = 0; i < nbuttons; i++) {
295 buttons[i].text = va_arg(ap, char *);
296 buttons[i].id = va_arg(ap, int);
299 radioline_common(cp, text, id, nacross, buttons, nbuttons);
304 * A set of radio buttons on the same line, without a static above
305 * them. Otherwise just like radioline.
307 void bareradioline(struct ctlpos *cp, int nacross, ...)
310 struct radio *buttons;
313 va_start(ap, nacross);
316 char *btext = va_arg(ap, char *);
320 bid = va_arg(ap, int);
323 buttons = smalloc(nbuttons * sizeof(struct radio));
324 va_start(ap, nacross);
325 for (i = 0; i < nbuttons; i++) {
326 buttons[i].text = va_arg(ap, char *);
327 buttons[i].id = va_arg(ap, int);
330 radioline_common(cp, NULL, 0, nacross, buttons, nbuttons);
335 * A set of radio buttons on multiple lines, with a static above
338 void radiobig(struct ctlpos *cp, char *text, int id, ...)
341 struct radio *buttons;
347 char *btext = va_arg(ap, char *);
351 bid = va_arg(ap, int);
354 buttons = smalloc(nbuttons * sizeof(struct radio));
356 for (i = 0; i < nbuttons; i++) {
357 buttons[i].text = va_arg(ap, char *);
358 buttons[i].id = va_arg(ap, int);
361 radioline_common(cp, text, id, 1, buttons, nbuttons);
366 * A single standalone checkbox.
368 void checkbox(struct ctlpos *cp, char *text, int id)
375 r.bottom = CHECKBOXHEIGHT;
376 cp->ypos += r.bottom + GAPBETWEEN;
377 doctl(cp, r, "BUTTON",
378 BS_NOTIFY | BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
383 * Wrap a piece of text for a static text control. Returns the
384 * wrapped text (a malloc'ed string containing \ns), and also
385 * returns the number of lines required.
387 char *staticwrap(struct ctlpos *cp, HWND hwnd, char *text, int *lines)
389 HDC hdc = GetDC(hwnd);
390 int lpx = GetDeviceCaps(hdc, LOGPIXELSX);
391 int width, nlines, j;
396 HFONT oldfont, newfont;
398 ret = smalloc(1+strlen(text));
401 pwidths = smalloc(sizeof(INT)*(1+strlen(text)));
404 * Work out the width the text will need to fit in, by doing
405 * the same adjustment that the `statictext' function itself
408 SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
409 r.left = r.top = r.bottom = 0;
411 MapDialogRect(hwnd, &r);
417 * We must select the correct font into the HDC before calling
418 * GetTextExtent*, or silly things will happen.
420 newfont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
421 oldfont = SelectObject(hdc, newfont);
424 if (!GetTextExtentExPoint(hdc, p, strlen(p), width,
425 &nfit, pwidths, &size) ||
426 (size_t)nfit >= strlen(p)) {
428 * Either GetTextExtentExPoint returned failure, or the
429 * whole of the rest of the text fits on this line.
430 * Either way, we stop wrapping, copy the remainder of
431 * the input string unchanged to the output, and leave.
438 * Now we search backwards along the string from `nfit',
439 * looking for a space at which to break the line. If we
440 * don't find one at all, that's fine - we'll just break
441 * the line at `nfit'.
443 for (j = nfit; j > 0; j--) {
444 if (isspace((unsigned char)p[j])) {
455 while (*p && isspace((unsigned char)*p))
461 SelectObject(hdc, oldfont);
462 ReleaseDC(cp->hwnd, hdc);
464 if (lines) *lines = nlines;
470 * A single standalone static text control.
472 void statictext(struct ctlpos *cp, char *text, int lines, int id)
479 r.bottom = STATICHEIGHT * lines;
480 cp->ypos += r.bottom + GAPBETWEEN;
481 doctl(cp, r, "STATIC",
482 WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP,
487 * An owner-drawn static text control for a panel title.
489 void paneltitle(struct ctlpos *cp, int id)
496 r.bottom = TITLEHEIGHT;
497 cp->ypos += r.bottom + GAPBETWEEN;
498 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
503 * A button on the right hand side, with a static to its left.
505 void staticbtn(struct ctlpos *cp, char *stext, int sid,
506 char *btext, int bid)
508 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
509 PUSHBTNHEIGHT : STATICHEIGHT);
511 int lwid, rwid, rpos;
513 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
514 lwid = rpos - 2 * GAPBETWEEN;
515 rwid = cp->width + GAPBETWEEN - rpos;
518 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
520 r.bottom = STATICHEIGHT;
521 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
524 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
526 r.bottom = PUSHBTNHEIGHT;
527 doctl(cp, r, "BUTTON",
528 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
531 cp->ypos += height + GAPBETWEEN;
535 * A simple push button.
537 void button(struct ctlpos *cp, char *btext, int bid, int defbtn)
544 r.bottom = PUSHBTNHEIGHT;
546 /* Q67655: the _dialog box_ must know which button is default
547 * as well as the button itself knowing */
548 if (defbtn && cp->hwnd)
549 SendMessage(cp->hwnd, DM_SETDEFID, bid, 0);
551 doctl(cp, r, "BUTTON",
552 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
553 (defbtn ? BS_DEFPUSHBUTTON : 0) | BS_PUSHBUTTON,
556 cp->ypos += PUSHBTNHEIGHT + GAPBETWEEN;
560 * Like staticbtn, but two buttons.
562 void static2btn(struct ctlpos *cp, char *stext, int sid,
563 char *btext1, int bid1, char *btext2, int bid2)
565 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
566 PUSHBTNHEIGHT : STATICHEIGHT);
568 int lwid, rwid1, rwid2, rpos1, rpos2;
570 rpos1 = GAPBETWEEN + (cp->width + GAPBETWEEN) / 2;
571 rpos2 = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
572 lwid = rpos1 - 2 * GAPBETWEEN;
573 rwid1 = rpos2 - rpos1 - GAPBETWEEN;
574 rwid2 = cp->width + GAPBETWEEN - rpos2;
577 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
579 r.bottom = STATICHEIGHT;
580 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
583 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
585 r.bottom = PUSHBTNHEIGHT;
586 doctl(cp, r, "BUTTON",
587 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
591 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
593 r.bottom = PUSHBTNHEIGHT;
594 doctl(cp, r, "BUTTON",
595 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
598 cp->ypos += height + GAPBETWEEN;
602 * An edit control on the right hand side, with a static to its left.
604 static void staticedit_internal(struct ctlpos *cp, char *stext,
605 int sid, int eid, int percentedit,
608 const int height = (EDITHEIGHT > STATICHEIGHT ?
609 EDITHEIGHT : STATICHEIGHT);
611 int lwid, rwid, rpos;
614 GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
615 lwid = rpos - 2 * GAPBETWEEN;
616 rwid = cp->width + GAPBETWEEN - rpos;
619 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
621 r.bottom = STATICHEIGHT;
622 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
625 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
627 r.bottom = EDITHEIGHT;
629 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
630 WS_EX_CLIENTEDGE, "", eid);
632 cp->ypos += height + GAPBETWEEN;
635 void staticedit(struct ctlpos *cp, char *stext,
636 int sid, int eid, int percentedit)
638 staticedit_internal(cp, stext, sid, eid, percentedit, 0);
641 void staticpassedit(struct ctlpos *cp, char *stext,
642 int sid, int eid, int percentedit)
644 staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
648 * A drop-down list box on the right hand side, with a static to
651 void staticddl(struct ctlpos *cp, char *stext,
652 int sid, int lid, int percentlist)
654 const int height = (COMBOHEIGHT > STATICHEIGHT ?
655 COMBOHEIGHT : STATICHEIGHT);
657 int lwid, rwid, rpos;
660 GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
661 lwid = rpos - 2 * GAPBETWEEN;
662 rwid = cp->width + GAPBETWEEN - rpos;
665 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
667 r.bottom = STATICHEIGHT;
668 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
671 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
673 r.bottom = COMBOHEIGHT*4;
674 doctl(cp, r, "COMBOBOX",
675 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
676 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
678 cp->ypos += height + GAPBETWEEN;
682 * A combo box on the right hand side, with a static to its left.
684 void staticcombo(struct ctlpos *cp, char *stext,
685 int sid, int lid, int percentlist)
687 const int height = (COMBOHEIGHT > STATICHEIGHT ?
688 COMBOHEIGHT : STATICHEIGHT);
690 int lwid, rwid, rpos;
693 GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
694 lwid = rpos - 2 * GAPBETWEEN;
695 rwid = cp->width + GAPBETWEEN - rpos;
698 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
700 r.bottom = STATICHEIGHT;
701 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
704 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
706 r.bottom = COMBOHEIGHT*10;
707 doctl(cp, r, "COMBOBOX",
708 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
709 CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
711 cp->ypos += height + GAPBETWEEN;
715 * A static, with a full-width drop-down list box below it.
717 void staticddlbig(struct ctlpos *cp, char *stext,
725 r.bottom = STATICHEIGHT;
726 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
727 cp->ypos += STATICHEIGHT;
732 r.bottom = COMBOHEIGHT*4;
733 doctl(cp, r, "COMBOBOX",
734 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
735 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
736 cp->ypos += COMBOHEIGHT + GAPBETWEEN;
740 * A big multiline edit control with a static labelling it.
742 void bigeditctrl(struct ctlpos *cp, char *stext,
743 int sid, int eid, int lines)
750 r.bottom = STATICHEIGHT;
751 cp->ypos += r.bottom + GAPWITHIN;
752 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
757 r.bottom = EDITHEIGHT + (lines - 1) * STATICHEIGHT;
758 cp->ypos += r.bottom + GAPBETWEEN;
760 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
761 WS_EX_CLIENTEDGE, "", eid);
765 * A list box with a static labelling it.
767 void listbox(struct ctlpos *cp, char *stext,
768 int sid, int lid, int lines, int multi)
776 r.bottom = STATICHEIGHT;
777 cp->ypos += r.bottom + GAPWITHIN;
778 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
784 r.bottom = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
785 cp->ypos += r.bottom + GAPBETWEEN;
786 doctl(cp, r, "LISTBOX",
787 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
788 LBS_NOTIFY | LBS_HASSTRINGS | LBS_USETABSTOPS |
789 (multi ? LBS_MULTIPLESEL : 0),
790 WS_EX_CLIENTEDGE, "", lid);
794 * A tab-control substitute when a real tab control is unavailable.
796 void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id)
798 const int height = (COMBOHEIGHT > STATICHEIGHT ?
799 COMBOHEIGHT : STATICHEIGHT);
801 int bigwid, lwid, rwid, rpos;
802 static const int BIGGAP = 15;
803 static const int MEDGAP = 3;
805 bigwid = cp->width + 2 * GAPBETWEEN - 2 * BIGGAP;
807 rpos = BIGGAP + (bigwid + BIGGAP) / 2;
808 lwid = rpos - 2 * BIGGAP;
809 rwid = bigwid + BIGGAP - rpos;
812 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
814 r.bottom = STATICHEIGHT;
815 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
818 r.top = cp->ypos + (height - COMBOHEIGHT) / 2;
820 r.bottom = COMBOHEIGHT * 10;
821 doctl(cp, r, "COMBOBOX",
822 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
823 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
825 cp->ypos += height + MEDGAP + GAPBETWEEN;
831 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
836 * A static line, followed by an edit control on the left hand side
837 * and a button on the right.
839 void editbutton(struct ctlpos *cp, char *stext, int sid,
840 int eid, char *btext, int bid)
842 const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
843 EDITHEIGHT : PUSHBTNHEIGHT);
845 int lwid, rwid, rpos;
850 r.bottom = STATICHEIGHT;
851 cp->ypos += r.bottom + GAPWITHIN;
852 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
854 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
855 lwid = rpos - 2 * GAPBETWEEN;
856 rwid = cp->width + GAPBETWEEN - rpos;
859 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
861 r.bottom = EDITHEIGHT;
863 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
864 WS_EX_CLIENTEDGE, "", eid);
867 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
869 r.bottom = PUSHBTNHEIGHT;
870 doctl(cp, r, "BUTTON",
871 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
874 cp->ypos += height + GAPBETWEEN;
878 * A special control for manipulating an ordered preference list
879 * (eg. for cipher selection).
880 * XXX: this is a rough hack and could be improved.
882 void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
883 char *stext, int sid, int listid, int upbid, int dnbid)
885 const static int percents[] = { 5, 75, 20 };
887 int xpos, percent = 0, i;
888 int listheight = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
889 const int BTNSHEIGHT = 2*PUSHBTNHEIGHT + GAPBETWEEN;
890 int totalheight, buttonpos;
892 /* Squirrel away IDs. */
893 hdl->listid = listid;
897 /* The static label. */
902 r.bottom = STATICHEIGHT;
903 cp->ypos += r.bottom + GAPWITHIN;
904 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
907 if (listheight > BTNSHEIGHT) {
908 totalheight = listheight;
909 buttonpos = (listheight - BTNSHEIGHT) / 2;
911 totalheight = BTNSHEIGHT;
915 for (i=0; i<3; i++) {
917 xpos = (cp->width + GAPBETWEEN) * percent / 100;
918 left = xpos + GAPBETWEEN;
919 percent += percents[i];
920 xpos = (cp->width + GAPBETWEEN) * percent / 100;
925 /* The drag list box. */
926 r.left = left; r.right = wid;
927 r.top = cp->ypos; r.bottom = listheight;
930 ctl = doctl(cp, r, "LISTBOX",
931 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
932 WS_VSCROLL | LBS_HASSTRINGS | LBS_USETABSTOPS,
940 /* The "Up" and "Down" buttons. */
941 /* XXX worry about accelerators if we have more than one
942 * prefslist on a panel */
943 r.left = left; r.right = wid;
944 r.top = cp->ypos + buttonpos; r.bottom = PUSHBTNHEIGHT;
945 doctl(cp, r, "BUTTON",
946 BS_NOTIFY | WS_CHILD | WS_VISIBLE |
947 WS_TABSTOP | BS_PUSHBUTTON,
950 r.left = left; r.right = wid;
951 r.top = cp->ypos + buttonpos + PUSHBTNHEIGHT + GAPBETWEEN;
952 r.bottom = PUSHBTNHEIGHT;
953 doctl(cp, r, "BUTTON",
954 BS_NOTIFY | WS_CHILD | WS_VISIBLE |
955 WS_TABSTOP | BS_PUSHBUTTON,
963 cp->ypos += totalheight + GAPBETWEEN;
968 * Helper function for prefslist: move item in list box.
970 static void pl_moveitem(HWND hwnd, int listid, int src, int dst)
974 /* Get the item's data. */
975 tlen = SendDlgItemMessage (hwnd, listid, LB_GETTEXTLEN, src, 0);
976 txt = smalloc(tlen+1);
977 SendDlgItemMessage (hwnd, listid, LB_GETTEXT, src, (LPARAM) txt);
978 val = SendDlgItemMessage (hwnd, listid, LB_GETITEMDATA, src, 0);
979 /* Deselect old location. */
980 SendDlgItemMessage (hwnd, listid, LB_SETSEL, FALSE, src);
981 /* Delete it at the old location. */
982 SendDlgItemMessage (hwnd, listid, LB_DELETESTRING, src, 0);
983 /* Insert it at new location. */
984 SendDlgItemMessage (hwnd, listid, LB_INSERTSTRING, dst,
986 SendDlgItemMessage (hwnd, listid, LB_SETITEMDATA, dst,
989 SendDlgItemMessage (hwnd, listid, LB_SETCURSEL, dst, 0);
993 int pl_itemfrompt(HWND hwnd, POINT cursor, BOOL scroll)
996 POINT uppoint, downpoint;
997 int updist, downdist, upitem, downitem, i;
1000 * Ghastly hackery to try to figure out not which
1001 * _item_, but which _gap between items_, the user
1002 * is pointing at. We do this by first working out
1003 * which list item is under the cursor, and then
1004 * working out how far the cursor would have to
1005 * move up or down before the answer was different.
1006 * Then we put the insertion point _above_ the
1007 * current item if the upper edge is closer than
1008 * the lower edge, or _below_ it if vice versa.
1010 ret = LBItemFromPt(hwnd, cursor, scroll);
1013 ret = LBItemFromPt(hwnd, cursor, FALSE);
1014 updist = downdist = 0;
1015 for (i = 1; i < 4096 && (!updist || !downdist); i++) {
1016 uppoint = downpoint = cursor;
1019 upitem = LBItemFromPt(hwnd, uppoint, FALSE);
1020 downitem = LBItemFromPt(hwnd, downpoint, FALSE);
1021 if (!updist && upitem != ret)
1023 if (!downdist && downitem != ret)
1026 if (downdist < updist)
1032 * Handler for prefslist above.
1034 * Return value has bit 0 set if the dialog box procedure needs to
1035 * return TRUE from handling this message; it has bit 1 set if a
1036 * change may have been made in the contents of the list.
1038 int handle_prefslist(struct prefslist *hdl,
1039 int *array, int maxmemb,
1040 int is_dlmsg, HWND hwnd,
1041 WPARAM wParam, LPARAM lParam)
1048 if ((int)wParam == hdl->listid) {
1049 DRAGLISTINFO *dlm = (DRAGLISTINFO *)lParam;
1050 int dest = 0; /* initialise to placate gcc */
1051 switch (dlm->uNotification) {
1054 SendDlgItemMessage(hwnd, hdl->listid,
1055 LB_ADDSTRING, 0, (LPARAM) "");
1057 hdl->srcitem = LBItemFromPt(dlm->hWnd, dlm->ptCursor, TRUE);
1059 /* XXX hack Q183115 */
1060 SetWindowLong(hwnd, DWL_MSGRESULT, TRUE);
1063 DrawInsert(hwnd, dlm->hWnd, -1); /* Clear arrow */
1064 SendDlgItemMessage(hwnd, hdl->listid,
1065 LB_DELETESTRING, hdl->dummyitem, 0);
1070 dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, TRUE);
1071 if (dest > hdl->dummyitem) dest = hdl->dummyitem;
1072 DrawInsert (hwnd, dlm->hWnd, dest);
1074 SetWindowLong(hwnd, DWL_MSGRESULT, DL_MOVECURSOR);
1076 SetWindowLong(hwnd, DWL_MSGRESULT, DL_STOPCURSOR);
1079 if (hdl->dragging) {
1080 dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, TRUE);
1081 if (dest > hdl->dummyitem) dest = hdl->dummyitem;
1082 DrawInsert (hwnd, dlm->hWnd, -1);
1084 SendDlgItemMessage(hwnd, hdl->listid,
1085 LB_DELETESTRING, hdl->dummyitem, 0);
1086 if (hdl->dragging) {
1089 /* Correct for "missing" item. */
1090 if (dest > hdl->srcitem) dest--;
1091 pl_moveitem(hwnd, hdl->listid, hdl->srcitem, dest);
1101 if (((LOWORD(wParam) == hdl->upbid) ||
1102 (LOWORD(wParam) == hdl->dnbid)) &&
1103 ((HIWORD(wParam) == BN_CLICKED) ||
1104 (HIWORD(wParam) == BN_DOUBLECLICKED))) {
1105 /* Move an item up or down the list. */
1106 /* Get the current selection, if any. */
1107 int selection = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCURSEL, 0, 0);
1108 if (selection == LB_ERR) {
1112 /* Get the total number of items. */
1113 nitems = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCOUNT, 0, 0);
1114 /* Should we do anything? */
1115 if (LOWORD(wParam) == hdl->upbid && (selection > 0))
1116 pl_moveitem(hwnd, hdl->listid, selection, selection - 1);
1117 else if (LOWORD(wParam) == hdl->dnbid && (selection < nitems - 1))
1118 pl_moveitem(hwnd, hdl->listid, selection, selection + 1);
1127 /* Update array to match the list box. */
1128 for (i=0; i < maxmemb; i++)
1129 array[i] = SendDlgItemMessage (hwnd, hdl->listid, LB_GETITEMDATA,
1137 * A progress bar (from Common Controls). We like our progress bars
1138 * to be smooth and unbroken, without those ugly divisions; some
1139 * older compilers may not support that, but that's life.
1141 void progressbar(struct ctlpos *cp, int id)
1145 r.left = GAPBETWEEN;
1147 r.right = cp->width;
1148 r.bottom = PROGBARHEIGHT;
1149 cp->ypos += r.bottom + GAPBETWEEN;
1151 doctl(cp, r, PROGRESS_CLASS, WS_CHILD | WS_VISIBLE
1155 , WS_EX_CLIENTEDGE, "", id);
1158 /* ----------------------------------------------------------------------
1159 * Platform-specific side of portable dialog-box mechanism.
1163 * This function takes a string, escapes all the ampersands, and
1164 * places a single (unescaped) ampersand in front of the first
1165 * occurrence of the given shortcut character (which may be
1168 * Return value is a malloc'ed copy of the processed version of the
1171 static char *shortcut_escape(char *text, char shortcut)
1177 return NULL; /* sfree won't choke on this */
1179 ret = smalloc(2*strlen(text)+1); /* size potentially doubles! */
1180 shortcut = tolower((unsigned char)shortcut);
1185 if (shortcut != NO_SHORTCUT &&
1186 tolower((unsigned char)*p) == shortcut) {
1188 shortcut = NO_SHORTCUT; /* stop it happening twice */
1189 } else if (*p == '&') {
1198 void winctrl_add_shortcuts(struct dlgparam *dp, struct winctrl *c)
1201 for (i = 0; i < lenof(c->shortcuts); i++)
1202 if (c->shortcuts[i] != NO_SHORTCUT) {
1203 unsigned char s = tolower((unsigned char)c->shortcuts[i]);
1204 assert(!dp->shortcuts[s]);
1205 dp->shortcuts[s] = TRUE;
1209 void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c)
1212 for (i = 0; i < lenof(c->shortcuts); i++)
1213 if (c->shortcuts[i] != NO_SHORTCUT) {
1214 unsigned char s = tolower((unsigned char)c->shortcuts[i]);
1215 assert(dp->shortcuts[s]);
1216 dp->shortcuts[s] = FALSE;
1220 static int winctrl_cmp_byctrl(void *av, void *bv)
1222 struct winctrl *a = (struct winctrl *)av;
1223 struct winctrl *b = (struct winctrl *)bv;
1224 if (a->ctrl < b->ctrl)
1226 else if (a->ctrl > b->ctrl)
1231 static int winctrl_cmp_byid(void *av, void *bv)
1233 struct winctrl *a = (struct winctrl *)av;
1234 struct winctrl *b = (struct winctrl *)bv;
1235 if (a->base_id < b->base_id)
1237 else if (a->base_id > b->base_id)
1242 static int winctrl_cmp_byctrl_find(void *av, void *bv)
1244 union control *a = (union control *)av;
1245 struct winctrl *b = (struct winctrl *)bv;
1248 else if (a > b->ctrl)
1253 static int winctrl_cmp_byid_find(void *av, void *bv)
1256 struct winctrl *b = (struct winctrl *)bv;
1257 if (*a < b->base_id)
1259 else if (*a >= b->base_id + b->num_ids)
1265 void winctrl_init(struct winctrls *wc)
1267 wc->byctrl = newtree234(winctrl_cmp_byctrl);
1268 wc->byid = newtree234(winctrl_cmp_byid);
1270 void winctrl_cleanup(struct winctrls *wc)
1274 while ((c = index234(wc->byid, 0)) != NULL) {
1275 winctrl_remove(wc, c);
1280 freetree234(wc->byctrl);
1281 freetree234(wc->byid);
1282 wc->byctrl = wc->byid = NULL;
1285 void winctrl_add(struct winctrls *wc, struct winctrl *c)
1287 struct winctrl *ret;
1289 ret = add234(wc->byctrl, c);
1292 ret = add234(wc->byid, c);
1296 void winctrl_remove(struct winctrls *wc, struct winctrl *c)
1298 struct winctrl *ret;
1299 ret = del234(wc->byctrl, c);
1300 ret = del234(wc->byid, c);
1304 struct winctrl *winctrl_findbyctrl(struct winctrls *wc, union control *ctrl)
1306 return find234(wc->byctrl, ctrl, winctrl_cmp_byctrl_find);
1309 struct winctrl *winctrl_findbyid(struct winctrls *wc, int id)
1311 return find234(wc->byid, &id, winctrl_cmp_byid_find);
1314 struct winctrl *winctrl_findbyindex(struct winctrls *wc, int index)
1316 return index234(wc->byid, index);
1319 void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
1320 struct ctlpos *cp, struct controlset *s, int *id)
1322 struct ctlpos columns[16];
1323 int ncols, colstart, colspan;
1325 struct ctlpos tabdelays[16];
1326 union control *tabdelayed[16];
1331 char shortcuts[MAX_SHORTCUTS_PER_CTRL];
1334 int i, base_id, num_ids;
1339 /* Start a containing box, if we have a boxname. */
1340 if (s->boxname && *s->boxname) {
1341 struct winctrl *c = smalloc(sizeof(struct winctrl));
1343 c->base_id = base_id;
1346 memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
1348 beginbox(cp, s->boxtitle, base_id);
1352 /* Draw a title, if we have one. */
1353 if (!s->boxname && s->boxtitle) {
1354 struct winctrl *c = smalloc(sizeof(struct winctrl));
1356 c->base_id = base_id;
1358 c->data = dupstr(s->boxtitle);
1359 memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
1361 paneltitle(cp, base_id);
1365 /* Initially we have just one column. */
1367 columns[0] = *cp; /* structure copy */
1369 /* And initially, there are no pending tab-delayed controls. */
1372 /* Loop over each control in the controlset. */
1373 for (i = 0; i < s->ncontrols; i++) {
1374 union control *ctrl = s->ctrls[i];
1377 * Generic processing that pertains to all control types.
1378 * At the end of this if statement, we'll have produced
1379 * `ctrl' (a pointer to the control we have to create, or
1380 * think about creating, in this iteration of the loop),
1381 * `pos' (a suitable ctlpos with which to position it), and
1382 * `c' (a winctrl structure to receive details of the
1383 * dialog IDs). Or we'll have done a `continue', if it was
1384 * CTRL_COLUMNS and doesn't require any control creation at
1387 if (ctrl->generic.type == CTRL_COLUMNS) {
1388 assert((ctrl->columns.ncols == 1) ^ (ncols == 1));
1392 * We're splitting into multiple columns.
1394 int lpercent, rpercent, lx, rx, i;
1396 ncols = ctrl->columns.ncols;
1397 assert(ncols <= lenof(columns));
1398 for (i = 1; i < ncols; i++)
1399 columns[i] = columns[0]; /* structure copy */
1402 for (i = 0; i < ncols; i++) {
1403 rpercent = lpercent + ctrl->columns.percentages[i];
1404 lx = columns[i].xoff + lpercent *
1405 (columns[i].width + GAPBETWEEN) / 100;
1406 rx = columns[i].xoff + rpercent *
1407 (columns[i].width + GAPBETWEEN) / 100;
1408 columns[i].xoff = lx;
1409 columns[i].width = rx - lx - GAPBETWEEN;
1410 lpercent = rpercent;
1414 * We're recombining the various columns into one.
1416 int maxy = columns[0].ypos;
1418 for (i = 1; i < ncols; i++)
1419 if (maxy < columns[i].ypos)
1420 maxy = columns[i].ypos;
1422 columns[0] = *cp; /* structure copy */
1423 columns[0].ypos = maxy;
1427 } else if (ctrl->generic.type == CTRL_TABDELAY) {
1430 assert(!ctrl->generic.tabdelay);
1431 ctrl = ctrl->tabdelay.ctrl;
1433 for (i = 0; i < ntabdelays; i++)
1434 if (tabdelayed[i] == ctrl)
1436 assert(i < ntabdelays); /* we have to have found it */
1438 pos = tabdelays[i]; /* structure copy */
1440 colstart = colspan = -1; /* indicate this was tab-delayed */
1444 * If it wasn't one of those, it's a genuine control;
1445 * so we'll have to compute a position for it now, by
1446 * checking its column span.
1450 colstart = COLUMN_START(ctrl->generic.column);
1451 colspan = COLUMN_SPAN(ctrl->generic.column);
1453 pos = columns[colstart]; /* structure copy */
1454 pos.width = columns[colstart+colspan-1].width +
1455 (columns[colstart+colspan-1].xoff - columns[colstart].xoff);
1457 for (col = colstart; col < colstart+colspan; col++)
1458 if (pos.ypos < columns[col].ypos)
1459 pos.ypos = columns[col].ypos;
1462 * If this control is to be tabdelayed, add it to the
1463 * tabdelay list, and unset pos.hwnd to inhibit actual
1466 if (ctrl->generic.tabdelay) {
1467 assert(ntabdelays < lenof(tabdelays));
1468 tabdelays[ntabdelays] = pos; /* structure copy */
1469 tabdelayed[ntabdelays] = ctrl;
1475 /* Most controls don't need anything in c->data. */
1478 /* And they all start off with no shortcuts registered. */
1479 memset(shortcuts, NO_SHORTCUT, lenof(shortcuts));
1483 * Now we're ready to actually create the control, by
1484 * switching on its type.
1486 switch (ctrl->generic.type) {
1489 char *wrapped, *escaped;
1492 wrapped = staticwrap(&pos, cp->hwnd,
1493 ctrl->generic.label, &lines);
1494 escaped = shortcut_escape(wrapped, NO_SHORTCUT);
1495 statictext(&pos, escaped, lines, base_id);
1501 num_ids = 2; /* static, edit */
1502 escaped = shortcut_escape(ctrl->editbox.label,
1503 ctrl->editbox.shortcut);
1504 shortcuts[nshortcuts++] = ctrl->editbox.shortcut;
1505 if (ctrl->editbox.percentwidth == 100) {
1506 if (ctrl->editbox.has_list)
1507 combobox(&pos, escaped,
1508 base_id, base_id+1);
1510 multiedit(&pos, ctrl->editbox.password, escaped,
1511 base_id, base_id+1, 100, NULL);
1513 if (ctrl->editbox.has_list) {
1514 staticcombo(&pos, escaped, base_id, base_id+1,
1515 ctrl->editbox.percentwidth);
1517 (ctrl->editbox.password ? staticpassedit : staticedit)
1518 (&pos, escaped, base_id, base_id+1,
1519 ctrl->editbox.percentwidth);
1525 num_ids = ctrl->radio.nbuttons + 1; /* label as well */
1527 struct radio *buttons;
1530 escaped = shortcut_escape(ctrl->radio.label,
1531 ctrl->radio.shortcut);
1532 shortcuts[nshortcuts++] = ctrl->radio.shortcut;
1534 buttons = smalloc(ctrl->radio.nbuttons * sizeof(struct radio));
1536 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1538 shortcut_escape(ctrl->radio.buttons[i],
1539 (char)(ctrl->radio.shortcuts ?
1540 ctrl->radio.shortcuts[i] :
1542 buttons[i].id = base_id + 1 + i;
1543 if (ctrl->radio.shortcuts) {
1544 assert(nshortcuts < MAX_SHORTCUTS_PER_CTRL);
1545 shortcuts[nshortcuts++] = ctrl->radio.shortcuts[i];
1549 radioline_common(&pos, escaped, base_id,
1550 ctrl->radio.ncolumns,
1551 buttons, ctrl->radio.nbuttons);
1553 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1554 sfree(buttons[i].text);
1562 escaped = shortcut_escape(ctrl->checkbox.label,
1563 ctrl->checkbox.shortcut);
1564 shortcuts[nshortcuts++] = ctrl->checkbox.shortcut;
1565 checkbox(&pos, escaped, base_id);
1569 escaped = shortcut_escape(ctrl->button.label,
1570 ctrl->button.shortcut);
1571 shortcuts[nshortcuts++] = ctrl->button.shortcut;
1573 button(&pos, escaped, base_id, ctrl->button.isdefault);
1578 escaped = shortcut_escape(ctrl->listbox.label,
1579 ctrl->listbox.shortcut);
1580 shortcuts[nshortcuts++] = ctrl->listbox.shortcut;
1581 if (ctrl->listbox.draglist) {
1582 data = smalloc(sizeof(struct prefslist));
1584 prefslist(data, &pos, ctrl->listbox.height, escaped,
1585 base_id, base_id+1, base_id+2, base_id+3);
1586 shortcuts[nshortcuts++] = 'u'; /* Up */
1587 shortcuts[nshortcuts++] = 'd'; /* Down */
1588 } else if (ctrl->listbox.height == 0) {
1589 /* Drop-down list. */
1590 if (ctrl->listbox.percentwidth == 100) {
1591 staticddlbig(&pos, escaped,
1592 base_id, base_id+1);
1594 staticddl(&pos, escaped, base_id,
1595 base_id+1, ctrl->listbox.percentwidth);
1598 /* Ordinary list. */
1599 listbox(&pos, escaped, base_id, base_id+1,
1600 ctrl->listbox.height, ctrl->listbox.multisel);
1602 if (ctrl->listbox.ncols) {
1604 * This method of getting the box width is a bit of
1605 * a hack; we'd do better to try to retrieve the
1606 * actual width in dialog units from doctl() just
1607 * before MapDialogRect. But that's going to be no
1608 * fun, and this should be good enough accuracy.
1610 int width = cp->width * ctrl->listbox.percentwidth;
1614 tabarray = smalloc((ctrl->listbox.ncols-1) * sizeof(int));
1616 for (i = 0; i < ctrl->listbox.ncols-1; i++) {
1617 percent += ctrl->listbox.percentages[i];
1618 tabarray[i] = width * percent / 10000;
1620 SendDlgItemMessage(cp->hwnd, base_id+1, LB_SETTABSTOPS,
1621 ctrl->listbox.ncols-1, (LPARAM)tabarray);
1626 case CTRL_FILESELECT:
1628 escaped = shortcut_escape(ctrl->fileselect.label,
1629 ctrl->fileselect.shortcut);
1630 shortcuts[nshortcuts++] = ctrl->fileselect.shortcut;
1631 editbutton(&pos, escaped, base_id, base_id+1,
1632 "Bro&wse...", base_id+2);
1633 shortcuts[nshortcuts++] = 'w';
1636 case CTRL_FONTSELECT:
1638 escaped = shortcut_escape(ctrl->fontselect.label,
1639 ctrl->fontselect.shortcut);
1640 shortcuts[nshortcuts++] = ctrl->fontselect.shortcut;
1641 statictext(&pos, escaped, 1, base_id);
1642 staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
1644 data = smalloc(sizeof(FontSpec));
1647 assert(!"Can't happen");
1648 num_ids = 0; /* placate gcc */
1653 * Create a `struct winctrl' for this control, and advance
1654 * the dialog ID counter, if it's actually been created
1655 * (and isn't tabdelayed).
1658 struct winctrl *c = smalloc(sizeof(struct winctrl));
1661 c->base_id = base_id;
1662 c->num_ids = num_ids;
1664 memcpy(c->shortcuts, shortcuts, sizeof(shortcuts));
1666 winctrl_add_shortcuts(dp, c);
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(void *dlg)
1710 struct dlgparam *dp = (struct dlgparam *)dlg;
1711 return dp->lastfocused;
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 = smalloc(len+1);
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);
1897 (msg == WM_COMMAND &&
1898 (HIWORD(wParam) == BN_CLICKED ||
1899 HIWORD(wParam) == BN_DOUBLECLICKED))) {
1901 char filename[FILENAME_MAX];
1904 memset(&of, 0, sizeof(of));
1905 #ifdef OPENFILENAME_SIZE_VERSION_400
1906 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
1908 of.lStructSize = 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.lpstrInitialDir = NULL;
1923 of.lpstrTitle = ctrl->fileselect.title;
1925 if (ctrl->fileselect.for_writing)
1926 ret = GetSaveFileName(&of);
1928 ret = GetOpenFileName(&of);
1930 SetDlgItemText(dp->hwnd, c->base_id + 1, filename);
1931 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1935 case CTRL_FONTSELECT:
1936 if (msg == WM_COMMAND && id == 2 &&
1937 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1938 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1940 (msg == WM_COMMAND &&
1941 (HIWORD(wParam) == BN_CLICKED ||
1942 HIWORD(wParam) == BN_DOUBLECLICKED))) {
1946 FontSpec fs = *(FontSpec *)c->data;
1949 lf.lfHeight = -MulDiv(fs.height,
1950 GetDeviceCaps(hdc, LOGPIXELSY), 72);
1952 lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
1953 lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
1954 lf.lfWeight = (fs.isbold ? FW_BOLD : 0);
1955 lf.lfCharSet = fs.charset;
1956 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
1957 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1958 lf.lfQuality = DEFAULT_QUALITY;
1959 lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
1960 strncpy(lf.lfFaceName, fs.name,
1961 sizeof(lf.lfFaceName) - 1);
1962 lf.lfFaceName[sizeof(lf.lfFaceName) - 1] = '\0';
1964 cf.lStructSize = sizeof(cf);
1965 cf.hwndOwner = dp->hwnd;
1967 cf.Flags = CF_FIXEDPITCHONLY | CF_FORCEFONTEXIST |
1968 CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
1970 if (ChooseFont(&cf)) {
1971 strncpy(fs.name, lf.lfFaceName,
1972 sizeof(fs.name) - 1);
1973 fs.name[sizeof(fs.name) - 1] = '\0';
1974 fs.isbold = (lf.lfWeight == FW_BOLD);
1975 fs.charset = lf.lfCharSet;
1976 fs.height = cf.iPointSize / 10;
1977 dlg_fontsel_set(ctrl, dp, fs);
1978 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1985 * If the above event handler has asked for a colour selector,
1986 * now is the time to generate one.
1988 if (dp->coloursel_wanted) {
1989 static CHOOSECOLOR cc;
1990 static DWORD custom[16] = { 0 }; /* zero initialisers */
1991 cc.lStructSize = sizeof(cc);
1992 cc.hwndOwner = dp->hwnd;
1993 cc.hInstance = (HWND) hinst;
1994 cc.lpCustColors = custom;
1995 cc.rgbResult = RGB(dp->coloursel_result.r,
1996 dp->coloursel_result.g,
1997 dp->coloursel_result.b);
1998 cc.Flags = CC_FULLOPEN | CC_RGBINIT;
1999 if (ChooseColor(&cc)) {
2000 dp->coloursel_result.r =
2001 (unsigned char) (cc.rgbResult & 0xFF);
2002 dp->coloursel_result.g =
2003 (unsigned char) (cc.rgbResult >> 8) & 0xFF;
2004 dp->coloursel_result.b =
2005 (unsigned char) (cc.rgbResult >> 16) & 0xFF;
2006 dp->coloursel_result.ok = TRUE;
2008 dp->coloursel_result.ok = FALSE;
2009 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_CALLBACK);
2016 * This function can be called to produce context help on a
2017 * control. Returns TRUE if it has actually launched WinHelp.
2019 int winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id)
2026 * Look up the control ID in our data.
2029 for (i = 0; i < dp->nctrltrees; i++) {
2030 c = winctrl_findbyid(dp->controltrees[i], id);
2035 return 0; /* we have nothing to do */
2038 * This is the Windows front end, so we're allowed to assume
2039 * `helpctx.p' is a context string.
2041 if (!c->ctrl || !c->ctrl->generic.helpctx.p)
2042 return 0; /* no help available for this ctrl */
2044 cmd = dupprintf("JI(`',`%s')", c->ctrl->generic.helpctx.p);
2045 WinHelp(hwnd, help_path, HELP_COMMAND, (DWORD)cmd);
2051 * Now the various functions that the platform-independent
2052 * mechanism can call to access the dialog box entries.
2055 static struct winctrl *dlg_findbyctrl(struct dlgparam *dp, union control *ctrl)
2059 for (i = 0; i < dp->nctrltrees; i++) {
2060 struct winctrl *c = winctrl_findbyctrl(dp->controltrees[i], ctrl);
2067 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
2069 struct dlgparam *dp = (struct dlgparam *)dlg;
2070 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2071 assert(c && c->ctrl->generic.type == CTRL_RADIO);
2072 CheckRadioButton(dp->hwnd,
2074 c->base_id + c->ctrl->radio.nbuttons,
2075 c->base_id + 1 + whichbutton);
2078 int dlg_radiobutton_get(union control *ctrl, void *dlg)
2080 struct dlgparam *dp = (struct dlgparam *)dlg;
2081 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2083 assert(c && c->ctrl->generic.type == CTRL_RADIO);
2084 for (i = 0; i < c->ctrl->radio.nbuttons; i++)
2085 if (IsDlgButtonChecked(dp->hwnd, c->base_id + 1 + i))
2087 assert(!"No radio button was checked?!");
2091 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
2093 struct dlgparam *dp = (struct dlgparam *)dlg;
2094 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2095 assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
2096 CheckDlgButton(dp->hwnd, c->base_id, (checked != 0));
2099 int dlg_checkbox_get(union control *ctrl, void *dlg)
2101 struct dlgparam *dp = (struct dlgparam *)dlg;
2102 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2103 assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
2104 return 0 != IsDlgButtonChecked(dp->hwnd, c->base_id);
2107 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
2109 struct dlgparam *dp = (struct dlgparam *)dlg;
2110 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2111 assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
2112 SetDlgItemText(dp->hwnd, c->base_id+1, text);
2115 void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
2117 struct dlgparam *dp = (struct dlgparam *)dlg;
2118 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2119 assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
2120 GetDlgItemText(dp->hwnd, c->base_id+1, buffer, length);
2121 buffer[length-1] = '\0';
2124 /* The `listbox' functions can also apply to combo boxes. */
2125 void dlg_listbox_clear(union control *ctrl, void *dlg)
2127 struct dlgparam *dp = (struct dlgparam *)dlg;
2128 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2131 (c->ctrl->generic.type == CTRL_LISTBOX ||
2132 (c->ctrl->generic.type == CTRL_EDITBOX &&
2133 c->ctrl->editbox.has_list)));
2134 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2135 LB_RESETCONTENT : CB_RESETCONTENT);
2136 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
2139 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
2141 struct dlgparam *dp = (struct dlgparam *)dlg;
2142 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2145 (c->ctrl->generic.type == CTRL_LISTBOX ||
2146 (c->ctrl->generic.type == CTRL_EDITBOX &&
2147 c->ctrl->editbox.has_list)));
2148 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2149 LB_DELETESTRING : CB_DELETESTRING);
2150 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2153 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
2155 struct dlgparam *dp = (struct dlgparam *)dlg;
2156 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2159 (c->ctrl->generic.type == CTRL_LISTBOX ||
2160 (c->ctrl->generic.type == CTRL_EDITBOX &&
2161 c->ctrl->editbox.has_list)));
2162 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2163 LB_ADDSTRING : CB_ADDSTRING);
2164 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
2168 * Each listbox entry may have a numeric id associated with it.
2169 * Note that some front ends only permit a string to be stored at
2170 * each position, which means that _if_ you put two identical
2171 * strings in any listbox then you MUST not assign them different
2172 * IDs and expect to get meaningful results back.
2174 void dlg_listbox_addwithindex(union control *ctrl, void *dlg,
2175 char const *text, int id)
2177 struct dlgparam *dp = (struct dlgparam *)dlg;
2178 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2179 int msg, msg2, index;
2181 (c->ctrl->generic.type == CTRL_LISTBOX ||
2182 (c->ctrl->generic.type == CTRL_EDITBOX &&
2183 c->ctrl->editbox.has_list)));
2184 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2185 LB_ADDSTRING : CB_ADDSTRING);
2186 msg2 = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2187 LB_SETITEMDATA : CB_SETITEMDATA);
2188 index = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
2189 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg2, index, (LPARAM)id);
2192 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
2194 struct dlgparam *dp = (struct dlgparam *)dlg;
2195 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2197 assert(c && c->ctrl->generic.type == CTRL_LISTBOX);
2198 msg = (c->ctrl->listbox.height != 0 ? LB_GETITEMDATA : CB_GETITEMDATA);
2200 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2203 /* dlg_listbox_index returns <0 if no single element is selected. */
2204 int dlg_listbox_index(union control *ctrl, void *dlg)
2206 struct dlgparam *dp = (struct dlgparam *)dlg;
2207 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2209 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2210 !c->ctrl->listbox.multisel);
2211 msg = (c->ctrl->listbox.height != 0 ? LB_GETCURSEL : CB_GETCURSEL);
2212 ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
2219 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
2221 struct dlgparam *dp = (struct dlgparam *)dlg;
2222 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2223 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2224 c->ctrl->listbox.multisel &&
2225 c->ctrl->listbox.height != 0);
2227 SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSEL, index, 0);
2230 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
2232 struct dlgparam *dp = (struct dlgparam *)dlg;
2233 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2235 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2236 !c->ctrl->listbox.multisel);
2237 msg = (c->ctrl->listbox.height != 0 ? LB_SETCURSEL : CB_SETCURSEL);
2238 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2241 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
2243 struct dlgparam *dp = (struct dlgparam *)dlg;
2244 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2245 assert(c && c->ctrl->generic.type == CTRL_TEXT);
2246 SetDlgItemText(dp->hwnd, c->base_id, text);
2249 void dlg_filesel_set(union control *ctrl, void *dlg, Filename fn)
2251 struct dlgparam *dp = (struct dlgparam *)dlg;
2252 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2253 assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
2254 SetDlgItemText(dp->hwnd, c->base_id+1, fn.path);
2257 void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
2259 struct dlgparam *dp = (struct dlgparam *)dlg;
2260 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2261 assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
2262 GetDlgItemText(dp->hwnd, c->base_id+1, fn->path, lenof(fn->path));
2263 fn->path[lenof(fn->path)-1] = '\0';
2266 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs)
2268 char *buf, *boldstr;
2269 struct dlgparam *dp = (struct dlgparam *)dlg;
2270 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2271 assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
2273 *(FontSpec *)c->data = fs; /* structure copy */
2275 boldstr = (fs.isbold ? "bold, " : "");
2277 buf = dupprintf("Font: %s, %sdefault height", fs.name, boldstr);
2279 buf = dupprintf("Font: %s, %s%d-point", fs.name, boldstr,
2280 (fs.height < 0 ? -fs.height : fs.height));
2281 SetDlgItemText(dp->hwnd, c->base_id+1, buf);
2285 void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs)
2287 struct dlgparam *dp = (struct dlgparam *)dlg;
2288 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2289 assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
2290 *fs = *(FontSpec *)c->data; /* structure copy */
2294 * Bracketing a large set of updates in these two functions will
2295 * cause the front end (if possible) to delay updating the screen
2296 * until it's all complete, thus avoiding flicker.
2298 void dlg_update_start(union control *ctrl, void *dlg)
2300 struct dlgparam *dp = (struct dlgparam *)dlg;
2301 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2302 if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
2303 SendDlgItemMessage(dp->hwnd, c->base_id+1, WM_SETREDRAW, FALSE, 0);
2307 void dlg_update_done(union control *ctrl, void *dlg)
2309 struct dlgparam *dp = (struct dlgparam *)dlg;
2310 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2311 if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
2312 HWND hw = GetDlgItem(dp->hwnd, c->base_id+1);
2313 SendMessage(hw, WM_SETREDRAW, TRUE, 0);
2314 InvalidateRect(hw, NULL, TRUE);
2318 void dlg_set_focus(union control *ctrl, void *dlg)
2320 struct dlgparam *dp = (struct dlgparam *)dlg;
2321 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2324 switch (ctrl->generic.type) {
2325 case CTRL_EDITBOX: id = c->base_id + 1; break;
2327 for (id = c->base_id + ctrl->radio.nbuttons; id > 1; id--)
2328 if (IsDlgButtonChecked(dp->hwnd, id))
2331 * In the theoretically-unlikely case that no button was
2332 * selected, id should come out of this as 1, which is a
2333 * reasonable enough choice.
2336 case CTRL_CHECKBOX: id = c->base_id; break;
2337 case CTRL_BUTTON: id = c->base_id; break;
2338 case CTRL_LISTBOX: id = c->base_id + 1; break;
2339 case CTRL_FILESELECT: id = c->base_id + 1; break;
2340 case CTRL_FONTSELECT: id = c->base_id + 2; break;
2341 default: id = c->base_id; break;
2343 ctl = GetDlgItem(dp->hwnd, id);
2348 * During event processing, you might well want to give an error
2349 * indication to the user. dlg_beep() is a quick and easy generic
2350 * error; dlg_error() puts up a message-box or equivalent.
2352 void dlg_beep(void *dlg)
2354 /* struct dlgparam *dp = (struct dlgparam *)dlg; */
2358 void dlg_error_msg(void *dlg, char *msg)
2360 struct dlgparam *dp = (struct dlgparam *)dlg;
2361 MessageBox(dp->hwnd, msg,
2362 dp->errtitle ? dp->errtitle : NULL,
2363 MB_OK | MB_ICONERROR);
2367 * This function signals to the front end that the dialog's
2368 * processing is completed, and passes an integer value (typically
2369 * a success status).
2371 void dlg_end(void *dlg, int value)
2373 struct dlgparam *dp = (struct dlgparam *)dlg;
2375 dp->endresult = value;
2378 void dlg_refresh(union control *ctrl, void *dlg)
2380 struct dlgparam *dp = (struct dlgparam *)dlg;
2386 * Send EVENT_REFRESH to absolutely everything.
2388 for (j = 0; j < dp->nctrltrees; j++) {
2390 (c = winctrl_findbyindex(dp->controltrees[j], i)) != NULL;
2392 if (c->ctrl && c->ctrl->generic.handler != NULL)
2393 c->ctrl->generic.handler(c->ctrl, dp,
2394 dp->data, EVENT_REFRESH);
2399 * Send EVENT_REFRESH to a specific control.
2401 if (ctrl->generic.handler != NULL)
2402 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
2406 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
2408 struct dlgparam *dp = (struct dlgparam *)dlg;
2409 dp->coloursel_wanted = TRUE;
2410 dp->coloursel_result.r = r;
2411 dp->coloursel_result.g = g;
2412 dp->coloursel_result.b = b;
2415 int dlg_coloursel_results(union control *ctrl, void *dlg,
2416 int *r, int *g, int *b)
2418 struct dlgparam *dp = (struct dlgparam *)dlg;
2419 if (dp->coloursel_result.ok) {
2420 *r = dp->coloursel_result.r;
2421 *g = dp->coloursel_result.g;
2422 *b = dp->coloursel_result.b;
2428 struct perctrl_privdata {
2429 union control *ctrl;
2434 static int perctrl_privdata_cmp(void *av, void *bv)
2436 struct perctrl_privdata *a = (struct perctrl_privdata *)av;
2437 struct perctrl_privdata *b = (struct perctrl_privdata *)bv;
2438 if (a->ctrl < b->ctrl)
2440 else if (a->ctrl > b->ctrl)
2445 void dp_init(struct dlgparam *dp)
2450 dp->focused = dp->lastfocused = NULL;
2451 memset(dp->shortcuts, 0, sizeof(dp->shortcuts));
2453 dp->errtitle = NULL;
2454 dp->privdata = newtree234(perctrl_privdata_cmp);
2457 void dp_add_tree(struct dlgparam *dp, struct winctrls *wc)
2459 assert(dp->nctrltrees < lenof(dp->controltrees));
2460 dp->controltrees[dp->nctrltrees++] = wc;
2463 void dp_cleanup(struct dlgparam *dp)
2465 struct perctrl_privdata *p;
2468 while ( (p = index234(dp->privdata, 0)) != NULL ) {
2469 del234(dp->privdata, p);
2474 freetree234(dp->privdata);
2475 dp->privdata = NULL;
2479 void *dlg_get_privdata(union control *ctrl, void *dlg)
2481 struct dlgparam *dp = (struct dlgparam *)dlg;
2482 struct perctrl_privdata tmp, *p;
2484 p = find234(dp->privdata, &tmp, NULL);
2491 void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
2493 struct dlgparam *dp = (struct dlgparam *)dlg;
2494 struct perctrl_privdata tmp, *p;
2496 p = find234(dp->privdata, &tmp, NULL);
2498 p = smalloc(sizeof(struct perctrl_privdata));
2500 p->needs_free = FALSE;
2501 add234(dp->privdata, p);
2506 void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
2508 struct dlgparam *dp = (struct dlgparam *)dlg;
2509 struct perctrl_privdata tmp, *p;
2511 p = find234(dp->privdata, &tmp, NULL);
2513 p = smalloc(sizeof(struct perctrl_privdata));
2515 p->needs_free = FALSE;
2516 add234(dp->privdata, p);
2518 assert(!p->needs_free);
2519 p->needs_free = TRUE;
2520 p->data = smalloc(size);