]> git.saurik.com Git - wxWidgets.git/blob - src/msw/clipbrd.cpp
deTABified
[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 #ifndef __WXMICROWIN__
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__)
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 case wxDF_DIB:
223 {
224 wxBitmap *bitmap = (wxBitmap *)data;
225
226 HGLOBAL hDIB = wxDIB::ConvertFromBitmap(GetHbitmapOf(*bitmap));
227 if ( hDIB )
228 {
229 handle = ::SetClipboardData(CF_DIB, hDIB);
230 }
231 break;
232 }
233
234 // VZ: I'm told that this code works, but it doesn't seem to work for me
235 // and, anyhow, I'd be highly surprized if it did. So I leave it here
236 // but IMNSHO it is completely broken.
237 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH)
238 case wxDF_METAFILE:
239 {
240 wxMetafile *wxMF = (wxMetafile *)data;
241 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
242 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
243
244 mf->mm = wxMF->GetWindowsMappingMode();
245 mf->xExt = width;
246 mf->yExt = height;
247 mf->hMF = (HMETAFILE) wxMF->GetHMETAFILE();
248 GlobalUnlock(data);
249 wxMF->SetHMETAFILE((WXHANDLE) NULL);
250
251 handle = SetClipboardData(CF_METAFILEPICT, data);
252 break;
253 }
254 #endif // wxUSE_METAFILE
255
256 #if wxUSE_ENH_METAFILE && !defined(__WIN16__)
257 case wxDF_ENHMETAFILE:
258 {
259 wxEnhMetaFile *emf = (wxEnhMetaFile *)data;
260 wxEnhMetaFile emfCopy = *emf;
261
262 handle = SetClipboardData(CF_ENHMETAFILE,
263 (void *)emfCopy.GetHENHMETAFILE());
264 }
265 break;
266 #endif // wxUSE_ENH_METAFILE
267
268 case CF_SYLK:
269 case CF_DIF:
270 case CF_TIFF:
271 case CF_PALETTE:
272 default:
273 {
274 wxLogError(_("Unsupported clipboard format."));
275 return FALSE;
276 }
277
278 case wxDF_OEMTEXT:
279 dataFormat = wxDF_TEXT;
280 // fall through
281
282 case wxDF_TEXT:
283 {
284 char *s = (char *)data;
285
286 width = strlen(s) + 1;
287 height = 1;
288 DWORD l = (width * height);
289 HANDLE hGlobalMemory = GlobalAlloc(GHND, l);
290 if ( hGlobalMemory )
291 {
292 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
293
294 memcpy(lpGlobalMemory, s, l);
295
296 GlobalUnlock(hGlobalMemory);
297 }
298
299 handle = SetClipboardData(dataFormat, hGlobalMemory);
300 break;
301 }
302 // Only tested with non-Unicode, Visual C++ 6.0 so far
303 #if defined(__VISUALC__) && !defined(UNICODE)
304 case wxDF_HTML:
305 {
306 char* html = (char *)data;
307
308 // Create temporary buffer for HTML header...
309 char *buf = new char [400 + strlen(html)];
310 if(!buf) return FALSE;
311
312 // Get clipboard id for HTML format...
313 static int cfid = 0;
314 if(!cfid) cfid = RegisterClipboardFormat(wxT("HTML Format"));
315
316 // Create a template string for the HTML header...
317 strcpy(buf,
318 "Version:0.9\r\n"
319 "StartHTML:00000000\r\n"
320 "EndHTML:00000000\r\n"
321 "StartFragment:00000000\r\n"
322 "EndFragment:00000000\r\n"
323 "<html><body>\r\n"
324 "<!--StartFragment -->\r\n");
325
326 // Append the HTML...
327 strcat(buf, html);
328 strcat(buf, "\r\n");
329 // Finish up the HTML format...
330 strcat(buf,
331 "<!--EndFragment-->\r\n"
332 "</body>\r\n"
333 "</html>");
334
335 // Now go back, calculate all the lengths, and write out the
336 // necessary header information. Note, wsprintf() truncates the
337 // string when you overwrite it so you follow up with code to replace
338 // the 0 appended at the end with a '\r'...
339 char *ptr = strstr(buf, "StartHTML");
340 wsprintf(ptr+10, "%08u", strstr(buf, "<html>") - buf);
341 *(ptr+10+8) = '\r';
342
343 ptr = strstr(buf, "EndHTML");
344 wsprintf(ptr+8, "%08u", strlen(buf));
345 *(ptr+8+8) = '\r';
346
347 ptr = strstr(buf, "StartFragment");
348 wsprintf(ptr+14, "%08u", strstr(buf, "<!--StartFrag") - buf);
349 *(ptr+14+8) = '\r';
350
351 ptr = strstr(buf, "EndFragment");
352 wsprintf(ptr+12, "%08u", strstr(buf, "<!--EndFrag") - buf);
353 *(ptr+12+8) = '\r';
354
355 // Now you have everything in place ready to put on the
356 // clipboard.
357
358 // Allocate global memory for transfer...
359 HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(buf)+4);
360
361 // Put your string in the global memory...
362 ptr = (char *)GlobalLock(hText);
363 strcpy(ptr, buf);
364 GlobalUnlock(hText);
365
366 handle = ::SetClipboardData(cfid, hText);
367
368 // Free memory...
369 GlobalFree(hText);
370
371 // Clean up...
372 delete [] buf;
373 break;
374 }
375 #endif
376 }
377
378 if ( handle == 0 )
379 {
380 wxLogSysError(_("Failed to set clipboard data."));
381
382 return FALSE;
383 }
384
385 return TRUE;
386 }
387
388 void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
389 {
390 void *retval = NULL;
391
392 switch ( dataFormat )
393 {
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 #if WXWIN_COMPATIBILITY_2
436 wxBM->SetOk(TRUE);
437 #endif // WXWIN_COMPATIBILITY_2
438 retval = wxBM;
439 break;
440 }
441
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("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("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( 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 format enumerator
739 bool result = FALSE;
740 wxArrayInt supportedFormats;
741 IEnumFORMATETC *pEnumFormatEtc = NULL;
742 hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
743 if ( FAILED(hr) || !pEnumFormatEtc )
744 {
745 wxLogSysError(hr,
746 _("Failed to retrieve the supported clipboard formats"));
747 }
748 else
749 {
750 // ask for the supported formats and see if there are any we support
751 FORMATETC formatEtc;
752 for ( ;; )
753 {
754 ULONG nCount;
755 hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount);
756
757 // don't use FAILED() because S_FALSE would pass it
758 if ( hr != S_OK )
759 {
760 // no more formats
761 break;
762 }
763
764 CLIPFORMAT cf = formatEtc.cfFormat;
765
766 #ifdef __WXDEBUG__
767 wxLogTrace(wxTRACE_OleCalls,
768 wxT("Object on the clipboard supports format %s."),
769 wxDataObject::GetFormatName(cf));
770 #endif // Debug
771
772 // is supported?
773 for ( size_t n = 0; n < nFormats; n++ )
774 {
775 if ( formats[n].GetFormatId() == cf )
776 {
777 if ( supportedFormats.Index(cf) == wxNOT_FOUND )
778 {
779 supportedFormats.Add(cf);
780 }
781 }
782 }
783 }
784
785 pEnumFormatEtc->Release();
786 }
787
788 if ( formats != &format )
789 {
790 delete [] formats;
791 }
792 //else: we didn't allocate any memory
793
794 if ( !supportedFormats.IsEmpty() )
795 {
796 FORMATETC formatEtc;
797 formatEtc.ptd = NULL;
798 formatEtc.dwAspect = DVASPECT_CONTENT;
799 formatEtc.lindex = -1;
800
801 size_t nSupportedFormats = supportedFormats.GetCount();
802 for ( size_t n = 0; !result && (n < nSupportedFormats); n++ )
803 {
804 STGMEDIUM medium;
805 formatEtc.cfFormat = supportedFormats[n];
806
807 // use the appropriate tymed
808 switch ( formatEtc.cfFormat )
809 {
810 case CF_BITMAP:
811 formatEtc.tymed = TYMED_GDI;
812 break;
813
814 case CF_METAFILEPICT:
815 formatEtc.tymed = TYMED_MFPICT;
816 break;
817
818 case CF_ENHMETAFILE:
819 formatEtc.tymed = TYMED_ENHMF;
820 break;
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 //else: unsupported format
859
860 // clean up and return
861 pDataObject->Release();
862
863 return result;
864 #elif wxUSE_DATAOBJ
865 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
866
867 wxDataFormat format = data.GetPreferredFormat();
868 switch ( format )
869 {
870 case wxDF_TEXT:
871 case wxDF_OEMTEXT:
872 {
873 wxTextDataObject& textDataObject = (wxTextDataObject &)data;
874 char* s = (char*)wxGetClipboardData(format);
875 if ( !s )
876 return FALSE;
877
878 textDataObject.SetText(s);
879 delete [] s;
880
881 return TRUE;
882 }
883
884 case wxDF_BITMAP:
885 case wxDF_DIB:
886 {
887 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
888 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat());
889 if ( !bitmap )
890 return FALSE;
891
892 bitmapDataObject.SetBitmap(*bitmap);
893 delete bitmap;
894
895 return TRUE;
896 }
897 #if wxUSE_METAFILE
898 case wxDF_METAFILE:
899 {
900 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
901 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
902 if ( !metaFile )
903 return FALSE;
904
905 metaFileDataObject.SetMetafile(*metaFile);
906 delete metaFile;
907
908 return TRUE;
909 }
910 #endif // wxUSE_METAFILE
911 }
912 return FALSE;
913 #else // !wxUSE_DATAOBJ
914 wxFAIL_MSG( wxT("no clipboard implementation") );
915 return FALSE;
916 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
917 }
918
919 #endif // wxUSE_CLIPBOARD
920