-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwin32_chrome.cpp
More file actions
210 lines (179 loc) · 8.85 KB
/
win32_chrome.cpp
File metadata and controls
210 lines (179 loc) · 8.85 KB
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
#include "win32_chrome.hpp"
namespace Win32Chrome {
Win32Chrome::Win32Chrome(HWND handle, Callbacks callbacks, bool maximizable = true, bool minimizable = true
) : m_handle(handle), m_callbacks(callbacks), m_minimizable(minimizable), m_maximizable(maximizable) {
SetProp(handle, LPSTRING, reinterpret_cast<HANDLE>(this));
LONG style = GetWindowLong(handle, GWL_STYLE);
m_originalStyle = style;
style |= WS_THICKFRAME | WS_SYSMENU | WS_VISIBLE;
if (minimizable) {
style |= WS_MINIMIZEBOX;
}
if (maximizable) {
style |= WS_MAXIMIZEBOX;
}
SetWindowLong(handle, GWL_STYLE, style);
m_originalProc = (WNDPROC)SetWindowLongPtr(handle, GWLP_WNDPROC, (LONG_PTR)wndProc);
SetWindowPos(handle, nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
const bool Win32Chrome::isMaximizable() {
return m_maximizable;
}
const bool Win32Chrome::isMinimizable() {
return m_minimizable;
}
void Win32Chrome::restore() {
SetWindowLong(m_handle, GWL_STYLE, m_originalStyle);
SetWindowLongPtr(m_handle, GWLP_WNDPROC, (LONG_PTR)m_originalProc);
SetWindowPos(m_handle, nullptr, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
RemoveProp(m_handle, LPSTRING);
}
LRESULT Win32Chrome::wndProc(HWND handle, UINT message, WPARAM w_param, LPARAM l_param) {
auto* instance = reinterpret_cast<Win32Chrome *>(GetProp(handle, LPSTRING));
if (!instance) {
return DefWindowProc(handle, message, w_param, l_param);
}
switch (message) {
case WM_NCCALCSIZE: {
BOOL const isMaximized = IsZoomed(handle);
if (!w_param) {
return DefWindowProc(handle, message, w_param, l_param);
}
UINT dpi = GetDpiForWindow(handle);
int frame_x = GetSystemMetricsForDpi(SM_CXFRAME, dpi);
int frame_y = GetSystemMetricsForDpi(SM_CYFRAME, dpi);
int padding = GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
NCCALCSIZE_PARAMS* params = (NCCALCSIZE_PARAMS *)l_param;
RECT* rcr = params->rgrc;
rcr->right -= frame_x + padding;
rcr->left += frame_x + padding;
rcr->bottom -= frame_y + padding;
if (isMaximized) {
rcr->top += frame_y + padding;
}
return 0;
}
case WM_CREATE: {
SetWindowPos(handle, nullptr, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
break;
}
case WM_NCHITTEST: {
BOOL const isMaximized = IsZoomed(handle);
LRESULT hit = DefWindowProc(handle, message, w_param, l_param);
switch (hit) {
case HTNOWHERE:
case HTRIGHT:
case HTLEFT:
case HTTOPLEFT:
case HTTOP:
case HTTOPRIGHT:
case HTBOTTOMRIGHT:
case HTBOTTOM:
case HTBOTTOMLEFT: {
return hit;
}
}
POINT cursor_point = {0};
cursor_point.x = GET_X_PARAM(l_param);
cursor_point.y = GET_Y_PARAM(l_param);
ScreenToClient(handle, &cursor_point);
ControlRects controls = instance->m_callbacks.TitlebarControlsBoundingBox();
if (isInsideBoundingBox(controls.Close, cursor_point.x, cursor_point.y)) {
return HTCLOSE;
}
if (isInsideBoundingBox(controls.Maximize, cursor_point.x, cursor_point.y) && instance->
isMaximizable()) {
return HTMAXBUTTON;
}
if (isInsideBoundingBox(controls.Minimize, cursor_point.x, cursor_point.y) && instance->
isMinimizable()) {
return HTMINBUTTON;
}
UINT dpi = GetDpiForWindow(handle);
int frame_y = GetSystemMetricsForDpi(SM_CYFRAME, dpi);
int padding = GetSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
if (!isMaximized && cursor_point.y > 0 && cursor_point.y < frame_y + padding) {
return HTTOP;
}
if (cursor_point.y < instance->m_callbacks.TitlebarBoundingBox().h && !instance->m_callbacks.
TitlebarControlHittest(cursor_point.x, cursor_point.y)) {
return HTCAPTION;
}
return HTCLIENT;
}
case WM_NCLBUTTONDOWN: {
POINT cursor_point = {0};
cursor_point.x = GET_X_PARAM(l_param);
cursor_point.y = GET_Y_PARAM(l_param);
ScreenToClient(handle, &cursor_point);
ControlRects controls = instance->m_callbacks.TitlebarControlsBoundingBox();
bool onButton = isInsideBoundingBox(controls.Close, cursor_point.x, cursor_point.y) ||
isInsideBoundingBox(controls.Maximize, cursor_point.x, cursor_point.y) ||
isInsideBoundingBox(controls.Minimize, cursor_point.x, cursor_point.y);
if (onButton) {
return 0;
}
return CallWindowProc(instance->m_originalProc, handle, message, w_param, l_param);
}
case WM_NCLBUTTONUP: {
POINT cursor_point = {0};
cursor_point.x = GET_X_PARAM(l_param);
cursor_point.y = GET_Y_PARAM(l_param);
ScreenToClient(handle, &cursor_point);
ControlRects controls = instance->m_callbacks.TitlebarControlsBoundingBox();
if (isInsideBoundingBox(controls.Close, cursor_point.x, cursor_point.y)) {
PostMessageW(handle, WM_CLOSE, 0, 0);
return 0;
}
if (isInsideBoundingBox(controls.Minimize, cursor_point.x, cursor_point.y) && instance->
isMinimizable()) {
ShowWindow(handle, SW_MINIMIZE);
return 0;
}
if (isInsideBoundingBox(controls.Maximize, cursor_point.x, cursor_point.y) && instance->
isMaximizable()) {
BOOL const isMaximized = IsZoomed(handle);
int mode = isMaximized ? SW_NORMAL : SW_MAXIMIZE;
ShowWindow(handle, mode);
return 0;
}
return CallWindowProc(instance->m_originalProc, handle, message, w_param, l_param);
}
case WM_NCRBUTTONUP: {
if (w_param == HTCAPTION) {
BOOL const isMaximized = IsZoomed(handle);
MENUITEMINFO info = {
.cbSize = sizeof(info),
.fMask = MIIM_STATE
};
HMENU const sys_menu = GetSystemMenu(handle, false);
setMenuItemState(sys_menu, &info, SC_RESTORE, isMaximized);
setMenuItemState(sys_menu, &info, SC_MOVE, !isMaximized);
setMenuItemState(sys_menu, &info, SC_SIZE, !isMaximized);
setMenuItemState(sys_menu, &info, SC_MINIMIZE, true);
instance->setMenuItemState(sys_menu, &info, SC_MAXIMIZE, !isMaximized);
instance->setMenuItemState(sys_menu, &info, SC_CLOSE, true);
BOOL const result = TrackPopupMenu(sys_menu, TPM_RETURNCMD, GET_X_PARAM(l_param),
GET_Y_PARAM(l_param),
0, handle, nullptr);
if (result != 0) {
PostMessage(handle, WM_SYSCOMMAND, result, 0);
}
}
return CallWindowProc(instance->m_originalProc, handle, message, w_param, l_param);
}
case WM_DESTROY: {
return CallWindowProc(instance->m_originalProc, handle, message, w_param, l_param);
}
}
return CallWindowProc(instance->m_originalProc, handle, message, w_param, l_param);
}
bool Win32Chrome::isInsideBoundingBox(Rect rect, int x, int y) {
return (x >= rect.x && x < (rect.x + rect.w)) &&
(y >= rect.y && y < (rect.y + rect.h));
}
void Win32Chrome::setMenuItemState(HMENU menu, MENUITEMINFO* menuiteminfo, UINT item, bool enabled) {
menuiteminfo->fState = enabled ? MF_ENABLED : MF_DISABLED;
SetMenuItemInfo(menu, item, false, menuiteminfo);
}
}