diff --git a/src/xrCore/os_clipboard.cpp b/src/xrCore/os_clipboard.cpp index 92fe290ee42..c1ce03ac103 100644 --- a/src/xrCore/os_clipboard.cpp +++ b/src/xrCore/os_clipboard.cpp @@ -3,95 +3,92 @@ // Created : 21.02.2008 // Author : Evgeniy Sokolov // Description : os clipboard class implementation +// +// Modified : 24.07.2018 +// Modified by : Xottab_DUTY //////////////////////////////////////////////////////////////////////////// #include "stdafx.h" #pragma hdrstop +#include #include "os_clipboard.h" #include "xrCore/_std_extensions.h" -void os_clipboard::copy_to_clipboard(LPCSTR buf) +void os_clipboard::copy_to_clipboard(pcstr buf) { -#if defined(WINDOWS) - if (!OpenClipboard(0)) - return; - u32 handle_size = (xr_strlen(buf) + 1) * sizeof(char); - HGLOBAL handle = GlobalAlloc(GHND, handle_size); - if (!handle) + if (SDL_SetClipboardText(buf) < 0) { - CloseClipboard(); - return; + Msg("! Failed to copy text to the clipboard: %s", SDL_GetError()); + Log(buf); } - - char* memory = (char*)GlobalLock(handle); - xr_strcpy(memory, handle_size, buf); - GlobalUnlock(handle); - EmptyClipboard(); - SetClipboardData(CF_TEXT, handle); - CloseClipboard(); -#endif } -void os_clipboard::paste_from_clipboard(LPSTR buffer, u32 const& buffer_size) +void os_clipboard::paste_from_clipboard(pstr buffer, size_t buffer_size) { VERIFY(buffer); VERIFY(buffer_size > 0); -#if defined(WINDOWS) - if (!OpenClipboard(0)) + + if (!SDL_HasClipboardText()) return; - HGLOBAL hmem = GetClipboardData(CF_TEXT); - if (!hmem) + char* clipData = SDL_GetClipboardText(); + + if (!clipData) + { + Msg("! Failed to paste text from the clipboard: %s", SDL_GetError()); return; + } + + strncpy_s(buffer, buffer_size, clipData, buffer_size - 1); - LPCSTR clipdata = (LPCSTR)GlobalLock(hmem); - strncpy_s(buffer, buffer_size, clipdata, buffer_size - 1); - buffer[buffer_size - 1] = 0; - for (u32 i = 0; i < strlen(buffer); ++i) + for (size_t i = 0; i < xr_strlen(buffer); ++i) { - char c = buffer[i]; - if (((isprint(c) == 0) && (c != char(-1))) || c == '\t' || c == '\n') // "я" = -1 + const char c = buffer[i]; + if (isprint(c) == 0 && c != char(-1) || c == '\t' || c == '\n') // "я" = -1 { buffer[i] = ' '; } } - GlobalUnlock(hmem); - CloseClipboard(); -#endif + SDL_free(clipData); } -void os_clipboard::update_clipboard(LPCSTR string) +void os_clipboard::update_clipboard(pcstr string) { -#if defined(WINDOWS) - if (!OpenClipboard(0)) + if (!string) + { + Log("! Why are you trying to copy nullptr to the clipboard?!"); return; + } - HGLOBAL handle = GetClipboardData(CF_TEXT); - if (!handle) + if (!SDL_HasClipboardText()) { - CloseClipboard(); copy_to_clipboard(string); return; } - LPSTR memory = (LPSTR)GlobalLock(handle); - int memory_length = (int)strlen(memory); - int string_length = (int)strlen(string); - int buffer_size = (memory_length + string_length + 1) * sizeof(char); -#ifndef _EDITOR - LPSTR buffer = (LPSTR)_alloca(buffer_size); -#else // #ifndef _EDITOR - LPSTR buffer = (LPSTR)xr_alloc(buffer_size); -#endif // #ifndef _EDITOR - xr_strcpy(buffer, buffer_size, memory); - GlobalUnlock(handle); - - xr_strcat(buffer, buffer_size, string); - CloseClipboard(); + char* clipData = SDL_GetClipboardText(); + + if (!clipData) + { + DEBUG_BREAK; + Msg("! Failed to get text from the clipboard: %s", SDL_GetError()); + Log("! Falling back to copy_to_clipboard()"); + copy_to_clipboard(string); + return; + } + + const size_t clipLength = xr_strlen(clipData); + const size_t stringLength = xr_strlen(string); + + const size_t bufferSize = (clipLength + stringLength + 1) * sizeof(char); + + pstr buffer = (pstr)_alloca(bufferSize); + + xr_strcpy(buffer, bufferSize, clipData); // copy the clipboard + xr_strcat(buffer, bufferSize, string); // copy the new string + + SDL_free(clipData); + copy_to_clipboard(buffer); -#ifdef _EDITOR - xr_free(buffer); -#endif // #ifdef _EDITOR -#endif } diff --git a/src/xrCore/os_clipboard.h b/src/xrCore/os_clipboard.h index d97f5165650..4b6750c1b29 100644 --- a/src/xrCore/os_clipboard.h +++ b/src/xrCore/os_clipboard.h @@ -11,9 +11,9 @@ namespace os_clipboard { -XRCORE_API void copy_to_clipboard(LPCSTR buf); -XRCORE_API void paste_from_clipboard(LPSTR buf, u32 const& buf_size); -XRCORE_API void update_clipboard(LPCSTR str); +XRCORE_API void copy_to_clipboard(pcstr buf); +XRCORE_API void paste_from_clipboard(pstr buf, size_t buf_size); +XRCORE_API void update_clipboard(pcstr str); } // namespace os_clipboard #endif // OS_CLIPBOARD_H_INCLUDED