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