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
|
// 9 october 2014
#include "../wininclude_windows.h"
#include "popover.h"
// #qo LIBS: user32 kernel32 gdi32
// TODO
// - should the parent window appear deactivated?
HWND popoverWindow;
void xpanic(char *msg, DWORD err)
{
printf("%d | %s\n", err, msg);
abort();
}
popover *p;
HRGN makePopoverRegion(HDC dc, LONG width, LONG height)
{
popoverPoint ppt[20];
POINT pt[20];
int i, n;
HRGN region;
n = popoverMakeFramePoints(p, (intptr_t) width, (intptr_t) height, ppt);
for (i = 0; i < n; i++) {
pt[i].x = (LONG) (ppt[i].x);
pt[i].y = (LONG) (ppt[i].y);
}
if (BeginPath(dc) == 0)
xpanic("error beginning path for Popover shape", GetLastError());
if (Polyline(dc, pt, n) == 0)
xpanic("error drawing lines in Popover shape", GetLastError());
if (EndPath(dc) == 0)
xpanic("error ending path for Popover shape", GetLastError());
region = PathToRegion(dc);
if (region == NULL)
xpanic("error converting Popover shape path to region", GetLastError());
return region;
}
#define msgPopoverPrepareLeftRight (WM_APP+50)
#define msgPopoverPrepareTopBottom (WM_APP+51)
LRESULT CALLBACK popoverproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC dc;
HRGN region;
RECT r;
LONG width;
LONG height;
WINDOWPOS *wp;
HBRUSH brush;
switch (uMsg) {
case WM_NCPAINT:
if (GetWindowRect(hwnd, &r) == 0)
xpanic("error getting Popover window rect for shape redraw", GetLastError());
width = r.right - r.left;
height = r.bottom - r.top;
dc = GetWindowDC(hwnd);
if (dc == NULL)
xpanic("error getting Popover window DC for drawing border", GetLastError());
region = makePopoverRegion(dc, width, height);
// don't call FillRgn(); WM_ERASEBKGND seems to do this to the non-client area for us already :S (TODO confirm)
// TODO arrow is black in wine
brush = (HBRUSH) GetStockObject(BLACK_BRUSH);
if (brush == NULL)
xpanic("error getting Popover border brush", GetLastError());
if (FrameRgn(dc, region, brush, 1, 1) == 0)
xpanic("error drawing Popover border", GetLastError());
if (DeleteObject(region) == 0)
xpanic("error deleting Popover shape region", GetLastError());
if (ReleaseDC(hwnd, dc) == 0)
xpanic("error releasing Popover window DC for shape drawing", GetLastError());
return 0;
case WM_WINDOWPOSCHANGED:
// this must be here; if it's in WM_NCPAINT weird things happen (see http://stackoverflow.com/questions/26288303/why-is-my-client-rectangle-drawing-behaving-bizarrely-pictures-provided-if-i-t)
wp = (WINDOWPOS *) lParam;
if ((wp->flags & SWP_NOSIZE) == 0) {
dc = GetWindowDC(hwnd);
if (dc == NULL)
xpanic("error getting Popover window DC for reshaping", GetLastError());
region = makePopoverRegion(dc, wp->cx, wp->cy);
if (SetWindowRgn(hwnd, region, TRUE) == 0)
xpanic("error setting Popover shape", GetLastError());
// don't delete the region; the window manager owns it now
if (ReleaseDC(hwnd, dc) == 0)
xpanic("error releasing Popover window DC for reshaping", GetLastError());
}
break; // defer to DefWindowProc()
case WM_NCCALCSIZE:
{
RECT *r = (RECT *) lParam;
NCCALCSIZE_PARAMS *np = (NCCALCSIZE_PARAMS *) lParam;
popoverRect pr;
if (wParam != FALSE)
r = &np->rgrc[0];
pr.left = (intptr_t) (r->left);
pr.top = (intptr_t) (r->top);
pr.right = (intptr_t) (r->right);
pr.bottom = (intptr_t) (r->bottom);
popoverWindowSizeToClientSize(p, &pr);
r->left = (LONG) (pr.left);
r->top = (LONG) (pr.top);
r->right = (LONG) (pr.right);
r->bottom = (LONG) (pr.bottom);
return 0;
}
case WM_PAINT:
dc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &r);
FillRect(dc, &r, GetSysColorBrush(COLOR_ACTIVECAPTION));
FrameRect(dc, &r, GetStockPen(WHITE_BRUSH));
EndPaint(hwnd, &ps);
return 0;
case msgPopoverPrepareLeftRight:
case msgPopoverPrepareTopBottom:
// TODO window edge detection
{
RECT r;
LONG width = 200, height = 200;
popoverRect control;
uintptr_t side;
popoverRect out;
if (GetWindowRect((HWND) wParam, &r) == 0)
xpanic("error getting window rect of Popover target", GetLastError());
control.left = (intptr_t) (r.left);
control.top = (intptr_t) (r.top);
control.right = (intptr_t) (r.right);
control.bottom = (intptr_t) (r.bottom);
switch (uMsg) {
case msgPopoverPrepareLeftRight:
side = popoverPointLeft;
break;
case msgPopoverPrepareTopBottom:
side = popoverPointTop;
break;
}
out = popoverPointAt(p, control, (intptr_t) width, (intptr_t) height, side);
if (MoveWindow(hwnd, out.left, out.top, out.right - out.left, out.bottom - out.top, TRUE) == 0)
xpanic("error repositioning Popover", GetLastError());
}
return 0;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
HWND button;
LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_COMMAND:
if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == 100) {
SendMessageW(popoverWindow, msgPopoverPrepareLeftRight, (WPARAM) button, 0);
ShowWindow(popoverWindow, SW_SHOW);
UpdateWindow(popoverWindow);
return 0;
}
break;
case WM_CLOSE:
PostQuitMessage(0);
return 0;
}
return DefWindowProcW(hwnd, uMsg, wParam, lParam);
}
int main(int argc, char *argv[])
{
WNDCLASSW wc;
HWND mainwin;
MSG msg;
p = popoverDataNew(NULL);
// TODO null check
ZeroMemory(&wc, sizeof (WNDCLASSW));
wc.lpszClassName = L"popover";
wc.lpfnWndProc = popoverproc;
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
wc.style = CS_DROPSHADOW | CS_NOCLOSE;
if (RegisterClassW(&wc) == 0)
abort();
popoverWindow = CreateWindowExW(WS_EX_TOPMOST,
L"popover", L"",
WS_POPUP,
0, 0, 150, 100,
NULL, NULL, NULL, NULL);
if (popoverWindow == NULL)
abort();
ZeroMemory(&wc, sizeof (WNDCLASSW));
wc.lpszClassName = L"mainwin";
wc.lpfnWndProc = wndproc;
wc.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
if (RegisterClassW(&wc) == 0)
abort();
mainwin = CreateWindowExW(0,
L"mainwin", L"Main Window",
WS_OVERLAPPEDWINDOW,
0, 0, 150, 100,
NULL, NULL, NULL, NULL);
if (mainwin == NULL)
abort();
button = CreateWindowExW(0,
L"button", L"Click Me",
BS_PUSHBUTTON | WS_CHILD | WS_VISIBLE,
20, 20, 100, 40,
mainwin, (HMENU) 100, NULL, NULL);
if (button == NULL)
abort();
ShowWindow(mainwin, SW_SHOWDEFAULT);
if (UpdateWindow(mainwin) == 0)
abort();
while (GetMessageW(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return 0;
}
|