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

TCHAR *_alf_panelClass = NULL;

typedef struct {
    ALFLayout layout;
    HFONT font;
} ALFPanelPriv;

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

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

static void
ALF_Panel_Paint(ALFPanelPriv *priv, HWND hwnd, HDC dc, RECT *r)
{
    (void)priv;
    ALF_Compat_DrawThemeParentBackground(hwnd, dc, r);

    int textlen = GetWindowTextLength(hwnd);
    if (textlen < 1)
        return;

    TCHAR *textbuf = ALF_New(TCHAR, (SIZE_T)textlen + 1);
    GetWindowText(hwnd, textbuf, textlen+1);

    HFONT oldFont = SelectFont(dc, priv->font);

    SetTextColor(dc, GetSysColor(COLOR_BTNTEXT));
    SetBkMode(dc, TRANSPARENT);

    RECT rc;
    GetClientRect(hwnd, &rc);

    DrawText(dc, textbuf, -1, &rc, DT_SINGLELINE | DT_EXPANDTABS | DT_HIDEPREFIX | DT_CENTER | DT_VCENTER);

    ALF_Free(textbuf);

    SelectFont(dc, oldFont);
}

static LRESULT WINAPI
ALF__PanelWindowProc(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);
        SetWindowLongPtr(window, 0, 0);
    }

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

    if (msg == WM_SETFONT) {
        priv->font = (HFONT)wparam;
        if (LOWORD(lparam) != 0)
            InvalidateRect(window, NULL, TRUE);

        // fallthrough to layout, will propagate font to children
    }

    if (msg == WM_GETFONT) {
        return (LRESULT)priv->font;
    }

    if (msg == WM_SETTEXT) {
        InvalidateRect(window, NULL, 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 (ALF_ShouldMessageBubble(window, msg, wparam, lparam))
        return SendMessage(GetParent(window), msg, wparam, lparam);

    if (msg == WM_SIZE) {
        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);
}

void
ALF_RegisterPanelClass(void)
{
    WNDCLASS cls;
    ZeroMemory(&cls, sizeof(cls));

    TCHAR  classNameBuf[256];
    ALF_BuildUniqueName(classNameBuf, TEXT("ALFPanel."), (ULONG_PTR)&_alf_panelClass);

    cls.hInstance = ALF_HINSTANCE;
    cls.hCursor = LoadCursor(NULL, (LPTSTR)IDC_ARROW);
    cls.lpszClassName = classNameBuf;
    cls.cbWndExtra = sizeof(void*);
    cls.lpfnWndProc = ALF__PanelWindowProc;

    ATOM classatom = RegisterClass(&cls);
    if (!classatom)
        MessageBox(NULL, TEXT("FATAL: Could not register panel class"), NULL, MB_OK);

    _alf_panelClass = MAKEINTATOM(classatom);
}

HWND
ALF_CreatePanelWindow(HWND parent, WORD id)
{
    return CreateWindowEx(WS_EX_CONTROLPARENT,
                          _alf_panelClass,
                          TEXT(""),
                          WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN,
                          0, 0, 0, 0,
                          parent,
                          (HMENU)(int)id,
                          ALF_HINSTANCE,
                          NULL);
}

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

    ALF_AddWidget(parent, x, y, hwndPanel, 0, 0, ALF_LAYOUT_SIZE_QUERY | ALF_LAYOUT_INHERITFONT);

    return hwndPanel;
}