1 | // dear imgui: Platform Backend for Windows (standard windows API for 32-bits AND 64-bits applications) |
2 | // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..) |
3 | |
4 | // Implemented features: |
5 | // [X] Platform: Clipboard support (for Win32 this is actually part of core dear imgui) |
6 | // [X] Platform: Mouse support. Can discriminate Mouse/TouchScreen/Pen. |
7 | // [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy VK_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set] |
8 | // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'. |
9 | // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'. |
10 | // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'. |
11 | |
12 | // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this. |
13 | // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need. |
14 | // Learn about Dear ImGui: |
15 | // - FAQ https://dearimgui.com/faq |
16 | // - Getting Started https://dearimgui.com/getting-started |
17 | // - Documentation https://dearimgui.com/docs (same as your local docs/ folder). |
18 | // - Introduction, links and more at the top of imgui.cpp |
19 | |
20 | #include "imgui.h" |
21 | #ifndef IMGUI_DISABLE |
22 | #include "imgui_impl_win32.h" |
23 | #ifndef WIN32_LEAN_AND_MEAN |
24 | #define WIN32_LEAN_AND_MEAN |
25 | #endif |
26 | #include <windows.h> |
27 | #include <windowsx.h> // GET_X_LPARAM(), GET_Y_LPARAM() |
28 | #include <tchar.h> |
29 | #include <dwmapi.h> |
30 | |
31 | // Configuration flags to add in your imconfig.h file: |
32 | //#define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD // Disable gamepad support. This was meaningful before <1.81 but we now load XInput dynamically so the option is now less relevant. |
33 | |
34 | // Using XInput for gamepad (will load DLL dynamically) |
35 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
36 | #include <xinput.h> |
37 | typedef DWORD (WINAPI *PFN_XInputGetCapabilities)(DWORD, DWORD, XINPUT_CAPABILITIES*); |
38 | typedef DWORD (WINAPI *PFN_XInputGetState)(DWORD, XINPUT_STATE*); |
39 | #endif |
40 | |
41 | // CHANGELOG |
42 | // (minor and older changes stripped away, please see git history for details) |
43 | // 2024-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface. |
44 | // 2023-10-05: Inputs: Added support for extra ImGuiKey values: F13 to F24 function keys, app back/forward keys. |
45 | // 2023-09-25: Inputs: Synthesize key-down event on key-up for VK_SNAPSHOT / ImGuiKey_PrintScreen as Windows doesn't emit it (same behavior as GLFW/SDL). |
46 | // 2023-09-07: Inputs: Added support for keyboard codepage conversion for when application is compiled in MBCS mode and using a non-Unicode window. |
47 | // 2023-04-19: Added ImGui_ImplWin32_InitForOpenGL() to facilitate combining raw Win32/Winapi with OpenGL. (#3218) |
48 | // 2023-04-04: Inputs: Added support for io.AddMouseSourceEvent() to discriminate ImGuiMouseSource_Mouse/ImGuiMouseSource_TouchScreen/ImGuiMouseSource_Pen. (#2702) |
49 | // 2023-02-15: Inputs: Use WM_NCMOUSEMOVE / WM_NCMOUSELEAVE to track mouse position over non-client area (e.g. OS decorations) when app is not focused. (#6045, #6162) |
50 | // 2023-02-02: Inputs: Flipping WM_MOUSEHWHEEL (horizontal mouse-wheel) value to match other backends and offer consistent horizontal scrolling direction. (#4019, #6096, #1463) |
51 | // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11. |
52 | // 2022-09-28: Inputs: Convert WM_CHAR values with MultiByteToWideChar() when window class was registered as MBCS (not Unicode). |
53 | // 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported). |
54 | // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion. |
55 | // 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[]. |
56 | // 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+). |
57 | // 2022-01-17: Inputs: always update key mods next and before a key event (not in NewFrame) to fix input queue with very low framerates. |
58 | // 2022-01-12: Inputs: Update mouse inputs using WM_MOUSEMOVE/WM_MOUSELEAVE + fallback to provide it when focused but not hovered/captured. More standard and will allow us to pass it to future input queue API. |
59 | // 2022-01-12: Inputs: Maintain our own copy of MouseButtonsDown mask instead of using ImGui::IsAnyMouseDown() which will be obsoleted. |
60 | // 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range. |
61 | // 2021-12-16: Inputs: Fill VK_LCONTROL/VK_RCONTROL/VK_LSHIFT/VK_RSHIFT/VK_LMENU/VK_RMENU for completeness. |
62 | // 2021-08-17: Calling io.AddFocusEvent() on WM_SETFOCUS/WM_KILLFOCUS messages. |
63 | // 2021-08-02: Inputs: Fixed keyboard modifiers being reported when host window doesn't have focus. |
64 | // 2021-07-29: Inputs: MousePos is correctly reported when the host platform window is hovered but not focused (using TrackMouseEvent() to receive WM_MOUSELEAVE events). |
65 | // 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX). |
66 | // 2021-06-08: Fixed ImGui_ImplWin32_EnableDpiAwareness() and ImGui_ImplWin32_GetDpiScaleForMonitor() to handle Windows 8.1/10 features without a manifest (per-monitor DPI, and properly calls SetProcessDpiAwareness() on 8.1). |
67 | // 2021-03-23: Inputs: Clearing keyboard down array when losing focus (WM_KILLFOCUS). |
68 | // 2021-02-18: Added ImGui_ImplWin32_EnableAlphaCompositing(). Non Visual Studio users will need to link with dwmapi.lib (MinGW/gcc: use -ldwmapi). |
69 | // 2021-02-17: Fixed ImGui_ImplWin32_EnableDpiAwareness() attempting to get SetProcessDpiAwareness from shcore.dll on Windows 8 whereas it is only supported on Windows 8.1. |
70 | // 2021-01-25: Inputs: Dynamically loading XInput DLL. |
71 | // 2020-12-04: Misc: Fixed setting of io.DisplaySize to invalid/uninitialized data when after hwnd has been closed. |
72 | // 2020-03-03: Inputs: Calling AddInputCharacterUTF16() to support surrogate pairs leading to codepoint >= 0x10000 (for more complete CJK inputs) |
73 | // 2020-02-17: Added ImGui_ImplWin32_EnableDpiAwareness(), ImGui_ImplWin32_GetDpiScaleForHwnd(), ImGui_ImplWin32_GetDpiScaleForMonitor() helper functions. |
74 | // 2020-01-14: Inputs: Added support for #define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD/IMGUI_IMPL_WIN32_DISABLE_LINKING_XINPUT. |
75 | // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor. |
76 | // 2019-05-11: Inputs: Don't filter value from WM_CHAR before calling AddInputCharacter(). |
77 | // 2019-01-17: Misc: Using GetForegroundWindow()+IsChild() instead of GetActiveWindow() to be compatible with windows created in a different thread or parent. |
78 | // 2019-01-17: Inputs: Added support for mouse buttons 4 and 5 via WM_XBUTTON* messages. |
79 | // 2019-01-15: Inputs: Added support for XInput gamepads (if ImGuiConfigFlags_NavEnableGamepad is set by user application). |
80 | // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window. |
81 | // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor. |
82 | // 2018-06-10: Inputs: Fixed handling of mouse wheel messages to support fine position messages (typically sent by track-pads). |
83 | // 2018-06-08: Misc: Extracted imgui_impl_win32.cpp/.h away from the old combined DX9/DX10/DX11/DX12 examples. |
84 | // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag. |
85 | // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling). |
86 | // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space. |
87 | // 2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set). |
88 | // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves. |
89 | // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support. |
90 | // 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert. |
91 | // 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag. |
92 | // 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read. |
93 | // 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging. |
94 | // 2016-11-12: Inputs: Only call Win32 ::SetCursor(nullptr) when io.MouseDrawCursor is set. |
95 | |
96 | // Forward Declarations |
97 | static void ImGui_ImplWin32_InitPlatformInterface(bool platformHasOwnDC); |
98 | static void ImGui_ImplWin32_ShutdownPlatformInterface(); |
99 | static void ImGui_ImplWin32_UpdateMonitors(); |
100 | |
101 | struct ImGui_ImplWin32_Data |
102 | { |
103 | HWND hWnd; |
104 | HWND MouseHwnd; |
105 | int MouseTrackedArea; // 0: not tracked, 1: client are, 2: non-client area |
106 | int MouseButtonsDown; |
107 | INT64 Time; |
108 | INT64 TicksPerSecond; |
109 | ImGuiMouseCursor LastMouseCursor; |
110 | UINT32 KeyboardCodePage; |
111 | bool WantUpdateMonitors; |
112 | |
113 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
114 | bool HasGamepad; |
115 | bool WantUpdateHasGamepad; |
116 | HMODULE XInputDLL; |
117 | PFN_XInputGetCapabilities XInputGetCapabilities; |
118 | PFN_XInputGetState XInputGetState; |
119 | #endif |
120 | |
121 | ImGui_ImplWin32_Data() { memset(s: (void*)this, c: 0, n: sizeof(*this)); } |
122 | }; |
123 | |
124 | // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts |
125 | // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts. |
126 | // FIXME: multi-context support is not well tested and probably dysfunctional in this backend. |
127 | // FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context. |
128 | static ImGui_ImplWin32_Data* ImGui_ImplWin32_GetBackendData() |
129 | { |
130 | return ImGui::GetCurrentContext() ? (ImGui_ImplWin32_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; |
131 | } |
132 | |
133 | // Functions |
134 | static void ImGui_ImplWin32_UpdateKeyboardCodePage() |
135 | { |
136 | // Retrieve keyboard code page, required for handling of non-Unicode Windows. |
137 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
138 | HKL keyboard_layout = ::GetKeyboardLayout(0); |
139 | LCID keyboard_lcid = MAKELCID(HIWORD(keyboard_layout), SORT_DEFAULT); |
140 | if (::GetLocaleInfoA(keyboard_lcid, (LOCALE_RETURN_NUMBER | LOCALE_IDEFAULTANSICODEPAGE), (LPSTR)&bd->KeyboardCodePage, sizeof(bd->KeyboardCodePage)) == 0) |
141 | bd->KeyboardCodePage = CP_ACP; // Fallback to default ANSI code page when fails. |
142 | } |
143 | |
144 | static bool ImGui_ImplWin32_InitEx(void* hwnd, bool platform_has_own_dc) |
145 | { |
146 | ImGuiIO& io = ImGui::GetIO(); |
147 | IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!" ); |
148 | |
149 | INT64 perf_frequency, perf_counter; |
150 | if (!::QueryPerformanceFrequency((LARGE_INTEGER*)&perf_frequency)) |
151 | return false; |
152 | if (!::QueryPerformanceCounter((LARGE_INTEGER*)&perf_counter)) |
153 | return false; |
154 | |
155 | // Setup backend capabilities flags |
156 | ImGui_ImplWin32_Data* bd = IM_NEW(ImGui_ImplWin32_Data)(); |
157 | io.BackendPlatformUserData = (void*)bd; |
158 | io.BackendPlatformName = "imgui_impl_win32" ; |
159 | io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional) |
160 | io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used) |
161 | io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional) |
162 | io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can call io.AddMouseViewportEvent() with correct data (optional) |
163 | |
164 | bd->hWnd = (HWND)hwnd; |
165 | bd->WantUpdateMonitors = true; |
166 | bd->TicksPerSecond = perf_frequency; |
167 | bd->Time = perf_counter; |
168 | bd->LastMouseCursor = ImGuiMouseCursor_COUNT; |
169 | ImGui_ImplWin32_UpdateKeyboardCodePage(); |
170 | |
171 | // Our mouse update function expect PlatformHandle to be filled for the main viewport |
172 | ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
173 | main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (void*)bd->hWnd; |
174 | if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) |
175 | ImGui_ImplWin32_InitPlatformInterface(platformHasOwnDC: platform_has_own_dc); |
176 | |
177 | // Dynamically load XInput library |
178 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
179 | bd->WantUpdateHasGamepad = true; |
180 | const char* xinput_dll_names[] = |
181 | { |
182 | "xinput1_4.dll" , // Windows 8+ |
183 | "xinput1_3.dll" , // DirectX SDK |
184 | "xinput9_1_0.dll" , // Windows Vista, Windows 7 |
185 | "xinput1_2.dll" , // DirectX SDK |
186 | "xinput1_1.dll" // DirectX SDK |
187 | }; |
188 | for (int n = 0; n < IM_ARRAYSIZE(xinput_dll_names); n++) |
189 | if (HMODULE dll = ::LoadLibraryA(xinput_dll_names[n])) |
190 | { |
191 | bd->XInputDLL = dll; |
192 | bd->XInputGetCapabilities = (PFN_XInputGetCapabilities)::GetProcAddress(dll, "XInputGetCapabilities" ); |
193 | bd->XInputGetState = (PFN_XInputGetState)::GetProcAddress(dll, "XInputGetState" ); |
194 | break; |
195 | } |
196 | #endif // IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
197 | |
198 | return true; |
199 | } |
200 | |
201 | IMGUI_IMPL_API bool ImGui_ImplWin32_Init(void* hwnd) |
202 | { |
203 | return ImGui_ImplWin32_InitEx(hwnd, platform_has_own_dc: false); |
204 | } |
205 | |
206 | IMGUI_IMPL_API bool ImGui_ImplWin32_InitForOpenGL(void* hwnd) |
207 | { |
208 | // OpenGL needs CS_OWNDC |
209 | return ImGui_ImplWin32_InitEx(hwnd, platform_has_own_dc: true); |
210 | } |
211 | |
212 | void ImGui_ImplWin32_Shutdown() |
213 | { |
214 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
215 | IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?" ); |
216 | ImGuiIO& io = ImGui::GetIO(); |
217 | |
218 | ImGui_ImplWin32_ShutdownPlatformInterface(); |
219 | |
220 | // Unload XInput library |
221 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
222 | if (bd->XInputDLL) |
223 | ::FreeLibrary(bd->XInputDLL); |
224 | #endif // IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
225 | |
226 | io.BackendPlatformName = nullptr; |
227 | io.BackendPlatformUserData = nullptr; |
228 | io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport); |
229 | IM_DELETE(p: bd); |
230 | } |
231 | |
232 | static bool ImGui_ImplWin32_UpdateMouseCursor() |
233 | { |
234 | ImGuiIO& io = ImGui::GetIO(); |
235 | if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) |
236 | return false; |
237 | |
238 | ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor(); |
239 | if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) |
240 | { |
241 | // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor |
242 | ::SetCursor(nullptr); |
243 | } |
244 | else |
245 | { |
246 | // Show OS mouse cursor |
247 | LPTSTR win32_cursor = IDC_ARROW; |
248 | switch (imgui_cursor) |
249 | { |
250 | case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break; |
251 | case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break; |
252 | case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break; |
253 | case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break; |
254 | case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break; |
255 | case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break; |
256 | case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break; |
257 | case ImGuiMouseCursor_Hand: win32_cursor = IDC_HAND; break; |
258 | case ImGuiMouseCursor_NotAllowed: win32_cursor = IDC_NO; break; |
259 | } |
260 | ::SetCursor(::LoadCursor(nullptr, win32_cursor)); |
261 | } |
262 | return true; |
263 | } |
264 | |
265 | static bool IsVkDown(int vk) |
266 | { |
267 | return (::GetKeyState(vk) & 0x8000) != 0; |
268 | } |
269 | |
270 | static void ImGui_ImplWin32_AddKeyEvent(ImGuiKey key, bool down, int native_keycode, int native_scancode = -1) |
271 | { |
272 | ImGuiIO& io = ImGui::GetIO(); |
273 | io.AddKeyEvent(key, down); |
274 | io.SetKeyEventNativeData(key, native_keycode, native_scancode); // To support legacy indexing (<1.87 user code) |
275 | IM_UNUSED(native_scancode); |
276 | } |
277 | |
278 | static void ImGui_ImplWin32_ProcessKeyEventsWorkarounds() |
279 | { |
280 | // Left & right Shift keys: when both are pressed together, Windows tend to not generate the WM_KEYUP event for the first released one. |
281 | if (ImGui::IsKeyDown(ImGuiKey_LeftShift) && !IsVkDown(VK_LSHIFT)) |
282 | ImGui_ImplWin32_AddKeyEvent(ImGuiKey_LeftShift, false, VK_LSHIFT); |
283 | if (ImGui::IsKeyDown(ImGuiKey_RightShift) && !IsVkDown(VK_RSHIFT)) |
284 | ImGui_ImplWin32_AddKeyEvent(ImGuiKey_RightShift, false, VK_RSHIFT); |
285 | |
286 | // Sometimes WM_KEYUP for Win key is not passed down to the app (e.g. for Win+V on some setups, according to GLFW). |
287 | if (ImGui::IsKeyDown(ImGuiKey_LeftSuper) && !IsVkDown(VK_LWIN)) |
288 | ImGui_ImplWin32_AddKeyEvent(ImGuiKey_LeftSuper, false, VK_LWIN); |
289 | if (ImGui::IsKeyDown(ImGuiKey_RightSuper) && !IsVkDown(VK_RWIN)) |
290 | ImGui_ImplWin32_AddKeyEvent(ImGuiKey_RightSuper, false, VK_RWIN); |
291 | } |
292 | |
293 | static void ImGui_ImplWin32_UpdateKeyModifiers() |
294 | { |
295 | ImGuiIO& io = ImGui::GetIO(); |
296 | io.AddKeyEvent(ImGuiMod_Ctrl, IsVkDown(VK_CONTROL)); |
297 | io.AddKeyEvent(ImGuiMod_Shift, IsVkDown(VK_SHIFT)); |
298 | io.AddKeyEvent(ImGuiMod_Alt, IsVkDown(VK_MENU)); |
299 | io.AddKeyEvent(ImGuiMod_Super, IsVkDown(VK_APPS)); |
300 | } |
301 | |
302 | // This code supports multi-viewports (multiple OS Windows mapped into different Dear ImGui viewports) |
303 | // Because of that, it is a little more complicated than your typical single-viewport binding code! |
304 | static void ImGui_ImplWin32_UpdateMouseData() |
305 | { |
306 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
307 | ImGuiIO& io = ImGui::GetIO(); |
308 | IM_ASSERT(bd->hWnd != 0); |
309 | |
310 | POINT mouse_screen_pos; |
311 | bool has_mouse_screen_pos = ::GetCursorPos(&mouse_screen_pos) != 0; |
312 | |
313 | HWND focused_window = ::GetForegroundWindow(); |
314 | const bool is_app_focused = (focused_window && (focused_window == bd->hWnd || ::IsChild(focused_window, bd->hWnd) || ImGui::FindViewportByPlatformHandle((void*)focused_window))); |
315 | if (is_app_focused) |
316 | { |
317 | // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) |
318 | // When multi-viewports are enabled, all Dear ImGui positions are same as OS positions. |
319 | if (io.WantSetMousePos) |
320 | { |
321 | POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y }; |
322 | if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0) |
323 | ::ClientToScreen(focused_window, &pos); |
324 | ::SetCursorPos(pos.x, pos.y); |
325 | } |
326 | |
327 | // (Optional) Fallback to provide mouse position when focused (WM_MOUSEMOVE already provides this when hovered or captured) |
328 | // This also fills a short gap when clicking non-client area: WM_NCMOUSELEAVE -> modal OS move -> gap -> WM_NCMOUSEMOVE |
329 | if (!io.WantSetMousePos && bd->MouseTrackedArea == 0 && has_mouse_screen_pos) |
330 | { |
331 | // Single viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window) |
332 | // (This is the position you can get with ::GetCursorPos() + ::ScreenToClient() or WM_MOUSEMOVE.) |
333 | // Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor) |
334 | // (This is the position you can get with ::GetCursorPos() or WM_MOUSEMOVE + ::ClientToScreen(). In theory adding viewport->Pos to a client position would also be the same.) |
335 | POINT mouse_pos = mouse_screen_pos; |
336 | if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)) |
337 | ::ScreenToClient(bd->hWnd, &mouse_pos); |
338 | io.AddMousePosEvent(x: (float)mouse_pos.x, y: (float)mouse_pos.y); |
339 | } |
340 | } |
341 | |
342 | // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering. |
343 | // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic. |
344 | // - [X] Win32 backend correctly ignore viewports with the _NoInputs flag (here using ::WindowFromPoint with WM_NCHITTEST + HTTRANSPARENT in WndProc does that) |
345 | // Some backend are not able to handle that correctly. If a backend report an hovered viewport that has the _NoInputs flag (e.g. when dragging a window |
346 | // for docking, the viewport has the _NoInputs flag in order to allow us to find the viewport under), then Dear ImGui is forced to ignore the value reported |
347 | // by the backend, and use its flawed heuristic to guess the viewport behind. |
348 | // - [X] Win32 backend correctly reports this regardless of another viewport behind focused and dragged from (we need this to find a useful drag and drop target). |
349 | ImGuiID mouse_viewport_id = 0; |
350 | if (has_mouse_screen_pos) |
351 | if (HWND hovered_hwnd = ::WindowFromPoint(mouse_screen_pos)) |
352 | if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(platform_handle: (void*)hovered_hwnd)) |
353 | mouse_viewport_id = viewport->ID; |
354 | io.AddMouseViewportEvent(id: mouse_viewport_id); |
355 | } |
356 | |
357 | // Gamepad navigation mapping |
358 | static void ImGui_ImplWin32_UpdateGamepads() |
359 | { |
360 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
361 | ImGuiIO& io = ImGui::GetIO(); |
362 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
363 | //if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs. |
364 | // return; |
365 | |
366 | // Calling XInputGetState() every frame on disconnected gamepads is unfortunately too slow. |
367 | // Instead we refresh gamepad availability by calling XInputGetCapabilities() _only_ after receiving WM_DEVICECHANGE. |
368 | if (bd->WantUpdateHasGamepad) |
369 | { |
370 | XINPUT_CAPABILITIES caps = {}; |
371 | bd->HasGamepad = bd->XInputGetCapabilities ? (bd->XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS) : false; |
372 | bd->WantUpdateHasGamepad = false; |
373 | } |
374 | |
375 | io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; |
376 | XINPUT_STATE xinput_state; |
377 | XINPUT_GAMEPAD& gamepad = xinput_state.Gamepad; |
378 | if (!bd->HasGamepad || bd->XInputGetState == nullptr || bd->XInputGetState(0, &xinput_state) != ERROR_SUCCESS) |
379 | return; |
380 | io.BackendFlags |= ImGuiBackendFlags_HasGamepad; |
381 | |
382 | #define IM_SATURATE(V) (V < 0.0f ? 0.0f : V > 1.0f ? 1.0f : V) |
383 | #define MAP_BUTTON(KEY_NO, BUTTON_ENUM) { io.AddKeyEvent(KEY_NO, (gamepad.wButtons & BUTTON_ENUM) != 0); } |
384 | #define MAP_ANALOG(KEY_NO, VALUE, V0, V1) { float vn = (float)(VALUE - V0) / (float)(V1 - V0); io.AddKeyAnalogEvent(KEY_NO, vn > 0.10f, IM_SATURATE(vn)); } |
385 | MAP_BUTTON(ImGuiKey_GamepadStart, XINPUT_GAMEPAD_START); |
386 | MAP_BUTTON(ImGuiKey_GamepadBack, XINPUT_GAMEPAD_BACK); |
387 | MAP_BUTTON(ImGuiKey_GamepadFaceLeft, XINPUT_GAMEPAD_X); |
388 | MAP_BUTTON(ImGuiKey_GamepadFaceRight, XINPUT_GAMEPAD_B); |
389 | MAP_BUTTON(ImGuiKey_GamepadFaceUp, XINPUT_GAMEPAD_Y); |
390 | MAP_BUTTON(ImGuiKey_GamepadFaceDown, XINPUT_GAMEPAD_A); |
391 | MAP_BUTTON(ImGuiKey_GamepadDpadLeft, XINPUT_GAMEPAD_DPAD_LEFT); |
392 | MAP_BUTTON(ImGuiKey_GamepadDpadRight, XINPUT_GAMEPAD_DPAD_RIGHT); |
393 | MAP_BUTTON(ImGuiKey_GamepadDpadUp, XINPUT_GAMEPAD_DPAD_UP); |
394 | MAP_BUTTON(ImGuiKey_GamepadDpadDown, XINPUT_GAMEPAD_DPAD_DOWN); |
395 | MAP_BUTTON(ImGuiKey_GamepadL1, XINPUT_GAMEPAD_LEFT_SHOULDER); |
396 | MAP_BUTTON(ImGuiKey_GamepadR1, XINPUT_GAMEPAD_RIGHT_SHOULDER); |
397 | MAP_ANALOG(ImGuiKey_GamepadL2, gamepad.bLeftTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD, 255); |
398 | MAP_ANALOG(ImGuiKey_GamepadR2, gamepad.bRightTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD, 255); |
399 | MAP_BUTTON(ImGuiKey_GamepadL3, XINPUT_GAMEPAD_LEFT_THUMB); |
400 | MAP_BUTTON(ImGuiKey_GamepadR3, XINPUT_GAMEPAD_RIGHT_THUMB); |
401 | MAP_ANALOG(ImGuiKey_GamepadLStickLeft, gamepad.sThumbLX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768); |
402 | MAP_ANALOG(ImGuiKey_GamepadLStickRight, gamepad.sThumbLX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767); |
403 | MAP_ANALOG(ImGuiKey_GamepadLStickUp, gamepad.sThumbLY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767); |
404 | MAP_ANALOG(ImGuiKey_GamepadLStickDown, gamepad.sThumbLY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768); |
405 | MAP_ANALOG(ImGuiKey_GamepadRStickLeft, gamepad.sThumbRX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768); |
406 | MAP_ANALOG(ImGuiKey_GamepadRStickRight, gamepad.sThumbRX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767); |
407 | MAP_ANALOG(ImGuiKey_GamepadRStickUp, gamepad.sThumbRY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767); |
408 | MAP_ANALOG(ImGuiKey_GamepadRStickDown, gamepad.sThumbRY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768); |
409 | #undef MAP_BUTTON |
410 | #undef MAP_ANALOG |
411 | #endif // #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
412 | } |
413 | |
414 | static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR monitor, HDC, LPRECT, LPARAM) |
415 | { |
416 | MONITORINFO info = {}; |
417 | info.cbSize = sizeof(MONITORINFO); |
418 | if (!::GetMonitorInfo(monitor, &info)) |
419 | return TRUE; |
420 | ImGuiPlatformMonitor imgui_monitor; |
421 | imgui_monitor.MainPos = ImVec2((float)info.rcMonitor.left, (float)info.rcMonitor.top); |
422 | imgui_monitor.MainSize = ImVec2((float)(info.rcMonitor.right - info.rcMonitor.left), (float)(info.rcMonitor.bottom - info.rcMonitor.top)); |
423 | imgui_monitor.WorkPos = ImVec2((float)info.rcWork.left, (float)info.rcWork.top); |
424 | imgui_monitor.WorkSize = ImVec2((float)(info.rcWork.right - info.rcWork.left), (float)(info.rcWork.bottom - info.rcWork.top)); |
425 | imgui_monitor.DpiScale = ImGui_ImplWin32_GetDpiScaleForMonitor(monitor); |
426 | imgui_monitor.PlatformHandle = (void*)monitor; |
427 | ImGuiPlatformIO& io = ImGui::GetPlatformIO(); |
428 | if (info.dwFlags & MONITORINFOF_PRIMARY) |
429 | io.Monitors.push_front(imgui_monitor); |
430 | else |
431 | io.Monitors.push_back(imgui_monitor); |
432 | return TRUE; |
433 | } |
434 | |
435 | static void ImGui_ImplWin32_UpdateMonitors() |
436 | { |
437 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
438 | ImGui::GetPlatformIO().Monitors.resize(new_size: 0); |
439 | ::EnumDisplayMonitors(nullptr, nullptr, ImGui_ImplWin32_UpdateMonitors_EnumFunc, 0); |
440 | bd->WantUpdateMonitors = false; |
441 | } |
442 | |
443 | void ImGui_ImplWin32_NewFrame() |
444 | { |
445 | ImGuiIO& io = ImGui::GetIO(); |
446 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
447 | IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplWin32_Init()?" ); |
448 | |
449 | // Setup display size (every frame to accommodate for window resizing) |
450 | RECT rect = { 0, 0, 0, 0 }; |
451 | ::GetClientRect(bd->hWnd, &rect); |
452 | io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); |
453 | if (bd->WantUpdateMonitors) |
454 | ImGui_ImplWin32_UpdateMonitors(); |
455 | |
456 | // Setup time step |
457 | INT64 current_time = 0; |
458 | ::QueryPerformanceCounter((LARGE_INTEGER*)¤t_time); |
459 | io.DeltaTime = (float)(current_time - bd->Time) / bd->TicksPerSecond; |
460 | bd->Time = current_time; |
461 | |
462 | // Update OS mouse position |
463 | ImGui_ImplWin32_UpdateMouseData(); |
464 | |
465 | // Process workarounds for known Windows key handling issues |
466 | ImGui_ImplWin32_ProcessKeyEventsWorkarounds(); |
467 | |
468 | // Update OS mouse cursor with the cursor requested by imgui |
469 | ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor(); |
470 | if (bd->LastMouseCursor != mouse_cursor) |
471 | { |
472 | bd->LastMouseCursor = mouse_cursor; |
473 | ImGui_ImplWin32_UpdateMouseCursor(); |
474 | } |
475 | |
476 | // Update game controllers (if enabled and available) |
477 | ImGui_ImplWin32_UpdateGamepads(); |
478 | } |
479 | |
480 | // There is no distinct VK_xxx for keypad enter, instead it is VK_RETURN + KF_EXTENDED, we assign it an arbitrary value to make code more readable (VK_ codes go up to 255) |
481 | #define IM_VK_KEYPAD_ENTER (VK_RETURN + 256) |
482 | |
483 | // Map VK_xxx to ImGuiKey_xxx. |
484 | static ImGuiKey ImGui_ImplWin32_VirtualKeyToImGuiKey(WPARAM wParam) |
485 | { |
486 | switch (wParam) |
487 | { |
488 | case VK_TAB: return ImGuiKey_Tab; |
489 | case VK_LEFT: return ImGuiKey_LeftArrow; |
490 | case VK_RIGHT: return ImGuiKey_RightArrow; |
491 | case VK_UP: return ImGuiKey_UpArrow; |
492 | case VK_DOWN: return ImGuiKey_DownArrow; |
493 | case VK_PRIOR: return ImGuiKey_PageUp; |
494 | case VK_NEXT: return ImGuiKey_PageDown; |
495 | case VK_HOME: return ImGuiKey_Home; |
496 | case VK_END: return ImGuiKey_End; |
497 | case VK_INSERT: return ImGuiKey_Insert; |
498 | case VK_DELETE: return ImGuiKey_Delete; |
499 | case VK_BACK: return ImGuiKey_Backspace; |
500 | case VK_SPACE: return ImGuiKey_Space; |
501 | case VK_RETURN: return ImGuiKey_Enter; |
502 | case VK_ESCAPE: return ImGuiKey_Escape; |
503 | case VK_OEM_7: return ImGuiKey_Apostrophe; |
504 | case VK_OEM_COMMA: return ImGuiKey_Comma; |
505 | case VK_OEM_MINUS: return ImGuiKey_Minus; |
506 | case VK_OEM_PERIOD: return ImGuiKey_Period; |
507 | case VK_OEM_2: return ImGuiKey_Slash; |
508 | case VK_OEM_1: return ImGuiKey_Semicolon; |
509 | case VK_OEM_PLUS: return ImGuiKey_Equal; |
510 | case VK_OEM_4: return ImGuiKey_LeftBracket; |
511 | case VK_OEM_5: return ImGuiKey_Backslash; |
512 | case VK_OEM_6: return ImGuiKey_RightBracket; |
513 | case VK_OEM_3: return ImGuiKey_GraveAccent; |
514 | case VK_CAPITAL: return ImGuiKey_CapsLock; |
515 | case VK_SCROLL: return ImGuiKey_ScrollLock; |
516 | case VK_NUMLOCK: return ImGuiKey_NumLock; |
517 | case VK_SNAPSHOT: return ImGuiKey_PrintScreen; |
518 | case VK_PAUSE: return ImGuiKey_Pause; |
519 | case VK_NUMPAD0: return ImGuiKey_Keypad0; |
520 | case VK_NUMPAD1: return ImGuiKey_Keypad1; |
521 | case VK_NUMPAD2: return ImGuiKey_Keypad2; |
522 | case VK_NUMPAD3: return ImGuiKey_Keypad3; |
523 | case VK_NUMPAD4: return ImGuiKey_Keypad4; |
524 | case VK_NUMPAD5: return ImGuiKey_Keypad5; |
525 | case VK_NUMPAD6: return ImGuiKey_Keypad6; |
526 | case VK_NUMPAD7: return ImGuiKey_Keypad7; |
527 | case VK_NUMPAD8: return ImGuiKey_Keypad8; |
528 | case VK_NUMPAD9: return ImGuiKey_Keypad9; |
529 | case VK_DECIMAL: return ImGuiKey_KeypadDecimal; |
530 | case VK_DIVIDE: return ImGuiKey_KeypadDivide; |
531 | case VK_MULTIPLY: return ImGuiKey_KeypadMultiply; |
532 | case VK_SUBTRACT: return ImGuiKey_KeypadSubtract; |
533 | case VK_ADD: return ImGuiKey_KeypadAdd; |
534 | case IM_VK_KEYPAD_ENTER: return ImGuiKey_KeypadEnter; |
535 | case VK_LSHIFT: return ImGuiKey_LeftShift; |
536 | case VK_LCONTROL: return ImGuiKey_LeftCtrl; |
537 | case VK_LMENU: return ImGuiKey_LeftAlt; |
538 | case VK_LWIN: return ImGuiKey_LeftSuper; |
539 | case VK_RSHIFT: return ImGuiKey_RightShift; |
540 | case VK_RCONTROL: return ImGuiKey_RightCtrl; |
541 | case VK_RMENU: return ImGuiKey_RightAlt; |
542 | case VK_RWIN: return ImGuiKey_RightSuper; |
543 | case VK_APPS: return ImGuiKey_Menu; |
544 | case '0': return ImGuiKey_0; |
545 | case '1': return ImGuiKey_1; |
546 | case '2': return ImGuiKey_2; |
547 | case '3': return ImGuiKey_3; |
548 | case '4': return ImGuiKey_4; |
549 | case '5': return ImGuiKey_5; |
550 | case '6': return ImGuiKey_6; |
551 | case '7': return ImGuiKey_7; |
552 | case '8': return ImGuiKey_8; |
553 | case '9': return ImGuiKey_9; |
554 | case 'A': return ImGuiKey_A; |
555 | case 'B': return ImGuiKey_B; |
556 | case 'C': return ImGuiKey_C; |
557 | case 'D': return ImGuiKey_D; |
558 | case 'E': return ImGuiKey_E; |
559 | case 'F': return ImGuiKey_F; |
560 | case 'G': return ImGuiKey_G; |
561 | case 'H': return ImGuiKey_H; |
562 | case 'I': return ImGuiKey_I; |
563 | case 'J': return ImGuiKey_J; |
564 | case 'K': return ImGuiKey_K; |
565 | case 'L': return ImGuiKey_L; |
566 | case 'M': return ImGuiKey_M; |
567 | case 'N': return ImGuiKey_N; |
568 | case 'O': return ImGuiKey_O; |
569 | case 'P': return ImGuiKey_P; |
570 | case 'Q': return ImGuiKey_Q; |
571 | case 'R': return ImGuiKey_R; |
572 | case 'S': return ImGuiKey_S; |
573 | case 'T': return ImGuiKey_T; |
574 | case 'U': return ImGuiKey_U; |
575 | case 'V': return ImGuiKey_V; |
576 | case 'W': return ImGuiKey_W; |
577 | case 'X': return ImGuiKey_X; |
578 | case 'Y': return ImGuiKey_Y; |
579 | case 'Z': return ImGuiKey_Z; |
580 | case VK_F1: return ImGuiKey_F1; |
581 | case VK_F2: return ImGuiKey_F2; |
582 | case VK_F3: return ImGuiKey_F3; |
583 | case VK_F4: return ImGuiKey_F4; |
584 | case VK_F5: return ImGuiKey_F5; |
585 | case VK_F6: return ImGuiKey_F6; |
586 | case VK_F7: return ImGuiKey_F7; |
587 | case VK_F8: return ImGuiKey_F8; |
588 | case VK_F9: return ImGuiKey_F9; |
589 | case VK_F10: return ImGuiKey_F10; |
590 | case VK_F11: return ImGuiKey_F11; |
591 | case VK_F12: return ImGuiKey_F12; |
592 | case VK_F13: return ImGuiKey_F13; |
593 | case VK_F14: return ImGuiKey_F14; |
594 | case VK_F15: return ImGuiKey_F15; |
595 | case VK_F16: return ImGuiKey_F16; |
596 | case VK_F17: return ImGuiKey_F17; |
597 | case VK_F18: return ImGuiKey_F18; |
598 | case VK_F19: return ImGuiKey_F19; |
599 | case VK_F20: return ImGuiKey_F20; |
600 | case VK_F21: return ImGuiKey_F21; |
601 | case VK_F22: return ImGuiKey_F22; |
602 | case VK_F23: return ImGuiKey_F23; |
603 | case VK_F24: return ImGuiKey_F24; |
604 | case VK_BROWSER_BACK: return ImGuiKey_AppBack; |
605 | case VK_BROWSER_FORWARD: return ImGuiKey_AppForward; |
606 | default: return ImGuiKey_None; |
607 | } |
608 | } |
609 | |
610 | // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions. |
611 | #ifndef WM_MOUSEHWHEEL |
612 | #define WM_MOUSEHWHEEL 0x020E |
613 | #endif |
614 | #ifndef DBT_DEVNODES_CHANGED |
615 | #define DBT_DEVNODES_CHANGED 0x0007 |
616 | #endif |
617 | |
618 | // Win32 message handler (process Win32 mouse/keyboard inputs, etc.) |
619 | // Call from your application's message handler. Keep calling your message handler unless this function returns TRUE. |
620 | // When implementing your own backend, you can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if Dear ImGui wants to use your inputs. |
621 | // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data. |
622 | // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data. |
623 | // Generally you may always pass all inputs to Dear ImGui, and hide them from your application based on those two flags. |
624 | // PS: In this Win32 handler, we use the capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinates when dragging mouse outside of our window bounds. |
625 | // PS: We treat DBLCLK messages as regular mouse down messages, so this code will work on windows classes that have the CS_DBLCLKS flag set. Our own example app code doesn't set this flag. |
626 | #if 0 |
627 | // Copy this line into your .cpp file to forward declare the function. |
628 | extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
629 | #endif |
630 | |
631 | // See https://learn.microsoft.com/en-us/windows/win32/tablet/system-events-and-mouse-messages |
632 | // Prefer to call this at the top of the message handler to avoid the possibility of other Win32 calls interfering with this. |
633 | static ImGuiMouseSource () |
634 | { |
635 | LPARAM = ::GetMessageExtraInfo(); |
636 | if ((extra_info & 0xFFFFFF80) == 0xFF515700) |
637 | return ImGuiMouseSource_Pen; |
638 | if ((extra_info & 0xFFFFFF80) == 0xFF515780) |
639 | return ImGuiMouseSource_TouchScreen; |
640 | return ImGuiMouseSource_Mouse; |
641 | } |
642 | |
643 | IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
644 | { |
645 | if (ImGui::GetCurrentContext() == nullptr) |
646 | return 0; |
647 | |
648 | ImGuiIO& io = ImGui::GetIO(); |
649 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
650 | |
651 | switch (msg) |
652 | { |
653 | case WM_MOUSEMOVE: |
654 | case WM_NCMOUSEMOVE: |
655 | { |
656 | // We need to call TrackMouseEvent in order to receive WM_MOUSELEAVE events |
657 | ImGuiMouseSource mouse_source = GetMouseSourceFromMessageExtraInfo(); |
658 | const int area = (msg == WM_MOUSEMOVE) ? 1 : 2; |
659 | bd->MouseHwnd = hwnd; |
660 | if (bd->MouseTrackedArea != area) |
661 | { |
662 | TRACKMOUSEEVENT tme_cancel = { sizeof(tme_cancel), TME_CANCEL, hwnd, 0 }; |
663 | TRACKMOUSEEVENT tme_track = { sizeof(tme_track), (DWORD)((area == 2) ? (TME_LEAVE | TME_NONCLIENT) : TME_LEAVE), hwnd, 0 }; |
664 | if (bd->MouseTrackedArea != 0) |
665 | ::TrackMouseEvent(&tme_cancel); |
666 | ::TrackMouseEvent(&tme_track); |
667 | bd->MouseTrackedArea = area; |
668 | } |
669 | POINT mouse_pos = { (LONG)GET_X_LPARAM(lParam), (LONG)GET_Y_LPARAM(lParam) }; |
670 | bool want_absolute_pos = (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) != 0; |
671 | if (msg == WM_MOUSEMOVE && want_absolute_pos) // WM_MOUSEMOVE are client-relative coordinates. |
672 | ::ClientToScreen(hwnd, &mouse_pos); |
673 | if (msg == WM_NCMOUSEMOVE && !want_absolute_pos) // WM_NCMOUSEMOVE are absolute coordinates. |
674 | ::ScreenToClient(hwnd, &mouse_pos); |
675 | io.AddMouseSourceEvent(source: mouse_source); |
676 | io.AddMousePosEvent(x: (float)mouse_pos.x, y: (float)mouse_pos.y); |
677 | break; |
678 | } |
679 | case WM_MOUSELEAVE: |
680 | case WM_NCMOUSELEAVE: |
681 | { |
682 | const int area = (msg == WM_MOUSELEAVE) ? 1 : 2; |
683 | if (bd->MouseTrackedArea == area) |
684 | { |
685 | if (bd->MouseHwnd == hwnd) |
686 | bd->MouseHwnd = nullptr; |
687 | bd->MouseTrackedArea = 0; |
688 | io.AddMousePosEvent(x: -FLT_MAX, y: -FLT_MAX); |
689 | } |
690 | break; |
691 | } |
692 | case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK: |
693 | case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK: |
694 | case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK: |
695 | case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK: |
696 | { |
697 | ImGuiMouseSource mouse_source = GetMouseSourceFromMessageExtraInfo(); |
698 | int button = 0; |
699 | if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { button = 0; } |
700 | if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; } |
701 | if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; } |
702 | if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; } |
703 | if (bd->MouseButtonsDown == 0 && ::GetCapture() == nullptr) |
704 | ::SetCapture(hwnd); |
705 | bd->MouseButtonsDown |= 1 << button; |
706 | io.AddMouseSourceEvent(source: mouse_source); |
707 | io.AddMouseButtonEvent(button, down: true); |
708 | return 0; |
709 | } |
710 | case WM_LBUTTONUP: |
711 | case WM_RBUTTONUP: |
712 | case WM_MBUTTONUP: |
713 | case WM_XBUTTONUP: |
714 | { |
715 | ImGuiMouseSource mouse_source = GetMouseSourceFromMessageExtraInfo(); |
716 | int button = 0; |
717 | if (msg == WM_LBUTTONUP) { button = 0; } |
718 | if (msg == WM_RBUTTONUP) { button = 1; } |
719 | if (msg == WM_MBUTTONUP) { button = 2; } |
720 | if (msg == WM_XBUTTONUP) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; } |
721 | bd->MouseButtonsDown &= ~(1 << button); |
722 | if (bd->MouseButtonsDown == 0 && ::GetCapture() == hwnd) |
723 | ::ReleaseCapture(); |
724 | io.AddMouseSourceEvent(source: mouse_source); |
725 | io.AddMouseButtonEvent(button, down: false); |
726 | return 0; |
727 | } |
728 | case WM_MOUSEWHEEL: |
729 | io.AddMouseWheelEvent(0.0f, (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA); |
730 | return 0; |
731 | case WM_MOUSEHWHEEL: |
732 | io.AddMouseWheelEvent(-(float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA, 0.0f); |
733 | return 0; |
734 | case WM_KEYDOWN: |
735 | case WM_KEYUP: |
736 | case WM_SYSKEYDOWN: |
737 | case WM_SYSKEYUP: |
738 | { |
739 | const bool is_key_down = (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN); |
740 | if (wParam < 256) |
741 | { |
742 | // Submit modifiers |
743 | ImGui_ImplWin32_UpdateKeyModifiers(); |
744 | |
745 | // Obtain virtual key code |
746 | // (keypad enter doesn't have its own... VK_RETURN with KF_EXTENDED flag means keypad enter, see IM_VK_KEYPAD_ENTER definition for details, it is mapped to ImGuiKey_KeyPadEnter.) |
747 | int vk = (int)wParam; |
748 | if ((wParam == VK_RETURN) && (HIWORD(lParam) & KF_EXTENDED)) |
749 | vk = IM_VK_KEYPAD_ENTER; |
750 | const ImGuiKey key = ImGui_ImplWin32_VirtualKeyToImGuiKey(vk); |
751 | const int scancode = (int)LOBYTE(HIWORD(lParam)); |
752 | |
753 | // Special behavior for VK_SNAPSHOT / ImGuiKey_PrintScreen as Windows doesn't emit the key down event. |
754 | if (key == ImGuiKey_PrintScreen && !is_key_down) |
755 | ImGui_ImplWin32_AddKeyEvent(key, down: true, native_keycode: vk, native_scancode: scancode); |
756 | |
757 | // Submit key event |
758 | if (key != ImGuiKey_None) |
759 | ImGui_ImplWin32_AddKeyEvent(key, down: is_key_down, native_keycode: vk, native_scancode: scancode); |
760 | |
761 | // Submit individual left/right modifier events |
762 | if (vk == VK_SHIFT) |
763 | { |
764 | // Important: Shift keys tend to get stuck when pressed together, missing key-up events are corrected in ImGui_ImplWin32_ProcessKeyEventsWorkarounds() |
765 | if (IsVkDown(VK_LSHIFT) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(ImGuiKey_LeftShift, is_key_down, VK_LSHIFT, scancode); } |
766 | if (IsVkDown(VK_RSHIFT) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(ImGuiKey_RightShift, is_key_down, VK_RSHIFT, scancode); } |
767 | } |
768 | else if (vk == VK_CONTROL) |
769 | { |
770 | if (IsVkDown(VK_LCONTROL) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(ImGuiKey_LeftCtrl, is_key_down, VK_LCONTROL, scancode); } |
771 | if (IsVkDown(VK_RCONTROL) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(ImGuiKey_RightCtrl, is_key_down, VK_RCONTROL, scancode); } |
772 | } |
773 | else if (vk == VK_MENU) |
774 | { |
775 | if (IsVkDown(VK_LMENU) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(ImGuiKey_LeftAlt, is_key_down, VK_LMENU, scancode); } |
776 | if (IsVkDown(VK_RMENU) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(ImGuiKey_RightAlt, is_key_down, VK_RMENU, scancode); } |
777 | } |
778 | } |
779 | return 0; |
780 | } |
781 | case WM_SETFOCUS: |
782 | case WM_KILLFOCUS: |
783 | io.AddFocusEvent(msg == WM_SETFOCUS); |
784 | return 0; |
785 | case WM_INPUTLANGCHANGE: |
786 | ImGui_ImplWin32_UpdateKeyboardCodePage(); |
787 | return 0; |
788 | case WM_CHAR: |
789 | if (::IsWindowUnicode(hwnd)) |
790 | { |
791 | // You can also use ToAscii()+GetKeyboardState() to retrieve characters. |
792 | if (wParam > 0 && wParam < 0x10000) |
793 | io.AddInputCharacterUTF16(c: (unsigned short)wParam); |
794 | } |
795 | else |
796 | { |
797 | wchar_t wch = 0; |
798 | ::MultiByteToWideChar(bd->KeyboardCodePage, MB_PRECOMPOSED, (char*)&wParam, 1, &wch, 1); |
799 | io.AddInputCharacter(c: wch); |
800 | } |
801 | return 0; |
802 | case WM_SETCURSOR: |
803 | // This is required to restore cursor when transitioning from e.g resize borders to client area. |
804 | if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor()) |
805 | return 1; |
806 | return 0; |
807 | case WM_DEVICECHANGE: |
808 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
809 | if ((UINT)wParam == DBT_DEVNODES_CHANGED) |
810 | bd->WantUpdateHasGamepad = true; |
811 | #endif |
812 | return 0; |
813 | case WM_DISPLAYCHANGE: |
814 | bd->WantUpdateMonitors = true; |
815 | return 0; |
816 | } |
817 | return 0; |
818 | } |
819 | |
820 | |
821 | //-------------------------------------------------------------------------------------------------------- |
822 | // DPI-related helpers (optional) |
823 | //-------------------------------------------------------------------------------------------------------- |
824 | // - Use to enable DPI awareness without having to create an application manifest. |
825 | // - Your own app may already do this via a manifest or explicit calls. This is mostly useful for our examples/ apps. |
826 | // - In theory we could call simple functions from Windows SDK such as SetProcessDPIAware(), SetProcessDpiAwareness(), etc. |
827 | // but most of the functions provided by Microsoft require Windows 8.1/10+ SDK at compile time and Windows 8/10+ at runtime, |
828 | // neither we want to require the user to have. So we dynamically select and load those functions to avoid dependencies. |
829 | //--------------------------------------------------------------------------------------------------------- |
830 | // This is the scheme successfully used by GLFW (from which we borrowed some of the code) and other apps aiming to be highly portable. |
831 | // ImGui_ImplWin32_EnableDpiAwareness() is just a helper called by main.cpp, we don't call it automatically. |
832 | // If you are trying to implement your own backend for your own engine, you may ignore that noise. |
833 | //--------------------------------------------------------------------------------------------------------- |
834 | |
835 | // Perform our own check with RtlVerifyVersionInfo() instead of using functions from <VersionHelpers.h> as they |
836 | // require a manifest to be functional for checks above 8.1. See https://github.com/ocornut/imgui/issues/4200 |
837 | static BOOL _IsWindowsVersionOrGreater(WORD major, WORD minor, WORD) |
838 | { |
839 | typedef LONG(WINAPI* PFN_RtlVerifyVersionInfo)(OSVERSIONINFOEXW*, ULONG, ULONGLONG); |
840 | static PFN_RtlVerifyVersionInfo RtlVerifyVersionInfoFn = nullptr; |
841 | if (RtlVerifyVersionInfoFn == nullptr) |
842 | if (HMODULE ntdllModule = ::GetModuleHandleA("ntdll.dll" )) |
843 | RtlVerifyVersionInfoFn = (PFN_RtlVerifyVersionInfo)GetProcAddress(ntdllModule, "RtlVerifyVersionInfo" ); |
844 | if (RtlVerifyVersionInfoFn == nullptr) |
845 | return FALSE; |
846 | |
847 | RTL_OSVERSIONINFOEXW versionInfo = { }; |
848 | ULONGLONG conditionMask = 0; |
849 | versionInfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW); |
850 | versionInfo.dwMajorVersion = major; |
851 | versionInfo.dwMinorVersion = minor; |
852 | VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL); |
853 | VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL); |
854 | return (RtlVerifyVersionInfoFn(&versionInfo, VER_MAJORVERSION | VER_MINORVERSION, conditionMask) == 0) ? TRUE : FALSE; |
855 | } |
856 | |
857 | #define _IsWindowsVistaOrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0600), LOBYTE(0x0600), 0) // _WIN32_WINNT_VISTA |
858 | #define _IsWindows8OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0602), LOBYTE(0x0602), 0) // _WIN32_WINNT_WIN8 |
859 | #define _IsWindows8Point1OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0603), LOBYTE(0x0603), 0) // _WIN32_WINNT_WINBLUE |
860 | #define _IsWindows10OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0A00), LOBYTE(0x0A00), 0) // _WIN32_WINNT_WINTHRESHOLD / _WIN32_WINNT_WIN10 |
861 | |
862 | #ifndef DPI_ENUMS_DECLARED |
863 | typedef enum { PROCESS_DPI_UNAWARE = 0, PROCESS_SYSTEM_DPI_AWARE = 1, PROCESS_PER_MONITOR_DPI_AWARE = 2 } PROCESS_DPI_AWARENESS; |
864 | typedef enum { MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2, MDT_DEFAULT = MDT_EFFECTIVE_DPI } MONITOR_DPI_TYPE; |
865 | #endif |
866 | #ifndef _DPI_AWARENESS_CONTEXTS_ |
867 | DECLARE_HANDLE(DPI_AWARENESS_CONTEXT); |
868 | #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE (DPI_AWARENESS_CONTEXT)-3 |
869 | #endif |
870 | #ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 |
871 | #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (DPI_AWARENESS_CONTEXT)-4 |
872 | #endif |
873 | typedef HRESULT(WINAPI* PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS); // Shcore.lib + dll, Windows 8.1+ |
874 | typedef HRESULT(WINAPI* PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); // Shcore.lib + dll, Windows 8.1+ |
875 | typedef DPI_AWARENESS_CONTEXT(WINAPI* PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); // User32.lib + dll, Windows 10 v1607+ (Creators Update) |
876 | |
877 | // Helper function to enable DPI awareness without setting up a manifest |
878 | void ImGui_ImplWin32_EnableDpiAwareness() |
879 | { |
880 | // Make sure monitors will be updated with latest correct scaling |
881 | if (ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData()) |
882 | bd->WantUpdateMonitors = true; |
883 | |
884 | if (_IsWindows10OrGreater()) |
885 | { |
886 | static HINSTANCE user32_dll = ::LoadLibraryA("user32.dll" ); // Reference counted per-process |
887 | if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "SetThreadDpiAwarenessContext" )) |
888 | { |
889 | SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); |
890 | return; |
891 | } |
892 | } |
893 | if (_IsWindows8Point1OrGreater()) |
894 | { |
895 | static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll" ); // Reference counted per-process |
896 | if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, "SetProcessDpiAwareness" )) |
897 | { |
898 | SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE); |
899 | return; |
900 | } |
901 | } |
902 | #if _WIN32_WINNT >= 0x0600 |
903 | ::SetProcessDPIAware(); |
904 | #endif |
905 | } |
906 | |
907 | #if defined(_MSC_VER) && !defined(NOGDI) |
908 | #pragma comment(lib, "gdi32") // Link with gdi32.lib for GetDeviceCaps(). MinGW will require linking with '-lgdi32' |
909 | #endif |
910 | |
911 | float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor) |
912 | { |
913 | UINT xdpi = 96, ydpi = 96; |
914 | if (_IsWindows8Point1OrGreater()) |
915 | { |
916 | static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll" ); // Reference counted per-process |
917 | static PFN_GetDpiForMonitor GetDpiForMonitorFn = nullptr; |
918 | if (GetDpiForMonitorFn == nullptr && shcore_dll != nullptr) |
919 | GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, "GetDpiForMonitor" ); |
920 | if (GetDpiForMonitorFn != nullptr) |
921 | { |
922 | GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi); |
923 | IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert! |
924 | return xdpi / 96.0f; |
925 | } |
926 | } |
927 | #ifndef NOGDI |
928 | const HDC dc = ::GetDC(nullptr); |
929 | xdpi = ::GetDeviceCaps(dc, LOGPIXELSX); |
930 | ydpi = ::GetDeviceCaps(dc, LOGPIXELSY); |
931 | IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert! |
932 | ::ReleaseDC(nullptr, dc); |
933 | #endif |
934 | return xdpi / 96.0f; |
935 | } |
936 | |
937 | float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd) |
938 | { |
939 | HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST); |
940 | return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor); |
941 | } |
942 | |
943 | //--------------------------------------------------------------------------------------------------------- |
944 | // Transparency related helpers (optional) |
945 | //-------------------------------------------------------------------------------------------------------- |
946 | |
947 | #if defined(_MSC_VER) |
948 | #pragma comment(lib, "dwmapi") // Link with dwmapi.lib. MinGW will require linking with '-ldwmapi' |
949 | #endif |
950 | |
951 | // [experimental] |
952 | // Borrowed from GLFW's function updateFramebufferTransparency() in src/win32_window.c |
953 | // (the Dwm* functions are Vista era functions but we are borrowing logic from GLFW) |
954 | void ImGui_ImplWin32_EnableAlphaCompositing(void* hwnd) |
955 | { |
956 | if (!_IsWindowsVistaOrGreater()) |
957 | return; |
958 | |
959 | BOOL composition; |
960 | if (FAILED(::DwmIsCompositionEnabled(&composition)) || !composition) |
961 | return; |
962 | |
963 | BOOL opaque; |
964 | DWORD color; |
965 | if (_IsWindows8OrGreater() || (SUCCEEDED(::DwmGetColorizationColor(&color, &opaque)) && !opaque)) |
966 | { |
967 | HRGN region = ::CreateRectRgn(0, 0, -1, -1); |
968 | DWM_BLURBEHIND bb = {}; |
969 | bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION; |
970 | bb.hRgnBlur = region; |
971 | bb.fEnable = TRUE; |
972 | ::DwmEnableBlurBehindWindow((HWND)hwnd, &bb); |
973 | ::DeleteObject(region); |
974 | } |
975 | else |
976 | { |
977 | DWM_BLURBEHIND bb = {}; |
978 | bb.dwFlags = DWM_BB_ENABLE; |
979 | ::DwmEnableBlurBehindWindow((HWND)hwnd, &bb); |
980 | } |
981 | } |
982 | |
983 | //--------------------------------------------------------------------------------------------------------- |
984 | // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT |
985 | // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously. |
986 | // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first.. |
987 | //-------------------------------------------------------------------------------------------------------- |
988 | |
989 | // Helper structure we store in the void* RendererUserData field of each ImGuiViewport to easily retrieve our backend data. |
990 | struct ImGui_ImplWin32_ViewportData |
991 | { |
992 | HWND Hwnd; |
993 | HWND HwndParent; |
994 | bool HwndOwned; |
995 | DWORD DwStyle; |
996 | DWORD DwExStyle; |
997 | |
998 | ImGui_ImplWin32_ViewportData() { Hwnd = HwndParent = nullptr; HwndOwned = false; DwStyle = DwExStyle = 0; } |
999 | ~ImGui_ImplWin32_ViewportData() { IM_ASSERT(Hwnd == nullptr); } |
1000 | }; |
1001 | |
1002 | static void ImGui_ImplWin32_GetWin32StyleFromViewportFlags(ImGuiViewportFlags flags, DWORD* out_style, DWORD* out_ex_style) |
1003 | { |
1004 | if (flags & ImGuiViewportFlags_NoDecoration) |
1005 | *out_style = WS_POPUP; |
1006 | else |
1007 | *out_style = WS_OVERLAPPEDWINDOW; |
1008 | |
1009 | if (flags & ImGuiViewportFlags_NoTaskBarIcon) |
1010 | *out_ex_style = WS_EX_TOOLWINDOW; |
1011 | else |
1012 | *out_ex_style = WS_EX_APPWINDOW; |
1013 | |
1014 | if (flags & ImGuiViewportFlags_TopMost) |
1015 | *out_ex_style |= WS_EX_TOPMOST; |
1016 | } |
1017 | |
1018 | static HWND ImGui_ImplWin32_GetHwndFromViewportID(ImGuiID viewport_id) |
1019 | { |
1020 | if (viewport_id != 0) |
1021 | if (ImGuiViewport* viewport = ImGui::FindViewportByID(viewport_id)) |
1022 | return (HWND)viewport->PlatformHandle; |
1023 | return nullptr; |
1024 | } |
1025 | |
1026 | static void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport) |
1027 | { |
1028 | ImGui_ImplWin32_ViewportData* vd = IM_NEW(ImGui_ImplWin32_ViewportData)(); |
1029 | viewport->PlatformUserData = vd; |
1030 | |
1031 | // Select style and parent window |
1032 | ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &vd->DwStyle, &vd->DwExStyle); |
1033 | vd->HwndParent = ImGui_ImplWin32_GetHwndFromViewportID(viewport->ParentViewportId); |
1034 | |
1035 | // Create window |
1036 | RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) }; |
1037 | ::AdjustWindowRectEx(&rect, vd->DwStyle, FALSE, vd->DwExStyle); |
1038 | vd->Hwnd = ::CreateWindowEx( |
1039 | vd->DwExStyle, _T("ImGui Platform" ), _T("Untitled" ), vd->DwStyle, // Style, class name, window name |
1040 | rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, // Window area |
1041 | vd->HwndParent, nullptr, ::GetModuleHandle(nullptr), nullptr); // Owner window, Menu, Instance, Param |
1042 | vd->HwndOwned = true; |
1043 | viewport->PlatformRequestResize = false; |
1044 | viewport->PlatformHandle = viewport->PlatformHandleRaw = vd->Hwnd; |
1045 | } |
1046 | |
1047 | static void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport) |
1048 | { |
1049 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
1050 | if (ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData) |
1051 | { |
1052 | if (::GetCapture() == vd->Hwnd) |
1053 | { |
1054 | // Transfer capture so if we started dragging from a window that later disappears, we'll still receive the MOUSEUP event. |
1055 | ::ReleaseCapture(); |
1056 | ::SetCapture(bd->hWnd); |
1057 | } |
1058 | if (vd->Hwnd && vd->HwndOwned) |
1059 | ::DestroyWindow(vd->Hwnd); |
1060 | vd->Hwnd = nullptr; |
1061 | IM_DELETE(p: vd); |
1062 | } |
1063 | viewport->PlatformUserData = viewport->PlatformHandle = nullptr; |
1064 | } |
1065 | |
1066 | static void ImGui_ImplWin32_ShowWindow(ImGuiViewport* viewport) |
1067 | { |
1068 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1069 | IM_ASSERT(vd->Hwnd != 0); |
1070 | |
1071 | // ShowParent() also brings parent to front, which is not always desirable, |
1072 | // so we temporarily disable parenting. (#7354) |
1073 | if (vd->HwndParent != NULL) |
1074 | ::SetWindowLongPtr(vd->Hwnd, GWLP_HWNDPARENT, (LONG_PTR)nullptr); |
1075 | |
1076 | if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) |
1077 | ::ShowWindow(vd->Hwnd, SW_SHOWNA); |
1078 | else |
1079 | ::ShowWindow(vd->Hwnd, SW_SHOW); |
1080 | |
1081 | // Restore |
1082 | if (vd->HwndParent != NULL) |
1083 | ::SetWindowLongPtr(vd->Hwnd, GWLP_HWNDPARENT, (LONG_PTR)vd->HwndParent); |
1084 | } |
1085 | |
1086 | static void ImGui_ImplWin32_UpdateWindow(ImGuiViewport* viewport) |
1087 | { |
1088 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1089 | IM_ASSERT(vd->Hwnd != 0); |
1090 | |
1091 | // Update Win32 parent if it changed _after_ creation |
1092 | // Unlike style settings derived from configuration flags, this is more likely to change for advanced apps that are manipulating ParentViewportID manually. |
1093 | HWND new_parent = ImGui_ImplWin32_GetHwndFromViewportID(viewport->ParentViewportId); |
1094 | if (new_parent != vd->HwndParent) |
1095 | { |
1096 | // Win32 windows can either have a "Parent" (for WS_CHILD window) or an "Owner" (which among other thing keeps window above its owner). |
1097 | // Our Dear Imgui-side concept of parenting only mostly care about what Win32 call "Owner". |
1098 | // The parent parameter of CreateWindowEx() sets up Parent OR Owner depending on WS_CHILD flag. In our case an Owner as we never use WS_CHILD. |
1099 | // Calling ::SetParent() here would be incorrect: it will create a full child relation, alter coordinate system and clipping. |
1100 | // Calling ::SetWindowLongPtr() with GWLP_HWNDPARENT seems correct although poorly documented. |
1101 | // https://devblogs.microsoft.com/oldnewthing/20100315-00/?p=14613 |
1102 | vd->HwndParent = new_parent; |
1103 | ::SetWindowLongPtr(vd->Hwnd, GWLP_HWNDPARENT, (LONG_PTR)vd->HwndParent); |
1104 | } |
1105 | |
1106 | // (Optional) Update Win32 style if it changed _after_ creation. |
1107 | // Generally they won't change unless configuration flags are changed, but advanced uses (such as manually rewriting viewport flags) make this useful. |
1108 | DWORD new_style; |
1109 | DWORD new_ex_style; |
1110 | ImGui_ImplWin32_GetWin32StyleFromViewportFlags(flags: viewport->Flags, out_style: &new_style, out_ex_style: &new_ex_style); |
1111 | |
1112 | // Only reapply the flags that have been changed from our point of view (as other flags are being modified by Windows) |
1113 | if (vd->DwStyle != new_style || vd->DwExStyle != new_ex_style) |
1114 | { |
1115 | // (Optional) Update TopMost state if it changed _after_ creation |
1116 | bool top_most_changed = (vd->DwExStyle & WS_EX_TOPMOST) != (new_ex_style & WS_EX_TOPMOST); |
1117 | HWND insert_after = top_most_changed ? ((viewport->Flags & ImGuiViewportFlags_TopMost) ? HWND_TOPMOST : HWND_NOTOPMOST) : 0; |
1118 | UINT swp_flag = top_most_changed ? 0 : SWP_NOZORDER; |
1119 | |
1120 | // Apply flags and position (since it is affected by flags) |
1121 | vd->DwStyle = new_style; |
1122 | vd->DwExStyle = new_ex_style; |
1123 | ::SetWindowLong(vd->Hwnd, GWL_STYLE, vd->DwStyle); |
1124 | ::SetWindowLong(vd->Hwnd, GWL_EXSTYLE, vd->DwExStyle); |
1125 | RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) }; |
1126 | ::AdjustWindowRectEx(&rect, vd->DwStyle, FALSE, vd->DwExStyle); // Client to Screen |
1127 | ::SetWindowPos(vd->Hwnd, insert_after, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, swp_flag | SWP_NOACTIVATE | SWP_FRAMECHANGED); |
1128 | ::ShowWindow(vd->Hwnd, SW_SHOWNA); // This is necessary when we alter the style |
1129 | viewport->PlatformRequestMove = viewport->PlatformRequestResize = true; |
1130 | } |
1131 | } |
1132 | |
1133 | static ImVec2 ImGui_ImplWin32_GetWindowPos(ImGuiViewport* viewport) |
1134 | { |
1135 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1136 | IM_ASSERT(vd->Hwnd != 0); |
1137 | POINT pos = { 0, 0 }; |
1138 | ::ClientToScreen(vd->Hwnd, &pos); |
1139 | return ImVec2((float)pos.x, (float)pos.y); |
1140 | } |
1141 | |
1142 | static void ImGui_ImplWin32_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos) |
1143 | { |
1144 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1145 | IM_ASSERT(vd->Hwnd != 0); |
1146 | RECT rect = { (LONG)pos.x, (LONG)pos.y, (LONG)pos.x, (LONG)pos.y }; |
1147 | ::AdjustWindowRectEx(&rect, vd->DwStyle, FALSE, vd->DwExStyle); |
1148 | ::SetWindowPos(vd->Hwnd, nullptr, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE); |
1149 | } |
1150 | |
1151 | static ImVec2 ImGui_ImplWin32_GetWindowSize(ImGuiViewport* viewport) |
1152 | { |
1153 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1154 | IM_ASSERT(vd->Hwnd != 0); |
1155 | RECT rect; |
1156 | ::GetClientRect(vd->Hwnd, &rect); |
1157 | return ImVec2(float(rect.right - rect.left), float(rect.bottom - rect.top)); |
1158 | } |
1159 | |
1160 | static void ImGui_ImplWin32_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) |
1161 | { |
1162 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1163 | IM_ASSERT(vd->Hwnd != 0); |
1164 | RECT rect = { 0, 0, (LONG)size.x, (LONG)size.y }; |
1165 | ::AdjustWindowRectEx(&rect, vd->DwStyle, FALSE, vd->DwExStyle); // Client to Screen |
1166 | ::SetWindowPos(vd->Hwnd, nullptr, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE); |
1167 | } |
1168 | |
1169 | static void ImGui_ImplWin32_SetWindowFocus(ImGuiViewport* viewport) |
1170 | { |
1171 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1172 | IM_ASSERT(vd->Hwnd != 0); |
1173 | ::BringWindowToTop(vd->Hwnd); |
1174 | ::SetForegroundWindow(vd->Hwnd); |
1175 | ::SetFocus(vd->Hwnd); |
1176 | } |
1177 | |
1178 | static bool ImGui_ImplWin32_GetWindowFocus(ImGuiViewport* viewport) |
1179 | { |
1180 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1181 | IM_ASSERT(vd->Hwnd != 0); |
1182 | return ::GetForegroundWindow() == vd->Hwnd; |
1183 | } |
1184 | |
1185 | static bool ImGui_ImplWin32_GetWindowMinimized(ImGuiViewport* viewport) |
1186 | { |
1187 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1188 | IM_ASSERT(vd->Hwnd != 0); |
1189 | return ::IsIconic(vd->Hwnd) != 0; |
1190 | } |
1191 | |
1192 | static void ImGui_ImplWin32_SetWindowTitle(ImGuiViewport* viewport, const char* title) |
1193 | { |
1194 | // ::SetWindowTextA() doesn't properly handle UTF-8 so we explicitely convert our string. |
1195 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1196 | IM_ASSERT(vd->Hwnd != 0); |
1197 | int n = ::MultiByteToWideChar(CP_UTF8, 0, title, -1, nullptr, 0); |
1198 | ImVector<wchar_t> title_w; |
1199 | title_w.resize(new_size: n); |
1200 | ::MultiByteToWideChar(CP_UTF8, 0, title, -1, title_w.Data, n); |
1201 | ::SetWindowTextW(vd->Hwnd, title_w.Data); |
1202 | } |
1203 | |
1204 | static void ImGui_ImplWin32_SetWindowAlpha(ImGuiViewport* viewport, float alpha) |
1205 | { |
1206 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1207 | IM_ASSERT(vd->Hwnd != 0); |
1208 | IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f); |
1209 | if (alpha < 1.0f) |
1210 | { |
1211 | DWORD style = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE) | WS_EX_LAYERED; |
1212 | ::SetWindowLongW(vd->Hwnd, GWL_EXSTYLE, style); |
1213 | ::SetLayeredWindowAttributes(vd->Hwnd, 0, (BYTE)(255 * alpha), LWA_ALPHA); |
1214 | } |
1215 | else |
1216 | { |
1217 | DWORD style = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED; |
1218 | ::SetWindowLongW(vd->Hwnd, GWL_EXSTYLE, style); |
1219 | } |
1220 | } |
1221 | |
1222 | static float ImGui_ImplWin32_GetWindowDpiScale(ImGuiViewport* viewport) |
1223 | { |
1224 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
1225 | IM_ASSERT(vd->Hwnd != 0); |
1226 | return ImGui_ImplWin32_GetDpiScaleForHwnd(vd->Hwnd); |
1227 | } |
1228 | |
1229 | // FIXME-DPI: Testing DPI related ideas |
1230 | static void ImGui_ImplWin32_OnChangedViewport(ImGuiViewport* viewport) |
1231 | { |
1232 | (void)viewport; |
1233 | #if 0 |
1234 | ImGuiStyle default_style; |
1235 | //default_style.WindowPadding = ImVec2(0, 0); |
1236 | //default_style.WindowBorderSize = 0.0f; |
1237 | //default_style.ItemSpacing.y = 3.0f; |
1238 | //default_style.FramePadding = ImVec2(0, 0); |
1239 | default_style.ScaleAllSizes(viewport->DpiScale); |
1240 | ImGuiStyle& style = ImGui::GetStyle(); |
1241 | style = default_style; |
1242 | #endif |
1243 | } |
1244 | |
1245 | static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) |
1246 | { |
1247 | if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) |
1248 | return true; |
1249 | |
1250 | if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hWnd)) |
1251 | { |
1252 | switch (msg) |
1253 | { |
1254 | case WM_CLOSE: |
1255 | viewport->PlatformRequestClose = true; |
1256 | return 0; |
1257 | case WM_MOVE: |
1258 | viewport->PlatformRequestMove = true; |
1259 | break; |
1260 | case WM_SIZE: |
1261 | viewport->PlatformRequestResize = true; |
1262 | break; |
1263 | case WM_MOUSEACTIVATE: |
1264 | if (viewport->Flags & ImGuiViewportFlags_NoFocusOnClick) |
1265 | return MA_NOACTIVATE; |
1266 | break; |
1267 | case WM_NCHITTEST: |
1268 | // Let mouse pass-through the window. This will allow the backend to call io.AddMouseViewportEvent() correctly. (which is optional). |
1269 | // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging. |
1270 | // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in |
1271 | // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system. |
1272 | if (viewport->Flags & ImGuiViewportFlags_NoInputs) |
1273 | return HTTRANSPARENT; |
1274 | break; |
1275 | } |
1276 | } |
1277 | |
1278 | return DefWindowProc(hWnd, msg, wParam, lParam); |
1279 | } |
1280 | |
1281 | static void ImGui_ImplWin32_InitPlatformInterface(bool platform_has_own_dc) |
1282 | { |
1283 | WNDCLASSEX wcex; |
1284 | wcex.cbSize = sizeof(WNDCLASSEX); |
1285 | wcex.style = CS_HREDRAW | CS_VREDRAW | (platform_has_own_dc ? CS_OWNDC : 0); |
1286 | wcex.lpfnWndProc = ImGui_ImplWin32_WndProcHandler_PlatformWindow; |
1287 | wcex.cbClsExtra = 0; |
1288 | wcex.cbWndExtra = 0; |
1289 | wcex.hInstance = ::GetModuleHandle(nullptr); |
1290 | wcex.hIcon = nullptr; |
1291 | wcex.hCursor = nullptr; |
1292 | wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1); |
1293 | wcex.lpszMenuName = nullptr; |
1294 | wcex.lpszClassName = _T("ImGui Platform" ); |
1295 | wcex.hIconSm = nullptr; |
1296 | ::RegisterClassEx(&wcex); |
1297 | |
1298 | ImGui_ImplWin32_UpdateMonitors(); |
1299 | |
1300 | // Register platform interface (will be coupled with a renderer interface) |
1301 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
1302 | platform_io.Platform_CreateWindow = ImGui_ImplWin32_CreateWindow; |
1303 | platform_io.Platform_DestroyWindow = ImGui_ImplWin32_DestroyWindow; |
1304 | platform_io.Platform_ShowWindow = ImGui_ImplWin32_ShowWindow; |
1305 | platform_io.Platform_SetWindowPos = ImGui_ImplWin32_SetWindowPos; |
1306 | platform_io.Platform_GetWindowPos = ImGui_ImplWin32_GetWindowPos; |
1307 | platform_io.Platform_SetWindowSize = ImGui_ImplWin32_SetWindowSize; |
1308 | platform_io.Platform_GetWindowSize = ImGui_ImplWin32_GetWindowSize; |
1309 | platform_io.Platform_SetWindowFocus = ImGui_ImplWin32_SetWindowFocus; |
1310 | platform_io.Platform_GetWindowFocus = ImGui_ImplWin32_GetWindowFocus; |
1311 | platform_io.Platform_GetWindowMinimized = ImGui_ImplWin32_GetWindowMinimized; |
1312 | platform_io.Platform_SetWindowTitle = ImGui_ImplWin32_SetWindowTitle; |
1313 | platform_io.Platform_SetWindowAlpha = ImGui_ImplWin32_SetWindowAlpha; |
1314 | platform_io.Platform_UpdateWindow = ImGui_ImplWin32_UpdateWindow; |
1315 | platform_io.Platform_GetWindowDpiScale = ImGui_ImplWin32_GetWindowDpiScale; // FIXME-DPI |
1316 | platform_io.Platform_OnChangedViewport = ImGui_ImplWin32_OnChangedViewport; // FIXME-DPI |
1317 | |
1318 | // Register main window handle (which is owned by the main application, not by us) |
1319 | // This is mostly for simplicity and consistency, so that our code (e.g. mouse handling etc.) can use same logic for main and secondary viewports. |
1320 | ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
1321 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
1322 | ImGui_ImplWin32_ViewportData* vd = IM_NEW(ImGui_ImplWin32_ViewportData)(); |
1323 | vd->Hwnd = bd->hWnd; |
1324 | vd->HwndOwned = false; |
1325 | main_viewport->PlatformUserData = vd; |
1326 | main_viewport->PlatformHandle = (void*)bd->hWnd; |
1327 | } |
1328 | |
1329 | static void ImGui_ImplWin32_ShutdownPlatformInterface() |
1330 | { |
1331 | ::UnregisterClass(_T("ImGui Platform" ), ::GetModuleHandle(nullptr)); |
1332 | ImGui::DestroyPlatformWindows(); |
1333 | } |
1334 | |
1335 | //--------------------------------------------------------------------------------------------------------- |
1336 | |
1337 | #endif // #ifndef IMGUI_DISABLE |
1338 | |