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