imgui_impl_win32.cpp 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. // dear imgui: Platform Backend for Windows (standard windows API for 32 and 64 bits applications)
  2. // This needs to be used along with a Renderer (e.g. DirectX11, OpenGL3, Vulkan..)
  3. // Implemented features:
  4. // [X] Platform: Clipboard support (for Win32 this is actually part of core dear imgui)
  5. // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange'.
  6. // [X] Platform: Keyboard arrays indexed using VK_* Virtual Key Codes, e.g. ImGui::IsKeyPressed(VK_SPACE).
  7. // [X] Platform: Gamepad support. Enabled with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  8. // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
  9. // You can copy and use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  10. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
  11. // Read online: https://github.com/ocornut/imgui/tree/master/docs
  12. #include "imgui.h"
  13. #include "imgui_impl_win32.h"
  14. #ifndef WIN32_LEAN_AND_MEAN
  15. #define WIN32_LEAN_AND_MEAN
  16. #endif
  17. #include <windows.h>
  18. #include <tchar.h>
  19. #include <dwmapi.h>
  20. // Configuration flags to add in your imconfig.h file:
  21. //#define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD // Disable gamepad support (this used to be meaningful before <1.81) but we know load XInput dynamically so the option is less relevant now.
  22. // Using XInput for gamepad (will load DLL dynamically)
  23. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  24. #include <XInput.h>
  25. typedef DWORD (WINAPI *PFN_XInputGetCapabilities)(DWORD, DWORD, XINPUT_CAPABILITIES*);
  26. typedef DWORD (WINAPI *PFN_XInputGetState)(DWORD, XINPUT_STATE*);
  27. #endif
  28. // CHANGELOG
  29. // (minor and older changes stripped away, please see git history for details)
  30. // 2021-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  31. // 2021-02-18: Added ImGui_ImplWin32_EnableAlphaCompositing(). Non Visual Studio users will need to link with dwmapi.lib (MinGW/gcc: use -ldwmapi).
  32. // 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.
  33. // 2021-01-25: Inputs: Dynamically loading XInput DLL.
  34. // 2020-12-04: Misc: Fixed setting of io.DisplaySize to invalid/uninitialized data when after hwnd has been closed.
  35. // 2020-03-03: Inputs: Calling AddInputCharacterUTF16() to support surrogate pairs leading to codepoint >= 0x10000 (for more complete CJK inputs)
  36. // 2020-02-17: Added ImGui_ImplWin32_EnableDpiAwareness(), ImGui_ImplWin32_GetDpiScaleForHwnd(), ImGui_ImplWin32_GetDpiScaleForMonitor() helper functions.
  37. // 2020-01-14: Inputs: Added support for #define IMGUI_IMPL_WIN32_DISABLE_GAMEPAD/IMGUI_IMPL_WIN32_DISABLE_LINKING_XINPUT.
  38. // 2019-12-05: Inputs: Added support for ImGuiMouseCursor_NotAllowed mouse cursor.
  39. // 2019-05-11: Inputs: Don't filter value from WM_CHAR before calling AddInputCharacter().
  40. // 2019-01-17: Misc: Using GetForegroundWindow()+IsChild() instead of GetActiveWindow() to be compatible with windows created in a different thread or parent.
  41. // 2019-01-17: Inputs: Added support for mouse buttons 4 and 5 via WM_XBUTTON* messages.
  42. // 2019-01-15: Inputs: Added support for XInput gamepads (if ImGuiConfigFlags_NavEnableGamepad is set by user application).
  43. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
  44. // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
  45. // 2018-06-10: Inputs: Fixed handling of mouse wheel messages to support fine position messages (typically sent by track-pads).
  46. // 2018-06-08: Misc: Extracted imgui_impl_win32.cpp/.h away from the old combined DX9/DX10/DX11/DX12 examples.
  47. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  48. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling).
  49. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  50. // 2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
  51. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  52. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  53. // 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert.
  54. // 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag.
  55. // 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read.
  56. // 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging.
  57. // 2016-11-12: Inputs: Only call Win32 ::SetCursor(NULL) when io.MouseDrawCursor is set.
  58. // Win32 Data
  59. static HWND g_hWnd = NULL;
  60. static INT64 g_Time = 0;
  61. static INT64 g_TicksPerSecond = 0;
  62. static ImGuiMouseCursor g_LastMouseCursor = ImGuiMouseCursor_COUNT;
  63. static bool g_HasGamepad = false;
  64. static bool g_WantUpdateHasGamepad = true;
  65. static bool g_WantUpdateMonitors = true;
  66. // Forward Declarations
  67. static void ImGui_ImplWin32_InitPlatformInterface();
  68. static void ImGui_ImplWin32_ShutdownPlatformInterface();
  69. static void ImGui_ImplWin32_UpdateMonitors();
  70. // XInput DLL and functions
  71. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  72. static HMODULE g_XInputDLL = NULL;
  73. static PFN_XInputGetCapabilities g_XInputGetCapabilities = NULL;
  74. static PFN_XInputGetState g_XInputGetState = NULL;
  75. #endif
  76. // Functions
  77. bool ImGui_ImplWin32_Init(void* hwnd)
  78. {
  79. if (!::QueryPerformanceFrequency((LARGE_INTEGER*)&g_TicksPerSecond))
  80. return false;
  81. if (!::QueryPerformanceCounter((LARGE_INTEGER*)&g_Time))
  82. return false;
  83. // Setup backend capabilities flags
  84. ImGuiIO& io = ImGui::GetIO();
  85. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  86. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  87. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  88. io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
  89. io.BackendPlatformName = "imgui_impl_win32";
  90. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  91. g_hWnd = (HWND)hwnd;
  92. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  93. main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (void*)g_hWnd;
  94. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  95. ImGui_ImplWin32_InitPlatformInterface();
  96. // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array that we will update during the application lifetime.
  97. io.KeyMap[ImGuiKey_Tab] = VK_TAB;
  98. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  99. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  100. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  101. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  102. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  103. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  104. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  105. io.KeyMap[ImGuiKey_End] = VK_END;
  106. io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
  107. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  108. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  109. io.KeyMap[ImGuiKey_Space] = VK_SPACE;
  110. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  111. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  112. io.KeyMap[ImGuiKey_KeyPadEnter] = VK_RETURN;
  113. io.KeyMap[ImGuiKey_A] = 'A';
  114. io.KeyMap[ImGuiKey_C] = 'C';
  115. io.KeyMap[ImGuiKey_V] = 'V';
  116. io.KeyMap[ImGuiKey_X] = 'X';
  117. io.KeyMap[ImGuiKey_Y] = 'Y';
  118. io.KeyMap[ImGuiKey_Z] = 'Z';
  119. // Dynamically load XInput library
  120. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  121. const char* xinput_dll_names[] =
  122. {
  123. "xinput1_4.dll", // Windows 8+
  124. "xinput1_3.dll", // DirectX SDK
  125. "xinput9_1_0.dll", // Windows Vista, Windows 7
  126. "xinput1_2.dll", // DirectX SDK
  127. "xinput1_1.dll" // DirectX SDK
  128. };
  129. for (int n = 0; n < IM_ARRAYSIZE(xinput_dll_names); n++)
  130. if (HMODULE dll = ::LoadLibraryA(xinput_dll_names[n]))
  131. {
  132. g_XInputDLL = dll;
  133. g_XInputGetCapabilities = (PFN_XInputGetCapabilities)::GetProcAddress(dll, "XInputGetCapabilities");
  134. g_XInputGetState = (PFN_XInputGetState)::GetProcAddress(dll, "XInputGetState");
  135. break;
  136. }
  137. #endif // IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  138. return true;
  139. }
  140. void ImGui_ImplWin32_Shutdown()
  141. {
  142. ImGui_ImplWin32_ShutdownPlatformInterface();
  143. // Unload XInput library
  144. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  145. if (g_XInputDLL)
  146. ::FreeLibrary(g_XInputDLL);
  147. g_XInputDLL = NULL;
  148. g_XInputGetCapabilities = NULL;
  149. g_XInputGetState = NULL;
  150. #endif // IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  151. g_hWnd = NULL;
  152. g_Time = 0;
  153. g_TicksPerSecond = 0;
  154. g_LastMouseCursor = ImGuiMouseCursor_COUNT;
  155. g_HasGamepad = false;
  156. g_WantUpdateHasGamepad = true;
  157. }
  158. static bool ImGui_ImplWin32_UpdateMouseCursor()
  159. {
  160. ImGuiIO& io = ImGui::GetIO();
  161. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  162. return false;
  163. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  164. if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
  165. {
  166. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  167. ::SetCursor(NULL);
  168. }
  169. else
  170. {
  171. // Show OS mouse cursor
  172. LPTSTR win32_cursor = IDC_ARROW;
  173. switch (imgui_cursor)
  174. {
  175. case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break;
  176. case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break;
  177. case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break;
  178. case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break;
  179. case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break;
  180. case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break;
  181. case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break;
  182. case ImGuiMouseCursor_Hand: win32_cursor = IDC_HAND; break;
  183. case ImGuiMouseCursor_NotAllowed: win32_cursor = IDC_NO; break;
  184. }
  185. ::SetCursor(::LoadCursor(NULL, win32_cursor));
  186. }
  187. return true;
  188. }
  189. // This code supports multi-viewports (multiple OS Windows mapped into different Dear ImGui viewports)
  190. // Because of that, it is a little more complicated than your typical single-viewport binding code!
  191. static void ImGui_ImplWin32_UpdateMousePos()
  192. {
  193. ImGuiIO& io = ImGui::GetIO();
  194. // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  195. // (When multi-viewports are enabled, all imgui positions are same as OS positions)
  196. if (io.WantSetMousePos)
  197. {
  198. POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
  199. if ((io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) != 0 || ::ClientToScreen(g_hWnd, &pos))
  200. ::ClientToScreen(g_hWnd, &pos);
  201. ::SetCursorPos(pos.x, pos.y);
  202. }
  203. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  204. io.MouseHoveredViewport = 0;
  205. // Set imgui mouse position
  206. POINT mouse_screen_pos;
  207. if (!::GetCursorPos(&mouse_screen_pos))
  208. return;
  209. if (HWND focused_hwnd = ::GetForegroundWindow())
  210. {
  211. if (::IsChild(focused_hwnd, g_hWnd))
  212. focused_hwnd = g_hWnd;
  213. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  214. {
  215. // 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)
  216. // This is the position you can get with GetCursorPos(). In theory adding viewport->Pos is also the reverse operation of doing ScreenToClient().
  217. if (ImGui::FindViewportByPlatformHandle((void*)focused_hwnd) != NULL)
  218. io.MousePos = ImVec2((float)mouse_screen_pos.x, (float)mouse_screen_pos.y);
  219. }
  220. else
  221. {
  222. // 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.)
  223. // This is the position you can get with GetCursorPos() + ScreenToClient() or from WM_MOUSEMOVE.
  224. if (focused_hwnd == g_hWnd)
  225. {
  226. POINT mouse_client_pos = mouse_screen_pos;
  227. ::ScreenToClient(focused_hwnd, &mouse_client_pos);
  228. io.MousePos = ImVec2((float)mouse_client_pos.x, (float)mouse_client_pos.y);
  229. }
  230. }
  231. }
  232. // (Optional) When using multiple viewports: set io.MouseHoveredViewport to the viewport the OS mouse cursor is hovering.
  233. // Important: this information is not easy to provide and many high-level windowing library won't be able to provide it correctly, because
  234. // - This is _ignoring_ viewports with the ImGuiViewportFlags_NoInputs flag (pass-through windows).
  235. // - This is _regardless_ of whether another viewport is focused or being dragged from.
  236. // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, imgui will ignore this field and infer the information by relying on the
  237. // rectangles and last focused time of every viewports it knows about. It will be unaware of foreign windows that may be sitting between or over your windows.
  238. if (HWND hovered_hwnd = ::WindowFromPoint(mouse_screen_pos))
  239. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hovered_hwnd))
  240. if ((viewport->Flags & ImGuiViewportFlags_NoInputs) == 0) // FIXME: We still get our NoInputs window with WM_NCHITTEST/HTTRANSPARENT code when decorated?
  241. io.MouseHoveredViewport = viewport->ID;
  242. }
  243. // Gamepad navigation mapping
  244. static void ImGui_ImplWin32_UpdateGamepads()
  245. {
  246. #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  247. ImGuiIO& io = ImGui::GetIO();
  248. memset(io.NavInputs, 0, sizeof(io.NavInputs));
  249. if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
  250. return;
  251. // Calling XInputGetState() every frame on disconnected gamepads is unfortunately too slow.
  252. // Instead we refresh gamepad availability by calling XInputGetCapabilities() _only_ after receiving WM_DEVICECHANGE.
  253. if (g_WantUpdateHasGamepad)
  254. {
  255. XINPUT_CAPABILITIES caps;
  256. g_HasGamepad = g_XInputGetCapabilities ? (g_XInputGetCapabilities(0, XINPUT_FLAG_GAMEPAD, &caps) == ERROR_SUCCESS) : false;
  257. g_WantUpdateHasGamepad = false;
  258. }
  259. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  260. XINPUT_STATE xinput_state;
  261. if (g_HasGamepad && g_XInputGetState && g_XInputGetState(0, &xinput_state) == ERROR_SUCCESS)
  262. {
  263. const XINPUT_GAMEPAD& gamepad = xinput_state.Gamepad;
  264. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  265. #define MAP_BUTTON(NAV_NO, BUTTON_ENUM) { io.NavInputs[NAV_NO] = (gamepad.wButtons & BUTTON_ENUM) ? 1.0f : 0.0f; }
  266. #define MAP_ANALOG(NAV_NO, VALUE, V0, V1) { float vn = (float)(VALUE - V0) / (float)(V1 - V0); if (vn > 1.0f) vn = 1.0f; if (vn > 0.0f && io.NavInputs[NAV_NO] < vn) io.NavInputs[NAV_NO] = vn; }
  267. MAP_BUTTON(ImGuiNavInput_Activate, XINPUT_GAMEPAD_A); // Cross / A
  268. MAP_BUTTON(ImGuiNavInput_Cancel, XINPUT_GAMEPAD_B); // Circle / B
  269. MAP_BUTTON(ImGuiNavInput_Menu, XINPUT_GAMEPAD_X); // Square / X
  270. MAP_BUTTON(ImGuiNavInput_Input, XINPUT_GAMEPAD_Y); // Triangle / Y
  271. MAP_BUTTON(ImGuiNavInput_DpadLeft, XINPUT_GAMEPAD_DPAD_LEFT); // D-Pad Left
  272. MAP_BUTTON(ImGuiNavInput_DpadRight, XINPUT_GAMEPAD_DPAD_RIGHT); // D-Pad Right
  273. MAP_BUTTON(ImGuiNavInput_DpadUp, XINPUT_GAMEPAD_DPAD_UP); // D-Pad Up
  274. MAP_BUTTON(ImGuiNavInput_DpadDown, XINPUT_GAMEPAD_DPAD_DOWN); // D-Pad Down
  275. MAP_BUTTON(ImGuiNavInput_FocusPrev, XINPUT_GAMEPAD_LEFT_SHOULDER); // L1 / LB
  276. MAP_BUTTON(ImGuiNavInput_FocusNext, XINPUT_GAMEPAD_RIGHT_SHOULDER); // R1 / RB
  277. MAP_BUTTON(ImGuiNavInput_TweakSlow, XINPUT_GAMEPAD_LEFT_SHOULDER); // L1 / LB
  278. MAP_BUTTON(ImGuiNavInput_TweakFast, XINPUT_GAMEPAD_RIGHT_SHOULDER); // R1 / RB
  279. MAP_ANALOG(ImGuiNavInput_LStickLeft, gamepad.sThumbLX, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32768);
  280. MAP_ANALOG(ImGuiNavInput_LStickRight, gamepad.sThumbLX, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
  281. MAP_ANALOG(ImGuiNavInput_LStickUp, gamepad.sThumbLY, +XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, +32767);
  282. MAP_ANALOG(ImGuiNavInput_LStickDown, gamepad.sThumbLY, -XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE, -32767);
  283. #undef MAP_BUTTON
  284. #undef MAP_ANALOG
  285. }
  286. #endif // #ifndef IMGUI_IMPL_WIN32_DISABLE_GAMEPAD
  287. }
  288. static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR monitor, HDC, LPRECT, LPARAM)
  289. {
  290. MONITORINFO info = {};
  291. info.cbSize = sizeof(MONITORINFO);
  292. if (!::GetMonitorInfo(monitor, &info))
  293. return TRUE;
  294. ImGuiPlatformMonitor imgui_monitor;
  295. imgui_monitor.MainPos = ImVec2((float)info.rcMonitor.left, (float)info.rcMonitor.top);
  296. imgui_monitor.MainSize = ImVec2((float)(info.rcMonitor.right - info.rcMonitor.left), (float)(info.rcMonitor.bottom - info.rcMonitor.top));
  297. imgui_monitor.WorkPos = ImVec2((float)info.rcWork.left, (float)info.rcWork.top);
  298. imgui_monitor.WorkSize = ImVec2((float)(info.rcWork.right - info.rcWork.left), (float)(info.rcWork.bottom - info.rcWork.top));
  299. imgui_monitor.DpiScale = ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
  300. ImGuiPlatformIO& io = ImGui::GetPlatformIO();
  301. if (info.dwFlags & MONITORINFOF_PRIMARY)
  302. io.Monitors.push_front(imgui_monitor);
  303. else
  304. io.Monitors.push_back(imgui_monitor);
  305. return TRUE;
  306. }
  307. static void ImGui_ImplWin32_UpdateMonitors()
  308. {
  309. ImGui::GetPlatformIO().Monitors.resize(0);
  310. ::EnumDisplayMonitors(NULL, NULL, ImGui_ImplWin32_UpdateMonitors_EnumFunc, NULL);
  311. g_WantUpdateMonitors = false;
  312. }
  313. void ImGui_ImplWin32_NewFrame()
  314. {
  315. ImGuiIO& io = ImGui::GetIO();
  316. IM_ASSERT(io.Fonts->IsBuilt() && "Font atlas not built! It is generally built by the renderer backend. Missing call to renderer _NewFrame() function? e.g. ImGui_ImplOpenGL3_NewFrame().");
  317. // Setup display size (every frame to accommodate for window resizing)
  318. RECT rect = { 0, 0, 0, 0 };
  319. ::GetClientRect(g_hWnd, &rect);
  320. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  321. if (g_WantUpdateMonitors)
  322. ImGui_ImplWin32_UpdateMonitors();
  323. // Setup time step
  324. INT64 current_time = 0;
  325. ::QueryPerformanceCounter((LARGE_INTEGER*)&current_time);
  326. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  327. g_Time = current_time;
  328. // Read keyboard modifiers inputs
  329. io.KeyCtrl = (::GetKeyState(VK_CONTROL) & 0x8000) != 0;
  330. io.KeyShift = (::GetKeyState(VK_SHIFT) & 0x8000) != 0;
  331. io.KeyAlt = (::GetKeyState(VK_MENU) & 0x8000) != 0;
  332. io.KeySuper = false;
  333. // io.KeysDown[], io.MousePos, io.MouseDown[], io.MouseWheel: filled by the WndProc handler below.
  334. // Update OS mouse position
  335. ImGui_ImplWin32_UpdateMousePos();
  336. // Update OS mouse cursor with the cursor requested by imgui
  337. ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  338. if (g_LastMouseCursor != mouse_cursor)
  339. {
  340. g_LastMouseCursor = mouse_cursor;
  341. ImGui_ImplWin32_UpdateMouseCursor();
  342. }
  343. // Update game controllers (if enabled and available)
  344. ImGui_ImplWin32_UpdateGamepads();
  345. }
  346. // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
  347. #ifndef WM_MOUSEHWHEEL
  348. #define WM_MOUSEHWHEEL 0x020E
  349. #endif
  350. #ifndef DBT_DEVNODES_CHANGED
  351. #define DBT_DEVNODES_CHANGED 0x0007
  352. #endif
  353. // Win32 message handler (process Win32 mouse/keyboard inputs, etc.)
  354. // Call from your application's message handler.
  355. // When implementing your own backend, you can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if Dear ImGui wants to use your inputs.
  356. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  357. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  358. // Generally you may always pass all inputs to Dear ImGui, and hide them from your application based on those two flags.
  359. // 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.
  360. // 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.
  361. #if 0
  362. // Copy this line into your .cpp file to forward declare the function.
  363. extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  364. #endif
  365. IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  366. {
  367. if (ImGui::GetCurrentContext() == NULL)
  368. return 0;
  369. ImGuiIO& io = ImGui::GetIO();
  370. switch (msg)
  371. {
  372. case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
  373. case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
  374. case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
  375. case WM_XBUTTONDOWN: case WM_XBUTTONDBLCLK:
  376. {
  377. int button = 0;
  378. if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) { button = 0; }
  379. if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) { button = 1; }
  380. if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) { button = 2; }
  381. if (msg == WM_XBUTTONDOWN || msg == WM_XBUTTONDBLCLK) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
  382. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
  383. ::SetCapture(hwnd);
  384. io.MouseDown[button] = true;
  385. return 0;
  386. }
  387. case WM_LBUTTONUP:
  388. case WM_RBUTTONUP:
  389. case WM_MBUTTONUP:
  390. case WM_XBUTTONUP:
  391. {
  392. int button = 0;
  393. if (msg == WM_LBUTTONUP) { button = 0; }
  394. if (msg == WM_RBUTTONUP) { button = 1; }
  395. if (msg == WM_MBUTTONUP) { button = 2; }
  396. if (msg == WM_XBUTTONUP) { button = (GET_XBUTTON_WPARAM(wParam) == XBUTTON1) ? 3 : 4; }
  397. io.MouseDown[button] = false;
  398. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
  399. ::ReleaseCapture();
  400. return 0;
  401. }
  402. case WM_MOUSEWHEEL:
  403. io.MouseWheel += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
  404. return 0;
  405. case WM_MOUSEHWHEEL:
  406. io.MouseWheelH += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
  407. return 0;
  408. case WM_KEYDOWN:
  409. case WM_SYSKEYDOWN:
  410. if (wParam < 256)
  411. io.KeysDown[wParam] = 1;
  412. return 0;
  413. case WM_KEYUP:
  414. case WM_SYSKEYUP:
  415. if (wParam < 256)
  416. io.KeysDown[wParam] = 0;
  417. return 0;
  418. case WM_CHAR:
  419. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  420. if (wParam > 0 && wParam < 0x10000)
  421. io.AddInputCharacterUTF16((unsigned short)wParam);
  422. return 0;
  423. case WM_SETCURSOR:
  424. if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor())
  425. return 1;
  426. return 0;
  427. case WM_DEVICECHANGE:
  428. if ((UINT)wParam == DBT_DEVNODES_CHANGED)
  429. g_WantUpdateHasGamepad = true;
  430. return 0;
  431. case WM_DISPLAYCHANGE:
  432. g_WantUpdateMonitors = true;
  433. return 0;
  434. }
  435. return 0;
  436. }
  437. //--------------------------------------------------------------------------------------------------------
  438. // DPI-related helpers (optional)
  439. //--------------------------------------------------------------------------------------------------------
  440. // - Use to enable DPI awareness without having to create an application manifest.
  441. // - Your own app may already do this via a manifest or explicit calls. This is mostly useful for our examples/ apps.
  442. // - In theory we could call simple functions from Windows SDK such as SetProcessDPIAware(), SetProcessDpiAwareness(), etc.
  443. // but most of the functions provided by Microsoft require Windows 8.1/10+ SDK at compile time and Windows 8/10+ at runtime,
  444. // neither we want to require the user to have. So we dynamically select and load those functions to avoid dependencies.
  445. //---------------------------------------------------------------------------------------------------------
  446. // This is the scheme successfully used by GLFW (from which we borrowed some of the code) and other apps aiming to be highly portable.
  447. // ImGui_ImplWin32_EnableDpiAwareness() is just a helper called by main.cpp, we don't call it automatically.
  448. // If you are trying to implement your own backend for your own engine, you may ignore that noise.
  449. //---------------------------------------------------------------------------------------------------------
  450. // Implement some of the functions and types normally declared in recent Windows SDK.
  451. #if !defined(_versionhelpers_H_INCLUDED_) && !defined(_INC_VERSIONHELPERS)
  452. static BOOL IsWindowsVersionOrGreater(WORD major, WORD minor, WORD sp)
  453. {
  454. OSVERSIONINFOEXW osvi = { sizeof(osvi), major, minor, 0, 0, { 0 }, sp, 0, 0, 0, 0 };
  455. DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR;
  456. ULONGLONG cond = ::VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL);
  457. cond = ::VerSetConditionMask(cond, VER_MINORVERSION, VER_GREATER_EQUAL);
  458. cond = ::VerSetConditionMask(cond, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
  459. return ::VerifyVersionInfoW(&osvi, mask, cond);
  460. }
  461. #define IsWindowsVistaOrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0600), LOBYTE(0x0600), 0) // _WIN32_WINNT_VISTA
  462. #define IsWindows8OrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0602), LOBYTE(0x0602), 0) // _WIN32_WINNT_WIN8
  463. #define IsWindows8Point1OrGreater() IsWindowsVersionOrGreater(HIBYTE(0x0603), LOBYTE(0x0603), 0) // _WIN32_WINNT_WINBLUE
  464. #endif
  465. #ifndef DPI_ENUMS_DECLARED
  466. typedef enum { PROCESS_DPI_UNAWARE = 0, PROCESS_SYSTEM_DPI_AWARE = 1, PROCESS_PER_MONITOR_DPI_AWARE = 2 } PROCESS_DPI_AWARENESS;
  467. typedef enum { MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2, MDT_DEFAULT = MDT_EFFECTIVE_DPI } MONITOR_DPI_TYPE;
  468. #endif
  469. #ifndef _DPI_AWARENESS_CONTEXTS_
  470. DECLARE_HANDLE(DPI_AWARENESS_CONTEXT);
  471. #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE (DPI_AWARENESS_CONTEXT)-3
  472. #endif
  473. #ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
  474. #define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 (DPI_AWARENESS_CONTEXT)-4
  475. #endif
  476. typedef HRESULT(WINAPI* PFN_SetProcessDpiAwareness)(PROCESS_DPI_AWARENESS); // Shcore.lib + dll, Windows 8.1+
  477. typedef HRESULT(WINAPI* PFN_GetDpiForMonitor)(HMONITOR, MONITOR_DPI_TYPE, UINT*, UINT*); // Shcore.lib + dll, Windows 8.1+
  478. typedef DPI_AWARENESS_CONTEXT(WINAPI* PFN_SetThreadDpiAwarenessContext)(DPI_AWARENESS_CONTEXT); // User32.lib + dll, Windows 10 v1607+ (Creators Update)
  479. // Helper function to enable DPI awareness without setting up a manifest
  480. void ImGui_ImplWin32_EnableDpiAwareness()
  481. {
  482. // Make sure monitors will be updated with latest correct scaling
  483. g_WantUpdateMonitors = true;
  484. // if (IsWindows10OrGreater()) // This needs a manifest to succeed. Instead we try to grab the function pointer!
  485. {
  486. static HINSTANCE user32_dll = ::LoadLibraryA("user32.dll"); // Reference counted per-process
  487. if (PFN_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContextFn = (PFN_SetThreadDpiAwarenessContext)::GetProcAddress(user32_dll, "SetThreadDpiAwarenessContext"))
  488. {
  489. SetThreadDpiAwarenessContextFn(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
  490. return;
  491. }
  492. }
  493. if (IsWindows8Point1OrGreater())
  494. {
  495. static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
  496. if (PFN_SetProcessDpiAwareness SetProcessDpiAwarenessFn = (PFN_SetProcessDpiAwareness)::GetProcAddress(shcore_dll, "SetProcessDpiAwareness"))
  497. {
  498. SetProcessDpiAwarenessFn(PROCESS_PER_MONITOR_DPI_AWARE);
  499. return;
  500. }
  501. }
  502. #if _WIN32_WINNT >= 0x0600
  503. ::SetProcessDPIAware();
  504. #endif
  505. }
  506. #if defined(_MSC_VER) && !defined(NOGDI)
  507. #pragma comment(lib, "gdi32") // Link with gdi32.lib for GetDeviceCaps(). MinGW will require linking with '-lgdi32'
  508. #endif
  509. float ImGui_ImplWin32_GetDpiScaleForMonitor(void* monitor)
  510. {
  511. UINT xdpi = 96, ydpi = 96;
  512. static BOOL bIsWindows8Point1OrGreater = IsWindows8Point1OrGreater();
  513. if (bIsWindows8Point1OrGreater)
  514. {
  515. static HINSTANCE shcore_dll = ::LoadLibraryA("shcore.dll"); // Reference counted per-process
  516. if (PFN_GetDpiForMonitor GetDpiForMonitorFn = (PFN_GetDpiForMonitor)::GetProcAddress(shcore_dll, "GetDpiForMonitor"))
  517. GetDpiForMonitorFn((HMONITOR)monitor, MDT_EFFECTIVE_DPI, &xdpi, &ydpi);
  518. }
  519. #ifndef NOGDI
  520. else
  521. {
  522. const HDC dc = ::GetDC(NULL);
  523. xdpi = ::GetDeviceCaps(dc, LOGPIXELSX);
  524. ydpi = ::GetDeviceCaps(dc, LOGPIXELSY);
  525. ::ReleaseDC(NULL, dc);
  526. }
  527. #endif
  528. IM_ASSERT(xdpi == ydpi); // Please contact me if you hit this assert!
  529. return xdpi / 96.0f;
  530. }
  531. float ImGui_ImplWin32_GetDpiScaleForHwnd(void* hwnd)
  532. {
  533. HMONITOR monitor = ::MonitorFromWindow((HWND)hwnd, MONITOR_DEFAULTTONEAREST);
  534. return ImGui_ImplWin32_GetDpiScaleForMonitor(monitor);
  535. }
  536. //--------------------------------------------------------------------------------------------------------
  537. // IME (Input Method Editor) basic support for e.g. Asian language users
  538. //--------------------------------------------------------------------------------------------------------
  539. #if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) // UWP doesn't have Win32 functions
  540. #define IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS
  541. #endif
  542. #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS)
  543. #define HAS_WIN32_IME 1
  544. #include <imm.h>
  545. #ifdef _MSC_VER
  546. #pragma comment(lib, "imm32")
  547. #endif
  548. static void ImGui_ImplWin32_SetImeInputPos(ImGuiViewport* viewport, ImVec2 pos)
  549. {
  550. COMPOSITIONFORM cf = { CFS_FORCE_POSITION,{ (LONG)(pos.x - viewport->Pos.x), (LONG)(pos.y - viewport->Pos.y) },{ 0, 0, 0, 0 } };
  551. if (HWND hwnd = (HWND)viewport->PlatformHandle)
  552. if (HIMC himc = ::ImmGetContext(hwnd))
  553. {
  554. ::ImmSetCompositionWindow(himc, &cf);
  555. ::ImmReleaseContext(hwnd, himc);
  556. }
  557. }
  558. #else
  559. #define HAS_WIN32_IME 0
  560. #endif
  561. //--------------------------------------------------------------------------------------------------------
  562. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  563. // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
  564. // 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..
  565. //--------------------------------------------------------------------------------------------------------
  566. // Helper structure we store in the void* RenderUserData field of each ImGuiViewport to easily retrieve our backend data.
  567. struct ImGuiViewportDataWin32
  568. {
  569. HWND Hwnd;
  570. bool HwndOwned;
  571. DWORD DwStyle;
  572. DWORD DwExStyle;
  573. ImGuiViewportDataWin32() { Hwnd = NULL; HwndOwned = false; DwStyle = DwExStyle = 0; }
  574. ~ImGuiViewportDataWin32() { IM_ASSERT(Hwnd == NULL); }
  575. };
  576. static void ImGui_ImplWin32_GetWin32StyleFromViewportFlags(ImGuiViewportFlags flags, DWORD* out_style, DWORD* out_ex_style)
  577. {
  578. if (flags & ImGuiViewportFlags_NoDecoration)
  579. *out_style = WS_POPUP;
  580. else
  581. *out_style = WS_OVERLAPPEDWINDOW;
  582. if (flags & ImGuiViewportFlags_NoTaskBarIcon)
  583. *out_ex_style = WS_EX_TOOLWINDOW;
  584. else
  585. *out_ex_style = WS_EX_APPWINDOW;
  586. if (flags & ImGuiViewportFlags_TopMost)
  587. *out_ex_style |= WS_EX_TOPMOST;
  588. }
  589. static void ImGui_ImplWin32_CreateWindow(ImGuiViewport* viewport)
  590. {
  591. ImGuiViewportDataWin32* data = IM_NEW(ImGuiViewportDataWin32)();
  592. viewport->PlatformUserData = data;
  593. // Select style and parent window
  594. ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &data->DwStyle, &data->DwExStyle);
  595. HWND parent_window = NULL;
  596. if (viewport->ParentViewportId != 0)
  597. if (ImGuiViewport* parent_viewport = ImGui::FindViewportByID(viewport->ParentViewportId))
  598. parent_window = (HWND)parent_viewport->PlatformHandle;
  599. // Create window
  600. RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) };
  601. ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle);
  602. data->Hwnd = ::CreateWindowEx(
  603. data->DwExStyle, _T("ImGui Platform"), _T("Untitled"), data->DwStyle, // Style, class name, window name
  604. rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, // Window area
  605. parent_window, NULL, ::GetModuleHandle(NULL), NULL); // Parent window, Menu, Instance, Param
  606. data->HwndOwned = true;
  607. viewport->PlatformRequestResize = false;
  608. viewport->PlatformHandle = viewport->PlatformHandleRaw = data->Hwnd;
  609. }
  610. static void ImGui_ImplWin32_DestroyWindow(ImGuiViewport* viewport)
  611. {
  612. if (ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData)
  613. {
  614. if (::GetCapture() == data->Hwnd)
  615. {
  616. // Transfer capture so if we started dragging from a window that later disappears, we'll still receive the MOUSEUP event.
  617. ::ReleaseCapture();
  618. ::SetCapture(g_hWnd);
  619. }
  620. if (data->Hwnd && data->HwndOwned)
  621. ::DestroyWindow(data->Hwnd);
  622. data->Hwnd = NULL;
  623. IM_DELETE(data);
  624. }
  625. viewport->PlatformUserData = viewport->PlatformHandle = NULL;
  626. }
  627. static void ImGui_ImplWin32_ShowWindow(ImGuiViewport* viewport)
  628. {
  629. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  630. IM_ASSERT(data->Hwnd != 0);
  631. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  632. ::ShowWindow(data->Hwnd, SW_SHOWNA);
  633. else
  634. ::ShowWindow(data->Hwnd, SW_SHOW);
  635. }
  636. static void ImGui_ImplWin32_UpdateWindow(ImGuiViewport* viewport)
  637. {
  638. // (Optional) Update Win32 style if it changed _after_ creation.
  639. // Generally they won't change unless configuration flags are changed, but advanced uses (such as manually rewriting viewport flags) make this useful.
  640. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  641. IM_ASSERT(data->Hwnd != 0);
  642. DWORD new_style;
  643. DWORD new_ex_style;
  644. ImGui_ImplWin32_GetWin32StyleFromViewportFlags(viewport->Flags, &new_style, &new_ex_style);
  645. // Only reapply the flags that have been changed from our point of view (as other flags are being modified by Windows)
  646. if (data->DwStyle != new_style || data->DwExStyle != new_ex_style)
  647. {
  648. // (Optional) Update TopMost state if it changed _after_ creation
  649. bool top_most_changed = (data->DwExStyle & WS_EX_TOPMOST) != (new_ex_style & WS_EX_TOPMOST);
  650. HWND insert_after = top_most_changed ? ((viewport->Flags & ImGuiViewportFlags_TopMost) ? HWND_TOPMOST : HWND_NOTOPMOST) : 0;
  651. UINT swp_flag = top_most_changed ? 0 : SWP_NOZORDER;
  652. // Apply flags and position (since it is affected by flags)
  653. data->DwStyle = new_style;
  654. data->DwExStyle = new_ex_style;
  655. ::SetWindowLong(data->Hwnd, GWL_STYLE, data->DwStyle);
  656. ::SetWindowLong(data->Hwnd, GWL_EXSTYLE, data->DwExStyle);
  657. RECT rect = { (LONG)viewport->Pos.x, (LONG)viewport->Pos.y, (LONG)(viewport->Pos.x + viewport->Size.x), (LONG)(viewport->Pos.y + viewport->Size.y) };
  658. ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); // Client to Screen
  659. ::SetWindowPos(data->Hwnd, insert_after, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, swp_flag | SWP_NOACTIVATE | SWP_FRAMECHANGED);
  660. ::ShowWindow(data->Hwnd, SW_SHOWNA); // This is necessary when we alter the style
  661. viewport->PlatformRequestMove = viewport->PlatformRequestResize = true;
  662. }
  663. }
  664. static ImVec2 ImGui_ImplWin32_GetWindowPos(ImGuiViewport* viewport)
  665. {
  666. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  667. IM_ASSERT(data->Hwnd != 0);
  668. POINT pos = { 0, 0 };
  669. ::ClientToScreen(data->Hwnd, &pos);
  670. return ImVec2((float)pos.x, (float)pos.y);
  671. }
  672. static void ImGui_ImplWin32_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  673. {
  674. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  675. IM_ASSERT(data->Hwnd != 0);
  676. RECT rect = { (LONG)pos.x, (LONG)pos.y, (LONG)pos.x, (LONG)pos.y };
  677. ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle);
  678. ::SetWindowPos(data->Hwnd, NULL, rect.left, rect.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
  679. }
  680. static ImVec2 ImGui_ImplWin32_GetWindowSize(ImGuiViewport* viewport)
  681. {
  682. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  683. IM_ASSERT(data->Hwnd != 0);
  684. RECT rect;
  685. ::GetClientRect(data->Hwnd, &rect);
  686. return ImVec2(float(rect.right - rect.left), float(rect.bottom - rect.top));
  687. }
  688. static void ImGui_ImplWin32_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  689. {
  690. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  691. IM_ASSERT(data->Hwnd != 0);
  692. RECT rect = { 0, 0, (LONG)size.x, (LONG)size.y };
  693. ::AdjustWindowRectEx(&rect, data->DwStyle, FALSE, data->DwExStyle); // Client to Screen
  694. ::SetWindowPos(data->Hwnd, NULL, 0, 0, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  695. }
  696. static void ImGui_ImplWin32_SetWindowFocus(ImGuiViewport* viewport)
  697. {
  698. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  699. IM_ASSERT(data->Hwnd != 0);
  700. ::BringWindowToTop(data->Hwnd);
  701. ::SetForegroundWindow(data->Hwnd);
  702. ::SetFocus(data->Hwnd);
  703. }
  704. static bool ImGui_ImplWin32_GetWindowFocus(ImGuiViewport* viewport)
  705. {
  706. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  707. IM_ASSERT(data->Hwnd != 0);
  708. return ::GetForegroundWindow() == data->Hwnd;
  709. }
  710. static bool ImGui_ImplWin32_GetWindowMinimized(ImGuiViewport* viewport)
  711. {
  712. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  713. IM_ASSERT(data->Hwnd != 0);
  714. return ::IsIconic(data->Hwnd) != 0;
  715. }
  716. static void ImGui_ImplWin32_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  717. {
  718. // ::SetWindowTextA() doesn't properly handle UTF-8 so we explicitely convert our string.
  719. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  720. IM_ASSERT(data->Hwnd != 0);
  721. int n = ::MultiByteToWideChar(CP_UTF8, 0, title, -1, NULL, 0);
  722. ImVector<wchar_t> title_w;
  723. title_w.resize(n);
  724. ::MultiByteToWideChar(CP_UTF8, 0, title, -1, title_w.Data, n);
  725. ::SetWindowTextW(data->Hwnd, title_w.Data);
  726. }
  727. static void ImGui_ImplWin32_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
  728. {
  729. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  730. IM_ASSERT(data->Hwnd != 0);
  731. IM_ASSERT(alpha >= 0.0f && alpha <= 1.0f);
  732. if (alpha < 1.0f)
  733. {
  734. DWORD style = ::GetWindowLongW(data->Hwnd, GWL_EXSTYLE) | WS_EX_LAYERED;
  735. ::SetWindowLongW(data->Hwnd, GWL_EXSTYLE, style);
  736. ::SetLayeredWindowAttributes(data->Hwnd, 0, (BYTE)(255 * alpha), LWA_ALPHA);
  737. }
  738. else
  739. {
  740. DWORD style = ::GetWindowLongW(data->Hwnd, GWL_EXSTYLE) & ~WS_EX_LAYERED;
  741. ::SetWindowLongW(data->Hwnd, GWL_EXSTYLE, style);
  742. }
  743. }
  744. static float ImGui_ImplWin32_GetWindowDpiScale(ImGuiViewport* viewport)
  745. {
  746. ImGuiViewportDataWin32* data = (ImGuiViewportDataWin32*)viewport->PlatformUserData;
  747. IM_ASSERT(data->Hwnd != 0);
  748. return ImGui_ImplWin32_GetDpiScaleForHwnd(data->Hwnd);
  749. }
  750. // FIXME-DPI: Testing DPI related ideas
  751. static void ImGui_ImplWin32_OnChangedViewport(ImGuiViewport* viewport)
  752. {
  753. (void)viewport;
  754. #if 0
  755. ImGuiStyle default_style;
  756. //default_style.WindowPadding = ImVec2(0, 0);
  757. //default_style.WindowBorderSize = 0.0f;
  758. //default_style.ItemSpacing.y = 3.0f;
  759. //default_style.FramePadding = ImVec2(0, 0);
  760. default_style.ScaleAllSizes(viewport->DpiScale);
  761. ImGuiStyle& style = ImGui::GetStyle();
  762. style = default_style;
  763. #endif
  764. }
  765. static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  766. {
  767. if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  768. return true;
  769. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle((void*)hWnd))
  770. {
  771. switch (msg)
  772. {
  773. case WM_CLOSE:
  774. viewport->PlatformRequestClose = true;
  775. return 0;
  776. case WM_MOVE:
  777. viewport->PlatformRequestMove = true;
  778. break;
  779. case WM_SIZE:
  780. viewport->PlatformRequestResize = true;
  781. break;
  782. case WM_MOUSEACTIVATE:
  783. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnClick)
  784. return MA_NOACTIVATE;
  785. break;
  786. case WM_NCHITTEST:
  787. // Let mouse pass-through the window. This will allow the backend to set io.MouseHoveredViewport properly (which is OPTIONAL).
  788. // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging.
  789. // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in
  790. // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system.
  791. if (viewport->Flags & ImGuiViewportFlags_NoInputs)
  792. return HTTRANSPARENT;
  793. break;
  794. }
  795. }
  796. return DefWindowProc(hWnd, msg, wParam, lParam);
  797. }
  798. static void ImGui_ImplWin32_InitPlatformInterface()
  799. {
  800. WNDCLASSEX wcex;
  801. wcex.cbSize = sizeof(WNDCLASSEX);
  802. wcex.style = CS_HREDRAW | CS_VREDRAW;
  803. wcex.lpfnWndProc = ImGui_ImplWin32_WndProcHandler_PlatformWindow;
  804. wcex.cbClsExtra = 0;
  805. wcex.cbWndExtra = 0;
  806. wcex.hInstance = ::GetModuleHandle(NULL);
  807. wcex.hIcon = NULL;
  808. wcex.hCursor = NULL;
  809. wcex.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
  810. wcex.lpszMenuName = NULL;
  811. wcex.lpszClassName = _T("ImGui Platform");
  812. wcex.hIconSm = NULL;
  813. ::RegisterClassEx(&wcex);
  814. ImGui_ImplWin32_UpdateMonitors();
  815. // Register platform interface (will be coupled with a renderer interface)
  816. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  817. platform_io.Platform_CreateWindow = ImGui_ImplWin32_CreateWindow;
  818. platform_io.Platform_DestroyWindow = ImGui_ImplWin32_DestroyWindow;
  819. platform_io.Platform_ShowWindow = ImGui_ImplWin32_ShowWindow;
  820. platform_io.Platform_SetWindowPos = ImGui_ImplWin32_SetWindowPos;
  821. platform_io.Platform_GetWindowPos = ImGui_ImplWin32_GetWindowPos;
  822. platform_io.Platform_SetWindowSize = ImGui_ImplWin32_SetWindowSize;
  823. platform_io.Platform_GetWindowSize = ImGui_ImplWin32_GetWindowSize;
  824. platform_io.Platform_SetWindowFocus = ImGui_ImplWin32_SetWindowFocus;
  825. platform_io.Platform_GetWindowFocus = ImGui_ImplWin32_GetWindowFocus;
  826. platform_io.Platform_GetWindowMinimized = ImGui_ImplWin32_GetWindowMinimized;
  827. platform_io.Platform_SetWindowTitle = ImGui_ImplWin32_SetWindowTitle;
  828. platform_io.Platform_SetWindowAlpha = ImGui_ImplWin32_SetWindowAlpha;
  829. platform_io.Platform_UpdateWindow = ImGui_ImplWin32_UpdateWindow;
  830. platform_io.Platform_GetWindowDpiScale = ImGui_ImplWin32_GetWindowDpiScale; // FIXME-DPI
  831. platform_io.Platform_OnChangedViewport = ImGui_ImplWin32_OnChangedViewport; // FIXME-DPI
  832. #if HAS_WIN32_IME
  833. platform_io.Platform_SetImeInputPos = ImGui_ImplWin32_SetImeInputPos;
  834. #endif
  835. // Register main window handle (which is owned by the main application, not by us)
  836. // 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.
  837. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  838. ImGuiViewportDataWin32* data = IM_NEW(ImGuiViewportDataWin32)();
  839. data->Hwnd = g_hWnd;
  840. data->HwndOwned = false;
  841. main_viewport->PlatformUserData = data;
  842. main_viewport->PlatformHandle = (void*)g_hWnd;
  843. }
  844. static void ImGui_ImplWin32_ShutdownPlatformInterface()
  845. {
  846. ::UnregisterClass(_T("ImGui Platform"), ::GetModuleHandle(NULL));
  847. }
  848. //---------------------------------------------------------------------------------------------------------
  849. // Transparency related helpers (optional)
  850. //--------------------------------------------------------------------------------------------------------
  851. #if defined(_MSC_VER)
  852. #pragma comment(lib, "dwmapi") // Link with dwmapi.lib. MinGW will require linking with '-ldwmapi'
  853. #endif
  854. // [experimental]
  855. // Borrowed from GLFW's function updateFramebufferTransparency() in src/win32_window.c
  856. // (the Dwm* functions are Vista era functions but we are borrowing logic from GLFW)
  857. void ImGui_ImplWin32_EnableAlphaCompositing(void* hwnd)
  858. {
  859. if (!IsWindowsVistaOrGreater())
  860. return;
  861. BOOL composition;
  862. if (FAILED(::DwmIsCompositionEnabled(&composition)) || !composition)
  863. return;
  864. BOOL opaque;
  865. DWORD color;
  866. if (IsWindows8OrGreater() || (SUCCEEDED(::DwmGetColorizationColor(&color, &opaque)) && !opaque))
  867. {
  868. HRGN region = ::CreateRectRgn(0, 0, -1, -1);
  869. DWM_BLURBEHIND bb = {};
  870. bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
  871. bb.hRgnBlur = region;
  872. bb.fEnable = TRUE;
  873. ::DwmEnableBlurBehindWindow((HWND)hwnd, &bb);
  874. ::DeleteObject(region);
  875. }
  876. else
  877. {
  878. DWM_BLURBEHIND bb = {};
  879. bb.dwFlags = DWM_BB_ENABLE;
  880. ::DwmEnableBlurBehindWindow((HWND)hwnd, &bb);
  881. }
  882. }
  883. //---------------------------------------------------------------------------------------------------------