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
);
322 #if WXWIN_COMPATIBILITY_2
324 #endif // WXWIN_COMPATIBILITY_2
336 wxLogError(_("Unsupported clipboard format."));
341 dataFormat
= wxDF_TEXT
;
346 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
350 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
354 char *s
= new char[hsize
];
358 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
360 memcpy(s
, lpGlobalMemory
, hsize
);
362 ::GlobalUnlock(hGlobalMemory
);
370 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
371 if ( !hGlobalMemory
)
374 DWORD size
= ::GlobalSize(hGlobalMemory
);
378 void *buf
= malloc(size
);
382 LPSTR lpGlobalMemory
= (LPSTR
)::GlobalLock(hGlobalMemory
);
384 memcpy(buf
, lpGlobalMemory
, size
);
386 ::GlobalUnlock(hGlobalMemory
);
395 wxLogSysError(_("Failed to retrieve data from the clipboard."));
401 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
403 return ::EnumClipboardFormats(dataFormat
);
406 int wxRegisterClipboardFormat(wxChar
*formatName
)
408 return ::RegisterClipboardFormat(formatName
);
411 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
415 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
418 // ---------------------------------------------------------------------------
420 // ---------------------------------------------------------------------------
422 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
424 wxClipboard::wxClipboard()
426 m_clearOnExit
= FALSE
;
429 wxClipboard::~wxClipboard()
437 void wxClipboard::Clear()
439 #if wxUSE_OLE_CLIPBOARD
440 if ( FAILED(OleSetClipboard(NULL
)) )
442 wxLogLastError("OleSetClipboard(NULL)");
447 bool wxClipboard::Flush()
449 #if wxUSE_OLE_CLIPBOARD
450 if ( FAILED(OleFlushClipboard()) )
452 wxLogLastError("OleFlushClipboard");
458 m_clearOnExit
= FALSE
;
462 #else // !wxUSE_OLE_CLIPBOARD
464 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
467 bool wxClipboard::Open()
469 // OLE opens clipboard for us
470 #if wxUSE_OLE_CLIPBOARD
473 return wxOpenClipboard();
477 bool wxClipboard::IsOpened() const
479 #if wxUSE_OLE_CLIPBOARD
482 return wxIsClipboardOpened();
486 bool wxClipboard::SetData( wxDataObject
*data
)
488 #if !wxUSE_OLE_CLIPBOARD
489 (void)wxEmptyClipboard();
490 #endif // wxUSE_OLE_CLIPBOARD
493 return AddData(data
);
498 bool wxClipboard::AddData( wxDataObject
*data
)
500 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
502 #if wxUSE_OLE_CLIPBOARD
503 HRESULT hr
= OleSetClipboard(data
->GetInterface());
506 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
508 // don't free anything in this case
513 // we have a problem here because we should delete wxDataObject, but we
514 // can't do it because IDataObject which we just gave to the clipboard
515 // would try to use it when it will need the data. IDataObject is ref
516 // counted and so doesn't suffer from such problem, so we release it now
517 // and tell it to delete wxDataObject when it is deleted itself.
518 data
->SetAutoDelete();
520 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
521 // using OLE clipboard when the app terminates - by default, we call
522 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
523 // wxClipboard::Flush() to chaneg this
524 m_clearOnExit
= TRUE
;
528 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
530 wxDataFormat format
= data
->GetFormat();
537 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
538 wxString
str(textDataObject
->GetText());
539 return wxSetClipboardData(format
, str
.c_str());
545 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
546 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
547 return wxSetClipboardData(data
->GetFormat(), &bitmap
);
553 wxMetafileDataObject
* metaFileDataObject
=
554 (wxMetafileDataObject
*) data
;
555 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
556 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
557 metaFileDataObject
->GetWidth(),
558 metaFileDataObject
->GetHeight());
560 #endif // wxUSE_METAFILE
563 return wxSetClipboardData(data
);
565 #else // !wxUSE_DATAOBJ
567 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
570 void wxClipboard::Close()
572 // OLE closes clipboard for us
573 #if !wxUSE_OLE_CLIPBOARD
578 bool wxClipboard::IsSupported( wxDataFormat format
)
580 return wxIsClipboardFormatAvailable(format
);
583 bool wxClipboard::GetData( wxDataObject
& data
)
585 #if wxUSE_OLE_CLIPBOARD
586 IDataObject
*pDataObject
= NULL
;
587 HRESULT hr
= OleGetClipboard(&pDataObject
);
588 if ( FAILED(hr
) || !pDataObject
)
590 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
595 // build the list of supported formats
596 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
597 wxDataFormat format
, *formats
;
600 // the most common case
605 // bad luck, need to alloc mem
606 formats
= new wxDataFormat
[nFormats
];
609 data
.GetAllFormats(formats
, wxDataObject::Set
);
611 // get the format enumerator
613 wxArrayInt supportedFormats
;
614 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
615 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
616 if ( FAILED(hr
) || !pEnumFormatEtc
)
619 _("Failed to retrieve the supported clipboard formats"));
623 // ask for the supported formats and see if there are any we support
628 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
630 // don't use FAILED() because S_FALSE would pass it
637 CLIPFORMAT cf
= formatEtc
.cfFormat
;
640 wxLogTrace(wxTRACE_OleCalls
,
641 wxT("Object on the clipboard supports format %s."),
642 wxDataObject::GetFormatName(cf
));
646 for ( size_t n
= 0; n
< nFormats
; n
++ )
648 if ( formats
[n
].GetFormatId() == cf
)
650 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
652 supportedFormats
.Add(cf
);
658 pEnumFormatEtc
->Release();
661 if ( formats
!= &format
)
665 //else: we didn't allocate any memory
667 if ( !supportedFormats
.IsEmpty() )
670 formatEtc
.ptd
= NULL
;
671 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
672 formatEtc
.lindex
= -1;
674 size_t nSupportedFormats
= supportedFormats
.GetCount();
675 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
678 formatEtc
.cfFormat
= supportedFormats
[n
];
680 // use the appropriate tymed
681 switch ( formatEtc
.cfFormat
)
684 formatEtc
.tymed
= TYMED_GDI
;
687 case CF_METAFILEPICT
:
688 formatEtc
.tymed
= TYMED_MFPICT
;
692 formatEtc
.tymed
= TYMED_HGLOBAL
;
696 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
699 // try other tymed for GDI objects
700 if ( formatEtc
.cfFormat
== CF_BITMAP
)
702 formatEtc
.tymed
= TYMED_HGLOBAL
;
703 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
709 // pass the data to the data object
710 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
713 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
715 // IDataObject only takes the ownership of data if it
716 // successfully got it - which is not the case here
717 ReleaseStgMedium(&medium
);
724 //else: unsupported tymed?
727 //else: unsupported format
729 // clean up and return
730 pDataObject
->Release();
734 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
736 wxDataFormat format
= data
.GetFormat();
742 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
743 char* s
= (char*)wxGetClipboardData(format
);
747 textDataObject
.SetText(s
);
756 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
757 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
->GetFormat());
761 bitmapDataObject
.SetBitmap(*bitmap
);
769 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
770 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
774 metaFileDataObject
.SetMetafile(*metaFile
);
779 #endif // wxUSE_METAFILE
781 #else // !wxUSE_DATAOBJ
782 wxFAIL_MSG( wxT("no clipboard implementation") );
783 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
789 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
790 #endif // wxUSE_CLIPBOARD