forked from hoffstadt/DearPyGui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmvViewport_win32.cpp
More file actions
543 lines (460 loc) · 14.4 KB
/
mvViewport_win32.cpp
File metadata and controls
543 lines (460 loc) · 14.4 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
#include "mvWindowsSpecifics.h"
static BYTE gprevious_ime_char;
static WORD glang_id;
// Forward declare message handler from imgui_impl_win32.cpp
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
static int
get_horizontal_shift(const HWND window_handle)
{
RECT window_rectangle, frame_rectangle;
GetWindowRect(window_handle, &window_rectangle);
if (DwmGetWindowAttribute(window_handle,
DWMWA_EXTENDED_FRAME_BOUNDS, &frame_rectangle, sizeof(RECT)) != S_OK)
{
return 0;
}
return frame_rectangle.left - window_rectangle.left;
}
static void
mvHandleModes(mvViewport& viewport)
{
mvViewportData* viewportData = (mvViewportData*)viewport.platformSpecifics;
viewportData->modes = WS_OVERLAPPED;
if (viewport.resizable && viewport.decorated) viewportData->modes |= WS_THICKFRAME;
if (viewport.decorated) {
viewportData->modes |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
}
else {
viewportData->modes |= WS_POPUP;
}
}
static void
mvPrerender(mvViewport& viewport)
{
MV_PROFILE_SCOPE("Viewport prerender")
mvViewportData* viewportData = (mvViewportData*)viewport.platformSpecifics;
if (viewportData->msg.message == WM_QUIT)
viewport.running = false;
{
// TODO: we probably need a separate mutex for this
std::lock_guard<std::recursive_mutex> lk(GContext->mutex);
if (viewport.posDirty)
{
int horizontal_shift = get_horizontal_shift(viewportData->handle);
SetWindowPos(viewportData->handle, viewport.alwaysOnTop ? HWND_TOPMOST : HWND_TOP, viewport.xpos - horizontal_shift, viewport.ypos, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE);
viewport.posDirty = false;
}
if (viewport.sizeDirty)
{
SetWindowPos(viewportData->handle, viewport.alwaysOnTop ? HWND_TOPMOST : HWND_TOP, 0, 0, viewport.actualWidth, viewport.actualHeight, SWP_SHOWWINDOW | SWP_NOMOVE);
viewport.sizeDirty = false;
}
if (viewport.modesDirty)
{
viewportData->modes = WS_OVERLAPPED;
if (viewport.resizable && viewport.decorated) viewportData->modes |= WS_THICKFRAME;
if (viewport.decorated) {
viewportData->modes |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
}
else {
viewportData->modes |= WS_POPUP;
}
SetWindowLongPtr(viewportData->handle, GWL_STYLE, viewportData->modes);
SetWindowPos(viewportData->handle, viewport.alwaysOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
viewport.modesDirty = false;
}
if (viewport.titleDirty)
{
SetWindowTextA(viewportData->handle, viewport.title.c_str());
viewport.titleDirty = false;
}
}
// Poll and handle messages (inputs, window resize, etc.)
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
if (GContext->IO.waitForInput)
::WaitMessage();
if (::PeekMessage(&viewportData->msg, nullptr, 0U, 0U, PM_REMOVE))
{
::TranslateMessage(&viewportData->msg);
::DispatchMessage(&viewportData->msg);
//continue;
}
{
// Font manager is thread-unsafe, so we'd better sync it
std::lock_guard<std::recursive_mutex> lk(GContext->mutex);
if (mvToolManager::GetFontManager().isInvalid())
{
mvToolManager::GetFontManager().rebuildAtlas();
ImGui_ImplDX11_InvalidateDeviceObjects();
mvToolManager::GetFontManager().updateAtlas();
}
}
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
}
static LRESULT
mvHandleMsg(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
{
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
return true;
static UINT_PTR puIDEvent = 0;
mvViewport* viewport = GContext->viewport;
mvGraphics& graphics = GContext->graphics;
mvViewportData* viewportData = (mvViewportData*)viewport->platformSpecifics;
mvGraphics_D3D11* graphicsData = (mvGraphics_D3D11*)graphics.backendSpecifics;
switch (msg)
{
case WM_PAINT:
{
if (GContext->frame > 0)
{
RECT rect;
RECT crect;
int awidth = 0;
int aheight = 0;
int cwidth = 0;
int cheight = 0;
if (GetWindowRect(hWnd, &rect))
{
awidth = rect.right - rect.left;
aheight = rect.bottom - rect.top;
}
if (GetClientRect(hWnd, &crect))
{
cwidth = crect.right - crect.left;
cheight = crect.bottom - crect.top;
}
{
std::lock_guard<std::recursive_mutex> lk(GContext->mutex);
viewport->actualWidth = awidth;
viewport->actualHeight = aheight;
GContext->viewport->clientHeight = cheight;
GContext->viewport->clientWidth = cwidth;
//GContext->viewport->resized = true;
mvOnResize();
GContext->viewport->resized = false;
if (mvToolManager::GetFontManager().isInvalid())
{
mvToolManager::GetFontManager().rebuildAtlas();
ImGui_ImplDX11_InvalidateDeviceObjects();
mvToolManager::GetFontManager().updateAtlas();
}
}
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
Render();
present(graphics, GContext->viewport->clearColor, GContext->viewport->vsync);
}
// must be called for the OS to do its thing
PAINTSTRUCT tPaint;
HDC tDeviceContext = BeginPaint(hWnd, &tPaint);
EndPaint(hWnd, &tPaint);
break;
}
case WM_GETMINMAXINFO:
{
// TODO: lock the mutex?
LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
lpMMI->ptMinTrackSize.x = viewport->minwidth;
lpMMI->ptMinTrackSize.y = viewport->minheight;
lpMMI->ptMaxTrackSize.x = viewport->maxwidth;
lpMMI->ptMaxTrackSize.y = viewport->maxheight;
break;
}
case WM_MOVING:
{
std::lock_guard<std::recursive_mutex> lk(GContext->mutex);
int horizontal_shift = get_horizontal_shift(viewportData->handle);
RECT rect = *(RECT*)(lParam);
viewport->xpos = rect.left + horizontal_shift;
viewport->ypos = rect.top;
break;
}
case WM_SIZE:
case WM_SIZING:
if (wParam != SIZE_MINIMIZED)
{
RECT rect;
RECT crect;
int awidth = 0;
int aheight = 0;
int cwidth = 0;
int cheight = 0;
if (GetWindowRect(hWnd, &rect))
{
awidth = rect.right - rect.left;
aheight = rect.bottom - rect.top;
}
if (GetClientRect(hWnd, &crect))
{
cwidth = crect.right - crect.left;
cheight = crect.bottom - crect.top;
}
std::lock_guard<std::recursive_mutex> lk(GContext->mutex);
viewport->actualWidth = awidth;
viewport->actualHeight = aheight;
if (viewport->decorated)
{
GContext->viewport->clientHeight = cheight;
GContext->viewport->clientWidth = cwidth;
}
else
{
GContext->viewport->clientHeight = cheight;
GContext->viewport->clientWidth = cwidth;
}
GContext->viewport->resized = true;
//mvOnResize();
// I believe this are only used for the error logger
viewport->width = (UINT)LOWORD(lParam);
viewport->height = (UINT)HIWORD(lParam);
if (graphicsData != nullptr)
{
if (viewport->decorated)
resize_swapchain(graphics, (int)(UINT)LOWORD(lParam), (int)(UINT)HIWORD(lParam));
else
resize_swapchain(graphics, awidth, aheight);
}
}
return 0;
case WM_ENTERSIZEMOVE:
{
// DefWindowProc below will block until mouse is released or moved.
// Timer events can still be caught so here we add a timer so we
// can continue rendering when catching the WM_TIMER event.
// Timer is killed in the WM_EXITSIZEMOVE case below.
puIDEvent = SetTimer(NULL, puIDEvent, USER_TIMER_MINIMUM, NULL);
SetTimer(hWnd, puIDEvent, USER_TIMER_MINIMUM, NULL);
break;
}
case WM_EXITSIZEMOVE:
{
KillTimer(hWnd, puIDEvent);
break;
}
case WM_TIMER:
{
if (wParam == puIDEvent)
mvOnResize();
break;
}
case WM_SYSCOMMAND:
if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
return 0;
break;
case WM_CLOSE:
if (GContext->viewport->disableClose) {
mvSubmitCallback([=]() {
mvRunCallback(GContext->callbackRegistry->onCloseCallback, 0, nullptr, GContext->callbackRegistry->onCloseCallbackUserData);
});
return 0;
}
GContext->started = false;
DestroyWindow(hWnd);
::PostQuitMessage(0);
return 0;
case WM_INPUTLANGCHANGE:
glang_id = LOWORD(lParam);
break;
case WM_IME_CHAR:
if (glang_id == 0x0412)
break;
auto& io = ImGui::GetIO();
DWORD wChar = (DWORD)wParam;
if (wChar <= 127)
{
io.AddInputCharacter(wChar);
return 0;
}
else if (wChar < 0xFF) {
//GBK also supports Traditional Chinese, Japanese, English, Russian and (partially) Greek.
//When input Japanese with MS Japanese IME under CP936, dual bytes of one Japanese char are send within one or two wParam in succession.
if (gprevious_ime_char == 0) {
gprevious_ime_char = (BYTE)(wChar & 0x00FF);
return 0;
}
else {
BYTE b2 = (BYTE)(wChar & 0x00FF);
wChar = MAKEWORD(gprevious_ime_char, b2);
gprevious_ime_char = 0;
}
}
else
{
// swap lower and upper part.
BYTE low = (BYTE)(wChar & 0x00FF);
BYTE high = (BYTE)((wChar & 0xFF00) >> 8);
if (gprevious_ime_char == 0)
wChar = MAKEWORD(high, low);
else
{
//wChar = MAKEWORD(previous_ime_char, high, low);
wChar = MAKEWORD(gprevious_ime_char, high);
gprevious_ime_char = low;
}
}
wchar_t ch[6];
MultiByteToWideChar(CP_OEMCP, 0, (LPCSTR)&wChar, 4, ch, 3);
io.AddInputCharacter(ch[0]);
return 0;
}
return ::DefWindowProc(hWnd, msg, wParam, lParam);
}
mvViewport*
mvCreateViewport(unsigned width, unsigned height)
{
mvViewport* viewport = new mvViewport();
viewport->width = width;
viewport->height = height;
viewport->platformSpecifics = new mvViewportData();
return viewport;
}
void
mvShowViewport(mvViewport& viewport, bool minimized, bool maximized)
{
mvViewportData* viewportData = (mvViewportData*)viewport.platformSpecifics;
viewportData->wc = {
sizeof(WNDCLASSEX),
CS_CLASSDC,
mvHandleMsg,
0L,
0L,
GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr,
_T(viewport.title.c_str()), nullptr
};
RegisterClassEx(&viewportData->wc);
mvHandleModes(viewport);
viewportData->handle = CreateWindow(viewportData->wc.lpszClassName, _T(viewport.title.c_str()),
viewportData->modes,
viewport.xpos, viewport.ypos,
viewport.actualWidth, viewport.actualHeight,
nullptr, nullptr, viewportData->wc.hInstance, nullptr);
viewport.clientHeight = viewport.actualHeight;
viewport.clientWidth = viewport.actualWidth;
if (!viewport.small_icon.empty())
{
HANDLE hIcon = LoadImage(0, _T(viewport.small_icon.c_str()), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
if (hIcon)
{
SendMessage(viewportData->handle, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
SendMessage(GetWindow(viewportData->handle, GW_OWNER), WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
}
}
if (!viewport.large_icon.empty())
{
HANDLE hIcon = LoadImage(0, _T(viewport.large_icon.c_str()), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
if (hIcon)
{
SendMessage(viewportData->handle, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
SendMessage(GetWindow(viewportData->handle, GW_OWNER), WM_SETICON, ICON_BIG, (LPARAM)hIcon);
}
}
gprevious_ime_char = 0;
HKL hkl = GetKeyboardLayout(0);
glang_id = LOWORD(hkl);
// Show the window
int cmdShow;
if (minimized)
cmdShow = SW_MINIMIZE;
else if (maximized)
cmdShow = SW_MAXIMIZE;
else
cmdShow = SW_SHOWDEFAULT;
::ShowWindow(viewportData->handle, cmdShow);
::UpdateWindow(viewportData->handle);
if (viewport.alwaysOnTop)
SetWindowPos(viewportData->handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigWindowsMoveFromTitleBarOnly = true;
if (GContext->IO.loadIniFile)
{
ImGui::LoadIniSettingsFromDisk(GContext->IO.iniFile.c_str());
io.IniFilename = nullptr;
if (GContext->IO.autoSaveIniFile)
io.IniFilename = GContext->IO.iniFile.c_str();
}
else
{
if (GContext->IO.iniFile.empty())
io.IniFilename = nullptr;
else
io.IniFilename = GContext->IO.iniFile.c_str();
}
if(GContext->IO.kbdNavigation)
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
if(GContext->IO.docking)
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigDockingWithShift = GContext->IO.dockingShiftOnly;
// Setup Dear ImGui style
ImGui::StyleColorsDark();
SetDefaultTheme();
// Setup Platform/Renderer bindings
ImGui_ImplWin32_Init(viewportData->handle);
}
void
mvMaximizeViewport(mvViewport& viewport)
{
mvViewportData* viewportData = (mvViewportData*)viewport.platformSpecifics;
ShowWindow(viewportData->handle, SW_MAXIMIZE);
}
void
mvMinimizeViewport(mvViewport& viewport)
{
mvViewportData* viewportData = (mvViewportData*)viewport.platformSpecifics;
ShowWindow(viewportData->handle, SW_MINIMIZE);
}
void
mvCleanupViewport(mvViewport& viewport)
{
mvViewportData* viewportData = (mvViewportData*)viewport.platformSpecifics;
ImGui_ImplWin32_Shutdown();
::DestroyWindow(viewportData->handle);
::UnregisterClass(viewportData->wc.lpszClassName, viewportData->wc.hInstance);
}
void
mvRenderFrame()
{
mvPrerender(*GContext->viewport);
Render();
present(GContext->graphics, GContext->viewport->clearColor, GContext->viewport->vsync);
}
void
mvToggleFullScreen(mvViewport& viewport)
{
mvViewportData* viewportData = (mvViewportData*)viewport.platformSpecifics;
static size_t storedWidth = 0;
static size_t storedHeight = 0;
static int storedXPos = 0;
static int storedYPos = 0;
size_t width = GetSystemMetrics(SM_CXSCREEN);
size_t height = GetSystemMetrics(SM_CYSCREEN);
if (viewport.fullScreen)
{
RECT rect;
rect.left = storedXPos;
rect.top = storedYPos;
rect.right = storedXPos + storedWidth;
rect.bottom = storedYPos + storedHeight;
SetWindowLongPtr(viewportData->handle, GWL_STYLE, WS_OVERLAPPEDWINDOW | WS_VISIBLE);
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);
MoveWindow(viewportData->handle, storedXPos, storedYPos, storedWidth, storedHeight, TRUE);
GContext->viewport->fullScreen = false;
}
else
{
storedWidth = GContext->viewport->actualWidth;
storedHeight = GContext->viewport->actualHeight;
storedXPos = GContext->viewport->xpos;
storedYPos = GContext->viewport->ypos;
SetWindowLongPtr(viewportData->handle, GWL_STYLE, WS_SYSMENU | WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_VISIBLE);
MoveWindow(viewportData->handle, 0, 0, width, height, TRUE);
GContext->viewport->fullScreen = true;
}
}