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
|
// 8 june 2016
#include "uipriv_windows.hpp"
struct formChild {
uiControl *c;
HWND label;
int stretchy;
int height;
};
struct uiForm {
uiWindowsControl c;
HWND hwnd;
std::vector<struct formChild> *controls;
int padded;
};
static void formPadding(uiForm *f, int *xpadding, int *ypadding)
{
uiWindowsSizing sizing;
*xpadding = 0;
*ypadding = 0;
if (f->padded) {
uiWindowsGetSizing(f->hwnd, &sizing);
uiWindowsSizingStandardPadding(&sizing, xpadding, ypadding);
}
}
// via http://msdn.microsoft.com/en-us/library/windows/desktop/dn742486.aspx#sizingandspacing
#define labelHeight 8
#define labelYOffset 3
static void formRelayout(uiForm *f)
{
RECT r;
int x, y, width, height;
int xpadding, ypadding;
int nStretchy;
int labelwid, stretchyht;
int thiswid;
int i;
int minimumWidth, minimumHeight;
uiWindowsSizing sizing;
int labelht, labelyoff;
int nVisible;
if (f->controls->size() == 0)
return;
uiWindowsEnsureGetClientRect(f->hwnd, &r);
x = r.left;
y = r.top;
width = r.right - r.left;
height = r.bottom - r.top;
// 0) get this Form's padding
formPadding(f, &xpadding, &ypadding);
// 1) get width of labels and height of non-stretchy controls
// this will tell us how much space will be left for controls
labelwid = 0;
stretchyht = height;
nStretchy = 0;
nVisible = 0;
for (struct formChild &fc : *(f->controls)) {
if (!uiControlVisible(fc.c)) {
ShowWindow(fc.label, SW_HIDE);
continue;
}
ShowWindow(fc.label, SW_SHOW);
nVisible++;
thiswid = uiWindowsWindowTextWidth(fc.label);
if (labelwid < thiswid)
labelwid = thiswid;
if (fc.stretchy) {
nStretchy++;
continue;
}
uiWindowsControlMinimumSize(uiWindowsControl(fc.c), &minimumWidth, &minimumHeight);
fc.height = minimumHeight;
stretchyht -= minimumHeight;
}
if (nVisible == 0) // nothing to do
return;
// 2) inset the available rect by the needed padding
width -= xpadding;
height -= (nVisible - 1) * ypadding;
stretchyht -= (nVisible - 1) * ypadding;
// 3) now get the width of controls and the height of stretchy controls
width -= labelwid;
if (nStretchy != 0) {
stretchyht /= nStretchy;
for (struct formChild &fc : *(f->controls)) {
if (!uiControlVisible(fc.c))
continue;
if (fc.stretchy)
fc.height = stretchyht;
}
}
// 4) get the y offset
labelyoff = labelYOffset;
uiWindowsGetSizing(f->hwnd, &sizing);
uiWindowsSizingDlgUnitsToPixels(&sizing, NULL, &labelyoff);
// 5) now we can position controls
// first, make relative to the top-left corner of the container
// also prefer left alignment on Windows
x = labelwid + xpadding;
y = 0;
for (const struct formChild &fc : *(f->controls)) {
if (!uiControlVisible(fc.c))
continue;
labelht = labelHeight;
uiWindowsGetSizing(f->hwnd, &sizing);
uiWindowsSizingDlgUnitsToPixels(&sizing, NULL, &labelht);
uiWindowsEnsureMoveWindowDuringResize(fc.label, 0, y + labelyoff - sizing.InternalLeading, labelwid, labelht);
uiWindowsEnsureMoveWindowDuringResize((HWND) uiControlHandle(fc.c), x, y, width, fc.height);
y += fc.height + ypadding;
}
}
static void uiFormDestroy(uiControl *c)
{
uiForm *f = uiForm(c);
for (const struct formChild &fc : *(f->controls)) {
uiControlSetParent(fc.c, NULL);
uiControlDestroy(fc.c);
uiWindowsEnsureDestroyWindow(fc.label);
}
delete f->controls;
uiWindowsEnsureDestroyWindow(f->hwnd);
uiFreeControl(uiControl(f));
}
uiWindowsControlDefaultHandle(uiForm)
uiWindowsControlDefaultParent(uiForm)
uiWindowsControlDefaultSetParent(uiForm)
uiWindowsControlDefaultToplevel(uiForm)
uiWindowsControlDefaultVisible(uiForm)
uiWindowsControlDefaultShow(uiForm)
uiWindowsControlDefaultHide(uiForm)
uiWindowsControlDefaultEnabled(uiForm)
uiWindowsControlDefaultEnable(uiForm)
uiWindowsControlDefaultDisable(uiForm)
static void uiFormSyncEnableState(uiWindowsControl *c, int enabled)
{
uiForm *f = uiForm(c);
if (uiWindowsShouldStopSyncEnableState(uiWindowsControl(f), enabled))
return;
for (const struct formChild &fc : *(f->controls))
uiWindowsControlSyncEnableState(uiWindowsControl(fc.c), enabled);
}
uiWindowsControlDefaultSetParentHWND(uiForm)
static void uiFormMinimumSize(uiWindowsControl *c, int *width, int *height)
{
uiForm *f = uiForm(c);
int xpadding, ypadding;
int nStretchy;
// these two contain the largest minimum width and height of all stretchy controls in the form
// all stretchy controls will use this value to determine the final minimum size
int maxLabelWidth, maxControlWidth;
int maxStretchyHeight;
int labelwid;
int i;
int minimumWidth, minimumHeight;
int nVisible;
uiWindowsSizing sizing;
*width = 0;
*height = 0;
if (f->controls->size() == 0)
return;
// 0) get this Form's padding
formPadding(f, &xpadding, &ypadding);
// 1) determine the longest width of all controls and labels; add in the height of non-stretchy controls and get (but not add in) the largest heights of stretchy controls
// we still add in like direction of stretchy controls
nStretchy = 0;
maxLabelWidth = 0;
maxControlWidth = 0;
maxStretchyHeight = 0;
nVisible = 0;
for (const struct formChild &fc : *(f->controls)) {
if (!uiControlVisible(fc.c))
continue;
nVisible++;
labelwid = uiWindowsWindowTextWidth(fc.label);
if (maxLabelWidth < labelwid)
maxLabelWidth = labelwid;
uiWindowsControlMinimumSize(uiWindowsControl(fc.c), &minimumWidth, &minimumHeight);
if (fc.stretchy) {
nStretchy++;
if (maxStretchyHeight < minimumHeight)
maxStretchyHeight = minimumHeight;
}
if (maxControlWidth < minimumWidth)
maxControlWidth = minimumWidth;
if (!fc.stretchy)
*height += minimumHeight;
}
if (nVisible == 0) // nothing to show; return 0x0
return;
*width += maxLabelWidth + maxControlWidth;
// 2) outset the desired rect with the needed padding
*width += xpadding;
*height += (nVisible - 1) * ypadding;
// 3) and now we can add in stretchy controls
*height += nStretchy * maxStretchyHeight;
}
static void uiFormMinimumSizeChanged(uiWindowsControl *c)
{
uiForm *f = uiForm(c);
if (uiWindowsControlTooSmall(uiWindowsControl(f))) {
uiWindowsControlContinueMinimumSizeChanged(uiWindowsControl(f));
return;
}
formRelayout(f);
}
uiWindowsControlDefaultLayoutRect(uiForm)
uiWindowsControlDefaultAssignControlIDZOrder(uiForm)
static void uiFormChildVisibilityChanged(uiWindowsControl *c)
{
// TODO eliminate the redundancy
uiWindowsControlMinimumSizeChanged(c);
}
static void formArrangeChildren(uiForm *f)
{
LONG_PTR controlID;
HWND insertAfter;
int i;
controlID = 100;
insertAfter = NULL;
for (const struct formChild &fc : *(f->controls)) {
// TODO assign label ID and z-order
uiWindowsControlAssignControlIDZOrder(uiWindowsControl(fc.c), &controlID, &insertAfter);
}
}
void uiFormAppend(uiForm *f, const char *label, uiControl *c, int stretchy)
{
struct formChild fc;
WCHAR *wlabel;
fc.c = c;
wlabel = toUTF16(label);
fc.label = uiWindowsEnsureCreateControlHWND(0,
L"STATIC", wlabel,
SS_LEFT | SS_NOPREFIX,
hInstance, NULL,
TRUE);
uiFree(wlabel);
uiWindowsEnsureSetParentHWND(fc.label, f->hwnd);
fc.stretchy = stretchy;
uiControlSetParent(fc.c, uiControl(f));
uiWindowsControlSetParentHWND(uiWindowsControl(fc.c), f->hwnd);
f->controls->push_back(fc);
formArrangeChildren(f);
uiWindowsControlMinimumSizeChanged(uiWindowsControl(f));
}
void uiFormDelete(uiForm *f, int index)
{
struct formChild fc;
fc = (*(f->controls))[index];
uiControlSetParent(fc.c, NULL);
uiWindowsControlSetParentHWND(uiWindowsControl(fc.c), NULL);
uiWindowsEnsureDestroyWindow(fc.label);
f->controls->erase(f->controls->begin() + index);
formArrangeChildren(f);
uiWindowsControlMinimumSizeChanged(uiWindowsControl(f));
}
int uiFormPadded(uiForm *f)
{
return f->padded;
}
void uiFormSetPadded(uiForm *f, int padded)
{
f->padded = padded;
uiWindowsControlMinimumSizeChanged(uiWindowsControl(f));
}
static void onResize(uiWindowsControl *c)
{
formRelayout(uiForm(c));
}
uiForm *uiNewForm(void)
{
uiForm *f;
uiWindowsNewControl(uiForm, f);
f->hwnd = uiWindowsMakeContainer(uiWindowsControl(f), onResize);
f->controls = new std::vector<struct formChild>;
return f;
}
|