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