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