From 0eeb6228b9f951780aef55ab092aecb724ca876a Mon Sep 17 00:00:00 2001 From: ocornut Date: Sun, 8 Mar 2015 17:23:24 +0000 Subject: [PATCH] Default clipboard handlers for Windows handle UTF-8 (the glfw ones already did that) --- imgui.cpp | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/imgui.cpp b/imgui.cpp index 0dd8848b7..f26b7d51e 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -8251,12 +8251,16 @@ static const char* GetClipboardTextFn_DefaultImpl() } if (!OpenClipboard(NULL)) return NULL; - HANDLE buf_handle = GetClipboardData(CF_TEXT); - if (buf_handle == NULL) + HANDLE wbuf_handle = GetClipboardData(CF_UNICODETEXT); + if (wbuf_handle == NULL) return NULL; - if (char* buf_global = (char*)GlobalLock(buf_handle)) - buf_local = ImStrdup(buf_global); - GlobalUnlock(buf_handle); + if (ImWchar* wbuf_global = (ImWchar*)GlobalLock(wbuf_handle)) + { + int buf_len = ImTextCountUtf8BytesFromWchar(wbuf_global, NULL) + 1; + buf_local = (char*)ImGui::MemAlloc(buf_len * sizeof(char)); + ImTextStrToUtf8(buf_local, buf_len, wbuf_global, NULL); + } + GlobalUnlock(wbuf_handle); CloseClipboard(); return buf_local; } @@ -8266,17 +8270,16 @@ static void SetClipboardTextFn_DefaultImpl(const char* text) { if (!OpenClipboard(NULL)) return; - const char* text_end = text + strlen(text); - const int buf_length = (int)(text_end - text) + 1; - HGLOBAL buf_handle = GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)buf_length * sizeof(char)); - if (buf_handle == NULL) + + const int wbuf_length = ImTextCountCharsFromUtf8(text, NULL) + 1; + HGLOBAL wbuf_handle = GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)wbuf_length * sizeof(ImWchar)); + if (wbuf_handle == NULL) return; - char* buf_global = (char *)GlobalLock(buf_handle); - memcpy(buf_global, text, (size_t)(text_end - text)); - buf_global[text_end - text] = 0; - GlobalUnlock(buf_handle); + ImWchar* wbuf_global = (ImWchar*)GlobalLock(wbuf_handle); + ImTextStrFromUtf8(wbuf_global, wbuf_length, text, NULL); + GlobalUnlock(wbuf_handle); EmptyClipboard(); - SetClipboardData(CF_TEXT, buf_handle); + SetClipboardData(CF_UNICODETEXT, wbuf_handle); CloseClipboard(); }