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)
29 cp->font = SendMessage(hwnd, WM_GETFONT, 0, 0);
31 GetClientRect(hwnd, &r);
35 MapDialogRect(hwnd, &r2);
36 cp->dlu4inpix = r2.right;
37 cp->width = (r.right * 4) / (r2.right) - 2 * GAPBETWEEN;
38 cp->xoff = leftborder;
39 cp->width -= leftborder + rightborder;
42 void doctl(struct ctlpos *cp, RECT r,
43 char *wclass, int wstyle, int exstyle, char *wtext, int wid)
47 * Note nonstandard use of RECT. This is deliberate: by
48 * transforming the width and height directly we arrange to
49 * have all supposedly same-sized controls really same-sized.
53 MapDialogRect(cp->hwnd, &r);
55 ctl = CreateWindowEx(exstyle, wclass, wtext, wstyle,
56 r.left, r.top, r.right, r.bottom,
57 cp->hwnd, (HMENU) wid, hinst, NULL);
58 SendMessage(ctl, WM_SETFONT, cp->font, MAKELPARAM(TRUE, 0));
62 * A title bar across the top of a sub-dialog.
64 void bartitle(struct ctlpos *cp, char *name, int id)
71 r.bottom = STATICHEIGHT;
72 cp->ypos += r.bottom + GAPBETWEEN;
73 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, name, id);
77 * Begin a grouping box, with or without a group title.
79 void beginbox(struct ctlpos *cp, char *name, int idbox)
81 cp->boxystart = cp->ypos;
83 cp->boxystart -= STATICHEIGHT / 2;
85 cp->ypos += STATICHEIGHT;
87 cp->width -= 2 * GAPXBOX;
96 void endbox(struct ctlpos *cp)
100 cp->width += 2 * GAPXBOX;
101 cp->ypos += GAPYBOX - GAPBETWEEN;
104 r.top = cp->boxystart;
105 r.bottom = cp->ypos - cp->boxystart;
106 doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0,
107 cp->boxtext ? cp->boxtext : "", cp->boxid);
112 * Some edit boxes. Each one has a static above it. The percentages
113 * of the horizontal space are provided.
115 void multiedit(struct ctlpos *cp, ...)
125 int staticid, editid, pcwidth;
126 text = va_arg(ap, char *);
129 staticid = va_arg(ap, int);
130 editid = va_arg(ap, int);
131 pcwidth = va_arg(ap, int);
133 r.left = xpos + GAPBETWEEN;
135 xpos = (cp->width + GAPBETWEEN) * percent / 100;
136 r.right = xpos - r.left;
139 r.bottom = STATICHEIGHT;
140 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
141 r.top = cp->ypos + 8 + GAPWITHIN;
142 r.bottom = EDITHEIGHT;
144 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
145 WS_EX_CLIENTEDGE, "", editid);
148 cp->ypos += 8 + GAPWITHIN + 12 + GAPBETWEEN;
152 * A set of radio buttons on the same line, with a static above
153 * them. `nacross' dictates how many parts the line is divided into
154 * (you might want this not to equal the number of buttons if you
155 * needed to line up some 2s and some 3s to look good in the same
158 * There's a bit of a hack in here to ensure that if nacross
159 * exceeds the actual number of buttons, the rightmost button
160 * really does get all the space right to the edge of the line, so
161 * you can do things like
163 * (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
165 void radioline(struct ctlpos *cp, char *text, int id, int nacross, ...)
176 r.bottom = STATICHEIGHT;
177 cp->ypos += r.bottom + GAPWITHIN;
178 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
179 va_start(ap, nacross);
182 btext = va_arg(ap, char *);
189 cp->ypos += r.bottom + GAPBETWEEN;
192 bid = va_arg(ap, int);
193 nextbtext = va_arg(ap, char *);
194 r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
197 (i + 1) * (cp->width + GAPBETWEEN) / nacross - r.left;
199 r.right = cp->width - r.left;
201 r.bottom = RADIOHEIGHT;
202 doctl(cp, r, "BUTTON",
203 BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
204 group, 0, btext, bid);
210 cp->ypos += r.bottom + GAPBETWEEN;
214 * A set of radio buttons on multiple lines, with a static above
217 void radiobig(struct ctlpos *cp, char *text, int id, ...)
226 r.bottom = STATICHEIGHT;
227 cp->ypos += r.bottom + GAPWITHIN;
228 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
234 btext = va_arg(ap, char *);
237 bid = va_arg(ap, int);
241 r.bottom = STATICHEIGHT;
242 cp->ypos += r.bottom + GAPWITHIN;
243 doctl(cp, r, "BUTTON",
244 BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
245 group, 0, btext, bid);
249 cp->ypos += GAPBETWEEN - GAPWITHIN;
253 * A single standalone checkbox.
255 void checkbox(struct ctlpos *cp, char *text, int id)
262 r.bottom = CHECKBOXHEIGHT;
263 cp->ypos += r.bottom + GAPBETWEEN;
264 doctl(cp, r, "BUTTON",
265 BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
270 * A single standalone static text control.
272 void statictext(struct ctlpos *cp, char *text, int id)
279 r.bottom = STATICHEIGHT;
280 cp->ypos += r.bottom + GAPBETWEEN;
281 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
285 * A button on the right hand side, with a static to its left.
287 void staticbtn(struct ctlpos *cp, char *stext, int sid,
288 char *btext, int bid)
290 const int height = (PUSHBTNHEIGHT > STATICHEIGHT ?
291 PUSHBTNHEIGHT : STATICHEIGHT);
293 int lwid, rwid, rpos;
295 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
296 lwid = rpos - 2 * GAPBETWEEN;
297 rwid = cp->width + GAPBETWEEN - rpos;
300 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
302 r.bottom = STATICHEIGHT;
303 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
306 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
308 r.bottom = PUSHBTNHEIGHT;
309 doctl(cp, r, "BUTTON",
310 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
313 cp->ypos += height + GAPBETWEEN;
317 * An edit control on the right hand side, with a static to its left.
319 static void staticedit_internal(struct ctlpos *cp, char *stext,
320 int sid, int eid, int percentedit,
323 const int height = (EDITHEIGHT > STATICHEIGHT ?
324 EDITHEIGHT : STATICHEIGHT);
326 int lwid, rwid, rpos;
329 GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
330 lwid = rpos - 2 * GAPBETWEEN;
331 rwid = cp->width + GAPBETWEEN - rpos;
334 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
336 r.bottom = STATICHEIGHT;
337 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
340 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
342 r.bottom = EDITHEIGHT;
344 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
345 WS_EX_CLIENTEDGE, "", eid);
347 cp->ypos += height + GAPBETWEEN;
350 void staticedit(struct ctlpos *cp, char *stext,
351 int sid, int eid, int percentedit)
353 staticedit_internal(cp, stext, sid, eid, percentedit, 0);
356 void staticpassedit(struct ctlpos *cp, char *stext,
357 int sid, int eid, int percentedit)
359 staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
363 * A big multiline edit control with a static labelling it.
365 void bigeditctrl(struct ctlpos *cp, char *stext,
366 int sid, int eid, int lines)
373 r.bottom = STATICHEIGHT;
374 cp->ypos += r.bottom + GAPWITHIN;
375 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
380 r.bottom = EDITHEIGHT + (lines - 1) * STATICHEIGHT;
381 cp->ypos += r.bottom + GAPBETWEEN;
383 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | ES_MULTILINE,
384 WS_EX_CLIENTEDGE, "", eid);
388 * A tab-control substitute when a real tab control is unavailable.
390 void ersatztab(struct ctlpos *cp, char *stext, int sid, int lid, int s2id)
392 const int height = (COMBOHEIGHT > STATICHEIGHT ?
393 COMBOHEIGHT : STATICHEIGHT);
395 int bigwid, lwid, rwid, rpos;
396 static const int BIGGAP = 15;
397 static const int MEDGAP = 3;
399 bigwid = cp->width + 2 * GAPBETWEEN - 2 * BIGGAP;
401 rpos = BIGGAP + (bigwid + BIGGAP) / 2;
402 lwid = rpos - 2 * BIGGAP;
403 rwid = bigwid + BIGGAP - rpos;
406 r.top = cp->ypos + (height - STATICHEIGHT) / 2;
408 r.bottom = STATICHEIGHT;
409 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
412 r.top = cp->ypos + (height - COMBOHEIGHT) / 2;
414 r.bottom = COMBOHEIGHT * 10;
415 doctl(cp, r, "COMBOBOX",
416 WS_CHILD | WS_VISIBLE | WS_TABSTOP |
417 CBS_DROPDOWNLIST | CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
419 cp->ypos += height + MEDGAP + GAPBETWEEN;
425 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_ETCHEDHORZ,
430 * A static line, followed by an edit control on the left hand side
431 * and a button on the right.
433 void editbutton(struct ctlpos *cp, char *stext, int sid,
434 int eid, char *btext, int bid)
436 const int height = (EDITHEIGHT > PUSHBTNHEIGHT ?
437 EDITHEIGHT : PUSHBTNHEIGHT);
439 int lwid, rwid, rpos;
444 r.bottom = STATICHEIGHT;
445 cp->ypos += r.bottom + GAPWITHIN;
446 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
448 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
449 lwid = rpos - 2 * GAPBETWEEN;
450 rwid = cp->width + GAPBETWEEN - rpos;
453 r.top = cp->ypos + (height - EDITHEIGHT) / 2;
455 r.bottom = EDITHEIGHT;
457 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
458 WS_EX_CLIENTEDGE, "", eid);
461 r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2;
463 r.bottom = PUSHBTNHEIGHT;
464 doctl(cp, r, "BUTTON",
465 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
468 cp->ypos += height + GAPBETWEEN;
472 * Special control which was hard to describe generically: the
473 * session-saver assembly. A static; below that an edit box; below
474 * that a list box. To the right of the list box, a column of
477 void sesssaver(struct ctlpos *cp, char *text,
478 int staticid, int editid, int listid, ...)
482 int lwid, rwid, rpos;
484 const int LISTDEFHEIGHT = 66;
486 rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
487 lwid = rpos - 2 * GAPBETWEEN;
488 rwid = cp->width + GAPBETWEEN - rpos;
490 /* The static control. */
494 r.bottom = STATICHEIGHT;
495 cp->ypos += r.bottom + GAPWITHIN;
496 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
498 /* The edit control. */
502 r.bottom = EDITHEIGHT;
503 cp->ypos += r.bottom + GAPWITHIN;
505 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
506 WS_EX_CLIENTEDGE, "", editid);
509 * The buttons (we should hold off on the list box until we
510 * know how big the buttons are).
512 va_start(ap, listid);
515 char *btext = va_arg(ap, char *);
519 bid = va_arg(ap, int);
523 r.bottom = PUSHBTNHEIGHT;
524 y += r.bottom + GAPWITHIN;
525 doctl(cp, r, "BUTTON",
526 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
530 /* Compute list box height. LISTDEFHEIGHT, or height of buttons. */
533 if (y < LISTDEFHEIGHT)
539 cp->ypos += y + GAPBETWEEN;
540 doctl(cp, r, "LISTBOX",
541 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL |
542 LBS_NOTIFY | LBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
546 * Another special control: the environment-variable setter. A
547 * static line first; then a pair of edit boxes with associated
548 * statics, and two buttons; then a list box.
550 void envsetter(struct ctlpos *cp, char *stext, int sid,
551 char *e1stext, int e1sid, int e1id,
552 char *e2stext, int e2sid, int e2id,
553 int listid, char *b1text, int b1id, char *b2text, int b2id)
556 const int height = (STATICHEIGHT > EDITHEIGHT
558 PUSHBTNHEIGHT ? STATICHEIGHT : EDITHEIGHT >
559 PUSHBTNHEIGHT ? EDITHEIGHT : PUSHBTNHEIGHT);
560 const static int percents[] = { 20, 35, 10, 25 };
561 int i, j, xpos, percent;
562 const int LISTHEIGHT = 42;
564 /* The static control. */
568 r.bottom = STATICHEIGHT;
569 cp->ypos += r.bottom + GAPWITHIN;
570 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
572 /* The statics+edits+buttons. */
573 for (j = 0; j < 2; j++) {
575 for (i = 0; i < 4; i++) {
576 xpos = (cp->width + GAPBETWEEN) * percent / 100;
577 r.left = xpos + GAPBETWEEN;
578 percent += percents[i];
579 xpos = (cp->width + GAPBETWEEN) * percent / 100;
580 r.right = xpos - r.left;
582 r.bottom = (i == 0 ? STATICHEIGHT :
583 i == 1 ? EDITHEIGHT : PUSHBTNHEIGHT);
584 r.top += (height - r.bottom) / 2;
586 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0,
587 j == 0 ? e1stext : e2stext, j == 0 ? e1sid : e2sid);
590 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
591 WS_EX_CLIENTEDGE, "", j == 0 ? e1id : e2id);
593 doctl(cp, r, "BUTTON",
594 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
595 0, j == 0 ? b1text : b2text, j == 0 ? b1id : b2id);
598 cp->ypos += height + GAPWITHIN;
605 r.bottom = LISTHEIGHT;
606 cp->ypos += r.bottom + GAPBETWEEN;
607 doctl(cp, r, "LISTBOX",
608 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS
609 | LBS_USETABSTOPS, WS_EX_CLIENTEDGE, "", listid);
613 * Yet another special control: the character-class setter. A
614 * static, then a list, then a line containing a
615 * button-and-static-and-edit.
617 void charclass(struct ctlpos *cp, char *stext, int sid, int listid,
618 char *btext, int bid, int eid, char *s2text, int s2id)
621 const int height = (STATICHEIGHT > EDITHEIGHT
623 PUSHBTNHEIGHT ? STATICHEIGHT : EDITHEIGHT >
624 PUSHBTNHEIGHT ? EDITHEIGHT : PUSHBTNHEIGHT);
625 const static int percents[] = { 30, 40, 30 };
626 int i, xpos, percent;
627 const int LISTHEIGHT = 66;
629 /* The static control. */
633 r.bottom = STATICHEIGHT;
634 cp->ypos += r.bottom + GAPWITHIN;
635 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
641 r.bottom = LISTHEIGHT;
642 cp->ypos += r.bottom + GAPWITHIN;
643 doctl(cp, r, "LISTBOX",
644 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS
645 | LBS_USETABSTOPS, WS_EX_CLIENTEDGE, "", listid);
647 /* The button+static+edit. */
649 for (i = 0; i < 3; i++) {
650 r.left = xpos + GAPBETWEEN;
651 percent += percents[i];
652 xpos = (cp->width + GAPBETWEEN) * percent / 100;
653 r.right = xpos - r.left;
655 r.bottom = (i == 0 ? PUSHBTNHEIGHT :
656 i == 1 ? STATICHEIGHT : EDITHEIGHT);
657 r.top += (height - r.bottom) / 2;
659 doctl(cp, r, "BUTTON",
660 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
663 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_CENTER,
667 WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL,
668 WS_EX_CLIENTEDGE, "", eid);
671 cp->ypos += height + GAPBETWEEN;
675 * A special control (horrors!). The colour editor. A static line;
676 * then on the left, a list box, and on the right, a sequence of
677 * two-part statics followed by a button.
679 void colouredit(struct ctlpos *cp, char *stext, int sid, int listid,
680 char *btext, int bid, ...)
685 int lwid, rwid, rpos;
686 const int LISTHEIGHT = 66;
688 /* The static control. */
692 r.bottom = STATICHEIGHT;
693 cp->ypos += r.bottom + GAPWITHIN;
694 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
696 rpos = GAPBETWEEN + 2 * (cp->width + GAPBETWEEN) / 3;
697 lwid = rpos - 2 * GAPBETWEEN;
698 rwid = cp->width + GAPBETWEEN - rpos;
704 r.bottom = LISTHEIGHT;
705 doctl(cp, r, "LISTBOX",
706 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_HASSTRINGS
707 | LBS_USETABSTOPS | LBS_NOTIFY, WS_EX_CLIENTEDGE, "", listid);
715 ltext = va_arg(ap, char *);
718 lid = va_arg(ap, int);
719 rid = va_arg(ap, int);
721 r.bottom = STATICHEIGHT;
722 y += r.bottom + GAPWITHIN;
725 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, ltext, lid);
726 r.left = rpos + r.right;
727 r.right = rwid - r.right;
728 doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_RIGHT, 0, "",
734 r.top = y + 2 * GAPWITHIN;
735 r.bottom = PUSHBTNHEIGHT;
738 doctl(cp, r, "BUTTON",
739 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON,
742 cp->ypos += LISTHEIGHT + GAPBETWEEN;
746 * A progress bar (from Common Controls). We like our progress bars
747 * to be smooth and unbroken, without those ugly divisions; some
748 * older compilers may not support that, but that's life.
750 void progressbar(struct ctlpos *cp, int id)
757 r.bottom = PROGBARHEIGHT;
758 cp->ypos += r.bottom + GAPBETWEEN;
760 doctl(cp, r, PROGRESS_CLASS, WS_CHILD | WS_VISIBLE
764 , WS_EX_CLIENTEDGE, "", id);