imgui_impl_win32.cpp 39 KB

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