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