2
0

imgui_impl_glfw.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178
  1. // dear imgui: Platform Backend for GLFW
  2. // This needs to be used along with a Renderer (e.g. OpenGL3, Vulkan, WebGPU..)
  3. // (Info: GLFW is a cross-platform general purpose library for handling windows, inputs, OpenGL/Vulkan graphics context creation, etc.)
  4. // (Requires: GLFW 3.1+. Prefer GLFW 3.3+ for full feature support.)
  5. // Implemented features:
  6. // [X] Platform: Clipboard support.
  7. // [X] Platform: Keyboard support. Since 1.87 we are using the io.AddKeyEvent() function. Pass ImGuiKey values to all key functions e.g. ImGui::IsKeyPressed(ImGuiKey_Space). [Legacy GLFW_KEY_* values will also be supported unless IMGUI_DISABLE_OBSOLETE_KEYIO is set]
  8. // [X] Platform: Gamepad support. Enable with 'io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad'.
  9. // [X] Platform: Mouse cursor shape and visibility. Disable with 'io.ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange' (note: the resizing cursors requires GLFW 3.4+).
  10. // [X] Platform: Multi-viewport support (multiple windows). Enable with 'io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable'.
  11. // Issues:
  12. // [ ] Platform: Multi-viewport support: ParentViewportID not honored, and so io.ConfigViewportsNoDefaultParent has no effect (minor).
  13. // You can use unmodified imgui_impl_* files in your project. See examples/ folder for examples of using this.
  14. // Prefer including the entire imgui/ repository into your project (either as a copy or as a submodule), and only build the backends you need.
  15. // If you are new to Dear ImGui, read documentation from the docs/ folder + read the top of imgui.cpp.
  16. // Read online: https://github.com/ocornut/imgui/tree/master/docs
  17. // CHANGELOG
  18. // (minor and older changes stripped away, please see git history for details)
  19. // 2022-XX-XX: Platform: Added support for multiple windows via the ImGuiPlatformIO interface.
  20. // 2022-10-11: Using 'nullptr' instead of 'NULL' as per our switch to C++11.
  21. // 2022-09-26: Inputs: Renamed ImGuiKey_ModXXX introduced in 1.87 to ImGuiMod_XXX (old names still supported).
  22. // 2022-09-01: Inputs: Honor GLFW_CURSOR_DISABLED by not setting mouse position.
  23. // 2022-04-30: Inputs: Fixed ImGui_ImplGlfw_TranslateUntranslatedKey() for lower case letters on OSX.
  24. // 2022-03-23: Inputs: Fixed a regression in 1.87 which resulted in keyboard modifiers events being reported incorrectly on Linux/X11.
  25. // 2022-02-07: Added ImGui_ImplGlfw_InstallCallbacks()/ImGui_ImplGlfw_RestoreCallbacks() helpers to facilitate user installing callbacks after initializing backend.
  26. // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago) with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
  27. // 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
  28. // 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
  29. // 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates.
  30. // 2022-01-12: *BREAKING CHANGE*: Now using glfwSetCursorPosCallback(). If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetCursorPosCallback() and forward it to the backend via ImGui_ImplGlfw_CursorPosCallback().
  31. // 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
  32. // 2022-01-05: Inputs: Converting GLFW untranslated keycodes back to translated keycodes (in the ImGui_ImplGlfw_KeyCallback() function) in order to match the behavior of every other backend, and facilitate the use of GLFW with lettered-shortcuts API.
  33. // 2021-08-17: *BREAKING CHANGE*: Now using glfwSetWindowFocusCallback() to calling io.AddFocusEvent(). If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetWindowFocusCallback() and forward it to the backend via ImGui_ImplGlfw_WindowFocusCallback().
  34. // 2021-07-29: *BREAKING CHANGE*: Now using glfwSetCursorEnterCallback(). MousePos is correctly reported when the host platform window is hovered but not focused. If you called ImGui_ImplGlfw_InitXXX() with install_callbacks = false, you MUST install glfwSetWindowFocusCallback() callback and forward it to the backend via ImGui_ImplGlfw_CursorEnterCallback().
  35. // 2021-06-29: Reorganized backend to pull data from a single structure to facilitate usage with multiple-contexts (all g_XXXX access changed to bd->XXXX).
  36. // 2020-01-17: Inputs: Disable error callback while assigning mouse cursors because some X11 setup don't have them and it generates errors.
  37. // 2019-12-05: Inputs: Added support for new mouse cursors added in GLFW 3.4+ (resizing cursors, not allowed cursor).
  38. // 2019-10-18: Misc: Previously installed user callbacks are now restored on shutdown.
  39. // 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
  40. // 2019-05-11: Inputs: Don't filter value from character callback before calling AddInputCharacter().
  41. // 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized.
  42. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
  43. // 2018-11-07: Inputs: When installing our GLFW callbacks, we save user's previously installed ones - if any - and chain call them.
  44. // 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls.
  45. // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
  46. // 2018-06-08: Misc: Extracted imgui_impl_glfw.cpp/.h away from the old combined GLFW+OpenGL/Vulkan examples.
  47. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  48. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value, passed to glfwSetCursor()).
  49. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  50. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  51. // 2018-01-25: Inputs: Added gamepad support if ImGuiConfigFlags_NavEnableGamepad is set.
  52. // 2018-01-25: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
  53. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  54. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  55. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  56. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  57. #include "imgui.h"
  58. #include "imgui_impl_glfw.h"
  59. // Clang warnings with -Weverything
  60. #if defined(__clang__)
  61. #pragma clang diagnostic push
  62. #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
  63. #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
  64. #endif
  65. // GLFW
  66. #include <GLFW/glfw3.h>
  67. #ifdef _WIN32
  68. #undef APIENTRY
  69. #define GLFW_EXPOSE_NATIVE_WIN32
  70. #include <GLFW/glfw3native.h> // for glfwGetWin32Window()
  71. #endif
  72. #ifdef __APPLE__
  73. #define GLFW_EXPOSE_NATIVE_COCOA
  74. #include <GLFW/glfw3native.h> // for glfwGetCocoaWindow()
  75. #endif
  76. #define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ GLFW_FLOATING
  77. #define GLFW_HAS_WINDOW_HOVERED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ GLFW_HOVERED
  78. #define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity
  79. #define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale
  80. #define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface
  81. #define GLFW_HAS_FOCUS_WINDOW (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwFocusWindow
  82. #define GLFW_HAS_FOCUS_ON_SHOW (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ GLFW_FOCUS_ON_SHOW
  83. #define GLFW_HAS_MONITOR_WORK_AREA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorWorkarea
  84. #define GLFW_HAS_OSX_WINDOW_POS_FIX (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 + GLFW_VERSION_REVISION * 10 >= 3310) // 3.3.1+ Fixed: Resizing window repositions it on MacOS #1553
  85. #ifdef GLFW_RESIZE_NESW_CURSOR // Let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2019-11-29 (cursors defines) // FIXME: Remove when GLFW 3.4 is released?
  86. #define GLFW_HAS_NEW_CURSORS (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3400) // 3.4+ GLFW_RESIZE_ALL_CURSOR, GLFW_RESIZE_NESW_CURSOR, GLFW_RESIZE_NWSE_CURSOR, GLFW_NOT_ALLOWED_CURSOR
  87. #else
  88. #define GLFW_HAS_NEW_CURSORS (0)
  89. #endif
  90. #ifdef GLFW_MOUSE_PASSTHROUGH // Let's be nice to people who pulled GLFW between 2019-04-16 (3.4 define) and 2020-07-17 (passthrough)
  91. #define GLFW_HAS_MOUSE_PASSTHROUGH (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3400) // 3.4+ GLFW_MOUSE_PASSTHROUGH
  92. #else
  93. #define GLFW_HAS_MOUSE_PASSTHROUGH (0)
  94. #endif
  95. #define GLFW_HAS_GAMEPAD_API (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetGamepadState() new api
  96. #define GLFW_HAS_GET_KEY_NAME (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwGetKeyName()
  97. // GLFW data
  98. enum GlfwClientApi
  99. {
  100. GlfwClientApi_Unknown,
  101. GlfwClientApi_OpenGL,
  102. GlfwClientApi_Vulkan
  103. };
  104. struct ImGui_ImplGlfw_Data
  105. {
  106. GLFWwindow* Window;
  107. GlfwClientApi ClientApi;
  108. double Time;
  109. GLFWwindow* MouseWindow;
  110. GLFWcursor* MouseCursors[ImGuiMouseCursor_COUNT];
  111. ImVec2 LastValidMousePos;
  112. GLFWwindow* KeyOwnerWindows[GLFW_KEY_LAST];
  113. bool InstalledCallbacks;
  114. bool WantUpdateMonitors;
  115. #ifdef _WIN32
  116. WNDPROC GlfwWndProc;
  117. #endif
  118. // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
  119. GLFWwindowfocusfun PrevUserCallbackWindowFocus;
  120. GLFWcursorposfun PrevUserCallbackCursorPos;
  121. GLFWcursorenterfun PrevUserCallbackCursorEnter;
  122. GLFWmousebuttonfun PrevUserCallbackMousebutton;
  123. GLFWscrollfun PrevUserCallbackScroll;
  124. GLFWkeyfun PrevUserCallbackKey;
  125. GLFWcharfun PrevUserCallbackChar;
  126. GLFWmonitorfun PrevUserCallbackMonitor;
  127. ImGui_ImplGlfw_Data() { memset((void*)this, 0, sizeof(*this)); }
  128. };
  129. // Backend data stored in io.BackendPlatformUserData to allow support for multiple Dear ImGui contexts
  130. // It is STRONGLY preferred that you use docking branch with multi-viewports (== single Dear ImGui context + multiple windows) instead of multiple Dear ImGui contexts.
  131. // FIXME: multi-context support is not well tested and probably dysfunctional in this backend.
  132. // - Because glfwPollEvents() process all windows and some events may be called outside of it, you will need to register your own callbacks
  133. // (passing install_callbacks=false in ImGui_ImplGlfw_InitXXX functions), set the current dear imgui context and then call our callbacks.
  134. // - Otherwise we may need to store a GLFWWindow* -> ImGuiContext* map and handle this in the backend, adding a little bit of extra complexity to it.
  135. // FIXME: some shared resources (mouse cursor shape, gamepad) are mishandled when using multi-context.
  136. static ImGui_ImplGlfw_Data* ImGui_ImplGlfw_GetBackendData()
  137. {
  138. return ImGui::GetCurrentContext() ? (ImGui_ImplGlfw_Data*)ImGui::GetIO().BackendPlatformUserData : nullptr;
  139. }
  140. // Forward Declarations
  141. static void ImGui_ImplGlfw_UpdateMonitors();
  142. static void ImGui_ImplGlfw_InitPlatformInterface();
  143. static void ImGui_ImplGlfw_ShutdownPlatformInterface();
  144. // Functions
  145. static const char* ImGui_ImplGlfw_GetClipboardText(void* user_data)
  146. {
  147. return glfwGetClipboardString((GLFWwindow*)user_data);
  148. }
  149. static void ImGui_ImplGlfw_SetClipboardText(void* user_data, const char* text)
  150. {
  151. glfwSetClipboardString((GLFWwindow*)user_data, text);
  152. }
  153. static ImGuiKey ImGui_ImplGlfw_KeyToImGuiKey(int key)
  154. {
  155. switch (key)
  156. {
  157. case GLFW_KEY_TAB: return ImGuiKey_Tab;
  158. case GLFW_KEY_LEFT: return ImGuiKey_LeftArrow;
  159. case GLFW_KEY_RIGHT: return ImGuiKey_RightArrow;
  160. case GLFW_KEY_UP: return ImGuiKey_UpArrow;
  161. case GLFW_KEY_DOWN: return ImGuiKey_DownArrow;
  162. case GLFW_KEY_PAGE_UP: return ImGuiKey_PageUp;
  163. case GLFW_KEY_PAGE_DOWN: return ImGuiKey_PageDown;
  164. case GLFW_KEY_HOME: return ImGuiKey_Home;
  165. case GLFW_KEY_END: return ImGuiKey_End;
  166. case GLFW_KEY_INSERT: return ImGuiKey_Insert;
  167. case GLFW_KEY_DELETE: return ImGuiKey_Delete;
  168. case GLFW_KEY_BACKSPACE: return ImGuiKey_Backspace;
  169. case GLFW_KEY_SPACE: return ImGuiKey_Space;
  170. case GLFW_KEY_ENTER: return ImGuiKey_Enter;
  171. case GLFW_KEY_ESCAPE: return ImGuiKey_Escape;
  172. case GLFW_KEY_APOSTROPHE: return ImGuiKey_Apostrophe;
  173. case GLFW_KEY_COMMA: return ImGuiKey_Comma;
  174. case GLFW_KEY_MINUS: return ImGuiKey_Minus;
  175. case GLFW_KEY_PERIOD: return ImGuiKey_Period;
  176. case GLFW_KEY_SLASH: return ImGuiKey_Slash;
  177. case GLFW_KEY_SEMICOLON: return ImGuiKey_Semicolon;
  178. case GLFW_KEY_EQUAL: return ImGuiKey_Equal;
  179. case GLFW_KEY_LEFT_BRACKET: return ImGuiKey_LeftBracket;
  180. case GLFW_KEY_BACKSLASH: return ImGuiKey_Backslash;
  181. case GLFW_KEY_RIGHT_BRACKET: return ImGuiKey_RightBracket;
  182. case GLFW_KEY_GRAVE_ACCENT: return ImGuiKey_GraveAccent;
  183. case GLFW_KEY_CAPS_LOCK: return ImGuiKey_CapsLock;
  184. case GLFW_KEY_SCROLL_LOCK: return ImGuiKey_ScrollLock;
  185. case GLFW_KEY_NUM_LOCK: return ImGuiKey_NumLock;
  186. case GLFW_KEY_PRINT_SCREEN: return ImGuiKey_PrintScreen;
  187. case GLFW_KEY_PAUSE: return ImGuiKey_Pause;
  188. case GLFW_KEY_KP_0: return ImGuiKey_Keypad0;
  189. case GLFW_KEY_KP_1: return ImGuiKey_Keypad1;
  190. case GLFW_KEY_KP_2: return ImGuiKey_Keypad2;
  191. case GLFW_KEY_KP_3: return ImGuiKey_Keypad3;
  192. case GLFW_KEY_KP_4: return ImGuiKey_Keypad4;
  193. case GLFW_KEY_KP_5: return ImGuiKey_Keypad5;
  194. case GLFW_KEY_KP_6: return ImGuiKey_Keypad6;
  195. case GLFW_KEY_KP_7: return ImGuiKey_Keypad7;
  196. case GLFW_KEY_KP_8: return ImGuiKey_Keypad8;
  197. case GLFW_KEY_KP_9: return ImGuiKey_Keypad9;
  198. case GLFW_KEY_KP_DECIMAL: return ImGuiKey_KeypadDecimal;
  199. case GLFW_KEY_KP_DIVIDE: return ImGuiKey_KeypadDivide;
  200. case GLFW_KEY_KP_MULTIPLY: return ImGuiKey_KeypadMultiply;
  201. case GLFW_KEY_KP_SUBTRACT: return ImGuiKey_KeypadSubtract;
  202. case GLFW_KEY_KP_ADD: return ImGuiKey_KeypadAdd;
  203. case GLFW_KEY_KP_ENTER: return ImGuiKey_KeypadEnter;
  204. case GLFW_KEY_KP_EQUAL: return ImGuiKey_KeypadEqual;
  205. case GLFW_KEY_LEFT_SHIFT: return ImGuiKey_LeftShift;
  206. case GLFW_KEY_LEFT_CONTROL: return ImGuiKey_LeftCtrl;
  207. case GLFW_KEY_LEFT_ALT: return ImGuiKey_LeftAlt;
  208. case GLFW_KEY_LEFT_SUPER: return ImGuiKey_LeftSuper;
  209. case GLFW_KEY_RIGHT_SHIFT: return ImGuiKey_RightShift;
  210. case GLFW_KEY_RIGHT_CONTROL: return ImGuiKey_RightCtrl;
  211. case GLFW_KEY_RIGHT_ALT: return ImGuiKey_RightAlt;
  212. case GLFW_KEY_RIGHT_SUPER: return ImGuiKey_RightSuper;
  213. case GLFW_KEY_MENU: return ImGuiKey_Menu;
  214. case GLFW_KEY_0: return ImGuiKey_0;
  215. case GLFW_KEY_1: return ImGuiKey_1;
  216. case GLFW_KEY_2: return ImGuiKey_2;
  217. case GLFW_KEY_3: return ImGuiKey_3;
  218. case GLFW_KEY_4: return ImGuiKey_4;
  219. case GLFW_KEY_5: return ImGuiKey_5;
  220. case GLFW_KEY_6: return ImGuiKey_6;
  221. case GLFW_KEY_7: return ImGuiKey_7;
  222. case GLFW_KEY_8: return ImGuiKey_8;
  223. case GLFW_KEY_9: return ImGuiKey_9;
  224. case GLFW_KEY_A: return ImGuiKey_A;
  225. case GLFW_KEY_B: return ImGuiKey_B;
  226. case GLFW_KEY_C: return ImGuiKey_C;
  227. case GLFW_KEY_D: return ImGuiKey_D;
  228. case GLFW_KEY_E: return ImGuiKey_E;
  229. case GLFW_KEY_F: return ImGuiKey_F;
  230. case GLFW_KEY_G: return ImGuiKey_G;
  231. case GLFW_KEY_H: return ImGuiKey_H;
  232. case GLFW_KEY_I: return ImGuiKey_I;
  233. case GLFW_KEY_J: return ImGuiKey_J;
  234. case GLFW_KEY_K: return ImGuiKey_K;
  235. case GLFW_KEY_L: return ImGuiKey_L;
  236. case GLFW_KEY_M: return ImGuiKey_M;
  237. case GLFW_KEY_N: return ImGuiKey_N;
  238. case GLFW_KEY_O: return ImGuiKey_O;
  239. case GLFW_KEY_P: return ImGuiKey_P;
  240. case GLFW_KEY_Q: return ImGuiKey_Q;
  241. case GLFW_KEY_R: return ImGuiKey_R;
  242. case GLFW_KEY_S: return ImGuiKey_S;
  243. case GLFW_KEY_T: return ImGuiKey_T;
  244. case GLFW_KEY_U: return ImGuiKey_U;
  245. case GLFW_KEY_V: return ImGuiKey_V;
  246. case GLFW_KEY_W: return ImGuiKey_W;
  247. case GLFW_KEY_X: return ImGuiKey_X;
  248. case GLFW_KEY_Y: return ImGuiKey_Y;
  249. case GLFW_KEY_Z: return ImGuiKey_Z;
  250. case GLFW_KEY_F1: return ImGuiKey_F1;
  251. case GLFW_KEY_F2: return ImGuiKey_F2;
  252. case GLFW_KEY_F3: return ImGuiKey_F3;
  253. case GLFW_KEY_F4: return ImGuiKey_F4;
  254. case GLFW_KEY_F5: return ImGuiKey_F5;
  255. case GLFW_KEY_F6: return ImGuiKey_F6;
  256. case GLFW_KEY_F7: return ImGuiKey_F7;
  257. case GLFW_KEY_F8: return ImGuiKey_F8;
  258. case GLFW_KEY_F9: return ImGuiKey_F9;
  259. case GLFW_KEY_F10: return ImGuiKey_F10;
  260. case GLFW_KEY_F11: return ImGuiKey_F11;
  261. case GLFW_KEY_F12: return ImGuiKey_F12;
  262. default: return ImGuiKey_None;
  263. }
  264. }
  265. static int ImGui_ImplGlfw_KeyToModifier(int key)
  266. {
  267. if (key == GLFW_KEY_LEFT_CONTROL || key == GLFW_KEY_RIGHT_CONTROL)
  268. return GLFW_MOD_CONTROL;
  269. if (key == GLFW_KEY_LEFT_SHIFT || key == GLFW_KEY_RIGHT_SHIFT)
  270. return GLFW_MOD_SHIFT;
  271. if (key == GLFW_KEY_LEFT_ALT || key == GLFW_KEY_RIGHT_ALT)
  272. return GLFW_MOD_ALT;
  273. if (key == GLFW_KEY_LEFT_SUPER || key == GLFW_KEY_RIGHT_SUPER)
  274. return GLFW_MOD_SUPER;
  275. return 0;
  276. }
  277. static void ImGui_ImplGlfw_UpdateKeyModifiers(int mods)
  278. {
  279. ImGuiIO& io = ImGui::GetIO();
  280. io.AddKeyEvent(ImGuiMod_Ctrl, (mods & GLFW_MOD_CONTROL) != 0);
  281. io.AddKeyEvent(ImGuiMod_Shift, (mods & GLFW_MOD_SHIFT) != 0);
  282. io.AddKeyEvent(ImGuiMod_Alt, (mods & GLFW_MOD_ALT) != 0);
  283. io.AddKeyEvent(ImGuiMod_Super, (mods & GLFW_MOD_SUPER) != 0);
  284. }
  285. void ImGui_ImplGlfw_MouseButtonCallback(GLFWwindow* window, int button, int action, int mods)
  286. {
  287. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  288. if (bd->PrevUserCallbackMousebutton != nullptr && window == bd->Window)
  289. bd->PrevUserCallbackMousebutton(window, button, action, mods);
  290. ImGui_ImplGlfw_UpdateKeyModifiers(mods);
  291. ImGuiIO& io = ImGui::GetIO();
  292. if (button >= 0 && button < ImGuiMouseButton_COUNT)
  293. io.AddMouseButtonEvent(button, action == GLFW_PRESS);
  294. }
  295. void ImGui_ImplGlfw_ScrollCallback(GLFWwindow* window, double xoffset, double yoffset)
  296. {
  297. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  298. if (bd->PrevUserCallbackScroll != nullptr && window == bd->Window)
  299. bd->PrevUserCallbackScroll(window, xoffset, yoffset);
  300. ImGuiIO& io = ImGui::GetIO();
  301. io.AddMouseWheelEvent((float)xoffset, (float)yoffset);
  302. }
  303. static int ImGui_ImplGlfw_TranslateUntranslatedKey(int key, int scancode)
  304. {
  305. #if GLFW_HAS_GET_KEY_NAME && !defined(__EMSCRIPTEN__)
  306. // GLFW 3.1+ attempts to "untranslate" keys, which goes the opposite of what every other framework does, making using lettered shortcuts difficult.
  307. // (It had reasons to do so: namely GLFW is/was more likely to be used for WASD-type game controls rather than lettered shortcuts, but IHMO the 3.1 change could have been done differently)
  308. // See https://github.com/glfw/glfw/issues/1502 for details.
  309. // Adding a workaround to undo this (so our keys are translated->untranslated->translated, likely a lossy process).
  310. // This won't cover edge cases but this is at least going to cover common cases.
  311. if (key >= GLFW_KEY_KP_0 && key <= GLFW_KEY_KP_EQUAL)
  312. return key;
  313. const char* key_name = glfwGetKeyName(key, scancode);
  314. if (key_name && key_name[0] != 0 && key_name[1] == 0)
  315. {
  316. const char char_names[] = "`-=[]\\,;\'./";
  317. const int char_keys[] = { GLFW_KEY_GRAVE_ACCENT, GLFW_KEY_MINUS, GLFW_KEY_EQUAL, GLFW_KEY_LEFT_BRACKET, GLFW_KEY_RIGHT_BRACKET, GLFW_KEY_BACKSLASH, GLFW_KEY_COMMA, GLFW_KEY_SEMICOLON, GLFW_KEY_APOSTROPHE, GLFW_KEY_PERIOD, GLFW_KEY_SLASH, 0 };
  318. IM_ASSERT(IM_ARRAYSIZE(char_names) == IM_ARRAYSIZE(char_keys));
  319. if (key_name[0] >= '0' && key_name[0] <= '9') { key = GLFW_KEY_0 + (key_name[0] - '0'); }
  320. else if (key_name[0] >= 'A' && key_name[0] <= 'Z') { key = GLFW_KEY_A + (key_name[0] - 'A'); }
  321. else if (key_name[0] >= 'a' && key_name[0] <= 'z') { key = GLFW_KEY_A + (key_name[0] - 'a'); }
  322. else if (const char* p = strchr(char_names, key_name[0])) { key = char_keys[p - char_names]; }
  323. }
  324. // if (action == GLFW_PRESS) printf("key %d scancode %d name '%s'\n", key, scancode, key_name);
  325. #else
  326. IM_UNUSED(scancode);
  327. #endif
  328. return key;
  329. }
  330. void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int keycode, int scancode, int action, int mods)
  331. {
  332. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  333. if (bd->PrevUserCallbackKey != nullptr && window == bd->Window)
  334. bd->PrevUserCallbackKey(window, keycode, scancode, action, mods);
  335. if (action != GLFW_PRESS && action != GLFW_RELEASE)
  336. return;
  337. // Workaround: X11 does not include current pressed/released modifier key in 'mods' flags. https://github.com/glfw/glfw/issues/1630
  338. if (int keycode_to_mod = ImGui_ImplGlfw_KeyToModifier(keycode))
  339. mods = (action == GLFW_PRESS) ? (mods | keycode_to_mod) : (mods & ~keycode_to_mod);
  340. ImGui_ImplGlfw_UpdateKeyModifiers(mods);
  341. if (keycode >= 0 && keycode < IM_ARRAYSIZE(bd->KeyOwnerWindows))
  342. bd->KeyOwnerWindows[keycode] = (action == GLFW_PRESS) ? window : nullptr;
  343. keycode = ImGui_ImplGlfw_TranslateUntranslatedKey(keycode, scancode);
  344. ImGuiIO& io = ImGui::GetIO();
  345. ImGuiKey imgui_key = ImGui_ImplGlfw_KeyToImGuiKey(keycode);
  346. io.AddKeyEvent(imgui_key, (action == GLFW_PRESS));
  347. io.SetKeyEventNativeData(imgui_key, keycode, scancode); // To support legacy indexing (<1.87 user code)
  348. }
  349. void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused)
  350. {
  351. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  352. if (bd->PrevUserCallbackWindowFocus != nullptr && window == bd->Window)
  353. bd->PrevUserCallbackWindowFocus(window, focused);
  354. ImGuiIO& io = ImGui::GetIO();
  355. io.AddFocusEvent(focused != 0);
  356. }
  357. void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y)
  358. {
  359. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  360. if (bd->PrevUserCallbackCursorPos != nullptr && window == bd->Window)
  361. bd->PrevUserCallbackCursorPos(window, x, y);
  362. if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
  363. return;
  364. ImGuiIO& io = ImGui::GetIO();
  365. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  366. {
  367. int window_x, window_y;
  368. glfwGetWindowPos(window, &window_x, &window_y);
  369. x += window_x;
  370. y += window_y;
  371. }
  372. io.AddMousePosEvent((float)x, (float)y);
  373. bd->LastValidMousePos = ImVec2((float)x, (float)y);
  374. }
  375. // Workaround: X11 seems to send spurious Leave/Enter events which would make us lose our position,
  376. // so we back it up and restore on Leave/Enter (see https://github.com/ocornut/imgui/issues/4984)
  377. void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered)
  378. {
  379. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  380. if (bd->PrevUserCallbackCursorEnter != nullptr && window == bd->Window)
  381. bd->PrevUserCallbackCursorEnter(window, entered);
  382. if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
  383. return;
  384. ImGuiIO& io = ImGui::GetIO();
  385. if (entered)
  386. {
  387. bd->MouseWindow = window;
  388. io.AddMousePosEvent(bd->LastValidMousePos.x, bd->LastValidMousePos.y);
  389. }
  390. else if (!entered && bd->MouseWindow == window)
  391. {
  392. bd->LastValidMousePos = io.MousePos;
  393. bd->MouseWindow = nullptr;
  394. io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
  395. }
  396. }
  397. void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c)
  398. {
  399. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  400. if (bd->PrevUserCallbackChar != nullptr && window == bd->Window)
  401. bd->PrevUserCallbackChar(window, c);
  402. ImGuiIO& io = ImGui::GetIO();
  403. io.AddInputCharacter(c);
  404. }
  405. void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int)
  406. {
  407. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  408. bd->WantUpdateMonitors = true;
  409. }
  410. void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window)
  411. {
  412. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  413. IM_ASSERT(bd->InstalledCallbacks == false && "Callbacks already installed!");
  414. IM_ASSERT(bd->Window == window);
  415. bd->PrevUserCallbackWindowFocus = glfwSetWindowFocusCallback(window, ImGui_ImplGlfw_WindowFocusCallback);
  416. bd->PrevUserCallbackCursorEnter = glfwSetCursorEnterCallback(window, ImGui_ImplGlfw_CursorEnterCallback);
  417. bd->PrevUserCallbackCursorPos = glfwSetCursorPosCallback(window, ImGui_ImplGlfw_CursorPosCallback);
  418. bd->PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
  419. bd->PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
  420. bd->PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback);
  421. bd->PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
  422. bd->PrevUserCallbackMonitor = glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback);
  423. bd->InstalledCallbacks = true;
  424. }
  425. void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window)
  426. {
  427. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  428. IM_ASSERT(bd->InstalledCallbacks == true && "Callbacks not installed!");
  429. IM_ASSERT(bd->Window == window);
  430. glfwSetWindowFocusCallback(window, bd->PrevUserCallbackWindowFocus);
  431. glfwSetCursorEnterCallback(window, bd->PrevUserCallbackCursorEnter);
  432. glfwSetCursorPosCallback(window, bd->PrevUserCallbackCursorPos);
  433. glfwSetMouseButtonCallback(window, bd->PrevUserCallbackMousebutton);
  434. glfwSetScrollCallback(window, bd->PrevUserCallbackScroll);
  435. glfwSetKeyCallback(window, bd->PrevUserCallbackKey);
  436. glfwSetCharCallback(window, bd->PrevUserCallbackChar);
  437. glfwSetMonitorCallback(bd->PrevUserCallbackMonitor);
  438. bd->InstalledCallbacks = false;
  439. bd->PrevUserCallbackWindowFocus = nullptr;
  440. bd->PrevUserCallbackCursorEnter = nullptr;
  441. bd->PrevUserCallbackCursorPos = nullptr;
  442. bd->PrevUserCallbackMousebutton = nullptr;
  443. bd->PrevUserCallbackScroll = nullptr;
  444. bd->PrevUserCallbackKey = nullptr;
  445. bd->PrevUserCallbackChar = nullptr;
  446. bd->PrevUserCallbackMonitor = nullptr;
  447. }
  448. static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api)
  449. {
  450. ImGuiIO& io = ImGui::GetIO();
  451. IM_ASSERT(io.BackendPlatformUserData == nullptr && "Already initialized a platform backend!");
  452. // Setup backend capabilities flags
  453. ImGui_ImplGlfw_Data* bd = IM_NEW(ImGui_ImplGlfw_Data)();
  454. io.BackendPlatformUserData = (void*)bd;
  455. io.BackendPlatformName = "imgui_impl_glfw";
  456. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  457. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  458. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  459. #if GLFW_HAS_MOUSE_PASSTHROUGH || (GLFW_HAS_WINDOW_HOVERED && defined(_WIN32))
  460. io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can call io.AddMouseViewportEvent() with correct data (optional)
  461. #endif
  462. bd->Window = window;
  463. bd->Time = 0.0;
  464. bd->WantUpdateMonitors = true;
  465. io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
  466. io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
  467. io.ClipboardUserData = bd->Window;
  468. // Create mouse cursors
  469. // (By design, on X11 cursors are user configurable and some cursors may be missing. When a cursor doesn't exist,
  470. // GLFW will emit an error which will often be printed by the app, so we temporarily disable error reporting.
  471. // Missing cursors will return nullptr and our _UpdateMouseCursor() function will use the Arrow cursor instead.)
  472. GLFWerrorfun prev_error_callback = glfwSetErrorCallback(nullptr);
  473. bd->MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  474. bd->MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
  475. bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR);
  476. bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
  477. bd->MouseCursors[ImGuiMouseCursor_Hand] = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
  478. #if GLFW_HAS_NEW_CURSORS
  479. bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_RESIZE_ALL_CURSOR);
  480. bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_RESIZE_NESW_CURSOR);
  481. bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_RESIZE_NWSE_CURSOR);
  482. bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_NOT_ALLOWED_CURSOR);
  483. #else
  484. bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  485. bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  486. bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  487. bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  488. #endif
  489. glfwSetErrorCallback(prev_error_callback);
  490. // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
  491. if (install_callbacks)
  492. ImGui_ImplGlfw_InstallCallbacks(window);
  493. // Update monitors the first time (note: monitor callback are broken in GLFW 3.2 and earlier, see github.com/glfw/glfw/issues/784)
  494. ImGui_ImplGlfw_UpdateMonitors();
  495. glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback);
  496. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  497. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  498. main_viewport->PlatformHandle = (void*)bd->Window;
  499. #ifdef _WIN32
  500. main_viewport->PlatformHandleRaw = glfwGetWin32Window(bd->Window);
  501. #elif defined(__APPLE__)
  502. main_viewport->PlatformHandleRaw = (void*)glfwGetCocoaWindow(bd->Window);
  503. #endif
  504. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  505. ImGui_ImplGlfw_InitPlatformInterface();
  506. bd->ClientApi = client_api;
  507. return true;
  508. }
  509. bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks)
  510. {
  511. return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_OpenGL);
  512. }
  513. bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks)
  514. {
  515. return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Vulkan);
  516. }
  517. bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks)
  518. {
  519. return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Unknown);
  520. }
  521. void ImGui_ImplGlfw_Shutdown()
  522. {
  523. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  524. IM_ASSERT(bd != nullptr && "No platform backend to shutdown, or already shutdown?");
  525. ImGuiIO& io = ImGui::GetIO();
  526. ImGui_ImplGlfw_ShutdownPlatformInterface();
  527. if (bd->InstalledCallbacks)
  528. ImGui_ImplGlfw_RestoreCallbacks(bd->Window);
  529. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
  530. glfwDestroyCursor(bd->MouseCursors[cursor_n]);
  531. io.BackendPlatformName = nullptr;
  532. io.BackendPlatformUserData = nullptr;
  533. IM_DELETE(bd);
  534. }
  535. static void ImGui_ImplGlfw_UpdateMouseData()
  536. {
  537. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  538. ImGuiIO& io = ImGui::GetIO();
  539. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  540. if (glfwGetInputMode(bd->Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
  541. {
  542. io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
  543. return;
  544. }
  545. ImGuiID mouse_viewport_id = 0;
  546. const ImVec2 mouse_pos_prev = io.MousePos;
  547. for (int n = 0; n < platform_io.Viewports.Size; n++)
  548. {
  549. ImGuiViewport* viewport = platform_io.Viewports[n];
  550. GLFWwindow* window = (GLFWwindow*)viewport->PlatformHandle;
  551. #ifdef __EMSCRIPTEN__
  552. const bool is_window_focused = true;
  553. #else
  554. const bool is_window_focused = glfwGetWindowAttrib(window, GLFW_FOCUSED) != 0;
  555. #endif
  556. if (is_window_focused)
  557. {
  558. // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  559. // When multi-viewports are enabled, all Dear ImGui positions are same as OS positions.
  560. if (io.WantSetMousePos)
  561. glfwSetCursorPos(window, (double)(mouse_pos_prev.x - viewport->Pos.x), (double)(mouse_pos_prev.y - viewport->Pos.y));
  562. // (Optional) Fallback to provide mouse position when focused (ImGui_ImplGlfw_CursorPosCallback already provides this when hovered or captured)
  563. if (bd->MouseWindow == nullptr)
  564. {
  565. double mouse_x, mouse_y;
  566. glfwGetCursorPos(window, &mouse_x, &mouse_y);
  567. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  568. {
  569. // Single viewport mode: mouse position in client window coordinates (io.MousePos is (0,0) when the mouse is on the upper-left corner of the app window)
  570. // Multi-viewport mode: mouse position in OS absolute coordinates (io.MousePos is (0,0) when the mouse is on the upper-left of the primary monitor)
  571. int window_x, window_y;
  572. glfwGetWindowPos(window, &window_x, &window_y);
  573. mouse_x += window_x;
  574. mouse_y += window_y;
  575. }
  576. bd->LastValidMousePos = ImVec2((float)mouse_x, (float)mouse_y);
  577. io.AddMousePosEvent((float)mouse_x, (float)mouse_y);
  578. }
  579. }
  580. // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering.
  581. // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic.
  582. // - [X] GLFW >= 3.3 backend ON WINDOWS ONLY does correctly ignore viewports with the _NoInputs flag.
  583. // - [!] GLFW <= 3.2 backend CANNOT correctly ignore viewports with the _NoInputs flag, and CANNOT reported Hovered Viewport because of mouse capture.
  584. // Some backend are not able to handle that correctly. If a backend report an hovered viewport that has the _NoInputs flag (e.g. when dragging a window
  585. // for docking, the viewport has the _NoInputs flag in order to allow us to find the viewport under), then Dear ImGui is forced to ignore the value reported
  586. // by the backend, and use its flawed heuristic to guess the viewport behind.
  587. // - [X] GLFW backend correctly reports this regardless of another viewport behind focused and dragged from (we need this to find a useful drag and drop target).
  588. // FIXME: This is currently only correct on Win32. See what we do below with the WM_NCHITTEST, missing an equivalent for other systems.
  589. // See https://github.com/glfw/glfw/issues/1236 if you want to help in making this a GLFW feature.
  590. #if GLFW_HAS_MOUSE_PASSTHROUGH || (GLFW_HAS_WINDOW_HOVERED && defined(_WIN32))
  591. const bool window_no_input = (viewport->Flags & ImGuiViewportFlags_NoInputs) != 0;
  592. #if GLFW_HAS_MOUSE_PASSTHROUGH
  593. glfwSetWindowAttrib(window, GLFW_MOUSE_PASSTHROUGH, window_no_input);
  594. #endif
  595. if (glfwGetWindowAttrib(window, GLFW_HOVERED) && !window_no_input)
  596. mouse_viewport_id = viewport->ID;
  597. #else
  598. // We cannot use bd->MouseWindow maintained from CursorEnter/Leave callbacks, because it is locked to the window capturing mouse.
  599. #endif
  600. }
  601. if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)
  602. io.AddMouseViewportEvent(mouse_viewport_id);
  603. }
  604. static void ImGui_ImplGlfw_UpdateMouseCursor()
  605. {
  606. ImGuiIO& io = ImGui::GetIO();
  607. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  608. if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(bd->Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
  609. return;
  610. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  611. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  612. for (int n = 0; n < platform_io.Viewports.Size; n++)
  613. {
  614. GLFWwindow* window = (GLFWwindow*)platform_io.Viewports[n]->PlatformHandle;
  615. if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
  616. {
  617. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  618. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  619. }
  620. else
  621. {
  622. // Show OS mouse cursor
  623. // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here.
  624. glfwSetCursor(window, bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]);
  625. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  626. }
  627. }
  628. }
  629. // Update gamepad inputs
  630. static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
  631. static void ImGui_ImplGlfw_UpdateGamepads()
  632. {
  633. ImGuiIO& io = ImGui::GetIO();
  634. if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
  635. return;
  636. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  637. #if GLFW_HAS_GAMEPAD_API
  638. GLFWgamepadstate gamepad;
  639. if (!glfwGetGamepadState(GLFW_JOYSTICK_1, &gamepad))
  640. return;
  641. #define MAP_BUTTON(KEY_NO, BUTTON_NO, _UNUSED) do { io.AddKeyEvent(KEY_NO, gamepad.buttons[BUTTON_NO] != 0); } while (0)
  642. #define MAP_ANALOG(KEY_NO, AXIS_NO, _UNUSED, V0, V1) do { float v = gamepad.axes[AXIS_NO]; v = (v - V0) / (V1 - V0); io.AddKeyAnalogEvent(KEY_NO, v > 0.10f, Saturate(v)); } while (0)
  643. #else
  644. int axes_count = 0, buttons_count = 0;
  645. const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
  646. const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
  647. if (axes_count == 0 || buttons_count == 0)
  648. return;
  649. #define MAP_BUTTON(KEY_NO, _UNUSED, BUTTON_NO) do { io.AddKeyEvent(KEY_NO, (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS)); } while (0)
  650. #define MAP_ANALOG(KEY_NO, _UNUSED, AXIS_NO, V0, V1) do { float v = (axes_count > AXIS_NO) ? axes[AXIS_NO] : V0; v = (v - V0) / (V1 - V0); io.AddKeyAnalogEvent(KEY_NO, v > 0.10f, Saturate(v)); } while (0)
  651. #endif
  652. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  653. MAP_BUTTON(ImGuiKey_GamepadStart, GLFW_GAMEPAD_BUTTON_START, 7);
  654. MAP_BUTTON(ImGuiKey_GamepadBack, GLFW_GAMEPAD_BUTTON_BACK, 6);
  655. MAP_BUTTON(ImGuiKey_GamepadFaceLeft, GLFW_GAMEPAD_BUTTON_X, 2); // Xbox X, PS Square
  656. MAP_BUTTON(ImGuiKey_GamepadFaceRight, GLFW_GAMEPAD_BUTTON_B, 1); // Xbox B, PS Circle
  657. MAP_BUTTON(ImGuiKey_GamepadFaceUp, GLFW_GAMEPAD_BUTTON_Y, 3); // Xbox Y, PS Triangle
  658. MAP_BUTTON(ImGuiKey_GamepadFaceDown, GLFW_GAMEPAD_BUTTON_A, 0); // Xbox A, PS Cross
  659. MAP_BUTTON(ImGuiKey_GamepadDpadLeft, GLFW_GAMEPAD_BUTTON_DPAD_LEFT, 13);
  660. MAP_BUTTON(ImGuiKey_GamepadDpadRight, GLFW_GAMEPAD_BUTTON_DPAD_RIGHT, 11);
  661. MAP_BUTTON(ImGuiKey_GamepadDpadUp, GLFW_GAMEPAD_BUTTON_DPAD_UP, 10);
  662. MAP_BUTTON(ImGuiKey_GamepadDpadDown, GLFW_GAMEPAD_BUTTON_DPAD_DOWN, 12);
  663. MAP_BUTTON(ImGuiKey_GamepadL1, GLFW_GAMEPAD_BUTTON_LEFT_BUMPER, 4);
  664. MAP_BUTTON(ImGuiKey_GamepadR1, GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER, 5);
  665. MAP_ANALOG(ImGuiKey_GamepadL2, GLFW_GAMEPAD_AXIS_LEFT_TRIGGER, 4, -0.75f, +1.0f);
  666. MAP_ANALOG(ImGuiKey_GamepadR2, GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, 5, -0.75f, +1.0f);
  667. MAP_BUTTON(ImGuiKey_GamepadL3, GLFW_GAMEPAD_BUTTON_LEFT_THUMB, 8);
  668. MAP_BUTTON(ImGuiKey_GamepadR3, GLFW_GAMEPAD_BUTTON_RIGHT_THUMB, 9);
  669. MAP_ANALOG(ImGuiKey_GamepadLStickLeft, GLFW_GAMEPAD_AXIS_LEFT_X, 0, -0.25f, -1.0f);
  670. MAP_ANALOG(ImGuiKey_GamepadLStickRight, GLFW_GAMEPAD_AXIS_LEFT_X, 0, +0.25f, +1.0f);
  671. MAP_ANALOG(ImGuiKey_GamepadLStickUp, GLFW_GAMEPAD_AXIS_LEFT_Y, 1, -0.25f, -1.0f);
  672. MAP_ANALOG(ImGuiKey_GamepadLStickDown, GLFW_GAMEPAD_AXIS_LEFT_Y, 1, +0.25f, +1.0f);
  673. MAP_ANALOG(ImGuiKey_GamepadRStickLeft, GLFW_GAMEPAD_AXIS_RIGHT_X, 2, -0.25f, -1.0f);
  674. MAP_ANALOG(ImGuiKey_GamepadRStickRight, GLFW_GAMEPAD_AXIS_RIGHT_X, 2, +0.25f, +1.0f);
  675. MAP_ANALOG(ImGuiKey_GamepadRStickUp, GLFW_GAMEPAD_AXIS_RIGHT_Y, 3, -0.25f, -1.0f);
  676. MAP_ANALOG(ImGuiKey_GamepadRStickDown, GLFW_GAMEPAD_AXIS_RIGHT_Y, 3, +0.25f, +1.0f);
  677. #undef MAP_BUTTON
  678. #undef MAP_ANALOG
  679. }
  680. static void ImGui_ImplGlfw_UpdateMonitors()
  681. {
  682. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  683. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  684. int monitors_count = 0;
  685. GLFWmonitor** glfw_monitors = glfwGetMonitors(&monitors_count);
  686. platform_io.Monitors.resize(0);
  687. for (int n = 0; n < monitors_count; n++)
  688. {
  689. ImGuiPlatformMonitor monitor;
  690. int x, y;
  691. glfwGetMonitorPos(glfw_monitors[n], &x, &y);
  692. const GLFWvidmode* vid_mode = glfwGetVideoMode(glfw_monitors[n]);
  693. monitor.MainPos = monitor.WorkPos = ImVec2((float)x, (float)y);
  694. monitor.MainSize = monitor.WorkSize = ImVec2((float)vid_mode->width, (float)vid_mode->height);
  695. #if GLFW_HAS_MONITOR_WORK_AREA
  696. int w, h;
  697. glfwGetMonitorWorkarea(glfw_monitors[n], &x, &y, &w, &h);
  698. if (w > 0 && h > 0) // Workaround a small GLFW issue reporting zero on monitor changes: https://github.com/glfw/glfw/pull/1761
  699. {
  700. monitor.WorkPos = ImVec2((float)x, (float)y);
  701. monitor.WorkSize = ImVec2((float)w, (float)h);
  702. }
  703. #endif
  704. #if GLFW_HAS_PER_MONITOR_DPI
  705. // 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.
  706. float x_scale, y_scale;
  707. glfwGetMonitorContentScale(glfw_monitors[n], &x_scale, &y_scale);
  708. monitor.DpiScale = x_scale;
  709. #endif
  710. platform_io.Monitors.push_back(monitor);
  711. }
  712. bd->WantUpdateMonitors = false;
  713. }
  714. void ImGui_ImplGlfw_NewFrame()
  715. {
  716. ImGuiIO& io = ImGui::GetIO();
  717. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  718. IM_ASSERT(bd != nullptr && "Did you call ImGui_ImplGlfw_InitForXXX()?");
  719. // Setup display size (every frame to accommodate for window resizing)
  720. int w, h;
  721. int display_w, display_h;
  722. glfwGetWindowSize(bd->Window, &w, &h);
  723. glfwGetFramebufferSize(bd->Window, &display_w, &display_h);
  724. io.DisplaySize = ImVec2((float)w, (float)h);
  725. if (w > 0 && h > 0)
  726. io.DisplayFramebufferScale = ImVec2((float)display_w / (float)w, (float)display_h / (float)h);
  727. if (bd->WantUpdateMonitors)
  728. ImGui_ImplGlfw_UpdateMonitors();
  729. // Setup time step
  730. double current_time = glfwGetTime();
  731. io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f);
  732. bd->Time = current_time;
  733. ImGui_ImplGlfw_UpdateMouseData();
  734. ImGui_ImplGlfw_UpdateMouseCursor();
  735. // Update game controllers (if enabled and available)
  736. ImGui_ImplGlfw_UpdateGamepads();
  737. }
  738. //--------------------------------------------------------------------------------------------------------
  739. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  740. // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
  741. // If you are new to dear imgui or creating a new binding for dear imgui, it is recommended that you completely ignore this section first..
  742. //--------------------------------------------------------------------------------------------------------
  743. // Helper structure we store in the void* RenderUserData field of each ImGuiViewport to easily retrieve our backend data.
  744. struct ImGui_ImplGlfw_ViewportData
  745. {
  746. GLFWwindow* Window;
  747. bool WindowOwned;
  748. int IgnoreWindowPosEventFrame;
  749. int IgnoreWindowSizeEventFrame;
  750. ImGui_ImplGlfw_ViewportData() { Window = nullptr; WindowOwned = false; IgnoreWindowSizeEventFrame = IgnoreWindowPosEventFrame = -1; }
  751. ~ImGui_ImplGlfw_ViewportData() { IM_ASSERT(Window == nullptr); }
  752. };
  753. static void ImGui_ImplGlfw_WindowCloseCallback(GLFWwindow* window)
  754. {
  755. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window))
  756. viewport->PlatformRequestClose = true;
  757. }
  758. // GLFW may dispatch window pos/size events after calling glfwSetWindowPos()/glfwSetWindowSize().
  759. // However: depending on the platform the callback may be invoked at different time:
  760. // - on Windows it appears to be called within the glfwSetWindowPos()/glfwSetWindowSize() call
  761. // - on Linux it is queued and invoked during glfwPollEvents()
  762. // Because the event doesn't always fire on glfwSetWindowXXX() we use a frame counter tag to only
  763. // ignore recent glfwSetWindowXXX() calls.
  764. static void ImGui_ImplGlfw_WindowPosCallback(GLFWwindow* window, int, int)
  765. {
  766. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window))
  767. {
  768. if (ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData)
  769. {
  770. bool ignore_event = (ImGui::GetFrameCount() <= vd->IgnoreWindowPosEventFrame + 1);
  771. //data->IgnoreWindowPosEventFrame = -1;
  772. if (ignore_event)
  773. return;
  774. }
  775. viewport->PlatformRequestMove = true;
  776. }
  777. }
  778. static void ImGui_ImplGlfw_WindowSizeCallback(GLFWwindow* window, int, int)
  779. {
  780. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window))
  781. {
  782. if (ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData)
  783. {
  784. bool ignore_event = (ImGui::GetFrameCount() <= vd->IgnoreWindowSizeEventFrame + 1);
  785. //data->IgnoreWindowSizeEventFrame = -1;
  786. if (ignore_event)
  787. return;
  788. }
  789. viewport->PlatformRequestResize = true;
  790. }
  791. }
  792. static void ImGui_ImplGlfw_CreateWindow(ImGuiViewport* viewport)
  793. {
  794. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  795. ImGui_ImplGlfw_ViewportData* vd = IM_NEW(ImGui_ImplGlfw_ViewportData)();
  796. viewport->PlatformUserData = vd;
  797. // GLFW 3.2 unfortunately always set focus on glfwCreateWindow() if GLFW_VISIBLE is set, regardless of GLFW_FOCUSED
  798. // With GLFW 3.3, the hint GLFW_FOCUS_ON_SHOW fixes this problem
  799. glfwWindowHint(GLFW_VISIBLE, false);
  800. glfwWindowHint(GLFW_FOCUSED, false);
  801. #if GLFW_HAS_FOCUS_ON_SHOW
  802. glfwWindowHint(GLFW_FOCUS_ON_SHOW, false);
  803. #endif
  804. glfwWindowHint(GLFW_DECORATED, (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? false : true);
  805. #if GLFW_HAS_WINDOW_TOPMOST
  806. glfwWindowHint(GLFW_FLOATING, (viewport->Flags & ImGuiViewportFlags_TopMost) ? true : false);
  807. #endif
  808. GLFWwindow* share_window = (bd->ClientApi == GlfwClientApi_OpenGL) ? bd->Window : nullptr;
  809. vd->Window = glfwCreateWindow((int)viewport->Size.x, (int)viewport->Size.y, "No Title Yet", nullptr, share_window);
  810. vd->WindowOwned = true;
  811. viewport->PlatformHandle = (void*)vd->Window;
  812. #ifdef _WIN32
  813. viewport->PlatformHandleRaw = glfwGetWin32Window(vd->Window);
  814. #elif defined(__APPLE__)
  815. viewport->PlatformHandleRaw = (void*)glfwGetCocoaWindow(vd->Window);
  816. #endif
  817. glfwSetWindowPos(vd->Window, (int)viewport->Pos.x, (int)viewport->Pos.y);
  818. // Install GLFW callbacks for secondary viewports
  819. glfwSetWindowFocusCallback(vd->Window, ImGui_ImplGlfw_WindowFocusCallback);
  820. glfwSetCursorEnterCallback(vd->Window, ImGui_ImplGlfw_CursorEnterCallback);
  821. glfwSetCursorPosCallback(vd->Window, ImGui_ImplGlfw_CursorPosCallback);
  822. glfwSetMouseButtonCallback(vd->Window, ImGui_ImplGlfw_MouseButtonCallback);
  823. glfwSetScrollCallback(vd->Window, ImGui_ImplGlfw_ScrollCallback);
  824. glfwSetKeyCallback(vd->Window, ImGui_ImplGlfw_KeyCallback);
  825. glfwSetCharCallback(vd->Window, ImGui_ImplGlfw_CharCallback);
  826. glfwSetWindowCloseCallback(vd->Window, ImGui_ImplGlfw_WindowCloseCallback);
  827. glfwSetWindowPosCallback(vd->Window, ImGui_ImplGlfw_WindowPosCallback);
  828. glfwSetWindowSizeCallback(vd->Window, ImGui_ImplGlfw_WindowSizeCallback);
  829. if (bd->ClientApi == GlfwClientApi_OpenGL)
  830. {
  831. glfwMakeContextCurrent(vd->Window);
  832. glfwSwapInterval(0);
  833. }
  834. }
  835. static void ImGui_ImplGlfw_DestroyWindow(ImGuiViewport* viewport)
  836. {
  837. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  838. if (ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData)
  839. {
  840. if (vd->WindowOwned)
  841. {
  842. #if !GLFW_HAS_MOUSE_PASSTHROUGH && GLFW_HAS_WINDOW_HOVERED && defined(_WIN32)
  843. HWND hwnd = (HWND)viewport->PlatformHandleRaw;
  844. ::RemovePropA(hwnd, "IMGUI_VIEWPORT");
  845. #endif
  846. // Release any keys that were pressed in the window being destroyed and are still held down,
  847. // because we will not receive any release events after window is destroyed.
  848. for (int i = 0; i < IM_ARRAYSIZE(bd->KeyOwnerWindows); i++)
  849. if (bd->KeyOwnerWindows[i] == vd->Window)
  850. ImGui_ImplGlfw_KeyCallback(vd->Window, i, 0, GLFW_RELEASE, 0); // Later params are only used for main viewport, on which this function is never called.
  851. glfwDestroyWindow(vd->Window);
  852. }
  853. vd->Window = nullptr;
  854. IM_DELETE(vd);
  855. }
  856. viewport->PlatformUserData = viewport->PlatformHandle = nullptr;
  857. }
  858. // We have submitted https://github.com/glfw/glfw/pull/1568 to allow GLFW to support "transparent inputs".
  859. // In the meanwhile we implement custom per-platform workarounds here (FIXME-VIEWPORT: Implement same work-around for Linux/OSX!)
  860. #if !GLFW_HAS_MOUSE_PASSTHROUGH && GLFW_HAS_WINDOW_HOVERED && defined(_WIN32)
  861. static LRESULT CALLBACK WndProcNoInputs(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  862. {
  863. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  864. if (msg == WM_NCHITTEST)
  865. {
  866. // Let mouse pass-through the window. This will allow the backend to call io.AddMouseViewportEvent() properly (which is OPTIONAL).
  867. // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging.
  868. // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in
  869. // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system.
  870. ImGuiViewport* viewport = (ImGuiViewport*)::GetPropA(hWnd, "IMGUI_VIEWPORT");
  871. if (viewport->Flags & ImGuiViewportFlags_NoInputs)
  872. return HTTRANSPARENT;
  873. }
  874. return ::CallWindowProc(bd->GlfwWndProc, hWnd, msg, wParam, lParam);
  875. }
  876. #endif
  877. static void ImGui_ImplGlfw_ShowWindow(ImGuiViewport* viewport)
  878. {
  879. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  880. #if defined(_WIN32)
  881. // GLFW hack: Hide icon from task bar
  882. HWND hwnd = (HWND)viewport->PlatformHandleRaw;
  883. if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)
  884. {
  885. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  886. ex_style &= ~WS_EX_APPWINDOW;
  887. ex_style |= WS_EX_TOOLWINDOW;
  888. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  889. }
  890. // GLFW hack: install hook for WM_NCHITTEST message handler
  891. #if !GLFW_HAS_MOUSE_PASSTHROUGH && GLFW_HAS_WINDOW_HOVERED && defined(_WIN32)
  892. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  893. ::SetPropA(hwnd, "IMGUI_VIEWPORT", viewport);
  894. if (bd->GlfwWndProc == nullptr)
  895. bd->GlfwWndProc = (WNDPROC)::GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  896. ::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)WndProcNoInputs);
  897. #endif
  898. #if !GLFW_HAS_FOCUS_ON_SHOW
  899. // GLFW hack: GLFW 3.2 has a bug where glfwShowWindow() also activates/focus the window.
  900. // The fix was pushed to GLFW repository on 2018/01/09 and should be included in GLFW 3.3 via a GLFW_FOCUS_ON_SHOW window attribute.
  901. // See https://github.com/glfw/glfw/issues/1189
  902. // FIXME-VIEWPORT: Implement same work-around for Linux/OSX in the meanwhile.
  903. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  904. {
  905. ::ShowWindow(hwnd, SW_SHOWNA);
  906. return;
  907. }
  908. #endif
  909. #endif
  910. glfwShowWindow(vd->Window);
  911. }
  912. static ImVec2 ImGui_ImplGlfw_GetWindowPos(ImGuiViewport* viewport)
  913. {
  914. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  915. int x = 0, y = 0;
  916. glfwGetWindowPos(vd->Window, &x, &y);
  917. return ImVec2((float)x, (float)y);
  918. }
  919. static void ImGui_ImplGlfw_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  920. {
  921. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  922. vd->IgnoreWindowPosEventFrame = ImGui::GetFrameCount();
  923. glfwSetWindowPos(vd->Window, (int)pos.x, (int)pos.y);
  924. }
  925. static ImVec2 ImGui_ImplGlfw_GetWindowSize(ImGuiViewport* viewport)
  926. {
  927. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  928. int w = 0, h = 0;
  929. glfwGetWindowSize(vd->Window, &w, &h);
  930. return ImVec2((float)w, (float)h);
  931. }
  932. static void ImGui_ImplGlfw_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  933. {
  934. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  935. #if __APPLE__ && !GLFW_HAS_OSX_WINDOW_POS_FIX
  936. // Native OS windows are positioned from the bottom-left corner on macOS, whereas on other platforms they are
  937. // positioned from the upper-left corner. GLFW makes an effort to convert macOS style coordinates, however it
  938. // doesn't handle it when changing size. We are manually moving the window in order for changes of size to be based
  939. // on the upper-left corner.
  940. int x, y, width, height;
  941. glfwGetWindowPos(vd->Window, &x, &y);
  942. glfwGetWindowSize(vd->Window, &width, &height);
  943. glfwSetWindowPos(vd->Window, x, y - height + size.y);
  944. #endif
  945. vd->IgnoreWindowSizeEventFrame = ImGui::GetFrameCount();
  946. glfwSetWindowSize(vd->Window, (int)size.x, (int)size.y);
  947. }
  948. static void ImGui_ImplGlfw_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  949. {
  950. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  951. glfwSetWindowTitle(vd->Window, title);
  952. }
  953. static void ImGui_ImplGlfw_SetWindowFocus(ImGuiViewport* viewport)
  954. {
  955. #if GLFW_HAS_FOCUS_WINDOW
  956. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  957. glfwFocusWindow(vd->Window);
  958. #else
  959. // FIXME: What are the effect of not having this function? At the moment imgui doesn't actually call SetWindowFocus - we set that up ahead, will answer that question later.
  960. (void)viewport;
  961. #endif
  962. }
  963. static bool ImGui_ImplGlfw_GetWindowFocus(ImGuiViewport* viewport)
  964. {
  965. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  966. return glfwGetWindowAttrib(vd->Window, GLFW_FOCUSED) != 0;
  967. }
  968. static bool ImGui_ImplGlfw_GetWindowMinimized(ImGuiViewport* viewport)
  969. {
  970. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  971. return glfwGetWindowAttrib(vd->Window, GLFW_ICONIFIED) != 0;
  972. }
  973. #if GLFW_HAS_WINDOW_ALPHA
  974. static void ImGui_ImplGlfw_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
  975. {
  976. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  977. glfwSetWindowOpacity(vd->Window, alpha);
  978. }
  979. #endif
  980. static void ImGui_ImplGlfw_RenderWindow(ImGuiViewport* viewport, void*)
  981. {
  982. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  983. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  984. if (bd->ClientApi == GlfwClientApi_OpenGL)
  985. glfwMakeContextCurrent(vd->Window);
  986. }
  987. static void ImGui_ImplGlfw_SwapBuffers(ImGuiViewport* viewport, void*)
  988. {
  989. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  990. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  991. if (bd->ClientApi == GlfwClientApi_OpenGL)
  992. {
  993. glfwMakeContextCurrent(vd->Window);
  994. glfwSwapBuffers(vd->Window);
  995. }
  996. }
  997. //--------------------------------------------------------------------------------------------------------
  998. // Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface)
  999. //--------------------------------------------------------------------------------------------------------
  1000. // Avoid including <vulkan.h> so we can build without it
  1001. #if GLFW_HAS_VULKAN
  1002. #ifndef VULKAN_H_
  1003. #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
  1004. #if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
  1005. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object;
  1006. #else
  1007. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object;
  1008. #endif
  1009. VK_DEFINE_HANDLE(VkInstance)
  1010. VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSurfaceKHR)
  1011. struct VkAllocationCallbacks;
  1012. enum VkResult { VK_RESULT_MAX_ENUM = 0x7FFFFFFF };
  1013. #endif // VULKAN_H_
  1014. extern "C" { extern GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface); }
  1015. static int ImGui_ImplGlfw_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface)
  1016. {
  1017. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  1018. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  1019. IM_UNUSED(bd);
  1020. IM_ASSERT(bd->ClientApi == GlfwClientApi_Vulkan);
  1021. VkResult err = glfwCreateWindowSurface((VkInstance)vk_instance, vd->Window, (const VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface);
  1022. return (int)err;
  1023. }
  1024. #endif // GLFW_HAS_VULKAN
  1025. static void ImGui_ImplGlfw_InitPlatformInterface()
  1026. {
  1027. // Register platform interface (will be coupled with a renderer interface)
  1028. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  1029. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  1030. platform_io.Platform_CreateWindow = ImGui_ImplGlfw_CreateWindow;
  1031. platform_io.Platform_DestroyWindow = ImGui_ImplGlfw_DestroyWindow;
  1032. platform_io.Platform_ShowWindow = ImGui_ImplGlfw_ShowWindow;
  1033. platform_io.Platform_SetWindowPos = ImGui_ImplGlfw_SetWindowPos;
  1034. platform_io.Platform_GetWindowPos = ImGui_ImplGlfw_GetWindowPos;
  1035. platform_io.Platform_SetWindowSize = ImGui_ImplGlfw_SetWindowSize;
  1036. platform_io.Platform_GetWindowSize = ImGui_ImplGlfw_GetWindowSize;
  1037. platform_io.Platform_SetWindowFocus = ImGui_ImplGlfw_SetWindowFocus;
  1038. platform_io.Platform_GetWindowFocus = ImGui_ImplGlfw_GetWindowFocus;
  1039. platform_io.Platform_GetWindowMinimized = ImGui_ImplGlfw_GetWindowMinimized;
  1040. platform_io.Platform_SetWindowTitle = ImGui_ImplGlfw_SetWindowTitle;
  1041. platform_io.Platform_RenderWindow = ImGui_ImplGlfw_RenderWindow;
  1042. platform_io.Platform_SwapBuffers = ImGui_ImplGlfw_SwapBuffers;
  1043. #if GLFW_HAS_WINDOW_ALPHA
  1044. platform_io.Platform_SetWindowAlpha = ImGui_ImplGlfw_SetWindowAlpha;
  1045. #endif
  1046. #if GLFW_HAS_VULKAN
  1047. platform_io.Platform_CreateVkSurface = ImGui_ImplGlfw_CreateVkSurface;
  1048. #endif
  1049. // Register main window handle (which is owned by the main application, not by us)
  1050. // This is mostly for simplicity and consistency, so that our code (e.g. mouse handling etc.) can use same logic for main and secondary viewports.
  1051. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  1052. ImGui_ImplGlfw_ViewportData* vd = IM_NEW(ImGui_ImplGlfw_ViewportData)();
  1053. vd->Window = bd->Window;
  1054. vd->WindowOwned = false;
  1055. main_viewport->PlatformUserData = vd;
  1056. main_viewport->PlatformHandle = (void*)bd->Window;
  1057. }
  1058. static void ImGui_ImplGlfw_ShutdownPlatformInterface()
  1059. {
  1060. ImGui::DestroyPlatformWindows();
  1061. }
  1062. #if defined(__clang__)
  1063. #pragma clang diagnostic pop
  1064. #endif