More WinCE mods
[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 #ifdef __GNUG__
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 #include <windows.h>
56
57 #include "wx/msw/private.h"
58
59 #if wxUSE_WXDIB
60 #include "wx/msw/dib.h"
61 #endif
62
63 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
64 // the functions using wxDataObject in wxClipboard
65 //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
66
67 #if wxUSE_DATAOBJ
68 #include "wx/dataobj.h"
69 #endif
70
71 #if wxUSE_OLE
72 // use OLE clipboard
73 #define wxUSE_OLE_CLIPBOARD 1
74 #else // !wxUSE_DATAOBJ
75 // use Win clipboard API
76 #define wxUSE_OLE_CLIPBOARD 0
77 #endif
78
79 #if wxUSE_OLE_CLIPBOARD
80 #include <ole2.h>
81 #endif // wxUSE_OLE_CLIPBOARD
82
83 #ifdef __WIN16__
84 #define memcpy hmemcpy
85 #endif
86
87 // ===========================================================================
88 // implementation
89 // ===========================================================================
90
91 // ---------------------------------------------------------------------------
92 // old-style clipboard functions using Windows API
93 // ---------------------------------------------------------------------------
94
95 static bool gs_wxClipboardIsOpen = FALSE;
96
97 bool wxOpenClipboard()
98 {
99 wxCHECK_MSG( !gs_wxClipboardIsOpen, TRUE, wxT("clipboard already opened.") );
100
101 wxWindow *win = wxTheApp->GetTopWindow();
102 if ( win )
103 {
104 gs_wxClipboardIsOpen = ::OpenClipboard((HWND)win->GetHWND()) != 0;
105
106 if ( !gs_wxClipboardIsOpen )
107 wxLogSysError(_("Failed to open the clipboard."));
108
109 return gs_wxClipboardIsOpen;
110 }
111 else
112 {
113 wxLogDebug(wxT("Can not open clipboard without a main window."));
114
115 return FALSE;
116 }
117 }
118
119 bool wxCloseClipboard()
120 {
121 wxCHECK_MSG( gs_wxClipboardIsOpen, FALSE, wxT("clipboard is not opened") );
122
123 gs_wxClipboardIsOpen = FALSE;
124
125 if ( ::CloseClipboard() == 0 )
126 {
127 wxLogSysError(_("Failed to close the clipboard."));
128
129 return FALSE;
130 }
131
132 return TRUE;
133 }
134
135 bool wxEmptyClipboard()
136 {
137 if ( ::EmptyClipboard() == 0 )
138 {
139 wxLogSysError(_("Failed to empty the clipboard."));
140
141 return FALSE;
142 }
143
144 return TRUE;
145 }
146
147 bool wxIsClipboardOpened()
148 {
149 return gs_wxClipboardIsOpen;
150 }
151
152 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
153 {
154 if ( ::IsClipboardFormatAvailable(dataFormat) )
155 {
156 // ok from the first try
157 return TRUE;
158 }
159
160 // for several standard formats, we can convert from some other ones too
161 switch ( dataFormat.GetFormatId() )
162 {
163 // for bitmaps, DIBs will also do
164 case CF_BITMAP:
165 return ::IsClipboardFormatAvailable(CF_DIB) != 0;
166
167 #if wxUSE_ENH_METAFILE && !defined(__WIN16__) && !defined(__WXWINCE__)
168 case CF_METAFILEPICT:
169 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0;
170 #endif // wxUSE_ENH_METAFILE
171
172 default:
173 return FALSE;
174 }
175 }
176
177
178 bool wxSetClipboardData(wxDataFormat dataFormat,
179 const void *data,
180 int width, int height)
181 {
182 HANDLE handle = 0; // return value of SetClipboardData
183
184 switch (dataFormat)
185 {
186 case wxDF_BITMAP:
187 {
188 wxBitmap *bitmap = (wxBitmap *)data;
189
190 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
191 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
192 HBITMAP old = (HBITMAP)
193 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
194 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
195 bitmap->GetWidth(),
196 bitmap->GetHeight());
197 if (!hBitmap)
198 {
199 SelectObject(hdcSrc, old);
200 DeleteDC(hdcMem);
201 DeleteDC(hdcSrc);
202 return FALSE;
203 }
204
205 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
206 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
207 hdcSrc, 0, 0, SRCCOPY);
208
209 // Select new bitmap out of memory DC
210 SelectObject(hdcMem, old1);
211
212 // Set the data
213 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
214
215 // Clean up
216 SelectObject(hdcSrc, old);
217 DeleteDC(hdcSrc);
218 DeleteDC(hdcMem);
219 break;
220 }
221
222 #if wxUSE_WXDIB
223 case wxDF_DIB:
224 {
225 wxBitmap *bitmap = (wxBitmap *)data;
226
227 HGLOBAL hDIB = wxDIB::ConvertFromBitmap(GetHbitmapOf(*bitmap));
228 if ( hDIB )
229 {
230 handle = ::SetClipboardData(CF_DIB, hDIB);
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(__WIN16__) && !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 #if WXWIN_COMPATIBILITY_2
439 wxBM->SetOk(TRUE);
440 #endif // WXWIN_COMPATIBILITY_2
441 retval = wxBM;
442 break;
443 }
444 #endif
445 case wxDF_METAFILE:
446 case CF_SYLK:
447 case CF_DIF:
448 case CF_TIFF:
449 case CF_PALETTE:
450 case wxDF_DIB:
451 wxLogError(_("Unsupported clipboard format."));
452 return NULL;
453
454 case wxDF_OEMTEXT:
455 dataFormat = wxDF_TEXT;
456 // fall through
457
458 case wxDF_TEXT:
459 {
460 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
461 if (!hGlobalMemory)
462 break;
463
464 DWORD hsize = ::GlobalSize(hGlobalMemory);
465 if (len)
466 *len = hsize;
467
468 char *s = new char[hsize];
469 if (!s)
470 break;
471
472 LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
473
474 memcpy(s, lpGlobalMemory, hsize);
475
476 GlobalUnlock(hGlobalMemory);
477
478 retval = s;
479 break;
480 }
481
482 default:
483 {
484 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
485 if ( !hGlobalMemory )
486 break;
487
488 DWORD size = ::GlobalSize(hGlobalMemory);
489 if ( len )
490 *len = size;
491
492 void *buf = malloc(size);
493 if ( !buf )
494 break;
495
496 LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
497
498 memcpy(buf, lpGlobalMemory, size);
499
500 GlobalUnlock(hGlobalMemory);
501
502 retval = buf;
503 break;
504 }
505 }
506
507 if ( !retval )
508 {
509 wxLogSysError(_("Failed to retrieve data from the clipboard."));
510 }
511
512 return retval;
513 }
514
515 wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
516 {
517 return (wxDataFormat::NativeFormat)::EnumClipboardFormats(dataFormat);
518 }
519
520 int wxRegisterClipboardFormat(wxChar *formatName)
521 {
522 return ::RegisterClipboardFormat(formatName);
523 }
524
525 bool wxGetClipboardFormatName(wxDataFormat dataFormat,
526 wxChar *formatName,
527 int maxCount)
528 {
529 return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
530 }
531
532 // ---------------------------------------------------------------------------
533 // wxClipboard
534 // ---------------------------------------------------------------------------
535
536 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
537
538 wxClipboard::wxClipboard()
539 {
540 m_clearOnExit = FALSE;
541 m_isOpened = FALSE;
542 }
543
544 wxClipboard::~wxClipboard()
545 {
546 if ( m_clearOnExit )
547 {
548 Clear();
549 }
550 }
551
552 void wxClipboard::Clear()
553 {
554 #if wxUSE_OLE_CLIPBOARD
555 if ( FAILED(OleSetClipboard(NULL)) )
556 {
557 wxLogLastError(wxT("OleSetClipboard(NULL)"));
558 }
559 #endif
560 }
561
562 bool wxClipboard::Flush()
563 {
564 #if wxUSE_OLE_CLIPBOARD
565 if ( FAILED(OleFlushClipboard()) )
566 {
567 wxLogLastError(wxT("OleFlushClipboard"));
568
569 return FALSE;
570 }
571 else
572 {
573 m_clearOnExit = FALSE;
574
575 return TRUE;
576 }
577 #else // !wxUSE_OLE_CLIPBOARD
578 return FALSE;
579 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
580 }
581
582 bool wxClipboard::Open()
583 {
584 // OLE opens clipboard for us
585 m_isOpened = TRUE;
586 #if wxUSE_OLE_CLIPBOARD
587 return TRUE;
588 #else
589 return wxOpenClipboard();
590 #endif
591 }
592
593 bool wxClipboard::IsOpened() const
594 {
595 #if wxUSE_OLE_CLIPBOARD
596 return m_isOpened;
597 #else
598 return wxIsClipboardOpened();
599 #endif
600 }
601
602 bool wxClipboard::SetData( wxDataObject *data )
603 {
604 #if !wxUSE_OLE_CLIPBOARD
605 (void)wxEmptyClipboard();
606 #endif // wxUSE_OLE_CLIPBOARD
607
608 if ( data )
609 return AddData(data);
610 else
611 return TRUE;
612 }
613
614 bool wxClipboard::AddData( wxDataObject *data )
615 {
616 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
617
618 #if wxUSE_OLE_CLIPBOARD
619 HRESULT hr = OleSetClipboard(data->GetInterface());
620 if ( FAILED(hr) )
621 {
622 wxLogSysError(hr, _("Failed to put data on the clipboard"));
623
624 // don't free anything in this case
625
626 return FALSE;
627 }
628
629 // we have a problem here because we should delete wxDataObject, but we
630 // can't do it because IDataObject which we just gave to the clipboard
631 // would try to use it when it will need the data. IDataObject is ref
632 // counted and so doesn't suffer from such problem, so we release it now
633 // and tell it to delete wxDataObject when it is deleted itself.
634 data->SetAutoDelete();
635
636 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
637 // using OLE clipboard when the app terminates - by default, we call
638 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
639 // wxClipboard::Flush() to chaneg this
640 m_clearOnExit = TRUE;
641
642 return TRUE;
643 #elif wxUSE_DATAOBJ
644 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
645
646 wxDataFormat format = data->GetPreferredFormat();
647
648 switch ( format )
649 {
650 case wxDF_TEXT:
651 case wxDF_OEMTEXT:
652 {
653 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
654 wxString str(textDataObject->GetText());
655 return wxSetClipboardData(format, str.c_str());
656 }
657
658 case wxDF_BITMAP:
659 case wxDF_DIB:
660 {
661 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
662 wxBitmap bitmap(bitmapDataObject->GetBitmap());
663 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
664 }
665
666 #if wxUSE_METAFILE
667 case wxDF_METAFILE:
668 {
669 #if 1
670 // TODO
671 wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values.");
672 return FALSE;
673 #else
674 wxMetafileDataObject* metaFileDataObject =
675 (wxMetafileDataObject*) data;
676 wxMetafile metaFile = metaFileDataObject->GetMetafile();
677 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
678 metaFileDataObject->GetWidth(),
679 metaFileDataObject->GetHeight());
680 #endif
681 }
682 #endif // wxUSE_METAFILE
683
684 default:
685 {
686 // This didn't compile, of course
687 // return wxSetClipboardData(data);
688 // TODO
689 wxLogError("Not implemented.");
690 return FALSE;
691 }
692 }
693 #else // !wxUSE_DATAOBJ
694 return FALSE;
695 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
696 }
697
698 void wxClipboard::Close()
699 {
700 m_isOpened = FALSE;
701 // OLE closes clipboard for us
702 #if !wxUSE_OLE_CLIPBOARD
703 wxCloseClipboard();
704 #endif
705 }
706
707 bool wxClipboard::IsSupported( wxDataFormat format )
708 {
709 return wxIsClipboardFormatAvailable(format);
710 }
711
712 bool wxClipboard::GetData( wxDataObject& data )
713 {
714 #if wxUSE_OLE_CLIPBOARD
715 IDataObject *pDataObject = NULL;
716 HRESULT hr = OleGetClipboard(&pDataObject);
717 if ( FAILED(hr) || !pDataObject )
718 {
719 wxLogSysError(hr, _("Failed to get data from the clipboard"));
720
721 return FALSE;
722 }
723
724 // build the list of supported formats
725 size_t nFormats = data.GetFormatCount(wxDataObject::Set);
726 wxDataFormat format;
727 wxDataFormat *formats;
728 if ( nFormats == 1 )
729 {
730 // the most common case
731 formats = &format;
732 }
733 else
734 {
735 // bad luck, need to alloc mem
736 formats = new wxDataFormat[nFormats];
737 }
738
739 data.GetAllFormats(formats, wxDataObject::Set);
740
741 // get the format enumerator
742 bool result = FALSE;
743 wxArrayInt supportedFormats;
744 IEnumFORMATETC *pEnumFormatEtc = NULL;
745 hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
746 if ( FAILED(hr) || !pEnumFormatEtc )
747 {
748 wxLogSysError(hr,
749 _("Failed to retrieve the supported clipboard formats"));
750 }
751 else
752 {
753 // ask for the supported formats and see if there are any we support
754 FORMATETC formatEtc;
755 for ( ;; )
756 {
757 ULONG nCount;
758 hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount);
759
760 // don't use FAILED() because S_FALSE would pass it
761 if ( hr != S_OK )
762 {
763 // no more formats
764 break;
765 }
766
767 CLIPFORMAT cf = formatEtc.cfFormat;
768
769 #ifdef __WXDEBUG__
770 wxLogTrace(wxTRACE_OleCalls,
771 wxT("Object on the clipboard supports format %s."),
772 wxDataObject::GetFormatName(cf));
773 #endif // Debug
774
775 // is supported?
776 for ( size_t n = 0; n < nFormats; n++ )
777 {
778 if ( formats[n].GetFormatId() == cf )
779 {
780 if ( supportedFormats.Index(cf) == wxNOT_FOUND )
781 {
782 supportedFormats.Add(cf);
783 }
784 }
785 }
786 }
787
788 pEnumFormatEtc->Release();
789 }
790
791 if ( formats != &format )
792 {
793 delete [] formats;
794 }
795 //else: we didn't allocate any memory
796
797 if ( !supportedFormats.IsEmpty() )
798 {
799 FORMATETC formatEtc;
800 formatEtc.ptd = NULL;
801 formatEtc.dwAspect = DVASPECT_CONTENT;
802 formatEtc.lindex = -1;
803
804 size_t nSupportedFormats = supportedFormats.GetCount();
805 for ( size_t n = 0; !result && (n < nSupportedFormats); n++ )
806 {
807 STGMEDIUM medium;
808 formatEtc.cfFormat = supportedFormats[n];
809
810 // use the appropriate tymed
811 switch ( formatEtc.cfFormat )
812 {
813 case CF_BITMAP:
814 formatEtc.tymed = TYMED_GDI;
815 break;
816 #ifndef __WXWINCE__
817 case CF_METAFILEPICT:
818 formatEtc.tymed = TYMED_MFPICT;
819 break;
820
821 case CF_ENHMETAFILE:
822 formatEtc.tymed = TYMED_ENHMF;
823 break;
824 #endif
825 default:
826 formatEtc.tymed = TYMED_HGLOBAL;
827 }
828
829 // try to get data
830 hr = pDataObject->GetData(&formatEtc, &medium);
831 if ( FAILED(hr) )
832 {
833 // try other tymed for GDI objects
834 if ( formatEtc.cfFormat == CF_BITMAP )
835 {
836 formatEtc.tymed = TYMED_HGLOBAL;
837 hr = pDataObject->GetData(&formatEtc, &medium);
838 }
839 }
840
841 if ( SUCCEEDED(hr) )
842 {
843 // pass the data to the data object
844 hr = data.GetInterface()->SetData(&formatEtc, &medium, TRUE);
845 if ( FAILED(hr) )
846 {
847 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
848
849 // IDataObject only takes the ownership of data if it
850 // successfully got it - which is not the case here
851 ReleaseStgMedium(&medium);
852 }
853 else
854 {
855 result = TRUE;
856 }
857 }
858 //else: unsupported tymed?
859 }
860 }
861 //else: unsupported format
862
863 // clean up and return
864 pDataObject->Release();
865
866 return result;
867 #elif wxUSE_DATAOBJ
868 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
869
870 wxDataFormat format = data.GetPreferredFormat();
871 switch ( format )
872 {
873 case wxDF_TEXT:
874 case wxDF_OEMTEXT:
875 {
876 wxTextDataObject& textDataObject = (wxTextDataObject &)data;
877 char* s = (char*)wxGetClipboardData(format);
878 if ( !s )
879 return FALSE;
880
881 textDataObject.SetText(s);
882 delete [] s;
883
884 return TRUE;
885 }
886
887 case wxDF_BITMAP:
888 case wxDF_DIB:
889 {
890 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
891 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat());
892 if ( !bitmap )
893 return FALSE;
894
895 bitmapDataObject.SetBitmap(*bitmap);
896 delete bitmap;
897
898 return TRUE;
899 }
900 #if wxUSE_METAFILE
901 case wxDF_METAFILE:
902 {
903 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
904 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
905 if ( !metaFile )
906 return FALSE;
907
908 metaFileDataObject.SetMetafile(*metaFile);
909 delete metaFile;
910
911 return TRUE;
912 }
913 #endif // wxUSE_METAFILE
914 }
915 return FALSE;
916 #else // !wxUSE_DATAOBJ
917 wxFAIL_MSG( wxT("no clipboard implementation") );
918 return FALSE;
919 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
920 }
921
922 #endif // wxUSE_CLIPBOARD
923