imgui_impl_glfw.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. // ImGui Platform Binding for: GLFW
  2. // This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan..)
  3. // (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)
  4. // Implemented features:
  5. // [X] Gamepad navigation mapping. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  6. // [X] Multi-viewport windows (when ImGuiConfigFlags_ViewportsEnable is enabled).
  7. // You can copy and use unmodified imgui_impl_* files in your project. See main.cpp for an example of using this.
  8. // If you use this binding you'll need to call 4 functions: ImGui_ImplXXXX_Init(), ImGui_ImplXXXX_NewFrame(), ImGui::Render() and ImGui_ImplXXXX_Shutdown().
  9. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  10. // https://github.com/ocornut/imgui
  11. // CHANGELOG
  12. // (minor and older changes stripped away, please see git history for details)
  13. // 2018-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  14. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  15. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value, passed to glfwSetCursor()).
  16. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  17. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  18. // 2018-01-25: Inputs: Added gamepad support if ImGuiConfigFlags_NavEnableGamepad is set.
  19. // 2018-01-25: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
  20. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  21. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  22. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  23. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  24. #include "imgui.h"
  25. #include "imgui_impl_glfw.h"
  26. // GLFW
  27. #include <GLFW/glfw3.h>
  28. #ifdef _WIN32
  29. #undef APIENTRY
  30. #define GLFW_EXPOSE_NATIVE_WIN32
  31. #include <GLFW/glfw3native.h> // for glfwGetWin32Window
  32. #endif
  33. #define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ GLFW_FLOATING
  34. #define GLFW_HAS_WINDOW_HOVERED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ GLFW_HOVERED
  35. #define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity
  36. #define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale
  37. #define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface
  38. // Data
  39. enum GlfwClientApi
  40. {
  41. GlfwClientApi_Unknown,
  42. GlfwClientApi_OpenGL,
  43. GlfwClientApi_Vulkan
  44. };
  45. static GLFWwindow* g_Window = NULL;
  46. static GlfwClientApi g_ClientApi = GlfwClientApi_Unknown;
  47. static double g_Time = 0.0f;
  48. static bool g_MouseJustPressed[5] = { false, false, false, false, false };
  49. static GLFWcursor* g_MouseCursors[ImGuiMouseCursor_Count_] = { 0 };
  50. // Forward Declarations
  51. static void ImGui_ImplGlfw_InitPlatformInterface();
  52. static void ImGui_ImplGlfw_ShutdownPlatformInterface();
  53. static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data)
  54. {
  55. return glfwGetClipboardString((GLFWwindow*)user_data);
  56. }
  57. static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text)
  58. {
  59. glfwSetClipboardString((GLFWwindow*)user_data, text);
  60. }
  61. void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow*, int button, int action, int /*mods*/)
  62. {
  63. if (action == GLFW_PRESS && button >= 0 && button < IM_ARRAYSIZE(g_MouseJustPressed))
  64. g_MouseJustPressed[button] = true;
  65. }
  66. void ImGui_ImplGlfw_ScrollCallback(GLFWwindow*, double xoffset, double yoffset)
  67. {
  68. ImGuiIO& io = ImGui::GetIO();
  69. io.MouseWheelH += (float)xoffset;
  70. io.MouseWheel += (float)yoffset;
  71. }
  72. void ImGui_ImplGlfw_KeyCallback(GLFWwindow*, int key, int, int action, int mods)
  73. {
  74. ImGuiIO& io = ImGui::GetIO();
  75. if (action == GLFW_PRESS)
  76. io.KeysDown[key] = true;
  77. if (action == GLFW_RELEASE)
  78. io.KeysDown[key] = false;
  79. (void)mods; // Modifiers are not reliable across systems
  80. io.KeyCtrl = io.KeysDown[GLFW_KEY_LEFT_CONTROL] || io.KeysDown[GLFW_KEY_RIGHT_CONTROL];
  81. io.KeyShift = io.KeysDown[GLFW_KEY_LEFT_SHIFT] || io.KeysDown[GLFW_KEY_RIGHT_SHIFT];
  82. io.KeyAlt = io.KeysDown[GLFW_KEY_LEFT_ALT] || io.KeysDown[GLFW_KEY_RIGHT_ALT];
  83. io.KeySuper = io.KeysDown[GLFW_KEY_LEFT_SUPER] || io.KeysDown[GLFW_KEY_RIGHT_SUPER];
  84. }
  85. void ImGui_ImplGlfw_CharCallback(GLFWwindow*, unsigned int c)
  86. {
  87. ImGuiIO& io = ImGui::GetIO();
  88. if (c > 0 && c < 0x10000)
  89. io.AddInputCharacter((unsigned short)c);
  90. }
  91. void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window)
  92. {
  93. glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
  94. glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
  95. glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback);
  96. glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
  97. }
  98. static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api)
  99. {
  100. g_Window = window;
  101. // Setup back-end capabilities flags
  102. ImGuiIO& io = ImGui::GetIO();
  103. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  104. //io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  105. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  106. #if GLFW_HAS_GLFW_HOVERED
  107. io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can set io.MouseHoveredViewport correctly (optional, not easy)
  108. #endif
  109. // Keyboard mapping. ImGui will use those indices to peek into the io.KeysDown[] array.
  110. io.KeyMap[ImGuiKey_Tab] = GLFW_KEY_TAB;
  111. io.KeyMap[ImGuiKey_LeftArrow] = GLFW_KEY_LEFT;
  112. io.KeyMap[ImGuiKey_RightArrow] = GLFW_KEY_RIGHT;
  113. io.KeyMap[ImGuiKey_UpArrow] = GLFW_KEY_UP;
  114. io.KeyMap[ImGuiKey_DownArrow] = GLFW_KEY_DOWN;
  115. io.KeyMap[ImGuiKey_PageUp] = GLFW_KEY_PAGE_UP;
  116. io.KeyMap[ImGuiKey_PageDown] = GLFW_KEY_PAGE_DOWN;
  117. io.KeyMap[ImGuiKey_Home] = GLFW_KEY_HOME;
  118. io.KeyMap[ImGuiKey_End] = GLFW_KEY_END;
  119. io.KeyMap[ImGuiKey_Insert] = GLFW_KEY_INSERT;
  120. io.KeyMap[ImGuiKey_Delete] = GLFW_KEY_DELETE;
  121. io.KeyMap[ImGuiKey_Backspace] = GLFW_KEY_BACKSPACE;
  122. io.KeyMap[ImGuiKey_Space] = GLFW_KEY_SPACE;
  123. io.KeyMap[ImGuiKey_Enter] = GLFW_KEY_ENTER;
  124. io.KeyMap[ImGuiKey_Escape] = GLFW_KEY_ESCAPE;
  125. io.KeyMap[ImGuiKey_A] = GLFW_KEY_A;
  126. io.KeyMap[ImGuiKey_C] = GLFW_KEY_C;
  127. io.KeyMap[ImGuiKey_V] = GLFW_KEY_V;
  128. io.KeyMap[ImGuiKey_X] = GLFW_KEY_X;
  129. io.KeyMap[ImGuiKey_Y] = GLFW_KEY_Y;
  130. io.KeyMap[ImGuiKey_Z] = GLFW_KEY_Z;
  131. io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
  132. io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
  133. io.ClipboardUserData = g_Window;
  134. g_MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  135. g_MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
  136. g_MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
  137. g_MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR);
  138. g_MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
  139. g_MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
  140. g_MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR); // FIXME: GLFW doesn't have this.
  141. if (install_callbacks)
  142. ImGui_ImplGlfw_InstallCallbacks(window);
  143. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  144. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  145. main_viewport->PlatformHandle = (void*)g_Window;
  146. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  147. ImGui_ImplGlfw_InitPlatformInterface();
  148. g_ClientApi = client_api;
  149. return true;
  150. }
  151. bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks)
  152. {
  153. return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_OpenGL);
  154. }
  155. bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks)
  156. {
  157. return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Vulkan);
  158. }
  159. void ImGui_ImplGlfw_Shutdown()
  160. {
  161. ImGui_ImplGlfw_ShutdownPlatformInterface();
  162. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_Count_; cursor_n++)
  163. {
  164. glfwDestroyCursor(g_MouseCursors[cursor_n]);
  165. g_MouseCursors[cursor_n] = NULL;
  166. }
  167. g_ClientApi = GlfwClientApi_Unknown;
  168. }
  169. static void ImGui_ImplGlfw_UpdateMouse()
  170. {
  171. #if 0
  172. // Set OS mouse position if requested (only used when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  173. if (io.WantSetMousePos)
  174. glfwSetCursorPos(g_Window, (double)io.MousePos.x, (double)io.MousePos.y);
  175. #endif
  176. ImGuiIO& io = ImGui::GetIO();
  177. io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
  178. io.MousePosViewport = 0;
  179. io.MouseHoveredViewport = 0;
  180. // Update buttons
  181. for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
  182. {
  183. // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame.
  184. io.MouseDown[i] = g_MouseJustPressed[i] || glfwGetMouseButton(g_Window, i) != 0;
  185. g_MouseJustPressed[i] = false;
  186. }
  187. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  188. for (int n = 0; n < platform_io.Viewports.Size; n++)
  189. {
  190. ImGuiViewport* viewport = platform_io.Viewports[n];
  191. GLFWwindow* window = (GLFWwindow*)viewport->PlatformHandle;
  192. IM_ASSERT(window != NULL);
  193. if (glfwGetWindowAttrib(window, GLFW_FOCUSED))
  194. {
  195. double mouse_x, mouse_y;
  196. glfwGetCursorPos(window, &mouse_x, &mouse_y);
  197. io.MousePos = ImVec2((float)mouse_x + viewport->Pos.x, (float)mouse_y + viewport->Pos.y);
  198. io.MousePosViewport = viewport->ID;
  199. for (int i = 0; i < IM_ARRAYSIZE(io.MouseDown); i++)
  200. io.MouseDown[i] |= glfwGetMouseButton(window, i) != 0;
  201. }
  202. #if GLFW_HAS_GLFW_HOVERED
  203. io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport;
  204. if (glfwGetWindowAttrib(window, GLFW_HOVERED) && !(viewport->Flags & ImGuiViewportFlags_NoInputs))
  205. io.MouseHoveredViewport = viewport->ID;
  206. #endif
  207. }
  208. // Update OS/hardware mouse cursor if imgui isn't drawing a software cursor
  209. // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here.
  210. if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) == 0 && glfwGetInputMode(g_Window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED)
  211. {
  212. ImGuiMouseCursor cursor = ImGui::GetMouseCursor();
  213. for (int n = 0; n < platform_io.Viewports.Size; n++)
  214. {
  215. GLFWwindow* window = (GLFWwindow*)platform_io.Viewports[n]->PlatformHandle;
  216. if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None)
  217. {
  218. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  219. }
  220. else
  221. {
  222. glfwSetCursor(window, g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]);
  223. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  224. }
  225. }
  226. }
  227. }
  228. void ImGui_ImplGlfw_NewFrame()
  229. {
  230. ImGuiIO& io = ImGui::GetIO();
  231. IM_ASSERT(io.Fonts->IsBuilt()); // Font atlas needs to be built, call renderer _NewFrame() function e.g. ImGui_ImplOpenGL3_NewFrame()
  232. // Setup display size
  233. int w, h;
  234. int display_w, display_h;
  235. glfwGetWindowSize(g_Window, &w, &h);
  236. glfwGetFramebufferSize(g_Window, &display_w, &display_h);
  237. io.DisplaySize = ImVec2((float)w, (float)h);
  238. io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0);
  239. // Setup time step
  240. double current_time = glfwGetTime();
  241. io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f);
  242. g_Time = current_time;
  243. ImGui_ImplGlfw_UpdateMouse();
  244. // Gamepad navigation mapping [BETA]
  245. memset(io.NavInputs, 0, sizeof(io.NavInputs));
  246. if (io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad)
  247. {
  248. // Update gamepad inputs
  249. #define MAP_BUTTON(NAV_NO, BUTTON_NO) { if (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS) io.NavInputs[NAV_NO] = 1.0f; }
  250. #define MAP_ANALOG(NAV_NO, AXIS_NO, V0, V1) { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); if (v > 1.0f) v = 1.0f; if (io.NavInputs[NAV_NO] < v) io.NavInputs[NAV_NO] = v; }
  251. int axes_count = 0, buttons_count = 0;
  252. const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
  253. const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
  254. MAP_BUTTON(ImGuiNavInput_Activate, 0); // Cross / A
  255. MAP_BUTTON(ImGuiNavInput_Cancel, 1); // Circle / B
  256. MAP_BUTTON(ImGuiNavInput_Menu, 2); // Square / X
  257. MAP_BUTTON(ImGuiNavInput_Input, 3); // Triangle / Y
  258. MAP_BUTTON(ImGuiNavInput_DpadLeft, 13); // D-Pad Left
  259. MAP_BUTTON(ImGuiNavInput_DpadRight, 11); // D-Pad Right
  260. MAP_BUTTON(ImGuiNavInput_DpadUp, 10); // D-Pad Up
  261. MAP_BUTTON(ImGuiNavInput_DpadDown, 12); // D-Pad Down
  262. MAP_BUTTON(ImGuiNavInput_FocusPrev, 4); // L1 / LB
  263. MAP_BUTTON(ImGuiNavInput_FocusNext, 5); // R1 / RB
  264. MAP_BUTTON(ImGuiNavInput_TweakSlow, 4); // L1 / LB
  265. MAP_BUTTON(ImGuiNavInput_TweakFast, 5); // R1 / RB
  266. MAP_ANALOG(ImGuiNavInput_LStickLeft, 0, -0.3f, -0.9f);
  267. MAP_ANALOG(ImGuiNavInput_LStickRight,0, +0.3f, +0.9f);
  268. MAP_ANALOG(ImGuiNavInput_LStickUp, 1, +0.3f, +0.9f);
  269. MAP_ANALOG(ImGuiNavInput_LStickDown, 1, -0.3f, -0.9f);
  270. #undef MAP_BUTTON
  271. #undef MAP_ANALOG
  272. if (axes_count > 0 && buttons_count > 0)
  273. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  274. else
  275. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  276. }
  277. // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application.
  278. ImGui::NewFrame();
  279. }
  280. // --------------------------------------------------------------------------------------------------------
  281. // Platform Windows
  282. // --------------------------------------------------------------------------------------------------------
  283. struct ImGuiViewportDataGlfw
  284. {
  285. GLFWwindow* Window;
  286. bool WindowOwned;
  287. ImGuiViewportDataGlfw() { Window = NULL; WindowOwned = false; }
  288. ~ImGuiViewportDataGlfw() { IM_ASSERT(Window == NULL); }
  289. };
  290. static void ImGui_ImplGlfw_WindowCloseCallback(GLFWwindow* window)
  291. {
  292. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window))
  293. viewport->PlatformRequestClose = true;
  294. }
  295. static void ImGui_ImplGlfw_WindowPosCallback(GLFWwindow* window, int, int)
  296. {
  297. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window))
  298. viewport->PlatformRequestMove = true;
  299. }
  300. static void ImGui_ImplGlfw_WindowSizeCallback(GLFWwindow* window, int, int)
  301. {
  302. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window))
  303. viewport->PlatformRequestResize = true;
  304. }
  305. static void ImGui_ImplGlfw_CreateWindow(ImGuiViewport* viewport)
  306. {
  307. ImGuiViewportDataGlfw* data = IM_NEW(ImGuiViewportDataGlfw)();
  308. viewport->PlatformUserData = data;
  309. // GLFW 3.2 unfortunately always set focus on glfwCreateWindow() if GLFW_VISIBLE is set, regardless of GLFW_FOCUSED
  310. glfwWindowHint(GLFW_VISIBLE, false);
  311. glfwWindowHint(GLFW_FOCUSED, false);
  312. glfwWindowHint(GLFW_DECORATED, (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? false : true);
  313. #if GLFW_HAS_WINDOW_TOPMOST
  314. glfwWindowHint(GLFW_FLOATING, (viewport->Flags & imGuiViewportFlags_TopMost) ? true : false);
  315. #endif
  316. GLFWwindow* share_window = (g_ClientApi == GlfwClientApi_OpenGL) ? g_Window : NULL;
  317. data->Window = glfwCreateWindow((int)viewport->Size.x, (int)viewport->Size.y, "No Title Yet", NULL, share_window);
  318. data->WindowOwned = true;
  319. viewport->PlatformHandle = (void*)data->Window;
  320. ImGui_ImplGlfw_InstallCallbacks(data->Window);
  321. glfwSetWindowCloseCallback(data->Window, ImGui_ImplGlfw_WindowCloseCallback);
  322. glfwSetWindowPosCallback(data->Window, ImGui_ImplGlfw_WindowPosCallback);
  323. glfwSetWindowSizeCallback(data->Window, ImGui_ImplGlfw_WindowSizeCallback);
  324. }
  325. static void ImGui_ImplGlfw_DestroyWindow(ImGuiViewport* viewport)
  326. {
  327. if (ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData)
  328. {
  329. if (data->WindowOwned)
  330. {
  331. #if GLFW_HAS_GLFW_HOVERED && defined(_WIN32)
  332. HWND hwnd = glfwGetWin32Window(data->Window);
  333. ::RemovePropA(hwnd, "IMGUI_VIEWPORT");
  334. #endif
  335. glfwDestroyWindow(data->Window);
  336. }
  337. data->Window = NULL;
  338. IM_DELETE(data);
  339. }
  340. viewport->PlatformUserData = viewport->PlatformHandle = NULL;
  341. }
  342. #if defined(_WIN32) && GLFW_HAS_GLFW_HOVERED
  343. static WNDPROC g_GlfwWndProc = NULL;
  344. static LRESULT CALLBACK WndProcNoInputs(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  345. {
  346. if (msg == WM_NCHITTEST)
  347. {
  348. // Let mouse pass-through the window. This will allow the back-end to set io.MouseHoveredViewport properly (which is OPTIONAL).
  349. // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging.
  350. // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in
  351. // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system.
  352. ImGuiViewport* viewport = (ImGuiViewport*)::GetPropA(hWnd, "IMGUI_VIEWPORT");
  353. if (viewport->Flags & ImGuiViewportFlags_NoInputs)
  354. return HTTRANSPARENT;
  355. }
  356. return ::CallWindowProc(g_GlfwWndProc, hWnd, msg, wParam, lParam);
  357. }
  358. #endif
  359. static void ImGui_ImplGlfw_ShowWindow(ImGuiViewport* viewport)
  360. {
  361. ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData;
  362. #if defined(_WIN32)
  363. // GLFW hack: Hide icon from task bar
  364. HWND hwnd = glfwGetWin32Window(data->Window);
  365. if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)
  366. {
  367. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  368. ex_style &= ~WS_EX_APPWINDOW;
  369. ex_style |= WS_EX_TOOLWINDOW;
  370. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  371. }
  372. // GLFW hack: install hook for WM_NCHITTEST message handler
  373. #if GLFW_HAS_GLFW_HOVERED && defined(_WIN32)
  374. ::SetPropA(hwnd, "IMGUI_VIEWPORT", viewport);
  375. if (g_GlfwWndProc == NULL)
  376. g_GlfwWndProc = (WNDPROC)::GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  377. ::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)WndProcNoInputs);
  378. #endif
  379. // GLFW hack: GLFW 3.2 has a bug where glfwShowWindow() also activates/focus the window.
  380. // The fix was pushed to GLFW repository on 2018/01/09 and should be included in GLFW 3.3. See https://github.com/glfw/glfw/issues/1179
  381. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  382. {
  383. ::ShowWindow(hwnd, SW_SHOWNA);
  384. return;
  385. }
  386. #endif
  387. glfwShowWindow(data->Window);
  388. }
  389. static ImVec2 ImGui_ImplGlfw_GetWindowPos(ImGuiViewport* viewport)
  390. {
  391. ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData;
  392. int x = 0, y = 0;
  393. glfwGetWindowPos(data->Window, &x, &y);
  394. return ImVec2((float)x, (float)y);
  395. }
  396. static void ImGui_ImplGlfw_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  397. {
  398. ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData;
  399. glfwSetWindowPos(data->Window, (int)pos.x, (int)pos.y);
  400. }
  401. static ImVec2 ImGui_ImplGlfw_GetWindowSize(ImGuiViewport* viewport)
  402. {
  403. ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData;
  404. int w = 0, h = 0;
  405. glfwGetWindowSize(data->Window, &w, &h);
  406. return ImVec2((float)w, (float)h);
  407. }
  408. static void ImGui_ImplGlfw_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  409. {
  410. ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData;
  411. glfwSetWindowSize(data->Window, (int)size.x, (int)size.y);
  412. }
  413. static void ImGui_ImplGlfw_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  414. {
  415. ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData;
  416. glfwSetWindowTitle(data->Window, title);
  417. }
  418. #if GLFW_HAS_WINDOW_ALPHA
  419. static void ImGui_ImplGlfw_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
  420. {
  421. ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData;
  422. glfwSetWindowOpacity(data->Window, alpha);
  423. }
  424. #endif
  425. static void ImGui_ImplGlfw_RenderWindow(ImGuiViewport* viewport, void*)
  426. {
  427. ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData;
  428. if (g_ClientApi == GlfwClientApi_OpenGL)
  429. glfwMakeContextCurrent(data->Window);
  430. }
  431. static void ImGui_ImplGlfw_SwapBuffers(ImGuiViewport* viewport, void*)
  432. {
  433. ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData;
  434. if (g_ClientApi == GlfwClientApi_OpenGL)
  435. glfwSwapBuffers(data->Window);
  436. }
  437. //--------------------------------------------------------------------------------------------------------
  438. // IME (Input Method Editor) basic support for e.g. Asian language users
  439. //--------------------------------------------------------------------------------------------------------
  440. // We provide a Win32 implementation because this is such a common issue for IME users
  441. #if defined(_WIN32) && !defined(IMGUI_DISABLE_WIN32_DEFAULT_IME_FUNCTIONS) && !defined(__GNUC__)
  442. #define HAS_WIN32_IME 1
  443. #include <imm.h>
  444. #ifdef _MSC_VER
  445. #pragma comment(lib, "imm32")
  446. #endif
  447. static void ImGui_ImplWin32_SetImeInputPos(ImGuiViewport* viewport, ImVec2 pos)
  448. {
  449. COMPOSITIONFORM cf = { CFS_FORCE_POSITION, { (LONG)(pos.x - viewport->Pos.x), (LONG)(pos.y - viewport->Pos.y) }, { 0, 0, 0, 0 } };
  450. if (ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData)
  451. if (HWND hwnd = glfwGetWin32Window(data->Window))
  452. if (HIMC himc = ImmGetContext(hwnd))
  453. ImmSetCompositionWindow(himc, &cf);
  454. }
  455. #else
  456. #define HAS_WIN32_IME 0
  457. #endif
  458. //--------------------------------------------------------------------------------------------------------
  459. // Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface)
  460. //--------------------------------------------------------------------------------------------------------
  461. // Avoid including <vulkan.h> so we can build without it
  462. #if GLFW_HAS_VULKAN
  463. #ifndef VULKAN_H_
  464. #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
  465. #if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
  466. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object;
  467. #else
  468. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object;
  469. #endif
  470. VK_DEFINE_HANDLE(VkInstance)
  471. VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSurfaceKHR)
  472. struct VkAllocationCallbacks;
  473. enum VkResult { VK_RESULT_MAX_ENUM = 0x7FFFFFFF };
  474. #endif // VULKAN_H_
  475. extern "C" { extern GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface); }
  476. static int ImGui_ImplGlfw_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface)
  477. {
  478. ImGuiViewportDataGlfw* data = (ImGuiViewportDataGlfw*)viewport->PlatformUserData;
  479. IM_ASSERT(g_ClientApi == GlfwClientApi_Vulkan);
  480. VkResult err = glfwCreateWindowSurface((VkInstance)vk_instance, data->Window, (const VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface);
  481. return (int)err;
  482. }
  483. #endif // GLFW_HAS_VULKAN
  484. // FIXME-PLATFORM: Update monitor list when changed (using glfwSetMonitorCallback?)
  485. // FIXME-PLATFORM: GLFW doesn't export monitor work area (see https://github.com/glfw/glfw/pull/989)
  486. static void ImGui_ImplGlfw_UpdateMonitors()
  487. {
  488. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  489. int monitors_count = 0;
  490. GLFWmonitor** glfw_monitors = glfwGetMonitors(&monitors_count);
  491. platform_io.Monitors.resize(0);
  492. for (int n = 0; n < monitors_count; n++)
  493. {
  494. ImGuiPlatformMonitor monitor;
  495. int x, y;
  496. glfwGetMonitorPos(glfw_monitors[n], &x, &y);
  497. const GLFWvidmode* vid_mode = glfwGetVideoMode(glfw_monitors[n]);
  498. monitor.FullMin = monitor.WorkMin = ImVec2((float)x, (float)y);
  499. monitor.FullMax = monitor.WorkMax = ImVec2((float)(x + vid_mode->width), (float)(y + vid_mode->height));
  500. #if GLFW_HAS_PER_MONITOR_DPI
  501. // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime.
  502. float x_scale, y_scale;
  503. glfwGetMonitorContentScale(glfw_monitors[n], &x_scale, &y_scale);
  504. monitor.DpiScale = x_scale;
  505. #endif
  506. platform_io.Monitors.push_back(monitor);
  507. }
  508. }
  509. static void ImGui_ImplGlfw_InitPlatformInterface()
  510. {
  511. // Register platform interface (will be coupled with a renderer interface)
  512. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  513. platform_io.Platform_CreateWindow = ImGui_ImplGlfw_CreateWindow;
  514. platform_io.Platform_DestroyWindow = ImGui_ImplGlfw_DestroyWindow;
  515. platform_io.Platform_ShowWindow = ImGui_ImplGlfw_ShowWindow;
  516. platform_io.Platform_SetWindowPos = ImGui_ImplGlfw_SetWindowPos;
  517. platform_io.Platform_GetWindowPos = ImGui_ImplGlfw_GetWindowPos;
  518. platform_io.Platform_SetWindowSize = ImGui_ImplGlfw_SetWindowSize;
  519. platform_io.Platform_GetWindowSize = ImGui_ImplGlfw_GetWindowSize;
  520. platform_io.Platform_SetWindowTitle = ImGui_ImplGlfw_SetWindowTitle;
  521. platform_io.Platform_RenderWindow = ImGui_ImplGlfw_RenderWindow;
  522. platform_io.Platform_SwapBuffers = ImGui_ImplGlfw_SwapBuffers;
  523. #if GLFW_HAS_WINDOW_ALPHA
  524. platform_io.Platform_SetWindowAlpha = ImGui_ImplGlfw_SetWindowAlpha;
  525. #endif
  526. #if GLFW_HAS_VULKAN
  527. platform_io.Platform_CreateVkSurface = ImGui_ImplGlfw_CreateVkSurface;
  528. #endif
  529. #if HAS_WIN32_IME
  530. platform_io.Platform_SetImeInputPos = ImGui_ImplWin32_SetImeInputPos;
  531. #endif
  532. ImGui_ImplGlfw_UpdateMonitors();
  533. // Register main window handle (which is owned by the main application, not by us)
  534. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  535. ImGuiViewportDataGlfw* data = IM_NEW(ImGuiViewportDataGlfw)();
  536. data->Window = g_Window;
  537. data->WindowOwned = false;
  538. main_viewport->PlatformUserData = data;
  539. main_viewport->PlatformHandle = (void*)g_Window;
  540. }
  541. static void ImGui_ImplGlfw_ShutdownPlatformInterface()
  542. {
  543. }