corrected wxLogErrors() in Clear() and Flush()
[wxWidgets.git] / src / msw / clipbrd.cpp
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
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "clipbrd.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/setup.h"
33 #endif
34
35 #if wxUSE_CLIPBOARD
36
37 #ifndef WX_PRECOMP
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"
44 #include "wx/intl.h"
45 #endif
46
47 #if wxUSE_METAFILE
48 #include "wx/metafile.h"
49 #endif
50
51 #include "wx/log.h"
52 #include "wx/clipbrd.h"
53
54 #include <string.h>
55
56 #include "wx/msw/private.h"
57
58 #if wxUSE_WXDIB
59 #include "wx/msw/dib.h"
60 #endif
61
62 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
63 // the functions using wxDataObject in wxClipboard
64 //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
65
66 #if wxUSE_DATAOBJ
67 #include "wx/dataobj.h"
68 #endif
69
70 #if wxUSE_OLE && !defined(__WXWINCE__)
71 // use OLE clipboard
72 #define wxUSE_OLE_CLIPBOARD 1
73 #else // !wxUSE_DATAOBJ
74 // use Win clipboard API
75 #define wxUSE_OLE_CLIPBOARD 0
76 #endif
77
78 #if wxUSE_OLE_CLIPBOARD
79 #include <ole2.h>
80 #endif // wxUSE_OLE_CLIPBOARD
81
82 // ===========================================================================
83 // implementation
84 // ===========================================================================
85
86 // ---------------------------------------------------------------------------
87 // old-style clipboard functions using Windows API
88 // ---------------------------------------------------------------------------
89
90 static bool gs_wxClipboardIsOpen = false;
91
92 bool wxOpenClipboard()
93 {
94 wxCHECK_MSG( !gs_wxClipboardIsOpen, true, wxT("clipboard already opened.") );
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 {
108 wxLogDebug(wxT("Can not open clipboard without a main window."));
109
110 return false;
111 }
112 }
113
114 bool wxCloseClipboard()
115 {
116 wxCHECK_MSG( gs_wxClipboardIsOpen, false, wxT("clipboard is not opened") );
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;
128 }
129
130 bool wxEmptyClipboard()
131 {
132 if ( ::EmptyClipboard() == 0 )
133 {
134 wxLogSysError(_("Failed to empty the clipboard."));
135
136 return false;
137 }
138
139 return true;
140 }
141
142 bool wxIsClipboardOpened()
143 {
144 return gs_wxClipboardIsOpen;
145 }
146
147 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
148 {
149 wxDataFormat::NativeFormat cf = dataFormat.GetFormatId();
150
151 if ( ::IsClipboardFormatAvailable(cf) )
152 {
153 // ok from the first try
154 return true;
155 }
156
157 // for several standard formats, we can convert from some other ones too
158 switch ( cf )
159 {
160 // for bitmaps, DIBs will also do
161 case CF_BITMAP:
162 return ::IsClipboardFormatAvailable(CF_DIB) != 0;
163
164 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
165 case CF_METAFILEPICT:
166 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0;
167 #endif // wxUSE_ENH_METAFILE
168
169 default:
170 return false;
171 }
172 }
173
174
175 bool wxSetClipboardData(wxDataFormat dataFormat,
176 const void *data,
177 int width, int height)
178 {
179 HANDLE handle = 0; // return value of SetClipboardData
180
181 switch (dataFormat)
182 {
183 case wxDF_BITMAP:
184 {
185 wxBitmap *bitmap = (wxBitmap *)data;
186
187 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
188 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
189 HBITMAP old = (HBITMAP)
190 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
191 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
192 bitmap->GetWidth(),
193 bitmap->GetHeight());
194 if (!hBitmap)
195 {
196 SelectObject(hdcSrc, old);
197 DeleteDC(hdcMem);
198 DeleteDC(hdcSrc);
199 return false;
200 }
201
202 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
203 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
204 hdcSrc, 0, 0, SRCCOPY);
205
206 // Select new bitmap out of memory DC
207 SelectObject(hdcMem, old1);
208
209 // Set the data
210 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
211
212 // Clean up
213 SelectObject(hdcSrc, old);
214 DeleteDC(hdcSrc);
215 DeleteDC(hdcMem);
216 break;
217 }
218
219 #if wxUSE_WXDIB
220 case wxDF_DIB:
221 {
222 wxBitmap *bitmap = (wxBitmap *)data;
223
224 if ( bitmap && bitmap->Ok() )
225 {
226 wxDIB dib(*bitmap);
227 if ( dib.IsOk() )
228 {
229 handle = ::SetClipboardData(CF_DIB, dib.Detach());
230 }
231 }
232 break;
233 }
234 #endif
235
236 // VZ: I'm told that this code works, but it doesn't seem to work for me
237 // and, anyhow, I'd be highly surprised if it did. So I leave it here
238 // but IMNSHO it is completely broken.
239 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
240 case wxDF_METAFILE:
241 {
242 wxMetafile *wxMF = (wxMetafile *)data;
243 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
244 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
245
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);
252
253 handle = SetClipboardData(CF_METAFILEPICT, data);
254 break;
255 }
256 #endif // wxUSE_METAFILE
257
258 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
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
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 }
279
280 case wxDF_OEMTEXT:
281 dataFormat = wxDF_TEXT;
282 // fall through
283
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 {
294 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
295
296 memcpy(lpGlobalMemory, s, l);
297
298 GlobalUnlock(hGlobalMemory);
299 }
300
301 handle = SetClipboardData(dataFormat, hGlobalMemory);
302 break;
303 }
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
378 }
379
380 if ( handle == 0 )
381 {
382 wxLogSysError(_("Failed to set clipboard data."));
383
384 return false;
385 }
386
387 return true;
388 }
389
390 void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
391 {
392 void *retval = NULL;
393
394 switch ( dataFormat )
395 {
396 #ifndef __WXWINCE__
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);
438 retval = wxBM;
439 break;
440 }
441 #endif
442 case wxDF_METAFILE:
443 case CF_SYLK:
444 case CF_DIF:
445 case CF_TIFF:
446 case CF_PALETTE:
447 case wxDF_DIB:
448 wxLogError(_("Unsupported clipboard format."));
449 return NULL;
450
451 case wxDF_OEMTEXT:
452 dataFormat = wxDF_TEXT;
453 // fall through
454
455 case wxDF_TEXT:
456 {
457 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
458 if (!hGlobalMemory)
459 break;
460
461 DWORD hsize = ::GlobalSize(hGlobalMemory);
462 if (len)
463 *len = hsize;
464
465 char *s = new char[hsize];
466 if (!s)
467 break;
468
469 LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
470
471 memcpy(s, lpGlobalMemory, hsize);
472
473 GlobalUnlock(hGlobalMemory);
474
475 retval = s;
476 break;
477 }
478
479 default:
480 {
481 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
482 if ( !hGlobalMemory )
483 break;
484
485 DWORD size = ::GlobalSize(hGlobalMemory);
486 if ( len )
487 *len = size;
488
489 void *buf = malloc(size);
490 if ( !buf )
491 break;
492
493 LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
494
495 memcpy(buf, lpGlobalMemory, size);
496
497 GlobalUnlock(hGlobalMemory);
498
499 retval = buf;
500 break;
501 }
502 }
503
504 if ( !retval )
505 {
506 wxLogSysError(_("Failed to retrieve data from the clipboard."));
507 }
508
509 return retval;
510 }
511
512 wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
513 {
514 return (wxDataFormat::NativeFormat)::EnumClipboardFormats(dataFormat);
515 }
516
517 int wxRegisterClipboardFormat(wxChar *formatName)
518 {
519 return ::RegisterClipboardFormat(formatName);
520 }
521
522 bool wxGetClipboardFormatName(wxDataFormat dataFormat,
523 wxChar *formatName,
524 int maxCount)
525 {
526 return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
527 }
528
529 // ---------------------------------------------------------------------------
530 // wxClipboard
531 // ---------------------------------------------------------------------------
532
533 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
534
535 wxClipboard::wxClipboard()
536 {
537 m_clearOnExit = false;
538 m_isOpened = false;
539 }
540
541 wxClipboard::~wxClipboard()
542 {
543 if ( m_clearOnExit )
544 {
545 Clear();
546 }
547 }
548
549 void wxClipboard::Clear()
550 {
551 #if wxUSE_OLE_CLIPBOARD
552 HRESULT hr = OleSetClipboard(NULL);
553 if ( FAILED(hr) )
554 {
555 wxLogApiError(wxT("OleSetClipboard(NULL)"), hr);
556 }
557 #endif // wxUSE_OLE_CLIPBOARD
558 }
559
560 bool wxClipboard::Flush()
561 {
562 #if wxUSE_OLE_CLIPBOARD
563 HRESULT hr = OleFlushClipboard();
564 if ( FAILED(hr) )
565 {
566 wxLogApiError(wxT("OleFlushClipboard"), hr);
567
568 return false;
569 }
570 else
571 {
572 m_clearOnExit = false;
573
574 return true;
575 }
576 #else // !wxUSE_OLE_CLIPBOARD
577 return false;
578 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
579 }
580
581 bool wxClipboard::Open()
582 {
583 // OLE opens clipboard for us
584 m_isOpened = true;
585 #if wxUSE_OLE_CLIPBOARD
586 return true;
587 #else
588 return wxOpenClipboard();
589 #endif
590 }
591
592 bool wxClipboard::IsOpened() const
593 {
594 #if wxUSE_OLE_CLIPBOARD
595 return m_isOpened;
596 #else
597 return wxIsClipboardOpened();
598 #endif
599 }
600
601 bool wxClipboard::SetData( wxDataObject *data )
602 {
603 #if !wxUSE_OLE_CLIPBOARD
604 (void)wxEmptyClipboard();
605 #endif // wxUSE_OLE_CLIPBOARD
606
607 if ( data )
608 return AddData(data);
609 else
610 return true;
611 }
612
613 bool wxClipboard::AddData( wxDataObject *data )
614 {
615 wxCHECK_MSG( data, false, wxT("data is invalid") );
616
617 #if wxUSE_OLE_CLIPBOARD
618 HRESULT hr = OleSetClipboard(data->GetInterface());
619 if ( FAILED(hr) )
620 {
621 wxLogSysError(hr, _("Failed to put data on the clipboard"));
622
623 // don't free anything in this case
624
625 return false;
626 }
627
628 // we have a problem here because we should delete wxDataObject, but we
629 // can't do it because IDataObject which we just gave to the clipboard
630 // would try to use it when it will need the data. IDataObject is ref
631 // counted and so doesn't suffer from such problem, so we release it now
632 // and tell it to delete wxDataObject when it is deleted itself.
633 data->SetAutoDelete();
634
635 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
636 // using OLE clipboard when the app terminates - by default, we call
637 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
638 // wxClipboard::Flush() to chaneg this
639 m_clearOnExit = true;
640
641 return true;
642 #elif wxUSE_DATAOBJ
643 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
644
645 wxDataFormat format = data->GetPreferredFormat();
646
647 switch ( format )
648 {
649 case wxDF_TEXT:
650 case wxDF_OEMTEXT:
651 {
652 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
653 wxString str(textDataObject->GetText());
654 return wxSetClipboardData(format, str.c_str());
655 }
656
657 case wxDF_BITMAP:
658 case wxDF_DIB:
659 {
660 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
661 wxBitmap bitmap(bitmapDataObject->GetBitmap());
662 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
663 }
664
665 #if wxUSE_METAFILE
666 case wxDF_METAFILE:
667 {
668 #if 1
669 // TODO
670 wxLogError(wxT("Not implemented because wxMetafileDataObject does not contain width and height values."));
671 return false;
672 #else
673 wxMetafileDataObject* metaFileDataObject =
674 (wxMetafileDataObject*) data;
675 wxMetafile metaFile = metaFileDataObject->GetMetafile();
676 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
677 metaFileDataObject->GetWidth(),
678 metaFileDataObject->GetHeight());
679 #endif
680 }
681 #endif // wxUSE_METAFILE
682
683 default:
684 {
685 // This didn't compile, of course
686 // return wxSetClipboardData(data);
687 // TODO
688 wxLogError(wxT("Not implemented."));
689 return false;
690 }
691 }
692 #else // !wxUSE_DATAOBJ
693 return false;
694 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
695 }
696
697 void wxClipboard::Close()
698 {
699 m_isOpened = false;
700 // OLE closes clipboard for us
701 #if !wxUSE_OLE_CLIPBOARD
702 wxCloseClipboard();
703 #endif
704 }
705
706 bool wxClipboard::IsSupported( const wxDataFormat& format )
707 {
708 return wxIsClipboardFormatAvailable(format);
709 }
710
711 bool wxClipboard::GetData( wxDataObject& data )
712 {
713 #if wxUSE_OLE_CLIPBOARD
714 IDataObject *pDataObject = NULL;
715 HRESULT hr = OleGetClipboard(&pDataObject);
716 if ( FAILED(hr) || !pDataObject )
717 {
718 wxLogSysError(hr, _("Failed to get data from the clipboard"));
719
720 return false;
721 }
722
723 // build the list of supported formats
724 size_t nFormats = data.GetFormatCount(wxDataObject::Set);
725 wxDataFormat format;
726 wxDataFormat *formats;
727 if ( nFormats == 1 )
728 {
729 // the most common case
730 formats = &format;
731 }
732 else
733 {
734 // bad luck, need to alloc mem
735 formats = new wxDataFormat[nFormats];
736 }
737
738 data.GetAllFormats(formats, wxDataObject::Set);
739
740 // get the data for the given formats
741 FORMATETC formatEtc;
742 CLIPFORMAT cf;
743 bool result = false;
744
745 // enumerate all explicit formats on the clipboard.
746 // note that this does not include implicit / synthetic (automatically
747 // converted) formats.
748 #ifdef __WXDEBUG__
749 // get the format enumerator
750 IEnumFORMATETC *pEnumFormatEtc = NULL;
751 hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
752 if ( FAILED(hr) || !pEnumFormatEtc )
753 {
754 wxLogSysError(hr,
755 _("Failed to retrieve the supported clipboard formats"));
756 }
757 else
758 {
759 // ask for the supported formats and see if there are any we support
760 for ( ;; )
761 {
762 ULONG nCount;
763 hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount);
764
765 // don't use FAILED() because S_FALSE would pass it
766 if ( hr != S_OK )
767 {
768 // no more formats
769 break;
770 }
771
772 cf = formatEtc.cfFormat;
773
774 wxLogTrace(wxTRACE_OleCalls,
775 wxT("Object on the clipboard supports format %s."),
776 wxDataObject::GetFormatName(cf));
777 }
778
779 pEnumFormatEtc->Release();
780 }
781 #endif // Debug
782
783 STGMEDIUM medium;
784 // stop at the first valid format found on the clipboard
785 for ( size_t n = 0; !result && (n < nFormats); n++ )
786 {
787 // convert to NativeFormat Id
788 cf = formats[n].GetFormatId();
789
790 // if the format is not available, try the next one
791 // this test includes implicit / sythetic formats
792 if ( !::IsClipboardFormatAvailable(cf) )
793 continue;
794
795 formatEtc.cfFormat = cf;
796 formatEtc.ptd = NULL;
797 formatEtc.dwAspect = DVASPECT_CONTENT;
798 formatEtc.lindex = -1;
799
800 // use the appropriate tymed
801 switch ( formatEtc.cfFormat )
802 {
803 case CF_BITMAP:
804 formatEtc.tymed = TYMED_GDI;
805 break;
806
807 #ifndef __WXWINCE__
808 case CF_METAFILEPICT:
809 formatEtc.tymed = TYMED_MFPICT;
810 break;
811
812 case CF_ENHMETAFILE:
813 formatEtc.tymed = TYMED_ENHMF;
814 break;
815 #endif
816
817 default:
818 formatEtc.tymed = TYMED_HGLOBAL;
819 }
820
821 // try to get data
822 hr = pDataObject->GetData(&formatEtc, &medium);
823 if ( FAILED(hr) )
824 {
825 // try other tymed for GDI objects
826 if ( formatEtc.cfFormat == CF_BITMAP )
827 {
828 formatEtc.tymed = TYMED_HGLOBAL;
829 hr = pDataObject->GetData(&formatEtc, &medium);
830 }
831 }
832
833 if ( SUCCEEDED(hr) )
834 {
835 // pass the data to the data object
836 hr = data.GetInterface()->SetData(&formatEtc, &medium, true);
837 if ( FAILED(hr) )
838 {
839 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
840
841 // IDataObject only takes the ownership of data if it
842 // successfully got it - which is not the case here
843 ReleaseStgMedium(&medium);
844 }
845 else
846 {
847 result = true;
848 }
849 }
850 //else: unsupported tymed?
851 }
852
853 if ( formats != &format )
854 {
855 delete [] formats;
856 }
857 //else: we didn't allocate any memory
858
859 // clean up and return
860 pDataObject->Release();
861
862 return result;
863 #elif wxUSE_DATAOBJ
864 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
865
866 wxDataFormat format = data.GetPreferredFormat();
867 switch ( format )
868 {
869 case wxDF_TEXT:
870 case wxDF_OEMTEXT:
871 {
872 wxTextDataObject& textDataObject = (wxTextDataObject &)data;
873 char* s = (char*)wxGetClipboardData(format);
874 if ( !s )
875 return false;
876
877 textDataObject.SetText(wxString::FromAscii(s));
878 delete [] s;
879
880 return true;
881 }
882
883 case wxDF_BITMAP:
884 case wxDF_DIB:
885 {
886 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
887 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat());
888 if ( !bitmap )
889 return false;
890
891 bitmapDataObject.SetBitmap(*bitmap);
892 delete bitmap;
893
894 return true;
895 }
896 #if wxUSE_METAFILE
897 case wxDF_METAFILE:
898 {
899 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
900 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
901 if ( !metaFile )
902 return false;
903
904 metaFileDataObject.SetMetafile(*metaFile);
905 delete metaFile;
906
907 return true;
908 }
909 #endif // wxUSE_METAFILE
910 }
911 return false;
912 #else // !wxUSE_DATAOBJ
913 wxFAIL_MSG( wxT("no clipboard implementation") );
914 return false;
915 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
916 }
917
918 #endif // wxUSE_CLIPBOARD
919