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