PuTTYgen: add an extra button to save a public key into a file
[sgt/putty] / winctrls.c
1 /*
2 * winctrls.c: routines to self-manage the controls in a dialog
3 * box.
4 */
5
6 #include <windows.h>
7 #include <commctrl.h>
8
9 #include "winstuff.h"
10 #include "puttymem.h"
11
12 #include "putty.h"
13
14 #define GAPBETWEEN 3
15 #define GAPWITHIN 1
16 #define GAPXBOX 7
17 #define GAPYBOX 4
18 #define DLGWIDTH 168
19 #define STATICHEIGHT 8
20 #define CHECKBOXHEIGHT 8
21 #define RADIOHEIGHT 8
22 #define EDITHEIGHT 12
23 #define COMBOHEIGHT 12
24 #define PUSHBTNHEIGHT 14
25 #define PROGBARHEIGHT 14
26
27 void ctlposinit(struct ctlpos *cp, HWND hwnd,
28 int leftborder, int rightborder, int topborder)
29 {
30 RECT r, r2;
31 cp->hwnd = hwnd;
32 cp->font = SendMessage(hwnd, WM_GETFONT, 0, 0);
33 cp->ypos = topborder;
34 GetClientRect(hwnd, &r);
35 r2.left = r2.top = 0;
36 r2.right = 4;
37 r2.bottom = 8;
38 MapDialogRect(hwnd, &r2);
39 cp->dlu4inpix = r2.right;
40 cp->width = (r.right * 4) / (r2.right) - 2 * GAPBETWEEN;
41 cp->xoff = leftborder;
42 cp->width -= leftborder + rightborder;
43 }
44
45 HWND doctl(struct ctlpos *cp, RECT r,
46 char *wclass, int wstyle, int exstyle, char *wtext, int wid)
47 {
48 HWND ctl;
49 /*
50 * Note nonstandard use of RECT. This is deliberate: by
51 * transforming the width and height directly we arrange to
52 * have all supposedly same-sized controls really same-sized.
53 */
54
55 r.left += cp->xoff;
56 MapDialogRect(cp->hwnd, &r);
57
58 ctl = CreateWindowEx(exstyle, wclass, wtext, wstyle,
59 r.left, r.top, r.right, r.bottom,
60 cp->hwnd, (HMENU) wid, hinst, NULL);
61 SendMessage(ctl, WM_SETFONT, cp->font, MAKELPARAM(TRUE, 0));
62 return ctl;
63 }
64
65 /*
66 * A title bar across the top of a sub-dialog.
67 */
68 void bartitle(struct ctlpos *cp, char *name, int id)
69 {
70 RECT r;
71
72 r.left = GAPBETWEEN;
73 r.right = cp->width;
74 r.top = cp->ypos;
75 r.bottom = STATICHEIGHT;
76 cp->ypos += r.bottom + GAPBETWEEN;
77 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, name, id);
78 }
79
80 /*
81 * Begin a grouping box, with or without a group title.
82 */
83 void beginbox(struct ctlpos *cp, char *name, int idbox)
84 {
85 cp->boxystart = cp->ypos;
86 if (!name)
87 cp->boxystart -= STATICHEIGHT / 2;
88 if (name)
89 cp->ypos += STATICHEIGHT;
90 cp->ypos += GAPYBOX;
91 cp->width -= 2 * GAPXBOX;
92 cp->xoff += GAPXBOX;
93 cp->boxid = idbox;
94 cp->boxtext = name;
95 }
96
97 /*
98 * End a grouping box.
99 */
100 void endbox(struct ctlpos *cp)
101 {
102 RECT r;
103 cp->xoff -= GAPXBOX;
104 cp->width += 2 * GAPXBOX;
105 cp->ypos += GAPYBOX - GAPBETWEEN;
106 r.left = GAPBETWEEN;
107 r.right = cp->width;
108 r.top = cp->boxystart;
109 r.bottom = cp->ypos - cp->boxystart;
110 doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0,
111 cp->boxtext ? cp->boxtext : "", cp->boxid);
112 cp->ypos += GAPYBOX;
113 }
114
115 /*
116 * Some edit boxes. Each one has a static above it. The percentages
117 * of the horizontal space are provided.
118 */
119 void multiedit(struct ctlpos *cp, ...)
120 {
121 RECT r;
122 va_list ap;
123 int percent, xpos;
124
125 percent = xpos = 0;
126 va_start(ap, cp);
127 while (1) {
128 char *text;
129 int staticid, editid, pcwidth;
130 text = va_arg(ap, char *);
131 if (!text)
132 break;
133 staticid = va_arg(ap, int);
134 editid = va_arg(ap, int);
135 pcwidth = va_arg(ap, int);
136
137 r.left = xpos + GAPBETWEEN;
138 percent += pcwidth;
139 xpos = (cp->width + GAPBETWEEN) * percent / 100;
140 r.right = xpos - r.left;
141
142 r.top = cp->ypos;
143 r.bottom = STATICHEIGHT;
144 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
145 r.top = cp->ypos + 8 + GAPWITHIN;
146 r.bottom = EDITHEIGHT;
147 doctl(cp, r, "EDIT",
148 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
149 WS_EX_CLIENTEDGE, "", editid);
150 }
151 va_end(ap);
152 cp->ypos += STATICHEIGHT + GAPWITHIN + EDITHEIGHT + GAPBETWEEN;
153 }
154
155 /*
156 * A static line, followed by a full-width drop-down list (ie a
157 * non-editing combo box).
158 */
159 void dropdownlist(struct ctlpos *cp, char *text, int staticid, int listid)
160 {
161 RECT r;
162 va_list ap;
163
164 r.left = GAPBETWEEN;
165 r.right = cp->width;
166
167 r.top = cp->ypos;
168 r.bottom = STATICHEIGHT;
169 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
170 r.top = cp->ypos + 8 + GAPWITHIN;
171 r.bottom = COMBOHEIGHT * 10;
172 doctl(cp, r, "COMBOBOX",
173 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
174 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
175
176 cp->ypos += STATICHEIGHT + GAPWITHIN + COMBOHEIGHT + GAPBETWEEN;
177 }
178
179 static void radioline_common(struct ctlpos *cp, int nacross, va_list ap)
180 {
181 RECT r;
182 int group;
183 int i;
184 char *btext;
185
186 group = WS_GROUP;
187 i = 0;
188 btext = va_arg(ap, char *);
189 while (1) {
190 char *nextbtext;
191 int bid;
192 if (!btext)
193 break;
194 if (i == nacross) {
195 cp->ypos += r.bottom + GAPBETWEEN;
196 i = 0;
197 }
198 bid = va_arg(ap, int);
199 nextbtext = va_arg(ap, char *);
200 r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
201 if (nextbtext)
202 r.right =
203 (i + 1) * (cp->width + GAPBETWEEN) / nacross - r.left;
204 else
205 r.right = cp->width - r.left;
206 r.top = cp->ypos;
207 r.bottom = RADIOHEIGHT;
208 doctl(cp, r, "BUTTON",
209 BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
210 group, 0, btext, bid);
211 group = 0;
212 i++;
213 btext = nextbtext;
214 }
215 cp->ypos += r.bottom + GAPBETWEEN;
216 }
217
218 /*
219 * A set of radio buttons on the same line, with a static above
220 * them. `nacross' dictates how many parts the line is divided into
221 * (you might want this not to equal the number of buttons if you
222 * needed to line up some 2s and some 3s to look good in the same
223 * panel).
224 *
225 * There's a bit of a hack in here to ensure that if nacross
226 * exceeds the actual number of buttons, the rightmost button
227 * really does get all the space right to the edge of the line, so
228 * you can do things like
229 *
230 * (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
231 */
232 void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...)
233 {
234 RECT r;
235 va_list ap;
236
237 r.left = GAPBETWEEN;
238 r.top = cp->ypos;
239 r.right = cp->width;
240 r.bottom = STATICHEIGHT;
241 cp->ypos += r.bottom + GAPWITHIN;
242 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
243 va_start(ap, nacross);
244 radioline_common(cp, nacross, ap);
245 va_end(ap);
246 }
247
248 /*
249 * A set of radio buttons on the same line, without a static above
250 * them. Otherwise just like radioline.
251 */
252 void bareradioline(struct ctlpos *cp, int nacross, ...)
253 {
254 va_list ap;
255
256 va_start(ap, nacross);
257 radioline_common(cp, nacross, ap);
258 va_end(ap);
259 }
260
261 /*
262 * A set of radio buttons on multiple lines, with a static above
263 * them.
264 */
265 void radiobig(struct ctlpos *cp, char *text, int id, ...)
266 {
267 RECT r;
268 va_list ap;
269 int group;
270
271 r.left = GAPBETWEEN;
272 r.top = cp->ypos;
273 r.right = cp->width;
274 r.bottom = STATICHEIGHT;
275 cp->ypos += r.bottom + GAPWITHIN;
276 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
277 va_start(ap, id);
278 group = WS_GROUP;
279 while (1) {
280 char *btext;
281 int bid;
282 btext = va_arg(ap, char *);
283 if (!btext)
284 break;
285 bid = va_arg(ap, int);
286 r.left = GAPBETWEEN;
287 r.top = cp->ypos;
288 r.right = cp->width;
289 r.bottom = STATICHEIGHT;
290 cp->ypos += r.bottom + GAPWITHIN;
291 doctl(cp, r, "BUTTON",
292 BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
293 group, 0, btext, bid);
294 group = 0;
295 }
296 va_end(ap);
297 cp->ypos += GAPBETWEEN - GAPWITHIN;
298 }
299
300 /*
301 * A single standalone checkbox.
302 */
303 void checkbox(struct ctlpos *cp, char *text, int id)
304 {
305 RECT r;
306
307 r.left = GAPBETWEEN;
308 r.top = cp->ypos;
309 r.right = cp->width;
310 r.bottom = CHECKBOXHEIGHT;
311 cp->ypos += r.bottom + GAPBETWEEN;
312 doctl(cp, r, "BUTTON",
313 BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
314 text, id);
315 }
316
317 /*
318 * A single standalone static text control.
319 */
320 void statictext(struct ctlpos *cp, char *text, int id)
321 {
322 RECT r;
323
324 r.left = GAPBETWEEN;
325 r.top = cp->ypos;
326 r.right = cp->width;
327 r.bottom = STATICHEIGHT;
328 cp->ypos += r.bottom + GAPBETWEEN;
329 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
330 }
331
332 /*
333 * A button on the right hand side, with a static to its left.
334 */
335 void staticbtn(struct ctlpos *cp, char *stext, int sid,
336 char *btext, int bid)
337 {
338 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
339 PUSHBTNHEIGHT : STATICHEIGHT);
340 RECT r;
341 int lwid, rwid, rpos;
342
343 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
344 lwid = rpos - 2 * GAPBETWEEN;
345 rwid = cp->width + GAPBETWEEN - rpos;
346
347 r.left = GAPBETWEEN;
348 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
349 r.right = lwid;
350 r.bottom = STATICHEIGHT;
351 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
352
353 r.left = rpos;
354 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
355 r.right = rwid;
356 r.bottom = PUSHBTNHEIGHT;
357 doctl(cp, r, "BUTTON",
358 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
359 0, btext, bid);
360
361 cp->ypos += height + GAPBETWEEN;
362 }
363
364 /*
365 * Like staticbtn, but two buttons.
366 */
367 void static2btn(struct ctlpos *cp, char *stext, int sid,
368 char *btext1, int bid1, char *btext2, int bid2)
369 {
370 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
371 PUSHBTNHEIGHT : STATICHEIGHT);
372 RECT r;
373 int lwid, rwid1, rwid2, rpos1, rpos2;
374
375 rpos1 = GAPBETWEEN + (cp->width + GAPBETWEEN) / 2;
376 rpos2 = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
377 lwid = rpos1 - 2 * GAPBETWEEN;
378 rwid1 = rpos2 - rpos1 - GAPBETWEEN;
379 rwid2 = cp->width + GAPBETWEEN - rpos2;
380
381 r.left = GAPBETWEEN;
382 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
383 r.right = lwid;
384 r.bottom = STATICHEIGHT;
385 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
386
387 r.left = rpos1;
388 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
389 r.right = rwid1;
390 r.bottom = PUSHBTNHEIGHT;
391 doctl(cp, r, "BUTTON",
392 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
393 0, btext1, bid1);
394
395 r.left = rpos2;
396 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
397 r.right = rwid2;
398 r.bottom = PUSHBTNHEIGHT;
399 doctl(cp, r, "BUTTON",
400 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
401 0, btext2, bid2);
402
403 cp->ypos += height + GAPBETWEEN;
404 }
405
406 /*
407 * An edit control on the right hand side, with a static to its left.
408 */
409 static void staticedit_internal(struct ctlpos *cp, char *stext,
410 int sid, int eid, int percentedit,
411 int style)
412 {
413 const int height = (EDITHEIGHT > STATICHEIGHT ?
414 EDITHEIGHT : STATICHEIGHT);
415 RECT r;
416 int lwid, rwid, rpos;
417
418 rpos =
419 GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
420 lwid = rpos - 2 * GAPBETWEEN;
421 rwid = cp->width + GAPBETWEEN - rpos;
422
423 r.left = GAPBETWEEN;
424 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
425 r.right = lwid;
426 r.bottom = STATICHEIGHT;
427 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
428
429 r.left = rpos;
430 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
431 r.right = rwid;
432 r.bottom = EDITHEIGHT;
433 doctl(cp, r, "EDIT",
434 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
435 WS_EX_CLIENTEDGE, "", eid);
436
437 cp->ypos += height + GAPBETWEEN;
438 }
439
440 void staticedit(struct ctlpos *cp, char *stext,
441 int sid, int eid, int percentedit)
442 {
443 staticedit_internal(cp, stext, sid, eid, percentedit, 0);
444 }
445
446 void staticpassedit(struct ctlpos *cp, char *stext,
447 int sid, int eid, int percentedit)
448 {
449 staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
450 }
451
452 /*
453 * A big multiline edit control with a static labelling it.
454 */
455 void bigeditctrl(struct ctlpos *cp, char *stext,
456 int sid, int eid, int lines)
457 {
458 RECT r;
459
460 r.left = GAPBETWEEN;
461 r.top = cp->ypos;
462 r.right = cp->width;
463 r.bottom = STATICHEIGHT;
464 cp->ypos += r.bottom + GAPWITHIN;
465 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
466
467 r.left = GAPBETWEEN;
468 r.top = cp->ypos;
469 r.right = cp->width;
470 r.bottom = EDITHEIGHT + (lines - 1) * STATICHEIGHT;
471 cp->ypos += r.bottom + GAPBETWEEN;
472 doctl(cp, r, "EDIT",
473 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
474 WS_EX_CLIENTEDGE, "", eid);
475 }
476
477 /*
478 * A tab-control substitute when a real tab control is unavailable.
479 */
480 void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id)
481 {
482 const int height = (COMBOHEIGHT > STATICHEIGHT ?
483 COMBOHEIGHT : STATICHEIGHT);
484 RECT r;
485 int bigwid, lwid, rwid, rpos;
486 static const int BIGGAP = 15;
487 static const int MEDGAP = 3;
488
489 bigwid = cp->width + 2 * GAPBETWEEN - 2 * BIGGAP;
490 cp->ypos += MEDGAP;
491 rpos = BIGGAP + (bigwid + BIGGAP) / 2;
492 lwid = rpos - 2 * BIGGAP;
493 rwid = bigwid + BIGGAP - rpos;
494
495 r.left = BIGGAP;
496 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
497 r.right = lwid;
498 r.bottom = STATICHEIGHT;
499 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
500
501 r.left = rpos;
502 r.top = cp->ypos + (height - COMBOHEIGHT) / 2;
503 r.right = rwid;
504 r.bottom = COMBOHEIGHT * 10;
505 doctl(cp, r, "COMBOBOX",
506 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
507 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
508
509 cp->ypos += height + MEDGAP + GAPBETWEEN;
510
511 r.left = GAPBETWEEN;
512 r.top = cp->ypos;
513 r.right = cp->width;
514 r.bottom = 2;
515 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
516 0, "", s2id);
517 }
518
519 /*
520 * A static line, followed by an edit control on the left hand side
521 * and a button on the right.
522 */
523 void editbutton(struct ctlpos *cp, char *stext, int sid,
524 int eid, char *btext, int bid)
525 {
526 const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
527 EDITHEIGHT : PUSHBTNHEIGHT);
528 RECT r;
529 int lwid, rwid, rpos;
530
531 r.left = GAPBETWEEN;
532 r.top = cp->ypos;
533 r.right = cp->width;
534 r.bottom = STATICHEIGHT;
535 cp->ypos += r.bottom + GAPWITHIN;
536 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
537
538 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
539 lwid = rpos - 2 * GAPBETWEEN;
540 rwid = cp->width + GAPBETWEEN - rpos;
541
542 r.left = GAPBETWEEN;
543 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
544 r.right = lwid;
545 r.bottom = EDITHEIGHT;
546 doctl(cp, r, "EDIT",
547 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
548 WS_EX_CLIENTEDGE, "", eid);
549
550 r.left = rpos;
551 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
552 r.right = rwid;
553 r.bottom = PUSHBTNHEIGHT;
554 doctl(cp, r, "BUTTON",
555 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
556 0, btext, bid);
557
558 cp->ypos += height + GAPBETWEEN;
559 }
560
561 /*
562 * Special control which was hard to describe generically: the
563 * session-saver assembly. A static; below that an edit box; below
564 * that a list box. To the right of the list box, a column of
565 * buttons.
566 */
567 void sesssaver(struct ctlpos *cp, char *text,
568 int staticid, int editid, int listid, ...)
569 {
570 RECT r;
571 va_list ap;
572 int lwid, rwid, rpos;
573 int y;
574 const int LISTDEFHEIGHT = 66;
575
576 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
577 lwid = rpos - 2 * GAPBETWEEN;
578 rwid = cp->width + GAPBETWEEN - rpos;
579
580 /* The static control. */
581 r.left = GAPBETWEEN;
582 r.top = cp->ypos;
583 r.right = lwid;
584 r.bottom = STATICHEIGHT;
585 cp->ypos += r.bottom + GAPWITHIN;
586 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
587
588 /* The edit control. */
589 r.left = GAPBETWEEN;
590 r.top = cp->ypos;
591 r.right = lwid;
592 r.bottom = EDITHEIGHT;
593 cp->ypos += r.bottom + GAPWITHIN;
594 doctl(cp, r, "EDIT",
595 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
596 WS_EX_CLIENTEDGE, "", editid);
597
598 /*
599 * The buttons (we should hold off on the list box until we
600 * know how big the buttons are).
601 */
602 va_start(ap, listid);
603 y = cp->ypos;
604 while (1) {
605 char *btext = va_arg(ap, char *);
606 int bid;
607 if (!btext)
608 break;
609 bid = va_arg(ap, int);
610 r.left = rpos;
611 r.top = y;
612 r.right = rwid;
613 r.bottom = PUSHBTNHEIGHT;
614 y += r.bottom + GAPWITHIN;
615 doctl(cp, r, "BUTTON",
616 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
617 0, btext, bid);
618 }
619
620 /* Compute list box height. LISTDEFHEIGHT, or height of buttons. */
621 y -= cp->ypos;
622 y -= GAPWITHIN;
623 if (y < LISTDEFHEIGHT)
624 y = LISTDEFHEIGHT;
625 r.left = GAPBETWEEN;
626 r.top = cp->ypos;
627 r.right = lwid;
628 r.bottom = y;
629 cp->ypos += y + GAPBETWEEN;
630 doctl(cp, r, "LISTBOX",
631 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
632 LBS_NOTIFY | LBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
633 }
634
635 /*
636 * Another special control: the environment-variable setter. A
637 * static line first; then a pair of edit boxes with associated
638 * statics, and two buttons; then a list box.
639 */
640 void envsetter(struct ctlpos *cp, char *stext, int sid,
641 char *e1stext, int e1sid, int e1id,
642 char *e2stext, int e2sid, int e2id,
643 int listid, char *b1text, int b1id, char *b2text, int b2id)
644 {
645 RECT r;
646 const int height = (STATICHEIGHT > EDITHEIGHT
647 && STATICHEIGHT >
648 PUSHBTNHEIGHT ? STATICHEIGHT : EDITHEIGHT >
649 PUSHBTNHEIGHT ? EDITHEIGHT : PUSHBTNHEIGHT);
650 const static int percents[] = { 20, 35, 10, 25 };
651 int i, j, xpos, percent;
652 const int LISTHEIGHT = 42;
653
654 /* The static control. */
655 r.left = GAPBETWEEN;
656 r.top = cp->ypos;
657 r.right = cp->width;
658 r.bottom = STATICHEIGHT;
659 cp->ypos += r.bottom + GAPWITHIN;
660 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
661
662 /* The statics+edits+buttons. */
663 for (j = 0; j < 2; j++) {
664 percent = 10;
665 for (i = 0; i < 4; i++) {
666 xpos = (cp->width + GAPBETWEEN) * percent / 100;
667 r.left = xpos + GAPBETWEEN;
668 percent += percents[i];
669 xpos = (cp->width + GAPBETWEEN) * percent / 100;
670 r.right = xpos - r.left;
671 r.top = cp->ypos;
672 r.bottom = (i == 0 ? STATICHEIGHT :
673 i == 1 ? EDITHEIGHT : PUSHBTNHEIGHT);
674 r.top += (height - r.bottom) / 2;
675 if (i == 0) {
676 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0,
677 j == 0 ? e1stext : e2stext, j == 0 ? e1sid : e2sid);
678 } else if (i == 1) {
679 doctl(cp, r, "EDIT",
680 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
681 WS_EX_CLIENTEDGE, "", j == 0 ? e1id : e2id);
682 } else if (i == 3) {
683 doctl(cp, r, "BUTTON",
684 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
685 0, j == 0 ? b1text : b2text, j == 0 ? b1id : b2id);
686 }
687 }
688 cp->ypos += height + GAPWITHIN;
689 }
690
691 /* The list box. */
692 r.left = GAPBETWEEN;
693 r.top = cp->ypos;
694 r.right = cp->width;
695 r.bottom = LISTHEIGHT;
696 cp->ypos += r.bottom + GAPBETWEEN;
697 doctl(cp, r, "LISTBOX",
698 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS
699 | LBS_USETABSTOPS, WS_EX_CLIENTEDGE, "", listid);
700 }
701
702 /*
703 * Yet another special control: the character-class setter. A
704 * static, then a list, then a line containing a
705 * button-and-static-and-edit.
706 */
707 void charclass(struct ctlpos *cp, char *stext, int sid, int listid,
708 char *btext, int bid, int eid, char *s2text, int s2id)
709 {
710 RECT r;
711 const int height = (STATICHEIGHT > EDITHEIGHT
712 && STATICHEIGHT >
713 PUSHBTNHEIGHT ? STATICHEIGHT : EDITHEIGHT >
714 PUSHBTNHEIGHT ? EDITHEIGHT : PUSHBTNHEIGHT);
715 const static int percents[] = { 30, 40, 30 };
716 int i, xpos, percent;
717 const int LISTHEIGHT = 66;
718
719 /* The static control. */
720 r.left = GAPBETWEEN;
721 r.top = cp->ypos;
722 r.right = cp->width;
723 r.bottom = STATICHEIGHT;
724 cp->ypos += r.bottom + GAPWITHIN;
725 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
726
727 /* The list box. */
728 r.left = GAPBETWEEN;
729 r.top = cp->ypos;
730 r.right = cp->width;
731 r.bottom = LISTHEIGHT;
732 cp->ypos += r.bottom + GAPWITHIN;
733 doctl(cp, r, "LISTBOX",
734 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS
735 | LBS_USETABSTOPS, WS_EX_CLIENTEDGE, "", listid);
736
737 /* The button+static+edit. */
738 percent = xpos = 0;
739 for (i = 0; i < 3; i++) {
740 r.left = xpos + GAPBETWEEN;
741 percent += percents[i];
742 xpos = (cp->width + GAPBETWEEN) * percent / 100;
743 r.right = xpos - r.left;
744 r.top = cp->ypos;
745 r.bottom = (i == 0 ? PUSHBTNHEIGHT :
746 i == 1 ? STATICHEIGHT : EDITHEIGHT);
747 r.top += (height - r.bottom) / 2;
748 if (i == 0) {
749 doctl(cp, r, "BUTTON",
750 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
751 0, btext, bid);
752 } else if (i == 1) {
753 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_CENTER,
754 0, s2text, s2id);
755 } else if (i == 2) {
756 doctl(cp, r, "EDIT",
757 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
758 WS_EX_CLIENTEDGE, "", eid);
759 }
760 }
761 cp->ypos += height + GAPBETWEEN;
762 }
763
764 /*
765 * A special control (horrors!). The colour editor. A static line;
766 * then on the left, a list box, and on the right, a sequence of
767 * two-part statics followed by a button.
768 */
769 void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
770 char *btext, int bid, ...)
771 {
772 RECT r;
773 int y;
774 va_list ap;
775 int lwid, rwid, rpos;
776 const int LISTHEIGHT = 66;
777
778 /* The static control. */
779 r.left = GAPBETWEEN;
780 r.top = cp->ypos;
781 r.right = cp->width;
782 r.bottom = STATICHEIGHT;
783 cp->ypos += r.bottom + GAPWITHIN;
784 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
785
786 rpos = GAPBETWEEN + 2 * (cp->width + GAPBETWEEN) / 3;
787 lwid = rpos - 2 * GAPBETWEEN;
788 rwid = cp->width + GAPBETWEEN - rpos;
789
790 /* The list box. */
791 r.left = GAPBETWEEN;
792 r.top = cp->ypos;
793 r.right = lwid;
794 r.bottom = LISTHEIGHT;
795 doctl(cp, r, "LISTBOX",
796 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS
797 | LBS_USETABSTOPS | LBS_NOTIFY, WS_EX_CLIENTEDGE, "", listid);
798
799 /* The statics. */
800 y = cp->ypos;
801 va_start(ap, bid);
802 while (1) {
803 char *ltext;
804 int lid, rid;
805 ltext = va_arg(ap, char *);
806 if (!ltext)
807 break;
808 lid = va_arg(ap, int);
809 rid = va_arg(ap, int);
810 r.top = y;
811 r.bottom = STATICHEIGHT;
812 y += r.bottom + GAPWITHIN;
813 r.left = rpos;
814 r.right = rwid / 2;
815 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, ltext, lid);
816 r.left = rpos + r.right;
817 r.right = rwid - r.right;
818 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_RIGHT, 0, "",
819 rid);
820 }
821 va_end(ap);
822
823 /* The button. */
824 r.top = y + 2 * GAPWITHIN;
825 r.bottom = PUSHBTNHEIGHT;
826 r.left = rpos;
827 r.right = rwid;
828 doctl(cp, r, "BUTTON",
829 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
830 0, btext, bid);
831
832 cp->ypos += LISTHEIGHT + GAPBETWEEN;
833 }
834
835 /*
836 * A special control for manipulating an ordered preference list
837 * (eg. for cipher selection).
838 * XXX: this is a rough hack and could be improved.
839 */
840 void prefslist(struct prefslist *hdl, struct ctlpos *cp, char *stext,
841 int sid, int listid, int upbid, int dnbid)
842 {
843 const static int percents[] = { 5, 75, 20 };
844 RECT r;
845 int xpos, percent = 0, i;
846 const int DEFLISTHEIGHT = 52; /* XXX configurable? */
847 const int BTNSHEIGHT = 2*PUSHBTNHEIGHT + GAPBETWEEN;
848 int totalheight;
849
850 /* Squirrel away IDs. */
851 hdl->listid = listid;
852 hdl->upbid = upbid;
853 hdl->dnbid = dnbid;
854
855 /* The static label. */
856 r.left = GAPBETWEEN;
857 r.top = cp->ypos;
858 r.right = cp->width;
859 r.bottom = STATICHEIGHT;
860 cp->ypos += r.bottom + GAPWITHIN;
861 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
862
863 /* XXX it'd be nice to centre the buttons wrt the listbox
864 * but we'd have to find out how high the latter actually is. */
865 if (DEFLISTHEIGHT > BTNSHEIGHT) {
866 totalheight = DEFLISTHEIGHT;
867 } else {
868 totalheight = BTNSHEIGHT;
869 }
870
871 for (i=0; i<3; i++) {
872 int left, wid;
873 xpos = (cp->width + GAPBETWEEN) * percent / 100;
874 left = xpos + GAPBETWEEN;
875 percent += percents[i];
876 xpos = (cp->width + GAPBETWEEN) * percent / 100;
877 wid = xpos - left;
878
879 switch (i) {
880 case 1:
881 /* The drag list box. */
882 r.left = left; r.right = wid;
883 r.top = cp->ypos; r.bottom = totalheight;
884 {
885 HWND ctl;
886 ctl = doctl(cp, r, "LISTBOX",
887 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
888 WS_VSCROLL | LBS_HASSTRINGS,
889 WS_EX_CLIENTEDGE,
890 "", listid);
891 MakeDragList(ctl);
892 }
893 break;
894
895 case 2:
896 /* The "Up" and "Down" buttons. */
897 /* XXX worry about accelerators if we have more than one
898 * prefslist on a panel */
899 r.left = left; r.right = wid;
900 r.top = cp->ypos; r.bottom = PUSHBTNHEIGHT;
901 doctl(cp, r, "BUTTON",
902 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
903 0, "&Up", upbid);
904
905 r.left = left; r.right = wid;
906 r.top = cp->ypos + PUSHBTNHEIGHT + GAPBETWEEN;
907 r.bottom = PUSHBTNHEIGHT;
908 doctl(cp, r, "BUTTON",
909 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
910 0, "&Down", dnbid);
911
912 break;
913
914 }
915 }
916
917 cp->ypos += totalheight + GAPBETWEEN;
918
919 }
920
921 /*
922 * Helper function for prefslist: move item in list box.
923 */
924 static void pl_moveitem(HWND hwnd, int listid, int src, int dst)
925 {
926 int tlen, val;
927 char *txt;
928 /* Get the item's data. */
929 tlen = SendDlgItemMessage (hwnd, listid, LB_GETTEXTLEN, src, 0);
930 txt = smalloc(tlen+1);
931 SendDlgItemMessage (hwnd, listid, LB_GETTEXT, src, (LPARAM) txt);
932 val = SendDlgItemMessage (hwnd, listid, LB_GETITEMDATA, src, 0);
933 /* Deselect old location. */
934 SendDlgItemMessage (hwnd, listid, LB_SETSEL, FALSE, src);
935 /* Delete it at the old location. */
936 SendDlgItemMessage (hwnd, listid, LB_DELETESTRING, src, 0);
937 /* Insert it at new location. */
938 SendDlgItemMessage (hwnd, listid, LB_INSERTSTRING, dst,
939 (LPARAM) txt);
940 SendDlgItemMessage (hwnd, listid, LB_SETITEMDATA, dst,
941 (LPARAM) val);
942 /* Set selection. */
943 SendDlgItemMessage (hwnd, listid, LB_SETCURSEL, dst, 0);
944 sfree (txt);
945 }
946
947 int pl_itemfrompt(HWND hwnd, POINT cursor, BOOL scroll)
948 {
949 int ret;
950 POINT uppoint, downpoint;
951 int updist, downdist, upitem, downitem, i;
952
953 /*
954 * Ghastly hackery to try to figure out not which
955 * _item_, but which _gap between items_, the user
956 * is pointing at. We do this by first working out
957 * which list item is under the cursor, and then
958 * working out how far the cursor would have to
959 * move up or down before the answer was different.
960 * Then we put the insertion point _above_ the
961 * current item if the upper edge is closer than
962 * the lower edge, or _below_ it if vice versa.
963 */
964 ret = LBItemFromPt(hwnd, cursor, scroll);
965 if (ret == -1)
966 return ret;
967 ret = LBItemFromPt(hwnd, cursor, FALSE);
968 updist = downdist = 0;
969 for (i = 1; i < 4096 && (!updist || !downdist); i++) {
970 uppoint = downpoint = cursor;
971 uppoint.y -= i;
972 downpoint.y += i;
973 upitem = LBItemFromPt(hwnd, uppoint, FALSE);
974 downitem = LBItemFromPt(hwnd, downpoint, FALSE);
975 if (!updist && upitem != ret)
976 updist = i;
977 if (!downdist && downitem != ret)
978 downdist = i;
979 }
980 if (downdist < updist)
981 ret++;
982 return ret;
983 }
984
985 /*
986 * Handler for prefslist above.
987 */
988 int handle_prefslist(struct prefslist *hdl,
989 int *array, int maxmemb,
990 int is_dlmsg, HWND hwnd,
991 WPARAM wParam, LPARAM lParam)
992 {
993 int i;
994 int ret;
995
996 if (is_dlmsg) {
997
998 if (wParam == hdl->listid) {
999 DRAGLISTINFO *dlm = (DRAGLISTINFO *)lParam;
1000 int dest;
1001 switch (dlm->uNotification) {
1002 case DL_BEGINDRAG:
1003 hdl->dummyitem =
1004 SendDlgItemMessage(hwnd, hdl->listid,
1005 LB_ADDSTRING, 0, (LPARAM) "");
1006
1007 hdl->srcitem = LBItemFromPt(dlm->hWnd, dlm->ptCursor, TRUE);
1008 hdl->dragging = 0;
1009 /* XXX hack Q183115 */
1010 SetWindowLong(hwnd, DWL_MSGRESULT, TRUE);
1011 ret = 1; break;
1012 case DL_CANCELDRAG:
1013 DrawInsert(hwnd, dlm->hWnd, -1); /* Clear arrow */
1014 SendDlgItemMessage(hwnd, hdl->listid,
1015 LB_DELETESTRING, hdl->dummyitem, 0);
1016 hdl->dragging = 0;
1017 ret = 1; break;
1018 case DL_DRAGGING:
1019 hdl->dragging = 1;
1020 dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, TRUE);
1021 if (dest > hdl->dummyitem) dest = hdl->dummyitem;
1022 DrawInsert (hwnd, dlm->hWnd, dest);
1023 if (dest >= 0)
1024 SetWindowLong(hwnd, DWL_MSGRESULT, DL_MOVECURSOR);
1025 else
1026 SetWindowLong(hwnd, DWL_MSGRESULT, DL_STOPCURSOR);
1027 ret = 1; break;
1028 case DL_DROPPED:
1029 ret = 1;
1030 if (!hdl->dragging) break;
1031 dest = pl_itemfrompt(dlm->hWnd, dlm->ptCursor, TRUE);
1032 if (dest > hdl->dummyitem) dest = hdl->dummyitem;
1033 DrawInsert (hwnd, dlm->hWnd, -1);
1034 SendDlgItemMessage(hwnd, hdl->listid,
1035 LB_DELETESTRING, hdl->dummyitem, 0);
1036 hdl->dragging = 0;
1037 if (dest >= 0) {
1038 /* Correct for "missing" item. This means you can't drag
1039 * an item to the end, but that seems to be the way this
1040 * control is used. */
1041 if (dest > hdl->srcitem) dest--;
1042 pl_moveitem(hwnd, hdl->listid, hdl->srcitem, dest);
1043 }
1044 ret = 1; break;
1045 }
1046 }
1047
1048 } else {
1049
1050 ret = 0;
1051 if (((LOWORD(wParam) == hdl->upbid) ||
1052 (LOWORD(wParam) == hdl->dnbid)) &&
1053 ((HIWORD(wParam) == BN_CLICKED) ||
1054 (HIWORD(wParam) == BN_DOUBLECLICKED))) {
1055 /* Move an item up or down the list. */
1056 /* Get the current selection, if any. */
1057 int selection = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCURSEL, 0, 0);
1058 if (selection == LB_ERR) {
1059 MessageBeep(0);
1060 } else {
1061 int nitems;
1062 /* Get the total number of items. */
1063 nitems = SendDlgItemMessage (hwnd, hdl->listid, LB_GETCOUNT, 0, 0);
1064 /* Should we do anything? */
1065 if (LOWORD(wParam) == hdl->upbid && (selection > 0))
1066 pl_moveitem(hwnd, hdl->listid, selection, selection - 1);
1067 else if (LOWORD(wParam) == hdl->dnbid && (selection < nitems - 1))
1068 pl_moveitem(hwnd, hdl->listid, selection, selection + 1);
1069 }
1070
1071 }
1072
1073 }
1074
1075 /* Update array to match the list box. */
1076 for (i=0; i < maxmemb; i++)
1077 array[i] = SendDlgItemMessage (hwnd, hdl->listid, LB_GETITEMDATA,
1078 i, 0);
1079
1080 return ret;
1081
1082 }
1083
1084 /*
1085 * A progress bar (from Common Controls). We like our progress bars
1086 * to be smooth and unbroken, without those ugly divisions; some
1087 * older compilers may not support that, but that's life.
1088 */
1089 void progressbar(struct ctlpos *cp, int id)
1090 {
1091 RECT r;
1092
1093 r.left = GAPBETWEEN;
1094 r.top = cp->ypos;
1095 r.right = cp->width;
1096 r.bottom = PROGBARHEIGHT;
1097 cp->ypos += r.bottom + GAPBETWEEN;
1098
1099 doctl(cp, r, PROGRESS_CLASS, WS_CHILD | WS_VISIBLE
1100 #ifdef PBS_SMOOTH
1101 | PBS_SMOOTH
1102 #endif
1103 , WS_EX_CLIENTEDGE, "", id);
1104 }
1105
1106 /*
1107 * Another special control: the forwarding options setter. First a
1108 * list box; next a static header line, introducing a pair of edit
1109 * boxes with associated statics, another button, and a radio
1110 * button pair.
1111 */
1112 void fwdsetter(struct ctlpos *cp, int listid, char *stext, int sid,
1113 char *e1stext, int e1sid, int e1id,
1114 char *e2stext, int e2sid, int e2id,
1115 char *btext, int bid)
1116 {
1117 RECT r;
1118 const int height = (STATICHEIGHT > EDITHEIGHT
1119 && STATICHEIGHT >
1120 PUSHBTNHEIGHT ? STATICHEIGHT : EDITHEIGHT >
1121 PUSHBTNHEIGHT ? EDITHEIGHT : PUSHBTNHEIGHT);
1122 const static int percents[] = { 25, 35, 15, 25 };
1123 int i, j, xpos, percent;
1124 const int LISTHEIGHT = 42;
1125
1126 /* The list box. */
1127 r.left = GAPBETWEEN;
1128 r.top = cp->ypos;
1129 r.right = cp->width;
1130 r.bottom = LISTHEIGHT;
1131 cp->ypos += r.bottom + GAPBETWEEN;
1132 doctl(cp, r, "LISTBOX",
1133 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS
1134 | LBS_USETABSTOPS, WS_EX_CLIENTEDGE, "", listid);
1135
1136 /* The static control. */
1137 r.left = GAPBETWEEN;
1138 r.top = cp->ypos;
1139 r.right = cp->width;
1140 r.bottom = STATICHEIGHT;
1141 cp->ypos += r.bottom + GAPWITHIN;
1142 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
1143
1144 /* The statics+edits+buttons. */
1145 for (j = 0; j < 2; j++) {
1146 percent = 0;
1147 for (i = 0; i < (j ? 2 : 4); i++) {
1148 xpos = (cp->width + GAPBETWEEN) * percent / 100;
1149 r.left = xpos + GAPBETWEEN;
1150 percent += percents[i];
1151 if (j==1 && i==1) percent = 100;
1152 xpos = (cp->width + GAPBETWEEN) * percent / 100;
1153 r.right = xpos - r.left;
1154 r.top = cp->ypos;
1155 r.bottom = (i == 0 ? STATICHEIGHT :
1156 i == 1 ? EDITHEIGHT : PUSHBTNHEIGHT);
1157 r.top += (height - r.bottom) / 2;
1158 if (i == 0) {
1159 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0,
1160 j == 0 ? e1stext : e2stext, j == 0 ? e1sid : e2sid);
1161 } else if (i == 1) {
1162 doctl(cp, r, "EDIT",
1163 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
1164 WS_EX_CLIENTEDGE, "", j == 0 ? e1id : e2id);
1165 } else if (i == 3) {
1166 doctl(cp, r, "BUTTON",
1167 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
1168 0, btext, bid);
1169 }
1170 }
1171 cp->ypos += height + GAPWITHIN;
1172 }
1173 }