]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: clipbrd.cpp | |
3 | // Purpose: Clipboard functionality | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 04/01/98 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
26f86486 VZ |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
2bda0e17 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
26f86486 | 24 | #pragma hdrstop |
2bda0e17 KB |
25 | #endif |
26 | ||
47d67540 | 27 | #if wxUSE_CLIPBOARD |
2bda0e17 KB |
28 | |
29 | #ifndef WX_PRECOMP | |
26f86486 VZ |
30 | #include "wx/object.h" |
31 | #include "wx/event.h" | |
32 | #include "wx/app.h" | |
33 | #include "wx/frame.h" | |
34 | #include "wx/bitmap.h" | |
35 | #include "wx/utils.h" | |
0c589ad0 | 36 | #include "wx/intl.h" |
2bda0e17 KB |
37 | #endif |
38 | ||
06e43511 | 39 | #if wxUSE_METAFILE |
26f86486 | 40 | #include "wx/metafile.h" |
06e43511 JS |
41 | #endif |
42 | ||
dbda9e86 | 43 | #include "wx/log.h" |
2bda0e17 | 44 | #include "wx/clipbrd.h" |
06e43511 | 45 | |
3f480da3 | 46 | #include <string.h> |
06e43511 | 47 | |
2bda0e17 | 48 | #include "wx/msw/private.h" |
794fe8cd | 49 | #include "wx/msw/ole/oleutils.h" |
8cb172b4 | 50 | |
4676948b | 51 | #if wxUSE_WXDIB |
794fe8cd | 52 | #include "wx/msw/dib.h" |
8cb172b4 | 53 | #endif |
2bda0e17 | 54 | |
d59ceba5 VZ |
55 | // wxDataObject is tied to OLE/drag and drop implementation, therefore so are |
56 | // the functions using wxDataObject in wxClipboard | |
1e6feb95 | 57 | //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP |
d59ceba5 VZ |
58 | |
59 | #if wxUSE_DATAOBJ | |
26f86486 | 60 | #include "wx/dataobj.h" |
bd52bee1 | 61 | #endif |
3f480da3 | 62 | |
f07dc2e2 | 63 | #if wxUSE_OLE && !defined(__WXWINCE__) |
b94e73ae VZ |
64 | // use OLE clipboard |
65 | #define wxUSE_OLE_CLIPBOARD 1 | |
d59ceba5 VZ |
66 | #else // !wxUSE_DATAOBJ |
67 | // use Win clipboard API | |
68 | #define wxUSE_OLE_CLIPBOARD 0 | |
2bda0e17 KB |
69 | #endif |
70 | ||
d59ceba5 VZ |
71 | #if wxUSE_OLE_CLIPBOARD |
72 | #include <ole2.h> | |
73 | #endif // wxUSE_OLE_CLIPBOARD | |
74 | ||
26f86486 VZ |
75 | // =========================================================================== |
76 | // implementation | |
77 | // =========================================================================== | |
78 | ||
79 | // --------------------------------------------------------------------------- | |
80 | // old-style clipboard functions using Windows API | |
81 | // --------------------------------------------------------------------------- | |
2bda0e17 | 82 | |
02b7b6b0 | 83 | static bool gs_wxClipboardIsOpen = false; |
26f86486 VZ |
84 | |
85 | bool wxOpenClipboard() | |
2bda0e17 | 86 | { |
02b7b6b0 | 87 | wxCHECK_MSG( !gs_wxClipboardIsOpen, true, wxT("clipboard already opened.") ); |
26f86486 VZ |
88 | |
89 | wxWindow *win = wxTheApp->GetTopWindow(); | |
90 | if ( win ) | |
91 | { | |
92 | gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0; | |
93 | ||
94 | if ( !gs_wxClipboardIsOpen ) | |
95 | wxLogSysError(_("Failed to open the clipboard.")); | |
96 | ||
97 | return gs_wxClipboardIsOpen; | |
98 | } | |
99 | else | |
100 | { | |
223d09f6 | 101 | wxLogDebug(wxT("Can not open clipboard without a main window.")); |
26f86486 | 102 | |
02b7b6b0 | 103 | return false; |
26f86486 | 104 | } |
2bda0e17 KB |
105 | } |
106 | ||
26f86486 | 107 | bool wxCloseClipboard() |
2bda0e17 | 108 | { |
02b7b6b0 | 109 | wxCHECK_MSG( gs_wxClipboardIsOpen, false, wxT("clipboard is not opened") ); |
26f86486 | 110 | |
02b7b6b0 | 111 | gs_wxClipboardIsOpen = false; |
26f86486 VZ |
112 | |
113 | if ( ::CloseClipboard() == 0 ) | |
114 | { | |
115 | wxLogSysError(_("Failed to close the clipboard.")); | |
116 | ||
02b7b6b0 | 117 | return false; |
26f86486 VZ |
118 | } |
119 | ||
02b7b6b0 | 120 | return true; |
2bda0e17 KB |
121 | } |
122 | ||
26f86486 | 123 | bool wxEmptyClipboard() |
2bda0e17 | 124 | { |
26f86486 VZ |
125 | if ( ::EmptyClipboard() == 0 ) |
126 | { | |
127 | wxLogSysError(_("Failed to empty the clipboard.")); | |
128 | ||
02b7b6b0 | 129 | return false; |
26f86486 VZ |
130 | } |
131 | ||
02b7b6b0 | 132 | return true; |
2bda0e17 KB |
133 | } |
134 | ||
26f86486 | 135 | bool wxIsClipboardOpened() |
2bda0e17 | 136 | { |
26f86486 | 137 | return gs_wxClipboardIsOpen; |
2bda0e17 KB |
138 | } |
139 | ||
06e43511 | 140 | bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat) |
2bda0e17 | 141 | { |
8cc4850c | 142 | wxDataFormat::NativeFormat cf = dataFormat.GetFormatId(); |
6c29712c JS |
143 | |
144 | if ( ::IsClipboardFormatAvailable(cf) ) | |
d9317fd4 VZ |
145 | { |
146 | // ok from the first try | |
02b7b6b0 | 147 | return true; |
d9317fd4 VZ |
148 | } |
149 | ||
150 | // for several standard formats, we can convert from some other ones too | |
6c29712c | 151 | switch ( cf ) |
d9317fd4 VZ |
152 | { |
153 | // for bitmaps, DIBs will also do | |
154 | case CF_BITMAP: | |
155 | return ::IsClipboardFormatAvailable(CF_DIB) != 0; | |
156 | ||
3a5bcc4d | 157 | #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__) |
d9317fd4 VZ |
158 | case CF_METAFILEPICT: |
159 | return ::IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0; | |
160 | #endif // wxUSE_ENH_METAFILE | |
161 | ||
162 | default: | |
02b7b6b0 | 163 | return false; |
d9317fd4 | 164 | } |
2bda0e17 KB |
165 | } |
166 | ||
e9196d9c | 167 | |
26f86486 VZ |
168 | bool wxSetClipboardData(wxDataFormat dataFormat, |
169 | const void *data, | |
170 | int width, int height) | |
2bda0e17 | 171 | { |
26f86486 VZ |
172 | HANDLE handle = 0; // return value of SetClipboardData |
173 | ||
174 | switch (dataFormat) | |
2bda0e17 | 175 | { |
26f86486 VZ |
176 | case wxDF_BITMAP: |
177 | { | |
178 | wxBitmap *bitmap = (wxBitmap *)data; | |
179 | ||
180 | HDC hdcMem = CreateCompatibleDC((HDC) NULL); | |
181 | HDC hdcSrc = CreateCompatibleDC((HDC) NULL); | |
182 | HBITMAP old = (HBITMAP) | |
183 | ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP()); | |
184 | HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc, | |
185 | bitmap->GetWidth(), | |
186 | bitmap->GetHeight()); | |
187 | if (!hBitmap) | |
188 | { | |
189 | SelectObject(hdcSrc, old); | |
190 | DeleteDC(hdcMem); | |
191 | DeleteDC(hdcSrc); | |
02b7b6b0 | 192 | return false; |
26f86486 VZ |
193 | } |
194 | ||
195 | HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap); | |
196 | BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(), | |
197 | hdcSrc, 0, 0, SRCCOPY); | |
198 | ||
199 | // Select new bitmap out of memory DC | |
200 | SelectObject(hdcMem, old1); | |
201 | ||
202 | // Set the data | |
203 | handle = ::SetClipboardData(CF_BITMAP, hBitmap); | |
204 | ||
205 | // Clean up | |
206 | SelectObject(hdcSrc, old); | |
207 | DeleteDC(hdcSrc); | |
208 | DeleteDC(hdcMem); | |
209 | break; | |
210 | } | |
211 | ||
4676948b | 212 | #if wxUSE_WXDIB |
26f86486 VZ |
213 | case wxDF_DIB: |
214 | { | |
26f86486 | 215 | wxBitmap *bitmap = (wxBitmap *)data; |
2b254edf | 216 | |
c2517b52 | 217 | if ( bitmap && bitmap->Ok() ) |
2b254edf | 218 | { |
c2517b52 VZ |
219 | wxDIB dib(*bitmap); |
220 | if ( dib.IsOk() ) | |
221 | { | |
222 | handle = ::SetClipboardData(CF_DIB, dib.Detach()); | |
223 | } | |
2b254edf | 224 | } |
26f86486 VZ |
225 | break; |
226 | } | |
4676948b | 227 | #endif |
26f86486 | 228 | |
d9317fd4 | 229 | // VZ: I'm told that this code works, but it doesn't seem to work for me |
4676948b | 230 | // and, anyhow, I'd be highly surprised if it did. So I leave it here |
d9317fd4 | 231 | // but IMNSHO it is completely broken. |
4676948b | 232 | #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__) |
26f86486 VZ |
233 | case wxDF_METAFILE: |
234 | { | |
235 | wxMetafile *wxMF = (wxMetafile *)data; | |
236 | HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1); | |
26f86486 | 237 | METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data); |
2bda0e17 | 238 | |
26f86486 VZ |
239 | mf->mm = wxMF->GetWindowsMappingMode(); |
240 | mf->xExt = width; | |
241 | mf->yExt = height; | |
242 | mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE(); | |
243 | GlobalUnlock(data); | |
244 | wxMF->SetHMETAFILE((WXHANDLE) NULL); | |
2bda0e17 | 245 | |
26f86486 VZ |
246 | handle = SetClipboardData(CF_METAFILEPICT, data); |
247 | break; | |
248 | } | |
d9317fd4 VZ |
249 | #endif // wxUSE_METAFILE |
250 | ||
3a5bcc4d | 251 | #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__) |
d9317fd4 VZ |
252 | case wxDF_ENHMETAFILE: |
253 | { | |
254 | wxEnhMetaFile *emf = (wxEnhMetaFile *)data; | |
255 | wxEnhMetaFile emfCopy = *emf; | |
256 | ||
257 | handle = SetClipboardData(CF_ENHMETAFILE, | |
258 | (void *)emfCopy.GetHENHMETAFILE()); | |
259 | } | |
260 | break; | |
261 | #endif // wxUSE_ENH_METAFILE | |
262 | ||
26f86486 VZ |
263 | case CF_SYLK: |
264 | case CF_DIF: | |
265 | case CF_TIFF: | |
266 | case CF_PALETTE: | |
267 | default: | |
268 | { | |
269 | wxLogError(_("Unsupported clipboard format.")); | |
02b7b6b0 | 270 | return false; |
26f86486 | 271 | } |
2bda0e17 | 272 | |
26f86486 VZ |
273 | case wxDF_OEMTEXT: |
274 | dataFormat = wxDF_TEXT; | |
275 | // fall through | |
2bda0e17 | 276 | |
26f86486 VZ |
277 | case wxDF_TEXT: |
278 | { | |
279 | char *s = (char *)data; | |
280 | ||
281 | width = strlen(s) + 1; | |
282 | height = 1; | |
283 | DWORD l = (width * height); | |
284 | HANDLE hGlobalMemory = GlobalAlloc(GHND, l); | |
285 | if ( hGlobalMemory ) | |
286 | { | |
26f86486 | 287 | LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory); |
2bda0e17 | 288 | |
26f86486 | 289 | memcpy(lpGlobalMemory, s, l); |
2bda0e17 | 290 | |
26f86486 VZ |
291 | GlobalUnlock(hGlobalMemory); |
292 | } | |
2bda0e17 | 293 | |
26f86486 VZ |
294 | handle = SetClipboardData(dataFormat, hGlobalMemory); |
295 | break; | |
296 | } | |
387ebd3e JS |
297 | // Only tested with non-Unicode, Visual C++ 6.0 so far |
298 | #if defined(__VISUALC__) && !defined(UNICODE) | |
299 | case wxDF_HTML: | |
300 | { | |
301 | char* html = (char *)data; | |
02b7b6b0 | 302 | |
387ebd3e JS |
303 | // Create temporary buffer for HTML header... |
304 | char *buf = new char [400 + strlen(html)]; | |
02b7b6b0 WS |
305 | if(!buf) return false; |
306 | ||
387ebd3e JS |
307 | // Get clipboard id for HTML format... |
308 | static int cfid = 0; | |
309 | if(!cfid) cfid = RegisterClipboardFormat(wxT("HTML Format")); | |
02b7b6b0 | 310 | |
387ebd3e JS |
311 | // Create a template string for the HTML header... |
312 | strcpy(buf, | |
313 | "Version:0.9\r\n" | |
314 | "StartHTML:00000000\r\n" | |
315 | "EndHTML:00000000\r\n" | |
316 | "StartFragment:00000000\r\n" | |
317 | "EndFragment:00000000\r\n" | |
318 | "<html><body>\r\n" | |
319 | "<!--StartFragment -->\r\n"); | |
02b7b6b0 | 320 | |
387ebd3e JS |
321 | // Append the HTML... |
322 | strcat(buf, html); | |
323 | strcat(buf, "\r\n"); | |
324 | // Finish up the HTML format... | |
325 | strcat(buf, | |
326 | "<!--EndFragment-->\r\n" | |
327 | "</body>\r\n" | |
328 | "</html>"); | |
02b7b6b0 | 329 | |
387ebd3e JS |
330 | // Now go back, calculate all the lengths, and write out the |
331 | // necessary header information. Note, wsprintf() truncates the | |
332 | // string when you overwrite it so you follow up with code to replace | |
333 | // the 0 appended at the end with a '\r'... | |
334 | char *ptr = strstr(buf, "StartHTML"); | |
335 | wsprintf(ptr+10, "%08u", strstr(buf, "<html>") - buf); | |
336 | *(ptr+10+8) = '\r'; | |
02b7b6b0 | 337 | |
387ebd3e JS |
338 | ptr = strstr(buf, "EndHTML"); |
339 | wsprintf(ptr+8, "%08u", strlen(buf)); | |
340 | *(ptr+8+8) = '\r'; | |
02b7b6b0 | 341 | |
387ebd3e JS |
342 | ptr = strstr(buf, "StartFragment"); |
343 | wsprintf(ptr+14, "%08u", strstr(buf, "<!--StartFrag") - buf); | |
344 | *(ptr+14+8) = '\r'; | |
02b7b6b0 | 345 | |
387ebd3e JS |
346 | ptr = strstr(buf, "EndFragment"); |
347 | wsprintf(ptr+12, "%08u", strstr(buf, "<!--EndFrag") - buf); | |
348 | *(ptr+12+8) = '\r'; | |
02b7b6b0 | 349 | |
387ebd3e JS |
350 | // Now you have everything in place ready to put on the |
351 | // clipboard. | |
02b7b6b0 | 352 | |
387ebd3e JS |
353 | // Allocate global memory for transfer... |
354 | HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(buf)+4); | |
02b7b6b0 | 355 | |
387ebd3e JS |
356 | // Put your string in the global memory... |
357 | ptr = (char *)GlobalLock(hText); | |
358 | strcpy(ptr, buf); | |
359 | GlobalUnlock(hText); | |
02b7b6b0 | 360 | |
387ebd3e | 361 | handle = ::SetClipboardData(cfid, hText); |
02b7b6b0 | 362 | |
387ebd3e JS |
363 | // Free memory... |
364 | GlobalFree(hText); | |
02b7b6b0 | 365 | |
387ebd3e JS |
366 | // Clean up... |
367 | delete [] buf; | |
368 | break; | |
369 | } | |
370 | #endif | |
2bda0e17 | 371 | } |
26f86486 VZ |
372 | |
373 | if ( handle == 0 ) | |
2bda0e17 | 374 | { |
26f86486 VZ |
375 | wxLogSysError(_("Failed to set clipboard data.")); |
376 | ||
02b7b6b0 | 377 | return false; |
2bda0e17 | 378 | } |
26f86486 | 379 | |
02b7b6b0 | 380 | return true; |
26f86486 VZ |
381 | } |
382 | ||
383 | void *wxGetClipboardData(wxDataFormat dataFormat, long *len) | |
384 | { | |
385 | void *retval = NULL; | |
386 | ||
387 | switch ( dataFormat ) | |
2bda0e17 | 388 | { |
4676948b | 389 | #ifndef __WXWINCE__ |
26f86486 VZ |
390 | case wxDF_BITMAP: |
391 | { | |
392 | BITMAP bm; | |
393 | HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP); | |
394 | if (!hBitmap) | |
395 | break; | |
396 | ||
397 | HDC hdcMem = CreateCompatibleDC((HDC) NULL); | |
398 | HDC hdcSrc = CreateCompatibleDC((HDC) NULL); | |
399 | ||
400 | HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap); | |
401 | GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm); | |
402 | ||
403 | HBITMAP hNewBitmap = CreateBitmapIndirect(&bm); | |
404 | ||
405 | if (!hNewBitmap) | |
406 | { | |
407 | SelectObject(hdcSrc, old); | |
408 | DeleteDC(hdcMem); | |
409 | DeleteDC(hdcSrc); | |
410 | break; | |
411 | } | |
412 | ||
413 | HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap); | |
414 | BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, | |
415 | hdcSrc, 0, 0, SRCCOPY); | |
416 | ||
417 | // Select new bitmap out of memory DC | |
418 | SelectObject(hdcMem, old1); | |
419 | ||
420 | // Clean up | |
421 | SelectObject(hdcSrc, old); | |
422 | DeleteDC(hdcSrc); | |
423 | DeleteDC(hdcMem); | |
424 | ||
425 | // Create and return a new wxBitmap | |
426 | wxBitmap *wxBM = new wxBitmap; | |
427 | wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap); | |
428 | wxBM->SetWidth(bm.bmWidth); | |
429 | wxBM->SetHeight(bm.bmHeight); | |
430 | wxBM->SetDepth(bm.bmPlanes); | |
26f86486 VZ |
431 | retval = wxBM; |
432 | break; | |
433 | } | |
4676948b | 434 | #endif |
26f86486 VZ |
435 | case wxDF_METAFILE: |
436 | case CF_SYLK: | |
437 | case CF_DIF: | |
438 | case CF_TIFF: | |
439 | case CF_PALETTE: | |
440 | case wxDF_DIB: | |
f7f50f49 VZ |
441 | wxLogError(_("Unsupported clipboard format.")); |
442 | return NULL; | |
26f86486 VZ |
443 | |
444 | case wxDF_OEMTEXT: | |
445 | dataFormat = wxDF_TEXT; | |
446 | // fall through | |
447 | ||
448 | case wxDF_TEXT: | |
449 | { | |
450 | HANDLE hGlobalMemory = ::GetClipboardData(dataFormat); | |
451 | if (!hGlobalMemory) | |
452 | break; | |
2bda0e17 | 453 | |
26f86486 VZ |
454 | DWORD hsize = ::GlobalSize(hGlobalMemory); |
455 | if (len) | |
456 | *len = hsize; | |
2bda0e17 | 457 | |
26f86486 VZ |
458 | char *s = new char[hsize]; |
459 | if (!s) | |
460 | break; | |
2bda0e17 | 461 | |
4676948b | 462 | LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory); |
2bda0e17 | 463 | |
26f86486 | 464 | memcpy(s, lpGlobalMemory, hsize); |
2bda0e17 | 465 | |
4676948b | 466 | GlobalUnlock(hGlobalMemory); |
26f86486 VZ |
467 | |
468 | retval = s; | |
469 | break; | |
470 | } | |
3f480da3 VZ |
471 | |
472 | default: | |
473 | { | |
474 | HANDLE hGlobalMemory = ::GetClipboardData(dataFormat); | |
475 | if ( !hGlobalMemory ) | |
476 | break; | |
477 | ||
478 | DWORD size = ::GlobalSize(hGlobalMemory); | |
479 | if ( len ) | |
480 | *len = size; | |
481 | ||
482 | void *buf = malloc(size); | |
483 | if ( !buf ) | |
484 | break; | |
485 | ||
4676948b | 486 | LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory); |
3f480da3 VZ |
487 | |
488 | memcpy(buf, lpGlobalMemory, size); | |
489 | ||
4676948b | 490 | GlobalUnlock(hGlobalMemory); |
3f480da3 VZ |
491 | |
492 | retval = buf; | |
493 | break; | |
494 | } | |
26f86486 | 495 | } |
2bda0e17 | 496 | |
26f86486 VZ |
497 | if ( !retval ) |
498 | { | |
499 | wxLogSysError(_("Failed to retrieve data from the clipboard.")); | |
2bda0e17 | 500 | } |
26f86486 VZ |
501 | |
502 | return retval; | |
2bda0e17 KB |
503 | } |
504 | ||
3f480da3 | 505 | wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat) |
2bda0e17 | 506 | { |
33ac7e6f | 507 | return (wxDataFormat::NativeFormat)::EnumClipboardFormats(dataFormat); |
2bda0e17 KB |
508 | } |
509 | ||
837e5743 | 510 | int wxRegisterClipboardFormat(wxChar *formatName) |
2bda0e17 KB |
511 | { |
512 | return ::RegisterClipboardFormat(formatName); | |
513 | } | |
514 | ||
26f86486 | 515 | bool wxGetClipboardFormatName(wxDataFormat dataFormat, |
837e5743 | 516 | wxChar *formatName, |
26f86486 | 517 | int maxCount) |
2bda0e17 | 518 | { |
26f86486 | 519 | return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0; |
2bda0e17 KB |
520 | } |
521 | ||
26f86486 | 522 | // --------------------------------------------------------------------------- |
06e43511 | 523 | // wxClipboard |
26f86486 | 524 | // --------------------------------------------------------------------------- |
2bda0e17 | 525 | |
26f86486 | 526 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject) |
2bda0e17 KB |
527 | |
528 | wxClipboard::wxClipboard() | |
529 | { | |
794fe8cd VZ |
530 | #if wxUSE_OLE_CLIPBOARD |
531 | wxOleInitialize(); | |
532 | #endif | |
533 | ||
02b7b6b0 WS |
534 | m_clearOnExit = false; |
535 | m_isOpened = false; | |
2bda0e17 KB |
536 | } |
537 | ||
538 | wxClipboard::~wxClipboard() | |
539 | { | |
d59ceba5 VZ |
540 | if ( m_clearOnExit ) |
541 | { | |
542 | Clear(); | |
543 | } | |
794fe8cd VZ |
544 | |
545 | #if wxUSE_OLE_CLIPBOARD | |
546 | wxOleUninitialize(); | |
547 | #endif | |
2bda0e17 KB |
548 | } |
549 | ||
06e43511 | 550 | void wxClipboard::Clear() |
2bda0e17 | 551 | { |
d59ceba5 | 552 | #if wxUSE_OLE_CLIPBOARD |
110a0fea VZ |
553 | HRESULT hr = OleSetClipboard(NULL); |
554 | if ( FAILED(hr) ) | |
d59ceba5 | 555 | { |
110a0fea | 556 | wxLogApiError(wxT("OleSetClipboard(NULL)"), hr); |
d59ceba5 | 557 | } |
110a0fea | 558 | #endif // wxUSE_OLE_CLIPBOARD |
d59ceba5 VZ |
559 | } |
560 | ||
561 | bool wxClipboard::Flush() | |
562 | { | |
7ffdaf81 | 563 | #if wxUSE_OLE_CLIPBOARD |
110a0fea VZ |
564 | HRESULT hr = OleFlushClipboard(); |
565 | if ( FAILED(hr) ) | |
d59ceba5 | 566 | { |
110a0fea | 567 | wxLogApiError(wxT("OleFlushClipboard"), hr); |
d59ceba5 | 568 | |
02b7b6b0 | 569 | return false; |
d59ceba5 VZ |
570 | } |
571 | else | |
572 | { | |
02b7b6b0 | 573 | m_clearOnExit = false; |
d59ceba5 | 574 | |
02b7b6b0 | 575 | return true; |
d59ceba5 | 576 | } |
7ffdaf81 | 577 | #else // !wxUSE_OLE_CLIPBOARD |
02b7b6b0 | 578 | return false; |
7ffdaf81 | 579 | #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD |
2bda0e17 KB |
580 | } |
581 | ||
06e43511 | 582 | bool wxClipboard::Open() |
2bda0e17 | 583 | { |
d59ceba5 | 584 | // OLE opens clipboard for us |
02b7b6b0 | 585 | m_isOpened = true; |
d59ceba5 | 586 | #if wxUSE_OLE_CLIPBOARD |
02b7b6b0 | 587 | return true; |
d59ceba5 | 588 | #else |
06e43511 | 589 | return wxOpenClipboard(); |
d59ceba5 | 590 | #endif |
2bda0e17 KB |
591 | } |
592 | ||
f536e0f2 VZ |
593 | bool wxClipboard::IsOpened() const |
594 | { | |
595 | #if wxUSE_OLE_CLIPBOARD | |
a36d790a | 596 | return m_isOpened; |
f536e0f2 VZ |
597 | #else |
598 | return wxIsClipboardOpened(); | |
599 | #endif | |
600 | } | |
601 | ||
06e43511 | 602 | bool wxClipboard::SetData( wxDataObject *data ) |
2bda0e17 | 603 | { |
51edda6a | 604 | #if !wxUSE_OLE_CLIPBOARD |
26f86486 | 605 | (void)wxEmptyClipboard(); |
51edda6a | 606 | #endif // wxUSE_OLE_CLIPBOARD |
26f86486 VZ |
607 | |
608 | if ( data ) | |
609 | return AddData(data); | |
610 | else | |
02b7b6b0 | 611 | return true; |
26f86486 VZ |
612 | } |
613 | ||
614 | bool wxClipboard::AddData( wxDataObject *data ) | |
615 | { | |
02b7b6b0 | 616 | wxCHECK_MSG( data, false, wxT("data is invalid") ); |
06e43511 | 617 | |
d59ceba5 VZ |
618 | #if wxUSE_OLE_CLIPBOARD |
619 | HRESULT hr = OleSetClipboard(data->GetInterface()); | |
620 | if ( FAILED(hr) ) | |
621 | { | |
622 | wxLogSysError(hr, _("Failed to put data on the clipboard")); | |
623 | ||
624 | // don't free anything in this case | |
625 | ||
02b7b6b0 | 626 | return false; |
d59ceba5 VZ |
627 | } |
628 | ||
629 | // we have a problem here because we should delete wxDataObject, but we | |
630 | // can't do it because IDataObject which we just gave to the clipboard | |
631 | // would try to use it when it will need the data. IDataObject is ref | |
632 | // counted and so doesn't suffer from such problem, so we release it now | |
633 | // and tell it to delete wxDataObject when it is deleted itself. | |
634 | data->SetAutoDelete(); | |
635 | ||
636 | // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when | |
637 | // using OLE clipboard when the app terminates - by default, we call | |
638 | // OleSetClipboard(NULL) which won't waste RAM, but the app can call | |
639 | // wxClipboard::Flush() to chaneg this | |
02b7b6b0 | 640 | m_clearOnExit = true; |
d59ceba5 | 641 | |
02b7b6b0 | 642 | return true; |
d59ceba5 | 643 | #elif wxUSE_DATAOBJ |
02b7b6b0 | 644 | wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") ); |
26f86486 | 645 | |
7fc0bd1c | 646 | wxDataFormat format = data->GetPreferredFormat(); |
26f86486 VZ |
647 | |
648 | switch ( format ) | |
06e43511 JS |
649 | { |
650 | case wxDF_TEXT: | |
651 | case wxDF_OEMTEXT: | |
652 | { | |
653 | wxTextDataObject* textDataObject = (wxTextDataObject*) data; | |
654 | wxString str(textDataObject->GetText()); | |
26f86486 | 655 | return wxSetClipboardData(format, str.c_str()); |
06e43511 | 656 | } |
26f86486 | 657 | |
06e43511 JS |
658 | case wxDF_BITMAP: |
659 | case wxDF_DIB: | |
660 | { | |
661 | wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data; | |
662 | wxBitmap bitmap(bitmapDataObject->GetBitmap()); | |
7fc0bd1c | 663 | return wxSetClipboardData(data->GetPreferredFormat(), &bitmap); |
06e43511 | 664 | } |
26f86486 | 665 | |
06e43511 JS |
666 | #if wxUSE_METAFILE |
667 | case wxDF_METAFILE: | |
668 | { | |
7fc0bd1c JS |
669 | #if 1 |
670 | // TODO | |
0b4f47a3 | 671 | wxLogError(wxT("Not implemented because wxMetafileDataObject does not contain width and height values.")); |
02b7b6b0 | 672 | return false; |
7fc0bd1c | 673 | #else |
33ac7e6f | 674 | wxMetafileDataObject* metaFileDataObject = |
26f86486 | 675 | (wxMetafileDataObject*) data; |
06e43511 | 676 | wxMetafile metaFile = metaFileDataObject->GetMetafile(); |
26f86486 VZ |
677 | return wxSetClipboardData(wxDF_METAFILE, &metaFile, |
678 | metaFileDataObject->GetWidth(), | |
679 | metaFileDataObject->GetHeight()); | |
7fc0bd1c | 680 | #endif |
06e43511 | 681 | } |
26f86486 VZ |
682 | #endif // wxUSE_METAFILE |
683 | ||
06e43511 | 684 | default: |
7fc0bd1c JS |
685 | { |
686 | // This didn't compile, of course | |
687 | // return wxSetClipboardData(data); | |
688 | // TODO | |
f07dc2e2 | 689 | wxLogError(wxT("Not implemented.")); |
02b7b6b0 | 690 | return false; |
7fc0bd1c | 691 | } |
06e43511 | 692 | } |
d59ceba5 | 693 | #else // !wxUSE_DATAOBJ |
02b7b6b0 | 694 | return false; |
d59ceba5 | 695 | #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ |
2bda0e17 KB |
696 | } |
697 | ||
06e43511 | 698 | void wxClipboard::Close() |
2bda0e17 | 699 | { |
02b7b6b0 | 700 | m_isOpened = false; |
d59ceba5 VZ |
701 | // OLE closes clipboard for us |
702 | #if !wxUSE_OLE_CLIPBOARD | |
06e43511 | 703 | wxCloseClipboard(); |
d59ceba5 | 704 | #endif |
2bda0e17 KB |
705 | } |
706 | ||
2af18715 | 707 | bool wxClipboard::IsSupported( const wxDataFormat& format ) |
2bda0e17 | 708 | { |
06e43511 | 709 | return wxIsClipboardFormatAvailable(format); |
2bda0e17 KB |
710 | } |
711 | ||
1e8335b0 | 712 | bool wxClipboard::GetData( wxDataObject& data ) |
2bda0e17 | 713 | { |
d59ceba5 VZ |
714 | #if wxUSE_OLE_CLIPBOARD |
715 | IDataObject *pDataObject = NULL; | |
716 | HRESULT hr = OleGetClipboard(&pDataObject); | |
717 | if ( FAILED(hr) || !pDataObject ) | |
718 | { | |
719 | wxLogSysError(hr, _("Failed to get data from the clipboard")); | |
720 | ||
02b7b6b0 | 721 | return false; |
d59ceba5 VZ |
722 | } |
723 | ||
724 | // build the list of supported formats | |
1e8335b0 | 725 | size_t nFormats = data.GetFormatCount(wxDataObject::Set); |
33ac7e6f | 726 | wxDataFormat format; |
c5639a87 | 727 | wxDataFormat *formats; |
d59ceba5 VZ |
728 | if ( nFormats == 1 ) |
729 | { | |
730 | // the most common case | |
731 | formats = &format; | |
732 | } | |
733 | else | |
734 | { | |
735 | // bad luck, need to alloc mem | |
736 | formats = new wxDataFormat[nFormats]; | |
737 | } | |
738 | ||
1e8335b0 | 739 | data.GetAllFormats(formats, wxDataObject::Set); |
d59ceba5 | 740 | |
6c29712c JS |
741 | // get the data for the given formats |
742 | FORMATETC formatEtc; | |
743 | CLIPFORMAT cf; | |
02b7b6b0 | 744 | bool result = false; |
6c29712c JS |
745 | |
746 | // enumerate all explicit formats on the clipboard. | |
747 | // note that this does not include implicit / synthetic (automatically | |
748 | // converted) formats. | |
749 | #ifdef __WXDEBUG__ | |
750 | // get the format enumerator | |
d59ceba5 VZ |
751 | IEnumFORMATETC *pEnumFormatEtc = NULL; |
752 | hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc); | |
753 | if ( FAILED(hr) || !pEnumFormatEtc ) | |
754 | { | |
755 | wxLogSysError(hr, | |
756 | _("Failed to retrieve the supported clipboard formats")); | |
757 | } | |
758 | else | |
759 | { | |
760 | // ask for the supported formats and see if there are any we support | |
d59ceba5 VZ |
761 | for ( ;; ) |
762 | { | |
763 | ULONG nCount; | |
764 | hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount); | |
765 | ||
766 | // don't use FAILED() because S_FALSE would pass it | |
767 | if ( hr != S_OK ) | |
768 | { | |
769 | // no more formats | |
770 | break; | |
771 | } | |
772 | ||
6c29712c | 773 | cf = formatEtc.cfFormat; |
d59ceba5 | 774 | |
d59ceba5 VZ |
775 | wxLogTrace(wxTRACE_OleCalls, |
776 | wxT("Object on the clipboard supports format %s."), | |
777 | wxDataObject::GetFormatName(cf)); | |
d59ceba5 VZ |
778 | } |
779 | ||
780 | pEnumFormatEtc->Release(); | |
781 | } | |
6c29712c | 782 | #endif // Debug |
d59ceba5 | 783 | |
6c29712c JS |
784 | STGMEDIUM medium; |
785 | // stop at the first valid format found on the clipboard | |
786 | for ( size_t n = 0; !result && (n < nFormats); n++ ) | |
d59ceba5 | 787 | { |
6c29712c JS |
788 | // convert to NativeFormat Id |
789 | cf = formats[n].GetFormatId(); | |
02b7b6b0 | 790 | |
6c29712c JS |
791 | // if the format is not available, try the next one |
792 | // this test includes implicit / sythetic formats | |
793 | if ( !::IsClipboardFormatAvailable(cf) ) | |
794 | continue; | |
02b7b6b0 | 795 | |
6c29712c | 796 | formatEtc.cfFormat = cf; |
d59ceba5 VZ |
797 | formatEtc.ptd = NULL; |
798 | formatEtc.dwAspect = DVASPECT_CONTENT; | |
799 | formatEtc.lindex = -1; | |
d59ceba5 | 800 | |
6c29712c JS |
801 | // use the appropriate tymed |
802 | switch ( formatEtc.cfFormat ) | |
d59ceba5 | 803 | { |
6c29712c JS |
804 | case CF_BITMAP: |
805 | formatEtc.tymed = TYMED_GDI; | |
806 | break; | |
d59ceba5 | 807 | |
4676948b | 808 | #ifndef __WXWINCE__ |
6c29712c JS |
809 | case CF_METAFILEPICT: |
810 | formatEtc.tymed = TYMED_MFPICT; | |
811 | break; | |
265b0c07 | 812 | |
6c29712c JS |
813 | case CF_ENHMETAFILE: |
814 | formatEtc.tymed = TYMED_ENHMF; | |
815 | break; | |
4676948b | 816 | #endif |
265b0c07 | 817 | |
6c29712c JS |
818 | default: |
819 | formatEtc.tymed = TYMED_HGLOBAL; | |
820 | } | |
821 | ||
822 | // try to get data | |
823 | hr = pDataObject->GetData(&formatEtc, &medium); | |
824 | if ( FAILED(hr) ) | |
825 | { | |
826 | // try other tymed for GDI objects | |
827 | if ( formatEtc.cfFormat == CF_BITMAP ) | |
d59ceba5 | 828 | { |
6c29712c JS |
829 | formatEtc.tymed = TYMED_HGLOBAL; |
830 | hr = pDataObject->GetData(&formatEtc, &medium); | |
d59ceba5 | 831 | } |
6c29712c | 832 | } |
d59ceba5 | 833 | |
6c29712c JS |
834 | if ( SUCCEEDED(hr) ) |
835 | { | |
836 | // pass the data to the data object | |
02b7b6b0 | 837 | hr = data.GetInterface()->SetData(&formatEtc, &medium, true); |
6c29712c | 838 | if ( FAILED(hr) ) |
d59ceba5 | 839 | { |
6c29712c | 840 | wxLogDebug(wxT("Failed to set data in wxIDataObject")); |
d59ceba5 | 841 | |
6c29712c JS |
842 | // IDataObject only takes the ownership of data if it |
843 | // successfully got it - which is not the case here | |
844 | ReleaseStgMedium(&medium); | |
845 | } | |
846 | else | |
847 | { | |
02b7b6b0 | 848 | result = true; |
d59ceba5 | 849 | } |
d59ceba5 | 850 | } |
6c29712c | 851 | //else: unsupported tymed? |
d59ceba5 | 852 | } |
6c29712c JS |
853 | |
854 | if ( formats != &format ) | |
855 | { | |
856 | delete [] formats; | |
857 | } | |
858 | //else: we didn't allocate any memory | |
d59ceba5 VZ |
859 | |
860 | // clean up and return | |
861 | pDataObject->Release(); | |
862 | ||
863 | return result; | |
864 | #elif wxUSE_DATAOBJ | |
02b7b6b0 | 865 | wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") ); |
26f86486 | 866 | |
7fc0bd1c | 867 | wxDataFormat format = data.GetPreferredFormat(); |
26f86486 | 868 | switch ( format ) |
06e43511 JS |
869 | { |
870 | case wxDF_TEXT: | |
871 | case wxDF_OEMTEXT: | |
872 | { | |
1e8335b0 VZ |
873 | wxTextDataObject& textDataObject = (wxTextDataObject &)data; |
874 | char* s = (char*)wxGetClipboardData(format); | |
875 | if ( !s ) | |
02b7b6b0 | 876 | return false; |
1e8335b0 | 877 | |
f07dc2e2 | 878 | textDataObject.SetText(wxString::FromAscii(s)); |
1e8335b0 VZ |
879 | delete [] s; |
880 | ||
02b7b6b0 | 881 | return true; |
06e43511 | 882 | } |
26f86486 | 883 | |
06e43511 JS |
884 | case wxDF_BITMAP: |
885 | case wxDF_DIB: | |
886 | { | |
1e8335b0 | 887 | wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data; |
7fc0bd1c | 888 | wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat()); |
1e8335b0 | 889 | if ( !bitmap ) |
02b7b6b0 | 890 | return false; |
1e8335b0 VZ |
891 | |
892 | bitmapDataObject.SetBitmap(*bitmap); | |
893 | delete bitmap; | |
894 | ||
02b7b6b0 | 895 | return true; |
06e43511 JS |
896 | } |
897 | #if wxUSE_METAFILE | |
898 | case wxDF_METAFILE: | |
899 | { | |
1e8335b0 | 900 | wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data; |
26f86486 | 901 | wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE); |
1e8335b0 | 902 | if ( !metaFile ) |
02b7b6b0 | 903 | return false; |
3f480da3 | 904 | |
1e8335b0 VZ |
905 | metaFileDataObject.SetMetafile(*metaFile); |
906 | delete metaFile; | |
26f86486 | 907 | |
02b7b6b0 | 908 | return true; |
1e8335b0 VZ |
909 | } |
910 | #endif // wxUSE_METAFILE | |
06e43511 | 911 | } |
02b7b6b0 | 912 | return false; |
d59ceba5 | 913 | #else // !wxUSE_DATAOBJ |
1e8335b0 | 914 | wxFAIL_MSG( wxT("no clipboard implementation") ); |
02b7b6b0 | 915 | return false; |
3897b707 | 916 | #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ |
2bda0e17 KB |
917 | } |
918 | ||
47d67540 | 919 | #endif // wxUSE_CLIPBOARD |
4ce81a75 | 920 |