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);
293 buttons
= snewn(nbuttons
, struct radio
);
294 va_start(ap
, nacross
);
295 for (i
= 0; i
< nbuttons
; i
++) {
296 buttons
[i
].text
= va_arg(ap
, char *);
297 buttons
[i
].id
= va_arg(ap
, int);
300 radioline_common(cp
, text
, id
, nacross
, buttons
, nbuttons
);
305 * A set of radio buttons on the same line, without a static above
306 * them. Otherwise just like radioline.
308 void bareradioline(struct ctlpos
*cp
, int nacross
, ...)
311 struct radio
*buttons
;
314 va_start(ap
, nacross
);
317 char *btext
= va_arg(ap
, char *);
321 bid
= va_arg(ap
, int);
324 buttons
= snewn(nbuttons
, struct radio
);
325 va_start(ap
, nacross
);
326 for (i
= 0; i
< nbuttons
; i
++) {
327 buttons
[i
].text
= va_arg(ap
, char *);
328 buttons
[i
].id
= va_arg(ap
, int);
331 radioline_common(cp
, NULL
, 0, nacross
, buttons
, nbuttons
);
336 * A set of radio buttons on multiple lines, with a static above
339 void radiobig(struct ctlpos
*cp
, char *text
, int id
, ...)
342 struct radio
*buttons
;
348 char *btext
= va_arg(ap
, char *);
352 bid
= va_arg(ap
, int);
355 buttons
= snewn(nbuttons
, struct radio
);
357 for (i
= 0; i
< nbuttons
; i
++) {
358 buttons
[i
].text
= va_arg(ap
, char *);
359 buttons
[i
].id
= va_arg(ap
, int);
362 radioline_common(cp
, text
, id
, 1, buttons
, nbuttons
);
367 * A single standalone checkbox.
369 void checkbox(struct ctlpos
*cp
, char *text
, int id
)
376 r
.bottom
= CHECKBOXHEIGHT
;
377 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
378 doctl(cp
, r
, "BUTTON",
379 BS_NOTIFY
| BS_AUTOCHECKBOX
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
, 0,
384 * Wrap a piece of text for a static text control. Returns the
385 * wrapped text (a malloc'ed string containing \ns), and also
386 * returns the number of lines required.
388 char *staticwrap(struct ctlpos
*cp
, HWND hwnd
, char *text
, int *lines
)
390 HDC hdc
= GetDC(hwnd
);
391 int lpx
= GetDeviceCaps(hdc
, LOGPIXELSX
);
392 int width
, nlines
, j
;
397 HFONT oldfont
, newfont
;
399 ret
= snewn(1+strlen(text
), char);
402 pwidths
= snewn(1+strlen(text
), INT
);
405 * Work out the width the text will need to fit in, by doing
406 * the same adjustment that the `statictext' function itself
409 SetMapMode(hdc
, MM_TEXT
); /* ensure logical units == pixels */
410 r
.left
= r
.top
= r
.bottom
= 0;
412 MapDialogRect(hwnd
, &r
);
418 * We must select the correct font into the HDC before calling
419 * GetTextExtent*, or silly things will happen.
421 newfont
= (HFONT
)SendMessage(hwnd
, WM_GETFONT
, 0, 0);
422 oldfont
= SelectObject(hdc
, newfont
);
425 if (!GetTextExtentExPoint(hdc
, p
, strlen(p
), width
,
426 &nfit
, pwidths
, &size
) ||
427 (size_t)nfit
>= strlen(p
)) {
429 * Either GetTextExtentExPoint returned failure, or the
430 * whole of the rest of the text fits on this line.
431 * Either way, we stop wrapping, copy the remainder of
432 * the input string unchanged to the output, and leave.
439 * Now we search backwards along the string from `nfit',
440 * looking for a space at which to break the line. If we
441 * don't find one at all, that's fine - we'll just break
442 * the line at `nfit'.
444 for (j
= nfit
; j
> 0; j
--) {
445 if (isspace((unsigned char)p
[j
])) {
456 while (*p
&& isspace((unsigned char)*p
))
462 SelectObject(hdc
, oldfont
);
463 ReleaseDC(cp
->hwnd
, hdc
);
465 if (lines
) *lines
= nlines
;
471 * A single standalone static text control.
473 void statictext(struct ctlpos
*cp
, char *text
, int lines
, int id
)
480 r
.bottom
= STATICHEIGHT
* lines
;
481 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
482 doctl(cp
, r
, "STATIC",
483 WS_CHILD
| WS_VISIBLE
| SS_LEFTNOWORDWRAP
,
488 * An owner-drawn static text control for a panel title.
490 void paneltitle(struct ctlpos
*cp
, int id
)
497 r
.bottom
= TITLEHEIGHT
;
498 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
499 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
| SS_OWNERDRAW
,
504 * A button on the right hand side, with a static to its left.
506 void staticbtn(struct ctlpos
*cp
, char *stext
, int sid
,
507 char *btext
, int bid
)
509 const int height
= (PUSHBTNHEIGHT
> STATICHEIGHT ?
510 PUSHBTNHEIGHT
: STATICHEIGHT
);
512 int lwid
, rwid
, rpos
;
514 rpos
= GAPBETWEEN
+ 3 * (cp
->width
+ GAPBETWEEN
) / 4;
515 lwid
= rpos
- 2 * GAPBETWEEN
;
516 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
519 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
521 r
.bottom
= STATICHEIGHT
;
522 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
525 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
527 r
.bottom
= PUSHBTNHEIGHT
;
528 doctl(cp
, r
, "BUTTON",
529 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
532 cp
->ypos
+= height
+ GAPBETWEEN
;
536 * A simple push button.
538 void button(struct ctlpos
*cp
, char *btext
, int bid
, int defbtn
)
545 r
.bottom
= PUSHBTNHEIGHT
;
547 /* Q67655: the _dialog box_ must know which button is default
548 * as well as the button itself knowing */
549 if (defbtn
&& cp
->hwnd
)
550 SendMessage(cp
->hwnd
, DM_SETDEFID
, bid
, 0);
552 doctl(cp
, r
, "BUTTON",
553 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
554 (defbtn ? BS_DEFPUSHBUTTON
: 0) | BS_PUSHBUTTON
,
557 cp
->ypos
+= PUSHBTNHEIGHT
+ GAPBETWEEN
;
561 * Like staticbtn, but two buttons.
563 void static2btn(struct ctlpos
*cp
, char *stext
, int sid
,
564 char *btext1
, int bid1
, char *btext2
, int bid2
)
566 const int height
= (PUSHBTNHEIGHT
> STATICHEIGHT ?
567 PUSHBTNHEIGHT
: STATICHEIGHT
);
569 int lwid
, rwid1
, rwid2
, rpos1
, rpos2
;
571 rpos1
= GAPBETWEEN
+ (cp
->width
+ GAPBETWEEN
) / 2;
572 rpos2
= GAPBETWEEN
+ 3 * (cp
->width
+ GAPBETWEEN
) / 4;
573 lwid
= rpos1
- 2 * GAPBETWEEN
;
574 rwid1
= rpos2
- rpos1
- GAPBETWEEN
;
575 rwid2
= cp
->width
+ GAPBETWEEN
- rpos2
;
578 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
580 r
.bottom
= STATICHEIGHT
;
581 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
584 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
586 r
.bottom
= PUSHBTNHEIGHT
;
587 doctl(cp
, r
, "BUTTON",
588 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
592 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
594 r
.bottom
= PUSHBTNHEIGHT
;
595 doctl(cp
, r
, "BUTTON",
596 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
599 cp
->ypos
+= height
+ GAPBETWEEN
;
603 * An edit control on the right hand side, with a static to its left.
605 static void staticedit_internal(struct ctlpos
*cp
, char *stext
,
606 int sid
, int eid
, int percentedit
,
609 const int height
= (EDITHEIGHT
> STATICHEIGHT ?
610 EDITHEIGHT
: STATICHEIGHT
);
612 int lwid
, rwid
, rpos
;
615 GAPBETWEEN
+ (100 - percentedit
) * (cp
->width
+ GAPBETWEEN
) / 100;
616 lwid
= rpos
- 2 * GAPBETWEEN
;
617 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
620 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
622 r
.bottom
= STATICHEIGHT
;
623 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
626 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
628 r
.bottom
= EDITHEIGHT
;
630 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| ES_AUTOHSCROLL
| style
,
631 WS_EX_CLIENTEDGE
, "", eid
);
633 cp
->ypos
+= height
+ GAPBETWEEN
;
636 void staticedit(struct ctlpos
*cp
, char *stext
,
637 int sid
, int eid
, int percentedit
)
639 staticedit_internal(cp
, stext
, sid
, eid
, percentedit
, 0);
642 void staticpassedit(struct ctlpos
*cp
, char *stext
,
643 int sid
, int eid
, int percentedit
)
645 staticedit_internal(cp
, stext
, sid
, eid
, percentedit
, ES_PASSWORD
);
649 * A drop-down list box on the right hand side, with a static to
652 void staticddl(struct ctlpos
*cp
, char *stext
,
653 int sid
, int lid
, int percentlist
)
655 const int height
= (COMBOHEIGHT
> STATICHEIGHT ?
656 COMBOHEIGHT
: STATICHEIGHT
);
658 int lwid
, rwid
, rpos
;
661 GAPBETWEEN
+ (100 - percentlist
) * (cp
->width
+ GAPBETWEEN
) / 100;
662 lwid
= rpos
- 2 * GAPBETWEEN
;
663 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
666 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
668 r
.bottom
= STATICHEIGHT
;
669 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
672 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
674 r
.bottom
= COMBOHEIGHT
*4;
675 doctl(cp
, r
, "COMBOBOX",
676 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
677 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
679 cp
->ypos
+= height
+ GAPBETWEEN
;
683 * A combo box on the right hand side, with a static to its left.
685 void staticcombo(struct ctlpos
*cp
, char *stext
,
686 int sid
, int lid
, int percentlist
)
688 const int height
= (COMBOHEIGHT
> STATICHEIGHT ?
689 COMBOHEIGHT
: STATICHEIGHT
);
691 int lwid
, rwid
, rpos
;
694 GAPBETWEEN
+ (100 - percentlist
) * (cp
->width
+ GAPBETWEEN
) / 100;
695 lwid
= rpos
- 2 * GAPBETWEEN
;
696 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
699 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
701 r
.bottom
= STATICHEIGHT
;
702 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
705 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
707 r
.bottom
= COMBOHEIGHT
*10;
708 doctl(cp
, r
, "COMBOBOX",
709 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
|
710 CBS_DROPDOWN
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
712 cp
->ypos
+= height
+ GAPBETWEEN
;
716 * A static, with a full-width drop-down list box below it.
718 void staticddlbig(struct ctlpos
*cp
, char *stext
,
726 r
.bottom
= STATICHEIGHT
;
727 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
728 cp
->ypos
+= STATICHEIGHT
;
733 r
.bottom
= COMBOHEIGHT
*4;
734 doctl(cp
, r
, "COMBOBOX",
735 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
736 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
737 cp
->ypos
+= COMBOHEIGHT
+ GAPBETWEEN
;
741 * A big multiline edit control with a static labelling it.
743 void bigeditctrl(struct ctlpos
*cp
, char *stext
,
744 int sid
, int eid
, int lines
)
751 r
.bottom
= STATICHEIGHT
;
752 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
753 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
758 r
.bottom
= EDITHEIGHT
+ (lines
- 1) * STATICHEIGHT
;
759 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
761 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
| ES_MULTILINE
,
762 WS_EX_CLIENTEDGE
, "", eid
);
766 * A list box with a static labelling it.
768 void listbox(struct ctlpos
*cp
, char *stext
,
769 int sid
, int lid
, int lines
, int multi
)
777 r
.bottom
= STATICHEIGHT
;
778 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
779 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
785 r
.bottom
= LISTHEIGHT
+ (lines
- 1) * LISTINCREMENT
;
786 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
787 doctl(cp
, r
, "LISTBOX",
788 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| WS_VSCROLL
|
789 LBS_NOTIFY
| LBS_HASSTRINGS
| LBS_USETABSTOPS
|
790 (multi ? LBS_MULTIPLESEL
: 0),
791 WS_EX_CLIENTEDGE
, "", lid
);
795 * A tab-control substitute when a real tab control is unavailable.
797 void ersatztab(struct ctlpos
*cp
, char *stext
, int sid
, int lid
, int s2id
)
799 const int height
= (COMBOHEIGHT
> STATICHEIGHT ?
800 COMBOHEIGHT
: STATICHEIGHT
);
802 int bigwid
, lwid
, rwid
, rpos
;
803 static const int BIGGAP
= 15;
804 static const int MEDGAP
= 3;
806 bigwid
= cp
->width
+ 2 * GAPBETWEEN
- 2 * BIGGAP
;
808 rpos
= BIGGAP
+ (bigwid
+ BIGGAP
) / 2;
809 lwid
= rpos
- 2 * BIGGAP
;
810 rwid
= bigwid
+ BIGGAP
- rpos
;
813 r
.top
= cp
->ypos
+ (height
- STATICHEIGHT
) / 2;
815 r
.bottom
= STATICHEIGHT
;
816 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
819 r
.top
= cp
->ypos
+ (height
- COMBOHEIGHT
) / 2;
821 r
.bottom
= COMBOHEIGHT
* 10;
822 doctl(cp
, r
, "COMBOBOX",
823 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
824 CBS_DROPDOWNLIST
| CBS_HASSTRINGS
, WS_EX_CLIENTEDGE
, "", lid
);
826 cp
->ypos
+= height
+ MEDGAP
+ GAPBETWEEN
;
832 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
| SS_ETCHEDHORZ
,
837 * A static line, followed by an edit control on the left hand side
838 * and a button on the right.
840 void editbutton(struct ctlpos
*cp
, char *stext
, int sid
,
841 int eid
, char *btext
, int bid
)
843 const int height
= (EDITHEIGHT
> PUSHBTNHEIGHT ?
844 EDITHEIGHT
: PUSHBTNHEIGHT
);
846 int lwid
, rwid
, rpos
;
851 r
.bottom
= STATICHEIGHT
;
852 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
853 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
855 rpos
= GAPBETWEEN
+ 3 * (cp
->width
+ GAPBETWEEN
) / 4;
856 lwid
= rpos
- 2 * GAPBETWEEN
;
857 rwid
= cp
->width
+ GAPBETWEEN
- rpos
;
860 r
.top
= cp
->ypos
+ (height
- EDITHEIGHT
) / 2;
862 r
.bottom
= EDITHEIGHT
;
864 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| ES_AUTOHSCROLL
,
865 WS_EX_CLIENTEDGE
, "", eid
);
868 r
.top
= cp
->ypos
+ (height
- PUSHBTNHEIGHT
) / 2;
870 r
.bottom
= PUSHBTNHEIGHT
;
871 doctl(cp
, r
, "BUTTON",
872 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
| BS_PUSHBUTTON
,
875 cp
->ypos
+= height
+ GAPBETWEEN
;
879 * A special control for manipulating an ordered preference list
880 * (eg. for cipher selection).
881 * XXX: this is a rough hack and could be improved.
883 void prefslist(struct prefslist
*hdl
, struct ctlpos
*cp
, int lines
,
884 char *stext
, int sid
, int listid
, int upbid
, int dnbid
)
886 const static int percents
[] = { 5, 75, 20 };
888 int xpos
, percent
= 0, i
;
889 int listheight
= LISTHEIGHT
+ (lines
- 1) * LISTINCREMENT
;
890 const int BTNSHEIGHT
= 2*PUSHBTNHEIGHT
+ GAPBETWEEN
;
891 int totalheight
, buttonpos
;
893 /* Squirrel away IDs. */
894 hdl
->listid
= listid
;
898 /* The static label. */
903 r
.bottom
= STATICHEIGHT
;
904 cp
->ypos
+= r
.bottom
+ GAPWITHIN
;
905 doctl(cp
, r
, "STATIC", WS_CHILD
| WS_VISIBLE
, 0, stext
, sid
);
908 if (listheight
> BTNSHEIGHT
) {
909 totalheight
= listheight
;
910 buttonpos
= (listheight
- BTNSHEIGHT
) / 2;
912 totalheight
= BTNSHEIGHT
;
916 for (i
=0; i
<3; i
++) {
918 xpos
= (cp
->width
+ GAPBETWEEN
) * percent
/ 100;
919 left
= xpos
+ GAPBETWEEN
;
920 percent
+= percents
[i
];
921 xpos
= (cp
->width
+ GAPBETWEEN
) * percent
/ 100;
926 /* The drag list box. */
927 r
.left
= left
; r
.right
= wid
;
928 r
.top
= cp
->ypos
; r
.bottom
= listheight
;
931 ctl
= doctl(cp
, r
, "LISTBOX",
932 WS_CHILD
| WS_VISIBLE
| WS_TABSTOP
|
933 WS_VSCROLL
| LBS_HASSTRINGS
| LBS_USETABSTOPS
,
941 /* The "Up" and "Down" buttons. */
942 /* XXX worry about accelerators if we have more than one
943 * prefslist on a panel */
944 r
.left
= left
; r
.right
= wid
;
945 r
.top
= cp
->ypos
+ buttonpos
; r
.bottom
= PUSHBTNHEIGHT
;
946 doctl(cp
, r
, "BUTTON",
947 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
|
948 WS_TABSTOP
| BS_PUSHBUTTON
,
951 r
.left
= left
; r
.right
= wid
;
952 r
.top
= cp
->ypos
+ buttonpos
+ PUSHBTNHEIGHT
+ GAPBETWEEN
;
953 r
.bottom
= PUSHBTNHEIGHT
;
954 doctl(cp
, r
, "BUTTON",
955 BS_NOTIFY
| WS_CHILD
| WS_VISIBLE
|
956 WS_TABSTOP
| BS_PUSHBUTTON
,
964 cp
->ypos
+= totalheight
+ GAPBETWEEN
;
969 * Helper function for prefslist: move item in list box.
971 static void pl_moveitem(HWND hwnd
, int listid
, int src
, int dst
)
975 /* Get the item's data. */
976 tlen
= SendDlgItemMessage (hwnd
, listid
, LB_GETTEXTLEN
, src
, 0);
977 txt
= snewn(tlen
+1, char);
978 SendDlgItemMessage (hwnd
, listid
, LB_GETTEXT
, src
, (LPARAM
) txt
);
979 val
= SendDlgItemMessage (hwnd
, listid
, LB_GETITEMDATA
, src
, 0);
980 /* Deselect old location. */
981 SendDlgItemMessage (hwnd
, listid
, LB_SETSEL
, FALSE
, src
);
982 /* Delete it at the old location. */
983 SendDlgItemMessage (hwnd
, listid
, LB_DELETESTRING
, src
, 0);
984 /* Insert it at new location. */
985 SendDlgItemMessage (hwnd
, listid
, LB_INSERTSTRING
, dst
,
987 SendDlgItemMessage (hwnd
, listid
, LB_SETITEMDATA
, dst
,
990 SendDlgItemMessage (hwnd
, listid
, LB_SETCURSEL
, dst
, 0);
994 int pl_itemfrompt(HWND hwnd
, POINT cursor
, BOOL scroll
)
997 POINT uppoint
, downpoint
;
998 int updist
, downdist
, upitem
, downitem
, i
;
1001 * Ghastly hackery to try to figure out not which
1002 * _item_, but which _gap between items_, the user
1003 * is pointing at. We do this by first working out
1004 * which list item is under the cursor, and then
1005 * working out how far the cursor would have to
1006 * move up or down before the answer was different.
1007 * Then we put the insertion point _above_ the
1008 * current item if the upper edge is closer than
1009 * the lower edge, or _below_ it if vice versa.
1011 ret
= LBItemFromPt(hwnd
, cursor
, scroll
);
1014 ret
= LBItemFromPt(hwnd
, cursor
, FALSE
);
1015 updist
= downdist
= 0;
1016 for (i
= 1; i
< 4096 && (!updist
|| !downdist
); i
++) {
1017 uppoint
= downpoint
= cursor
;
1020 upitem
= LBItemFromPt(hwnd
, uppoint
, FALSE
);
1021 downitem
= LBItemFromPt(hwnd
, downpoint
, FALSE
);
1022 if (!updist
&& upitem
!= ret
)
1024 if (!downdist
&& downitem
!= ret
)
1027 if (downdist
< updist
)
1033 * Handler for prefslist above.
1035 * Return value has bit 0 set if the dialog box procedure needs to
1036 * return TRUE from handling this message; it has bit 1 set if a
1037 * change may have been made in the contents of the list.
1039 int handle_prefslist(struct prefslist
*hdl
,
1040 int *array
, int maxmemb
,
1041 int is_dlmsg
, HWND hwnd
,
1042 WPARAM wParam
, LPARAM lParam
)
1049 if ((int)wParam
== hdl
->listid
) {
1050 DRAGLISTINFO
*dlm
= (DRAGLISTINFO
*)lParam
;
1051 int dest
= 0; /* initialise to placate gcc */
1052 switch (dlm
->uNotification
) {
1055 SendDlgItemMessage(hwnd
, hdl
->listid
,
1056 LB_ADDSTRING
, 0, (LPARAM
) "");
1058 hdl
->srcitem
= LBItemFromPt(dlm
->hWnd
, dlm
->ptCursor
, TRUE
);
1060 /* XXX hack Q183115 */
1061 SetWindowLong(hwnd
, DWL_MSGRESULT
, TRUE
);
1064 DrawInsert(hwnd
, dlm
->hWnd
, -1); /* Clear arrow */
1065 SendDlgItemMessage(hwnd
, hdl
->listid
,
1066 LB_DELETESTRING
, hdl
->dummyitem
, 0);
1071 dest
= pl_itemfrompt(dlm
->hWnd
, dlm
->ptCursor
, TRUE
);
1072 if (dest
> hdl
->dummyitem
) dest
= hdl
->dummyitem
;
1073 DrawInsert (hwnd
, dlm
->hWnd
, dest
);
1075 SetWindowLong(hwnd
, DWL_MSGRESULT
, DL_MOVECURSOR
);
1077 SetWindowLong(hwnd
, DWL_MSGRESULT
, DL_STOPCURSOR
);
1080 if (hdl
->dragging
) {
1081 dest
= pl_itemfrompt(dlm
->hWnd
, dlm
->ptCursor
, TRUE
);
1082 if (dest
> hdl
->dummyitem
) dest
= hdl
->dummyitem
;
1083 DrawInsert (hwnd
, dlm
->hWnd
, -1);
1085 SendDlgItemMessage(hwnd
, hdl
->listid
,
1086 LB_DELETESTRING
, hdl
->dummyitem
, 0);
1087 if (hdl
->dragging
) {
1090 /* Correct for "missing" item. */
1091 if (dest
> hdl
->srcitem
) dest
--;
1092 pl_moveitem(hwnd
, hdl
->listid
, hdl
->srcitem
, dest
);
1102 if (((LOWORD(wParam
) == hdl
->upbid
) ||
1103 (LOWORD(wParam
) == hdl
->dnbid
)) &&
1104 ((HIWORD(wParam
) == BN_CLICKED
) ||
1105 (HIWORD(wParam
) == BN_DOUBLECLICKED
))) {
1106 /* Move an item up or down the list. */
1107 /* Get the current selection, if any. */
1108 int selection
= SendDlgItemMessage (hwnd
, hdl
->listid
, LB_GETCURSEL
, 0, 0);
1109 if (selection
== LB_ERR
) {
1113 /* Get the total number of items. */
1114 nitems
= SendDlgItemMessage (hwnd
, hdl
->listid
, LB_GETCOUNT
, 0, 0);
1115 /* Should we do anything? */
1116 if (LOWORD(wParam
) == hdl
->upbid
&& (selection
> 0))
1117 pl_moveitem(hwnd
, hdl
->listid
, selection
, selection
- 1);
1118 else if (LOWORD(wParam
) == hdl
->dnbid
&& (selection
< nitems
- 1))
1119 pl_moveitem(hwnd
, hdl
->listid
, selection
, selection
+ 1);
1128 /* Update array to match the list box. */
1129 for (i
=0; i
< maxmemb
; i
++)
1130 array
[i
] = SendDlgItemMessage (hwnd
, hdl
->listid
, LB_GETITEMDATA
,
1138 * A progress bar (from Common Controls). We like our progress bars
1139 * to be smooth and unbroken, without those ugly divisions; some
1140 * older compilers may not support that, but that's life.
1142 void progressbar(struct ctlpos
*cp
, int id
)
1146 r
.left
= GAPBETWEEN
;
1148 r
.right
= cp
->width
;
1149 r
.bottom
= PROGBARHEIGHT
;
1150 cp
->ypos
+= r
.bottom
+ GAPBETWEEN
;
1152 doctl(cp
, r
, PROGRESS_CLASS
, WS_CHILD
| WS_VISIBLE
1156 , WS_EX_CLIENTEDGE
, "", id
);
1159 /* ----------------------------------------------------------------------
1160 * Platform-specific side of portable dialog-box mechanism.
1164 * This function takes a string, escapes all the ampersands, and
1165 * places a single (unescaped) ampersand in front of the first
1166 * occurrence of the given shortcut character (which may be
1169 * Return value is a malloc'ed copy of the processed version of the
1172 static char *shortcut_escape(char *text
, char shortcut
)
1178 return NULL
; /* sfree won't choke on this */
1180 ret
= snewn(2*strlen(text
)+1, char); /* size potentially doubles! */
1181 shortcut
= tolower((unsigned char)shortcut
);
1186 if (shortcut
!= NO_SHORTCUT
&&
1187 tolower((unsigned char)*p
) == shortcut
) {
1189 shortcut
= NO_SHORTCUT
; /* stop it happening twice */
1190 } else if (*p
== '&') {
1199 void winctrl_add_shortcuts(struct dlgparam
*dp
, struct winctrl
*c
)
1202 for (i
= 0; i
< lenof(c
->shortcuts
); i
++)
1203 if (c
->shortcuts
[i
] != NO_SHORTCUT
) {
1204 unsigned char s
= tolower((unsigned char)c
->shortcuts
[i
]);
1205 assert(!dp
->shortcuts
[s
]);
1206 dp
->shortcuts
[s
] = TRUE
;
1210 void winctrl_rem_shortcuts(struct dlgparam
*dp
, struct winctrl
*c
)
1213 for (i
= 0; i
< lenof(c
->shortcuts
); i
++)
1214 if (c
->shortcuts
[i
] != NO_SHORTCUT
) {
1215 unsigned char s
= tolower((unsigned char)c
->shortcuts
[i
]);
1216 assert(dp
->shortcuts
[s
]);
1217 dp
->shortcuts
[s
] = FALSE
;
1221 static int winctrl_cmp_byctrl(void *av
, void *bv
)
1223 struct winctrl
*a
= (struct winctrl
*)av
;
1224 struct winctrl
*b
= (struct winctrl
*)bv
;
1225 if (a
->ctrl
< b
->ctrl
)
1227 else if (a
->ctrl
> b
->ctrl
)
1232 static int winctrl_cmp_byid(void *av
, void *bv
)
1234 struct winctrl
*a
= (struct winctrl
*)av
;
1235 struct winctrl
*b
= (struct winctrl
*)bv
;
1236 if (a
->base_id
< b
->base_id
)
1238 else if (a
->base_id
> b
->base_id
)
1243 static int winctrl_cmp_byctrl_find(void *av
, void *bv
)
1245 union control
*a
= (union control
*)av
;
1246 struct winctrl
*b
= (struct winctrl
*)bv
;
1249 else if (a
> b
->ctrl
)
1254 static int winctrl_cmp_byid_find(void *av
, void *bv
)
1257 struct winctrl
*b
= (struct winctrl
*)bv
;
1258 if (*a
< b
->base_id
)
1260 else if (*a
>= b
->base_id
+ b
->num_ids
)
1266 void winctrl_init(struct winctrls
*wc
)
1268 wc
->byctrl
= newtree234(winctrl_cmp_byctrl
);
1269 wc
->byid
= newtree234(winctrl_cmp_byid
);
1271 void winctrl_cleanup(struct winctrls
*wc
)
1275 while ((c
= index234(wc
->byid
, 0)) != NULL
) {
1276 winctrl_remove(wc
, c
);
1281 freetree234(wc
->byctrl
);
1282 freetree234(wc
->byid
);
1283 wc
->byctrl
= wc
->byid
= NULL
;
1286 void winctrl_add(struct winctrls
*wc
, struct winctrl
*c
)
1288 struct winctrl
*ret
;
1290 ret
= add234(wc
->byctrl
, c
);
1293 ret
= add234(wc
->byid
, c
);
1297 void winctrl_remove(struct winctrls
*wc
, struct winctrl
*c
)
1299 struct winctrl
*ret
;
1300 ret
= del234(wc
->byctrl
, c
);
1301 ret
= del234(wc
->byid
, c
);
1305 struct winctrl
*winctrl_findbyctrl(struct winctrls
*wc
, union control
*ctrl
)
1307 return find234(wc
->byctrl
, ctrl
, winctrl_cmp_byctrl_find
);
1310 struct winctrl
*winctrl_findbyid(struct winctrls
*wc
, int id
)
1312 return find234(wc
->byid
, &id
, winctrl_cmp_byid_find
);
1315 struct winctrl
*winctrl_findbyindex(struct winctrls
*wc
, int index
)
1317 return index234(wc
->byid
, index
);
1320 void winctrl_layout(struct dlgparam
*dp
, struct winctrls
*wc
,
1321 struct ctlpos
*cp
, struct controlset
*s
, int *id
)
1323 struct ctlpos columns
[16];
1324 int ncols
, colstart
, colspan
;
1326 struct ctlpos tabdelays
[16];
1327 union control
*tabdelayed
[16];
1332 char shortcuts
[MAX_SHORTCUTS_PER_CTRL
];
1335 int i
, actual_base_id
, base_id
, num_ids
;
1340 /* Start a containing box, if we have a boxname. */
1341 if (s
->boxname
&& *s
->boxname
) {
1342 struct winctrl
*c
= snew(struct winctrl
);
1344 c
->base_id
= base_id
;
1347 memset(c
->shortcuts
, NO_SHORTCUT
, lenof(c
->shortcuts
));
1349 beginbox(cp
, s
->boxtitle
, base_id
);
1353 /* Draw a title, if we have one. */
1354 if (!s
->boxname
&& s
->boxtitle
) {
1355 struct winctrl
*c
= snew(struct winctrl
);
1357 c
->base_id
= base_id
;
1359 c
->data
= dupstr(s
->boxtitle
);
1360 memset(c
->shortcuts
, NO_SHORTCUT
, lenof(c
->shortcuts
));
1362 paneltitle(cp
, base_id
);
1366 /* Initially we have just one column. */
1368 columns
[0] = *cp
; /* structure copy */
1370 /* And initially, there are no pending tab-delayed controls. */
1373 /* Loop over each control in the controlset. */
1374 for (i
= 0; i
< s
->ncontrols
; i
++) {
1375 union control
*ctrl
= s
->ctrls
[i
];
1378 * Generic processing that pertains to all control types.
1379 * At the end of this if statement, we'll have produced
1380 * `ctrl' (a pointer to the control we have to create, or
1381 * think about creating, in this iteration of the loop),
1382 * `pos' (a suitable ctlpos with which to position it), and
1383 * `c' (a winctrl structure to receive details of the
1384 * dialog IDs). Or we'll have done a `continue', if it was
1385 * CTRL_COLUMNS and doesn't require any control creation at
1388 if (ctrl
->generic
.type
== CTRL_COLUMNS
) {
1389 assert((ctrl
->columns
.ncols
== 1) ^ (ncols
== 1));
1393 * We're splitting into multiple columns.
1395 int lpercent
, rpercent
, lx
, rx
, i
;
1397 ncols
= ctrl
->columns
.ncols
;
1398 assert(ncols
<= lenof(columns
));
1399 for (i
= 1; i
< ncols
; i
++)
1400 columns
[i
] = columns
[0]; /* structure copy */
1403 for (i
= 0; i
< ncols
; i
++) {
1404 rpercent
= lpercent
+ ctrl
->columns
.percentages
[i
];
1405 lx
= columns
[i
].xoff
+ lpercent
*
1406 (columns
[i
].width
+ GAPBETWEEN
) / 100;
1407 rx
= columns
[i
].xoff
+ rpercent
*
1408 (columns
[i
].width
+ GAPBETWEEN
) / 100;
1409 columns
[i
].xoff
= lx
;
1410 columns
[i
].width
= rx
- lx
- GAPBETWEEN
;
1411 lpercent
= rpercent
;
1415 * We're recombining the various columns into one.
1417 int maxy
= columns
[0].ypos
;
1419 for (i
= 1; i
< ncols
; i
++)
1420 if (maxy
< columns
[i
].ypos
)
1421 maxy
= columns
[i
].ypos
;
1423 columns
[0] = *cp
; /* structure copy */
1424 columns
[0].ypos
= maxy
;
1428 } else if (ctrl
->generic
.type
== CTRL_TABDELAY
) {
1431 assert(!ctrl
->generic
.tabdelay
);
1432 ctrl
= ctrl
->tabdelay
.ctrl
;
1434 for (i
= 0; i
< ntabdelays
; i
++)
1435 if (tabdelayed
[i
] == ctrl
)
1437 assert(i
< ntabdelays
); /* we have to have found it */
1439 pos
= tabdelays
[i
]; /* structure copy */
1441 colstart
= colspan
= -1; /* indicate this was tab-delayed */
1445 * If it wasn't one of those, it's a genuine control;
1446 * so we'll have to compute a position for it now, by
1447 * checking its column span.
1451 colstart
= COLUMN_START(ctrl
->generic
.column
);
1452 colspan
= COLUMN_SPAN(ctrl
->generic
.column
);
1454 pos
= columns
[colstart
]; /* structure copy */
1455 pos
.width
= columns
[colstart
+colspan
-1].width
+
1456 (columns
[colstart
+colspan
-1].xoff
- columns
[colstart
].xoff
);
1458 for (col
= colstart
; col
< colstart
+colspan
; col
++)
1459 if (pos
.ypos
< columns
[col
].ypos
)
1460 pos
.ypos
= columns
[col
].ypos
;
1463 * If this control is to be tabdelayed, add it to the
1464 * tabdelay list, and unset pos.hwnd to inhibit actual
1467 if (ctrl
->generic
.tabdelay
) {
1468 assert(ntabdelays
< lenof(tabdelays
));
1469 tabdelays
[ntabdelays
] = pos
; /* structure copy */
1470 tabdelayed
[ntabdelays
] = ctrl
;
1476 /* Most controls don't need anything in c->data. */
1479 /* And they all start off with no shortcuts registered. */
1480 memset(shortcuts
, NO_SHORTCUT
, lenof(shortcuts
));
1483 /* Almost all controls start at base_id. */
1484 actual_base_id
= base_id
;
1487 * Now we're ready to actually create the control, by
1488 * switching on its type.
1490 switch (ctrl
->generic
.type
) {
1493 char *wrapped
, *escaped
;
1496 wrapped
= staticwrap(&pos
, cp
->hwnd
,
1497 ctrl
->generic
.label
, &lines
);
1498 escaped
= shortcut_escape(wrapped
, NO_SHORTCUT
);
1499 statictext(&pos
, escaped
, lines
, base_id
);
1505 num_ids
= 2; /* static, edit */
1506 escaped
= shortcut_escape(ctrl
->editbox
.label
,
1507 ctrl
->editbox
.shortcut
);
1508 shortcuts
[nshortcuts
++] = ctrl
->editbox
.shortcut
;
1509 if (ctrl
->editbox
.percentwidth
== 100) {
1510 if (ctrl
->editbox
.has_list
)
1511 combobox(&pos
, escaped
,
1512 base_id
, base_id
+1);
1514 multiedit(&pos
, ctrl
->editbox
.password
, escaped
,
1515 base_id
, base_id
+1, 100, NULL
);
1517 if (ctrl
->editbox
.has_list
) {
1518 staticcombo(&pos
, escaped
, base_id
, base_id
+1,
1519 ctrl
->editbox
.percentwidth
);
1521 (ctrl
->editbox
.password ? staticpassedit
: staticedit
)
1522 (&pos
, escaped
, base_id
, base_id
+1,
1523 ctrl
->editbox
.percentwidth
);
1529 num_ids
= ctrl
->radio
.nbuttons
+ 1; /* label as well */
1531 struct radio
*buttons
;
1534 escaped
= shortcut_escape(ctrl
->radio
.label
,
1535 ctrl
->radio
.shortcut
);
1536 shortcuts
[nshortcuts
++] = ctrl
->radio
.shortcut
;
1538 buttons
= snewn(ctrl
->radio
.nbuttons
, struct radio
);
1540 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1542 shortcut_escape(ctrl
->radio
.buttons
[i
],
1543 (char)(ctrl
->radio
.shortcuts ?
1544 ctrl
->radio
.shortcuts
[i
] :
1546 buttons
[i
].id
= base_id
+ 1 + i
;
1547 if (ctrl
->radio
.shortcuts
) {
1548 assert(nshortcuts
< MAX_SHORTCUTS_PER_CTRL
);
1549 shortcuts
[nshortcuts
++] = ctrl
->radio
.shortcuts
[i
];
1553 radioline_common(&pos
, escaped
, base_id
,
1554 ctrl
->radio
.ncolumns
,
1555 buttons
, ctrl
->radio
.nbuttons
);
1557 for (i
= 0; i
< ctrl
->radio
.nbuttons
; i
++) {
1558 sfree(buttons
[i
].text
);
1566 escaped
= shortcut_escape(ctrl
->checkbox
.label
,
1567 ctrl
->checkbox
.shortcut
);
1568 shortcuts
[nshortcuts
++] = ctrl
->checkbox
.shortcut
;
1569 checkbox(&pos
, escaped
, base_id
);
1573 escaped
= shortcut_escape(ctrl
->button
.label
,
1574 ctrl
->button
.shortcut
);
1575 shortcuts
[nshortcuts
++] = ctrl
->button
.shortcut
;
1576 if (ctrl
->button
.iscancel
)
1577 actual_base_id
= IDCANCEL
;
1579 button(&pos
, escaped
, actual_base_id
, ctrl
->button
.isdefault
);
1584 escaped
= shortcut_escape(ctrl
->listbox
.label
,
1585 ctrl
->listbox
.shortcut
);
1586 shortcuts
[nshortcuts
++] = ctrl
->listbox
.shortcut
;
1587 if (ctrl
->listbox
.draglist
) {
1588 data
= snew(struct prefslist
);
1590 prefslist(data
, &pos
, ctrl
->listbox
.height
, escaped
,
1591 base_id
, base_id
+1, base_id
+2, base_id
+3);
1592 shortcuts
[nshortcuts
++] = 'u'; /* Up */
1593 shortcuts
[nshortcuts
++] = 'd'; /* Down */
1594 } else if (ctrl
->listbox
.height
== 0) {
1595 /* Drop-down list. */
1596 if (ctrl
->listbox
.percentwidth
== 100) {
1597 staticddlbig(&pos
, escaped
,
1598 base_id
, base_id
+1);
1600 staticddl(&pos
, escaped
, base_id
,
1601 base_id
+1, ctrl
->listbox
.percentwidth
);
1604 /* Ordinary list. */
1605 listbox(&pos
, escaped
, base_id
, base_id
+1,
1606 ctrl
->listbox
.height
, ctrl
->listbox
.multisel
);
1608 if (ctrl
->listbox
.ncols
) {
1610 * This method of getting the box width is a bit of
1611 * a hack; we'd do better to try to retrieve the
1612 * actual width in dialog units from doctl() just
1613 * before MapDialogRect. But that's going to be no
1614 * fun, and this should be good enough accuracy.
1616 int width
= cp
->width
* ctrl
->listbox
.percentwidth
;
1620 tabarray
= snewn(ctrl
->listbox
.ncols
-1, int);
1622 for (i
= 0; i
< ctrl
->listbox
.ncols
-1; i
++) {
1623 percent
+= ctrl
->listbox
.percentages
[i
];
1624 tabarray
[i
] = width
* percent
/ 10000;
1626 SendDlgItemMessage(cp
->hwnd
, base_id
+1, LB_SETTABSTOPS
,
1627 ctrl
->listbox
.ncols
-1, (LPARAM
)tabarray
);
1632 case CTRL_FILESELECT
:
1634 escaped
= shortcut_escape(ctrl
->fileselect
.label
,
1635 ctrl
->fileselect
.shortcut
);
1636 shortcuts
[nshortcuts
++] = ctrl
->fileselect
.shortcut
;
1637 editbutton(&pos
, escaped
, base_id
, base_id
+1,
1638 "Bro&wse...", base_id
+2);
1639 shortcuts
[nshortcuts
++] = 'w';
1642 case CTRL_FONTSELECT
:
1644 escaped
= shortcut_escape(ctrl
->fontselect
.label
,
1645 ctrl
->fontselect
.shortcut
);
1646 shortcuts
[nshortcuts
++] = ctrl
->fontselect
.shortcut
;
1647 statictext(&pos
, escaped
, 1, base_id
);
1648 staticbtn(&pos
, "", base_id
+1, "Change...", base_id
+2);
1650 data
= snew(FontSpec
);
1653 assert(!"Can't happen");
1654 num_ids
= 0; /* placate gcc */
1659 * Create a `struct winctrl' for this control, and advance
1660 * the dialog ID counter, if it's actually been created
1661 * (and isn't tabdelayed).
1664 struct winctrl
*c
= snew(struct winctrl
);
1667 c
->base_id
= actual_base_id
;
1668 c
->num_ids
= num_ids
;
1670 memcpy(c
->shortcuts
, shortcuts
, sizeof(shortcuts
));
1672 winctrl_add_shortcuts(dp
, c
);
1673 if (actual_base_id
== base_id
)
1677 if (colstart
>= 0) {
1679 * Update the ypos in all columns crossed by this
1683 for (i
= colstart
; i
< colstart
+colspan
; i
++)
1684 columns
[i
].ypos
= pos
.ypos
;
1689 * We've now finished laying out the controls; so now update
1690 * the ctlpos and control ID that were passed in, terminate
1691 * any containing box, and return.
1693 for (i
= 0; i
< ncols
; i
++)
1694 if (cp
->ypos
< columns
[i
].ypos
)
1695 cp
->ypos
= columns
[i
].ypos
;
1698 if (s
->boxname
&& *s
->boxname
)
1702 static void winctrl_set_focus(union control
*ctrl
, struct dlgparam
*dp
,
1707 dp
->lastfocused
= dp
->focused
;
1709 } else if (!has_focus
&& dp
->focused
== ctrl
) {
1710 dp
->lastfocused
= dp
->focused
;
1715 union control
*dlg_last_focused(union control
*ctrl
, void *dlg
)
1717 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
1718 return dp
->focused
== ctrl ? dp
->lastfocused
: dp
->focused
;
1722 * The dialog-box procedure calls this function to handle Windows
1723 * messages on a control we manage.
1725 int winctrl_handle_command(struct dlgparam
*dp
, UINT msg
,
1726 WPARAM wParam
, LPARAM lParam
)
1729 union control
*ctrl
;
1731 static UINT draglistmsg
= WM_NULL
;
1734 * Filter out pointless window messages. Our interest is in
1735 * WM_COMMAND and the drag list message, and nothing else.
1737 if (draglistmsg
== WM_NULL
)
1738 draglistmsg
= RegisterWindowMessage (DRAGLISTMSGSTRING
);
1740 if (msg
!= draglistmsg
&& msg
!= WM_COMMAND
&& msg
!= WM_DRAWITEM
)
1744 * Look up the control ID in our data.
1747 for (i
= 0; i
< dp
->nctrltrees
; i
++) {
1748 c
= winctrl_findbyid(dp
->controltrees
[i
], LOWORD(wParam
));
1753 return 0; /* we have nothing to do */
1755 if (msg
== WM_DRAWITEM
) {
1757 * Owner-draw request for a panel title.
1759 LPDRAWITEMSTRUCT di
= (LPDRAWITEMSTRUCT
) lParam
;
1761 RECT r
= di
->rcItem
;
1764 SetMapMode(hdc
, MM_TEXT
); /* ensure logical units == pixels */
1766 GetTextExtentPoint32(hdc
, (char *)c
->data
,
1767 strlen((char *)c
->data
), &s
);
1768 DrawEdge(hdc
, &r
, EDGE_ETCHED
, BF_ADJUST
| BF_RECT
);
1770 r
.left
+ (r
.right
-r
.left
-s
.cx
)/2,
1771 r
.top
+ (r
.bottom
-r
.top
-s
.cy
)/2,
1772 (char *)c
->data
, strlen((char *)c
->data
));
1778 id
= LOWORD(wParam
) - c
->base_id
;
1780 if (!ctrl
|| !ctrl
->generic
.handler
)
1781 return 0; /* nothing we can do here */
1784 * From here on we do not issue `return' statements until the
1785 * very end of the dialog box: any event handler is entitled to
1786 * ask for a colour selector, so we _must_ always allow control
1787 * to reach the end of this switch statement so that the
1788 * subsequent code can test dp->coloursel_wanted().
1791 dp
->coloursel_wanted
= FALSE
;
1794 * Now switch on the control type and the message.
1796 switch (ctrl
->generic
.type
) {
1798 if (msg
== WM_COMMAND
&& !ctrl
->editbox
.has_list
&&
1799 (HIWORD(wParam
) == EN_SETFOCUS
|| HIWORD(wParam
) == EN_KILLFOCUS
))
1800 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == EN_SETFOCUS
);
1801 if (msg
== WM_COMMAND
&& ctrl
->editbox
.has_list
&&
1802 (HIWORD(wParam
)==CBN_SETFOCUS
|| HIWORD(wParam
)==CBN_KILLFOCUS
))
1803 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == CBN_SETFOCUS
);
1805 if (msg
== WM_COMMAND
&& !ctrl
->editbox
.has_list
&&
1806 HIWORD(wParam
) == EN_CHANGE
)
1807 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1808 if (msg
== WM_COMMAND
&&
1809 ctrl
->editbox
.has_list
) {
1810 if (HIWORD(wParam
) == CBN_SELCHANGE
) {
1814 index
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1,
1815 CB_GETCURSEL
, 0, 0);
1816 len
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1,
1817 CB_GETLBTEXTLEN
, index
, 0);
1818 text
= snewn(len
+1, char);
1819 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, CB_GETLBTEXT
,
1820 index
, (LPARAM
)text
);
1821 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, text
);
1823 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1824 } else if (HIWORD(wParam
) == CBN_EDITCHANGE
) {
1825 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1826 } else if (HIWORD(wParam
) == CBN_KILLFOCUS
) {
1827 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
1833 if (msg
== WM_COMMAND
&&
1834 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1835 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1837 * We sometimes get spurious BN_CLICKED messages for the
1838 * radio button that is just about to _lose_ selection, if
1839 * we're switching using the arrow keys. Therefore we
1840 * double-check that the button in wParam is actually
1841 * checked before generating an event.
1843 if (msg
== WM_COMMAND
&&
1844 (HIWORD(wParam
) == BN_CLICKED
||
1845 HIWORD(wParam
) == BN_DOUBLECLICKED
) &&
1846 IsDlgButtonChecked(dp
->hwnd
, LOWORD(wParam
))) {
1847 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1851 if (msg
== WM_COMMAND
&&
1852 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1853 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1854 if (msg
== WM_COMMAND
&&
1855 (HIWORD(wParam
) == BN_CLICKED
||
1856 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
1857 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1861 if (msg
== WM_COMMAND
&&
1862 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1863 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1864 if (msg
== WM_COMMAND
&&
1865 (HIWORD(wParam
) == BN_CLICKED
||
1866 HIWORD(wParam
) == BN_DOUBLECLICKED
)) {
1867 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1871 if (msg
== WM_COMMAND
&& ctrl
->listbox
.height
!= 0 &&
1872 (HIWORD(wParam
)==LBN_SETFOCUS
|| HIWORD(wParam
)==LBN_KILLFOCUS
))
1873 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == LBN_SETFOCUS
);
1874 if (msg
== WM_COMMAND
&& ctrl
->listbox
.height
== 0 &&
1875 (HIWORD(wParam
)==CBN_SETFOCUS
|| HIWORD(wParam
)==CBN_KILLFOCUS
))
1876 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == CBN_SETFOCUS
);
1877 if (msg
== WM_COMMAND
&& id
>= 2 &&
1878 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1879 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1880 if (ctrl
->listbox
.draglist
) {
1882 pret
= handle_prefslist(c
->data
, NULL
, 0, (msg
!= WM_COMMAND
),
1883 dp
->hwnd
, wParam
, lParam
);
1885 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1888 if (msg
== WM_COMMAND
&& HIWORD(wParam
) == LBN_DBLCLK
) {
1889 SetCapture(dp
->hwnd
);
1890 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_ACTION
);
1891 } else if (msg
== WM_COMMAND
&& HIWORD(wParam
) == LBN_SELCHANGE
) {
1892 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_SELCHANGE
);
1896 case CTRL_FILESELECT
:
1897 if (msg
== WM_COMMAND
&& id
== 1 &&
1898 (HIWORD(wParam
) == EN_SETFOCUS
|| HIWORD(wParam
) == EN_KILLFOCUS
))
1899 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == EN_SETFOCUS
);
1900 if (msg
== WM_COMMAND
&& id
== 2 &&
1901 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1902 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1903 if (msg
== WM_COMMAND
&& id
== 1 && HIWORD(wParam
) == EN_CHANGE
)
1904 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1906 (msg
== WM_COMMAND
&&
1907 (HIWORD(wParam
) == BN_CLICKED
||
1908 HIWORD(wParam
) == BN_DOUBLECLICKED
))) {
1910 char filename
[FILENAME_MAX
];
1913 memset(&of
, 0, sizeof(of
));
1914 #ifdef OPENFILENAME_SIZE_VERSION_400
1915 of
.lStructSize
= OPENFILENAME_SIZE_VERSION_400
;
1917 of
.lStructSize
= sizeof(of
);
1919 of
.hwndOwner
= dp
->hwnd
;
1920 if (ctrl
->fileselect
.filter
)
1921 of
.lpstrFilter
= ctrl
->fileselect
.filter
;
1923 of
.lpstrFilter
= "All Files (*.*)\0*\0\0\0";
1924 of
.lpstrCustomFilter
= NULL
;
1925 of
.nFilterIndex
= 1;
1926 of
.lpstrFile
= filename
;
1927 GetDlgItemText(dp
->hwnd
, c
->base_id
+1, filename
, lenof(filename
));
1928 filename
[lenof(filename
)-1] = '\0';
1929 of
.nMaxFile
= lenof(filename
);
1930 of
.lpstrFileTitle
= NULL
;
1931 of
.lpstrInitialDir
= NULL
;
1932 of
.lpstrTitle
= ctrl
->fileselect
.title
;
1934 if (ctrl
->fileselect
.for_writing
)
1935 ret
= GetSaveFileName(&of
);
1937 ret
= GetOpenFileName(&of
);
1939 SetDlgItemText(dp
->hwnd
, c
->base_id
+ 1, filename
);
1940 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1944 case CTRL_FONTSELECT
:
1945 if (msg
== WM_COMMAND
&& id
== 2 &&
1946 (HIWORD(wParam
) == BN_SETFOCUS
|| HIWORD(wParam
) == BN_KILLFOCUS
))
1947 winctrl_set_focus(ctrl
, dp
, HIWORD(wParam
) == BN_SETFOCUS
);
1949 (msg
== WM_COMMAND
&&
1950 (HIWORD(wParam
) == BN_CLICKED
||
1951 HIWORD(wParam
) == BN_DOUBLECLICKED
))) {
1955 FontSpec fs
= *(FontSpec
*)c
->data
;
1958 lf
.lfHeight
= -MulDiv(fs
.height
,
1959 GetDeviceCaps(hdc
, LOGPIXELSY
), 72);
1961 lf
.lfWidth
= lf
.lfEscapement
= lf
.lfOrientation
= 0;
1962 lf
.lfItalic
= lf
.lfUnderline
= lf
.lfStrikeOut
= 0;
1963 lf
.lfWeight
= (fs
.isbold ? FW_BOLD
: 0);
1964 lf
.lfCharSet
= fs
.charset
;
1965 lf
.lfOutPrecision
= OUT_DEFAULT_PRECIS
;
1966 lf
.lfClipPrecision
= CLIP_DEFAULT_PRECIS
;
1967 lf
.lfQuality
= DEFAULT_QUALITY
;
1968 lf
.lfPitchAndFamily
= FIXED_PITCH
| FF_DONTCARE
;
1969 strncpy(lf
.lfFaceName
, fs
.name
,
1970 sizeof(lf
.lfFaceName
) - 1);
1971 lf
.lfFaceName
[sizeof(lf
.lfFaceName
) - 1] = '\0';
1973 cf
.lStructSize
= sizeof(cf
);
1974 cf
.hwndOwner
= dp
->hwnd
;
1976 cf
.Flags
= CF_FIXEDPITCHONLY
| CF_FORCEFONTEXIST
|
1977 CF_INITTOLOGFONTSTRUCT
| CF_SCREENFONTS
;
1979 if (ChooseFont(&cf
)) {
1980 strncpy(fs
.name
, lf
.lfFaceName
,
1981 sizeof(fs
.name
) - 1);
1982 fs
.name
[sizeof(fs
.name
) - 1] = '\0';
1983 fs
.isbold
= (lf
.lfWeight
== FW_BOLD
);
1984 fs
.charset
= lf
.lfCharSet
;
1985 fs
.height
= cf
.iPointSize
/ 10;
1986 dlg_fontsel_set(ctrl
, dp
, fs
);
1987 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_VALCHANGE
);
1994 * If the above event handler has asked for a colour selector,
1995 * now is the time to generate one.
1997 if (dp
->coloursel_wanted
) {
1998 static CHOOSECOLOR cc
;
1999 static DWORD custom
[16] = { 0 }; /* zero initialisers */
2000 cc
.lStructSize
= sizeof(cc
);
2001 cc
.hwndOwner
= dp
->hwnd
;
2002 cc
.hInstance
= (HWND
) hinst
;
2003 cc
.lpCustColors
= custom
;
2004 cc
.rgbResult
= RGB(dp
->coloursel_result
.r
,
2005 dp
->coloursel_result
.g
,
2006 dp
->coloursel_result
.b
);
2007 cc
.Flags
= CC_FULLOPEN
| CC_RGBINIT
;
2008 if (ChooseColor(&cc
)) {
2009 dp
->coloursel_result
.r
=
2010 (unsigned char) (cc
.rgbResult
& 0xFF);
2011 dp
->coloursel_result
.g
=
2012 (unsigned char) (cc
.rgbResult
>> 8) & 0xFF;
2013 dp
->coloursel_result
.b
=
2014 (unsigned char) (cc
.rgbResult
>> 16) & 0xFF;
2015 dp
->coloursel_result
.ok
= TRUE
;
2017 dp
->coloursel_result
.ok
= FALSE
;
2018 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_CALLBACK
);
2025 * This function can be called to produce context help on a
2026 * control. Returns TRUE if it has actually launched WinHelp.
2028 int winctrl_context_help(struct dlgparam
*dp
, HWND hwnd
, int id
)
2035 * Look up the control ID in our data.
2038 for (i
= 0; i
< dp
->nctrltrees
; i
++) {
2039 c
= winctrl_findbyid(dp
->controltrees
[i
], id
);
2044 return 0; /* we have nothing to do */
2047 * This is the Windows front end, so we're allowed to assume
2048 * `helpctx.p' is a context string.
2050 if (!c
->ctrl
|| !c
->ctrl
->generic
.helpctx
.p
)
2051 return 0; /* no help available for this ctrl */
2053 cmd
= dupprintf("JI(`',`%s')", c
->ctrl
->generic
.helpctx
.p
);
2054 WinHelp(hwnd
, help_path
, HELP_COMMAND
, (DWORD
)cmd
);
2060 * Now the various functions that the platform-independent
2061 * mechanism can call to access the dialog box entries.
2064 static struct winctrl
*dlg_findbyctrl(struct dlgparam
*dp
, union control
*ctrl
)
2068 for (i
= 0; i
< dp
->nctrltrees
; i
++) {
2069 struct winctrl
*c
= winctrl_findbyctrl(dp
->controltrees
[i
], ctrl
);
2076 void dlg_radiobutton_set(union control
*ctrl
, void *dlg
, int whichbutton
)
2078 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2079 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2080 assert(c
&& c
->ctrl
->generic
.type
== CTRL_RADIO
);
2081 CheckRadioButton(dp
->hwnd
,
2083 c
->base_id
+ c
->ctrl
->radio
.nbuttons
,
2084 c
->base_id
+ 1 + whichbutton
);
2087 int dlg_radiobutton_get(union control
*ctrl
, void *dlg
)
2089 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2090 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2092 assert(c
&& c
->ctrl
->generic
.type
== CTRL_RADIO
);
2093 for (i
= 0; i
< c
->ctrl
->radio
.nbuttons
; i
++)
2094 if (IsDlgButtonChecked(dp
->hwnd
, c
->base_id
+ 1 + i
))
2096 assert(!"No radio button was checked?!");
2100 void dlg_checkbox_set(union control
*ctrl
, void *dlg
, int checked
)
2102 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2103 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2104 assert(c
&& c
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
2105 CheckDlgButton(dp
->hwnd
, c
->base_id
, (checked
!= 0));
2108 int dlg_checkbox_get(union control
*ctrl
, void *dlg
)
2110 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2111 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2112 assert(c
&& c
->ctrl
->generic
.type
== CTRL_CHECKBOX
);
2113 return 0 != IsDlgButtonChecked(dp
->hwnd
, c
->base_id
);
2116 void dlg_editbox_set(union control
*ctrl
, void *dlg
, char const *text
)
2118 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2119 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2120 assert(c
&& c
->ctrl
->generic
.type
== CTRL_EDITBOX
);
2121 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, text
);
2124 void dlg_editbox_get(union control
*ctrl
, void *dlg
, char *buffer
, int length
)
2126 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2127 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2128 assert(c
&& c
->ctrl
->generic
.type
== CTRL_EDITBOX
);
2129 GetDlgItemText(dp
->hwnd
, c
->base_id
+1, buffer
, length
);
2130 buffer
[length
-1] = '\0';
2133 /* The `listbox' functions can also apply to combo boxes. */
2134 void dlg_listbox_clear(union control
*ctrl
, void *dlg
)
2136 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2137 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2140 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2141 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2142 c
->ctrl
->editbox
.has_list
)));
2143 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2144 LB_RESETCONTENT
: CB_RESETCONTENT
);
2145 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, 0);
2148 void dlg_listbox_del(union control
*ctrl
, void *dlg
, int index
)
2150 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2151 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2154 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2155 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2156 c
->ctrl
->editbox
.has_list
)));
2157 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2158 LB_DELETESTRING
: CB_DELETESTRING
);
2159 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, index
, 0);
2162 void dlg_listbox_add(union control
*ctrl
, void *dlg
, char const *text
)
2164 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2165 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2168 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2169 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2170 c
->ctrl
->editbox
.has_list
)));
2171 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2172 LB_ADDSTRING
: CB_ADDSTRING
);
2173 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, (LPARAM
)text
);
2177 * Each listbox entry may have a numeric id associated with it.
2178 * Note that some front ends only permit a string to be stored at
2179 * each position, which means that _if_ you put two identical
2180 * strings in any listbox then you MUST not assign them different
2181 * IDs and expect to get meaningful results back.
2183 void dlg_listbox_addwithid(union control
*ctrl
, void *dlg
,
2184 char const *text
, int id
)
2186 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2187 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2188 int msg
, msg2
, index
;
2190 (c
->ctrl
->generic
.type
== CTRL_LISTBOX
||
2191 (c
->ctrl
->generic
.type
== CTRL_EDITBOX
&&
2192 c
->ctrl
->editbox
.has_list
)));
2193 msg
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2194 LB_ADDSTRING
: CB_ADDSTRING
);
2195 msg2
= (c
->ctrl
->generic
.type
==CTRL_LISTBOX
&& c
->ctrl
->listbox
.height
!=0 ?
2196 LB_SETITEMDATA
: CB_SETITEMDATA
);
2197 index
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, (LPARAM
)text
);
2198 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg2
, index
, (LPARAM
)id
);
2201 int dlg_listbox_getid(union control
*ctrl
, void *dlg
, int index
)
2203 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2204 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2206 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
);
2207 msg
= (c
->ctrl
->listbox
.height
!= 0 ? LB_GETITEMDATA
: CB_GETITEMDATA
);
2209 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, index
, 0);
2212 /* dlg_listbox_index returns <0 if no single element is selected. */
2213 int dlg_listbox_index(union control
*ctrl
, void *dlg
)
2215 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2216 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2218 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
&&
2219 !c
->ctrl
->listbox
.multisel
);
2220 msg
= (c
->ctrl
->listbox
.height
!= 0 ? LB_GETCURSEL
: CB_GETCURSEL
);
2221 ret
= SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, 0, 0);
2228 int dlg_listbox_issel(union control
*ctrl
, void *dlg
, int index
)
2230 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2231 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2232 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
&&
2233 c
->ctrl
->listbox
.multisel
&&
2234 c
->ctrl
->listbox
.height
!= 0);
2236 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, LB_GETSEL
, index
, 0);
2239 void dlg_listbox_select(union control
*ctrl
, void *dlg
, int index
)
2241 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2242 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2244 assert(c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
&&
2245 !c
->ctrl
->listbox
.multisel
);
2246 msg
= (c
->ctrl
->listbox
.height
!= 0 ? LB_SETCURSEL
: CB_SETCURSEL
);
2247 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, msg
, index
, 0);
2250 void dlg_text_set(union control
*ctrl
, void *dlg
, char const *text
)
2252 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2253 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2254 assert(c
&& c
->ctrl
->generic
.type
== CTRL_TEXT
);
2255 SetDlgItemText(dp
->hwnd
, c
->base_id
, text
);
2258 void dlg_filesel_set(union control
*ctrl
, void *dlg
, Filename fn
)
2260 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2261 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2262 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FILESELECT
);
2263 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, fn
.path
);
2266 void dlg_filesel_get(union control
*ctrl
, void *dlg
, Filename
*fn
)
2268 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2269 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2270 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FILESELECT
);
2271 GetDlgItemText(dp
->hwnd
, c
->base_id
+1, fn
->path
, lenof(fn
->path
));
2272 fn
->path
[lenof(fn
->path
)-1] = '\0';
2275 void dlg_fontsel_set(union control
*ctrl
, void *dlg
, FontSpec fs
)
2277 char *buf
, *boldstr
;
2278 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2279 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2280 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
2282 *(FontSpec
*)c
->data
= fs
; /* structure copy */
2284 boldstr
= (fs
.isbold ?
"bold, " : "");
2286 buf
= dupprintf("Font: %s, %sdefault height", fs
.name
, boldstr
);
2288 buf
= dupprintf("Font: %s, %s%d-point", fs
.name
, boldstr
,
2289 (fs
.height
< 0 ?
-fs
.height
: fs
.height
));
2290 SetDlgItemText(dp
->hwnd
, c
->base_id
+1, buf
);
2294 void dlg_fontsel_get(union control
*ctrl
, void *dlg
, FontSpec
*fs
)
2296 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2297 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2298 assert(c
&& c
->ctrl
->generic
.type
== CTRL_FONTSELECT
);
2299 *fs
= *(FontSpec
*)c
->data
; /* structure copy */
2303 * Bracketing a large set of updates in these two functions will
2304 * cause the front end (if possible) to delay updating the screen
2305 * until it's all complete, thus avoiding flicker.
2307 void dlg_update_start(union control
*ctrl
, void *dlg
)
2309 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2310 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2311 if (c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
) {
2312 SendDlgItemMessage(dp
->hwnd
, c
->base_id
+1, WM_SETREDRAW
, FALSE
, 0);
2316 void dlg_update_done(union control
*ctrl
, void *dlg
)
2318 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2319 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2320 if (c
&& c
->ctrl
->generic
.type
== CTRL_LISTBOX
) {
2321 HWND hw
= GetDlgItem(dp
->hwnd
, c
->base_id
+1);
2322 SendMessage(hw
, WM_SETREDRAW
, TRUE
, 0);
2323 InvalidateRect(hw
, NULL
, TRUE
);
2327 void dlg_set_focus(union control
*ctrl
, void *dlg
)
2329 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2330 struct winctrl
*c
= dlg_findbyctrl(dp
, ctrl
);
2333 switch (ctrl
->generic
.type
) {
2334 case CTRL_EDITBOX
: id
= c
->base_id
+ 1; break;
2336 for (id
= c
->base_id
+ ctrl
->radio
.nbuttons
; id
> 1; id
--)
2337 if (IsDlgButtonChecked(dp
->hwnd
, id
))
2340 * In the theoretically-unlikely case that no button was
2341 * selected, id should come out of this as 1, which is a
2342 * reasonable enough choice.
2345 case CTRL_CHECKBOX
: id
= c
->base_id
; break;
2346 case CTRL_BUTTON
: id
= c
->base_id
; break;
2347 case CTRL_LISTBOX
: id
= c
->base_id
+ 1; break;
2348 case CTRL_FILESELECT
: id
= c
->base_id
+ 1; break;
2349 case CTRL_FONTSELECT
: id
= c
->base_id
+ 2; break;
2350 default: id
= c
->base_id
; break;
2352 ctl
= GetDlgItem(dp
->hwnd
, id
);
2357 * During event processing, you might well want to give an error
2358 * indication to the user. dlg_beep() is a quick and easy generic
2359 * error; dlg_error() puts up a message-box or equivalent.
2361 void dlg_beep(void *dlg
)
2363 /* struct dlgparam *dp = (struct dlgparam *)dlg; */
2367 void dlg_error_msg(void *dlg
, char *msg
)
2369 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2370 MessageBox(dp
->hwnd
, msg
,
2371 dp
->errtitle ? dp
->errtitle
: NULL
,
2372 MB_OK
| MB_ICONERROR
);
2376 * This function signals to the front end that the dialog's
2377 * processing is completed, and passes an integer value (typically
2378 * a success status).
2380 void dlg_end(void *dlg
, int value
)
2382 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2384 dp
->endresult
= value
;
2387 void dlg_refresh(union control
*ctrl
, void *dlg
)
2389 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2395 * Send EVENT_REFRESH to absolutely everything.
2397 for (j
= 0; j
< dp
->nctrltrees
; j
++) {
2399 (c
= winctrl_findbyindex(dp
->controltrees
[j
], i
)) != NULL
;
2401 if (c
->ctrl
&& c
->ctrl
->generic
.handler
!= NULL
)
2402 c
->ctrl
->generic
.handler(c
->ctrl
, dp
,
2403 dp
->data
, EVENT_REFRESH
);
2408 * Send EVENT_REFRESH to a specific control.
2410 if (ctrl
->generic
.handler
!= NULL
)
2411 ctrl
->generic
.handler(ctrl
, dp
, dp
->data
, EVENT_REFRESH
);
2415 void dlg_coloursel_start(union control
*ctrl
, void *dlg
, int r
, int g
, int b
)
2417 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2418 dp
->coloursel_wanted
= TRUE
;
2419 dp
->coloursel_result
.r
= r
;
2420 dp
->coloursel_result
.g
= g
;
2421 dp
->coloursel_result
.b
= b
;
2424 int dlg_coloursel_results(union control
*ctrl
, void *dlg
,
2425 int *r
, int *g
, int *b
)
2427 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2428 if (dp
->coloursel_result
.ok
) {
2429 *r
= dp
->coloursel_result
.r
;
2430 *g
= dp
->coloursel_result
.g
;
2431 *b
= dp
->coloursel_result
.b
;
2437 struct perctrl_privdata
{
2438 union control
*ctrl
;
2443 static int perctrl_privdata_cmp(void *av
, void *bv
)
2445 struct perctrl_privdata
*a
= (struct perctrl_privdata
*)av
;
2446 struct perctrl_privdata
*b
= (struct perctrl_privdata
*)bv
;
2447 if (a
->ctrl
< b
->ctrl
)
2449 else if (a
->ctrl
> b
->ctrl
)
2454 void dp_init(struct dlgparam
*dp
)
2459 dp
->focused
= dp
->lastfocused
= NULL
;
2460 memset(dp
->shortcuts
, 0, sizeof(dp
->shortcuts
));
2462 dp
->wintitle
= dp
->errtitle
= NULL
;
2463 dp
->privdata
= newtree234(perctrl_privdata_cmp
);
2466 void dp_add_tree(struct dlgparam
*dp
, struct winctrls
*wc
)
2468 assert(dp
->nctrltrees
< lenof(dp
->controltrees
));
2469 dp
->controltrees
[dp
->nctrltrees
++] = wc
;
2472 void dp_cleanup(struct dlgparam
*dp
)
2474 struct perctrl_privdata
*p
;
2477 while ( (p
= index234(dp
->privdata
, 0)) != NULL
) {
2478 del234(dp
->privdata
, p
);
2483 freetree234(dp
->privdata
);
2484 dp
->privdata
= NULL
;
2486 sfree(dp
->wintitle
);
2487 sfree(dp
->errtitle
);
2490 void *dlg_get_privdata(union control
*ctrl
, void *dlg
)
2492 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2493 struct perctrl_privdata tmp
, *p
;
2495 p
= find234(dp
->privdata
, &tmp
, NULL
);
2502 void dlg_set_privdata(union control
*ctrl
, void *dlg
, void *ptr
)
2504 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2505 struct perctrl_privdata tmp
, *p
;
2507 p
= find234(dp
->privdata
, &tmp
, NULL
);
2509 p
= snew(struct perctrl_privdata
);
2511 p
->needs_free
= FALSE
;
2512 add234(dp
->privdata
, p
);
2517 void *dlg_alloc_privdata(union control
*ctrl
, void *dlg
, size_t size
)
2519 struct dlgparam
*dp
= (struct dlgparam
*)dlg
;
2520 struct perctrl_privdata tmp
, *p
;
2522 p
= find234(dp
->privdata
, &tmp
, NULL
);
2524 p
= snew(struct perctrl_privdata
);
2526 p
->needs_free
= FALSE
;
2527 add234(dp
->privdata
, p
);
2529 assert(!p
->needs_free
);
2530 p
->needs_free
= TRUE
;
2532 * This is an internal allocation routine, so it's allowed to
2533 * use smalloc directly.
2535 p
->data
= smalloc(size
);