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
wxOpenClipboard()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
wxCloseClipboard()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
wxEmptyClipboard()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
wxIsClipboardOpened()135 bool wxIsClipboardOpened()
136 {
137 return gs_wxClipboardIsOpen;
138 }
139
wxIsClipboardFormatAvailable(wxDataFormat dataFormat)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
wxSetClipboardData(wxDataFormat dataFormat,const void * data,int width,int height)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
300 case wxDF_HTML:
301 {
302 char* html = (char *)data;
303
304 // Create temporary buffer for HTML header...
305 char *buf = new char [400 + strlen(html)];
306 if(!buf) return false;
307
308 // Create a template string for the HTML header...
309 strcpy(buf,
310 "Version:0.9\r\n"
311 "StartHTML:00000000\r\n"
312 "EndHTML:00000000\r\n"
313 "StartFragment:00000000\r\n"
314 "EndFragment:00000000\r\n"
315 "<html><body>\r\n"
316 "<!--StartFragment -->\r\n");
317
318 // Append the HTML...
319 strcat(buf, html);
320 strcat(buf, "\r\n");
321 // Finish up the HTML format...
322 strcat(buf,
323 "<!--EndFragment-->\r\n"
324 "</body>\r\n"
325 "</html>");
326
327 // Now go back, calculate all the lengths, and write out the
328 // necessary header information. Note, wsprintf() truncates the
329 // string when you overwrite it so you follow up with code to replace
330 // the 0 appended at the end with a '\r'...
331 char *ptr = strstr(buf, "StartHTML");
332 sprintf(ptr+10, "%08u", (unsigned)(strstr(buf, "<html>") - buf));
333 *(ptr+10+8) = '\r';
334
335 ptr = strstr(buf, "EndHTML");
336 sprintf(ptr+8, "%08u", (unsigned)strlen(buf));
337 *(ptr+8+8) = '\r';
338
339 ptr = strstr(buf, "StartFragment");
340 sprintf(ptr+14, "%08u", (unsigned)(strstr(buf, "<!--StartFrag") - buf));
341 *(ptr+14+8) = '\r';
342
343 ptr = strstr(buf, "EndFragment");
344 sprintf(ptr+12, "%08u", (unsigned)(strstr(buf, "<!--EndFrag") - buf));
345 *(ptr+12+8) = '\r';
346
347 // Now you have everything in place ready to put on the
348 // clipboard.
349
350 // Allocate global memory for transfer...
351 HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(buf)+4);
352
353 // Put your string in the global memory...
354 ptr = (char *)GlobalLock(hText);
355 strcpy(ptr, buf);
356 GlobalUnlock(hText);
357
358 handle = ::SetClipboardData(gs_htmlcfid, hText);
359
360 // Free memory...
361 GlobalFree(hText);
362
363 // Clean up...
364 delete [] buf;
365 break;
366 }
367 }
368
369 if ( handle == 0 )
370 {
371 wxLogSysError(_("Failed to set clipboard data."));
372
373 return false;
374 }
375
376 return true;
377 }
378
wxGetClipboardData(wxDataFormat dataFormat,long * len)379 void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
380 {
381 void *retval = NULL;
382
383 switch ( dataFormat )
384 {
385 #ifndef __WXWINCE__
386 case wxDF_BITMAP:
387 {
388 BITMAP bm;
389 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
390 if (!hBitmap)
391 break;
392
393 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
394 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
395
396 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
397 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
398
399 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
400
401 if (!hNewBitmap)
402 {
403 SelectObject(hdcSrc, old);
404 DeleteDC(hdcMem);
405 DeleteDC(hdcSrc);
406 break;
407 }
408
409 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
410 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
411 hdcSrc, 0, 0, SRCCOPY);
412
413 // Select new bitmap out of memory DC
414 SelectObject(hdcMem, old1);
415
416 // Clean up
417 SelectObject(hdcSrc, old);
418 DeleteDC(hdcSrc);
419 DeleteDC(hdcMem);
420
421 // Create and return a new wxBitmap
422 wxBitmap *wxBM = new wxBitmap;
423 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
424 wxBM->SetWidth(bm.bmWidth);
425 wxBM->SetHeight(bm.bmHeight);
426 wxBM->SetDepth(bm.bmPlanes);
427 retval = wxBM;
428 break;
429 }
430 #endif
431 case wxDF_METAFILE:
432 case CF_SYLK:
433 case CF_DIF:
434 case CF_TIFF:
435 case CF_PALETTE:
436 case wxDF_DIB:
437 wxLogError(_("Unsupported clipboard format."));
438 return NULL;
439
440 case wxDF_OEMTEXT:
441 dataFormat = wxDF_TEXT;
442 // fall through
443
444 case wxDF_TEXT:
445 {
446 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
447 if (!hGlobalMemory)
448 break;
449
450 DWORD hsize = ::GlobalSize(hGlobalMemory);
451 if (len)
452 *len = hsize;
453
454 char *s = new char[hsize];
455 if (!s)
456 break;
457
458 LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
459
460 memcpy(s, lpGlobalMemory, hsize);
461
462 GlobalUnlock(hGlobalMemory);
463
464 retval = s;
465 break;
466 }
467
468 default:
469 {
470 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
471 if ( !hGlobalMemory )
472 break;
473
474 DWORD size = ::GlobalSize(hGlobalMemory);
475 if ( len )
476 *len = size;
477
478 void *buf = malloc(size);
479 if ( !buf )
480 break;
481
482 LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
483
484 memcpy(buf, lpGlobalMemory, size);
485
486 GlobalUnlock(hGlobalMemory);
487
488 retval = buf;
489 break;
490 }
491 }
492
493 if ( !retval )
494 {
495 wxLogSysError(_("Failed to retrieve data from the clipboard."));
496 }
497
498 return retval;
499 }
500
wxEnumClipboardFormats(wxDataFormat dataFormat)501 wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
502 {
503 return (wxDataFormat::NativeFormat)::EnumClipboardFormats(dataFormat);
504 }
505
wxRegisterClipboardFormat(wxChar * formatName)506 int wxRegisterClipboardFormat(wxChar *formatName)
507 {
508 return ::RegisterClipboardFormat(formatName);
509 }
510
wxGetClipboardFormatName(wxDataFormat dataFormat,wxChar * formatName,int maxCount)511 bool wxGetClipboardFormatName(wxDataFormat dataFormat,
512 wxChar *formatName,
513 int maxCount)
514 {
515 return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
516 }
517
518 // ---------------------------------------------------------------------------
519 // wxClipboard
520 // ---------------------------------------------------------------------------
521
IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)522 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
523
524 wxClipboard::wxClipboard()
525 {
526 #if wxUSE_OLE_CLIPBOARD
527 wxOleInitialize();
528 #endif
529
530 m_lastDataObject = NULL;
531 m_isOpened = false;
532 }
533
~wxClipboard()534 wxClipboard::~wxClipboard()
535 {
536 if ( m_lastDataObject )
537 {
538 Clear();
539 }
540
541 #if wxUSE_OLE_CLIPBOARD
542 wxOleUninitialize();
543 #endif
544 }
545
Clear()546 void wxClipboard::Clear()
547 {
548 if ( IsUsingPrimarySelection() )
549 return;
550
551 #if wxUSE_OLE_CLIPBOARD
552 if (m_lastDataObject)
553 {
554 // don't touch data set by other applications
555 HRESULT hr = OleIsCurrentClipboard(m_lastDataObject);
556 if (S_OK == hr)
557 {
558 hr = OleSetClipboard(NULL);
559 if ( FAILED(hr) )
560 {
561 wxLogApiError(wxT("OleSetClipboard(NULL)"), hr);
562 }
563 }
564 m_lastDataObject = NULL;
565 }
566 #endif // wxUSE_OLE_CLIPBOARD
567 }
568
Flush()569 bool wxClipboard::Flush()
570 {
571 #if wxUSE_OLE_CLIPBOARD
572 if (m_lastDataObject)
573 {
574 // don't touch data set by other applications
575 HRESULT hr = OleIsCurrentClipboard(m_lastDataObject);
576 m_lastDataObject = NULL;
577 if (S_OK == hr)
578 {
579 hr = OleFlushClipboard();
580 if ( FAILED(hr) )
581 {
582 wxLogApiError(wxT("OleFlushClipboard"), hr);
583
584 return false;
585 }
586 return true;
587 }
588 }
589 return false;
590 #else // !wxUSE_OLE_CLIPBOARD
591 return false;
592 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
593 }
594
Open()595 bool wxClipboard::Open()
596 {
597 // Get clipboard id for HTML format...
598 if(!gs_htmlcfid)
599 gs_htmlcfid = RegisterClipboardFormat(wxT("HTML Format"));
600
601 // OLE opens clipboard for us
602 m_isOpened = true;
603 #if wxUSE_OLE_CLIPBOARD
604 return true;
605 #else
606 return wxOpenClipboard();
607 #endif
608 }
609
IsOpened() const610 bool wxClipboard::IsOpened() const
611 {
612 #if wxUSE_OLE_CLIPBOARD
613 return m_isOpened;
614 #else
615 return wxIsClipboardOpened();
616 #endif
617 }
618
SetData(wxDataObject * data)619 bool wxClipboard::SetData( wxDataObject *data )
620 {
621 if ( IsUsingPrimarySelection() )
622 return false;
623
624 #if !wxUSE_OLE_CLIPBOARD
625 (void)wxEmptyClipboard();
626 #endif // wxUSE_OLE_CLIPBOARD
627
628 if ( data )
629 return AddData(data);
630 else
631 return true;
632 }
633
AddData(wxDataObject * data)634 bool wxClipboard::AddData( wxDataObject *data )
635 {
636 if ( IsUsingPrimarySelection() )
637 return false;
638
639 wxCHECK_MSG( data, false, wxT("data is invalid") );
640
641 #if wxUSE_OLE_CLIPBOARD
642 HRESULT hr = OleSetClipboard(data->GetInterface());
643 if ( FAILED(hr) )
644 {
645 wxLogSysError(hr, _("Failed to put data on the clipboard"));
646
647 // don't free anything in this case
648
649 return false;
650 }
651
652 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
653 // using OLE clipboard when the app terminates - by default, we call
654 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
655 // wxClipboard::Flush() to change this
656 m_lastDataObject = data->GetInterface();
657
658 // we have a problem here because we should delete wxDataObject, but we
659 // can't do it because IDataObject which we just gave to the clipboard
660 // would try to use it when it will need the data. IDataObject is ref
661 // counted and so doesn't suffer from such problem, so we release it now
662 // and tell it to delete wxDataObject when it is deleted itself.
663 data->SetAutoDelete();
664
665 return true;
666 #elif wxUSE_DATAOBJ
667 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
668
669 wxDataFormat format = data->GetPreferredFormat();
670
671 switch ( format )
672 {
673 case wxDF_TEXT:
674 case wxDF_OEMTEXT:
675 {
676 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
677 wxString str(textDataObject->GetText());
678 return wxSetClipboardData(format, str.c_str());
679 }
680
681 case wxDF_BITMAP:
682 case wxDF_DIB:
683 {
684 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
685 wxBitmap bitmap(bitmapDataObject->GetBitmap());
686 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
687 }
688
689 #if wxUSE_METAFILE
690 case wxDF_METAFILE:
691 {
692 #if 1
693 // TODO
694 wxLogError(wxT("Not implemented because wxMetafileDataObject does not contain width and height values."));
695 return false;
696 #else
697 wxMetafileDataObject* metaFileDataObject =
698 (wxMetafileDataObject*) data;
699 wxMetafile metaFile = metaFileDataObject->GetMetafile();
700 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
701 metaFileDataObject->GetWidth(),
702 metaFileDataObject->GetHeight());
703 #endif
704 }
705 #endif // wxUSE_METAFILE
706
707 default:
708 {
709 // This didn't compile, of course
710 // return wxSetClipboardData(data);
711 // TODO
712 wxLogError(wxT("Not implemented."));
713 return false;
714 }
715 }
716 #else // !wxUSE_DATAOBJ
717 return false;
718 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
719 }
720
Close()721 void wxClipboard::Close()
722 {
723 m_isOpened = false;
724 // OLE closes clipboard for us
725 #if !wxUSE_OLE_CLIPBOARD
726 wxCloseClipboard();
727 #endif
728 }
729
IsSupported(const wxDataFormat & format)730 bool wxClipboard::IsSupported( const wxDataFormat& format )
731 {
732 return !IsUsingPrimarySelection() && wxIsClipboardFormatAvailable(format);
733 }
734
GetData(wxDataObject & data)735 bool wxClipboard::GetData( wxDataObject& data )
736 {
737 if ( IsUsingPrimarySelection() )
738 return false;
739
740 #if wxUSE_OLE_CLIPBOARD
741 IDataObject *pDataObject = NULL;
742 HRESULT hr = OleGetClipboard(&pDataObject);
743 if ( FAILED(hr) || !pDataObject )
744 {
745 wxLogSysError(hr, _("Failed to get data from the clipboard"));
746
747 return false;
748 }
749
750 // build the list of supported formats
751 size_t nFormats = data.GetFormatCount(wxDataObject::Set);
752 wxDataFormat format;
753 wxDataFormat *formats;
754 if ( nFormats == 1 )
755 {
756 // the most common case
757 formats = &format;
758 }
759 else
760 {
761 // bad luck, need to alloc mem
762 formats = new wxDataFormat[nFormats];
763 }
764
765 data.GetAllFormats(formats, wxDataObject::Set);
766
767 // get the data for the given formats
768 FORMATETC formatEtc;
769 CLIPFORMAT cf;
770 bool result = false;
771
772 // enumerate all explicit formats on the clipboard.
773 // note that this does not include implicit / synthetic (automatically
774 // converted) formats.
775 #if wxDEBUG_LEVEL >= 2
776 // get the format enumerator
777 IEnumFORMATETC *pEnumFormatEtc = NULL;
778 hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
779 if ( FAILED(hr) || !pEnumFormatEtc )
780 {
781 wxLogSysError(hr,
782 _("Failed to retrieve the supported clipboard formats"));
783 }
784 else
785 {
786 // ask for the supported formats and see if there are any we support
787 for ( ;; )
788 {
789 ULONG nCount;
790 hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount);
791
792 // don't use FAILED() because S_FALSE would pass it
793 if ( hr != S_OK )
794 {
795 // no more formats
796 break;
797 }
798
799 cf = formatEtc.cfFormat;
800
801 wxLogTrace(wxTRACE_OleCalls,
802 wxT("Object on the clipboard supports format %s."),
803 wxDataObject::GetFormatName(cf));
804 }
805
806 pEnumFormatEtc->Release();
807 }
808 #endif // wxDEBUG_LEVEL >= 2
809
810 STGMEDIUM medium;
811 // stop at the first valid format found on the clipboard
812 for ( size_t n = 0; !result && (n < nFormats); n++ )
813 {
814 // convert to NativeFormat Id
815 cf = formats[n].GetFormatId();
816
817 if (cf == wxDF_HTML)
818 cf = gs_htmlcfid;
819 // if the format is not available, try the next one
820 // this test includes implicit / sythetic formats
821 if ( !::IsClipboardFormatAvailable(cf) )
822 continue;
823
824 formatEtc.cfFormat = cf;
825 formatEtc.ptd = NULL;
826 formatEtc.dwAspect = DVASPECT_CONTENT;
827 formatEtc.lindex = -1;
828
829 // use the appropriate tymed
830 switch ( formatEtc.cfFormat )
831 {
832 case CF_BITMAP:
833 formatEtc.tymed = TYMED_GDI;
834 break;
835
836 #ifndef __WXWINCE__
837 case CF_METAFILEPICT:
838 formatEtc.tymed = TYMED_MFPICT;
839 break;
840
841 case CF_ENHMETAFILE:
842 formatEtc.tymed = TYMED_ENHMF;
843 break;
844 #endif
845
846 default:
847 formatEtc.tymed = TYMED_HGLOBAL;
848 }
849
850 // try to get data
851 hr = pDataObject->GetData(&formatEtc, &medium);
852 if ( FAILED(hr) )
853 {
854 // try other tymed for GDI objects
855 if ( formatEtc.cfFormat == CF_BITMAP )
856 {
857 formatEtc.tymed = TYMED_HGLOBAL;
858 hr = pDataObject->GetData(&formatEtc, &medium);
859 }
860 }
861
862 if ( SUCCEEDED(hr) )
863 {
864 // pass the data to the data object
865 hr = data.GetInterface()->SetData(&formatEtc, &medium, true);
866 if ( FAILED(hr) )
867 {
868 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
869
870 // IDataObject only takes the ownership of data if it
871 // successfully got it - which is not the case here
872 ReleaseStgMedium(&medium);
873 }
874 else
875 {
876 result = true;
877 }
878 }
879 //else: unsupported tymed?
880 }
881
882 if ( formats != &format )
883 {
884 delete [] formats;
885 }
886 //else: we didn't allocate any memory
887
888 // clean up and return
889 pDataObject->Release();
890
891 return result;
892 #elif wxUSE_DATAOBJ
893 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
894
895 wxDataFormat format = data.GetPreferredFormat();
896 switch ( format )
897 {
898 case wxDF_TEXT:
899 case wxDF_OEMTEXT:
900 {
901 wxTextDataObject& textDataObject = (wxTextDataObject &)data;
902 char* s = (char*)wxGetClipboardData(format);
903 if ( !s )
904 return false;
905
906 textDataObject.SetText(wxString::FromAscii(s));
907 delete [] s;
908
909 return true;
910 }
911
912 case wxDF_BITMAP:
913 case wxDF_DIB:
914 {
915 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
916 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat());
917 if ( !bitmap )
918 return false;
919
920 bitmapDataObject.SetBitmap(*bitmap);
921 delete bitmap;
922
923 return true;
924 }
925 #if wxUSE_METAFILE
926 case wxDF_METAFILE:
927 {
928 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
929 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
930 if ( !metaFile )
931 return false;
932
933 metaFileDataObject.SetMetafile(*metaFile);
934 delete metaFile;
935
936 return true;
937 }
938 #endif // wxUSE_METAFILE
939 }
940 return false;
941 #else // !wxUSE_DATAOBJ
942 wxFAIL_MSG( wxT("no clipboard implementation") );
943 return false;
944 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
945 }
946
947 #endif // wxUSE_CLIPBOARD
948