-
Notifications
You must be signed in to change notification settings - Fork 269
Expand file tree
/
Copy pathWin32_Window.cpp
More file actions
716 lines (641 loc) · 35.3 KB
/
Win32_Window.cpp
File metadata and controls
716 lines (641 loc) · 35.3 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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/* <editor-fold desc="MIT License">
Copyright(c) 2018 Robert Osfield
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
</editor-fold> */
#include <vsg/io/Logger.h>
#include <vsg/io/Options.h>
#include <vsg/platform/win32/Win32_Window.h>
#include <vsg/ui/ApplicationEvent.h>
#include <vsg/ui/ScrollWheelEvent.h>
using namespace vsg;
using namespace vsgWin32;
namespace vsg
{
// Provide the Window::create(...) implementation that automatically maps to a Win32_Window
ref_ptr<Window> Window::create(vsg::ref_ptr<WindowTraits> traits)
{
return vsgWin32::Win32_Window::create(traits);
}
} // namespace vsg
namespace vsgWin32
{
class VSG_DECLSPEC Win32Surface : public vsg::Surface
{
public:
Win32Surface(vsg::Instance* instance, HWND window) :
vsg::Surface(VK_NULL_HANDLE, instance)
{
VkWin32SurfaceCreateInfoKHR surfaceCreateInfo{};
surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR;
surfaceCreateInfo.hinstance = ::GetModuleHandle(NULL);
surfaceCreateInfo.hwnd = window;
surfaceCreateInfo.pNext = nullptr;
auto result = vkCreateWin32SurfaceKHR(*instance, &surfaceCreateInfo, _instance->getAllocationCallbacks(), &_surface);
if (result != VK_SUCCESS)
{
throw Exception{"Failed to created Win32Surface.", result};
}
}
};
// our windows events callback
LRESULT CALLBACK Win32WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
Win32_Window* win = reinterpret_cast<Win32_Window*>(GetWindowLongPtr(hwnd, GWLP_USERDATA));
if (win != nullptr)
{
#if 1
// return if vsg::Win32_Window is able to handle it, otherwise fallback to the ::DefWindowProc(..) below
if (win->handleWin32Messages(msg, wParam, lParam)) return 0;
#else
win->handleWin32Messages(msg, wParam, lParam);
#endif
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
} // namespace vsgWin32
vsg::Exception vsgWin32::getLastErrorAsException(const std::string_view& prefix)
{
DWORD errorCode = GetLastError();
if (errorCode == NOERROR) return Exception{std::string(prefix), 0};
// Hopefully the error will be representable with the current eight-bit code page as Exception doesn't support wide strings
LPSTR buffer;
// Ignore the dodgy cast from pointer-to-pointer to pointer
// the argument should really be a union of those types as it's interpreted differently depending on the flags passed
std::size_t length = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr, errorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&buffer, 0, nullptr);
std::string message(buffer, length);
LocalFree(buffer);
if (!prefix.empty()) message.insert(message.begin(), prefix.begin(), prefix.end());
return Exception{message, static_cast<int>(errorCode)};
}
KeyboardMap::KeyboardMap()
{
// Note that Windows virtual key 'A' etc. correspond to the unmodified character 'a', hence the map below assigns capital letters to their corresponding lowercase ones.
// will modify this map.
// clang-format off
_vk2vsg =
{
{0x0 , KEY_Undefined},
{'0' , KEY_0},
{'1' , KEY_1},
{'2' , KEY_2},
{'3' , KEY_3},
{'4' , KEY_4},
{'5' , KEY_5},
{'6' , KEY_6},
{'7' , KEY_7},
{'8' , KEY_8},
{'9' , KEY_9},
{'A' , KEY_a},
{'B' , KEY_b},
{'C' , KEY_c},
{'D' , KEY_d},
{'E' , KEY_e},
{'F' , KEY_f},
{'G' , KEY_g},
{'H' , KEY_h},
{'I' , KEY_i},
{'J' , KEY_j},
{'K' , KEY_k},
{'L' , KEY_l},
{'M' , KEY_m},
{'N' , KEY_n},
{'O' , KEY_o},
{'P' , KEY_p},
{'Q' , KEY_q},
{'R' , KEY_r},
{'S' , KEY_s},
{'T' , KEY_t},
{'U' , KEY_u},
{'V' , KEY_v},
{'W' , KEY_w},
{'X' , KEY_x},
{'Y' , KEY_y},
{'Z' , KEY_z},
{VK_LBUTTON , KEY_Undefined},
{VK_RBUTTON , KEY_Undefined},
{VK_CANCEL , KEY_Undefined},
{VK_MBUTTON , KEY_Undefined},
{VK_XBUTTON1 , KEY_Undefined}, /* NOT contiguous with L & RBUTTON */
{VK_XBUTTON2 , KEY_Undefined}, /* NOT contiguous with L & RBUTTON */
{VK_BACK , KEY_BackSpace},
{VK_TAB , KEY_Tab},
{VK_CLEAR , KEY_Clear},
{VK_RETURN , KEY_Return},
{VK_SHIFT , KEY_Undefined},
{VK_CONTROL , KEY_Undefined},
{VK_MENU , KEY_Alt_L},
{VK_PAUSE , KEY_Pause},
{VK_CAPITAL , KEY_Undefined},
{VK_KANA , KEY_Undefined},
{VK_HANGEUL , KEY_Undefined},
{VK_HANGUL , KEY_Undefined},
{VK_JUNJA , KEY_Undefined},
{VK_FINAL , KEY_Undefined},
{VK_HANJA , KEY_Undefined},
{VK_KANJI , KEY_Undefined},
// {VK_IME_ON , KEY_Undefined},
// {VK_IME_OFF , KEY_Undefined},
{VK_ESCAPE , KEY_Escape },
{VK_CONVERT , KEY_Undefined},
{VK_NONCONVERT , KEY_Undefined},
{VK_ACCEPT , KEY_Undefined},
{VK_MODECHANGE , KEY_Undefined},
{VK_SPACE , KEY_Space },
{VK_PRIOR , KEY_Prior },
{VK_NEXT , KEY_Next },
{VK_END , KEY_End },
{VK_HOME , KEY_Home },
{VK_LEFT , KEY_Left },
{VK_UP , KEY_Up },
{VK_RIGHT , KEY_Right },
{VK_DOWN , KEY_Down },
{VK_SELECT , KEY_Select},
{VK_SNAPSHOT , KEY_Print},
{VK_EXECUTE , KEY_Execute},
{VK_SNAPSHOT , KEY_Undefined},
{VK_INSERT , KEY_Insert},
{VK_DELETE , KEY_Delete},
{VK_HELP , KEY_Help},
{VK_LWIN , KEY_Super_L},
{VK_RWIN , KEY_Super_R},
{VK_APPS , KEY_Menu},
{VK_SLEEP , KEY_Undefined},
{VK_NUMPAD0 , KEY_KP_0},
{VK_NUMPAD1 , KEY_KP_1},
{VK_NUMPAD2 , KEY_KP_2},
{VK_NUMPAD3 , KEY_KP_3},
{VK_NUMPAD4 , KEY_KP_4},
{VK_NUMPAD5 , KEY_KP_5},
{VK_NUMPAD6 , KEY_KP_6},
{VK_NUMPAD7 , KEY_KP_7},
{VK_NUMPAD8 , KEY_KP_8},
{VK_NUMPAD9 , KEY_KP_9},
{VK_MULTIPLY , KEY_KP_Multiply},
{VK_ADD , KEY_KP_Add},
{VK_SEPARATOR , KEY_KP_Separator},
{VK_SUBTRACT , KEY_KP_Subtract},
{VK_DECIMAL , KEY_KP_Decimal},
{VK_DIVIDE , KEY_KP_Divide},
{VK_F1 , KEY_F1},
{VK_F2 , KEY_F2},
{VK_F3 , KEY_F3},
{VK_F4 , KEY_F4},
{VK_F5 , KEY_F5},
{VK_F6 , KEY_F6},
{VK_F7 , KEY_F7},
{VK_F8 , KEY_F8},
{VK_F9 , KEY_F9},
{VK_F10 , KEY_F10},
{VK_F11 , KEY_F11},
{VK_F12 , KEY_F12},
{VK_F13 , KEY_F13},
{VK_F14 , KEY_F14},
{VK_F15 , KEY_F15},
{VK_F16 , KEY_F16},
{VK_F17 , KEY_F17},
{VK_F18 , KEY_F18},
{VK_F19 , KEY_F19},
{VK_F20 , KEY_F20},
{VK_F21 , KEY_F21},
{VK_F22 , KEY_F22},
{VK_F23 , KEY_F23},
{VK_F24 , KEY_F24},
{VK_NAVIGATION_VIEW , KEY_Undefined}, // reserved
{VK_NAVIGATION_MENU , KEY_Undefined}, // reserved
{VK_NAVIGATION_UP , KEY_Undefined}, // reserved
{VK_NAVIGATION_DOWN , KEY_Undefined}, // reserved
{VK_NAVIGATION_LEFT , KEY_Undefined}, // reserved
{VK_NAVIGATION_RIGHT , KEY_Undefined}, // reserved
{VK_NAVIGATION_ACCEPT , KEY_Undefined}, // reserved
{VK_NAVIGATION_CANCEL , KEY_Undefined}, // reserved
{VK_NUMLOCK , KEY_Undefined},
{VK_SCROLL , KEY_Scroll_Lock},
{VK_LSHIFT , KEY_Shift_L},
{VK_RSHIFT , KEY_Shift_R},
{VK_LCONTROL , KEY_Control_L},
{VK_RCONTROL , KEY_Control_R},
{VK_LMENU , KEY_Alt_L},
{VK_RMENU , KEY_Alt_R},
{VK_BROWSER_BACK , KEY_Undefined},
{VK_BROWSER_FORWARD , KEY_Undefined},
{VK_BROWSER_REFRESH , KEY_Undefined},
{VK_BROWSER_STOP , KEY_Undefined},
{VK_BROWSER_SEARCH , KEY_Undefined},
{VK_BROWSER_FAVORITES , KEY_Undefined},
{VK_BROWSER_HOME , KEY_Undefined},
{VK_VOLUME_MUTE , KEY_Undefined},
{VK_VOLUME_DOWN , KEY_Undefined},
{VK_VOLUME_UP , KEY_Undefined},
{VK_MEDIA_NEXT_TRACK , KEY_Undefined},
{VK_MEDIA_PREV_TRACK , KEY_Undefined},
{VK_MEDIA_STOP , KEY_Undefined},
{VK_MEDIA_PLAY_PAUSE , KEY_Undefined},
{VK_LAUNCH_MAIL , KEY_Undefined},
{VK_LAUNCH_MEDIA_SELECT , KEY_Undefined},
{VK_LAUNCH_APP1 , KEY_Undefined},
{VK_LAUNCH_APP2 , KEY_Undefined},
{VK_OEM_1 , KEY_Semicolon}, // ';:' for US
{VK_OEM_PLUS , KEY_Equals }, // '+' any country
{VK_OEM_COMMA , KEY_Comma }, // ',' any country
{VK_OEM_MINUS , KEY_Minus }, // '-' any country
{VK_OEM_PERIOD , KEY_Period }, // '.' any country
{VK_OEM_2 , KEY_Slash }, // '/?' for US
{VK_OEM_3 , KEY_Tilde }, // '`~' for US
{VK_GAMEPAD_A , KEY_Undefined},
{VK_GAMEPAD_B , KEY_Undefined},
{VK_GAMEPAD_X , KEY_Undefined},
{VK_GAMEPAD_Y , KEY_Undefined},
{VK_GAMEPAD_RIGHT_SHOULDER , KEY_Undefined},
{VK_GAMEPAD_LEFT_SHOULDER , KEY_Undefined},
{VK_GAMEPAD_LEFT_TRIGGER , KEY_Undefined},
{VK_GAMEPAD_RIGHT_TRIGGER , KEY_Undefined},
{VK_GAMEPAD_DPAD_UP , KEY_Undefined},
{VK_GAMEPAD_DPAD_DOWN , KEY_Undefined},
{VK_GAMEPAD_DPAD_LEFT , KEY_Undefined},
{VK_GAMEPAD_DPAD_RIGHT , KEY_Undefined},
{VK_GAMEPAD_MENU , KEY_Undefined},
{VK_GAMEPAD_VIEW , KEY_Undefined},
{VK_GAMEPAD_LEFT_THUMBSTICK_BUTTON , KEY_Undefined},
{VK_GAMEPAD_RIGHT_THUMBSTICK_BUTTON , KEY_Undefined},
{VK_GAMEPAD_LEFT_THUMBSTICK_UP , KEY_Undefined},
{VK_GAMEPAD_LEFT_THUMBSTICK_DOWN , KEY_Undefined},
{VK_GAMEPAD_LEFT_THUMBSTICK_RIGHT , KEY_Undefined},
{VK_GAMEPAD_LEFT_THUMBSTICK_LEFT , KEY_Undefined},
{VK_GAMEPAD_RIGHT_THUMBSTICK_UP , KEY_Undefined},
{VK_GAMEPAD_RIGHT_THUMBSTICK_DOWN , KEY_Undefined},
{VK_GAMEPAD_RIGHT_THUMBSTICK_RIGHT , KEY_Undefined},
{VK_GAMEPAD_RIGHT_THUMBSTICK_LEFT , KEY_Undefined},
{VK_OEM_4 , KEY_Leftbracket}, // '[{' for US
{VK_OEM_5 , KEY_Backslash}, // '\|' for US
{VK_OEM_6 , KEY_Rightbracket}, // ']}' for US
{VK_OEM_7 , KEY_Quote}, // ''"' for US
{VK_OEM_8 , KEY_Undefined},
{VK_OEM_AX , KEY_Undefined}, // 'AX' key on Japanese AX kbd
{VK_OEM_102 , KEY_Undefined}, // "<>" or "\|" on RT 102-key kbd.
{VK_ICO_HELP , KEY_Undefined}, // Help key on ICO
{VK_ICO_00 , KEY_Undefined}, // 00 key on ICO
{VK_PROCESSKEY , KEY_Undefined},
{VK_ICO_CLEAR , KEY_Undefined},
{VK_PACKET , KEY_Undefined},
{VK_OEM_RESET , KEY_Undefined},
{VK_OEM_JUMP , KEY_Undefined},
{VK_OEM_PA1 , KEY_Undefined},
{VK_OEM_PA2 , KEY_Undefined},
{VK_OEM_PA3 , KEY_Undefined},
{VK_OEM_WSCTRL , KEY_Undefined},
{VK_OEM_CUSEL , KEY_Undefined},
{VK_OEM_ATTN , KEY_Undefined},
{VK_OEM_FINISH , KEY_Undefined},
{VK_OEM_COPY , KEY_Undefined},
{VK_OEM_AUTO , KEY_Undefined},
{VK_OEM_ENLW , KEY_Undefined},
{VK_OEM_BACKTAB , KEY_Undefined},
{VK_ATTN , KEY_Undefined},
{VK_CRSEL , KEY_Undefined},
{VK_EXSEL , KEY_Undefined},
{VK_EREOF , KEY_Undefined},
{VK_PLAY , KEY_Undefined},
{VK_ZOOM , KEY_Undefined},
{VK_NONAME , KEY_Undefined},
{VK_PA1 , KEY_Undefined},
{VK_OEM_CLEAR , KEY_Undefined}
};
// clang-format on
}
Win32_Window::Win32_Window(vsg::ref_ptr<WindowTraits> traits) :
Inherit(traits),
_window(nullptr)
{
_keyboard = new KeyboardMap;
#ifdef UNICODE
std::wstring windowClass;
convert_utf(traits->windowClass, windowClass);
std::wstring windowTitle;
convert_utf(traits->windowTitle, windowTitle);
#else
const auto& windowClass = traits->windowClass;
const auto& windowTitle = traits->windowTitle;
#endif
bool createWindow = true;
if (traits->nativeWindow.has_value())
{
auto nativeWindow = std::any_cast<HWND>(traits->nativeWindow);
if (nativeWindow)
{
_window = nativeWindow;
createWindow = false;
}
}
if (createWindow)
{
// register window class
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc = Win32WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = ::GetModuleHandle(NULL);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = 0;
wc.lpszMenuName = 0;
wc.lpszClassName = windowClass.c_str();
wc.hIconSm = 0;
if (::RegisterClassEx(&wc) == 0)
{
auto lastError = ::GetLastError();
if (lastError != ERROR_CLASS_ALREADY_EXISTS) throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) failed to create Window, could not register window class :");
}
// fetch screen display information
std::vector<DISPLAY_DEVICE> displayDevices;
DISPLAY_DEVICE displayDevice;
displayDevice.cb = sizeof(displayDevice);
for (uint32_t deviceNum = 0; EnumDisplayDevices(nullptr, deviceNum, &displayDevice, 0); ++deviceNum)
{
if (displayDevice.StateFlags & DISPLAY_DEVICE_MIRRORING_DRIVER) continue;
if (!(displayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) continue;
displayDevices.push_back(displayDevice);
}
// assume a traits->screenNum of < 0 will default to screen 0
int32_t screenNum = traits->screenNum < 0 ? 0 : traits->screenNum;
if (screenNum >= static_cast<int32_t>(displayDevices.size()))
throw Exception{"Error: vsg::Win32_Window::Win32_Window(...) failed to create Window, screenNum is out of range.", VK_ERROR_INVALID_EXTERNAL_HANDLE};
DEVMODE deviceMode;
deviceMode.dmSize = sizeof(deviceMode);
deviceMode.dmDriverExtra = 0;
if (!::EnumDisplaySettings(displayDevices[screenNum].DeviceName, ENUM_CURRENT_SETTINGS, &deviceMode))
throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) failed to create Window, EnumDisplaySettings failed to fetch display settings : ");
// setup window rect and style
int32_t screenx = 0;
int32_t screeny = 0;
RECT windowRect;
uint32_t windowStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
uint32_t extendedStyle = 0;
if (!traits->fullscreen)
{
screenx = deviceMode.dmPosition.x + traits->x;
screeny = deviceMode.dmPosition.y + traits->y;
windowRect.left = screenx;
windowRect.top = screeny;
windowRect.right = windowRect.left + traits->width;
windowRect.bottom = windowRect.top + traits->height;
if (traits->decoration)
{
windowStyle |= WS_OVERLAPPEDWINDOW;
extendedStyle |= WS_EX_WINDOWEDGE |
WS_EX_APPWINDOW |
WS_EX_OVERLAPPEDWINDOW |
WS_EX_ACCEPTFILES |
WS_EX_LTRREADING;
// if decorated call adjust to account for borders etc
if (!::AdjustWindowRectEx(&windowRect, windowStyle, FALSE, extendedStyle))
throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) failed to create Window, AdjustWindowRectEx failed : ");
}
}
else
{
screenx = deviceMode.dmPosition.x;
screeny = deviceMode.dmPosition.y;
windowRect.left = screenx;
windowRect.top = screeny;
windowRect.right = windowRect.left + deviceMode.dmPelsWidth;
windowRect.bottom = windowRect.top + deviceMode.dmPelsHeight;
}
// create the window
_window = ::CreateWindowEx(extendedStyle, windowClass.c_str(), windowTitle.c_str(), windowStyle,
windowRect.left, windowRect.top, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top,
NULL, NULL, ::GetModuleHandle(NULL), NULL);
if (_window == nullptr) throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) failed to create Window, CreateWindowEx did not return a valid window handle : ");
// set window handle user data pointer to hold ref to this so we can retrieve in WindowsProc
// this function lies about failing so we must jump through some hoops
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setwindowlongptra#return-value
SetLastError(NOERROR);
if (!SetWindowLongPtr(_window, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(this)) && GetLastError() != NOERROR)
throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) SetWindowLongPtr(..) failed : ");
// reposition once the window has been created to account for borders etc
if (!::SetWindowPos(_window, nullptr, screenx, screeny, windowRect.right - windowRect.left, windowRect.bottom - windowRect.top, 0))
throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) SetWindowPos(..) failed : ");
traits->x = windowRect.left;
traits->y = windowRect.top;
traits->systemConnection = wc.hInstance;
ShowWindow(_window, SW_SHOW);
SetForegroundWindow(_window);
if (!SetFocus(_window))
throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) SetFocus(..) failed : ");
}
// get client rect to find final width and height of the view
RECT clientRect;
if (!::GetClientRect(_window, &clientRect))
throw getLastErrorAsException("vsg::Win32_Window::Win32_Window(...) GetClientRect(..) failed : ");
uint32_t finalWidth = clientRect.right - clientRect.left;
uint32_t finalHeight = clientRect.bottom - clientRect.top;
if (traits->device)
{
share(traits->device);
}
_extent2D.width = finalWidth;
_extent2D.height = finalHeight;
// assign dimensions
traits->width = finalWidth;
traits->height = finalHeight;
traits->nativeWindow = _window;
_windowMapped = true;
}
Win32_Window::~Win32_Window()
{
clear();
if (_window != nullptr)
{
TCHAR className[MAX_PATH];
GetClassName(_window, className, MAX_PATH);
if (!::DestroyWindow(_window))
{
vsg::warn(getLastErrorAsException("Win32_Window::~Win32_Window() ::DestroyWindow(_window) failed : "));
}
_window = nullptr;
// when should we unregister??
::UnregisterClass(className, ::GetModuleHandle(NULL));
}
}
void Win32_Window::_initSurface()
{
if (!_instance) _initInstance();
_surface = new vsgWin32::Win32Surface(_instance, _window);
}
bool Win32_Window::visible() const
{
return _window != 0 && _windowMapped;
}
void Win32_Window::releaseWindow()
{
_window = {};
}
bool Win32_Window::pollEvents(vsg::UIEvents& events)
{
vsg::clock::time_point event_time = vsg::clock::now();
MSG msg;
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
// somehow close all windows
events.emplace_back(vsg::CloseWindowEvent::create(this, event_time));
}
else
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
}
return Window::pollEvents(events);
}
void Win32_Window::resize()
{
RECT windowRect;
if (GetClientRect(_window, &windowRect))
{
_extent2D.width = windowRect.right - windowRect.left;
_extent2D.height = windowRect.bottom - windowRect.top;
buildSwapchain();
}
else
{
vsg::warn("Win32_Window::resize() GetClientRect(..) call failed unable to rebuild swapchain.");
}
}
bool Win32_Window::handleWin32Messages(UINT msg, WPARAM wParam, LPARAM lParam)
{
vsg::clock::time_point event_time = vsg::clock::now();
// get the current window rect
auto getWindowExtents = [](HWND window, int32_t& x, int32_t& y, int32_t& width, int32_t& height) -> bool {
RECT windowRect;
if (GetClientRect(window, &windowRect) == 0) return false;
x = windowRect.left;
y = windowRect.top;
width = windowRect.right - windowRect.left;
height = windowRect.bottom - windowRect.top;
return true;
};
switch (msg)
{
case WM_CLOSE:
bufferedEvents.emplace_back(vsg::CloseWindowEvent::create(this, event_time));
return true;
case WM_SHOWWINDOW: {
int32_t winx, winy, winw, winh;
if (getWindowExtents(_window, winx, winy, winw, winh))
{
bufferedEvents.emplace_back(vsg::ExposeWindowEvent::create(this, event_time, winx, winy, winw, winh));
}
return true;
}
case WM_DESTROY:
bufferedEvents.emplace_back(vsg::TerminateEvent::create(event_time));
_windowMapped = false;
return true;
case WM_PAINT:
if (resizeCallback)
resizeCallback();
ValidateRect(_window, NULL);
return true;
case WM_MOUSEMOVE: {
int32_t mx = GET_X_LPARAM(lParam);
int32_t my = GET_Y_LPARAM(lParam);
bufferedEvents.emplace_back(vsg::MoveEvent::create(this, event_time, mx, my, getButtonMask(wParam)));
return true;
}
break;
case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_XBUTTONDOWN:
case WM_LBUTTONDBLCLK:
case WM_MBUTTONDBLCLK:
case WM_RBUTTONDBLCLK:
case WM_XBUTTONDBLCLK: {
int32_t mx = GET_X_LPARAM(lParam);
int32_t my = GET_Y_LPARAM(lParam);
bufferedEvents.emplace_back(vsg::ButtonPressEvent::create(this, event_time, mx, my, getButtonMask(wParam), getButtonDownEventDetail(msg, HIWORD(wParam))));
//::SetCapture(_window);
return true;
}
break;
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
case WM_XBUTTONUP: {
int32_t mx = GET_X_LPARAM(lParam);
int32_t my = GET_Y_LPARAM(lParam);
bufferedEvents.emplace_back(vsg::ButtonReleaseEvent::create(this, event_time, mx, my, getButtonMask(wParam), getButtonUpEventDetail(msg, HIWORD(wParam))));
//::ReleaseCapture(); // should only release once all mouse buttons are released ??
return true;
}
break;
case WM_MOUSEWHEEL: {
bufferedEvents.emplace_back(vsg::ScrollWheelEvent::create(this, event_time, GET_WHEEL_DELTA_WPARAM(wParam) < 0 ? vec3(0.0f, -1.0f, 0.0f) : vec3(0.0f, 1.0f, 0.0f)));
return true;
}
case WM_MOVE: {
int32_t winx, winy, winw, winh;
if (getWindowExtents(_window, winx, winy, winw, winh))
{
bufferedEvents.emplace_back(vsg::ConfigureWindowEvent::create(this, event_time, winx, winy, winw, winh));
}
return true;
}
case WM_SIZE: {
int32_t winx, winy, winw, winh;
if (getWindowExtents(_window, winx, winy, winw, winh))
{
if (wParam == SIZE_MINIMIZED || wParam == SIZE_MAXHIDE || winw == 0 || winh == 0)
{
_windowMapped = false;
}
else
{
_windowMapped = true;
bufferedEvents.emplace_back(vsg::ConfigureWindowEvent::create(this, event_time, winx, winy, winw, winh));
}
}
return true;
}
case WM_EXITSIZEMOVE:
break;
case WM_KEYDOWN:
case WM_SYSKEYDOWN: {
vsg::KeySymbol keySymbol, modifiedKeySymbol;
vsg::KeyModifier keyModifier;
if (_keyboard->getKeySymbol(wParam, lParam, keySymbol, modifiedKeySymbol, keyModifier))
{
int32_t repeatCount = (lParam & 0xffff);
bufferedEvents.emplace_back(vsg::KeyPressEvent::create(this, event_time, keySymbol, modifiedKeySymbol, keyModifier, repeatCount));
}
return true;
}
case WM_KEYUP:
case WM_SYSKEYUP: {
vsg::KeySymbol keySymbol, modifiedKeySymbol;
vsg::KeyModifier keyModifier;
if (_keyboard->getKeySymbol(wParam, lParam, keySymbol, modifiedKeySymbol, keyModifier))
{
bufferedEvents.emplace_back(vsg::KeyReleaseEvent::create(this, event_time, keySymbol, modifiedKeySymbol, keyModifier, 0));
}
return true;
}
case WM_SETFOCUS: {
bufferedEvents.emplace_back(vsg::FocusInEvent::create(this, event_time));
return true;
}
case WM_KILLFOCUS: {
bufferedEvents.emplace_back(vsg::FocusOutEvent::create(this, event_time));
return true;
}
default:
break;
}
return false;
}