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