]>
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$ | |
8 | // Copyright: (c) Julian Smart and Markus Holzem | |
26f86486 | 9 | // Licence: wxWindows license |
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 KB |
57 | #include "wx/msw/private.h" |
58 | #include "wx/msw/dib.h" | |
59 | ||
d59ceba5 VZ |
60 | // wxDataObject is tied to OLE/drag and drop implementation, therefore so are |
61 | // the functions using wxDataObject in wxClipboard | |
62 | #define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP | |
63 | ||
64 | #if wxUSE_DATAOBJ | |
26f86486 | 65 | #include "wx/dataobj.h" |
3f480da3 | 66 | |
d59ceba5 VZ |
67 | // use OLE clipboard |
68 | #define wxUSE_OLE_CLIPBOARD 1 | |
69 | #else // !wxUSE_DATAOBJ | |
70 | // use Win clipboard API | |
71 | #define wxUSE_OLE_CLIPBOARD 0 | |
2bda0e17 KB |
72 | #endif |
73 | ||
d59ceba5 VZ |
74 | #if wxUSE_OLE_CLIPBOARD |
75 | #include <ole2.h> | |
76 | #endif // wxUSE_OLE_CLIPBOARD | |
77 | ||
3f480da3 VZ |
78 | #ifdef __WIN16__ |
79 | #define memcpy hmemcpy | |
80 | #endif | |
06e43511 | 81 | |
26f86486 VZ |
82 | // =========================================================================== |
83 | // implementation | |
84 | // =========================================================================== | |
85 | ||
86 | // --------------------------------------------------------------------------- | |
87 | // old-style clipboard functions using Windows API | |
88 | // --------------------------------------------------------------------------- | |
2bda0e17 | 89 | |
26f86486 VZ |
90 | static bool gs_wxClipboardIsOpen = FALSE; |
91 | ||
92 | bool wxOpenClipboard() | |
2bda0e17 | 93 | { |
223d09f6 | 94 | wxCHECK_MSG( !gs_wxClipboardIsOpen, TRUE, wxT("clipboard already opened.") ); |
26f86486 VZ |
95 | |
96 | wxWindow *win = wxTheApp->GetTopWindow(); | |
97 | if ( win ) | |
98 | { | |
99 | gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0; | |
100 | ||
101 | if ( !gs_wxClipboardIsOpen ) | |
102 | wxLogSysError(_("Failed to open the clipboard.")); | |
103 | ||
104 | return gs_wxClipboardIsOpen; | |
105 | } | |
106 | else | |
107 | { | |
223d09f6 | 108 | wxLogDebug(wxT("Can not open clipboard without a main window.")); |
26f86486 VZ |
109 | |
110 | return FALSE; | |
111 | } | |
2bda0e17 KB |
112 | } |
113 | ||
26f86486 | 114 | bool wxCloseClipboard() |
2bda0e17 | 115 | { |
223d09f6 | 116 | wxCHECK_MSG( gs_wxClipboardIsOpen, FALSE, wxT("clipboard is not opened") ); |
26f86486 VZ |
117 | |
118 | gs_wxClipboardIsOpen = FALSE; | |
119 | ||
120 | if ( ::CloseClipboard() == 0 ) | |
121 | { | |
122 | wxLogSysError(_("Failed to close the clipboard.")); | |
123 | ||
124 | return FALSE; | |
125 | } | |
126 | ||
127 | return TRUE; | |
2bda0e17 KB |
128 | } |
129 | ||
26f86486 | 130 | bool wxEmptyClipboard() |
2bda0e17 | 131 | { |
26f86486 VZ |
132 | if ( ::EmptyClipboard() == 0 ) |
133 | { | |
134 | wxLogSysError(_("Failed to empty the clipboard.")); | |
135 | ||
136 | return FALSE; | |
137 | } | |
138 | ||
139 | return TRUE; | |
2bda0e17 KB |
140 | } |
141 | ||
26f86486 | 142 | bool wxIsClipboardOpened() |
2bda0e17 | 143 | { |
26f86486 | 144 | return gs_wxClipboardIsOpen; |
2bda0e17 KB |
145 | } |
146 | ||
06e43511 | 147 | bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat) |
2bda0e17 | 148 | { |
d9317fd4 VZ |
149 | if ( ::IsClipboardFormatAvailable(dataFormat) ) |
150 | { | |
151 | // ok from the first try | |
152 | return TRUE; | |
153 | } | |
154 | ||
155 | // for several standard formats, we can convert from some other ones too | |
156 | switch ( dataFormat.GetFormatId() ) | |
157 | { | |
158 | // for bitmaps, DIBs will also do | |
159 | case CF_BITMAP: | |
160 | return ::IsClipboardFormatAvailable(CF_DIB) != 0; | |
161 | ||
5dd26b08 | 162 | #if wxUSE_ENH_METAFILE && !defined(__WIN16__) |
d9317fd4 VZ |
163 | case CF_METAFILEPICT: |
164 | return ::IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0; | |
165 | #endif // wxUSE_ENH_METAFILE | |
166 | ||
167 | default: | |
168 | return FALSE; | |
169 | } | |
2bda0e17 KB |
170 | } |
171 | ||
26f86486 VZ |
172 | bool wxSetClipboardData(wxDataFormat dataFormat, |
173 | const void *data, | |
174 | int width, int height) | |
2bda0e17 | 175 | { |
26f86486 VZ |
176 | HANDLE handle = 0; // return value of SetClipboardData |
177 | ||
178 | switch (dataFormat) | |
2bda0e17 | 179 | { |
26f86486 VZ |
180 | case wxDF_BITMAP: |
181 | { | |
182 | wxBitmap *bitmap = (wxBitmap *)data; | |
183 | ||
184 | HDC hdcMem = CreateCompatibleDC((HDC) NULL); | |
185 | HDC hdcSrc = CreateCompatibleDC((HDC) NULL); | |
186 | HBITMAP old = (HBITMAP) | |
187 | ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP()); | |
188 | HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc, | |
189 | bitmap->GetWidth(), | |
190 | bitmap->GetHeight()); | |
191 | if (!hBitmap) | |
192 | { | |
193 | SelectObject(hdcSrc, old); | |
194 | DeleteDC(hdcMem); | |
195 | DeleteDC(hdcSrc); | |
196 | return FALSE; | |
197 | } | |
198 | ||
199 | HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap); | |
200 | BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(), | |
201 | hdcSrc, 0, 0, SRCCOPY); | |
202 | ||
203 | // Select new bitmap out of memory DC | |
204 | SelectObject(hdcMem, old1); | |
205 | ||
206 | // Set the data | |
207 | handle = ::SetClipboardData(CF_BITMAP, hBitmap); | |
208 | ||
209 | // Clean up | |
210 | SelectObject(hdcSrc, old); | |
211 | DeleteDC(hdcSrc); | |
212 | DeleteDC(hdcMem); | |
213 | break; | |
214 | } | |
215 | ||
216 | case wxDF_DIB: | |
217 | { | |
47d67540 | 218 | #if wxUSE_IMAGE_LOADING_IN_MSW |
26f86486 VZ |
219 | wxBitmap *bitmap = (wxBitmap *)data; |
220 | HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP(); | |
221 | // NULL palette means to use the system one | |
ef3ab009 | 222 | HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL); |
26f86486 | 223 | handle = SetClipboardData(CF_DIB, hDIB); |
d59ceba5 | 224 | #endif // wxUSE_IMAGE_LOADING_IN_MSW |
26f86486 VZ |
225 | break; |
226 | } | |
227 | ||
d9317fd4 VZ |
228 | // VZ: I'm told that this code works, but it doesn't seem to work for me |
229 | // and, anyhow, I'd be highly surprized if it did. So I leave it here | |
230 | // but IMNSHO it is completely broken. | |
231 | #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) | |
26f86486 VZ |
232 | case wxDF_METAFILE: |
233 | { | |
234 | wxMetafile *wxMF = (wxMetafile *)data; | |
235 | HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1); | |
26f86486 | 236 | METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data); |
2bda0e17 | 237 | |
26f86486 VZ |
238 | mf->mm = wxMF->GetWindowsMappingMode(); |
239 | mf->xExt = width; | |
240 | mf->yExt = height; | |
241 | mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE(); | |
242 | GlobalUnlock(data); | |
243 | wxMF->SetHMETAFILE((WXHANDLE) NULL); | |
2bda0e17 | 244 | |
26f86486 VZ |
245 | handle = SetClipboardData(CF_METAFILEPICT, data); |
246 | break; | |
247 | } | |
d9317fd4 VZ |
248 | #endif // wxUSE_METAFILE |
249 | ||
5dd26b08 | 250 | #if wxUSE_ENH_METAFILE && !defined(__WIN16__) |
d9317fd4 VZ |
251 | case wxDF_ENHMETAFILE: |
252 | { | |
253 | wxEnhMetaFile *emf = (wxEnhMetaFile *)data; | |
254 | wxEnhMetaFile emfCopy = *emf; | |
255 | ||
256 | handle = SetClipboardData(CF_ENHMETAFILE, | |
257 | (void *)emfCopy.GetHENHMETAFILE()); | |
258 | } | |
259 | break; | |
260 | #endif // wxUSE_ENH_METAFILE | |
261 | ||
26f86486 VZ |
262 | case CF_SYLK: |
263 | case CF_DIF: | |
264 | case CF_TIFF: | |
265 | case CF_PALETTE: | |
266 | default: | |
267 | { | |
268 | wxLogError(_("Unsupported clipboard format.")); | |
269 | return FALSE; | |
270 | } | |
2bda0e17 | 271 | |
26f86486 VZ |
272 | case wxDF_OEMTEXT: |
273 | dataFormat = wxDF_TEXT; | |
274 | // fall through | |
2bda0e17 | 275 | |
26f86486 VZ |
276 | case wxDF_TEXT: |
277 | { | |
278 | char *s = (char *)data; | |
279 | ||
280 | width = strlen(s) + 1; | |
281 | height = 1; | |
282 | DWORD l = (width * height); | |
283 | HANDLE hGlobalMemory = GlobalAlloc(GHND, l); | |
284 | if ( hGlobalMemory ) | |
285 | { | |
26f86486 | 286 | LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory); |
2bda0e17 | 287 | |
26f86486 | 288 | memcpy(lpGlobalMemory, s, l); |
2bda0e17 | 289 | |
26f86486 VZ |
290 | GlobalUnlock(hGlobalMemory); |
291 | } | |
2bda0e17 | 292 | |
26f86486 VZ |
293 | handle = SetClipboardData(dataFormat, hGlobalMemory); |
294 | break; | |
295 | } | |
2bda0e17 | 296 | } |
26f86486 VZ |
297 | |
298 | if ( handle == 0 ) | |
2bda0e17 | 299 | { |
26f86486 VZ |
300 | wxLogSysError(_("Failed to set clipboard data.")); |
301 | ||
302 | return FALSE; | |
2bda0e17 | 303 | } |
26f86486 VZ |
304 | |
305 | return TRUE; | |
306 | } | |
307 | ||
308 | void *wxGetClipboardData(wxDataFormat dataFormat, long *len) | |
309 | { | |
310 | void *retval = NULL; | |
311 | ||
312 | switch ( dataFormat ) | |
2bda0e17 | 313 | { |
26f86486 VZ |
314 | case wxDF_BITMAP: |
315 | { | |
316 | BITMAP bm; | |
317 | HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP); | |
318 | if (!hBitmap) | |
319 | break; | |
320 | ||
321 | HDC hdcMem = CreateCompatibleDC((HDC) NULL); | |
322 | HDC hdcSrc = CreateCompatibleDC((HDC) NULL); | |
323 | ||
324 | HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap); | |
325 | GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm); | |
326 | ||
327 | HBITMAP hNewBitmap = CreateBitmapIndirect(&bm); | |
328 | ||
329 | if (!hNewBitmap) | |
330 | { | |
331 | SelectObject(hdcSrc, old); | |
332 | DeleteDC(hdcMem); | |
333 | DeleteDC(hdcSrc); | |
334 | break; | |
335 | } | |
336 | ||
337 | HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap); | |
338 | BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, | |
339 | hdcSrc, 0, 0, SRCCOPY); | |
340 | ||
341 | // Select new bitmap out of memory DC | |
342 | SelectObject(hdcMem, old1); | |
343 | ||
344 | // Clean up | |
345 | SelectObject(hdcSrc, old); | |
346 | DeleteDC(hdcSrc); | |
347 | DeleteDC(hdcMem); | |
348 | ||
349 | // Create and return a new wxBitmap | |
350 | wxBitmap *wxBM = new wxBitmap; | |
351 | wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap); | |
352 | wxBM->SetWidth(bm.bmWidth); | |
353 | wxBM->SetHeight(bm.bmHeight); | |
354 | wxBM->SetDepth(bm.bmPlanes); | |
6d167489 | 355 | #if WXWIN_COMPATIBILITY_2 |
26f86486 | 356 | wxBM->SetOk(TRUE); |
6d167489 | 357 | #endif // WXWIN_COMPATIBILITY_2 |
26f86486 VZ |
358 | retval = wxBM; |
359 | break; | |
360 | } | |
361 | ||
362 | case wxDF_METAFILE: | |
363 | case CF_SYLK: | |
364 | case CF_DIF: | |
365 | case CF_TIFF: | |
366 | case CF_PALETTE: | |
367 | case wxDF_DIB: | |
26f86486 VZ |
368 | { |
369 | wxLogError(_("Unsupported clipboard format.")); | |
370 | return FALSE; | |
371 | } | |
372 | ||
373 | case wxDF_OEMTEXT: | |
374 | dataFormat = wxDF_TEXT; | |
375 | // fall through | |
376 | ||
377 | case wxDF_TEXT: | |
378 | { | |
379 | HANDLE hGlobalMemory = ::GetClipboardData(dataFormat); | |
380 | if (!hGlobalMemory) | |
381 | break; | |
2bda0e17 | 382 | |
26f86486 VZ |
383 | DWORD hsize = ::GlobalSize(hGlobalMemory); |
384 | if (len) | |
385 | *len = hsize; | |
2bda0e17 | 386 | |
26f86486 VZ |
387 | char *s = new char[hsize]; |
388 | if (!s) | |
389 | break; | |
2bda0e17 | 390 | |
26f86486 | 391 | LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory); |
2bda0e17 | 392 | |
26f86486 | 393 | memcpy(s, lpGlobalMemory, hsize); |
2bda0e17 | 394 | |
26f86486 VZ |
395 | ::GlobalUnlock(hGlobalMemory); |
396 | ||
397 | retval = s; | |
398 | break; | |
399 | } | |
3f480da3 VZ |
400 | |
401 | default: | |
402 | { | |
403 | HANDLE hGlobalMemory = ::GetClipboardData(dataFormat); | |
404 | if ( !hGlobalMemory ) | |
405 | break; | |
406 | ||
407 | DWORD size = ::GlobalSize(hGlobalMemory); | |
408 | if ( len ) | |
409 | *len = size; | |
410 | ||
411 | void *buf = malloc(size); | |
412 | if ( !buf ) | |
413 | break; | |
414 | ||
415 | LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory); | |
416 | ||
417 | memcpy(buf, lpGlobalMemory, size); | |
418 | ||
419 | ::GlobalUnlock(hGlobalMemory); | |
420 | ||
421 | retval = buf; | |
422 | break; | |
423 | } | |
26f86486 | 424 | } |
2bda0e17 | 425 | |
26f86486 VZ |
426 | if ( !retval ) |
427 | { | |
428 | wxLogSysError(_("Failed to retrieve data from the clipboard.")); | |
2bda0e17 | 429 | } |
26f86486 VZ |
430 | |
431 | return retval; | |
2bda0e17 KB |
432 | } |
433 | ||
3f480da3 | 434 | wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat) |
2bda0e17 | 435 | { |
3f480da3 | 436 | return ::EnumClipboardFormats(dataFormat); |
2bda0e17 KB |
437 | } |
438 | ||
837e5743 | 439 | int wxRegisterClipboardFormat(wxChar *formatName) |
2bda0e17 KB |
440 | { |
441 | return ::RegisterClipboardFormat(formatName); | |
442 | } | |
443 | ||
26f86486 | 444 | bool wxGetClipboardFormatName(wxDataFormat dataFormat, |
837e5743 | 445 | wxChar *formatName, |
26f86486 | 446 | int maxCount) |
2bda0e17 | 447 | { |
26f86486 | 448 | return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0; |
2bda0e17 KB |
449 | } |
450 | ||
26f86486 | 451 | // --------------------------------------------------------------------------- |
06e43511 | 452 | // wxClipboard |
26f86486 | 453 | // --------------------------------------------------------------------------- |
2bda0e17 | 454 | |
26f86486 | 455 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject) |
2bda0e17 KB |
456 | |
457 | wxClipboard::wxClipboard() | |
458 | { | |
d59ceba5 | 459 | m_clearOnExit = FALSE; |
2bda0e17 KB |
460 | } |
461 | ||
462 | wxClipboard::~wxClipboard() | |
463 | { | |
d59ceba5 VZ |
464 | if ( m_clearOnExit ) |
465 | { | |
466 | Clear(); | |
467 | } | |
2bda0e17 KB |
468 | } |
469 | ||
06e43511 | 470 | void wxClipboard::Clear() |
2bda0e17 | 471 | { |
d59ceba5 VZ |
472 | #if wxUSE_OLE_CLIPBOARD |
473 | if ( FAILED(OleSetClipboard(NULL)) ) | |
474 | { | |
475 | wxLogLastError("OleSetClipboard(NULL)"); | |
476 | } | |
477 | #endif | |
478 | } | |
479 | ||
480 | bool wxClipboard::Flush() | |
481 | { | |
7ffdaf81 | 482 | #if wxUSE_OLE_CLIPBOARD |
d59ceba5 VZ |
483 | if ( FAILED(OleFlushClipboard()) ) |
484 | { | |
485 | wxLogLastError("OleFlushClipboard"); | |
486 | ||
487 | return FALSE; | |
488 | } | |
489 | else | |
490 | { | |
491 | m_clearOnExit = FALSE; | |
492 | ||
493 | return TRUE; | |
494 | } | |
7ffdaf81 VZ |
495 | #else // !wxUSE_OLE_CLIPBOARD |
496 | return FALSE; | |
497 | #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD | |
2bda0e17 KB |
498 | } |
499 | ||
06e43511 | 500 | bool wxClipboard::Open() |
2bda0e17 | 501 | { |
d59ceba5 VZ |
502 | // OLE opens clipboard for us |
503 | #if wxUSE_OLE_CLIPBOARD | |
504 | return TRUE; | |
505 | #else | |
06e43511 | 506 | return wxOpenClipboard(); |
d59ceba5 | 507 | #endif |
2bda0e17 KB |
508 | } |
509 | ||
f536e0f2 VZ |
510 | bool wxClipboard::IsOpened() const |
511 | { | |
512 | #if wxUSE_OLE_CLIPBOARD | |
513 | return TRUE; | |
514 | #else | |
515 | return wxIsClipboardOpened(); | |
516 | #endif | |
517 | } | |
518 | ||
06e43511 | 519 | bool wxClipboard::SetData( wxDataObject *data ) |
2bda0e17 | 520 | { |
51edda6a | 521 | #if !wxUSE_OLE_CLIPBOARD |
26f86486 | 522 | (void)wxEmptyClipboard(); |
51edda6a | 523 | #endif // wxUSE_OLE_CLIPBOARD |
26f86486 VZ |
524 | |
525 | if ( data ) | |
526 | return AddData(data); | |
527 | else | |
528 | return TRUE; | |
529 | } | |
530 | ||
531 | bool wxClipboard::AddData( wxDataObject *data ) | |
532 | { | |
223d09f6 | 533 | wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); |
06e43511 | 534 | |
d59ceba5 VZ |
535 | #if wxUSE_OLE_CLIPBOARD |
536 | HRESULT hr = OleSetClipboard(data->GetInterface()); | |
537 | if ( FAILED(hr) ) | |
538 | { | |
539 | wxLogSysError(hr, _("Failed to put data on the clipboard")); | |
540 | ||
541 | // don't free anything in this case | |
542 | ||
543 | return FALSE; | |
544 | } | |
545 | ||
546 | // we have a problem here because we should delete wxDataObject, but we | |
547 | // can't do it because IDataObject which we just gave to the clipboard | |
548 | // would try to use it when it will need the data. IDataObject is ref | |
549 | // counted and so doesn't suffer from such problem, so we release it now | |
550 | // and tell it to delete wxDataObject when it is deleted itself. | |
551 | data->SetAutoDelete(); | |
552 | ||
553 | // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when | |
554 | // using OLE clipboard when the app terminates - by default, we call | |
555 | // OleSetClipboard(NULL) which won't waste RAM, but the app can call | |
556 | // wxClipboard::Flush() to chaneg this | |
557 | m_clearOnExit = TRUE; | |
558 | ||
559 | return TRUE; | |
560 | #elif wxUSE_DATAOBJ | |
223d09f6 | 561 | wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") ); |
26f86486 VZ |
562 | |
563 | wxDataFormat format = data->GetFormat(); | |
564 | ||
565 | switch ( format ) | |
06e43511 JS |
566 | { |
567 | case wxDF_TEXT: | |
568 | case wxDF_OEMTEXT: | |
569 | { | |
570 | wxTextDataObject* textDataObject = (wxTextDataObject*) data; | |
571 | wxString str(textDataObject->GetText()); | |
26f86486 | 572 | return wxSetClipboardData(format, str.c_str()); |
06e43511 | 573 | } |
26f86486 | 574 | |
06e43511 JS |
575 | case wxDF_BITMAP: |
576 | case wxDF_DIB: | |
577 | { | |
578 | wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data; | |
579 | wxBitmap bitmap(bitmapDataObject->GetBitmap()); | |
26f86486 | 580 | return wxSetClipboardData(data->GetFormat(), &bitmap); |
06e43511 | 581 | } |
26f86486 | 582 | |
06e43511 JS |
583 | #if wxUSE_METAFILE |
584 | case wxDF_METAFILE: | |
585 | { | |
26f86486 VZ |
586 | wxMetafileDataObject* metaFileDataObject = |
587 | (wxMetafileDataObject*) data; | |
06e43511 | 588 | wxMetafile metaFile = metaFileDataObject->GetMetafile(); |
26f86486 VZ |
589 | return wxSetClipboardData(wxDF_METAFILE, &metaFile, |
590 | metaFileDataObject->GetWidth(), | |
591 | metaFileDataObject->GetHeight()); | |
06e43511 | 592 | } |
26f86486 VZ |
593 | #endif // wxUSE_METAFILE |
594 | ||
06e43511 | 595 | default: |
3f480da3 | 596 | return wxSetClipboardData(data); |
06e43511 | 597 | } |
d59ceba5 | 598 | #else // !wxUSE_DATAOBJ |
06e43511 | 599 | return FALSE; |
d59ceba5 | 600 | #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ |
2bda0e17 KB |
601 | } |
602 | ||
06e43511 | 603 | void wxClipboard::Close() |
2bda0e17 | 604 | { |
d59ceba5 VZ |
605 | // OLE closes clipboard for us |
606 | #if !wxUSE_OLE_CLIPBOARD | |
06e43511 | 607 | wxCloseClipboard(); |
d59ceba5 | 608 | #endif |
2bda0e17 KB |
609 | } |
610 | ||
26f86486 | 611 | bool wxClipboard::IsSupported( wxDataFormat format ) |
2bda0e17 | 612 | { |
06e43511 | 613 | return wxIsClipboardFormatAvailable(format); |
2bda0e17 KB |
614 | } |
615 | ||
1e8335b0 | 616 | bool wxClipboard::GetData( wxDataObject& data ) |
2bda0e17 | 617 | { |
d59ceba5 VZ |
618 | #if wxUSE_OLE_CLIPBOARD |
619 | IDataObject *pDataObject = NULL; | |
620 | HRESULT hr = OleGetClipboard(&pDataObject); | |
621 | if ( FAILED(hr) || !pDataObject ) | |
622 | { | |
623 | wxLogSysError(hr, _("Failed to get data from the clipboard")); | |
624 | ||
625 | return FALSE; | |
626 | } | |
627 | ||
628 | // build the list of supported formats | |
1e8335b0 | 629 | size_t nFormats = data.GetFormatCount(wxDataObject::Set); |
d59ceba5 VZ |
630 | wxDataFormat format, *formats; |
631 | if ( nFormats == 1 ) | |
632 | { | |
633 | // the most common case | |
634 | formats = &format; | |
635 | } | |
636 | else | |
637 | { | |
638 | // bad luck, need to alloc mem | |
639 | formats = new wxDataFormat[nFormats]; | |
640 | } | |
641 | ||
1e8335b0 | 642 | data.GetAllFormats(formats, wxDataObject::Set); |
d59ceba5 VZ |
643 | |
644 | // get the format enumerator | |
645 | bool result = FALSE; | |
646 | wxArrayInt supportedFormats; | |
647 | IEnumFORMATETC *pEnumFormatEtc = NULL; | |
648 | hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc); | |
649 | if ( FAILED(hr) || !pEnumFormatEtc ) | |
650 | { | |
651 | wxLogSysError(hr, | |
652 | _("Failed to retrieve the supported clipboard formats")); | |
653 | } | |
654 | else | |
655 | { | |
656 | // ask for the supported formats and see if there are any we support | |
657 | FORMATETC formatEtc; | |
658 | for ( ;; ) | |
659 | { | |
660 | ULONG nCount; | |
661 | hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount); | |
662 | ||
663 | // don't use FAILED() because S_FALSE would pass it | |
664 | if ( hr != S_OK ) | |
665 | { | |
666 | // no more formats | |
667 | break; | |
668 | } | |
669 | ||
670 | CLIPFORMAT cf = formatEtc.cfFormat; | |
671 | ||
672 | #ifdef __WXDEBUG__ | |
673 | wxLogTrace(wxTRACE_OleCalls, | |
674 | wxT("Object on the clipboard supports format %s."), | |
675 | wxDataObject::GetFormatName(cf)); | |
676 | #endif // Debug | |
677 | ||
678 | // is supported? | |
679 | for ( size_t n = 0; n < nFormats; n++ ) | |
680 | { | |
681 | if ( formats[n].GetFormatId() == cf ) | |
682 | { | |
683 | if ( supportedFormats.Index(cf) == wxNOT_FOUND ) | |
684 | { | |
685 | supportedFormats.Add(cf); | |
686 | } | |
687 | } | |
688 | } | |
689 | } | |
690 | ||
691 | pEnumFormatEtc->Release(); | |
692 | } | |
693 | ||
694 | if ( formats != &format ) | |
695 | { | |
696 | delete [] formats; | |
697 | } | |
698 | //else: we didn't allocate any memory | |
699 | ||
700 | if ( !supportedFormats.IsEmpty() ) | |
701 | { | |
702 | FORMATETC formatEtc; | |
703 | formatEtc.ptd = NULL; | |
704 | formatEtc.dwAspect = DVASPECT_CONTENT; | |
705 | formatEtc.lindex = -1; | |
d59ceba5 VZ |
706 | |
707 | size_t nSupportedFormats = supportedFormats.GetCount(); | |
708 | for ( size_t n = 0; !result && (n < nSupportedFormats); n++ ) | |
709 | { | |
710 | STGMEDIUM medium; | |
711 | formatEtc.cfFormat = supportedFormats[n]; | |
712 | ||
265b0c07 VZ |
713 | // use the appropriate tymed |
714 | switch ( formatEtc.cfFormat ) | |
715 | { | |
716 | case CF_BITMAP: | |
717 | formatEtc.tymed = TYMED_GDI; | |
718 | break; | |
719 | ||
720 | case CF_METAFILEPICT: | |
721 | formatEtc.tymed = TYMED_MFPICT; | |
722 | break; | |
723 | ||
d9317fd4 VZ |
724 | case CF_ENHMETAFILE: |
725 | formatEtc.tymed = TYMED_ENHMF; | |
726 | break; | |
727 | ||
265b0c07 VZ |
728 | default: |
729 | formatEtc.tymed = TYMED_HGLOBAL; | |
730 | } | |
731 | ||
d59ceba5 VZ |
732 | // try to get data |
733 | hr = pDataObject->GetData(&formatEtc, &medium); | |
734 | if ( FAILED(hr) ) | |
735 | { | |
736 | // try other tymed for GDI objects | |
737 | if ( formatEtc.cfFormat == CF_BITMAP ) | |
738 | { | |
739 | formatEtc.tymed = TYMED_HGLOBAL; | |
740 | hr = pDataObject->GetData(&formatEtc, &medium); | |
741 | } | |
742 | } | |
743 | ||
744 | if ( SUCCEEDED(hr) ) | |
745 | { | |
746 | // pass the data to the data object | |
1e8335b0 | 747 | hr = data.GetInterface()->SetData(&formatEtc, &medium, TRUE); |
d59ceba5 VZ |
748 | if ( FAILED(hr) ) |
749 | { | |
750 | wxLogDebug(wxT("Failed to set data in wxIDataObject")); | |
751 | ||
752 | // IDataObject only takes the ownership of data if it | |
753 | // successfully got it - which is not the case here | |
754 | ReleaseStgMedium(&medium); | |
755 | } | |
756 | else | |
757 | { | |
758 | result = TRUE; | |
759 | } | |
760 | } | |
761 | //else: unsupported tymed? | |
762 | } | |
763 | } | |
764 | //else: unsupported format | |
765 | ||
766 | // clean up and return | |
767 | pDataObject->Release(); | |
768 | ||
769 | return result; | |
770 | #elif wxUSE_DATAOBJ | |
223d09f6 | 771 | wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") ); |
26f86486 | 772 | |
1e8335b0 | 773 | wxDataFormat format = data.GetFormat(); |
26f86486 | 774 | switch ( format ) |
06e43511 JS |
775 | { |
776 | case wxDF_TEXT: | |
777 | case wxDF_OEMTEXT: | |
778 | { | |
1e8335b0 VZ |
779 | wxTextDataObject& textDataObject = (wxTextDataObject &)data; |
780 | char* s = (char*)wxGetClipboardData(format); | |
781 | if ( !s ) | |
06e43511 | 782 | return FALSE; |
1e8335b0 VZ |
783 | |
784 | textDataObject.SetText(s); | |
785 | delete [] s; | |
786 | ||
787 | return TRUE; | |
06e43511 | 788 | } |
26f86486 | 789 | |
06e43511 JS |
790 | case wxDF_BITMAP: |
791 | case wxDF_DIB: | |
792 | { | |
1e8335b0 | 793 | wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data; |
26f86486 | 794 | wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetFormat()); |
1e8335b0 | 795 | if ( !bitmap ) |
06e43511 | 796 | return FALSE; |
1e8335b0 VZ |
797 | |
798 | bitmapDataObject.SetBitmap(*bitmap); | |
799 | delete bitmap; | |
800 | ||
801 | return TRUE; | |
06e43511 JS |
802 | } |
803 | #if wxUSE_METAFILE | |
804 | case wxDF_METAFILE: | |
805 | { | |
1e8335b0 | 806 | wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data; |
26f86486 | 807 | wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE); |
1e8335b0 | 808 | if ( !metaFile ) |
06e43511 | 809 | return FALSE; |
3f480da3 | 810 | |
1e8335b0 VZ |
811 | metaFileDataObject.SetMetafile(*metaFile); |
812 | delete metaFile; | |
26f86486 | 813 | |
1e8335b0 VZ |
814 | return TRUE; |
815 | } | |
816 | #endif // wxUSE_METAFILE | |
06e43511 | 817 | } |
d59ceba5 | 818 | #else // !wxUSE_DATAOBJ |
1e8335b0 VZ |
819 | wxFAIL_MSG( wxT("no clipboard implementation") ); |
820 | #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ | |
821 | ||
06e43511 | 822 | return FALSE; |
2bda0e17 KB |
823 | } |
824 | ||
26f86486 VZ |
825 | #else |
826 | #error "Please turn wxUSE_CLIPBOARD on to compile this file." | |
47d67540 | 827 | #endif // wxUSE_CLIPBOARD |
4ce81a75 | 828 |