-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwindow_windows.cpp
More file actions
576 lines (491 loc) · 16.6 KB
/
window_windows.cpp
File metadata and controls
576 lines (491 loc) · 16.6 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
#include <windows.h>
#include <dwmapi.h>
#include <iostream>
#include <string>
#include "../../window.h"
#pragma comment(lib, "dwmapi.lib")
namespace nativeapi {
// Helper function to convert string to wide string
std::wstring StringToWideString(const std::string& str) {
if (str.empty()) return std::wstring();
int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), nullptr, 0);
std::wstring wstrTo(size_needed, 0);
MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
return wstrTo;
}
// Helper function to convert wide string to string
std::string WideStringToString(const std::wstring& wstr) {
if (wstr.empty()) return std::string();
int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), nullptr, 0, nullptr, nullptr);
std::string strTo(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, nullptr, nullptr);
return strTo;
}
// Private implementation class
class Window::Impl {
public:
Impl(HWND hwnd) : hwnd_(hwnd) {}
HWND hwnd_;
WINDOWPLACEMENT saved_placement_ = {};
bool placement_saved_ = false;
};
Window::Window() : pimpl_(new Impl(nullptr)) {
id = -1;
}
Window::Window(void* window) : pimpl_(new Impl(static_cast<HWND>(window))) {
id = reinterpret_cast<WindowID>(pimpl_->hwnd_);
}
Window::~Window() {
delete pimpl_;
}
void Window::Focus() {
if (pimpl_->hwnd_) {
SetForegroundWindow(pimpl_->hwnd_);
SetFocus(pimpl_->hwnd_);
}
}
void Window::Blur() {
if (pimpl_->hwnd_) {
SetWindowPos(pimpl_->hwnd_, HWND_BOTTOM, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
}
}
bool Window::IsFocused() const {
return pimpl_->hwnd_ && GetForegroundWindow() == pimpl_->hwnd_;
}
void Window::Show() {
if (pimpl_->hwnd_) {
ShowWindow(pimpl_->hwnd_, SW_SHOW);
SetForegroundWindow(pimpl_->hwnd_);
}
}
void Window::ShowInactive() {
if (pimpl_->hwnd_) {
ShowWindow(pimpl_->hwnd_, SW_SHOWNOACTIVATE);
}
}
void Window::Hide() {
if (pimpl_->hwnd_) {
ShowWindow(pimpl_->hwnd_, SW_HIDE);
}
}
bool Window::IsVisible() const {
return pimpl_->hwnd_ && IsWindowVisible(pimpl_->hwnd_);
}
void Window::Maximize() {
if (pimpl_->hwnd_ && !IsMaximized()) {
ShowWindow(pimpl_->hwnd_, SW_MAXIMIZE);
}
}
void Window::Unmaximize() {
if (pimpl_->hwnd_ && IsMaximized()) {
ShowWindow(pimpl_->hwnd_, SW_RESTORE);
}
}
bool Window::IsMaximized() const {
if (!pimpl_->hwnd_) return false;
WINDOWPLACEMENT placement;
placement.length = sizeof(WINDOWPLACEMENT);
if (GetWindowPlacement(pimpl_->hwnd_, &placement)) {
return placement.showCmd == SW_SHOWMAXIMIZED;
}
return false;
}
void Window::Minimize() {
if (pimpl_->hwnd_ && !IsMinimized()) {
ShowWindow(pimpl_->hwnd_, SW_MINIMIZE);
}
}
void Window::Restore() {
if (pimpl_->hwnd_) {
ShowWindow(pimpl_->hwnd_, SW_RESTORE);
}
}
bool Window::IsMinimized() const {
if (!pimpl_->hwnd_) return false;
WINDOWPLACEMENT placement;
placement.length = sizeof(WINDOWPLACEMENT);
if (GetWindowPlacement(pimpl_->hwnd_, &placement)) {
return placement.showCmd == SW_SHOWMINIMIZED;
}
return false;
}
void Window::SetFullScreen(bool is_full_screen) {
if (!pimpl_->hwnd_) return;
if (is_full_screen && !IsFullScreen()) {
// Save current window placement
pimpl_->saved_placement_.length = sizeof(WINDOWPLACEMENT);
GetWindowPlacement(pimpl_->hwnd_, &pimpl_->saved_placement_);
pimpl_->placement_saved_ = true;
// Get monitor info
HMONITOR hmonitor = MonitorFromWindow(pimpl_->hwnd_, MONITOR_DEFAULTTONEAREST);
MONITORINFO mi = { sizeof(mi) };
if (GetMonitorInfo(hmonitor, &mi)) {
// Remove window decorations
SetWindowLong(pimpl_->hwnd_, GWL_STYLE, WS_POPUP | WS_VISIBLE);
SetWindowPos(pimpl_->hwnd_, HWND_TOP,
mi.rcMonitor.left, mi.rcMonitor.top,
mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top,
SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
}
} else if (!is_full_screen && IsFullScreen()) {
// Restore window decorations
SetWindowLong(pimpl_->hwnd_, GWL_STYLE, WS_OVERLAPPEDWINDOW);
// Restore previous position and size
if (pimpl_->placement_saved_) {
SetWindowPlacement(pimpl_->hwnd_, &pimpl_->saved_placement_);
pimpl_->placement_saved_ = false;
}
SetWindowPos(pimpl_->hwnd_, nullptr, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER |
SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
}
}
bool Window::IsFullScreen() const {
if (!pimpl_->hwnd_) return false;
LONG style = GetWindowLong(pimpl_->hwnd_, GWL_STYLE);
return (style & WS_POPUP) && !(style & WS_OVERLAPPEDWINDOW);
}
void Window::SetBounds(Rectangle bounds) {
if (pimpl_->hwnd_) {
SetWindowPos(pimpl_->hwnd_, nullptr,
static_cast<int>(bounds.x), static_cast<int>(bounds.y),
static_cast<int>(bounds.width), static_cast<int>(bounds.height),
SWP_NOZORDER | SWP_NOACTIVATE);
}
}
Rectangle Window::GetBounds() const {
Rectangle bounds = {0, 0, 0, 0};
if (pimpl_->hwnd_) {
RECT rect;
if (GetWindowRect(pimpl_->hwnd_, &rect)) {
bounds.x = static_cast<double>(rect.left);
bounds.y = static_cast<double>(rect.top);
bounds.width = static_cast<double>(rect.right - rect.left);
bounds.height = static_cast<double>(rect.bottom - rect.top);
}
}
return bounds;
}
void Window::SetSize(Size size, bool animate) {
if (pimpl_->hwnd_) {
RECT rect;
if (GetWindowRect(pimpl_->hwnd_, &rect)) {
if (animate) {
// Simple animation simulation - in practice you might want smoother animation
UINT flags = SWP_NOZORDER | SWP_NOACTIVATE;
SetWindowPos(pimpl_->hwnd_, nullptr, rect.left, rect.top,
static_cast<int>(size.width), static_cast<int>(size.height), flags);
} else {
SetWindowPos(pimpl_->hwnd_, nullptr, rect.left, rect.top,
static_cast<int>(size.width), static_cast<int>(size.height),
SWP_NOZORDER | SWP_NOACTIVATE);
}
}
}
}
Size Window::GetSize() const {
Size size = {0, 0};
if (pimpl_->hwnd_) {
RECT rect;
if (GetWindowRect(pimpl_->hwnd_, &rect)) {
size.width = static_cast<double>(rect.right - rect.left);
size.height = static_cast<double>(rect.bottom - rect.top);
}
}
return size;
}
void Window::SetContentSize(Size size) {
if (pimpl_->hwnd_) {
RECT windowRect, clientRect;
if (GetWindowRect(pimpl_->hwnd_, &windowRect) && GetClientRect(pimpl_->hwnd_, &clientRect)) {
int borderWidth = (windowRect.right - windowRect.left) - (clientRect.right - clientRect.left);
int borderHeight = (windowRect.bottom - windowRect.top) - (clientRect.bottom - clientRect.top);
SetWindowPos(pimpl_->hwnd_, nullptr, windowRect.left, windowRect.top,
static_cast<int>(size.width) + borderWidth,
static_cast<int>(size.height) + borderHeight,
SWP_NOZORDER | SWP_NOACTIVATE);
}
}
}
Size Window::GetContentSize() const {
Size size = {0, 0};
if (pimpl_->hwnd_) {
RECT rect;
if (GetClientRect(pimpl_->hwnd_, &rect)) {
size.width = static_cast<double>(rect.right - rect.left);
size.height = static_cast<double>(rect.bottom - rect.top);
}
}
return size;
}
void Window::SetMinimumSize(Size size) {
// Windows doesn't have a direct API for this, would need to handle WM_GETMINMAXINFO
// This is a placeholder implementation
}
Size Window::GetMinimumSize() {
// Placeholder implementation
return Size{0, 0};
}
void Window::SetMaximumSize(Size size) {
// Windows doesn't have a direct API for this, would need to handle WM_GETMINMAXINFO
// This is a placeholder implementation
}
Size Window::GetMaximumSize() {
// Placeholder implementation
return Size{0, 0};
}
void Window::SetResizable(bool is_resizable) {
if (pimpl_->hwnd_) {
LONG style = GetWindowLong(pimpl_->hwnd_, GWL_STYLE);
if (is_resizable) {
style |= WS_THICKFRAME;
} else {
style &= ~WS_THICKFRAME;
}
SetWindowLong(pimpl_->hwnd_, GWL_STYLE, style);
SetWindowPos(pimpl_->hwnd_, nullptr, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
}
bool Window::IsResizable() const {
if (pimpl_->hwnd_) {
LONG style = GetWindowLong(pimpl_->hwnd_, GWL_STYLE);
return (style & WS_THICKFRAME) != 0;
}
return false;
}
void Window::SetMovable(bool is_movable) {
// Windows doesn't have a direct equivalent, this would require message handling
// Placeholder implementation
}
bool Window::IsMovable() const {
// Most Windows windows are movable by default
return true;
}
void Window::SetMinimizable(bool is_minimizable) {
if (pimpl_->hwnd_) {
LONG style = GetWindowLong(pimpl_->hwnd_, GWL_STYLE);
if (is_minimizable) {
style |= WS_MINIMIZEBOX;
} else {
style &= ~WS_MINIMIZEBOX;
}
SetWindowLong(pimpl_->hwnd_, GWL_STYLE, style);
SetWindowPos(pimpl_->hwnd_, nullptr, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
}
bool Window::IsMinimizable() const {
if (pimpl_->hwnd_) {
LONG style = GetWindowLong(pimpl_->hwnd_, GWL_STYLE);
return (style & WS_MINIMIZEBOX) != 0;
}
return false;
}
void Window::SetMaximizable(bool is_maximizable) {
if (pimpl_->hwnd_) {
LONG style = GetWindowLong(pimpl_->hwnd_, GWL_STYLE);
if (is_maximizable) {
style |= WS_MAXIMIZEBOX;
} else {
style &= ~WS_MAXIMIZEBOX;
}
SetWindowLong(pimpl_->hwnd_, GWL_STYLE, style);
SetWindowPos(pimpl_->hwnd_, nullptr, 0, 0, 0, 0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);
}
}
bool Window::IsMaximizable() const {
if (pimpl_->hwnd_) {
LONG style = GetWindowLong(pimpl_->hwnd_, GWL_STYLE);
return (style & WS_MAXIMIZEBOX) != 0;
}
return false;
}
void Window::SetFullScreenable(bool is_full_screenable) {
// Windows doesn't have a specific fullscreen capability flag
// This is handled by the SetFullScreen method
}
bool Window::IsFullScreenable() const {
// Most windows can be made fullscreen
return true;
}
void Window::SetClosable(bool is_closable) {
if (pimpl_->hwnd_) {
HMENU hMenu = GetSystemMenu(pimpl_->hwnd_, FALSE);
if (hMenu) {
if (is_closable) {
EnableMenuItem(hMenu, SC_CLOSE, MF_ENABLED);
} else {
EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED);
}
}
}
}
bool Window::IsClosable() const {
if (pimpl_->hwnd_) {
HMENU hMenu = GetSystemMenu(pimpl_->hwnd_, FALSE);
if (hMenu) {
MENUITEMINFOW mii = {};
mii.cbSize = sizeof(MENUITEMINFOW);
mii.fMask = MIIM_STATE;
if (GetMenuItemInfoW(hMenu, SC_CLOSE, FALSE, &mii)) {
return !(mii.fState & MFS_GRAYED);
}
}
}
return true;
}
void Window::SetAlwaysOnTop(bool is_always_on_top) {
if (pimpl_->hwnd_) {
SetWindowPos(pimpl_->hwnd_,
is_always_on_top ? HWND_TOPMOST : HWND_NOTOPMOST,
0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
}
bool Window::IsAlwaysOnTop() {
if (pimpl_->hwnd_) {
LONG_PTR exStyle = GetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE);
return (exStyle & WS_EX_TOPMOST) != 0;
}
return false;
}
void Window::SetPosition(Point point) {
if (pimpl_->hwnd_) {
SetWindowPos(pimpl_->hwnd_, nullptr,
static_cast<int>(point.x), static_cast<int>(point.y),
0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
}
Point Window::GetPosition() {
Point point = {0, 0};
if (pimpl_->hwnd_) {
RECT rect;
if (GetWindowRect(pimpl_->hwnd_, &rect)) {
point.x = static_cast<double>(rect.left);
point.y = static_cast<double>(rect.top);
}
}
return point;
}
void Window::SetTitle(std::string title) {
if (pimpl_->hwnd_) {
std::wstring wtitle = StringToWideString(title);
SetWindowTextW(pimpl_->hwnd_, wtitle.c_str());
}
}
std::string Window::GetTitle() {
if (pimpl_->hwnd_) {
int length = GetWindowTextLengthW(pimpl_->hwnd_);
if (length > 0) {
std::wstring wtitle(length + 1, L'\0');
GetWindowTextW(pimpl_->hwnd_, &wtitle[0], length + 1);
wtitle.resize(length);
return WideStringToString(wtitle);
}
}
return std::string();
}
void Window::SetHasShadow(bool has_shadow) {
// Windows handles shadows automatically, but we can modify the window attributes
if (pimpl_->hwnd_) {
LONG_PTR style = GetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE);
if (has_shadow) {
style &= ~WS_EX_NOACTIVATE; // Enable shadow
} else {
style |= WS_EX_NOACTIVATE; // This can reduce shadow in some cases
}
SetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE, style);
}
}
bool Window::HasShadow() const {
// Windows typically shows shadows for most windows
return true;
}
void Window::SetOpacity(float opacity) {
if (pimpl_->hwnd_) {
LONG_PTR exStyle = GetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE);
exStyle |= WS_EX_LAYERED;
SetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE, exStyle);
BYTE alpha = static_cast<BYTE>(opacity * 255);
SetLayeredWindowAttributes(pimpl_->hwnd_, 0, alpha, LWA_ALPHA);
}
}
float Window::GetOpacity() const {
if (pimpl_->hwnd_) {
BYTE alpha;
COLORREF colorKey;
DWORD flags;
if (GetLayeredWindowAttributes(pimpl_->hwnd_, &colorKey, &alpha, &flags)) {
if (flags & LWA_ALPHA) {
return static_cast<float>(alpha) / 255.0f;
}
}
}
return 1.0f;
}
void Window::SetVisibleOnAllWorkspaces(bool is_visible_on_all_workspaces) {
// Windows doesn't have the same virtual desktop concept as macOS
// This would require more complex implementation with Virtual Desktop APIs
}
bool Window::IsVisibleOnAllWorkspaces() const {
// Default behavior on Windows
return false;
}
void Window::SetIgnoreMouseEvents(bool is_ignore_mouse_events) {
if (pimpl_->hwnd_) {
LONG_PTR exStyle = GetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE);
if (is_ignore_mouse_events) {
exStyle |= WS_EX_TRANSPARENT;
} else {
exStyle &= ~WS_EX_TRANSPARENT;
}
SetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE, exStyle);
}
}
bool Window::IsIgnoreMouseEvents() const {
if (pimpl_->hwnd_) {
LONG_PTR exStyle = GetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE);
return (exStyle & WS_EX_TRANSPARENT) != 0;
}
return false;
}
void Window::SetFocusable(bool is_focusable) {
if (pimpl_->hwnd_) {
LONG_PTR exStyle = GetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE);
if (is_focusable) {
exStyle &= ~WS_EX_NOACTIVATE;
} else {
exStyle |= WS_EX_NOACTIVATE;
}
SetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE, exStyle);
}
}
bool Window::IsFocusable() const {
if (pimpl_->hwnd_) {
LONG_PTR exStyle = GetWindowLongPtr(pimpl_->hwnd_, GWL_EXSTYLE);
return (exStyle & WS_EX_NOACTIVATE) == 0;
}
return true;
}
void Window::StartDragging() {
if (pimpl_->hwnd_) {
// Simulate a title bar drag
ReleaseCapture();
SendMessage(pimpl_->hwnd_, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
}
void Window::StartResizing() {
if (pimpl_->hwnd_) {
// Start resize from bottom-right corner
ReleaseCapture();
SendMessage(pimpl_->hwnd_, WM_NCLBUTTONDOWN, HTBOTTOMRIGHT, 0);
}
}
void* Window::GetNSWindow() const {
// This method name suggests macOS compatibility - return HWND instead
return pimpl_->hwnd_;
}
} // namespace nativeapi