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"
71 #if wxUSE_OLE && !defined(__WXWINCE__)
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
83 // ===========================================================================
85 // ===========================================================================
87 // ---------------------------------------------------------------------------
88 // old-style clipboard functions using Windows API
89 // ---------------------------------------------------------------------------
91 static bool gs_wxClipboardIsOpen
= FALSE
;
93 bool wxOpenClipboard()
95 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, wxT("clipboard already opened.") );
97 wxWindow
*win
= wxTheApp
->GetTopWindow();
100 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
102 if ( !gs_wxClipboardIsOpen
)
103 wxLogSysError(_("Failed to open the clipboard."));
105 return gs_wxClipboardIsOpen
;
109 wxLogDebug(wxT("Can not open clipboard without a main window."));
115 bool wxCloseClipboard()
117 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, wxT("clipboard is not opened") );
119 gs_wxClipboardIsOpen
= FALSE
;
121 if ( ::CloseClipboard() == 0 )
123 wxLogSysError(_("Failed to close the clipboard."));
131 bool wxEmptyClipboard()
133 if ( ::EmptyClipboard() == 0 )
135 wxLogSysError(_("Failed to empty the clipboard."));
143 bool wxIsClipboardOpened()
145 return gs_wxClipboardIsOpen
;
148 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
150 if ( ::IsClipboardFormatAvailable(dataFormat
) )
152 // ok from the first try
156 // for several standard formats, we can convert from some other ones too
157 switch ( dataFormat
.GetFormatId() )
159 // for bitmaps, DIBs will also do
161 return ::IsClipboardFormatAvailable(CF_DIB
) != 0;
163 #if wxUSE_ENH_METAFILE && !defined(__WIN16__) && !defined(__WXWINCE__)
164 case CF_METAFILEPICT
:
165 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
166 #endif // wxUSE_ENH_METAFILE
174 bool wxSetClipboardData(wxDataFormat dataFormat
,
176 int width
, int height
)
178 HANDLE handle
= 0; // return value of SetClipboardData
184 wxBitmap
*bitmap
= (wxBitmap
*)data
;
186 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
187 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
188 HBITMAP old
= (HBITMAP
)
189 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
190 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
192 bitmap
->GetHeight());
195 SelectObject(hdcSrc
, old
);
201 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
202 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
203 hdcSrc
, 0, 0, SRCCOPY
);
205 // Select new bitmap out of memory DC
206 SelectObject(hdcMem
, old1
);
209 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
212 SelectObject(hdcSrc
, old
);
221 wxBitmap
*bitmap
= (wxBitmap
*)data
;
223 HGLOBAL hDIB
= wxDIB::ConvertFromBitmap(GetHbitmapOf(*bitmap
));
226 handle
= ::SetClipboardData(CF_DIB
, hDIB
);
232 // VZ: I'm told that this code works, but it doesn't seem to work for me
233 // and, anyhow, I'd be highly surprised if it did. So I leave it here
234 // but IMNSHO it is completely broken.
235 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
238 wxMetafile
*wxMF
= (wxMetafile
*)data
;
239 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
240 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
242 mf
->mm
= wxMF
->GetWindowsMappingMode();
245 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
247 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
249 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
252 #endif // wxUSE_METAFILE
254 #if wxUSE_ENH_METAFILE && !defined(__WIN16__) && !defined(__WXWINCE__)
255 case wxDF_ENHMETAFILE
:
257 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
258 wxEnhMetaFile emfCopy
= *emf
;
260 handle
= SetClipboardData(CF_ENHMETAFILE
,
261 (void *)emfCopy
.GetHENHMETAFILE());
264 #endif // wxUSE_ENH_METAFILE
272 wxLogError(_("Unsupported clipboard format."));
277 dataFormat
= wxDF_TEXT
;
282 char *s
= (char *)data
;
284 width
= strlen(s
) + 1;
286 DWORD l
= (width
* height
);
287 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
290 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
292 memcpy(lpGlobalMemory
, s
, l
);
294 GlobalUnlock(hGlobalMemory
);
297 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
300 // Only tested with non-Unicode, Visual C++ 6.0 so far
301 #if defined(__VISUALC__) && !defined(UNICODE)
304 char* html
= (char *)data
;
306 // Create temporary buffer for HTML header...
307 char *buf
= new char [400 + strlen(html
)];
308 if(!buf
) return FALSE
;
310 // Get clipboard id for HTML format...
312 if(!cfid
) cfid
= RegisterClipboardFormat(wxT("HTML Format"));
314 // Create a template string for the HTML header...
317 "StartHTML:00000000\r\n"
318 "EndHTML:00000000\r\n"
319 "StartFragment:00000000\r\n"
320 "EndFragment:00000000\r\n"
322 "<!--StartFragment -->\r\n");
324 // Append the HTML...
327 // Finish up the HTML format...
329 "<!--EndFragment-->\r\n"
333 // Now go back, calculate all the lengths, and write out the
334 // necessary header information. Note, wsprintf() truncates the
335 // string when you overwrite it so you follow up with code to replace
336 // the 0 appended at the end with a '\r'...
337 char *ptr
= strstr(buf
, "StartHTML");
338 wsprintf(ptr
+10, "%08u", strstr(buf
, "<html>") - buf
);
341 ptr
= strstr(buf
, "EndHTML");
342 wsprintf(ptr
+8, "%08u", strlen(buf
));
345 ptr
= strstr(buf
, "StartFragment");
346 wsprintf(ptr
+14, "%08u", strstr(buf
, "<!--StartFrag") - buf
);
349 ptr
= strstr(buf
, "EndFragment");
350 wsprintf(ptr
+12, "%08u", strstr(buf
, "<!--EndFrag") - buf
);
353 // Now you have everything in place ready to put on the
356 // Allocate global memory for transfer...
357 HGLOBAL hText
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
, strlen(buf
)+4);
359 // Put your string in the global memory...
360 ptr
= (char *)GlobalLock(hText
);
364 handle
= ::SetClipboardData(cfid
, hText
);
378 wxLogSysError(_("Failed to set clipboard data."));
386 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
390 switch ( dataFormat
)
396 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
400 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
401 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
403 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
404 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
406 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
410 SelectObject(hdcSrc
, old
);
416 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
417 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
418 hdcSrc
, 0, 0, SRCCOPY
);
420 // Select new bitmap out of memory DC
421 SelectObject(hdcMem
, old1
);
424 SelectObject(hdcSrc
, old
);
428 // Create and return a new wxBitmap
429 wxBitmap
*wxBM
= new wxBitmap
;
430 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
431 wxBM
->SetWidth(bm
.bmWidth
);
432 wxBM
->SetHeight(bm
.bmHeight
);
433 wxBM
->SetDepth(bm
.bmPlanes
);
434 #if WXWIN_COMPATIBILITY_2
436 #endif // WXWIN_COMPATIBILITY_2
447 wxLogError(_("Unsupported clipboard format."));
451 dataFormat
= wxDF_TEXT
;
456 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
460 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
464 char *s
= new char[hsize
];
468 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
470 memcpy(s
, lpGlobalMemory
, hsize
);
472 GlobalUnlock(hGlobalMemory
);
480 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
481 if ( !hGlobalMemory
)
484 DWORD size
= ::GlobalSize(hGlobalMemory
);
488 void *buf
= malloc(size
);
492 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
494 memcpy(buf
, lpGlobalMemory
, size
);
496 GlobalUnlock(hGlobalMemory
);
505 wxLogSysError(_("Failed to retrieve data from the clipboard."));
511 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
513 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
516 int wxRegisterClipboardFormat(wxChar
*formatName
)
518 return ::RegisterClipboardFormat(formatName
);
521 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
525 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
528 // ---------------------------------------------------------------------------
530 // ---------------------------------------------------------------------------
532 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
534 wxClipboard::wxClipboard()
536 m_clearOnExit
= FALSE
;
540 wxClipboard::~wxClipboard()
548 void wxClipboard::Clear()
550 #if wxUSE_OLE_CLIPBOARD
551 if ( FAILED(OleSetClipboard(NULL
)) )
553 wxLogLastError(wxT("OleSetClipboard(NULL)"));
558 bool wxClipboard::Flush()
560 #if wxUSE_OLE_CLIPBOARD
561 if ( FAILED(OleFlushClipboard()) )
563 wxLogLastError(wxT("OleFlushClipboard"));
569 m_clearOnExit
= FALSE
;
573 #else // !wxUSE_OLE_CLIPBOARD
575 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
578 bool wxClipboard::Open()
580 // OLE opens clipboard for us
582 #if wxUSE_OLE_CLIPBOARD
585 return wxOpenClipboard();
589 bool wxClipboard::IsOpened() const
591 #if wxUSE_OLE_CLIPBOARD
594 return wxIsClipboardOpened();
598 bool wxClipboard::SetData( wxDataObject
*data
)
600 #if !wxUSE_OLE_CLIPBOARD
601 (void)wxEmptyClipboard();
602 #endif // wxUSE_OLE_CLIPBOARD
605 return AddData(data
);
610 bool wxClipboard::AddData( wxDataObject
*data
)
612 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
614 #if wxUSE_OLE_CLIPBOARD
615 HRESULT hr
= OleSetClipboard(data
->GetInterface());
618 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
620 // don't free anything in this case
625 // we have a problem here because we should delete wxDataObject, but we
626 // can't do it because IDataObject which we just gave to the clipboard
627 // would try to use it when it will need the data. IDataObject is ref
628 // counted and so doesn't suffer from such problem, so we release it now
629 // and tell it to delete wxDataObject when it is deleted itself.
630 data
->SetAutoDelete();
632 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
633 // using OLE clipboard when the app terminates - by default, we call
634 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
635 // wxClipboard::Flush() to chaneg this
636 m_clearOnExit
= TRUE
;
640 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
642 wxDataFormat format
= data
->GetPreferredFormat();
649 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
650 wxString
str(textDataObject
->GetText());
651 return wxSetClipboardData(format
, str
.c_str());
657 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
658 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
659 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
667 wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values.");
670 wxMetafileDataObject
* metaFileDataObject
=
671 (wxMetafileDataObject
*) data
;
672 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
673 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
674 metaFileDataObject
->GetWidth(),
675 metaFileDataObject
->GetHeight());
678 #endif // wxUSE_METAFILE
682 // This didn't compile, of course
683 // return wxSetClipboardData(data);
685 wxLogError(wxT("Not implemented."));
689 #else // !wxUSE_DATAOBJ
691 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
694 void wxClipboard::Close()
697 // OLE closes clipboard for us
698 #if !wxUSE_OLE_CLIPBOARD
703 bool wxClipboard::IsSupported( wxDataFormat format
)
705 return wxIsClipboardFormatAvailable(format
);
708 bool wxClipboard::GetData( wxDataObject
& data
)
710 #if wxUSE_OLE_CLIPBOARD
711 IDataObject
*pDataObject
= NULL
;
712 HRESULT hr
= OleGetClipboard(&pDataObject
);
713 if ( FAILED(hr
) || !pDataObject
)
715 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
720 // build the list of supported formats
721 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
723 wxDataFormat
*formats
;
726 // the most common case
731 // bad luck, need to alloc mem
732 formats
= new wxDataFormat
[nFormats
];
735 data
.GetAllFormats(formats
, wxDataObject::Set
);
737 // get the format enumerator
739 wxArrayInt supportedFormats
;
740 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
741 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
742 if ( FAILED(hr
) || !pEnumFormatEtc
)
745 _("Failed to retrieve the supported clipboard formats"));
749 // ask for the supported formats and see if there are any we support
754 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
756 // don't use FAILED() because S_FALSE would pass it
763 CLIPFORMAT cf
= formatEtc
.cfFormat
;
766 wxLogTrace(wxTRACE_OleCalls
,
767 wxT("Object on the clipboard supports format %s."),
768 wxDataObject::GetFormatName(cf
));
772 for ( size_t n
= 0; n
< nFormats
; n
++ )
774 if ( formats
[n
].GetFormatId() == cf
)
776 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
778 supportedFormats
.Add(cf
);
784 pEnumFormatEtc
->Release();
787 if ( formats
!= &format
)
791 //else: we didn't allocate any memory
793 if ( !supportedFormats
.IsEmpty() )
796 formatEtc
.ptd
= NULL
;
797 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
798 formatEtc
.lindex
= -1;
800 size_t nSupportedFormats
= supportedFormats
.GetCount();
801 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
804 formatEtc
.cfFormat
= supportedFormats
[n
];
806 // use the appropriate tymed
807 switch ( formatEtc
.cfFormat
)
810 formatEtc
.tymed
= TYMED_GDI
;
813 case CF_METAFILEPICT
:
814 formatEtc
.tymed
= TYMED_MFPICT
;
818 formatEtc
.tymed
= TYMED_ENHMF
;
822 formatEtc
.tymed
= TYMED_HGLOBAL
;
826 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
829 // try other tymed for GDI objects
830 if ( formatEtc
.cfFormat
== CF_BITMAP
)
832 formatEtc
.tymed
= TYMED_HGLOBAL
;
833 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
839 // pass the data to the data object
840 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
843 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
845 // IDataObject only takes the ownership of data if it
846 // successfully got it - which is not the case here
847 ReleaseStgMedium(&medium
);
854 //else: unsupported tymed?
857 //else: unsupported format
859 // clean up and return
860 pDataObject
->Release();
864 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
866 wxDataFormat format
= data
.GetPreferredFormat();
872 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
873 char* s
= (char*)wxGetClipboardData(format
);
877 textDataObject
.SetText(wxString::FromAscii(s
));
886 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
887 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
891 bitmapDataObject
.SetBitmap(*bitmap
);
899 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
900 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
904 metaFileDataObject
.SetMetafile(*metaFile
);
909 #endif // wxUSE_METAFILE
912 #else // !wxUSE_DATAOBJ
913 wxFAIL_MSG( wxT("no clipboard implementation") );
915 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
918 #endif // wxUSE_CLIPBOARD