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"
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__) && !defined(__WXWINCE__)
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
);
225 wxBitmap
*bitmap
= (wxBitmap
*)data
;
227 HGLOBAL hDIB
= wxDIB::ConvertFromBitmap(GetHbitmapOf(*bitmap
));
230 handle
= ::SetClipboardData(CF_DIB
, hDIB
);
236 // VZ: I'm told that this code works, but it doesn't seem to work for me
237 // and, anyhow, I'd be highly surprised if it did. So I leave it here
238 // but IMNSHO it is completely broken.
239 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
242 wxMetafile
*wxMF
= (wxMetafile
*)data
;
243 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
244 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
246 mf
->mm
= wxMF
->GetWindowsMappingMode();
249 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
251 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
253 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
256 #endif // wxUSE_METAFILE
258 #if wxUSE_ENH_METAFILE && !defined(__WIN16__) && !defined(__WXWINCE__)
259 case wxDF_ENHMETAFILE
:
261 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
262 wxEnhMetaFile emfCopy
= *emf
;
264 handle
= SetClipboardData(CF_ENHMETAFILE
,
265 (void *)emfCopy
.GetHENHMETAFILE());
268 #endif // wxUSE_ENH_METAFILE
276 wxLogError(_("Unsupported clipboard format."));
281 dataFormat
= wxDF_TEXT
;
286 char *s
= (char *)data
;
288 width
= strlen(s
) + 1;
290 DWORD l
= (width
* height
);
291 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
294 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
296 memcpy(lpGlobalMemory
, s
, l
);
298 GlobalUnlock(hGlobalMemory
);
301 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
304 // Only tested with non-Unicode, Visual C++ 6.0 so far
305 #if defined(__VISUALC__) && !defined(UNICODE)
308 char* html
= (char *)data
;
310 // Create temporary buffer for HTML header...
311 char *buf
= new char [400 + strlen(html
)];
312 if(!buf
) return FALSE
;
314 // Get clipboard id for HTML format...
316 if(!cfid
) cfid
= RegisterClipboardFormat(wxT("HTML Format"));
318 // Create a template string for the HTML header...
321 "StartHTML:00000000\r\n"
322 "EndHTML:00000000\r\n"
323 "StartFragment:00000000\r\n"
324 "EndFragment:00000000\r\n"
326 "<!--StartFragment -->\r\n");
328 // Append the HTML...
331 // Finish up the HTML format...
333 "<!--EndFragment-->\r\n"
337 // Now go back, calculate all the lengths, and write out the
338 // necessary header information. Note, wsprintf() truncates the
339 // string when you overwrite it so you follow up with code to replace
340 // the 0 appended at the end with a '\r'...
341 char *ptr
= strstr(buf
, "StartHTML");
342 wsprintf(ptr
+10, "%08u", strstr(buf
, "<html>") - buf
);
345 ptr
= strstr(buf
, "EndHTML");
346 wsprintf(ptr
+8, "%08u", strlen(buf
));
349 ptr
= strstr(buf
, "StartFragment");
350 wsprintf(ptr
+14, "%08u", strstr(buf
, "<!--StartFrag") - buf
);
353 ptr
= strstr(buf
, "EndFragment");
354 wsprintf(ptr
+12, "%08u", strstr(buf
, "<!--EndFrag") - buf
);
357 // Now you have everything in place ready to put on the
360 // Allocate global memory for transfer...
361 HGLOBAL hText
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
, strlen(buf
)+4);
363 // Put your string in the global memory...
364 ptr
= (char *)GlobalLock(hText
);
368 handle
= ::SetClipboardData(cfid
, hText
);
382 wxLogSysError(_("Failed to set clipboard data."));
390 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
394 switch ( dataFormat
)
400 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
404 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
405 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
407 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
408 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
410 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
414 SelectObject(hdcSrc
, old
);
420 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
421 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
422 hdcSrc
, 0, 0, SRCCOPY
);
424 // Select new bitmap out of memory DC
425 SelectObject(hdcMem
, old1
);
428 SelectObject(hdcSrc
, old
);
432 // Create and return a new wxBitmap
433 wxBitmap
*wxBM
= new wxBitmap
;
434 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
435 wxBM
->SetWidth(bm
.bmWidth
);
436 wxBM
->SetHeight(bm
.bmHeight
);
437 wxBM
->SetDepth(bm
.bmPlanes
);
438 #if WXWIN_COMPATIBILITY_2
440 #endif // WXWIN_COMPATIBILITY_2
451 wxLogError(_("Unsupported clipboard format."));
455 dataFormat
= wxDF_TEXT
;
460 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
464 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
468 char *s
= new char[hsize
];
472 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
474 memcpy(s
, lpGlobalMemory
, hsize
);
476 GlobalUnlock(hGlobalMemory
);
484 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
485 if ( !hGlobalMemory
)
488 DWORD size
= ::GlobalSize(hGlobalMemory
);
492 void *buf
= malloc(size
);
496 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
498 memcpy(buf
, lpGlobalMemory
, size
);
500 GlobalUnlock(hGlobalMemory
);
509 wxLogSysError(_("Failed to retrieve data from the clipboard."));
515 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
517 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
520 int wxRegisterClipboardFormat(wxChar
*formatName
)
522 return ::RegisterClipboardFormat(formatName
);
525 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
529 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
532 // ---------------------------------------------------------------------------
534 // ---------------------------------------------------------------------------
536 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
538 wxClipboard::wxClipboard()
540 m_clearOnExit
= FALSE
;
544 wxClipboard::~wxClipboard()
552 void wxClipboard::Clear()
554 #if wxUSE_OLE_CLIPBOARD
555 if ( FAILED(OleSetClipboard(NULL
)) )
557 wxLogLastError(wxT("OleSetClipboard(NULL)"));
562 bool wxClipboard::Flush()
564 #if wxUSE_OLE_CLIPBOARD
565 if ( FAILED(OleFlushClipboard()) )
567 wxLogLastError(wxT("OleFlushClipboard"));
573 m_clearOnExit
= FALSE
;
577 #else // !wxUSE_OLE_CLIPBOARD
579 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
582 bool wxClipboard::Open()
584 // OLE opens clipboard for us
586 #if wxUSE_OLE_CLIPBOARD
589 return wxOpenClipboard();
593 bool wxClipboard::IsOpened() const
595 #if wxUSE_OLE_CLIPBOARD
598 return wxIsClipboardOpened();
602 bool wxClipboard::SetData( wxDataObject
*data
)
604 #if !wxUSE_OLE_CLIPBOARD
605 (void)wxEmptyClipboard();
606 #endif // wxUSE_OLE_CLIPBOARD
609 return AddData(data
);
614 bool wxClipboard::AddData( wxDataObject
*data
)
616 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
618 #if wxUSE_OLE_CLIPBOARD
619 HRESULT hr
= OleSetClipboard(data
->GetInterface());
622 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
624 // don't free anything in this case
629 // we have a problem here because we should delete wxDataObject, but we
630 // can't do it because IDataObject which we just gave to the clipboard
631 // would try to use it when it will need the data. IDataObject is ref
632 // counted and so doesn't suffer from such problem, so we release it now
633 // and tell it to delete wxDataObject when it is deleted itself.
634 data
->SetAutoDelete();
636 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
637 // using OLE clipboard when the app terminates - by default, we call
638 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
639 // wxClipboard::Flush() to chaneg this
640 m_clearOnExit
= TRUE
;
644 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
646 wxDataFormat format
= data
->GetPreferredFormat();
653 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
654 wxString
str(textDataObject
->GetText());
655 return wxSetClipboardData(format
, str
.c_str());
661 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
662 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
663 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
671 wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values.");
674 wxMetafileDataObject
* metaFileDataObject
=
675 (wxMetafileDataObject
*) data
;
676 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
677 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
678 metaFileDataObject
->GetWidth(),
679 metaFileDataObject
->GetHeight());
682 #endif // wxUSE_METAFILE
686 // This didn't compile, of course
687 // return wxSetClipboardData(data);
689 wxLogError("Not implemented.");
693 #else // !wxUSE_DATAOBJ
695 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
698 void wxClipboard::Close()
701 // OLE closes clipboard for us
702 #if !wxUSE_OLE_CLIPBOARD
707 bool wxClipboard::IsSupported( wxDataFormat format
)
709 return wxIsClipboardFormatAvailable(format
);
712 bool wxClipboard::GetData( wxDataObject
& data
)
714 #if wxUSE_OLE_CLIPBOARD
715 IDataObject
*pDataObject
= NULL
;
716 HRESULT hr
= OleGetClipboard(&pDataObject
);
717 if ( FAILED(hr
) || !pDataObject
)
719 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
724 // build the list of supported formats
725 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
727 wxDataFormat
*formats
;
730 // the most common case
735 // bad luck, need to alloc mem
736 formats
= new wxDataFormat
[nFormats
];
739 data
.GetAllFormats(formats
, wxDataObject::Set
);
741 // get the format enumerator
743 wxArrayInt supportedFormats
;
744 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
745 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
746 if ( FAILED(hr
) || !pEnumFormatEtc
)
749 _("Failed to retrieve the supported clipboard formats"));
753 // ask for the supported formats and see if there are any we support
758 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
760 // don't use FAILED() because S_FALSE would pass it
767 CLIPFORMAT cf
= formatEtc
.cfFormat
;
770 wxLogTrace(wxTRACE_OleCalls
,
771 wxT("Object on the clipboard supports format %s."),
772 wxDataObject::GetFormatName(cf
));
776 for ( size_t n
= 0; n
< nFormats
; n
++ )
778 if ( formats
[n
].GetFormatId() == cf
)
780 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
782 supportedFormats
.Add(cf
);
788 pEnumFormatEtc
->Release();
791 if ( formats
!= &format
)
795 //else: we didn't allocate any memory
797 if ( !supportedFormats
.IsEmpty() )
800 formatEtc
.ptd
= NULL
;
801 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
802 formatEtc
.lindex
= -1;
804 size_t nSupportedFormats
= supportedFormats
.GetCount();
805 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
808 formatEtc
.cfFormat
= supportedFormats
[n
];
810 // use the appropriate tymed
811 switch ( formatEtc
.cfFormat
)
814 formatEtc
.tymed
= TYMED_GDI
;
817 case CF_METAFILEPICT
:
818 formatEtc
.tymed
= TYMED_MFPICT
;
822 formatEtc
.tymed
= TYMED_ENHMF
;
826 formatEtc
.tymed
= TYMED_HGLOBAL
;
830 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
833 // try other tymed for GDI objects
834 if ( formatEtc
.cfFormat
== CF_BITMAP
)
836 formatEtc
.tymed
= TYMED_HGLOBAL
;
837 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
843 // pass the data to the data object
844 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
847 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
849 // IDataObject only takes the ownership of data if it
850 // successfully got it - which is not the case here
851 ReleaseStgMedium(&medium
);
858 //else: unsupported tymed?
861 //else: unsupported format
863 // clean up and return
864 pDataObject
->Release();
868 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
870 wxDataFormat format
= data
.GetPreferredFormat();
876 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
877 char* s
= (char*)wxGetClipboardData(format
);
881 textDataObject
.SetText(s
);
890 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
891 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
895 bitmapDataObject
.SetBitmap(*bitmap
);
903 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
904 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
908 metaFileDataObject
.SetMetafile(*metaFile
);
913 #endif // wxUSE_METAFILE
916 #else // !wxUSE_DATAOBJ
917 wxFAIL_MSG( wxT("no clipboard implementation") );
919 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
922 #endif // wxUSE_CLIPBOARD