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"
59 #ifndef __WXMICROWIN__
60 #include "wx/msw/dib.h"
63 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
64 // the functions using wxDataObject in wxClipboard
65 //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
68 #include "wx/dataobj.h"
70 // No: don't necessarily use OLE clipboard with data object
73 #define wxUSE_OLE_CLIPBOARD 1
75 #else // !wxUSE_DATAOBJ
76 // use Win clipboard API
77 #define wxUSE_OLE_CLIPBOARD 0
80 #if wxUSE_OLE_CLIPBOARD
82 #endif // wxUSE_OLE_CLIPBOARD
85 #define memcpy hmemcpy
88 // ===========================================================================
90 // ===========================================================================
92 // ---------------------------------------------------------------------------
93 // old-style clipboard functions using Windows API
94 // ---------------------------------------------------------------------------
96 static bool gs_wxClipboardIsOpen
= FALSE
;
98 bool wxOpenClipboard()
100 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, wxT("clipboard already opened.") );
102 wxWindow
*win
= wxTheApp
->GetTopWindow();
105 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
107 if ( !gs_wxClipboardIsOpen
)
108 wxLogSysError(_("Failed to open the clipboard."));
110 return gs_wxClipboardIsOpen
;
114 wxLogDebug(wxT("Can not open clipboard without a main window."));
120 bool wxCloseClipboard()
122 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, wxT("clipboard is not opened") );
124 gs_wxClipboardIsOpen
= FALSE
;
126 if ( ::CloseClipboard() == 0 )
128 wxLogSysError(_("Failed to close the clipboard."));
136 bool wxEmptyClipboard()
138 if ( ::EmptyClipboard() == 0 )
140 wxLogSysError(_("Failed to empty the clipboard."));
148 bool wxIsClipboardOpened()
150 return gs_wxClipboardIsOpen
;
153 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
155 if ( ::IsClipboardFormatAvailable(dataFormat
) )
157 // ok from the first try
161 // for several standard formats, we can convert from some other ones too
162 switch ( dataFormat
.GetFormatId() )
164 // for bitmaps, DIBs will also do
166 return ::IsClipboardFormatAvailable(CF_DIB
) != 0;
168 #if wxUSE_ENH_METAFILE && !defined(__WIN16__)
169 case CF_METAFILEPICT
:
170 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
171 #endif // wxUSE_ENH_METAFILE
178 bool wxSetClipboardData(wxDataFormat dataFormat
,
180 int width
, int height
)
182 HANDLE handle
= 0; // return value of SetClipboardData
188 wxBitmap
*bitmap
= (wxBitmap
*)data
;
190 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
191 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
192 HBITMAP old
= (HBITMAP
)
193 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
194 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
196 bitmap
->GetHeight());
199 SelectObject(hdcSrc
, old
);
205 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
206 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
207 hdcSrc
, 0, 0, SRCCOPY
);
209 // Select new bitmap out of memory DC
210 SelectObject(hdcMem
, old1
);
213 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
216 SelectObject(hdcSrc
, old
);
224 #if wxUSE_IMAGE_LOADING_IN_MSW
225 wxBitmap
*bitmap
= (wxBitmap
*)data
;
226 HBITMAP hBitmap
= (HBITMAP
)bitmap
->GetHBITMAP();
227 // NULL palette means to use the system one
228 HANDLE hDIB
= wxBitmapToDIB(hBitmap
, (HPALETTE
)NULL
);
229 handle
= SetClipboardData(CF_DIB
, hDIB
);
230 #endif // wxUSE_IMAGE_LOADING_IN_MSW
234 // VZ: I'm told that this code works, but it doesn't seem to work for me
235 // and, anyhow, I'd be highly surprized if it did. So I leave it here
236 // but IMNSHO it is completely broken.
237 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH)
240 wxMetafile
*wxMF
= (wxMetafile
*)data
;
241 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
242 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
244 mf
->mm
= wxMF
->GetWindowsMappingMode();
247 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
249 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
251 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
254 #endif // wxUSE_METAFILE
256 #if wxUSE_ENH_METAFILE && !defined(__WIN16__)
257 case wxDF_ENHMETAFILE
:
259 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
260 wxEnhMetaFile emfCopy
= *emf
;
262 handle
= SetClipboardData(CF_ENHMETAFILE
,
263 (void *)emfCopy
.GetHENHMETAFILE());
266 #endif // wxUSE_ENH_METAFILE
274 wxLogError(_("Unsupported clipboard format."));
279 dataFormat
= wxDF_TEXT
;
284 char *s
= (char *)data
;
286 width
= strlen(s
) + 1;
288 DWORD l
= (width
* height
);
289 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
292 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
294 memcpy(lpGlobalMemory
, s
, l
);
296 GlobalUnlock(hGlobalMemory
);
299 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
306 wxLogSysError(_("Failed to set clipboard data."));
314 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
318 switch ( dataFormat
)
323 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
327 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
328 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
330 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
331 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
333 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
337 SelectObject(hdcSrc
, old
);
343 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
344 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
345 hdcSrc
, 0, 0, SRCCOPY
);
347 // Select new bitmap out of memory DC
348 SelectObject(hdcMem
, old1
);
351 SelectObject(hdcSrc
, old
);
355 // Create and return a new wxBitmap
356 wxBitmap
*wxBM
= new wxBitmap
;
357 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
358 wxBM
->SetWidth(bm
.bmWidth
);
359 wxBM
->SetHeight(bm
.bmHeight
);
360 wxBM
->SetDepth(bm
.bmPlanes
);
361 #if WXWIN_COMPATIBILITY_2
363 #endif // WXWIN_COMPATIBILITY_2
375 wxLogError(_("Unsupported clipboard format."));
380 dataFormat
= wxDF_TEXT
;
385 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
389 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
393 char *s
= new char[hsize
];
397 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
399 memcpy(s
, lpGlobalMemory
, hsize
);
401 ::GlobalUnlock(hGlobalMemory
);
409 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
410 if ( !hGlobalMemory
)
413 DWORD size
= ::GlobalSize(hGlobalMemory
);
417 void *buf
= malloc(size
);
421 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
423 memcpy(buf
, lpGlobalMemory
, size
);
425 ::GlobalUnlock(hGlobalMemory
);
434 wxLogSysError(_("Failed to retrieve data from the clipboard."));
440 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
442 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
445 int wxRegisterClipboardFormat(wxChar
*formatName
)
447 return ::RegisterClipboardFormat(formatName
);
450 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
454 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
457 // ---------------------------------------------------------------------------
459 // ---------------------------------------------------------------------------
461 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
463 wxClipboard::wxClipboard()
465 m_clearOnExit
= FALSE
;
468 wxClipboard::~wxClipboard()
476 void wxClipboard::Clear()
478 #if wxUSE_OLE_CLIPBOARD
479 if ( FAILED(OleSetClipboard(NULL
)) )
481 wxLogLastError(wxT("OleSetClipboard(NULL)"));
486 bool wxClipboard::Flush()
488 #if wxUSE_OLE_CLIPBOARD
489 if ( FAILED(OleFlushClipboard()) )
491 wxLogLastError(wxT("OleFlushClipboard"));
497 m_clearOnExit
= FALSE
;
501 #else // !wxUSE_OLE_CLIPBOARD
503 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
506 bool wxClipboard::Open()
508 // OLE opens clipboard for us
509 #if wxUSE_OLE_CLIPBOARD
512 return wxOpenClipboard();
516 bool wxClipboard::IsOpened() const
518 #if wxUSE_OLE_CLIPBOARD
521 return wxIsClipboardOpened();
525 bool wxClipboard::SetData( wxDataObject
*data
)
527 #if !wxUSE_OLE_CLIPBOARD
528 (void)wxEmptyClipboard();
529 #endif // wxUSE_OLE_CLIPBOARD
532 return AddData(data
);
537 bool wxClipboard::AddData( wxDataObject
*data
)
539 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
541 #if wxUSE_OLE_CLIPBOARD
542 HRESULT hr
= OleSetClipboard(data
->GetInterface());
545 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
547 // don't free anything in this case
552 // we have a problem here because we should delete wxDataObject, but we
553 // can't do it because IDataObject which we just gave to the clipboard
554 // would try to use it when it will need the data. IDataObject is ref
555 // counted and so doesn't suffer from such problem, so we release it now
556 // and tell it to delete wxDataObject when it is deleted itself.
557 data
->SetAutoDelete();
559 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
560 // using OLE clipboard when the app terminates - by default, we call
561 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
562 // wxClipboard::Flush() to chaneg this
563 m_clearOnExit
= TRUE
;
567 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
569 wxDataFormat format
= data
->GetFormat();
576 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
577 wxString
str(textDataObject
->GetText());
578 return wxSetClipboardData(format
, str
.c_str());
584 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
585 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
586 return wxSetClipboardData(data
->GetFormat(), &bitmap
);
592 wxMetafileDataObject
* metaFileDataObject
=
593 (wxMetafileDataObject
*) data
;
594 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
595 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
596 metaFileDataObject
->GetWidth(),
597 metaFileDataObject
->GetHeight());
599 #endif // wxUSE_METAFILE
602 return wxSetClipboardData(data
);
604 #else // !wxUSE_DATAOBJ
606 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
609 void wxClipboard::Close()
611 // OLE closes clipboard for us
612 #if !wxUSE_OLE_CLIPBOARD
617 bool wxClipboard::IsSupported( wxDataFormat format
)
619 return wxIsClipboardFormatAvailable(format
);
622 bool wxClipboard::GetData( wxDataObject
& data
)
624 #if wxUSE_OLE_CLIPBOARD
625 IDataObject
*pDataObject
= NULL
;
626 HRESULT hr
= OleGetClipboard(&pDataObject
);
627 if ( FAILED(hr
) || !pDataObject
)
629 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
634 // build the list of supported formats
635 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
637 wxDataFormat
*formats
;
640 // the most common case
645 // bad luck, need to alloc mem
646 formats
= new wxDataFormat
[nFormats
];
649 data
.GetAllFormats(formats
, wxDataObject::Set
);
651 // get the format enumerator
653 wxArrayInt supportedFormats
;
654 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
655 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
656 if ( FAILED(hr
) || !pEnumFormatEtc
)
659 _("Failed to retrieve the supported clipboard formats"));
663 // ask for the supported formats and see if there are any we support
668 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
670 // don't use FAILED() because S_FALSE would pass it
677 CLIPFORMAT cf
= formatEtc
.cfFormat
;
680 wxLogTrace(wxTRACE_OleCalls
,
681 wxT("Object on the clipboard supports format %s."),
682 wxDataObject::GetFormatName(cf
));
686 for ( size_t n
= 0; n
< nFormats
; n
++ )
688 if ( formats
[n
].GetFormatId() == cf
)
690 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
692 supportedFormats
.Add(cf
);
698 pEnumFormatEtc
->Release();
701 if ( formats
!= &format
)
705 //else: we didn't allocate any memory
707 if ( !supportedFormats
.IsEmpty() )
710 formatEtc
.ptd
= NULL
;
711 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
712 formatEtc
.lindex
= -1;
714 size_t nSupportedFormats
= supportedFormats
.GetCount();
715 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
718 formatEtc
.cfFormat
= supportedFormats
[n
];
720 // use the appropriate tymed
721 switch ( formatEtc
.cfFormat
)
724 formatEtc
.tymed
= TYMED_GDI
;
727 case CF_METAFILEPICT
:
728 formatEtc
.tymed
= TYMED_MFPICT
;
732 formatEtc
.tymed
= TYMED_ENHMF
;
736 formatEtc
.tymed
= TYMED_HGLOBAL
;
740 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
743 // try other tymed for GDI objects
744 if ( formatEtc
.cfFormat
== CF_BITMAP
)
746 formatEtc
.tymed
= TYMED_HGLOBAL
;
747 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
753 // pass the data to the data object
754 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
757 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
759 // IDataObject only takes the ownership of data if it
760 // successfully got it - which is not the case here
761 ReleaseStgMedium(&medium
);
768 //else: unsupported tymed?
771 //else: unsupported format
773 // clean up and return
774 pDataObject
->Release();
778 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
780 wxDataFormat format
= data
.GetFormat();
786 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
787 char* s
= (char*)wxGetClipboardData(format
);
791 textDataObject
.SetText(s
);
800 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
801 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
->GetFormat());
805 bitmapDataObject
.SetBitmap(*bitmap
);
813 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
814 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
818 metaFileDataObject
.SetMetafile(*metaFile
);
823 #endif // wxUSE_METAFILE
826 #else // !wxUSE_DATAOBJ
827 wxFAIL_MSG( wxT("no clipboard implementation") );
829 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
833 // #error "Please turn wxUSE_CLIPBOARD on to compile this file."
834 #endif // wxUSE_CLIPBOARD