]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
29 #include "wx/clipbrd.h"
32 #include "wx/object.h"
36 #include "wx/bitmap.h"
43 #include "wx/metafile.h"
49 #include "wx/msw/private.h"
50 #include "wx/msw/ole/oleutils.h"
53 #include "wx/msw/dib.h"
56 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
57 // the functions using wxDataObject in wxClipboard
58 //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
61 #include "wx/dataobj.h"
64 #if wxUSE_OLE && !defined(__WXWINCE__)
66 #define wxUSE_OLE_CLIPBOARD 1
67 #else // !wxUSE_DATAOBJ
68 // use Win clipboard API
69 #define wxUSE_OLE_CLIPBOARD 0
72 #if wxUSE_OLE_CLIPBOARD
74 #endif // wxUSE_OLE_CLIPBOARD
76 // ===========================================================================
78 // ===========================================================================
80 // ---------------------------------------------------------------------------
81 // old-style clipboard functions using Windows API
82 // ---------------------------------------------------------------------------
84 static bool gs_wxClipboardIsOpen
= false;
86 bool wxOpenClipboard()
88 wxCHECK_MSG( !gs_wxClipboardIsOpen
, true, wxT("clipboard already opened.") );
90 wxWindow
*win
= wxTheApp
->GetTopWindow();
93 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
95 if ( !gs_wxClipboardIsOpen
)
96 wxLogSysError(_("Failed to open the clipboard."));
98 return gs_wxClipboardIsOpen
;
102 wxLogDebug(wxT("Can not open clipboard without a main window."));
108 bool wxCloseClipboard()
110 wxCHECK_MSG( gs_wxClipboardIsOpen
, false, wxT("clipboard is not opened") );
112 gs_wxClipboardIsOpen
= false;
114 if ( ::CloseClipboard() == 0 )
116 wxLogSysError(_("Failed to close the clipboard."));
124 bool wxEmptyClipboard()
126 if ( ::EmptyClipboard() == 0 )
128 wxLogSysError(_("Failed to empty the clipboard."));
136 bool wxIsClipboardOpened()
138 return gs_wxClipboardIsOpen
;
141 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
143 wxDataFormat::NativeFormat cf
= dataFormat
.GetFormatId();
145 if ( ::IsClipboardFormatAvailable(cf
) )
147 // ok from the first try
151 // for several standard formats, we can convert from some other ones too
154 // for bitmaps, DIBs will also do
156 return ::IsClipboardFormatAvailable(CF_DIB
) != 0;
158 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
159 case CF_METAFILEPICT
:
160 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
161 #endif // wxUSE_ENH_METAFILE
169 bool wxSetClipboardData(wxDataFormat dataFormat
,
171 int width
, int height
)
173 HANDLE handle
= 0; // return value of SetClipboardData
179 wxBitmap
*bitmap
= (wxBitmap
*)data
;
181 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
182 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
183 HBITMAP old
= (HBITMAP
)
184 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
185 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
187 bitmap
->GetHeight());
190 SelectObject(hdcSrc
, old
);
196 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
197 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
198 hdcSrc
, 0, 0, SRCCOPY
);
200 // Select new bitmap out of memory DC
201 SelectObject(hdcMem
, old1
);
204 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
207 SelectObject(hdcSrc
, old
);
216 wxBitmap
*bitmap
= (wxBitmap
*)data
;
218 if ( bitmap
&& bitmap
->Ok() )
223 handle
= ::SetClipboardData(CF_DIB
, dib
.Detach());
230 // VZ: I'm told that this code works, but it doesn't seem to work for me
231 // and, anyhow, I'd be highly surprised if it did. So I leave it here
232 // but IMNSHO it is completely broken.
233 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
236 wxMetafile
*wxMF
= (wxMetafile
*)data
;
237 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
238 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
240 mf
->mm
= wxMF
->GetWindowsMappingMode();
243 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
245 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
247 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
250 #endif // wxUSE_METAFILE
252 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
253 case wxDF_ENHMETAFILE
:
255 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
256 wxEnhMetaFile emfCopy
= *emf
;
258 handle
= SetClipboardData(CF_ENHMETAFILE
,
259 (void *)emfCopy
.GetHENHMETAFILE());
262 #endif // wxUSE_ENH_METAFILE
270 wxLogError(_("Unsupported clipboard format."));
275 dataFormat
= wxDF_TEXT
;
280 char *s
= (char *)data
;
282 width
= strlen(s
) + 1;
284 DWORD l
= (width
* height
);
285 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
288 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
290 memcpy(lpGlobalMemory
, s
, l
);
292 GlobalUnlock(hGlobalMemory
);
295 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
298 // Only tested with Visual C++ 6.0 so far
299 #if defined(__VISUALC__)
302 char* html
= (char *)data
;
304 // Create temporary buffer for HTML header...
305 char *buf
= new char [400 + strlen(html
)];
306 if(!buf
) return false;
308 // Get clipboard id for HTML format...
310 if(!cfid
) cfid
= RegisterClipboardFormat(wxT("HTML Format"));
312 // Create a template string for the HTML header...
315 "StartHTML:00000000\r\n"
316 "EndHTML:00000000\r\n"
317 "StartFragment:00000000\r\n"
318 "EndFragment:00000000\r\n"
320 "<!--StartFragment -->\r\n");
322 // Append the HTML...
325 // Finish up the HTML format...
327 "<!--EndFragment-->\r\n"
331 // Now go back, calculate all the lengths, and write out the
332 // necessary header information. Note, wsprintf() truncates the
333 // string when you overwrite it so you follow up with code to replace
334 // the 0 appended at the end with a '\r'...
335 char *ptr
= strstr(buf
, "StartHTML");
336 sprintf(ptr
+10, "%08u", strstr(buf
, "<html>") - buf
);
339 ptr
= strstr(buf
, "EndHTML");
340 sprintf(ptr
+8, "%08u", strlen(buf
));
343 ptr
= strstr(buf
, "StartFragment");
344 sprintf(ptr
+14, "%08u", strstr(buf
, "<!--StartFrag") - buf
);
347 ptr
= strstr(buf
, "EndFragment");
348 sprintf(ptr
+12, "%08u", strstr(buf
, "<!--EndFrag") - buf
);
351 // Now you have everything in place ready to put on the
354 // Allocate global memory for transfer...
355 HGLOBAL hText
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
, strlen(buf
)+4);
357 // Put your string in the global memory...
358 ptr
= (char *)GlobalLock(hText
);
362 handle
= ::SetClipboardData(cfid
, hText
);
376 wxLogSysError(_("Failed to set clipboard data."));
384 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
388 switch ( dataFormat
)
394 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
398 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
399 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
401 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
402 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
404 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
408 SelectObject(hdcSrc
, old
);
414 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
415 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
416 hdcSrc
, 0, 0, SRCCOPY
);
418 // Select new bitmap out of memory DC
419 SelectObject(hdcMem
, old1
);
422 SelectObject(hdcSrc
, old
);
426 // Create and return a new wxBitmap
427 wxBitmap
*wxBM
= new wxBitmap
;
428 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
429 wxBM
->SetWidth(bm
.bmWidth
);
430 wxBM
->SetHeight(bm
.bmHeight
);
431 wxBM
->SetDepth(bm
.bmPlanes
);
442 wxLogError(_("Unsupported clipboard format."));
446 dataFormat
= wxDF_TEXT
;
451 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
455 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
459 char *s
= new char[hsize
];
463 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
465 memcpy(s
, lpGlobalMemory
, hsize
);
467 GlobalUnlock(hGlobalMemory
);
475 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
476 if ( !hGlobalMemory
)
479 DWORD size
= ::GlobalSize(hGlobalMemory
);
483 void *buf
= malloc(size
);
487 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
489 memcpy(buf
, lpGlobalMemory
, size
);
491 GlobalUnlock(hGlobalMemory
);
500 wxLogSysError(_("Failed to retrieve data from the clipboard."));
506 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
508 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
511 int wxRegisterClipboardFormat(wxChar
*formatName
)
513 return ::RegisterClipboardFormat(formatName
);
516 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
520 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
523 // ---------------------------------------------------------------------------
525 // ---------------------------------------------------------------------------
527 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
529 wxClipboard::wxClipboard()
531 #if wxUSE_OLE_CLIPBOARD
535 m_lastDataObject
= NULL
;
539 wxClipboard::~wxClipboard()
541 if ( m_lastDataObject
)
546 #if wxUSE_OLE_CLIPBOARD
551 void wxClipboard::Clear()
553 #if wxUSE_OLE_CLIPBOARD
554 if (m_lastDataObject
)
556 // don't touch data set by other applications
557 HRESULT hr
= OleIsCurrentClipboard(m_lastDataObject
);
560 hr
= OleSetClipboard(NULL
);
563 wxLogApiError(wxT("OleSetClipboard(NULL)"), hr
);
566 m_lastDataObject
= NULL
;
568 #endif // wxUSE_OLE_CLIPBOARD
571 bool wxClipboard::Flush()
573 #if wxUSE_OLE_CLIPBOARD
574 if (m_lastDataObject
)
576 // don't touch data set by other applications
577 HRESULT hr
= OleIsCurrentClipboard(m_lastDataObject
);
578 m_lastDataObject
= NULL
;
581 hr
= OleFlushClipboard();
584 wxLogApiError(wxT("OleFlushClipboard"), hr
);
592 #else // !wxUSE_OLE_CLIPBOARD
594 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
597 bool wxClipboard::Open()
599 // OLE opens clipboard for us
601 #if wxUSE_OLE_CLIPBOARD
604 return wxOpenClipboard();
608 bool wxClipboard::IsOpened() const
610 #if wxUSE_OLE_CLIPBOARD
613 return wxIsClipboardOpened();
617 bool wxClipboard::SetData( wxDataObject
*data
)
619 #if !wxUSE_OLE_CLIPBOARD
620 (void)wxEmptyClipboard();
621 #endif // wxUSE_OLE_CLIPBOARD
624 return AddData(data
);
629 bool wxClipboard::AddData( wxDataObject
*data
)
631 wxCHECK_MSG( data
, false, wxT("data is invalid") );
633 #if wxUSE_OLE_CLIPBOARD
634 HRESULT hr
= OleSetClipboard(data
->GetInterface());
637 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
639 // don't free anything in this case
644 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
645 // using OLE clipboard when the app terminates - by default, we call
646 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
647 // wxClipboard::Flush() to change this
648 m_lastDataObject
= data
->GetInterface();
650 // we have a problem here because we should delete wxDataObject, but we
651 // can't do it because IDataObject which we just gave to the clipboard
652 // would try to use it when it will need the data. IDataObject is ref
653 // counted and so doesn't suffer from such problem, so we release it now
654 // and tell it to delete wxDataObject when it is deleted itself.
655 data
->SetAutoDelete();
659 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
661 wxDataFormat format
= data
->GetPreferredFormat();
668 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
669 wxString
str(textDataObject
->GetText());
670 return wxSetClipboardData(format
, str
.c_str());
676 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
677 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
678 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
686 wxLogError(wxT("Not implemented because wxMetafileDataObject does not contain width and height values."));
689 wxMetafileDataObject
* metaFileDataObject
=
690 (wxMetafileDataObject
*) data
;
691 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
692 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
693 metaFileDataObject
->GetWidth(),
694 metaFileDataObject
->GetHeight());
697 #endif // wxUSE_METAFILE
701 // This didn't compile, of course
702 // return wxSetClipboardData(data);
704 wxLogError(wxT("Not implemented."));
708 #else // !wxUSE_DATAOBJ
710 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
713 void wxClipboard::Close()
716 // OLE closes clipboard for us
717 #if !wxUSE_OLE_CLIPBOARD
722 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
724 return wxIsClipboardFormatAvailable(format
);
727 bool wxClipboard::GetData( wxDataObject
& data
)
729 #if wxUSE_OLE_CLIPBOARD
730 IDataObject
*pDataObject
= NULL
;
731 HRESULT hr
= OleGetClipboard(&pDataObject
);
732 if ( FAILED(hr
) || !pDataObject
)
734 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
739 // build the list of supported formats
740 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
742 wxDataFormat
*formats
;
745 // the most common case
750 // bad luck, need to alloc mem
751 formats
= new wxDataFormat
[nFormats
];
754 data
.GetAllFormats(formats
, wxDataObject::Set
);
756 // get the data for the given formats
761 // enumerate all explicit formats on the clipboard.
762 // note that this does not include implicit / synthetic (automatically
763 // converted) formats.
765 // get the format enumerator
766 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
767 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
768 if ( FAILED(hr
) || !pEnumFormatEtc
)
771 _("Failed to retrieve the supported clipboard formats"));
775 // ask for the supported formats and see if there are any we support
779 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
781 // don't use FAILED() because S_FALSE would pass it
788 cf
= formatEtc
.cfFormat
;
790 wxLogTrace(wxTRACE_OleCalls
,
791 wxT("Object on the clipboard supports format %s."),
792 wxDataObject::GetFormatName(cf
));
795 pEnumFormatEtc
->Release();
800 // stop at the first valid format found on the clipboard
801 for ( size_t n
= 0; !result
&& (n
< nFormats
); n
++ )
803 // convert to NativeFormat Id
804 cf
= formats
[n
].GetFormatId();
806 // if the format is not available, try the next one
807 // this test includes implicit / sythetic formats
808 if ( !::IsClipboardFormatAvailable(cf
) )
811 formatEtc
.cfFormat
= cf
;
812 formatEtc
.ptd
= NULL
;
813 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
814 formatEtc
.lindex
= -1;
816 // use the appropriate tymed
817 switch ( formatEtc
.cfFormat
)
820 formatEtc
.tymed
= TYMED_GDI
;
824 case CF_METAFILEPICT
:
825 formatEtc
.tymed
= TYMED_MFPICT
;
829 formatEtc
.tymed
= TYMED_ENHMF
;
834 formatEtc
.tymed
= TYMED_HGLOBAL
;
838 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
841 // try other tymed for GDI objects
842 if ( formatEtc
.cfFormat
== CF_BITMAP
)
844 formatEtc
.tymed
= TYMED_HGLOBAL
;
845 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
851 // pass the data to the data object
852 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, true);
855 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
857 // IDataObject only takes the ownership of data if it
858 // successfully got it - which is not the case here
859 ReleaseStgMedium(&medium
);
866 //else: unsupported tymed?
869 if ( formats
!= &format
)
873 //else: we didn't allocate any memory
875 // clean up and return
876 pDataObject
->Release();
880 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
882 wxDataFormat format
= data
.GetPreferredFormat();
888 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
889 char* s
= (char*)wxGetClipboardData(format
);
893 textDataObject
.SetText(wxString::FromAscii(s
));
902 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
903 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
907 bitmapDataObject
.SetBitmap(*bitmap
);
915 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
916 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
920 metaFileDataObject
.SetMetafile(*metaFile
);
925 #endif // wxUSE_METAFILE
928 #else // !wxUSE_DATAOBJ
929 wxFAIL_MSG( wxT("no clipboard implementation") );
931 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
934 #endif // wxUSE_CLIPBOARD