imgui_impl_win32.cpp 41 KB

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