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