-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinctrls.c
1227 lines (1100 loc) · 32.4 KB
/
winctrls.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// winctrls.c (part of mintty)
// Copyright 2008-11 Andy Koppe
// Adapted from code from PuTTY-0.60 by Simon Tatham and team.
// Licensed under the terms of the GNU General Public License v3 or later.
#include "winctrls.h"
#include "winpriv.h"
#define _RPCNDR_H
#define _WTYPES_H
#define _OLE2_H
#include <commdlg.h>
/*
* winctrls.c: routines to self-manage the controls in a dialog
* box.
*/
/*
* Possible TODO in new cross-platform config box stuff:
*
* - When lining up two controls alongside each other, I wonder if
* we could conveniently arrange to centre them vertically?
* Particularly ugly in the current setup is the `Add new
* forwarded port:' static next to the rather taller `Remove'
* button.
*/
#define GAPBETWEEN 3
#define GAPWITHIN 1
#define GAPXBOX 7
#define GAPYBOX 4
#define DLGWIDTH 168
#define STATICHEIGHT 8
#define TITLEHEIGHT 12
#define CHECKBOXHEIGHT 8
#define RADIOHEIGHT 8
#define EDITHEIGHT 12
#define LISTHEIGHT 11
#define LISTINCREMENT 8
#define COMBOHEIGHT 12
#define PUSHBTNHEIGHT 14
#define PROGBARHEIGHT 14
void
ctrlposinit(ctrlpos * cp, HWND wnd, int leftborder, int rightborder,
int topborder)
{
RECT r, r2;
cp->wnd = wnd;
cp->font = SendMessage(wnd, WM_GETFONT, 0, 0);
cp->ypos = topborder;
GetClientRect(wnd, &r);
r2.left = r2.top = 0;
r2.right = 4;
r2.bottom = 8;
MapDialogRect(wnd, &r2);
cp->dlu4inpix = r2.right;
cp->width = (r.right * 4) / (r2.right) - 2 * GAPBETWEEN;
cp->xoff = leftborder;
cp->width -= leftborder + rightborder;
}
static HWND
doctl(ctrlpos * cp, RECT r, char *wclass, int wstyle, int exstyle,
string wtext, int wid)
{
HWND ctl;
/*
* Note nonstandard use of RECT. This is deliberate: by
* transforming the width and height directly we arrange to
* have all supposedly same-sized controls really same-sized.
*/
r.left += cp->xoff;
MapDialogRect(cp->wnd, &r);
/*
* We can pass in cp->wnd == null, to indicate a dry run
* without creating any actual controls.
*/
if (cp->wnd) {
ctl =
CreateWindowEx(exstyle, wclass, wtext, wstyle, r.left, r.top, r.right,
r.bottom, cp->wnd, (HMENU)(INT_PTR)wid, inst, null);
SendMessage(ctl, WM_SETFONT, cp->font, MAKELPARAM(true, 0));
if (!strcmp(wclass, "LISTBOX")) {
/*
* Bizarre Windows bug: the list box calculates its
* number of lines based on the font it has at creation
* time, but sending it WM_SETFONT doesn't cause it to
* recalculate. So now, _after_ we've sent it
* WM_SETFONT, we explicitly resize it (to the same
* size it was already!) to force it to reconsider.
*/
SetWindowPos(ctl, null, 0, 0, r.right, r.bottom,
SWP_NOACTIVATE | SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOZORDER);
}
}
else
ctl = null;
return ctl;
}
/*
* Begin a grouping box, with or without a group title.
*/
static void
beginbox(ctrlpos * cp, char *name, int idbox)
{
cp->boxystart = cp->ypos;
if (!name)
cp->boxystart -= STATICHEIGHT / 2;
if (name)
cp->ypos += STATICHEIGHT;
cp->ypos += GAPYBOX;
cp->width -= 2 * GAPXBOX;
cp->xoff += GAPXBOX;
cp->boxid = idbox;
cp->boxtext = name;
}
/*
* End a grouping box.
*/
static void
endbox(ctrlpos * cp)
{
RECT r;
cp->xoff -= GAPXBOX;
cp->width += 2 * GAPXBOX;
cp->ypos += GAPYBOX - GAPBETWEEN;
r.left = GAPBETWEEN;
r.right = cp->width;
r.top = cp->boxystart;
r.bottom = cp->ypos - cp->boxystart;
doctl(cp, r, "BUTTON", BS_GROUPBOX | WS_CHILD | WS_VISIBLE, 0,
cp->boxtext ? cp->boxtext : "", cp->boxid);
cp->ypos += GAPYBOX;
}
/*
* A static line, followed by a full-width edit box.
*/
static void
editboxfw(ctrlpos * cp, int password, char *text, int staticid, int editid)
{
RECT r;
r.left = GAPBETWEEN;
r.right = cp->width;
if (text) {
r.top = cp->ypos;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
cp->ypos += STATICHEIGHT + GAPWITHIN;
}
r.top = cp->ypos;
r.bottom = EDITHEIGHT;
doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | (password ?
ES_PASSWORD : 0),
WS_EX_CLIENTEDGE, "", editid);
cp->ypos += EDITHEIGHT + GAPBETWEEN;
}
/*
* A static line, followed by a full-width combo box.
*/
static void
combobox(ctrlpos * cp, char *text, int staticid, int listid)
{
RECT r;
r.left = GAPBETWEEN;
r.right = cp->width;
if (text) {
r.top = cp->ypos;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, staticid);
cp->ypos += STATICHEIGHT + GAPWITHIN;
}
r.top = cp->ypos;
r.bottom = COMBOHEIGHT * 10;
doctl(cp, r, "COMBOBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWN |
CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", listid);
cp->ypos += COMBOHEIGHT + GAPBETWEEN;
}
typedef struct {
string label;
int id;
} radio;
static void
radioline_common(ctrlpos * cp, char *text, int id, int nacross,
radio * buttons, int nbuttons)
{
RECT r;
int group;
int i;
int j;
if (text) {
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, text, id);
}
group = WS_GROUP;
i = 0;
for (j = 0; j < nbuttons; j++) {
if (i == nacross) {
cp->ypos += r.bottom + (nacross > 1 ? GAPBETWEEN : GAPWITHIN);
i = 0;
}
r.left = GAPBETWEEN + i * (cp->width + GAPBETWEEN) / nacross;
if (j < nbuttons - 1)
r.right = (i + 1) * (cp->width + GAPBETWEEN) / nacross - r.left;
else
r.right = cp->width - r.left;
r.top = cp->ypos;
r.bottom = RADIOHEIGHT;
doctl(cp, r, "BUTTON",
BS_NOTIFY | BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
group, 0, buttons[j].label, buttons[j].id);
group = 0;
i++;
}
cp->ypos += r.bottom + GAPBETWEEN;
}
/*
* A single standalone checkbox.
*/
static void
checkbox(ctrlpos * cp, char *text, int id)
{
RECT r;
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = CHECKBOXHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "BUTTON",
BS_NOTIFY | BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 0,
text, id);
}
/*
* An owner-drawn static text control for a panel title.
*/
static void
paneltitle(ctrlpos * cp, int id)
{
RECT r;
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = TITLEHEIGHT;
cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE | SS_OWNERDRAW, 0, null, id);
}
/*
* A button on the right hand side, with a static to its left.
*/
static void
staticbtn(ctrlpos * cp, char *stext, int sid, char *btext, int bid)
{
const int height =
(PUSHBTNHEIGHT > STATICHEIGHT ? PUSHBTNHEIGHT : STATICHEIGHT);
RECT r;
int lwid, rwid, rpos;
rpos = GAPBETWEEN + 3 * (cp->width + GAPBETWEEN) / 4;
lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN;
r.top = cp->ypos + (height - STATICHEIGHT) / 2 - 1;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos;
r.top = cp->ypos + (height - PUSHBTNHEIGHT) / 2 - 1;
r.right = rwid;
r.bottom = PUSHBTNHEIGHT;
doctl(cp, r, "BUTTON",
BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_PUSHBUTTON, 0,
btext, bid);
cp->ypos += height + GAPBETWEEN;
}
/*
* A simple push button.
*/
static void
button(ctrlpos * cp, char *btext, int bid, int defbtn)
{
RECT r;
r.left = GAPBETWEEN;
r.top = cp->ypos - 1;
r.right = cp->width;
r.bottom = PUSHBTNHEIGHT;
/* Q67655: the _dialog box_ must know which button is default
* as well as the button itself knowing */
if (defbtn && cp->wnd)
SendMessage(cp->wnd, DM_SETDEFID, bid, 0);
doctl(cp, r, "BUTTON",
BS_NOTIFY | WS_CHILD | WS_VISIBLE | WS_TABSTOP |
(defbtn ? BS_DEFPUSHBUTTON : 0) | BS_PUSHBUTTON, 0, btext, bid);
cp->ypos += PUSHBTNHEIGHT + GAPBETWEEN;
}
/*
* An edit control on the right hand side, with a static to its left.
*/
static void
staticedit_internal(ctrlpos * cp, char *stext, int sid, int eid,
int percentedit, int style)
{
const int height = (EDITHEIGHT > STATICHEIGHT ? EDITHEIGHT : STATICHEIGHT);
RECT r;
int lwid, rwid, rpos;
rpos = GAPBETWEEN + (100 - percentedit) * (cp->width + GAPBETWEEN) / 100;
lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN;
r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos;
r.top = cp->ypos + (height - EDITHEIGHT) / 2;
r.right = rwid;
r.bottom = EDITHEIGHT;
doctl(cp, r, "EDIT",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_AUTOHSCROLL | style,
WS_EX_CLIENTEDGE, "", eid);
cp->ypos += height + GAPBETWEEN;
}
static void
staticedit(ctrlpos * cp, char *stext, int sid, int eid, int percentedit)
{
staticedit_internal(cp, stext, sid, eid, percentedit, 0);
}
static void
staticpassedit(ctrlpos * cp, char *stext, int sid, int eid, int percentedit)
{
staticedit_internal(cp, stext, sid, eid, percentedit, ES_PASSWORD);
}
/*
* A drop-down list box on the right hand side, with a static to
* its left.
*/
static void
staticddl(ctrlpos * cp, char *stext, int sid, int lid, int percentlist)
{
const int height = (COMBOHEIGHT > STATICHEIGHT ? COMBOHEIGHT : STATICHEIGHT);
RECT r;
int lwid, rwid, rpos;
rpos = GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN;
r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos;
r.top = cp->ypos + (height - EDITHEIGHT) / 2;
r.right = rwid;
r.bottom = COMBOHEIGHT * 4;
doctl(cp, r, "COMBOBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWNLIST |
CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
cp->ypos += height + GAPBETWEEN;
}
/*
* A combo box on the right hand side, with a static to its left.
*/
static void
staticcombo(ctrlpos * cp, char *stext, int sid, int lid, int percentlist)
{
const int height = (COMBOHEIGHT > STATICHEIGHT ? COMBOHEIGHT : STATICHEIGHT);
RECT r;
int lwid, rwid, rpos;
rpos = GAPBETWEEN + (100 - percentlist) * (cp->width + GAPBETWEEN) / 100;
lwid = rpos - 2 * GAPBETWEEN;
rwid = cp->width + GAPBETWEEN - rpos;
r.left = GAPBETWEEN;
r.top = cp->ypos + (height - STATICHEIGHT) / 2;
r.right = lwid;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
r.left = rpos;
r.top = cp->ypos + (height - EDITHEIGHT) / 2;
r.right = rwid;
r.bottom = COMBOHEIGHT * 10;
doctl(cp, r, "COMBOBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWN |
CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
cp->ypos += height + GAPBETWEEN;
}
/*
* A static, with a full-width drop-down list box below it.
*/
static void
staticddlbig(ctrlpos * cp, char *stext, int sid, int lid)
{
RECT r;
if (stext) {
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
cp->ypos += STATICHEIGHT;
}
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = COMBOHEIGHT * 4;
doctl(cp, r, "COMBOBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | CBS_DROPDOWNLIST |
CBS_HASSTRINGS, WS_EX_CLIENTEDGE, "", lid);
cp->ypos += COMBOHEIGHT + GAPBETWEEN;
}
/*
* A list box with a static labelling it.
*/
static void
listbox(ctrlpos * cp, char *stext, int sid, int lid, int lines)
{
RECT r;
if (stext != null) {
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = STATICHEIGHT;
cp->ypos += r.bottom + GAPWITHIN;
doctl(cp, r, "STATIC", WS_CHILD | WS_VISIBLE, 0, stext, sid);
}
r.left = GAPBETWEEN;
r.top = cp->ypos;
r.right = cp->width;
r.bottom = LISTHEIGHT + (lines - 1) * LISTINCREMENT;
cp->ypos += r.bottom + GAPBETWEEN;
doctl(cp, r, "LISTBOX",
WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_VSCROLL | LBS_NOTIFY |
LBS_HASSTRINGS | LBS_USETABSTOPS, WS_EX_CLIENTEDGE, "", lid);
}
/* ----------------------------------------------------------------------
* Platform-specific side of portable dialog-box mechanism.
*/
void
winctrl_init(winctrls *wc)
{
wc->first = wc->last = null;
}
void
winctrl_cleanup(winctrls *wc)
{
winctrl *c = wc->first;
while (c) {
winctrl *next = c->next;
if (c->ctrl)
c->ctrl->plat_ctrl = null;
free(c->data);
free(c);
c = next;
}
wc->first = wc->last = null;
}
static void
winctrl_add(winctrls *wc, winctrl *c)
{
if (wc->last)
wc->last->next = c;
else
wc->first = c;
wc->last = c;
if (c->ctrl)
c->ctrl->plat_ctrl = c;
}
static winctrl *
winctrl_findbyid(winctrls *wc, int id)
{
for (winctrl *c = wc->first; c; c = c->next) {
if (id >= c->base_id && id < c->base_id + c->num_ids)
return c;
}
return 0;
}
static winctrl *
new_winctrl(int base_id, void *data)
{
winctrl *c = new(winctrl);
c->next = null;
c->ctrl = null;
c->base_id = base_id;
c->num_ids = 1;
c->data = data;
return c;
}
void
winctrl_layout(winctrls *wc, ctrlpos *cp, controlset *s, int *id)
{
ctrlpos columns[16];
int ncols, colstart, colspan;
ctrlpos pos;
int actual_base_id, base_id, num_ids;
void *data;
base_id = *id;
if (!s->ncolumns) {
/* Draw a title. */
winctrl *c = new_winctrl(base_id, strdup(s->boxtitle));
winctrl_add(wc, c);
paneltitle(cp, base_id);
base_id++;
}
else if (*s->pathname) {
/* Start a containing box. */
winctrl *c = new_winctrl(base_id, null);
winctrl_add(wc, c);
beginbox(cp, s->boxtitle, base_id);
base_id++;
}
/* Initially we have just one column. */
ncols = 1;
columns[0] = *cp; /* structure copy */
/* Loop over each control in the controlset. */
for (int i = 0; i < s->ncontrols; i++) {
control *ctrl = s->ctrls[i];
/*
* Generic processing that pertains to all control types.
* At the end of this if statement, we'll have produced
* `ctrl' (a pointer to the control we have to create, or
* think about creating, in this iteration of the loop),
* `pos' (a suitable ctrlpos with which to position it), and
* `c' (a winctrl structure to receive details of the
* dialog IDs). Or we'll have done a `continue', if it was
* CTRL_COLUMNS and doesn't require any control creation at
* all.
*/
if (ctrl->type == CTRL_COLUMNS) {
assert((ctrl->columns.ncols == 1) ^ (ncols == 1));
if (ncols == 1) {
/*
* We're splitting into multiple columns.
*/
int lpercent, rpercent, lx, rx, i;
ncols = ctrl->columns.ncols;
assert(ncols <= (int) lengthof(columns));
for (i = 1; i < ncols; i++)
columns[i] = columns[0]; /* structure copy */
lpercent = 0;
for (i = 0; i < ncols; i++) {
rpercent = lpercent + ctrl->columns.percentages[i];
lx =
columns[i].xoff + lpercent * (columns[i].width + GAPBETWEEN) / 100;
rx =
columns[i].xoff + rpercent * (columns[i].width + GAPBETWEEN) / 100;
columns[i].xoff = lx;
columns[i].width = rx - lx - GAPBETWEEN;
lpercent = rpercent;
}
}
else {
/*
* We're recombining the various columns into one.
*/
int maxy = columns[0].ypos;
int i;
for (i = 1; i < ncols; i++)
if (maxy < columns[i].ypos)
maxy = columns[i].ypos;
ncols = 1;
columns[0] = *cp; /* structure copy */
columns[0].ypos = maxy;
}
continue;
}
else {
/*
* If it wasn't one of those, it's a genuine control;
* so we'll have to compute a position for it now, by
* checking its column span.
*/
int col;
colstart = COLUMN_START(ctrl->column);
colspan = COLUMN_SPAN(ctrl->column);
pos = columns[colstart]; /* structure copy */
pos.width =
columns[colstart + colspan - 1].width +
(columns[colstart + colspan - 1].xoff - columns[colstart].xoff);
for (col = colstart; col < colstart + colspan; col++)
if (pos.ypos < columns[col].ypos)
pos.ypos = columns[col].ypos;
}
/* Most controls don't need anything in c->data. */
data = null;
/* Almost all controls start at base_id. */
actual_base_id = base_id;
/*
* Now we're ready to actually create the control, by
* switching on its type.
*/
switch (ctrl->type) {
when CTRL_EDITBOX: {
num_ids = 2; /* static, edit */
if (ctrl->editbox.percentwidth == 100) {
if (ctrl->editbox.has_list)
combobox(&pos, ctrl->label, base_id, base_id + 1);
else
editboxfw(&pos, ctrl->editbox.password, ctrl->label, base_id,
base_id + 1);
}
else {
if (ctrl->editbox.has_list) {
staticcombo(&pos, ctrl->label, base_id, base_id + 1,
ctrl->editbox.percentwidth);
}
else {
(ctrl->editbox.password ? staticpassedit : staticedit)
(&pos, ctrl->label, base_id, base_id + 1, ctrl->editbox.percentwidth);
}
}
}
when CTRL_RADIO: {
num_ids = ctrl->radio.nbuttons + 1; /* label as well */
radio buttons[ctrl->radio.nbuttons];
for (int i = 0; i < ctrl->radio.nbuttons; i++) {
buttons[i].label = ctrl->radio.labels[i];
buttons[i].id = base_id + 1 + i;
}
radioline_common(&pos, ctrl->label, base_id, ctrl->radio.ncolumns,
buttons, ctrl->radio.nbuttons);
}
when CTRL_CHECKBOX: {
num_ids = 1;
checkbox(&pos, ctrl->label, base_id);
}
when CTRL_BUTTON: {
if (ctrl->button.iscancel)
actual_base_id = IDCANCEL;
num_ids = 1;
button(&pos, ctrl->label, actual_base_id, ctrl->button.isdefault);
}
when CTRL_LISTBOX: {
num_ids = 2;
if (ctrl->listbox.height == 0) {
/* Drop-down list. */
if (ctrl->listbox.percentwidth == 100)
staticddlbig(&pos, ctrl->label, base_id, base_id + 1);
else
staticddl(&pos, ctrl->label, base_id, base_id + 1,
ctrl->listbox.percentwidth);
}
else {
/* Ordinary list. */
listbox(&pos, ctrl->label, base_id, base_id + 1, ctrl->listbox.height);
}
if (ctrl->listbox.ncols) {
/*
* This method of getting the box width is a bit of
* a hack; we'd do better to try to retrieve the
* actual width in dialog units from doctl() just
* before MapDialogRect. But that's going to be no
* fun, and this should be good enough accuracy.
*/
int width = cp->width * ctrl->listbox.percentwidth;
int tabarray[ctrl->listbox.ncols - 1];
int percent = 0;
for (int i = 0; i < ctrl->listbox.ncols - 1; i++) {
percent += ctrl->listbox.percentages[i];
tabarray[i] = width * percent / 10000;
}
SendDlgItemMessage(cp->wnd, base_id + 1, LB_SETTABSTOPS,
ctrl->listbox.ncols - 1, (LPARAM) tabarray);
}
}
when CTRL_FONTSELECT: {
num_ids = 3;
staticbtn(&pos, "", base_id + 1, "&Select...", base_id + 2);
data = new(font_spec);
}
otherwise:
assert(!"Can't happen");
num_ids = 0; /* placate gcc */
}
/*
* Create a `winctrl' for this control, and advance
* the dialog ID counter, if it's actually been created
*/
if (pos.wnd) {
winctrl *c = new(winctrl);
c->next = null;
c->ctrl = ctrl;
c->base_id = actual_base_id;
c->num_ids = num_ids;
c->data = data;
winctrl_add(wc, c);
if (actual_base_id == base_id)
base_id += num_ids;
}
if (colstart >= 0) {
/*
* Update the ypos in all columns crossed by this
* control.
*/
int i;
for (i = colstart; i < colstart + colspan; i++)
columns[i].ypos = pos.ypos;
}
}
/*
* We've now finished laying out the controls; so now update
* the ctrlpos and control ID that were passed in, terminate
* any containing box, and return.
*/
for (int i = 0; i < ncols; i++)
if (cp->ypos < columns[i].ypos)
cp->ypos = columns[i].ypos;
*id = base_id;
if (s->ncolumns && *s->pathname)
endbox(cp);
}
static void
winctrl_set_focus(control *ctrl, int has_focus)
{
if (has_focus)
dlg.focused = ctrl;
else if (!has_focus && dlg.focused == ctrl)
dlg.focused = null;
}
static void
select_font(winctrl *c)
{
font_spec fs = *(font_spec *) c->data;
HDC dc = GetDC(0);
LOGFONT lf;
lf.lfHeight = -MulDiv(fs.size, GetDeviceCaps(dc, LOGPIXELSY), 72);
ReleaseDC(0, dc);
lf.lfWidth = lf.lfEscapement = lf.lfOrientation = 0;
lf.lfItalic = lf.lfUnderline = lf.lfStrikeOut = 0;
lf.lfWeight = (fs.isbold ? FW_BOLD : 0);
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = DEFAULT_QUALITY;
lf.lfPitchAndFamily = FIXED_PITCH | FF_DONTCARE;
strlcpy(lf.lfFaceName, fs.name, sizeof lf.lfFaceName);
CHOOSEFONT cf;
cf.lStructSize = sizeof (cf);
cf.hwndOwner = dlg.wnd;
cf.lpLogFont = &lf;
cf.Flags =
CF_FIXEDPITCHONLY | CF_FORCEFONTEXIST | CF_INITTOLOGFONTSTRUCT |
CF_SCREENFONTS | CF_NOSCRIPTSEL;
if (ChooseFont(&cf)) {
strset(&fs.name, lf.lfFaceName);
fs.isbold = (lf.lfWeight == FW_BOLD);
fs.size = cf.iPointSize / 10;
dlg_fontsel_set(c->ctrl, &fs);
c->ctrl->handler(c->ctrl, EVENT_VALCHANGE);
}
}
/*
* The dialog-box procedure calls this function to handle Windows
* messages on a control we manage.
*/
int
winctrl_handle_command(UINT msg, WPARAM wParam, LPARAM lParam)
{
winctrl *c;
control *ctrl;
int i, id, ret;
/*
* Look up the control ID in our data.
*/
c = null;
for (i = 0; i < dlg.nctrltrees; i++) {
c = winctrl_findbyid(dlg.controltrees[i], LOWORD(wParam));
if (c)
break;
}
if (!c)
return 0; /* we have nothing to do */
if (msg == WM_DRAWITEM) {
/*
* Owner-draw request for a panel title.
*/
LPDRAWITEMSTRUCT di = (LPDRAWITEMSTRUCT) lParam;
HDC dc = di->hDC;
RECT r = di->rcItem;
SIZE s;
SetMapMode(dc, MM_TEXT); /* ensure logical units == pixels */
GetTextExtentPoint32(dc, (char *) c->data, strlen((char *) c->data), &s);
DrawEdge(dc, &r, EDGE_ETCHED, BF_ADJUST | BF_RECT);
TextOut(dc, r.left + (r.right - r.left - s.cx) / 2,
r.top + (r.bottom - r.top - s.cy) / 2, (char *) c->data,
strlen((char *) c->data));
return true;
}
ctrl = c->ctrl;
id = LOWORD(wParam) - c->base_id;
if (!ctrl || !ctrl->handler)
return 0; /* nothing we can do here */
/*
* From here on we do not issue `return' statements until the
* very end of the dialog box: any event handler is entitled to
* ask for a colour selector, so we _must_ always allow control
* to reach the end of this switch statement so that the
* subsequent code can test dlg.coloursel_wanted().
*/
ret = 0;
dlg.coloursel_wanted = false;
/*
* Now switch on the control type and the message.
*/
if (msg == WM_COMMAND) {
WORD note = HIWORD(wParam);
switch (ctrl->type) {
when CTRL_RADIO:
switch (note) {
when BN_SETFOCUS or BN_KILLFOCUS:
winctrl_set_focus(ctrl, note == BN_SETFOCUS);
when BN_CLICKED or BN_DOUBLECLICKED:
/*
* We sometimes get spurious BN_CLICKED messages for the
* radio button that is just about to _lose_ selection, if
* we're switching using the arrow keys. Therefore we
* double-check that the button in wParam is actually
* checked before generating an event.
*/
if (IsDlgButtonChecked(dlg.wnd, LOWORD(wParam)))
ctrl->handler(ctrl, EVENT_VALCHANGE);
}
when CTRL_CHECKBOX:
switch (note) {
when BN_SETFOCUS or BN_KILLFOCUS:
winctrl_set_focus(ctrl, note == BN_SETFOCUS);
when BN_CLICKED or BN_DOUBLECLICKED:
ctrl->handler(ctrl, EVENT_VALCHANGE);
}
when CTRL_BUTTON:
switch (note) {
when BN_SETFOCUS or BN_KILLFOCUS:
winctrl_set_focus(ctrl, note == BN_SETFOCUS);
when BN_CLICKED or BN_DOUBLECLICKED:
ctrl->handler(ctrl, EVENT_ACTION);
}
when CTRL_FONTSELECT:
if (id == 2) {
switch (note) {
when BN_SETFOCUS or BN_KILLFOCUS:
winctrl_set_focus(ctrl, note == BN_SETFOCUS);
when BN_CLICKED or BN_DOUBLECLICKED:
select_font(c);
}
}
when CTRL_LISTBOX:
if (ctrl->listbox.height != 0 &&
(note == LBN_SETFOCUS || note == LBN_KILLFOCUS))
winctrl_set_focus(ctrl, note == LBN_SETFOCUS);
else if (ctrl->listbox.height == 0 &&
(note == CBN_SETFOCUS || note == CBN_KILLFOCUS))
winctrl_set_focus(ctrl, note == CBN_SETFOCUS);
else if (id >= 2 && (note == BN_SETFOCUS || note == BN_KILLFOCUS))
winctrl_set_focus(ctrl, note == BN_SETFOCUS);
else if (note == LBN_DBLCLK) {
SetCapture(dlg.wnd);
ctrl->handler(ctrl, EVENT_ACTION);
}
else if (note == LBN_SELCHANGE)
ctrl->handler(ctrl, EVENT_SELCHANGE);
when CTRL_EDITBOX:
if (ctrl->editbox.has_list) {
switch (note) {
when CBN_SETFOCUS:
winctrl_set_focus(ctrl, true);
when CBN_KILLFOCUS:
winctrl_set_focus(ctrl, false);
ctrl->handler(ctrl, EVENT_UNFOCUS);
when CBN_SELCHANGE: {
int index = SendDlgItemMessage(
dlg.wnd, c->base_id + 1, CB_GETCURSEL, 0, 0);
int len = SendDlgItemMessage(
dlg.wnd, c->base_id + 1, CB_GETLBTEXTLEN, index, 0);
char text[len + 1];
SendDlgItemMessage(
dlg.wnd, c->base_id + 1, CB_GETLBTEXT, index, (LPARAM) text);
SetDlgItemText(dlg.wnd, c->base_id + 1, text);
ctrl->handler(ctrl, EVENT_SELCHANGE);
}
when CBN_EDITCHANGE:
ctrl->handler(ctrl, EVENT_VALCHANGE);
}
}
else {
switch (note) {
when EN_SETFOCUS:
winctrl_set_focus(ctrl, true);
when EN_KILLFOCUS:
winctrl_set_focus(ctrl, false);
ctrl->handler(ctrl, EVENT_UNFOCUS);
when EN_CHANGE:
ctrl->handler(ctrl, EVENT_VALCHANGE);
}
}
}
}
/*
* If the above event handler has asked for a colour selector,