| 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 | #include "imgui.h" |
| 97 | #ifndef IMGUI_DISABLE |
| 98 | #include "imgui_impl_win32.h" |
| 99 | #ifndef WIN32_LEAN_AND_MEAN |
| 100 | #define WIN32_LEAN_AND_MEAN |
| 101 | #endif |
| 102 | #include <windows.h> |
| 103 | #include <windowsx.h> |
| 104 | #include <tchar.h> |
| 105 | #include <dwmapi.h> |
| 106 | |
| 107 | |
| 108 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
| 109 | #include <xinput.h> |
| 110 | typedef DWORD(WINAPI* PFN_XInputGetCapabilities)(DWORD, DWORD, XINPUT_CAPABILITIES*); |
| 111 | typedef DWORD(WINAPI* PFN_XInputGetState)(DWORD, XINPUT_STATE*); |
| 112 | #endif |
| 113 | typedef BOOL(WINAPI* PFN_AdjustWindowRectExForDpi)(LPRECT, DWORD, BOOL, DWORD, UINT); |
| 114 | |
| 115 | |
| 116 | #if defined(__clang__) |
| 117 | #pragma clang diagnostic push |
| 118 | #pragma clang diagnostic ignored "-Wcast-function-type" |
| 119 | #endif |
| 120 | #if defined(__GNUC__) |
| 121 | #pragma GCC diagnostic push |
| 122 | #pragma GCC diagnostic ignored "-Wpragmas" |
| 123 | #pragma GCC diagnostic ignored "-Wcast-function-type" |
| 124 | #endif |
| 125 | |
| 126 | |
| 127 | static void ImGui_ImplWin32_InitMultiViewportSupport(bool platform_has_own_dc); |
| 128 | static void ImGui_ImplWin32_ShutdownMultiViewportSupport(); |
| 129 | static void ImGui_ImplWin32_UpdateMonitors(); |
| 130 | static bool ImGui_ImplWin32_IsDpiAwarenessPerMonitorAwareV2(); |
| 131 | |
| 132 | struct ImGui_ImplWin32_Data |
| 133 | { |
| 134 | HWND hWnd; |
| 135 | HWND MouseHwnd; |
| 136 | int MouseTrackedArea; |
| 137 | int MouseButtonsDown; |
| 138 | INT64 Time; |
| 139 | INT64 TicksPerSecond; |
| 140 | ImGuiMouseCursor LastMouseCursor; |
| 141 | UINT32 KeyboardCodePage; |
| 142 | bool WantUpdateMonitors; |
| 143 | bool IsPerMonitorDpiAwareV2; |
| 144 | PFN_AdjustWindowRectExForDpi AdjustWindowRectExForDpi; |
| 145 | |
| 146 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
| 147 | bool HasGamepad; |
| 148 | bool WantUpdateHasGamepad; |
| 149 | HMODULE XInputDLL; |
| 150 | PFN_XInputGetCapabilities XInputGetCapabilities; |
| 151 | PFN_XInputGetState XInputGetState; |
| 152 | DWORD XInputPacketNumber; |
| 153 | #endif |
| 154 | |
| 155 | ImGui_ImplWin32_Data() { memset((void*)this, 0, sizeof(*this)); } |
| 156 | }; |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | static ImGui_ImplWin32_Data* ImGui_ImplWin32_GetBackendData() |
| 163 | { |
| 164 | return ImGui::GetCurrentContext() ? (ImGui_ImplWin32_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr; |
| 165 | } |
| 166 | static ImGui_ImplWin32_Data* ImGui_ImplWin32_GetBackendData(ImGuiIO& io) |
| 167 | { |
| 168 | return (ImGui_ImplWin32_Data*)io.BackendPlatformUserData; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | static void ImGui_ImplWin32_UpdateKeyboardCodePage(ImGuiIO& io) |
| 173 | { |
| 174 | |
| 175 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io); |
| 176 | HKL keyboard_layout = ::GetKeyboardLayout(0); |
| 177 | LCID keyboard_lcid = MAKELCID(HIWORD(keyboard_layout), SORT_DEFAULT); |
| 178 | if (::GetLocaleInfoA(keyboard_lcid, (LOCALE_RETURN_NUMBER | LOCALE_IDEFAULTANSICODEPAGE), (LPSTR)&bd->KeyboardCodePage, sizeof(bd->KeyboardCodePage)) == 0) |
| 179 | bd->KeyboardCodePage = CP_ACP; |
| 180 | } |
| 181 | |
| 182 | static bool ImGui_ImplWin32_InitEx(void* hwnd, bool platform_has_own_dc) |
| 183 | { |
| 184 | ImGuiIO& io = ImGui::GetIO(); |
| 185 | IMGUI_CHECKVERSION(); |
| 186 | IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!"); |
| 187 | |
| 188 | INT64 perf_frequency, perf_counter; |
| 189 | if (!::QueryPerformanceFrequency((LARGE_INTEGER*)&perf_frequency)) |
| 190 | return false; |
| 191 | if (!::QueryPerformanceCounter((LARGE_INTEGER*)&perf_counter)) |
| 192 | return false; |
| 193 | |
| 194 | |
| 195 | ImGui_ImplWin32_Data* bd = IM_NEW(ImGui_ImplWin32_Data)(); |
| 196 | io.BackendPlatformUserData = (void*)bd; |
| 197 | io.BackendPlatformName = "imgui_impl_win32"; |
| 198 | io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; |
| 199 | io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; |
| 200 | io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; |
| 201 | io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; |
| 202 | io.BackendFlags |= ImGuiBackendFlags_HasParentViewport; |
| 203 | |
| 204 | bd->hWnd = (HWND)hwnd; |
| 205 | bd->TicksPerSecond = perf_frequency; |
| 206 | bd->Time = perf_counter; |
| 207 | bd->LastMouseCursor = ImGuiMouseCursor_COUNT; |
| 208 | ImGui_ImplWin32_UpdateKeyboardCodePage(io); |
| 209 | |
| 210 | |
| 211 | ImGui_ImplWin32_UpdateMonitors(); |
| 212 | |
| 213 | |
| 214 | ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
| 215 | main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (void*)bd->hWnd; |
| 216 | |
| 217 | |
| 218 | |
| 219 | ::SetPropA(bd->hWnd, "IMGUI_CONTEXT", ImGui::GetCurrentContext()); |
| 220 | ImGui_ImplWin32_InitMultiViewportSupport(platform_has_own_dc); |
| 221 | |
| 222 | if (HINSTANCE user32_dll = ::GetModuleHandleA("user32.dll")) |
| 223 | bd->AdjustWindowRectExForDpi = (PFN_AdjustWindowRectExForDpi)::GetProcAddress(user32_dll, "AdjustWindowRectExForDpi"); |
| 224 | bd->IsPerMonitorDpiAwareV2 = ImGui_ImplWin32_IsDpiAwarenessPerMonitorAwareV2(); |
| 225 | |
| 226 | |
| 227 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
| 228 | bd->WantUpdateHasGamepad = true; |
| 229 | const char* xinput_dll_names[] = |
| 230 | { |
| 231 | "xinput1_4.dll", |
| 232 | "xinput1_3.dll", |
| 233 | "xinput9_1_0.dll", |
| 234 | "xinput1_2.dll", |
| 235 | "xinput1_1.dll" |
| 236 | }; |
| 237 | for (int n = 0; n < IM_COUNTOF(xinput_dll_names); n++) |
| 238 | if (HMODULE dll = ::LoadLibraryA(xinput_dll_names[n])) |
| 239 | { |
| 240 | bd->XInputDLL = dll; |
| 241 | bd->XInputGetCapabilities = (PFN_XInputGetCapabilities)::GetProcAddress(dll, "XInputGetCapabilities"); |
| 242 | bd->XInputGetState = (PFN_XInputGetState)::GetProcAddress(dll, "XInputGetState"); |
| 243 | break; |
| 244 | } |
| 245 | #endif |
| 246 | |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | IMGUI_IMPL_API bool ImGui_ImplWin32_Init(void* hwnd) |
| 251 | { |
| 252 | return ImGui_ImplWin32_InitEx(hwnd, false); |
| 253 | } |
| 254 | |
| 255 | IMGUI_IMPL_API bool ImGui_ImplWin32_InitForOpenGL(void* hwnd) |
| 256 | { |
| 257 | |
| 258 | return ImGui_ImplWin32_InitEx(hwnd, true); |
| 259 | } |
| 260 | |
| 261 | void ImGui_ImplWin32_Shutdown() |
| 262 | { |
| 263 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
| 264 | IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?"); |
| 265 | ImGuiIO& io = ImGui::GetIO(); |
| 266 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 267 | |
| 268 | ::SetPropA(bd->hWnd, "IMGUI_CONTEXT", nullptr); |
| 269 | ImGui_ImplWin32_ShutdownMultiViewportSupport(); |
| 270 | |
| 271 | |
| 272 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
| 273 | if (bd->XInputDLL) |
| 274 | ::FreeLibrary(bd->XInputDLL); |
| 275 | #endif |
| 276 | |
| 277 | io.BackendPlatformName = nullptr; |
| 278 | io.BackendPlatformUserData = nullptr; |
| 279 | io.BackendFlags &= ~(ImGuiBackendFlags_HasMouseCursors | ImGuiBackendFlags_HasSetMousePos | ImGuiBackendFlags_HasGamepad | ImGuiBackendFlags_PlatformHasViewports | ImGuiBackendFlags_HasMouseHoveredViewport | ImGuiBackendFlags_HasParentViewport); |
| 280 | platform_io.ClearPlatformHandlers(); |
| 281 | IM_DELETE(bd); |
| 282 | } |
| 283 | |
| 284 | static bool ImGui_ImplWin32_UpdateMouseCursor(ImGuiIO& io, ImGuiMouseCursor imgui_cursor) |
| 285 | { |
| 286 | if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) |
| 287 | return false; |
| 288 | |
| 289 | if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor) |
| 290 | { |
| 291 | |
| 292 | ::SetCursor(nullptr); |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | |
| 297 | LPTSTR win32_cursor = IDC_ARROW; |
| 298 | switch (imgui_cursor) |
| 299 | { |
| 300 | case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break; |
| 301 | case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break; |
| 302 | case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break; |
| 303 | case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break; |
| 304 | case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break; |
| 305 | case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break; |
| 306 | case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break; |
| 307 | case ImGuiMouseCursor_Hand: win32_cursor = IDC_HAND; break; |
| 308 | case ImGuiMouseCursor_Wait: win32_cursor = IDC_WAIT; break; |
| 309 | case ImGuiMouseCursor_Progress: win32_cursor = IDC_APPSTARTING; break; |
| 310 | case ImGuiMouseCursor_NotAllowed: win32_cursor = IDC_NO; break; |
| 311 | } |
| 312 | ::SetCursor(::LoadCursor(nullptr, win32_cursor)); |
| 313 | } |
| 314 | return true; |
| 315 | } |
| 316 | |
| 317 | static bool IsVkDown(int vk) |
| 318 | { |
| 319 | return (::GetKeyState(vk) & 0x8000) != 0; |
| 320 | } |
| 321 | |
| 322 | static void ImGui_ImplWin32_AddKeyEvent(ImGuiIO& io, ImGuiKey key, bool down, int native_keycode, int native_scancode = -1) |
| 323 | { |
| 324 | io.AddKeyEvent(key, down); |
| 325 | io.SetKeyEventNativeData(key, native_keycode, native_scancode); |
| 326 | IM_UNUSED(native_scancode); |
| 327 | } |
| 328 | |
| 329 | static void ImGui_ImplWin32_ProcessKeyEventsWorkarounds(ImGuiIO& io) |
| 330 | { |
| 331 | |
| 332 | if (ImGui::IsKeyDown(ImGuiKey_LeftShift) && !IsVkDown(VK_LSHIFT)) |
| 333 | ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftShift, false, VK_LSHIFT); |
| 334 | if (ImGui::IsKeyDown(ImGuiKey_RightShift) && !IsVkDown(VK_RSHIFT)) |
| 335 | ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightShift, false, VK_RSHIFT); |
| 336 | |
| 337 | |
| 338 | if (ImGui::IsKeyDown(ImGuiKey_LeftSuper) && !IsVkDown(VK_LWIN)) |
| 339 | ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftSuper, false, VK_LWIN); |
| 340 | if (ImGui::IsKeyDown(ImGuiKey_RightSuper) && !IsVkDown(VK_RWIN)) |
| 341 | ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightSuper, false, VK_RWIN); |
| 342 | } |
| 343 | |
| 344 | static void ImGui_ImplWin32_UpdateKeyModifiers(ImGuiIO& io) |
| 345 | { |
| 346 | io.AddKeyEvent(ImGuiMod_Ctrl, IsVkDown(VK_CONTROL)); |
| 347 | io.AddKeyEvent(ImGuiMod_Shift, IsVkDown(VK_SHIFT)); |
| 348 | io.AddKeyEvent(ImGuiMod_Alt, IsVkDown(VK_MENU)); |
| 349 | io.AddKeyEvent(ImGuiMod_Super, IsVkDown(VK_LWIN) || IsVkDown(VK_RWIN)); |
| 350 | } |
| 351 | |
| 352 | static ImGuiViewport* ImGui_ImplWin32_FindViewportByPlatformHandle(ImGuiPlatformIO& platform_io, HWND hwnd) |
| 353 | { |
| 354 | |
| 355 | |
| 356 | |
| 357 | for (ImGuiViewport* viewport : platform_io.Viewports) |
| 358 | if (viewport->PlatformHandle == hwnd) |
| 359 | return viewport; |
| 360 | return nullptr; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | |
| 365 | static void ImGui_ImplWin32_UpdateMouseData(ImGuiIO& io, ImGuiPlatformIO& platform_io) |
| 366 | { |
| 367 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io); |
| 368 | IM_ASSERT(bd->hWnd != 0); |
| 369 | |
| 370 | POINT mouse_screen_pos; |
| 371 | bool has_mouse_screen_pos = ::GetCursorPos(&mouse_screen_pos) != 0; |
| 372 | |
| 373 | HWND focused_window = ::GetForegroundWindow(); |
| 374 | const bool is_app_focused = (focused_window && (focused_window == bd->hWnd || ::IsChild(focused_window, bd->hWnd) || ImGui_ImplWin32_FindViewportByPlatformHandle(platform_io, focused_window))); |
| 375 | if (is_app_focused) |
| 376 | { |
| 377 | |
| 378 | |
| 379 | if (io.WantSetMousePos) |
| 380 | { |
| 381 | POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y }; |
| 382 | if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) == 0) |
| 383 | ::ClientToScreen(focused_window, &pos); |
| 384 | ::SetCursorPos(pos.x, pos.y); |
| 385 | } |
| 386 | |
| 387 | |
| 388 | |
| 389 | if (!io.WantSetMousePos && bd->MouseTrackedArea == 0 && has_mouse_screen_pos) |
| 390 | { |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | POINT mouse_pos = mouse_screen_pos; |
| 396 | if (!(io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)) |
| 397 | ::ScreenToClient(bd->hWnd, &mouse_pos); |
| 398 | io.AddMousePosEvent((float)mouse_pos.x, (float)mouse_pos.y); |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | ImGuiID mouse_viewport_id = 0; |
| 410 | if (has_mouse_screen_pos) |
| 411 | if (HWND hovered_hwnd = ::WindowFromPoint(mouse_screen_pos)) |
| 412 | if (ImGuiViewport* viewport = ImGui_ImplWin32_FindViewportByPlatformHandle(platform_io, hovered_hwnd)) |
| 413 | mouse_viewport_id = viewport->ID; |
| 414 | io.AddMouseViewportEvent(mouse_viewport_id); |
| 415 | } |
| 416 | |
| 417 | |
| 418 | static void ImGui_ImplWin32_UpdateGamepads(ImGuiIO& io) |
| 419 | { |
| 420 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
| 421 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io); |
| 422 | |
| 423 | |
| 424 | |
| 425 | if (bd->WantUpdateHasGamepad) |
| 426 | { |
| 427 | XINPUT_CAPABILITIES caps = {}; |
| 428 | bd->HasGamepad = bd->XInputGetCapabilities ? (bd->XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS) : false; |
| 429 | bd->WantUpdateHasGamepad = false; |
| 430 | } |
| 431 | |
| 432 | io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad; |
| 433 | XINPUT_STATE xinput_state; |
| 434 | XINPUT_GAMEPAD& gamepad = xinput_state.Gamepad; |
| 435 | if (!bd->HasGamepad || bd->XInputGetState == nullptr || bd->XInputGetState(0, &xinput_state) != ERROR_SUCCESS) |
| 436 | return; |
| 437 | io.BackendFlags |= ImGuiBackendFlags_HasGamepad; |
| 438 | if (bd->XInputPacketNumber != 0 && bd->XInputPacketNumber == xinput_state.dwPacketNumber) |
| 439 | return; |
| 440 | bd->XInputPacketNumber = xinput_state.dwPacketNumber; |
| 441 | |
| 442 | #define IM_SATURATE(V) (V < 0.0f ? 0.0f : V > 1.0f ? 1.0f : V) |
| 443 | #define MAP_BUTTON(KEY_NO, BUTTON_ENUM) { io.AddKeyEvent(KEY_NO, (gamepad.wButtons & BUTTON_ENUM) != 0); } |
| 444 | #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)); } |
| 445 | MAP_BUTTON(ImGuiKey_GamepadStart, XINPUT_GAMEPAD_START); |
| 446 | MAP_BUTTON(ImGuiKey_GamepadBack, XINPUT_GAMEPAD_BACK); |
| 447 | MAP_BUTTON(ImGuiKey_GamepadFaceLeft, XINPUT_GAMEPAD_X); |
| 448 | MAP_BUTTON(ImGuiKey_GamepadFaceRight, XINPUT_GAMEPAD_B); |
| 449 | MAP_BUTTON(ImGuiKey_GamepadFaceUp, XINPUT_GAMEPAD_Y); |
| 450 | MAP_BUTTON(ImGuiKey_GamepadFaceDown, XINPUT_GAMEPAD_A); |
| 451 | MAP_BUTTON(ImGuiKey_GamepadDpadLeft, XINPUT_GAMEPAD_DPAD_LEFT); |
| 452 | MAP_BUTTON(ImGuiKey_GamepadDpadRight, XINPUT_GAMEPAD_DPAD_RIGHT); |
| 453 | MAP_BUTTON(ImGuiKey_GamepadDpadUp, XINPUT_GAMEPAD_DPAD_UP); |
| 454 | MAP_BUTTON(ImGuiKey_GamepadDpadDown, XINPUT_GAMEPAD_DPAD_DOWN); |
| 455 | MAP_BUTTON(ImGuiKey_GamepadL1, XINPUT_GAMEPAD_LEFT_SHOULDER); |
| 456 | MAP_BUTTON(ImGuiKey_GamepadR1, XINPUT_GAMEPAD_RIGHT_SHOULDER); |
| 457 | MAP_ANALOG(ImGuiKey_GamepadL2, gamepad.bLeftTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD, 255); |
| 458 | MAP_ANALOG(ImGuiKey_GamepadR2, gamepad.bRightTrigger, XINPUT_GAMEPAD_TRIGGER_THRESHOLD, 255); |
| 459 | MAP_BUTTON(ImGuiKey_GamepadL3, XINPUT_GAMEPAD_LEFT_THUMB); |
| 460 | MAP_BUTTON(ImGuiKey_GamepadR3, XINPUT_GAMEPAD_RIGHT_THUMB); |
| 461 | MAP_ANALOG(ImGuiKey_GamepadLStickLeft, gamepad.sThumbLX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768); |
| 462 | MAP_ANALOG(ImGuiKey_GamepadLStickRight, gamepad.sThumbLX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767); |
| 463 | MAP_ANALOG(ImGuiKey_GamepadLStickUp, gamepad.sThumbLY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767); |
| 464 | MAP_ANALOG(ImGuiKey_GamepadLStickDown, gamepad.sThumbLY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768); |
| 465 | MAP_ANALOG(ImGuiKey_GamepadRStickLeft, gamepad.sThumbRX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768); |
| 466 | MAP_ANALOG(ImGuiKey_GamepadRStickRight, gamepad.sThumbRX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767); |
| 467 | MAP_ANALOG(ImGuiKey_GamepadRStickUp, gamepad.sThumbRY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767); |
| 468 | MAP_ANALOG(ImGuiKey_GamepadRStickDown, gamepad.sThumbRY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768); |
| 469 | #undef MAP_BUTTON |
| 470 | #undef MAP_ANALOG |
| 471 | #else |
| 472 | IM_UNUSED(io); |
| 473 | #endif |
| 474 | } |
| 475 | |
| 476 | static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR monitor, HDC, LPRECT, LPARAM) |
| 477 | { |
| 478 | MONITORINFO info = {}; |
| 479 | info.cbSize = sizeof(MONITORINFO); |
| 480 | if (!::GetMonitorInfo(monitor, &info)) |
| 481 | return TRUE; |
| 482 | ImGuiPlatformMonitor imgui_monitor; |
| 483 | imgui_monitor.MainPos = ImVec2((float)info.rcMonitor.left, (float)info.rcMonitor.top); |
| 484 | imgui_monitor.MainSize = ImVec2((float)(info.rcMonitor.right - info.rcMonitor.left), (float)(info.rcMonitor.bottom - info.rcMonitor.top)); |
| 485 | imgui_monitor.WorkPos = ImVec2((float)info.rcWork.left, (float)info.rcWork.top); |
| 486 | imgui_monitor.WorkSize = ImVec2((float)(info.rcWork.right - info.rcWork.left), (float)(info.rcWork.bottom - info.rcWork.top)); |
| 487 | imgui_monitor.DpiScale = ImGui_ImplWin32_GetDpiScaleForMonitor(monitor); |
| 488 | imgui_monitor.PlatformHandle = (void*)monitor; |
| 489 | if (imgui_monitor.DpiScale <= 0.0f) |
| 490 | return TRUE; |
| 491 | ImGuiPlatformIO& io = ImGui::GetPlatformIO(); |
| 492 | if (info.dwFlags & MONITORINFOF_PRIMARY) |
| 493 | io.Monitors.push_front(imgui_monitor); |
| 494 | else |
| 495 | io.Monitors.push_back(imgui_monitor); |
| 496 | return TRUE; |
| 497 | } |
| 498 | |
| 499 | static void ImGui_ImplWin32_UpdateMonitors() |
| 500 | { |
| 501 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
| 502 | ImGui::GetPlatformIO().Monitors.resize(0); |
| 503 | ::EnumDisplayMonitors(nullptr, nullptr, ImGui_ImplWin32_UpdateMonitors_EnumFunc, 0); |
| 504 | bd->WantUpdateMonitors = false; |
| 505 | } |
| 506 | |
| 507 | void ImGui_ImplWin32_NewFrame() |
| 508 | { |
| 509 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
| 510 | IM_ASSERT(bd != nullptr && "Context or backend not initialized? Did you call ImGui_ImplWin32_Init()?"); |
| 511 | ImGuiIO& io = ImGui::GetIO(); |
| 512 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 513 | |
| 514 | |
| 515 | RECT rect = { 0, 0, 0, 0 }; |
| 516 | ::GetClientRect(bd->hWnd, &rect); |
| 517 | io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top)); |
| 518 | if (bd->WantUpdateMonitors) |
| 519 | ImGui_ImplWin32_UpdateMonitors(); |
| 520 | |
| 521 | |
| 522 | INT64 current_time = 0; |
| 523 | ::QueryPerformanceCounter((LARGE_INTEGER*)¤t_time); |
| 524 | io.DeltaTime = (float)((double)(current_time - bd->Time) / (double)bd->TicksPerSecond); |
| 525 | bd->Time = current_time; |
| 526 | |
| 527 | |
| 528 | ImGui_ImplWin32_UpdateMouseData(io, platform_io); |
| 529 | |
| 530 | |
| 531 | ImGui_ImplWin32_ProcessKeyEventsWorkarounds(io); |
| 532 | |
| 533 | |
| 534 | ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor(); |
| 535 | if (bd->LastMouseCursor != mouse_cursor) |
| 536 | { |
| 537 | bd->LastMouseCursor = mouse_cursor; |
| 538 | ImGui_ImplWin32_UpdateMouseCursor(io, mouse_cursor); |
| 539 | } |
| 540 | |
| 541 | |
| 542 | ImGui_ImplWin32_UpdateGamepads(io); |
| 543 | } |
| 544 | |
| 545 | |
| 546 | |
| 547 | ImGuiKey ImGui_ImplWin32_KeyEventToImGuiKey(WPARAM wParam, LPARAM lParam); |
| 548 | ImGuiKey ImGui_ImplWin32_KeyEventToImGuiKey(WPARAM wParam, LPARAM lParam) |
| 549 | { |
| 550 | |
| 551 | if ((wParam == VK_RETURN) && (HIWORD(lParam) & KF_EXTENDED)) |
| 552 | return ImGuiKey_KeypadEnter; |
| 553 | |
| 554 | const int scancode = (int)LOBYTE(HIWORD(lParam)); |
| 555 | |
| 556 | switch (wParam) |
| 557 | { |
| 558 | case VK_TAB: return ImGuiKey_Tab; |
| 559 | case VK_LEFT: return ImGuiKey_LeftArrow; |
| 560 | case VK_RIGHT: return ImGuiKey_RightArrow; |
| 561 | case VK_UP: return ImGuiKey_UpArrow; |
| 562 | case VK_DOWN: return ImGuiKey_DownArrow; |
| 563 | case VK_PRIOR: return ImGuiKey_PageUp; |
| 564 | case VK_NEXT: return ImGuiKey_PageDown; |
| 565 | case VK_HOME: return ImGuiKey_Home; |
| 566 | case VK_END: return ImGuiKey_End; |
| 567 | case VK_INSERT: return ImGuiKey_Insert; |
| 568 | case VK_DELETE: return ImGuiKey_Delete; |
| 569 | case VK_BACK: return ImGuiKey_Backspace; |
| 570 | case VK_SPACE: return ImGuiKey_Space; |
| 571 | case VK_RETURN: return ImGuiKey_Enter; |
| 572 | case VK_ESCAPE: return ImGuiKey_Escape; |
| 573 | |
| 574 | case VK_OEM_COMMA: return ImGuiKey_Comma; |
| 575 | |
| 576 | case VK_OEM_PERIOD: return ImGuiKey_Period; |
| 577 | |
| 578 | |
| 579 | |
| 580 | |
| 581 | |
| 582 | |
| 583 | |
| 584 | case VK_CAPITAL: return ImGuiKey_CapsLock; |
| 585 | case VK_SCROLL: return ImGuiKey_ScrollLock; |
| 586 | case VK_NUMLOCK: return ImGuiKey_NumLock; |
| 587 | case VK_SNAPSHOT: return ImGuiKey_PrintScreen; |
| 588 | case VK_PAUSE: return ImGuiKey_Pause; |
| 589 | case VK_NUMPAD0: return ImGuiKey_Keypad0; |
| 590 | case VK_NUMPAD1: return ImGuiKey_Keypad1; |
| 591 | case VK_NUMPAD2: return ImGuiKey_Keypad2; |
| 592 | case VK_NUMPAD3: return ImGuiKey_Keypad3; |
| 593 | case VK_NUMPAD4: return ImGuiKey_Keypad4; |
| 594 | case VK_NUMPAD5: return ImGuiKey_Keypad5; |
| 595 | case VK_NUMPAD6: return ImGuiKey_Keypad6; |
| 596 | case VK_NUMPAD7: return ImGuiKey_Keypad7; |
| 597 | case VK_NUMPAD8: return ImGuiKey_Keypad8; |
| 598 | case VK_NUMPAD9: return ImGuiKey_Keypad9; |
| 599 | case VK_DECIMAL: return ImGuiKey_KeypadDecimal; |
| 600 | case VK_DIVIDE: return ImGuiKey_KeypadDivide; |
| 601 | case VK_MULTIPLY: return ImGuiKey_KeypadMultiply; |
| 602 | case VK_SUBTRACT: return ImGuiKey_KeypadSubtract; |
| 603 | case VK_ADD: return ImGuiKey_KeypadAdd; |
| 604 | case VK_LSHIFT: return ImGuiKey_LeftShift; |
| 605 | case VK_LCONTROL: return ImGuiKey_LeftCtrl; |
| 606 | case VK_LMENU: return ImGuiKey_LeftAlt; |
| 607 | case VK_LWIN: return ImGuiKey_LeftSuper; |
| 608 | case VK_RSHIFT: return ImGuiKey_RightShift; |
| 609 | case VK_RCONTROL: return ImGuiKey_RightCtrl; |
| 610 | case VK_RMENU: return ImGuiKey_RightAlt; |
| 611 | case VK_RWIN: return ImGuiKey_RightSuper; |
| 612 | case VK_APPS: return ImGuiKey_Menu; |
| 613 | case '0': return ImGuiKey_0; |
| 614 | case '1': return ImGuiKey_1; |
| 615 | case '2': return ImGuiKey_2; |
| 616 | case '3': return ImGuiKey_3; |
| 617 | case '4': return ImGuiKey_4; |
| 618 | case '5': return ImGuiKey_5; |
| 619 | case '6': return ImGuiKey_6; |
| 620 | case '7': return ImGuiKey_7; |
| 621 | case '8': return ImGuiKey_8; |
| 622 | case '9': return ImGuiKey_9; |
| 623 | case 'A': return ImGuiKey_A; |
| 624 | case 'B': return ImGuiKey_B; |
| 625 | case 'C': return ImGuiKey_C; |
| 626 | case 'D': return ImGuiKey_D; |
| 627 | case 'E': return ImGuiKey_E; |
| 628 | case 'F': return ImGuiKey_F; |
| 629 | case 'G': return ImGuiKey_G; |
| 630 | case 'H': return ImGuiKey_H; |
| 631 | case 'I': return ImGuiKey_I; |
| 632 | case 'J': return ImGuiKey_J; |
| 633 | case 'K': return ImGuiKey_K; |
| 634 | case 'L': return ImGuiKey_L; |
| 635 | case 'M': return ImGuiKey_M; |
| 636 | case 'N': return ImGuiKey_N; |
| 637 | case 'O': return ImGuiKey_O; |
| 638 | case 'P': return ImGuiKey_P; |
| 639 | case 'Q': return ImGuiKey_Q; |
| 640 | case 'R': return ImGuiKey_R; |
| 641 | case 'S': return ImGuiKey_S; |
| 642 | case 'T': return ImGuiKey_T; |
| 643 | case 'U': return ImGuiKey_U; |
| 644 | case 'V': return ImGuiKey_V; |
| 645 | case 'W': return ImGuiKey_W; |
| 646 | case 'X': return ImGuiKey_X; |
| 647 | case 'Y': return ImGuiKey_Y; |
| 648 | case 'Z': return ImGuiKey_Z; |
| 649 | case VK_F1: return ImGuiKey_F1; |
| 650 | case VK_F2: return ImGuiKey_F2; |
| 651 | case VK_F3: return ImGuiKey_F3; |
| 652 | case VK_F4: return ImGuiKey_F4; |
| 653 | case VK_F5: return ImGuiKey_F5; |
| 654 | case VK_F6: return ImGuiKey_F6; |
| 655 | case VK_F7: return ImGuiKey_F7; |
| 656 | case VK_F8: return ImGuiKey_F8; |
| 657 | case VK_F9: return ImGuiKey_F9; |
| 658 | case VK_F10: return ImGuiKey_F10; |
| 659 | case VK_F11: return ImGuiKey_F11; |
| 660 | case VK_F12: return ImGuiKey_F12; |
| 661 | case VK_F13: return ImGuiKey_F13; |
| 662 | case VK_F14: return ImGuiKey_F14; |
| 663 | case VK_F15: return ImGuiKey_F15; |
| 664 | case VK_F16: return ImGuiKey_F16; |
| 665 | case VK_F17: return ImGuiKey_F17; |
| 666 | case VK_F18: return ImGuiKey_F18; |
| 667 | case VK_F19: return ImGuiKey_F19; |
| 668 | case VK_F20: return ImGuiKey_F20; |
| 669 | case VK_F21: return ImGuiKey_F21; |
| 670 | case VK_F22: return ImGuiKey_F22; |
| 671 | case VK_F23: return ImGuiKey_F23; |
| 672 | case VK_F24: return ImGuiKey_F24; |
| 673 | case VK_BROWSER_BACK: return ImGuiKey_AppBack; |
| 674 | case VK_BROWSER_FORWARD: return ImGuiKey_AppForward; |
| 675 | default: break; |
| 676 | } |
| 677 | |
| 678 | |
| 679 | |
| 680 | switch (scancode) |
| 681 | { |
| 682 | case 41: return ImGuiKey_GraveAccent; |
| 683 | case 12: return ImGuiKey_Minus; |
| 684 | case 13: return ImGuiKey_Equal; |
| 685 | case 26: return ImGuiKey_LeftBracket; |
| 686 | case 27: return ImGuiKey_RightBracket; |
| 687 | case 86: return ImGuiKey_Oem102; |
| 688 | case 43: return ImGuiKey_Backslash; |
| 689 | case 39: return ImGuiKey_Semicolon; |
| 690 | case 40: return ImGuiKey_Apostrophe; |
| 691 | case 51: return ImGuiKey_Comma; |
| 692 | case 52: return ImGuiKey_Period; |
| 693 | case 53: return ImGuiKey_Slash; |
| 694 | default: break; |
| 695 | } |
| 696 | |
| 697 | return ImGuiKey_None; |
| 698 | } |
| 699 | |
| 700 | |
| 701 | #ifndef WM_MOUSEHWHEEL |
| 702 | #define WM_MOUSEHWHEEL 0x020E |
| 703 | #endif |
| 704 | #ifndef DBT_DEVNODES_CHANGED |
| 705 | #define DBT_DEVNODES_CHANGED 0x0007 |
| 706 | #endif |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | static ImGuiMouseSource ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo() |
| 712 | { |
| 713 | LPARAM extra_info = ::GetMessageExtraInfo(); |
| 714 | if ((extra_info & 0xFFFFFF80) == 0xFF515700) |
| 715 | return ImGuiMouseSource_Pen; |
| 716 | if ((extra_info & 0xFFFFFF80) == 0xFF515780) |
| 717 | return ImGuiMouseSource_TouchScreen; |
| 718 | return ImGuiMouseSource_Mouse; |
| 719 | } |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); |
| 731 | extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandlerEx(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, ImGuiIO& io); |
| 732 | |
| 733 | #ifndef WM_DPICHANGED |
| 734 | #define WM_DPICHANGED 0x02E0 |
| 735 | #endif |
| 736 | |
| 737 | IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 738 | { |
| 739 | |
| 740 | |
| 741 | if (ImGui::GetCurrentContext() == nullptr) |
| 742 | return 0; |
| 743 | return ImGui_ImplWin32_WndProcHandlerEx(hwnd, msg, wParam, lParam, ImGui::GetIO()); |
| 744 | } |
| 745 | |
| 746 | |
| 747 | IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandlerEx(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, ImGuiIO& io) |
| 748 | { |
| 749 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(io); |
| 750 | if (bd == nullptr) |
| 751 | return 0; |
| 752 | switch (msg) |
| 753 | { |
| 754 | case WM_MOUSEMOVE: |
| 755 | case WM_NCMOUSEMOVE: |
| 756 | { |
| 757 | |
| 758 | ImGuiMouseSource mouse_source = ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo(); |
| 759 | const int area = (msg == WM_MOUSEMOVE) ? 1 : 2; |
| 760 | bd->MouseHwnd = hwnd; |
| 761 | if (bd->MouseTrackedArea != area) |
| 762 | { |
| 763 | TRACKMOUSEEVENT tme_cancel = { sizeof(tme_cancel), TME_CANCEL, hwnd, 0 }; |
| 764 | TRACKMOUSEEVENT tme_track = { sizeof(tme_track), (DWORD)((area == 2) ? (TME_LEAVE | TME_NONCLIENT) : TME_LEAVE), hwnd, 0 }; |
| 765 | if (bd->MouseTrackedArea != 0) |
| 766 | ::TrackMouseEvent(&tme_cancel); |
| 767 | ::TrackMouseEvent(&tme_track); |
| 768 | bd->MouseTrackedArea = area; |
| 769 | } |
| 770 | POINT mouse_pos = { (LONG)GET_X_LPARAM(lParam), (LONG)GET_Y_LPARAM(lParam) }; |
| 771 | bool want_absolute_pos = (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) != 0; |
| 772 | if (msg == WM_MOUSEMOVE && want_absolute_pos) |
| 773 | ::ClientToScreen(hwnd, &mouse_pos); |
| 774 | if (msg == WM_NCMOUSEMOVE && !want_absolute_pos) |
| 775 | ::ScreenToClient(hwnd, &mouse_pos); |
| 776 | io.AddMouseSourceEvent(mouse_source); |
| 777 | io.AddMousePosEvent((float)mouse_pos.x, (float)mouse_pos.y); |
| 778 | return 0; |
| 779 | } |
| 780 | case WM_MOUSELEAVE: |
| 781 | case WM_NCMOUSELEAVE: |
| 782 | { |
| 783 | const int area = (msg == WM_MOUSELEAVE) ? 1 : 2; |
| 784 | if (bd->MouseTrackedArea == area) |
| 785 | { |
| 786 | if (bd->MouseHwnd == hwnd) |
| 787 | bd->MouseHwnd = nullptr; |
| 788 | bd->MouseTrackedArea = 0; |
| 789 | io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); |
| 790 | } |
| 791 | return 0; |
| 792 | } |
| 793 | case WM_DESTROY: |
| 794 | if (bd->MouseHwnd == hwnd && bd->MouseTrackedArea != 0) |
| 795 | { |
| 796 | TRACKMOUSEEVENT tme_cancel = { sizeof(tme_cancel), TME_CANCEL, hwnd, 0 }; |
| 797 | ::TrackMouseEvent(&tme_cancel); |
| 798 | bd->MouseHwnd = nullptr; |
| 799 | bd->MouseTrackedArea = 0; |
| 800 | io.AddMousePosEvent(-FLT_MAX, -FLT_MAX); |
| 801 | } |
| 802 | return 0; |
| 803 | case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK: |
| 804 | case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK: |
| 805 | case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK: |
| 806 | case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK: |
| 807 | { |
| 808 | ImGuiMouseSource mouse_source = ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo(); |
| 809 | int button = 0; |
| 810 | if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { button = 0; } |
| 811 | if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; } |
| 812 | if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; } |
| 813 | if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; } |
| 814 | HWND hwnd_with_capture = ::GetCapture(); |
| 815 | if (bd->MouseButtonsDown != 0 && hwnd_with_capture != hwnd) |
| 816 | bd->MouseButtonsDown = 0; |
| 817 | if (bd->MouseButtonsDown == 0 && hwnd_with_capture == nullptr) |
| 818 | ::SetCapture(hwnd); |
| 819 | bd->MouseButtonsDown |= 1 << button; |
| 820 | io.AddMouseSourceEvent(mouse_source); |
| 821 | io.AddMouseButtonEvent(button, true); |
| 822 | return 0; |
| 823 | } |
| 824 | case WM_LBUTTONUP: |
| 825 | case WM_RBUTTONUP: |
| 826 | case WM_MBUTTONUP: |
| 827 | case WM_XBUTTONUP: |
| 828 | { |
| 829 | ImGuiMouseSource mouse_source = ImGui_ImplWin32_GetMouseSourceFromMessageExtraInfo(); |
| 830 | int button = 0; |
| 831 | if (msg == WM_LBUTTONUP) { button = 0; } |
| 832 | if (msg == WM_RBUTTONUP) { button = 1; } |
| 833 | if (msg == WM_MBUTTONUP) { button = 2; } |
| 834 | if (msg == WM_XBUTTONUP) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; } |
| 835 | bd->MouseButtonsDown &= ~(1 << button); |
| 836 | if (bd->MouseButtonsDown == 0 && ::GetCapture() == hwnd) |
| 837 | ::ReleaseCapture(); |
| 838 | io.AddMouseSourceEvent(mouse_source); |
| 839 | io.AddMouseButtonEvent(button, false); |
| 840 | return 0; |
| 841 | } |
| 842 | case WM_MOUSEWHEEL: |
| 843 | io.AddMouseWheelEvent(0.0f, (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA); |
| 844 | return 0; |
| 845 | case WM_MOUSEHWHEEL: |
| 846 | io.AddMouseWheelEvent(-(float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA, 0.0f); |
| 847 | return 0; |
| 848 | case WM_KEYDOWN: |
| 849 | case WM_KEYUP: |
| 850 | case WM_SYSKEYDOWN: |
| 851 | case WM_SYSKEYUP: |
| 852 | { |
| 853 | const bool is_key_down = (msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN); |
| 854 | if (wParam < 256) |
| 855 | { |
| 856 | |
| 857 | ImGui_ImplWin32_UpdateKeyModifiers(io); |
| 858 | |
| 859 | |
| 860 | const ImGuiKey key = ImGui_ImplWin32_KeyEventToImGuiKey(wParam, lParam); |
| 861 | const int vk = (int)wParam; |
| 862 | const int scancode = (int)LOBYTE(HIWORD(lParam)); |
| 863 | |
| 864 | |
| 865 | if (key == ImGuiKey_PrintScreen && !is_key_down) |
| 866 | ImGui_ImplWin32_AddKeyEvent(io, key, true, vk, scancode); |
| 867 | |
| 868 | |
| 869 | if (key != ImGuiKey_None) |
| 870 | ImGui_ImplWin32_AddKeyEvent(io, key, is_key_down, vk, scancode); |
| 871 | |
| 872 | |
| 873 | if (vk == VK_SHIFT) |
| 874 | { |
| 875 | |
| 876 | if (IsVkDown(VK_LSHIFT) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftShift, is_key_down, VK_LSHIFT, scancode); } |
| 877 | if (IsVkDown(VK_RSHIFT) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightShift, is_key_down, VK_RSHIFT, scancode); } |
| 878 | } |
| 879 | else if (vk == VK_CONTROL) |
| 880 | { |
| 881 | if (IsVkDown(VK_LCONTROL) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftCtrl, is_key_down, VK_LCONTROL, scancode); } |
| 882 | if (IsVkDown(VK_RCONTROL) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightCtrl, is_key_down, VK_RCONTROL, scancode); } |
| 883 | } |
| 884 | else if (vk == VK_MENU) |
| 885 | { |
| 886 | if (IsVkDown(VK_LMENU) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_LeftAlt, is_key_down, VK_LMENU, scancode); } |
| 887 | if (IsVkDown(VK_RMENU) == is_key_down) { ImGui_ImplWin32_AddKeyEvent(io, ImGuiKey_RightAlt, is_key_down, VK_RMENU, scancode); } |
| 888 | } |
| 889 | } |
| 890 | return 0; |
| 891 | } |
| 892 | case WM_SETFOCUS: |
| 893 | case WM_KILLFOCUS: |
| 894 | io.AddFocusEvent(msg == WM_SETFOCUS); |
| 895 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
| 896 | bd->XInputPacketNumber = 0; |
| 897 | #endif |
| 898 | return 0; |
| 899 | case WM_INPUTLANGCHANGE: |
| 900 | ImGui_ImplWin32_UpdateKeyboardCodePage(io); |
| 901 | return 0; |
| 902 | case WM_CHAR: |
| 903 | if (::IsWindowUnicode(hwnd)) |
| 904 | { |
| 905 | |
| 906 | if (wParam > 0 && wParam < 0x10000) |
| 907 | io.AddInputCharacterUTF16((unsigned short)wParam); |
| 908 | } |
| 909 | else |
| 910 | { |
| 911 | wchar_t wch = 0; |
| 912 | ::MultiByteToWideChar(bd->KeyboardCodePage, MB_PRECOMPOSED, (char*)&wParam, 2, &wch, 1); |
| 913 | io.AddInputCharacter(wch); |
| 914 | } |
| 915 | return 0; |
| 916 | case WM_IME_COMPOSITION: |
| 917 | { |
| 918 | |
| 919 | |
| 920 | LRESULT result = ::DefWindowProcW(hwnd, msg, wParam, lParam); |
| 921 | return (lParam & GCS_RESULTSTR) ? 1 : result; |
| 922 | } |
| 923 | case WM_IME_CHAR: |
| 924 | if (::IsWindowUnicode(hwnd) == FALSE) |
| 925 | { |
| 926 | if (::IsDBCSLeadByte(HIBYTE(wParam))) |
| 927 | wParam = (WPARAM)MAKEWORD(HIBYTE(wParam), LOBYTE(wParam)); |
| 928 | wchar_t wch = 0; |
| 929 | ::MultiByteToWideChar(bd->KeyboardCodePage, MB_PRECOMPOSED, (char*)&wParam, 2, &wch, 1); |
| 930 | io.AddInputCharacterUTF16(wch); |
| 931 | return 1; |
| 932 | } |
| 933 | return 0; |
| 934 | case WM_SETCURSOR: |
| 935 | |
| 936 | if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor(io, bd->LastMouseCursor)) |
| 937 | return 1; |
| 938 | return 0; |
| 939 | case WM_DEVICECHANGE: |
| 940 | #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD |
| 941 | if ((UINT)wParam == DBT_DEVNODES_CHANGED) |
| 942 | bd->WantUpdateHasGamepad = true; |
| 943 | #endif |
| 944 | return 0; |
| 945 | case WM_DISPLAYCHANGE: |
| 946 | bd->WantUpdateMonitors = true; |
| 947 | return 0; |
| 948 | case WM_SETTINGCHANGE: |
| 949 | if (wParam == SPI_SETWORKAREA) |
| 950 | bd->WantUpdateMonitors = true; |
| 951 | return 0; |
| 952 | case WM_DPICHANGED: |
| 953 | { |
| 954 | |
| 955 | const RECT* suggested_rect = (RECT*)lParam; |
| 956 | if (io.ConfigDpiScaleViewports) |
| 957 | ::SetWindowPos(hwnd, nullptr, suggested_rect->left, suggested_rect->top, suggested_rect->right - suggested_rect->left, suggested_rect->bottom - suggested_rect->top, SWP_NOZORDER | SWP_NOACTIVATE); |
| 958 | return 0; |
| 959 | } |
| 960 | } |
| 961 | return 0; |
| 962 | } |
| 963 | |
| 964 | |
| 965 | |
| 966 | |
| 967 | |
| 968 | |
| 969 | |
| 970 | |
| 971 | |
| 972 | |
| 973 | |
| 974 | |
| 975 | |
| 976 | |
| 977 | |
| 978 | |
| 979 | |
| 980 | |
| 981 | static BOOL _IsWindowsVersionOrGreater(WORD major, WORD minor, WORD) |
| 982 | { |
| 983 | typedef LONG(WINAPI* PFN_RtlVerifyVersionInfo)(OSVERSIONINFOEXW*, ULONG, ULONGLONG); |
| 984 | static PFN_RtlVerifyVersionInfo RtlVerifyVersionInfoFn = nullptr; |
| 985 | if (RtlVerifyVersionInfoFn == nullptr) |
| 986 | if (HMODULE ntdllModule = ::GetModuleHandleA("ntdll.dll")) |
| 987 | RtlVerifyVersionInfoFn = (PFN_RtlVerifyVersionInfo)GetProcAddress(ntdllModule, "RtlVerifyVersionInfo"); |
| 988 | if (RtlVerifyVersionInfoFn == nullptr) |
| 989 | return FALSE; |
| 990 | |
| 991 | RTL_OSVERSIONINFOEXW versionInfo = { }; |
| 992 | ULONGLONG conditionMask = 0; |
| 993 | versionInfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW); |
| 994 | versionInfo.dwMajorVersion = major; |
| 995 | versionInfo.dwMinorVersion = minor; |
| 996 | VER_SET_CONDITION(conditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL); |
| 997 | VER_SET_CONDITION(conditionMask, VER_MINORVERSION, VER_GREATER_EQUAL); |
| 998 | return (RtlVerifyVersionInfoFn(&versionInfo, VER_MAJORVERSION | VER_MINORVERSION, conditionMask) == 0) ? TRUE : FALSE; |
| 999 | } |
| 1000 | |
| 1001 | #define _IsWindowsVistaOrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0600), LOBYTE(0x0600), 0) |
| 1002 | #define _IsWindows8OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0602), LOBYTE(0x0602), 0) |
| 1003 | #define _IsWindows8Point1OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0603), LOBYTE(0x0603), 0) |
| 1004 | #define _IsWindows10OrGreater() _IsWindowsVersionOrGreater(HIBYTE(0x0A00), LOBYTE(0x0A00), 0) |
| 1005 | |
| 1006 | #ifndef DPI_ENUMS_DECLARED |
| 1007 | typedef enum { PROCESS_DPI_UNAWARE = 0, PROCESS_SYSTEM_DPI_AWARE = 1, PROCESS_PER_MONITOR_DPI_AWARE = 2 } PROCESS_DPI_AWARENESS; |
| 1008 | typedef enum { MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2, MDT_DEFAULT = MDT_EFFECTIVE_DPI } MONITOR_DPI_TYPE; |
| 1009 | #endif |
| 1010 | #ifndef _DPI_AWARENESS_CONTEXTS_ |
| 1011 | DECLARE_HANDLE(DPI_AWARENESS_CONTEXT); |
| 1012 | #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE (DPI_AWARENESS_CONTEXT)-3 |
| 1013 | #endif |
| 1014 | #ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 |
| 1015 | #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (DPI_AWARENESS_CONTEXT)-4 |
| 1016 | #endif |
| 1017 | typedef HRESULT(WINAPI* PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS); |
| 1018 | typedef HRESULT(WINAPI* PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); |
| 1019 | typedef DPI_AWARENESS_CONTEXT(WINAPI* PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); |
| 1020 | typedef BOOL (WINAPI* PFN_SetProcessDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); |
| 1021 | typedef DPI_AWARENESS_CONTEXT(WINAPI* PFN_GetThreadDpiAwarenessContext)(); |
| 1022 | typedef BOOL(WINAPI* PFN_AreDpiAwarenessContextsEqual)(DPI_AWARENESS_CONTEXT, DPI_AWARENESS_CONTEXT); |
| 1023 | |
| 1024 | |
| 1025 | void ImGui_ImplWin32_EnableDpiAwareness() |
| 1026 | { |
| 1027 | |
| 1028 | if (ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData()) |
| 1029 | bd->WantUpdateMonitors = true; |
| 1030 | |
| 1031 | if (_IsWindows10OrGreater()) |
| 1032 | { |
| 1033 | static HINSTANCE user32_dll = ::LoadLibraryA("user32.dll"); |
| 1034 | if (PFN_SetProcessDpiAwarenessContext SetProcessDpiAwarenessContextFn = (PFN_SetProcessDpiAwarenessContext)::GetProcAddress(user32_dll, "SetProcessDpiAwarenessContext")) |
| 1035 | { |
| 1036 | SetProcessDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); |
| 1037 | return; |
| 1038 | } |
| 1039 | if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "SetThreadDpiAwarenessContext")) |
| 1040 | { |
| 1041 | SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); |
| 1042 | return; |
| 1043 | } |
| 1044 | } |
| 1045 | if (_IsWindows8Point1OrGreater()) |
| 1046 | { |
| 1047 | static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); |
| 1048 | if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, "SetProcessDpiAwareness")) |
| 1049 | { |
| 1050 | SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE); |
| 1051 | return; |
| 1052 | } |
| 1053 | } |
| 1054 | #if _WIN32_WINNT >= 0x0600 |
| 1055 | ::SetProcessDPIAware(); |
| 1056 | #endif |
| 1057 | } |
| 1058 | |
| 1059 | static bool ImGui_ImplWin32_IsDpiAwarenessPerMonitorAwareV2() |
| 1060 | { |
| 1061 | if (!_IsWindows10OrGreater()) |
| 1062 | return false; |
| 1063 | static HINSTANCE user32_dll = ::GetModuleHandleA("user32.dll"); |
| 1064 | PFN_GetThreadDpiAwarenessContext GetThreadDpiAwarenessContextFn = (PFN_GetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "GetThreadDpiAwarenessContext"); |
| 1065 | PFN_AreDpiAwarenessContextsEqual AreDpiAwarenessContextsEqualFn = (PFN_AreDpiAwarenessContextsEqual)::GetProcAddress(user32_dll, "AreDpiAwarenessContextsEqual"); |
| 1066 | if (GetThreadDpiAwarenessContextFn && AreDpiAwarenessContextsEqualFn) |
| 1067 | return AreDpiAwarenessContextsEqualFn(GetThreadDpiAwarenessContextFn(), DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) != 0; |
| 1068 | return false; |
| 1069 | } |
| 1070 | |
| 1071 | #if defined(_MSC_VER) && !defined(NOGDI) |
| 1072 | #pragma comment(lib, "gdi32") |
| 1073 | #endif |
| 1074 | |
| 1075 | float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor) |
| 1076 | { |
| 1077 | UINT xdpi = 96, ydpi = 96; |
| 1078 | if (_IsWindows8Point1OrGreater()) |
| 1079 | { |
| 1080 | static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); |
| 1081 | static PFN_GetDpiForMonitor GetDpiForMonitorFn = nullptr; |
| 1082 | if (GetDpiForMonitorFn == nullptr && shcore_dll != nullptr) |
| 1083 | GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, "GetDpiForMonitor"); |
| 1084 | if (GetDpiForMonitorFn != nullptr) |
| 1085 | { |
| 1086 | GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi); |
| 1087 | IM_ASSERT(xdpi == ydpi); |
| 1088 | return (float)xdpi / 96.0f; |
| 1089 | } |
| 1090 | } |
| 1091 | #ifndef NOGDI |
| 1092 | const HDC dc = ::GetDC(nullptr); |
| 1093 | xdpi = ::GetDeviceCaps(dc, LOGPIXELSX); |
| 1094 | ydpi = ::GetDeviceCaps(dc, LOGPIXELSY); |
| 1095 | IM_ASSERT(xdpi == ydpi); |
| 1096 | ::ReleaseDC(nullptr, dc); |
| 1097 | #endif |
| 1098 | return (float)xdpi / 96.0f; |
| 1099 | } |
| 1100 | |
| 1101 | float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd) |
| 1102 | { |
| 1103 | HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST); |
| 1104 | return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor); |
| 1105 | } |
| 1106 | |
| 1107 | |
| 1108 | |
| 1109 | |
| 1110 | |
| 1111 | #if defined(_MSC_VER) |
| 1112 | #pragma comment(lib, "dwmapi") |
| 1113 | #endif |
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | |
| 1118 | void ImGui_ImplWin32_EnableAlphaCompositing(void* hwnd) |
| 1119 | { |
| 1120 | if (!_IsWindowsVistaOrGreater()) |
| 1121 | return; |
| 1122 | |
| 1123 | BOOL composition; |
| 1124 | if (FAILED(::DwmIsCompositionEnabled(&composition)) || !composition) |
| 1125 | return; |
| 1126 | |
| 1127 | BOOL opaque; |
| 1128 | DWORD color; |
| 1129 | if (_IsWindows8OrGreater() || (SUCCEEDED(::DwmGetColorizationColor(&color, &opaque)) && !opaque)) |
| 1130 | { |
| 1131 | HRGN region = ::CreateRectRgn(0, 0, -1, -1); |
| 1132 | DWM_BLURBEHIND bb = {}; |
| 1133 | bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION; |
| 1134 | bb.hRgnBlur = region; |
| 1135 | bb.fEnable = TRUE; |
| 1136 | ::DwmEnableBlurBehindWindow((HWND)hwnd, &bb); |
| 1137 | ::DeleteObject(region); |
| 1138 | } |
| 1139 | else |
| 1140 | { |
| 1141 | DWM_BLURBEHIND bb = {}; |
| 1142 | bb.dwFlags = DWM_BB_ENABLE; |
| 1143 | ::DwmEnableBlurBehindWindow((HWND)hwnd, &bb); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | |
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | struct ImGui_ImplWin32_ViewportData |
| 1155 | { |
| 1156 | HWND Hwnd; |
| 1157 | HWND HwndParent; |
| 1158 | bool HwndOwned; |
| 1159 | DWORD DwStyle; |
| 1160 | DWORD DwExStyle; |
| 1161 | |
| 1162 | ImGui_ImplWin32_ViewportData() { Hwnd = HwndParent = nullptr; HwndOwned = false; DwStyle = DwExStyle = 0; } |
| 1163 | ~ImGui_ImplWin32_ViewportData() { IM_ASSERT(Hwnd == nullptr); } |
| 1164 | }; |
| 1165 | |
| 1166 | static void ImGui_ImplWin32_GetWin32StyleFromViewportFlags(ImGuiViewportFlags flags, DWORD* out_style, DWORD* out_ex_style) |
| 1167 | { |
| 1168 | if (flags & ImGuiViewportFlags_NoDecoration) |
| 1169 | *out_style = WS_POPUP; |
| 1170 | else |
| 1171 | *out_style = WS_OVERLAPPEDWINDOW; |
| 1172 | |
| 1173 | if (flags & ImGuiViewportFlags_NoTaskBarIcon) |
| 1174 | *out_ex_style = WS_EX_TOOLWINDOW; |
| 1175 | else |
| 1176 | *out_ex_style = WS_EX_APPWINDOW; |
| 1177 | |
| 1178 | if (flags & ImGuiViewportFlags_TopMost) |
| 1179 | *out_ex_style |= WS_EX_TOPMOST; |
| 1180 | } |
| 1181 | |
| 1182 | static HWND ImGui_ImplWin32_GetHwndFromViewport(ImGuiViewport* viewport) |
| 1183 | { |
| 1184 | if (viewport != nullptr) |
| 1185 | return (HWND)viewport->PlatformHandle; |
| 1186 | return nullptr; |
| 1187 | } |
| 1188 | |
| 1189 | static void ImGui_ImplWin32_AdjustWindowRect(ImGuiViewport* viewport, RECT* rect, DWORD style, DWORD ex_style) |
| 1190 | { |
| 1191 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
| 1192 | if (bd->AdjustWindowRectExForDpi && bd->IsPerMonitorDpiAwareV2) |
| 1193 | { |
| 1194 | UINT dpi = (UINT)(viewport->DpiScale * 96.0f + 0.5f); |
| 1195 | if (dpi != 0 && bd->AdjustWindowRectExForDpi(rect, style, FALSE, ex_style, dpi)) |
| 1196 | return; |
| 1197 | } |
| 1198 | ::AdjustWindowRectEx(rect, style, FALSE, ex_style); |
| 1199 | } |
| 1200 | |
| 1201 | static void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport) |
| 1202 | { |
| 1203 | ImGui_ImplWin32_ViewportData* vd = IM_NEW(ImGui_ImplWin32_ViewportData)(); |
| 1204 | viewport->PlatformUserData = vd; |
| 1205 | |
| 1206 | |
| 1207 | ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &vd->DwStyle, &vd->DwExStyle); |
| 1208 | vd->HwndParent = ImGui_ImplWin32_GetHwndFromViewport(viewport->ParentViewport); |
| 1209 | |
| 1210 | |
| 1211 | RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) }; |
| 1212 | ImGui_ImplWin32_AdjustWindowRect(viewport, &rect, vd->DwStyle, vd->DwExStyle); |
| 1213 | vd->Hwnd = ::CreateWindowExW( |
| 1214 | vd->DwExStyle, L"ImGui Platform", L"Untitled", vd->DwStyle, |
| 1215 | rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, |
| 1216 | vd->HwndParent, nullptr, ::GetModuleHandle(nullptr), nullptr); |
| 1217 | vd->HwndOwned = true; |
| 1218 | viewport->PlatformRequestResize = false; |
| 1219 | viewport->PlatformHandle = viewport->PlatformHandleRaw = vd->Hwnd; |
| 1220 | |
| 1221 | |
| 1222 | ::SetPropA(vd->Hwnd, "IMGUI_CONTEXT", ImGui::GetCurrentContext()); |
| 1223 | } |
| 1224 | |
| 1225 | static void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport) |
| 1226 | { |
| 1227 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
| 1228 | if (ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData) |
| 1229 | { |
| 1230 | if (::GetCapture() == vd->Hwnd) |
| 1231 | { |
| 1232 | |
| 1233 | ::ReleaseCapture(); |
| 1234 | ::SetCapture(bd->hWnd); |
| 1235 | } |
| 1236 | if (vd->Hwnd && vd->HwndOwned) |
| 1237 | ::DestroyWindow(vd->Hwnd); |
| 1238 | vd->Hwnd = nullptr; |
| 1239 | IM_DELETE(vd); |
| 1240 | } |
| 1241 | viewport->PlatformUserData = viewport->PlatformHandle = nullptr; |
| 1242 | } |
| 1243 | |
| 1244 | static void ImGui_ImplWin32_ShowWindow(ImGuiViewport* viewport) |
| 1245 | { |
| 1246 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1247 | IM_ASSERT(vd->Hwnd != 0); |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | bool avoid_bringing_parent_to_front = vd->HwndParent != NULL && (viewport->Flags & (ImGuiViewportFlags_NoFocusOnAppearing | ImGuiViewportFlags_NoTaskBarIcon)) != 0; |
| 1252 | if (avoid_bringing_parent_to_front) |
| 1253 | ::SetWindowLongPtr(vd->Hwnd, GWLP_HWNDPARENT, (LONG_PTR)nullptr); |
| 1254 | |
| 1255 | if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing) |
| 1256 | ::ShowWindow(vd->Hwnd, SW_SHOWNA); |
| 1257 | else |
| 1258 | ::ShowWindow(vd->Hwnd, SW_SHOW); |
| 1259 | |
| 1260 | |
| 1261 | if (avoid_bringing_parent_to_front) |
| 1262 | ::SetWindowLongPtr(vd->Hwnd, GWLP_HWNDPARENT, (LONG_PTR)vd->HwndParent); |
| 1263 | } |
| 1264 | |
| 1265 | static void ImGui_ImplWin32_UpdateWindow(ImGuiViewport* viewport) |
| 1266 | { |
| 1267 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1268 | IM_ASSERT(vd->Hwnd != 0); |
| 1269 | |
| 1270 | |
| 1271 | |
| 1272 | HWND new_parent = ImGui_ImplWin32_GetHwndFromViewport(viewport->ParentViewport); |
| 1273 | if (new_parent != vd->HwndParent) |
| 1274 | { |
| 1275 | |
| 1276 | |
| 1277 | |
| 1278 | |
| 1279 | |
| 1280 | |
| 1281 | vd->HwndParent = new_parent; |
| 1282 | ::SetWindowLongPtr(vd->Hwnd, GWLP_HWNDPARENT, (LONG_PTR)vd->HwndParent); |
| 1283 | } |
| 1284 | |
| 1285 | |
| 1286 | |
| 1287 | DWORD new_style; |
| 1288 | DWORD new_ex_style; |
| 1289 | ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &new_style, &new_ex_style); |
| 1290 | |
| 1291 | |
| 1292 | if (vd->DwStyle != new_style || vd->DwExStyle != new_ex_style) |
| 1293 | { |
| 1294 | |
| 1295 | bool top_most_changed = (vd->DwExStyle & WS_EX_TOPMOST) != (new_ex_style & WS_EX_TOPMOST); |
| 1296 | HWND insert_after = top_most_changed ? ((viewport->Flags & ImGuiViewportFlags_TopMost) ? HWND_TOPMOST : HWND_NOTOPMOST) : 0; |
| 1297 | UINT swp_flag = top_most_changed ? 0 : SWP_NOZORDER; |
| 1298 | |
| 1299 | |
| 1300 | vd->DwStyle = new_style; |
| 1301 | vd->DwExStyle = new_ex_style; |
| 1302 | ::SetWindowLong(vd->Hwnd, GWL_STYLE, vd->DwStyle); |
| 1303 | ::SetWindowLong(vd->Hwnd, GWL_EXSTYLE, vd->DwExStyle); |
| 1304 | RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) }; |
| 1305 | ImGui_ImplWin32_AdjustWindowRect(viewport, &rect, vd->DwStyle, vd->DwExStyle); |
| 1306 | ::SetWindowPos(vd->Hwnd, insert_after, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, swp_flag | SWP_NOACTIVATE | SWP_FRAMECHANGED); |
| 1307 | ::ShowWindow(vd->Hwnd, SW_SHOWNA); |
| 1308 | viewport->PlatformRequestMove = viewport->PlatformRequestResize = true; |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | static ImVec2 ImGui_ImplWin32_GetWindowPos(ImGuiViewport* viewport) |
| 1313 | { |
| 1314 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1315 | IM_ASSERT(vd->Hwnd != 0); |
| 1316 | POINT pos = { 0, 0 }; |
| 1317 | ::ClientToScreen(vd->Hwnd, &pos); |
| 1318 | return ImVec2((float)pos.x, (float)pos.y); |
| 1319 | } |
| 1320 | |
| 1321 | static void ImGui_ImplWin32_UpdateWin32StyleFromWindow(ImGuiViewport* viewport) |
| 1322 | { |
| 1323 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1324 | vd->DwStyle = ::GetWindowLongW(vd->Hwnd, GWL_STYLE); |
| 1325 | vd->DwExStyle = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE); |
| 1326 | } |
| 1327 | |
| 1328 | static void ImGui_ImplWin32_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos) |
| 1329 | { |
| 1330 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1331 | IM_ASSERT(vd->Hwnd != 0); |
| 1332 | RECT rect = { (LONG)pos.x, (LONG)pos.y, (LONG)pos.x, (LONG)pos.y }; |
| 1333 | if (viewport->Flags & ImGuiViewportFlags_OwnedByApp) |
| 1334 | ImGui_ImplWin32_UpdateWin32StyleFromWindow(viewport); |
| 1335 | ImGui_ImplWin32_AdjustWindowRect(viewport, &rect, vd->DwStyle, vd->DwExStyle); |
| 1336 | ::SetWindowPos(vd->Hwnd, nullptr, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE); |
| 1337 | } |
| 1338 | |
| 1339 | static ImVec2 ImGui_ImplWin32_GetWindowSize(ImGuiViewport* viewport) |
| 1340 | { |
| 1341 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1342 | IM_ASSERT(vd->Hwnd != 0); |
| 1343 | RECT rect; |
| 1344 | ::GetClientRect(vd->Hwnd, &rect); |
| 1345 | return ImVec2(float(rect.right - rect.left), float(rect.bottom - rect.top)); |
| 1346 | } |
| 1347 | |
| 1348 | static void ImGui_ImplWin32_SetWindowSize(ImGuiViewport* viewport, ImVec2 size) |
| 1349 | { |
| 1350 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1351 | IM_ASSERT(vd->Hwnd != 0); |
| 1352 | RECT rect = { 0, 0, (LONG)size.x, (LONG)size.y }; |
| 1353 | if (viewport->Flags & ImGuiViewportFlags_OwnedByApp) |
| 1354 | ImGui_ImplWin32_UpdateWin32StyleFromWindow(viewport); |
| 1355 | ImGui_ImplWin32_AdjustWindowRect(viewport, &rect, vd->DwStyle, vd->DwExStyle); |
| 1356 | ::SetWindowPos(vd->Hwnd, nullptr, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE); |
| 1357 | } |
| 1358 | |
| 1359 | static void ImGui_ImplWin32_SetWindowFocus(ImGuiViewport* viewport) |
| 1360 | { |
| 1361 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1362 | IM_ASSERT(vd->Hwnd != 0); |
| 1363 | ::BringWindowToTop(vd->Hwnd); |
| 1364 | ::SetForegroundWindow(vd->Hwnd); |
| 1365 | ::SetFocus(vd->Hwnd); |
| 1366 | } |
| 1367 | |
| 1368 | static bool ImGui_ImplWin32_GetWindowFocus(ImGuiViewport* viewport) |
| 1369 | { |
| 1370 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1371 | IM_ASSERT(vd->Hwnd != 0); |
| 1372 | return ::GetForegroundWindow() == vd->Hwnd; |
| 1373 | } |
| 1374 | |
| 1375 | static bool ImGui_ImplWin32_GetWindowMinimized(ImGuiViewport* viewport) |
| 1376 | { |
| 1377 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1378 | IM_ASSERT(vd->Hwnd != 0); |
| 1379 | return ::IsIconic(vd->Hwnd) != 0; |
| 1380 | } |
| 1381 | |
| 1382 | static void ImGui_ImplWin32_SetWindowTitle(ImGuiViewport* viewport, const char* title) |
| 1383 | { |
| 1384 | |
| 1385 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1386 | IM_ASSERT(vd->Hwnd != 0); |
| 1387 | int n = ::MultiByteToWideChar(CP_UTF8, 0, title, -1, nullptr, 0); |
| 1388 | ImVector<wchar_t> title_w; |
| 1389 | title_w.resize(n); |
| 1390 | ::MultiByteToWideChar(CP_UTF8, 0, title, -1, title_w.Data, n); |
| 1391 | |
| 1392 | |
| 1393 | |
| 1394 | |
| 1395 | |
| 1396 | ::DefWindowProcW(vd->Hwnd, WM_SETTEXT, 0, (LPARAM)title_w.Data); |
| 1397 | } |
| 1398 | |
| 1399 | static void ImGui_ImplWin32_SetWindowAlpha(ImGuiViewport* viewport, float alpha) |
| 1400 | { |
| 1401 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1402 | IM_ASSERT(vd->Hwnd != 0); |
| 1403 | IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f); |
| 1404 | if (alpha < 1.0f) |
| 1405 | { |
| 1406 | DWORD ex_style = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE) | WS_EX_LAYERED; |
| 1407 | ::SetWindowLongW(vd->Hwnd, GWL_EXSTYLE, ex_style); |
| 1408 | ::SetLayeredWindowAttributes(vd->Hwnd, 0, (BYTE)(255 * alpha), LWA_ALPHA); |
| 1409 | } |
| 1410 | else |
| 1411 | { |
| 1412 | DWORD ex_style = ::GetWindowLongW(vd->Hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED; |
| 1413 | ::SetWindowLongW(vd->Hwnd, GWL_EXSTYLE, ex_style); |
| 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | static float ImGui_ImplWin32_GetWindowDpiScale(ImGuiViewport* viewport) |
| 1418 | { |
| 1419 | ImGui_ImplWin32_ViewportData* vd = (ImGui_ImplWin32_ViewportData*)viewport->PlatformUserData; |
| 1420 | IM_ASSERT(vd->Hwnd != 0); |
| 1421 | return ImGui_ImplWin32_GetDpiScaleForHwnd(vd->Hwnd); |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | static void ImGui_ImplWin32_OnChangedViewport(ImGuiViewport* viewport) |
| 1426 | { |
| 1427 | (void)viewport; |
| 1428 | #if 0 |
| 1429 | ImGuiStyle default_style; |
| 1430 | |
| 1431 | |
| 1432 | |
| 1433 | |
| 1434 | default_style.ScaleAllSizes(viewport->DpiScale); |
| 1435 | ImGuiStyle& style = ImGui::GetStyle(); |
| 1436 | style = default_style; |
| 1437 | #endif |
| 1438 | } |
| 1439 | |
| 1440 | namespace ImGui { extern ImGuiIO& GetIO(ImGuiContext*); extern ImGuiPlatformIO& GetPlatformIO(ImGuiContext*); } |
| 1441 | |
| 1442 | static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) |
| 1443 | { |
| 1444 | |
| 1445 | ImGuiContext* ctx = (ImGuiContext*)::GetPropA(hWnd, "IMGUI_CONTEXT"); |
| 1446 | if (ctx == NULL) |
| 1447 | return ::DefWindowProcW(hWnd, msg, wParam, lParam); |
| 1448 | |
| 1449 | ImGuiIO& io = ImGui::GetIO(ctx); |
| 1450 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(ctx); |
| 1451 | LRESULT result = 0; |
| 1452 | if (ImGui_ImplWin32_WndProcHandlerEx(hWnd, msg, wParam, lParam, io)) |
| 1453 | result = 1; |
| 1454 | else if (ImGuiViewport* viewport = ImGui_ImplWin32_FindViewportByPlatformHandle(platform_io, hWnd)) |
| 1455 | { |
| 1456 | switch (msg) |
| 1457 | { |
| 1458 | case WM_CLOSE: |
| 1459 | viewport->PlatformRequestClose = true; |
| 1460 | return 0; |
| 1461 | case WM_MOVE: |
| 1462 | viewport->PlatformRequestMove = true; |
| 1463 | break; |
| 1464 | case WM_SIZE: |
| 1465 | viewport->PlatformRequestResize = true; |
| 1466 | break; |
| 1467 | case WM_MOUSEACTIVATE: |
| 1468 | if (viewport->Flags & ImGuiViewportFlags_NoFocusOnClick) |
| 1469 | result = MA_NOACTIVATE; |
| 1470 | break; |
| 1471 | case WM_NCHITTEST: |
| 1472 | |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | if (viewport->Flags & ImGuiViewportFlags_NoInputs) |
| 1477 | result = HTTRANSPARENT; |
| 1478 | break; |
| 1479 | } |
| 1480 | } |
| 1481 | if (result == 0) |
| 1482 | result = ::DefWindowProcW(hWnd, msg, wParam, lParam); |
| 1483 | return result; |
| 1484 | } |
| 1485 | |
| 1486 | static void ImGui_ImplWin32_InitMultiViewportSupport(bool platform_has_own_dc) |
| 1487 | { |
| 1488 | WNDCLASSEXW wcex; |
| 1489 | wcex.cbSize = sizeof(WNDCLASSEXW); |
| 1490 | wcex.style = CS_HREDRAW | CS_VREDRAW | (platform_has_own_dc ? CS_OWNDC : 0); |
| 1491 | wcex.lpfnWndProc = ImGui_ImplWin32_WndProcHandler_PlatformWindow; |
| 1492 | wcex.cbClsExtra = 0; |
| 1493 | wcex.cbWndExtra = 0; |
| 1494 | wcex.hInstance = ::GetModuleHandle(nullptr); |
| 1495 | wcex.hIcon = nullptr; |
| 1496 | wcex.hCursor = nullptr; |
| 1497 | wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1); |
| 1498 | wcex.lpszMenuName = nullptr; |
| 1499 | wcex.lpszClassName = L"ImGui Platform"; |
| 1500 | wcex.hIconSm = nullptr; |
| 1501 | ::RegisterClassExW(&wcex); |
| 1502 | |
| 1503 | ImGui_ImplWin32_UpdateMonitors(); |
| 1504 | |
| 1505 | |
| 1506 | ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); |
| 1507 | platform_io.Platform_CreateWindow = ImGui_ImplWin32_CreateWindow; |
| 1508 | platform_io.Platform_DestroyWindow = ImGui_ImplWin32_DestroyWindow; |
| 1509 | platform_io.Platform_ShowWindow = ImGui_ImplWin32_ShowWindow; |
| 1510 | platform_io.Platform_SetWindowPos = ImGui_ImplWin32_SetWindowPos; |
| 1511 | platform_io.Platform_GetWindowPos = ImGui_ImplWin32_GetWindowPos; |
| 1512 | platform_io.Platform_SetWindowSize = ImGui_ImplWin32_SetWindowSize; |
| 1513 | platform_io.Platform_GetWindowSize = ImGui_ImplWin32_GetWindowSize; |
| 1514 | platform_io.Platform_SetWindowFocus = ImGui_ImplWin32_SetWindowFocus; |
| 1515 | platform_io.Platform_GetWindowFocus = ImGui_ImplWin32_GetWindowFocus; |
| 1516 | platform_io.Platform_GetWindowMinimized = ImGui_ImplWin32_GetWindowMinimized; |
| 1517 | platform_io.Platform_SetWindowTitle = ImGui_ImplWin32_SetWindowTitle; |
| 1518 | platform_io.Platform_SetWindowAlpha = ImGui_ImplWin32_SetWindowAlpha; |
| 1519 | platform_io.Platform_UpdateWindow = ImGui_ImplWin32_UpdateWindow; |
| 1520 | platform_io.Platform_GetWindowDpiScale = ImGui_ImplWin32_GetWindowDpiScale; |
| 1521 | platform_io.Platform_OnChangedViewport = ImGui_ImplWin32_OnChangedViewport; |
| 1522 | |
| 1523 | |
| 1524 | |
| 1525 | ImGuiViewport* main_viewport = ImGui::GetMainViewport(); |
| 1526 | ImGui_ImplWin32_Data* bd = ImGui_ImplWin32_GetBackendData(); |
| 1527 | ImGui_ImplWin32_ViewportData* vd = IM_NEW(ImGui_ImplWin32_ViewportData)(); |
| 1528 | vd->Hwnd = bd->hWnd; |
| 1529 | vd->HwndOwned = false; |
| 1530 | main_viewport->PlatformUserData = vd; |
| 1531 | } |
| 1532 | |
| 1533 | static void ImGui_ImplWin32_ShutdownMultiViewportSupport() |
| 1534 | { |
| 1535 | ::UnregisterClassW(L"ImGui Platform", ::GetModuleHandle(nullptr)); |
| 1536 | ImGui::DestroyPlatformWindows(); |
| 1537 | } |
| 1538 | |
| 1539 | |
| 1540 | |
| 1541 | #if defined(__GNUC__) |
| 1542 | #pragma GCC diagnostic pop |
| 1543 | #endif |
| 1544 | #if defined(__clang__) |
| 1545 | #pragma clang diagnostic pop |
| 1546 | #endif |
| 1547 | |
| 1548 | #endif |