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