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