summaryrefslogtreecommitdiff
path: root/alf/alfgroupbox.cpp
blob: 0b60d8136d6b6b0c18b1b14dea5228f6270b9cee (plain)
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
#include "alfpriv.h"

#define BP_GROUPBOX 4
#define GBS_NORMAL 1
#define GBS_DISABLED 2
#define TMT_TEXTCOLOR 3803

typedef struct {
    ALFLayout layout;
    HTHEME    theme;
    HWND      label;
    SIZE      calculatedLabelSize;
} ALFGroupBoxPriv;

static void ALF_GroupBox_BeginCalcSizes(ALFLayout *layout, HWND hwnd);

static void ALF_GroupBox_HandleContainerControlSize(ALFLayout *layout, HWND groupbox, HWND child, SIZE *s);

static void ALF_GroupBox_EndCalcSizes(ALFLayout *layout, HWND hwnd, SIZE *containerMinSize);

static void ALF_GroupBox_PreContainerControlApplyPos(ALFLayout *layout, HWND groupbox, HWND child, RECT *r);

static void ALF_GroupBox_HandleThemeChange(ALFGroupBoxPriv *priv, HWND hwnd);

static void
ALF_GroupBox_IntializePriv(ALFGroupBoxPriv *priv, HWND window, const TCHAR *text)
{
    ALF_Layout_Init(&priv->layout);
    priv->layout.beginCalcSizes = ALF_GroupBox_BeginCalcSizes;
    priv->layout.handleContainerControlSize = ALF_GroupBox_HandleContainerControlSize;
    priv->layout.endCalcSizes = ALF_GroupBox_EndCalcSizes;
    priv->layout.preApplyLayoutToContainerControl = ALF_GroupBox_PreContainerControlApplyPos;

    priv->label = ALF_AddLabel(window, (WORD)-1, -1, -1, text);
    ALF_Label_SetStyle(priv->label, ALF_LABEL_ALIGN_LEFT | ALF_LABEL_ALIGN_TOP);

    ALF_GroupBox_HandleThemeChange(priv, window);
}

static void
ALF_GroupBox_ClearPriv(ALFGroupBoxPriv *priv)
{
    ALF_Layout_Clear(&priv->layout);
    ALF_Compat_CloseThemeData(priv->theme);
}

static void
ALF_GroupBox_PaintFrameUxtheme(ALFGroupBoxPriv *priv, HWND hwnd, HDC dc, RECT *rcPaint)
{
    int state = IsWindowEnabled(hwnd) ? GBS_NORMAL : GBS_DISABLED;

    if (ALF_Compat_IsThemeBackgroundPartiallyTransparent(priv->theme, BP_GROUPBOX, state)) {
        if (priv->layout.bgcolor == ALF_COLOR_TRANSPARENT) {
            ALF_Compat_DrawThemeParentBackground(hwnd, dc, rcPaint);
        } else {
            ALF_FillRect(dc, rcPaint, priv->layout.bgcolor);
        }
    }

    RECT rc;
    GetClientRect(hwnd, &rc);

    // Skip drawing the border if that is not requested, since stretching that bitmap might be expensive.
    // Will happen when a transparent child calls DrawThemeParentBackground.
    RECT rcTop = { 0, 0, rc.right-rc.left, priv->layout.containerMargins.top };
    RECT rcLeft = { 0, 0, priv->layout.containerMargins.left, rc.bottom-rc.top };
    RECT rcRight = { rc.right-rc.left-priv->layout.containerMargins.right, 0, rc.right-rc.left, rc.bottom-rc.top };
    RECT rcBottom = { 0, rc.bottom-rc.top-priv->layout.containerMargins.bottom, rc.right-rc.left, rc.bottom-rc.top };
    if (!RectVisible(dc, &rcTop) && !RectVisible(dc, &rcLeft) && !RectVisible(dc, &rcRight) && !RectVisible(dc, &rcBottom))
        return;

    rc.top += priv->calculatedLabelSize.cy / 2;

    HRGN oldClipR = CreateRectRgn(0, 0, 0, 0);
    if (!GetClipRgn(dc, oldClipR)) {
        DeleteRgn(oldClipR);
        oldClipR = NULL;
    }

    RECT rcLabel;
    GetWindowRect(priv->label, &rcLabel);
    MapWindowRect(NULL, hwnd, &rcLabel);

    rcLabel.left -= 2;  // FIXME: is this correct?
    rcLabel.right += 2;

    ExcludeClipRect(dc, rcLabel.left, rcLabel.top, rcLabel.right, rcLabel.bottom);

    ALF_Compat_DrawThemeBackground(priv->theme, dc, BP_GROUPBOX, state, &rc, rcPaint);

    SelectClipRgn(dc, oldClipR);
    DeleteObject(oldClipR);
}

static void
ALF_GroupBox_PaintFrameClassic(ALFGroupBoxPriv *priv, HWND hwnd, HDC dc, RECT *rcPaint)
{
    if (priv->layout.bgcolor == ALF_COLOR_TRANSPARENT) {
        ALF_Compat_DrawThemeParentBackground(hwnd, dc, rcPaint);
    } else {
        ALF_FillRect(dc, rcPaint, priv->layout.bgcolor);
    }

    HFONT oldfont = SelectFont(dc, priv->layout.font);

    RECT rcClient = { 0,0,0,0 };
    GetClientRect(hwnd, &rcClient);


    RECT rcLabel = { 0,0,0,0 };
    GetWindowRect(priv->label, &rcLabel);
    MapWindowRect(NULL, hwnd, &rcLabel);

    RECT rcFrame;
    rcFrame.top  = (rcLabel.bottom - rcLabel.top) / 2;
    rcFrame.left = 0;
    rcFrame.bottom = rcLabel.bottom;
    rcFrame.right = rcLabel.left - 2; // XXX: is that correct?
    DrawEdge(dc, &rcFrame, EDGE_ETCHED, BF_TOPLEFT);

    rcFrame.left = rcLabel.right + 2; // XXX: is that correct?
    rcFrame.right = rcClient.right;
    DrawEdge(dc, &rcFrame, EDGE_ETCHED, BF_TOPRIGHT);

    rcFrame.top = rcLabel.bottom;
    rcFrame.left = 0;
    rcFrame.right = rcClient.right;
    rcFrame.bottom = rcClient.bottom;
    DrawEdge(dc, &rcFrame, EDGE_ETCHED, BF_LEFT | BF_BOTTOM | BF_RIGHT);

    SelectFont(dc, oldfont);
}

static void
ALF_GroupBox_Paint(ALFGroupBoxPriv *priv, HWND hwnd, HDC dc, RECT *r)
{
    if (priv->theme)
        ALF_GroupBox_PaintFrameUxtheme(priv, hwnd, dc, r);
    else
        ALF_GroupBox_PaintFrameClassic(priv, hwnd, dc, r);
}

static void
ALF_GroupBox_BeginCalcSizes(ALFLayout *layout, HWND hwnd)
{
    ALFGroupBoxPriv *priv = (ALFGroupBoxPriv *)layout;
    (void)hwnd;

    priv->layout.containerMargins.left   = 2 + ALF_CentipointsToPixels(525, priv->layout.dpi);
    priv->layout.containerMargins.top    = 2 + ALF_CentipointsToPixels(300, priv->layout.dpi);
    priv->layout.containerMargins.right  = 2 + ALF_CentipointsToPixels(525, priv->layout.dpi);
    priv->layout.containerMargins.bottom = 2 + ALF_CentipointsToPixels(525, priv->layout.dpi);
    priv->calculatedLabelSize.cx = 0;
    priv->calculatedLabelSize.cy = 0;
}

static void
ALF_GroupBox_HandleContainerControlSize(ALFLayout *layout, HWND groupbox, HWND child, SIZE *s)
{
    ALFGroupBoxPriv *priv = (ALFGroupBoxPriv *)layout;
    (void)groupbox;

    if (child == priv->label) {
        priv->calculatedLabelSize = *s;

        int h = priv->calculatedLabelSize.cy + ALF_CentipointsToPixels(300, priv->layout.dpi);
        if (h > priv->layout.containerMargins.top) {
            priv->layout.containerMargins.top = h;
        }
    }
}

static void ALF_GroupBox_EndCalcSizes(ALFLayout *layout, HWND hwnd, SIZE *containerMinSize)
{
    ALFGroupBoxPriv *priv = (ALFGroupBoxPriv *)layout;
    (void)hwnd;

    containerMinSize->cx = priv->layout.containerMargins.left + priv->layout.containerMargins.right
                            + priv->calculatedLabelSize.cx;
    containerMinSize->cy = priv->layout.containerMargins.top + priv->layout.containerMargins.bottom;
}

static void ALF_GroupBox_PreContainerControlApplyPos(ALFLayout *layout, HWND groupbox, HWND child, RECT *r)
{
    ALFGroupBoxPriv *priv = (ALFGroupBoxPriv *)layout;

    if (child == priv->label) {
        RECT rOld;
        GetClientRect(child, &rOld);

        if (rOld.bottom - rOld.top != priv->calculatedLabelSize.cy ||
                rOld.right - rOld.left != priv->calculatedLabelSize.cx) {
            // if the label changed, the top part of the border needs to be redrawn
            RECT rcGb;
            GetClientRect(groupbox, &rcGb);
            RECT i = { 0, 0, rcGb.right - rcGb.left, priv->calculatedLabelSize.cy };
            InvalidateRect(groupbox, &i, TRUE);
        }


        r->left = priv->layout.containerMargins.left;
        r->top = 0;
        r->right = r->left + priv->calculatedLabelSize.cx;
        r->bottom = priv->calculatedLabelSize.cy;
    }
}

static void
ALF_GroupBox_HandleThemeChange(ALFGroupBoxPriv *priv, HWND hwnd)
{
    if (priv->theme)
        ALF_Compat_CloseThemeData(priv->theme);

    priv->theme = NULL;

    if (ALF_Compat_IsAppThemed())
        priv->theme = ALF_Compat_OpenThemeData(hwnd, L"BUTTON");

    InvalidateRect(hwnd, NULL, TRUE);

    if (priv->theme) {
        COLORREF textcolor = (COLORREF)ALF_COLOR_SYS(COLOR_BTNTEXT);
        ALF_Compat_GetThemeColor(priv->theme, BP_GROUPBOX, GBS_NORMAL, TMT_TEXTCOLOR, &textcolor);

        ALF_SetTextColor(priv->label, (ALFColor)textcolor);
    } else {
        ALF_SetTextColor(priv->label, ALF_COLOR_SYS(COLOR_BTNTEXT));
    }
}

static LRESULT WINAPI
ALF_GroupBox_WindowProc(HWND window, UINT msg, WPARAM wparam, LPARAM lparam)
{
    if (msg == WM_NCCREATE) {
        ALFGroupBoxPriv *p = ALF_New(ALFGroupBoxPriv, 1);
        SetWindowLongPtr(window, 0, (LONG_PTR)p);

        ALF_GroupBox_IntializePriv(p, window, ((CREATESTRUCT *)lparam)->lpszName);
    }

    ALFGroupBoxPriv *priv = (ALFGroupBoxPriv *)GetWindowLongPtr(window, 0);
    if (!priv)
        return DefWindowProc(window, msg, wparam, lparam); // FIXME! shouldn't happen

    if (msg == WM_NCDESTROY) {
        ALF_GroupBox_ClearPriv(priv);
        ALF_Free(priv);
        priv = NULL;
        SetWindowLongPtr(window, 0, 0);
    }

    if (msg == WM_ERASEBKGND) {
        return TRUE;
    }

    if (msg == WM_PRINTCLIENT) {
        RECT r = { 0, 0, 0, 0 };
        GetClientRect(window, &r);

        ALF_GroupBox_Paint(priv, window, (HDC)wparam, &r);

        return TRUE;
    }

    if (msg == WM_PAINT) {
        PAINTSTRUCT ps;
        HDC dc = BeginPaint(window, &ps);

        ALF_GroupBox_Paint(priv, window, dc, &ps.rcPaint);

        EndPaint(window, &ps);

        return 0;
    }

    if (msg == WM_SETTEXT) {
        return SendMessage(priv->label, WM_SETTEXT, wparam, lparam);
    }

    if (msg == WM_GETTEXT) {
        return SendMessage(priv->label, WM_GETTEXT, wparam, lparam);
    }

    if (ALF_ShouldMessageBubble(window, msg, wparam, lparam))
        return SendMessage(GetParent(window), msg, wparam, lparam);

    if (msg == WM_SIZE) {
        RECT c;
        GetClientRect(window, &c);

        if (c.right > priv->layout.allocatedControlRect.right + priv->layout.containerMargins.right) {
            // enlarged to the right -> invalidate place occupied by old border
            RECT i = { priv->layout.allocatedControlRect.right,
                        0,
                        c.right,
                        c.bottom };
            InvalidateRect(window, &i, TRUE);
        }
        if (c.bottom > priv->layout.allocatedControlRect.bottom + priv->layout.containerMargins.bottom) {
            // enlarged to the bottom -> invalidate place occupied by old border
            RECT i = { 0,
                        priv->layout.allocatedControlRect.bottom,
                        c.right,
                        c.bottom };
            InvalidateRect(window, &i, TRUE);
        }
        if (c.right < priv->layout.allocatedControlRect.right + priv->layout.containerMargins.right) {
            // shrunk horizontally -> invalidate place now occupied by new border
            RECT i = { c.right - priv->layout.containerMargins.right,
                        0,
                        c.right,
                        c.bottom };
            InvalidateRect(window, &i, TRUE);
        }
        if (c.bottom < priv->layout.allocatedControlRect.bottom + priv->layout.containerMargins.bottom) {
            // shrunk vertically -> invalidate place now ocuupied by new border
            RECT i = { 0,
                        c.bottom - priv->layout.containerMargins.bottom,
                        c.right,
                        c.bottom };
            InvalidateRect(window, &i, TRUE);
        }


        ALF_Layout_Apply(&priv->layout, window);
    }

    if (msg == WM_THEMECHANGED) {
        ALF_GroupBox_HandleThemeChange(priv, window);
    }

    if (msg == WM_ENABLE) {
        EnableWindow(priv->label, (BOOL)wparam);
    }

    if (msg == WM_NCHITTEST) {
        return HTTRANSPARENT;
    }

    LRESULT ret = 0;
    if (ALF_Layout_HandleMessage(&priv->layout, window, msg, wparam, lparam, &ret))
        return ret;

    return DefWindowProc(window, msg, wparam, lparam);
}

HWND
ALF_AddGroupBox(HWND parent, WORD id, int x, int y, const TCHAR *text)
{
    HWND hwndPanel = ALF_CreateControlWindow(WS_EX_CONTROLPARENT,
                                             text,
                                             WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_TABSTOP,
                                             0, 0, 0, 0,
                                             parent,
                                             (HMENU)(UINT_PTR)id,
                                             ALF_GroupBox_WindowProc,
                                             NULL);

    ALF_AddControl(parent, x, y, hwndPanel, 0, 0, ALF_LAYOUT_SIZE_QUERY | ALF_LAYOUT_INHERITFONT | ALF_LAYOUT_INHERITBGCOLOR | ALF_LAYOUT_SENDBGCHANGE | ALF_LAYOUT_SENDDPICHANGE);

    return hwndPanel;
}