]> git.saurik.com Git - wxWidgets.git/blame - src/msw/clipbrd.cpp
update to make digitalmars compile/link make clean
[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;
2bda0e17
KB
542}
543
544wxClipboard::~wxClipboard()
545{
d59ceba5
VZ
546 if ( m_clearOnExit )
547 {
548 Clear();
549 }
2bda0e17
KB
550}
551
06e43511 552void wxClipboard::Clear()
2bda0e17 553{
d59ceba5
VZ
554#if wxUSE_OLE_CLIPBOARD
555 if ( FAILED(OleSetClipboard(NULL)) )
556 {
f6bcfd97 557 wxLogLastError(wxT("OleSetClipboard(NULL)"));
d59ceba5
VZ
558 }
559#endif
560}
561
562bool wxClipboard::Flush()
563{
7ffdaf81 564#if wxUSE_OLE_CLIPBOARD
d59ceba5
VZ
565 if ( FAILED(OleFlushClipboard()) )
566 {
f6bcfd97 567 wxLogLastError(wxT("OleFlushClipboard"));
d59ceba5
VZ
568
569 return FALSE;
570 }
571 else
572 {
573 m_clearOnExit = FALSE;
574
575 return TRUE;
576 }
7ffdaf81
VZ
577#else // !wxUSE_OLE_CLIPBOARD
578 return FALSE;
579#endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
2bda0e17
KB
580}
581
06e43511 582bool wxClipboard::Open()
2bda0e17 583{
d59ceba5
VZ
584 // OLE opens clipboard for us
585#if wxUSE_OLE_CLIPBOARD
586 return TRUE;
587#else
06e43511 588 return wxOpenClipboard();
d59ceba5 589#endif
2bda0e17
KB
590}
591
f536e0f2
VZ
592bool wxClipboard::IsOpened() const
593{
594#if wxUSE_OLE_CLIPBOARD
595 return TRUE;
596#else
597 return wxIsClipboardOpened();
598#endif
599}
600
06e43511 601bool wxClipboard::SetData( wxDataObject *data )
2bda0e17 602{
51edda6a 603#if !wxUSE_OLE_CLIPBOARD
26f86486 604 (void)wxEmptyClipboard();
51edda6a 605#endif // wxUSE_OLE_CLIPBOARD
26f86486
VZ
606
607 if ( data )
608 return AddData(data);
609 else
610 return TRUE;
611}
612
613bool wxClipboard::AddData( wxDataObject *data )
614{
223d09f6 615 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
06e43511 616
d59ceba5
VZ
617#if wxUSE_OLE_CLIPBOARD
618 HRESULT hr = OleSetClipboard(data->GetInterface());
619 if ( FAILED(hr) )
620 {
621 wxLogSysError(hr, _("Failed to put data on the clipboard"));
622
623 // don't free anything in this case
624
625 return FALSE;
626 }
627
628 // we have a problem here because we should delete wxDataObject, but we
629 // can't do it because IDataObject which we just gave to the clipboard
630 // would try to use it when it will need the data. IDataObject is ref
631 // counted and so doesn't suffer from such problem, so we release it now
632 // and tell it to delete wxDataObject when it is deleted itself.
633 data->SetAutoDelete();
634
635 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
636 // using OLE clipboard when the app terminates - by default, we call
637 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
638 // wxClipboard::Flush() to chaneg this
639 m_clearOnExit = TRUE;
640
641 return TRUE;
642#elif wxUSE_DATAOBJ
223d09f6 643 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
26f86486 644
7fc0bd1c 645 wxDataFormat format = data->GetPreferredFormat();
26f86486
VZ
646
647 switch ( format )
06e43511
JS
648 {
649 case wxDF_TEXT:
650 case wxDF_OEMTEXT:
651 {
652 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
653 wxString str(textDataObject->GetText());
26f86486 654 return wxSetClipboardData(format, str.c_str());
06e43511 655 }
26f86486 656
06e43511
JS
657 case wxDF_BITMAP:
658 case wxDF_DIB:
659 {
660 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data;
661 wxBitmap bitmap(bitmapDataObject->GetBitmap());
7fc0bd1c 662 return wxSetClipboardData(data->GetPreferredFormat(), &bitmap);
06e43511 663 }
26f86486 664
06e43511
JS
665#if wxUSE_METAFILE
666 case wxDF_METAFILE:
667 {
7fc0bd1c
JS
668#if 1
669 // TODO
670 wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values.");
671 return FALSE;
672#else
33ac7e6f 673 wxMetafileDataObject* metaFileDataObject =
26f86486 674 (wxMetafileDataObject*) data;
06e43511 675 wxMetafile metaFile = metaFileDataObject->GetMetafile();
26f86486
VZ
676 return wxSetClipboardData(wxDF_METAFILE, &metaFile,
677 metaFileDataObject->GetWidth(),
678 metaFileDataObject->GetHeight());
7fc0bd1c 679#endif
06e43511 680 }
26f86486
VZ
681#endif // wxUSE_METAFILE
682
06e43511 683 default:
7fc0bd1c
JS
684 {
685// This didn't compile, of course
686// return wxSetClipboardData(data);
687 // TODO
688 wxLogError("Not implemented.");
689 return FALSE;
690 }
06e43511 691 }
d59ceba5 692#else // !wxUSE_DATAOBJ
06e43511 693 return FALSE;
d59ceba5 694#endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
2bda0e17
KB
695}
696
06e43511 697void wxClipboard::Close()
2bda0e17 698{
d59ceba5
VZ
699 // OLE closes clipboard for us
700#if !wxUSE_OLE_CLIPBOARD
06e43511 701 wxCloseClipboard();
d59ceba5 702#endif
2bda0e17
KB
703}
704
26f86486 705bool wxClipboard::IsSupported( wxDataFormat format )
2bda0e17 706{
06e43511 707 return wxIsClipboardFormatAvailable(format);
2bda0e17
KB
708}
709
1e8335b0 710bool wxClipboard::GetData( wxDataObject& data )
2bda0e17 711{
d59ceba5
VZ
712#if wxUSE_OLE_CLIPBOARD
713 IDataObject *pDataObject = NULL;
714 HRESULT hr = OleGetClipboard(&pDataObject);
715 if ( FAILED(hr) || !pDataObject )
716 {
717 wxLogSysError(hr, _("Failed to get data from the clipboard"));
718
719 return FALSE;
720 }
721
722 // build the list of supported formats
1e8335b0 723 size_t nFormats = data.GetFormatCount(wxDataObject::Set);
33ac7e6f 724 wxDataFormat format;
c5639a87 725 wxDataFormat *formats;
d59ceba5
VZ
726 if ( nFormats == 1 )
727 {
728 // the most common case
729 formats = &format;
730 }
731 else
732 {
733 // bad luck, need to alloc mem
734 formats = new wxDataFormat[nFormats];
735 }
736
1e8335b0 737 data.GetAllFormats(formats, wxDataObject::Set);
d59ceba5
VZ
738
739 // get the format enumerator
740 bool result = FALSE;
741 wxArrayInt supportedFormats;
742 IEnumFORMATETC *pEnumFormatEtc = NULL;
743 hr = pDataObject->EnumFormatEtc(DATADIR_GET, &pEnumFormatEtc);
744 if ( FAILED(hr) || !pEnumFormatEtc )
745 {
746 wxLogSysError(hr,
747 _("Failed to retrieve the supported clipboard formats"));
748 }
749 else
750 {
751 // ask for the supported formats and see if there are any we support
752 FORMATETC formatEtc;
753 for ( ;; )
754 {
755 ULONG nCount;
756 hr = pEnumFormatEtc->Next(1, &formatEtc, &nCount);
757
758 // don't use FAILED() because S_FALSE would pass it
759 if ( hr != S_OK )
760 {
761 // no more formats
762 break;
763 }
764
765 CLIPFORMAT cf = formatEtc.cfFormat;
766
767#ifdef __WXDEBUG__
768 wxLogTrace(wxTRACE_OleCalls,
769 wxT("Object on the clipboard supports format %s."),
770 wxDataObject::GetFormatName(cf));
771#endif // Debug
772
773 // is supported?
774 for ( size_t n = 0; n < nFormats; n++ )
775 {
776 if ( formats[n].GetFormatId() == cf )
777 {
778 if ( supportedFormats.Index(cf) == wxNOT_FOUND )
779 {
780 supportedFormats.Add(cf);
781 }
782 }
783 }
784 }
785
786 pEnumFormatEtc->Release();
787 }
788
789 if ( formats != &format )
790 {
791 delete [] formats;
792 }
793 //else: we didn't allocate any memory
794
795 if ( !supportedFormats.IsEmpty() )
796 {
797 FORMATETC formatEtc;
798 formatEtc.ptd = NULL;
799 formatEtc.dwAspect = DVASPECT_CONTENT;
800 formatEtc.lindex = -1;
d59ceba5
VZ
801
802 size_t nSupportedFormats = supportedFormats.GetCount();
803 for ( size_t n = 0; !result && (n < nSupportedFormats); n++ )
804 {
805 STGMEDIUM medium;
806 formatEtc.cfFormat = supportedFormats[n];
807
265b0c07
VZ
808 // use the appropriate tymed
809 switch ( formatEtc.cfFormat )
810 {
811 case CF_BITMAP:
812 formatEtc.tymed = TYMED_GDI;
813 break;
814
815 case CF_METAFILEPICT:
816 formatEtc.tymed = TYMED_MFPICT;
817 break;
818
d9317fd4
VZ
819 case CF_ENHMETAFILE:
820 formatEtc.tymed = TYMED_ENHMF;
821 break;
822
265b0c07
VZ
823 default:
824 formatEtc.tymed = TYMED_HGLOBAL;
825 }
826
d59ceba5
VZ
827 // try to get data
828 hr = pDataObject->GetData(&formatEtc, &medium);
829 if ( FAILED(hr) )
830 {
831 // try other tymed for GDI objects
832 if ( formatEtc.cfFormat == CF_BITMAP )
833 {
834 formatEtc.tymed = TYMED_HGLOBAL;
835 hr = pDataObject->GetData(&formatEtc, &medium);
836 }
837 }
838
839 if ( SUCCEEDED(hr) )
840 {
841 // pass the data to the data object
1e8335b0 842 hr = data.GetInterface()->SetData(&formatEtc, &medium, TRUE);
d59ceba5
VZ
843 if ( FAILED(hr) )
844 {
845 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
846
847 // IDataObject only takes the ownership of data if it
848 // successfully got it - which is not the case here
849 ReleaseStgMedium(&medium);
850 }
851 else
852 {
853 result = TRUE;
854 }
855 }
856 //else: unsupported tymed?
857 }
858 }
859 //else: unsupported format
860
861 // clean up and return
862 pDataObject->Release();
863
864 return result;
865#elif wxUSE_DATAOBJ
223d09f6 866 wxCHECK_MSG( wxIsClipboardOpened(), FALSE, wxT("clipboard not open") );
26f86486 867
7fc0bd1c 868 wxDataFormat format = data.GetPreferredFormat();
26f86486 869 switch ( format )
06e43511
JS
870 {
871 case wxDF_TEXT:
872 case wxDF_OEMTEXT:
873 {
1e8335b0
VZ
874 wxTextDataObject& textDataObject = (wxTextDataObject &)data;
875 char* s = (char*)wxGetClipboardData(format);
876 if ( !s )
06e43511 877 return FALSE;
1e8335b0
VZ
878
879 textDataObject.SetText(s);
880 delete [] s;
881
882 return TRUE;
06e43511 883 }
26f86486 884
06e43511
JS
885 case wxDF_BITMAP:
886 case wxDF_DIB:
887 {
1e8335b0 888 wxBitmapDataObject& bitmapDataObject = (wxBitmapDataObject &)data;
7fc0bd1c 889 wxBitmap* bitmap = (wxBitmap *)wxGetClipboardData(data.GetPreferredFormat());
1e8335b0 890 if ( !bitmap )
06e43511 891 return FALSE;
1e8335b0
VZ
892
893 bitmapDataObject.SetBitmap(*bitmap);
894 delete bitmap;
895
896 return TRUE;
06e43511
JS
897 }
898#if wxUSE_METAFILE
899 case wxDF_METAFILE:
900 {
1e8335b0 901 wxMetafileDataObject& metaFileDataObject = (wxMetafileDataObject &)data;
26f86486 902 wxMetafile* metaFile = (wxMetafile *)wxGetClipboardData(wxDF_METAFILE);
1e8335b0 903 if ( !metaFile )
06e43511 904 return FALSE;
3f480da3 905
1e8335b0
VZ
906 metaFileDataObject.SetMetafile(*metaFile);
907 delete metaFile;
26f86486 908
1e8335b0
VZ
909 return TRUE;
910 }
911#endif // wxUSE_METAFILE
06e43511 912 }
3897b707 913 return FALSE;
d59ceba5 914#else // !wxUSE_DATAOBJ
1e8335b0 915 wxFAIL_MSG( wxT("no clipboard implementation") );
06e43511 916 return FALSE;
3897b707 917#endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
2bda0e17
KB
918}
919
47d67540 920#endif // wxUSE_CLIPBOARD
4ce81a75 921