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'
30 #define STATICHEIGHT 8
31 #define TITLEHEIGHT 12
32 #define CHECKBOXHEIGHT 8
36 #define LISTINCREMENT 8
37 #define COMBOHEIGHT 12
38 #define PUSHBTNHEIGHT 14
39 #define PROGBARHEIGHT 14
41 void ctlposinit(struct ctlpos
*cp
, HWND hwnd
,
42 int leftborder
, int rightborder
, int topborder
)
46 cp
->font
= SendMessage(hwnd
, WM_GETFONT
, 0, 0);
48 GetClientRect(hwnd
, &r
);
52 MapDialogRect(hwnd
, &r2
);
53 cp
->dlu4inpix
= r2
.right
;
54 cp
->width
= (r
.right
* 4) / (r2
.right
) - 2 * GAPBETWEEN
;
55 cp
->xoff
= leftborder
;
56 cp
->width
-= leftborder
+ rightborder
;
59 HWND
doctl(struct ctlpos
*cp
, RECT r
,
60 char *wclass
, int wstyle
, int exstyle
, char *wtext
, int wid
)
64 * Note nonstandard use of RECT. This is deliberate: by
65 * transforming the width and height directly we arrange to
66 * have all supposedly same-sized controls really same-sized.
70 MapDialogRect(cp
->hwnd
, &r
);
73 * We can pass in cp->hwnd == NULL, to indicate a dry run
74 * without creating any actual controls.
77 ctl
= CreateWindowEx(exstyle
, wclass
, wtext
, wstyle
,
78 r
.left
, r
.top
, r
.right
, r
.bottom
,
79 cp
->hwnd
, (HMENU
) wid
, hinst
, NULL
);
80 SendMessage(ctl
, WM_SETFONT
, cp
->font
, MAKELPARAM(TRUE
, 0));
82 if (!strcmp(wclass
, "LISTBOX")) {
84 * Bizarre Windows bug: the list box calculates its
85 * number of lines based on the font it has at creation
86 * time, but sending it WM_SETFONT doesn't cause it to
87 * recalculate. So now, _after_ we've sent it
88 * WM_SETFONT, we explicitly resize it (to the same
89 * size it was already!) to force it to reconsider.
91 SetWindowPos(ctl
, NULL
, 0, 0, r
.right
, r
.bottom
,
92 SWP_NOACTIVATE
| SWP_NOCOPYBITS
|
93 SWP_NOMOVE
| SWP_NOZORDER
);
101 * A title bar across the top of a sub-dialog.
103 void bartitle(struct ctlpos
*cp
, char *name
, int id
)
110 r
.bottom
= STATICHEIGHT
;
111 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
112 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, name
, id
);
116 * Begin a grouping box, with or without a group title.
118 void beginbox(struct ctlpos
*cp
, char *name
, int idbox
)
120 cp
->boxystart
= cp
->ypos
;
122 cp
->boxystart
-= STATICHEIGHT
/ 2;
124 cp
->ypos
+= STATICHEIGHT
;
126 cp
->width
-= 2 * GAPXBOX
;
133 * End a grouping box.
135 void endbox(struct ctlpos
*cp
)
139 cp
->width
+= 2 * GAPXBOX
;
140 cp
->ypos
+= GAPYBOX
- GAPBETWEEN
;
143 r
.top
= cp
->boxystart
;
144 r
.bottom
= cp
->ypos
- cp
->boxystart
;
145 doctl(cp
, r
, "BUTTON", BS_GROUPBOX
| WS_CHILD
| WS_VISIBLE
, 0,
146 cp
->boxtext ? cp
->boxtext
: "", cp
->boxid
);
151 * A static line, followed by a full-width edit box.
153 void editboxfw(struct ctlpos
*cp
, int password
, char *text
,
154 int staticid
, int editid
)
163 r
.bottom
= STATICHEIGHT
;
164 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, text
, staticid
);
165 cp
->ypos
+= STATICHEIGHT
+ GAPWITHIN
;
168 r
.bottom
= EDITHEIGHT
;
170 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| ES_AUTOHSCROLL
|
171 (password ? ES_PASSWORD
: 0),
172 WS_EX_CLIENTEDGE
, "", editid
);
173 cp
->ypos
+= EDITHEIGHT
+ GAPBETWEEN
;
177 * A static line, followed by a full-width combo box.
179 void combobox(struct ctlpos
*cp
, char *text
, int staticid
, int listid
)
188 r
.bottom
= STATICHEIGHT
;
189 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, text
, staticid
);
190 cp
->ypos
+= STATICHEIGHT
+ GAPWITHIN
;
193 r
.bottom
= COMBOHEIGHT
* 10;
194 doctl(cp
, r
, "COMBOBOX",
195 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
|
196 CBS_DROPDOWN
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", listid
);
197 cp
->ypos
+= COMBOHEIGHT
+ GAPBETWEEN
;
200 struct radio
{ char *text
; int id
; };
202 static void radioline_common(struct ctlpos
*cp
, char *text
, int id
,
203 int nacross
, struct radio
*buttons
, int nbuttons
)
214 r
.bottom
= STATICHEIGHT
;
215 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
216 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, text
, id
);
221 for (j
= 0; j
< nbuttons
; j
++) {
222 char *btext
= buttons
[j
].text
;
223 int bid
= buttons
[j
].id
;
226 cp
->ypos
+= r
.bottom
+ (nacross
> 1 ? GAPBETWEEN
: GAPWITHIN
);
229 r
.left
= GAPBETWEEN
+ i
* (cp
->width
+ GAPBETWEEN
) / nacross
;
232 (i
+ 1) * (cp
->width
+ GAPBETWEEN
) / nacross
- r
.left
;
234 r
.right
= cp
->width
- r
.left
;
236 r
.bottom
= RADIOHEIGHT
;
237 doctl(cp
, r
, "BUTTON",
238 BS_NOTIFY
| BS_AUTORADIOBUTTON
| WS_CHILD
|
239 WS_VISIBLE
| WS_TABSTOP
| group
, 0, btext
, bid
);
243 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
247 * A set of radio buttons on the same line, with a static above
248 * them. `nacross' dictates how many parts the line is divided into
249 * (you might want this not to equal the number of buttons if you
250 * needed to line up some 2s and some 3s to look good in the same
253 * There's a bit of a hack in here to ensure that if nacross
254 * exceeds the actual number of buttons, the rightmost button
255 * really does get all the space right to the edge of the line, so
256 * you can do things like
258 * (*) Button1 (*) Button2 (*) ButtonWithReallyLongTitle
260 void radioline(struct ctlpos
*cp
, char *text
, int id
, int nacross
, ...)
263 struct radio
*buttons
;
266 va_start(ap
, nacross
);
269 char *btext
= va_arg(ap
, char *);
273 bid
= va_arg(ap
, int);
277 buttons
= snewn(nbuttons
, struct radio
);
278 va_start(ap
, nacross
);
279 for (i
= 0; i
< nbuttons
; i
++) {
280 buttons
[i
].text
= va_arg(ap
, char *);
281 buttons
[i
].id
= va_arg(ap
, int);
284 radioline_common(cp
, text
, id
, nacross
, buttons
, nbuttons
);
289 * A set of radio buttons on the same line, without a static above
290 * them. Otherwise just like radioline.
292 void bareradioline(struct ctlpos
*cp
, int nacross
, ...)
295 struct radio
*buttons
;
298 va_start(ap
, nacross
);
301 char *btext
= va_arg(ap
, char *);
305 bid
= va_arg(ap
, int);
308 buttons
= snewn(nbuttons
, struct radio
);
309 va_start(ap
, nacross
);
310 for (i
= 0; i
< nbuttons
; i
++) {
311 buttons
[i
].text
= va_arg(ap
, char *);
312 buttons
[i
].id
= va_arg(ap
, int);
315 radioline_common(cp
, NULL
, 0, nacross
, buttons
, nbuttons
);
320 * A set of radio buttons on multiple lines, with a static above
323 void radiobig(struct ctlpos
*cp
, char *text
, int id
, ...)
326 struct radio
*buttons
;
332 char *btext
= va_arg(ap
, char *);
336 bid
= va_arg(ap
, int);
339 buttons
= snewn(nbuttons
, struct radio
);
341 for (i
= 0; i
< nbuttons
; i
++) {
342 buttons
[i
].text
= va_arg(ap
, char *);
343 buttons
[i
].id
= va_arg(ap
, int);
346 radioline_common(cp
, text
, id
, 1, buttons
, nbuttons
);
351 * A single standalone checkbox.
353 void checkbox(struct ctlpos
*cp
, char *text
, int id
)
360 r
.bottom
= CHECKBOXHEIGHT
;
361 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
362 doctl(cp
, r
, "BUTTON",
363 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
, 0,
368 * Wrap a piece of text for a static text control. Returns the
369 * wrapped text (a malloc'ed string containing \ns), and also
370 * returns the number of lines required.
372 char *staticwrap(struct ctlpos
*cp
, HWND hwnd
, char *text
, int *lines
)
374 HDC hdc
= GetDC(hwnd
);
375 int lpx
= GetDeviceCaps(hdc
, LOGPIXELSX
);
376 int width
, nlines
, j
;
381 HFONT oldfont
, newfont
;
383 ret
= snewn(1+strlen(text
), char);
386 pwidths
= snewn(1+strlen(text
), INT
);
389 * Work out the width the text will need to fit in, by doing
390 * the same adjustment that the `statictext' function itself
393 SetMapMode(hdc
, MM_TEXT
); /* ensure logical units == pixels */
394 r
.left
= r
.top
= r
.bottom
= 0;
396 MapDialogRect(hwnd
, &r
);
402 * We must select the correct font into the HDC before calling
403 * GetTextExtent*, or silly things will happen.
405 newfont
= (HFONT
)SendMessage(hwnd
, WM_GETFONT
, 0, 0);
406 oldfont
= SelectObject(hdc
, newfont
);
409 if (!GetTextExtentExPoint(hdc
, p
, strlen(p
), width
,
410 &nfit
, pwidths
, &size
) ||
411 (size_t)nfit
>= strlen(p
)) {
413 * Either GetTextExtentExPoint returned failure, or the
414 * whole of the rest of the text fits on this line.
415 * Either way, we stop wrapping, copy the remainder of
416 * the input string unchanged to the output, and leave.
423 * Now we search backwards along the string from `nfit',
424 * looking for a space at which to break the line. If we
425 * don't find one at all, that's fine - we'll just break
426 * the line at `nfit'.
428 for (j
= nfit
; j
> 0; j
--) {
429 if (isspace((unsigned char)p
[j
])) {
440 while (*p
&& isspace((unsigned char)*p
))
446 SelectObject(hdc
, oldfont
);
447 ReleaseDC(cp
->hwnd
, hdc
);
449 if (lines
) *lines
= nlines
;
455 * A single standalone static text control.
457 void statictext(struct ctlpos
*cp
, char *text
, int lines
, int id
)
464 r
.bottom
= STATICHEIGHT
* lines
;
465 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
466 doctl(cp
, r
, "STATIC",
467 WS_CHILD
| WS_VISIBLE
| SS_LEFTNOWORDWRAP
,
472 * An owner-drawn static text control for a panel title.
474 void paneltitle(struct ctlpos
*cp
, int id
)
481 r
.bottom
= TITLEHEIGHT
;
482 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
483 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
| SS_OWNERDRAW
,
488 * A button on the right hand side, with a static to its left.
490 void staticbtn(struct ctlpos
*cp
, char *stext
, int sid
,
491 char *btext
, int bid
)
493 const int height
= (PUSHBTNHEIGHT
> STATICHEIGHT ?
494 PUSHBTNHEIGHT
: STATICHEIGHT
);
496 int lwid
, rwid
, rpos
;
498 rpos
= GAPBETWEEN
+ 3 * (cp
->width
+ GAPBETWEEN
) / 4;
499 lwid
= rpos
- 2 * GAPBETWEEN
;
500 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
503 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
505 r
.bottom
= STATICHEIGHT
;
506 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
509 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
511 r
.bottom
= PUSHBTNHEIGHT
;
512 doctl(cp
, r
, "BUTTON",
513 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
516 cp
->ypos
+= height
+ GAPBETWEEN
;
520 * A simple push button.
522 void button(struct ctlpos
*cp
, char *btext
, int bid
, int defbtn
)
529 r
.bottom
= PUSHBTNHEIGHT
;
531 /* Q67655: the _dialog box_ must know which button is default
532 * as well as the button itself knowing */
533 if (defbtn
&& cp
->hwnd
)
534 SendMessage(cp
->hwnd
, DM_SETDEFID
, bid
, 0);
536 doctl(cp
, r
, "BUTTON",
537 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
538 (defbtn ? BS_DEFPUSHBUTTON
: 0) | BS_PUSHBUTTON
,
541 cp
->ypos
+= PUSHBTNHEIGHT
+ GAPBETWEEN
;
545 * Like staticbtn, but two buttons.
547 void static2btn(struct ctlpos
*cp
, char *stext
, int sid
,
548 char *btext1
, int bid1
, char *btext2
, int bid2
)
550 const int height
= (PUSHBTNHEIGHT
> STATICHEIGHT ?
551 PUSHBTNHEIGHT
: STATICHEIGHT
);
553 int lwid
, rwid1
, rwid2
, rpos1
, rpos2
;
555 rpos1
= GAPBETWEEN
+ (cp
->width
+ GAPBETWEEN
) / 2;
556 rpos2
= GAPBETWEEN
+ 3 * (cp
->width
+ GAPBETWEEN
) / 4;
557 lwid
= rpos1
- 2 * GAPBETWEEN
;
558 rwid1
= rpos2
- rpos1
- GAPBETWEEN
;
559 rwid2
= cp
->width
+ GAPBETWEEN
- rpos2
;
562 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
564 r
.bottom
= STATICHEIGHT
;
565 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
568 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
570 r
.bottom
= PUSHBTNHEIGHT
;
571 doctl(cp
, r
, "BUTTON",
572 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
576 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
578 r
.bottom
= PUSHBTNHEIGHT
;
579 doctl(cp
, r
, "BUTTON",
580 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
583 cp
->ypos
+= height
+ GAPBETWEEN
;
587 * An edit control on the right hand side, with a static to its left.
589 static void staticedit_internal(struct ctlpos
*cp
, char *stext
,
590 int sid
, int eid
, int percentedit
,
593 const int height
= (EDITHEIGHT
> STATICHEIGHT ?
594 EDITHEIGHT
: STATICHEIGHT
);
596 int lwid
, rwid
, rpos
;
599 GAPBETWEEN
+ (100 - percentedit
) * (cp
->width
+ GAPBETWEEN
) / 100;
600 lwid
= rpos
- 2 * GAPBETWEEN
;
601 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
604 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
606 r
.bottom
= STATICHEIGHT
;
607 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
610 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
612 r
.bottom
= EDITHEIGHT
;
614 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| ES_AUTOHSCROLL
| style
,
615 WS_EX_CLIENTEDGE
, "", eid
);
617 cp
->ypos
+= height
+ GAPBETWEEN
;
620 void staticedit(struct ctlpos
*cp
, char *stext
,
621 int sid
, int eid
, int percentedit
)
623 staticedit_internal(cp
, stext
, sid
, eid
, percentedit
, 0);
626 void staticpassedit(struct ctlpos
*cp
, char *stext
,
627 int sid
, int eid
, int percentedit
)
629 staticedit_internal(cp
, stext
, sid
, eid
, percentedit
, ES_PASSWORD
);
633 * A drop-down list box on the right hand side, with a static to
636 void staticddl(struct ctlpos
*cp
, char *stext
,
637 int sid
, int lid
, int percentlist
)
639 const int height
= (COMBOHEIGHT
> STATICHEIGHT ?
640 COMBOHEIGHT
: STATICHEIGHT
);
642 int lwid
, rwid
, rpos
;
645 GAPBETWEEN
+ (100 - percentlist
) * (cp
->width
+ GAPBETWEEN
) / 100;
646 lwid
= rpos
- 2 * GAPBETWEEN
;
647 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
650 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
652 r
.bottom
= STATICHEIGHT
;
653 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
656 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
658 r
.bottom
= COMBOHEIGHT
*4;
659 doctl(cp
, r
, "COMBOBOX",
660 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
|
661 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
663 cp
->ypos
+= height
+ GAPBETWEEN
;
667 * A combo box on the right hand side, with a static to its left.
669 void staticcombo(struct ctlpos
*cp
, char *stext
,
670 int sid
, int lid
, int percentlist
)
672 const int height
= (COMBOHEIGHT
> STATICHEIGHT ?
673 COMBOHEIGHT
: STATICHEIGHT
);
675 int lwid
, rwid
, rpos
;
678 GAPBETWEEN
+ (100 - percentlist
) * (cp
->width
+ GAPBETWEEN
) / 100;
679 lwid
= rpos
- 2 * GAPBETWEEN
;
680 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
683 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
685 r
.bottom
= STATICHEIGHT
;
686 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
689 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
691 r
.bottom
= COMBOHEIGHT
*10;
692 doctl(cp
, r
, "COMBOBOX",
693 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
|
694 CBS_DROPDOWN
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
696 cp
->ypos
+= height
+ GAPBETWEEN
;
700 * A static, with a full-width drop-down list box below it.
702 void staticddlbig(struct ctlpos
*cp
, char *stext
,
711 r
.bottom
= STATICHEIGHT
;
712 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
713 cp
->ypos
+= STATICHEIGHT
;
719 r
.bottom
= COMBOHEIGHT
*4;
720 doctl(cp
, r
, "COMBOBOX",
721 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
|
722 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
723 cp
->ypos
+= COMBOHEIGHT
+ GAPBETWEEN
;
727 * A big multiline edit control with a static labelling it.
729 void bigeditctrl(struct ctlpos
*cp
, char *stext
,
730 int sid
, int eid
, int lines
)
738 r
.bottom
= STATICHEIGHT
;
739 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
740 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
746 r
.bottom
= EDITHEIGHT
+ (lines
- 1) * STATICHEIGHT
;
747 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
749 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
| ES_MULTILINE
,
750 WS_EX_CLIENTEDGE
, "", eid
);
754 * A list box with a static labelling it.
756 void listbox(struct ctlpos
*cp
, char *stext
,
757 int sid
, int lid
, int lines
, int multi
)
765 r
.bottom
= STATICHEIGHT
;
766 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
767 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
773 r
.bottom
= LISTHEIGHT
+ (lines
- 1) * LISTINCREMENT
;
774 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
775 doctl(cp
, r
, "LISTBOX",
776 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
|
777 LBS_NOTIFY
| LBS_HASSTRINGS
| LBS_USETABSTOPS
|
778 (multi ? LBS_MULTIPLESEL
: 0),
779 WS_EX_CLIENTEDGE
, "", lid
);
783 * A tab-control substitute when a real tab control is unavailable.
785 void ersatztab(struct ctlpos
*cp
, char *stext
, int sid
, int lid
, int s2id
)
787 const int height
= (COMBOHEIGHT
> STATICHEIGHT ?
788 COMBOHEIGHT
: STATICHEIGHT
);
790 int bigwid
, lwid
, rwid
, rpos
;
791 static const int BIGGAP
= 15;
792 static const int MEDGAP
= 3;
794 bigwid
= cp
->width
+ 2 * GAPBETWEEN
- 2 * BIGGAP
;
796 rpos
= BIGGAP
+ (bigwid
+ BIGGAP
) / 2;
797 lwid
= rpos
- 2 * BIGGAP
;
798 rwid
= bigwid
+ BIGGAP
- rpos
;
801 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
803 r
.bottom
= STATICHEIGHT
;
804 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
807 r
.top
= cp
->ypos
+ (height
- COMBOHEIGHT
) / 2;
809 r
.bottom
= COMBOHEIGHT
* 10;
810 doctl(cp
, r
, "COMBOBOX",
811 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
812 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
814 cp
->ypos
+= height
+ MEDGAP
+ GAPBETWEEN
;
820 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
| SS_ETCHEDHORZ
,
825 * A static line, followed by an edit control on the left hand side
826 * and a button on the right.
828 void editbutton(struct ctlpos
*cp
, char *stext
, int sid
,
829 int eid
, char *btext
, int bid
)
831 const int height
= (EDITHEIGHT
> PUSHBTNHEIGHT ?
832 EDITHEIGHT
: PUSHBTNHEIGHT
);
834 int lwid
, rwid
, rpos
;
839 r
.bottom
= STATICHEIGHT
;
840 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
841 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
843 rpos
= GAPBETWEEN
+ 3 * (cp
->width
+ GAPBETWEEN
) / 4;
844 lwid
= rpos
- 2 * GAPBETWEEN
;
845 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
848 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
850 r
.bottom
= EDITHEIGHT
;
852 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| ES_AUTOHSCROLL
,
853 WS_EX_CLIENTEDGE
, "", eid
);
856 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
858 r
.bottom
= PUSHBTNHEIGHT
;
859 doctl(cp
, r
, "BUTTON",
860 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
863 cp
->ypos
+= height
+ GAPBETWEEN
;
867 * A special control for manipulating an ordered preference list
868 * (eg. for cipher selection).
869 * XXX: this is a rough hack and could be improved.
871 void prefslist(struct prefslist
*hdl
, struct ctlpos
*cp
, int lines
,
872 char *stext
, int sid
, int listid
, int upbid
, int dnbid
)
874 const static int percents
[] = { 5, 75, 20 };
876 int xpos
, percent
= 0, i
;
877 int listheight
= LISTHEIGHT
+ (lines
- 1) * LISTINCREMENT
;
878 const int BTNSHEIGHT
= 2*PUSHBTNHEIGHT
+ GAPBETWEEN
;
879 int totalheight
, buttonpos
;
881 /* Squirrel away IDs. */
882 hdl
->listid
= listid
;
886 /* The static label. */
891 r
.bottom
= STATICHEIGHT
;
892 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
893 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
896 if (listheight
> BTNSHEIGHT
) {
897 totalheight
= listheight
;
898 buttonpos
= (listheight
- BTNSHEIGHT
) / 2;
900 totalheight
= BTNSHEIGHT
;
904 for (i
=0; i
<3; i
++) {
906 xpos
= (cp
->width
+ GAPBETWEEN
) * percent
/ 100;
907 left
= xpos
+ GAPBETWEEN
;
908 percent
+= percents
[i
];
909 xpos
= (cp
->width
+ GAPBETWEEN
) * percent
/ 100;
914 /* The drag list box. */
915 r
.left
= left
; r
.right
= wid
;
916 r
.top
= cp
->ypos
; r
.bottom
= listheight
;
919 ctl
= doctl(cp
, r
, "LISTBOX",
920 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
921 WS_VSCROLL
| LBS_HASSTRINGS
| LBS_USETABSTOPS
,
929 /* The "Up" and "Down" buttons. */
930 /* XXX worry about accelerators if we have more than one
931 * prefslist on a panel */
932 r
.left
= left
; r
.right
= wid
;
933 r
.top
= cp
->ypos
+ buttonpos
; r
.bottom
= PUSHBTNHEIGHT
;
934 doctl(cp
, r
, "BUTTON",
935 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
|
936 WS_TABSTOP
| BS_PUSHBUTTON
,
939 r
.left
= left
; r
.right
= wid
;
940 r
.top
= cp
->ypos
+ buttonpos
+ PUSHBTNHEIGHT
+ GAPBETWEEN
;
941 r
.bottom
= PUSHBTNHEIGHT
;
942 doctl(cp
, r
, "BUTTON",
943 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
|
944 WS_TABSTOP
| BS_PUSHBUTTON
,
952 cp
->ypos
+= totalheight
+ GAPBETWEEN
;
957 * Helper function for prefslist: move item in list box.
959 static void pl_moveitem(HWND hwnd
, int listid
, int src
, int dst
)
963 /* Get the item's data. */
964 tlen
= SendDlgItemMessage (hwnd
, listid
, LB_GETTEXTLEN
, src
, 0);
965 txt
= snewn(tlen
+1, char);
966 SendDlgItemMessage (hwnd
, listid
, LB_GETTEXT
, src
, (LPARAM
) txt
);
967 val
= SendDlgItemMessage (hwnd
, listid
, LB_GETITEMDATA
, src
, 0);
968 /* Deselect old location. */
969 SendDlgItemMessage (hwnd
, listid
, LB_SETSEL
, FALSE
, src
);
970 /* Delete it at the old location. */
971 SendDlgItemMessage (hwnd
, listid
, LB_DELETESTRING
, src
, 0);
972 /* Insert it at new location. */
973 SendDlgItemMessage (hwnd
, listid
, LB_INSERTSTRING
, dst
,
975 SendDlgItemMessage (hwnd
, listid
, LB_SETITEMDATA
, dst
,
978 SendDlgItemMessage (hwnd
, listid
, LB_SETCURSEL
, dst
, 0);
982 int pl_itemfrompt(HWND hwnd
, POINT cursor
, BOOL scroll
)
985 POINT uppoint
, downpoint
;
986 int updist
, downdist
, upitem
, downitem
, i
;
989 * Ghastly hackery to try to figure out not which
990 * _item_, but which _gap between items_, the user
991 * is pointing at. We do this by first working out
992 * which list item is under the cursor, and then
993 * working out how far the cursor would have to
994 * move up or down before the answer was different.
995 * Then we put the insertion point _above_ the
996 * current item if the upper edge is closer than
997 * the lower edge, or _below_ it if vice versa.
999 ret
= LBItemFromPt(hwnd
, cursor
, scroll
);
1002 ret
= LBItemFromPt(hwnd
, cursor
, FALSE
);
1003 updist
= downdist
= 0;
1004 for (i
= 1; i
< 4096 && (!updist
|| !downdist
); i
++) {
1005 uppoint
= downpoint
= cursor
;
1008 upitem
= LBItemFromPt(hwnd
, uppoint
, FALSE
);
1009 downitem
= LBItemFromPt(hwnd
, downpoint
, FALSE
);
1010 if (!updist
&& upitem
!= ret
)
1012 if (!downdist
&& downitem
!= ret
)
1015 if (downdist
< updist
)
1021 * Handler for prefslist above.
1023 * Return value has bit 0 set if the dialog box procedure needs to
1024 * return TRUE from handling this message; it has bit 1 set if a
1025 * change may have been made in the contents of the list.
1027 int handle_prefslist(struct prefslist
*hdl
,
1028 int *array
, int maxmemb
,
1029 int is_dlmsg
, HWND hwnd
,
1030 WPARAM wParam
, LPARAM lParam
)
1037 if ((int)wParam
== hdl
->listid
) {
1038 DRAGLISTINFO
*dlm
= (DRAGLISTINFO
*)lParam
;
1039 int dest
= 0; /* initialise to placate gcc */
1040 switch (dlm
->uNotification
) {
1042 /* Add a dummy item to make pl_itemfrompt() work
1044 * FIXME: this causes scrollbar glitches if the count of
1045 * listbox contains >= its height. */
1047 SendDlgItemMessage(hwnd
, hdl
->listid
,
1048 LB_ADDSTRING
, 0, (LPARAM
) "");
1050 hdl
->srcitem
= LBItemFromPt(dlm
->hWnd
, dlm
->ptCursor
, TRUE
);
1052 /* XXX hack Q183115 */
1053 SetWindowLongPtr(hwnd
, DWLP_MSGRESULT
, TRUE
);
1056 DrawInsert(hwnd
, dlm
->hWnd
, -1); /* Clear arrow */
1057 SendDlgItemMessage(hwnd
, hdl
->listid
,
1058 LB_DELETESTRING
, hdl
->dummyitem
, 0);
1063 dest
= pl_itemfrompt(dlm
->hWnd
, dlm
->ptCursor
, TRUE
);
1064 if (dest
> hdl
->dummyitem
) dest
= hdl
->dummyitem
;
1065 DrawInsert (hwnd
, dlm
->hWnd
, dest
);
1067 SetWindowLongPtr(hwnd
, DWLP_MSGRESULT
, DL_MOVECURSOR
);
1069 SetWindowLongPtr(hwnd
, DWLP_MSGRESULT
, DL_STOPCURSOR
);
1072 if (hdl
->dragging
) {
1073 dest
= pl_itemfrompt(dlm
->hWnd
, dlm
->ptCursor
, TRUE
);
1074 if (dest
> hdl
->dummyitem
) dest
= hdl
->dummyitem
;
1075 DrawInsert (hwnd
, dlm
->hWnd
, -1);
1077 SendDlgItemMessage(hwnd
, hdl
->listid
,
1078 LB_DELETESTRING
, hdl
->dummyitem
, 0);
1079 if (hdl
->dragging
) {
1082 /* Correct for "missing" item. */
1083 if (dest
> hdl
->srcitem
) dest
--;
1084 pl_moveitem(hwnd
, hdl
->listid
, hdl
->srcitem
, dest
);
1094 if (((LOWORD(wParam
) == hdl
->upbid
) ||
1095 (LOWORD(wParam
) == hdl
->dnbid
)) &&
1096 ((HIWORD(wParam
) == BN_CLICKED
) ||
1097 (HIWORD(wParam
) == BN_DOUBLECLICKED
))) {
1098 /* Move an item up or down the list. */
1099 /* Get the current selection, if any. */
1100 int selection
= SendDlgItemMessage (hwnd
, hdl
->listid
, LB_GETCURSEL
, 0, 0);
1101 if (selection
== LB_ERR
) {
1105 /* Get the total number of items. */
1106 nitems
= SendDlgItemMessage (hwnd
, hdl
->listid
, LB_GETCOUNT
, 0, 0);
1107 /* Should we do anything? */
1108 if (LOWORD(wParam
) == hdl
->upbid
&& (selection
> 0))
1109 pl_moveitem(hwnd
, hdl
->listid
, selection
, selection
- 1);
1110 else if (LOWORD(wParam
) == hdl
->dnbid
&& (selection
< nitems
- 1))
1111 pl_moveitem(hwnd
, hdl
->listid
, selection
, selection
+ 1);
1120 /* Update array to match the list box. */
1121 for (i
=0; i
< maxmemb
; i
++)
1122 array
[i
] = SendDlgItemMessage (hwnd
, hdl
->listid
, LB_GETITEMDATA
,
1130 * A progress bar (from Common Controls). We like our progress bars
1131 * to be smooth and unbroken, without those ugly divisions; some
1132 * older compilers may not support that, but that's life.
1134 void progressbar(struct ctlpos
*cp
, int id
)
1138 r
.left
= GAPBETWEEN
;
1140 r
.right
= cp
->width
;
1141 r
.bottom
= PROGBARHEIGHT
;
1142 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
1144 doctl(cp
, r
, PROGRESS_CLASS
, WS_CHILD
| WS_VISIBLE
1148 , WS_EX_CLIENTEDGE
, "", id
);
1151 /* ----------------------------------------------------------------------
1152 * Platform-specific side of portable dialog-box mechanism.
1156 * This function takes a string, escapes all the ampersands, and
1157 * places a single (unescaped) ampersand in front of the first
1158 * occurrence of the given shortcut character (which may be
1161 * Return value is a malloc'ed copy of the processed version of the
1164 static char *shortcut_escape(char *text
, char shortcut
)
1170 return NULL
; /* sfree won't choke on this */
1172 ret
= snewn(2*strlen(text
)+1, char); /* size potentially doubles! */
1173 shortcut
= tolower((unsigned char)shortcut
);
1178 if (shortcut
!= NO_SHORTCUT
&&
1179 tolower((unsigned char)*p
) == shortcut
) {
1181 shortcut
= NO_SHORTCUT
; /* stop it happening twice */
1182 } else if (*p
== '&') {
1191 void winctrl_add_shortcuts(struct dlgparam
*dp
, struct winctrl
*c
)
1194 for (i
= 0; i
< lenof(c
->shortcuts
); i
++)
1195 if (c
->shortcuts
[i
] != NO_SHORTCUT
) {
1196 unsigned char s
= tolower((unsigned char)c
->shortcuts
[i
]);
1197 assert(!dp
->shortcuts
[s
]);
1198 dp
->shortcuts
[s
] = TRUE
;
1202 void winctrl_rem_shortcuts(struct dlgparam
*dp
, struct winctrl
*c
)
1205 for (i
= 0; i
< lenof(c
->shortcuts
); i
++)
1206 if (c
->shortcuts
[i
] != NO_SHORTCUT
) {
1207 unsigned char s
= tolower((unsigned char)c
->shortcuts
[i
]);
1208 assert(dp
->shortcuts
[s
]);
1209 dp
->shortcuts
[s
] = FALSE
;
1213 static int winctrl_cmp_byctrl(void *av
, void *bv
)
1215 struct winctrl
*a
= (struct winctrl
*)av
;
1216 struct winctrl
*b
= (struct winctrl
*)bv
;
1217 if (a
->ctrl
< b
->ctrl
)
1219 else if (a
->ctrl
> b
->ctrl
)
1224 static int winctrl_cmp_byid(void *av
, void *bv
)
1226 struct winctrl
*a
= (struct winctrl
*)av
;
1227 struct winctrl
*b
= (struct winctrl
*)bv
;
1228 if (a
->base_id
< b
->base_id
)
1230 else if (a
->base_id
> b
->base_id
)
1235 static int winctrl_cmp_byctrl_find(void *av
, void *bv
)
1237 union control
*a
= (union control
*)av
;
1238 struct winctrl
*b
= (struct winctrl
*)bv
;
1241 else if (a
> b
->ctrl
)
1246 static int winctrl_cmp_byid_find(void *av
, void *bv
)
1249 struct winctrl
*b
= (struct winctrl
*)bv
;
1250 if (*a
< b
->base_id
)
1252 else if (*a
>= b
->base_id
+ b
->num_ids
)
1258 void winctrl_init(struct winctrls
*wc
)
1260 wc
->byctrl
= newtree234(winctrl_cmp_byctrl
);
1261 wc
->byid
= newtree234(winctrl_cmp_byid
);
1263 void winctrl_cleanup(struct winctrls
*wc
)
1267 while ((c
= index234(wc
->byid
, 0)) != NULL
) {
1268 winctrl_remove(wc
, c
);
1273 freetree234(wc
->byctrl
);
1274 freetree234(wc
->byid
);
1275 wc
->byctrl
= wc
->byid
= NULL
;
1278 void winctrl_add(struct winctrls
*wc
, struct winctrl
*c
)
1280 struct winctrl
*ret
;
1282 ret
= add234(wc
->byctrl
, c
);
1285 ret
= add234(wc
->byid
, c
);
1289 void winctrl_remove(struct winctrls
*wc
, struct winctrl
*c
)
1291 struct winctrl
*ret
;
1292 ret
= del234(wc
->byctrl
, c
);
1293 ret
= del234(wc
->byid
, c
);
1297 struct winctrl
*winctrl_findbyctrl(struct winctrls
*wc
, union control
*ctrl
)
1299 return find234(wc
->byctrl
, ctrl
, winctrl_cmp_byctrl_find
);
1302 struct winctrl
*winctrl_findbyid(struct winctrls
*wc
, int id
)
1304 return find234(wc
->byid
, &id
, winctrl_cmp_byid_find
);
1307 struct winctrl
*winctrl_findbyindex(struct winctrls
*wc
, int index
)
1309 return index234(wc
->byid
, index
);
1312 void winctrl_layout(struct dlgparam
*dp
, struct winctrls
*wc
,
1313 struct ctlpos
*cp
, struct controlset
*s
, int *id
)
1315 struct ctlpos columns
[16];
1316 int ncols
, colstart
, colspan
;
1318 struct ctlpos tabdelays
[16];
1319 union control
*tabdelayed
[16];
1324 char shortcuts
[MAX_SHORTCUTS_PER_CTRL
];
1327 int i
, actual_base_id
, base_id
, num_ids
;
1332 /* Start a containing box, if we have a boxname. */
1333 if (s
->boxname
&& *s
->boxname
) {
1334 struct winctrl
*c
= snew(struct winctrl
);
1336 c
->base_id
= base_id
;
1339 memset(c
->shortcuts
, NO_SHORTCUT
, lenof(c
->shortcuts
));
1341 beginbox(cp
, s
->boxtitle
, base_id
);
1345 /* Draw a title, if we have one. */
1346 if (!s
->boxname
&& s
->boxtitle
) {
1347 struct winctrl
*c
= snew(struct winctrl
);
1349 c
->base_id
= base_id
;
1351 c
->data
= dupstr(s
->boxtitle
);
1352 memset(c
->shortcuts
, NO_SHORTCUT
, lenof(c
->shortcuts
));
1354 paneltitle(cp
, base_id
);
1358 /* Initially we have just one column. */
1360 columns
[0] = *cp
; /* structure copy */
1362 /* And initially, there are no pending tab-delayed controls. */
1365 /* Loop over each control in the controlset. */
1366 for (i
= 0; i
< s
->ncontrols
; i
++) {
1367 union control
*ctrl
= s
->ctrls
[i
];
1370 * Generic processing that pertains to all control types.
1371 * At the end of this if statement, we'll have produced
1372 * `ctrl' (a pointer to the control we have to create, or
1373 * think about creating, in this iteration of the loop),
1374 * `pos' (a suitable ctlpos with which to position it), and
1375 * `c' (a winctrl structure to receive details of the
1376 * dialog IDs). Or we'll have done a `continue', if it was
1377 * CTRL_COLUMNS and doesn't require any control creation at
1380 if (ctrl
->generic
.type
== CTRL_COLUMNS
) {
1381 assert((ctrl
->columns
.ncols
== 1) ^ (ncols
== 1));
1385 * We're splitting into multiple columns.
1387 int lpercent
, rpercent
, lx
, rx
, i
;
1389 ncols
= ctrl
->columns
.ncols
;
1390 assert(ncols
<= lenof(columns
));
1391 for (i
= 1; i
< ncols
; i
++)
1392 columns
[i
] = columns
[0]; /* structure copy */
1395 for (i
= 0; i
< ncols
; i
++) {
1396 rpercent
= lpercent
+ ctrl
->columns
.percentages
[i
];
1397 lx
= columns
[i
].xoff
+ lpercent
*
1398 (columns
[i
].width
+ GAPBETWEEN
) / 100;
1399 rx
= columns
[i
].xoff
+ rpercent
*
1400 (columns
[i
].width
+ GAPBETWEEN
) / 100;
1401 columns
[i
].xoff
= lx
;
1402 columns
[i
].width
= rx
- lx
- GAPBETWEEN
;
1403 lpercent
= rpercent
;
1407 * We're recombining the various columns into one.
1409 int maxy
= columns
[0].ypos
;
1411 for (i
= 1; i
< ncols
; i
++)
1412 if (maxy
< columns
[i
].ypos
)
1413 maxy
= columns
[i
].ypos
;
1415 columns
[0] = *cp
; /* structure copy */
1416 columns
[0].ypos
= maxy
;
1420 } else if (ctrl
->generic
.type
== CTRL_TABDELAY
) {
1423 assert(!ctrl
->generic
.tabdelay
);
1424 ctrl
= ctrl
->tabdelay
.ctrl
;
1426 for (i
= 0; i
< ntabdelays
; i
++)
1427 if (tabdelayed
[i
] == ctrl
)
1429 assert(i
< ntabdelays
); /* we have to have found it */
1431 pos
= tabdelays
[i
]; /* structure copy */
1433 colstart
= colspan
= -1; /* indicate this was tab-delayed */
1437 * If it wasn't one of those, it's a genuine control;
1438 * so we'll have to compute a position for it now, by
1439 * checking its column span.
1443 colstart
= COLUMN_START(ctrl
->generic
.column
);
1444 colspan
= COLUMN_SPAN(ctrl
->generic
.column
);
1446 pos
= columns
[colstart
]; /* structure copy */
1447 pos
.width
= columns
[colstart
+colspan
-1].width
+
1448 (columns
[colstart
+colspan
-1].xoff
- columns
[colstart
].xoff
);
1450 for (col
= colstart
; col
< colstart
+colspan
; col
++)
1451 if (pos
.ypos
< columns
[col
].ypos
)
1452 pos
.ypos
= columns
[col
].ypos
;
1455 * If this control is to be tabdelayed, add it to the
1456 * tabdelay list, and unset pos.hwnd to inhibit actual
1459 if (ctrl
->generic
.tabdelay
) {
1460 assert(ntabdelays
< lenof(tabdelays
));
1461 tabdelays
[ntabdelays
] = pos
; /* structure copy */
1462 tabdelayed
[ntabdelays
] = ctrl
;
1468 /* Most controls don't need anything in c->data. */
1471 /* And they all start off with no shortcuts registered. */
1472 memset(shortcuts
, NO_SHORTCUT
, lenof(shortcuts
));
1475 /* Almost all controls start at base_id. */
1476 actual_base_id
= base_id
;
1479 * Now we're ready to actually create the control, by
1480 * switching on its type.
1482 switch (ctrl
->generic
.type
) {
1485 char *wrapped
, *escaped
;
1488 wrapped
= staticwrap(&pos
, cp
->hwnd
,
1489 ctrl
->generic
.label
, &lines
);
1490 escaped
= shortcut_escape(wrapped
, NO_SHORTCUT
);
1491 statictext(&pos
, escaped
, lines
, base_id
);
1497 num_ids
= 2; /* static, edit */
1498 escaped
= shortcut_escape(ctrl
->editbox
.label
,
1499 ctrl
->editbox
.shortcut
);
1500 shortcuts
[nshortcuts
++] = ctrl
->editbox
.shortcut
;
1501 if (ctrl
->editbox
.percentwidth
== 100) {
1502 if (ctrl
->editbox
.has_list
)
1503 combobox(&pos
, escaped
,
1504 base_id
, base_id
+1);
1506 editboxfw(&pos
, ctrl
->editbox
.password
, escaped
,
1507 base_id
, base_id
+1);
1509 if (ctrl
->editbox
.has_list
) {
1510 staticcombo(&pos
, escaped
, base_id
, base_id
+1,
1511 ctrl
->editbox
.percentwidth
);
1513 (ctrl
->editbox
.password ? staticpassedit
: staticedit
)
1514 (&pos
, escaped
, base_id
, base_id
+1,
1515 ctrl
->editbox
.percentwidth
);
1521 num_ids
= ctrl
->radio
.nbuttons
+ 1; /* label as well */
1523 struct radio
*buttons
;
1526 escaped
= shortcut_escape(ctrl
->radio
.label
,
1527 ctrl
->radio
.shortcut
);
1528 shortcuts
[nshortcuts
++] = ctrl
->radio
.shortcut
;
1530 buttons
= snewn(ctrl
->radio
.nbuttons
, struct radio
);
1532 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1534 shortcut_escape(ctrl
->radio
.buttons
[i
],
1535 (char)(ctrl
->radio
.shortcuts ?
1536 ctrl
->radio
.shortcuts
[i
] :
1538 buttons
[i
].id
= base_id
+ 1 + i
;
1539 if (ctrl
->radio
.shortcuts
) {
1540 assert(nshortcuts
< MAX_SHORTCUTS_PER_CTRL
);
1541 shortcuts
[nshortcuts
++] = ctrl
->radio
.shortcuts
[i
];
1545 radioline_common(&pos
, escaped
, base_id
,
1546 ctrl
->radio
.ncolumns
,
1547 buttons
, ctrl
->radio
.nbuttons
);
1549 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1550 sfree(buttons
[i
].text
);
1558 escaped
= shortcut_escape(ctrl
->checkbox
.label
,
1559 ctrl
->checkbox
.shortcut
);
1560 shortcuts
[nshortcuts
++] = ctrl
->checkbox
.shortcut
;
1561 checkbox(&pos
, escaped
, base_id
);
1565 escaped
= shortcut_escape(ctrl
->button
.label
,
1566 ctrl
->button
.shortcut
);
1567 shortcuts
[nshortcuts
++] = ctrl
->button
.shortcut
;
1568 if (ctrl
->button
.iscancel
)
1569 actual_base_id
= IDCANCEL
;
1571 button(&pos
, escaped
, actual_base_id
, ctrl
->button
.isdefault
);
1576 escaped
= shortcut_escape(ctrl
->listbox
.label
,
1577 ctrl
->listbox
.shortcut
);
1578 shortcuts
[nshortcuts
++] = ctrl
->listbox
.shortcut
;
1579 if (ctrl
->listbox
.draglist
) {
1580 data
= snew(struct prefslist
);
1582 prefslist(data
, &pos
, ctrl
->listbox
.height
, escaped
,
1583 base_id
, base_id
+1, base_id
+2, base_id
+3);
1584 shortcuts
[nshortcuts
++] = 'u'; /* Up */
1585 shortcuts
[nshortcuts
++] = 'd'; /* Down */
1586 } else if (ctrl
->listbox
.height
== 0) {
1587 /* Drop-down list. */
1588 if (ctrl
->listbox
.percentwidth
== 100) {
1589 staticddlbig(&pos
, escaped
,
1590 base_id
, base_id
+1);
1592 staticddl(&pos
, escaped
, base_id
,
1593 base_id
+1, ctrl
->listbox
.percentwidth
);
1596 /* Ordinary list. */
1597 listbox(&pos
, escaped
, base_id
, base_id
+1,
1598 ctrl
->listbox
.height
, ctrl
->listbox
.multisel
);
1600 if (ctrl
->listbox
.ncols
) {
1602 * This method of getting the box width is a bit of
1603 * a hack; we'd do better to try to retrieve the
1604 * actual width in dialog units from doctl() just
1605 * before MapDialogRect. But that's going to be no
1606 * fun, and this should be good enough accuracy.
1608 int width
= cp
->width
* ctrl
->listbox
.percentwidth
;
1612 tabarray
= snewn(ctrl
->listbox
.ncols
-1, int);
1614 for (i
= 0; i
< ctrl
->listbox
.ncols
-1; i
++) {
1615 percent
+= ctrl
->listbox
.percentages
[i
];
1616 tabarray
[i
] = width
* percent
/ 10000;
1618 SendDlgItemMessage(cp
->hwnd
, base_id
+1, LB_SETTABSTOPS
,
1619 ctrl
->listbox
.ncols
-1, (LPARAM
)tabarray
);
1624 case CTRL_FILESELECT
:
1626 escaped
= shortcut_escape(ctrl
->fileselect
.label
,
1627 ctrl
->fileselect
.shortcut
);
1628 shortcuts
[nshortcuts
++] = ctrl
->fileselect
.shortcut
;
1629 editbutton(&pos
, escaped
, base_id
, base_id
+1,
1630 "Bro&wse...", base_id
+2);
1631 shortcuts
[nshortcuts
++] = 'w';
1634 case CTRL_FONTSELECT
:
1636 escaped
= shortcut_escape(ctrl
->fontselect
.label
,
1637 ctrl
->fontselect
.shortcut
);
1638 shortcuts
[nshortcuts
++] = ctrl
->fontselect
.shortcut
;
1639 statictext(&pos
, escaped
, 1, base_id
);
1640 staticbtn(&pos
, "", base_id
+1, "Change...", base_id
+2);
1642 data
= snew(FontSpec
);
1645 assert(!"Can't happen");
1646 num_ids
= 0; /* placate gcc */
1651 * Create a `struct winctrl' for this control, and advance
1652 * the dialog ID counter, if it's actually been created
1653 * (and isn't tabdelayed).
1656 struct winctrl
*c
= snew(struct winctrl
);
1659 c
->base_id
= actual_base_id
;
1660 c
->num_ids
= num_ids
;
1662 memcpy(c
->shortcuts
, shortcuts
, sizeof(shortcuts
));
1664 winctrl_add_shortcuts(dp
, c
);
1665 if (actual_base_id
== base_id
)
1669 if (colstart
>= 0) {
1671 * Update the ypos in all columns crossed by this
1675 for (i
= colstart
; i
< colstart
+colspan
; i
++)
1676 columns
[i
].ypos
= pos
.ypos
;
1681 * We've now finished laying out the controls; so now update
1682 * the ctlpos and control ID that were passed in, terminate
1683 * any containing box, and return.
1685 for (i
= 0; i
< ncols
; i
++)
1686 if (cp
->ypos
< columns
[i
].ypos
)
1687 cp
->ypos
= columns
[i
].ypos
;
1690 if (s
->boxname
&& *s
->boxname
)
1694 static void winctrl_set_focus(union control
*ctrl
, struct dlgparam
*dp
,
1699 dp
->lastfocused
= dp
->focused
;
1701 } else if (!has_focus
&& dp
->focused
== ctrl
) {
1702 dp
->lastfocused
= dp
->focused
;
1707 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1709 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1710 return dp
->focused
== ctrl ? dp
->lastfocused
: dp
->focused
;
1714 * The dialog-box procedure calls this function to handle Windows
1715 * messages on a control we manage.
1717 int winctrl_handle_command(struct dlgparam
*dp
, UINT msg
,
1718 WPARAM wParam
, LPARAM lParam
)
1721 union control
*ctrl
;
1723 static UINT draglistmsg
= WM_NULL
;
1726 * Filter out pointless window messages. Our interest is in
1727 * WM_COMMAND and the drag list message, and nothing else.
1729 if (draglistmsg
== WM_NULL
)
1730 draglistmsg
= RegisterWindowMessage (DRAGLISTMSGSTRING
);
1732 if (msg
!= draglistmsg
&& msg
!= WM_COMMAND
&& msg
!= WM_DRAWITEM
)
1736 * Look up the control ID in our data.
1739 for (i
= 0; i
< dp
->nctrltrees
; i
++) {
1740 c
= winctrl_findbyid(dp
->controltrees
[i
], LOWORD(wParam
));
1745 return 0; /* we have nothing to do */
1747 if (msg
== WM_DRAWITEM
) {
1749 * Owner-draw request for a panel title.
1751 LPDRAWITEMSTRUCT di
= (LPDRAWITEMSTRUCT
) lParam
;
1753 RECT r
= di
->rcItem
;
1756 SetMapMode(hdc
, MM_TEXT
); /* ensure logical units == pixels */
1758 GetTextExtentPoint32(hdc
, (char *)c
->data
,
1759 strlen((char *)c
->data
), &s
);
1760 DrawEdge(hdc
, &r
, EDGE_ETCHED
, BF_ADJUST
| BF_RECT
);
1762 r
.left
+ (r
.right
-r
.left
-s
.cx
)/2,
1763 r
.top
+ (r
.bottom
-r
.top
-s
.cy
)/2,
1764 (char *)c
->data
, strlen((char *)c
->data
));
1770 id
= LOWORD(wParam
) - c
->base_id
;
1772 if (!ctrl
|| !ctrl
->generic
.handler
)
1773 return 0; /* nothing we can do here */
1776 * From here on we do not issue `return' statements until the
1777 * very end of the dialog box: any event handler is entitled to
1778 * ask for a colour selector, so we _must_ always allow control
1779 * to reach the end of this switch statement so that the
1780 * subsequent code can test dp->coloursel_wanted().
1783 dp
->coloursel_wanted
= FALSE
;
1786 * Now switch on the control type and the message.
1788 switch (ctrl
->generic
.type
) {
1790 if (msg
== WM_COMMAND
&& !ctrl
->editbox
.has_list
&&
1791 (HIWORD(wParam
) == EN_SETFOCUS
|| HIWORD(wParam
) == EN_KILLFOCUS
))
1792 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == EN_SETFOCUS
);
1793 if (msg
== WM_COMMAND
&& ctrl
->editbox
.has_list
&&
1794 (HIWORD(wParam
)==CBN_SETFOCUS
|| HIWORD(wParam
)==CBN_KILLFOCUS
))
1795 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == CBN_SETFOCUS
);
1797 if (msg
== WM_COMMAND
&& !ctrl
->editbox
.has_list
&&
1798 HIWORD(wParam
) == EN_CHANGE
)
1799 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1800 if (msg
== WM_COMMAND
&&
1801 ctrl
->editbox
.has_list
) {
1802 if (HIWORD(wParam
) == CBN_SELCHANGE
) {
1806 index
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1,
1807 CB_GETCURSEL
, 0, 0);
1808 len
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1,
1809 CB_GETLBTEXTLEN
, index
, 0);
1810 text
= snewn(len
+1, char);
1811 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, CB_GETLBTEXT
,
1812 index
, (LPARAM
)text
);
1813 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, text
);
1815 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1816 } else if (HIWORD(wParam
) == CBN_EDITCHANGE
) {
1817 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1818 } else if (HIWORD(wParam
) == CBN_KILLFOCUS
) {
1819 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
1825 if (msg
== WM_COMMAND
&&
1826 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1827 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1829 * We sometimes get spurious BN_CLICKED messages for the
1830 * radio button that is just about to _lose_ selection, if
1831 * we're switching using the arrow keys. Therefore we
1832 * double-check that the button in wParam is actually
1833 * checked before generating an event.
1835 if (msg
== WM_COMMAND
&&
1836 (HIWORD(wParam
) == BN_CLICKED
||
1837 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
1838 IsDlgButtonChecked(dp
->hwnd
, LOWORD(wParam
))) {
1839 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1843 if (msg
== WM_COMMAND
&&
1844 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1845 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1846 if (msg
== WM_COMMAND
&&
1847 (HIWORD(wParam
) == BN_CLICKED
||
1848 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
1849 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1853 if (msg
== WM_COMMAND
&&
1854 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1855 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1856 if (msg
== WM_COMMAND
&&
1857 (HIWORD(wParam
) == BN_CLICKED
||
1858 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
1859 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1863 if (msg
== WM_COMMAND
&& ctrl
->listbox
.height
!= 0 &&
1864 (HIWORD(wParam
)==LBN_SETFOCUS
|| HIWORD(wParam
)==LBN_KILLFOCUS
))
1865 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == LBN_SETFOCUS
);
1866 if (msg
== WM_COMMAND
&& ctrl
->listbox
.height
== 0 &&
1867 (HIWORD(wParam
)==CBN_SETFOCUS
|| HIWORD(wParam
)==CBN_KILLFOCUS
))
1868 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == CBN_SETFOCUS
);
1869 if (msg
== WM_COMMAND
&& id
>= 2 &&
1870 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1871 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1872 if (ctrl
->listbox
.draglist
) {
1874 pret
= handle_prefslist(c
->data
, NULL
, 0, (msg
!= WM_COMMAND
),
1875 dp
->hwnd
, wParam
, lParam
);
1877 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1880 if (msg
== WM_COMMAND
&& HIWORD(wParam
) == LBN_DBLCLK
) {
1881 SetCapture(dp
->hwnd
);
1882 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1883 } else if (msg
== WM_COMMAND
&& HIWORD(wParam
) == LBN_SELCHANGE
) {
1884 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1888 case CTRL_FILESELECT
:
1889 if (msg
== WM_COMMAND
&& id
== 1 &&
1890 (HIWORD(wParam
) == EN_SETFOCUS
|| HIWORD(wParam
) == EN_KILLFOCUS
))
1891 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == EN_SETFOCUS
);
1892 if (msg
== WM_COMMAND
&& id
== 2 &&
1893 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1894 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1895 if (msg
== WM_COMMAND
&& id
== 1 && HIWORD(wParam
) == EN_CHANGE
)
1896 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1898 (msg
== WM_COMMAND
&&
1899 (HIWORD(wParam
) == BN_CLICKED
||
1900 HIWORD(wParam
) == BN_DOUBLECLICKED
))) {
1902 char filename
[FILENAME_MAX
];
1904 memset(&of
, 0, sizeof(of
));
1905 of
.hwndOwner
= dp
->hwnd
;
1906 if (ctrl
->fileselect
.filter
)
1907 of
.lpstrFilter
= ctrl
->fileselect
.filter
;
1909 of
.lpstrFilter
= "All Files (*.*)\0*\0\0\0";
1910 of
.lpstrCustomFilter
= NULL
;
1911 of
.nFilterIndex
= 1;
1912 of
.lpstrFile
= filename
;
1913 GetDlgItemText(dp
->hwnd
, c
->base_id
+1, filename
, lenof(filename
));
1914 filename
[lenof(filename
)-1] = '\0';
1915 of
.nMaxFile
= lenof(filename
);
1916 of
.lpstrFileTitle
= NULL
;
1917 of
.lpstrTitle
= ctrl
->fileselect
.title
;
1919 if (request_file(NULL
, &of
, FALSE
, ctrl
->fileselect
.for_writing
)) {
1920 SetDlgItemText(dp
->hwnd
, c
->base_id
+ 1, filename
);
1921 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1925 case CTRL_FONTSELECT
:
1926 if (msg
== WM_COMMAND
&& id
== 2 &&
1927 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1928 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1930 (msg
== WM_COMMAND
&&
1931 (HIWORD(wParam
) == BN_CLICKED
||
1932 HIWORD(wParam
) == BN_DOUBLECLICKED
))) {
1936 FontSpec fs
= *(FontSpec
*)c
->data
;
1939 lf
.lfHeight
= -MulDiv(fs
.height
,
1940 GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
1942 lf
.lfWidth
= lf
.lfEscapement
= lf
.lfOrientation
= 0;
1943 lf
.lfItalic
= lf
.lfUnderline
= lf
.lfStrikeOut
= 0;
1944 lf
.lfWeight
= (fs
.isbold ? FW_BOLD
: 0);
1945 lf
.lfCharSet
= fs
.charset
;
1946 lf
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
1947 lf
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
1948 lf
.lfQuality
= DEFAULT_QUALITY
;
1949 lf
.lfPitchAndFamily
= FIXED_PITCH
| FF_DONTCARE
;
1950 strncpy(lf
.lfFaceName
, fs
.name
,
1951 sizeof(lf
.lfFaceName
) - 1);
1952 lf
.lfFaceName
[sizeof(lf
.lfFaceName
) - 1] = '\0';
1954 cf
.lStructSize
= sizeof(cf
);
1955 cf
.hwndOwner
= dp
->hwnd
;
1957 cf
.Flags
= CF_FIXEDPITCHONLY
| CF_FORCEFONTEXIST
|
1958 CF_INITTOLOGFONTSTRUCT
| CF_SCREENFONTS
;
1960 if (ChooseFont(&cf
)) {
1961 strncpy(fs
.name
, lf
.lfFaceName
,
1962 sizeof(fs
.name
) - 1);
1963 fs
.name
[sizeof(fs
.name
) - 1] = '\0';
1964 fs
.isbold
= (lf
.lfWeight
== FW_BOLD
);
1965 fs
.charset
= lf
.lfCharSet
;
1966 fs
.height
= cf
.iPointSize
/ 10;
1967 dlg_fontsel_set(ctrl
, dp
, fs
);
1968 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1975 * If the above event handler has asked for a colour selector,
1976 * now is the time to generate one.
1978 if (dp
->coloursel_wanted
) {
1979 static CHOOSECOLOR cc
;
1980 static DWORD custom
[16] = { 0 }; /* zero initialisers */
1981 cc
.lStructSize
= sizeof(cc
);
1982 cc
.hwndOwner
= dp
->hwnd
;
1983 cc
.hInstance
= (HWND
) hinst
;
1984 cc
.lpCustColors
= custom
;
1985 cc
.rgbResult
= RGB(dp
->coloursel_result
.r
,
1986 dp
->coloursel_result
.g
,
1987 dp
->coloursel_result
.b
);
1988 cc
.Flags
= CC_FULLOPEN
| CC_RGBINIT
;
1989 if (ChooseColor(&cc
)) {
1990 dp
->coloursel_result
.r
=
1991 (unsigned char) (cc
.rgbResult
& 0xFF);
1992 dp
->coloursel_result
.g
=
1993 (unsigned char) (cc
.rgbResult
>> 8) & 0xFF;
1994 dp
->coloursel_result
.b
=
1995 (unsigned char) (cc
.rgbResult
>> 16) & 0xFF;
1996 dp
->coloursel_result
.ok
= TRUE
;
1998 dp
->coloursel_result
.ok
= FALSE
;
1999 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
2006 * This function can be called to produce context help on a
2007 * control. Returns TRUE if it has actually launched WinHelp.
2009 int winctrl_context_help(struct dlgparam
*dp
, HWND hwnd
, int id
)
2016 * Look up the control ID in our data.
2019 for (i
= 0; i
< dp
->nctrltrees
; i
++) {
2020 c
= winctrl_findbyid(dp
->controltrees
[i
], id
);
2025 return 0; /* we have nothing to do */
2028 * This is the Windows front end, so we're allowed to assume
2029 * `helpctx.p' is a context string.
2031 if (!c
->ctrl
|| !c
->ctrl
->generic
.helpctx
.p
)
2032 return 0; /* no help available for this ctrl */
2034 cmd
= dupprintf("JI(`',`%s')", c
->ctrl
->generic
.helpctx
.p
);
2035 WinHelp(hwnd
, help_path
, HELP_COMMAND
, (DWORD
)cmd
);
2041 * Now the various functions that the platform-independent
2042 * mechanism can call to access the dialog box entries.
2045 static struct winctrl
*dlg_findbyctrl(struct dlgparam
*dp
, union control
*ctrl
)
2049 for (i
= 0; i
< dp
->nctrltrees
; i
++) {
2050 struct winctrl
*c
= winctrl_findbyctrl(dp
->controltrees
[i
], ctrl
);
2057 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
2059 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2060 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2061 assert(c
&& c
->ctrl
->generic
.type
== CTRL_RADIO
);
2062 CheckRadioButton(dp
->hwnd
,
2064 c
->base_id
+ c
->ctrl
->radio
.nbuttons
,
2065 c
->base_id
+ 1 + whichbutton
);
2068 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
2070 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2071 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2073 assert(c
&& c
->ctrl
->generic
.type
== CTRL_RADIO
);
2074 for (i
= 0; i
< c
->ctrl
->radio
.nbuttons
; i
++)
2075 if (IsDlgButtonChecked(dp
->hwnd
, c
->base_id
+ 1 + i
))
2077 assert(!"No radio button was checked?!");
2081 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
2083 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2084 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2085 assert(c
&& c
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
2086 CheckDlgButton(dp
->hwnd
, c
->base_id
, (checked
!= 0));
2089 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
2091 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2092 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2093 assert(c
&& c
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
2094 return 0 != IsDlgButtonChecked(dp
->hwnd
, c
->base_id
);
2097 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
2099 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2100 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2101 assert(c
&& c
->ctrl
->generic
.type
== CTRL_EDITBOX
);
2102 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, text
);
2105 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
2107 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2108 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2109 assert(c
&& c
->ctrl
->generic
.type
== CTRL_EDITBOX
);
2110 GetDlgItemText(dp
->hwnd
, c
->base_id
+1, buffer
, length
);
2111 buffer
[length
-1] = '\0';
2114 /* The `listbox' functions can also apply to combo boxes. */
2115 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
2117 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2118 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2121 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2122 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2123 c
->ctrl
->editbox
.has_list
)));
2124 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2125 LB_RESETCONTENT
: CB_RESETCONTENT
);
2126 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, 0);
2129 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
2131 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2132 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2135 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2136 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2137 c
->ctrl
->editbox
.has_list
)));
2138 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2139 LB_DELETESTRING
: CB_DELETESTRING
);
2140 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, index
, 0);
2143 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
2145 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2146 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2149 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2150 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2151 c
->ctrl
->editbox
.has_list
)));
2152 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2153 LB_ADDSTRING
: CB_ADDSTRING
);
2154 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, (LPARAM
)text
);
2158 * Each listbox entry may have a numeric id associated with it.
2159 * Note that some front ends only permit a string to be stored at
2160 * each position, which means that _if_ you put two identical
2161 * strings in any listbox then you MUST not assign them different
2162 * IDs and expect to get meaningful results back.
2164 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
2165 char const *text
, int id
)
2167 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2168 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2169 int msg
, msg2
, index
;
2171 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2172 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2173 c
->ctrl
->editbox
.has_list
)));
2174 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2175 LB_ADDSTRING
: CB_ADDSTRING
);
2176 msg2
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2177 LB_SETITEMDATA
: CB_SETITEMDATA
);
2178 index
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, (LPARAM
)text
);
2179 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg2
, index
, (LPARAM
)id
);
2182 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
2184 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2185 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2187 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
);
2188 msg
= (c
->ctrl
->listbox
.height
!= 0 ? LB_GETITEMDATA
: CB_GETITEMDATA
);
2190 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, index
, 0);
2193 /* dlg_listbox_index returns <0 if no single element is selected. */
2194 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
2196 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2197 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2199 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
&&
2200 !c
->ctrl
->listbox
.multisel
);
2201 msg
= (c
->ctrl
->listbox
.height
!= 0 ? LB_GETCURSEL
: CB_GETCURSEL
);
2202 ret
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, 0);
2209 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
2211 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2212 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2213 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
&&
2214 c
->ctrl
->listbox
.multisel
&&
2215 c
->ctrl
->listbox
.height
!= 0);
2217 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, LB_GETSEL
, index
, 0);
2220 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
2222 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2223 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2225 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
&&
2226 !c
->ctrl
->listbox
.multisel
);
2227 msg
= (c
->ctrl
->listbox
.height
!= 0 ? LB_SETCURSEL
: CB_SETCURSEL
);
2228 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, index
, 0);
2231 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
2233 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2234 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2235 assert(c
&& c
->ctrl
->generic
.type
== CTRL_TEXT
);
2236 SetDlgItemText(dp
->hwnd
, c
->base_id
, text
);
2239 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
2241 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2242 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2243 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FILESELECT
);
2244 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, fn
.path
);
2247 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
2249 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2250 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2251 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FILESELECT
);
2252 GetDlgItemText(dp
->hwnd
, c
->base_id
+1, fn
->path
, lenof(fn
->path
));
2253 fn
->path
[lenof(fn
->path
)-1] = '\0';
2256 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
2258 char *buf
, *boldstr
;
2259 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2260 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2261 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
2263 *(FontSpec
*)c
->data
= fs
; /* structure copy */
2265 boldstr
= (fs
.isbold ?
"bold, " : "");
2267 buf
= dupprintf("Font: %s, %sdefault height", fs
.name
, boldstr
);
2269 buf
= dupprintf("Font: %s, %s%d-%s", fs
.name
, boldstr
,
2270 (fs
.height
< 0 ?
-fs
.height
: fs
.height
),
2271 (fs
.height
< 0 ?
"pixel" : "point"));
2272 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, buf
);
2276 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
2278 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2279 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2280 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
2281 *fs
= *(FontSpec
*)c
->data
; /* structure copy */
2285 * Bracketing a large set of updates in these two functions will
2286 * cause the front end (if possible) to delay updating the screen
2287 * until it's all complete, thus avoiding flicker.
2289 void dlg_update_start(union control
*ctrl
, void *dlg
)
2291 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2292 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2293 if (c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
) {
2294 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, WM_SETREDRAW
, FALSE
, 0);
2298 void dlg_update_done(union control
*ctrl
, void *dlg
)
2300 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2301 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2302 if (c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
) {
2303 HWND hw
= GetDlgItem(dp
->hwnd
, c
->base_id
+1);
2304 SendMessage(hw
, WM_SETREDRAW
, TRUE
, 0);
2305 InvalidateRect(hw
, NULL
, TRUE
);
2309 void dlg_set_focus(union control
*ctrl
, void *dlg
)
2311 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2312 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2315 switch (ctrl
->generic
.type
) {
2316 case CTRL_EDITBOX
: id
= c
->base_id
+ 1; break;
2318 for (id
= c
->base_id
+ ctrl
->radio
.nbuttons
; id
> 1; id
--)
2319 if (IsDlgButtonChecked(dp
->hwnd
, id
))
2322 * In the theoretically-unlikely case that no button was
2323 * selected, id should come out of this as 1, which is a
2324 * reasonable enough choice.
2327 case CTRL_CHECKBOX
: id
= c
->base_id
; break;
2328 case CTRL_BUTTON
: id
= c
->base_id
; break;
2329 case CTRL_LISTBOX
: id
= c
->base_id
+ 1; break;
2330 case CTRL_FILESELECT
: id
= c
->base_id
+ 1; break;
2331 case CTRL_FONTSELECT
: id
= c
->base_id
+ 2; break;
2332 default: id
= c
->base_id
; break;
2334 ctl
= GetDlgItem(dp
->hwnd
, id
);
2339 * During event processing, you might well want to give an error
2340 * indication to the user. dlg_beep() is a quick and easy generic
2341 * error; dlg_error() puts up a message-box or equivalent.
2343 void dlg_beep(void *dlg
)
2345 /* struct dlgparam *dp = (struct dlgparam *)dlg; */
2349 void dlg_error_msg(void *dlg
, char *msg
)
2351 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2352 MessageBox(dp
->hwnd
, msg
,
2353 dp
->errtitle ? dp
->errtitle
: NULL
,
2354 MB_OK
| MB_ICONERROR
);
2358 * This function signals to the front end that the dialog's
2359 * processing is completed, and passes an integer value (typically
2360 * a success status).
2362 void dlg_end(void *dlg
, int value
)
2364 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2366 dp
->endresult
= value
;
2369 void dlg_refresh(union control
*ctrl
, void *dlg
)
2371 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2377 * Send EVENT_REFRESH to absolutely everything.
2379 for (j
= 0; j
< dp
->nctrltrees
; j
++) {
2381 (c
= winctrl_findbyindex(dp
->controltrees
[j
], i
)) != NULL
;
2383 if (c
->ctrl
&& c
->ctrl
->generic
.handler
!= NULL
)
2384 c
->ctrl
->generic
.handler(c
->ctrl
, dp
,
2385 dp
->data
, EVENT_REFRESH
);
2390 * Send EVENT_REFRESH to a specific control.
2392 if (ctrl
->generic
.handler
!= NULL
)
2393 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
2397 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
2399 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2400 dp
->coloursel_wanted
= TRUE
;
2401 dp
->coloursel_result
.r
= r
;
2402 dp
->coloursel_result
.g
= g
;
2403 dp
->coloursel_result
.b
= b
;
2406 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
2407 int *r
, int *g
, int *b
)
2409 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2410 if (dp
->coloursel_result
.ok
) {
2411 *r
= dp
->coloursel_result
.r
;
2412 *g
= dp
->coloursel_result
.g
;
2413 *b
= dp
->coloursel_result
.b
;
2419 struct perctrl_privdata
{
2420 union control
*ctrl
;
2425 static int perctrl_privdata_cmp(void *av
, void *bv
)
2427 struct perctrl_privdata
*a
= (struct perctrl_privdata
*)av
;
2428 struct perctrl_privdata
*b
= (struct perctrl_privdata
*)bv
;
2429 if (a
->ctrl
< b
->ctrl
)
2431 else if (a
->ctrl
> b
->ctrl
)
2436 void dp_init(struct dlgparam
*dp
)
2441 dp
->focused
= dp
->lastfocused
= NULL
;
2442 memset(dp
->shortcuts
, 0, sizeof(dp
->shortcuts
));
2444 dp
->wintitle
= dp
->errtitle
= NULL
;
2445 dp
->privdata
= newtree234(perctrl_privdata_cmp
);
2448 void dp_add_tree(struct dlgparam
*dp
, struct winctrls
*wc
)
2450 assert(dp
->nctrltrees
< lenof(dp
->controltrees
));
2451 dp
->controltrees
[dp
->nctrltrees
++] = wc
;
2454 void dp_cleanup(struct dlgparam
*dp
)
2456 struct perctrl_privdata
*p
;
2459 while ( (p
= index234(dp
->privdata
, 0)) != NULL
) {
2460 del234(dp
->privdata
, p
);
2465 freetree234(dp
->privdata
);
2466 dp
->privdata
= NULL
;
2468 sfree(dp
->wintitle
);
2469 sfree(dp
->errtitle
);
2472 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
2474 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2475 struct perctrl_privdata tmp
, *p
;
2477 p
= find234(dp
->privdata
, &tmp
, NULL
);
2484 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
2486 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2487 struct perctrl_privdata tmp
, *p
;
2489 p
= find234(dp
->privdata
, &tmp
, NULL
);
2491 p
= snew(struct perctrl_privdata
);
2493 p
->needs_free
= FALSE
;
2494 add234(dp
->privdata
, p
);
2499 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
2501 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2502 struct perctrl_privdata tmp
, *p
;
2504 p
= find234(dp
->privdata
, &tmp
, NULL
);
2506 p
= snew(struct perctrl_privdata
);
2508 p
->needs_free
= FALSE
;
2509 add234(dp
->privdata
, p
);
2511 assert(!p
->needs_free
);
2512 p
->needs_free
= TRUE
;
2514 * This is an internal allocation routine, so it's allowed to
2515 * use smalloc directly.
2517 p
->data
= smalloc(size
);