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