1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
56 #include "wx/msw/private.h"
59 #include "wx/msw/dib.h"
62 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
63 // the functions using wxDataObject in wxClipboard
64 //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
67 #include "wx/dataobj.h"
70 #if wxUSE_OLE && !defined(__WXWINCE__)
72 #define wxUSE_OLE_CLIPBOARD 1
73 #else // !wxUSE_DATAOBJ
74 // use Win clipboard API
75 #define wxUSE_OLE_CLIPBOARD 0
78 #if wxUSE_OLE_CLIPBOARD
80 #endif // wxUSE_OLE_CLIPBOARD
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__) && !defined(__WXWINCE__)
163 case CF_METAFILEPICT
:
164 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
165 #endif // wxUSE_ENH_METAFILE
173 bool wxSetClipboardData(wxDataFormat dataFormat
,
175 int width
, int height
)
177 HANDLE handle
= 0; // return value of SetClipboardData
183 wxBitmap
*bitmap
= (wxBitmap
*)data
;
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
,
191 bitmap
->GetHeight());
194 SelectObject(hdcSrc
, old
);
200 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
201 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
202 hdcSrc
, 0, 0, SRCCOPY
);
204 // Select new bitmap out of memory DC
205 SelectObject(hdcMem
, old1
);
208 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
211 SelectObject(hdcSrc
, old
);
220 wxBitmap
*bitmap
= (wxBitmap
*)data
;
222 HGLOBAL hDIB
= wxDIB::ConvertFromBitmap(GetHbitmapOf(*bitmap
));
225 handle
= ::SetClipboardData(CF_DIB
, hDIB
);
231 // VZ: I'm told that this code works, but it doesn't seem to work for me
232 // and, anyhow, I'd be highly surprised if it did. So I leave it here
233 // but IMNSHO it is completely broken.
234 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
237 wxMetafile
*wxMF
= (wxMetafile
*)data
;
238 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
239 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
241 mf
->mm
= wxMF
->GetWindowsMappingMode();
244 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
246 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
248 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
251 #endif // wxUSE_METAFILE
253 #if wxUSE_ENH_METAFILE && !defined(__WIN16__) && !defined(__WXWINCE__)
254 case wxDF_ENHMETAFILE
:
256 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
257 wxEnhMetaFile emfCopy
= *emf
;
259 handle
= SetClipboardData(CF_ENHMETAFILE
,
260 (void *)emfCopy
.GetHENHMETAFILE());
263 #endif // wxUSE_ENH_METAFILE
271 wxLogError(_("Unsupported clipboard format."));
276 dataFormat
= wxDF_TEXT
;
281 char *s
= (char *)data
;
283 width
= strlen(s
) + 1;
285 DWORD l
= (width
* height
);
286 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
289 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
291 memcpy(lpGlobalMemory
, s
, l
);
293 GlobalUnlock(hGlobalMemory
);
296 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
299 // Only tested with non-Unicode, Visual C++ 6.0 so far
300 #if defined(__VISUALC__) && !defined(UNICODE)
303 char* html
= (char *)data
;
305 // Create temporary buffer for HTML header...
306 char *buf
= new char [400 + strlen(html
)];
307 if(!buf
) return FALSE
;
309 // Get clipboard id for HTML format...
311 if(!cfid
) cfid
= RegisterClipboardFormat(wxT("HTML Format"));
313 // Create a template string for the HTML header...
316 "StartHTML:00000000\r\n"
317 "EndHTML:00000000\r\n"
318 "StartFragment:00000000\r\n"
319 "EndFragment:00000000\r\n"
321 "<!--StartFragment -->\r\n");
323 // Append the HTML...
326 // Finish up the HTML format...
328 "<!--EndFragment-->\r\n"
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
);
340 ptr
= strstr(buf
, "EndHTML");
341 wsprintf(ptr
+8, "%08u", strlen(buf
));
344 ptr
= strstr(buf
, "StartFragment");
345 wsprintf(ptr
+14, "%08u", strstr(buf
, "<!--StartFrag") - buf
);
348 ptr
= strstr(buf
, "EndFragment");
349 wsprintf(ptr
+12, "%08u", strstr(buf
, "<!--EndFrag") - buf
);
352 // Now you have everything in place ready to put on the
355 // Allocate global memory for transfer...
356 HGLOBAL hText
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
, strlen(buf
)+4);
358 // Put your string in the global memory...
359 ptr
= (char *)GlobalLock(hText
);
363 handle
= ::SetClipboardData(cfid
, hText
);
377 wxLogSysError(_("Failed to set clipboard data."));
385 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
389 switch ( dataFormat
)
395 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
399 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
400 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
402 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
403 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
405 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
409 SelectObject(hdcSrc
, old
);
415 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
416 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
417 hdcSrc
, 0, 0, SRCCOPY
);
419 // Select new bitmap out of memory DC
420 SelectObject(hdcMem
, old1
);
423 SelectObject(hdcSrc
, old
);
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
);
433 #if WXWIN_COMPATIBILITY_2
435 #endif // WXWIN_COMPATIBILITY_2
446 wxLogError(_("Unsupported clipboard format."));
450 dataFormat
= wxDF_TEXT
;
455 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
459 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
463 char *s
= new char[hsize
];
467 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
469 memcpy(s
, lpGlobalMemory
, hsize
);
471 GlobalUnlock(hGlobalMemory
);
479 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
480 if ( !hGlobalMemory
)
483 DWORD size
= ::GlobalSize(hGlobalMemory
);
487 void *buf
= malloc(size
);
491 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
493 memcpy(buf
, lpGlobalMemory
, size
);
495 GlobalUnlock(hGlobalMemory
);
504 wxLogSysError(_("Failed to retrieve data from the clipboard."));
510 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
512 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
515 int wxRegisterClipboardFormat(wxChar
*formatName
)
517 return ::RegisterClipboardFormat(formatName
);
520 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
524 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
527 // ---------------------------------------------------------------------------
529 // ---------------------------------------------------------------------------
531 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
533 wxClipboard::wxClipboard()
535 m_clearOnExit
= FALSE
;
539 wxClipboard::~wxClipboard()
547 void wxClipboard::Clear()
549 #if wxUSE_OLE_CLIPBOARD
550 if ( FAILED(OleSetClipboard(NULL
)) )
552 wxLogLastError(wxT("OleSetClipboard(NULL)"));
557 bool wxClipboard::Flush()
559 #if wxUSE_OLE_CLIPBOARD
560 if ( FAILED(OleFlushClipboard()) )
562 wxLogLastError(wxT("OleFlushClipboard"));
568 m_clearOnExit
= FALSE
;
572 #else // !wxUSE_OLE_CLIPBOARD
574 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
577 bool wxClipboard::Open()
579 // OLE opens clipboard for us
581 #if wxUSE_OLE_CLIPBOARD
584 return wxOpenClipboard();
588 bool wxClipboard::IsOpened() const
590 #if wxUSE_OLE_CLIPBOARD
593 return wxIsClipboardOpened();
597 bool wxClipboard::SetData( wxDataObject
*data
)
599 #if !wxUSE_OLE_CLIPBOARD
600 (void)wxEmptyClipboard();
601 #endif // wxUSE_OLE_CLIPBOARD
604 return AddData(data
);
609 bool wxClipboard::AddData( wxDataObject
*data
)
611 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
613 #if wxUSE_OLE_CLIPBOARD
614 HRESULT hr
= OleSetClipboard(data
->GetInterface());
617 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
619 // don't free anything in this case
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();
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
;
639 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
641 wxDataFormat format
= data
->GetPreferredFormat();
648 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
649 wxString
str(textDataObject
->GetText());
650 return wxSetClipboardData(format
, str
.c_str());
656 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
657 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
658 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
666 wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values.");
669 wxMetafileDataObject
* metaFileDataObject
=
670 (wxMetafileDataObject
*) data
;
671 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
672 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
673 metaFileDataObject
->GetWidth(),
674 metaFileDataObject
->GetHeight());
677 #endif // wxUSE_METAFILE
681 // This didn't compile, of course
682 // return wxSetClipboardData(data);
684 wxLogError(wxT("Not implemented."));
688 #else // !wxUSE_DATAOBJ
690 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
693 void wxClipboard::Close()
696 // OLE closes clipboard for us
697 #if !wxUSE_OLE_CLIPBOARD
702 bool wxClipboard::IsSupported( wxDataFormat format
)
704 return wxIsClipboardFormatAvailable(format
);
707 bool wxClipboard::GetData( wxDataObject
& data
)
709 #if wxUSE_OLE_CLIPBOARD
710 IDataObject
*pDataObject
= NULL
;
711 HRESULT hr
= OleGetClipboard(&pDataObject
);
712 if ( FAILED(hr
) || !pDataObject
)
714 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
719 // build the list of supported formats
720 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
722 wxDataFormat
*formats
;
725 // the most common case
730 // bad luck, need to alloc mem
731 formats
= new wxDataFormat
[nFormats
];
734 data
.GetAllFormats(formats
, wxDataObject::Set
);
736 // get the format enumerator
738 wxArrayInt supportedFormats
;
739 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
740 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
741 if ( FAILED(hr
) || !pEnumFormatEtc
)
744 _("Failed to retrieve the supported clipboard formats"));
748 // ask for the supported formats and see if there are any we support
753 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
755 // don't use FAILED() because S_FALSE would pass it
762 CLIPFORMAT cf
= formatEtc
.cfFormat
;
765 wxLogTrace(wxTRACE_OleCalls
,
766 wxT("Object on the clipboard supports format %s."),
767 wxDataObject::GetFormatName(cf
));
771 for ( size_t n
= 0; n
< nFormats
; n
++ )
773 if ( formats
[n
].GetFormatId() == cf
)
775 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
777 supportedFormats
.Add(cf
);
783 pEnumFormatEtc
->Release();
786 if ( formats
!= &format
)
790 //else: we didn't allocate any memory
792 if ( !supportedFormats
.IsEmpty() )
795 formatEtc
.ptd
= NULL
;
796 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
797 formatEtc
.lindex
= -1;
799 size_t nSupportedFormats
= supportedFormats
.GetCount();
800 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
803 formatEtc
.cfFormat
= supportedFormats
[n
];
805 // use the appropriate tymed
806 switch ( formatEtc
.cfFormat
)
809 formatEtc
.tymed
= TYMED_GDI
;
812 case CF_METAFILEPICT
:
813 formatEtc
.tymed
= TYMED_MFPICT
;
817 formatEtc
.tymed
= TYMED_ENHMF
;
821 formatEtc
.tymed
= TYMED_HGLOBAL
;
825 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
828 // try other tymed for GDI objects
829 if ( formatEtc
.cfFormat
== CF_BITMAP
)
831 formatEtc
.tymed
= TYMED_HGLOBAL
;
832 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
838 // pass the data to the data object
839 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
842 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
844 // IDataObject only takes the ownership of data if it
845 // successfully got it - which is not the case here
846 ReleaseStgMedium(&medium
);
853 //else: unsupported tymed?
856 //else: unsupported format
858 // clean up and return
859 pDataObject
->Release();
863 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
865 wxDataFormat format
= data
.GetPreferredFormat();
871 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
872 char* s
= (char*)wxGetClipboardData(format
);
876 textDataObject
.SetText(wxString::FromAscii(s
));
885 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
886 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
890 bitmapDataObject
.SetBitmap(*bitmap
);
898 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
899 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
903 metaFileDataObject
.SetMetafile(*metaFile
);
908 #endif // wxUSE_METAFILE
911 #else // !wxUSE_DATAOBJ
912 wxFAIL_MSG( wxT("no clipboard implementation") );
914 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
917 #endif // wxUSE_CLIPBOARD