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
* wxTheClipboard
= (wxClipboard
*)NULL
;
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::SetData( wxDataObject
*data
)
479 (void)wxEmptyClipboard();
482 return AddData(data
);
487 bool wxClipboard::AddData( wxDataObject
*data
)
489 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
491 #if wxUSE_OLE_CLIPBOARD
492 HRESULT hr
= OleSetClipboard(data
->GetInterface());
495 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
497 // don't free anything in this case
502 // we have a problem here because we should delete wxDataObject, but we
503 // can't do it because IDataObject which we just gave to the clipboard
504 // would try to use it when it will need the data. IDataObject is ref
505 // counted and so doesn't suffer from such problem, so we release it now
506 // and tell it to delete wxDataObject when it is deleted itself.
507 data
->SetAutoDelete();
509 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
510 // using OLE clipboard when the app terminates - by default, we call
511 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
512 // wxClipboard::Flush() to chaneg this
513 m_clearOnExit
= TRUE
;
517 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
519 wxDataFormat format
= data
->GetFormat();
526 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
527 wxString
str(textDataObject
->GetText());
528 return wxSetClipboardData(format
, str
.c_str());
534 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
535 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
536 return wxSetClipboardData(data
->GetFormat(), &bitmap
);
542 wxMetafileDataObject
* metaFileDataObject
=
543 (wxMetafileDataObject
*) data
;
544 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
545 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
546 metaFileDataObject
->GetWidth(),
547 metaFileDataObject
->GetHeight());
549 #endif // wxUSE_METAFILE
552 return wxSetClipboardData(data
);
554 #else // !wxUSE_DATAOBJ
556 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
559 void wxClipboard::Close()
561 // OLE closes clipboard for us
562 #if !wxUSE_OLE_CLIPBOARD
567 bool wxClipboard::IsSupported( wxDataFormat format
)
569 return wxIsClipboardFormatAvailable(format
);
572 bool wxClipboard::GetData( wxDataObject
*data
)
574 wxCHECK_MSG( data
, FALSE
, wxT("invalid data object") );
576 #if wxUSE_OLE_CLIPBOARD
577 IDataObject
*pDataObject
= NULL
;
578 HRESULT hr
= OleGetClipboard(&pDataObject
);
579 if ( FAILED(hr
) || !pDataObject
)
581 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
586 // build the list of supported formats
587 size_t nFormats
= data
->GetFormatCount(FALSE
/* for SetData() */);
588 wxDataFormat format
, *formats
;
591 // the most common case
596 // bad luck, need to alloc mem
597 formats
= new wxDataFormat
[nFormats
];
600 data
->GetAllFormats(formats
, FALSE
);
602 // get the format enumerator
604 wxArrayInt supportedFormats
;
605 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
606 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
607 if ( FAILED(hr
) || !pEnumFormatEtc
)
610 _("Failed to retrieve the supported clipboard formats"));
614 // ask for the supported formats and see if there are any we support
619 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
621 // don't use FAILED() because S_FALSE would pass it
628 CLIPFORMAT cf
= formatEtc
.cfFormat
;
631 wxLogTrace(wxTRACE_OleCalls
,
632 wxT("Object on the clipboard supports format %s."),
633 wxDataObject::GetFormatName(cf
));
637 for ( size_t n
= 0; n
< nFormats
; n
++ )
639 if ( formats
[n
].GetFormatId() == cf
)
641 if ( supportedFormats
.Index(cf
) == wxNOT_FOUND
)
643 supportedFormats
.Add(cf
);
649 pEnumFormatEtc
->Release();
652 if ( formats
!= &format
)
656 //else: we didn't allocate any memory
658 if ( !supportedFormats
.IsEmpty() )
661 formatEtc
.ptd
= NULL
;
662 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
663 formatEtc
.lindex
= -1;
664 formatEtc
.tymed
= TYMED_HGLOBAL
;
666 size_t nSupportedFormats
= supportedFormats
.GetCount();
667 for ( size_t n
= 0; !result
&& (n
< nSupportedFormats
); n
++ )
670 formatEtc
.cfFormat
= supportedFormats
[n
];
673 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
676 // try other tymed for GDI objects
677 if ( formatEtc
.cfFormat
== CF_BITMAP
)
679 formatEtc
.tymed
= TYMED_HGLOBAL
;
680 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
686 // pass the data to the data object
687 hr
= data
->GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
690 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
692 // IDataObject only takes the ownership of data if it
693 // successfully got it - which is not the case here
694 ReleaseStgMedium(&medium
);
701 //else: unsupported tymed?
704 //else: unsupported format
706 // clean up and return
707 pDataObject
->Release();
711 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
713 wxDataFormat format
= data
->GetFormat();
719 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
720 char* s
= (char*) wxGetClipboardData(format
);
723 textDataObject
->SetText(s
);
734 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*)data
;
735 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
->GetFormat());
738 bitmapDataObject
->SetBitmap(* bitmap
);
748 wxMetafileDataObject
* metaFileDataObject
= (wxMetafileDataObject
*)data
;
749 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
752 metaFileDataObject
->SetMetafile(*metaFile
);
763 void *buf
= wxGetClipboardData(format
, &len
);
766 // FIXME this is for testing only!!
767 ((wxPrivateDataObject
*)data
)->SetData(buf
, len
);
776 #else // !wxUSE_DATAOBJ
778 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
782 #error "Please turn wxUSE_CLIPBOARD on to compile this file."
783 #endif // wxUSE_CLIPBOARD