-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathwindow.cpp
277 lines (236 loc) · 7.69 KB
/
window.cpp
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
/*=============================================================================
Copyright (c) 2016-2023 Joel de Guzman
Distributed under the MIT License (https://opensource.org/licenses/MIT)
=============================================================================*/
#include <elements/window.hpp>
#include <elements/support.hpp>
#include "utils.hpp"
namespace elements = cycfi::elements;
namespace cycfi::elements
{
// UTF8 conversion utils defined in base_view.cpp
// Convert a wide Unicode string to an UTF8 string
std::string utf8_encode(std::wstring const& wstr);
// Convert an UTF8 string to a wide Unicode String
std::wstring utf8_decode(std::string const& str);
namespace
{
struct window_info
{
window* wptr = nullptr;
view_limits limits = {};
};
window_info* get_window_info(HWND hwnd)
{
auto param = GetWindowLongPtrW(hwnd, GWLP_USERDATA);
return reinterpret_cast<window_info*>(param);
}
void disable_close(HWND hwnd)
{
EnableMenuItem(GetSystemMenu(hwnd, FALSE), SC_CLOSE,
MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
}
void disable_minimize(HWND hwnd)
{
SetWindowLongW(hwnd, GWL_STYLE,
GetWindowLongW(hwnd, GWL_STYLE) & ~WS_MINIMIZEBOX);
}
void disable_maximize(HWND hwnd)
{
SetWindowLongW(hwnd, GWL_STYLE,
GetWindowLongW(hwnd, GWL_STYLE) & ~WS_MAXIMIZEBOX);
}
void disable_resize(HWND hwnd)
{
SetWindowLongW(hwnd, GWL_STYLE,
GetWindowLongW(hwnd, GWL_STYLE) & ~WS_SIZEBOX);
disable_maximize(hwnd);
}
LRESULT on_close(window* win)
{
if (win && win->on_close)
win->on_close();
return 0;
}
BOOL CALLBACK for_each_child(HWND child, LPARAM lParam)
{
LPRECT bounds = (LPRECT) lParam;
MoveWindow(
child,
0, 0,
bounds->right,
bounds->bottom,
TRUE);
// Make sure the child window is visible.
ShowWindow(child, SW_SHOW);
return true;
}
LRESULT on_size(HWND hwnd)
{
RECT bounds;
GetClientRect(hwnd, &bounds);
EnumChildWindows(hwnd, for_each_child, (LPARAM) &bounds);
return 0;
}
POINT window_frame_size(HWND hwnd)
{
RECT content, frame;
POINT extra;
GetClientRect(hwnd, &content);
GetWindowRect(hwnd, &frame);
extra.x = (frame.right - frame.left) - content.right;
extra.y = (frame.bottom - frame.top) - content.bottom;
return extra;
}
void constrain_size(HWND hwnd, RECT& r, view_limits limits)
{
auto scale = get_scale_for_window(hwnd);
auto extra = window_frame_size(hwnd);
auto w = ((r.right - r.left) - extra.x) / scale;
auto h = ((r.bottom - r.top) - extra.y) / scale;
if (w > limits.max.x)
r.right = r.left + extra.x + (limits.max.x * scale);
if (w < limits.min.x)
r.right = r.left + extra.x + (limits.min.x * scale);
if (h > limits.max.y)
r.bottom = r.top + extra.y + (limits.max.y * scale);
if (h < limits.min.y)
r.bottom = r.top + extra.y + (limits.min.y * scale);
}
LRESULT CALLBACK handle_event(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
auto* info = get_window_info(hwnd);
switch (message)
{
case WM_CLOSE:
ShowWindow(hwnd, SW_HIDE);
return on_close(info->wptr);
case WM_DPICHANGED:
case WM_SIZE:
return on_size(hwnd);
case WM_SIZING:
if (info)
{
auto& r = *reinterpret_cast<RECT*>(lparam);
constrain_size(hwnd, r, info->limits);
}
break;
default:
return DefWindowProcW(hwnd, message, wparam, lparam);
}
return 0;
}
struct init_window_class
{
init_window_class()
{
WNDCLASSW windowClass = {};
windowClass.hbrBackground = nullptr;
windowClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
windowClass.hInstance = GetModuleHandleW(nullptr);
windowClass.lpfnWndProc = handle_event;
windowClass.lpszClassName = L"ElementsWindow";
windowClass.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassW(&windowClass))
MessageBoxW(nullptr, L"Could not register class", L"Error", MB_OK);
}
};
}
window::window(std::string const& name, int style_, rect const& bounds)
{
static init_window_class init;
std::wstring wname = utf8_decode(name);
#ifdef ELEMENTS_HOST_ONLY_WIN7
auto scale = 1.0f;
#else
auto scale = GetDpiForSystem() / 96.0f;
#endif
_window = CreateWindowW(
L"ElementsWindow",
wname.c_str(),
WS_OVERLAPPEDWINDOW,
bounds.left * scale, bounds.top * scale,
bounds.width() * scale, bounds.height() * scale,
nullptr, nullptr, nullptr,
nullptr
);
auto* info = new window_info{this};
SetWindowLongPtrW(_window, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(info));
if (!(style_ & closable))
disable_close(_window);
if (!(style_ & miniaturizable))
disable_minimize(_window);
if (!(style_ & resizable))
disable_resize(_window);
// Sets the app icon for the window to show on the titlebar.
// The IDI_ELEMENTS_APP_ICON icon id should be defined in a resource file.
HICON hIcon = LoadIcon(GetModuleHandle(nullptr), TEXT("IDI_ELEMENTS_APP_ICON"));
if (hIcon)
::SendMessage(_window, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
ShowWindow(_window, SW_RESTORE);
}
window::~window()
{
delete get_window_info(_window);
DeleteObject(_window);
}
point window::size() const
{
auto scale = get_scale_for_window(_window);
RECT frame;
GetWindowRect(_window, &frame);
return {
float((frame.right - frame.left) / scale),
float((frame.bottom - frame.top) / scale)
};
}
void window::size(point const& p)
{
auto scale = get_scale_for_window(_window);
RECT frame;
GetWindowRect(_window, &frame);
frame.right = frame.left + (p.x * scale);
frame.bottom = frame.top + (p.y * scale);
constrain_size(
_window, frame, get_window_info(_window)->limits);
MoveWindow(
_window, frame.left, frame.top,
frame.right - frame.left,
frame.bottom - frame.top,
true // repaint
);
}
void window::limits(view_limits limits_)
{
get_window_info(_window)->limits = limits_;
RECT frame;
GetWindowRect(_window, &frame);
constrain_size(
_window, frame, get_window_info(_window)->limits);
MoveWindow(
_window, frame.left, frame.top,
frame.right - frame.left,
frame.bottom - frame.top,
true // repaint
);
}
point window::position() const
{
auto scale = get_scale_for_window(_window);
RECT frame;
GetWindowRect(_window, &frame);
return {float(frame.left / scale), float(frame.top / scale)};
}
void window::position(point const& p)
{
auto scale = get_scale_for_window(_window);
RECT frame;
GetWindowRect(_window, &frame);
MoveWindow(
_window, p.x * scale, p.y * scale,
frame.right - frame.left,
frame.bottom - frame.top,
true // repaint
);
}
}