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