|
3 | 3 | // Created : 21.02.2008 |
4 | 4 | // Author : Evgeniy Sokolov |
5 | 5 | // Description : os clipboard class implementation |
| 6 | +// |
| 7 | +// Modified : 24.07.2018 |
| 8 | +// Modified by : Xottab_DUTY |
6 | 9 | //////////////////////////////////////////////////////////////////////////// |
7 | 10 |
|
8 | 11 | #include "stdafx.h" |
9 | 12 | #pragma hdrstop |
| 13 | +#include <SDL.h> |
10 | 14 | #include "os_clipboard.h" |
11 | 15 | #include "xrCore/_std_extensions.h" |
12 | 16 |
|
13 | | -void os_clipboard::copy_to_clipboard(LPCSTR buf) |
| 17 | +void os_clipboard::copy_to_clipboard(pcstr buf) |
14 | 18 | { |
15 | | -#if defined(WINDOWS) |
16 | | - if (!OpenClipboard(0)) |
17 | | - return; |
18 | | - u32 handle_size = (xr_strlen(buf) + 1) * sizeof(char); |
19 | | - HGLOBAL handle = GlobalAlloc(GHND, handle_size); |
20 | | - if (!handle) |
| 19 | + if (SDL_SetClipboardText(buf) < 0) |
21 | 20 | { |
22 | | - CloseClipboard(); |
23 | | - return; |
| 21 | + Msg("! Failed to copy text to the clipboard: %s", SDL_GetError()); |
| 22 | + Log(buf); |
24 | 23 | } |
25 | | - |
26 | | - char* memory = (char*)GlobalLock(handle); |
27 | | - xr_strcpy(memory, handle_size, buf); |
28 | | - GlobalUnlock(handle); |
29 | | - EmptyClipboard(); |
30 | | - SetClipboardData(CF_TEXT, handle); |
31 | | - CloseClipboard(); |
32 | | -#endif |
33 | 24 | } |
34 | 25 |
|
35 | | -void os_clipboard::paste_from_clipboard(LPSTR buffer, u32 const& buffer_size) |
| 26 | +void os_clipboard::paste_from_clipboard(pstr buffer, size_t buffer_size) |
36 | 27 | { |
37 | 28 | VERIFY(buffer); |
38 | 29 | VERIFY(buffer_size > 0); |
39 | | -#if defined(WINDOWS) |
40 | | - if (!OpenClipboard(0)) |
| 30 | + |
| 31 | + if (!SDL_HasClipboardText()) |
41 | 32 | return; |
42 | 33 |
|
43 | | - HGLOBAL hmem = GetClipboardData(CF_TEXT); |
44 | | - if (!hmem) |
| 34 | + char* clipData = SDL_GetClipboardText(); |
| 35 | + |
| 36 | + if (!clipData) |
| 37 | + { |
| 38 | + Msg("! Failed to paste text from the clipboard: %s", SDL_GetError()); |
45 | 39 | return; |
| 40 | + } |
| 41 | + |
| 42 | + strncpy_s(buffer, buffer_size, clipData, buffer_size - 1); |
46 | 43 |
|
47 | | - LPCSTR clipdata = (LPCSTR)GlobalLock(hmem); |
48 | | - strncpy_s(buffer, buffer_size, clipdata, buffer_size - 1); |
49 | | - buffer[buffer_size - 1] = 0; |
50 | | - for (u32 i = 0; i < strlen(buffer); ++i) |
| 44 | + for (size_t i = 0; i < xr_strlen(buffer); ++i) |
51 | 45 | { |
52 | | - char c = buffer[i]; |
53 | | - if (((isprint(c) == 0) && (c != char(-1))) || c == '\t' || c == '\n') // "я" = -1 |
| 46 | + const char c = buffer[i]; |
| 47 | + if (isprint(c) == 0 && c != char(-1) || c == '\t' || c == '\n') // "я" = -1 |
54 | 48 | { |
55 | 49 | buffer[i] = ' '; |
56 | 50 | } |
57 | 51 | } |
58 | 52 |
|
59 | | - GlobalUnlock(hmem); |
60 | | - CloseClipboard(); |
61 | | -#endif |
| 53 | + SDL_free(clipData); |
62 | 54 | } |
63 | 55 |
|
64 | | -void os_clipboard::update_clipboard(LPCSTR string) |
| 56 | +void os_clipboard::update_clipboard(pcstr string) |
65 | 57 | { |
66 | | -#if defined(WINDOWS) |
67 | | - if (!OpenClipboard(0)) |
| 58 | + if (!string) |
| 59 | + { |
| 60 | + Log("! Why are you trying to copy nullptr to the clipboard?!"); |
68 | 61 | return; |
| 62 | + } |
69 | 63 |
|
70 | | - HGLOBAL handle = GetClipboardData(CF_TEXT); |
71 | | - if (!handle) |
| 64 | + if (!SDL_HasClipboardText()) |
72 | 65 | { |
73 | | - CloseClipboard(); |
74 | 66 | copy_to_clipboard(string); |
75 | 67 | return; |
76 | 68 | } |
77 | 69 |
|
78 | | - LPSTR memory = (LPSTR)GlobalLock(handle); |
79 | | - int memory_length = (int)strlen(memory); |
80 | | - int string_length = (int)strlen(string); |
81 | | - int buffer_size = (memory_length + string_length + 1) * sizeof(char); |
82 | | -#ifndef _EDITOR |
83 | | - LPSTR buffer = (LPSTR)_alloca(buffer_size); |
84 | | -#else // #ifndef _EDITOR |
85 | | - LPSTR buffer = (LPSTR)xr_alloc<char>(buffer_size); |
86 | | -#endif // #ifndef _EDITOR |
87 | | - xr_strcpy(buffer, buffer_size, memory); |
88 | | - GlobalUnlock(handle); |
89 | | - |
90 | | - xr_strcat(buffer, buffer_size, string); |
91 | | - CloseClipboard(); |
| 70 | + char* clipData = SDL_GetClipboardText(); |
| 71 | + |
| 72 | + if (!clipData) |
| 73 | + { |
| 74 | + DEBUG_BREAK; |
| 75 | + Msg("! Failed to get text from the clipboard: %s", SDL_GetError()); |
| 76 | + Log("! Falling back to copy_to_clipboard()"); |
| 77 | + copy_to_clipboard(string); |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + const size_t clipLength = xr_strlen(clipData); |
| 82 | + const size_t stringLength = xr_strlen(string); |
| 83 | + |
| 84 | + const size_t bufferSize = (clipLength + stringLength + 1) * sizeof(char); |
| 85 | + |
| 86 | + pstr buffer = (pstr)_alloca(bufferSize); |
| 87 | + |
| 88 | + xr_strcpy(buffer, bufferSize, clipData); // copy the clipboard |
| 89 | + xr_strcat(buffer, bufferSize, string); // copy the new string |
| 90 | + |
| 91 | + SDL_free(clipData); |
| 92 | + |
92 | 93 | copy_to_clipboard(buffer); |
93 | | -#ifdef _EDITOR |
94 | | - xr_free(buffer); |
95 | | -#endif // #ifdef _EDITOR |
96 | | -#endif |
97 | 94 | } |
0 commit comments