]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winctrls.c
Fix a few more clang-generated warnings.
[PuTTY.git] / windows / winctrls.c
1 /*
2  * winctrls.c: routines to self-manage the controls in a dialog
3  * box.
4  */
5
6 /*
7  * Possible TODO in new cross-platform config box stuff:
8  *
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'
13  *    button.
14  */
15
16 #include <assert.h>
17 #include <ctype.h>
18
19 #include "putty.h"
20 #include "misc.h"
21 #include "dialog.h"
22
23 #include <commctrl.h>
24
25 #define GAPBETWEEN 3
26 #define GAPWITHIN 1
27 #define GAPXBOX 7
28 #define GAPYBOX 4
29 #define DLGWIDTH 168
30 #define STATICHEIGHT 8
31 #define TITLEHEIGHT 12
32 #define CHECKBOXHEIGHT 8
33 #define RADIOHEIGHT 8
34 #define EDITHEIGHT 12
35 #define LISTHEIGHT 11
36 #define LISTINCREMENT 8
37 #define COMBOHEIGHT 12
38 #define PUSHBTNHEIGHT 14
39 #define PROGBARHEIGHT 14
40
41 void ctlposinit(struct ctlpos *cp, HWND hwnd,
42                 int leftborder, int rightborder, int topborder)
43 {
44     RECT r, r2;
45     cp->hwnd = hwnd;
46     cp->font = SendMessage(hwnd, WM_GETFONT, 0, 0);
47     cp->ypos = topborder;
48     GetClientRect(hwnd, &r);
49     r2.left = r2.top = 0;
50     r2.right = 4;
51     r2.bottom = 8;
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;
57 }
58
59 HWND doctl(struct ctlpos *cp, RECT r,
60            char *wclass, int wstyle, int exstyle, char *wtext, int wid)
61 {
62     HWND ctl;
63     /*
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.
67      */
68
69     r.left += cp->xoff;
70     MapDialogRect(cp->hwnd, &r);
71
72     /*
73      * We can pass in cp->hwnd == NULL, to indicate a dry run
74      * without creating any actual controls.
75      */
76     if (cp->hwnd) {
77         ctl = CreateWindowEx(exstyle, wclass, wtext, wstyle,
78                              r.left, r.top, r.right, r.bottom,
79                              cp->hwnd, (HMENU)(ULONG_PTR)wid, hinst, NULL);
80         SendMessage(ctl, WM_SETFONT, cp->font, MAKELPARAM(TRUE, 0));
81
82         if (!strcmp(wclass, "LISTBOX")) {
83             /*
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.
90              */
91             SetWindowPos(ctl, NULL, 0, 0, r.right, r.bottom,
92                          SWP_NOACTIVATE | SWP_NOCOPYBITS |
93                          SWP_NOMOVE | SWP_NOZORDER);
94         }
95     } else
96         ctl = NULL;
97     return ctl;
98 }
99
100 /*
101  * A title bar across the top of a sub-dialog.
102  */
103 void bartitle(struct ctlpos *cp, char *name, int id)
104 {
105     RECT r;
106
107     r.left = GAPBETWEEN;
108     r.right = cp->width;
109     r.top = cp->ypos;
110     r.bottom = STATICHEIGHT;
111     cp->ypos += r.bottom + GAPBETWEEN;
112     doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, name, id);
113 }
114
115 /*
116  * Begin a grouping box, with or without a group title.
117  */
118 void beginbox(struct ctlpos *cp, char *name, int idbox)
119 {
120     cp->boxystart = cp->ypos;
121     if (!name)
122         cp->boxystart -= STATICHEIGHT / 2;
123     if (name)
124         cp->ypos += STATICHEIGHT;
125     cp->ypos += GAPYBOX;
126     cp->width -= 2 * GAPXBOX;
127     cp->xoff += GAPXBOX;
128     cp->boxid = idbox;
129     cp->boxtext = name;
130 }
131
132 /*
133  * End a grouping box.
134  */
135 void endbox(struct ctlpos *cp)
136 {
137     RECT r;
138     cp->xoff -= GAPXBOX;
139     cp->width += 2 * GAPXBOX;
140     cp->ypos += GAPYBOX - GAPBETWEEN;
141     r.left = GAPBETWEEN;
142     r.right = cp->width;
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);
147     cp->ypos += GAPYBOX;
148 }
149
150 /*
151  * A static line, followed by a full-width edit box.
152  */
153 void editboxfw(struct ctlpos *cp, int password, char *text,
154                int staticid, int editid)
155 {
156     RECT r;
157
158     r.left = GAPBETWEEN;
159     r.right = cp->width;
160
161     if (text) {
162         r.top = cp->ypos;
163         r.bottom = STATICHEIGHT;
164         doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
165         cp->ypos += STATICHEIGHT + GAPWITHIN;
166     }
167     r.top = cp->ypos;
168     r.bottom = EDITHEIGHT;
169     doctl(cp, r, "EDIT",
170           WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL |
171           (password ? ES_PASSWORD : 0),
172           WS_EX_CLIENTEDGE, "", editid);
173     cp->ypos += EDITHEIGHT + GAPBETWEEN;
174 }
175
176 /*
177  * A static line, followed by a full-width combo box.
178  */
179 void combobox(struct ctlpos *cp, char *text, int staticid, int listid)
180 {
181     RECT r;
182
183     r.left = GAPBETWEEN;
184     r.right = cp->width;
185
186     if (text) {
187         r.top = cp->ypos;
188         r.bottom = STATICHEIGHT;
189         doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
190         cp->ypos += STATICHEIGHT + GAPWITHIN;
191     }
192     r.top = cp->ypos;
193     r.bottom = COMBOHEIGHT * 10;
194     doctl(cp, r, "COMBOBOX",
195           WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
196           CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
197     cp->ypos += COMBOHEIGHT + GAPBETWEEN;
198 }
199
200 struct radio { char *text; int id; };
201
202 static void radioline_common(struct ctlpos *cp, char *text, int id,
203                              int nacross, struct radio *buttons, int nbuttons)
204 {
205     RECT r;
206     int group;
207     int i;
208     int j;
209
210     if (text) {
211         r.left = GAPBETWEEN;
212         r.top = cp->ypos;
213         r.right = cp->width;
214         r.bottom = STATICHEIGHT;
215         cp->ypos += r.bottom + GAPWITHIN;
216         doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
217     }
218
219     group = WS_GROUP;
220     i = 0;
221     for (j = 0; j < nbuttons; j++) {
222         char *btext = buttons[j].text;
223         int bid = buttons[j].id;
224
225         if (i == nacross) {
226             cp->ypos += r.bottom + (nacross > 1 ? GAPBETWEEN : GAPWITHIN);
227             i = 0;
228         }
229         r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
230         if (j < nbuttons-1)
231             r.right =
232                 (i + 1) * (cp->width + GAPBETWEEN) / nacross - r.left;
233         else
234             r.right = cp->width - r.left;
235         r.top = cp->ypos;
236         r.bottom = RADIOHEIGHT;
237         doctl(cp, r, "BUTTON",
238               BS_NOTIFY | BS_AUTORADIOBUTTON | WS_CHILD |
239               WS_VISIBLE | WS_TABSTOP | group, 0, btext, bid);
240         group = 0;
241         i++;
242     }
243     cp->ypos += r.bottom + GAPBETWEEN;
244 }
245
246 /*
247  * A set of radio buttons on the same line, with a static above
248  * them. `nacross' dictates how many parts the line is divided into
249  * (you might want this not to equal the number of buttons if you
250  * needed to line up some 2s and some 3s to look good in the same
251  * panel).
252  * 
253  * There's a bit of a hack in here to ensure that if nacross
254  * exceeds the actual number of buttons, the rightmost button
255  * really does get all the space right to the edge of the line, so
256  * you can do things like
257  * 
258  * (*) Button1  (*) Button2  (*) ButtonWithReallyLongTitle
259  */
260 void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...)
261 {
262     va_list ap;
263     struct radio *buttons;
264     int i, nbuttons;
265
266     va_start(ap, nacross);
267     nbuttons = 0;
268     while (1) {
269         char *btext = va_arg(ap, char *);
270         if (!btext)
271             break;
272         (void) va_arg(ap, int); /* id */
273         nbuttons++;
274     }
275     va_end(ap);
276     buttons = snewn(nbuttons, struct radio);
277     va_start(ap, nacross);
278     for (i = 0; i < nbuttons; i++) {
279         buttons[i].text = va_arg(ap, char *);
280         buttons[i].id = va_arg(ap, int);
281     }
282     va_end(ap);
283     radioline_common(cp, text, id, nacross, buttons, nbuttons);
284     sfree(buttons);
285 }
286
287 /*
288  * A set of radio buttons on the same line, without a static above
289  * them. Otherwise just like radioline.
290  */
291 void bareradioline(struct ctlpos *cp, int nacross, ...)
292 {
293     va_list ap;
294     struct radio *buttons;
295     int i, nbuttons;
296
297     va_start(ap, nacross);
298     nbuttons = 0;
299     while (1) {
300         char *btext = va_arg(ap, char *);
301         if (!btext)
302             break;
303         (void) va_arg(ap, int); /* id */
304     }
305     va_end(ap);
306     buttons = snewn(nbuttons, struct radio);
307     va_start(ap, nacross);
308     for (i = 0; i < nbuttons; i++) {
309         buttons[i].text = va_arg(ap, char *);
310         buttons[i].id = va_arg(ap, int);
311     }
312     va_end(ap);
313     radioline_common(cp, NULL, 0, nacross, buttons, nbuttons);
314     sfree(buttons);
315 }
316
317 /*
318  * A set of radio buttons on multiple lines, with a static above
319  * them.
320  */
321 void radiobig(struct ctlpos *cp, char *text, int id, ...)
322 {
323     va_list ap;
324     struct radio *buttons;
325     int i, nbuttons;
326
327     va_start(ap, id);
328     nbuttons = 0;
329     while (1) {
330         char *btext = va_arg(ap, char *);
331         if (!btext)
332             break;
333         (void) va_arg(ap, int); /* id */
334     }
335     va_end(ap);
336     buttons = snewn(nbuttons, struct radio);
337     va_start(ap, id);
338     for (i = 0; i < nbuttons; i++) {
339         buttons[i].text = va_arg(ap, char *);
340         buttons[i].id = va_arg(ap, int);
341     }
342     va_end(ap);
343     radioline_common(cp, text, id, 1, buttons, nbuttons);
344     sfree(buttons);
345 }
346
347 /*
348  * A single standalone checkbox.
349  */
350 void checkbox(struct ctlpos *cp, char *text, int id)
351 {
352     RECT r;
353
354     r.left = GAPBETWEEN;
355     r.top = cp->ypos;
356     r.right = cp->width;
357     r.bottom = CHECKBOXHEIGHT;
358     cp->ypos += r.bottom + GAPBETWEEN;
359     doctl(cp, r, "BUTTON",
360           BS_NOTIFY | BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
361           text, id);
362 }
363
364 /*
365  * Wrap a piece of text for a static text control. Returns the
366  * wrapped text (a malloc'ed string containing \ns), and also
367  * returns the number of lines required.
368  */
369 char *staticwrap(struct ctlpos *cp, HWND hwnd, char *text, int *lines)
370 {
371     HDC hdc = GetDC(hwnd);
372     int width, nlines, j;
373     INT *pwidths, nfit;
374     SIZE size;
375     char *ret, *p, *q;
376     RECT r;
377     HFONT oldfont, newfont;
378
379     ret = snewn(1+strlen(text), char);
380     p = text;
381     q = ret;
382     pwidths = snewn(1+strlen(text), INT);
383
384     /*
385      * Work out the width the text will need to fit in, by doing
386      * the same adjustment that the `statictext' function itself
387      * will perform.
388      */
389     SetMapMode(hdc, MM_TEXT);          /* ensure logical units == pixels */
390     r.left = r.top = r.bottom = 0;
391     r.right = cp->width;
392     MapDialogRect(hwnd, &r);
393     width = r.right;
394
395     nlines = 1;
396
397     /*
398      * We must select the correct font into the HDC before calling
399      * GetTextExtent*, or silly things will happen.
400      */
401     newfont = (HFONT)SendMessage(hwnd, WM_GETFONT, 0, 0);
402     oldfont = SelectObject(hdc, newfont);
403
404     while (*p) {
405         if (!GetTextExtentExPoint(hdc, p, strlen(p), width,
406                                   &nfit, pwidths, &size) ||
407             (size_t)nfit >= strlen(p)) {
408             /*
409              * Either GetTextExtentExPoint returned failure, or the
410              * whole of the rest of the text fits on this line.
411              * Either way, we stop wrapping, copy the remainder of
412              * the input string unchanged to the output, and leave.
413              */
414             strcpy(q, p);
415             break;
416         }
417
418         /*
419          * Now we search backwards along the string from `nfit',
420          * looking for a space at which to break the line. If we
421          * don't find one at all, that's fine - we'll just break
422          * the line at `nfit'.
423          */
424         for (j = nfit; j > 0; j--) {
425             if (isspace((unsigned char)p[j])) {
426                 nfit = j;
427                 break;
428             }
429         }
430
431         strncpy(q, p, nfit);
432         q[nfit] = '\n';
433         q += nfit+1;
434
435         p += nfit;
436         while (*p && isspace((unsigned char)*p))
437             p++;
438
439         nlines++;
440     }
441
442     SelectObject(hdc, oldfont);
443     ReleaseDC(cp->hwnd, hdc);
444
445     if (lines) *lines = nlines;
446
447     sfree(pwidths);
448
449     return ret;
450 }
451
452 /*
453  * A single standalone static text control.
454  */
455 void statictext(struct ctlpos *cp, char *text, int lines, int id)
456 {
457     RECT r;
458
459     r.left = GAPBETWEEN;
460     r.top = cp->ypos;
461     r.right = cp->width;
462     r.bottom = STATICHEIGHT * lines;
463     cp->ypos += r.bottom + GAPBETWEEN;
464     doctl(cp, r, "STATIC",
465           WS_CHILD | WS_VISIBLE | SS_LEFTNOWORDWRAP,
466           0, text, id);
467 }
468
469 /*
470  * An owner-drawn static text control for a panel title.
471  */
472 void paneltitle(struct ctlpos *cp, int id)
473 {
474     RECT r;
475
476     r.left = GAPBETWEEN;
477     r.top = cp->ypos;
478     r.right = cp->width;
479     r.bottom = TITLEHEIGHT;
480     cp->ypos += r.bottom + GAPBETWEEN;
481     doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_OWNERDRAW,
482           0, NULL, id);
483 }
484
485 /*
486  * A button on the right hand side, with a static to its left.
487  */
488 void staticbtn(struct ctlpos *cp, char *stext, int sid,
489                char *btext, int bid)
490 {
491     const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
492                         PUSHBTNHEIGHT : STATICHEIGHT);
493     RECT r;
494     int lwid, rwid, rpos;
495
496     rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
497     lwid = rpos - 2 * GAPBETWEEN;
498     rwid = cp->width + GAPBETWEEN - rpos;
499
500     r.left = GAPBETWEEN;
501     r.top = cp->ypos + (height - STATICHEIGHT) / 2;
502     r.right = lwid;
503     r.bottom = STATICHEIGHT;
504     doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
505
506     r.left = rpos;
507     r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
508     r.right = rwid;
509     r.bottom = PUSHBTNHEIGHT;
510     doctl(cp, r, "BUTTON",
511           BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
512           0, btext, bid);
513
514     cp->ypos += height + GAPBETWEEN;
515 }
516
517 /*
518  * A simple push button.
519  */
520 void button(struct ctlpos *cp, char *btext, int bid, int defbtn)
521 {
522     RECT r;
523
524     r.left = GAPBETWEEN;
525     r.top = cp->ypos;
526     r.right = cp->width;
527     r.bottom = PUSHBTNHEIGHT;
528
529     /* Q67655: the _dialog box_ must know which button is default
530      * as well as the button itself knowing */
531     if (defbtn && cp->hwnd)
532         SendMessage(cp->hwnd, DM_SETDEFID, bid, 0);
533
534     doctl(cp, r, "BUTTON",
535           BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
536           (defbtn ? BS_DEFPUSHBUTTON : 0) | BS_PUSHBUTTON,
537           0, btext, bid);
538
539     cp->ypos += PUSHBTNHEIGHT + GAPBETWEEN;
540 }
541
542 /*
543  * Like staticbtn, but two buttons.
544  */
545 void static2btn(struct ctlpos *cp, char *stext, int sid,
546                 char *btext1, int bid1, char *btext2, int bid2)
547 {
548     const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
549                         PUSHBTNHEIGHT : STATICHEIGHT);
550     RECT r;
551     int lwid, rwid1, rwid2, rpos1, rpos2;
552
553     rpos1 = GAPBETWEEN + (cp->width + GAPBETWEEN) / 2;
554     rpos2 = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
555     lwid = rpos1 - 2 * GAPBETWEEN;
556     rwid1 = rpos2 - rpos1 - GAPBETWEEN;
557     rwid2 = cp->width + GAPBETWEEN - rpos2;
558
559     r.left = GAPBETWEEN;
560     r.top = cp->ypos + (height - STATICHEIGHT) / 2;
561     r.right = lwid;
562     r.bottom = STATICHEIGHT;
563     doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
564
565     r.left = rpos1;
566     r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
567     r.right = rwid1;
568     r.bottom = PUSHBTNHEIGHT;
569     doctl(cp, r, "BUTTON",
570           BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
571           0, btext1, bid1);
572
573     r.left = rpos2;
574     r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
575     r.right = rwid2;
576     r.bottom = PUSHBTNHEIGHT;
577     doctl(cp, r, "BUTTON",
578           BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
579           0, btext2, bid2);
580
581     cp->ypos += height + GAPBETWEEN;
582 }
583
584 /*
585  * An edit control on the right hand side, with a static to its left.
586  */
587 static void staticedit_internal(struct ctlpos *cp, char *stext,
588                                 int sid, int eid, int percentedit,
589                                 int style)
590 {
591     const int height = (EDITHEIGHT > STATICHEIGHT ?
592                         EDITHEIGHT : STATICHEIGHT);
593     RECT r;
594     int lwid, rwid, rpos;
595
596     rpos =
597         GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
598     lwid = rpos - 2 * GAPBETWEEN;
599     rwid = cp->width + GAPBETWEEN - rpos;
600
601     r.left = GAPBETWEEN;
602     r.top = cp->ypos + (height - STATICHEIGHT) / 2;
603     r.right = lwid;
604     r.bottom = STATICHEIGHT;
605     doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
606
607     r.left = rpos;
608     r.top = cp->ypos + (height - EDITHEIGHT) / 2;
609     r.right = rwid;
610     r.bottom = EDITHEIGHT;
611     doctl(cp, r, "EDIT",
612           WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
613           WS_EX_CLIENTEDGE, "", eid);
614
615     cp->ypos += height + GAPBETWEEN;
616 }
617
618 void staticedit(struct ctlpos *cp, char *stext,
619                 int sid, int eid, int percentedit)
620 {
621     staticedit_internal(cp, stext, sid, eid, percentedit, 0);
622 }
623
624 void staticpassedit(struct ctlpos *cp, char *stext,
625                     int sid, int eid, int percentedit)
626 {
627     staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
628 }
629
630 /*
631  * A drop-down list box on the right hand side, with a static to
632  * its left.
633  */
634 void staticddl(struct ctlpos *cp, char *stext,
635                int sid, int lid, int percentlist)
636 {
637     const int height = (COMBOHEIGHT > STATICHEIGHT ?
638                         COMBOHEIGHT : STATICHEIGHT);
639     RECT r;
640     int lwid, rwid, rpos;
641
642     rpos =
643         GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
644     lwid = rpos - 2 * GAPBETWEEN;
645     rwid = cp->width + GAPBETWEEN - rpos;
646
647     r.left = GAPBETWEEN;
648     r.top = cp->ypos + (height - STATICHEIGHT) / 2;
649     r.right = lwid;
650     r.bottom = STATICHEIGHT;
651     doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
652
653     r.left = rpos;
654     r.top = cp->ypos + (height - EDITHEIGHT) / 2;
655     r.right = rwid;
656     r.bottom = COMBOHEIGHT*4;
657     doctl(cp, r, "COMBOBOX",
658           WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
659           CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
660
661     cp->ypos += height + GAPBETWEEN;
662 }
663
664 /*
665  * A combo box on the right hand side, with a static to its left.
666  */
667 void staticcombo(struct ctlpos *cp, char *stext,
668                  int sid, int lid, int percentlist)
669 {
670     const int height = (COMBOHEIGHT > STATICHEIGHT ?
671                         COMBOHEIGHT : STATICHEIGHT);
672     RECT r;
673     int lwid, rwid, rpos;
674
675     rpos =
676         GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
677     lwid = rpos - 2 * GAPBETWEEN;
678     rwid = cp->width + GAPBETWEEN - rpos;
679
680     r.left = GAPBETWEEN;
681     r.top = cp->ypos + (height - STATICHEIGHT) / 2;
682     r.right = lwid;
683     r.bottom = STATICHEIGHT;
684     doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
685
686     r.left = rpos;
687     r.top = cp->ypos + (height - EDITHEIGHT) / 2;
688     r.right = rwid;
689     r.bottom = COMBOHEIGHT*10;
690     doctl(cp, r, "COMBOBOX",
691           WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
692           CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
693
694     cp->ypos += height + GAPBETWEEN;
695 }
696
697 /*
698  * A static, with a full-width drop-down list box below it.
699  */
700 void staticddlbig(struct ctlpos *cp, char *stext,
701                   int sid, int lid)
702 {
703     RECT r;
704
705     if (stext) {
706         r.left = GAPBETWEEN;
707         r.top = cp->ypos;
708         r.right = cp->width;
709         r.bottom = STATICHEIGHT;
710         doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
711         cp->ypos += STATICHEIGHT;
712     }
713
714     r.left = GAPBETWEEN;
715     r.top = cp->ypos;
716     r.right = cp->width;
717     r.bottom = COMBOHEIGHT*4;
718     doctl(cp, r, "COMBOBOX",
719           WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
720           CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
721     cp->ypos += COMBOHEIGHT + GAPBETWEEN;
722 }
723
724 /*
725  * A big multiline edit control with a static labelling it.
726  */
727 void bigeditctrl(struct ctlpos *cp, char *stext,
728                  int sid, int eid, int lines)
729 {
730     RECT r;
731
732     if (stext) {
733         r.left = GAPBETWEEN;
734         r.top = cp->ypos;
735         r.right = cp->width;
736         r.bottom = STATICHEIGHT;
737         cp->ypos += r.bottom + GAPWITHIN;
738         doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
739     }
740
741     r.left = GAPBETWEEN;
742     r.top = cp->ypos;
743     r.right = cp->width;
744     r.bottom = EDITHEIGHT + (lines - 1) * STATICHEIGHT;
745     cp->ypos += r.bottom + GAPBETWEEN;
746     doctl(cp, r, "EDIT",
747           WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
748           WS_EX_CLIENTEDGE, "", eid);
749 }
750
751 /*
752  * A list box with a static labelling it.
753  */
754 void listbox(struct ctlpos *cp, char *stext,
755              int sid, int lid, int lines, int multi)
756 {
757     RECT r;
758
759     if (stext != NULL) {
760         r.left = GAPBETWEEN;
761         r.top = cp->ypos;
762         r.right = cp->width;
763         r.bottom = STATICHEIGHT;
764         cp->ypos += r.bottom + GAPWITHIN;
765         doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
766     }
767
768     r.left = GAPBETWEEN;
769     r.top = cp->ypos;
770     r.right = cp->width;
771     r.bottom = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
772     cp->ypos += r.bottom + GAPBETWEEN;
773     doctl(cp, r, "LISTBOX",
774           WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
775           LBS_NOTIFY | LBS_HASSTRINGS | LBS_USETABSTOPS |
776           (multi ? LBS_MULTIPLESEL : 0),
777           WS_EX_CLIENTEDGE, "", lid);
778 }
779
780 /*
781  * A tab-control substitute when a real tab control is unavailable.
782  */
783 void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id)
784 {
785     const int height = (COMBOHEIGHT > STATICHEIGHT ?
786                         COMBOHEIGHT : STATICHEIGHT);
787     RECT r;
788     int bigwid, lwid, rwid, rpos;
789     static const int BIGGAP = 15;
790     static const int MEDGAP = 3;
791
792     bigwid = cp->width + 2 * GAPBETWEEN - 2 * BIGGAP;
793     cp->ypos += MEDGAP;
794     rpos = BIGGAP + (bigwid + BIGGAP) / 2;
795     lwid = rpos - 2 * BIGGAP;
796     rwid = bigwid + BIGGAP - rpos;
797
798     r.left = BIGGAP;
799     r.top = cp->ypos + (height - STATICHEIGHT) / 2;
800     r.right = lwid;
801     r.bottom = STATICHEIGHT;
802     doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
803
804     r.left = rpos;
805     r.top = cp->ypos + (height - COMBOHEIGHT) / 2;
806     r.right = rwid;
807     r.bottom = COMBOHEIGHT * 10;
808     doctl(cp, r, "COMBOBOX",
809           WS_CHILD | WS_VISIBLE | WS_TABSTOP |
810           CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
811
812     cp->ypos += height + MEDGAP + GAPBETWEEN;
813
814     r.left = GAPBETWEEN;
815     r.top = cp->ypos;
816     r.right = cp->width;
817     r.bottom = 2;
818     doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
819           0, "", s2id);
820 }
821
822 /*
823  * A static line, followed by an edit control on the left hand side
824  * and a button on the right.
825  */
826 void editbutton(struct ctlpos *cp, char *stext, int sid,
827                 int eid, char *btext, int bid)
828 {
829     const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
830                         EDITHEIGHT : PUSHBTNHEIGHT);
831     RECT r;
832     int lwid, rwid, rpos;
833
834     r.left = GAPBETWEEN;
835     r.top = cp->ypos;
836     r.right = cp->width;
837     r.bottom = STATICHEIGHT;
838     cp->ypos += r.bottom + GAPWITHIN;
839     doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
840
841     rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
842     lwid = rpos - 2 * GAPBETWEEN;
843     rwid = cp->width + GAPBETWEEN - rpos;
844
845     r.left = GAPBETWEEN;
846     r.top = cp->ypos + (height - EDITHEIGHT) / 2;
847     r.right = lwid;
848     r.bottom = EDITHEIGHT;
849     doctl(cp, r, "EDIT",
850           WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
851           WS_EX_CLIENTEDGE, "", eid);
852
853     r.left = rpos;
854     r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
855     r.right = rwid;
856     r.bottom = PUSHBTNHEIGHT;
857     doctl(cp, r, "BUTTON",
858           BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
859           0, btext, bid);
860
861     cp->ypos += height + GAPBETWEEN;
862 }
863
864 /*
865  * A special control for manipulating an ordered preference list
866  * (eg. for cipher selection).
867  * XXX: this is a rough hack and could be improved.
868  */
869 void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
870                char *stext, int sid, int listid, int upbid, int dnbid)
871 {
872     const static int percents[] = { 5, 75, 20 };
873     RECT r;
874     int xpos, percent = 0, i;
875     int listheight = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
876     const int BTNSHEIGHT = 2*PUSHBTNHEIGHT + GAPBETWEEN;
877     int totalheight, buttonpos;
878
879     /* Squirrel away IDs. */
880     hdl->listid = listid;
881     hdl->upbid  = upbid;
882     hdl->dnbid  = dnbid;
883
884     /* The static label. */
885     if (stext != NULL) {
886         r.left = GAPBETWEEN;
887         r.top = cp->ypos;
888         r.right = cp->width;
889         r.bottom = STATICHEIGHT;
890         cp->ypos += r.bottom + GAPWITHIN;
891         doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
892     }
893
894     if (listheight > BTNSHEIGHT) {
895         totalheight = listheight;
896         buttonpos = (listheight - BTNSHEIGHT) / 2;
897     } else {
898         totalheight = BTNSHEIGHT;
899         buttonpos = 0;
900     }
901
902     for (i=0; i<3; i++) {
903         int left, wid;
904         xpos = (cp->width + GAPBETWEEN) * percent / 100;
905         left = xpos + GAPBETWEEN;
906         percent += percents[i];
907         xpos = (cp->width + GAPBETWEEN) * percent / 100;
908         wid = xpos - left;
909
910         switch (i) {
911           case 1:
912             /* The drag list box. */
913             r.left = left; r.right = wid;
914             r.top = cp->ypos; r.bottom = listheight;
915             {
916                 HWND ctl;
917                 ctl = doctl(cp, r, "LISTBOX",
918                             WS_CHILD | WS_VISIBLE | WS_TABSTOP |
919                             WS_VSCROLL | LBS_HASSTRINGS | LBS_USETABSTOPS,
920                             WS_EX_CLIENTEDGE,
921                             "", listid);
922                 MakeDragList(ctl);
923             }
924             break;
925
926           case 2:
927             /* The "Up" and "Down" buttons. */
928             /* XXX worry about accelerators if we have more than one
929              * prefslist on a panel */
930             r.left = left; r.right = wid;
931             r.top = cp->ypos + buttonpos; r.bottom = PUSHBTNHEIGHT;
932             doctl(cp, r, "BUTTON",
933                   BS_NOTIFY | WS_CHILD | WS_VISIBLE |
934                   WS_TABSTOP | BS_PUSHBUTTON,
935                   0, "&Up", upbid);
936
937             r.left = left; r.right = wid;
938             r.top = cp->ypos + buttonpos + PUSHBTNHEIGHT + GAPBETWEEN;
939             r.bottom = PUSHBTNHEIGHT;
940             doctl(cp, r, "BUTTON",
941                   BS_NOTIFY | WS_CHILD | WS_VISIBLE |
942                   WS_TABSTOP | BS_PUSHBUTTON,
943                   0, "&Down", dnbid);
944
945             break;
946
947         }
948     }
949
950     cp->ypos += totalheight + GAPBETWEEN;
951
952 }
953
954 /*
955  * Helper function for prefslist: move item in list box.
956  */
957 static void pl_moveitem(HWND hwnd, int listid, int src, int dst)
958 {
959     int tlen, val;
960     char *txt;
961     /* Get the item's data. */
962     tlen = SendDlgItemMessage (hwnd, listid, LB_GETTEXTLEN, src, 0);
963     txt = snewn(tlen+1, char);
964     SendDlgItemMessage (hwnd, listid, LB_GETTEXT, src, (LPARAM) txt);
965     val = SendDlgItemMessage (hwnd, listid, LB_GETITEMDATA, src, 0);
966     /* Deselect old location. */
967     SendDlgItemMessage (hwnd, listid, LB_SETSEL, FALSE, src);
968     /* Delete it at the old location. */
969     SendDlgItemMessage (hwnd, listid, LB_DELETESTRING, src, 0);
970     /* Insert it at new location. */
971     SendDlgItemMessage (hwnd, listid, LB_INSERTSTRING, dst,
972                         (LPARAM) txt);
973     SendDlgItemMessage (hwnd, listid, LB_SETITEMDATA, dst,
974                         (LPARAM) val);
975     /* Set selection. */
976     SendDlgItemMessage (hwnd, listid, LB_SETCURSEL, dst, 0);
977     sfree (txt);
978 }
979
980 int pl_itemfrompt(HWND hwnd, POINT cursor, BOOL scroll)
981 {
982     int ret;
983     POINT uppoint, downpoint;
984     int updist, downdist, upitem, downitem, i;
985
986     /*
987      * Ghastly hackery to try to figure out not which
988      * _item_, but which _gap between items_, the user
989      * is pointing at. We do this by first working out
990      * which list item is under the cursor, and then
991      * working out how far the cursor would have to
992      * move up or down before the answer was different.
993      * Then we put the insertion point _above_ the
994      * current item if the upper edge is closer than
995      * the lower edge, or _below_ it if vice versa.
996      */
997     ret = LBItemFromPt(hwnd, cursor, scroll);
998     if (ret == -1)
999         return ret;
1000     ret = LBItemFromPt(hwnd, cursor, FALSE);
1001     updist = downdist = 0;
1002     for (i = 1; i < 4096 && (!updist || !downdist); i++) {
1003         uppoint = downpoint = cursor;
1004         uppoint.y -= i;
1005         downpoint.y += i;
1006         upitem = LBItemFromPt(hwnd, uppoint, FALSE);
1007         downitem = LBItemFromPt(hwnd, downpoint, FALSE);
1008         if (!updist && upitem != ret)
1009             updist = i;
1010         if (!downdist && downitem != ret)
1011             downdist = i;
1012     }
1013     if (downdist < updist)
1014         ret++;
1015     return ret;
1016 }
1017
1018 /*
1019  * Handler for prefslist above.
1020  * 
1021  * Return value has bit 0 set if the dialog box procedure needs to
1022  * return TRUE from handling this message; it has bit 1 set if a
1023  * change may have been made in the contents of the list.
1024  */
1025 int handle_prefslist(struct prefslist *hdl,
1026                      int *array, int maxmemb,
1027                      int is_dlmsg, HWND hwnd,
1028                      WPARAM wParam, LPARAM lParam)
1029 {
1030     int i;
1031     int ret = 0;
1032
1033     if (is_dlmsg) {
1034
1035         if ((int)wParam == hdl->listid) {
1036             DRAGLISTINFO *dlm = (DRAGLISTINFO *)lParam;
1037             int dest = 0;              /* initialise to placate gcc */
1038             switch (dlm->uNotification) {
1039               case DL_BEGINDRAG:
1040                 /* Add a dummy item to make pl_itemfrompt() work
1041                  * better.
1042                  * FIXME: this causes scrollbar glitches if the count of
1043                  *        listbox contains >= its height. */
1044                 hdl->dummyitem =
1045                     SendDlgItemMessage(hwnd, hdl->listid,
1046                                        LB_ADDSTRING, 0, (LPARAM) "");
1047
1048                 hdl->srcitem = LBItemFromPt(dlm->hWnd, dlm->ptCursor, TRUE);
1049                 hdl->dragging = 0;
1050                 /* XXX hack Q183115 */
1051                 SetWindowLongPtr(hwnd, DWLP_MSGRESULT, TRUE);
1052                 ret |= 1; break;
1053               case DL_CANCELDRAG:
1054                 DrawInsert(hwnd, dlm->hWnd, -1);     /* Clear arrow */
1055                 SendDlgItemMessage(hwnd, hdl->listid,
1056                                    LB_DELETESTRING, hdl->dummyitem, 0);
1057                 hdl->dragging = 0;
1058                 ret |= 1; break;
1059               case DL_DRAGGING:
1060                 hdl->dragging = 1;
1061                 dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, TRUE);
1062                 if (dest > hdl->dummyitem) dest = hdl->dummyitem;
1063                 DrawInsert (hwnd, dlm->hWnd, dest);
1064                 if (dest >= 0)
1065                     SetWindowLongPtr(hwnd, DWLP_MSGRESULT, DL_MOVECURSOR);
1066                 else
1067                     SetWindowLongPtr(hwnd, DWLP_MSGRESULT, DL_STOPCURSOR);
1068                 ret |= 1; break;
1069               case DL_DROPPED:
1070                 if (hdl->dragging) {
1071                     dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, TRUE);
1072                     if (dest > hdl->dummyitem) dest = hdl->dummyitem;
1073                     DrawInsert (hwnd, dlm->hWnd, -1);
1074                 }
1075                 SendDlgItemMessage(hwnd, hdl->listid,
1076                                    LB_DELETESTRING, hdl->dummyitem, 0);
1077                 if (hdl->dragging) {
1078                     hdl->dragging = 0;
1079                     if (dest >= 0) {
1080                         /* Correct for "missing" item. */
1081                         if (dest > hdl->srcitem) dest--;
1082                         pl_moveitem(hwnd, hdl->listid, hdl->srcitem, dest);
1083                     }
1084                     ret |= 2;
1085                 }
1086                 ret |= 1; break;
1087             }
1088         }
1089
1090     } else {
1091
1092         if (((LOWORD(wParam) == hdl->upbid) ||
1093              (LOWORD(wParam) == hdl->dnbid)) &&
1094             ((HIWORD(wParam) == BN_CLICKED) ||
1095              (HIWORD(wParam) == BN_DOUBLECLICKED))) {
1096             /* Move an item up or down the list. */
1097             /* Get the current selection, if any. */
1098             int selection = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCURSEL, 0, 0);
1099             if (selection == LB_ERR) {
1100                 MessageBeep(0);
1101             } else {
1102                 int nitems;
1103                 /* Get the total number of items. */
1104                 nitems = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCOUNT, 0, 0);
1105                 /* Should we do anything? */
1106                 if (LOWORD(wParam) == hdl->upbid && (selection > 0))
1107                     pl_moveitem(hwnd, hdl->listid, selection, selection - 1);
1108                 else if (LOWORD(wParam) == hdl->dnbid && (selection < nitems - 1))
1109                     pl_moveitem(hwnd, hdl->listid, selection, selection + 1);
1110                 ret |= 2;
1111             }
1112
1113         }
1114
1115     }
1116
1117     if (array) {
1118         /* Update array to match the list box. */
1119         for (i=0; i < maxmemb; i++)
1120             array[i] = SendDlgItemMessage (hwnd, hdl->listid, LB_GETITEMDATA,
1121                                            i, 0);
1122     }
1123
1124     return ret;
1125 }
1126
1127 /*
1128  * A progress bar (from Common Controls). We like our progress bars
1129  * to be smooth and unbroken, without those ugly divisions; some
1130  * older compilers may not support that, but that's life.
1131  */
1132 void progressbar(struct ctlpos *cp, int id)
1133 {
1134     RECT r;
1135
1136     r.left = GAPBETWEEN;
1137     r.top = cp->ypos;
1138     r.right = cp->width;
1139     r.bottom = PROGBARHEIGHT;
1140     cp->ypos += r.bottom + GAPBETWEEN;
1141
1142     doctl(cp, r, PROGRESS_CLASS, WS_CHILD | WS_VISIBLE
1143 #ifdef PBS_SMOOTH
1144           | PBS_SMOOTH
1145 #endif
1146           , WS_EX_CLIENTEDGE, "", id);
1147 }
1148
1149 /* ----------------------------------------------------------------------
1150  * Platform-specific side of portable dialog-box mechanism.
1151  */
1152
1153 /*
1154  * This function takes a string, escapes all the ampersands, and
1155  * places a single (unescaped) ampersand in front of the first
1156  * occurrence of the given shortcut character (which may be
1157  * NO_SHORTCUT).
1158  * 
1159  * Return value is a malloc'ed copy of the processed version of the
1160  * string.
1161  */
1162 static char *shortcut_escape(const char *text, char shortcut)
1163 {
1164     char *ret;
1165     char const *p;
1166     char *q;
1167
1168     if (!text)
1169         return NULL;                   /* sfree won't choke on this */
1170
1171     ret = snewn(2*strlen(text)+1, char);   /* size potentially doubles! */
1172     shortcut = tolower((unsigned char)shortcut);
1173
1174     p = text;
1175     q = ret;
1176     while (*p) {
1177         if (shortcut != NO_SHORTCUT &&
1178             tolower((unsigned char)*p) == shortcut) {
1179             *q++ = '&';
1180             shortcut = NO_SHORTCUT;    /* stop it happening twice */
1181         } else if (*p == '&') {
1182             *q++ = '&';
1183         }
1184         *q++ = *p++;
1185     }
1186     *q = '\0';
1187     return ret;
1188 }
1189
1190 void winctrl_add_shortcuts(struct dlgparam *dp, struct winctrl *c)
1191 {
1192     int i;
1193     for (i = 0; i < lenof(c->shortcuts); i++)
1194         if (c->shortcuts[i] != NO_SHORTCUT) {
1195             unsigned char s = tolower((unsigned char)c->shortcuts[i]);
1196             assert(!dp->shortcuts[s]);
1197             dp->shortcuts[s] = TRUE;
1198         }
1199 }
1200
1201 void winctrl_rem_shortcuts(struct dlgparam *dp, struct winctrl *c)
1202 {
1203     int i;
1204     for (i = 0; i < lenof(c->shortcuts); i++)
1205         if (c->shortcuts[i] != NO_SHORTCUT) {
1206             unsigned char s = tolower((unsigned char)c->shortcuts[i]);
1207             assert(dp->shortcuts[s]);
1208             dp->shortcuts[s] = FALSE;
1209         }
1210 }
1211
1212 static int winctrl_cmp_byctrl(void *av, void *bv)
1213 {
1214     struct winctrl *a = (struct winctrl *)av;
1215     struct winctrl *b = (struct winctrl *)bv;
1216     if (a->ctrl < b->ctrl)
1217         return -1;
1218     else if (a->ctrl > b->ctrl)
1219         return +1;
1220     else
1221         return 0;
1222 }
1223 static int winctrl_cmp_byid(void *av, void *bv)
1224 {
1225     struct winctrl *a = (struct winctrl *)av;
1226     struct winctrl *b = (struct winctrl *)bv;
1227     if (a->base_id < b->base_id)
1228         return -1;
1229     else if (a->base_id > b->base_id)
1230         return +1;
1231     else
1232         return 0;
1233 }
1234 static int winctrl_cmp_byctrl_find(void *av, void *bv)
1235 {
1236     union control *a = (union control *)av;
1237     struct winctrl *b = (struct winctrl *)bv;
1238     if (a < b->ctrl)
1239         return -1;
1240     else if (a > b->ctrl)
1241         return +1;
1242     else
1243         return 0;
1244 }
1245 static int winctrl_cmp_byid_find(void *av, void *bv)
1246 {
1247     int *a = (int *)av;
1248     struct winctrl *b = (struct winctrl *)bv;
1249     if (*a < b->base_id)
1250         return -1;
1251     else if (*a >= b->base_id + b->num_ids)
1252         return +1;
1253     else
1254         return 0;
1255 }
1256
1257 void winctrl_init(struct winctrls *wc)
1258 {
1259     wc->byctrl = newtree234(winctrl_cmp_byctrl);
1260     wc->byid = newtree234(winctrl_cmp_byid);
1261 }
1262 void winctrl_cleanup(struct winctrls *wc)
1263 {
1264     struct winctrl *c;
1265
1266     while ((c = index234(wc->byid, 0)) != NULL) {
1267         winctrl_remove(wc, c);
1268         sfree(c->data);
1269         sfree(c);
1270     }
1271
1272     freetree234(wc->byctrl);
1273     freetree234(wc->byid);
1274     wc->byctrl = wc->byid = NULL;
1275 }
1276
1277 void winctrl_add(struct winctrls *wc, struct winctrl *c)
1278 {
1279     struct winctrl *ret;
1280     if (c->ctrl) {
1281         ret = add234(wc->byctrl, c);
1282         assert(ret == c);
1283     }
1284     ret = add234(wc->byid, c);
1285     assert(ret == c);
1286 }
1287
1288 void winctrl_remove(struct winctrls *wc, struct winctrl *c)
1289 {
1290     struct winctrl *ret;
1291     ret = del234(wc->byctrl, c);
1292     ret = del234(wc->byid, c);
1293     assert(ret == c);
1294 }
1295
1296 struct winctrl *winctrl_findbyctrl(struct winctrls *wc, union control *ctrl)
1297 {
1298     return find234(wc->byctrl, ctrl, winctrl_cmp_byctrl_find);
1299 }
1300
1301 struct winctrl *winctrl_findbyid(struct winctrls *wc, int id)
1302 {
1303     return find234(wc->byid, &id, winctrl_cmp_byid_find);
1304 }
1305
1306 struct winctrl *winctrl_findbyindex(struct winctrls *wc, int index)
1307 {
1308     return index234(wc->byid, index);
1309 }
1310
1311 void winctrl_layout(struct dlgparam *dp, struct winctrls *wc,
1312                     struct ctlpos *cp, struct controlset *s, int *id)
1313 {
1314     struct ctlpos columns[16];
1315     int ncols, colstart, colspan;
1316
1317     struct ctlpos tabdelays[16];
1318     union control *tabdelayed[16];
1319     int ntabdelays;
1320
1321     struct ctlpos pos;
1322
1323     char shortcuts[MAX_SHORTCUTS_PER_CTRL];
1324     int nshortcuts;
1325     char *escaped;
1326     int i, actual_base_id, base_id, num_ids;
1327     void *data;
1328
1329     base_id = *id;
1330
1331     /* Start a containing box, if we have a boxname. */
1332     if (s->boxname && *s->boxname) {
1333         struct winctrl *c = snew(struct winctrl);
1334         c->ctrl = NULL;
1335         c->base_id = base_id;
1336         c->num_ids = 1;
1337         c->data = NULL;
1338         memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
1339         winctrl_add(wc, c);
1340         beginbox(cp, s->boxtitle, base_id);
1341         base_id++;
1342     }
1343
1344     /* Draw a title, if we have one. */
1345     if (!s->boxname && s->boxtitle) {
1346         struct winctrl *c = snew(struct winctrl);
1347         c->ctrl = NULL;
1348         c->base_id = base_id;
1349         c->num_ids = 1;
1350         c->data = dupstr(s->boxtitle);
1351         memset(c->shortcuts, NO_SHORTCUT, lenof(c->shortcuts));
1352         winctrl_add(wc, c);
1353         paneltitle(cp, base_id);
1354         base_id++;
1355     }
1356
1357     /* Initially we have just one column. */
1358     ncols = 1;
1359     columns[0] = *cp;                  /* structure copy */
1360
1361     /* And initially, there are no pending tab-delayed controls. */
1362     ntabdelays = 0;
1363
1364     /* Loop over each control in the controlset. */
1365     for (i = 0; i < s->ncontrols; i++) {
1366         union control *ctrl = s->ctrls[i];
1367
1368         /*
1369          * Generic processing that pertains to all control types.
1370          * At the end of this if statement, we'll have produced
1371          * `ctrl' (a pointer to the control we have to create, or
1372          * think about creating, in this iteration of the loop),
1373          * `pos' (a suitable ctlpos with which to position it), and
1374          * `c' (a winctrl structure to receive details of the
1375          * dialog IDs). Or we'll have done a `continue', if it was
1376          * CTRL_COLUMNS and doesn't require any control creation at
1377          * all.
1378          */
1379         if (ctrl->generic.type == CTRL_COLUMNS) {
1380             assert((ctrl->columns.ncols == 1) ^ (ncols == 1));
1381
1382             if (ncols == 1) {
1383                 /*
1384                  * We're splitting into multiple columns.
1385                  */
1386                 int lpercent, rpercent, lx, rx, i;
1387
1388                 ncols = ctrl->columns.ncols;
1389                 assert(ncols <= lenof(columns));
1390                 for (i = 1; i < ncols; i++)
1391                     columns[i] = columns[0];   /* structure copy */
1392
1393                 lpercent = 0;
1394                 for (i = 0; i < ncols; i++) {
1395                     rpercent = lpercent + ctrl->columns.percentages[i];
1396                     lx = columns[i].xoff + lpercent *
1397                         (columns[i].width + GAPBETWEEN) / 100;
1398                     rx = columns[i].xoff + rpercent *
1399                         (columns[i].width + GAPBETWEEN) / 100;
1400                     columns[i].xoff = lx;
1401                     columns[i].width = rx - lx - GAPBETWEEN;
1402                     lpercent = rpercent;
1403                 }
1404             } else {
1405                 /*
1406                  * We're recombining the various columns into one.
1407                  */
1408                 int maxy = columns[0].ypos;
1409                 int i;
1410                 for (i = 1; i < ncols; i++)
1411                     if (maxy < columns[i].ypos)
1412                         maxy = columns[i].ypos;
1413                 ncols = 1;
1414                 columns[0] = *cp;      /* structure copy */
1415                 columns[0].ypos = maxy;
1416             }
1417
1418             continue;
1419         } else if (ctrl->generic.type == CTRL_TABDELAY) {
1420             int i;
1421
1422             assert(!ctrl->generic.tabdelay);
1423             ctrl = ctrl->tabdelay.ctrl;
1424
1425             for (i = 0; i < ntabdelays; i++)
1426                 if (tabdelayed[i] == ctrl)
1427                     break;
1428             assert(i < ntabdelays);    /* we have to have found it */
1429
1430             pos = tabdelays[i];        /* structure copy */
1431
1432             colstart = colspan = -1;   /* indicate this was tab-delayed */
1433
1434         } else {
1435             /*
1436              * If it wasn't one of those, it's a genuine control;
1437              * so we'll have to compute a position for it now, by
1438              * checking its column span.
1439              */
1440             int col;
1441
1442             colstart = COLUMN_START(ctrl->generic.column);
1443             colspan = COLUMN_SPAN(ctrl->generic.column);
1444
1445             pos = columns[colstart];   /* structure copy */
1446             pos.width = columns[colstart+colspan-1].width +
1447                 (columns[colstart+colspan-1].xoff - columns[colstart].xoff);
1448
1449             for (col = colstart; col < colstart+colspan; col++)
1450                 if (pos.ypos < columns[col].ypos)
1451                     pos.ypos = columns[col].ypos;
1452
1453             /*
1454              * If this control is to be tabdelayed, add it to the
1455              * tabdelay list, and unset pos.hwnd to inhibit actual
1456              * control creation.
1457              */
1458             if (ctrl->generic.tabdelay) {
1459                 assert(ntabdelays < lenof(tabdelays));
1460                 tabdelays[ntabdelays] = pos;   /* structure copy */
1461                 tabdelayed[ntabdelays] = ctrl;
1462                 ntabdelays++;
1463                 pos.hwnd = NULL;
1464             }
1465         }
1466
1467         /* Most controls don't need anything in c->data. */
1468         data = NULL;
1469
1470         /* And they all start off with no shortcuts registered. */
1471         memset(shortcuts, NO_SHORTCUT, lenof(shortcuts));
1472         nshortcuts = 0;
1473
1474         /* Almost all controls start at base_id. */
1475         actual_base_id = base_id;
1476
1477         /*
1478          * Now we're ready to actually create the control, by
1479          * switching on its type.
1480          */
1481         switch (ctrl->generic.type) {
1482           case CTRL_TEXT:
1483             {
1484                 char *wrapped, *escaped;
1485                 int lines;
1486                 num_ids = 1;
1487                 wrapped = staticwrap(&pos, cp->hwnd,
1488                                      ctrl->generic.label, &lines);
1489                 escaped = shortcut_escape(wrapped, NO_SHORTCUT);
1490                 statictext(&pos, escaped, lines, base_id);
1491                 sfree(escaped);
1492                 sfree(wrapped);
1493             }
1494             break;
1495           case CTRL_EDITBOX:
1496             num_ids = 2;               /* static, edit */
1497             escaped = shortcut_escape(ctrl->editbox.label,
1498                                       ctrl->editbox.shortcut);
1499             shortcuts[nshortcuts++] = ctrl->editbox.shortcut;
1500             if (ctrl->editbox.percentwidth == 100) {
1501                 if (ctrl->editbox.has_list)
1502                     combobox(&pos, escaped,
1503                              base_id, base_id+1);
1504                 else
1505                     editboxfw(&pos, ctrl->editbox.password, escaped,
1506                               base_id, base_id+1);
1507             } else {
1508                 if (ctrl->editbox.has_list) {
1509                     staticcombo(&pos, escaped, base_id, base_id+1,
1510                                 ctrl->editbox.percentwidth);
1511                 } else {
1512                     (ctrl->editbox.password ? staticpassedit : staticedit)
1513                         (&pos, escaped, base_id, base_id+1,
1514                          ctrl->editbox.percentwidth);
1515                 }
1516             }
1517             sfree(escaped);
1518             break;
1519           case CTRL_RADIO:
1520             num_ids = ctrl->radio.nbuttons + 1;   /* label as well */
1521             {
1522                 struct radio *buttons;
1523                 int i;
1524
1525                 escaped = shortcut_escape(ctrl->radio.label,
1526                                           ctrl->radio.shortcut);
1527                 shortcuts[nshortcuts++] = ctrl->radio.shortcut;
1528
1529                 buttons = snewn(ctrl->radio.nbuttons, struct radio);
1530
1531                 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1532                     buttons[i].text =
1533                         shortcut_escape(ctrl->radio.buttons[i],
1534                                         (char)(ctrl->radio.shortcuts ?
1535                                                ctrl->radio.shortcuts[i] :
1536                                                NO_SHORTCUT));
1537                     buttons[i].id = base_id + 1 + i;
1538                     if (ctrl->radio.shortcuts) {
1539                         assert(nshortcuts < MAX_SHORTCUTS_PER_CTRL);
1540                         shortcuts[nshortcuts++] = ctrl->radio.shortcuts[i];
1541                     }
1542                 }
1543
1544                 radioline_common(&pos, escaped, base_id,
1545                                  ctrl->radio.ncolumns,
1546                                  buttons, ctrl->radio.nbuttons);
1547
1548                 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1549                     sfree(buttons[i].text);
1550                 }
1551                 sfree(buttons);
1552                 sfree(escaped);
1553             }
1554             break;
1555           case CTRL_CHECKBOX:
1556             num_ids = 1;
1557             escaped = shortcut_escape(ctrl->checkbox.label,
1558                                       ctrl->checkbox.shortcut);
1559             shortcuts[nshortcuts++] = ctrl->checkbox.shortcut;
1560             checkbox(&pos, escaped, base_id);
1561             sfree(escaped);
1562             break;
1563           case CTRL_BUTTON:
1564             escaped = shortcut_escape(ctrl->button.label,
1565                                       ctrl->button.shortcut);
1566             shortcuts[nshortcuts++] = ctrl->button.shortcut;
1567             if (ctrl->button.iscancel)
1568                 actual_base_id = IDCANCEL;
1569             num_ids = 1;
1570             button(&pos, escaped, actual_base_id, ctrl->button.isdefault);
1571             sfree(escaped);
1572             break;
1573           case CTRL_LISTBOX:
1574             num_ids = 2;
1575             escaped = shortcut_escape(ctrl->listbox.label,
1576                                       ctrl->listbox.shortcut);
1577             shortcuts[nshortcuts++] = ctrl->listbox.shortcut;
1578             if (ctrl->listbox.draglist) {
1579                 data = snew(struct prefslist);
1580                 num_ids = 4;
1581                 prefslist(data, &pos, ctrl->listbox.height, escaped,
1582                           base_id, base_id+1, base_id+2, base_id+3);
1583                 shortcuts[nshortcuts++] = 'u';   /* Up */
1584                 shortcuts[nshortcuts++] = 'd';   /* Down */
1585             } else if (ctrl->listbox.height == 0) {
1586                 /* Drop-down list. */
1587                 if (ctrl->listbox.percentwidth == 100) {
1588                     staticddlbig(&pos, escaped,
1589                                  base_id, base_id+1);
1590                 } else {
1591                     staticddl(&pos, escaped, base_id,
1592                               base_id+1, ctrl->listbox.percentwidth);
1593                 }
1594             } else {
1595                 /* Ordinary list. */
1596                 listbox(&pos, escaped, base_id, base_id+1,
1597                         ctrl->listbox.height, ctrl->listbox.multisel);
1598             }
1599             if (ctrl->listbox.ncols) {
1600                 /*
1601                  * This method of getting the box width is a bit of
1602                  * a hack; we'd do better to try to retrieve the
1603                  * actual width in dialog units from doctl() just
1604                  * before MapDialogRect. But that's going to be no
1605                  * fun, and this should be good enough accuracy.
1606                  */
1607                 int width = cp->width * ctrl->listbox.percentwidth;
1608                 int *tabarray;
1609                 int i, percent;
1610
1611                 tabarray = snewn(ctrl->listbox.ncols-1, int);
1612                 percent = 0;
1613                 for (i = 0; i < ctrl->listbox.ncols-1; i++) {
1614                     percent += ctrl->listbox.percentages[i];
1615                     tabarray[i] = width * percent / 10000;
1616                 }
1617                 SendDlgItemMessage(cp->hwnd, base_id+1, LB_SETTABSTOPS,
1618                                    ctrl->listbox.ncols-1, (LPARAM)tabarray);
1619                 sfree(tabarray);
1620             }
1621             sfree(escaped);
1622             break;
1623           case CTRL_FILESELECT:
1624             num_ids = 3;
1625             escaped = shortcut_escape(ctrl->fileselect.label,
1626                                       ctrl->fileselect.shortcut);
1627             shortcuts[nshortcuts++] = ctrl->fileselect.shortcut;
1628             editbutton(&pos, escaped, base_id, base_id+1,
1629                        "Bro&wse...", base_id+2);
1630             shortcuts[nshortcuts++] = 'w';
1631             sfree(escaped);
1632             break;
1633           case CTRL_FONTSELECT:
1634             num_ids = 3;
1635             escaped = shortcut_escape(ctrl->fontselect.label,
1636                                       ctrl->fontselect.shortcut);
1637             shortcuts[nshortcuts++] = ctrl->fontselect.shortcut;
1638             statictext(&pos, escaped, 1, base_id);
1639             staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
1640             data = fontspec_new("", 0, 0, 0);
1641             sfree(escaped);
1642             break;
1643           default:
1644             assert(!"Can't happen");
1645             num_ids = 0;               /* placate gcc */
1646             break;
1647         }
1648
1649         /*
1650          * Create a `struct winctrl' for this control, and advance
1651          * the dialog ID counter, if it's actually been created
1652          * (and isn't tabdelayed).
1653          */
1654         if (pos.hwnd) {
1655             struct winctrl *c = snew(struct winctrl);
1656
1657             c->ctrl = ctrl;
1658             c->base_id = actual_base_id;
1659             c->num_ids = num_ids;
1660             c->data = data;
1661             memcpy(c->shortcuts, shortcuts, sizeof(shortcuts));
1662             winctrl_add(wc, c);
1663             winctrl_add_shortcuts(dp, c);
1664             if (actual_base_id == base_id)
1665                 base_id += num_ids;
1666         } else {
1667             sfree(data);
1668         }
1669
1670         if (colstart >= 0) {
1671             /*
1672              * Update the ypos in all columns crossed by this
1673              * control.
1674              */
1675             int i;
1676             for (i = colstart; i < colstart+colspan; i++)
1677                 columns[i].ypos = pos.ypos;
1678         }
1679     }
1680
1681     /*
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.
1685      */
1686     for (i = 0; i < ncols; i++)
1687         if (cp->ypos < columns[i].ypos)
1688             cp->ypos = columns[i].ypos;
1689     *id = base_id;
1690
1691     if (s->boxname && *s->boxname)
1692         endbox(cp);
1693 }
1694
1695 static void winctrl_set_focus(union control *ctrl, struct dlgparam *dp,
1696                               int has_focus)
1697 {
1698     if (has_focus) {
1699         if (dp->focused)
1700             dp->lastfocused = dp->focused;
1701         dp->focused = ctrl;
1702     } else if (!has_focus && dp->focused == ctrl) {
1703         dp->lastfocused = dp->focused;
1704         dp->focused = NULL;
1705     }
1706 }
1707
1708 union control *dlg_last_focused(union control *ctrl, void *dlg)
1709 {
1710     struct dlgparam *dp = (struct dlgparam *)dlg;
1711     return dp->focused == ctrl ? dp->lastfocused : dp->focused;
1712 }
1713
1714 /*
1715  * The dialog-box procedure calls this function to handle Windows
1716  * messages on a control we manage.
1717  */
1718 int winctrl_handle_command(struct dlgparam *dp, UINT msg,
1719                            WPARAM wParam, LPARAM lParam)
1720 {
1721     struct winctrl *c;
1722     union control *ctrl;
1723     int i, id, ret;
1724     static UINT draglistmsg = WM_NULL;
1725
1726     /*
1727      * Filter out pointless window messages. Our interest is in
1728      * WM_COMMAND and the drag list message, and nothing else.
1729      */
1730     if (draglistmsg == WM_NULL)
1731         draglistmsg = RegisterWindowMessage (DRAGLISTMSGSTRING);
1732
1733     if (msg != draglistmsg && msg != WM_COMMAND && msg != WM_DRAWITEM)
1734         return 0;
1735
1736     /*
1737      * Look up the control ID in our data.
1738      */
1739     c = NULL;
1740     for (i = 0; i < dp->nctrltrees; i++) {
1741         c = winctrl_findbyid(dp->controltrees[i], LOWORD(wParam));
1742         if (c)
1743             break;
1744     }
1745     if (!c)
1746         return 0;                      /* we have nothing to do */
1747
1748     if (msg == WM_DRAWITEM) {
1749         /*
1750          * Owner-draw request for a panel title.
1751          */
1752         LPDRAWITEMSTRUCT di = (LPDRAWITEMSTRUCT) lParam;
1753         HDC hdc = di->hDC;
1754         RECT r = di->rcItem;
1755         SIZE s;
1756
1757         SetMapMode(hdc, MM_TEXT);      /* ensure logical units == pixels */
1758
1759         GetTextExtentPoint32(hdc, (char *)c->data,
1760                                  strlen((char *)c->data), &s);
1761         DrawEdge(hdc, &r, EDGE_ETCHED, BF_ADJUST | BF_RECT);
1762         TextOut(hdc,
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));
1766
1767         return TRUE;
1768     }
1769
1770     ctrl = c->ctrl;
1771     id = LOWORD(wParam) - c->base_id;
1772
1773     if (!ctrl || !ctrl->generic.handler)
1774         return 0;                      /* nothing we can do here */
1775
1776     /*
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().
1782      */
1783     ret = 0;
1784     dp->coloursel_wanted = FALSE;
1785
1786     /*
1787      * Now switch on the control type and the message.
1788      */
1789     switch (ctrl->generic.type) {
1790       case CTRL_EDITBOX:
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);
1797
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) {
1804                 int index, len;
1805                 char *text;
1806
1807                 index = SendDlgItemMessage(dp->hwnd, c->base_id+1,
1808                                            CB_GETCURSEL, 0, 0);
1809                 len = SendDlgItemMessage(dp->hwnd, c->base_id+1,
1810                                          CB_GETLBTEXTLEN, index, 0);
1811                 text = snewn(len+1, char);
1812                 SendDlgItemMessage(dp->hwnd, c->base_id+1, CB_GETLBTEXT,
1813                                    index, (LPARAM)text);
1814                 SetDlgItemText(dp->hwnd, c->base_id+1, text);
1815                 sfree(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);
1821             }
1822
1823         }
1824         break;
1825       case CTRL_RADIO:
1826         if (msg == WM_COMMAND &&
1827             (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1828             winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1829         /*
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.
1835          */
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);
1841         }
1842         break;
1843       case CTRL_CHECKBOX:
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);
1851         }
1852         break;
1853       case CTRL_BUTTON:
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);
1861         }
1862         break;
1863       case CTRL_LISTBOX:
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) {
1874             int pret;
1875             pret = handle_prefslist(c->data, NULL, 0, (msg != WM_COMMAND),
1876                                     dp->hwnd, wParam, lParam);
1877             if (pret & 2)
1878                 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1879             ret = pret & 1;
1880         } else {
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);
1886             }
1887         }
1888         break;
1889       case CTRL_FILESELECT:
1890         if (msg == WM_COMMAND && id == 1 &&
1891             (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
1892             winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
1893         if (msg == WM_COMMAND && id == 2 &&
1894             (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1895             winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1896         if (msg == WM_COMMAND && id == 1 && HIWORD(wParam) == EN_CHANGE)
1897             ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1898         if (id == 2 &&
1899             (msg == WM_COMMAND &&
1900              (HIWORD(wParam) == BN_CLICKED ||
1901               HIWORD(wParam) == BN_DOUBLECLICKED))) {
1902             OPENFILENAME of;
1903             char filename[FILENAME_MAX];
1904
1905             memset(&of, 0, sizeof(of));
1906             of.hwndOwner = dp->hwnd;
1907             if (ctrl->fileselect.filter)
1908                 of.lpstrFilter = ctrl->fileselect.filter;
1909             else
1910                 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
1911             of.lpstrCustomFilter = NULL;
1912             of.nFilterIndex = 1;
1913             of.lpstrFile = filename;
1914             GetDlgItemText(dp->hwnd, c->base_id+1, filename, lenof(filename));
1915             filename[lenof(filename)-1] = '\0';
1916             of.nMaxFile = lenof(filename);
1917             of.lpstrFileTitle = NULL;
1918             of.lpstrTitle = ctrl->fileselect.title;
1919             of.Flags = 0;
1920             if (request_file(NULL, &of, FALSE, ctrl->fileselect.for_writing)) {
1921                 SetDlgItemText(dp->hwnd, c->base_id + 1, filename);
1922                 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1923             }
1924         }
1925         break;
1926       case CTRL_FONTSELECT:
1927         if (msg == WM_COMMAND && id == 2 &&
1928             (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1929             winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1930         if (id == 2 &&
1931             (msg == WM_COMMAND &&
1932              (HIWORD(wParam) == BN_CLICKED ||
1933               HIWORD(wParam) == BN_DOUBLECLICKED))) {
1934             CHOOSEFONT cf;
1935             LOGFONT lf;
1936             HDC hdc;
1937             FontSpec *fs = (FontSpec *)c->data;
1938
1939             hdc = GetDC(0);
1940             lf.lfHeight = -MulDiv(fs->height,
1941                                   GetDeviceCaps(hdc, LOGPIXELSY), 72);
1942             ReleaseDC(0, hdc);
1943             lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
1944             lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
1945             lf.lfWeight = (fs->isbold ? FW_BOLD : 0);
1946             lf.lfCharSet = fs->charset;
1947             lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
1948             lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1949             lf.lfQuality = DEFAULT_QUALITY;
1950             lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
1951             strncpy(lf.lfFaceName, fs->name,
1952                     sizeof(lf.lfFaceName) - 1);
1953             lf.lfFaceName[sizeof(lf.lfFaceName) - 1] = '\0';
1954
1955             cf.lStructSize = sizeof(cf);
1956             cf.hwndOwner = dp->hwnd;
1957             cf.lpLogFont = &lf;
1958             cf.Flags = (dp->fixed_pitch_fonts ? CF_FIXEDPITCHONLY : 0) |
1959                 CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
1960
1961             if (ChooseFont(&cf)) {
1962                 fs = fontspec_new(lf.lfFaceName, (lf.lfWeight == FW_BOLD),
1963                                   cf.iPointSize / 10, lf.lfCharSet);
1964                 dlg_fontsel_set(ctrl, dp, fs);
1965                 fontspec_free(fs);
1966
1967                 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1968             }
1969         }
1970         break;
1971     }
1972
1973     /*
1974      * If the above event handler has asked for a colour selector,
1975      * now is the time to generate one.
1976      */
1977     if (dp->coloursel_wanted) {
1978         static CHOOSECOLOR cc;
1979         static DWORD custom[16] = { 0 };    /* zero initialisers */
1980         cc.lStructSize = sizeof(cc);
1981         cc.hwndOwner = dp->hwnd;
1982         cc.hInstance = (HWND) hinst;
1983         cc.lpCustColors = custom;
1984         cc.rgbResult = RGB(dp->coloursel_result.r,
1985                            dp->coloursel_result.g,
1986                            dp->coloursel_result.b);
1987         cc.Flags = CC_FULLOPEN | CC_RGBINIT;
1988         if (ChooseColor(&cc)) {
1989             dp->coloursel_result.r =
1990                 (unsigned char) (cc.rgbResult & 0xFF);
1991             dp->coloursel_result.g =
1992                 (unsigned char) (cc.rgbResult >> 8) & 0xFF;
1993             dp->coloursel_result.b =
1994                 (unsigned char) (cc.rgbResult >> 16) & 0xFF;
1995             dp->coloursel_result.ok = TRUE;
1996         } else
1997             dp->coloursel_result.ok = FALSE;
1998         ctrl->generic.handler(ctrl, dp, dp->data, EVENT_CALLBACK);
1999     }
2000
2001     return ret;
2002 }
2003
2004 /*
2005  * This function can be called to produce context help on a
2006  * control. Returns TRUE if it has actually launched some help.
2007  */
2008 int winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id)
2009 {
2010     int i;
2011     struct winctrl *c;
2012
2013     /*
2014      * Look up the control ID in our data.
2015      */
2016     c = NULL;
2017     for (i = 0; i < dp->nctrltrees; i++) {
2018         c = winctrl_findbyid(dp->controltrees[i], id);
2019         if (c)
2020             break;
2021     }
2022     if (!c)
2023         return 0;                      /* we have nothing to do */
2024
2025     /*
2026      * This is the Windows front end, so we're allowed to assume
2027      * `helpctx.p' is a context string.
2028      */
2029     if (!c->ctrl || !c->ctrl->generic.helpctx.p)
2030         return 0;                      /* no help available for this ctrl */
2031
2032     launch_help(hwnd, c->ctrl->generic.helpctx.p);
2033     return 1;
2034 }
2035
2036 /*
2037  * Now the various functions that the platform-independent
2038  * mechanism can call to access the dialog box entries.
2039  */
2040
2041 static struct winctrl *dlg_findbyctrl(struct dlgparam *dp, union control *ctrl)
2042 {
2043     int i;
2044
2045     for (i = 0; i < dp->nctrltrees; i++) {
2046         struct winctrl *c = winctrl_findbyctrl(dp->controltrees[i], ctrl);
2047         if (c)
2048             return c;
2049     }
2050     return NULL;
2051 }
2052
2053 void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
2054 {
2055     struct dlgparam *dp = (struct dlgparam *)dlg;
2056     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2057     assert(c && c->ctrl->generic.type == CTRL_RADIO);
2058     CheckRadioButton(dp->hwnd,
2059                      c->base_id + 1,
2060                      c->base_id + c->ctrl->radio.nbuttons,
2061                      c->base_id + 1 + whichbutton);
2062 }
2063
2064 int dlg_radiobutton_get(union control *ctrl, void *dlg)
2065 {
2066     struct dlgparam *dp = (struct dlgparam *)dlg;
2067     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2068     int i;
2069     assert(c && c->ctrl->generic.type == CTRL_RADIO);
2070     for (i = 0; i < c->ctrl->radio.nbuttons; i++)
2071         if (IsDlgButtonChecked(dp->hwnd, c->base_id + 1 + i))
2072             return i;
2073     assert(!"No radio button was checked?!");
2074     return 0;
2075 }
2076
2077 void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
2078 {
2079     struct dlgparam *dp = (struct dlgparam *)dlg;
2080     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2081     assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
2082     CheckDlgButton(dp->hwnd, c->base_id, (checked != 0));
2083 }
2084
2085 int dlg_checkbox_get(union control *ctrl, void *dlg)
2086 {
2087     struct dlgparam *dp = (struct dlgparam *)dlg;
2088     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2089     assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
2090     return 0 != IsDlgButtonChecked(dp->hwnd, c->base_id);
2091 }
2092
2093 void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
2094 {
2095     struct dlgparam *dp = (struct dlgparam *)dlg;
2096     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2097     assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
2098     SetDlgItemText(dp->hwnd, c->base_id+1, text);
2099 }
2100
2101 char *dlg_editbox_get(union control *ctrl, void *dlg)
2102 {
2103     struct dlgparam *dp = (struct dlgparam *)dlg;
2104     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2105     assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
2106     return GetDlgItemText_alloc(dp->hwnd, c->base_id+1);
2107 }
2108
2109 /* The `listbox' functions can also apply to combo boxes. */
2110 void dlg_listbox_clear(union control *ctrl, void *dlg)
2111 {
2112     struct dlgparam *dp = (struct dlgparam *)dlg;
2113     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2114     int msg;
2115     assert(c &&
2116            (c->ctrl->generic.type == CTRL_LISTBOX ||
2117             (c->ctrl->generic.type == CTRL_EDITBOX &&
2118              c->ctrl->editbox.has_list)));
2119     msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2120            LB_RESETCONTENT : CB_RESETCONTENT);
2121     SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
2122 }
2123
2124 void dlg_listbox_del(union control *ctrl, void *dlg, int index)
2125 {
2126     struct dlgparam *dp = (struct dlgparam *)dlg;
2127     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2128     int msg;
2129     assert(c &&
2130            (c->ctrl->generic.type == CTRL_LISTBOX ||
2131             (c->ctrl->generic.type == CTRL_EDITBOX &&
2132              c->ctrl->editbox.has_list)));
2133     msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2134            LB_DELETESTRING : CB_DELETESTRING);
2135     SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2136 }
2137
2138 void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
2139 {
2140     struct dlgparam *dp = (struct dlgparam *)dlg;
2141     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2142     int msg;
2143     assert(c &&
2144            (c->ctrl->generic.type == CTRL_LISTBOX ||
2145             (c->ctrl->generic.type == CTRL_EDITBOX &&
2146              c->ctrl->editbox.has_list)));
2147     msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2148            LB_ADDSTRING : CB_ADDSTRING);
2149     SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
2150 }
2151
2152 /*
2153  * Each listbox entry may have a numeric id associated with it.
2154  * Note that some front ends only permit a string to be stored at
2155  * each position, which means that _if_ you put two identical
2156  * strings in any listbox then you MUST not assign them different
2157  * IDs and expect to get meaningful results back.
2158  */
2159 void dlg_listbox_addwithid(union control *ctrl, void *dlg,
2160                            char const *text, int id)
2161 {
2162     struct dlgparam *dp = (struct dlgparam *)dlg;
2163     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2164     int msg, msg2, index;
2165     assert(c &&
2166            (c->ctrl->generic.type == CTRL_LISTBOX ||
2167             (c->ctrl->generic.type == CTRL_EDITBOX &&
2168              c->ctrl->editbox.has_list)));
2169     msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2170            LB_ADDSTRING : CB_ADDSTRING);
2171     msg2 = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2172            LB_SETITEMDATA : CB_SETITEMDATA);
2173     index = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
2174     SendDlgItemMessage(dp->hwnd, c->base_id+1, msg2, index, (LPARAM)id);
2175 }
2176
2177 int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
2178 {
2179     struct dlgparam *dp = (struct dlgparam *)dlg;
2180     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2181     int msg;
2182     assert(c && c->ctrl->generic.type == CTRL_LISTBOX);
2183     msg = (c->ctrl->listbox.height != 0 ? LB_GETITEMDATA : CB_GETITEMDATA);
2184     return
2185         SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2186 }
2187
2188 /* dlg_listbox_index returns <0 if no single element is selected. */
2189 int dlg_listbox_index(union control *ctrl, void *dlg)
2190 {
2191     struct dlgparam *dp = (struct dlgparam *)dlg;
2192     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2193     int msg, ret;
2194     assert(c && c->ctrl->generic.type == CTRL_LISTBOX);
2195     if (c->ctrl->listbox.multisel) {
2196         assert(c->ctrl->listbox.height != 0); /* not combo box */
2197         ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSELCOUNT, 0, 0);
2198         if (ret == LB_ERR || ret > 1)
2199             return -1;
2200     }
2201     msg = (c->ctrl->listbox.height != 0 ? LB_GETCURSEL : CB_GETCURSEL);
2202     ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
2203     if (ret == LB_ERR)
2204         return -1;
2205     else
2206         return ret;
2207 }
2208
2209 int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
2210 {
2211     struct dlgparam *dp = (struct dlgparam *)dlg;
2212     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2213     assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2214            c->ctrl->listbox.multisel &&
2215            c->ctrl->listbox.height != 0);
2216     return
2217         SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSEL, index, 0);
2218 }
2219
2220 void dlg_listbox_select(union control *ctrl, void *dlg, int index)
2221 {
2222     struct dlgparam *dp = (struct dlgparam *)dlg;
2223     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2224     int msg;
2225     assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2226            !c->ctrl->listbox.multisel);
2227     msg = (c->ctrl->listbox.height != 0 ? LB_SETCURSEL : CB_SETCURSEL);
2228     SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2229 }
2230
2231 void dlg_text_set(union control *ctrl, void *dlg, char const *text)
2232 {
2233     struct dlgparam *dp = (struct dlgparam *)dlg;
2234     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2235     assert(c && c->ctrl->generic.type == CTRL_TEXT);
2236     SetDlgItemText(dp->hwnd, c->base_id, text);
2237 }
2238
2239 void dlg_label_change(union control *ctrl, void *dlg, char const *text)
2240 {
2241     struct dlgparam *dp = (struct dlgparam *)dlg;
2242     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2243     char *escaped = NULL;
2244     int id = -1;
2245
2246     assert(c);
2247     switch (c->ctrl->generic.type) {
2248       case CTRL_EDITBOX:
2249         escaped = shortcut_escape(text, c->ctrl->editbox.shortcut);
2250         id = c->base_id;
2251         break;
2252       case CTRL_RADIO:
2253         escaped = shortcut_escape(text, c->ctrl->radio.shortcut);
2254         id = c->base_id;
2255         break;
2256       case CTRL_CHECKBOX:
2257         escaped = shortcut_escape(text, ctrl->checkbox.shortcut);
2258         id = c->base_id;
2259         break;
2260       case CTRL_BUTTON:
2261         escaped = shortcut_escape(text, ctrl->button.shortcut);
2262         id = c->base_id;
2263         break;
2264       case CTRL_LISTBOX:
2265         escaped = shortcut_escape(text, ctrl->listbox.shortcut);
2266         id = c->base_id;
2267         break;
2268       case CTRL_FILESELECT:
2269         escaped = shortcut_escape(text, ctrl->fileselect.shortcut);
2270         id = c->base_id;
2271         break;
2272       case CTRL_FONTSELECT:
2273         escaped = shortcut_escape(text, ctrl->fontselect.shortcut);
2274         id = c->base_id;
2275         break;
2276       default:
2277         assert(!"Can't happen");
2278         break;
2279     }
2280     if (escaped) {
2281         SetDlgItemText(dp->hwnd, id, escaped);
2282         sfree(escaped);
2283     }
2284 }
2285
2286 void dlg_filesel_set(union control *ctrl, void *dlg, Filename *fn)
2287 {
2288     struct dlgparam *dp = (struct dlgparam *)dlg;
2289     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2290     assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
2291     SetDlgItemText(dp->hwnd, c->base_id+1, fn->path);
2292 }
2293
2294 Filename *dlg_filesel_get(union control *ctrl, void *dlg)
2295 {
2296     struct dlgparam *dp = (struct dlgparam *)dlg;
2297     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2298     char *tmp;
2299     Filename *ret;
2300     assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
2301     tmp = GetDlgItemText_alloc(dp->hwnd, c->base_id+1);
2302     ret = filename_from_str(tmp);
2303     sfree(tmp);
2304     return ret;
2305 }
2306
2307 void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec *fs)
2308 {
2309     char *buf, *boldstr;
2310     struct dlgparam *dp = (struct dlgparam *)dlg;
2311     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2312     assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
2313
2314     fontspec_free((FontSpec *)c->data);
2315     c->data = fontspec_copy(fs);
2316
2317     boldstr = (fs->isbold ? "bold, " : "");
2318     if (fs->height == 0)
2319         buf = dupprintf("Font: %s, %sdefault height", fs->name, boldstr);
2320     else
2321         buf = dupprintf("Font: %s, %s%d-%s", fs->name, boldstr,
2322                         (fs->height < 0 ? -fs->height : fs->height),
2323                         (fs->height < 0 ? "pixel" : "point"));
2324     SetDlgItemText(dp->hwnd, c->base_id+1, buf);
2325     sfree(buf);
2326
2327     dlg_auto_set_fixed_pitch_flag(dp);
2328 }
2329
2330 FontSpec *dlg_fontsel_get(union control *ctrl, void *dlg)
2331 {
2332     struct dlgparam *dp = (struct dlgparam *)dlg;
2333     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2334     assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
2335     return fontspec_copy((FontSpec *)c->data);
2336 }
2337
2338 /*
2339  * Bracketing a large set of updates in these two functions will
2340  * cause the front end (if possible) to delay updating the screen
2341  * until it's all complete, thus avoiding flicker.
2342  */
2343 void dlg_update_start(union control *ctrl, void *dlg)
2344 {
2345     struct dlgparam *dp = (struct dlgparam *)dlg;
2346     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2347     if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
2348         SendDlgItemMessage(dp->hwnd, c->base_id+1, WM_SETREDRAW, FALSE, 0);
2349     }
2350 }
2351
2352 void dlg_update_done(union control *ctrl, void *dlg)
2353 {
2354     struct dlgparam *dp = (struct dlgparam *)dlg;
2355     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2356     if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
2357         HWND hw = GetDlgItem(dp->hwnd, c->base_id+1);
2358         SendMessage(hw, WM_SETREDRAW, TRUE, 0);
2359         InvalidateRect(hw, NULL, TRUE);
2360     }
2361 }
2362
2363 void dlg_set_focus(union control *ctrl, void *dlg)
2364 {
2365     struct dlgparam *dp = (struct dlgparam *)dlg;
2366     struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2367     int id;
2368     HWND ctl;
2369     if (!c)
2370         return;
2371     switch (ctrl->generic.type) {
2372       case CTRL_EDITBOX: id = c->base_id + 1; break;
2373       case CTRL_RADIO:
2374         for (id = c->base_id + ctrl->radio.nbuttons; id > 1; id--)
2375             if (IsDlgButtonChecked(dp->hwnd, id))
2376                 break;
2377         /*
2378          * In the theoretically-unlikely case that no button was
2379          * selected, id should come out of this as 1, which is a
2380          * reasonable enough choice.
2381          */
2382         break;
2383       case CTRL_CHECKBOX: id = c->base_id; break;
2384       case CTRL_BUTTON: id = c->base_id; break;
2385       case CTRL_LISTBOX: id = c->base_id + 1; break;
2386       case CTRL_FILESELECT: id = c->base_id + 1; break;
2387       case CTRL_FONTSELECT: id = c->base_id + 2; break;
2388       default: id = c->base_id; break;
2389     }
2390     ctl = GetDlgItem(dp->hwnd, id);
2391     SetFocus(ctl);
2392 }
2393
2394 /*
2395  * During event processing, you might well want to give an error
2396  * indication to the user. dlg_beep() is a quick and easy generic
2397  * error; dlg_error() puts up a message-box or equivalent.
2398  */
2399 void dlg_beep(void *dlg)
2400 {
2401     /* struct dlgparam *dp = (struct dlgparam *)dlg; */
2402     MessageBeep(0);
2403 }
2404
2405 void dlg_error_msg(void *dlg, const char *msg)
2406 {
2407     struct dlgparam *dp = (struct dlgparam *)dlg;
2408     MessageBox(dp->hwnd, msg,
2409                dp->errtitle ? dp->errtitle : NULL,
2410                MB_OK | MB_ICONERROR);
2411 }
2412
2413 /*
2414  * This function signals to the front end that the dialog's
2415  * processing is completed, and passes an integer value (typically
2416  * a success status).
2417  */
2418 void dlg_end(void *dlg, int value)
2419 {
2420     struct dlgparam *dp = (struct dlgparam *)dlg;
2421     dp->ended = TRUE;
2422     dp->endresult = value;
2423 }
2424
2425 void dlg_refresh(union control *ctrl, void *dlg)
2426 {
2427     struct dlgparam *dp = (struct dlgparam *)dlg;
2428     int i, j;
2429     struct winctrl *c;
2430
2431     if (!ctrl) {
2432         /*
2433          * Send EVENT_REFRESH to absolutely everything.
2434          */
2435         for (j = 0; j < dp->nctrltrees; j++) {
2436             for (i = 0;
2437                  (c = winctrl_findbyindex(dp->controltrees[j], i)) != NULL;
2438                  i++) {
2439                 if (c->ctrl && c->ctrl->generic.handler != NULL)
2440                     c->ctrl->generic.handler(c->ctrl, dp,
2441                                              dp->data, EVENT_REFRESH);
2442             }
2443         }
2444     } else {
2445         /*
2446          * Send EVENT_REFRESH to a specific control.
2447          */
2448         if (ctrl->generic.handler != NULL)
2449             ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
2450     }
2451 }
2452
2453 void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
2454 {
2455     struct dlgparam *dp = (struct dlgparam *)dlg;
2456     dp->coloursel_wanted = TRUE;
2457     dp->coloursel_result.r = r;
2458     dp->coloursel_result.g = g;
2459     dp->coloursel_result.b = b;
2460 }
2461
2462 int dlg_coloursel_results(union control *ctrl, void *dlg,
2463                           int *r, int *g, int *b)
2464 {
2465     struct dlgparam *dp = (struct dlgparam *)dlg;
2466     if (dp->coloursel_result.ok) {
2467         *r = dp->coloursel_result.r;
2468         *g = dp->coloursel_result.g;
2469         *b = dp->coloursel_result.b;
2470         return 1;
2471     } else
2472         return 0;
2473 }
2474
2475 void dlg_auto_set_fixed_pitch_flag(void *dlg)
2476 {
2477     struct dlgparam *dp = (struct dlgparam *)dlg;
2478     Conf *conf = (Conf *)dp->data;
2479     FontSpec *fs;
2480     int quality;
2481     HFONT hfont;
2482     HDC hdc;
2483     TEXTMETRIC tm;
2484     int is_var;
2485
2486     /*
2487      * Attempt to load the current font, and see if it's
2488      * variable-pitch. If so, start off the fixed-pitch flag for the
2489      * dialog box as false.
2490      *
2491      * We assume here that any client of the dlg_* mechanism which is
2492      * using font selectors at all is also using a normal 'Conf *'
2493      * as dp->data.
2494      */
2495
2496     quality = conf_get_int(conf, CONF_font_quality);
2497     fs = conf_get_fontspec(conf, CONF_font);
2498
2499     hfont = CreateFont(0, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE,
2500                        DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
2501                        CLIP_DEFAULT_PRECIS, FONT_QUALITY(quality),
2502                        FIXED_PITCH | FF_DONTCARE, fs->name);
2503     hdc = GetDC(NULL);
2504     if (hdc && SelectObject(hdc, hfont) && GetTextMetrics(hdc, &tm)) {
2505         /* Note that the TMPF_FIXED_PITCH bit is defined upside down :-( */
2506         is_var = (tm.tmPitchAndFamily & TMPF_FIXED_PITCH);
2507     } else {
2508         is_var = FALSE;                /* assume it's basically normal */
2509     }
2510     if (hdc)
2511         ReleaseDC(NULL, hdc);
2512     if (hfont)
2513         DeleteObject(hfont);
2514
2515     if (is_var)
2516         dp->fixed_pitch_fonts = FALSE;
2517 }
2518
2519 int dlg_get_fixed_pitch_flag(void *dlg)
2520 {
2521     struct dlgparam *dp = (struct dlgparam *)dlg;
2522     return dp->fixed_pitch_fonts;
2523 }
2524
2525 void dlg_set_fixed_pitch_flag(void *dlg, int flag)
2526 {
2527     struct dlgparam *dp = (struct dlgparam *)dlg;
2528     dp->fixed_pitch_fonts = flag;
2529 }
2530
2531 void dp_init(struct dlgparam *dp)
2532 {
2533     dp->nctrltrees = 0;
2534     dp->data = NULL;
2535     dp->ended = FALSE;
2536     dp->focused = dp->lastfocused = NULL;
2537     memset(dp->shortcuts, 0, sizeof(dp->shortcuts));
2538     dp->hwnd = NULL;
2539     dp->wintitle = dp->errtitle = NULL;
2540     dp->fixed_pitch_fonts = TRUE;
2541 }
2542
2543 void dp_add_tree(struct dlgparam *dp, struct winctrls *wc)
2544 {
2545     assert(dp->nctrltrees < lenof(dp->controltrees));
2546     dp->controltrees[dp->nctrltrees++] = wc;
2547 }
2548
2549 void dp_cleanup(struct dlgparam *dp)
2550 {
2551     sfree(dp->wintitle);
2552     sfree(dp->errtitle);
2553 }