imgui_impl_win32.cpp 10 KB

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