]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "clipbrd.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/object.h"
38 #include "wx/bitmap.h"
44 #include "wx/metafile.h"
48 #include "wx/clipbrd.h"
52 #include "wx/msw/private.h"
53 #include "wx/msw/ole/oleutils.h"
56 #include "wx/msw/dib.h"
59 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
60 // the functions using wxDataObject in wxClipboard
61 //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
64 #include "wx/dataobj.h"
67 #if wxUSE_OLE && !defined(__WXWINCE__)
69 #define wxUSE_OLE_CLIPBOARD 1
70 #else // !wxUSE_DATAOBJ
71 // use Win clipboard API
72 #define wxUSE_OLE_CLIPBOARD 0
75 #if wxUSE_OLE_CLIPBOARD
77 #endif // wxUSE_OLE_CLIPBOARD
79 // ===========================================================================
81 // ===========================================================================
83 // ---------------------------------------------------------------------------
84 // old-style clipboard functions using Windows API
85 // ---------------------------------------------------------------------------
87 static bool gs_wxClipboardIsOpen
= false;
89 bool wxOpenClipboard()
91 wxCHECK_MSG( !gs_wxClipboardIsOpen
, true, wxT("clipboard already opened.") );
93 wxWindow
*win
= wxTheApp
->GetTopWindow();
96 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
98 if ( !gs_wxClipboardIsOpen
)
99 wxLogSysError(_("Failed to open the clipboard."));
101 return gs_wxClipboardIsOpen
;
105 wxLogDebug(wxT("Can not open clipboard without a main window."));
111 bool wxCloseClipboard()
113 wxCHECK_MSG( gs_wxClipboardIsOpen
, false, wxT("clipboard is not opened") );
115 gs_wxClipboardIsOpen
= false;
117 if ( ::CloseClipboard() == 0 )
119 wxLogSysError(_("Failed to close the clipboard."));
127 bool wxEmptyClipboard()
129 if ( ::EmptyClipboard() == 0 )
131 wxLogSysError(_("Failed to empty the clipboard."));
139 bool wxIsClipboardOpened()
141 return gs_wxClipboardIsOpen
;
144 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
146 wxDataFormat::NativeFormat cf
= dataFormat
.GetFormatId();
148 if ( ::IsClipboardFormatAvailable(cf
) )
150 // ok from the first try
154 // for several standard formats, we can convert from some other ones too
157 // for bitmaps, DIBs will also do
159 return ::IsClipboardFormatAvailable(CF_DIB
) != 0;
161 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
162 case CF_METAFILEPICT
:
163 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
164 #endif // wxUSE_ENH_METAFILE
172 bool wxSetClipboardData(wxDataFormat dataFormat
,
174 int width
, int height
)
176 HANDLE handle
= 0; // return value of SetClipboardData
182 wxBitmap
*bitmap
= (wxBitmap
*)data
;
184 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
185 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
186 HBITMAP old
= (HBITMAP
)
187 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
188 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
190 bitmap
->GetHeight());
193 SelectObject(hdcSrc
, old
);
199 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
200 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
201 hdcSrc
, 0, 0, SRCCOPY
);
203 // Select new bitmap out of memory DC
204 SelectObject(hdcMem
, old1
);
207 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
210 SelectObject(hdcSrc
, old
);
219 wxBitmap
*bitmap
= (wxBitmap
*)data
;
221 if ( bitmap
&& bitmap
->Ok() )
226 handle
= ::SetClipboardData(CF_DIB
, dib
.Detach());
233 // VZ: I'm told that this code works, but it doesn't seem to work for me
234 // and, anyhow, I'd be highly surprised if it did. So I leave it here
235 // but IMNSHO it is completely broken.
236 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
239 wxMetafile
*wxMF
= (wxMetafile
*)data
;
240 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
241 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
243 mf
->mm
= wxMF
->GetWindowsMappingMode();
246 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
248 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
250 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
253 #endif // wxUSE_METAFILE
255 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
256 case wxDF_ENHMETAFILE
:
258 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
259 wxEnhMetaFile emfCopy
= *emf
;
261 handle
= SetClipboardData(CF_ENHMETAFILE
,
262 (void *)emfCopy
.GetHENHMETAFILE());
265 #endif // wxUSE_ENH_METAFILE
273 wxLogError(_("Unsupported clipboard format."));
278 dataFormat
= wxDF_TEXT
;
283 char *s
= (char *)data
;
285 width
= strlen(s
) + 1;
287 DWORD l
= (width
* height
);
288 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
291 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
293 memcpy(lpGlobalMemory
, s
, l
);
295 GlobalUnlock(hGlobalMemory
);
298 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
301 // Only tested with non-Unicode, Visual C++ 6.0 so far
302 #if defined(__VISUALC__) && !defined(UNICODE)
305 char* html
= (char *)data
;
307 // Create temporary buffer for HTML header...
308 char *buf
= new char [400 + strlen(html
)];
309 if(!buf
) return false;
311 // Get clipboard id for HTML format...
313 if(!cfid
) cfid
= RegisterClipboardFormat(wxT("HTML Format"));
315 // Create a template string for the HTML header...
318 "StartHTML:00000000\r\n"
319 "EndHTML:00000000\r\n"
320 "StartFragment:00000000\r\n"
321 "EndFragment:00000000\r\n"
323 "<!--StartFragment -->\r\n");
325 // Append the HTML...
328 // Finish up the HTML format...
330 "<!--EndFragment-->\r\n"
334 // Now go back, calculate all the lengths, and write out the
335 // necessary header information. Note, wsprintf() truncates the
336 // string when you overwrite it so you follow up with code to replace
337 // the 0 appended at the end with a '\r'...
338 char *ptr
= strstr(buf
, "StartHTML");
339 wsprintf(ptr
+10, "%08u", strstr(buf
, "<html>") - buf
);
342 ptr
= strstr(buf
, "EndHTML");
343 wsprintf(ptr
+8, "%08u", strlen(buf
));
346 ptr
= strstr(buf
, "StartFragment");
347 wsprintf(ptr
+14, "%08u", strstr(buf
, "<!--StartFrag") - buf
);
350 ptr
= strstr(buf
, "EndFragment");
351 wsprintf(ptr
+12, "%08u", strstr(buf
, "<!--EndFrag") - buf
);
354 // Now you have everything in place ready to put on the
357 // Allocate global memory for transfer...
358 HGLOBAL hText
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
, strlen(buf
)+4);
360 // Put your string in the global memory...
361 ptr
= (char *)GlobalLock(hText
);
365 handle
= ::SetClipboardData(cfid
, hText
);
379 wxLogSysError(_("Failed to set clipboard data."));
387 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
391 switch ( dataFormat
)
397 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
401 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
402 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
404 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
405 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
407 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
411 SelectObject(hdcSrc
, old
);
417 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
418 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
419 hdcSrc
, 0, 0, SRCCOPY
);
421 // Select new bitmap out of memory DC
422 SelectObject(hdcMem
, old1
);
425 SelectObject(hdcSrc
, old
);
429 // Create and return a new wxBitmap
430 wxBitmap
*wxBM
= new wxBitmap
;
431 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
432 wxBM
->SetWidth(bm
.bmWidth
);
433 wxBM
->SetHeight(bm
.bmHeight
);
434 wxBM
->SetDepth(bm
.bmPlanes
);
445 wxLogError(_("Unsupported clipboard format."));
449 dataFormat
= wxDF_TEXT
;
454 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
458 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
462 char *s
= new char[hsize
];
466 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
468 memcpy(s
, lpGlobalMemory
, hsize
);
470 GlobalUnlock(hGlobalMemory
);
478 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
479 if ( !hGlobalMemory
)
482 DWORD size
= ::GlobalSize(hGlobalMemory
);
486 void *buf
= malloc(size
);
490 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
492 memcpy(buf
, lpGlobalMemory
, size
);
494 GlobalUnlock(hGlobalMemory
);
503 wxLogSysError(_("Failed to retrieve data from the clipboard."));
509 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
511 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
514 int wxRegisterClipboardFormat(wxChar
*formatName
)
516 return ::RegisterClipboardFormat(formatName
);
519 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
523 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
526 // ---------------------------------------------------------------------------
528 // ---------------------------------------------------------------------------
530 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
532 wxClipboard::wxClipboard()
534 #if wxUSE_OLE_CLIPBOARD
538 m_clearOnExit
= false;
542 wxClipboard::~wxClipboard()
549 #if wxUSE_OLE_CLIPBOARD
554 void wxClipboard::Clear()
556 #if wxUSE_OLE_CLIPBOARD
557 HRESULT hr
= OleSetClipboard(NULL
);
560 wxLogApiError(wxT("OleSetClipboard(NULL)"), hr
);
562 #endif // wxUSE_OLE_CLIPBOARD
565 bool wxClipboard::Flush()
567 #if wxUSE_OLE_CLIPBOARD
568 HRESULT hr
= OleFlushClipboard();
571 wxLogApiError(wxT("OleFlushClipboard"), hr
);
577 m_clearOnExit
= false;
581 #else // !wxUSE_OLE_CLIPBOARD
583 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
586 bool wxClipboard::Open()
588 // OLE opens clipboard for us
590 #if wxUSE_OLE_CLIPBOARD
593 return wxOpenClipboard();
597 bool wxClipboard::IsOpened() const
599 #if wxUSE_OLE_CLIPBOARD
602 return wxIsClipboardOpened();
606 bool wxClipboard::SetData( wxDataObject
*data
)
608 #if !wxUSE_OLE_CLIPBOARD
609 (void)wxEmptyClipboard();
610 #endif // wxUSE_OLE_CLIPBOARD
613 return AddData(data
);
618 bool wxClipboard::AddData( wxDataObject
*data
)
620 wxCHECK_MSG( data
, false, wxT("data is invalid") );
622 #if wxUSE_OLE_CLIPBOARD
623 HRESULT hr
= OleSetClipboard(data
->GetInterface());
626 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
628 // don't free anything in this case
633 // we have a problem here because we should delete wxDataObject, but we
634 // can't do it because IDataObject which we just gave to the clipboard
635 // would try to use it when it will need the data. IDataObject is ref
636 // counted and so doesn't suffer from such problem, so we release it now
637 // and tell it to delete wxDataObject when it is deleted itself.
638 data
->SetAutoDelete();
640 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
641 // using OLE clipboard when the app terminates - by default, we call
642 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
643 // wxClipboard::Flush() to chaneg this
644 m_clearOnExit
= true;
648 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
650 wxDataFormat format
= data
->GetPreferredFormat();
657 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
658 wxString
str(textDataObject
->GetText());
659 return wxSetClipboardData(format
, str
.c_str());
665 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
666 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
667 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
675 wxLogError(wxT("Not implemented because wxMetafileDataObject does not contain width and height values."));
678 wxMetafileDataObject
* metaFileDataObject
=
679 (wxMetafileDataObject
*) data
;
680 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
681 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
682 metaFileDataObject
->GetWidth(),
683 metaFileDataObject
->GetHeight());
686 #endif // wxUSE_METAFILE
690 // This didn't compile, of course
691 // return wxSetClipboardData(data);
693 wxLogError(wxT("Not implemented."));
697 #else // !wxUSE_DATAOBJ
699 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
702 void wxClipboard::Close()
705 // OLE closes clipboard for us
706 #if !wxUSE_OLE_CLIPBOARD
711 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
713 return wxIsClipboardFormatAvailable(format
);
716 bool wxClipboard::GetData( wxDataObject
& data
)
718 #if wxUSE_OLE_CLIPBOARD
719 IDataObject
*pDataObject
= NULL
;
720 HRESULT hr
= OleGetClipboard(&pDataObject
);
721 if ( FAILED(hr
) || !pDataObject
)
723 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
728 // build the list of supported formats
729 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
731 wxDataFormat
*formats
;
734 // the most common case
739 // bad luck, need to alloc mem
740 formats
= new wxDataFormat
[nFormats
];
743 data
.GetAllFormats(formats
, wxDataObject::Set
);
745 // get the data for the given formats
750 // enumerate all explicit formats on the clipboard.
751 // note that this does not include implicit / synthetic (automatically
752 // converted) formats.
754 // get the format enumerator
755 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
756 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
757 if ( FAILED(hr
) || !pEnumFormatEtc
)
760 _("Failed to retrieve the supported clipboard formats"));
764 // ask for the supported formats and see if there are any we support
768 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
770 // don't use FAILED() because S_FALSE would pass it
777 cf
= formatEtc
.cfFormat
;
779 wxLogTrace(wxTRACE_OleCalls
,
780 wxT("Object on the clipboard supports format %s."),
781 wxDataObject::GetFormatName(cf
));
784 pEnumFormatEtc
->Release();
789 // stop at the first valid format found on the clipboard
790 for ( size_t n
= 0; !result
&& (n
< nFormats
); n
++ )
792 // convert to NativeFormat Id
793 cf
= formats
[n
].GetFormatId();
795 // if the format is not available, try the next one
796 // this test includes implicit / sythetic formats
797 if ( !::IsClipboardFormatAvailable(cf
) )
800 formatEtc
.cfFormat
= cf
;
801 formatEtc
.ptd
= NULL
;
802 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
803 formatEtc
.lindex
= -1;
805 // use the appropriate tymed
806 switch ( formatEtc
.cfFormat
)
809 formatEtc
.tymed
= TYMED_GDI
;
813 case CF_METAFILEPICT
:
814 formatEtc
.tymed
= TYMED_MFPICT
;
818 formatEtc
.tymed
= TYMED_ENHMF
;
823 formatEtc
.tymed
= TYMED_HGLOBAL
;
827 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
830 // try other tymed for GDI objects
831 if ( formatEtc
.cfFormat
== CF_BITMAP
)
833 formatEtc
.tymed
= TYMED_HGLOBAL
;
834 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
840 // pass the data to the data object
841 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, true);
844 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
846 // IDataObject only takes the ownership of data if it
847 // successfully got it - which is not the case here
848 ReleaseStgMedium(&medium
);
855 //else: unsupported tymed?
858 if ( formats
!= &format
)
862 //else: we didn't allocate any memory
864 // clean up and return
865 pDataObject
->Release();
869 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
871 wxDataFormat format
= data
.GetPreferredFormat();
877 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
878 char* s
= (char*)wxGetClipboardData(format
);
882 textDataObject
.SetText(wxString::FromAscii(s
));
891 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
892 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
896 bitmapDataObject
.SetBitmap(*bitmap
);
904 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
905 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
909 metaFileDataObject
.SetMetafile(*metaFile
);
914 #endif // wxUSE_METAFILE
917 #else // !wxUSE_DATAOBJ
918 wxFAIL_MSG( wxT("no clipboard implementation") );
920 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
923 #endif // wxUSE_CLIPBOARD