]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/ole/dataobj.cpp
3 // Purpose: implementation of wx[I]DataObject class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
31 #include "wx/wxcrtvararg.h"
34 #include "wx/dataobj.h"
36 #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
38 #include "wx/scopedarray.h"
39 #include "wx/vector.h"
40 #include "wx/msw/private.h" // includes <windows.h>
46 // for some compilers, the entire ole2.h must be included, not only oleauto.h
47 #if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__) || defined(__WXWINCE__)
54 #include "wx/msw/ole/oleutils.h"
56 #include "wx/msw/dib.h"
58 #ifndef CFSTR_SHELLURL
59 #define CFSTR_SHELLURL wxT("UniformResourceLocator")
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
67 static const wxChar
*GetTymedName(DWORD tymed
);
68 #else // !wxDEBUG_LEVEL
69 #define GetTymedName(tymed) wxEmptyString
70 #endif // wxDEBUG_LEVEL/!wxDEBUG_LEVEL
75 wxDataFormat
HtmlFormatFixup(wxDataFormat format
)
77 // Since the HTML format is dynamically registered, the wxDF_HTML
78 // format does not match the native constant in the way other formats do,
79 // so for the format checks below to work, we must change the native
80 // id to the wxDF_HTML constant.
82 if (::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)))
84 if (s_szBuf
== wxString("HTML Format"))
90 // helper function for wxCopyStgMedium()
91 HGLOBAL
wxGlobalClone(HGLOBAL hglobIn
)
93 HGLOBAL hglobOut
= NULL
;
95 LPVOID pvIn
= GlobalLock(hglobIn
);
98 SIZE_T cb
= GlobalSize(hglobIn
);
99 hglobOut
= GlobalAlloc(GMEM_FIXED
, cb
);
102 CopyMemory(hglobOut
, pvIn
, cb
);
104 GlobalUnlock(hglobIn
);
110 // Copies the given STGMEDIUM structure.
112 // This is an local implementation of the function with the same name in
113 // urlmon.lib but to use that function would require linking with urlmon.lib
114 // and we don't want to require it, so simple reimplement it here.
115 HRESULT
wxCopyStgMedium(const STGMEDIUM
*pmediumIn
, STGMEDIUM
*pmediumOut
)
118 STGMEDIUM stgmOut
= *pmediumIn
;
120 if (pmediumIn
->pUnkForRelease
== NULL
&&
121 !(pmediumIn
->tymed
& (TYMED_ISTREAM
| TYMED_ISTORAGE
)))
123 // Object needs to be cloned.
124 if (pmediumIn
->tymed
== TYMED_HGLOBAL
)
126 stgmOut
.hGlobal
= wxGlobalClone(pmediumIn
->hGlobal
);
127 if (!stgmOut
.hGlobal
)
129 hres
= E_OUTOFMEMORY
;
134 hres
= DV_E_TYMED
; // Don't know how to clone GDI objects.
138 if ( SUCCEEDED(hres
) )
140 switch ( stgmOut
.tymed
)
143 stgmOut
.pstm
->AddRef();
147 stgmOut
.pstg
->AddRef();
151 if ( stgmOut
.pUnkForRelease
)
152 stgmOut
.pUnkForRelease
->AddRef();
154 *pmediumOut
= stgmOut
;
160 } // anonymous namespace
162 // ----------------------------------------------------------------------------
163 // wxIEnumFORMATETC interface implementation
164 // ----------------------------------------------------------------------------
166 class wxIEnumFORMATETC
: public IEnumFORMATETC
169 wxIEnumFORMATETC(const wxDataFormat
* formats
, ULONG nCount
);
170 virtual ~wxIEnumFORMATETC() { delete [] m_formats
; }
173 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
174 STDMETHODIMP
Skip(ULONG celt
);
175 STDMETHODIMP
Reset();
176 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
178 DECLARE_IUNKNOWN_METHODS
;
181 CLIPFORMAT
*m_formats
; // formats we can provide data in
182 ULONG m_nCount
, // number of formats we support
183 m_nCurrent
; // current enum position
185 wxDECLARE_NO_COPY_CLASS(wxIEnumFORMATETC
);
188 // ----------------------------------------------------------------------------
189 // wxIDataObject implementation of IDataObject interface
190 // ----------------------------------------------------------------------------
192 class wxIDataObject
: public IDataObject
195 wxIDataObject(wxDataObject
*pDataObject
);
196 virtual ~wxIDataObject();
198 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
199 // is), but in some cases, the situation is reversed, that is we delete it
200 // when this object is deleted - setting this flag enables such logic
201 void SetDeleteFlag() { m_mustDelete
= true; }
204 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
205 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
206 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
207 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
208 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
209 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
210 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
211 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
212 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
214 DECLARE_IUNKNOWN_METHODS
;
217 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
221 wxDECLARE_NO_COPY_CLASS(wxIDataObject
);
223 // The following code is need to be able to store system data the operating
224 // system is using for it own purposes, e.g. drag images.
226 class SystemDataEntry
229 // Ctor takes ownership of the pointers.
230 SystemDataEntry(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
)
231 : pformatetc(pformatetc
), pmedium(pmedium
)
241 FORMATETC
*pformatetc
;
244 typedef wxVector
<SystemDataEntry
*> SystemData
;
246 // get system data specified by the given format
247 bool GetSystemData(wxDataFormat format
, STGMEDIUM
*) const;
249 // determines if the data object contains system data specified by the given format.
250 bool HasSystemData(wxDataFormat format
) const;
253 HRESULT
SaveSystemData(FORMATETC
*, STGMEDIUM
*, BOOL fRelease
);
255 // container for system data
256 SystemData m_systemData
;
260 wxIDataObject::GetSystemData(wxDataFormat format
, STGMEDIUM
*pmedium
) const
262 for ( SystemData::const_iterator it
= m_systemData
.begin();
263 it
!= m_systemData
.end();
266 FORMATETC
* formatEtc
= (*it
)->pformatetc
;
267 if ( formatEtc
->cfFormat
== format
)
269 wxCopyStgMedium((*it
)->pmedium
, pmedium
);
278 wxIDataObject::HasSystemData(wxDataFormat format
) const
280 for ( SystemData::const_iterator it
= m_systemData
.begin();
281 it
!= m_systemData
.end();
284 FORMATETC
* formatEtc
= (*it
)->pformatetc
;
285 if ( formatEtc
->cfFormat
== format
)
294 wxIDataObject::SaveSystemData(FORMATETC
*pformatetc
,
298 if ( pformatetc
== NULL
|| pmedium
== NULL
)
301 // remove entry if already available
302 for ( SystemData::iterator it
= m_systemData
.begin();
303 it
!= m_systemData
.end();
306 if ( pformatetc
->tymed
& (*it
)->pformatetc
->tymed
&&
307 pformatetc
->dwAspect
== (*it
)->pformatetc
->dwAspect
&&
308 pformatetc
->cfFormat
== (*it
)->pformatetc
->cfFormat
)
311 m_systemData
.erase(it
);
316 // create new format/medium
317 FORMATETC
* pnewformatEtc
= new FORMATETC
;
318 STGMEDIUM
* pnewmedium
= new STGMEDIUM
;
320 wxZeroMemory(*pnewformatEtc
);
321 wxZeroMemory(*pnewmedium
);
324 *pnewformatEtc
= *pformatetc
;
326 // copy or take ownerschip of medium
328 *pnewmedium
= *pmedium
;
330 wxCopyStgMedium(pmedium
, pnewmedium
);
333 m_systemData
.push_back(new SystemDataEntry(pnewformatEtc
, pnewmedium
));
338 // ============================================================================
340 // ============================================================================
342 // ----------------------------------------------------------------------------
344 // ----------------------------------------------------------------------------
346 void wxDataFormat::SetId(const wxString
& format
)
348 m_format
= (wxDataFormat::NativeFormat
)::RegisterClipboardFormat(format
.t_str());
351 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
355 wxString
wxDataFormat::GetId() const
357 static const int max
= 256;
361 wxCHECK_MSG( !IsStandard(), s
,
362 wxT("name of predefined format cannot be retrieved") );
364 int len
= ::GetClipboardFormatName(m_format
, wxStringBuffer(s
, max
), max
);
368 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
369 return wxEmptyString
;
375 // ----------------------------------------------------------------------------
377 // ----------------------------------------------------------------------------
379 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
381 ADD_IID(EnumFORMATETC
)
384 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
386 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
390 m_formats
= new CLIPFORMAT
[nCount
];
391 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
392 if (formats
[n
].GetFormatId() != wxDF_HTML
)
393 m_formats
[n
] = formats
[n
].GetFormatId();
395 m_formats
[n
] = ::RegisterClipboardFormat(wxT("HTML Format"));
399 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
403 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
405 ULONG numFetched
= 0;
406 while (m_nCurrent
< m_nCount
&& numFetched
< celt
) {
408 format
.cfFormat
= m_formats
[m_nCurrent
++];
410 format
.dwAspect
= DVASPECT_CONTENT
;
412 format
.tymed
= TYMED_HGLOBAL
;
419 *pceltFetched
= numFetched
;
421 return numFetched
== celt
? S_OK
: S_FALSE
;
424 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
426 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
429 if ( m_nCurrent
< m_nCount
)
432 // no, can't skip this many elements
438 STDMETHODIMP
wxIEnumFORMATETC::Reset()
440 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
447 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
449 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
451 // unfortunately, we can't reuse the code in ctor - types are different
452 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
453 pNew
->m_nCount
= m_nCount
;
454 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
455 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
456 pNew
->m_formats
[n
] = m_formats
[n
];
464 // ----------------------------------------------------------------------------
466 // ----------------------------------------------------------------------------
468 BEGIN_IID_TABLE(wxIDataObject
)
473 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
475 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
477 m_pDataObject
= pDataObject
;
478 m_mustDelete
= false;
481 wxIDataObject::~wxIDataObject()
483 // delete system data
484 for ( SystemData::iterator it
= m_systemData
.begin();
485 it
!= m_systemData
.end();
493 delete m_pDataObject
;
497 // get data functions
498 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
500 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
502 // is data is in our format?
503 HRESULT hr
= QueryGetData(pformatetcIn
);
507 // for the bitmaps and metafiles we use the handles instead of global memory
509 wxDataFormat format
= (wxDataFormat::NativeFormat
)pformatetcIn
->cfFormat
;
510 format
= HtmlFormatFixup(format
);
512 // is this system data?
513 if ( GetSystemData(format
, pmedium
) )
515 // pmedium is already filled with corresponding data, so we're ready.
522 pmedium
->tymed
= TYMED_GDI
;
525 case wxDF_ENHMETAFILE
:
526 pmedium
->tymed
= TYMED_ENHMF
;
531 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
532 sizeof(METAFILEPICT
));
533 if ( !pmedium
->hGlobal
) {
534 wxLogLastError(wxT("GlobalAlloc"));
535 return E_OUTOFMEMORY
;
537 pmedium
->tymed
= TYMED_MFPICT
;
542 size_t size
= m_pDataObject
->GetDataSize(format
);
544 // it probably means that the method is just not implemented
545 wxLogDebug(wxT("Invalid data size - can't be 0"));
547 return DV_E_FORMATETC
;
550 // we may need extra space for the buffer size
551 size
+= m_pDataObject
->GetBufferOffset( format
);
553 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
554 if ( hGlobal
== NULL
) {
555 wxLogLastError(wxT("GlobalAlloc"));
556 return E_OUTOFMEMORY
;
560 pmedium
->tymed
= TYMED_HGLOBAL
;
561 pmedium
->hGlobal
= hGlobal
;
564 pmedium
->pUnkForRelease
= NULL
;
567 hr
= GetDataHere(pformatetcIn
, pmedium
);
569 // free resources we allocated
570 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
571 GlobalFree(pmedium
->hGlobal
);
580 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
583 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
585 // put data in caller provided medium
586 switch ( pmedium
->tymed
)
589 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
594 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
595 &pmedium
->hEnhMetaFile
) )
600 // fall through - we pass METAFILEPICT through HGLOBAL
605 HGLOBAL hGlobal
= pmedium
->hGlobal
;
606 void *pBuf
= GlobalLock(hGlobal
);
607 if ( pBuf
== NULL
) {
608 wxLogLastError(wxT("GlobalLock"));
609 return E_OUTOFMEMORY
;
612 wxDataFormat format
= pformatetc
->cfFormat
;
614 // possibly put the size in the beginning of the buffer
615 pBuf
= m_pDataObject
->SetSizeInBuffer
618 ::GlobalSize(hGlobal
),
622 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
625 GlobalUnlock(hGlobal
);
637 // set data functions
638 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
642 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
644 switch ( pmedium
->tymed
)
647 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
651 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
655 // check if this format is supported
656 if ( !m_pDataObject
->IsSupported(pformatetc
->cfFormat
,
659 // As this is not a supported format (content data), assume it
660 // is system data and save it.
661 return SaveSystemData(pformatetc
, pmedium
, fRelease
);
666 // fall through - we pass METAFILEPICT through HGLOBAL
669 wxDataFormat format
= pformatetc
->cfFormat
;
671 format
= HtmlFormatFixup(format
);
673 // check if this format is supported
674 if ( !m_pDataObject
->IsSupported(format
, wxDataObject::Set
) ) {
675 // As above, assume that unsupported format must be system
676 // data and just save it.
677 return SaveSystemData(pformatetc
, pmedium
, fRelease
);
681 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
682 if ( pBuf
== NULL
) {
683 wxLogLastError(wxT("GlobalLock"));
685 return E_OUTOFMEMORY
;
688 // we've got a problem with SetData() here because the base
689 // class version requires the size parameter which we don't
690 // have anywhere in OLE data transfer - so we need to
691 // synthetise it for known formats and we suppose that all data
692 // in custom formats starts with a DWORD containing the size
699 size
= strlen((const char *)pBuf
);
701 #if !(defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
703 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) )
704 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
706 size
= wxWcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
713 // these formats don't use size at all, anyhow (but
714 // pass data by handle, which is always a single DWORD)
720 // the handler will calculate size itself (it's too
721 // complicated to do it here)
726 case CF_METAFILEPICT
:
727 size
= sizeof(METAFILEPICT
);
731 pBuf
= m_pDataObject
->
732 GetSizeFromBuffer(pBuf
, &size
, format
);
733 size
-= m_pDataObject
->GetBufferOffset(format
);
736 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
738 GlobalUnlock(pmedium
->hGlobal
);
751 // we own the medium, so we must release it - but do *not* free any
752 // data we pass by handle because we have copied it elsewhere
753 switch ( pmedium
->tymed
)
756 pmedium
->hBitmap
= 0;
760 pmedium
->hMetaFilePict
= 0;
764 pmedium
->hEnhMetaFile
= 0;
768 ReleaseStgMedium(pmedium
);
774 // information functions
775 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
777 // do we accept data in this format?
778 if ( pformatetc
== NULL
) {
779 wxLogTrace(wxTRACE_OleCalls
,
780 wxT("wxIDataObject::QueryGetData: invalid ptr."));
785 // the only one allowed by current COM implementation
786 if ( pformatetc
->lindex
!= -1 ) {
787 wxLogTrace(wxTRACE_OleCalls
,
788 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
794 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
795 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
796 wxLogTrace(wxTRACE_OleCalls
,
797 wxT("wxIDataObject::QueryGetData: bad dwAspect %ld"),
798 pformatetc
->dwAspect
);
800 return DV_E_DVASPECT
;
803 // and now check the type of data requested
804 wxDataFormat format
= pformatetc
->cfFormat
;
805 format
= HtmlFormatFixup(format
);
807 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
808 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
809 wxGetFormatName(format
));
811 else if ( HasSystemData(format
) )
813 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok (system data)"),
814 wxGetFormatName(format
));
815 // this is system data, so no further checks needed.
819 wxLogTrace(wxTRACE_OleCalls
,
820 wxT("wxIDataObject::QueryGetData: %s unsupported"),
821 wxGetFormatName(format
));
823 return DV_E_FORMATETC
;
826 // we only transfer data by global memory, except for some particular cases
827 DWORD tymed
= pformatetc
->tymed
;
828 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
829 !(tymed
& TYMED_HGLOBAL
) ) {
830 // it's not what we're waiting for
831 wxLogTrace(wxTRACE_OleCalls
,
832 wxT("wxIDataObject::QueryGetData: %s != %s"),
834 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
843 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
844 FORMATETC
*pFormatetcOut
)
846 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
848 // TODO we might want something better than this trivial implementation here
849 if ( pFormatetcOut
!= NULL
)
850 pFormatetcOut
->ptd
= NULL
;
852 return DATA_S_SAMEFORMATETC
;
855 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
856 IEnumFORMATETC
**ppenumFormatEtc
)
858 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
860 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
863 // format count is total of user specified and system formats.
864 const size_t ourFormatCount
= m_pDataObject
->GetFormatCount(dir
);
865 const size_t sysFormatCount
= m_systemData
.size();
868 nFormatCount
= wx_truncate_cast(ULONG
, ourFormatCount
+ sysFormatCount
);
870 // fill format array with formats ...
871 wxScopedArray
<wxDataFormat
> formats(new wxDataFormat
[nFormatCount
]);
873 // ... from content data (supported formats)
874 m_pDataObject
->GetAllFormats(formats
.get(), dir
);
876 // ... from system data
877 for ( size_t j
= 0; j
< sysFormatCount
; j
++ )
879 SystemDataEntry
* entry
= m_systemData
[j
];
880 wxDataFormat
& format
= formats
[ourFormatCount
+ j
];
881 format
= entry
->pformatetc
->cfFormat
;
884 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
.get(), nFormatCount
);
886 *ppenumFormatEtc
= pEnum
;
891 // ----------------------------------------------------------------------------
892 // advise sink functions (not implemented)
893 // ----------------------------------------------------------------------------
895 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
896 DWORD
WXUNUSED(advf
),
897 IAdviseSink
*WXUNUSED(pAdvSink
),
898 DWORD
*WXUNUSED(pdwConnection
))
900 return OLE_E_ADVISENOTSUPPORTED
;
903 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
905 return OLE_E_ADVISENOTSUPPORTED
;
908 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
910 return OLE_E_ADVISENOTSUPPORTED
;
913 // ----------------------------------------------------------------------------
915 // ----------------------------------------------------------------------------
917 wxDataObject::wxDataObject()
919 m_pIDataObject
= new wxIDataObject(this);
920 m_pIDataObject
->AddRef();
923 wxDataObject::~wxDataObject()
925 ReleaseInterface(m_pIDataObject
);
928 void wxDataObject::SetAutoDelete()
930 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
931 m_pIDataObject
->Release();
933 // so that the dtor doesn't crash
934 m_pIDataObject
= NULL
;
937 size_t wxDataObject::GetBufferOffset(const wxDataFormat
& format
)
939 // if we prepend the size of the data to the buffer itself, account for it
940 return NeedsVerbatimData(format
) ? 0 : sizeof(size_t);
943 const void *wxDataObject::GetSizeFromBuffer(const void *buffer
,
945 const wxDataFormat
& WXUNUSED(format
))
947 // hack: the third parameter is declared non-const in Wine's headers so
948 // cast away the const
949 const size_t realsz
= ::HeapSize(::GetProcessHeap(), 0,
950 const_cast<void*>(buffer
));
951 if ( realsz
== (size_t)-1 )
953 // note that HeapSize() does not set last error
954 wxLogApiError(wxT("HeapSize"), 0);
963 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
964 const wxDataFormat
& format
)
966 size_t* p
= (size_t *)buffer
;
967 if ( !NeedsVerbatimData(format
) )
969 // prepend the size to the data and skip it
978 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
980 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
982 #pragma warning(disable:4063)
985 static wxChar s_szBuf
[256];
987 case CF_TEXT
: return wxT("CF_TEXT");
988 case CF_BITMAP
: return wxT("CF_BITMAP");
989 case CF_SYLK
: return wxT("CF_SYLK");
990 case CF_DIF
: return wxT("CF_DIF");
991 case CF_TIFF
: return wxT("CF_TIFF");
992 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
993 case CF_DIB
: return wxT("CF_DIB");
994 case CF_PALETTE
: return wxT("CF_PALETTE");
995 case CF_PENDATA
: return wxT("CF_PENDATA");
996 case CF_RIFF
: return wxT("CF_RIFF");
997 case CF_WAVE
: return wxT("CF_WAVE");
998 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
1000 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
1001 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
1002 case CF_LOCALE
: return wxT("CF_LOCALE");
1003 case CF_HDROP
: return wxT("CF_HDROP");
1007 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
1009 // it must be a new predefined format we don't know the name of
1010 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
1017 #pragma warning(default:4063)
1021 #endif // wxDEBUG_LEVEL
1023 // ----------------------------------------------------------------------------
1024 // wxBitmapDataObject supports CF_DIB format
1025 // ----------------------------------------------------------------------------
1027 // TODO: support CF_DIB under Windows CE as well
1029 size_t wxBitmapDataObject::GetDataSize() const
1031 #if wxUSE_WXDIB && !defined(__WXWINCE__)
1032 return wxDIB::ConvertFromBitmap(NULL
, GetHbitmapOf(GetBitmap()));
1038 bool wxBitmapDataObject::GetDataHere(void *buf
) const
1040 #if wxUSE_WXDIB && !defined(__WXWINCE__)
1041 BITMAPINFO
* const pbi
= (BITMAPINFO
*)buf
;
1043 return wxDIB::ConvertFromBitmap(pbi
, GetHbitmapOf(GetBitmap())) != 0;
1050 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
1052 #if wxUSE_WXDIB && !defined(__WXWINCE__)
1053 const BITMAPINFO
* const pbmi
= (const BITMAPINFO
*)buf
;
1055 HBITMAP hbmp
= wxDIB::ConvertToBitmap(pbmi
);
1057 wxCHECK_MSG( hbmp
, FALSE
, wxT("pasting/dropping invalid bitmap") );
1059 const BITMAPINFOHEADER
* const pbmih
= &pbmi
->bmiHeader
;
1060 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1061 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1063 // TODO: create wxPalette if the bitmap has any
1074 // ----------------------------------------------------------------------------
1075 // wxBitmapDataObject2 supports CF_BITMAP format
1076 // ----------------------------------------------------------------------------
1078 // the bitmaps aren't passed by value as other types of data (i.e. by copying
1079 // the data into a global memory chunk and passing it to the clipboard or
1080 // another application or whatever), but by handle, so these generic functions
1081 // don't make much sense to them.
1083 size_t wxBitmapDataObject2::GetDataSize() const
1088 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
1090 // we put a bitmap handle into pBuf
1091 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
1096 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
1098 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
1101 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
1103 wxLogLastError(wxT("GetObject(HBITMAP)"));
1106 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
1107 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1109 if ( !bitmap
.IsOk() ) {
1110 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
1122 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
1124 if ( format
.GetFormatId() == CF_DIB
)
1129 // shouldn't be selected into a DC or GetDIBits() would fail
1130 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
1131 wxT("can't copy bitmap selected into wxMemoryDC") );
1133 // first get the info
1135 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
1136 NULL
, &bi
, DIB_RGB_COLORS
) )
1138 wxLogLastError(wxT("GetDIBits(NULL)"));
1143 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
1147 // no data to copy - we don't pass HBITMAP via global memory
1152 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
1155 wxASSERT_MSG( m_bitmap
.IsOk(), wxT("copying invalid bitmap") );
1157 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
1158 if ( format
.GetFormatId() == CF_DIB
)
1163 // shouldn't be selected into a DC or GetDIBits() would fail
1164 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
1165 wxT("can't copy bitmap selected into wxMemoryDC") );
1167 // first get the info
1168 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
1169 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
1171 wxLogLastError(wxT("GetDIBits(NULL)"));
1176 // and now copy the bits
1177 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
1178 pbi
, DIB_RGB_COLORS
) )
1180 wxLogLastError(wxT("GetDIBits"));
1187 // we put a bitmap handle into pBuf
1188 *(HBITMAP
*)pBuf
= hbmp
;
1194 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
1195 size_t size
, const void *pBuf
)
1198 if ( format
.GetFormatId() == CF_DIB
)
1200 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1201 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1204 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
1205 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1206 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1207 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
1210 wxLogLastError(wxT("CreateDIBitmap"));
1213 m_bitmap
.SetWidth(pbmih
->biWidth
);
1214 m_bitmap
.SetHeight(pbmih
->biHeight
);
1218 // it's easy with bitmaps: we pass them by handle
1219 hbmp
= *(HBITMAP
*)pBuf
;
1222 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
1224 wxLogLastError(wxT("GetObject(HBITMAP)"));
1227 m_bitmap
.SetWidth(bmp
.bmWidth
);
1228 m_bitmap
.SetHeight(bmp
.bmHeight
);
1229 m_bitmap
.SetDepth(bmp
.bmPlanes
);
1232 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1234 wxASSERT_MSG( m_bitmap
.IsOk(), wxT("pasting invalid bitmap") );
1241 // ----------------------------------------------------------------------------
1243 // ----------------------------------------------------------------------------
1245 bool wxFileDataObject::SetData(size_t WXUNUSED(size
),
1246 const void *WXUNUSED_IN_WINCE(pData
))
1249 m_filenames
.Empty();
1251 // the documentation states that the first member of DROPFILES structure is
1252 // a "DWORD offset of double NUL terminated file list". What they mean by
1253 // this (I wonder if you see it immediately) is that the list starts at
1254 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
1255 // to use DragQueryFile to work with this structure, but not told where and
1256 // how to get HDROP.
1257 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
1259 // get number of files (magic value -1)
1260 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
1262 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
1264 // for each file get the length, allocate memory and then get the name
1267 for ( n
= 0; n
< nFiles
; n
++ ) {
1268 // +1 for terminating NUL
1269 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
1271 UINT len2
= ::DragQueryFile(hdrop
, n
, wxStringBuffer(str
, len
), len
);
1272 m_filenames
.Add(str
);
1274 if ( len2
!= len
- 1 ) {
1275 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
1276 %d characters, %d expected."), len2
, len
- 1);
1286 void wxFileDataObject::AddFile(const wxString
& file
)
1288 // just add file to filenames array
1289 // all useful data (such as DROPFILES struct) will be
1290 // created later as necessary
1291 m_filenames
.Add(file
);
1294 size_t wxFileDataObject::GetDataSize() const
1297 // size returned will be the size of the DROPFILES structure, plus the list
1298 // of filesnames (null byte separated), plus a double null at the end
1300 // if no filenames in list, size is 0
1301 if ( m_filenames
.empty() )
1304 #if wxUSE_UNICODE_MSLU
1306 if ( wxGetOsVersion() == wxOS_WINDOWS_9X
)
1308 // Win9x always uses ANSI file names and MSLU doesn't help with this
1313 sizeOfChar
= sizeof(wxChar
);
1315 #else // !wxUSE_UNICODE_MSLU
1316 static const size_t sizeOfChar
= sizeof(wxChar
);
1317 #endif // wxUSE_UNICODE_MSLU/!wxUSE_UNICODE_MSLU
1319 // initial size of DROPFILES struct + null byte
1320 size_t sz
= sizeof(DROPFILES
) + sizeOfChar
;
1322 const size_t count
= m_filenames
.size();
1323 for ( size_t i
= 0; i
< count
; i
++ )
1325 // add filename length plus null byte
1327 #if wxUSE_UNICODE_MSLU
1328 if ( sizeOfChar
== 1 )
1329 len
= strlen(m_filenames
[i
].mb_str(*wxConvFileName
));
1331 #endif // wxUSE_UNICODE_MSLU
1332 len
= m_filenames
[i
].length();
1334 sz
+= (len
+ 1) * sizeOfChar
;
1343 bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData
)) const
1346 // pData points to an externally allocated memory block
1347 // created using the size returned by GetDataSize()
1349 // if pData is NULL, or there are no files, return
1350 if ( !pData
|| m_filenames
.empty() )
1353 // convert data pointer to a DROPFILES struct pointer
1354 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1356 // initialize DROPFILES struct
1357 pDrop
->pFiles
= sizeof(DROPFILES
);
1358 pDrop
->fNC
= FALSE
; // not non-client coords
1359 #if wxUSE_UNICODE_MSLU
1360 pDrop
->fWide
= wxGetOsVersion() != wxOS_WINDOWS_9X
? TRUE
: FALSE
;
1362 pDrop
->fWide
= wxUSE_UNICODE
;
1365 const size_t sizeOfChar
= pDrop
->fWide
? sizeof(wchar_t) : 1;
1367 // set start of filenames list (null separated)
1368 BYTE
*pbuf
= (BYTE
*)(pDrop
+ 1);
1370 const size_t count
= m_filenames
.size();
1371 for ( size_t i
= 0; i
< count
; i
++ )
1373 // copy filename to pbuf and add null terminator
1375 #if wxUSE_UNICODE_MSLU
1376 if ( sizeOfChar
== 1 )
1378 wxCharBuffer
buf(m_filenames
[i
].mb_str(*wxConvFileName
));
1380 memcpy(pbuf
, buf
, len
*sizeOfChar
);
1383 #endif // wxUSE_UNICODE_MSLU
1385 len
= m_filenames
[i
].length();
1386 memcpy(pbuf
, m_filenames
[i
].t_str(), len
*sizeOfChar
);
1389 pbuf
+= len
*sizeOfChar
;
1391 memset(pbuf
, 0, sizeOfChar
);
1395 // add final null terminator
1396 memset(pbuf
, 0, sizeOfChar
);
1404 // ----------------------------------------------------------------------------
1406 // ----------------------------------------------------------------------------
1408 // Work around bug in Wine headers
1409 #if defined(__WINE__) && defined(CFSTR_SHELLURL) && wxUSE_UNICODE
1410 #undef CFSTR_SHELLURL
1411 #define CFSTR_SHELLURL wxT("CFSTR_SHELLURL")
1414 class CFSTR_SHELLURLDataObject
: public wxCustomDataObject
1417 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1419 virtual size_t GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
1424 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1425 const wxDataFormat
& WXUNUSED(format
) )
1427 // CFSTR_SHELLURL is _always_ ANSI text
1428 *size
= strlen( (const char*)buffer
);
1433 virtual void* SetSizeInBuffer( void* buffer
, size_t WXUNUSED(size
),
1434 const wxDataFormat
& WXUNUSED(format
) )
1439 wxDECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject
);
1444 wxURLDataObject::wxURLDataObject(const wxString
& url
)
1446 // we support CF_TEXT and CFSTR_SHELLURL formats which are basically the
1447 // same but it seems that some browsers only provide one of them so we have
1449 Add(new wxTextDataObject
);
1450 Add(new CFSTR_SHELLURLDataObject());
1452 // we don't have any data yet
1453 m_dataObjectLast
= NULL
;
1459 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1463 m_dataObjectLast
= GetObject(format
);
1465 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1466 wxT("unsupported format in wxURLDataObject"));
1468 return m_dataObjectLast
->SetData(len
, buf
);
1471 wxString
wxURLDataObject::GetURL() const
1474 wxCHECK_MSG( m_dataObjectLast
, url
, wxT("no data in wxURLDataObject") );
1476 if ( m_dataObjectLast
->GetPreferredFormat() == CFSTR_SHELLURL
)
1478 const size_t len
= m_dataObjectLast
->GetDataSize();
1482 // CFSTR_SHELLURL is always ANSI so we need to convert it from it in
1485 wxCharBuffer
buf(len
);
1487 if ( m_dataObjectLast
->GetDataHere(buf
.data()) )
1489 #else // !wxUSE_UNICODE
1490 // in ANSI build no conversion is necessary
1491 m_dataObjectLast
->GetDataHere(wxStringBuffer(url
, len
));
1492 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1494 else // must be wxTextDataObject
1496 url
= static_cast<wxTextDataObject
*>(m_dataObjectLast
)->GetText();
1502 void wxURLDataObject::SetURL(const wxString
& url
)
1504 wxCharBuffer
urlMB(url
.mb_str());
1507 const size_t len
= strlen(urlMB
);
1510 // wxTextDataObject takes the number of characters in the string, not
1511 // the size of the buffer (which would be len+1)
1512 SetData(wxDF_TEXT
, len
, urlMB
);
1513 #endif // !wxUSE_UNICODE
1515 // however CFSTR_SHELLURLDataObject doesn't append NUL automatically
1516 // but we probably still want to have it on the clipboard (just to be
1517 // safe), so do append it
1518 SetData(wxDataFormat(CFSTR_SHELLURL
), len
+ 1, urlMB
);
1522 SetData(wxDF_UNICODETEXT
, url
.length()*sizeof(wxChar
), url
.wc_str());
1526 // ----------------------------------------------------------------------------
1527 // private functions
1528 // ----------------------------------------------------------------------------
1532 static const wxChar
*GetTymedName(DWORD tymed
)
1534 static wxChar s_szBuf
[128];
1536 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1537 case TYMED_FILE
: return wxT("TYMED_FILE");
1538 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1539 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1540 case TYMED_GDI
: return wxT("TYMED_GDI");
1541 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1542 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1544 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1551 #else // not using OLE at all
1553 // ----------------------------------------------------------------------------
1555 // ----------------------------------------------------------------------------
1559 wxDataObject::wxDataObject()
1563 wxDataObject::~wxDataObject()
1567 void wxDataObject::SetAutoDelete()
1571 const wxChar
*wxDataObject::GetFormatName(wxDataFormat
WXUNUSED(format
))
1576 #endif // wxUSE_DATAOBJ
1578 #endif // wxUSE_OLE/!wxUSE_OLE