cgv
Loading...
Searching...
No Matches
clipboard.cxx
1#include "clipboard.h"
2
3#include <iostream>
4
5#ifdef WIN32
6#include <windows.h>
7#endif
8
9namespace cgv {
10 namespace os {
11
12#ifdef WIN32
13bool copy_text_to_clipboard(const char* clipboard_text)
14{
15 if ( !OpenClipboard(NULL) )
16 {
17 std::cout << "Cannot open the Clipboard" << std::endl;
18 return false;
19 }
20 // Remove the current Clipboard contents
21 if( !EmptyClipboard() )
22 {
23 std::cout << "Cannot empty the Clipboard" << std::endl;
24 return false;
25 }
26 // ...
27 // Get the currently selected data
28 // ...
29 // For the appropriate data formats...
30 HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE,
31 (strlen(clipboard_text)+ 1) * sizeof(TCHAR));
32 if (hglbCopy == NULL)
33 {
34 CloseClipboard();
35 return false;
36 }
37
38 // Lock the handle and copy the text to the buffer.
39
40 LPTSTR lptstrCopy = (LPTSTR)GlobalLock(hglbCopy);
41 memcpy(lptstrCopy, clipboard_text,
42 strlen(clipboard_text) * sizeof(TCHAR));
43 lptstrCopy[strlen(clipboard_text)] = (TCHAR) 0; // null character
44 GlobalUnlock(hglbCopy);
45
46
47 if ( ::SetClipboardData( CF_TEXT, hglbCopy) == NULL )
48 {
49 std::cout << "Unable to set Clipboard data" << std::endl;
50 CloseClipboard();
51 return false;
52 }
53 // ...
54 CloseClipboard();
55 return true;
56}
57bool copy_rgb_image_to_clipboard(int w, int h, const unsigned char* image_buffer)
58{
59 if ( !OpenClipboard(NULL) )
60 {
61 std::cout << "Cannot open the Clipboard" << std::endl;
62 return false;
63 }
64 // Remove the current Clipboard contents
65 if( !EmptyClipboard() )
66 {
67 std::cout << "Cannot empty the Clipboard" << std::endl;
68 return false;
69 }
70 // ...
71 // Get the currently selected data
72 // ...
73 // For the appropriate data formats...
74 unsigned int src_line_length = w*3;
75 unsigned int dst_line_length = src_line_length;
76 if ((dst_line_length&3) != 0)
77 dst_line_length = (dst_line_length/4+1)*4;
78 unsigned int size = sizeof(BITMAPINFO)+dst_line_length*h;
79 HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, size);
80 if (hglbCopy == NULL)
81 {
82 CloseClipboard();
83 return false;
84 }
85
86 // Lock the handle and copy the text to the buffer.
87 PBITMAPINFO pbmi = (PBITMAPINFO)GlobalLock(hglbCopy);
88 PBITMAPINFOHEADER pbhi = &pbmi->bmiHeader;
89 pbhi->biSize = sizeof(BITMAPINFOHEADER);
90 pbhi->biWidth = w;
91 pbhi->biHeight = h;
92 pbhi->biPlanes = 3;
93 pbhi->biBitCount = 24;
94 pbhi->biCompression = BI_RGB;
95 pbhi->biSizeImage = 0;
96 pbhi->biXPelsPerMeter = 1000000;
97 pbhi->biYPelsPerMeter = 1000000;
98 pbhi->biClrUsed = 0;
99 pbhi->biClrImportant = 0;
100 unsigned char* dst = (unsigned char*)&pbmi->bmiColors[0];
101 const unsigned char* src = image_buffer;
102 for (int i=0; i<h; ++i) {
103 unsigned char* dst_1 = dst;
104 const unsigned char* src_1 = src;
105 for (int j = 0; j<w; ++j) {
106 *dst_1++ = src_1[2];
107 *dst_1++ = src_1[1];
108 *dst_1++ = src_1[0];
109 src_1 += 3;
110 }
111 dst += dst_line_length;
112 src += src_line_length;
113 }
114 GlobalUnlock(hglbCopy);
115
116 if ( ::SetClipboardData( CF_DIB, hglbCopy) == NULL )
117 {
118 std::cout << "Unable to set Clipboard data" << std::endl;
119 CloseClipboard();
120 return false;
121 }
122 // ...
123 CloseClipboard();
124 return true;
125}
126bool get_text_from_clipboard(std::string& text, bool clear_clipboard)
127{
128 if (!IsClipboardFormatAvailable(CF_TEXT))
129 return false;
130 bool result = false;
131 if (OpenClipboard(nullptr)) {
132 // Get handle of clipboard object for ANSI text
133 HANDLE hData = GetClipboardData(CF_TEXT);
134 if (hData != nullptr) {
135 // Lock the handle to get the actual text pointer
136 char* pszText = static_cast<char*>(GlobalLock(hData));
137 if (pszText != nullptr) {
138 // Save text in a string class instance
139 text = std::string(pszText);
140 result = true;
141 }
142 // Release the lock
143 GlobalUnlock(hData);
144 }
145 // if requested, clear the clipboard
146 if (clear_clipboard)
147 EmptyClipboard();
148 // Release the clipboard
149 CloseClipboard();
150 }
151 return result;
152}
153bool get_rgb_image_from_clipboard(int& w, int& h, std::vector<char>& data, bool clear_clipboard)
154{
155 if (!IsClipboardFormatAvailable(CF_BITMAP))
156 return false;
157 bool result = false;
158 // Try opening the clipboard
159 if (OpenClipboard(nullptr)) {
160 // Get handle of clipboard object for ANSI text
161 HANDLE hData = GetClipboardData(CF_BITMAP);
162 if (hData != nullptr) {
163 HBITMAP hBitmap = (HBITMAP)hData;
164 BITMAP bmp;
165 // Retrieve the bitmap color format, width, and height.
166 if (GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bmp)) {
167 BITMAPINFO bmi;
168 std::fill((uint8_t*)&bmi, (uint8_t*)(&bmi + 1), 0);
169 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
170 w = bmi.bmiHeader.biWidth = bmp.bmWidth;
171 h = bmi.bmiHeader.biHeight = bmp.bmHeight;
172 bmi.bmiHeader.biPlanes = 1;
173 bmi.bmiHeader.biBitCount = 24;
174 bmi.bmiHeader.biCompression = BI_RGB;
175 data.resize(3 * bmp.bmHeight * bmp.bmWidth);
176 if (GetDIBits(GetDC(0), hBitmap, 0, (WORD)bmp.bmHeight, data.data(), &bmi, DIB_RGB_COLORS)) {
177 result = true;
178 }
179 }
180 }
181 // if requested, clear the clipboard
182 if (clear_clipboard)
183 EmptyClipboard();
184 // Release the clipboard
185 CloseClipboard();
186 }
187 return true;
188}
189
190#else
191
192bool copy_text_to_clipboard(const char* text) {
193 // FIXME implement this (for now this is a no-op on non-windows systems)
194 return false;
195}
196
197bool copy_rgb_image_to_clipboard(int w, int h, const unsigned char* image_buffer) {
198 // FIXME implement this (for now this is a no-op on non-windows systems)
199 return false;
200}
201bool get_text_from_clipboard(std::string& text, bool clear_clipboard)
202{
203 // FIXME implement this (for now this is a no-op on non-windows systems)
204 return false;
205}
206
208bool get_rgb_image_from_clipboard(int& w, int& h, std::vector<char>& data, bool clear_clipboard)
209{
210 // FIXME implement this (for now this is a no-op on non-windows systems)
211 return false;
212}
213
214#endif
215
216 }
217}
the cgv namespace
Definition print.h:11