1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #pragma implementation "clipbrd.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
38 #include "wx/object.h"
42 #include "wx/bitmap.h"
48 #include "wx/metafile.h"
52 #include "wx/clipbrd.h"
57 #include "wx/msw/private.h"
58 #include "wx/msw/dib.h"
60 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
61 // the functions using wxDataObject in wxClipboard
62 #define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
65 #include "wx/dataobj.h"
68 #define wxUSE_OLE_CLIPBOARD 1
69 #else // !wxUSE_DATAOBJ
70 // use Win clipboard API
71 #define wxUSE_OLE_CLIPBOARD 0
74 #if wxUSE_OLE_CLIPBOARD
76 #endif // wxUSE_OLE_CLIPBOARD
79 #define memcpy hmemcpy
82 // ===========================================================================
84 // ===========================================================================
86 // ---------------------------------------------------------------------------
87 // old-style clipboard functions using Windows API
88 // ---------------------------------------------------------------------------
90 static bool gs_wxClipboardIsOpen
= FALSE
;
92 bool wxOpenClipboard()
94 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, wxT("clipboard already opened.") );
96 wxWindow
*win
= wxTheApp
->GetTopWindow();
99 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
101 if ( !gs_wxClipboardIsOpen
)
102 wxLogSysError(_("Failed to open the clipboard."));
104 return gs_wxClipboardIsOpen
;
108 wxLogDebug(wxT("Can not open clipboard without a main window."));
114 bool wxCloseClipboard()
116 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, wxT("clipboard is not opened") );
118 gs_wxClipboardIsOpen
= FALSE
;
120 if ( ::CloseClipboard() == 0 )
122 wxLogSysError(_("Failed to close the clipboard."));
130 bool wxEmptyClipboard()
132 if ( ::EmptyClipboard() == 0 )
134 wxLogSysError(_("Failed to empty the clipboard."));
142 bool wxIsClipboardOpened()
144 return gs_wxClipboardIsOpen
;
147 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
149 if ( ::IsClipboardFormatAvailable(dataFormat
) )
151 // ok from the first try
155 // for several standard formats, we can convert from some other ones too
156 switch ( dataFormat
.GetFormatId() )
158 // for bitmaps, DIBs will also do
160 return ::IsClipboardFormatAvailable(CF_DIB
) != 0;
162 #if wxUSE_ENH_METAFILE && !defined(__WIN16__)
163 case CF_METAFILEPICT
:
164 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
165 #endif // wxUSE_ENH_METAFILE
172 bool wxSetClipboardData(wxDataFormat dataFormat
,
174 int width
, int height
)
176 HANDLE handle
= 0; // return value of SetClipboardData
182 wxBitmap
*bitmap
= (wxBitmap
*)data
;
184 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
185 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
186 HBITMAP old
= (HBITMAP
)
187 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
188 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
190 bitmap
->GetHeight());
193 SelectObject(hdcSrc
, old
);
199 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
200 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
201 hdcSrc
, 0, 0, SRCCOPY
);
203 // Select new bitmap out of memory DC
204 SelectObject(hdcMem
, old1
);
207 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
210 SelectObject(hdcSrc
, old
);
218 #if wxUSE_IMAGE_LOADING_IN_MSW
219 wxBitmap
*bitmap
= (wxBitmap
*)data
;
220 HBITMAP hBitmap
= (HBITMAP
)bitmap
->GetHBITMAP();
221 // NULL palette means to use the system one
222 HANDLE hDIB
= wxBitmapToDIB(hBitmap
, (HPALETTE
)NULL
);
223 handle
= SetClipboardData(CF_DIB
, hDIB
);
224 #endif // wxUSE_IMAGE_LOADING_IN_MSW
228 // VZ: I'm told that this code works, but it doesn't seem to work for me
229 // and, anyhow, I'd be highly surprized if it did. So I leave it here
230 // but IMNSHO it is completely broken.
231 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH)
234 wxMetafile
*wxMF
= (wxMetafile
*)data
;
235 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
236 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
238 mf
->mm
= wxMF
->GetWindowsMappingMode();
241 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
243 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
245 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
248 #endif // wxUSE_METAFILE
250 #if wxUSE_ENH_METAFILE && !defined(__WIN16__)
251 case wxDF_ENHMETAFILE
:
253 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
254 wxEnhMetaFile emfCopy
= *emf
;
256 handle
= SetClipboardData(CF_ENHMETAFILE
,
257 (void *)emfCopy
.GetHENHMETAFILE());
260 #endif // wxUSE_ENH_METAFILE
268 wxLogError(_("Unsupported clipboard format."));
273 dataFormat
= wxDF_TEXT
;
278 char *s
= (char *)data
;
280 width
= strlen(s
) + 1;
282 DWORD l
= (width
* height
);
283 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
286 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
288 memcpy(lpGlobalMemory
, s
, l
);
290 GlobalUnlock(hGlobalMemory
);
293 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
300 wxLogSysError(_("Failed to set clipboard data."));
308 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
312 switch ( dataFormat
)
317 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
321 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
322 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
324 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
325 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
327 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
331 SelectObject(hdcSrc
, old
);
337 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
338 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
339 hdcSrc
, 0, 0, SRCCOPY
);
341 // Select new bitmap out of memory DC
342 SelectObject(hdcMem
, old1
);
345 SelectObject(hdcSrc
, old
);
349 // Create and return a new wxBitmap
350 wxBitmap
*wxBM
= new wxBitmap
;
351 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
352 wxBM
->SetWidth(bm
.bmWidth
);
353 wxBM
->SetHeight(bm
.bmHeight
);
354 wxBM
->SetDepth(bm
.bmPlanes
);
355 #if WXWIN_COMPATIBILITY_2
357 #endif // WXWIN_COMPATIBILITY_2
369 wxLogError(_("Unsupported clipboard format."));
374 dataFormat
= wxDF_TEXT
;
379 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
383 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
387 char *s
= new char[hsize
];
391 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
393 memcpy(s
, lpGlobalMemory
, hsize
);
395 ::GlobalUnlock(hGlobalMemory
);
403 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
404 if ( !hGlobalMemory
)
407 DWORD size
= ::GlobalSize(hGlobalMemory
);
411 void *buf
= malloc(size
);
415 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
417 memcpy(buf
, lpGlobalMemory
, size
);
419 ::GlobalUnlock(hGlobalMemory
);
428 wxLogSysError(_("Failed to retrieve data from the clipboard."));
434 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
436 return ::EnumClipboardFormats(dataFormat
);
439 int wxRegisterClipboardFormat(wxChar
*formatName
)
441 return ::RegisterClipboardFormat(formatName
);
444 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
448 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
451 // ---------------------------------------------------------------------------
453 // ---------------------------------------------------------------------------
455 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
457 wxClipboard::wxClipboard()
459 m_clearOnExit
= FALSE
;
462 wxClipboard::~wxClipboard()
470 void wxClipboard::Clear()
472 #if wxUSE_OLE_CLIPBOARD
473 if ( FAILED(OleSetClipboard(NULL
)) )
475 wxLogLastError(wxT("OleSetClipboard(NULL)"));
480 bool wxClipboard::Flush()
482 #if wxUSE_OLE_CLIPBOARD
483 if ( FAILED(OleFlushClipboard()) )
485 wxLogLastError(wxT("OleFlushClipboard"));
491 m_clearOnExit
= FALSE
;
495 #else // !wxUSE_OLE_CLIPBOARD
497 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
500 bool wxClipboard::Open()
502 // OLE opens clipboard for us
503 #if wxUSE_OLE_CLIPBOARD
506 return wxOpenClipboard();
510 bool wxClipboard::IsOpened() const
512 #if wxUSE_OLE_CLIPBOARD
515 return wxIsClipboardOpened();
519 bool wxClipboard::SetData( wxDataObject
*data
)
521 #if !wxUSE_OLE_CLIPBOARD
522 (void)wxEmptyClipboard();
523 #endif // wxUSE_OLE_CLIPBOARD
526 return AddData(data
);
531 bool wxClipboard::AddData( wxDataObject
*data
)
533 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
535 #if wxUSE_OLE_CLIPBOARD
536 HRESULT hr
= OleSetClipboard(data
->GetInterface());
539 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
541 // don't free anything in this case
546 // we have a problem here because we should delete wxDataObject, but we
547 // can't do it because IDataObject which we just gave to the clipboard
548 // would try to use it when it will need the data. IDataObject is ref
549 // counted and so doesn't suffer from such problem, so we release it now
550 // and tell it to delete wxDataObject when it is deleted itself.
551 data
->SetAutoDelete();
553 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
554 // using OLE clipboard when the app terminates - by default, we call
555 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
556 // wxClipboard::Flush() to chaneg this
557 m_clearOnExit
= TRUE
;
561 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
563 wxDataFormat format
= data
->GetFormat();
570 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
571 wxString
str(textDataObject
->GetText());
572 return wxSetClipboardData(format
, str
.c_str());
578 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
579 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
580 return wxSetClipboardData(data
->GetFormat(), &bitmap
);
586 wxMetafileDataObject
* metaFileDataObject
=
587 (wxMetafileDataObject
*) data
;
588 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
589 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
590 metaFileDataObject
->GetWidth(),
591 metaFileDataObject
->GetHeight());
593 #endif // wxUSE_METAFILE
596 return wxSetClipboardData(data
);
598 #else // !wxUSE_DATAOBJ
600 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
603 void wxClipboard::Close()
605 // OLE closes clipboard for us
606 #if !wxUSE_OLE_CLIPBOARD
611 bool wxClipboard::IsSupported( wxDataFormat format
)
613 return wxIsClipboardFormatAvailable(format
);
616 bool wxClipboard::GetData( wxDataObject
& data
)
618 #if wxUSE_OLE_CLIPBOARD
619 IDataObject
*pDataObject
= NULL
;
620 HRESULT hr
= OleGetClipboard(&pDataObject
);
621 if ( FAILED(hr
) || !pDataObject
)
623 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
628 // build the list of supported formats
629 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
630 wxDataFormat format
, *formats
;
633 // the most common case
638 // bad luck, need to alloc mem
639 formats
= new wxDataFormat
[nFormats
];
642 data
.GetAllFormats(formats
, wxDataObject::Set
);
644 // get the format enumerator
646 wxArrayInt supportedFormats
;
647 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
648 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
649 if ( FAILED(hr
) || !pEnumFormatEtc
)
652 _("Failed to retrieve the supported clipboard formats"));
656 // ask for the supported formats and see if there are any we support
661 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
663 // don't use FAILED() because S_FALSE would pass it
670 CLIPFORMAT cf
= formatEtc
.cfFormat
;
673 wxLogTrace(wxTRACE_OleCalls
,
674 wxT("Object on the clipboard supports format %s."),
675 wxDataObject::GetFormatName(cf
));
679 for ( size_t n
= 0; n
< nFormats
; n
++ )
681 if ( formats
[n
].GetFormatId() == cf
)
683 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
685 supportedFormats
.Add(cf
);
691 pEnumFormatEtc
->Release();
694 if ( formats
!= &format
)
698 //else: we didn't allocate any memory
700 if ( !supportedFormats
.IsEmpty() )
703 formatEtc
.ptd
= NULL
;
704 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
705 formatEtc
.lindex
= -1;
707 size_t nSupportedFormats
= supportedFormats
.GetCount();
708 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
711 formatEtc
.cfFormat
= supportedFormats
[n
];
713 // use the appropriate tymed
714 switch ( formatEtc
.cfFormat
)
717 formatEtc
.tymed
= TYMED_GDI
;
720 case CF_METAFILEPICT
:
721 formatEtc
.tymed
= TYMED_MFPICT
;
725 formatEtc
.tymed
= TYMED_ENHMF
;
729 formatEtc
.tymed
= TYMED_HGLOBAL
;
733 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
736 // try other tymed for GDI objects
737 if ( formatEtc
.cfFormat
== CF_BITMAP
)
739 formatEtc
.tymed
= TYMED_HGLOBAL
;
740 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
746 // pass the data to the data object
747 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
750 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
752 // IDataObject only takes the ownership of data if it
753 // successfully got it - which is not the case here
754 ReleaseStgMedium(&medium
);
761 //else: unsupported tymed?
764 //else: unsupported format
766 // clean up and return
767 pDataObject
->Release();
771 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
773 wxDataFormat format
= data
.GetFormat();
779 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
780 char* s
= (char*)wxGetClipboardData(format
);
784 textDataObject
.SetText(s
);
793 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
794 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
->GetFormat());
798 bitmapDataObject
.SetBitmap(*bitmap
);
806 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
807 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
811 metaFileDataObject
.SetMetafile(*metaFile
);
816 #endif // wxUSE_METAFILE
819 #else // !wxUSE_DATAOBJ
820 wxFAIL_MSG( wxT("no clipboard implementation") );
822 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
826 // #error "Please turn wxUSE_CLIPBOARD on to compile this file."
827 #endif // wxUSE_CLIPBOARD