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
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 wxBitmap
*bitmap
= (wxBitmap
*)data
;
226 HGLOBAL hDIB
= wxDIB::ConvertFromBitmap(GetHbitmapOf(*bitmap
));
229 handle
= ::SetClipboardData(CF_DIB
, hDIB
);
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
);
302 // Only tested with non-Unicode, Visual C++ 6.0 so far
303 #if defined(__VISUALC__) && !defined(UNICODE)
306 char* html
= (char *)data
;
308 // Create temporary buffer for HTML header...
309 char *buf
= new char [400 + strlen(html
)];
310 if(!buf
) return FALSE
;
312 // Get clipboard id for HTML format...
314 if(!cfid
) cfid
= RegisterClipboardFormat(wxT("HTML Format"));
316 // Create a template string for the HTML header...
319 "StartHTML:00000000\r\n"
320 "EndHTML:00000000\r\n"
321 "StartFragment:00000000\r\n"
322 "EndFragment:00000000\r\n"
324 "<!--StartFragment -->\r\n");
326 // Append the HTML...
329 // Finish up the HTML format...
331 "<!--EndFragment-->\r\n"
335 // Now go back, calculate all the lengths, and write out the
336 // necessary header information. Note, wsprintf() truncates the
337 // string when you overwrite it so you follow up with code to replace
338 // the 0 appended at the end with a '\r'...
339 char *ptr
= strstr(buf
, "StartHTML");
340 wsprintf(ptr
+10, "%08u", strstr(buf
, "<html>") - buf
);
343 ptr
= strstr(buf
, "EndHTML");
344 wsprintf(ptr
+8, "%08u", strlen(buf
));
347 ptr
= strstr(buf
, "StartFragment");
348 wsprintf(ptr
+14, "%08u", strstr(buf
, "<!--StartFrag") - buf
);
351 ptr
= strstr(buf
, "EndFragment");
352 wsprintf(ptr
+12, "%08u", strstr(buf
, "<!--EndFrag") - buf
);
355 // Now you have everything in place ready to put on the
358 // Allocate global memory for transfer...
359 HGLOBAL hText
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
, strlen(buf
)+4);
361 // Put your string in the global memory...
362 ptr
= (char *)GlobalLock(hText
);
366 handle
= ::SetClipboardData(cfid
, hText
);
380 wxLogSysError(_("Failed to set clipboard data."));
388 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
392 switch ( dataFormat
)
397 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
401 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
402 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
404 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
405 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
407 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
411 SelectObject(hdcSrc
, old
);
417 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
418 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
419 hdcSrc
, 0, 0, SRCCOPY
);
421 // Select new bitmap out of memory DC
422 SelectObject(hdcMem
, old1
);
425 SelectObject(hdcSrc
, old
);
429 // Create and return a new wxBitmap
430 wxBitmap
*wxBM
= new wxBitmap
;
431 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
432 wxBM
->SetWidth(bm
.bmWidth
);
433 wxBM
->SetHeight(bm
.bmHeight
);
434 wxBM
->SetDepth(bm
.bmPlanes
);
435 #if WXWIN_COMPATIBILITY_2
437 #endif // WXWIN_COMPATIBILITY_2
448 wxLogError(_("Unsupported clipboard format."));
452 dataFormat
= wxDF_TEXT
;
457 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
461 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
465 char *s
= new char[hsize
];
469 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
471 memcpy(s
, lpGlobalMemory
, hsize
);
473 ::GlobalUnlock(hGlobalMemory
);
481 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
482 if ( !hGlobalMemory
)
485 DWORD size
= ::GlobalSize(hGlobalMemory
);
489 void *buf
= malloc(size
);
493 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
495 memcpy(buf
, lpGlobalMemory
, size
);
497 ::GlobalUnlock(hGlobalMemory
);
506 wxLogSysError(_("Failed to retrieve data from the clipboard."));
512 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
514 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
517 int wxRegisterClipboardFormat(wxChar
*formatName
)
519 return ::RegisterClipboardFormat(formatName
);
522 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
526 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
529 // ---------------------------------------------------------------------------
531 // ---------------------------------------------------------------------------
533 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
535 wxClipboard::wxClipboard()
537 m_clearOnExit
= FALSE
;
541 wxClipboard::~wxClipboard()
549 void wxClipboard::Clear()
551 #if wxUSE_OLE_CLIPBOARD
552 if ( FAILED(OleSetClipboard(NULL
)) )
554 wxLogLastError(wxT("OleSetClipboard(NULL)"));
559 bool wxClipboard::Flush()
561 #if wxUSE_OLE_CLIPBOARD
562 if ( FAILED(OleFlushClipboard()) )
564 wxLogLastError(wxT("OleFlushClipboard"));
570 m_clearOnExit
= FALSE
;
574 #else // !wxUSE_OLE_CLIPBOARD
576 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
579 bool wxClipboard::Open()
581 // OLE opens clipboard for us
583 #if wxUSE_OLE_CLIPBOARD
586 return wxOpenClipboard();
590 bool wxClipboard::IsOpened() const
592 #if wxUSE_OLE_CLIPBOARD
595 return wxIsClipboardOpened();
599 bool wxClipboard::SetData( wxDataObject
*data
)
601 #if !wxUSE_OLE_CLIPBOARD
602 (void)wxEmptyClipboard();
603 #endif // wxUSE_OLE_CLIPBOARD
606 return AddData(data
);
611 bool wxClipboard::AddData( wxDataObject
*data
)
613 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
615 #if wxUSE_OLE_CLIPBOARD
616 HRESULT hr
= OleSetClipboard(data
->GetInterface());
619 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
621 // don't free anything in this case
626 // we have a problem here because we should delete wxDataObject, but we
627 // can't do it because IDataObject which we just gave to the clipboard
628 // would try to use it when it will need the data. IDataObject is ref
629 // counted and so doesn't suffer from such problem, so we release it now
630 // and tell it to delete wxDataObject when it is deleted itself.
631 data
->SetAutoDelete();
633 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
634 // using OLE clipboard when the app terminates - by default, we call
635 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
636 // wxClipboard::Flush() to chaneg this
637 m_clearOnExit
= TRUE
;
641 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
643 wxDataFormat format
= data
->GetPreferredFormat();
650 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
651 wxString
str(textDataObject
->GetText());
652 return wxSetClipboardData(format
, str
.c_str());
658 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
659 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
660 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
668 wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values.");
671 wxMetafileDataObject
* metaFileDataObject
=
672 (wxMetafileDataObject
*) data
;
673 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
674 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
675 metaFileDataObject
->GetWidth(),
676 metaFileDataObject
->GetHeight());
679 #endif // wxUSE_METAFILE
683 // This didn't compile, of course
684 // return wxSetClipboardData(data);
686 wxLogError("Not implemented.");
690 #else // !wxUSE_DATAOBJ
692 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
695 void wxClipboard::Close()
698 // OLE closes clipboard for us
699 #if !wxUSE_OLE_CLIPBOARD
704 bool wxClipboard::IsSupported( wxDataFormat format
)
706 return wxIsClipboardFormatAvailable(format
);
709 bool wxClipboard::GetData( wxDataObject
& data
)
711 #if wxUSE_OLE_CLIPBOARD
712 IDataObject
*pDataObject
= NULL
;
713 HRESULT hr
= OleGetClipboard(&pDataObject
);
714 if ( FAILED(hr
) || !pDataObject
)
716 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
721 // build the list of supported formats
722 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
724 wxDataFormat
*formats
;
727 // the most common case
732 // bad luck, need to alloc mem
733 formats
= new wxDataFormat
[nFormats
];
736 data
.GetAllFormats(formats
, wxDataObject::Set
);
738 // get the format enumerator
740 wxArrayInt supportedFormats
;
741 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
742 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
743 if ( FAILED(hr
) || !pEnumFormatEtc
)
746 _("Failed to retrieve the supported clipboard formats"));
750 // ask for the supported formats and see if there are any we support
755 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
757 // don't use FAILED() because S_FALSE would pass it
764 CLIPFORMAT cf
= formatEtc
.cfFormat
;
767 wxLogTrace(wxTRACE_OleCalls
,
768 wxT("Object on the clipboard supports format %s."),
769 wxDataObject::GetFormatName(cf
));
773 for ( size_t n
= 0; n
< nFormats
; n
++ )
775 if ( formats
[n
].GetFormatId() == cf
)
777 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
779 supportedFormats
.Add(cf
);
785 pEnumFormatEtc
->Release();
788 if ( formats
!= &format
)
792 //else: we didn't allocate any memory
794 if ( !supportedFormats
.IsEmpty() )
797 formatEtc
.ptd
= NULL
;
798 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
799 formatEtc
.lindex
= -1;
801 size_t nSupportedFormats
= supportedFormats
.GetCount();
802 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
805 formatEtc
.cfFormat
= supportedFormats
[n
];
807 // use the appropriate tymed
808 switch ( formatEtc
.cfFormat
)
811 formatEtc
.tymed
= TYMED_GDI
;
814 case CF_METAFILEPICT
:
815 formatEtc
.tymed
= TYMED_MFPICT
;
819 formatEtc
.tymed
= TYMED_ENHMF
;
823 formatEtc
.tymed
= TYMED_HGLOBAL
;
827 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
830 // try other tymed for GDI objects
831 if ( formatEtc
.cfFormat
== CF_BITMAP
)
833 formatEtc
.tymed
= TYMED_HGLOBAL
;
834 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
840 // pass the data to the data object
841 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
844 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
846 // IDataObject only takes the ownership of data if it
847 // successfully got it - which is not the case here
848 ReleaseStgMedium(&medium
);
855 //else: unsupported tymed?
858 //else: unsupported format
860 // clean up and return
861 pDataObject
->Release();
865 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
867 wxDataFormat format
= data
.GetPreferredFormat();
873 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
874 char* s
= (char*)wxGetClipboardData(format
);
878 textDataObject
.SetText(s
);
887 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
888 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
892 bitmapDataObject
.SetBitmap(*bitmap
);
900 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
901 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
905 metaFileDataObject
.SetMetafile(*metaFile
);
910 #endif // wxUSE_METAFILE
913 #else // !wxUSE_DATAOBJ
914 wxFAIL_MSG( wxT("no clipboard implementation") );
916 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
919 #endif // wxUSE_CLIPBOARD