Skip to content

Commit

Permalink
Viewports: Backported 'void* ImGuiViewport::PlatformHandle' from dock…
Browse files Browse the repository at this point in the history
…ing branch for use by backends.
  • Loading branch information
ocornut committed Jun 26, 2024
1 parent eb1cc4b commit 32f9dfc
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 19 deletions.
15 changes: 8 additions & 7 deletions backends/imgui_impl_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ struct ImGui_ImplGlfw_Data
GLFWcharfun PrevUserCallbackChar;
GLFWmonitorfun PrevUserCallbackMonitor;
#ifdef _WIN32
WNDPROC GlfwWndProc;
WNDPROC PrevWndProc;
#endif

ImGui_ImplGlfw_Data() { memset((void*)this, 0, sizeof(*this)); }
Expand Down Expand Up @@ -497,7 +497,7 @@ static LRESULT CALLBACK ImGui_ImplGlfw_WndProc(HWND hWnd, UINT msg, WPARAM wPara
ImGui::GetIO().AddMouseSourceEvent(GetMouseSourceFromMessageExtraInfo());
break;
}
return ::CallWindowProcW(bd->GlfwWndProc, hWnd, msg, wParam, lParam);
return ::CallWindowProcW(bd->PrevWndProc, hWnd, msg, wParam, lParam);
}
#endif

Expand Down Expand Up @@ -612,6 +612,7 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw

// Set platform dependent data in viewport
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
main_viewport->PlatformHandle = (void*)bd->Window;
#ifdef _WIN32
main_viewport->PlatformHandleRaw = glfwGetWin32Window(bd->Window);
#elif defined(__APPLE__)
Expand All @@ -622,8 +623,8 @@ static bool ImGui_ImplGlfw_Init(GLFWwindow* window, bool install_callbacks, Glfw

// Windows: register a WndProc hook so we can intercept some messages.
#ifdef _WIN32
bd->GlfwWndProc = (WNDPROC)::GetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC);
IM_ASSERT(bd->GlfwWndProc != nullptr);
bd->PrevWndProc = (WNDPROC)::GetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC);
IM_ASSERT(bd->PrevWndProc != nullptr);
::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)ImGui_ImplGlfw_WndProc);
#endif

Expand Down Expand Up @@ -661,11 +662,11 @@ void ImGui_ImplGlfw_Shutdown()
for (ImGuiMouseCursor cursor_n = 0; cursor_n < ImGuiMouseCursor_COUNT; cursor_n++)
glfwDestroyCursor(bd->MouseCursors[cursor_n]);

// Windows: register a WndProc hook so we can intercept some messages.
// Windows: restore our WndProc hook
#ifdef _WIN32
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)bd->GlfwWndProc);
bd->GlfwWndProc = nullptr;
::SetWindowLongPtrW((HWND)main_viewport->PlatformHandleRaw, GWLP_WNDPROC, (LONG_PTR)bd->PrevWndProc);
bd->PrevWndProc = nullptr;
#endif

io.BackendPlatformName = nullptr;
Expand Down
4 changes: 3 additions & 1 deletion backends/imgui_impl_osx.mm
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ - (void)setImePosX:(float)posX imePosY:(float)posY

- (void)updateImePosWithView:(NSView *)view
{
NSWindow *window = view.window;
NSWindow* window = view.window;
if (!window)
return;
NSRect contentRect = [window contentRectForFrameRect:window.frame];
Expand Down Expand Up @@ -402,6 +402,8 @@ bool ImGui_ImplOSX_Init(NSView* view)
//io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos; // We can honor io.WantSetMousePos requests (optional, rarely used)

bd->Observer = [ImGuiObserver new];
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (__bridge_retained void*)bd->Window;

// Load cursors. Some of them are undocumented.
bd->MouseCursorHidden = false;
Expand Down
17 changes: 9 additions & 8 deletions backends/imgui_impl_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ bool ImGui_ImplSDL2_ProcessEvent(const SDL_Event* event)
return false;
}

static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer)
static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer, void* sdl_gl_context)
{
ImGuiIO& io = ImGui::GetIO();
IMGUI_CHECKVERSION();
Expand Down Expand Up @@ -448,6 +448,7 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer)
// Set platform dependent data in viewport
// Our mouse update function expect PlatformHandle to be filled for the main viewport
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
main_viewport->PlatformHandle = (void*)window;
main_viewport->PlatformHandleRaw = nullptr;
SDL_SysWMinfo info;
SDL_VERSION(&info.version);
Expand Down Expand Up @@ -481,44 +482,44 @@ static bool ImGui_ImplSDL2_Init(SDL_Window* window, SDL_Renderer* renderer)
SDL_SetHint(SDL_HINT_MOUSE_AUTO_CAPTURE, "0");
#endif

(void)sdl_gl_context; // Unused in 'master' branch.
return true;
}

bool ImGui_ImplSDL2_InitForOpenGL(SDL_Window* window, void* sdl_gl_context)
{
IM_UNUSED(sdl_gl_context); // Viewport branch will need this.
return ImGui_ImplSDL2_Init(window, nullptr);
return ImGui_ImplSDL2_Init(window, nullptr, sdl_gl_context);
}

bool ImGui_ImplSDL2_InitForVulkan(SDL_Window* window)
{
#if !SDL_HAS_VULKAN
IM_ASSERT(0 && "Unsupported");
#endif
return ImGui_ImplSDL2_Init(window, nullptr);
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
}

bool ImGui_ImplSDL2_InitForD3D(SDL_Window* window)
{
#if !defined(_WIN32)
IM_ASSERT(0 && "Unsupported");
#endif
return ImGui_ImplSDL2_Init(window, nullptr);
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
}

bool ImGui_ImplSDL2_InitForMetal(SDL_Window* window)
{
return ImGui_ImplSDL2_Init(window, nullptr);
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
}

bool ImGui_ImplSDL2_InitForSDLRenderer(SDL_Window* window, SDL_Renderer* renderer)
{
return ImGui_ImplSDL2_Init(window, renderer);
return ImGui_ImplSDL2_Init(window, renderer, nullptr);
}

bool ImGui_ImplSDL2_InitForOther(SDL_Window* window)
{
return ImGui_ImplSDL2_Init(window, nullptr);
return ImGui_ImplSDL2_Init(window, nullptr, nullptr);
}

static void ImGui_ImplSDL2_CloseGamepads();
Expand Down
2 changes: 1 addition & 1 deletion backends/imgui_impl_sdl3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ bool ImGui_ImplSDL3_ProcessEvent(const SDL_Event* event)

static void ImGui_ImplSDL3_SetupPlatformHandles(ImGuiViewport* viewport, SDL_Window* window)
{
IM_UNUSED(window);
viewport->PlatformHandle = window;
viewport->PlatformHandleRaw = nullptr;
#if defined(__WIN32__) && !defined(__WINRT__)
viewport->PlatformHandleRaw = (HWND)SDL_GetProperty(SDL_GetWindowProperties(window), "SDL.window.win32.hwnd", nullptr);
Expand Down
3 changes: 2 additions & 1 deletion backends/imgui_impl_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ static bool ImGui_ImplWin32_InitEx(void* hwnd, bool platform_has_own_dc)
ImGui_ImplWin32_UpdateKeyboardCodePage();

// Set platform dependent data in viewport
ImGui::GetMainViewport()->PlatformHandleRaw = (void*)hwnd;
ImGuiViewport* main_viewport = ImGui::GetMainViewport();
main_viewport->PlatformHandle = main_viewport->PlatformHandleRaw = (void*)bd->hWnd;
IM_UNUSED(platform_has_own_dc); // Used in 'docking' branch

// Dynamically load XInput library
Expand Down
2 changes: 2 additions & 0 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ Other changes:
frame instead of last drag source frame, which makes a difference if not resubmitting
payload every frame. (#143)
- Debug Tools: Metrics/Debugger: Browsing a Storage perform hover lookup on identifier.
- Viewports: Backported 'void* ImGuiViewport::PlatformHandle' from docking branch for
use by backends.
- Backends: SDL3: Update for API removal of keysym field in SDL_KeyboardEvent. (#7728)
- Backends: Vulkan: Remove Volk/ from volk.h #include directives. (#7722, #6582, #4854)
[@martin-ejdestig]
Expand Down
3 changes: 2 additions & 1 deletion imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -2202,7 +2202,7 @@ struct ImGuiIO
// - Backends may have other side-effects on focus loss, so this will reduce side-effects but not necessary remove all of them.
bool ConfigDebugIgnoreFocusLoss; // = false // Ignore io.AddFocusEvent(false), consequently not calling io.ClearInputKeys()/io.ClearInputMouse() in input processing.

// Options to audit .ini data
// Option to audit .ini data
bool ConfigDebugIniSettings; // = false // Save .ini data with extra comments (particularly helpful for Docking, but makes saving slower)

//------------------------------------------------------------------
Expand Down Expand Up @@ -3240,6 +3240,7 @@ struct ImGuiViewport
ImVec2 WorkSize; // Work Area: Size of the viewport minus task bars, menu bars, status bars (<= Size)

// Platform/Backend Dependent Data
void* PlatformHandle; // void* to hold higher-level, platform window handle (e.g. HWND, GLFWWindow*, SDL_Window*)
void* PlatformHandleRaw; // void* to hold lower-level, platform-native window handle (under Win32 this is expected to be a HWND, unused for other platforms)

ImGuiViewport() { memset(this, 0, sizeof(*this)); }
Expand Down

0 comments on commit 32f9dfc

Please sign in to comment.