2 * winctrls.c: routines to self-manage the controls in a dialog
16 #define STATICHEIGHT 8
17 #define CHECKBOXHEIGHT 8
20 #define COMBOHEIGHT 12
21 #define PUSHBTNHEIGHT 14
22 #define PROGBARHEIGHT 14
24 void ctlposinit(struct ctlpos *cp, HWND hwnd,
25 int leftborder, int rightborder, int topborder) {
28 cp->font = SendMessage(hwnd, WM_GETFONT, 0, 0);
30 GetClientRect(hwnd, &r);
34 MapDialogRect(hwnd, &r2);
35 cp->dlu4inpix = r2.right;
36 cp->width = (r.right * 4) / (r2.right) - 2*GAPBETWEEN;
37 cp->xoff = leftborder;
38 cp->width -= leftborder + rightborder;
41 void doctl(struct ctlpos *cp, RECT r,
42 char *wclass, int wstyle, int exstyle,
43 char *wtext, int wid) {
46 * Note nonstandard use of RECT. This is deliberate: by
47 * transforming the width and height directly we arrange to
48 * have all supposedly same-sized controls really same-sized.
52 MapDialogRect(cp->hwnd, &r);
54 ctl = CreateWindowEx(exstyle, wclass, wtext, wstyle,
55 r.left, r.top, r.right, r.bottom,
56 cp->hwnd, (HMENU)wid, hinst, NULL);
57 SendMessage(ctl, WM_SETFONT, cp->font, MAKELPARAM(TRUE, 0));
61 * A title bar across the top of a sub-dialog.
63 void bartitle(struct ctlpos *cp, char *name, int id) {
66 r.left = GAPBETWEEN; r.right = cp->width;
67 r.top = cp->ypos; r.bottom = STATICHEIGHT;
68 cp->ypos += r.bottom + GAPBETWEEN;
69 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, name, id);
73 * Begin a grouping box, with or without a group title.
75 void beginbox(struct ctlpos *cp, char *name, int idbox) {
76 cp->boxystart = cp->ypos;
78 cp->boxystart -= STATICHEIGHT/2;
80 cp->ypos += STATICHEIGHT;
82 cp->width -= 2*GAPXBOX;
91 void endbox(struct ctlpos *cp) {
94 cp->width += 2*GAPXBOX;
95 cp->ypos += GAPYBOX - GAPBETWEEN;
96 r.left = GAPBETWEEN; r.right = cp->width;
97 r.top = cp->boxystart; r.bottom = cp->ypos - cp->boxystart;
98 doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0,
99 cp->boxtext ? cp->boxtext : "", cp->boxid);
104 * Some edit boxes. Each one has a static above it. The percentages
105 * of the horizontal space are provided.
107 void multiedit(struct ctlpos *cp, ...) {
116 int staticid, editid, pcwidth;
117 text = va_arg(ap, char *);
120 staticid = va_arg(ap, int);
121 editid = va_arg(ap, int);
122 pcwidth = va_arg(ap, int);
124 r.left = xpos + GAPBETWEEN;
126 xpos = (cp->width + GAPBETWEEN) * percent / 100;
127 r.right = xpos - r.left;
129 r.top = cp->ypos; r.bottom = STATICHEIGHT;
130 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0,
132 r.top = cp->ypos + 8 + GAPWITHIN; r.bottom = EDITHEIGHT;
134 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
139 cp->ypos += 8+GAPWITHIN+12+GAPBETWEEN;
143 * A set of radio buttons on the same line, with a static above
144 * them. `nacross' dictates how many parts the line is divided into
145 * (you might want this not to equal the number of buttons if you
146 * needed to line up some 2s and some 3s to look good in the same
149 * There's a bit of a hack in here to ensure that if nacross
150 * exceeds the actual number of buttons, the rightmost button
151 * really does get all the space right to the edge of the line, so
152 * you can do things like
154 * (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
156 void radioline(struct ctlpos *cp,
157 char *text, int id, int nacross, ...) {
164 r.left = GAPBETWEEN; r.top = cp->ypos;
165 r.right = cp->width; r.bottom = STATICHEIGHT;
166 cp->ypos += r.bottom + GAPWITHIN;
167 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
168 va_start(ap, nacross);
171 btext = va_arg(ap, char *);
178 cp->ypos += r.bottom + GAPBETWEEN;
181 bid = va_arg(ap, int);
182 nextbtext = va_arg(ap, char *);
183 r.left = GAPBETWEEN + i * (cp->width+GAPBETWEEN)/nacross;
185 r.right = (i+1) * (cp->width+GAPBETWEEN)/nacross - r.left;
187 r.right = cp->width - r.left;
188 r.top = cp->ypos; r.bottom = RADIOHEIGHT;
189 doctl(cp, r, "BUTTON",
190 BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP | group,
198 cp->ypos += r.bottom + GAPBETWEEN;
202 * A set of radio buttons on multiple lines, with a static above
205 void radiobig(struct ctlpos *cp, char *text, int id, ...) {
210 r.left = GAPBETWEEN; r.top = cp->ypos;
211 r.right = cp->width; r.bottom = STATICHEIGHT;
212 cp->ypos += r.bottom + GAPWITHIN;
213 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
219 btext = va_arg(ap, char *);
222 bid = va_arg(ap, int);
223 r.left = GAPBETWEEN; r.top = cp->ypos;
224 r.right = cp->width; r.bottom = STATICHEIGHT;
225 cp->ypos += r.bottom + GAPWITHIN;
226 doctl(cp, r, "BUTTON",
227 BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP | group,
233 cp->ypos += GAPBETWEEN - GAPWITHIN;
237 * A single standalone checkbox.
239 void checkbox(struct ctlpos *cp, char *text, int id) {
242 r.left = GAPBETWEEN; r.top = cp->ypos;
243 r.right = cp->width; r.bottom = CHECKBOXHEIGHT;
244 cp->ypos += r.bottom + GAPBETWEEN;
245 doctl(cp, r, "BUTTON",
246 BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
251 * A single standalone static text control.
253 void statictext(struct ctlpos *cp, char *text, int id) {
256 r.left = GAPBETWEEN; r.top = cp->ypos;
257 r.right = cp->width; r.bottom = STATICHEIGHT;
258 cp->ypos += r.bottom + GAPBETWEEN;
259 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
263 * A button on the right hand side, with a static to its left.
265 void staticbtn(struct ctlpos *cp, char *stext, int sid,
266 char *btext, int bid) {
267 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
268 PUSHBTNHEIGHT : STATICHEIGHT);
270 int lwid, rwid, rpos;
272 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
273 lwid = rpos - 2*GAPBETWEEN;
274 rwid = cp->width + GAPBETWEEN - rpos;
276 r.left = GAPBETWEEN; r.top = cp->ypos + (height-STATICHEIGHT)/2;
277 r.right = lwid; r.bottom = STATICHEIGHT;
278 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
280 r.left = rpos; r.top = cp->ypos + (height-PUSHBTNHEIGHT)/2;
281 r.right = rwid; r.bottom = PUSHBTNHEIGHT;
282 doctl(cp, r, "BUTTON",
283 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
287 cp->ypos += height + GAPBETWEEN;
291 * An edit control on the right hand side, with a static to its left.
293 static void staticedit_internal(struct ctlpos *cp, char *stext,
294 int sid, int eid, int percentedit,
296 const int height = (EDITHEIGHT > STATICHEIGHT ?
297 EDITHEIGHT : STATICHEIGHT);
299 int lwid, rwid, rpos;
301 rpos = GAPBETWEEN + (100-percentedit) * (cp->width + GAPBETWEEN) / 100;
302 lwid = rpos - 2*GAPBETWEEN;
303 rwid = cp->width + GAPBETWEEN - rpos;
305 r.left = GAPBETWEEN; r.top = cp->ypos + (height-STATICHEIGHT)/2;
306 r.right = lwid; r.bottom = STATICHEIGHT;
307 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
309 r.left = rpos; r.top = cp->ypos + (height-EDITHEIGHT)/2;
310 r.right = rwid; r.bottom = EDITHEIGHT;
312 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
316 cp->ypos += height + GAPBETWEEN;
319 void staticedit(struct ctlpos *cp, char *stext,
320 int sid, int eid, int percentedit) {
321 staticedit_internal(cp, stext, sid, eid, percentedit, 0);
324 void staticpassedit(struct ctlpos *cp, char *stext,
325 int sid, int eid, int percentedit) {
326 staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
330 * A big multiline edit control with a static labelling it.
332 void bigeditctrl(struct ctlpos *cp, char *stext,
333 int sid, int eid, int lines) {
336 r.left = GAPBETWEEN; r.top = cp->ypos;
337 r.right = cp->width; r.bottom = STATICHEIGHT;
338 cp->ypos += r.bottom + GAPWITHIN;
339 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
341 r.left = GAPBETWEEN; r.top = cp->ypos;
342 r.right = cp->width; r.bottom = EDITHEIGHT + (lines-1) * STATICHEIGHT;
343 cp->ypos += r.bottom + GAPBETWEEN;
345 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
351 * A tab-control substitute when a real tab control is unavailable.
353 void ersatztab(struct ctlpos *cp, char *stext, int sid,
355 const int height = (COMBOHEIGHT > STATICHEIGHT ?
356 COMBOHEIGHT : STATICHEIGHT);
358 int bigwid, lwid, rwid, rpos;
359 static const int BIGGAP = 15;
360 static const int MEDGAP = 3;
362 bigwid = cp->width + 2*GAPBETWEEN - 2*BIGGAP;
364 rpos = BIGGAP + (bigwid + BIGGAP) / 2;
365 lwid = rpos - 2*BIGGAP;
366 rwid = bigwid + BIGGAP - rpos;
368 r.left = BIGGAP; r.top = cp->ypos + (height-STATICHEIGHT)/2;
369 r.right = lwid; r.bottom = STATICHEIGHT;
370 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
372 r.left = rpos; r.top = cp->ypos + (height-COMBOHEIGHT)/2;
373 r.right = rwid; r.bottom = COMBOHEIGHT*10;
374 doctl(cp, r, "COMBOBOX",
375 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
376 CBS_DROPDOWNLIST | CBS_HASSTRINGS,
380 cp->ypos += height + MEDGAP + GAPBETWEEN;
382 r.left = GAPBETWEEN; r.top = cp->ypos;
383 r.right = cp->width; r.bottom = 2;
384 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
389 * A static line, followed by an edit control on the left hand side
390 * and a button on the right.
392 void editbutton(struct ctlpos *cp, char *stext, int sid,
393 int eid, char *btext, int bid) {
394 const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
395 EDITHEIGHT : PUSHBTNHEIGHT);
397 int lwid, rwid, rpos;
399 r.left = GAPBETWEEN; r.top = cp->ypos;
400 r.right = cp->width; r.bottom = STATICHEIGHT;
401 cp->ypos += r.bottom + GAPWITHIN;
402 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
404 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
405 lwid = rpos - 2*GAPBETWEEN;
406 rwid = cp->width + GAPBETWEEN - rpos;
408 r.left = GAPBETWEEN; r.top = cp->ypos + (height-EDITHEIGHT)/2;
409 r.right = lwid; r.bottom = EDITHEIGHT;
411 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
415 r.left = rpos; r.top = cp->ypos + (height-PUSHBTNHEIGHT)/2;
416 r.right = rwid; r.bottom = PUSHBTNHEIGHT;
417 doctl(cp, r, "BUTTON",
418 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
422 cp->ypos += height + GAPBETWEEN;
426 * Special control which was hard to describe generically: the
427 * session-saver assembly. A static; below that an edit box; below
428 * that a list box. To the right of the list box, a column of
431 void sesssaver(struct ctlpos *cp, char *text,
432 int staticid, int editid, int listid, ...) {
435 int lwid, rwid, rpos;
437 const int LISTDEFHEIGHT = 66;
439 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
440 lwid = rpos - 2*GAPBETWEEN;
441 rwid = cp->width + GAPBETWEEN - rpos;
443 /* The static control. */
444 r.left = GAPBETWEEN; r.top = cp->ypos;
445 r.right = lwid; r.bottom = STATICHEIGHT;
446 cp->ypos += r.bottom + GAPWITHIN;
447 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
449 /* The edit control. */
450 r.left = GAPBETWEEN; r.top = cp->ypos;
451 r.right = lwid; r.bottom = EDITHEIGHT;
452 cp->ypos += r.bottom + GAPWITHIN;
454 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
459 * The buttons (we should hold off on the list box until we
460 * know how big the buttons are).
462 va_start(ap, listid);
465 char *btext = va_arg(ap, char *);
468 bid = va_arg(ap, int);
469 r.left = rpos; r.top = y;
470 r.right = rwid; r.bottom = PUSHBTNHEIGHT;
471 y += r.bottom + GAPWITHIN;
472 doctl(cp, r, "BUTTON",
473 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
478 /* Compute list box height. LISTDEFHEIGHT, or height of buttons. */
481 if (y < LISTDEFHEIGHT) y = LISTDEFHEIGHT;
482 r.left = GAPBETWEEN; r.top = cp->ypos;
483 r.right = lwid; r.bottom = y;
484 cp->ypos += y + GAPBETWEEN;
485 doctl(cp, r, "LISTBOX",
486 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
487 LBS_NOTIFY | LBS_HASSTRINGS,
493 * Another special control: the environment-variable setter. A
494 * static line first; then a pair of edit boxes with associated
495 * statics, and two buttons; then a list box.
497 void envsetter(struct ctlpos *cp, char *stext, int sid,
498 char *e1stext, int e1sid, int e1id,
499 char *e2stext, int e2sid, int e2id,
501 char *b1text, int b1id, char *b2text, int b2id) {
503 const int height = (STATICHEIGHT > EDITHEIGHT && STATICHEIGHT > PUSHBTNHEIGHT ?
505 EDITHEIGHT > PUSHBTNHEIGHT ?
506 EDITHEIGHT : PUSHBTNHEIGHT);
507 const static int percents[] = { 20, 35, 10, 25 };
508 int i, j, xpos, percent;
509 const int LISTHEIGHT = 42;
511 /* The static control. */
512 r.left = GAPBETWEEN; r.top = cp->ypos;
513 r.right = cp->width; r.bottom = STATICHEIGHT;
514 cp->ypos += r.bottom + GAPWITHIN;
515 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
517 /* The statics+edits+buttons. */
518 for (j = 0; j < 2; j++) {
520 for (i = 0; i < 4; i++) {
521 xpos = (cp->width + GAPBETWEEN) * percent / 100;
522 r.left = xpos + GAPBETWEEN;
523 percent += percents[i];
524 xpos = (cp->width + GAPBETWEEN) * percent / 100;
525 r.right = xpos - r.left;
527 r.bottom = (i==0 ? STATICHEIGHT :
530 r.top += (height-r.bottom)/2;
532 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0,
533 j==0 ? e1stext : e2stext, j==0 ? e1sid : e2sid);
536 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
538 "", j==0 ? e1id : e2id);
540 doctl(cp, r, "BUTTON",
541 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
543 j==0 ? b1text : b2text, j==0 ? b1id : b2id);
546 cp->ypos += height + GAPWITHIN;
550 r.left = GAPBETWEEN; r.top = cp->ypos;
551 r.right = cp->width; r.bottom = LISTHEIGHT;
552 cp->ypos += r.bottom + GAPBETWEEN;
553 doctl(cp, r, "LISTBOX",
554 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS |
561 * Yet another special control: the character-class setter. A
562 * static, then a list, then a line containing a
563 * button-and-static-and-edit.
565 void charclass(struct ctlpos *cp, char *stext, int sid, int listid,
566 char *btext, int bid, int eid, char *s2text, int s2id) {
568 const int height = (STATICHEIGHT > EDITHEIGHT && STATICHEIGHT > PUSHBTNHEIGHT ?
570 EDITHEIGHT > PUSHBTNHEIGHT ?
571 EDITHEIGHT : PUSHBTNHEIGHT);
572 const static int percents[] = { 30, 40, 30 };
573 int i, xpos, percent;
574 const int LISTHEIGHT = 66;
576 /* The static control. */
577 r.left = GAPBETWEEN; r.top = cp->ypos;
578 r.right = cp->width; r.bottom = STATICHEIGHT;
579 cp->ypos += r.bottom + GAPWITHIN;
580 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
583 r.left = GAPBETWEEN; r.top = cp->ypos;
584 r.right = cp->width; r.bottom = LISTHEIGHT;
585 cp->ypos += r.bottom + GAPWITHIN;
586 doctl(cp, r, "LISTBOX",
587 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS |
592 /* The button+static+edit. */
594 for (i = 0; i < 3; i++) {
595 r.left = xpos + GAPBETWEEN;
596 percent += percents[i];
597 xpos = (cp->width + GAPBETWEEN) * percent / 100;
598 r.right = xpos - r.left;
600 r.bottom = (i==0 ? PUSHBTNHEIGHT :
601 i==1 ? STATICHEIGHT :
603 r.top += (height-r.bottom)/2;
605 doctl(cp, r, "BUTTON",
606 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
609 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_CENTER,
613 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
614 WS_EX_CLIENTEDGE, "", eid);
617 cp->ypos += height + GAPBETWEEN;
621 * A special control (horrors!). The colour editor. A static line;
622 * then on the left, a list box, and on the right, a sequence of
623 * two-part statics followed by a button.
625 void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
626 char *btext, int bid, ...) {
630 int lwid, rwid, rpos;
631 const int LISTHEIGHT = 66;
633 /* The static control. */
634 r.left = GAPBETWEEN; r.top = cp->ypos;
635 r.right = cp->width; r.bottom = STATICHEIGHT;
636 cp->ypos += r.bottom + GAPWITHIN;
637 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
639 rpos = GAPBETWEEN + 2 * (cp->width + GAPBETWEEN) / 3;
640 lwid = rpos - 2*GAPBETWEEN;
641 rwid = cp->width + GAPBETWEEN - rpos;
644 r.left = GAPBETWEEN; r.top = cp->ypos;
645 r.right = lwid; r.bottom = LISTHEIGHT;
646 doctl(cp, r, "LISTBOX",
647 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS |
648 LBS_USETABSTOPS | LBS_NOTIFY,
658 ltext = va_arg(ap, char *);
660 lid = va_arg(ap, int);
661 rid = va_arg(ap, int);
662 r.top = y; r.bottom = STATICHEIGHT;
663 y += r.bottom + GAPWITHIN;
664 r.left = rpos; r.right = rwid/2;
665 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, ltext, lid);
666 r.left = rpos + r.right; r.right = rwid - r.right;
667 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_RIGHT, 0, "", rid);
672 r.top = y + 2*GAPWITHIN; r.bottom = PUSHBTNHEIGHT;
673 r.left = rpos; r.right = rwid;
674 doctl(cp, r, "BUTTON",
675 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
678 cp->ypos += LISTHEIGHT + GAPBETWEEN;
682 * A progress bar (from Common Controls). We like our progress bars
683 * to be smooth and unbroken, without those ugly divisions; some
684 * older compilers may not support that, but that's life.
686 void progressbar(struct ctlpos *cp, int id) {
689 r.left = GAPBETWEEN; r.top = cp->ypos;
690 r.right = cp->width; r.bottom = PROGBARHEIGHT;
691 cp->ypos += r.bottom + GAPBETWEEN;
693 doctl(cp, r, PROGRESS_CLASS,
694 WS_CHILD | WS_VISIBLE
698 , WS_EX_CLIENTEDGE, "", id);