Made internal clipboard/IME handlers not rely on implicit GImGui context (#5856)

Code in SetPlatformImeDataFn_DefaultImpl amends 3a90dc389 by temporarily setting field in caller site.
This commit is contained in:
ocornut 2023-03-08 16:21:25 +01:00
parent c8ad25caa6
commit b5f9381036

View File

@ -991,8 +991,8 @@ static void WindowSettingsHandler_ApplyAll(ImGuiContext*, ImGuiSetti
static void WindowSettingsHandler_WriteAll(ImGuiContext*, ImGuiSettingsHandler*, ImGuiTextBuffer* buf); static void WindowSettingsHandler_WriteAll(ImGuiContext*, ImGuiSettingsHandler*, ImGuiTextBuffer* buf);
// Platform Dependents default implementation for IO functions // Platform Dependents default implementation for IO functions
static const char* GetClipboardTextFn_DefaultImpl(void* user_data); static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx);
static void SetClipboardTextFn_DefaultImpl(void* user_data, const char* text); static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text);
static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport, ImGuiPlatformImeData* data); static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport, ImGuiPlatformImeData* data);
namespace ImGui namespace ImGui
@ -1220,12 +1220,9 @@ ImGuiIO::ImGuiIO()
ConfigMemoryCompactTimer = 60.0f; ConfigMemoryCompactTimer = 60.0f;
// Platform Functions // Platform Functions
// Note: Initialize() will setup default clipboard/ime handlers.
BackendPlatformName = BackendRendererName = NULL; BackendPlatformName = BackendRendererName = NULL;
BackendPlatformUserData = BackendRendererUserData = BackendLanguageUserData = NULL; BackendPlatformUserData = BackendRendererUserData = BackendLanguageUserData = NULL;
GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations
SetClipboardTextFn = SetClipboardTextFn_DefaultImpl;
ClipboardUserData = NULL;
SetPlatformImeDataFn = SetPlatformImeDataFn_DefaultImpl;
// Input (NB: we already have memset zero the entire structure!) // Input (NB: we already have memset zero the entire structure!)
MousePos = ImVec2(-FLT_MAX, -FLT_MAX); MousePos = ImVec2(-FLT_MAX, -FLT_MAX);
@ -3496,6 +3493,12 @@ void ImGui::Initialize()
// Setup default localization table // Setup default localization table
LocalizeRegisterEntries(GLocalizationEntriesEnUS, IM_ARRAYSIZE(GLocalizationEntriesEnUS)); LocalizeRegisterEntries(GLocalizationEntriesEnUS, IM_ARRAYSIZE(GLocalizationEntriesEnUS));
// Setup default platform clipboard/IME handlers.
g.IO.GetClipboardTextFn = GetClipboardTextFn_DefaultImpl; // Platform dependent default implementations
g.IO.SetClipboardTextFn = SetClipboardTextFn_DefaultImpl;
g.IO.ClipboardUserData = (void*)&g; // Default implementation use the ImGuiContext as user data (ideally those would be arguments to the function)
g.IO.SetPlatformImeDataFn = SetPlatformImeDataFn_DefaultImpl;
// Create default viewport // Create default viewport
ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)(); ImGuiViewportP* viewport = IM_NEW(ImGuiViewportP)();
g.Viewports.push_back(viewport); g.Viewports.push_back(viewport);
@ -4837,7 +4840,19 @@ void ImGui::EndFrame()
if (g.IO.SetPlatformImeDataFn && memcmp(ime_data, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0) if (g.IO.SetPlatformImeDataFn && memcmp(ime_data, &g.PlatformImeDataPrev, sizeof(ImGuiPlatformImeData)) != 0)
{ {
IMGUI_DEBUG_LOG_IO("Calling io.SetPlatformImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y); IMGUI_DEBUG_LOG_IO("Calling io.SetPlatformImeDataFn(): WantVisible: %d, InputPos (%.2f,%.2f)\n", ime_data->WantVisible, ime_data->InputPos.x, ime_data->InputPos.y);
g.IO.SetPlatformImeDataFn(GetMainViewport(), ime_data); ImGuiViewport* viewport = GetMainViewport();
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
if (viewport->PlatformHandleRaw == NULL && g.IO.ImeWindowHandle != NULL)
{
viewport->PlatformHandleRaw = g.IO.ImeWindowHandle;
g.IO.SetPlatformImeDataFn(viewport, ime_data);
viewport->PlatformHandleRaw = NULL;
}
else
#endif
{
g.IO.SetPlatformImeDataFn(viewport, ime_data);
}
} }
// Hide implicit/fallback "Debug" window if it hasn't been used // Hide implicit/fallback "Debug" window if it hasn't been used
@ -12893,9 +12908,9 @@ static void ImGui::UpdateViewportsNewFrame()
// Win32 clipboard implementation // Win32 clipboard implementation
// We use g.ClipboardHandlerData for temporary storage to ensure it is freed on Shutdown() // We use g.ClipboardHandlerData for temporary storage to ensure it is freed on Shutdown()
static const char* GetClipboardTextFn_DefaultImpl(void*) static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *(ImGuiContext*)user_data_ctx;
g.ClipboardHandlerData.clear(); g.ClipboardHandlerData.clear();
if (!::OpenClipboard(NULL)) if (!::OpenClipboard(NULL))
return NULL; return NULL;
@ -12956,8 +12971,9 @@ static void SetClipboardTextFn_DefaultImpl(void*, const char* text)
} }
} }
static const char* GetClipboardTextFn_DefaultImpl(void*) static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx)
{ {
ImGuiContext& g = *(ImGuiContext*)user_data_ctx;
if (!main_clipboard) if (!main_clipboard)
PasteboardCreate(kPasteboardClipboard, &main_clipboard); PasteboardCreate(kPasteboardClipboard, &main_clipboard);
PasteboardSynchronize(main_clipboard); PasteboardSynchronize(main_clipboard);
@ -12975,7 +12991,6 @@ static const char* GetClipboardTextFn_DefaultImpl(void*)
CFDataRef cf_data; CFDataRef cf_data;
if (PasteboardCopyItemFlavorData(main_clipboard, item_id, CFSTR("public.utf8-plain-text"), &cf_data) == noErr) if (PasteboardCopyItemFlavorData(main_clipboard, item_id, CFSTR("public.utf8-plain-text"), &cf_data) == noErr)
{ {
ImGuiContext& g = *GImGui;
g.ClipboardHandlerData.clear(); g.ClipboardHandlerData.clear();
int length = (int)CFDataGetLength(cf_data); int length = (int)CFDataGetLength(cf_data);
g.ClipboardHandlerData.resize(length + 1); g.ClipboardHandlerData.resize(length + 1);
@ -12992,15 +13007,15 @@ static const char* GetClipboardTextFn_DefaultImpl(void*)
#else #else
// Local Dear ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers. // Local Dear ImGui-only clipboard implementation, if user hasn't defined better clipboard handlers.
static const char* GetClipboardTextFn_DefaultImpl(void*) static const char* GetClipboardTextFn_DefaultImpl(void* user_data_ctx)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *(ImGuiContext*)user_data_ctx;
return g.ClipboardHandlerData.empty() ? NULL : g.ClipboardHandlerData.begin(); return g.ClipboardHandlerData.empty() ? NULL : g.ClipboardHandlerData.begin();
} }
static void SetClipboardTextFn_DefaultImpl(void*, const char* text) static void SetClipboardTextFn_DefaultImpl(void* user_data_ctx, const char* text)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *(ImGuiContext*)user_data_ctx;
g.ClipboardHandlerData.clear(); g.ClipboardHandlerData.clear();
const char* text_end = text + strlen(text); const char* text_end = text + strlen(text);
g.ClipboardHandlerData.resize((int)(text_end - text) + 1); g.ClipboardHandlerData.resize((int)(text_end - text) + 1);
@ -13022,10 +13037,6 @@ static void SetPlatformImeDataFn_DefaultImpl(ImGuiViewport* viewport, ImGuiPlatf
{ {
// Notify OS Input Method Editor of text input position // Notify OS Input Method Editor of text input position
HWND hwnd = (HWND)viewport->PlatformHandleRaw; HWND hwnd = (HWND)viewport->PlatformHandleRaw;
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
if (hwnd == 0)
hwnd = (HWND)ImGui::GetIO().ImeWindowHandle;
#endif
if (hwnd == 0) if (hwnd == 0)
return; return;