]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/clipbrd.cpp
fa65cd2a21c08fc59136a528236f2762b719cad1
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;
83 bool wxOpenClipboard()
85 wxCHECK_MSG( !gs_wxClipboardIsOpen
, true, wxT("clipboard already opened.") );
87 wxWindow
*win
= wxTheApp
->GetTopWindow();
90 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
92 if ( !gs_wxClipboardIsOpen
)
94 wxLogSysError(_("Failed to open the clipboard."));
97 return gs_wxClipboardIsOpen
;
101 wxLogDebug(wxT("Can not open clipboard without a main window."));
107 bool wxCloseClipboard()
109 wxCHECK_MSG( gs_wxClipboardIsOpen
, false, wxT("clipboard is not opened") );
111 gs_wxClipboardIsOpen
= false;
113 if ( ::CloseClipboard() == 0 )
115 wxLogSysError(_("Failed to close the clipboard."));
123 bool wxEmptyClipboard()
125 if ( ::EmptyClipboard() == 0 )
127 wxLogSysError(_("Failed to empty the clipboard."));
135 bool wxIsClipboardOpened()
137 return gs_wxClipboardIsOpen
;
140 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
142 wxDataFormat::NativeFormat cf
= dataFormat
.GetFormatId();
144 if ( ::IsClipboardFormatAvailable(cf
) )
146 // ok from the first try
150 // for several standard formats, we can convert from some other ones too
153 // for bitmaps, DIBs will also do
155 return ::IsClipboardFormatAvailable(CF_DIB
) != 0;
157 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
158 case CF_METAFILEPICT
:
159 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
160 #endif // wxUSE_ENH_METAFILE
168 bool wxSetClipboardData(wxDataFormat dataFormat
,
170 int width
, int height
)
172 HANDLE handle
= 0; // return value of SetClipboardData
178 wxBitmap
*bitmap
= (wxBitmap
*)data
;
180 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
181 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
182 HBITMAP old
= (HBITMAP
)
183 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
184 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
186 bitmap
->GetHeight());
189 SelectObject(hdcSrc
, old
);
195 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
196 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
197 hdcSrc
, 0, 0, SRCCOPY
);
199 // Select new bitmap out of memory DC
200 SelectObject(hdcMem
, old1
);
203 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
206 SelectObject(hdcSrc
, old
);
215 wxBitmap
*bitmap
= (wxBitmap
*)data
;
217 if ( bitmap
&& bitmap
->Ok() )
222 handle
= ::SetClipboardData(CF_DIB
, dib
.Detach());
229 // VZ: I'm told that this code works, but it doesn't seem to work for me
230 // and, anyhow, I'd be highly surprised if it did. So I leave it here
231 // but IMNSHO it is completely broken.
232 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
235 wxMetafile
*wxMF
= (wxMetafile
*)data
;
236 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
237 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
239 mf
->mm
= wxMF
->GetWindowsMappingMode();
242 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
244 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
246 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
249 #endif // wxUSE_METAFILE
251 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
252 case wxDF_ENHMETAFILE
:
254 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
255 wxEnhMetaFile emfCopy
= *emf
;
257 handle
= SetClipboardData(CF_ENHMETAFILE
,
258 (void *)emfCopy
.GetHENHMETAFILE());
261 #endif // wxUSE_ENH_METAFILE
269 wxLogError(_("Unsupported clipboard format."));
274 dataFormat
= wxDF_TEXT
;
279 char *s
= (char *)data
;
281 width
= strlen(s
) + 1;
283 DWORD l
= (width
* height
);
284 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
287 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
289 memcpy(lpGlobalMemory
, s
, l
);
291 GlobalUnlock(hGlobalMemory
);
294 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
297 // Only tested with Visual C++ 6.0 so far
298 #if defined(__VISUALC__)
301 char* html
= (char *)data
;
303 // Create temporary buffer for HTML header...
304 char *buf
= new char [400 + strlen(html
)];
305 if(!buf
) return false;
307 // Get clipboard id for HTML format...
309 if(!cfid
) cfid
= RegisterClipboardFormat(wxT("HTML Format"));
311 // Create a template string for the HTML header...
314 "StartHTML:00000000\r\n"
315 "EndHTML:00000000\r\n"
316 "StartFragment:00000000\r\n"
317 "EndFragment:00000000\r\n"
319 "<!--StartFragment -->\r\n");
321 // Append the HTML...
324 // Finish up the HTML format...
326 "<!--EndFragment-->\r\n"
330 // Now go back, calculate all the lengths, and write out the
331 // necessary header information. Note, wsprintf() truncates the
332 // string when you overwrite it so you follow up with code to replace
333 // the 0 appended at the end with a '\r'...
334 char *ptr
= strstr(buf
, "StartHTML");
335 sprintf(ptr
+10, "%08u", strstr(buf
, "<html>") - buf
);
338 ptr
= strstr(buf
, "EndHTML");
339 sprintf(ptr
+8, "%08u", strlen(buf
));
342 ptr
= strstr(buf
, "StartFragment");
343 sprintf(ptr
+14, "%08u", strstr(buf
, "<!--StartFrag") - buf
);
346 ptr
= strstr(buf
, "EndFragment");
347 sprintf(ptr
+12, "%08u", strstr(buf
, "<!--EndFrag") - buf
);
350 // Now you have everything in place ready to put on the
353 // Allocate global memory for transfer...
354 HGLOBAL hText
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
, strlen(buf
)+4);
356 // Put your string in the global memory...
357 ptr
= (char *)GlobalLock(hText
);
361 handle
= ::SetClipboardData(cfid
, hText
);
375 wxLogSysError(_("Failed to set clipboard data."));
383 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
387 switch ( dataFormat
)
393 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
397 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
398 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
400 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
401 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
403 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
407 SelectObject(hdcSrc
, old
);
413 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
414 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
415 hdcSrc
, 0, 0, SRCCOPY
);
417 // Select new bitmap out of memory DC
418 SelectObject(hdcMem
, old1
);
421 SelectObject(hdcSrc
, old
);
425 // Create and return a new wxBitmap
426 wxBitmap
*wxBM
= new wxBitmap
;
427 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
428 wxBM
->SetWidth(bm
.bmWidth
);
429 wxBM
->SetHeight(bm
.bmHeight
);
430 wxBM
->SetDepth(bm
.bmPlanes
);
441 wxLogError(_("Unsupported clipboard format."));
445 dataFormat
= wxDF_TEXT
;
450 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
454 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
458 char *s
= new char[hsize
];
462 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
464 memcpy(s
, lpGlobalMemory
, hsize
);
466 GlobalUnlock(hGlobalMemory
);
474 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
475 if ( !hGlobalMemory
)
478 DWORD size
= ::GlobalSize(hGlobalMemory
);
482 void *buf
= malloc(size
);
486 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
488 memcpy(buf
, lpGlobalMemory
, size
);
490 GlobalUnlock(hGlobalMemory
);
499 wxLogSysError(_("Failed to retrieve data from the clipboard."));
505 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
507 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
510 int wxRegisterClipboardFormat(wxChar
*formatName
)
512 return ::RegisterClipboardFormat(formatName
);
515 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
519 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
522 // ---------------------------------------------------------------------------
524 // ---------------------------------------------------------------------------
526 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
528 wxClipboard::wxClipboard()
530 #if wxUSE_OLE_CLIPBOARD
534 m_lastDataObject
= NULL
;
538 wxClipboard::~wxClipboard()
540 if ( m_lastDataObject
)
545 #if wxUSE_OLE_CLIPBOARD
550 void wxClipboard::Clear()
552 if ( IsUsingPrimarySelection() )
555 #if wxUSE_OLE_CLIPBOARD
556 if (m_lastDataObject
)
558 // don't touch data set by other applications
559 HRESULT hr
= OleIsCurrentClipboard(m_lastDataObject
);
562 hr
= OleSetClipboard(NULL
);
565 wxLogApiError(wxT("OleSetClipboard(NULL)"), hr
);
568 m_lastDataObject
= NULL
;
570 #endif // wxUSE_OLE_CLIPBOARD
573 bool wxClipboard::Flush()
575 #if wxUSE_OLE_CLIPBOARD
576 if (m_lastDataObject
)
578 // don't touch data set by other applications
579 HRESULT hr
= OleIsCurrentClipboard(m_lastDataObject
);
580 m_lastDataObject
= NULL
;
583 hr
= OleFlushClipboard();
586 wxLogApiError(wxT("OleFlushClipboard"), hr
);
594 #else // !wxUSE_OLE_CLIPBOARD
596 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
599 bool wxClipboard::Open()
601 // OLE opens clipboard for us
603 #if wxUSE_OLE_CLIPBOARD
606 return wxOpenClipboard();
610 bool wxClipboard::IsOpened() const
612 #if wxUSE_OLE_CLIPBOARD
615 return wxIsClipboardOpened();
619 bool wxClipboard::SetData( wxDataObject
*data
)
621 if ( IsUsingPrimarySelection() )
624 #if !wxUSE_OLE_CLIPBOARD
625 (void)wxEmptyClipboard();
626 #endif // wxUSE_OLE_CLIPBOARD
629 return AddData(data
);
634 bool wxClipboard::AddData( wxDataObject
*data
)
636 if ( IsUsingPrimarySelection() )
639 wxCHECK_MSG( data
, false, wxT("data is invalid") );
641 #if wxUSE_OLE_CLIPBOARD
642 HRESULT hr
= OleSetClipboard(data
->GetInterface());
645 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
647 // don't free anything in this case
652 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
653 // using OLE clipboard when the app terminates - by default, we call
654 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
655 // wxClipboard::Flush() to change this
656 m_lastDataObject
= data
->GetInterface();
658 // we have a problem here because we should delete wxDataObject, but we
659 // can't do it because IDataObject which we just gave to the clipboard
660 // would try to use it when it will need the data. IDataObject is ref
661 // counted and so doesn't suffer from such problem, so we release it now
662 // and tell it to delete wxDataObject when it is deleted itself.
663 data
->SetAutoDelete();
667 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
669 wxDataFormat format
= data
->GetPreferredFormat();
676 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
677 wxString
str(textDataObject
->GetText());
678 return wxSetClipboardData(format
, str
.c_str());
684 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
685 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
686 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
694 wxLogError(wxT("Not implemented because wxMetafileDataObject does not contain width and height values."));
697 wxMetafileDataObject
* metaFileDataObject
=
698 (wxMetafileDataObject
*) data
;
699 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
700 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
701 metaFileDataObject
->GetWidth(),
702 metaFileDataObject
->GetHeight());
705 #endif // wxUSE_METAFILE
709 // This didn't compile, of course
710 // return wxSetClipboardData(data);
712 wxLogError(wxT("Not implemented."));
716 #else // !wxUSE_DATAOBJ
718 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
721 void wxClipboard::Close()
724 // OLE closes clipboard for us
725 #if !wxUSE_OLE_CLIPBOARD
730 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
732 return !IsUsingPrimarySelection() && wxIsClipboardFormatAvailable(format
);
735 bool wxClipboard::GetData( wxDataObject
& data
)
737 if ( IsUsingPrimarySelection() )
740 #if wxUSE_OLE_CLIPBOARD
741 IDataObject
*pDataObject
= NULL
;
742 HRESULT hr
= OleGetClipboard(&pDataObject
);
743 if ( FAILED(hr
) || !pDataObject
)
745 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
750 // build the list of supported formats
751 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
753 wxDataFormat
*formats
;
756 // the most common case
761 // bad luck, need to alloc mem
762 formats
= new wxDataFormat
[nFormats
];
765 data
.GetAllFormats(formats
, wxDataObject::Set
);
767 // get the data for the given formats
772 // enumerate all explicit formats on the clipboard.
773 // note that this does not include implicit / synthetic (automatically
774 // converted) formats.
775 #if wxDEBUG_LEVEL >= 2
776 // get the format enumerator
777 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
778 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
779 if ( FAILED(hr
) || !pEnumFormatEtc
)
782 _("Failed to retrieve the supported clipboard formats"));
786 // ask for the supported formats and see if there are any we support
790 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
792 // don't use FAILED() because S_FALSE would pass it
799 cf
= formatEtc
.cfFormat
;
801 wxLogTrace(wxTRACE_OleCalls
,
802 wxT("Object on the clipboard supports format %s."),
803 wxDataObject::GetFormatName(cf
));
806 pEnumFormatEtc
->Release();
808 #endif // wxDEBUG_LEVEL >= 2
811 // stop at the first valid format found on the clipboard
812 for ( size_t n
= 0; !result
&& (n
< nFormats
); n
++ )
814 // convert to NativeFormat Id
815 cf
= formats
[n
].GetFormatId();
817 // if the format is not available, try the next one
818 // this test includes implicit / sythetic formats
819 if ( !::IsClipboardFormatAvailable(cf
) )
822 formatEtc
.cfFormat
= cf
;
823 formatEtc
.ptd
= NULL
;
824 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
825 formatEtc
.lindex
= -1;
827 // use the appropriate tymed
828 switch ( formatEtc
.cfFormat
)
831 formatEtc
.tymed
= TYMED_GDI
;
835 case CF_METAFILEPICT
:
836 formatEtc
.tymed
= TYMED_MFPICT
;
840 formatEtc
.tymed
= TYMED_ENHMF
;
845 formatEtc
.tymed
= TYMED_HGLOBAL
;
849 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
852 // try other tymed for GDI objects
853 if ( formatEtc
.cfFormat
== CF_BITMAP
)
855 formatEtc
.tymed
= TYMED_HGLOBAL
;
856 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
862 // pass the data to the data object
863 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, true);
866 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
868 // IDataObject only takes the ownership of data if it
869 // successfully got it - which is not the case here
870 ReleaseStgMedium(&medium
);
877 //else: unsupported tymed?
880 if ( formats
!= &format
)
884 //else: we didn't allocate any memory
886 // clean up and return
887 pDataObject
->Release();
891 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
893 wxDataFormat format
= data
.GetPreferredFormat();
899 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
900 char* s
= (char*)wxGetClipboardData(format
);
904 textDataObject
.SetText(wxString::FromAscii(s
));
913 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
914 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
918 bitmapDataObject
.SetBitmap(*bitmap
);
926 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
927 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
931 metaFileDataObject
.SetMetafile(*metaFile
);
936 #endif // wxUSE_METAFILE
939 #else // !wxUSE_DATAOBJ
940 wxFAIL_MSG( wxT("no clipboard implementation") );
942 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
945 #endif // wxUSE_CLIPBOARD