]> git.saurik.com Git - wxWidgets.git/blob - src/msw/clipbrd.cpp
Add --with-sdl to the build doc and RPM spec
[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(__WIN16__) && !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 HGLOBAL hDIB = wxDIB::ConvertFromBitmap(GetHbitmapOf(*bitmap));
225 if ( hDIB )
226 {
227 handle = ::SetClipboardData(CF_DIB, hDIB);
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(__WIN16__) && !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 m_clearOnExit = FALSE;
535 m_isOpened = FALSE;
536 }
537
538 wxClipboard::~wxClipboard()
539 {
540 if ( m_clearOnExit )
541 {
542 Clear();
543 }
544 }
545
546 void wxClipboard::Clear()
547 {
548 #if wxUSE_OLE_CLIPBOARD
549 if ( FAILED(OleSetClipboard(NULL)) )
550 {
551 wxLogLastError(wxT("OleSetClipboard(NULL)"));
552 }
553 #endif
554 }
555
556 bool wxClipboard::Flush()
557 {
558 #if wxUSE_OLE_CLIPBOARD
559 if ( FAILED(OleFlushClipboard()) )
560 {
561 wxLogLastError(wxT("OleFlushClipboard"));
562
563 return FALSE;
564 }
565 else
566 {
567 m_clearOnExit = FALSE;
568
569 return TRUE;
570 }
571 #else // !wxUSE_OLE_CLIPBOARD
572 return FALSE;
573 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
574 }
575
576 bool wxClipboard::Open()
577 {
578 // OLE opens clipboard for us
579 m_isOpened = TRUE;
580 #if wxUSE_OLE_CLIPBOARD
581 return TRUE;
582 #else
583 return wxOpenClipboard();
584 #endif
585 }
586
587 bool wxClipboard::IsOpened() const
588 {
589 #if wxUSE_OLE_CLIPBOARD
590 return m_isOpened;
591 #else
592 return wxIsClipboardOpened();
593 #endif
594 }
595
596 bool wxClipboard::SetData( wxDataObject *data )
597 {
598 #if !wxUSE_OLE_CLIPBOARD
599 (void)wxEmptyClipboard();
600 #endif // wxUSE_OLE_CLIPBOARD
601
602 if ( data )
603 return AddData(data);
604 else
605 return TRUE;
606 }
607
608 bool wxClipboard::AddData( wxDataObject *data )
609 {
610 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
611
612 #if wxUSE_OLE_CLIPBOARD
613 HRESULT hr = OleSetClipboard(data->GetInterface());
614 if ( FAILED(hr) )
615 {
616 wxLogSysError(hr, _("Failed to put data on the clipboard"));
617
618 // don't free anything in this case
619
620 return FALSE;
621 }
622
623 // we have a problem here because we should delete wxDataObject, but we
624 // can't do it because IDataObject which we just gave to the clipboard
625 // would try to use it when it will need the data. IDataObject is ref
626 // counted and so doesn't suffer from such problem, so we release it now
627 // and tell it to delete wxDataObject when it is deleted itself.
628 data->SetAutoDelete();
629
630 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
631 // using OLE clipboard when the app terminates - by default, we call
632 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
633 // wxClipboard::Flush() to chaneg this
634 m_clearOnExit = TRUE;
635
636 return TRUE;
637 #elif wxUSE_DATAOBJ
638 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
639
640 wxDataFormat format = data->GetPreferredFormat();
641
642 switch ( format )
643 {
644 case wxDF_TEXT:
645 case wxDF_OEMTEXT:
646 {
647 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
648 wxString str(textDataObject->GetText());
649 return wxSetClipboardData(format, str.c_str());
650 }
651
652 case wxDF_BITMAP:
653 case wxDF_DIB:
654 {
655 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
656 wxBitmap bitmap(bitmapDataObject->GetBitmap());
657 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
658 }
659
660 #if wxUSE_METAFILE
661 case wxDF_METAFILE:
662 {
663 #if 1
664 // TODO
665 wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values.");
666 return FALSE;
667 #else
668 wxMetafileDataObject* metaFileDataObject =
669 (wxMetafileDataObject*) data;
670 wxMetafile metaFile = metaFileDataObject->GetMetafile();
671 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
672 metaFileDataObject->GetWidth(),
673 metaFileDataObject->GetHeight());
674 #endif
675 }
676 #endif // wxUSE_METAFILE
677
678 default:
679 {
680 // This didn't compile, of course
681 // return wxSetClipboardData(data);
682 // TODO
683 wxLogError(wxT("Not implemented."));
684 return FALSE;
685 }
686 }
687 #else // !wxUSE_DATAOBJ
688 return FALSE;
689 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
690 }
691
692 void wxClipboard::Close()
693 {
694 m_isOpened = FALSE;
695 // OLE closes clipboard for us
696 #if !wxUSE_OLE_CLIPBOARD
697 wxCloseClipboard();
698 #endif
699 }
700
701 bool wxClipboard::IsSupported( wxDataFormat format )
702 {
703 return wxIsClipboardFormatAvailable(format);
704 }
705
706 bool wxClipboard::GetData( wxDataObject& data )
707 {
708 #if wxUSE_OLE_CLIPBOARD
709 IDataObject *pDataObject = NULL;
710 HRESULT hr = OleGetClipboard(&pDataObject);
711 if ( FAILED(hr) || !pDataObject )
712 {
713 wxLogSysError(hr, _("Failed to get data from the clipboard"));
714
715 return FALSE;
716 }
717
718 // build the list of supported formats
719 size_t nFormats = data.GetFormatCount(wxDataObject::Set);
720 wxDataFormat format;
721 wxDataFormat *formats;
722 if ( nFormats == 1 )
723 {
724 // the most common case
725 formats = &format;
726 }
727 else
728 {
729 // bad luck, need to alloc mem
730 formats = new wxDataFormat[nFormats];
731 }
732
733 data.GetAllFormats(formats, wxDataObject::Set);
734
735 // get the data for the given formats
736 FORMATETC formatEtc;
737 CLIPFORMAT cf;
738 bool result = FALSE;
739
740 // enumerate all explicit formats on the clipboard.
741 // note that this does not include implicit / synthetic (automatically
742 // converted) formats.
743 #ifdef __WXDEBUG__
744 // get the format enumerator
745 IEnumFORMATETC *pEnumFormatEtc = NULL;
746 hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
747 if ( FAILED(hr) || !pEnumFormatEtc )
748 {
749 wxLogSysError(hr,
750 _("Failed to retrieve the supported clipboard formats"));
751 }
752 else
753 {
754 // ask for the supported formats and see if there are any we support
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 cf = formatEtc.cfFormat;
768
769 wxLogTrace(wxTRACE_OleCalls,
770 wxT("Object on the clipboard supports format %s."),
771 wxDataObject::GetFormatName(cf));
772 }
773
774 pEnumFormatEtc->Release();
775 }
776 #endif // Debug
777
778 STGMEDIUM medium;
779 // stop at the first valid format found on the clipboard
780 for ( size_t n = 0; !result && (n < nFormats); n++ )
781 {
782 // convert to NativeFormat Id
783 cf = formats[n].GetFormatId();
784
785 // if the format is not available, try the next one
786 // this test includes implicit / sythetic formats
787 if ( !::IsClipboardFormatAvailable(cf) )
788 continue;
789
790 formatEtc.cfFormat = cf;
791 formatEtc.ptd = NULL;
792 formatEtc.dwAspect = DVASPECT_CONTENT;
793 formatEtc.lindex = -1;
794
795 // use the appropriate tymed
796 switch ( formatEtc.cfFormat )
797 {
798 case CF_BITMAP:
799 formatEtc.tymed = TYMED_GDI;
800 break;
801
802 #ifndef __WXWINCE__
803 case CF_METAFILEPICT:
804 formatEtc.tymed = TYMED_MFPICT;
805 break;
806
807 case CF_ENHMETAFILE:
808 formatEtc.tymed = TYMED_ENHMF;
809 break;
810 #endif
811
812 default:
813 formatEtc.tymed = TYMED_HGLOBAL;
814 }
815
816 // try to get data
817 hr = pDataObject->GetData(&formatEtc, &medium);
818 if ( FAILED(hr) )
819 {
820 // try other tymed for GDI objects
821 if ( formatEtc.cfFormat == CF_BITMAP )
822 {
823 formatEtc.tymed = TYMED_HGLOBAL;
824 hr = pDataObject->GetData(&formatEtc, &medium);
825 }
826 }
827
828 if ( SUCCEEDED(hr) )
829 {
830 // pass the data to the data object
831 hr = data.GetInterface()->SetData(&formatEtc, &medium, TRUE);
832 if ( FAILED(hr) )
833 {
834 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
835
836 // IDataObject only takes the ownership of data if it
837 // successfully got it - which is not the case here
838 ReleaseStgMedium(&medium);
839 }
840 else
841 {
842 result = TRUE;
843 }
844 }
845 //else: unsupported tymed?
846 }
847
848 if ( formats != &format )
849 {
850 delete [] formats;
851 }
852 //else: we didn't allocate any memory
853
854 // clean up and return
855 pDataObject->Release();
856
857 return result;
858 #elif wxUSE_DATAOBJ
859 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
860
861 wxDataFormat format = data.GetPreferredFormat();
862 switch ( format )
863 {
864 case wxDF_TEXT:
865 case wxDF_OEMTEXT:
866 {
867 wxTextDataObject& textDataObject = (wxTextDataObject &)data;
868 char* s = (char*)wxGetClipboardData(format);
869 if ( !s )
870 return FALSE;
871
872 textDataObject.SetText(wxString::FromAscii(s));
873 delete [] s;
874
875 return TRUE;
876 }
877
878 case wxDF_BITMAP:
879 case wxDF_DIB:
880 {
881 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
882 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat());
883 if ( !bitmap )
884 return FALSE;
885
886 bitmapDataObject.SetBitmap(*bitmap);
887 delete bitmap;
888
889 return TRUE;
890 }
891 #if wxUSE_METAFILE
892 case wxDF_METAFILE:
893 {
894 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
895 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
896 if ( !metaFile )
897 return FALSE;
898
899 metaFileDataObject.SetMetafile(*metaFile);
900 delete metaFile;
901
902 return TRUE;
903 }
904 #endif // wxUSE_METAFILE
905 }
906 return FALSE;
907 #else // !wxUSE_DATAOBJ
908 wxFAIL_MSG( wxT("no clipboard implementation") );
909 return FALSE;
910 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
911 }
912
913 #endif // wxUSE_CLIPBOARD
914