From 34f1f8d089ab1f7958c874265ff37cced54d18ed Mon Sep 17 00:00:00 2001 From: sfan5 Date: Mon, 30 Aug 2021 21:51:24 +0200 Subject: [PATCH] Make clipboard UTF-8 on Windows too --- source/Irrlicht/COSOperator.cpp | 26 +++++++++++++++++--------- source/Irrlicht/COSOperator.h | 4 ++++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/source/Irrlicht/COSOperator.cpp b/source/Irrlicht/COSOperator.cpp index 64f841f..9ff4a0e 100644 --- a/source/Irrlicht/COSOperator.cpp +++ b/source/Irrlicht/COSOperator.cpp @@ -70,16 +70,20 @@ void COSOperator::copyToClipboard(const c8 *text) const EmptyClipboard(); + core::stringw tempbuffer; + core::multibyteToWString(tempbuffer, text); + const u32 size = (tempbuffer.size() + 1) * sizeof(wchar_t); + HGLOBAL clipbuffer; - char * buffer; + void * buffer; - clipbuffer = GlobalAlloc(GMEM_DDESHARE, strlen(text)+1); - buffer = (char*)GlobalLock(clipbuffer); + clipbuffer = GlobalAlloc(GMEM_MOVEABLE, size); + buffer = GlobalLock(clipbuffer); - strcpy(buffer, text); + memcpy(buffer, tempbuffer.c_str(), size); GlobalUnlock(clipbuffer); - SetClipboardData(CF_TEXT, clipbuffer); + SetClipboardData(CF_UNICODETEXT, clipbuffer); CloseClipboard(); #elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_) @@ -113,13 +117,17 @@ const c8* COSOperator::getTextFromClipboard() const if (!OpenClipboard(NULL)) return 0; - char * buffer = 0; + wchar_t * buffer = 0; + + HANDLE hData = GetClipboardData( CF_UNICODETEXT ); + buffer = (wchar_t*) GlobalLock( hData ); + + core::wStringToMultibyte(ClipboardBuf, buffer); - HANDLE hData = GetClipboardData( CF_TEXT ); - buffer = (char*)GlobalLock( hData ); GlobalUnlock( hData ); CloseClipboard(); - return buffer; + + return ClipboardBuf.c_str(); #elif defined(_IRR_COMPILE_WITH_OSX_DEVICE_) NSString* str = nil; diff --git a/source/Irrlicht/COSOperator.h b/source/Irrlicht/COSOperator.h index 2d47695..bdf4d1d 100644 --- a/source/Irrlicht/COSOperator.h +++ b/source/Irrlicht/COSOperator.h @@ -48,6 +48,10 @@ private: CIrrDeviceLinux * IrrDeviceLinux; #endif +#ifdef _IRR_WINDOWS_API_ + mutable core::stringc ClipboardBuf; +#endif + }; } // end namespace