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