imgui_impl_win32.cpp 9.9 KB

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