imgui_impl_win32.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. // Missing features:
  8. // [ ] Platform: Gamepad support (best leaving it to user application to fill io.NavInputs[] with gamepad inputs from their source of choice).
  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. // CHANGELOG
  17. // (minor and older changes stripped away, please see git history for details)
  18. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
  19. // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
  20. // 2018-06-10: Inputs: Fixed handling of mouse wheel messages to support fine position messages (typically sent by track-pads).
  21. // 2018-06-08: Misc: Extracted imgui_impl_win32.cpp/.h away from the old combined DX9/DX10/DX11/DX12 examples.
  22. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors and ImGuiBackendFlags_HasSetMousePos flags + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  23. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value and WM_SETCURSOR message handling).
  24. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  25. // 2018-02-06: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
  26. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  27. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  28. // 2018-01-08: Inputs: Added mapping for ImGuiKey_Insert.
  29. // 2018-01-05: Inputs: Added WM_LBUTTONDBLCLK double-click handlers for window classes with the CS_DBLCLKS flag.
  30. // 2017-10-23: Inputs: Added WM_SYSKEYDOWN / WM_SYSKEYUP handlers so e.g. the VK_MENU key can be read.
  31. // 2017-10-23: Inputs: Using Win32 ::SetCapture/::GetCapture() to retrieve mouse positions outside the client area when dragging.
  32. // 2016-11-12: Inputs: Only call Win32 ::SetCursor(NULL) when io.MouseDrawCursor is set.
  33. // Win32 Data
  34. static HWND g_hWnd = 0;
  35. static INT64 g_Time = 0;
  36. static INT64 g_TicksPerSecond = 0;
  37. static ImGuiMouseCursor g_LastMouseCursor = ImGuiMouseCursor_COUNT;
  38. // Functions
  39. bool ImGui_ImplWin32_Init(void* hwnd)
  40. {
  41. if (!::QueryPerformanceFrequency((LARGE_INTEGER *)&g_TicksPerSecond))
  42. return false;
  43. if (!::QueryPerformanceCounter((LARGE_INTEGER *)&g_Time))
  44. return false;
  45. // Setup back-end capabilities flags
  46. g_hWnd = (HWND)hwnd;
  47. ImGuiIO& io = ImGui::GetIO();
  48. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  49. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  50. io.BackendPlatformName = "imgui_impl_win32";
  51. io.ImeWindowHandle = hwnd;
  52. // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array that we will update during the application lifetime.
  53. io.KeyMap[ImGuiKey_Tab] = VK_TAB;
  54. io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
  55. io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
  56. io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
  57. io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
  58. io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
  59. io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
  60. io.KeyMap[ImGuiKey_Home] = VK_HOME;
  61. io.KeyMap[ImGuiKey_End] = VK_END;
  62. io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
  63. io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
  64. io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
  65. io.KeyMap[ImGuiKey_Space] = VK_SPACE;
  66. io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
  67. io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
  68. io.KeyMap[ImGuiKey_A] = 'A';
  69. io.KeyMap[ImGuiKey_C] = 'C';
  70. io.KeyMap[ImGuiKey_V] = 'V';
  71. io.KeyMap[ImGuiKey_X] = 'X';
  72. io.KeyMap[ImGuiKey_Y] = 'Y';
  73. io.KeyMap[ImGuiKey_Z] = 'Z';
  74. return true;
  75. }
  76. void ImGui_ImplWin32_Shutdown()
  77. {
  78. g_hWnd = (HWND)0;
  79. }
  80. static bool ImGui_ImplWin32_UpdateMouseCursor()
  81. {
  82. ImGuiIO& io = ImGui::GetIO();
  83. if (io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange)
  84. return false;
  85. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  86. if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
  87. {
  88. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  89. ::SetCursor(NULL);
  90. }
  91. else
  92. {
  93. // Show OS mouse cursor
  94. LPTSTR win32_cursor = IDC_ARROW;
  95. switch (imgui_cursor)
  96. {
  97. case ImGuiMouseCursor_Arrow: win32_cursor = IDC_ARROW; break;
  98. case ImGuiMouseCursor_TextInput: win32_cursor = IDC_IBEAM; break;
  99. case ImGuiMouseCursor_ResizeAll: win32_cursor = IDC_SIZEALL; break;
  100. case ImGuiMouseCursor_ResizeEW: win32_cursor = IDC_SIZEWE; break;
  101. case ImGuiMouseCursor_ResizeNS: win32_cursor = IDC_SIZENS; break;
  102. case ImGuiMouseCursor_ResizeNESW: win32_cursor = IDC_SIZENESW; break;
  103. case ImGuiMouseCursor_ResizeNWSE: win32_cursor = IDC_SIZENWSE; break;
  104. case ImGuiMouseCursor_Hand: win32_cursor = IDC_HAND; break;
  105. }
  106. ::SetCursor(::LoadCursor(NULL, win32_cursor));
  107. }
  108. return true;
  109. }
  110. static void ImGui_ImplWin32_UpdateMousePos()
  111. {
  112. ImGuiIO& io = ImGui::GetIO();
  113. // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  114. if (io.WantSetMousePos)
  115. {
  116. POINT pos = { (int)io.MousePos.x, (int)io.MousePos.y };
  117. ::ClientToScreen(g_hWnd, &pos);
  118. ::SetCursorPos(pos.x, pos.y);
  119. }
  120. // Set mouse position
  121. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  122. POINT pos;
  123. if (::GetActiveWindow() == g_hWnd && ::GetCursorPos(&pos))
  124. if (::ScreenToClient(g_hWnd, &pos))
  125. io.MousePos = ImVec2((float)pos.x, (float)pos.y);
  126. }
  127. void ImGui_ImplWin32_NewFrame()
  128. {
  129. ImGuiIO& io = ImGui::GetIO();
  130. // Setup display size (every frame to accommodate for window resizing)
  131. RECT rect;
  132. ::GetClientRect(g_hWnd, &rect);
  133. io.DisplaySize = ImVec2((float)(rect.right - rect.left), (float)(rect.bottom - rect.top));
  134. // Setup time step
  135. INT64 current_time;
  136. ::QueryPerformanceCounter((LARGE_INTEGER *)&current_time);
  137. io.DeltaTime = (float)(current_time - g_Time) / g_TicksPerSecond;
  138. g_Time = current_time;
  139. // Read keyboard modifiers inputs
  140. io.KeyCtrl = (::GetKeyState(VK_CONTROL) & 0x8000) != 0;
  141. io.KeyShift = (::GetKeyState(VK_SHIFT) & 0x8000) != 0;
  142. io.KeyAlt = (::GetKeyState(VK_MENU) & 0x8000) != 0;
  143. io.KeySuper = false;
  144. // io.KeysDown[], io.MousePos, io.MouseDown[], io.MouseWheel: filled by the WndProc handler below.
  145. // Update OS mouse position
  146. ImGui_ImplWin32_UpdateMousePos();
  147. // Update OS mouse cursor with the cursor requested by imgui
  148. ImGuiMouseCursor mouse_cursor = io.MouseDrawCursor ? ImGuiMouseCursor_None : ImGui::GetMouseCursor();
  149. if (g_LastMouseCursor != mouse_cursor)
  150. {
  151. g_LastMouseCursor = mouse_cursor;
  152. ImGui_ImplWin32_UpdateMouseCursor();
  153. }
  154. }
  155. // Allow compilation with old Windows SDK. MinGW doesn't have default _WIN32_WINNT/WINVER versions.
  156. #ifndef WM_MOUSEHWHEEL
  157. #define WM_MOUSEHWHEEL 0x020E
  158. #endif
  159. // Process Win32 mouse/keyboard inputs.
  160. // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  161. // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  162. // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  163. // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  164. // PS: In this Win32 handler, we use the capture API (GetCapture/SetCapture/ReleaseCapture) to be able to read mouse coordinations when dragging mouse outside of our window bounds.
  165. // 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.
  166. IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  167. {
  168. if (ImGui::GetCurrentContext() == NULL)
  169. return 0;
  170. ImGuiIO& io = ImGui::GetIO();
  171. switch (msg)
  172. {
  173. case WM_LBUTTONDOWN: case WM_LBUTTONDBLCLK:
  174. case WM_RBUTTONDOWN: case WM_RBUTTONDBLCLK:
  175. case WM_MBUTTONDOWN: case WM_MBUTTONDBLCLK:
  176. {
  177. int button = 0;
  178. if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONDBLCLK) button = 0;
  179. if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONDBLCLK) button = 1;
  180. if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONDBLCLK) button = 2;
  181. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == NULL)
  182. ::SetCapture(hwnd);
  183. io.MouseDown[button] = true;
  184. return 0;
  185. }
  186. case WM_LBUTTONUP:
  187. case WM_RBUTTONUP:
  188. case WM_MBUTTONUP:
  189. {
  190. int button = 0;
  191. if (msg == WM_LBUTTONUP) button = 0;
  192. if (msg == WM_RBUTTONUP) button = 1;
  193. if (msg == WM_MBUTTONUP) button = 2;
  194. io.MouseDown[button] = false;
  195. if (!ImGui::IsAnyMouseDown() && ::GetCapture() == hwnd)
  196. ::ReleaseCapture();
  197. return 0;
  198. }
  199. case WM_MOUSEWHEEL:
  200. io.MouseWheel += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
  201. return 0;
  202. case WM_MOUSEHWHEEL:
  203. io.MouseWheelH += (float)GET_WHEEL_DELTA_WPARAM(wParam) / (float)WHEEL_DELTA;
  204. return 0;
  205. case WM_KEYDOWN:
  206. case WM_SYSKEYDOWN:
  207. if (wParam < 256)
  208. io.KeysDown[wParam] = 1;
  209. return 0;
  210. case WM_KEYUP:
  211. case WM_SYSKEYUP:
  212. if (wParam < 256)
  213. io.KeysDown[wParam] = 0;
  214. return 0;
  215. case WM_CHAR:
  216. // You can also use ToAscii()+GetKeyboardState() to retrieve characters.
  217. if (wParam > 0 && wParam < 0x10000)
  218. io.AddInputCharacter((unsigned short)wParam);
  219. return 0;
  220. case WM_SETCURSOR:
  221. if (LOWORD(lParam) == HTCLIENT && ImGui_ImplWin32_UpdateMouseCursor())
  222. return 1;
  223. return 0;
  224. }
  225. return 0;
  226. }