2 * winctrls.c: routines to self-manage the controls in a dialog
7 * Possible TODO in new cross-platform config box stuff:
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'
33 #define STATICHEIGHT 8
34 #define TITLEHEIGHT 12
35 #define CHECKBOXHEIGHT 8
39 #define LISTINCREMENT 8
40 #define COMBOHEIGHT 12
41 #define PUSHBTNHEIGHT 14
42 #define PROGBARHEIGHT 14
44 void ctlposinit(struct ctlpos
*cp
, HWND hwnd
,
45 int leftborder
, int rightborder
, int topborder
)
49 cp
->font
= SendMessage(hwnd
, WM_GETFONT
, 0, 0);
51 GetClientRect(hwnd
, &r
);
55 MapDialogRect(hwnd
, &r2
);
56 cp
->dlu4inpix
= r2
.right
;
57 cp
->width
= (r
.right
* 4) / (r2
.right
) - 2 * GAPBETWEEN
;
58 cp
->xoff
= leftborder
;
59 cp
->width
-= leftborder
+ rightborder
;
62 HWND
doctl(struct ctlpos
*cp
, RECT r
,
63 char *wclass
, int wstyle
, int exstyle
, char *wtext
, int wid
)
67 * Note nonstandard use of RECT. This is deliberate: by
68 * transforming the width and height directly we arrange to
69 * have all supposedly same-sized controls really same-sized.
73 MapDialogRect(cp
->hwnd
, &r
);
76 * We can pass in cp->hwnd == NULL, to indicate a dry run
77 * without creating any actual controls.
80 ctl
= CreateWindowEx(exstyle
, wclass
, wtext
, wstyle
,
81 r
.left
, r
.top
, r
.right
, r
.bottom
,
82 cp
->hwnd
, (HMENU
) wid
, hinst
, NULL
);
83 SendMessage(ctl
, WM_SETFONT
, cp
->font
, MAKELPARAM(TRUE
, 0));
85 if (!strcmp(wclass
, "LISTBOX")) {
87 * Bizarre Windows bug: the list box calculates its
88 * number of lines based on the font it has at creation
89 * time, but sending it WM_SETFONT doesn't cause it to
90 * recalculate. So now, _after_ we've sent it
91 * WM_SETFONT, we explicitly resize it (to the same
92 * size it was already!) to force it to reconsider.
94 SetWindowPos(ctl
, NULL
, 0, 0, r
.right
, r
.bottom
,
95 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
96 SWP_NOMOVE
| SWP_NOZORDER
);
104 * A title bar across the top of a sub-dialog.
106 void bartitle(struct ctlpos
*cp
, char *name
, int id
)
113 r
.bottom
= STATICHEIGHT
;
114 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
115 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, name
, id
);
119 * Begin a grouping box, with or without a group title.
121 void beginbox(struct ctlpos
*cp
, char *name
, int idbox
)
123 cp
->boxystart
= cp
->ypos
;
125 cp
->boxystart
-= STATICHEIGHT
/ 2;
127 cp
->ypos
+= STATICHEIGHT
;
129 cp
->width
-= 2 * GAPXBOX
;
136 * End a grouping box.
138 void endbox(struct ctlpos
*cp
)
142 cp
->width
+= 2 * GAPXBOX
;
143 cp
->ypos
+= GAPYBOX
- GAPBETWEEN
;
146 r
.top
= cp
->boxystart
;
147 r
.bottom
= cp
->ypos
- cp
->boxystart
;
148 doctl(cp
, r
, "BUTTON", BS_GROUPBOX
| WS_CHILD
| WS_VISIBLE
, 0,
149 cp
->boxtext ? cp
->boxtext
: "", cp
->boxid
);
154 * Some edit boxes. Each one has a static above it. The percentages
155 * of the horizontal space are provided.
157 void multiedit(struct ctlpos
*cp
, int password
, ...)
164 va_start(ap
, password
);
167 int staticid
, editid
, pcwidth
;
168 text
= va_arg(ap
, char *);
171 staticid
= va_arg(ap
, int);
172 editid
= va_arg(ap
, int);
173 pcwidth
= va_arg(ap
, int);
175 r
.left
= xpos
+ GAPBETWEEN
;
177 xpos
= (cp
->width
+ GAPBETWEEN
) * percent
/ 100;
178 r
.right
= xpos
- r
.left
;
181 r
.bottom
= STATICHEIGHT
;
182 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, text
, staticid
);
183 r
.top
= cp
->ypos
+ 8 + GAPWITHIN
;
184 r
.bottom
= EDITHEIGHT
;
186 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| ES_AUTOHSCROLL
|
187 (password ? ES_PASSWORD
: 0),
188 WS_EX_CLIENTEDGE
, "", editid
);
191 cp
->ypos
+= STATICHEIGHT
+ GAPWITHIN
+ EDITHEIGHT
+ GAPBETWEEN
;
195 * A static line, followed by a full-width combo box.
197 void combobox(struct ctlpos
*cp
, char *text
, int staticid
, int listid
)
205 r
.bottom
= STATICHEIGHT
;
206 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, text
, staticid
);
207 r
.top
= cp
->ypos
+ 8 + GAPWITHIN
;
208 r
.bottom
= COMBOHEIGHT
* 10;
209 doctl(cp
, r
, "COMBOBOX",
210 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
|
211 CBS_DROPDOWN
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", listid
);
213 cp
->ypos
+= STATICHEIGHT
+ GAPWITHIN
+ COMBOHEIGHT
+ GAPBETWEEN
;
216 struct radio
{ char *text
; int id
; };
218 static void radioline_common(struct ctlpos
*cp
, char *text
, int id
,
219 int nacross
, struct radio
*buttons
, int nbuttons
)
230 r
.bottom
= STATICHEIGHT
;
231 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
232 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, text
, id
);
237 for (j
= 0; j
< nbuttons
; j
++) {
238 char *btext
= buttons
[j
].text
;
239 int bid
= buttons
[j
].id
;
242 cp
->ypos
+= r
.bottom
+ (nacross
> 1 ? GAPBETWEEN
: GAPWITHIN
);
245 r
.left
= GAPBETWEEN
+ i
* (cp
->width
+ GAPBETWEEN
) / nacross
;
248 (i
+ 1) * (cp
->width
+ GAPBETWEEN
) / nacross
- r
.left
;
250 r
.right
= cp
->width
- r
.left
;
252 r
.bottom
= RADIOHEIGHT
;
253 doctl(cp
, r
, "BUTTON",
254 BS_NOTIFY
| BS_AUTORADIOBUTTON
| WS_CHILD
|
255 WS_VISIBLE
| WS_TABSTOP
| group
, 0, btext
, bid
);
259 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
263 * A set of radio buttons on the same line, with a static above
264 * them. `nacross' dictates how many parts the line is divided into
265 * (you might want this not to equal the number of buttons if you
266 * needed to line up some 2s and some 3s to look good in the same
269 * There's a bit of a hack in here to ensure that if nacross
270 * exceeds the actual number of buttons, the rightmost button
271 * really does get all the space right to the edge of the line, so
272 * you can do things like
274 * (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
276 void radioline(struct ctlpos
*cp
, char *text
, int id
, int nacross
, ...)
279 struct radio
*buttons
;
282 va_start(ap
, nacross
);
285 char *btext
= va_arg(ap
, char *);
289 bid
= va_arg(ap
, int);
292 buttons
= smalloc(nbuttons
* sizeof(struct radio
));
293 va_start(ap
, nacross
);
294 for (i
= 0; i
< nbuttons
; i
++) {
295 buttons
[i
].text
= va_arg(ap
, char *);
296 buttons
[i
].id
= va_arg(ap
, int);
299 radioline_common(cp
, text
, id
, nacross
, buttons
, nbuttons
);
304 * A set of radio buttons on the same line, without a static above
305 * them. Otherwise just like radioline.
307 void bareradioline(struct ctlpos
*cp
, int nacross
, ...)
310 struct radio
*buttons
;
313 va_start(ap
, nacross
);
316 char *btext
= va_arg(ap
, char *);
320 bid
= va_arg(ap
, int);
323 buttons
= smalloc(nbuttons
* sizeof(struct radio
));
324 va_start(ap
, nacross
);
325 for (i
= 0; i
< nbuttons
; i
++) {
326 buttons
[i
].text
= va_arg(ap
, char *);
327 buttons
[i
].id
= va_arg(ap
, int);
330 radioline_common(cp
, NULL
, 0, nacross
, buttons
, nbuttons
);
335 * A set of radio buttons on multiple lines, with a static above
338 void radiobig(struct ctlpos
*cp
, char *text
, int id
, ...)
341 struct radio
*buttons
;
347 char *btext
= va_arg(ap
, char *);
351 bid
= va_arg(ap
, int);
354 buttons
= smalloc(nbuttons
* sizeof(struct radio
));
356 for (i
= 0; i
< nbuttons
; i
++) {
357 buttons
[i
].text
= va_arg(ap
, char *);
358 buttons
[i
].id
= va_arg(ap
, int);
361 radioline_common(cp
, text
, id
, 1, buttons
, nbuttons
);
366 * A single standalone checkbox.
368 void checkbox(struct ctlpos
*cp
, char *text
, int id
)
375 r
.bottom
= CHECKBOXHEIGHT
;
376 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
377 doctl(cp
, r
, "BUTTON",
378 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
, 0,
383 * Wrap a piece of text for a static text control. Returns the
384 * wrapped text (a malloc'ed string containing \ns), and also
385 * returns the number of lines required.
387 char *staticwrap(struct ctlpos
*cp
, HWND hwnd
, char *text
, int *lines
)
389 HDC hdc
= GetDC(hwnd
);
390 int lpx
= GetDeviceCaps(hdc
, LOGPIXELSX
);
391 int width
, nlines
, j
;
397 ret
= smalloc(1+strlen(text
));
400 pwidths
= smalloc(sizeof(INT
)*(1+strlen(text
)));
403 * Work out the width the text will need to fit in, by doing
404 * the same adjustment that the `statictext' function itself
407 * We must first convert from dialog-box units into pixels, and
408 * then from pixels into the `logical units' that Windows uses
409 * within GDI. You can't make this stuff up.
411 r
.left
= r
.top
= r
.bottom
= 0;
413 MapDialogRect(hwnd
, &r
);
414 width
= MulDiv(r
.right
, lpx
, 72);
419 if (!GetTextExtentExPoint(hdc
, p
, strlen(p
), width
,
420 &nfit
, pwidths
, &size
) ||
421 (size_t)nfit
>= strlen(p
)) {
423 * Either GetTextExtentExPoint returned failure, or the
424 * whole of the rest of the text fits on this line.
425 * Either way, we stop wrapping, copy the remainder of
426 * the input string unchanged to the output, and leave.
433 * Now we search backwards along the string from `nfit',
434 * looking for a space at which to break the line. If we
435 * don't find one at all, that's fine - we'll just break
436 * the line at `nfit'.
438 for (j
= nfit
; j
> 0; j
--) {
439 if (isspace((unsigned char)p
[j
])) {
450 while (*p
&& isspace((unsigned char)*p
))
456 ReleaseDC(cp
->hwnd
, hdc
);
458 if (lines
) *lines
= nlines
;
464 * A single standalone static text control.
466 void statictext(struct ctlpos
*cp
, char *text
, int lines
, int id
)
473 r
.bottom
= STATICHEIGHT
* lines
;
474 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
475 doctl(cp
, r
, "STATIC",
476 WS_CHILD
| WS_VISIBLE
| SS_LEFTNOWORDWRAP
,
481 * An owner-drawn static text control for a panel title.
483 void paneltitle(struct ctlpos
*cp
, int id
)
490 r
.bottom
= TITLEHEIGHT
;
491 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
492 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
| SS_OWNERDRAW
,
497 * A button on the right hand side, with a static to its left.
499 void staticbtn(struct ctlpos
*cp
, char *stext
, int sid
,
500 char *btext
, int bid
)
502 const int height
= (PUSHBTNHEIGHT
> STATICHEIGHT ?
503 PUSHBTNHEIGHT
: STATICHEIGHT
);
505 int lwid
, rwid
, rpos
;
507 rpos
= GAPBETWEEN
+ 3 * (cp
->width
+ GAPBETWEEN
) / 4;
508 lwid
= rpos
- 2 * GAPBETWEEN
;
509 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
512 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
514 r
.bottom
= STATICHEIGHT
;
515 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
518 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
520 r
.bottom
= PUSHBTNHEIGHT
;
521 doctl(cp
, r
, "BUTTON",
522 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
525 cp
->ypos
+= height
+ GAPBETWEEN
;
529 * A simple push button.
531 void button(struct ctlpos
*cp
, char *btext
, int bid
, int defbtn
)
538 r
.bottom
= PUSHBTNHEIGHT
;
540 /* Q67655: the _dialog box_ must know which button is default
541 * as well as the button itself knowing */
542 if (defbtn
&& cp
->hwnd
)
543 SendMessage(cp
->hwnd
, DM_SETDEFID
, bid
, 0);
545 doctl(cp
, r
, "BUTTON",
546 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
547 (defbtn ? BS_DEFPUSHBUTTON
: 0) | BS_PUSHBUTTON
,
550 cp
->ypos
+= PUSHBTNHEIGHT
+ GAPBETWEEN
;
554 * Like staticbtn, but two buttons.
556 void static2btn(struct ctlpos
*cp
, char *stext
, int sid
,
557 char *btext1
, int bid1
, char *btext2
, int bid2
)
559 const int height
= (PUSHBTNHEIGHT
> STATICHEIGHT ?
560 PUSHBTNHEIGHT
: STATICHEIGHT
);
562 int lwid
, rwid1
, rwid2
, rpos1
, rpos2
;
564 rpos1
= GAPBETWEEN
+ (cp
->width
+ GAPBETWEEN
) / 2;
565 rpos2
= GAPBETWEEN
+ 3 * (cp
->width
+ GAPBETWEEN
) / 4;
566 lwid
= rpos1
- 2 * GAPBETWEEN
;
567 rwid1
= rpos2
- rpos1
- GAPBETWEEN
;
568 rwid2
= cp
->width
+ GAPBETWEEN
- rpos2
;
571 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
573 r
.bottom
= STATICHEIGHT
;
574 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
577 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
579 r
.bottom
= PUSHBTNHEIGHT
;
580 doctl(cp
, r
, "BUTTON",
581 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
585 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
587 r
.bottom
= PUSHBTNHEIGHT
;
588 doctl(cp
, r
, "BUTTON",
589 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
592 cp
->ypos
+= height
+ GAPBETWEEN
;
596 * An edit control on the right hand side, with a static to its left.
598 static void staticedit_internal(struct ctlpos
*cp
, char *stext
,
599 int sid
, int eid
, int percentedit
,
602 const int height
= (EDITHEIGHT
> STATICHEIGHT ?
603 EDITHEIGHT
: STATICHEIGHT
);
605 int lwid
, rwid
, rpos
;
608 GAPBETWEEN
+ (100 - percentedit
) * (cp
->width
+ GAPBETWEEN
) / 100;
609 lwid
= rpos
- 2 * GAPBETWEEN
;
610 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
613 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
615 r
.bottom
= STATICHEIGHT
;
616 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
619 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
621 r
.bottom
= EDITHEIGHT
;
623 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| ES_AUTOHSCROLL
| style
,
624 WS_EX_CLIENTEDGE
, "", eid
);
626 cp
->ypos
+= height
+ GAPBETWEEN
;
629 void staticedit(struct ctlpos
*cp
, char *stext
,
630 int sid
, int eid
, int percentedit
)
632 staticedit_internal(cp
, stext
, sid
, eid
, percentedit
, 0);
635 void staticpassedit(struct ctlpos
*cp
, char *stext
,
636 int sid
, int eid
, int percentedit
)
638 staticedit_internal(cp
, stext
, sid
, eid
, percentedit
, ES_PASSWORD
);
642 * A drop-down list box on the right hand side, with a static to
645 void staticddl(struct ctlpos
*cp
, char *stext
,
646 int sid
, int lid
, int percentlist
)
648 const int height
= (COMBOHEIGHT
> STATICHEIGHT ?
649 COMBOHEIGHT
: STATICHEIGHT
);
651 int lwid
, rwid
, rpos
;
654 GAPBETWEEN
+ (100 - percentlist
) * (cp
->width
+ GAPBETWEEN
) / 100;
655 lwid
= rpos
- 2 * GAPBETWEEN
;
656 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
659 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
661 r
.bottom
= STATICHEIGHT
;
662 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
665 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
667 r
.bottom
= COMBOHEIGHT
*4;
668 doctl(cp
, r
, "COMBOBOX",
669 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
670 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
672 cp
->ypos
+= height
+ GAPBETWEEN
;
676 * A combo box on the right hand side, with a static to its left.
678 void staticcombo(struct ctlpos
*cp
, char *stext
,
679 int sid
, int lid
, int percentlist
)
681 const int height
= (COMBOHEIGHT
> STATICHEIGHT ?
682 COMBOHEIGHT
: STATICHEIGHT
);
684 int lwid
, rwid
, rpos
;
687 GAPBETWEEN
+ (100 - percentlist
) * (cp
->width
+ GAPBETWEEN
) / 100;
688 lwid
= rpos
- 2 * GAPBETWEEN
;
689 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
692 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
694 r
.bottom
= STATICHEIGHT
;
695 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
698 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
700 r
.bottom
= COMBOHEIGHT
*10;
701 doctl(cp
, r
, "COMBOBOX",
702 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
|
703 CBS_DROPDOWN
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
705 cp
->ypos
+= height
+ GAPBETWEEN
;
709 * A static, with a full-width drop-down list box below it.
711 void staticddlbig(struct ctlpos
*cp
, char *stext
,
719 r
.bottom
= STATICHEIGHT
;
720 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
721 cp
->ypos
+= STATICHEIGHT
;
726 r
.bottom
= COMBOHEIGHT
*4;
727 doctl(cp
, r
, "COMBOBOX",
728 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
729 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
730 cp
->ypos
+= COMBOHEIGHT
+ GAPBETWEEN
;
734 * A big multiline edit control with a static labelling it.
736 void bigeditctrl(struct ctlpos
*cp
, char *stext
,
737 int sid
, int eid
, int lines
)
744 r
.bottom
= STATICHEIGHT
;
745 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
746 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
751 r
.bottom
= EDITHEIGHT
+ (lines
- 1) * STATICHEIGHT
;
752 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
754 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
| ES_MULTILINE
,
755 WS_EX_CLIENTEDGE
, "", eid
);
759 * A list box with a static labelling it.
761 void listbox(struct ctlpos
*cp
, char *stext
,
762 int sid
, int lid
, int lines
, int multi
)
770 r
.bottom
= STATICHEIGHT
;
771 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
772 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
778 r
.bottom
= LISTHEIGHT
+ (lines
- 1) * LISTINCREMENT
;
779 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
780 doctl(cp
, r
, "LISTBOX",
781 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
|
782 LBS_NOTIFY
| LBS_HASSTRINGS
| LBS_USETABSTOPS
|
783 (multi ? LBS_MULTIPLESEL
: 0),
784 WS_EX_CLIENTEDGE
, "", lid
);
788 * A tab-control substitute when a real tab control is unavailable.
790 void ersatztab(struct ctlpos
*cp
, char *stext
, int sid
, int lid
, int s2id
)
792 const int height
= (COMBOHEIGHT
> STATICHEIGHT ?
793 COMBOHEIGHT
: STATICHEIGHT
);
795 int bigwid
, lwid
, rwid
, rpos
;
796 static const int BIGGAP
= 15;
797 static const int MEDGAP
= 3;
799 bigwid
= cp
->width
+ 2 * GAPBETWEEN
- 2 * BIGGAP
;
801 rpos
= BIGGAP
+ (bigwid
+ BIGGAP
) / 2;
802 lwid
= rpos
- 2 * BIGGAP
;
803 rwid
= bigwid
+ BIGGAP
- rpos
;
806 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
808 r
.bottom
= STATICHEIGHT
;
809 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
812 r
.top
= cp
->ypos
+ (height
- COMBOHEIGHT
) / 2;
814 r
.bottom
= COMBOHEIGHT
* 10;
815 doctl(cp
, r
, "COMBOBOX",
816 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
817 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
819 cp
->ypos
+= height
+ MEDGAP
+ GAPBETWEEN
;
825 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
| SS_ETCHEDHORZ
,
830 * A static line, followed by an edit control on the left hand side
831 * and a button on the right.
833 void editbutton(struct ctlpos
*cp
, char *stext
, int sid
,
834 int eid
, char *btext
, int bid
)
836 const int height
= (EDITHEIGHT
> PUSHBTNHEIGHT ?
837 EDITHEIGHT
: PUSHBTNHEIGHT
);
839 int lwid
, rwid
, rpos
;
844 r
.bottom
= STATICHEIGHT
;
845 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
846 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
848 rpos
= GAPBETWEEN
+ 3 * (cp
->width
+ GAPBETWEEN
) / 4;
849 lwid
= rpos
- 2 * GAPBETWEEN
;
850 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
853 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
855 r
.bottom
= EDITHEIGHT
;
857 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| ES_AUTOHSCROLL
,
858 WS_EX_CLIENTEDGE
, "", eid
);
861 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
863 r
.bottom
= PUSHBTNHEIGHT
;
864 doctl(cp
, r
, "BUTTON",
865 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
868 cp
->ypos
+= height
+ GAPBETWEEN
;
872 * A special control for manipulating an ordered preference list
873 * (eg. for cipher selection).
874 * XXX: this is a rough hack and could be improved.
876 void prefslist(struct prefslist
*hdl
, struct ctlpos
*cp
, int lines
,
877 char *stext
, int sid
, int listid
, int upbid
, int dnbid
)
879 const static int percents
[] = { 5, 75, 20 };
881 int xpos
, percent
= 0, i
;
882 int listheight
= LISTHEIGHT
+ (lines
- 1) * LISTINCREMENT
;
883 const int BTNSHEIGHT
= 2*PUSHBTNHEIGHT
+ GAPBETWEEN
;
884 int totalheight
, buttonpos
;
886 /* Squirrel away IDs. */
887 hdl
->listid
= listid
;
891 /* The static label. */
896 r
.bottom
= STATICHEIGHT
;
897 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
898 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
901 if (listheight
> BTNSHEIGHT
) {
902 totalheight
= listheight
;
903 buttonpos
= (listheight
- BTNSHEIGHT
) / 2;
905 totalheight
= BTNSHEIGHT
;
909 for (i
=0; i
<3; i
++) {
911 xpos
= (cp
->width
+ GAPBETWEEN
) * percent
/ 100;
912 left
= xpos
+ GAPBETWEEN
;
913 percent
+= percents
[i
];
914 xpos
= (cp
->width
+ GAPBETWEEN
) * percent
/ 100;
919 /* The drag list box. */
920 r
.left
= left
; r
.right
= wid
;
921 r
.top
= cp
->ypos
; r
.bottom
= listheight
;
924 ctl
= doctl(cp
, r
, "LISTBOX",
925 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
926 WS_VSCROLL
| LBS_HASSTRINGS
| LBS_USETABSTOPS
,
934 /* The "Up" and "Down" buttons. */
935 /* XXX worry about accelerators if we have more than one
936 * prefslist on a panel */
937 r
.left
= left
; r
.right
= wid
;
938 r
.top
= cp
->ypos
+ buttonpos
; r
.bottom
= PUSHBTNHEIGHT
;
939 doctl(cp
, r
, "BUTTON",
940 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
|
941 WS_TABSTOP
| BS_PUSHBUTTON
,
944 r
.left
= left
; r
.right
= wid
;
945 r
.top
= cp
->ypos
+ buttonpos
+ PUSHBTNHEIGHT
+ GAPBETWEEN
;
946 r
.bottom
= PUSHBTNHEIGHT
;
947 doctl(cp
, r
, "BUTTON",
948 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
|
949 WS_TABSTOP
| BS_PUSHBUTTON
,
957 cp
->ypos
+= totalheight
+ GAPBETWEEN
;
962 * Helper function for prefslist: move item in list box.
964 static void pl_moveitem(HWND hwnd
, int listid
, int src
, int dst
)
968 /* Get the item's data. */
969 tlen
= SendDlgItemMessage (hwnd
, listid
, LB_GETTEXTLEN
, src
, 0);
970 txt
= smalloc(tlen
+1);
971 SendDlgItemMessage (hwnd
, listid
, LB_GETTEXT
, src
, (LPARAM
) txt
);
972 val
= SendDlgItemMessage (hwnd
, listid
, LB_GETITEMDATA
, src
, 0);
973 /* Deselect old location. */
974 SendDlgItemMessage (hwnd
, listid
, LB_SETSEL
, FALSE
, src
);
975 /* Delete it at the old location. */
976 SendDlgItemMessage (hwnd
, listid
, LB_DELETESTRING
, src
, 0);
977 /* Insert it at new location. */
978 SendDlgItemMessage (hwnd
, listid
, LB_INSERTSTRING
, dst
,
980 SendDlgItemMessage (hwnd
, listid
, LB_SETITEMDATA
, dst
,
983 SendDlgItemMessage (hwnd
, listid
, LB_SETCURSEL
, dst
, 0);
987 int pl_itemfrompt(HWND hwnd
, POINT cursor
, BOOL scroll
)
990 POINT uppoint
, downpoint
;
991 int updist
, downdist
, upitem
, downitem
, i
;
994 * Ghastly hackery to try to figure out not which
995 * _item_, but which _gap between items_, the user
996 * is pointing at. We do this by first working out
997 * which list item is under the cursor, and then
998 * working out how far the cursor would have to
999 * move up or down before the answer was different.
1000 * Then we put the insertion point _above_ the
1001 * current item if the upper edge is closer than
1002 * the lower edge, or _below_ it if vice versa.
1004 ret
= LBItemFromPt(hwnd
, cursor
, scroll
);
1007 ret
= LBItemFromPt(hwnd
, cursor
, FALSE
);
1008 updist
= downdist
= 0;
1009 for (i
= 1; i
< 4096 && (!updist
|| !downdist
); i
++) {
1010 uppoint
= downpoint
= cursor
;
1013 upitem
= LBItemFromPt(hwnd
, uppoint
, FALSE
);
1014 downitem
= LBItemFromPt(hwnd
, downpoint
, FALSE
);
1015 if (!updist
&& upitem
!= ret
)
1017 if (!downdist
&& downitem
!= ret
)
1020 if (downdist
< updist
)
1026 * Handler for prefslist above.
1028 * Return value has bit 0 set if the dialog box procedure needs to
1029 * return TRUE from handling this message; it has bit 1 set if a
1030 * change may have been made in the contents of the list.
1032 int handle_prefslist(struct prefslist
*hdl
,
1033 int *array
, int maxmemb
,
1034 int is_dlmsg
, HWND hwnd
,
1035 WPARAM wParam
, LPARAM lParam
)
1042 if ((int)wParam
== hdl
->listid
) {
1043 DRAGLISTINFO
*dlm
= (DRAGLISTINFO
*)lParam
;
1044 int dest
= 0; /* initialise to placate gcc */
1045 switch (dlm
->uNotification
) {
1048 SendDlgItemMessage(hwnd
, hdl
->listid
,
1049 LB_ADDSTRING
, 0, (LPARAM
) "");
1051 hdl
->srcitem
= LBItemFromPt(dlm
->hWnd
, dlm
->ptCursor
, TRUE
);
1053 /* XXX hack Q183115 */
1054 SetWindowLong(hwnd
, DWL_MSGRESULT
, TRUE
);
1057 DrawInsert(hwnd
, dlm
->hWnd
, -1); /* Clear arrow */
1058 SendDlgItemMessage(hwnd
, hdl
->listid
,
1059 LB_DELETESTRING
, hdl
->dummyitem
, 0);
1064 dest
= pl_itemfrompt(dlm
->hWnd
, dlm
->ptCursor
, TRUE
);
1065 if (dest
> hdl
->dummyitem
) dest
= hdl
->dummyitem
;
1066 DrawInsert (hwnd
, dlm
->hWnd
, dest
);
1068 SetWindowLong(hwnd
, DWL_MSGRESULT
, DL_MOVECURSOR
);
1070 SetWindowLong(hwnd
, DWL_MSGRESULT
, DL_STOPCURSOR
);
1073 if (hdl
->dragging
) {
1074 dest
= pl_itemfrompt(dlm
->hWnd
, dlm
->ptCursor
, TRUE
);
1075 if (dest
> hdl
->dummyitem
) dest
= hdl
->dummyitem
;
1076 DrawInsert (hwnd
, dlm
->hWnd
, -1);
1078 SendDlgItemMessage(hwnd
, hdl
->listid
,
1079 LB_DELETESTRING
, hdl
->dummyitem
, 0);
1080 if (hdl
->dragging
) {
1083 /* Correct for "missing" item. */
1084 if (dest
> hdl
->srcitem
) dest
--;
1085 pl_moveitem(hwnd
, hdl
->listid
, hdl
->srcitem
, dest
);
1095 if (((LOWORD(wParam
) == hdl
->upbid
) ||
1096 (LOWORD(wParam
) == hdl
->dnbid
)) &&
1097 ((HIWORD(wParam
) == BN_CLICKED
) ||
1098 (HIWORD(wParam
) == BN_DOUBLECLICKED
))) {
1099 /* Move an item up or down the list. */
1100 /* Get the current selection, if any. */
1101 int selection
= SendDlgItemMessage (hwnd
, hdl
->listid
, LB_GETCURSEL
, 0, 0);
1102 if (selection
== LB_ERR
) {
1106 /* Get the total number of items. */
1107 nitems
= SendDlgItemMessage (hwnd
, hdl
->listid
, LB_GETCOUNT
, 0, 0);
1108 /* Should we do anything? */
1109 if (LOWORD(wParam
) == hdl
->upbid
&& (selection
> 0))
1110 pl_moveitem(hwnd
, hdl
->listid
, selection
, selection
- 1);
1111 else if (LOWORD(wParam
) == hdl
->dnbid
&& (selection
< nitems
- 1))
1112 pl_moveitem(hwnd
, hdl
->listid
, selection
, selection
+ 1);
1121 /* Update array to match the list box. */
1122 for (i
=0; i
< maxmemb
; i
++)
1123 array
[i
] = SendDlgItemMessage (hwnd
, hdl
->listid
, LB_GETITEMDATA
,
1131 * A progress bar (from Common Controls). We like our progress bars
1132 * to be smooth and unbroken, without those ugly divisions; some
1133 * older compilers may not support that, but that's life.
1135 void progressbar(struct ctlpos
*cp
, int id
)
1139 r
.left
= GAPBETWEEN
;
1141 r
.right
= cp
->width
;
1142 r
.bottom
= PROGBARHEIGHT
;
1143 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
1145 doctl(cp
, r
, PROGRESS_CLASS
, WS_CHILD
| WS_VISIBLE
1149 , WS_EX_CLIENTEDGE
, "", id
);
1152 /* ----------------------------------------------------------------------
1153 * Platform-specific side of portable dialog-box mechanism.
1157 * This function takes a string, escapes all the ampersands, and
1158 * places a single (unescaped) ampersand in front of the first
1159 * occurrence of the given shortcut character (which may be
1162 * Return value is a malloc'ed copy of the processed version of the
1165 static char *shortcut_escape(char *text
, char shortcut
)
1171 return NULL
; /* sfree won't choke on this */
1173 ret
= smalloc(2*strlen(text
)+1); /* size potentially doubles! */
1174 shortcut
= tolower((unsigned char)shortcut
);
1179 if (shortcut
!= NO_SHORTCUT
&&
1180 tolower((unsigned char)*p
) == shortcut
) {
1182 shortcut
= NO_SHORTCUT
; /* stop it happening twice */
1183 } else if (*p
== '&') {
1192 void winctrl_add_shortcuts(struct dlgparam
*dp
, struct winctrl
*c
)
1195 for (i
= 0; i
< lenof(c
->shortcuts
); i
++)
1196 if (c
->shortcuts
[i
] != NO_SHORTCUT
) {
1197 unsigned char s
= tolower((unsigned char)c
->shortcuts
[i
]);
1198 assert(!dp
->shortcuts
[s
]);
1199 dp
->shortcuts
[s
] = TRUE
;
1203 void winctrl_rem_shortcuts(struct dlgparam
*dp
, struct winctrl
*c
)
1206 for (i
= 0; i
< lenof(c
->shortcuts
); i
++)
1207 if (c
->shortcuts
[i
] != NO_SHORTCUT
) {
1208 unsigned char s
= tolower((unsigned char)c
->shortcuts
[i
]);
1209 assert(dp
->shortcuts
[s
]);
1210 dp
->shortcuts
[s
] = FALSE
;
1214 static int winctrl_cmp_byctrl(void *av
, void *bv
)
1216 struct winctrl
*a
= (struct winctrl
*)av
;
1217 struct winctrl
*b
= (struct winctrl
*)bv
;
1218 if (a
->ctrl
< b
->ctrl
)
1220 else if (a
->ctrl
> b
->ctrl
)
1225 static int winctrl_cmp_byid(void *av
, void *bv
)
1227 struct winctrl
*a
= (struct winctrl
*)av
;
1228 struct winctrl
*b
= (struct winctrl
*)bv
;
1229 if (a
->base_id
< b
->base_id
)
1231 else if (a
->base_id
> b
->base_id
)
1236 static int winctrl_cmp_byctrl_find(void *av
, void *bv
)
1238 union control
*a
= (union control
*)av
;
1239 struct winctrl
*b
= (struct winctrl
*)bv
;
1242 else if (a
> b
->ctrl
)
1247 static int winctrl_cmp_byid_find(void *av
, void *bv
)
1250 struct winctrl
*b
= (struct winctrl
*)bv
;
1251 if (*a
< b
->base_id
)
1253 else if (*a
>= b
->base_id
+ b
->num_ids
)
1259 void winctrl_init(struct winctrls
*wc
)
1261 wc
->byctrl
= newtree234(winctrl_cmp_byctrl
);
1262 wc
->byid
= newtree234(winctrl_cmp_byid
);
1264 void winctrl_cleanup(struct winctrls
*wc
)
1268 while ((c
= index234(wc
->byid
, 0)) != NULL
) {
1269 winctrl_remove(wc
, c
);
1274 freetree234(wc
->byctrl
);
1275 freetree234(wc
->byid
);
1276 wc
->byctrl
= wc
->byid
= NULL
;
1279 void winctrl_add(struct winctrls
*wc
, struct winctrl
*c
)
1281 struct winctrl
*ret
;
1283 ret
= add234(wc
->byctrl
, c
);
1286 ret
= add234(wc
->byid
, c
);
1290 void winctrl_remove(struct winctrls
*wc
, struct winctrl
*c
)
1292 struct winctrl
*ret
;
1293 ret
= del234(wc
->byctrl
, c
);
1294 ret
= del234(wc
->byid
, c
);
1298 struct winctrl
*winctrl_findbyctrl(struct winctrls
*wc
, union control
*ctrl
)
1300 return find234(wc
->byctrl
, ctrl
, winctrl_cmp_byctrl_find
);
1303 struct winctrl
*winctrl_findbyid(struct winctrls
*wc
, int id
)
1305 return find234(wc
->byid
, &id
, winctrl_cmp_byid_find
);
1308 struct winctrl
*winctrl_findbyindex(struct winctrls
*wc
, int index
)
1310 return index234(wc
->byid
, index
);
1313 void winctrl_layout(struct dlgparam
*dp
, struct winctrls
*wc
,
1314 struct ctlpos
*cp
, struct controlset
*s
, int *id
)
1316 struct ctlpos columns
[16];
1317 int ncols
, colstart
, colspan
;
1319 struct ctlpos tabdelays
[16];
1320 union control
*tabdelayed
[16];
1325 char shortcuts
[MAX_SHORTCUTS_PER_CTRL
];
1328 int i
, base_id
, num_ids
;
1333 /* Start a containing box, if we have a boxname. */
1334 if (s
->boxname
&& *s
->boxname
) {
1335 struct winctrl
*c
= smalloc(sizeof(struct winctrl
));
1337 c
->base_id
= base_id
;
1340 memset(c
->shortcuts
, NO_SHORTCUT
, lenof(c
->shortcuts
));
1342 beginbox(cp
, s
->boxtitle
, base_id
);
1346 /* Draw a title, if we have one. */
1347 if (!s
->boxname
&& s
->boxtitle
) {
1348 struct winctrl
*c
= smalloc(sizeof(struct winctrl
));
1350 c
->base_id
= base_id
;
1352 c
->data
= dupstr(s
->boxtitle
);
1353 memset(c
->shortcuts
, NO_SHORTCUT
, lenof(c
->shortcuts
));
1355 paneltitle(cp
, base_id
);
1359 /* Initially we have just one column. */
1361 columns
[0] = *cp
; /* structure copy */
1363 /* And initially, there are no pending tab-delayed controls. */
1366 /* Loop over each control in the controlset. */
1367 for (i
= 0; i
< s
->ncontrols
; i
++) {
1368 union control
*ctrl
= s
->ctrls
[i
];
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
1381 if (ctrl
->generic
.type
== CTRL_COLUMNS
) {
1382 assert((ctrl
->columns
.ncols
== 1) ^ (ncols
== 1));
1386 * We're splitting into multiple columns.
1388 int lpercent
, rpercent
, lx
, rx
, i
;
1390 ncols
= ctrl
->columns
.ncols
;
1391 assert(ncols
<= lenof(columns
));
1392 for (i
= 1; i
< ncols
; i
++)
1393 columns
[i
] = columns
[0]; /* structure copy */
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
;
1408 * We're recombining the various columns into one.
1410 int maxy
= columns
[0].ypos
;
1412 for (i
= 1; i
< ncols
; i
++)
1413 if (maxy
< columns
[i
].ypos
)
1414 maxy
= columns
[i
].ypos
;
1416 columns
[0] = *cp
; /* structure copy */
1417 columns
[0].ypos
= maxy
;
1421 } else if (ctrl
->generic
.type
== CTRL_TABDELAY
) {
1424 assert(!ctrl
->generic
.tabdelay
);
1425 ctrl
= ctrl
->tabdelay
.ctrl
;
1427 for (i
= 0; i
< ntabdelays
; i
++)
1428 if (tabdelayed
[i
] == ctrl
)
1430 assert(i
< ntabdelays
); /* we have to have found it */
1432 pos
= tabdelays
[i
]; /* structure copy */
1434 colstart
= colspan
= -1; /* indicate this was tab-delayed */
1438 * If it wasn't one of those, it's a genuine control;
1439 * so we'll have to compute a position for it now, by
1440 * checking its column span.
1444 colstart
= COLUMN_START(ctrl
->generic
.column
);
1445 colspan
= COLUMN_SPAN(ctrl
->generic
.column
);
1447 pos
= columns
[colstart
]; /* structure copy */
1448 pos
.width
= columns
[colstart
+colspan
-1].width
+
1449 (columns
[colstart
+colspan
-1].xoff
- columns
[colstart
].xoff
);
1451 for (col
= colstart
; col
< colstart
+colspan
; col
++)
1452 if (pos
.ypos
< columns
[col
].ypos
)
1453 pos
.ypos
= columns
[col
].ypos
;
1456 * If this control is to be tabdelayed, add it to the
1457 * tabdelay list, and unset pos.hwnd to inhibit actual
1460 if (ctrl
->generic
.tabdelay
) {
1461 assert(ntabdelays
< lenof(tabdelays
));
1462 tabdelays
[ntabdelays
] = pos
; /* structure copy */
1463 tabdelayed
[ntabdelays
] = ctrl
;
1469 /* Most controls don't need anything in c->data. */
1472 /* And they all start off with no shortcuts registered. */
1473 memset(shortcuts
, NO_SHORTCUT
, lenof(shortcuts
));
1477 * Now we're ready to actually create the control, by
1478 * switching on its type.
1480 switch (ctrl
->generic
.type
) {
1483 char *wrapped
, *escaped
;
1486 wrapped
= staticwrap(&pos
, cp
->hwnd
,
1487 ctrl
->generic
.label
, &lines
);
1488 escaped
= shortcut_escape(wrapped
, NO_SHORTCUT
);
1489 statictext(&pos
, escaped
, lines
, base_id
);
1495 num_ids
= 2; /* static, edit */
1496 escaped
= shortcut_escape(ctrl
->editbox
.label
,
1497 ctrl
->editbox
.shortcut
);
1498 shortcuts
[nshortcuts
++] = ctrl
->editbox
.shortcut
;
1499 if (ctrl
->editbox
.percentwidth
== 100) {
1500 if (ctrl
->editbox
.has_list
)
1501 combobox(&pos
, escaped
,
1502 base_id
, base_id
+1);
1504 multiedit(&pos
, ctrl
->editbox
.password
, escaped
,
1505 base_id
, base_id
+1, 100, NULL
);
1507 if (ctrl
->editbox
.has_list
) {
1508 staticcombo(&pos
, escaped
, base_id
, base_id
+1,
1509 ctrl
->editbox
.percentwidth
);
1511 (ctrl
->editbox
.password ? staticpassedit
: staticedit
)
1512 (&pos
, escaped
, base_id
, base_id
+1,
1513 ctrl
->editbox
.percentwidth
);
1519 num_ids
= ctrl
->radio
.nbuttons
+ 1; /* label as well */
1521 struct radio
*buttons
;
1524 escaped
= shortcut_escape(ctrl
->radio
.label
,
1525 ctrl
->radio
.shortcut
);
1526 shortcuts
[nshortcuts
++] = ctrl
->radio
.shortcut
;
1528 buttons
= smalloc(ctrl
->radio
.nbuttons
* sizeof(struct radio
));
1530 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1532 shortcut_escape(ctrl
->radio
.buttons
[i
],
1533 (char)(ctrl
->radio
.shortcuts ?
1534 ctrl
->radio
.shortcuts
[i
] :
1536 buttons
[i
].id
= base_id
+ 1 + i
;
1537 if (ctrl
->radio
.shortcuts
) {
1538 assert(nshortcuts
< MAX_SHORTCUTS_PER_CTRL
);
1539 shortcuts
[nshortcuts
++] = ctrl
->radio
.shortcuts
[i
];
1543 radioline_common(&pos
, escaped
, base_id
,
1544 ctrl
->radio
.ncolumns
,
1545 buttons
, ctrl
->radio
.nbuttons
);
1547 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1548 sfree(buttons
[i
].text
);
1556 escaped
= shortcut_escape(ctrl
->checkbox
.label
,
1557 ctrl
->checkbox
.shortcut
);
1558 shortcuts
[nshortcuts
++] = ctrl
->checkbox
.shortcut
;
1559 checkbox(&pos
, escaped
, base_id
);
1563 escaped
= shortcut_escape(ctrl
->button
.label
,
1564 ctrl
->button
.shortcut
);
1565 shortcuts
[nshortcuts
++] = ctrl
->button
.shortcut
;
1567 button(&pos
, escaped
, base_id
, ctrl
->button
.isdefault
);
1572 escaped
= shortcut_escape(ctrl
->listbox
.label
,
1573 ctrl
->listbox
.shortcut
);
1574 shortcuts
[nshortcuts
++] = ctrl
->listbox
.shortcut
;
1575 if (ctrl
->listbox
.draglist
) {
1576 data
= smalloc(sizeof(struct prefslist
));
1578 prefslist(data
, &pos
, ctrl
->listbox
.height
, escaped
,
1579 base_id
, base_id
+1, base_id
+2, base_id
+3);
1580 shortcuts
[nshortcuts
++] = 'u'; /* Up */
1581 shortcuts
[nshortcuts
++] = 'd'; /* Down */
1582 } else if (ctrl
->listbox
.height
== 0) {
1583 /* Drop-down list. */
1584 if (ctrl
->listbox
.percentwidth
== 100) {
1585 staticddlbig(&pos
, escaped
,
1586 base_id
, base_id
+1);
1588 staticddl(&pos
, escaped
, base_id
,
1589 base_id
+1, ctrl
->listbox
.percentwidth
);
1592 /* Ordinary list. */
1593 listbox(&pos
, escaped
, base_id
, base_id
+1,
1594 ctrl
->listbox
.height
, ctrl
->listbox
.multisel
);
1596 if (ctrl
->listbox
.ncols
) {
1598 * This method of getting the box width is a bit of
1599 * a hack; we'd do better to try to retrieve the
1600 * actual width in dialog units from doctl() just
1601 * before MapDialogRect. But that's going to be no
1602 * fun, and this should be good enough accuracy.
1604 int width
= cp
->width
* ctrl
->listbox
.percentwidth
;
1608 tabarray
= smalloc((ctrl
->listbox
.ncols
-1) * sizeof(int));
1610 for (i
= 0; i
< ctrl
->listbox
.ncols
-1; i
++) {
1611 percent
+= ctrl
->listbox
.percentages
[i
];
1612 tabarray
[i
] = width
* percent
/ 10000;
1614 SendDlgItemMessage(cp
->hwnd
, base_id
+1, LB_SETTABSTOPS
,
1615 ctrl
->listbox
.ncols
-1, (LPARAM
)tabarray
);
1620 case CTRL_FILESELECT
:
1622 escaped
= shortcut_escape(ctrl
->fileselect
.label
,
1623 ctrl
->fileselect
.shortcut
);
1624 shortcuts
[nshortcuts
++] = ctrl
->fileselect
.shortcut
;
1625 editbutton(&pos
, escaped
, base_id
, base_id
+1,
1626 "Bro&wse...", base_id
+2);
1627 shortcuts
[nshortcuts
++] = 'w';
1630 case CTRL_FONTSELECT
:
1632 escaped
= shortcut_escape(ctrl
->fontselect
.label
,
1633 ctrl
->fontselect
.shortcut
);
1634 shortcuts
[nshortcuts
++] = ctrl
->fontselect
.shortcut
;
1635 statictext(&pos
, escaped
, 1, base_id
);
1636 staticbtn(&pos
, "", base_id
+1, "Change...", base_id
+2);
1638 data
= smalloc(sizeof(FontSpec
));
1641 assert(!"Can't happen");
1642 num_ids
= 0; /* placate gcc */
1647 * Create a `struct winctrl' for this control, and advance
1648 * the dialog ID counter, if it's actually been created
1649 * (and isn't tabdelayed).
1652 struct winctrl
*c
= smalloc(sizeof(struct winctrl
));
1655 c
->base_id
= base_id
;
1656 c
->num_ids
= num_ids
;
1658 memcpy(c
->shortcuts
, shortcuts
, sizeof(shortcuts
));
1660 winctrl_add_shortcuts(dp
, c
);
1664 if (colstart
>= 0) {
1666 * Update the ypos in all columns crossed by this
1670 for (i
= colstart
; i
< colstart
+colspan
; i
++)
1671 columns
[i
].ypos
= pos
.ypos
;
1676 * We've now finished laying out the controls; so now update
1677 * the ctlpos and control ID that were passed in, terminate
1678 * any containing box, and return.
1680 for (i
= 0; i
< ncols
; i
++)
1681 if (cp
->ypos
< columns
[i
].ypos
)
1682 cp
->ypos
= columns
[i
].ypos
;
1685 if (s
->boxname
&& *s
->boxname
)
1689 static void winctrl_set_focus(union control
*ctrl
, struct dlgparam
*dp
,
1694 dp
->lastfocused
= dp
->focused
;
1696 } else if (!has_focus
&& dp
->focused
== ctrl
) {
1697 dp
->lastfocused
= dp
->focused
;
1702 union control
*dlg_last_focused(void *dlg
)
1704 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1705 return dp
->lastfocused
;
1709 * The dialog-box procedure calls this function to handle Windows
1710 * messages on a control we manage.
1712 int winctrl_handle_command(struct dlgparam
*dp
, UINT msg
,
1713 WPARAM wParam
, LPARAM lParam
)
1716 union control
*ctrl
;
1718 static UINT draglistmsg
= WM_NULL
;
1721 * Filter out pointless window messages. Our interest is in
1722 * WM_COMMAND and the drag list message, and nothing else.
1724 if (draglistmsg
== WM_NULL
)
1725 draglistmsg
= RegisterWindowMessage (DRAGLISTMSGSTRING
);
1727 if (msg
!= draglistmsg
&& msg
!= WM_COMMAND
&& msg
!= WM_DRAWITEM
)
1731 * Look up the control ID in our data.
1734 for (i
= 0; i
< dp
->nctrltrees
; i
++) {
1735 c
= winctrl_findbyid(dp
->controltrees
[i
], LOWORD(wParam
));
1740 return 0; /* we have nothing to do */
1742 if (msg
== WM_DRAWITEM
) {
1744 * Owner-draw request for a panel title.
1746 LPDRAWITEMSTRUCT di
= (LPDRAWITEMSTRUCT
) lParam
;
1748 RECT r
= di
->rcItem
;
1751 GetTextExtentPoint32(hdc
, (char *)c
->data
,
1752 strlen((char *)c
->data
), &s
);
1753 DrawEdge(hdc
, &r
, EDGE_ETCHED
, BF_ADJUST
| BF_RECT
);
1755 r
.left
+ (r
.right
-r
.left
-s
.cx
)/2,
1756 r
.top
+ (r
.bottom
-r
.top
-s
.cy
)/2,
1757 (char *)c
->data
, strlen((char *)c
->data
));
1763 id
= LOWORD(wParam
) - c
->base_id
;
1765 if (!ctrl
|| !ctrl
->generic
.handler
)
1766 return 0; /* nothing we can do here */
1769 * From here on we do not issue `return' statements until the
1770 * very end of the dialog box: any event handler is entitled to
1771 * ask for a colour selector, so we _must_ always allow control
1772 * to reach the end of this switch statement so that the
1773 * subsequent code can test dp->coloursel_wanted().
1776 dp
->coloursel_wanted
= FALSE
;
1779 * Now switch on the control type and the message.
1781 switch (ctrl
->generic
.type
) {
1783 if (msg
== WM_COMMAND
&& !ctrl
->editbox
.has_list
&&
1784 (HIWORD(wParam
) == EN_SETFOCUS
|| HIWORD(wParam
) == EN_KILLFOCUS
))
1785 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == EN_SETFOCUS
);
1786 if (msg
== WM_COMMAND
&& ctrl
->editbox
.has_list
&&
1787 (HIWORD(wParam
)==CBN_SETFOCUS
|| HIWORD(wParam
)==CBN_KILLFOCUS
))
1788 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == CBN_SETFOCUS
);
1790 if (msg
== WM_COMMAND
&& !ctrl
->editbox
.has_list
&&
1791 HIWORD(wParam
) == EN_CHANGE
)
1792 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1793 if (msg
== WM_COMMAND
&&
1794 ctrl
->editbox
.has_list
) {
1795 if (HIWORD(wParam
) == CBN_SELCHANGE
) {
1799 index
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1,
1800 CB_GETCURSEL
, 0, 0);
1801 len
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1,
1802 CB_GETLBTEXTLEN
, index
, 0);
1803 text
= smalloc(len
+1);
1804 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, CB_GETLBTEXT
,
1805 index
, (LPARAM
)text
);
1806 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, text
);
1808 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1809 } else if (HIWORD(wParam
) == CBN_EDITCHANGE
) {
1810 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1811 } else if (HIWORD(wParam
) == CBN_KILLFOCUS
) {
1812 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
1818 if (msg
== WM_COMMAND
&&
1819 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1820 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1822 * We sometimes get spurious BN_CLICKED messages for the
1823 * radio button that is just about to _lose_ selection, if
1824 * we're switching using the arrow keys. Therefore we
1825 * double-check that the button in wParam is actually
1826 * checked before generating an event.
1828 if (msg
== WM_COMMAND
&&
1829 (HIWORD(wParam
) == BN_CLICKED
||
1830 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
1831 IsDlgButtonChecked(dp
->hwnd
, LOWORD(wParam
))) {
1832 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1836 if (msg
== WM_COMMAND
&&
1837 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1838 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1839 if (msg
== WM_COMMAND
&&
1840 (HIWORD(wParam
) == BN_CLICKED
||
1841 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
1842 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1846 if (msg
== WM_COMMAND
&&
1847 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1848 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1849 if (msg
== WM_COMMAND
&&
1850 (HIWORD(wParam
) == BN_CLICKED
||
1851 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
1852 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1856 if (msg
== WM_COMMAND
&& ctrl
->listbox
.height
!= 0 &&
1857 (HIWORD(wParam
)==LBN_SETFOCUS
|| HIWORD(wParam
)==LBN_KILLFOCUS
))
1858 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == LBN_SETFOCUS
);
1859 if (msg
== WM_COMMAND
&& ctrl
->listbox
.height
== 0 &&
1860 (HIWORD(wParam
)==CBN_SETFOCUS
|| HIWORD(wParam
)==CBN_KILLFOCUS
))
1861 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == CBN_SETFOCUS
);
1862 if (msg
== WM_COMMAND
&& id
>= 2 &&
1863 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1864 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1865 if (ctrl
->listbox
.draglist
) {
1867 pret
= handle_prefslist(c
->data
, NULL
, 0, (msg
!= WM_COMMAND
),
1868 dp
->hwnd
, wParam
, lParam
);
1870 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1873 if (msg
== WM_COMMAND
&& HIWORD(wParam
) == LBN_DBLCLK
) {
1874 SetCapture(dp
->hwnd
);
1875 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1876 } else if (msg
== WM_COMMAND
&& HIWORD(wParam
) == LBN_SELCHANGE
) {
1877 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1881 case CTRL_FILESELECT
:
1882 if (msg
== WM_COMMAND
&& id
== 1 &&
1883 (HIWORD(wParam
) == EN_SETFOCUS
|| HIWORD(wParam
) == EN_KILLFOCUS
))
1884 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == EN_SETFOCUS
);
1885 if (msg
== WM_COMMAND
&& id
== 2 &&
1886 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1887 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1889 (msg
== WM_COMMAND
&&
1890 (HIWORD(wParam
) == BN_CLICKED
||
1891 HIWORD(wParam
) == BN_DOUBLECLICKED
))) {
1893 char filename
[FILENAME_MAX
];
1896 memset(&of
, 0, sizeof(of
));
1897 #ifdef OPENFILENAME_SIZE_VERSION_400
1898 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
1900 of
.lStructSize
= sizeof(of
);
1902 of
.hwndOwner
= dp
->hwnd
;
1903 if (ctrl
->fileselect
.filter
)
1904 of
.lpstrFilter
= ctrl
->fileselect
.filter
;
1906 of
.lpstrFilter
= "All Files (*.*)\0*\0\0\0";
1907 of
.lpstrCustomFilter
= NULL
;
1908 of
.nFilterIndex
= 1;
1909 of
.lpstrFile
= filename
;
1910 GetDlgItemText(dp
->hwnd
, c
->base_id
+1, filename
, lenof(filename
));
1911 filename
[lenof(filename
)-1] = '\0';
1912 of
.nMaxFile
= lenof(filename
);
1913 of
.lpstrFileTitle
= NULL
;
1914 of
.lpstrInitialDir
= NULL
;
1915 of
.lpstrTitle
= ctrl
->fileselect
.title
;
1917 if (ctrl
->fileselect
.for_writing
)
1918 ret
= GetSaveFileName(&of
);
1920 ret
= GetOpenFileName(&of
);
1922 SetDlgItemText(dp
->hwnd
, c
->base_id
+ 1, filename
);
1923 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1927 case CTRL_FONTSELECT
:
1928 if (msg
== WM_COMMAND
&& id
== 2 &&
1929 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1930 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1932 (msg
== WM_COMMAND
&&
1933 (HIWORD(wParam
) == BN_CLICKED
||
1934 HIWORD(wParam
) == BN_DOUBLECLICKED
))) {
1938 FontSpec fs
= *(FontSpec
*)c
->data
;
1941 lf
.lfHeight
= -MulDiv(fs
.height
,
1942 GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
1944 lf
.lfWidth
= lf
.lfEscapement
= lf
.lfOrientation
= 0;
1945 lf
.lfItalic
= lf
.lfUnderline
= lf
.lfStrikeOut
= 0;
1946 lf
.lfWeight
= (fs
.isbold ? FW_BOLD
: 0);
1947 lf
.lfCharSet
= fs
.charset
;
1948 lf
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
1949 lf
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
1950 lf
.lfQuality
= DEFAULT_QUALITY
;
1951 lf
.lfPitchAndFamily
= FIXED_PITCH
| FF_DONTCARE
;
1952 strncpy(lf
.lfFaceName
, fs
.name
,
1953 sizeof(lf
.lfFaceName
) - 1);
1954 lf
.lfFaceName
[sizeof(lf
.lfFaceName
) - 1] = '\0';
1956 cf
.lStructSize
= sizeof(cf
);
1957 cf
.hwndOwner
= dp
->hwnd
;
1959 cf
.Flags
= CF_FIXEDPITCHONLY
| CF_FORCEFONTEXIST
|
1960 CF_INITTOLOGFONTSTRUCT
| CF_SCREENFONTS
;
1962 if (ChooseFont(&cf
)) {
1963 strncpy(fs
.name
, lf
.lfFaceName
,
1964 sizeof(fs
.name
) - 1);
1965 fs
.name
[sizeof(fs
.name
) - 1] = '\0';
1966 fs
.isbold
= (lf
.lfWeight
== FW_BOLD
);
1967 fs
.charset
= lf
.lfCharSet
;
1968 fs
.height
= cf
.iPointSize
/ 10;
1969 dlg_fontsel_set(ctrl
, dp
, fs
);
1970 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1977 * If the above event handler has asked for a colour selector,
1978 * now is the time to generate one.
1980 if (dp
->coloursel_wanted
) {
1981 static CHOOSECOLOR cc
;
1982 static DWORD custom
[16] = { 0 }; /* zero initialisers */
1983 cc
.lStructSize
= sizeof(cc
);
1984 cc
.hwndOwner
= dp
->hwnd
;
1985 cc
.hInstance
= (HWND
) hinst
;
1986 cc
.lpCustColors
= custom
;
1987 cc
.rgbResult
= RGB(dp
->coloursel_result
.r
,
1988 dp
->coloursel_result
.g
,
1989 dp
->coloursel_result
.b
);
1990 cc
.Flags
= CC_FULLOPEN
| CC_RGBINIT
;
1991 if (ChooseColor(&cc
)) {
1992 dp
->coloursel_result
.r
=
1993 (unsigned char) (cc
.rgbResult
& 0xFF);
1994 dp
->coloursel_result
.g
=
1995 (unsigned char) (cc
.rgbResult
>> 8) & 0xFF;
1996 dp
->coloursel_result
.b
=
1997 (unsigned char) (cc
.rgbResult
>> 16) & 0xFF;
1998 dp
->coloursel_result
.ok
= TRUE
;
2000 dp
->coloursel_result
.ok
= FALSE
;
2001 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
2008 * This function can be called to produce context help on a
2009 * control. Returns TRUE if it has actually launched WinHelp.
2011 int winctrl_context_help(struct dlgparam
*dp
, HWND hwnd
, int id
)
2018 * Look up the control ID in our data.
2021 for (i
= 0; i
< dp
->nctrltrees
; i
++) {
2022 c
= winctrl_findbyid(dp
->controltrees
[i
], id
);
2027 return 0; /* we have nothing to do */
2030 * This is the Windows front end, so we're allowed to assume
2031 * `helpctx.p' is a context string.
2033 if (!c
->ctrl
|| !c
->ctrl
->generic
.helpctx
.p
)
2034 return 0; /* no help available for this ctrl */
2036 cmd
= dupprintf("JI(`',`%s')", c
->ctrl
->generic
.helpctx
.p
);
2037 WinHelp(hwnd
, help_path
, HELP_COMMAND
, (DWORD
)cmd
);
2043 * Now the various functions that the platform-independent
2044 * mechanism can call to access the dialog box entries.
2047 static struct winctrl
*dlg_findbyctrl(struct dlgparam
*dp
, union control
*ctrl
)
2051 for (i
= 0; i
< dp
->nctrltrees
; i
++) {
2052 struct winctrl
*c
= winctrl_findbyctrl(dp
->controltrees
[i
], ctrl
);
2059 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
2061 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2062 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2063 assert(c
&& c
->ctrl
->generic
.type
== CTRL_RADIO
);
2064 CheckRadioButton(dp
->hwnd
,
2066 c
->base_id
+ c
->ctrl
->radio
.nbuttons
,
2067 c
->base_id
+ 1 + whichbutton
);
2070 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
2072 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2073 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2075 assert(c
&& c
->ctrl
->generic
.type
== CTRL_RADIO
);
2076 for (i
= 0; i
< c
->ctrl
->radio
.nbuttons
; i
++)
2077 if (IsDlgButtonChecked(dp
->hwnd
, c
->base_id
+ 1 + i
))
2079 assert(!"No radio button was checked?!");
2083 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
2085 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2086 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2087 assert(c
&& c
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
2088 CheckDlgButton(dp
->hwnd
, c
->base_id
, (checked
!= 0));
2091 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
2093 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2094 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2095 assert(c
&& c
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
2096 return 0 != IsDlgButtonChecked(dp
->hwnd
, c
->base_id
);
2099 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
2101 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2102 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2103 assert(c
&& c
->ctrl
->generic
.type
== CTRL_EDITBOX
);
2104 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, text
);
2107 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
2109 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2110 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2111 assert(c
&& c
->ctrl
->generic
.type
== CTRL_EDITBOX
);
2112 GetDlgItemText(dp
->hwnd
, c
->base_id
+1, buffer
, length
);
2113 buffer
[length
-1] = '\0';
2116 /* The `listbox' functions can also apply to combo boxes. */
2117 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
2119 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2120 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2123 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2124 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2125 c
->ctrl
->editbox
.has_list
)));
2126 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2127 LB_RESETCONTENT
: CB_RESETCONTENT
);
2128 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, 0);
2131 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
2133 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2134 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2137 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2138 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2139 c
->ctrl
->editbox
.has_list
)));
2140 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2141 LB_DELETESTRING
: CB_DELETESTRING
);
2142 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, index
, 0);
2145 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
2147 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2148 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2151 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2152 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2153 c
->ctrl
->editbox
.has_list
)));
2154 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2155 LB_ADDSTRING
: CB_ADDSTRING
);
2156 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, (LPARAM
)text
);
2160 * Each listbox entry may have a numeric id associated with it.
2161 * Note that some front ends only permit a string to be stored at
2162 * each position, which means that _if_ you put two identical
2163 * strings in any listbox then you MUST not assign them different
2164 * IDs and expect to get meaningful results back.
2166 void dlg_listbox_addwithindex(union control
*ctrl
, void *dlg
,
2167 char const *text
, int id
)
2169 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2170 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2171 int msg
, msg2
, index
;
2173 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2174 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2175 c
->ctrl
->editbox
.has_list
)));
2176 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2177 LB_ADDSTRING
: CB_ADDSTRING
);
2178 msg2
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2179 LB_SETITEMDATA
: CB_SETITEMDATA
);
2180 index
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, (LPARAM
)text
);
2181 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg2
, index
, (LPARAM
)id
);
2184 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
2186 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2187 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2189 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
);
2190 msg
= (c
->ctrl
->listbox
.height
!= 0 ? LB_GETITEMDATA
: CB_GETITEMDATA
);
2192 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, index
, 0);
2195 /* dlg_listbox_index returns <0 if no single element is selected. */
2196 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
2198 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2199 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2201 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
&&
2202 !c
->ctrl
->listbox
.multisel
);
2203 msg
= (c
->ctrl
->listbox
.height
!= 0 ? LB_GETCURSEL
: CB_GETCURSEL
);
2204 ret
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, 0);
2211 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
2213 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2214 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2215 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
&&
2216 c
->ctrl
->listbox
.multisel
&&
2217 c
->ctrl
->listbox
.height
!= 0);
2219 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, LB_GETSEL
, index
, 0);
2222 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
2224 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2225 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2227 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
&&
2228 !c
->ctrl
->listbox
.multisel
);
2229 msg
= (c
->ctrl
->listbox
.height
!= 0 ? LB_SETCURSEL
: CB_SETCURSEL
);
2230 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, index
, 0);
2233 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
2235 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2236 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2237 assert(c
&& c
->ctrl
->generic
.type
== CTRL_TEXT
);
2238 SetDlgItemText(dp
->hwnd
, c
->base_id
, text
);
2241 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
2243 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2244 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2245 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FILESELECT
);
2246 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, fn
.path
);
2249 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
2251 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2252 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2253 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FILESELECT
);
2254 GetDlgItemText(dp
->hwnd
, c
->base_id
+1, fn
->path
, lenof(fn
->path
));
2255 fn
->path
[lenof(fn
->path
)-1] = '\0';
2258 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
2260 char *buf
, *boldstr
;
2261 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2262 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2263 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
2265 *(FontSpec
*)c
->data
= fs
; /* structure copy */
2267 boldstr
= (fs
.isbold ?
"bold, " : "");
2269 buf
= dupprintf("Font: %s, %sdefault height", fs
.name
, boldstr
);
2271 buf
= dupprintf("Font: %s, %s%d-point", fs
.name
, boldstr
,
2272 (fs
.height
< 0 ?
-fs
.height
: fs
.height
));
2273 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, buf
);
2277 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
2279 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2280 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2281 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
2282 *fs
= *(FontSpec
*)c
->data
; /* structure copy */
2286 * Bracketing a large set of updates in these two functions will
2287 * cause the front end (if possible) to delay updating the screen
2288 * until it's all complete, thus avoiding flicker.
2290 void dlg_update_start(union control
*ctrl
, void *dlg
)
2292 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2293 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2294 if (c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
) {
2295 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, WM_SETREDRAW
, FALSE
, 0);
2299 void dlg_update_done(union control
*ctrl
, void *dlg
)
2301 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2302 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2303 if (c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
) {
2304 HWND hw
= GetDlgItem(dp
->hwnd
, c
->base_id
+1);
2305 SendMessage(hw
, WM_SETREDRAW
, TRUE
, 0);
2306 InvalidateRect(hw
, NULL
, TRUE
);
2310 void dlg_set_focus(union control
*ctrl
, void *dlg
)
2312 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2313 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2316 switch (ctrl
->generic
.type
) {
2317 case CTRL_EDITBOX
: id
= c
->base_id
+ 1; break;
2319 for (id
= c
->base_id
+ ctrl
->radio
.nbuttons
; id
> 1; id
--)
2320 if (IsDlgButtonChecked(dp
->hwnd
, id
))
2323 * In the theoretically-unlikely case that no button was
2324 * selected, id should come out of this as 1, which is a
2325 * reasonable enough choice.
2328 case CTRL_CHECKBOX
: id
= c
->base_id
; break;
2329 case CTRL_BUTTON
: id
= c
->base_id
; break;
2330 case CTRL_LISTBOX
: id
= c
->base_id
+ 1; break;
2331 case CTRL_FILESELECT
: id
= c
->base_id
+ 1; break;
2332 case CTRL_FONTSELECT
: id
= c
->base_id
+ 2; break;
2333 default: id
= c
->base_id
; break;
2335 ctl
= GetDlgItem(dp
->hwnd
, id
);
2340 * During event processing, you might well want to give an error
2341 * indication to the user. dlg_beep() is a quick and easy generic
2342 * error; dlg_error() puts up a message-box or equivalent.
2344 void dlg_beep(void *dlg
)
2346 /* struct dlgparam *dp = (struct dlgparam *)dlg; */
2350 void dlg_error_msg(void *dlg
, char *msg
)
2352 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2353 MessageBox(dp
->hwnd
, msg
,
2354 dp
->errtitle ? dp
->errtitle
: NULL
,
2355 MB_OK
| MB_ICONERROR
);
2359 * This function signals to the front end that the dialog's
2360 * processing is completed, and passes an integer value (typically
2361 * a success status).
2363 void dlg_end(void *dlg
, int value
)
2365 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2367 dp
->endresult
= value
;
2370 void dlg_refresh(union control
*ctrl
, void *dlg
)
2372 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2378 * Send EVENT_REFRESH to absolutely everything.
2380 for (j
= 0; j
< dp
->nctrltrees
; j
++) {
2382 (c
= winctrl_findbyindex(dp
->controltrees
[j
], i
)) != NULL
;
2384 if (c
->ctrl
&& c
->ctrl
->generic
.handler
!= NULL
)
2385 c
->ctrl
->generic
.handler(c
->ctrl
, dp
,
2386 dp
->data
, EVENT_REFRESH
);
2391 * Send EVENT_REFRESH to a specific control.
2393 if (ctrl
->generic
.handler
!= NULL
)
2394 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
2398 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
2400 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2401 dp
->coloursel_wanted
= TRUE
;
2402 dp
->coloursel_result
.r
= r
;
2403 dp
->coloursel_result
.g
= g
;
2404 dp
->coloursel_result
.b
= b
;
2407 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
2408 int *r
, int *g
, int *b
)
2410 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2411 if (dp
->coloursel_result
.ok
) {
2412 *r
= dp
->coloursel_result
.r
;
2413 *g
= dp
->coloursel_result
.g
;
2414 *b
= dp
->coloursel_result
.b
;