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