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