imgui_impl_win32.cpp 10 KB

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