-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipboard.cpp
More file actions
127 lines (109 loc) · 3.16 KB
/
Copy pathclipboard.cpp
File metadata and controls
127 lines (109 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "clipboard.hpp"
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#endif
namespace droidcli::cli {
#ifdef _WIN32
ClipboardWriteResult write_text_to_clipboard(const core::String& text)
{
ClipboardWriteResult result;
if (!OpenClipboard(nullptr))
{
result.error_message = "could not open the clipboard (another process may be holding it)";
return result;
}
// CF_UNICODETEXT (not CF_TEXT) so non-ASCII content - emoji, non-English
// text - survives the copy intact.
const int wide_length = MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, nullptr, 0);
if (wide_length <= 0)
{
CloseClipboard();
result.error_message = "failed to convert text to UTF-16 for the clipboard";
return result;
}
const HGLOBAL buffer_handle = GlobalAlloc(GMEM_MOVEABLE, static_cast<SIZE_T>(wide_length) * sizeof(wchar_t));
if (buffer_handle == nullptr)
{
CloseClipboard();
result.error_message = "GlobalAlloc failed";
return result;
}
wchar_t* buffer = static_cast<wchar_t*>(GlobalLock(buffer_handle));
if (buffer == nullptr)
{
GlobalFree(buffer_handle);
CloseClipboard();
result.error_message = "GlobalLock failed";
return result;
}
MultiByteToWideChar(CP_UTF8, 0, text.c_str(), -1, buffer, wide_length);
GlobalUnlock(buffer_handle);
EmptyClipboard();
// The clipboard owns buffer_handle after a successful SetClipboardData -
// do not GlobalFree it ourselves.
result.ok = SetClipboardData(CF_UNICODETEXT, buffer_handle) != nullptr;
if (!result.ok)
{
GlobalFree(buffer_handle);
result.error_message = "SetClipboardData failed";
}
CloseClipboard();
return result;
}
ClipboardReadResult read_text_from_clipboard()
{
ClipboardReadResult result;
if (!IsClipboardFormatAvailable(CF_UNICODETEXT))
{
result.error_message = "clipboard does not currently hold text";
return result;
}
if (!OpenClipboard(nullptr))
{
result.error_message = "could not open the clipboard (another process may be holding it)";
return result;
}
const HANDLE data_handle = GetClipboardData(CF_UNICODETEXT);
if (data_handle == nullptr)
{
CloseClipboard();
result.error_message = "GetClipboardData failed";
return result;
}
const wchar_t* wide_text = static_cast<const wchar_t*>(GlobalLock(data_handle));
if (wide_text == nullptr)
{
CloseClipboard();
result.error_message = "GlobalLock failed";
return result;
}
const int utf8_length = WideCharToMultiByte(CP_UTF8, 0, wide_text, -1, nullptr, 0, nullptr, nullptr);
if (utf8_length > 1)
{
result.text.resize(static_cast<size_t>(utf8_length) - 1);
WideCharToMultiByte(CP_UTF8, 0, wide_text, -1, result.text.data(), utf8_length, nullptr, nullptr);
result.ok = true;
}
else
{
result.error_message = "clipboard text was empty";
}
GlobalUnlock(data_handle);
CloseClipboard();
return result;
}
#else
ClipboardWriteResult write_text_to_clipboard(const core::String& text)
{
(void)text;
return ClipboardWriteResult{false, "clipboard access is not implemented on this platform"};
}
ClipboardReadResult read_text_from_clipboard()
{
return ClipboardReadResult{false, {}, "clipboard access is not implemented on this platform"};
}
#endif
} // namespace droidcli::cli