summaryrefslogtreecommitdiff
path: root/alf/alfpanel.cpp
blob: be02890556f406b107f18219dac096854fa90b77 (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
#include "alfpriv.h"

typedef struct {
    ALFLayout layout;
    const ALFPanelVTable *vtbl;
    void *closure;
    DWORD edge;
} ALFPanelPriv;

static void ALF_Panel_CalculateContainerMetrics(ALFLayout *layout, HWND hwnd, SIZE *minSize);

static void
ALF_Panel_IntializePriv(ALFPanelPriv *priv)
{
    ALF_Layout_Init(&priv->layout);
    priv->layout.endCalcSizes = ALF_Panel_CalculateContainerMetrics;
}

static void
ALF_Panel_ClearPriv(ALFPanelPriv *priv)
{
    ALF_Layout_Clear(&priv->layout);
}

static void
ALF_Panel_InternalDefPaint(ALFPanelPriv *priv, HWND hwnd, HDC dc, RECT *r)
{
    RECT r2;
    GetClientRect(hwnd, &r2);
    if (priv->edge) {
        DrawEdge(dc, &r2, priv->edge, BF_ADJUST|BF_RECT);
    }

    IntersectRect(&r2, &r2, r);
    if (priv->layout.bgcolor == ALF_COLOR_TRANSPARENT) {
        ALF_Compat_DrawThemeParentBackground(hwnd, dc, &r2);
    } else {
        ALF_FillRect(dc, &r2, priv->layout.bgcolor);
    }
}


void
ALF_Panel_DefPaint(HWND panel, HDC hDC, RECT *rcPaint)
{
    ALFPanelPriv *priv = (ALFPanelPriv*)GetWindowLongPtr(panel, 0);
    ALF_Panel_InternalDefPaint(priv, panel, hDC, rcPaint);
}

static void
ALF_Panel_Paint(ALFPanelPriv *priv, HWND hwnd, HDC dc, RECT *r)
{
    if (priv->vtbl && priv->vtbl->paint) {
        priv->vtbl->paint(priv->closure, hwnd, dc, r);
    } else {
        ALF_Panel_InternalDefPaint(priv, hwnd, dc, r);
    }
}

static void
ALF_Panel_InternalSetEdge(ALFPanelPriv *priv, HWND hwnd, DWORD edge)
{
    if (priv->edge == edge)
        return;

    priv->edge = edge;
    ALF_Layout_Invalidate(&priv->layout, hwnd);
    InvalidateRect(hwnd, NULL, TRUE);
}

void
ALF_Panel_SetVTable(HWND panel, const ALFPanelVTable *vtbl, void *closure)
{
    ALFPanelSetVtblParams p = { vtbl, closure };
    SendMessage(panel, ALF_WM_PANEL_SETVTABLE, 0, (LPARAM)&p);
}

static void
ALF_Panel_CalculateContainerMetrics(ALFLayout *layout, HWND hwnd, SIZE *minSize)
{
    (void)hwnd;
    ALFPanelPriv *priv = (ALFPanelPriv *)layout;

    int w = 0;

    if (priv->edge & BDR_INNER)
        w++;

    if (priv->edge & BDR_OUTER)
        w++;

    priv->layout.containerMargins.left   = w;
    priv->layout.containerMargins.top    = w;
    priv->layout.containerMargins.right  = w;
    priv->layout.containerMargins.bottom = w;
    minSize->cx = minSize->cy = 2*w;
}

LRESULT
ALF_Panel_DefWindowProc(HWND window, UINT msg, WPARAM wparam, LPARAM lparam)
{
    ALFPanelPriv *priv = (ALFPanelPriv *)GetWindowLongPtr(window, 0);

    if (msg == WM_DESTROY) {
        if (priv->vtbl && priv->vtbl->destroy)
            priv->vtbl->destroy(priv->closure, window);
    }

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

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

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

        return TRUE;
    }

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

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

        EndPaint(window, &ps);

        return 0;
    }

    if (msg == ALF_WM_PANEL_SETVTABLE) {
        const ALFPanelSetVtblParams *params = (const ALFPanelSetVtblParams *)lparam;
        if (!params)
            return FALSE;

        priv->vtbl = params->vtbl;
        priv->closure = params->closure;
        if (priv->vtbl->attachvtbl) {
            priv->vtbl->attachvtbl(priv->closure, window);
        }
        return TRUE;
    }

    if (msg == ALF_WM_PANEL_GETEDGE) {
        return (LRESULT)priv->edge;
    }

    if (msg == ALF_WM_PANEL_SETEDGE) {
        ALF_Panel_InternalSetEdge(priv, window, (DWORD)lparam);
        return TRUE;
    }

    if (msg == WM_COMMAND && priv->vtbl && priv->vtbl->command) {
        HWND source = (HWND)lparam;
        WORD code   = HIWORD(wparam);
        WORD id     = LOWORD(wparam);
        LRESULT r   = priv->vtbl->command(priv->closure, window, code, id, source);
        if (r)
            return r;
    }

    if (msg == WM_NOTIFY && priv->vtbl && priv->vtbl->notify) {
        NMHDR *nmhdr = (NMHDR *)lparam;
        LRESULT r = priv->vtbl->notify(priv->closure, window, wparam, nmhdr);
        if (r)
            return r;
    }

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

    if (msg == WM_SIZE) {
        // if we have a border, invalidate that (and only that) when resizing
        if (priv->edge) {
            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);
    }

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

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

DWORD
ALF_Panel_Edge(HWND panel)
{
    return (DWORD)SendMessage(panel, ALF_WM_PANEL_GETEDGE, 0, 0);
}

void
ALF_Panel_SetEdge(HWND panel, DWORD edge)
{
    SendMessage(panel, ALF_WM_PANEL_SETEDGE, 0, (LPARAM)edge);
}

static LRESULT WINAPI
ALF_Panel_WindowProc(HWND window, UINT msg, WPARAM wparam, LPARAM lparam)
{
    if (msg == WM_NCCREATE) {
        ALFPanelPriv *p = ALF_New(ALFPanelPriv, 1);

        ALF_Panel_IntializePriv(p);

        SetWindowLongPtr(window, 0, (LONG_PTR)p);
    }

    ALFPanelPriv *priv = (ALFPanelPriv *)GetWindowLongPtr(window, 0);

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

    if (priv) {
        if (priv->vtbl && priv->vtbl->message) {
            return priv->vtbl->message(priv->closure, window, msg, wparam, lparam);
        } else {
            return ALF_Panel_DefWindowProc(window, msg, wparam, lparam);
        }
    } else {
        return DefWindowProc(window, msg, wparam, lparam);
    }
}

HWND
ALF_CreatePanelWindow(HWND parent, WORD id)
{
    return ALF_CreateControlWindow(WS_EX_CONTROLPARENT,
                                   TEXT(""),
                                   WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN,
                                   0, 0, 0, 0,
                                   parent,
                                   (HMENU)(ULONG_PTR)id,
                                   ALF_Panel_WindowProc,
                                   NULL);
}

HWND
ALF_AddPanel(HWND parent, WORD id, int x, int y)
{
    HWND hwndPanel = ALF_CreatePanelWindow(parent, id);

    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;
}