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