]>
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"
38 #include "wx/object.h"
42 #include "wx/bitmap.h"
48 #include "wx/metafile.h"
52 #include "wx/clipbrd.h"
56 #include "wx/msw/private.h"
59 #include "wx/msw/dib.h"
62 // wxDataObject is tied to OLE/drag and drop implementation, therefore so are
63 // the functions using wxDataObject in wxClipboard
64 //#define wxUSE_DATAOBJ wxUSE_DRAG_AND_DROP
67 #include "wx/dataobj.h"
70 #if wxUSE_OLE && !defined(__WXWINCE__)
72 #define wxUSE_OLE_CLIPBOARD 1
73 #else // !wxUSE_DATAOBJ
74 // use Win clipboard API
75 #define wxUSE_OLE_CLIPBOARD 0
78 #if wxUSE_OLE_CLIPBOARD
80 #endif // wxUSE_OLE_CLIPBOARD
82 // ===========================================================================
84 // ===========================================================================
86 // ---------------------------------------------------------------------------
87 // old-style clipboard functions using Windows API
88 // ---------------------------------------------------------------------------
90 static bool gs_wxClipboardIsOpen
= FALSE
;
92 bool wxOpenClipboard()
94 wxCHECK_MSG( !gs_wxClipboardIsOpen
, TRUE
, wxT("clipboard already opened.") );
96 wxWindow
*win
= wxTheApp
->GetTopWindow();
99 gs_wxClipboardIsOpen
= ::OpenClipboard((HWND
)win
->GetHWND()) != 0;
101 if ( !gs_wxClipboardIsOpen
)
102 wxLogSysError(_("Failed to open the clipboard."));
104 return gs_wxClipboardIsOpen
;
108 wxLogDebug(wxT("Can not open clipboard without a main window."));
114 bool wxCloseClipboard()
116 wxCHECK_MSG( gs_wxClipboardIsOpen
, FALSE
, wxT("clipboard is not opened") );
118 gs_wxClipboardIsOpen
= FALSE
;
120 if ( ::CloseClipboard() == 0 )
122 wxLogSysError(_("Failed to close the clipboard."));
130 bool wxEmptyClipboard()
132 if ( ::EmptyClipboard() == 0 )
134 wxLogSysError(_("Failed to empty the clipboard."));
142 bool wxIsClipboardOpened()
144 return gs_wxClipboardIsOpen
;
147 bool wxIsClipboardFormatAvailable(wxDataFormat dataFormat
)
149 wxDataFormat::NativeFormat cf
= dataFormat
.GetFormatId();
151 if ( ::IsClipboardFormatAvailable(cf
) )
153 // ok from the first try
157 // for several standard formats, we can convert from some other ones too
160 // for bitmaps, DIBs will also do
162 return ::IsClipboardFormatAvailable(CF_DIB
) != 0;
164 #if wxUSE_ENH_METAFILE && !defined(__WIN16__) && !defined(__WXWINCE__)
165 case CF_METAFILEPICT
:
166 return ::IsClipboardFormatAvailable(CF_ENHMETAFILE
) != 0;
167 #endif // wxUSE_ENH_METAFILE
175 bool wxSetClipboardData(wxDataFormat dataFormat
,
177 int width
, int height
)
179 HANDLE handle
= 0; // return value of SetClipboardData
185 wxBitmap
*bitmap
= (wxBitmap
*)data
;
187 HDC hdcMem
= CreateCompatibleDC((HDC
) NULL
);
188 HDC hdcSrc
= CreateCompatibleDC((HDC
) NULL
);
189 HBITMAP old
= (HBITMAP
)
190 ::SelectObject(hdcSrc
, (HBITMAP
)bitmap
->GetHBITMAP());
191 HBITMAP hBitmap
= CreateCompatibleBitmap(hdcSrc
,
193 bitmap
->GetHeight());
196 SelectObject(hdcSrc
, old
);
202 HBITMAP old1
= (HBITMAP
) SelectObject(hdcMem
, hBitmap
);
203 BitBlt(hdcMem
, 0, 0, bitmap
->GetWidth(), bitmap
->GetHeight(),
204 hdcSrc
, 0, 0, SRCCOPY
);
206 // Select new bitmap out of memory DC
207 SelectObject(hdcMem
, old1
);
210 handle
= ::SetClipboardData(CF_BITMAP
, hBitmap
);
213 SelectObject(hdcSrc
, old
);
222 wxBitmap
*bitmap
= (wxBitmap
*)data
;
224 HGLOBAL hDIB
= wxDIB::ConvertFromBitmap(GetHbitmapOf(*bitmap
));
227 handle
= ::SetClipboardData(CF_DIB
, hDIB
);
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(__WIN16__) && !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 m_clearOnExit
= FALSE
;
538 wxClipboard::~wxClipboard()
546 void wxClipboard::Clear()
548 #if wxUSE_OLE_CLIPBOARD
549 if ( FAILED(OleSetClipboard(NULL
)) )
551 wxLogLastError(wxT("OleSetClipboard(NULL)"));
556 bool wxClipboard::Flush()
558 #if wxUSE_OLE_CLIPBOARD
559 if ( FAILED(OleFlushClipboard()) )
561 wxLogLastError(wxT("OleFlushClipboard"));
567 m_clearOnExit
= FALSE
;
571 #else // !wxUSE_OLE_CLIPBOARD
573 #endif // wxUSE_OLE_CLIPBOARD/!wxUSE_OLE_CLIPBOARD
576 bool wxClipboard::Open()
578 // OLE opens clipboard for us
580 #if wxUSE_OLE_CLIPBOARD
583 return wxOpenClipboard();
587 bool wxClipboard::IsOpened() const
589 #if wxUSE_OLE_CLIPBOARD
592 return wxIsClipboardOpened();
596 bool wxClipboard::SetData( wxDataObject
*data
)
598 #if !wxUSE_OLE_CLIPBOARD
599 (void)wxEmptyClipboard();
600 #endif // wxUSE_OLE_CLIPBOARD
603 return AddData(data
);
608 bool wxClipboard::AddData( wxDataObject
*data
)
610 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
612 #if wxUSE_OLE_CLIPBOARD
613 HRESULT hr
= OleSetClipboard(data
->GetInterface());
616 wxLogSysError(hr
, _("Failed to put data on the clipboard"));
618 // don't free anything in this case
623 // we have a problem here because we should delete wxDataObject, but we
624 // can't do it because IDataObject which we just gave to the clipboard
625 // would try to use it when it will need the data. IDataObject is ref
626 // counted and so doesn't suffer from such problem, so we release it now
627 // and tell it to delete wxDataObject when it is deleted itself.
628 data
->SetAutoDelete();
630 // we have to call either OleSetClipboard(NULL) or OleFlushClipboard() when
631 // using OLE clipboard when the app terminates - by default, we call
632 // OleSetClipboard(NULL) which won't waste RAM, but the app can call
633 // wxClipboard::Flush() to chaneg this
634 m_clearOnExit
= TRUE
;
638 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
640 wxDataFormat format
= data
->GetPreferredFormat();
647 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
648 wxString
str(textDataObject
->GetText());
649 return wxSetClipboardData(format
, str
.c_str());
655 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
656 wxBitmap
bitmap(bitmapDataObject
->GetBitmap());
657 return wxSetClipboardData(data
->GetPreferredFormat(), &bitmap
);
665 wxLogError("Not implemented because wxMetafileDataObject does not contain width and height values.");
668 wxMetafileDataObject
* metaFileDataObject
=
669 (wxMetafileDataObject
*) data
;
670 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
671 return wxSetClipboardData(wxDF_METAFILE
, &metaFile
,
672 metaFileDataObject
->GetWidth(),
673 metaFileDataObject
->GetHeight());
676 #endif // wxUSE_METAFILE
680 // This didn't compile, of course
681 // return wxSetClipboardData(data);
683 wxLogError(wxT("Not implemented."));
687 #else // !wxUSE_DATAOBJ
689 #endif // wxUSE_DATAOBJ/!wxUSE_DATAOBJ
692 void wxClipboard::Close()
695 // OLE closes clipboard for us
696 #if !wxUSE_OLE_CLIPBOARD
701 bool wxClipboard::IsSupported( wxDataFormat format
)
703 return wxIsClipboardFormatAvailable(format
);
706 bool wxClipboard::GetData( wxDataObject
& data
)
708 #if wxUSE_OLE_CLIPBOARD
709 IDataObject
*pDataObject
= NULL
;
710 HRESULT hr
= OleGetClipboard(&pDataObject
);
711 if ( FAILED(hr
) || !pDataObject
)
713 wxLogSysError(hr
, _("Failed to get data from the clipboard"));
718 // build the list of supported formats
719 size_t nFormats
= data
.GetFormatCount(wxDataObject::Set
);
721 wxDataFormat
*formats
;
724 // the most common case
729 // bad luck, need to alloc mem
730 formats
= new wxDataFormat
[nFormats
];
733 data
.GetAllFormats(formats
, wxDataObject::Set
);
735 // get the data for the given formats
740 // enumerate all explicit formats on the clipboard.
741 // note that this does not include implicit / synthetic (automatically
742 // converted) formats.
744 // get the format enumerator
745 IEnumFORMATETC
*pEnumFormatEtc
= NULL
;
746 hr
= pDataObject
->EnumFormatEtc(DATADIR_GET
, &pEnumFormatEtc
);
747 if ( FAILED(hr
) || !pEnumFormatEtc
)
750 _("Failed to retrieve the supported clipboard formats"));
754 // ask for the supported formats and see if there are any we support
758 hr
= pEnumFormatEtc
->Next(1, &formatEtc
, &nCount
);
760 // don't use FAILED() because S_FALSE would pass it
767 cf
= formatEtc
.cfFormat
;
769 wxLogTrace(wxTRACE_OleCalls
,
770 wxT("Object on the clipboard supports format %s."),
771 wxDataObject::GetFormatName(cf
));
774 pEnumFormatEtc
->Release();
779 // stop at the first valid format found on the clipboard
780 for ( size_t n
= 0; !result
&& (n
< nFormats
); n
++ )
782 // convert to NativeFormat Id
783 cf
= formats
[n
].GetFormatId();
785 // if the format is not available, try the next one
786 // this test includes implicit / sythetic formats
787 if ( !::IsClipboardFormatAvailable(cf
) )
790 formatEtc
.cfFormat
= cf
;
791 formatEtc
.ptd
= NULL
;
792 formatEtc
.dwAspect
= DVASPECT_CONTENT
;
793 formatEtc
.lindex
= -1;
795 // use the appropriate tymed
796 switch ( formatEtc
.cfFormat
)
799 formatEtc
.tymed
= TYMED_GDI
;
803 case CF_METAFILEPICT
:
804 formatEtc
.tymed
= TYMED_MFPICT
;
808 formatEtc
.tymed
= TYMED_ENHMF
;
813 formatEtc
.tymed
= TYMED_HGLOBAL
;
817 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
820 // try other tymed for GDI objects
821 if ( formatEtc
.cfFormat
== CF_BITMAP
)
823 formatEtc
.tymed
= TYMED_HGLOBAL
;
824 hr
= pDataObject
->GetData(&formatEtc
, &medium
);
830 // pass the data to the data object
831 hr
= data
.GetInterface()->SetData(&formatEtc
, &medium
, TRUE
);
834 wxLogDebug(wxT("Failed to set data in wxIDataObject"));
836 // IDataObject only takes the ownership of data if it
837 // successfully got it - which is not the case here
838 ReleaseStgMedium(&medium
);
845 //else: unsupported tymed?
848 if ( formats
!= &format
)
852 //else: we didn't allocate any memory
854 // clean up and return
855 pDataObject
->Release();
859 wxCHECK_MSG( wxIsClipboardOpened(), FALSE
, wxT("clipboard not open") );
861 wxDataFormat format
= data
.GetPreferredFormat();
867 wxTextDataObject
& textDataObject
= (wxTextDataObject
&)data
;
868 char* s
= (char*)wxGetClipboardData(format
);
872 textDataObject
.SetText(wxString::FromAscii(s
));
881 wxBitmapDataObject
& bitmapDataObject
= (wxBitmapDataObject
&)data
;
882 wxBitmap
* bitmap
= (wxBitmap
*)wxGetClipboardData(data
.GetPreferredFormat());
886 bitmapDataObject
.SetBitmap(*bitmap
);
894 wxMetafileDataObject
& metaFileDataObject
= (wxMetafileDataObject
&)data
;
895 wxMetafile
* metaFile
= (wxMetafile
*)wxGetClipboardData(wxDF_METAFILE
);
899 metaFileDataObject
.SetMetafile(*metaFile
);
904 #endif // wxUSE_METAFILE
907 #else // !wxUSE_DATAOBJ
908 wxFAIL_MSG( wxT("no clipboard implementation") );
910 #endif // wxUSE_OLE_CLIPBOARD/wxUSE_DATAOBJ
913 #endif // wxUSE_CLIPBOARD