Made default clipboard handlers for Win32 and OSX use a buffer inside the main context instead of a static buffer, so it can be freed properly on Shutdown. (#3110)

This commit is contained in:
omar 2020-04-09 13:34:39 +02:00
parent 832fda8488
commit fb70d90fef
3 changed files with 21 additions and 17 deletions

View File

@ -73,6 +73,8 @@ Other Changes:
to a solution rather than encourage people to add braces in the codebase. to a solution rather than encourage people to add braces in the codebase.
- Misc: Added additional checks in EndFrame() to verify that io.KeyXXX values have not been - Misc: Added additional checks in EndFrame() to verify that io.KeyXXX values have not been
tampered with between NewFrame() and EndFrame(). tampered with between NewFrame() and EndFrame().
- Misc: Made default clipboard handlers for Win32 and OSX use a buffer inside the main context
instead of a static buffer, so it can be freed properly on Shutdown. (#3110)
- Misc, Freetype: Fixed support for IMGUI_STB_RECT_PACK_FILENAME compile time directive - Misc, Freetype: Fixed support for IMGUI_STB_RECT_PACK_FILENAME compile time directive
in imgui_freetype.cpp (matching support in the regular code path). (#3062) [@DonKult] in imgui_freetype.cpp (matching support in the regular code path). (#3062) [@DonKult]
- Metrics: Made Tools section more prominent. Showing wire-frame mesh directly hovering the ImDrawCmd - Metrics: Made Tools section more prominent. Showing wire-frame mesh directly hovering the ImDrawCmd

View File

@ -3985,7 +3985,7 @@ void ImGui::Shutdown(ImGuiContext* context)
g.CurrentTabBarStack.clear(); g.CurrentTabBarStack.clear();
g.ShrinkWidthBuffer.clear(); g.ShrinkWidthBuffer.clear();
g.PrivateClipboard.clear(); g.ClipboardHandlerData.clear();
g.MenusIdSubmittedThisFrame.clear(); g.MenusIdSubmittedThisFrame.clear();
g.InputTextState.ClearFreeMemory(); g.InputTextState.ClearFreeMemory();
@ -9805,10 +9805,11 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl
#endif #endif
// Win32 clipboard implementation // Win32 clipboard implementation
// 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*)
{ {
static ImVector<char> buf_local; ImGuiContext& g = *GImGui;
buf_local.clear(); g.ClipboardHandlerData.clear();
if (!::OpenClipboard(NULL)) if (!::OpenClipboard(NULL))
return NULL; return NULL;
HANDLE wbuf_handle = ::GetClipboardData(CF_UNICODETEXT); HANDLE wbuf_handle = ::GetClipboardData(CF_UNICODETEXT);
@ -9820,12 +9821,12 @@ static const char* GetClipboardTextFn_DefaultImpl(void*)
if (const WCHAR* wbuf_global = (const WCHAR*)::GlobalLock(wbuf_handle)) if (const WCHAR* wbuf_global = (const WCHAR*)::GlobalLock(wbuf_handle))
{ {
int buf_len = ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, NULL, 0, NULL, NULL); int buf_len = ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, NULL, 0, NULL, NULL);
buf_local.resize(buf_len); g.ClipboardHandlerData.resize(buf_len);
::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, buf_local.Data, buf_len, NULL, NULL); ::WideCharToMultiByte(CP_UTF8, 0, wbuf_global, -1, g.ClipboardHandlerData.Data, buf_len, NULL, NULL);
} }
::GlobalUnlock(wbuf_handle); ::GlobalUnlock(wbuf_handle);
::CloseClipboard(); ::CloseClipboard();
return buf_local.Data; return g.ClipboardHandlerData.Data;
} }
static void SetClipboardTextFn_DefaultImpl(void*, const char* text) static void SetClipboardTextFn_DefaultImpl(void*, const char* text)
@ -9887,13 +9888,14 @@ 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)
{ {
static ImVector<char> clipboard_text; ImGuiContext& g = *GImGui;
g.ClipboardHandlerData.clear();
int length = (int)CFDataGetLength(cf_data); int length = (int)CFDataGetLength(cf_data);
clipboard_text.resize(length + 1); g.ClipboardHandlerData.resize(length + 1);
CFDataGetBytes(cf_data, CFRangeMake(0, length), (UInt8*)clipboard_text.Data); CFDataGetBytes(cf_data, CFRangeMake(0, length), (UInt8*)g.ClipboardHandlerData.Data);
clipboard_text[length] = 0; g.ClipboardHandlerData[length] = 0;
CFRelease(cf_data); CFRelease(cf_data);
return clipboard_text.Data; return g.ClipboardHandlerData.Data;
} }
} }
} }
@ -9906,17 +9908,17 @@ static const char* GetClipboardTextFn_DefaultImpl(void*)
static const char* GetClipboardTextFn_DefaultImpl(void*) static const char* GetClipboardTextFn_DefaultImpl(void*)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
return g.PrivateClipboard.empty() ? NULL : g.PrivateClipboard.begin(); return g.ClipboardHandlerData.empty() ? NULL : g.ClipboardHandlerData.begin();
} }
static void SetClipboardTextFn_DefaultImpl(void*, const char* text) static void SetClipboardTextFn_DefaultImpl(void*, const char* text)
{ {
ImGuiContext& g = *GImGui; ImGuiContext& g = *GImGui;
g.PrivateClipboard.clear(); g.ClipboardHandlerData.clear();
const char* text_end = text + strlen(text); const char* text_end = text + strlen(text);
g.PrivateClipboard.resize((int)(text_end - text) + 1); g.ClipboardHandlerData.resize((int)(text_end - text) + 1);
memcpy(&g.PrivateClipboard[0], text, (size_t)(text_end - text)); memcpy(&g.ClipboardHandlerData[0], text, (size_t)(text_end - text));
g.PrivateClipboard[(int)(text_end - text)] = 0; g.ClipboardHandlerData[(int)(text_end - text)] = 0;
} }
#endif #endif

View File

@ -1189,7 +1189,7 @@ struct ImGuiContext
float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio float DragSpeedDefaultRatio; // If speed == 0.0f, uses (max-min) * DragSpeedDefaultRatio
float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage? float ScrollbarClickDeltaToGrabCenter; // Distance between mouse and center of grab box, normalized in parent space. Use storage?
int TooltipOverrideCount; int TooltipOverrideCount;
ImVector<char> PrivateClipboard; // If no custom clipboard handler is defined ImVector<char> ClipboardHandlerData; // If no custom clipboard handler is defined
ImVector<ImGuiID> MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once ImVector<ImGuiID> MenusIdSubmittedThisFrame; // A list of menu IDs that were rendered at least once
// Platform support // Platform support