]>
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
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ===========================================================================
13 // ===========================================================================
15 // ---------------------------------------------------------------------------
17 // ---------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
28 #include "wx/clipbrd.h"
31 #include "wx/object.h"
35 #include "wx/bitmap.h"
39 #include "wx/dataobj.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
60 #if wxUSE_OLE && !defined(__WXWINCE__)
62 #define wxUSE_OLE_CLIPBOARD 1
63 #else // !wxUSE_DATAOBJ
64 // use Win clipboard API
65 #define wxUSE_OLE_CLIPBOARD 0
68 #if wxUSE_OLE_CLIPBOARD
70 #endif // wxUSE_OLE_CLIPBOARD
72 // ===========================================================================
74 // ===========================================================================
76 // ---------------------------------------------------------------------------
77 // old-style clipboard functions using Windows API
78 // ---------------------------------------------------------------------------
80 static bool gs_wxClipboardIsOpen
= false;
81 static int gs_htmlcfid
= 0;
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("Cannot 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();
146 if ( ::IsClipboardFormatAvailable(cf
) )
148 // ok from the first try
152 // for several standard formats, we can convert from some other ones too
155 // for bitmaps, DIBs will also do
157 return ::IsClipboardFormatAvailable(CF_DIB
) != 0;
159 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
160 case CF_METAFILEPICT
:
161 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
162 #endif // wxUSE_ENH_METAFILE
170 bool wxSetClipboardData(wxDataFormat dataFormat
,
172 int width
, int height
)
174 HANDLE handle
= 0; // return value of SetClipboardData
180 wxBitmap
*bitmap
= (wxBitmap
*)data
;
182 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
183 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
184 HBITMAP old
= (HBITMAP
)
185 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
186 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
188 bitmap
->GetHeight());
191 SelectObject(hdcSrc
, old
);
197 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
198 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
199 hdcSrc
, 0, 0, SRCCOPY
);
201 // Select new bitmap out of memory DC
202 SelectObject(hdcMem
, old1
);
205 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
208 SelectObject(hdcSrc
, old
);
217 wxBitmap
*bitmap
= (wxBitmap
*)data
;
219 if ( bitmap
&& bitmap
->IsOk() )
224 handle
= ::SetClipboardData(CF_DIB
, dib
.Detach());
231 // VZ: I'm told that this code works, but it doesn't seem to work for me
232 // and, anyhow, I'd be highly surprised if it did. So I leave it here
233 // but IMNSHO it is completely broken.
234 #if wxUSE_METAFILE && !defined(wxMETAFILE_IS_ENH) && !defined(__WXWINCE__)
237 wxMetafile
*wxMF
= (wxMetafile
*)data
;
238 HANDLE data
= GlobalAlloc(GHND
, sizeof(METAFILEPICT
) + 1);
239 METAFILEPICT
*mf
= (METAFILEPICT
*)GlobalLock(data
);
241 mf
->mm
= wxMF
->GetWindowsMappingMode();
244 mf
->hMF
= (HMETAFILE
) wxMF
->GetHMETAFILE();
246 wxMF
->SetHMETAFILE((WXHANDLE
) NULL
);
248 handle
= SetClipboardData(CF_METAFILEPICT
, data
);
251 #endif // wxUSE_METAFILE
253 #if wxUSE_ENH_METAFILE && !defined(__WXWINCE__)
254 case wxDF_ENHMETAFILE
:
256 wxEnhMetaFile
*emf
= (wxEnhMetaFile
*)data
;
257 wxEnhMetaFile emfCopy
= *emf
;
259 handle
= SetClipboardData(CF_ENHMETAFILE
,
260 (void *)emfCopy
.GetHENHMETAFILE());
263 #endif // wxUSE_ENH_METAFILE
271 wxLogError(_("Unsupported clipboard format."));
276 dataFormat
= wxDF_TEXT
;
281 char *s
= (char *)data
;
283 width
= strlen(s
) + 1;
285 DWORD l
= (width
* height
);
286 HANDLE hGlobalMemory
= GlobalAlloc(GHND
, l
);
289 LPSTR lpGlobalMemory
= (LPSTR
)GlobalLock(hGlobalMemory
);
291 memcpy(lpGlobalMemory
, s
, l
);
293 GlobalUnlock(hGlobalMemory
);
296 handle
= SetClipboardData(dataFormat
, hGlobalMemory
);
299 // Only tested with Visual C++ 6.0 so far
300 #if defined(__VISUALC__)
303 char* html
= (char *)data
;
305 // Create temporary buffer for HTML header...
306 char *buf
= new char [400 + strlen(html
)];
307 if(!buf
) return false;
309 // Create a template string for the HTML header...
312 "StartHTML:00000000\r\n"
313 "EndHTML:00000000\r\n"
314 "StartFragment:00000000\r\n"
315 "EndFragment:00000000\r\n"
317 "<!--StartFragment -->\r\n");
319 // Append the HTML...
322 // Finish up the HTML format...
324 "<!--EndFragment-->\r\n"
328 // Now go back, calculate all the lengths, and write out the
329 // necessary header information. Note, wsprintf() truncates the
330 // string when you overwrite it so you follow up with code to replace
331 // the 0 appended at the end with a '\r'...
332 char *ptr
= strstr(buf
, "StartHTML");
333 sprintf(ptr
+10, "%08u", (unsigned)(strstr(buf
, "<html>") - buf
));
336 ptr
= strstr(buf
, "EndHTML");
337 sprintf(ptr
+8, "%08u", (unsigned)strlen(buf
));
340 ptr
= strstr(buf
, "StartFragment");
341 sprintf(ptr
+14, "%08u", (unsigned)(strstr(buf
, "<!--StartFrag") - buf
));
344 ptr
= strstr(buf
, "EndFragment");
345 sprintf(ptr
+12, "%08u", (unsigned)(strstr(buf
, "<!--EndFrag") - buf
));
348 // Now you have everything in place ready to put on the
351 // Allocate global memory for transfer...
352 HGLOBAL hText
= GlobalAlloc(GMEM_MOVEABLE
|GMEM_DDESHARE
, strlen(buf
)+4);
354 // Put your string in the global memory...
355 ptr
= (char *)GlobalLock(hText
);
359 handle
= ::SetClipboardData(gs_htmlcfid
, hText
);
373 wxLogSysError(_("Failed to set clipboard data."));
381 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
385 switch ( dataFormat
)
391 HBITMAP hBitmap
= (HBITMAP
) GetClipboardData(CF_BITMAP
);
395 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
396 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
398 HBITMAP old
= (HBITMAP
) ::SelectObject(hdcSrc
, hBitmap
);
399 GetObject(hBitmap
, sizeof(BITMAP
), (LPSTR
)&bm
);
401 HBITMAP hNewBitmap
= CreateBitmapIndirect(&bm
);
405 SelectObject(hdcSrc
, old
);
411 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hNewBitmap
);
412 BitBlt(hdcMem
, 0, 0, bm
.bmWidth
, bm
.bmHeight
,
413 hdcSrc
, 0, 0, SRCCOPY
);
415 // Select new bitmap out of memory DC
416 SelectObject(hdcMem
, old1
);
419 SelectObject(hdcSrc
, old
);
423 // Create and return a new wxBitmap
424 wxBitmap
*wxBM
= new wxBitmap
;
425 wxBM
->SetHBITMAP((WXHBITMAP
) hNewBitmap
);
426 wxBM
->SetWidth(bm
.bmWidth
);
427 wxBM
->SetHeight(bm
.bmHeight
);
428 wxBM
->SetDepth(bm
.bmPlanes
);
439 wxLogError(_("Unsupported clipboard format."));
443 dataFormat
= wxDF_TEXT
;
448 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
452 DWORD hsize
= ::GlobalSize(hGlobalMemory
);
456 char *s
= new char[hsize
];
460 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
462 memcpy(s
, lpGlobalMemory
, hsize
);
464 GlobalUnlock(hGlobalMemory
);
472 HANDLE hGlobalMemory
= ::GetClipboardData(dataFormat
);
473 if ( !hGlobalMemory
)
476 DWORD size
= ::GlobalSize(hGlobalMemory
);
480 void *buf
= malloc(size
);
484 LPSTR lpGlobalMemory
= (LPSTR
) GlobalLock(hGlobalMemory
);
486 memcpy(buf
, lpGlobalMemory
, size
);
488 GlobalUnlock(hGlobalMemory
);
497 wxLogSysError(_("Failed to retrieve data from the clipboard."));
503 wxDataFormat
wxEnumClipboardFormats(wxDataFormat dataFormat
)
505 return (wxDataFormat::NativeFormat
)::EnumClipboardFormats(dataFormat
);
508 int wxRegisterClipboardFormat(wxChar
*formatName
)
510 return ::RegisterClipboardFormat(formatName
);
513 bool wxGetClipboardFormatName(wxDataFormat dataFormat
,
517 return ::GetClipboardFormatName((int)dataFormat
, formatName
, maxCount
) > 0;
520 // ---------------------------------------------------------------------------
522 // ---------------------------------------------------------------------------
524 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
526 wxClipboard::wxClipboard()
528 #if wxUSE_OLE_CLIPBOARD
532 m_lastDataObject
= NULL
;
536 wxClipboard::~wxClipboard()
538 if ( m_lastDataObject
)
543 #if wxUSE_OLE_CLIPBOARD
548 void wxClipboard::Clear()
550 if ( IsUsingPrimarySelection() )
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 // Get clipboard id for HTML format...
601 gs_htmlcfid
= RegisterClipboardFormat(wxT("HTML Format"));
603 // OLE opens clipboard for us
605 #if wxUSE_OLE_CLIPBOARD
608 return wxOpenClipboard();
612 bool wxClipboard::IsOpened() const
614 #if wxUSE_OLE_CLIPBOARD
617 return wxIsClipboardOpened();
621 bool wxClipboard::SetData( wxDataObject
*data
)
623 if ( IsUsingPrimarySelection() )
626 #if !wxUSE_OLE_CLIPBOARD
627 (void)wxEmptyClipboard();
628 #endif // wxUSE_OLE_CLIPBOARD
631 return AddData(data
);
636 bool wxClipboard::AddData( wxDataObject
*data
)
638 if ( IsUsingPrimarySelection() )
641 wxCHECK_MSG( data
, false, wxT("data is invalid") );
643 #if wxUSE_OLE_CLIPBOARD
644 HRESULT hr
= OleSetClipboard(data
->GetInterface());
647 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
649 // don't free anything in this case
654 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
655 // using OLE clipboard when the app terminates - by default, we call
656 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
657 // wxClipboard::Flush() to change this
658 m_lastDataObject
= data
->GetInterface();
660 // we have a problem here because we should delete wxDataObject, but we
661 // can't do it because IDataObject which we just gave to the clipboard
662 // would try to use it when it will need the data. IDataObject is ref
663 // counted and so doesn't suffer from such problem, so we release it now
664 // and tell it to delete wxDataObject when it is deleted itself.
665 data
->SetAutoDelete();
669 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
671 wxDataFormat format
= data
->GetPreferredFormat();
678 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
679 wxString
str(textDataObject
->GetText());
680 return wxSetClipboardData(format
, str
.c_str());
686 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
687 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
688 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
696 wxLogError(wxT("Not implemented because wxMetafileDataObject does not contain width and height values."));
699 wxMetafileDataObject
* metaFileDataObject
=
700 (wxMetafileDataObject
*) data
;
701 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
702 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
703 metaFileDataObject
->GetWidth(),
704 metaFileDataObject
->GetHeight());
707 #endif // wxUSE_METAFILE
711 // This didn't compile, of course
712 // return wxSetClipboardData(data);
714 wxLogError(wxT("Not implemented."));
718 #else // !wxUSE_DATAOBJ
720 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
723 void wxClipboard::Close()
726 // OLE closes clipboard for us
727 #if !wxUSE_OLE_CLIPBOARD
732 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
734 return !IsUsingPrimarySelection() && wxIsClipboardFormatAvailable(format
);
737 bool wxClipboard::GetData( wxDataObject
& data
)
739 if ( IsUsingPrimarySelection() )
742 #if wxUSE_OLE_CLIPBOARD
743 IDataObject
*pDataObject
= NULL
;
744 HRESULT hr
= OleGetClipboard(&pDataObject
);
745 if ( FAILED(hr
) || !pDataObject
)
747 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
752 // build the list of supported formats
753 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
755 wxDataFormat
*formats
;
758 // the most common case
763 // bad luck, need to alloc mem
764 formats
= new wxDataFormat
[nFormats
];
767 data
.GetAllFormats(formats
, wxDataObject::Set
);
769 // get the data for the given formats
774 // enumerate all explicit formats on the clipboard.
775 // note that this does not include implicit / synthetic (automatically
776 // converted) formats.
777 #if wxDEBUG_LEVEL >= 2
778 // get the format enumerator
779 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
780 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
781 if ( FAILED(hr
) || !pEnumFormatEtc
)
784 _("Failed to retrieve the supported clipboard formats"));
788 // ask for the supported formats and see if there are any we support
792 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
794 // don't use FAILED() because S_FALSE would pass it
801 cf
= formatEtc
.cfFormat
;
803 wxLogTrace(wxTRACE_OleCalls
,
804 wxT("Object on the clipboard supports format %s."),
805 wxDataObject::GetFormatName(cf
));
808 pEnumFormatEtc
->Release();
810 #endif // wxDEBUG_LEVEL >= 2
813 // stop at the first valid format found on the clipboard
814 for ( size_t n
= 0; !result
&& (n
< nFormats
); n
++ )
816 // convert to NativeFormat Id
817 cf
= formats
[n
].GetFormatId();
821 // if the format is not available, try the next one
822 // this test includes implicit / sythetic formats
823 if ( !::IsClipboardFormatAvailable(cf
) )
826 formatEtc
.cfFormat
= cf
;
827 formatEtc
.ptd
= NULL
;
828 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
829 formatEtc
.lindex
= -1;
831 // use the appropriate tymed
832 switch ( formatEtc
.cfFormat
)
835 formatEtc
.tymed
= TYMED_GDI
;
839 case CF_METAFILEPICT
:
840 formatEtc
.tymed
= TYMED_MFPICT
;
844 formatEtc
.tymed
= TYMED_ENHMF
;
849 formatEtc
.tymed
= TYMED_HGLOBAL
;
853 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
856 // try other tymed for GDI objects
857 if ( formatEtc
.cfFormat
== CF_BITMAP
)
859 formatEtc
.tymed
= TYMED_HGLOBAL
;
860 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
866 // pass the data to the data object
867 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, true);
870 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
872 // IDataObject only takes the ownership of data if it
873 // successfully got it - which is not the case here
874 ReleaseStgMedium(&medium
);
881 //else: unsupported tymed?
884 if ( formats
!= &format
)
888 //else: we didn't allocate any memory
890 // clean up and return
891 pDataObject
->Release();
895 wxCHECK_MSG( wxIsClipboardOpened(), false, wxT("clipboard not open") );
897 wxDataFormat format
= data
.GetPreferredFormat();
903 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
904 char* s
= (char*)wxGetClipboardData(format
);
908 textDataObject
.SetText(wxString::FromAscii(s
));
917 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
918 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
922 bitmapDataObject
.SetBitmap(*bitmap
);
930 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
931 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
935 metaFileDataObject
.SetMetafile(*metaFile
);
940 #endif // wxUSE_METAFILE
943 #else // !wxUSE_DATAOBJ
944 wxFAIL_MSG( wxT("no clipboard implementation") );
946 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
949 #endif // wxUSE_CLIPBOARD