Introduced wrapper macros snew(), snewn() and sresize() for the
[u/mdw/putty] / winctrls.c
CommitLineData
8c3cd914 1/*
2 * winctrls.c: routines to self-manage the controls in a dialog
3 * box.
4 */
5
fe8abbf4 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
8c3cd914 16#include <windows.h>
6e522441 17#include <commctrl.h>
fe8abbf4 18#include <assert.h>
d1582b2e 19#include <ctype.h>
8c3cd914 20
21#include "winstuff.h"
fe8abbf4 22#include "misc.h"
23#include "dialog.h"
ca20bfcf 24#include "puttymem.h"
25
26#include "putty.h"
8c3cd914 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
fe8abbf4 34#define TITLEHEIGHT 12
8c3cd914 35#define CHECKBOXHEIGHT 8
36#define RADIOHEIGHT 8
37#define EDITHEIGHT 12
fe8abbf4 38#define LISTHEIGHT 11
39#define LISTINCREMENT 8
8c3cd914 40#define COMBOHEIGHT 12
41#define PUSHBTNHEIGHT 14
6e522441 42#define PROGBARHEIGHT 14
8c3cd914 43
44void ctlposinit(struct ctlpos *cp, HWND hwnd,
32874aea 45 int leftborder, int rightborder, int topborder)
46{
8c3cd914 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;
32874aea 57 cp->width = (r.right * 4) / (r2.right) - 2 * GAPBETWEEN;
8c3cd914 58 cp->xoff = leftborder;
59 cp->width -= leftborder + rightborder;
60}
61
ca20bfcf 62HWND doctl(struct ctlpos *cp, RECT r,
32874aea 63 char *wclass, int wstyle, int exstyle, char *wtext, int wid)
64{
8c3cd914 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
fe8abbf4 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 }
d1582b2e 98 } else
99 ctl = NULL;
ca20bfcf 100 return ctl;
8c3cd914 101}
102
103/*
104 * A title bar across the top of a sub-dialog.
105 */
32874aea 106void bartitle(struct ctlpos *cp, char *name, int id)
107{
8c3cd914 108 RECT r;
109
32874aea 110 r.left = GAPBETWEEN;
111 r.right = cp->width;
112 r.top = cp->ypos;
113 r.bottom = STATICHEIGHT;
8c3cd914 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 */
32874aea 121void beginbox(struct ctlpos *cp, char *name, int idbox)
122{
8c3cd914 123 cp->boxystart = cp->ypos;
3ac9cd9f 124 if (!name)
32874aea 125 cp->boxystart -= STATICHEIGHT / 2;
8c3cd914 126 if (name)
32874aea 127 cp->ypos += STATICHEIGHT;
8c3cd914 128 cp->ypos += GAPYBOX;
32874aea 129 cp->width -= 2 * GAPXBOX;
8c3cd914 130 cp->xoff += GAPXBOX;
131 cp->boxid = idbox;
8c3cd914 132 cp->boxtext = name;
133}
134
135/*
136 * End a grouping box.
137 */
32874aea 138void endbox(struct ctlpos *cp)
139{
8c3cd914 140 RECT r;
141 cp->xoff -= GAPXBOX;
32874aea 142 cp->width += 2 * GAPXBOX;
8c3cd914 143 cp->ypos += GAPYBOX - GAPBETWEEN;
32874aea 144 r.left = GAPBETWEEN;
145 r.right = cp->width;
146 r.top = cp->boxystart;
147 r.bottom = cp->ypos - cp->boxystart;
3ac9cd9f 148 doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0,
32874aea 149 cp->boxtext ? cp->boxtext : "", cp->boxid);
8c3cd914 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 */
fe8abbf4 157void multiedit(struct ctlpos *cp, int password, ...)
32874aea 158{
8c3cd914 159 RECT r;
160 va_list ap;
161 int percent, xpos;
162
163 percent = xpos = 0;
fe8abbf4 164 va_start(ap, password);
8c3cd914 165 while (1) {
32874aea 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",
fe8abbf4 186 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL |
187 (password ? ES_PASSWORD : 0),
32874aea 188 WS_EX_CLIENTEDGE, "", editid);
8c3cd914 189 }
190 va_end(ap);
875b193f 191 cp->ypos += STATICHEIGHT + GAPWITHIN + EDITHEIGHT + GAPBETWEEN;
192}
193
194/*
b8ae1f0f 195 * A static line, followed by a full-width combo box.
875b193f 196 */
b8ae1f0f 197void combobox(struct ctlpos *cp, char *text, int staticid, int listid)
875b193f 198{
199 RECT r;
875b193f 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 |
b8ae1f0f 211 CBS_DROPDOWN | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
875b193f 212
213 cp->ypos += STATICHEIGHT + GAPWITHIN + COMBOHEIGHT + GAPBETWEEN;
8c3cd914 214}
215
fe8abbf4 216struct radio { char *text; int id; };
217
218static void radioline_common(struct ctlpos *cp, char *text, int id,
219 int nacross, struct radio *buttons, int nbuttons)
32874aea 220{
8c3cd914 221 RECT r;
8c3cd914 222 int group;
223 int i;
fe8abbf4 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 }
8c3cd914 234
8c3cd914 235 group = WS_GROUP;
236 i = 0;
fe8abbf4 237 for (j = 0; j < nbuttons; j++) {
238 char *btext = buttons[j].text;
239 int bid = buttons[j].id;
240
32874aea 241 if (i == nacross) {
fe8abbf4 242 cp->ypos += r.bottom + (nacross > 1 ? GAPBETWEEN : GAPWITHIN);
32874aea 243 i = 0;
f37caa11 244 }
32874aea 245 r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
fe8abbf4 246 if (j < nbuttons-1)
32874aea 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",
fe8abbf4 254 BS_NOTIFY | BS_AUTORADIOBUTTON | WS_CHILD |
255 WS_VISIBLE | WS_TABSTOP | group, 0, btext, bid);
32874aea 256 group = 0;
257 i++;
8c3cd914 258 }
8c3cd914 259 cp->ypos += r.bottom + GAPBETWEEN;
260}
261
262/*
d74d141c 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 */
276void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...)
277{
d74d141c 278 va_list ap;
fe8abbf4 279 struct radio *buttons;
280 int i, nbuttons;
d74d141c 281
d74d141c 282 va_start(ap, nacross);
fe8abbf4 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);
3d88e64d 292 buttons = snewn(nbuttons, struct radio);
fe8abbf4 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 }
d74d141c 298 va_end(ap);
fe8abbf4 299 radioline_common(cp, text, id, nacross, buttons, nbuttons);
300 sfree(buttons);
d74d141c 301}
302
303/*
304 * A set of radio buttons on the same line, without a static above
305 * them. Otherwise just like radioline.
306 */
307void bareradioline(struct ctlpos *cp, int nacross, ...)
308{
309 va_list ap;
fe8abbf4 310 struct radio *buttons;
311 int i, nbuttons;
d74d141c 312
313 va_start(ap, nacross);
fe8abbf4 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);
3d88e64d 323 buttons = snewn(nbuttons, struct radio);
fe8abbf4 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 }
d74d141c 329 va_end(ap);
fe8abbf4 330 radioline_common(cp, NULL, 0, nacross, buttons, nbuttons);
331 sfree(buttons);
d74d141c 332}
333
334/*
8c3cd914 335 * A set of radio buttons on multiple lines, with a static above
336 * them.
337 */
32874aea 338void radiobig(struct ctlpos *cp, char *text, int id, ...)
339{
8c3cd914 340 va_list ap;
fe8abbf4 341 struct radio *buttons;
342 int i, nbuttons;
8c3cd914 343
8c3cd914 344 va_start(ap, id);
fe8abbf4 345 nbuttons = 0;
8c3cd914 346 while (1) {
fe8abbf4 347 char *btext = va_arg(ap, char *);
32874aea 348 int bid;
32874aea 349 if (!btext)
350 break;
351 bid = va_arg(ap, int);
8c3cd914 352 }
353 va_end(ap);
3d88e64d 354 buttons = snewn(nbuttons, struct radio);
fe8abbf4 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);
8c3cd914 363}
364
365/*
366 * A single standalone checkbox.
367 */
32874aea 368void checkbox(struct ctlpos *cp, char *text, int id)
369{
8c3cd914 370 RECT r;
371
32874aea 372 r.left = GAPBETWEEN;
373 r.top = cp->ypos;
374 r.right = cp->width;
375 r.bottom = CHECKBOXHEIGHT;
8c3cd914 376 cp->ypos += r.bottom + GAPBETWEEN;
377 doctl(cp, r, "BUTTON",
fe8abbf4 378 BS_NOTIFY | BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
32874aea 379 text, id);
8c3cd914 380}
381
382/*
fe8abbf4 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 */
387char *staticwrap(struct ctlpos *cp, HWND hwnd, char *text, int *lines)
388{
fe8abbf4 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;
97332719 396 HFONT oldfont, newfont;
fe8abbf4 397
3d88e64d 398 ret = snewn(1+strlen(text), char);
fe8abbf4 399 p = text;
400 q = ret;
3d88e64d 401 pwidths = snewn(1+strlen(text), INT);
fe8abbf4 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.
fe8abbf4 407 */
97332719 408 SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
fe8abbf4 409 r.left = r.top = r.bottom = 0;
410 r.right = cp->width;
411 MapDialogRect(hwnd, &r);
97332719 412 width = r.right;
fe8abbf4 413
414 nlines = 1;
415
97332719 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
fe8abbf4 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
97332719 461 SelectObject(hdc, oldfont);
fe8abbf4 462 ReleaseDC(cp->hwnd, hdc);
463
464 if (lines) *lines = nlines;
465
466 return ret;
467}
468
469/*
6e522441 470 * A single standalone static text control.
471 */
66ee282a 472void statictext(struct ctlpos *cp, char *text, int lines, int id)
32874aea 473{
6e522441 474 RECT r;
475
32874aea 476 r.left = GAPBETWEEN;
477 r.top = cp->ypos;
478 r.right = cp->width;
66ee282a 479 r.bottom = STATICHEIGHT * lines;
6e522441 480 cp->ypos += r.bottom + GAPBETWEEN;
fe8abbf4 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 */
489void 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);
6e522441 500}
501
502/*
8c3cd914 503 * A button on the right hand side, with a static to its left.
504 */
505void staticbtn(struct ctlpos *cp, char *stext, int sid,
32874aea 506 char *btext, int bid)
507{
8c3cd914 508 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
32874aea 509 PUSHBTNHEIGHT : STATICHEIGHT);
8c3cd914 510 RECT r;
511 int lwid, rwid, rpos;
512
513 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
32874aea 514 lwid = rpos - 2 * GAPBETWEEN;
8c3cd914 515 rwid = cp->width + GAPBETWEEN - rpos;
516
32874aea 517 r.left = GAPBETWEEN;
518 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
519 r.right = lwid;
520 r.bottom = STATICHEIGHT;
8c3cd914 521 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
522
32874aea 523 r.left = rpos;
524 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
525 r.right = rwid;
526 r.bottom = PUSHBTNHEIGHT;
8c3cd914 527 doctl(cp, r, "BUTTON",
fe8abbf4 528 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
32874aea 529 0, btext, bid);
8c3cd914 530
531 cp->ypos += height + GAPBETWEEN;
532}
533
534/*
fe8abbf4 535 * A simple push button.
536 */
537void 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/*
af282e3b 560 * Like staticbtn, but two buttons.
561 */
562void 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",
fe8abbf4 587 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
af282e3b 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",
fe8abbf4 595 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
af282e3b 596 0, btext2, bid2);
597
598 cp->ypos += height + GAPBETWEEN;
599}
600
601/*
8c3cd914 602 * An edit control on the right hand side, with a static to its left.
603 */
6e522441 604static void staticedit_internal(struct ctlpos *cp, char *stext,
32874aea 605 int sid, int eid, int percentedit,
606 int style)
607{
8c3cd914 608 const int height = (EDITHEIGHT > STATICHEIGHT ?
32874aea 609 EDITHEIGHT : STATICHEIGHT);
8c3cd914 610 RECT r;
611 int lwid, rwid, rpos;
612
32874aea 613 rpos =
614 GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
615 lwid = rpos - 2 * GAPBETWEEN;
8c3cd914 616 rwid = cp->width + GAPBETWEEN - rpos;
617
32874aea 618 r.left = GAPBETWEEN;
619 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
620 r.right = lwid;
621 r.bottom = STATICHEIGHT;
8c3cd914 622 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
623
32874aea 624 r.left = rpos;
625 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
626 r.right = rwid;
627 r.bottom = EDITHEIGHT;
8c3cd914 628 doctl(cp, r, "EDIT",
32874aea 629 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
630 WS_EX_CLIENTEDGE, "", eid);
8c3cd914 631
632 cp->ypos += height + GAPBETWEEN;
633}
634
6e522441 635void staticedit(struct ctlpos *cp, char *stext,
32874aea 636 int sid, int eid, int percentedit)
637{
6e522441 638 staticedit_internal(cp, stext, sid, eid, percentedit, 0);
639}
640
641void staticpassedit(struct ctlpos *cp, char *stext,
32874aea 642 int sid, int eid, int percentedit)
643{
6e522441 644 staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
645}
646
647/*
2c9c6388 648 * A drop-down list box on the right hand side, with a static to
649 * its left.
650 */
651void 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/*
fe8abbf4 682 * A combo box on the right hand side, with a static to its left.
683 */
684void 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 */
717void 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/*
6e522441 740 * A big multiline edit control with a static labelling it.
741 */
742void bigeditctrl(struct ctlpos *cp, char *stext,
32874aea 743 int sid, int eid, int lines)
744{
6e522441 745 RECT r;
746
32874aea 747 r.left = GAPBETWEEN;
748 r.top = cp->ypos;
749 r.right = cp->width;
750 r.bottom = STATICHEIGHT;
6e522441 751 cp->ypos += r.bottom + GAPWITHIN;
752 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
753
32874aea 754 r.left = GAPBETWEEN;
755 r.top = cp->ypos;
756 r.right = cp->width;
757 r.bottom = EDITHEIGHT + (lines - 1) * STATICHEIGHT;
6e522441 758 cp->ypos += r.bottom + GAPBETWEEN;
759 doctl(cp, r, "EDIT",
32874aea 760 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
761 WS_EX_CLIENTEDGE, "", eid);
6e522441 762}
763
8c3cd914 764/*
fe8abbf4 765 * A list box with a static labelling it.
766 */
767void 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/*
8c3cd914 794 * A tab-control substitute when a real tab control is unavailable.
795 */
32874aea 796void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id)
797{
8c3cd914 798 const int height = (COMBOHEIGHT > STATICHEIGHT ?
32874aea 799 COMBOHEIGHT : STATICHEIGHT);
8c3cd914 800 RECT r;
801 int bigwid, lwid, rwid, rpos;
802 static const int BIGGAP = 15;
803 static const int MEDGAP = 3;
804
32874aea 805 bigwid = cp->width + 2 * GAPBETWEEN - 2 * BIGGAP;
8c3cd914 806 cp->ypos += MEDGAP;
807 rpos = BIGGAP + (bigwid + BIGGAP) / 2;
32874aea 808 lwid = rpos - 2 * BIGGAP;
8c3cd914 809 rwid = bigwid + BIGGAP - rpos;
810
32874aea 811 r.left = BIGGAP;
812 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
813 r.right = lwid;
814 r.bottom = STATICHEIGHT;
8c3cd914 815 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
816
32874aea 817 r.left = rpos;
818 r.top = cp->ypos + (height - COMBOHEIGHT) / 2;
819 r.right = rwid;
820 r.bottom = COMBOHEIGHT * 10;
8c3cd914 821 doctl(cp, r, "COMBOBOX",
32874aea 822 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
823 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
8c3cd914 824
825 cp->ypos += height + MEDGAP + GAPBETWEEN;
826
32874aea 827 r.left = GAPBETWEEN;
828 r.top = cp->ypos;
829 r.right = cp->width;
830 r.bottom = 2;
8c3cd914 831 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
32874aea 832 0, "", s2id);
8c3cd914 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 */
839void editbutton(struct ctlpos *cp, char *stext, int sid,
32874aea 840 int eid, char *btext, int bid)
841{
8c3cd914 842 const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
32874aea 843 EDITHEIGHT : PUSHBTNHEIGHT);
8c3cd914 844 RECT r;
845 int lwid, rwid, rpos;
846
32874aea 847 r.left = GAPBETWEEN;
848 r.top = cp->ypos;
849 r.right = cp->width;
850 r.bottom = STATICHEIGHT;
8c3cd914 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;
32874aea 855 lwid = rpos - 2 * GAPBETWEEN;
8c3cd914 856 rwid = cp->width + GAPBETWEEN - rpos;
857
32874aea 858 r.left = GAPBETWEEN;
859 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
860 r.right = lwid;
861 r.bottom = EDITHEIGHT;
8c3cd914 862 doctl(cp, r, "EDIT",
32874aea 863 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
864 WS_EX_CLIENTEDGE, "", eid);
8c3cd914 865
32874aea 866 r.left = rpos;
867 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
868 r.right = rwid;
869 r.bottom = PUSHBTNHEIGHT;
8c3cd914 870 doctl(cp, r, "BUTTON",
fe8abbf4 871 BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
32874aea 872 0, btext, bid);
8c3cd914 873
874 cp->ypos += height + GAPBETWEEN;
875}
876
877/*
fe8abbf4 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.
8c3cd914 881 */
fe8abbf4 882void prefslist(struct prefslist *hdl, struct ctlpos *cp, int lines,
883 char *stext, int sid, int listid, int upbid, int dnbid)
32874aea 884{
fe8abbf4 885 const static int percents[] = { 5, 75, 20 };
8c3cd914 886 RECT r;
fe8abbf4 887 int xpos, percent = 0, i;
888 int listheight = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
889 const int BTNSHEIGHT = 2*PUSHBTNHEIGHT + GAPBETWEEN;
890 int totalheight, buttonpos;
8c3cd914 891
fe8abbf4 892 /* Squirrel away IDs. */
893 hdl->listid = listid;
894 hdl->upbid = upbid;
895 hdl->dnbid = dnbid;
8c3cd914 896
fe8abbf4 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);
8c3cd914 905 }
906
fe8abbf4 907 if (listheight > BTNSHEIGHT) {
908 totalheight = listheight;
909 buttonpos = (listheight - BTNSHEIGHT) / 2;
910 } else {
911 totalheight = BTNSHEIGHT;
912 buttonpos = 0;
ca20bfcf 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;
fe8abbf4 927 r.top = cp->ypos; r.bottom = listheight;
ca20bfcf 928 {
929 HWND ctl;
930 ctl = doctl(cp, r, "LISTBOX",
931 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
fe8abbf4 932 WS_VSCROLL | LBS_HASSTRINGS | LBS_USETABSTOPS,
ca20bfcf 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;
fe8abbf4 944 r.top = cp->ypos + buttonpos; r.bottom = PUSHBTNHEIGHT;
ca20bfcf 945 doctl(cp, r, "BUTTON",
fe8abbf4 946 BS_NOTIFY | WS_CHILD | WS_VISIBLE |
947 WS_TABSTOP | BS_PUSHBUTTON,
ca20bfcf 948 0, "&Up", upbid);
949
950 r.left = left; r.right = wid;
fe8abbf4 951 r.top = cp->ypos + buttonpos + PUSHBTNHEIGHT + GAPBETWEEN;
ca20bfcf 952 r.bottom = PUSHBTNHEIGHT;
953 doctl(cp, r, "BUTTON",
fe8abbf4 954 BS_NOTIFY | WS_CHILD | WS_VISIBLE |
955 WS_TABSTOP | BS_PUSHBUTTON,
ca20bfcf 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 */
970static 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);
3d88e64d 976 txt = snewn(tlen+1, char);
ca20bfcf 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
993int 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);
ca20bfcf 1011 if (ret == -1)
1012 return ret;
1013 ret = LBItemFromPt(hwnd, cursor, FALSE);
ca20bfcf 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.
fe8abbf4 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.
ca20bfcf 1037 */
1038int 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;
fe8abbf4 1044 int ret = 0;
ca20bfcf 1045
1046 if (is_dlmsg) {
1047
cdcbdf3b 1048 if ((int)wParam == hdl->listid) {
ca20bfcf 1049 DRAGLISTINFO *dlm = (DRAGLISTINFO *)lParam;
d1582b2e 1050 int dest = 0; /* initialise to placate gcc */
ca20bfcf 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);
fe8abbf4 1061 ret |= 1; break;
ca20bfcf 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;
fe8abbf4 1067 ret |= 1; break;
ca20bfcf 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);
fe8abbf4 1077 ret |= 1; break;
ca20bfcf 1078 case DL_DROPPED:
2b241edd 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 }
ca20bfcf 1084 SendDlgItemMessage(hwnd, hdl->listid,
1085 LB_DELETESTRING, hdl->dummyitem, 0);
2b241edd 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 }
fe8abbf4 1093 ret |= 2;
ca20bfcf 1094 }
fe8abbf4 1095 ret |= 1; break;
ca20bfcf 1096 }
1097 }
1098
1099 } else {
1100
ca20bfcf 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);
fe8abbf4 1119 ret |= 2;
ca20bfcf 1120 }
1121
1122 }
1123
1124 }
1125
fe8abbf4 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 }
ca20bfcf 1132
1133 return ret;
ca20bfcf 1134}
1135
1136/*
6e522441 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 */
32874aea 1141void progressbar(struct ctlpos *cp, int id)
1142{
6e522441 1143 RECT r;
1144
32874aea 1145 r.left = GAPBETWEEN;
1146 r.top = cp->ypos;
1147 r.right = cp->width;
1148 r.bottom = PROGBARHEIGHT;
6e522441 1149 cp->ypos += r.bottom + GAPBETWEEN;
1150
32874aea 1151 doctl(cp, r, PROGRESS_CLASS, WS_CHILD | WS_VISIBLE
6e522441 1152#ifdef PBS_SMOOTH
32874aea 1153 | PBS_SMOOTH
6e522441 1154#endif
32874aea 1155 , WS_EX_CLIENTEDGE, "", id);
6e522441 1156}
d74d141c 1157
fe8abbf4 1158/* ----------------------------------------------------------------------
1159 * Platform-specific side of portable dialog-box mechanism.
1160 */
1161
d74d141c 1162/*
fe8abbf4 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.
d74d141c 1170 */
fe8abbf4 1171static 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
3d88e64d 1179 ret = snewn(2*strlen(text)+1, char); /* size potentially doubles! */
fe8abbf4 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}
d74d141c 1197
fe8abbf4 1198void 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
1209void 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
1220static 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}
1231static 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}
1242static 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}
1253static 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
1265void winctrl_init(struct winctrls *wc)
1266{
1267 wc->byctrl = newtree234(winctrl_cmp_byctrl);
1268 wc->byid = newtree234(winctrl_cmp_byid);
1269}
1270void 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 }
d74d141c 1279
fe8abbf4 1280 freetree234(wc->byctrl);
1281 freetree234(wc->byid);
1282 wc->byctrl = wc->byid = NULL;
1283}
1284
1285void 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
1296void 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
1304struct winctrl *winctrl_findbyctrl(struct winctrls *wc, union control *ctrl)
1305{
1306 return find234(wc->byctrl, ctrl, winctrl_cmp_byctrl_find);
1307}
1308
1309struct winctrl *winctrl_findbyid(struct winctrls *wc, int id)
1310{
1311 return find234(wc->byid, &id, winctrl_cmp_byid_find);
1312}
1313
1314struct winctrl *winctrl_findbyindex(struct winctrls *wc, int index)
1315{
1316 return index234(wc->byid, index);
1317}
1318
1319void 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
d1582b2e 1331 char shortcuts[MAX_SHORTCUTS_PER_CTRL];
1332 int nshortcuts;
fe8abbf4 1333 char *escaped;
0bd8d76d 1334 int i, actual_base_id, base_id, num_ids;
fe8abbf4 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) {
3d88e64d 1341 struct winctrl *c = snew(struct winctrl);
fe8abbf4 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) {
3d88e64d 1354 struct winctrl *c = snew(struct winctrl);
fe8abbf4 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
fe8abbf4 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 {
a4b92c62 1413 /*
fe8abbf4 1414 * We're recombining the various columns into one.
a4b92c62 1415 */
fe8abbf4 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;
fe8abbf4 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
d1582b2e 1440 colstart = colspan = -1; /* indicate this was tab-delayed */
1441
fe8abbf4 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;
d74d141c 1472 }
1473 }
fe8abbf4 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
0bd8d76d 1482 /* Almost all controls start at base_id. */
1483 actual_base_id = base_id;
1484
fe8abbf4 1485 /*
1486 * Now we're ready to actually create the control, by
1487 * switching on its type.
1488 */
1489 switch (ctrl->generic.type) {
1490 case CTRL_TEXT:
1491 {
1492 char *wrapped, *escaped;
1493 int lines;
1494 num_ids = 1;
1495 wrapped = staticwrap(&pos, cp->hwnd,
1496 ctrl->generic.label, &lines);
1497 escaped = shortcut_escape(wrapped, NO_SHORTCUT);
1498 statictext(&pos, escaped, lines, base_id);
1499 sfree(escaped);
1500 sfree(wrapped);
1501 }
1502 break;
1503 case CTRL_EDITBOX:
1504 num_ids = 2; /* static, edit */
1505 escaped = shortcut_escape(ctrl->editbox.label,
1506 ctrl->editbox.shortcut);
1507 shortcuts[nshortcuts++] = ctrl->editbox.shortcut;
1508 if (ctrl->editbox.percentwidth == 100) {
1509 if (ctrl->editbox.has_list)
1510 combobox(&pos, escaped,
1511 base_id, base_id+1);
1512 else
1513 multiedit(&pos, ctrl->editbox.password, escaped,
1514 base_id, base_id+1, 100, NULL);
1515 } else {
1516 if (ctrl->editbox.has_list) {
1517 staticcombo(&pos, escaped, base_id, base_id+1,
1518 ctrl->editbox.percentwidth);
1519 } else {
1520 (ctrl->editbox.password ? staticpassedit : staticedit)
1521 (&pos, escaped, base_id, base_id+1,
1522 ctrl->editbox.percentwidth);
1523 }
1524 }
1525 sfree(escaped);
1526 break;
1527 case CTRL_RADIO:
1528 num_ids = ctrl->radio.nbuttons + 1; /* label as well */
1529 {
1530 struct radio *buttons;
1531 int i;
1532
1533 escaped = shortcut_escape(ctrl->radio.label,
1534 ctrl->radio.shortcut);
1535 shortcuts[nshortcuts++] = ctrl->radio.shortcut;
1536
3d88e64d 1537 buttons = snewn(ctrl->radio.nbuttons, struct radio);
fe8abbf4 1538
1539 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1540 buttons[i].text =
1541 shortcut_escape(ctrl->radio.buttons[i],
1542 (char)(ctrl->radio.shortcuts ?
1543 ctrl->radio.shortcuts[i] :
1544 NO_SHORTCUT));
1545 buttons[i].id = base_id + 1 + i;
1546 if (ctrl->radio.shortcuts) {
1547 assert(nshortcuts < MAX_SHORTCUTS_PER_CTRL);
1548 shortcuts[nshortcuts++] = ctrl->radio.shortcuts[i];
1549 }
1550 }
1551
1552 radioline_common(&pos, escaped, base_id,
1553 ctrl->radio.ncolumns,
1554 buttons, ctrl->radio.nbuttons);
1555
1556 for (i = 0; i < ctrl->radio.nbuttons; i++) {
1557 sfree(buttons[i].text);
1558 }
1559 sfree(buttons);
1560 sfree(escaped);
1561 }
1562 break;
1563 case CTRL_CHECKBOX:
1564 num_ids = 1;
1565 escaped = shortcut_escape(ctrl->checkbox.label,
1566 ctrl->checkbox.shortcut);
1567 shortcuts[nshortcuts++] = ctrl->checkbox.shortcut;
1568 checkbox(&pos, escaped, base_id);
1569 sfree(escaped);
1570 break;
1571 case CTRL_BUTTON:
1572 escaped = shortcut_escape(ctrl->button.label,
1573 ctrl->button.shortcut);
1574 shortcuts[nshortcuts++] = ctrl->button.shortcut;
0bd8d76d 1575 if (ctrl->button.iscancel)
1576 actual_base_id = IDCANCEL;
fe8abbf4 1577 num_ids = 1;
0bd8d76d 1578 button(&pos, escaped, actual_base_id, ctrl->button.isdefault);
fe8abbf4 1579 sfree(escaped);
1580 break;
1581 case CTRL_LISTBOX:
1582 num_ids = 2;
1583 escaped = shortcut_escape(ctrl->listbox.label,
1584 ctrl->listbox.shortcut);
1585 shortcuts[nshortcuts++] = ctrl->listbox.shortcut;
1586 if (ctrl->listbox.draglist) {
3d88e64d 1587 data = snew(struct prefslist);
fe8abbf4 1588 num_ids = 4;
1589 prefslist(data, &pos, ctrl->listbox.height, escaped,
1590 base_id, base_id+1, base_id+2, base_id+3);
1591 shortcuts[nshortcuts++] = 'u'; /* Up */
1592 shortcuts[nshortcuts++] = 'd'; /* Down */
1593 } else if (ctrl->listbox.height == 0) {
1594 /* Drop-down list. */
1595 if (ctrl->listbox.percentwidth == 100) {
1596 staticddlbig(&pos, escaped,
1597 base_id, base_id+1);
1598 } else {
1599 staticddl(&pos, escaped, base_id,
1600 base_id+1, ctrl->listbox.percentwidth);
1601 }
1602 } else {
1603 /* Ordinary list. */
1604 listbox(&pos, escaped, base_id, base_id+1,
1605 ctrl->listbox.height, ctrl->listbox.multisel);
1606 }
1607 if (ctrl->listbox.ncols) {
1608 /*
1609 * This method of getting the box width is a bit of
1610 * a hack; we'd do better to try to retrieve the
1611 * actual width in dialog units from doctl() just
1612 * before MapDialogRect. But that's going to be no
1613 * fun, and this should be good enough accuracy.
1614 */
1615 int width = cp->width * ctrl->listbox.percentwidth;
1616 int *tabarray;
1617 int i, percent;
1618
3d88e64d 1619 tabarray = snewn(ctrl->listbox.ncols-1, int);
fe8abbf4 1620 percent = 0;
1621 for (i = 0; i < ctrl->listbox.ncols-1; i++) {
1622 percent += ctrl->listbox.percentages[i];
1623 tabarray[i] = width * percent / 10000;
1624 }
1625 SendDlgItemMessage(cp->hwnd, base_id+1, LB_SETTABSTOPS,
1626 ctrl->listbox.ncols-1, (LPARAM)tabarray);
1627 sfree(tabarray);
1628 }
1629 sfree(escaped);
1630 break;
1631 case CTRL_FILESELECT:
1632 num_ids = 3;
1633 escaped = shortcut_escape(ctrl->fileselect.label,
1634 ctrl->fileselect.shortcut);
1635 shortcuts[nshortcuts++] = ctrl->fileselect.shortcut;
1636 editbutton(&pos, escaped, base_id, base_id+1,
1637 "Bro&wse...", base_id+2);
1638 shortcuts[nshortcuts++] = 'w';
1639 sfree(escaped);
1640 break;
1641 case CTRL_FONTSELECT:
1642 num_ids = 3;
1643 escaped = shortcut_escape(ctrl->fontselect.label,
1644 ctrl->fontselect.shortcut);
1645 shortcuts[nshortcuts++] = ctrl->fontselect.shortcut;
1646 statictext(&pos, escaped, 1, base_id);
1647 staticbtn(&pos, "", base_id+1, "Change...", base_id+2);
1648 sfree(escaped);
3d88e64d 1649 data = snew(FontSpec);
fe8abbf4 1650 break;
1651 default:
1652 assert(!"Can't happen");
d1582b2e 1653 num_ids = 0; /* placate gcc */
fe8abbf4 1654 break;
1655 }
1656
1657 /*
1658 * Create a `struct winctrl' for this control, and advance
1659 * the dialog ID counter, if it's actually been created
1660 * (and isn't tabdelayed).
1661 */
1662 if (pos.hwnd) {
3d88e64d 1663 struct winctrl *c = snew(struct winctrl);
fe8abbf4 1664
1665 c->ctrl = ctrl;
0bd8d76d 1666 c->base_id = actual_base_id;
fe8abbf4 1667 c->num_ids = num_ids;
1668 c->data = data;
1669 memcpy(c->shortcuts, shortcuts, sizeof(shortcuts));
1670 winctrl_add(wc, c);
1671 winctrl_add_shortcuts(dp, c);
0bd8d76d 1672 if (actual_base_id == base_id)
1673 base_id += num_ids;
fe8abbf4 1674 }
1675
d1582b2e 1676 if (colstart >= 0) {
fe8abbf4 1677 /*
1678 * Update the ypos in all columns crossed by this
1679 * control.
1680 */
1681 int i;
1682 for (i = colstart; i < colstart+colspan; i++)
1683 columns[i].ypos = pos.ypos;
1684 }
d74d141c 1685 }
fe8abbf4 1686
1687 /*
1688 * We've now finished laying out the controls; so now update
1689 * the ctlpos and control ID that were passed in, terminate
1690 * any containing box, and return.
1691 */
1692 for (i = 0; i < ncols; i++)
1693 if (cp->ypos < columns[i].ypos)
1694 cp->ypos = columns[i].ypos;
1695 *id = base_id;
1696
1697 if (s->boxname && *s->boxname)
1698 endbox(cp);
1699}
1700
1701static void winctrl_set_focus(union control *ctrl, struct dlgparam *dp,
1702 int has_focus)
1703{
1704 if (has_focus) {
1705 if (dp->focused)
1706 dp->lastfocused = dp->focused;
1707 dp->focused = ctrl;
1708 } else if (!has_focus && dp->focused == ctrl) {
1709 dp->lastfocused = dp->focused;
1710 dp->focused = NULL;
1711 }
1712}
1713
0bd8d76d 1714union control *dlg_last_focused(union control *ctrl, void *dlg)
fe8abbf4 1715{
1716 struct dlgparam *dp = (struct dlgparam *)dlg;
0bd8d76d 1717 return dp->focused == ctrl ? dp->lastfocused : dp->focused;
fe8abbf4 1718}
1719
1720/*
1721 * The dialog-box procedure calls this function to handle Windows
1722 * messages on a control we manage.
1723 */
1724int winctrl_handle_command(struct dlgparam *dp, UINT msg,
1725 WPARAM wParam, LPARAM lParam)
1726{
1727 struct winctrl *c;
1728 union control *ctrl;
1729 int i, id, ret;
1730 static UINT draglistmsg = WM_NULL;
1731
1732 /*
1733 * Filter out pointless window messages. Our interest is in
1734 * WM_COMMAND and the drag list message, and nothing else.
1735 */
1736 if (draglistmsg == WM_NULL)
1737 draglistmsg = RegisterWindowMessage (DRAGLISTMSGSTRING);
1738
1739 if (msg != draglistmsg && msg != WM_COMMAND && msg != WM_DRAWITEM)
1740 return 0;
1741
1742 /*
1743 * Look up the control ID in our data.
1744 */
d1582b2e 1745 c = NULL;
fe8abbf4 1746 for (i = 0; i < dp->nctrltrees; i++) {
1747 c = winctrl_findbyid(dp->controltrees[i], LOWORD(wParam));
1748 if (c)
1749 break;
1750 }
1751 if (!c)
1752 return 0; /* we have nothing to do */
1753
1754 if (msg == WM_DRAWITEM) {
1755 /*
1756 * Owner-draw request for a panel title.
1757 */
1758 LPDRAWITEMSTRUCT di = (LPDRAWITEMSTRUCT) lParam;
1759 HDC hdc = di->hDC;
1760 RECT r = di->rcItem;
1761 SIZE s;
1762
97332719 1763 SetMapMode(hdc, MM_TEXT); /* ensure logical units == pixels */
1764
fe8abbf4 1765 GetTextExtentPoint32(hdc, (char *)c->data,
1766 strlen((char *)c->data), &s);
1767 DrawEdge(hdc, &r, EDGE_ETCHED, BF_ADJUST | BF_RECT);
1768 TextOut(hdc,
1769 r.left + (r.right-r.left-s.cx)/2,
1770 r.top + (r.bottom-r.top-s.cy)/2,
1771 (char *)c->data, strlen((char *)c->data));
1772
1773 return TRUE;
1774 }
1775
1776 ctrl = c->ctrl;
1777 id = LOWORD(wParam) - c->base_id;
1778
1779 if (!ctrl || !ctrl->generic.handler)
1780 return 0; /* nothing we can do here */
1781
1782 /*
1783 * From here on we do not issue `return' statements until the
1784 * very end of the dialog box: any event handler is entitled to
1785 * ask for a colour selector, so we _must_ always allow control
1786 * to reach the end of this switch statement so that the
1787 * subsequent code can test dp->coloursel_wanted().
1788 */
1789 ret = 0;
1790 dp->coloursel_wanted = FALSE;
1791
1792 /*
1793 * Now switch on the control type and the message.
1794 */
1795 switch (ctrl->generic.type) {
1796 case CTRL_EDITBOX:
1797 if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
1798 (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
1799 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
1800 if (msg == WM_COMMAND && ctrl->editbox.has_list &&
1801 (HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
1802 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
1803
1804 if (msg == WM_COMMAND && !ctrl->editbox.has_list &&
1805 HIWORD(wParam) == EN_CHANGE)
1806 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1807 if (msg == WM_COMMAND &&
1808 ctrl->editbox.has_list) {
1809 if (HIWORD(wParam) == CBN_SELCHANGE) {
1810 int index, len;
1811 char *text;
1812
1813 index = SendDlgItemMessage(dp->hwnd, c->base_id+1,
1814 CB_GETCURSEL, 0, 0);
1815 len = SendDlgItemMessage(dp->hwnd, c->base_id+1,
1816 CB_GETLBTEXTLEN, index, 0);
3d88e64d 1817 text = snewn(len+1, char);
fe8abbf4 1818 SendDlgItemMessage(dp->hwnd, c->base_id+1, CB_GETLBTEXT,
1819 index, (LPARAM)text);
1820 SetDlgItemText(dp->hwnd, c->base_id+1, text);
1821 sfree(text);
1822 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1823 } else if (HIWORD(wParam) == CBN_EDITCHANGE) {
1824 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1825 } else if (HIWORD(wParam) == CBN_KILLFOCUS) {
1826 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
1827 }
1828
1829 }
1830 break;
1831 case CTRL_RADIO:
1832 if (msg == WM_COMMAND &&
1833 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1834 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1835 /*
1836 * We sometimes get spurious BN_CLICKED messages for the
1837 * radio button that is just about to _lose_ selection, if
1838 * we're switching using the arrow keys. Therefore we
1839 * double-check that the button in wParam is actually
1840 * checked before generating an event.
1841 */
1842 if (msg == WM_COMMAND &&
d1582b2e 1843 (HIWORD(wParam) == BN_CLICKED ||
1844 HIWORD(wParam) == BN_DOUBLECLICKED) &&
fe8abbf4 1845 IsDlgButtonChecked(dp->hwnd, LOWORD(wParam))) {
1846 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1847 }
1848 break;
1849 case CTRL_CHECKBOX:
1850 if (msg == WM_COMMAND &&
1851 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1852 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1853 if (msg == WM_COMMAND &&
1854 (HIWORD(wParam) == BN_CLICKED ||
1855 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1856 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1857 }
1858 break;
1859 case CTRL_BUTTON:
1860 if (msg == WM_COMMAND &&
1861 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1862 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1863 if (msg == WM_COMMAND &&
1864 (HIWORD(wParam) == BN_CLICKED ||
1865 HIWORD(wParam) == BN_DOUBLECLICKED)) {
1866 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_ACTION);
1867 }
1868 break;
1869 case CTRL_LISTBOX:
1870 if (msg == WM_COMMAND && ctrl->listbox.height != 0 &&
1871 (HIWORD(wParam)==LBN_SETFOCUS || HIWORD(wParam)==LBN_KILLFOCUS))
1872 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == LBN_SETFOCUS);
1873 if (msg == WM_COMMAND && ctrl->listbox.height == 0 &&
1874 (HIWORD(wParam)==CBN_SETFOCUS || HIWORD(wParam)==CBN_KILLFOCUS))
1875 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == CBN_SETFOCUS);
1876 if (msg == WM_COMMAND && id >= 2 &&
1877 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1878 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1879 if (ctrl->listbox.draglist) {
1880 int pret;
1881 pret = handle_prefslist(c->data, NULL, 0, (msg != WM_COMMAND),
1882 dp->hwnd, wParam, lParam);
1883 if (pret & 2)
1884 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1885 ret = pret & 1;
1886 } else {
1887 if (msg == WM_COMMAND && HIWORD(wParam) == LBN_DBLCLK) {
1888 SetCapture(dp->hwnd);
1889 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_ACTION);
1890 } else if (msg == WM_COMMAND && HIWORD(wParam) == LBN_SELCHANGE) {
1891 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_SELCHANGE);
1892 }
1893 }
1894 break;
1895 case CTRL_FILESELECT:
1896 if (msg == WM_COMMAND && id == 1 &&
1897 (HIWORD(wParam) == EN_SETFOCUS || HIWORD(wParam) == EN_KILLFOCUS))
1898 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == EN_SETFOCUS);
1899 if (msg == WM_COMMAND && id == 2 &&
1900 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1901 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
875e0b16 1902 if (msg == WM_COMMAND && id == 1 && HIWORD(wParam) == EN_CHANGE)
1903 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
fe8abbf4 1904 if (id == 2 &&
1905 (msg == WM_COMMAND &&
1906 (HIWORD(wParam) == BN_CLICKED ||
1907 HIWORD(wParam) == BN_DOUBLECLICKED))) {
1908 OPENFILENAME of;
1909 char filename[FILENAME_MAX];
1910 int ret;
1911
1912 memset(&of, 0, sizeof(of));
1913#ifdef OPENFILENAME_SIZE_VERSION_400
1914 of.lStructSize = OPENFILENAME_SIZE_VERSION_400;
1915#else
1916 of.lStructSize = sizeof(of);
1917#endif
1918 of.hwndOwner = dp->hwnd;
1919 if (ctrl->fileselect.filter)
1920 of.lpstrFilter = ctrl->fileselect.filter;
1921 else
1922 of.lpstrFilter = "All Files (*.*)\0*\0\0\0";
1923 of.lpstrCustomFilter = NULL;
1924 of.nFilterIndex = 1;
1925 of.lpstrFile = filename;
1926 GetDlgItemText(dp->hwnd, c->base_id+1, filename, lenof(filename));
1927 filename[lenof(filename)-1] = '\0';
1928 of.nMaxFile = lenof(filename);
1929 of.lpstrFileTitle = NULL;
1930 of.lpstrInitialDir = NULL;
1931 of.lpstrTitle = ctrl->fileselect.title;
1932 of.Flags = 0;
1933 if (ctrl->fileselect.for_writing)
1934 ret = GetSaveFileName(&of);
1935 else
1936 ret = GetOpenFileName(&of);
1937 if (ret) {
1938 SetDlgItemText(dp->hwnd, c->base_id + 1, filename);
1939 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1940 }
1941 }
1942 break;
1943 case CTRL_FONTSELECT:
1944 if (msg == WM_COMMAND && id == 2 &&
1945 (HIWORD(wParam) == BN_SETFOCUS || HIWORD(wParam) == BN_KILLFOCUS))
1946 winctrl_set_focus(ctrl, dp, HIWORD(wParam) == BN_SETFOCUS);
1947 if (id == 2 &&
1948 (msg == WM_COMMAND &&
1949 (HIWORD(wParam) == BN_CLICKED ||
1950 HIWORD(wParam) == BN_DOUBLECLICKED))) {
1951 CHOOSEFONT cf;
1952 LOGFONT lf;
1953 HDC hdc;
1954 FontSpec fs = *(FontSpec *)c->data;
1955
1956 hdc = GetDC(0);
1957 lf.lfHeight = -MulDiv(fs.height,
1958 GetDeviceCaps(hdc, LOGPIXELSY), 72);
1959 ReleaseDC(0, hdc);
1960 lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
1961 lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
1962 lf.lfWeight = (fs.isbold ? FW_BOLD : 0);
1963 lf.lfCharSet = fs.charset;
1964 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
1965 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
1966 lf.lfQuality = DEFAULT_QUALITY;
1967 lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
1968 strncpy(lf.lfFaceName, fs.name,
1969 sizeof(lf.lfFaceName) - 1);
1970 lf.lfFaceName[sizeof(lf.lfFaceName) - 1] = '\0';
1971
1972 cf.lStructSize = sizeof(cf);
1973 cf.hwndOwner = dp->hwnd;
1974 cf.lpLogFont = &lf;
1975 cf.Flags = CF_FIXEDPITCHONLY | CF_FORCEFONTEXIST |
1976 CF_INITTOLOGFONTSTRUCT | CF_SCREENFONTS;
1977
1978 if (ChooseFont(&cf)) {
1979 strncpy(fs.name, lf.lfFaceName,
1980 sizeof(fs.name) - 1);
1981 fs.name[sizeof(fs.name) - 1] = '\0';
1982 fs.isbold = (lf.lfWeight == FW_BOLD);
1983 fs.charset = lf.lfCharSet;
1984 fs.height = cf.iPointSize / 10;
1985 dlg_fontsel_set(ctrl, dp, fs);
1986 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_VALCHANGE);
1987 }
1988 }
1989 break;
1990 }
1991
1992 /*
1993 * If the above event handler has asked for a colour selector,
1994 * now is the time to generate one.
1995 */
1996 if (dp->coloursel_wanted) {
1997 static CHOOSECOLOR cc;
1998 static DWORD custom[16] = { 0 }; /* zero initialisers */
1999 cc.lStructSize = sizeof(cc);
2000 cc.hwndOwner = dp->hwnd;
2001 cc.hInstance = (HWND) hinst;
2002 cc.lpCustColors = custom;
2003 cc.rgbResult = RGB(dp->coloursel_result.r,
2004 dp->coloursel_result.g,
2005 dp->coloursel_result.b);
2006 cc.Flags = CC_FULLOPEN | CC_RGBINIT;
2007 if (ChooseColor(&cc)) {
2008 dp->coloursel_result.r =
2009 (unsigned char) (cc.rgbResult & 0xFF);
2010 dp->coloursel_result.g =
2011 (unsigned char) (cc.rgbResult >> 8) & 0xFF;
2012 dp->coloursel_result.b =
2013 (unsigned char) (cc.rgbResult >> 16) & 0xFF;
2014 dp->coloursel_result.ok = TRUE;
2015 } else
2016 dp->coloursel_result.ok = FALSE;
2017 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_CALLBACK);
2018 }
2019
2020 return ret;
2021}
2022
2023/*
2024 * This function can be called to produce context help on a
2025 * control. Returns TRUE if it has actually launched WinHelp.
2026 */
2027int winctrl_context_help(struct dlgparam *dp, HWND hwnd, int id)
2028{
2029 int i;
2030 struct winctrl *c;
2031 char *cmd;
2032
2033 /*
2034 * Look up the control ID in our data.
2035 */
d1582b2e 2036 c = NULL;
fe8abbf4 2037 for (i = 0; i < dp->nctrltrees; i++) {
2038 c = winctrl_findbyid(dp->controltrees[i], id);
2039 if (c)
2040 break;
2041 }
2042 if (!c)
2043 return 0; /* we have nothing to do */
2044
2045 /*
2046 * This is the Windows front end, so we're allowed to assume
2047 * `helpctx.p' is a context string.
2048 */
2049 if (!c->ctrl || !c->ctrl->generic.helpctx.p)
2050 return 0; /* no help available for this ctrl */
2051
2052 cmd = dupprintf("JI(`',`%s')", c->ctrl->generic.helpctx.p);
2053 WinHelp(hwnd, help_path, HELP_COMMAND, (DWORD)cmd);
2054 sfree(cmd);
2055 return 1;
2056}
2057
2058/*
2059 * Now the various functions that the platform-independent
2060 * mechanism can call to access the dialog box entries.
2061 */
2062
2063static struct winctrl *dlg_findbyctrl(struct dlgparam *dp, union control *ctrl)
2064{
2065 int i;
2066
2067 for (i = 0; i < dp->nctrltrees; i++) {
2068 struct winctrl *c = winctrl_findbyctrl(dp->controltrees[i], ctrl);
2069 if (c)
2070 return c;
2071 }
2072 return NULL;
2073}
2074
2075void dlg_radiobutton_set(union control *ctrl, void *dlg, int whichbutton)
2076{
2077 struct dlgparam *dp = (struct dlgparam *)dlg;
2078 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2079 assert(c && c->ctrl->generic.type == CTRL_RADIO);
2080 CheckRadioButton(dp->hwnd,
2081 c->base_id + 1,
2082 c->base_id + c->ctrl->radio.nbuttons,
2083 c->base_id + 1 + whichbutton);
2084}
2085
2086int dlg_radiobutton_get(union control *ctrl, void *dlg)
2087{
2088 struct dlgparam *dp = (struct dlgparam *)dlg;
2089 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2090 int i;
2091 assert(c && c->ctrl->generic.type == CTRL_RADIO);
2092 for (i = 0; i < c->ctrl->radio.nbuttons; i++)
2093 if (IsDlgButtonChecked(dp->hwnd, c->base_id + 1 + i))
2094 return i;
2095 assert(!"No radio button was checked?!");
2096 return 0;
2097}
2098
2099void dlg_checkbox_set(union control *ctrl, void *dlg, int checked)
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 CheckDlgButton(dp->hwnd, c->base_id, (checked != 0));
2105}
2106
2107int dlg_checkbox_get(union control *ctrl, void *dlg)
2108{
2109 struct dlgparam *dp = (struct dlgparam *)dlg;
2110 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2111 assert(c && c->ctrl->generic.type == CTRL_CHECKBOX);
2112 return 0 != IsDlgButtonChecked(dp->hwnd, c->base_id);
2113}
2114
2115void dlg_editbox_set(union control *ctrl, void *dlg, char const *text)
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 SetDlgItemText(dp->hwnd, c->base_id+1, text);
2121}
2122
2123void dlg_editbox_get(union control *ctrl, void *dlg, char *buffer, int length)
2124{
2125 struct dlgparam *dp = (struct dlgparam *)dlg;
2126 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2127 assert(c && c->ctrl->generic.type == CTRL_EDITBOX);
2128 GetDlgItemText(dp->hwnd, c->base_id+1, buffer, length);
2129 buffer[length-1] = '\0';
2130}
2131
2132/* The `listbox' functions can also apply to combo boxes. */
2133void dlg_listbox_clear(union control *ctrl, void *dlg)
2134{
2135 struct dlgparam *dp = (struct dlgparam *)dlg;
2136 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2137 int msg;
2138 assert(c &&
2139 (c->ctrl->generic.type == CTRL_LISTBOX ||
d1582b2e 2140 (c->ctrl->generic.type == CTRL_EDITBOX &&
2141 c->ctrl->editbox.has_list)));
fe8abbf4 2142 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2143 LB_RESETCONTENT : CB_RESETCONTENT);
2144 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
2145}
2146
2147void dlg_listbox_del(union control *ctrl, void *dlg, int index)
2148{
2149 struct dlgparam *dp = (struct dlgparam *)dlg;
2150 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2151 int msg;
2152 assert(c &&
2153 (c->ctrl->generic.type == CTRL_LISTBOX ||
d1582b2e 2154 (c->ctrl->generic.type == CTRL_EDITBOX &&
2155 c->ctrl->editbox.has_list)));
fe8abbf4 2156 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2157 LB_DELETESTRING : CB_DELETESTRING);
2158 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2159}
2160
2161void dlg_listbox_add(union control *ctrl, void *dlg, char const *text)
2162{
2163 struct dlgparam *dp = (struct dlgparam *)dlg;
2164 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2165 int msg;
2166 assert(c &&
2167 (c->ctrl->generic.type == CTRL_LISTBOX ||
d1582b2e 2168 (c->ctrl->generic.type == CTRL_EDITBOX &&
2169 c->ctrl->editbox.has_list)));
fe8abbf4 2170 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2171 LB_ADDSTRING : CB_ADDSTRING);
2172 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
2173}
2174
2175/*
2176 * Each listbox entry may have a numeric id associated with it.
2177 * Note that some front ends only permit a string to be stored at
2178 * each position, which means that _if_ you put two identical
2179 * strings in any listbox then you MUST not assign them different
2180 * IDs and expect to get meaningful results back.
2181 */
4eaff8d4 2182void dlg_listbox_addwithid(union control *ctrl, void *dlg,
2183 char const *text, int id)
fe8abbf4 2184{
2185 struct dlgparam *dp = (struct dlgparam *)dlg;
2186 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2187 int msg, msg2, index;
2188 assert(c &&
2189 (c->ctrl->generic.type == CTRL_LISTBOX ||
d1582b2e 2190 (c->ctrl->generic.type == CTRL_EDITBOX &&
2191 c->ctrl->editbox.has_list)));
fe8abbf4 2192 msg = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2193 LB_ADDSTRING : CB_ADDSTRING);
2194 msg2 = (c->ctrl->generic.type==CTRL_LISTBOX && c->ctrl->listbox.height!=0 ?
2195 LB_SETITEMDATA : CB_SETITEMDATA);
2196 index = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, (LPARAM)text);
2197 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg2, index, (LPARAM)id);
2198}
2199
2200int dlg_listbox_getid(union control *ctrl, void *dlg, int index)
2201{
2202 struct dlgparam *dp = (struct dlgparam *)dlg;
2203 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2204 int msg;
2205 assert(c && c->ctrl->generic.type == CTRL_LISTBOX);
2206 msg = (c->ctrl->listbox.height != 0 ? LB_GETITEMDATA : CB_GETITEMDATA);
2207 return
2208 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2209}
2210
2211/* dlg_listbox_index returns <0 if no single element is selected. */
2212int dlg_listbox_index(union control *ctrl, void *dlg)
2213{
2214 struct dlgparam *dp = (struct dlgparam *)dlg;
2215 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2216 int msg, ret;
2217 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2218 !c->ctrl->listbox.multisel);
2219 msg = (c->ctrl->listbox.height != 0 ? LB_GETCURSEL : CB_GETCURSEL);
2220 ret = SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, 0, 0);
2221 if (ret == LB_ERR)
2222 return -1;
2223 else
2224 return ret;
2225}
2226
2227int dlg_listbox_issel(union control *ctrl, void *dlg, int index)
2228{
2229 struct dlgparam *dp = (struct dlgparam *)dlg;
2230 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2231 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2232 c->ctrl->listbox.multisel &&
2233 c->ctrl->listbox.height != 0);
2234 return
2235 SendDlgItemMessage(dp->hwnd, c->base_id+1, LB_GETSEL, index, 0);
2236}
2237
2238void dlg_listbox_select(union control *ctrl, void *dlg, int index)
2239{
2240 struct dlgparam *dp = (struct dlgparam *)dlg;
2241 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2242 int msg;
2243 assert(c && c->ctrl->generic.type == CTRL_LISTBOX &&
2244 !c->ctrl->listbox.multisel);
2245 msg = (c->ctrl->listbox.height != 0 ? LB_SETCURSEL : CB_SETCURSEL);
2246 SendDlgItemMessage(dp->hwnd, c->base_id+1, msg, index, 0);
2247}
2248
2249void dlg_text_set(union control *ctrl, void *dlg, char const *text)
2250{
2251 struct dlgparam *dp = (struct dlgparam *)dlg;
2252 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2253 assert(c && c->ctrl->generic.type == CTRL_TEXT);
2254 SetDlgItemText(dp->hwnd, c->base_id, text);
2255}
2256
2257void dlg_filesel_set(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 SetDlgItemText(dp->hwnd, c->base_id+1, fn.path);
2263}
2264
2265void dlg_filesel_get(union control *ctrl, void *dlg, Filename *fn)
2266{
2267 struct dlgparam *dp = (struct dlgparam *)dlg;
2268 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2269 assert(c && c->ctrl->generic.type == CTRL_FILESELECT);
2270 GetDlgItemText(dp->hwnd, c->base_id+1, fn->path, lenof(fn->path));
2271 fn->path[lenof(fn->path)-1] = '\0';
2272}
2273
2274void dlg_fontsel_set(union control *ctrl, void *dlg, FontSpec fs)
2275{
2276 char *buf, *boldstr;
2277 struct dlgparam *dp = (struct dlgparam *)dlg;
2278 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2279 assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
2280
2281 *(FontSpec *)c->data = fs; /* structure copy */
2282
2283 boldstr = (fs.isbold ? "bold, " : "");
2284 if (fs.height == 0)
2285 buf = dupprintf("Font: %s, %sdefault height", fs.name, boldstr);
2286 else
2287 buf = dupprintf("Font: %s, %s%d-point", fs.name, boldstr,
2288 (fs.height < 0 ? -fs.height : fs.height));
2289 SetDlgItemText(dp->hwnd, c->base_id+1, buf);
2290 sfree(buf);
2291}
2292
2293void dlg_fontsel_get(union control *ctrl, void *dlg, FontSpec *fs)
2294{
2295 struct dlgparam *dp = (struct dlgparam *)dlg;
2296 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2297 assert(c && c->ctrl->generic.type == CTRL_FONTSELECT);
2298 *fs = *(FontSpec *)c->data; /* structure copy */
2299}
2300
2301/*
2302 * Bracketing a large set of updates in these two functions will
2303 * cause the front end (if possible) to delay updating the screen
2304 * until it's all complete, thus avoiding flicker.
2305 */
2306void dlg_update_start(union control *ctrl, void *dlg)
2307{
2308 struct dlgparam *dp = (struct dlgparam *)dlg;
2309 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2310 if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
2311 SendDlgItemMessage(dp->hwnd, c->base_id+1, WM_SETREDRAW, FALSE, 0);
2312 }
2313}
2314
2315void dlg_update_done(union control *ctrl, void *dlg)
2316{
2317 struct dlgparam *dp = (struct dlgparam *)dlg;
2318 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2319 if (c && c->ctrl->generic.type == CTRL_LISTBOX) {
2320 HWND hw = GetDlgItem(dp->hwnd, c->base_id+1);
2321 SendMessage(hw, WM_SETREDRAW, TRUE, 0);
2322 InvalidateRect(hw, NULL, TRUE);
2323 }
2324}
2325
2326void dlg_set_focus(union control *ctrl, void *dlg)
2327{
2328 struct dlgparam *dp = (struct dlgparam *)dlg;
2329 struct winctrl *c = dlg_findbyctrl(dp, ctrl);
2330 int id;
2331 HWND ctl;
2332 switch (ctrl->generic.type) {
2333 case CTRL_EDITBOX: id = c->base_id + 1; break;
2334 case CTRL_RADIO:
2335 for (id = c->base_id + ctrl->radio.nbuttons; id > 1; id--)
2336 if (IsDlgButtonChecked(dp->hwnd, id))
2337 break;
2338 /*
2339 * In the theoretically-unlikely case that no button was
2340 * selected, id should come out of this as 1, which is a
2341 * reasonable enough choice.
2342 */
2343 break;
2344 case CTRL_CHECKBOX: id = c->base_id; break;
2345 case CTRL_BUTTON: id = c->base_id; break;
2346 case CTRL_LISTBOX: id = c->base_id + 1; break;
2347 case CTRL_FILESELECT: id = c->base_id + 1; break;
2348 case CTRL_FONTSELECT: id = c->base_id + 2; break;
2349 default: id = c->base_id; break;
2350 }
2351 ctl = GetDlgItem(dp->hwnd, id);
2352 SetFocus(ctl);
2353}
2354
2355/*
2356 * During event processing, you might well want to give an error
2357 * indication to the user. dlg_beep() is a quick and easy generic
2358 * error; dlg_error() puts up a message-box or equivalent.
2359 */
2360void dlg_beep(void *dlg)
2361{
2362 /* struct dlgparam *dp = (struct dlgparam *)dlg; */
2363 MessageBeep(0);
2364}
2365
2366void dlg_error_msg(void *dlg, char *msg)
2367{
2368 struct dlgparam *dp = (struct dlgparam *)dlg;
2369 MessageBox(dp->hwnd, msg,
2370 dp->errtitle ? dp->errtitle : NULL,
2371 MB_OK | MB_ICONERROR);
2372}
2373
2374/*
2375 * This function signals to the front end that the dialog's
2376 * processing is completed, and passes an integer value (typically
2377 * a success status).
2378 */
2379void dlg_end(void *dlg, int value)
2380{
2381 struct dlgparam *dp = (struct dlgparam *)dlg;
2382 dp->ended = TRUE;
2383 dp->endresult = value;
2384}
2385
2386void dlg_refresh(union control *ctrl, void *dlg)
2387{
2388 struct dlgparam *dp = (struct dlgparam *)dlg;
2389 int i, j;
2390 struct winctrl *c;
2391
2392 if (!ctrl) {
2393 /*
2394 * Send EVENT_REFRESH to absolutely everything.
2395 */
2396 for (j = 0; j < dp->nctrltrees; j++) {
2397 for (i = 0;
2398 (c = winctrl_findbyindex(dp->controltrees[j], i)) != NULL;
2399 i++) {
2400 if (c->ctrl && c->ctrl->generic.handler != NULL)
2401 c->ctrl->generic.handler(c->ctrl, dp,
2402 dp->data, EVENT_REFRESH);
2403 }
2404 }
2405 } else {
2406 /*
2407 * Send EVENT_REFRESH to a specific control.
2408 */
2409 if (ctrl->generic.handler != NULL)
2410 ctrl->generic.handler(ctrl, dp, dp->data, EVENT_REFRESH);
2411 }
2412}
2413
2414void dlg_coloursel_start(union control *ctrl, void *dlg, int r, int g, int b)
2415{
2416 struct dlgparam *dp = (struct dlgparam *)dlg;
2417 dp->coloursel_wanted = TRUE;
2418 dp->coloursel_result.r = r;
2419 dp->coloursel_result.g = g;
2420 dp->coloursel_result.b = b;
2421}
2422
2423int dlg_coloursel_results(union control *ctrl, void *dlg,
2424 int *r, int *g, int *b)
2425{
2426 struct dlgparam *dp = (struct dlgparam *)dlg;
2427 if (dp->coloursel_result.ok) {
2428 *r = dp->coloursel_result.r;
2429 *g = dp->coloursel_result.g;
2430 *b = dp->coloursel_result.b;
2431 return 1;
2432 } else
2433 return 0;
d74d141c 2434}
4e6d4091 2435
2436struct perctrl_privdata {
2437 union control *ctrl;
2438 void *data;
2439 int needs_free;
2440};
2441
2442static int perctrl_privdata_cmp(void *av, void *bv)
2443{
2444 struct perctrl_privdata *a = (struct perctrl_privdata *)av;
2445 struct perctrl_privdata *b = (struct perctrl_privdata *)bv;
2446 if (a->ctrl < b->ctrl)
2447 return -1;
2448 else if (a->ctrl > b->ctrl)
2449 return +1;
2450 return 0;
2451}
2452
2453void dp_init(struct dlgparam *dp)
2454{
2455 dp->nctrltrees = 0;
2456 dp->data = NULL;
2457 dp->ended = FALSE;
2458 dp->focused = dp->lastfocused = NULL;
2459 memset(dp->shortcuts, 0, sizeof(dp->shortcuts));
2460 dp->hwnd = NULL;
2461 dp->errtitle = NULL;
2462 dp->privdata = newtree234(perctrl_privdata_cmp);
2463}
2464
2465void dp_add_tree(struct dlgparam *dp, struct winctrls *wc)
2466{
2467 assert(dp->nctrltrees < lenof(dp->controltrees));
2468 dp->controltrees[dp->nctrltrees++] = wc;
2469}
2470
2471void dp_cleanup(struct dlgparam *dp)
2472{
2473 struct perctrl_privdata *p;
2474
2475 if (dp->privdata) {
2476 while ( (p = index234(dp->privdata, 0)) != NULL ) {
2477 del234(dp->privdata, p);
2478 if (p->needs_free)
2479 sfree(p->data);
2480 sfree(p);
2481 }
2482 freetree234(dp->privdata);
2483 dp->privdata = NULL;
2484 }
2485}
2486
2487void *dlg_get_privdata(union control *ctrl, void *dlg)
2488{
2489 struct dlgparam *dp = (struct dlgparam *)dlg;
2490 struct perctrl_privdata tmp, *p;
2491 tmp.ctrl = ctrl;
2492 p = find234(dp->privdata, &tmp, NULL);
2493 if (p)
2494 return p->data;
2495 else
2496 return NULL;
2497}
2498
2499void dlg_set_privdata(union control *ctrl, void *dlg, void *ptr)
2500{
2501 struct dlgparam *dp = (struct dlgparam *)dlg;
2502 struct perctrl_privdata tmp, *p;
2503 tmp.ctrl = ctrl;
2504 p = find234(dp->privdata, &tmp, NULL);
2505 if (!p) {
3d88e64d 2506 p = snew(struct perctrl_privdata);
4e6d4091 2507 p->ctrl = ctrl;
2508 p->needs_free = FALSE;
2509 add234(dp->privdata, p);
2510 }
2511 p->data = ptr;
2512}
2513
2514void *dlg_alloc_privdata(union control *ctrl, void *dlg, size_t size)
2515{
2516 struct dlgparam *dp = (struct dlgparam *)dlg;
2517 struct perctrl_privdata tmp, *p;
2518 tmp.ctrl = ctrl;
2519 p = find234(dp->privdata, &tmp, NULL);
2520 if (!p) {
3d88e64d 2521 p = snew(struct perctrl_privdata);
4e6d4091 2522 p->ctrl = ctrl;
2523 p->needs_free = FALSE;
2524 add234(dp->privdata, p);
2525 }
2526 assert(!p->needs_free);
2527 p->needs_free = TRUE;
3d88e64d 2528 /*
2529 * This is an internal allocation routine, so it's allowed to
2530 * use smalloc directly.
2531 */
4e6d4091 2532 p->data = smalloc(size);
2533 return p->data;
2534}