1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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"
73 #define wxUSE_OLE_CLIPBOARD 1
74 #else // !wxUSE_DATAOBJ
75 // use Win clipboard API
76 #define wxUSE_OLE_CLIPBOARD 0
79 #if wxUSE_OLE_CLIPBOARD
81 #endif // wxUSE_OLE_CLIPBOARD
84 #define memcpy hmemcpy
87 // ===========================================================================
89 // ===========================================================================
91 // ---------------------------------------------------------------------------
92 // old-style clipboard functions using Windows API
93 // ---------------------------------------------------------------------------
95 static bool gs_wxClipboardIsOpen
= FALSE
;
97 bool wxOpenClipboard()
99 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, wxT("clipboard already opened.") );
101 wxWindow
*win
= wxTheApp
->GetTopWindow();
104 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
106 if ( !gs_wxClipboardIsOpen
)
107 wxLogSysError(_("Failed to open the clipboard."));
109 return gs_wxClipboardIsOpen
;
113 wxLogDebug(wxT("Can not open clipboard without a main window."));
119 bool wxCloseClipboard()
121 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, wxT("clipboard is not opened") );
123 gs_wxClipboardIsOpen
= FALSE
;
125 if ( ::CloseClipboard() == 0 )
127 wxLogSysError(_("Failed to close the clipboard."));
135 bool wxEmptyClipboard()
137 if ( ::EmptyClipboard() == 0 )
139 wxLogSysError(_("Failed to empty the clipboard."));
147 bool wxIsClipboardOpened()
149 return gs_wxClipboardIsOpen
;
152 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
154 if ( ::IsClipboardFormatAvailable(dataFormat
) )
156 // ok from the first try
160 // for several standard formats, we can convert from some other ones too
161 switch ( dataFormat
.GetFormatId() )
163 // for bitmaps, DIBs will also do
165 return ::IsClipboardFormatAvailable(CF_DIB
) != 0;
167 #if wxUSE_ENH_METAFILE && !defined(__WIN16__)
168 case CF_METAFILEPICT
:
169 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
170 #endif // wxUSE_ENH_METAFILE
177 #ifdef __DIGITALMARS__
178 extern "C" HGLOBAL
wxDIB::ConvertFromBitmap(HBITMAP hbmp
);
182 bool wxSetClipboardData(wxDataFormat dataFormat
,
184 int width
, int height
)
186 HANDLE handle
= 0; // return value of SetClipboardData
192 wxBitmap
*bitmap
= (wxBitmap
*)data
;
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
,
200 bitmap
->GetHeight());
203 SelectObject(hdcSrc
, old
);
209 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
210 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
211 hdcSrc
, 0, 0, SRCCOPY
);
213 // Select new bitmap out of memory DC
214 SelectObject(hdcMem
, old1
);
217 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
220 SelectObject(hdcSrc
, old
);
228 wxBitmap
*bitmap
= (wxBitmap
*)data
;
230 HGLOBAL hDIB
= wxDIB::ConvertFromBitmap(GetHbitmapOf(*bitmap
));
233 handle
= ::SetClipboardData(CF_DIB
, hDIB
);
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)
244 wxMetafile
*wxMF
= (wxMetafile
*)data
;
245 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
246 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
248 mf
->mm
= wxMF
->GetWindowsMappingMode();
251 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
253 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
255 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
258 #endif // wxUSE_METAFILE
260 #if wxUSE_ENH_METAFILE && !defined(__WIN16__)
261 case wxDF_ENHMETAFILE
:
263 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
264 wxEnhMetaFile emfCopy
= *emf
;
266 handle
= SetClipboardData(CF_ENHMETAFILE
,
267 (void *)emfCopy
.GetHENHMETAFILE());
270 #endif // wxUSE_ENH_METAFILE
278 wxLogError(_("Unsupported clipboard format."));
283 dataFormat
= wxDF_TEXT
;
288 char *s
= (char *)data
;
290 width
= strlen(s
) + 1;
292 DWORD l
= (width
* height
);
293 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
296 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
298 memcpy(lpGlobalMemory
, s
, l
);
300 GlobalUnlock(hGlobalMemory
);
303 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
306 // Only tested with non-Unicode, Visual C++ 6.0 so far
307 #if defined(__VISUALC__) && !defined(UNICODE)
310 char* html
= (char *)data
;
312 // Create temporary buffer for HTML header...
313 char *buf
= new char [400 + strlen(html
)];
314 if(!buf
) return FALSE
;
316 // Get clipboard id for HTML format...
318 if(!cfid
) cfid
= RegisterClipboardFormat(wxT("HTML Format"));
320 // Create a template string for the HTML header...
323 "StartHTML:00000000\r\n"
324 "EndHTML:00000000\r\n"
325 "StartFragment:00000000\r\n"
326 "EndFragment:00000000\r\n"
328 "<!--StartFragment -->\r\n");
330 // Append the HTML...
333 // Finish up the HTML format...
335 "<!--EndFragment-->\r\n"
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
);
347 ptr
= strstr(buf
, "EndHTML");
348 wsprintf(ptr
+8, "%08u", strlen(buf
));
351 ptr
= strstr(buf
, "StartFragment");
352 wsprintf(ptr
+14, "%08u", strstr(buf
, "<!--StartFrag") - buf
);
355 ptr
= strstr(buf
, "EndFragment");
356 wsprintf(ptr
+12, "%08u", strstr(buf
, "<!--EndFrag") - buf
);
359 // Now you have everything in place ready to put on the
362 // Allocate global memory for transfer...
363 HGLOBAL hText
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
, strlen(buf
)+4);
365 // Put your string in the global memory...
366 ptr
= (char *)GlobalLock(hText
);
370 handle
= ::SetClipboardData(cfid
, hText
);
384 wxLogSysError(_("Failed to set clipboard data."));
392 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
396 switch ( dataFormat
)
401 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
405 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
406 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
408 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
409 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
411 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
415 SelectObject(hdcSrc
, old
);
421 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
422 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
423 hdcSrc
, 0, 0, SRCCOPY
);
425 // Select new bitmap out of memory DC
426 SelectObject(hdcMem
, old1
);
429 SelectObject(hdcSrc
, old
);
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
);
439 #if WXWIN_COMPATIBILITY_2
441 #endif // WXWIN_COMPATIBILITY_2
452 wxLogError(_("Unsupported clipboard format."));
456 dataFormat
= wxDF_TEXT
;
461 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
465 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
469 char *s
= new char[hsize
];
473 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
475 memcpy(s
, lpGlobalMemory
, hsize
);
477 ::GlobalUnlock(hGlobalMemory
);
485 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
486 if ( !hGlobalMemory
)
489 DWORD size
= ::GlobalSize(hGlobalMemory
);
493 void *buf
= malloc(size
);
497 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
499 memcpy(buf
, lpGlobalMemory
, size
);
501 ::GlobalUnlock(hGlobalMemory
);
510 wxLogSysError(_("Failed to retrieve data from the clipboard."));
516 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
518 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
521 int wxRegisterClipboardFormat(wxChar
*formatName
)
523 return ::RegisterClipboardFormat(formatName
);
526 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
530 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
533 // ---------------------------------------------------------------------------
535 // ---------------------------------------------------------------------------
537 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
539 wxClipboard::wxClipboard()
541 m_clearOnExit
= FALSE
;
545 wxClipboard::~wxClipboard()
553 void wxClipboard::Clear()
555 #if wxUSE_OLE_CLIPBOARD
556 if ( FAILED(OleSetClipboard(NULL
)) )
558 wxLogLastError(wxT("OleSetClipboard(NULL)"));
563 bool wxClipboard::Flush()
565 #if wxUSE_OLE_CLIPBOARD
566 if ( FAILED(OleFlushClipboard()) )
568 wxLogLastError(wxT("OleFlushClipboard"));
574 m_clearOnExit
= FALSE
;
578 #else // !wxUSE_OLE_CLIPBOARD
580 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
583 bool wxClipboard::Open()
585 // OLE opens clipboard for us
587 #if wxUSE_OLE_CLIPBOARD
590 return wxOpenClipboard();
594 bool wxClipboard::IsOpened() const
596 #if wxUSE_OLE_CLIPBOARD
599 return wxIsClipboardOpened();
603 bool wxClipboard::SetData( wxDataObject
*data
)
605 #if !wxUSE_OLE_CLIPBOARD
606 (void)wxEmptyClipboard();
607 #endif // wxUSE_OLE_CLIPBOARD
610 return AddData(data
);
615 bool wxClipboard::AddData( wxDataObject
*data
)
617 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
619 #if wxUSE_OLE_CLIPBOARD
620 HRESULT hr
= OleSetClipboard(data
->GetInterface());
623 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
625 // don't free anything in this case
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();
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
;
645 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
647 wxDataFormat format
= data
->GetPreferredFormat();
654 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
655 wxString
str(textDataObject
->GetText());
656 return wxSetClipboardData(format
, str
.c_str());
662 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
663 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
664 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
672 wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values.");
675 wxMetafileDataObject
* metaFileDataObject
=
676 (wxMetafileDataObject
*) data
;
677 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
678 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
679 metaFileDataObject
->GetWidth(),
680 metaFileDataObject
->GetHeight());
683 #endif // wxUSE_METAFILE
687 // This didn't compile, of course
688 // return wxSetClipboardData(data);
690 wxLogError("Not implemented.");
694 #else // !wxUSE_DATAOBJ
696 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
699 void wxClipboard::Close()
702 // OLE closes clipboard for us
703 #if !wxUSE_OLE_CLIPBOARD
708 bool wxClipboard::IsSupported( wxDataFormat format
)
710 return wxIsClipboardFormatAvailable(format
);
713 bool wxClipboard::GetData( wxDataObject
& data
)
715 #if wxUSE_OLE_CLIPBOARD
716 IDataObject
*pDataObject
= NULL
;
717 HRESULT hr
= OleGetClipboard(&pDataObject
);
718 if ( FAILED(hr
) || !pDataObject
)
720 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
725 // build the list of supported formats
726 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
728 wxDataFormat
*formats
;
731 // the most common case
736 // bad luck, need to alloc mem
737 formats
= new wxDataFormat
[nFormats
];
740 data
.GetAllFormats(formats
, wxDataObject::Set
);
742 // get the format enumerator
744 wxArrayInt supportedFormats
;
745 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
746 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
747 if ( FAILED(hr
) || !pEnumFormatEtc
)
750 _("Failed to retrieve the supported clipboard formats"));
754 // ask for the supported formats and see if there are any we support
759 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
761 // don't use FAILED() because S_FALSE would pass it
768 CLIPFORMAT cf
= formatEtc
.cfFormat
;
771 wxLogTrace(wxTRACE_OleCalls
,
772 wxT("Object on the clipboard supports format %s."),
773 wxDataObject::GetFormatName(cf
));
777 for ( size_t n
= 0; n
< nFormats
; n
++ )
779 if ( formats
[n
].GetFormatId() == cf
)
781 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
783 supportedFormats
.Add(cf
);
789 pEnumFormatEtc
->Release();
792 if ( formats
!= &format
)
796 //else: we didn't allocate any memory
798 if ( !supportedFormats
.IsEmpty() )
801 formatEtc
.ptd
= NULL
;
802 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
803 formatEtc
.lindex
= -1;
805 size_t nSupportedFormats
= supportedFormats
.GetCount();
806 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
809 formatEtc
.cfFormat
= supportedFormats
[n
];
811 // use the appropriate tymed
812 switch ( formatEtc
.cfFormat
)
815 formatEtc
.tymed
= TYMED_GDI
;
818 case CF_METAFILEPICT
:
819 formatEtc
.tymed
= TYMED_MFPICT
;
823 formatEtc
.tymed
= TYMED_ENHMF
;
827 formatEtc
.tymed
= TYMED_HGLOBAL
;
831 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
834 // try other tymed for GDI objects
835 if ( formatEtc
.cfFormat
== CF_BITMAP
)
837 formatEtc
.tymed
= TYMED_HGLOBAL
;
838 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
844 // pass the data to the data object
845 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
848 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
850 // IDataObject only takes the ownership of data if it
851 // successfully got it - which is not the case here
852 ReleaseStgMedium(&medium
);
859 //else: unsupported tymed?
862 //else: unsupported format
864 // clean up and return
865 pDataObject
->Release();
869 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
871 wxDataFormat format
= data
.GetPreferredFormat();
877 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
878 char* s
= (char*)wxGetClipboardData(format
);
882 textDataObject
.SetText(s
);
891 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
892 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
896 bitmapDataObject
.SetBitmap(*bitmap
);
904 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
905 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
909 metaFileDataObject
.SetMetafile(*metaFile
);
914 #endif // wxUSE_METAFILE
917 #else // !wxUSE_DATAOBJ
918 wxFAIL_MSG( wxT("no clipboard implementation") );
920 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
923 #endif // wxUSE_CLIPBOARD