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"
58 #include "wx/msw/dib.h"
60 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
61 // the functions using wxDataObject in wxClipboard
62 #define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
65 #include "wx/dataobj.h"
68 #define wxUSE_OLE_CLIPBOARD 1
69 #else // !wxUSE_DATAOBJ
70 // use Win clipboard API
71 #define wxUSE_OLE_CLIPBOARD 0
74 #if wxUSE_OLE_CLIPBOARD
76 #endif // wxUSE_OLE_CLIPBOARD
79 #define memcpy hmemcpy
82 // ===========================================================================
84 // ===========================================================================
86 // ---------------------------------------------------------------------------
87 // old-style clipboard functions using Windows API
88 // ---------------------------------------------------------------------------
90 static bool gs_wxClipboardIsOpen
= FALSE
;
92 bool wxOpenClipboard()
94 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, wxT("clipboard already opened.") );
96 wxWindow
*win
= wxTheApp
->GetTopWindow();
99 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
101 if ( !gs_wxClipboardIsOpen
)
102 wxLogSysError(_("Failed to open the clipboard."));
104 return gs_wxClipboardIsOpen
;
108 wxLogDebug(wxT("Can not open clipboard without a main window."));
114 bool wxCloseClipboard()
116 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, wxT("clipboard is not opened") );
118 gs_wxClipboardIsOpen
= FALSE
;
120 if ( ::CloseClipboard() == 0 )
122 wxLogSysError(_("Failed to close the clipboard."));
130 bool wxEmptyClipboard()
132 if ( ::EmptyClipboard() == 0 )
134 wxLogSysError(_("Failed to empty the clipboard."));
142 bool wxIsClipboardOpened()
144 return gs_wxClipboardIsOpen
;
147 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
149 // for bitmaps, DIBs will also do
150 return (::IsClipboardFormatAvailable(dataFormat
) != 0) ||
151 (dataFormat
.GetFormatId() == CF_BITMAP
&&
152 ::IsClipboardFormatAvailable(CF_DIB
));
155 bool wxSetClipboardData(wxDataFormat dataFormat
,
157 int width
, int height
)
159 HANDLE handle
= 0; // return value of SetClipboardData
165 wxBitmap
*bitmap
= (wxBitmap
*)data
;
167 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
168 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
169 HBITMAP old
= (HBITMAP
)
170 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
171 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
173 bitmap
->GetHeight());
176 SelectObject(hdcSrc
, old
);
182 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
183 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
184 hdcSrc
, 0, 0, SRCCOPY
);
186 // Select new bitmap out of memory DC
187 SelectObject(hdcMem
, old1
);
190 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
193 SelectObject(hdcSrc
, old
);
201 #if wxUSE_IMAGE_LOADING_IN_MSW
202 wxBitmap
*bitmap
= (wxBitmap
*)data
;
203 HBITMAP hBitmap
= (HBITMAP
)bitmap
->GetHBITMAP();
204 // NULL palette means to use the system one
205 HANDLE hDIB
= wxBitmapToDIB(hBitmap
, (HPALETTE
)NULL
);
206 handle
= SetClipboardData(CF_DIB
, hDIB
);
207 #endif // wxUSE_IMAGE_LOADING_IN_MSW
214 wxMetafile
*wxMF
= (wxMetafile
*)data
;
215 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
216 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
218 mf
->mm
= wxMF
->GetWindowsMappingMode();
221 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
223 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
225 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
235 wxLogError(_("Unsupported clipboard format."));
240 dataFormat
= wxDF_TEXT
;
245 char *s
= (char *)data
;
247 width
= strlen(s
) + 1;
249 DWORD l
= (width
* height
);
250 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
253 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
255 memcpy(lpGlobalMemory
, s
, l
);
257 GlobalUnlock(hGlobalMemory
);
260 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
267 wxLogSysError(_("Failed to set clipboard data."));
275 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
279 switch ( dataFormat
)
284 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
288 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
289 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
291 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
292 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
294 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
298 SelectObject(hdcSrc
, old
);
304 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
305 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
306 hdcSrc
, 0, 0, SRCCOPY
);
308 // Select new bitmap out of memory DC
309 SelectObject(hdcMem
, old1
);
312 SelectObject(hdcSrc
, old
);
316 // Create and return a new wxBitmap
317 wxBitmap
*wxBM
= new wxBitmap
;
318 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
319 wxBM
->SetWidth(bm
.bmWidth
);
320 wxBM
->SetHeight(bm
.bmHeight
);
321 wxBM
->SetDepth(bm
.bmPlanes
);
334 wxLogError(_("Unsupported clipboard format."));
339 dataFormat
= wxDF_TEXT
;
344 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
348 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
352 char *s
= new char[hsize
];
356 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
358 memcpy(s
, lpGlobalMemory
, hsize
);
360 ::GlobalUnlock(hGlobalMemory
);
368 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
369 if ( !hGlobalMemory
)
372 DWORD size
= ::GlobalSize(hGlobalMemory
);
376 void *buf
= malloc(size
);
380 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
382 memcpy(buf
, lpGlobalMemory
, size
);
384 ::GlobalUnlock(hGlobalMemory
);
393 wxLogSysError(_("Failed to retrieve data from the clipboard."));
399 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
401 return ::EnumClipboardFormats(dataFormat
);
404 int wxRegisterClipboardFormat(wxChar
*formatName
)
406 return ::RegisterClipboardFormat(formatName
);
409 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
413 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
416 // ---------------------------------------------------------------------------
418 // ---------------------------------------------------------------------------
420 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
422 wxClipboard::wxClipboard()
424 m_clearOnExit
= FALSE
;
427 wxClipboard::~wxClipboard()
435 void wxClipboard::Clear()
437 #if wxUSE_OLE_CLIPBOARD
438 if ( FAILED(OleSetClipboard(NULL
)) )
440 wxLogLastError("OleSetClipboard(NULL)");
445 bool wxClipboard::Flush()
447 #if wxUSE_OLE_CLIPBOARD
448 if ( FAILED(OleFlushClipboard()) )
450 wxLogLastError("OleFlushClipboard");
456 m_clearOnExit
= FALSE
;
460 #else // !wxUSE_OLE_CLIPBOARD
462 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
465 bool wxClipboard::Open()
467 // OLE opens clipboard for us
468 #if wxUSE_OLE_CLIPBOARD
471 return wxOpenClipboard();
475 bool wxClipboard::IsOpened() const
477 #if wxUSE_OLE_CLIPBOARD
480 return wxIsClipboardOpened();
484 bool wxClipboard::SetData( wxDataObject
*data
)
486 (void)wxEmptyClipboard();
489 return AddData(data
);
494 bool wxClipboard::AddData( wxDataObject
*data
)
496 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
498 #if wxUSE_OLE_CLIPBOARD
499 HRESULT hr
= OleSetClipboard(data
->GetInterface());
502 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
504 // don't free anything in this case
509 // we have a problem here because we should delete wxDataObject, but we
510 // can't do it because IDataObject which we just gave to the clipboard
511 // would try to use it when it will need the data. IDataObject is ref
512 // counted and so doesn't suffer from such problem, so we release it now
513 // and tell it to delete wxDataObject when it is deleted itself.
514 data
->SetAutoDelete();
516 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
517 // using OLE clipboard when the app terminates - by default, we call
518 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
519 // wxClipboard::Flush() to chaneg this
520 m_clearOnExit
= TRUE
;
524 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
526 wxDataFormat format
= data
->GetFormat();
533 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
534 wxString
str(textDataObject
->GetText());
535 return wxSetClipboardData(format
, str
.c_str());
541 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
542 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
543 return wxSetClipboardData(data
->GetFormat(), &bitmap
);
549 wxMetafileDataObject
* metaFileDataObject
=
550 (wxMetafileDataObject
*) data
;
551 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
552 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
553 metaFileDataObject
->GetWidth(),
554 metaFileDataObject
->GetHeight());
556 #endif // wxUSE_METAFILE
559 return wxSetClipboardData(data
);
561 #else // !wxUSE_DATAOBJ
563 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
566 void wxClipboard::Close()
568 // OLE closes clipboard for us
569 #if !wxUSE_OLE_CLIPBOARD
574 bool wxClipboard::IsSupported( wxDataFormat format
)
576 return wxIsClipboardFormatAvailable(format
);
579 bool wxClipboard::GetData( wxDataObject
& data
)
581 #if wxUSE_OLE_CLIPBOARD
582 IDataObject
*pDataObject
= NULL
;
583 HRESULT hr
= OleGetClipboard(&pDataObject
);
584 if ( FAILED(hr
) || !pDataObject
)
586 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
591 // build the list of supported formats
592 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
593 wxDataFormat format
, *formats
;
596 // the most common case
601 // bad luck, need to alloc mem
602 formats
= new wxDataFormat
[nFormats
];
605 data
.GetAllFormats(formats
, wxDataObject::Set
);
607 // get the format enumerator
609 wxArrayInt supportedFormats
;
610 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
611 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
612 if ( FAILED(hr
) || !pEnumFormatEtc
)
615 _("Failed to retrieve the supported clipboard formats"));
619 // ask for the supported formats and see if there are any we support
624 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
626 // don't use FAILED() because S_FALSE would pass it
633 CLIPFORMAT cf
= formatEtc
.cfFormat
;
636 wxLogTrace(wxTRACE_OleCalls
,
637 wxT("Object on the clipboard supports format %s."),
638 wxDataObject::GetFormatName(cf
));
642 for ( size_t n
= 0; n
< nFormats
; n
++ )
644 if ( formats
[n
].GetFormatId() == cf
)
646 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
648 supportedFormats
.Add(cf
);
654 pEnumFormatEtc
->Release();
657 if ( formats
!= &format
)
661 //else: we didn't allocate any memory
663 if ( !supportedFormats
.IsEmpty() )
666 formatEtc
.ptd
= NULL
;
667 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
668 formatEtc
.lindex
= -1;
669 formatEtc
.tymed
= TYMED_HGLOBAL
;
671 size_t nSupportedFormats
= supportedFormats
.GetCount();
672 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
675 formatEtc
.cfFormat
= supportedFormats
[n
];
678 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
681 // try other tymed for GDI objects
682 if ( formatEtc
.cfFormat
== CF_BITMAP
)
684 formatEtc
.tymed
= TYMED_HGLOBAL
;
685 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
691 // pass the data to the data object
692 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
695 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
697 // IDataObject only takes the ownership of data if it
698 // successfully got it - which is not the case here
699 ReleaseStgMedium(&medium
);
706 //else: unsupported tymed?
709 //else: unsupported format
711 // clean up and return
712 pDataObject
->Release();
716 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
718 wxDataFormat format
= data
.GetFormat();
724 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
725 char* s
= (char*)wxGetClipboardData(format
);
729 textDataObject
.SetText(s
);
738 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
739 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
->GetFormat());
743 bitmapDataObject
.SetBitmap(*bitmap
);
751 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
752 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
756 metaFileDataObject
.SetMetafile(*metaFile
);
761 #endif // wxUSE_METAFILE
763 #else // !wxUSE_DATAOBJ
764 wxFAIL_MSG( wxT("no clipboard implementation") );
765 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
771 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
772 #endif // wxUSE_CLIPBOARD