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