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