]> git.saurik.com Git - wxWidgets.git/blob - src/msw/clipbrd.cpp
Fix wxSTC compilation without wxUSE_DRAG_AND_DROP after r65827.
[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 #include "wx/dataobj.h"
41 #endif
42
43 #if wxUSE_METAFILE
44 #include "wx/metafile.h"
45 #endif
46
47
48 #include <string.h>
49
50 #include "wx/msw/private.h"
51 #include "wx/msw/ole/oleutils.h"
52
53 #if wxUSE_WXDIB
54 #include "wx/msw/dib.h"
55 #endif
56
57 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
58 // the functions using wxDataObject in wxClipboard
59 //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
60
61 #if wxUSE_OLE && !defined(__WXWINCE__)
62 // use OLE clipboard
63 #define wxUSE_OLE_CLIPBOARD 1
64 #else // !wxUSE_DATAOBJ
65 // use Win clipboard API
66 #define wxUSE_OLE_CLIPBOARD 0
67 #endif
68
69 #if wxUSE_OLE_CLIPBOARD
70 #include <ole2.h>
71 #endif // wxUSE_OLE_CLIPBOARD
72
73 // ===========================================================================
74 // implementation
75 // ===========================================================================
76
77 // ---------------------------------------------------------------------------
78 // old-style clipboard functions using Windows API
79 // ---------------------------------------------------------------------------
80
81 static bool gs_wxClipboardIsOpen = false;
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("Can not 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
144 if ( ::IsClipboardFormatAvailable(cf) )
145 {
146 // ok from the first try
147 return true;
148 }
149
150 // for several standard formats, we can convert from some other ones too
151 switch ( cf )
152 {
153 // for bitmaps, DIBs will also do
154 case CF_BITMAP:
155 return ::IsClipboardFormatAvailable(CF_DIB) != 0;
156
157 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
158 case CF_METAFILEPICT:
159 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0;
160 #endif // wxUSE_ENH_METAFILE
161
162 default:
163 return false;
164 }
165 }
166
167
168 bool wxSetClipboardData(wxDataFormat dataFormat,
169 const void *data,
170 int width, int height)
171 {
172 HANDLE handle = 0; // return value of SetClipboardData
173
174 switch (dataFormat)
175 {
176 case wxDF_BITMAP:
177 {
178 wxBitmap *bitmap = (wxBitmap *)data;
179
180 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
181 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
182 HBITMAP old = (HBITMAP)
183 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
184 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
185 bitmap->GetWidth(),
186 bitmap->GetHeight());
187 if (!hBitmap)
188 {
189 SelectObject(hdcSrc, old);
190 DeleteDC(hdcMem);
191 DeleteDC(hdcSrc);
192 return false;
193 }
194
195 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
196 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
197 hdcSrc, 0, 0, SRCCOPY);
198
199 // Select new bitmap out of memory DC
200 SelectObject(hdcMem, old1);
201
202 // Set the data
203 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
204
205 // Clean up
206 SelectObject(hdcSrc, old);
207 DeleteDC(hdcSrc);
208 DeleteDC(hdcMem);
209 break;
210 }
211
212 #if wxUSE_WXDIB
213 case wxDF_DIB:
214 {
215 wxBitmap *bitmap = (wxBitmap *)data;
216
217 if ( bitmap && bitmap->Ok() )
218 {
219 wxDIB dib(*bitmap);
220 if ( dib.IsOk() )
221 {
222 handle = ::SetClipboardData(CF_DIB, dib.Detach());
223 }
224 }
225 break;
226 }
227 #endif
228
229 // VZ: I'm told that this code works, but it doesn't seem to work for me
230 // and, anyhow, I'd be highly surprised if it did. So I leave it here
231 // but IMNSHO it is completely broken.
232 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
233 case wxDF_METAFILE:
234 {
235 wxMetafile *wxMF = (wxMetafile *)data;
236 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
237 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
238
239 mf->mm = wxMF->GetWindowsMappingMode();
240 mf->xExt = width;
241 mf->yExt = height;
242 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
243 GlobalUnlock(data);
244 wxMF->SetHMETAFILE((WXHANDLE) NULL);
245
246 handle = SetClipboardData(CF_METAFILEPICT, data);
247 break;
248 }
249 #endif // wxUSE_METAFILE
250
251 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
252 case wxDF_ENHMETAFILE:
253 {
254 wxEnhMetaFile *emf = (wxEnhMetaFile *)data;
255 wxEnhMetaFile emfCopy = *emf;
256
257 handle = SetClipboardData(CF_ENHMETAFILE,
258 (void *)emfCopy.GetHENHMETAFILE());
259 }
260 break;
261 #endif // wxUSE_ENH_METAFILE
262
263 case CF_SYLK:
264 case CF_DIF:
265 case CF_TIFF:
266 case CF_PALETTE:
267 default:
268 {
269 wxLogError(_("Unsupported clipboard format."));
270 return false;
271 }
272
273 case wxDF_OEMTEXT:
274 dataFormat = wxDF_TEXT;
275 // fall through
276
277 case wxDF_TEXT:
278 {
279 char *s = (char *)data;
280
281 width = strlen(s) + 1;
282 height = 1;
283 DWORD l = (width * height);
284 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
285 if ( hGlobalMemory )
286 {
287 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
288
289 memcpy(lpGlobalMemory, s, l);
290
291 GlobalUnlock(hGlobalMemory);
292 }
293
294 handle = SetClipboardData(dataFormat, hGlobalMemory);
295 break;
296 }
297 // Only tested with Visual C++ 6.0 so far
298 #if defined(__VISUALC__)
299 case wxDF_HTML:
300 {
301 char* html = (char *)data;
302
303 // Create temporary buffer for HTML header...
304 char *buf = new char [400 + strlen(html)];
305 if(!buf) return false;
306
307 // Get clipboard id for HTML format...
308 static int cfid = 0;
309 if(!cfid) cfid = RegisterClipboardFormat(wxT("HTML Format"));
310
311 // Create a template string for the HTML header...
312 strcpy(buf,
313 "Version:0.9\r\n"
314 "StartHTML:00000000\r\n"
315 "EndHTML:00000000\r\n"
316 "StartFragment:00000000\r\n"
317 "EndFragment:00000000\r\n"
318 "<html><body>\r\n"
319 "<!--StartFragment -->\r\n");
320
321 // Append the HTML...
322 strcat(buf, html);
323 strcat(buf, "\r\n");
324 // Finish up the HTML format...
325 strcat(buf,
326 "<!--EndFragment-->\r\n"
327 "</body>\r\n"
328 "</html>");
329
330 // Now go back, calculate all the lengths, and write out the
331 // necessary header information. Note, wsprintf() truncates the
332 // string when you overwrite it so you follow up with code to replace
333 // the 0 appended at the end with a '\r'...
334 char *ptr = strstr(buf, "StartHTML");
335 sprintf(ptr+10, "%08u", strstr(buf, "<html>") - buf);
336 *(ptr+10+8) = '\r';
337
338 ptr = strstr(buf, "EndHTML");
339 sprintf(ptr+8, "%08u", strlen(buf));
340 *(ptr+8+8) = '\r';
341
342 ptr = strstr(buf, "StartFragment");
343 sprintf(ptr+14, "%08u", strstr(buf, "<!--StartFrag") - buf);
344 *(ptr+14+8) = '\r';
345
346 ptr = strstr(buf, "EndFragment");
347 sprintf(ptr+12, "%08u", strstr(buf, "<!--EndFrag") - buf);
348 *(ptr+12+8) = '\r';
349
350 // Now you have everything in place ready to put on the
351 // clipboard.
352
353 // Allocate global memory for transfer...
354 HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(buf)+4);
355
356 // Put your string in the global memory...
357 ptr = (char *)GlobalLock(hText);
358 strcpy(ptr, buf);
359 GlobalUnlock(hText);
360
361 handle = ::SetClipboardData(cfid, hText);
362
363 // Free memory...
364 GlobalFree(hText);
365
366 // Clean up...
367 delete [] buf;
368 break;
369 }
370 #endif
371 }
372
373 if ( handle == 0 )
374 {
375 wxLogSysError(_("Failed to set clipboard data."));
376
377 return false;
378 }
379
380 return true;
381 }
382
383 void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
384 {
385 void *retval = NULL;
386
387 switch ( dataFormat )
388 {
389 #ifndef __WXWINCE__
390 case wxDF_BITMAP:
391 {
392 BITMAP bm;
393 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
394 if (!hBitmap)
395 break;
396
397 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
398 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
399
400 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
401 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
402
403 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
404
405 if (!hNewBitmap)
406 {
407 SelectObject(hdcSrc, old);
408 DeleteDC(hdcMem);
409 DeleteDC(hdcSrc);
410 break;
411 }
412
413 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
414 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
415 hdcSrc, 0, 0, SRCCOPY);
416
417 // Select new bitmap out of memory DC
418 SelectObject(hdcMem, old1);
419
420 // Clean up
421 SelectObject(hdcSrc, old);
422 DeleteDC(hdcSrc);
423 DeleteDC(hdcMem);
424
425 // Create and return a new wxBitmap
426 wxBitmap *wxBM = new wxBitmap;
427 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
428 wxBM->SetWidth(bm.bmWidth);
429 wxBM->SetHeight(bm.bmHeight);
430 wxBM->SetDepth(bm.bmPlanes);
431 retval = wxBM;
432 break;
433 }
434 #endif
435 case wxDF_METAFILE:
436 case CF_SYLK:
437 case CF_DIF:
438 case CF_TIFF:
439 case CF_PALETTE:
440 case wxDF_DIB:
441 wxLogError(_("Unsupported clipboard format."));
442 return NULL;
443
444 case wxDF_OEMTEXT:
445 dataFormat = wxDF_TEXT;
446 // fall through
447
448 case wxDF_TEXT:
449 {
450 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
451 if (!hGlobalMemory)
452 break;
453
454 DWORD hsize = ::GlobalSize(hGlobalMemory);
455 if (len)
456 *len = hsize;
457
458 char *s = new char[hsize];
459 if (!s)
460 break;
461
462 LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
463
464 memcpy(s, lpGlobalMemory, hsize);
465
466 GlobalUnlock(hGlobalMemory);
467
468 retval = s;
469 break;
470 }
471
472 default:
473 {
474 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
475 if ( !hGlobalMemory )
476 break;
477
478 DWORD size = ::GlobalSize(hGlobalMemory);
479 if ( len )
480 *len = size;
481
482 void *buf = malloc(size);
483 if ( !buf )
484 break;
485
486 LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
487
488 memcpy(buf, lpGlobalMemory, size);
489
490 GlobalUnlock(hGlobalMemory);
491
492 retval = buf;
493 break;
494 }
495 }
496
497 if ( !retval )
498 {
499 wxLogSysError(_("Failed to retrieve data from the clipboard."));
500 }
501
502 return retval;
503 }
504
505 wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
506 {
507 return (wxDataFormat::NativeFormat)::EnumClipboardFormats(dataFormat);
508 }
509
510 int wxRegisterClipboardFormat(wxChar *formatName)
511 {
512 return ::RegisterClipboardFormat(formatName);
513 }
514
515 bool wxGetClipboardFormatName(wxDataFormat dataFormat,
516 wxChar *formatName,
517 int maxCount)
518 {
519 return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
520 }
521
522 // ---------------------------------------------------------------------------
523 // wxClipboard
524 // ---------------------------------------------------------------------------
525
526 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
527
528 wxClipboard::wxClipboard()
529 {
530 #if wxUSE_OLE_CLIPBOARD
531 wxOleInitialize();
532 #endif
533
534 m_lastDataObject = NULL;
535 m_isOpened = false;
536 }
537
538 wxClipboard::~wxClipboard()
539 {
540 if ( m_lastDataObject )
541 {
542 Clear();
543 }
544
545 #if wxUSE_OLE_CLIPBOARD
546 wxOleUninitialize();
547 #endif
548 }
549
550 void wxClipboard::Clear()
551 {
552 if ( IsUsingPrimarySelection() )
553 return;
554
555 #if wxUSE_OLE_CLIPBOARD
556 if (m_lastDataObject)
557 {
558 // don't touch data set by other applications
559 HRESULT hr = OleIsCurrentClipboard(m_lastDataObject);
560 if (S_OK == hr)
561 {
562 hr = OleSetClipboard(NULL);
563 if ( FAILED(hr) )
564 {
565 wxLogApiError(wxT("OleSetClipboard(NULL)"), hr);
566 }
567 }
568 m_lastDataObject = NULL;
569 }
570 #endif // wxUSE_OLE_CLIPBOARD
571 }
572
573 bool wxClipboard::Flush()
574 {
575 #if wxUSE_OLE_CLIPBOARD
576 if (m_lastDataObject)
577 {
578 // don't touch data set by other applications
579 HRESULT hr = OleIsCurrentClipboard(m_lastDataObject);
580 m_lastDataObject = NULL;
581 if (S_OK == hr)
582 {
583 hr = OleFlushClipboard();
584 if ( FAILED(hr) )
585 {
586 wxLogApiError(wxT("OleFlushClipboard"), hr);
587
588 return false;
589 }
590 return true;
591 }
592 }
593 return false;
594 #else // !wxUSE_OLE_CLIPBOARD
595 return false;
596 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
597 }
598
599 bool wxClipboard::Open()
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
610 bool wxClipboard::IsOpened() const
611 {
612 #if wxUSE_OLE_CLIPBOARD
613 return m_isOpened;
614 #else
615 return wxIsClipboardOpened();
616 #endif
617 }
618
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
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
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
730 bool wxClipboard::IsSupported( const wxDataFormat& format )
731 {
732 return !IsUsingPrimarySelection() && wxIsClipboardFormatAvailable(format);
733 }
734
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 the format is not available, try the next one
818 // this test includes implicit / sythetic formats
819 if ( !::IsClipboardFormatAvailable(cf) )
820 continue;
821
822 formatEtc.cfFormat = cf;
823 formatEtc.ptd = NULL;
824 formatEtc.dwAspect = DVASPECT_CONTENT;
825 formatEtc.lindex = -1;
826
827 // use the appropriate tymed
828 switch ( formatEtc.cfFormat )
829 {
830 case CF_BITMAP:
831 formatEtc.tymed = TYMED_GDI;
832 break;
833
834 #ifndef __WXWINCE__
835 case CF_METAFILEPICT:
836 formatEtc.tymed = TYMED_MFPICT;
837 break;
838
839 case CF_ENHMETAFILE:
840 formatEtc.tymed = TYMED_ENHMF;
841 break;
842 #endif
843
844 default:
845 formatEtc.tymed = TYMED_HGLOBAL;
846 }
847
848 // try to get data
849 hr = pDataObject->GetData(&formatEtc, &medium);
850 if ( FAILED(hr) )
851 {
852 // try other tymed for GDI objects
853 if ( formatEtc.cfFormat == CF_BITMAP )
854 {
855 formatEtc.tymed = TYMED_HGLOBAL;
856 hr = pDataObject->GetData(&formatEtc, &medium);
857 }
858 }
859
860 if ( SUCCEEDED(hr) )
861 {
862 // pass the data to the data object
863 hr = data.GetInterface()->SetData(&formatEtc, &medium, true);
864 if ( FAILED(hr) )
865 {
866 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
867
868 // IDataObject only takes the ownership of data if it
869 // successfully got it - which is not the case here
870 ReleaseStgMedium(&medium);
871 }
872 else
873 {
874 result = true;
875 }
876 }
877 //else: unsupported tymed?
878 }
879
880 if ( formats != &format )
881 {
882 delete [] formats;
883 }
884 //else: we didn't allocate any memory
885
886 // clean up and return
887 pDataObject->Release();
888
889 return result;
890 #elif wxUSE_DATAOBJ
891 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
892
893 wxDataFormat format = data.GetPreferredFormat();
894 switch ( format )
895 {
896 case wxDF_TEXT:
897 case wxDF_OEMTEXT:
898 {
899 wxTextDataObject& textDataObject = (wxTextDataObject &)data;
900 char* s = (char*)wxGetClipboardData(format);
901 if ( !s )
902 return false;
903
904 textDataObject.SetText(wxString::FromAscii(s));
905 delete [] s;
906
907 return true;
908 }
909
910 case wxDF_BITMAP:
911 case wxDF_DIB:
912 {
913 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
914 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat());
915 if ( !bitmap )
916 return false;
917
918 bitmapDataObject.SetBitmap(*bitmap);
919 delete bitmap;
920
921 return true;
922 }
923 #if wxUSE_METAFILE
924 case wxDF_METAFILE:
925 {
926 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
927 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
928 if ( !metaFile )
929 return false;
930
931 metaFileDataObject.SetMetafile(*metaFile);
932 delete metaFile;
933
934 return true;
935 }
936 #endif // wxUSE_METAFILE
937 }
938 return false;
939 #else // !wxUSE_DATAOBJ
940 wxFAIL_MSG( wxT("no clipboard implementation") );
941 return false;
942 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
943 }
944
945 #endif // wxUSE_CLIPBOARD