1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
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 bool wxSetClipboardData(wxDataFormat dataFormat
,
179 int width
, int height
)
181 HANDLE handle
= 0; // return value of SetClipboardData
187 wxBitmap
*bitmap
= (wxBitmap
*)data
;
189 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
190 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
191 HBITMAP old
= (HBITMAP
)
192 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
193 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
195 bitmap
->GetHeight());
198 SelectObject(hdcSrc
, old
);
204 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
205 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
206 hdcSrc
, 0, 0, SRCCOPY
);
208 // Select new bitmap out of memory DC
209 SelectObject(hdcMem
, old1
);
212 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
215 SelectObject(hdcSrc
, old
);
223 #if wxUSE_IMAGE_LOADING_IN_MSW
224 wxBitmap
*bitmap
= (wxBitmap
*)data
;
225 HBITMAP hBitmap
= (HBITMAP
)bitmap
->GetHBITMAP();
226 // NULL palette means to use the system one
227 HANDLE hDIB
= wxBitmapToDIB(hBitmap
, (HPALETTE
)NULL
);
228 handle
= SetClipboardData(CF_DIB
, hDIB
);
229 #endif // wxUSE_IMAGE_LOADING_IN_MSW
233 // VZ: I'm told that this code works, but it doesn't seem to work for me
234 // and, anyhow, I'd be highly surprized if it did. So I leave it here
235 // but IMNSHO it is completely broken.
236 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH)
239 wxMetafile
*wxMF
= (wxMetafile
*)data
;
240 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
241 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
243 mf
->mm
= wxMF
->GetWindowsMappingMode();
246 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
248 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
250 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
253 #endif // wxUSE_METAFILE
255 #if wxUSE_ENH_METAFILE && !defined(__WIN16__)
256 case wxDF_ENHMETAFILE
:
258 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
259 wxEnhMetaFile emfCopy
= *emf
;
261 handle
= SetClipboardData(CF_ENHMETAFILE
,
262 (void *)emfCopy
.GetHENHMETAFILE());
265 #endif // wxUSE_ENH_METAFILE
273 wxLogError(_("Unsupported clipboard format."));
278 dataFormat
= wxDF_TEXT
;
283 char *s
= (char *)data
;
285 width
= strlen(s
) + 1;
287 DWORD l
= (width
* height
);
288 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
291 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
293 memcpy(lpGlobalMemory
, s
, l
);
295 GlobalUnlock(hGlobalMemory
);
298 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
305 wxLogSysError(_("Failed to set clipboard data."));
313 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
317 switch ( dataFormat
)
322 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
326 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
327 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
329 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
330 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
332 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
336 SelectObject(hdcSrc
, old
);
342 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
343 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
344 hdcSrc
, 0, 0, SRCCOPY
);
346 // Select new bitmap out of memory DC
347 SelectObject(hdcMem
, old1
);
350 SelectObject(hdcSrc
, old
);
354 // Create and return a new wxBitmap
355 wxBitmap
*wxBM
= new wxBitmap
;
356 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
357 wxBM
->SetWidth(bm
.bmWidth
);
358 wxBM
->SetHeight(bm
.bmHeight
);
359 wxBM
->SetDepth(bm
.bmPlanes
);
360 #if WXWIN_COMPATIBILITY_2
362 #endif // WXWIN_COMPATIBILITY_2
374 wxLogError(_("Unsupported clipboard format."));
379 dataFormat
= wxDF_TEXT
;
384 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
388 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
392 char *s
= new char[hsize
];
396 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
398 memcpy(s
, lpGlobalMemory
, hsize
);
400 ::GlobalUnlock(hGlobalMemory
);
408 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
409 if ( !hGlobalMemory
)
412 DWORD size
= ::GlobalSize(hGlobalMemory
);
416 void *buf
= malloc(size
);
420 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
422 memcpy(buf
, lpGlobalMemory
, size
);
424 ::GlobalUnlock(hGlobalMemory
);
433 wxLogSysError(_("Failed to retrieve data from the clipboard."));
439 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
441 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
444 int wxRegisterClipboardFormat(wxChar
*formatName
)
446 return ::RegisterClipboardFormat(formatName
);
449 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
453 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
456 // ---------------------------------------------------------------------------
458 // ---------------------------------------------------------------------------
460 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
462 wxClipboard::wxClipboard()
464 m_clearOnExit
= FALSE
;
467 wxClipboard::~wxClipboard()
475 void wxClipboard::Clear()
477 #if wxUSE_OLE_CLIPBOARD
478 if ( FAILED(OleSetClipboard(NULL
)) )
480 wxLogLastError(wxT("OleSetClipboard(NULL)"));
485 bool wxClipboard::Flush()
487 #if wxUSE_OLE_CLIPBOARD
488 if ( FAILED(OleFlushClipboard()) )
490 wxLogLastError(wxT("OleFlushClipboard"));
496 m_clearOnExit
= FALSE
;
500 #else // !wxUSE_OLE_CLIPBOARD
502 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
505 bool wxClipboard::Open()
507 // OLE opens clipboard for us
508 #if wxUSE_OLE_CLIPBOARD
511 return wxOpenClipboard();
515 bool wxClipboard::IsOpened() const
517 #if wxUSE_OLE_CLIPBOARD
520 return wxIsClipboardOpened();
524 bool wxClipboard::SetData( wxDataObject
*data
)
526 #if !wxUSE_OLE_CLIPBOARD
527 (void)wxEmptyClipboard();
528 #endif // wxUSE_OLE_CLIPBOARD
531 return AddData(data
);
536 bool wxClipboard::AddData( wxDataObject
*data
)
538 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
540 #if wxUSE_OLE_CLIPBOARD
541 HRESULT hr
= OleSetClipboard(data
->GetInterface());
544 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
546 // don't free anything in this case
551 // we have a problem here because we should delete wxDataObject, but we
552 // can't do it because IDataObject which we just gave to the clipboard
553 // would try to use it when it will need the data. IDataObject is ref
554 // counted and so doesn't suffer from such problem, so we release it now
555 // and tell it to delete wxDataObject when it is deleted itself.
556 data
->SetAutoDelete();
558 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
559 // using OLE clipboard when the app terminates - by default, we call
560 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
561 // wxClipboard::Flush() to chaneg this
562 m_clearOnExit
= TRUE
;
566 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
568 wxDataFormat format
= data
->GetPreferredFormat();
575 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
576 wxString
str(textDataObject
->GetText());
577 return wxSetClipboardData(format
, str
.c_str());
583 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
584 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
585 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
593 wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values.");
596 wxMetafileDataObject
* metaFileDataObject
=
597 (wxMetafileDataObject
*) data
;
598 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
599 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
600 metaFileDataObject
->GetWidth(),
601 metaFileDataObject
->GetHeight());
604 #endif // wxUSE_METAFILE
608 // This didn't compile, of course
609 // return wxSetClipboardData(data);
611 wxLogError("Not implemented.");
615 #else // !wxUSE_DATAOBJ
617 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
620 void wxClipboard::Close()
622 // OLE closes clipboard for us
623 #if !wxUSE_OLE_CLIPBOARD
628 bool wxClipboard::IsSupported( wxDataFormat format
)
630 return wxIsClipboardFormatAvailable(format
);
633 bool wxClipboard::GetData( wxDataObject
& data
)
635 #if wxUSE_OLE_CLIPBOARD
636 IDataObject
*pDataObject
= NULL
;
637 HRESULT hr
= OleGetClipboard(&pDataObject
);
638 if ( FAILED(hr
) || !pDataObject
)
640 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
645 // build the list of supported formats
646 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
648 wxDataFormat
*formats
;
651 // the most common case
656 // bad luck, need to alloc mem
657 formats
= new wxDataFormat
[nFormats
];
660 data
.GetAllFormats(formats
, wxDataObject::Set
);
662 // get the format enumerator
664 wxArrayInt supportedFormats
;
665 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
666 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
667 if ( FAILED(hr
) || !pEnumFormatEtc
)
670 _("Failed to retrieve the supported clipboard formats"));
674 // ask for the supported formats and see if there are any we support
679 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
681 // don't use FAILED() because S_FALSE would pass it
688 CLIPFORMAT cf
= formatEtc
.cfFormat
;
691 wxLogTrace(wxTRACE_OleCalls
,
692 wxT("Object on the clipboard supports format %s."),
693 wxDataObject::GetFormatName(cf
));
697 for ( size_t n
= 0; n
< nFormats
; n
++ )
699 if ( formats
[n
].GetFormatId() == cf
)
701 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
703 supportedFormats
.Add(cf
);
709 pEnumFormatEtc
->Release();
712 if ( formats
!= &format
)
716 //else: we didn't allocate any memory
718 if ( !supportedFormats
.IsEmpty() )
721 formatEtc
.ptd
= NULL
;
722 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
723 formatEtc
.lindex
= -1;
725 size_t nSupportedFormats
= supportedFormats
.GetCount();
726 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
729 formatEtc
.cfFormat
= supportedFormats
[n
];
731 // use the appropriate tymed
732 switch ( formatEtc
.cfFormat
)
735 formatEtc
.tymed
= TYMED_GDI
;
738 case CF_METAFILEPICT
:
739 formatEtc
.tymed
= TYMED_MFPICT
;
743 formatEtc
.tymed
= TYMED_ENHMF
;
747 formatEtc
.tymed
= TYMED_HGLOBAL
;
751 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
754 // try other tymed for GDI objects
755 if ( formatEtc
.cfFormat
== CF_BITMAP
)
757 formatEtc
.tymed
= TYMED_HGLOBAL
;
758 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
764 // pass the data to the data object
765 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
768 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
770 // IDataObject only takes the ownership of data if it
771 // successfully got it - which is not the case here
772 ReleaseStgMedium(&medium
);
779 //else: unsupported tymed?
782 //else: unsupported format
784 // clean up and return
785 pDataObject
->Release();
789 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
791 wxDataFormat format
= data
.GetPreferredFormat();
797 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
798 char* s
= (char*)wxGetClipboardData(format
);
802 textDataObject
.SetText(s
);
811 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
812 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
816 bitmapDataObject
.SetBitmap(*bitmap
);
824 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
825 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
829 metaFileDataObject
.SetMetafile(*metaFile
);
834 #endif // wxUSE_METAFILE
837 #else // !wxUSE_DATAOBJ
838 wxFAIL_MSG( wxT("no clipboard implementation") );
840 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
843 #endif // wxUSE_CLIPBOARD