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