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