]> git.saurik.com Git - wxWidgets.git/blame - src/msw/clipbrd.cpp
wxInputConsumer
[wxWidgets.git] / src / msw / clipbrd.cpp
CommitLineData
2bda0e17
KB
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 and Markus Holzem
26f86486 9// Licence: wxWindows license
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
26f86486
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17 20#ifdef __GNUG__
26f86486 21 #pragma implementation "clipbrd.h"
2bda0e17
KB
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
26f86486 28 #pragma hdrstop
2bda0e17
KB
29#endif
30
31#ifndef WX_PRECOMP
26f86486 32 #include "wx/setup.h"
2bda0e17
KB
33#endif
34
47d67540 35#if wxUSE_CLIPBOARD
2bda0e17
KB
36
37#ifndef WX_PRECOMP
26f86486
VZ
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"
0c589ad0 44 #include "wx/intl.h"
2bda0e17
KB
45#endif
46
06e43511 47#if wxUSE_METAFILE
26f86486 48 #include "wx/metafile.h"
06e43511
JS
49#endif
50
dbda9e86 51#include "wx/log.h"
2bda0e17 52#include "wx/clipbrd.h"
06e43511 53
3f480da3 54#include <string.h>
06e43511
JS
55#include <windows.h>
56
2bda0e17 57#include "wx/msw/private.h"
8cb172b4
JS
58
59#ifndef __WXMICROWIN__
2bda0e17 60#include "wx/msw/dib.h"
8cb172b4 61#endif
2bda0e17 62
d59ceba5
VZ
63// wxDataObject is tied to OLE/drag and drop implementation, therefore so are
64// the functions using wxDataObject in wxClipboard
1e6feb95 65//#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
d59ceba5
VZ
66
67#if wxUSE_DATAOBJ
26f86486 68 #include "wx/dataobj.h"
bd52bee1 69#endif
3f480da3 70
bd52bee1 71#if wxUSE_OLE
b94e73ae
VZ
72 // use OLE clipboard
73 #define wxUSE_OLE_CLIPBOARD 1
d59ceba5
VZ
74#else // !wxUSE_DATAOBJ
75 // use Win clipboard API
76 #define wxUSE_OLE_CLIPBOARD 0
2bda0e17
KB
77#endif
78
d59ceba5
VZ
79#if wxUSE_OLE_CLIPBOARD
80 #include <ole2.h>
81#endif // wxUSE_OLE_CLIPBOARD
82
3f480da3
VZ
83#ifdef __WIN16__
84 #define memcpy hmemcpy
85#endif
06e43511 86
26f86486
VZ
87// ===========================================================================
88// implementation
89// ===========================================================================
90
91// ---------------------------------------------------------------------------
92// old-style clipboard functions using Windows API
93// ---------------------------------------------------------------------------
2bda0e17 94
26f86486
VZ
95static bool gs_wxClipboardIsOpen = FALSE;
96
97bool wxOpenClipboard()
2bda0e17 98{
223d09f6 99 wxCHECK_MSG( !gs_wxClipboardIsOpen, TRUE, wxT("clipboard already opened.") );
26f86486
VZ
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 {
223d09f6 113 wxLogDebug(wxT("Can not open clipboard without a main window."));
26f86486
VZ
114
115 return FALSE;
116 }
2bda0e17
KB
117}
118
26f86486 119bool wxCloseClipboard()
2bda0e17 120{
223d09f6 121 wxCHECK_MSG( gs_wxClipboardIsOpen, FALSE, wxT("clipboard is not opened") );
26f86486
VZ
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;
2bda0e17
KB
133}
134
26f86486 135bool wxEmptyClipboard()
2bda0e17 136{
26f86486
VZ
137 if ( ::EmptyClipboard() == 0 )
138 {
139 wxLogSysError(_("Failed to empty the clipboard."));
140
141 return FALSE;
142 }
143
144 return TRUE;
2bda0e17
KB
145}
146
26f86486 147bool wxIsClipboardOpened()
2bda0e17 148{
26f86486 149 return gs_wxClipboardIsOpen;
2bda0e17
KB
150}
151
06e43511 152bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
2bda0e17 153{
d9317fd4
VZ
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
5dd26b08 167#if wxUSE_ENH_METAFILE && !defined(__WIN16__)
d9317fd4
VZ
168 case CF_METAFILEPICT:
169 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0;
170#endif // wxUSE_ENH_METAFILE
171
172 default:
173 return FALSE;
174 }
2bda0e17
KB
175}
176
26f86486
VZ
177bool wxSetClipboardData(wxDataFormat dataFormat,
178 const void *data,
179 int width, int height)
2bda0e17 180{
26f86486
VZ
181 HANDLE handle = 0; // return value of SetClipboardData
182
183 switch (dataFormat)
2bda0e17 184 {
26f86486
VZ
185 case wxDF_BITMAP:
186 {
187 wxBitmap *bitmap = (wxBitmap *)data;
188
189 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
190 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
191 HBITMAP old = (HBITMAP)
192 ::SelectObject(hdcSrc, (HBITMAP)bitmap->GetHBITMAP());
193 HBITMAP hBitmap = CreateCompatibleBitmap(hdcSrc,
194 bitmap->GetWidth(),
195 bitmap->GetHeight());
196 if (!hBitmap)
197 {
198 SelectObject(hdcSrc, old);
199 DeleteDC(hdcMem);
200 DeleteDC(hdcSrc);
201 return FALSE;
202 }
203
204 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hBitmap);
205 BitBlt(hdcMem, 0, 0, bitmap->GetWidth(), bitmap->GetHeight(),
206 hdcSrc, 0, 0, SRCCOPY);
207
208 // Select new bitmap out of memory DC
209 SelectObject(hdcMem, old1);
210
211 // Set the data
212 handle = ::SetClipboardData(CF_BITMAP, hBitmap);
213
214 // Clean up
215 SelectObject(hdcSrc, old);
216 DeleteDC(hdcSrc);
217 DeleteDC(hdcMem);
218 break;
219 }
220
221 case wxDF_DIB:
222 {
47d67540 223#if wxUSE_IMAGE_LOADING_IN_MSW
26f86486
VZ
224 wxBitmap *bitmap = (wxBitmap *)data;
225 HBITMAP hBitmap = (HBITMAP)bitmap->GetHBITMAP();
226 // NULL palette means to use the system one
33ac7e6f 227 HANDLE hDIB = wxBitmapToDIB(hBitmap, (HPALETTE)NULL);
26f86486 228 handle = SetClipboardData(CF_DIB, hDIB);
d59ceba5 229#endif // wxUSE_IMAGE_LOADING_IN_MSW
26f86486
VZ
230 break;
231 }
232
d9317fd4
VZ
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 surprized if it did. So I leave it here
235 // but IMNSHO it is completely broken.
236#if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH)
26f86486
VZ
237 case wxDF_METAFILE:
238 {
239 wxMetafile *wxMF = (wxMetafile *)data;
240 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
26f86486 241 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
2bda0e17 242
26f86486
VZ
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);
2bda0e17 249
26f86486
VZ
250 handle = SetClipboardData(CF_METAFILEPICT, data);
251 break;
252 }
d9317fd4
VZ
253#endif // wxUSE_METAFILE
254
5dd26b08 255#if wxUSE_ENH_METAFILE && !defined(__WIN16__)
d9317fd4
VZ
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
26f86486
VZ
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 }
2bda0e17 276
26f86486
VZ
277 case wxDF_OEMTEXT:
278 dataFormat = wxDF_TEXT;
279 // fall through
2bda0e17 280
26f86486
VZ
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 {
26f86486 291 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
2bda0e17 292
26f86486 293 memcpy(lpGlobalMemory, s, l);
2bda0e17 294
26f86486
VZ
295 GlobalUnlock(hGlobalMemory);
296 }
2bda0e17 297
26f86486
VZ
298 handle = SetClipboardData(dataFormat, hGlobalMemory);
299 break;
300 }
2bda0e17 301 }
26f86486
VZ
302
303 if ( handle == 0 )
2bda0e17 304 {
26f86486
VZ
305 wxLogSysError(_("Failed to set clipboard data."));
306
307 return FALSE;
2bda0e17 308 }
26f86486
VZ
309
310 return TRUE;
311}
312
313void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
314{
315 void *retval = NULL;
316
317 switch ( dataFormat )
2bda0e17 318 {
26f86486
VZ
319 case wxDF_BITMAP:
320 {
321 BITMAP bm;
322 HBITMAP hBitmap = (HBITMAP) GetClipboardData(CF_BITMAP);
323 if (!hBitmap)
324 break;
325
326 HDC hdcMem = CreateCompatibleDC((HDC) NULL);
327 HDC hdcSrc = CreateCompatibleDC((HDC) NULL);
328
329 HBITMAP old = (HBITMAP) ::SelectObject(hdcSrc, hBitmap);
330 GetObject(hBitmap, sizeof(BITMAP), (LPSTR)&bm);
331
332 HBITMAP hNewBitmap = CreateBitmapIndirect(&bm);
333
334 if (!hNewBitmap)
335 {
336 SelectObject(hdcSrc, old);
337 DeleteDC(hdcMem);
338 DeleteDC(hdcSrc);
339 break;
340 }
341
342 HBITMAP old1 = (HBITMAP) SelectObject(hdcMem, hNewBitmap);
343 BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight,
344 hdcSrc, 0, 0, SRCCOPY);
345
346 // Select new bitmap out of memory DC
347 SelectObject(hdcMem, old1);
348
349 // Clean up
350 SelectObject(hdcSrc, old);
351 DeleteDC(hdcSrc);
352 DeleteDC(hdcMem);
353
354 // Create and return a new wxBitmap
355 wxBitmap *wxBM = new wxBitmap;
356 wxBM->SetHBITMAP((WXHBITMAP) hNewBitmap);
357 wxBM->SetWidth(bm.bmWidth);
358 wxBM->SetHeight(bm.bmHeight);
359 wxBM->SetDepth(bm.bmPlanes);
6d167489 360#if WXWIN_COMPATIBILITY_2
26f86486 361 wxBM->SetOk(TRUE);
6d167489 362#endif // WXWIN_COMPATIBILITY_2
26f86486
VZ
363 retval = wxBM;
364 break;
365 }
366
367 case wxDF_METAFILE:
368 case CF_SYLK:
369 case CF_DIF:
370 case CF_TIFF:
371 case CF_PALETTE:
372 case wxDF_DIB:
26f86486
VZ
373 {
374 wxLogError(_("Unsupported clipboard format."));
375 return FALSE;
376 }
377
378 case wxDF_OEMTEXT:
379 dataFormat = wxDF_TEXT;
380 // fall through
381
382 case wxDF_TEXT:
383 {
384 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
385 if (!hGlobalMemory)
386 break;
2bda0e17 387
26f86486
VZ
388 DWORD hsize = ::GlobalSize(hGlobalMemory);
389 if (len)
390 *len = hsize;
2bda0e17 391
26f86486
VZ
392 char *s = new char[hsize];
393 if (!s)
394 break;
2bda0e17 395
26f86486 396 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
2bda0e17 397
26f86486 398 memcpy(s, lpGlobalMemory, hsize);
2bda0e17 399
26f86486
VZ
400 ::GlobalUnlock(hGlobalMemory);
401
402 retval = s;
403 break;
404 }
3f480da3
VZ
405
406 default:
407 {
408 HANDLE hGlobalMemory = ::GetClipboardData(dataFormat);
409 if ( !hGlobalMemory )
410 break;
411
412 DWORD size = ::GlobalSize(hGlobalMemory);
413 if ( len )
414 *len = size;
415
416 void *buf = malloc(size);
417 if ( !buf )
418 break;
419
420 LPSTR lpGlobalMemory = (LPSTR)::GlobalLock(hGlobalMemory);
421
422 memcpy(buf, lpGlobalMemory, size);
423
424 ::GlobalUnlock(hGlobalMemory);
425
426 retval = buf;
427 break;
428 }
26f86486 429 }
2bda0e17 430
26f86486
VZ
431 if ( !retval )
432 {
433 wxLogSysError(_("Failed to retrieve data from the clipboard."));
2bda0e17 434 }
26f86486
VZ
435
436 return retval;
2bda0e17
KB
437}
438
3f480da3 439wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
2bda0e17 440{
33ac7e6f 441 return (wxDataFormat::NativeFormat)::EnumClipboardFormats(dataFormat);
2bda0e17
KB
442}
443
837e5743 444int wxRegisterClipboardFormat(wxChar *formatName)
2bda0e17
KB
445{
446 return ::RegisterClipboardFormat(formatName);
447}
448
26f86486 449bool wxGetClipboardFormatName(wxDataFormat dataFormat,
837e5743 450 wxChar *formatName,
26f86486 451 int maxCount)
2bda0e17 452{
26f86486 453 return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
2bda0e17
KB
454}
455
26f86486 456// ---------------------------------------------------------------------------
06e43511 457// wxClipboard
26f86486 458// ---------------------------------------------------------------------------
2bda0e17 459
26f86486 460IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
2bda0e17
KB
461
462wxClipboard::wxClipboard()
463{
d59ceba5 464 m_clearOnExit = FALSE;
2bda0e17
KB
465}
466
467wxClipboard::~wxClipboard()
468{
d59ceba5
VZ
469 if ( m_clearOnExit )
470 {
471 Clear();
472 }
2bda0e17
KB
473}
474
06e43511 475void wxClipboard::Clear()
2bda0e17 476{
d59ceba5
VZ
477#if wxUSE_OLE_CLIPBOARD
478 if ( FAILED(OleSetClipboard(NULL)) )
479 {
f6bcfd97 480 wxLogLastError(wxT("OleSetClipboard(NULL)"));
d59ceba5
VZ
481 }
482#endif
483}
484
485bool wxClipboard::Flush()
486{
7ffdaf81 487#if wxUSE_OLE_CLIPBOARD
d59ceba5
VZ
488 if ( FAILED(OleFlushClipboard()) )
489 {
f6bcfd97 490 wxLogLastError(wxT("OleFlushClipboard"));
d59ceba5
VZ
491
492 return FALSE;
493 }
494 else
495 {
496 m_clearOnExit = FALSE;
497
498 return TRUE;
499 }
7ffdaf81
VZ
500#else // !wxUSE_OLE_CLIPBOARD
501 return FALSE;
502#endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
2bda0e17
KB
503}
504
06e43511 505bool wxClipboard::Open()
2bda0e17 506{
d59ceba5
VZ
507 // OLE opens clipboard for us
508#if wxUSE_OLE_CLIPBOARD
509 return TRUE;
510#else
06e43511 511 return wxOpenClipboard();
d59ceba5 512#endif
2bda0e17
KB
513}
514
f536e0f2
VZ
515bool wxClipboard::IsOpened() const
516{
517#if wxUSE_OLE_CLIPBOARD
518 return TRUE;
519#else
520 return wxIsClipboardOpened();
521#endif
522}
523
06e43511 524bool wxClipboard::SetData( wxDataObject *data )
2bda0e17 525{
51edda6a 526#if !wxUSE_OLE_CLIPBOARD
26f86486 527 (void)wxEmptyClipboard();
51edda6a 528#endif // wxUSE_OLE_CLIPBOARD
26f86486
VZ
529
530 if ( data )
531 return AddData(data);
532 else
533 return TRUE;
534}
535
536bool wxClipboard::AddData( wxDataObject *data )
537{
223d09f6 538 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
06e43511 539
d59ceba5
VZ
540#if wxUSE_OLE_CLIPBOARD
541 HRESULT hr = OleSetClipboard(data->GetInterface());
542 if ( FAILED(hr) )
543 {
544 wxLogSysError(hr, _("Failed to put data on the clipboard"));
545
546 // don't free anything in this case
547
548 return FALSE;
549 }
550
551 // we have a problem here because we should delete wxDataObject, but we
552 // can't do it because IDataObject which we just gave to the clipboard
553 // would try to use it when it will need the data. IDataObject is ref
554 // counted and so doesn't suffer from such problem, so we release it now
555 // and tell it to delete wxDataObject when it is deleted itself.
556 data->SetAutoDelete();
557
558 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
559 // using OLE clipboard when the app terminates - by default, we call
560 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
561 // wxClipboard::Flush() to chaneg this
562 m_clearOnExit = TRUE;
563
564 return TRUE;
565#elif wxUSE_DATAOBJ
223d09f6 566 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
26f86486
VZ
567
568 wxDataFormat format = data->GetFormat();
569
570 switch ( format )
06e43511
JS
571 {
572 case wxDF_TEXT:
573 case wxDF_OEMTEXT:
574 {
575 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
576 wxString str(textDataObject->GetText());
26f86486 577 return wxSetClipboardData(format, str.c_str());
06e43511 578 }
26f86486 579
06e43511
JS
580 case wxDF_BITMAP:
581 case wxDF_DIB:
582 {
583 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
584 wxBitmap bitmap(bitmapDataObject->GetBitmap());
26f86486 585 return wxSetClipboardData(data->GetFormat(), &bitmap);
06e43511 586 }
26f86486 587
06e43511
JS
588#if wxUSE_METAFILE
589 case wxDF_METAFILE:
590 {
33ac7e6f 591 wxMetafileDataObject* metaFileDataObject =
26f86486 592 (wxMetafileDataObject*) data;
06e43511 593 wxMetafile metaFile = metaFileDataObject->GetMetafile();
26f86486
VZ
594 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
595 metaFileDataObject->GetWidth(),
596 metaFileDataObject->GetHeight());
06e43511 597 }
26f86486
VZ
598#endif // wxUSE_METAFILE
599
06e43511 600 default:
3f480da3 601 return wxSetClipboardData(data);
06e43511 602 }
d59ceba5 603#else // !wxUSE_DATAOBJ
06e43511 604 return FALSE;
d59ceba5 605#endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
2bda0e17
KB
606}
607
06e43511 608void wxClipboard::Close()
2bda0e17 609{
d59ceba5
VZ
610 // OLE closes clipboard for us
611#if !wxUSE_OLE_CLIPBOARD
06e43511 612 wxCloseClipboard();
d59ceba5 613#endif
2bda0e17
KB
614}
615
26f86486 616bool wxClipboard::IsSupported( wxDataFormat format )
2bda0e17 617{
06e43511 618 return wxIsClipboardFormatAvailable(format);
2bda0e17
KB
619}
620
1e8335b0 621bool wxClipboard::GetData( wxDataObject& data )
2bda0e17 622{
d59ceba5
VZ
623#if wxUSE_OLE_CLIPBOARD
624 IDataObject *pDataObject = NULL;
625 HRESULT hr = OleGetClipboard(&pDataObject);
626 if ( FAILED(hr) || !pDataObject )
627 {
628 wxLogSysError(hr, _("Failed to get data from the clipboard"));
629
630 return FALSE;
631 }
632
633 // build the list of supported formats
1e8335b0 634 size_t nFormats = data.GetFormatCount(wxDataObject::Set);
33ac7e6f 635 wxDataFormat format;
c5639a87 636 wxDataFormat *formats;
d59ceba5
VZ
637 if ( nFormats == 1 )
638 {
639 // the most common case
640 formats = &format;
641 }
642 else
643 {
644 // bad luck, need to alloc mem
645 formats = new wxDataFormat[nFormats];
646 }
647
1e8335b0 648 data.GetAllFormats(formats, wxDataObject::Set);
d59ceba5
VZ
649
650 // get the format enumerator
651 bool result = FALSE;
652 wxArrayInt supportedFormats;
653 IEnumFORMATETC *pEnumFormatEtc = NULL;
654 hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
655 if ( FAILED(hr) || !pEnumFormatEtc )
656 {
657 wxLogSysError(hr,
658 _("Failed to retrieve the supported clipboard formats"));
659 }
660 else
661 {
662 // ask for the supported formats and see if there are any we support
663 FORMATETC formatEtc;
664 for ( ;; )
665 {
666 ULONG nCount;
667 hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount);
668
669 // don't use FAILED() because S_FALSE would pass it
670 if ( hr != S_OK )
671 {
672 // no more formats
673 break;
674 }
675
676 CLIPFORMAT cf = formatEtc.cfFormat;
677
678#ifdef __WXDEBUG__
679 wxLogTrace(wxTRACE_OleCalls,
680 wxT("Object on the clipboard supports format %s."),
681 wxDataObject::GetFormatName(cf));
682#endif // Debug
683
684 // is supported?
685 for ( size_t n = 0; n < nFormats; n++ )
686 {
687 if ( formats[n].GetFormatId() == cf )
688 {
689 if ( supportedFormats.Index(cf) == wxNOT_FOUND )
690 {
691 supportedFormats.Add(cf);
692 }
693 }
694 }
695 }
696
697 pEnumFormatEtc->Release();
698 }
699
700 if ( formats != &format )
701 {
702 delete [] formats;
703 }
704 //else: we didn't allocate any memory
705
706 if ( !supportedFormats.IsEmpty() )
707 {
708 FORMATETC formatEtc;
709 formatEtc.ptd = NULL;
710 formatEtc.dwAspect = DVASPECT_CONTENT;
711 formatEtc.lindex = -1;
d59ceba5
VZ
712
713 size_t nSupportedFormats = supportedFormats.GetCount();
714 for ( size_t n = 0; !result && (n < nSupportedFormats); n++ )
715 {
716 STGMEDIUM medium;
717 formatEtc.cfFormat = supportedFormats[n];
718
265b0c07
VZ
719 // use the appropriate tymed
720 switch ( formatEtc.cfFormat )
721 {
722 case CF_BITMAP:
723 formatEtc.tymed = TYMED_GDI;
724 break;
725
726 case CF_METAFILEPICT:
727 formatEtc.tymed = TYMED_MFPICT;
728 break;
729
d9317fd4
VZ
730 case CF_ENHMETAFILE:
731 formatEtc.tymed = TYMED_ENHMF;
732 break;
733
265b0c07
VZ
734 default:
735 formatEtc.tymed = TYMED_HGLOBAL;
736 }
737
d59ceba5
VZ
738 // try to get data
739 hr = pDataObject->GetData(&formatEtc, &medium);
740 if ( FAILED(hr) )
741 {
742 // try other tymed for GDI objects
743 if ( formatEtc.cfFormat == CF_BITMAP )
744 {
745 formatEtc.tymed = TYMED_HGLOBAL;
746 hr = pDataObject->GetData(&formatEtc, &medium);
747 }
748 }
749
750 if ( SUCCEEDED(hr) )
751 {
752 // pass the data to the data object
1e8335b0 753 hr = data.GetInterface()->SetData(&formatEtc, &medium, TRUE);
d59ceba5
VZ
754 if ( FAILED(hr) )
755 {
756 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
757
758 // IDataObject only takes the ownership of data if it
759 // successfully got it - which is not the case here
760 ReleaseStgMedium(&medium);
761 }
762 else
763 {
764 result = TRUE;
765 }
766 }
767 //else: unsupported tymed?
768 }
769 }
770 //else: unsupported format
771
772 // clean up and return
773 pDataObject->Release();
774
775 return result;
776#elif wxUSE_DATAOBJ
223d09f6 777 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
26f86486 778
1e8335b0 779 wxDataFormat format = data.GetFormat();
26f86486 780 switch ( format )
06e43511
JS
781 {
782 case wxDF_TEXT:
783 case wxDF_OEMTEXT:
784 {
1e8335b0
VZ
785 wxTextDataObject& textDataObject = (wxTextDataObject &)data;
786 char* s = (char*)wxGetClipboardData(format);
787 if ( !s )
06e43511 788 return FALSE;
1e8335b0
VZ
789
790 textDataObject.SetText(s);
791 delete [] s;
792
793 return TRUE;
06e43511 794 }
26f86486 795
06e43511
JS
796 case wxDF_BITMAP:
797 case wxDF_DIB:
798 {
1e8335b0 799 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
26f86486 800 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data->GetFormat());
1e8335b0 801 if ( !bitmap )
06e43511 802 return FALSE;
1e8335b0
VZ
803
804 bitmapDataObject.SetBitmap(*bitmap);
805 delete bitmap;
806
807 return TRUE;
06e43511
JS
808 }
809#if wxUSE_METAFILE
810 case wxDF_METAFILE:
811 {
1e8335b0 812 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
26f86486 813 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
1e8335b0 814 if ( !metaFile )
06e43511 815 return FALSE;
3f480da3 816
1e8335b0
VZ
817 metaFileDataObject.SetMetafile(*metaFile);
818 delete metaFile;
26f86486 819
1e8335b0
VZ
820 return TRUE;
821 }
822#endif // wxUSE_METAFILE
06e43511 823 }
3897b707 824 return FALSE;
d59ceba5 825#else // !wxUSE_DATAOBJ
1e8335b0 826 wxFAIL_MSG( wxT("no clipboard implementation") );
06e43511 827 return FALSE;
3897b707 828#endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
2bda0e17
KB
829}
830
47d67540 831#endif // wxUSE_CLIPBOARD
4ce81a75 832