]> git.saurik.com Git - wxWidgets.git/blame - src/msw/clipbrd.cpp
check that the version of __sync_sub_and_fetch that returns a value is supported...
[wxWidgets.git] / src / msw / clipbrd.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
e4db172a 2// Name: src/msw/clipbrd.cpp
2bda0e17
KB
3// Purpose: Clipboard functionality
4// Author: Julian Smart
5// Modified by:
6// Created: 04/01/98
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
26f86486
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
26f86486 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
47d67540 27#if wxUSE_CLIPBOARD
2bda0e17 28
e4db172a
WS
29#include "wx/clipbrd.h"
30
2bda0e17 31#ifndef WX_PRECOMP
26f86486
VZ
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"
0c589ad0 38 #include "wx/intl.h"
e4db172a 39 #include "wx/log.h"
28f92d74 40 #include "wx/dataobj.h"
2bda0e17
KB
41#endif
42
06e43511 43#if wxUSE_METAFILE
26f86486 44 #include "wx/metafile.h"
06e43511
JS
45#endif
46
06e43511 47
3f480da3 48#include <string.h>
06e43511 49
2bda0e17 50#include "wx/msw/private.h"
794fe8cd 51#include "wx/msw/ole/oleutils.h"
8cb172b4 52
4676948b 53#if wxUSE_WXDIB
794fe8cd 54 #include "wx/msw/dib.h"
8cb172b4 55#endif
2bda0e17 56
d59ceba5
VZ
57// wxDataObject is tied to OLE/drag and drop implementation, therefore so are
58// the functions using wxDataObject in wxClipboard
1e6feb95 59//#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
d59ceba5 60
f07dc2e2 61#if wxUSE_OLE && !defined(__WXWINCE__)
b94e73ae
VZ
62 // use OLE clipboard
63 #define wxUSE_OLE_CLIPBOARD 1
d59ceba5
VZ
64#else // !wxUSE_DATAOBJ
65 // use Win clipboard API
66 #define wxUSE_OLE_CLIPBOARD 0
2bda0e17
KB
67#endif
68
d59ceba5
VZ
69#if wxUSE_OLE_CLIPBOARD
70 #include <ole2.h>
71#endif // wxUSE_OLE_CLIPBOARD
72
26f86486
VZ
73// ===========================================================================
74// implementation
75// ===========================================================================
76
77// ---------------------------------------------------------------------------
78// old-style clipboard functions using Windows API
79// ---------------------------------------------------------------------------
2bda0e17 80
02b7b6b0 81static bool gs_wxClipboardIsOpen = false;
26f86486
VZ
82
83bool wxOpenClipboard()
2bda0e17 84{
02b7b6b0 85 wxCHECK_MSG( !gs_wxClipboardIsOpen, true, wxT("clipboard already opened.") );
26f86486
VZ
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 {
223d09f6 99 wxLogDebug(wxT("Can not open clipboard without a main window."));
26f86486 100
02b7b6b0 101 return false;
26f86486 102 }
2bda0e17
KB
103}
104
26f86486 105bool wxCloseClipboard()
2bda0e17 106{
02b7b6b0 107 wxCHECK_MSG( gs_wxClipboardIsOpen, false, wxT("clipboard is not opened") );
26f86486 108
02b7b6b0 109 gs_wxClipboardIsOpen = false;
26f86486
VZ
110
111 if ( ::CloseClipboard() == 0 )
112 {
113 wxLogSysError(_("Failed to close the clipboard."));
114
02b7b6b0 115 return false;
26f86486
VZ
116 }
117
02b7b6b0 118 return true;
2bda0e17
KB
119}
120
26f86486 121bool wxEmptyClipboard()
2bda0e17 122{
26f86486
VZ
123 if ( ::EmptyClipboard() == 0 )
124 {
125 wxLogSysError(_("Failed to empty the clipboard."));
126
02b7b6b0 127 return false;
26f86486
VZ
128 }
129
02b7b6b0 130 return true;
2bda0e17
KB
131}
132
26f86486 133bool wxIsClipboardOpened()
2bda0e17 134{
26f86486 135 return gs_wxClipboardIsOpen;
2bda0e17
KB
136}
137
06e43511 138bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat)
2bda0e17 139{
8cc4850c 140 wxDataFormat::NativeFormat cf = dataFormat.GetFormatId();
6c29712c
JS
141
142 if ( ::IsClipboardFormatAvailable(cf) )
d9317fd4
VZ
143 {
144 // ok from the first try
02b7b6b0 145 return true;
d9317fd4
VZ
146 }
147
148 // for several standard formats, we can convert from some other ones too
6c29712c 149 switch ( cf )
d9317fd4
VZ
150 {
151 // for bitmaps, DIBs will also do
152 case CF_BITMAP:
153 return ::IsClipboardFormatAvailable(CF_DIB) != 0;
154
3a5bcc4d 155#if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
d9317fd4
VZ
156 case CF_METAFILEPICT:
157 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0;
158#endif // wxUSE_ENH_METAFILE
159
160 default:
02b7b6b0 161 return false;
d9317fd4 162 }
2bda0e17
KB
163}
164
e9196d9c 165
26f86486
VZ
166bool wxSetClipboardData(wxDataFormat dataFormat,
167 const void *data,
168 int width, int height)
2bda0e17 169{
26f86486
VZ
170 HANDLE handle = 0; // return value of SetClipboardData
171
172 switch (dataFormat)
2bda0e17 173 {
26f86486
VZ
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);
02b7b6b0 190 return false;
26f86486
VZ
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
4676948b 210#if wxUSE_WXDIB
26f86486
VZ
211 case wxDF_DIB:
212 {
26f86486 213 wxBitmap *bitmap = (wxBitmap *)data;
2b254edf 214
c2517b52 215 if ( bitmap && bitmap->Ok() )
2b254edf 216 {
c2517b52
VZ
217 wxDIB dib(*bitmap);
218 if ( dib.IsOk() )
219 {
220 handle = ::SetClipboardData(CF_DIB, dib.Detach());
221 }
2b254edf 222 }
26f86486
VZ
223 break;
224 }
4676948b 225#endif
26f86486 226
d9317fd4 227 // VZ: I'm told that this code works, but it doesn't seem to work for me
4676948b 228 // and, anyhow, I'd be highly surprised if it did. So I leave it here
d9317fd4 229 // but IMNSHO it is completely broken.
4676948b 230#if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
26f86486
VZ
231 case wxDF_METAFILE:
232 {
233 wxMetafile *wxMF = (wxMetafile *)data;
234 HANDLE data = GlobalAlloc(GHND, sizeof(METAFILEPICT) + 1);
26f86486 235 METAFILEPICT *mf = (METAFILEPICT *)GlobalLock(data);
2bda0e17 236
26f86486
VZ
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);
2bda0e17 243
26f86486
VZ
244 handle = SetClipboardData(CF_METAFILEPICT, data);
245 break;
246 }
d9317fd4
VZ
247#endif // wxUSE_METAFILE
248
3a5bcc4d 249#if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
d9317fd4
VZ
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
26f86486
VZ
261 case CF_SYLK:
262 case CF_DIF:
263 case CF_TIFF:
264 case CF_PALETTE:
265 default:
266 {
267 wxLogError(_("Unsupported clipboard format."));
02b7b6b0 268 return false;
26f86486 269 }
2bda0e17 270
26f86486
VZ
271 case wxDF_OEMTEXT:
272 dataFormat = wxDF_TEXT;
273 // fall through
2bda0e17 274
26f86486
VZ
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 {
26f86486 285 LPSTR lpGlobalMemory = (LPSTR)GlobalLock(hGlobalMemory);
2bda0e17 286
26f86486 287 memcpy(lpGlobalMemory, s, l);
2bda0e17 288
26f86486
VZ
289 GlobalUnlock(hGlobalMemory);
290 }
2bda0e17 291
26f86486
VZ
292 handle = SetClipboardData(dataFormat, hGlobalMemory);
293 break;
294 }
6270539b
JS
295 // Only tested with Visual C++ 6.0 so far
296#if defined(__VISUALC__)
387ebd3e
JS
297 case wxDF_HTML:
298 {
299 char* html = (char *)data;
02b7b6b0 300
387ebd3e
JS
301 // Create temporary buffer for HTML header...
302 char *buf = new char [400 + strlen(html)];
02b7b6b0
WS
303 if(!buf) return false;
304
387ebd3e
JS
305 // Get clipboard id for HTML format...
306 static int cfid = 0;
307 if(!cfid) cfid = RegisterClipboardFormat(wxT("HTML Format"));
02b7b6b0 308
387ebd3e
JS
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");
02b7b6b0 318
387ebd3e
JS
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>");
02b7b6b0 327
387ebd3e
JS
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");
6270539b 333 sprintf(ptr+10, "%08u", strstr(buf, "<html>") - buf);
387ebd3e 334 *(ptr+10+8) = '\r';
02b7b6b0 335
387ebd3e 336 ptr = strstr(buf, "EndHTML");
6270539b 337 sprintf(ptr+8, "%08u", strlen(buf));
387ebd3e 338 *(ptr+8+8) = '\r';
02b7b6b0 339
387ebd3e 340 ptr = strstr(buf, "StartFragment");
6270539b 341 sprintf(ptr+14, "%08u", strstr(buf, "<!--StartFrag") - buf);
387ebd3e 342 *(ptr+14+8) = '\r';
02b7b6b0 343
387ebd3e 344 ptr = strstr(buf, "EndFragment");
6270539b 345 sprintf(ptr+12, "%08u", strstr(buf, "<!--EndFrag") - buf);
387ebd3e 346 *(ptr+12+8) = '\r';
02b7b6b0 347
387ebd3e
JS
348 // Now you have everything in place ready to put on the
349 // clipboard.
02b7b6b0 350
387ebd3e
JS
351 // Allocate global memory for transfer...
352 HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(buf)+4);
02b7b6b0 353
387ebd3e
JS
354 // Put your string in the global memory...
355 ptr = (char *)GlobalLock(hText);
356 strcpy(ptr, buf);
357 GlobalUnlock(hText);
02b7b6b0 358
387ebd3e 359 handle = ::SetClipboardData(cfid, hText);
02b7b6b0 360
387ebd3e
JS
361 // Free memory...
362 GlobalFree(hText);
02b7b6b0 363
387ebd3e
JS
364 // Clean up...
365 delete [] buf;
366 break;
367 }
368#endif
2bda0e17 369 }
26f86486
VZ
370
371 if ( handle == 0 )
2bda0e17 372 {
26f86486
VZ
373 wxLogSysError(_("Failed to set clipboard data."));
374
02b7b6b0 375 return false;
2bda0e17 376 }
26f86486 377
02b7b6b0 378 return true;
26f86486
VZ
379}
380
381void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
382{
383 void *retval = NULL;
384
385 switch ( dataFormat )
2bda0e17 386 {
4676948b 387#ifndef __WXWINCE__
26f86486
VZ
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);
26f86486
VZ
429 retval = wxBM;
430 break;
431 }
4676948b 432#endif
26f86486
VZ
433 case wxDF_METAFILE:
434 case CF_SYLK:
435 case CF_DIF:
436 case CF_TIFF:
437 case CF_PALETTE:
438 case wxDF_DIB:
f7f50f49
VZ
439 wxLogError(_("Unsupported clipboard format."));
440 return NULL;
26f86486
VZ
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;
2bda0e17 451
26f86486
VZ
452 DWORD hsize = ::GlobalSize(hGlobalMemory);
453 if (len)
454 *len = hsize;
2bda0e17 455
26f86486
VZ
456 char *s = new char[hsize];
457 if (!s)
458 break;
2bda0e17 459
4676948b 460 LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
2bda0e17 461
26f86486 462 memcpy(s, lpGlobalMemory, hsize);
2bda0e17 463
4676948b 464 GlobalUnlock(hGlobalMemory);
26f86486
VZ
465
466 retval = s;
467 break;
468 }
3f480da3
VZ
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
4676948b 484 LPSTR lpGlobalMemory = (LPSTR) GlobalLock(hGlobalMemory);
3f480da3
VZ
485
486 memcpy(buf, lpGlobalMemory, size);
487
4676948b 488 GlobalUnlock(hGlobalMemory);
3f480da3
VZ
489
490 retval = buf;
491 break;
492 }
26f86486 493 }
2bda0e17 494
26f86486
VZ
495 if ( !retval )
496 {
497 wxLogSysError(_("Failed to retrieve data from the clipboard."));
2bda0e17 498 }
26f86486
VZ
499
500 return retval;
2bda0e17
KB
501}
502
3f480da3 503wxDataFormat wxEnumClipboardFormats(wxDataFormat dataFormat)
2bda0e17 504{
33ac7e6f 505 return (wxDataFormat::NativeFormat)::EnumClipboardFormats(dataFormat);
2bda0e17
KB
506}
507
837e5743 508int wxRegisterClipboardFormat(wxChar *formatName)
2bda0e17
KB
509{
510 return ::RegisterClipboardFormat(formatName);
511}
512
26f86486 513bool wxGetClipboardFormatName(wxDataFormat dataFormat,
837e5743 514 wxChar *formatName,
26f86486 515 int maxCount)
2bda0e17 516{
26f86486 517 return ::GetClipboardFormatName((int)dataFormat, formatName, maxCount) > 0;
2bda0e17
KB
518}
519
26f86486 520// ---------------------------------------------------------------------------
06e43511 521// wxClipboard
26f86486 522// ---------------------------------------------------------------------------
2bda0e17 523
26f86486 524IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
2bda0e17
KB
525
526wxClipboard::wxClipboard()
527{
794fe8cd
VZ
528#if wxUSE_OLE_CLIPBOARD
529 wxOleInitialize();
530#endif
531
c039fef5 532 m_lastDataObject = NULL;
02b7b6b0 533 m_isOpened = false;
2bda0e17
KB
534}
535
536wxClipboard::~wxClipboard()
537{
c039fef5 538 if ( m_lastDataObject )
d59ceba5 539 {
0edfdd96 540 Clear();
d59ceba5 541 }
794fe8cd
VZ
542
543#if wxUSE_OLE_CLIPBOARD
544 wxOleUninitialize();
545#endif
2bda0e17
KB
546}
547
06e43511 548void wxClipboard::Clear()
2bda0e17 549{
9005f2ed
VZ
550 if ( IsUsingPrimarySelection() )
551 return;
552
d59ceba5 553#if wxUSE_OLE_CLIPBOARD
c039fef5
VZ
554 if (m_lastDataObject)
555 {
556 // don't touch data set by other applications
557 HRESULT hr = OleIsCurrentClipboard(m_lastDataObject);
558 if (S_OK == hr)
559 {
560 hr = OleSetClipboard(NULL);
164e8d41
PC
561 if ( FAILED(hr) )
562 {
563 wxLogApiError(wxT("OleSetClipboard(NULL)"), hr);
564 }
c039fef5
VZ
565 }
566 m_lastDataObject = NULL;
567 }
110a0fea 568#endif // wxUSE_OLE_CLIPBOARD
d59ceba5
VZ
569}
570
571bool wxClipboard::Flush()
572{
7ffdaf81 573#if wxUSE_OLE_CLIPBOARD
c039fef5
VZ
574 if (m_lastDataObject)
575 {
576 // don't touch data set by other applications
577 HRESULT hr = OleIsCurrentClipboard(m_lastDataObject);
578 m_lastDataObject = NULL;
579 if (S_OK == hr)
580 {
581 hr = OleFlushClipboard();
164e8d41
PC
582 if ( FAILED(hr) )
583 {
584 wxLogApiError(wxT("OleFlushClipboard"), hr);
d59ceba5 585
164e8d41
PC
586 return false;
587 }
588 return true;
589 }
c039fef5
VZ
590 }
591 return false;
7ffdaf81 592#else // !wxUSE_OLE_CLIPBOARD
02b7b6b0 593 return false;
7ffdaf81 594#endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
2bda0e17
KB
595}
596
06e43511 597bool wxClipboard::Open()
2bda0e17 598{
d59ceba5 599 // OLE opens clipboard for us
02b7b6b0 600 m_isOpened = true;
d59ceba5 601#if wxUSE_OLE_CLIPBOARD
02b7b6b0 602 return true;
d59ceba5 603#else
06e43511 604 return wxOpenClipboard();
d59ceba5 605#endif
2bda0e17
KB
606}
607
f536e0f2
VZ
608bool wxClipboard::IsOpened() const
609{
610#if wxUSE_OLE_CLIPBOARD
a36d790a 611 return m_isOpened;
f536e0f2
VZ
612#else
613 return wxIsClipboardOpened();
614#endif
615}
616
06e43511 617bool wxClipboard::SetData( wxDataObject *data )
2bda0e17 618{
9005f2ed
VZ
619 if ( IsUsingPrimarySelection() )
620 return false;
621
51edda6a 622#if !wxUSE_OLE_CLIPBOARD
26f86486 623 (void)wxEmptyClipboard();
51edda6a 624#endif // wxUSE_OLE_CLIPBOARD
26f86486
VZ
625
626 if ( data )
627 return AddData(data);
628 else
02b7b6b0 629 return true;
26f86486
VZ
630}
631
632bool wxClipboard::AddData( wxDataObject *data )
633{
9005f2ed
VZ
634 if ( IsUsingPrimarySelection() )
635 return false;
636
02b7b6b0 637 wxCHECK_MSG( data, false, wxT("data is invalid") );
06e43511 638
d59ceba5
VZ
639#if wxUSE_OLE_CLIPBOARD
640 HRESULT hr = OleSetClipboard(data->GetInterface());
641 if ( FAILED(hr) )
642 {
643 wxLogSysError(hr, _("Failed to put data on the clipboard"));
644
645 // don't free anything in this case
646
02b7b6b0 647 return false;
d59ceba5
VZ
648 }
649
c039fef5
VZ
650 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
651 // using OLE clipboard when the app terminates - by default, we call
652 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
653 // wxClipboard::Flush() to change this
654 m_lastDataObject = data->GetInterface();
655
d59ceba5
VZ
656 // we have a problem here because we should delete wxDataObject, but we
657 // can't do it because IDataObject which we just gave to the clipboard
658 // would try to use it when it will need the data. IDataObject is ref
659 // counted and so doesn't suffer from such problem, so we release it now
660 // and tell it to delete wxDataObject when it is deleted itself.
661 data->SetAutoDelete();
662
02b7b6b0 663 return true;
d59ceba5 664#elif wxUSE_DATAOBJ
02b7b6b0 665 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
26f86486 666
7fc0bd1c 667 wxDataFormat format = data->GetPreferredFormat();
26f86486
VZ
668
669 switch ( format )
06e43511
JS
670 {
671 case wxDF_TEXT:
672 case wxDF_OEMTEXT:
673 {
674 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
675 wxString str(textDataObject->GetText());
26f86486 676 return wxSetClipboardData(format, str.c_str());
06e43511 677 }
26f86486 678
06e43511
JS
679 case wxDF_BITMAP:
680 case wxDF_DIB:
681 {
682 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
683 wxBitmap bitmap(bitmapDataObject->GetBitmap());
7fc0bd1c 684 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
06e43511 685 }
26f86486 686
06e43511
JS
687#if wxUSE_METAFILE
688 case wxDF_METAFILE:
689 {
7fc0bd1c
JS
690#if 1
691 // TODO
0b4f47a3 692 wxLogError(wxT("Not implemented because wxMetafileDataObject does not contain width and height values."));
02b7b6b0 693 return false;
7fc0bd1c 694#else
33ac7e6f 695 wxMetafileDataObject* metaFileDataObject =
26f86486 696 (wxMetafileDataObject*) data;
06e43511 697 wxMetafile metaFile = metaFileDataObject->GetMetafile();
26f86486
VZ
698 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
699 metaFileDataObject->GetWidth(),
700 metaFileDataObject->GetHeight());
7fc0bd1c 701#endif
06e43511 702 }
26f86486
VZ
703#endif // wxUSE_METAFILE
704
06e43511 705 default:
7fc0bd1c
JS
706 {
707// This didn't compile, of course
708// return wxSetClipboardData(data);
709 // TODO
f07dc2e2 710 wxLogError(wxT("Not implemented."));
02b7b6b0 711 return false;
7fc0bd1c 712 }
06e43511 713 }
d59ceba5 714#else // !wxUSE_DATAOBJ
02b7b6b0 715 return false;
d59ceba5 716#endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
2bda0e17
KB
717}
718
06e43511 719void wxClipboard::Close()
2bda0e17 720{
02b7b6b0 721 m_isOpened = false;
d59ceba5
VZ
722 // OLE closes clipboard for us
723#if !wxUSE_OLE_CLIPBOARD
06e43511 724 wxCloseClipboard();
d59ceba5 725#endif
2bda0e17
KB
726}
727
2af18715 728bool wxClipboard::IsSupported( const wxDataFormat& format )
2bda0e17 729{
9005f2ed 730 return !IsUsingPrimarySelection() && wxIsClipboardFormatAvailable(format);
2bda0e17
KB
731}
732
1e8335b0 733bool wxClipboard::GetData( wxDataObject& data )
2bda0e17 734{
9005f2ed
VZ
735 if ( IsUsingPrimarySelection() )
736 return false;
737
d59ceba5
VZ
738#if wxUSE_OLE_CLIPBOARD
739 IDataObject *pDataObject = NULL;
740 HRESULT hr = OleGetClipboard(&pDataObject);
741 if ( FAILED(hr) || !pDataObject )
742 {
743 wxLogSysError(hr, _("Failed to get data from the clipboard"));
744
02b7b6b0 745 return false;
d59ceba5
VZ
746 }
747
748 // build the list of supported formats
1e8335b0 749 size_t nFormats = data.GetFormatCount(wxDataObject::Set);
33ac7e6f 750 wxDataFormat format;
c5639a87 751 wxDataFormat *formats;
d59ceba5
VZ
752 if ( nFormats == 1 )
753 {
754 // the most common case
755 formats = &format;
756 }
757 else
758 {
759 // bad luck, need to alloc mem
760 formats = new wxDataFormat[nFormats];
761 }
762
1e8335b0 763 data.GetAllFormats(formats, wxDataObject::Set);
d59ceba5 764
6c29712c
JS
765 // get the data for the given formats
766 FORMATETC formatEtc;
767 CLIPFORMAT cf;
02b7b6b0 768 bool result = false;
6c29712c
JS
769
770 // enumerate all explicit formats on the clipboard.
771 // note that this does not include implicit / synthetic (automatically
772 // converted) formats.
773#ifdef __WXDEBUG__
774 // get the format enumerator
d59ceba5
VZ
775 IEnumFORMATETC *pEnumFormatEtc = NULL;
776 hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
777 if ( FAILED(hr) || !pEnumFormatEtc )
778 {
779 wxLogSysError(hr,
780 _("Failed to retrieve the supported clipboard formats"));
781 }
782 else
783 {
784 // ask for the supported formats and see if there are any we support
d59ceba5
VZ
785 for ( ;; )
786 {
787 ULONG nCount;
788 hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount);
789
790 // don't use FAILED() because S_FALSE would pass it
791 if ( hr != S_OK )
792 {
793 // no more formats
794 break;
795 }
796
6c29712c 797 cf = formatEtc.cfFormat;
d59ceba5 798
d59ceba5
VZ
799 wxLogTrace(wxTRACE_OleCalls,
800 wxT("Object on the clipboard supports format %s."),
801 wxDataObject::GetFormatName(cf));
d59ceba5
VZ
802 }
803
804 pEnumFormatEtc->Release();
805 }
6c29712c 806#endif // Debug
d59ceba5 807
6c29712c
JS
808 STGMEDIUM medium;
809 // stop at the first valid format found on the clipboard
810 for ( size_t n = 0; !result && (n < nFormats); n++ )
d59ceba5 811 {
6c29712c
JS
812 // convert to NativeFormat Id
813 cf = formats[n].GetFormatId();
02b7b6b0 814
6c29712c
JS
815 // if the format is not available, try the next one
816 // this test includes implicit / sythetic formats
817 if ( !::IsClipboardFormatAvailable(cf) )
818 continue;
02b7b6b0 819
6c29712c 820 formatEtc.cfFormat = cf;
d59ceba5
VZ
821 formatEtc.ptd = NULL;
822 formatEtc.dwAspect = DVASPECT_CONTENT;
823 formatEtc.lindex = -1;
d59ceba5 824
6c29712c
JS
825 // use the appropriate tymed
826 switch ( formatEtc.cfFormat )
d59ceba5 827 {
6c29712c
JS
828 case CF_BITMAP:
829 formatEtc.tymed = TYMED_GDI;
830 break;
d59ceba5 831
4676948b 832#ifndef __WXWINCE__
6c29712c
JS
833 case CF_METAFILEPICT:
834 formatEtc.tymed = TYMED_MFPICT;
835 break;
265b0c07 836
6c29712c
JS
837 case CF_ENHMETAFILE:
838 formatEtc.tymed = TYMED_ENHMF;
839 break;
4676948b 840#endif
265b0c07 841
6c29712c
JS
842 default:
843 formatEtc.tymed = TYMED_HGLOBAL;
844 }
845
846 // try to get data
847 hr = pDataObject->GetData(&formatEtc, &medium);
848 if ( FAILED(hr) )
849 {
850 // try other tymed for GDI objects
851 if ( formatEtc.cfFormat == CF_BITMAP )
d59ceba5 852 {
6c29712c
JS
853 formatEtc.tymed = TYMED_HGLOBAL;
854 hr = pDataObject->GetData(&formatEtc, &medium);
d59ceba5 855 }
6c29712c 856 }
d59ceba5 857
6c29712c
JS
858 if ( SUCCEEDED(hr) )
859 {
860 // pass the data to the data object
02b7b6b0 861 hr = data.GetInterface()->SetData(&formatEtc, &medium, true);
6c29712c 862 if ( FAILED(hr) )
d59ceba5 863 {
6c29712c 864 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
d59ceba5 865
6c29712c
JS
866 // IDataObject only takes the ownership of data if it
867 // successfully got it - which is not the case here
868 ReleaseStgMedium(&medium);
869 }
870 else
871 {
02b7b6b0 872 result = true;
d59ceba5 873 }
d59ceba5 874 }
6c29712c 875 //else: unsupported tymed?
d59ceba5 876 }
6c29712c
JS
877
878 if ( formats != &format )
879 {
880 delete [] formats;
881 }
882 //else: we didn't allocate any memory
d59ceba5
VZ
883
884 // clean up and return
885 pDataObject->Release();
886
887 return result;
888#elif wxUSE_DATAOBJ
02b7b6b0 889 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
26f86486 890
7fc0bd1c 891 wxDataFormat format = data.GetPreferredFormat();
26f86486 892 switch ( format )
06e43511
JS
893 {
894 case wxDF_TEXT:
895 case wxDF_OEMTEXT:
896 {
1e8335b0
VZ
897 wxTextDataObject& textDataObject = (wxTextDataObject &)data;
898 char* s = (char*)wxGetClipboardData(format);
899 if ( !s )
02b7b6b0 900 return false;
1e8335b0 901
f07dc2e2 902 textDataObject.SetText(wxString::FromAscii(s));
1e8335b0
VZ
903 delete [] s;
904
02b7b6b0 905 return true;
06e43511 906 }
26f86486 907
06e43511
JS
908 case wxDF_BITMAP:
909 case wxDF_DIB:
910 {
1e8335b0 911 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
7fc0bd1c 912 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat());
1e8335b0 913 if ( !bitmap )
02b7b6b0 914 return false;
1e8335b0
VZ
915
916 bitmapDataObject.SetBitmap(*bitmap);
917 delete bitmap;
918
02b7b6b0 919 return true;
06e43511
JS
920 }
921#if wxUSE_METAFILE
922 case wxDF_METAFILE:
923 {
1e8335b0 924 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
26f86486 925 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
1e8335b0 926 if ( !metaFile )
02b7b6b0 927 return false;
3f480da3 928
1e8335b0
VZ
929 metaFileDataObject.SetMetafile(*metaFile);
930 delete metaFile;
26f86486 931
02b7b6b0 932 return true;
1e8335b0
VZ
933 }
934#endif // wxUSE_METAFILE
06e43511 935 }
02b7b6b0 936 return false;
d59ceba5 937#else // !wxUSE_DATAOBJ
1e8335b0 938 wxFAIL_MSG( wxT("no clipboard implementation") );
02b7b6b0 939 return false;
3897b707 940#endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
2bda0e17
KB
941}
942
47d67540 943#endif // wxUSE_CLIPBOARD