]>
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"
40 #include "wx/dataobj.h"
44 #include "wx/metafile.h"
50 #include "wx/msw/private.h"
51 #include "wx/msw/ole/oleutils.h"
54 #include "wx/msw/dib.h"
57 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
58 // the functions using wxDataObject in wxClipboard
59 //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
61 #if wxUSE_OLE && !defined(__WXWINCE__)
63 #define wxUSE_OLE_CLIPBOARD 1
64 #else // !wxUSE_DATAOBJ
65 // use Win clipboard API
66 #define wxUSE_OLE_CLIPBOARD 0
69 #if wxUSE_OLE_CLIPBOARD
71 #endif // wxUSE_OLE_CLIPBOARD
73 // ===========================================================================
75 // ===========================================================================
77 // ---------------------------------------------------------------------------
78 // old-style clipboard functions using Windows API
79 // ---------------------------------------------------------------------------
81 static bool gs_wxClipboardIsOpen
= false;
82 static int gs_htmlcfid
= 0;
84 bool wxOpenClipboard()
86 wxCHECK_MSG( !gs_wxClipboardIsOpen
, true, wxT("clipboard already opened.") );
88 wxWindow
*win
= wxTheApp
->GetTopWindow();
91 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
93 if ( !gs_wxClipboardIsOpen
)
95 wxLogSysError(_("Failed to open the clipboard."));
98 return gs_wxClipboardIsOpen
;
102 wxLogDebug(wxT("Cannot 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();
147 if ( ::IsClipboardFormatAvailable(cf
) )
149 // ok from the first try
153 // for several standard formats, we can convert from some other ones too
156 // for bitmaps, DIBs will also do
158 return ::IsClipboardFormatAvailable(CF_DIB
) != 0;
160 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
161 case CF_METAFILEPICT
:
162 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
163 #endif // wxUSE_ENH_METAFILE
171 bool wxSetClipboardData(wxDataFormat dataFormat
,
173 int width
, int height
)
175 HANDLE handle
= 0; // return value of SetClipboardData
181 wxBitmap
*bitmap
= (wxBitmap
*)data
;
183 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
184 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
185 HBITMAP old
= (HBITMAP
)
186 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
187 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
189 bitmap
->GetHeight());
192 SelectObject(hdcSrc
, old
);
198 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
199 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
200 hdcSrc
, 0, 0, SRCCOPY
);
202 // Select new bitmap out of memory DC
203 SelectObject(hdcMem
, old1
);
206 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
209 SelectObject(hdcSrc
, old
);
218 wxBitmap
*bitmap
= (wxBitmap
*)data
;
220 if ( bitmap
&& bitmap
->IsOk() )
225 handle
= ::SetClipboardData(CF_DIB
, dib
.Detach());
232 // VZ: I'm told that this code works, but it doesn't seem to work for me
233 // and, anyhow, I'd be highly surprised if it did. So I leave it here
234 // but IMNSHO it is completely broken.
235 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
238 wxMetafile
*wxMF
= (wxMetafile
*)data
;
239 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
240 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
242 mf
->mm
= wxMF
->GetWindowsMappingMode();
245 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
247 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
249 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
252 #endif // wxUSE_METAFILE
254 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
255 case wxDF_ENHMETAFILE
:
257 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
258 wxEnhMetaFile emfCopy
= *emf
;
260 handle
= SetClipboardData(CF_ENHMETAFILE
,
261 (void *)emfCopy
.GetHENHMETAFILE());
264 #endif // wxUSE_ENH_METAFILE
272 wxLogError(_("Unsupported clipboard format."));
277 dataFormat
= wxDF_TEXT
;
282 char *s
= (char *)data
;
284 width
= strlen(s
) + 1;
286 DWORD l
= (width
* height
);
287 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
290 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
292 memcpy(lpGlobalMemory
, s
, l
);
294 GlobalUnlock(hGlobalMemory
);
297 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
300 // Only tested with Visual C++ 6.0 so far
301 #if defined(__VISUALC__)
304 char* html
= (char *)data
;
306 // Create temporary buffer for HTML header...
307 char *buf
= new char [400 + strlen(html
)];
308 if(!buf
) return false;
310 // Create a template string for the HTML header...
313 "StartHTML:00000000\r\n"
314 "EndHTML:00000000\r\n"
315 "StartFragment:00000000\r\n"
316 "EndFragment:00000000\r\n"
318 "<!--StartFragment -->\r\n");
320 // Append the HTML...
323 // Finish up the HTML format...
325 "<!--EndFragment-->\r\n"
329 // Now go back, calculate all the lengths, and write out the
330 // necessary header information. Note, wsprintf() truncates the
331 // string when you overwrite it so you follow up with code to replace
332 // the 0 appended at the end with a '\r'...
333 char *ptr
= strstr(buf
, "StartHTML");
334 sprintf(ptr
+10, "%08u", (unsigned)(strstr(buf
, "<html>") - buf
));
337 ptr
= strstr(buf
, "EndHTML");
338 sprintf(ptr
+8, "%08u", (unsigned)strlen(buf
));
341 ptr
= strstr(buf
, "StartFragment");
342 sprintf(ptr
+14, "%08u", (unsigned)(strstr(buf
, "<!--StartFrag") - buf
));
345 ptr
= strstr(buf
, "EndFragment");
346 sprintf(ptr
+12, "%08u", (unsigned)(strstr(buf
, "<!--EndFrag") - buf
));
349 // Now you have everything in place ready to put on the
352 // Allocate global memory for transfer...
353 HGLOBAL hText
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
, strlen(buf
)+4);
355 // Put your string in the global memory...
356 ptr
= (char *)GlobalLock(hText
);
360 handle
= ::SetClipboardData(gs_htmlcfid
, hText
);
374 wxLogSysError(_("Failed to set clipboard data."));
382 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
386 switch ( dataFormat
)
392 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
396 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
397 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
399 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
400 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
402 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
406 SelectObject(hdcSrc
, old
);
412 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
413 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
414 hdcSrc
, 0, 0, SRCCOPY
);
416 // Select new bitmap out of memory DC
417 SelectObject(hdcMem
, old1
);
420 SelectObject(hdcSrc
, old
);
424 // Create and return a new wxBitmap
425 wxBitmap
*wxBM
= new wxBitmap
;
426 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
427 wxBM
->SetWidth(bm
.bmWidth
);
428 wxBM
->SetHeight(bm
.bmHeight
);
429 wxBM
->SetDepth(bm
.bmPlanes
);
440 wxLogError(_("Unsupported clipboard format."));
444 dataFormat
= wxDF_TEXT
;
449 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
453 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
457 char *s
= new char[hsize
];
461 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
463 memcpy(s
, lpGlobalMemory
, hsize
);
465 GlobalUnlock(hGlobalMemory
);
473 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
474 if ( !hGlobalMemory
)
477 DWORD size
= ::GlobalSize(hGlobalMemory
);
481 void *buf
= malloc(size
);
485 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
487 memcpy(buf
, lpGlobalMemory
, size
);
489 GlobalUnlock(hGlobalMemory
);
498 wxLogSysError(_("Failed to retrieve data from the clipboard."));
504 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
506 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
509 int wxRegisterClipboardFormat(wxChar
*formatName
)
511 return ::RegisterClipboardFormat(formatName
);
514 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
518 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
521 // ---------------------------------------------------------------------------
523 // ---------------------------------------------------------------------------
525 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
527 wxClipboard::wxClipboard()
529 #if wxUSE_OLE_CLIPBOARD
533 m_lastDataObject
= NULL
;
537 wxClipboard::~wxClipboard()
539 if ( m_lastDataObject
)
544 #if wxUSE_OLE_CLIPBOARD
549 void wxClipboard::Clear()
551 if ( IsUsingPrimarySelection() )
554 #if wxUSE_OLE_CLIPBOARD
555 if (m_lastDataObject
)
557 // don't touch data set by other applications
558 HRESULT hr
= OleIsCurrentClipboard(m_lastDataObject
);
561 hr
= OleSetClipboard(NULL
);
564 wxLogApiError(wxT("OleSetClipboard(NULL)"), hr
);
567 m_lastDataObject
= NULL
;
569 #endif // wxUSE_OLE_CLIPBOARD
572 bool wxClipboard::Flush()
574 #if wxUSE_OLE_CLIPBOARD
575 if (m_lastDataObject
)
577 // don't touch data set by other applications
578 HRESULT hr
= OleIsCurrentClipboard(m_lastDataObject
);
579 m_lastDataObject
= NULL
;
582 hr
= OleFlushClipboard();
585 wxLogApiError(wxT("OleFlushClipboard"), hr
);
593 #else // !wxUSE_OLE_CLIPBOARD
595 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
598 bool wxClipboard::Open()
600 // Get clipboard id for HTML format...
602 gs_htmlcfid
= RegisterClipboardFormat(wxT("HTML Format"));
604 // OLE opens clipboard for us
606 #if wxUSE_OLE_CLIPBOARD
609 return wxOpenClipboard();
613 bool wxClipboard::IsOpened() const
615 #if wxUSE_OLE_CLIPBOARD
618 return wxIsClipboardOpened();
622 bool wxClipboard::SetData( wxDataObject
*data
)
624 if ( IsUsingPrimarySelection() )
627 #if !wxUSE_OLE_CLIPBOARD
628 (void)wxEmptyClipboard();
629 #endif // wxUSE_OLE_CLIPBOARD
632 return AddData(data
);
637 bool wxClipboard::AddData( wxDataObject
*data
)
639 if ( IsUsingPrimarySelection() )
642 wxCHECK_MSG( data
, false, wxT("data is invalid") );
644 #if wxUSE_OLE_CLIPBOARD
645 HRESULT hr
= OleSetClipboard(data
->GetInterface());
648 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
650 // don't free anything in this case
655 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
656 // using OLE clipboard when the app terminates - by default, we call
657 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
658 // wxClipboard::Flush() to change this
659 m_lastDataObject
= data
->GetInterface();
661 // we have a problem here because we should delete wxDataObject, but we
662 // can't do it because IDataObject which we just gave to the clipboard
663 // would try to use it when it will need the data. IDataObject is ref
664 // counted and so doesn't suffer from such problem, so we release it now
665 // and tell it to delete wxDataObject when it is deleted itself.
666 data
->SetAutoDelete();
670 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
672 wxDataFormat format
= data
->GetPreferredFormat();
679 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
680 wxString
str(textDataObject
->GetText());
681 return wxSetClipboardData(format
, str
.c_str());
687 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
688 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
689 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
697 wxLogError(wxT("Not implemented because wxMetafileDataObject does not contain width and height values."));
700 wxMetafileDataObject
* metaFileDataObject
=
701 (wxMetafileDataObject
*) data
;
702 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
703 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
704 metaFileDataObject
->GetWidth(),
705 metaFileDataObject
->GetHeight());
708 #endif // wxUSE_METAFILE
712 // This didn't compile, of course
713 // return wxSetClipboardData(data);
715 wxLogError(wxT("Not implemented."));
719 #else // !wxUSE_DATAOBJ
721 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
724 void wxClipboard::Close()
727 // OLE closes clipboard for us
728 #if !wxUSE_OLE_CLIPBOARD
733 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
735 return !IsUsingPrimarySelection() && wxIsClipboardFormatAvailable(format
);
738 bool wxClipboard::GetData( wxDataObject
& data
)
740 if ( IsUsingPrimarySelection() )
743 #if wxUSE_OLE_CLIPBOARD
744 IDataObject
*pDataObject
= NULL
;
745 HRESULT hr
= OleGetClipboard(&pDataObject
);
746 if ( FAILED(hr
) || !pDataObject
)
748 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
753 // build the list of supported formats
754 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
756 wxDataFormat
*formats
;
759 // the most common case
764 // bad luck, need to alloc mem
765 formats
= new wxDataFormat
[nFormats
];
768 data
.GetAllFormats(formats
, wxDataObject::Set
);
770 // get the data for the given formats
775 // enumerate all explicit formats on the clipboard.
776 // note that this does not include implicit / synthetic (automatically
777 // converted) formats.
778 #if wxDEBUG_LEVEL >= 2
779 // get the format enumerator
780 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
781 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
782 if ( FAILED(hr
) || !pEnumFormatEtc
)
785 _("Failed to retrieve the supported clipboard formats"));
789 // ask for the supported formats and see if there are any we support
793 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
795 // don't use FAILED() because S_FALSE would pass it
802 cf
= formatEtc
.cfFormat
;
804 wxLogTrace(wxTRACE_OleCalls
,
805 wxT("Object on the clipboard supports format %s."),
806 wxDataObject::GetFormatName(cf
));
809 pEnumFormatEtc
->Release();
811 #endif // wxDEBUG_LEVEL >= 2
814 // stop at the first valid format found on the clipboard
815 for ( size_t n
= 0; !result
&& (n
< nFormats
); n
++ )
817 // convert to NativeFormat Id
818 cf
= formats
[n
].GetFormatId();
822 // if the format is not available, try the next one
823 // this test includes implicit / sythetic formats
824 if ( !::IsClipboardFormatAvailable(cf
) )
827 formatEtc
.cfFormat
= cf
;
828 formatEtc
.ptd
= NULL
;
829 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
830 formatEtc
.lindex
= -1;
832 // use the appropriate tymed
833 switch ( formatEtc
.cfFormat
)
836 formatEtc
.tymed
= TYMED_GDI
;
840 case CF_METAFILEPICT
:
841 formatEtc
.tymed
= TYMED_MFPICT
;
845 formatEtc
.tymed
= TYMED_ENHMF
;
850 formatEtc
.tymed
= TYMED_HGLOBAL
;
854 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
857 // try other tymed for GDI objects
858 if ( formatEtc
.cfFormat
== CF_BITMAP
)
860 formatEtc
.tymed
= TYMED_HGLOBAL
;
861 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
867 // pass the data to the data object
868 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, true);
871 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
873 // IDataObject only takes the ownership of data if it
874 // successfully got it - which is not the case here
875 ReleaseStgMedium(&medium
);
882 //else: unsupported tymed?
885 if ( formats
!= &format
)
889 //else: we didn't allocate any memory
891 // clean up and return
892 pDataObject
->Release();
896 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
898 wxDataFormat format
= data
.GetPreferredFormat();
904 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
905 char* s
= (char*)wxGetClipboardData(format
);
909 textDataObject
.SetText(wxString::FromAscii(s
));
918 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
919 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
923 bitmapDataObject
.SetBitmap(*bitmap
);
931 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
932 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
936 metaFileDataObject
.SetMetafile(*metaFile
);
941 #endif // wxUSE_METAFILE
944 #else // !wxUSE_DATAOBJ
945 wxFAIL_MSG( wxT("no clipboard implementation") );
947 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
950 #endif // wxUSE_CLIPBOARD