2
0

imgui_impl_glfw.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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-04-30: Inputs: Fixed ImGui_ImplGlfw_TranslateUntranslatedKey() for lower case letters on OSX.
  21. // 2022-03-23: Inputs: Fixed a regression in 1.87 which resulted in keyboard modifiers events being reported incorrectly on Linux/X11.
  22. // 2022-02-07: Added ImGui_ImplGlfw_InstallCallbacks()/ImGui_ImplGlfw_RestoreCallbacks() helpers to facilitate user installing callbacks after initializing backend.
  23. // 2022-01-26: Inputs: replaced short-lived io.AddKeyModsEvent() (added two weeks ago)with io.AddKeyEvent() using ImGuiKey_ModXXX flags. Sorry for the confusion.
  24. // 2021-01-20: Inputs: calling new io.AddKeyAnalogEvent() for gamepad support, instead of writing directly to io.NavInputs[].
  25. // 2022-01-17: Inputs: calling new io.AddMousePosEvent(), io.AddMouseButtonEvent(), io.AddMouseWheelEvent() API (1.87+).
  26. // 2022-01-17: Inputs: always update key mods next and before key event (not in NewFrame) to fix input queue with very low framerates.
  27. // 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().
  28. // 2022-01-10: Inputs: calling new io.AddKeyEvent(), io.AddKeyModsEvent() + io.SetKeyEventNativeData() API (1.87+). Support for full ImGuiKey range.
  29. // 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.
  30. // 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().
  31. // 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().
  32. // 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).
  33. // 2020-01-17: Inputs: Disable error callback while assigning mouse cursors because some X11 setup don't have them and it generates errors.
  34. // 2019-12-05: Inputs: Added support for new mouse cursors added in GLFW 3.4+ (resizing cursors, not allowed cursor).
  35. // 2019-10-18: Misc: Previously installed user callbacks are now restored on shutdown.
  36. // 2019-07-21: Inputs: Added mapping for ImGuiKey_KeyPadEnter.
  37. // 2019-05-11: Inputs: Don't filter value from character callback before calling AddInputCharacter().
  38. // 2019-03-12: Misc: Preserve DisplayFramebufferScale when main window is minimized.
  39. // 2018-11-30: Misc: Setting up io.BackendPlatformName so it can be displayed in the About Window.
  40. // 2018-11-07: Inputs: When installing our GLFW callbacks, we save user's previously installed ones - if any - and chain call them.
  41. // 2018-08-01: Inputs: Workaround for Emscripten which doesn't seem to handle focus related calls.
  42. // 2018-06-29: Inputs: Added support for the ImGuiMouseCursor_Hand cursor.
  43. // 2018-06-08: Misc: Extracted imgui_impl_glfw.cpp/.h away from the old combined GLFW+OpenGL/Vulkan examples.
  44. // 2018-03-20: Misc: Setup io.BackendFlags ImGuiBackendFlags_HasMouseCursors flag + honor ImGuiConfigFlags_NoMouseCursorChange flag.
  45. // 2018-02-20: Inputs: Added support for mouse cursors (ImGui::GetMouseCursor() value, passed to glfwSetCursor()).
  46. // 2018-02-06: Misc: Removed call to ImGui::Shutdown() which is not available from 1.60 WIP, user needs to call CreateContext/DestroyContext themselves.
  47. // 2018-02-06: Inputs: Added mapping for ImGuiKey_Space.
  48. // 2018-01-25: Inputs: Added gamepad support if ImGuiConfigFlags_NavEnableGamepad is set.
  49. // 2018-01-25: Inputs: Honoring the io.WantSetMousePos by repositioning the mouse (when using navigation and ImGuiConfigFlags_NavMoveMouse is set).
  50. // 2018-01-20: Inputs: Added Horizontal Mouse Wheel support.
  51. // 2018-01-18: Inputs: Added mapping for ImGuiKey_Insert.
  52. // 2017-08-25: Inputs: MousePos set to -FLT_MAX,-FLT_MAX when mouse is unavailable/missing (instead of -1,-1).
  53. // 2016-10-15: Misc: Added a void* user_data parameter to Clipboard function handlers.
  54. #include "imgui.h"
  55. #include "imgui_impl_glfw.h"
  56. // Clang warnings with -Weverything
  57. #if defined(__clang__)
  58. #pragma clang diagnostic push
  59. #pragma clang diagnostic ignored "-Wold-style-cast" // warning: use of old-style cast
  60. #pragma clang diagnostic ignored "-Wsign-conversion" // warning: implicit conversion changes signedness
  61. #if __has_warning("-Wzero-as-null-pointer-constant")
  62. #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
  63. #endif
  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 : NULL;
  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(ImGuiKey_ModCtrl, (mods & GLFW_MOD_CONTROL) != 0);
  281. io.AddKeyEvent(ImGuiKey_ModShift, (mods & GLFW_MOD_SHIFT) != 0);
  282. io.AddKeyEvent(ImGuiKey_ModAlt, (mods & GLFW_MOD_ALT) != 0);
  283. io.AddKeyEvent(ImGuiKey_ModSuper, (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 != NULL && 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 != NULL && 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 != NULL && 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 : NULL;
  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 != NULL && 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 != NULL && window == bd->Window)
  361. bd->PrevUserCallbackCursorPos(window, x, y);
  362. ImGuiIO& io = ImGui::GetIO();
  363. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  364. {
  365. int window_x, window_y;
  366. glfwGetWindowPos(window, &window_x, &window_y);
  367. x += window_x;
  368. y += window_y;
  369. }
  370. io.AddMousePosEvent((float)x, (float)y);
  371. bd->LastValidMousePos = ImVec2((float)x, (float)y);
  372. }
  373. // Workaround: X11 seems to send spurious Leave/Enter events which would make us lose our position,
  374. // so we back it up and restore on Leave/Enter (see https://github.com/ocornut/imgui/issues/4984)
  375. void ImGui_ImplGlfw_CursorEnterCallback(GLFWwindow* window, int entered)
  376. {
  377. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  378. if (bd->PrevUserCallbackCursorEnter != NULL && window == bd->Window)
  379. bd->PrevUserCallbackCursorEnter(window, entered);
  380. ImGuiIO& io = ImGui::GetIO();
  381. if (entered)
  382. {
  383. bd->MouseWindow = window;
  384. io.AddMousePosEvent(bd->LastValidMousePos.x, bd->LastValidMousePos.y);
  385. }
  386. else if (!entered && bd->MouseWindow == window)
  387. {
  388. bd->LastValidMousePos = io.MousePos;
  389. bd->MouseWindow = NULL;
  390. io.AddMousePosEvent(-FLT_MAX, -FLT_MAX);
  391. }
  392. }
  393. void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c)
  394. {
  395. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  396. if (bd->PrevUserCallbackChar != NULL && window == bd->Window)
  397. bd->PrevUserCallbackChar(window, c);
  398. ImGuiIO& io = ImGui::GetIO();
  399. io.AddInputCharacter(c);
  400. }
  401. void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor*, int)
  402. {
  403. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  404. bd->WantUpdateMonitors = true;
  405. }
  406. void ImGui_ImplGlfw_InstallCallbacks(GLFWwindow* window)
  407. {
  408. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  409. IM_ASSERT(bd->InstalledCallbacks == false && "Callbacks already installed!");
  410. IM_ASSERT(bd->Window == window);
  411. bd->PrevUserCallbackWindowFocus = glfwSetWindowFocusCallback(window, ImGui_ImplGlfw_WindowFocusCallback);
  412. bd->PrevUserCallbackCursorEnter = glfwSetCursorEnterCallback(window, ImGui_ImplGlfw_CursorEnterCallback);
  413. bd->PrevUserCallbackCursorPos = glfwSetCursorPosCallback(window, ImGui_ImplGlfw_CursorPosCallback);
  414. bd->PrevUserCallbackMousebutton = glfwSetMouseButtonCallback(window, ImGui_ImplGlfw_MouseButtonCallback);
  415. bd->PrevUserCallbackScroll = glfwSetScrollCallback(window, ImGui_ImplGlfw_ScrollCallback);
  416. bd->PrevUserCallbackKey = glfwSetKeyCallback(window, ImGui_ImplGlfw_KeyCallback);
  417. bd->PrevUserCallbackChar = glfwSetCharCallback(window, ImGui_ImplGlfw_CharCallback);
  418. bd->PrevUserCallbackMonitor = glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback);
  419. bd->InstalledCallbacks = true;
  420. }
  421. void ImGui_ImplGlfw_RestoreCallbacks(GLFWwindow* window)
  422. {
  423. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  424. IM_ASSERT(bd->InstalledCallbacks == true && "Callbacks not installed!");
  425. IM_ASSERT(bd->Window == window);
  426. glfwSetWindowFocusCallback(window, bd->PrevUserCallbackWindowFocus);
  427. glfwSetCursorEnterCallback(window, bd->PrevUserCallbackCursorEnter);
  428. glfwSetCursorPosCallback(window, bd->PrevUserCallbackCursorPos);
  429. glfwSetMouseButtonCallback(window, bd->PrevUserCallbackMousebutton);
  430. glfwSetScrollCallback(window, bd->PrevUserCallbackScroll);
  431. glfwSetKeyCallback(window, bd->PrevUserCallbackKey);
  432. glfwSetCharCallback(window, bd->PrevUserCallbackChar);
  433. glfwSetMonitorCallback(bd->PrevUserCallbackMonitor);
  434. bd->InstalledCallbacks = false;
  435. bd->PrevUserCallbackWindowFocus = NULL;
  436. bd->PrevUserCallbackCursorEnter = NULL;
  437. bd->PrevUserCallbackCursorPos = NULL;
  438. bd->PrevUserCallbackMousebutton = NULL;
  439. bd->PrevUserCallbackScroll = NULL;
  440. bd->PrevUserCallbackKey = NULL;
  441. bd->PrevUserCallbackChar = NULL;
  442. bd->PrevUserCallbackMonitor = NULL;
  443. }
  444. static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, GlfwClientApi client_api)
  445. {
  446. ImGuiIO& io = ImGui::GetIO();
  447. IM_ASSERT(io.BackendPlatformUserData == NULL && "Already initialized a platform backend!");
  448. // Setup backend capabilities flags
  449. ImGui_ImplGlfw_Data* bd = IM_NEW(ImGui_ImplGlfw_Data)();
  450. io.BackendPlatformUserData = (void*)bd;
  451. io.BackendPlatformName = "imgui_impl_glfw";
  452. io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors; // We can honor GetMouseCursor() values (optional)
  453. io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)
  454. io.BackendFlags |= ImGuiBackendFlags_PlatformHasViewports; // We can create multi-viewports on the Platform side (optional)
  455. #if GLFW_HAS_MOUSE_PASSTHROUGH || (GLFW_HAS_WINDOW_HOVERED && defined(_WIN32))
  456. io.BackendFlags |= ImGuiBackendFlags_HasMouseHoveredViewport; // We can call io.AddMouseViewportEvent() with correct data (optional)
  457. #endif
  458. bd->Window = window;
  459. bd->Time = 0.0;
  460. bd->WantUpdateMonitors = true;
  461. io.SetClipboardTextFn = ImGui_ImplGlfw_SetClipboardText;
  462. io.GetClipboardTextFn = ImGui_ImplGlfw_GetClipboardText;
  463. io.ClipboardUserData = bd->Window;
  464. // Create mouse cursors
  465. // (By design, on X11 cursors are user configurable and some cursors may be missing. When a cursor doesn't exist,
  466. // GLFW will emit an error which will often be printed by the app, so we temporarily disable error reporting.
  467. // Missing cursors will return NULL and our _UpdateMouseCursor() function will use the Arrow cursor instead.)
  468. GLFWerrorfun prev_error_callback = glfwSetErrorCallback(NULL);
  469. bd->MouseCursors[ImGuiMouseCursor_Arrow] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  470. bd->MouseCursors[ImGuiMouseCursor_TextInput] = glfwCreateStandardCursor(GLFW_IBEAM_CURSOR);
  471. bd->MouseCursors[ImGuiMouseCursor_ResizeNS] = glfwCreateStandardCursor(GLFW_VRESIZE_CURSOR);
  472. bd->MouseCursors[ImGuiMouseCursor_ResizeEW] = glfwCreateStandardCursor(GLFW_HRESIZE_CURSOR);
  473. bd->MouseCursors[ImGuiMouseCursor_Hand] = glfwCreateStandardCursor(GLFW_HAND_CURSOR);
  474. #if GLFW_HAS_NEW_CURSORS
  475. bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_RESIZE_ALL_CURSOR);
  476. bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_RESIZE_NESW_CURSOR);
  477. bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_RESIZE_NWSE_CURSOR);
  478. bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_NOT_ALLOWED_CURSOR);
  479. #else
  480. bd->MouseCursors[ImGuiMouseCursor_ResizeAll] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  481. bd->MouseCursors[ImGuiMouseCursor_ResizeNESW] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  482. bd->MouseCursors[ImGuiMouseCursor_ResizeNWSE] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  483. bd->MouseCursors[ImGuiMouseCursor_NotAllowed] = glfwCreateStandardCursor(GLFW_ARROW_CURSOR);
  484. #endif
  485. glfwSetErrorCallback(prev_error_callback);
  486. // Chain GLFW callbacks: our callbacks will call the user's previously installed callbacks, if any.
  487. if (install_callbacks)
  488. ImGui_ImplGlfw_InstallCallbacks(window);
  489. // Update monitors the first time (note: monitor callback are broken in GLFW 3.2 and earlier, see github.com/glfw/glfw/issues/784)
  490. ImGui_ImplGlfw_UpdateMonitors();
  491. glfwSetMonitorCallback(ImGui_ImplGlfw_MonitorCallback);
  492. // Our mouse update function expect PlatformHandle to be filled for the main viewport
  493. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  494. main_viewport->PlatformHandle = (void*)bd->Window;
  495. #ifdef _WIN32
  496. main_viewport->PlatformHandleRaw = glfwGetWin32Window(bd->Window);
  497. #elif defined(__APPLE__)
  498. main_viewport->PlatformHandleRaw = (void*)glfwGetCocoaWindow(bd->Window);
  499. #endif
  500. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  501. ImGui_ImplGlfw_InitPlatformInterface();
  502. bd->ClientApi = client_api;
  503. return true;
  504. }
  505. bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow* window, bool install_callbacks)
  506. {
  507. return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_OpenGL);
  508. }
  509. bool ImGui_ImplGlfw_InitForVulkan(GLFWwindow* window, bool install_callbacks)
  510. {
  511. return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Vulkan);
  512. }
  513. bool ImGui_ImplGlfw_InitForOther(GLFWwindow* window, bool install_callbacks)
  514. {
  515. return ImGui_ImplGlfw_Init(window, install_callbacks, GlfwClientApi_Unknown);
  516. }
  517. void ImGui_ImplGlfw_Shutdown()
  518. {
  519. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  520. IM_ASSERT(bd != NULL && "No platform backend to shutdown, or already shutdown?");
  521. ImGuiIO& io = ImGui::GetIO();
  522. ImGui_ImplGlfw_ShutdownPlatformInterface();
  523. if (bd->InstalledCallbacks)
  524. ImGui_ImplGlfw_RestoreCallbacks(bd->Window);
  525. for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
  526. glfwDestroyCursor(bd->MouseCursors[cursor_n]);
  527. io.BackendPlatformName = NULL;
  528. io.BackendPlatformUserData = NULL;
  529. IM_DELETE(bd);
  530. }
  531. static void ImGui_ImplGlfw_UpdateMouseData()
  532. {
  533. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  534. ImGuiIO& io = ImGui::GetIO();
  535. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  536. ImGuiID mouse_viewport_id = 0;
  537. const ImVec2 mouse_pos_prev = io.MousePos;
  538. for (int n = 0; n < platform_io.Viewports.Size; n++)
  539. {
  540. ImGuiViewport* viewport = platform_io.Viewports[n];
  541. GLFWwindow* window = (GLFWwindow*)viewport->PlatformHandle;
  542. #ifdef __EMSCRIPTEN__
  543. const bool is_window_focused = true;
  544. #else
  545. const bool is_window_focused = glfwGetWindowAttrib(window, GLFW_FOCUSED) != 0;
  546. #endif
  547. if (is_window_focused)
  548. {
  549. // (Optional) Set OS mouse position from Dear ImGui if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user)
  550. // When multi-viewports are enabled, all Dear ImGui positions are same as OS positions.
  551. if (io.WantSetMousePos)
  552. glfwSetCursorPos(window, (double)(mouse_pos_prev.x - viewport->Pos.x), (double)(mouse_pos_prev.y - viewport->Pos.y));
  553. // (Optional) Fallback to provide mouse position when focused (ImGui_ImplGlfw_CursorPosCallback already provides this when hovered or captured)
  554. if (bd->MouseWindow == NULL)
  555. {
  556. double mouse_x, mouse_y;
  557. glfwGetCursorPos(window, &mouse_x, &mouse_y);
  558. if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
  559. {
  560. // 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)
  561. // 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)
  562. int window_x, window_y;
  563. glfwGetWindowPos(window, &window_x, &window_y);
  564. mouse_x += window_x;
  565. mouse_y += window_y;
  566. }
  567. bd->LastValidMousePos = ImVec2((float)mouse_x, (float)mouse_y);
  568. io.AddMousePosEvent((float)mouse_x, (float)mouse_y);
  569. }
  570. }
  571. // (Optional) When using multiple viewports: call io.AddMouseViewportEvent() with the viewport the OS mouse cursor is hovering.
  572. // If ImGuiBackendFlags_HasMouseHoveredViewport is not set by the backend, Dear imGui will ignore this field and infer the information using its flawed heuristic.
  573. // - [X] GLFW >= 3.3 backend ON WINDOWS ONLY does correctly ignore viewports with the _NoInputs flag.
  574. // - [!] GLFW <= 3.2 backend CANNOT correctly ignore viewports with the _NoInputs flag, and CANNOT reported Hovered Viewport because of mouse capture.
  575. // 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
  576. // 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
  577. // by the backend, and use its flawed heuristic to guess the viewport behind.
  578. // - [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).
  579. // FIXME: This is currently only correct on Win32. See what we do below with the WM_NCHITTEST, missing an equivalent for other systems.
  580. // See https://github.com/glfw/glfw/issues/1236 if you want to help in making this a GLFW feature.
  581. #if GLFW_HAS_MOUSE_PASSTHROUGH || (GLFW_HAS_WINDOW_HOVERED && defined(_WIN32))
  582. const bool window_no_input = (viewport->Flags & ImGuiViewportFlags_NoInputs) != 0;
  583. #if GLFW_HAS_MOUSE_PASSTHROUGH
  584. glfwSetWindowAttrib(window, GLFW_MOUSE_PASSTHROUGH, window_no_input);
  585. #endif
  586. if (glfwGetWindowAttrib(window, GLFW_HOVERED) && !window_no_input)
  587. mouse_viewport_id = viewport->ID;
  588. #else
  589. // We cannot use bd->MouseWindow maintained from CursorEnter/Leave callbacks, because it is locked to the window capturing mouse.
  590. #endif
  591. }
  592. if (io.BackendFlags & ImGuiBackendFlags_HasMouseHoveredViewport)
  593. io.AddMouseViewportEvent(mouse_viewport_id);
  594. }
  595. static void ImGui_ImplGlfw_UpdateMouseCursor()
  596. {
  597. ImGuiIO& io = ImGui::GetIO();
  598. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  599. if ((io.ConfigFlags & ImGuiConfigFlags_NoMouseCursorChange) || glfwGetInputMode(bd->Window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
  600. return;
  601. ImGuiMouseCursor imgui_cursor = ImGui::GetMouseCursor();
  602. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  603. for (int n = 0; n < platform_io.Viewports.Size; n++)
  604. {
  605. GLFWwindow* window = (GLFWwindow*)platform_io.Viewports[n]->PlatformHandle;
  606. if (imgui_cursor == ImGuiMouseCursor_None || io.MouseDrawCursor)
  607. {
  608. // Hide OS mouse cursor if imgui is drawing it or if it wants no cursor
  609. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
  610. }
  611. else
  612. {
  613. // Show OS mouse cursor
  614. // FIXME-PLATFORM: Unfocused windows seems to fail changing the mouse cursor with GLFW 3.2, but 3.3 works here.
  615. glfwSetCursor(window, bd->MouseCursors[imgui_cursor] ? bd->MouseCursors[imgui_cursor] : bd->MouseCursors[ImGuiMouseCursor_Arrow]);
  616. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
  617. }
  618. }
  619. }
  620. // Update gamepad inputs
  621. static inline float Saturate(float v) { return v < 0.0f ? 0.0f : v > 1.0f ? 1.0f : v; }
  622. static void ImGui_ImplGlfw_UpdateGamepads()
  623. {
  624. ImGuiIO& io = ImGui::GetIO();
  625. if ((io.ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0) // FIXME: Technically feeding gamepad shouldn't depend on this now that they are regular inputs.
  626. return;
  627. io.BackendFlags &= ~ImGuiBackendFlags_HasGamepad;
  628. #if GLFW_HAS_GAMEPAD_API
  629. GLFWgamepadstate gamepad;
  630. if (!glfwGetGamepadState(GLFW_JOYSTICK_1, &gamepad))
  631. return;
  632. #define MAP_BUTTON(KEY_NO, BUTTON_NO, _UNUSED) do { io.AddKeyEvent(KEY_NO, gamepad.buttons[BUTTON_NO] != 0); } while (0)
  633. #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)
  634. #else
  635. int axes_count = 0, buttons_count = 0;
  636. const float* axes = glfwGetJoystickAxes(GLFW_JOYSTICK_1, &axes_count);
  637. const unsigned char* buttons = glfwGetJoystickButtons(GLFW_JOYSTICK_1, &buttons_count);
  638. if (axes_count == 0 || buttons_count == 0)
  639. return;
  640. #define MAP_BUTTON(KEY_NO, _UNUSED, BUTTON_NO) do { io.AddKeyEvent(KEY_NO, (buttons_count > BUTTON_NO && buttons[BUTTON_NO] == GLFW_PRESS)); } while (0)
  641. #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)
  642. #endif
  643. io.BackendFlags |= ImGuiBackendFlags_HasGamepad;
  644. MAP_BUTTON(ImGuiKey_GamepadStart, GLFW_GAMEPAD_BUTTON_START, 7);
  645. MAP_BUTTON(ImGuiKey_GamepadBack, GLFW_GAMEPAD_BUTTON_BACK, 6);
  646. MAP_BUTTON(ImGuiKey_GamepadFaceLeft, GLFW_GAMEPAD_BUTTON_X, 2); // Xbox X, PS Square
  647. MAP_BUTTON(ImGuiKey_GamepadFaceRight, GLFW_GAMEPAD_BUTTON_B, 1); // Xbox B, PS Circle
  648. MAP_BUTTON(ImGuiKey_GamepadFaceUp, GLFW_GAMEPAD_BUTTON_Y, 3); // Xbox Y, PS Triangle
  649. MAP_BUTTON(ImGuiKey_GamepadFaceDown, GLFW_GAMEPAD_BUTTON_A, 0); // Xbox A, PS Cross
  650. MAP_BUTTON(ImGuiKey_GamepadDpadLeft, GLFW_GAMEPAD_BUTTON_DPAD_LEFT, 13);
  651. MAP_BUTTON(ImGuiKey_GamepadDpadRight, GLFW_GAMEPAD_BUTTON_DPAD_RIGHT, 11);
  652. MAP_BUTTON(ImGuiKey_GamepadDpadUp, GLFW_GAMEPAD_BUTTON_DPAD_UP, 10);
  653. MAP_BUTTON(ImGuiKey_GamepadDpadDown, GLFW_GAMEPAD_BUTTON_DPAD_DOWN, 12);
  654. MAP_BUTTON(ImGuiKey_GamepadL1, GLFW_GAMEPAD_BUTTON_LEFT_BUMPER, 4);
  655. MAP_BUTTON(ImGuiKey_GamepadR1, GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER, 5);
  656. MAP_ANALOG(ImGuiKey_GamepadL2, GLFW_GAMEPAD_AXIS_LEFT_TRIGGER, 4, -0.75f, +1.0f);
  657. MAP_ANALOG(ImGuiKey_GamepadR2, GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER, 5, -0.75f, +1.0f);
  658. MAP_BUTTON(ImGuiKey_GamepadL3, GLFW_GAMEPAD_BUTTON_LEFT_THUMB, 8);
  659. MAP_BUTTON(ImGuiKey_GamepadR3, GLFW_GAMEPAD_BUTTON_RIGHT_THUMB, 9);
  660. MAP_ANALOG(ImGuiKey_GamepadLStickLeft, GLFW_GAMEPAD_AXIS_LEFT_X, 0, -0.25f, -1.0f);
  661. MAP_ANALOG(ImGuiKey_GamepadLStickRight, GLFW_GAMEPAD_AXIS_LEFT_X, 0, +0.25f, +1.0f);
  662. MAP_ANALOG(ImGuiKey_GamepadLStickUp, GLFW_GAMEPAD_AXIS_LEFT_Y, 1, -0.25f, -1.0f);
  663. MAP_ANALOG(ImGuiKey_GamepadLStickDown, GLFW_GAMEPAD_AXIS_LEFT_Y, 1, +0.25f, +1.0f);
  664. MAP_ANALOG(ImGuiKey_GamepadRStickLeft, GLFW_GAMEPAD_AXIS_RIGHT_X, 2, -0.25f, -1.0f);
  665. MAP_ANALOG(ImGuiKey_GamepadRStickRight, GLFW_GAMEPAD_AXIS_RIGHT_X, 2, +0.25f, +1.0f);
  666. MAP_ANALOG(ImGuiKey_GamepadRStickUp, GLFW_GAMEPAD_AXIS_RIGHT_Y, 3, -0.25f, -1.0f);
  667. MAP_ANALOG(ImGuiKey_GamepadRStickDown, GLFW_GAMEPAD_AXIS_RIGHT_Y, 3, +0.25f, +1.0f);
  668. #undef MAP_BUTTON
  669. #undef MAP_ANALOG
  670. }
  671. static void ImGui_ImplGlfw_UpdateMonitors()
  672. {
  673. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  674. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  675. int monitors_count = 0;
  676. GLFWmonitor** glfw_monitors = glfwGetMonitors(&monitors_count);
  677. platform_io.Monitors.resize(0);
  678. for (int n = 0; n < monitors_count; n++)
  679. {
  680. ImGuiPlatformMonitor monitor;
  681. int x, y;
  682. glfwGetMonitorPos(glfw_monitors[n], &x, &y);
  683. const GLFWvidmode* vid_mode = glfwGetVideoMode(glfw_monitors[n]);
  684. monitor.MainPos = monitor.WorkPos = ImVec2((float)x, (float)y);
  685. monitor.MainSize = monitor.WorkSize = ImVec2((float)vid_mode->width, (float)vid_mode->height);
  686. #if GLFW_HAS_MONITOR_WORK_AREA
  687. int w, h;
  688. glfwGetMonitorWorkarea(glfw_monitors[n], &x, &y, &w, &h);
  689. if (w > 0 && h > 0) // Workaround a small GLFW issue reporting zero on monitor changes: https://github.com/glfw/glfw/pull/1761
  690. {
  691. monitor.WorkPos = ImVec2((float)x, (float)y);
  692. monitor.WorkSize = ImVec2((float)w, (float)h);
  693. }
  694. #endif
  695. #if GLFW_HAS_PER_MONITOR_DPI
  696. // 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.
  697. float x_scale, y_scale;
  698. glfwGetMonitorContentScale(glfw_monitors[n], &x_scale, &y_scale);
  699. monitor.DpiScale = x_scale;
  700. #endif
  701. platform_io.Monitors.push_back(monitor);
  702. }
  703. bd->WantUpdateMonitors = false;
  704. }
  705. void ImGui_ImplGlfw_NewFrame()
  706. {
  707. ImGuiIO& io = ImGui::GetIO();
  708. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  709. IM_ASSERT(bd != NULL && "Did you call ImGui_ImplGlfw_InitForXXX()?");
  710. // Setup display size (every frame to accommodate for window resizing)
  711. int w, h;
  712. int display_w, display_h;
  713. glfwGetWindowSize(bd->Window, &w, &h);
  714. glfwGetFramebufferSize(bd->Window, &display_w, &display_h);
  715. io.DisplaySize = ImVec2((float)w, (float)h);
  716. if (w > 0 && h > 0)
  717. io.DisplayFramebufferScale = ImVec2((float)display_w / (float)w, (float)display_h / (float)h);
  718. if (bd->WantUpdateMonitors)
  719. ImGui_ImplGlfw_UpdateMonitors();
  720. // Setup time step
  721. double current_time = glfwGetTime();
  722. io.DeltaTime = bd->Time > 0.0 ? (float)(current_time - bd->Time) : (float)(1.0f / 60.0f);
  723. bd->Time = current_time;
  724. ImGui_ImplGlfw_UpdateMouseData();
  725. ImGui_ImplGlfw_UpdateMouseCursor();
  726. // Update game controllers (if enabled and available)
  727. ImGui_ImplGlfw_UpdateGamepads();
  728. }
  729. //--------------------------------------------------------------------------------------------------------
  730. // MULTI-VIEWPORT / PLATFORM INTERFACE SUPPORT
  731. // This is an _advanced_ and _optional_ feature, allowing the backend to create and handle multiple viewports simultaneously.
  732. // 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..
  733. //--------------------------------------------------------------------------------------------------------
  734. // Helper structure we store in the void* RenderUserData field of each ImGuiViewport to easily retrieve our backend data.
  735. struct ImGui_ImplGlfw_ViewportData
  736. {
  737. GLFWwindow* Window;
  738. bool WindowOwned;
  739. int IgnoreWindowPosEventFrame;
  740. int IgnoreWindowSizeEventFrame;
  741. ImGui_ImplGlfw_ViewportData() { Window = NULL; WindowOwned = false; IgnoreWindowSizeEventFrame = IgnoreWindowPosEventFrame = -1; }
  742. ~ImGui_ImplGlfw_ViewportData() { IM_ASSERT(Window == NULL); }
  743. };
  744. static void ImGui_ImplGlfw_WindowCloseCallback(GLFWwindow* window)
  745. {
  746. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window))
  747. viewport->PlatformRequestClose = true;
  748. }
  749. // GLFW may dispatch window pos/size events after calling glfwSetWindowPos()/glfwSetWindowSize().
  750. // However: depending on the platform the callback may be invoked at different time:
  751. // - on Windows it appears to be called within the glfwSetWindowPos()/glfwSetWindowSize() call
  752. // - on Linux it is queued and invoked during glfwPollEvents()
  753. // Because the event doesn't always fire on glfwSetWindowXXX() we use a frame counter tag to only
  754. // ignore recent glfwSetWindowXXX() calls.
  755. static void ImGui_ImplGlfw_WindowPosCallback(GLFWwindow* window, int, int)
  756. {
  757. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window))
  758. {
  759. if (ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData)
  760. {
  761. bool ignore_event = (ImGui::GetFrameCount() <= vd->IgnoreWindowPosEventFrame + 1);
  762. //data->IgnoreWindowPosEventFrame = -1;
  763. if (ignore_event)
  764. return;
  765. }
  766. viewport->PlatformRequestMove = true;
  767. }
  768. }
  769. static void ImGui_ImplGlfw_WindowSizeCallback(GLFWwindow* window, int, int)
  770. {
  771. if (ImGuiViewport* viewport = ImGui::FindViewportByPlatformHandle(window))
  772. {
  773. if (ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData)
  774. {
  775. bool ignore_event = (ImGui::GetFrameCount() <= vd->IgnoreWindowSizeEventFrame + 1);
  776. //data->IgnoreWindowSizeEventFrame = -1;
  777. if (ignore_event)
  778. return;
  779. }
  780. viewport->PlatformRequestResize = true;
  781. }
  782. }
  783. static void ImGui_ImplGlfw_CreateWindow(ImGuiViewport* viewport)
  784. {
  785. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  786. ImGui_ImplGlfw_ViewportData* vd = IM_NEW(ImGui_ImplGlfw_ViewportData)();
  787. viewport->PlatformUserData = vd;
  788. // GLFW 3.2 unfortunately always set focus on glfwCreateWindow() if GLFW_VISIBLE is set, regardless of GLFW_FOCUSED
  789. // With GLFW 3.3, the hint GLFW_FOCUS_ON_SHOW fixes this problem
  790. glfwWindowHint(GLFW_VISIBLE, false);
  791. glfwWindowHint(GLFW_FOCUSED, false);
  792. #if GLFW_HAS_FOCUS_ON_SHOW
  793. glfwWindowHint(GLFW_FOCUS_ON_SHOW, false);
  794. #endif
  795. glfwWindowHint(GLFW_DECORATED, (viewport->Flags & ImGuiViewportFlags_NoDecoration) ? false : true);
  796. #if GLFW_HAS_WINDOW_TOPMOST
  797. glfwWindowHint(GLFW_FLOATING, (viewport->Flags & ImGuiViewportFlags_TopMost) ? true : false);
  798. #endif
  799. GLFWwindow* share_window = (bd->ClientApi == GlfwClientApi_OpenGL) ? bd->Window : NULL;
  800. vd->Window = glfwCreateWindow((int)viewport->Size.x, (int)viewport->Size.y, "No Title Yet", NULL, share_window);
  801. vd->WindowOwned = true;
  802. viewport->PlatformHandle = (void*)vd->Window;
  803. #ifdef _WIN32
  804. viewport->PlatformHandleRaw = glfwGetWin32Window(vd->Window);
  805. #elif defined(__APPLE__)
  806. viewport->PlatformHandleRaw = (void*)glfwGetCocoaWindow(vd->Window);
  807. #endif
  808. glfwSetWindowPos(vd->Window, (int)viewport->Pos.x, (int)viewport->Pos.y);
  809. // Install GLFW callbacks for secondary viewports
  810. glfwSetWindowFocusCallback(vd->Window, ImGui_ImplGlfw_WindowFocusCallback);
  811. glfwSetCursorEnterCallback(vd->Window, ImGui_ImplGlfw_CursorEnterCallback);
  812. glfwSetCursorPosCallback(vd->Window, ImGui_ImplGlfw_CursorPosCallback);
  813. glfwSetMouseButtonCallback(vd->Window, ImGui_ImplGlfw_MouseButtonCallback);
  814. glfwSetScrollCallback(vd->Window, ImGui_ImplGlfw_ScrollCallback);
  815. glfwSetKeyCallback(vd->Window, ImGui_ImplGlfw_KeyCallback);
  816. glfwSetCharCallback(vd->Window, ImGui_ImplGlfw_CharCallback);
  817. glfwSetWindowCloseCallback(vd->Window, ImGui_ImplGlfw_WindowCloseCallback);
  818. glfwSetWindowPosCallback(vd->Window, ImGui_ImplGlfw_WindowPosCallback);
  819. glfwSetWindowSizeCallback(vd->Window, ImGui_ImplGlfw_WindowSizeCallback);
  820. if (bd->ClientApi == GlfwClientApi_OpenGL)
  821. {
  822. glfwMakeContextCurrent(vd->Window);
  823. glfwSwapInterval(0);
  824. }
  825. }
  826. static void ImGui_ImplGlfw_DestroyWindow(ImGuiViewport* viewport)
  827. {
  828. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  829. if (ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData)
  830. {
  831. if (vd->WindowOwned)
  832. {
  833. #if !GLFW_HAS_MOUSE_PASSTHROUGH && GLFW_HAS_WINDOW_HOVERED && defined(_WIN32)
  834. HWND hwnd = (HWND)viewport->PlatformHandleRaw;
  835. ::RemovePropA(hwnd, "IMGUI_VIEWPORT");
  836. #endif
  837. // Release any keys that were pressed in the window being destroyed and are still held down,
  838. // because we will not receive any release events after window is destroyed.
  839. for (int i = 0; i < IM_ARRAYSIZE(bd->KeyOwnerWindows); i++)
  840. if (bd->KeyOwnerWindows[i] == vd->Window)
  841. 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.
  842. glfwDestroyWindow(vd->Window);
  843. }
  844. vd->Window = NULL;
  845. IM_DELETE(vd);
  846. }
  847. viewport->PlatformUserData = viewport->PlatformHandle = NULL;
  848. }
  849. // We have submitted https://github.com/glfw/glfw/pull/1568 to allow GLFW to support "transparent inputs".
  850. // In the meanwhile we implement custom per-platform workarounds here (FIXME-VIEWPORT: Implement same work-around for Linux/OSX!)
  851. #if !GLFW_HAS_MOUSE_PASSTHROUGH && GLFW_HAS_WINDOW_HOVERED && defined(_WIN32)
  852. static LRESULT CALLBACK WndProcNoInputs(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  853. {
  854. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  855. if (msg == WM_NCHITTEST)
  856. {
  857. // Let mouse pass-through the window. This will allow the backend to call io.AddMouseViewportEvent() properly (which is OPTIONAL).
  858. // The ImGuiViewportFlags_NoInputs flag is set while dragging a viewport, as want to detect the window behind the one we are dragging.
  859. // If you cannot easily access those viewport flags from your windowing/event code: you may manually synchronize its state e.g. in
  860. // your main loop after calling UpdatePlatformWindows(). Iterate all viewports/platform windows and pass the flag to your windowing system.
  861. ImGuiViewport* viewport = (ImGuiViewport*)::GetPropA(hWnd, "IMGUI_VIEWPORT");
  862. if (viewport->Flags & ImGuiViewportFlags_NoInputs)
  863. return HTTRANSPARENT;
  864. }
  865. return ::CallWindowProc(bd->GlfwWndProc, hWnd, msg, wParam, lParam);
  866. }
  867. #endif
  868. static void ImGui_ImplGlfw_ShowWindow(ImGuiViewport* viewport)
  869. {
  870. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  871. #if defined(_WIN32)
  872. // GLFW hack: Hide icon from task bar
  873. HWND hwnd = (HWND)viewport->PlatformHandleRaw;
  874. if (viewport->Flags & ImGuiViewportFlags_NoTaskBarIcon)
  875. {
  876. LONG ex_style = ::GetWindowLong(hwnd, GWL_EXSTYLE);
  877. ex_style &= ~WS_EX_APPWINDOW;
  878. ex_style |= WS_EX_TOOLWINDOW;
  879. ::SetWindowLong(hwnd, GWL_EXSTYLE, ex_style);
  880. }
  881. // GLFW hack: install hook for WM_NCHITTEST message handler
  882. #if !GLFW_HAS_MOUSE_PASSTHROUGH && GLFW_HAS_WINDOW_HOVERED && defined(_WIN32)
  883. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  884. ::SetPropA(hwnd, "IMGUI_VIEWPORT", viewport);
  885. if (bd->GlfwWndProc == NULL)
  886. bd->GlfwWndProc = (WNDPROC)::GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  887. ::SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR)WndProcNoInputs);
  888. #endif
  889. #if !GLFW_HAS_FOCUS_ON_SHOW
  890. // GLFW hack: GLFW 3.2 has a bug where glfwShowWindow() also activates/focus the window.
  891. // 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.
  892. // See https://github.com/glfw/glfw/issues/1189
  893. // FIXME-VIEWPORT: Implement same work-around for Linux/OSX in the meanwhile.
  894. if (viewport->Flags & ImGuiViewportFlags_NoFocusOnAppearing)
  895. {
  896. ::ShowWindow(hwnd, SW_SHOWNA);
  897. return;
  898. }
  899. #endif
  900. #endif
  901. glfwShowWindow(vd->Window);
  902. }
  903. static ImVec2 ImGui_ImplGlfw_GetWindowPos(ImGuiViewport* viewport)
  904. {
  905. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  906. int x = 0, y = 0;
  907. glfwGetWindowPos(vd->Window, &x, &y);
  908. return ImVec2((float)x, (float)y);
  909. }
  910. static void ImGui_ImplGlfw_SetWindowPos(ImGuiViewport* viewport, ImVec2 pos)
  911. {
  912. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  913. vd->IgnoreWindowPosEventFrame = ImGui::GetFrameCount();
  914. glfwSetWindowPos(vd->Window, (int)pos.x, (int)pos.y);
  915. }
  916. static ImVec2 ImGui_ImplGlfw_GetWindowSize(ImGuiViewport* viewport)
  917. {
  918. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  919. int w = 0, h = 0;
  920. glfwGetWindowSize(vd->Window, &w, &h);
  921. return ImVec2((float)w, (float)h);
  922. }
  923. static void ImGui_ImplGlfw_SetWindowSize(ImGuiViewport* viewport, ImVec2 size)
  924. {
  925. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  926. #if __APPLE__ && !GLFW_HAS_OSX_WINDOW_POS_FIX
  927. // Native OS windows are positioned from the bottom-left corner on macOS, whereas on other platforms they are
  928. // positioned from the upper-left corner. GLFW makes an effort to convert macOS style coordinates, however it
  929. // doesn't handle it when changing size. We are manually moving the window in order for changes of size to be based
  930. // on the upper-left corner.
  931. int x, y, width, height;
  932. glfwGetWindowPos(vd->Window, &x, &y);
  933. glfwGetWindowSize(vd->Window, &width, &height);
  934. glfwSetWindowPos(vd->Window, x, y - height + size.y);
  935. #endif
  936. vd->IgnoreWindowSizeEventFrame = ImGui::GetFrameCount();
  937. glfwSetWindowSize(vd->Window, (int)size.x, (int)size.y);
  938. }
  939. static void ImGui_ImplGlfw_SetWindowTitle(ImGuiViewport* viewport, const char* title)
  940. {
  941. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  942. glfwSetWindowTitle(vd->Window, title);
  943. }
  944. static void ImGui_ImplGlfw_SetWindowFocus(ImGuiViewport* viewport)
  945. {
  946. #if GLFW_HAS_FOCUS_WINDOW
  947. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  948. glfwFocusWindow(vd->Window);
  949. #else
  950. // 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.
  951. (void)viewport;
  952. #endif
  953. }
  954. static bool ImGui_ImplGlfw_GetWindowFocus(ImGuiViewport* viewport)
  955. {
  956. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  957. return glfwGetWindowAttrib(vd->Window, GLFW_FOCUSED) != 0;
  958. }
  959. static bool ImGui_ImplGlfw_GetWindowMinimized(ImGuiViewport* viewport)
  960. {
  961. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  962. return glfwGetWindowAttrib(vd->Window, GLFW_ICONIFIED) != 0;
  963. }
  964. #if GLFW_HAS_WINDOW_ALPHA
  965. static void ImGui_ImplGlfw_SetWindowAlpha(ImGuiViewport* viewport, float alpha)
  966. {
  967. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  968. glfwSetWindowOpacity(vd->Window, alpha);
  969. }
  970. #endif
  971. static void ImGui_ImplGlfw_RenderWindow(ImGuiViewport* viewport, void*)
  972. {
  973. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  974. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  975. if (bd->ClientApi == GlfwClientApi_OpenGL)
  976. glfwMakeContextCurrent(vd->Window);
  977. }
  978. static void ImGui_ImplGlfw_SwapBuffers(ImGuiViewport* viewport, void*)
  979. {
  980. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  981. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  982. if (bd->ClientApi == GlfwClientApi_OpenGL)
  983. {
  984. glfwMakeContextCurrent(vd->Window);
  985. glfwSwapBuffers(vd->Window);
  986. }
  987. }
  988. //--------------------------------------------------------------------------------------------------------
  989. // Vulkan support (the Vulkan renderer needs to call a platform-side support function to create the surface)
  990. //--------------------------------------------------------------------------------------------------------
  991. // Avoid including <vulkan.h> so we can build without it
  992. #if GLFW_HAS_VULKAN
  993. #ifndef VULKAN_H_
  994. #define VK_DEFINE_HANDLE(object) typedef struct object##_T* object;
  995. #if defined(__LP64__) || defined(_WIN64) || defined(__x86_64__) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__)
  996. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef struct object##_T *object;
  997. #else
  998. #define VK_DEFINE_NON_DISPATCHABLE_HANDLE(object) typedef uint64_t object;
  999. #endif
  1000. VK_DEFINE_HANDLE(VkInstance)
  1001. VK_DEFINE_NON_DISPATCHABLE_HANDLE(VkSurfaceKHR)
  1002. struct VkAllocationCallbacks;
  1003. enum VkResult { VK_RESULT_MAX_ENUM = 0x7FFFFFFF };
  1004. #endif // VULKAN_H_
  1005. extern "C" { extern GLFWAPI VkResult glfwCreateWindowSurface(VkInstance instance, GLFWwindow* window, const VkAllocationCallbacks* allocator, VkSurfaceKHR* surface); }
  1006. static int ImGui_ImplGlfw_CreateVkSurface(ImGuiViewport* viewport, ImU64 vk_instance, const void* vk_allocator, ImU64* out_vk_surface)
  1007. {
  1008. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  1009. ImGui_ImplGlfw_ViewportData* vd = (ImGui_ImplGlfw_ViewportData*)viewport->PlatformUserData;
  1010. IM_UNUSED(bd);
  1011. IM_ASSERT(bd->ClientApi == GlfwClientApi_Vulkan);
  1012. VkResult err = glfwCreateWindowSurface((VkInstance)vk_instance, vd->Window, (const VkAllocationCallbacks*)vk_allocator, (VkSurfaceKHR*)out_vk_surface);
  1013. return (int)err;
  1014. }
  1015. #endif // GLFW_HAS_VULKAN
  1016. static void ImGui_ImplGlfw_InitPlatformInterface()
  1017. {
  1018. // Register platform interface (will be coupled with a renderer interface)
  1019. ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData();
  1020. ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
  1021. platform_io.Platform_CreateWindow = ImGui_ImplGlfw_CreateWindow;
  1022. platform_io.Platform_DestroyWindow = ImGui_ImplGlfw_DestroyWindow;
  1023. platform_io.Platform_ShowWindow = ImGui_ImplGlfw_ShowWindow;
  1024. platform_io.Platform_SetWindowPos = ImGui_ImplGlfw_SetWindowPos;
  1025. platform_io.Platform_GetWindowPos = ImGui_ImplGlfw_GetWindowPos;
  1026. platform_io.Platform_SetWindowSize = ImGui_ImplGlfw_SetWindowSize;
  1027. platform_io.Platform_GetWindowSize = ImGui_ImplGlfw_GetWindowSize;
  1028. platform_io.Platform_SetWindowFocus = ImGui_ImplGlfw_SetWindowFocus;
  1029. platform_io.Platform_GetWindowFocus = ImGui_ImplGlfw_GetWindowFocus;
  1030. platform_io.Platform_GetWindowMinimized = ImGui_ImplGlfw_GetWindowMinimized;
  1031. platform_io.Platform_SetWindowTitle = ImGui_ImplGlfw_SetWindowTitle;
  1032. platform_io.Platform_RenderWindow = ImGui_ImplGlfw_RenderWindow;
  1033. platform_io.Platform_SwapBuffers = ImGui_ImplGlfw_SwapBuffers;
  1034. #if GLFW_HAS_WINDOW_ALPHA
  1035. platform_io.Platform_SetWindowAlpha = ImGui_ImplGlfw_SetWindowAlpha;
  1036. #endif
  1037. #if GLFW_HAS_VULKAN
  1038. platform_io.Platform_CreateVkSurface = ImGui_ImplGlfw_CreateVkSurface;
  1039. #endif
  1040. // Register main window handle (which is owned by the main application, not by us)
  1041. // 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.
  1042. ImGuiViewport* main_viewport = ImGui::GetMainViewport();
  1043. ImGui_ImplGlfw_ViewportData* vd = IM_NEW(ImGui_ImplGlfw_ViewportData)();
  1044. vd->Window = bd->Window;
  1045. vd->WindowOwned = false;
  1046. main_viewport->PlatformUserData = vd;
  1047. main_viewport->PlatformHandle = (void*)bd->Window;
  1048. }
  1049. static void ImGui_ImplGlfw_ShutdownPlatformInterface()
  1050. {
  1051. ImGui::DestroyPlatformWindows();
  1052. }
  1053. #if defined(__clang__)
  1054. #pragma clang diagnostic pop
  1055. #endif