]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dataobj.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
32 #include <wx/msw/ole/oleutils.h>
33 #include <wx/msw/ole/dataobj.h>
40 // ----------------------------------------------------------------------------
42 // ----------------------------------------------------------------------------
44 static const char *GetTymedName(DWORD tymed
);
46 // ----------------------------------------------------------------------------
47 // wxIEnumFORMATETC interface implementation
48 // ----------------------------------------------------------------------------
49 class wxIEnumFORMATETC
: public IEnumFORMATETC
52 wxIEnumFORMATETC(CLIPFORMAT cf
);
54 DECLARE_IUNKNOWN_METHODS
;
57 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
58 STDMETHODIMP
Skip(ULONG celt
);
60 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
63 FORMATETC m_format
; // (unique @@@) format we can provide data in
64 ULONG m_nCurrent
; // current enum position (currently either 0 or 1)
67 // ----------------------------------------------------------------------------
68 // wxIDataObject implementation of IDataObject interface
69 // ----------------------------------------------------------------------------
70 class wxIDataObject
: public IDataObject
73 wxIDataObject(wxDataObject
*pDataObject
);
75 DECLARE_IUNKNOWN_METHODS
;
78 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
79 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
80 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
81 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
82 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
83 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
84 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
85 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
86 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
89 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
92 // ============================================================================
94 // ============================================================================
96 // ----------------------------------------------------------------------------
98 // ----------------------------------------------------------------------------
100 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
102 ADD_IID(EnumFORMATETC
)
105 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
107 wxIEnumFORMATETC::wxIEnumFORMATETC(CLIPFORMAT cf
)
109 m_format
.cfFormat
= cf
;
111 m_format
.dwAspect
= DVASPECT_CONTENT
;
112 m_format
.lindex
= -1;
113 m_format
.tymed
= TYMED_HGLOBAL
;
118 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
122 wxLogTrace("wxIEnumFORMATETC::Next");
127 if ( m_nCurrent
== 0 ) {
137 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
139 wxLogTrace("wxIEnumFORMATETC::Skip");
141 if ( m_nCurrent
== 0 )
147 STDMETHODIMP
wxIEnumFORMATETC::Reset()
149 wxLogTrace("wxIEnumFORMATETC::Reset");
156 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
158 wxLogTrace("wxIEnumFORMATETC::Clone");
160 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(m_format
.cfFormat
);
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 BEGIN_IID_TABLE(wxIDataObject
)
176 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
178 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
181 m_pDataObject
= pDataObject
;
184 // get data functions
185 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
187 wxLogTrace("wxIDataObject::GetData");
189 // is data is in our format?
190 HRESULT hr
= QueryGetData(pformatetcIn
);
195 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
196 m_pDataObject
->GetDataSize());
197 if ( hGlobal
== NULL
) {
198 wxLogLastError("GlobalAlloc");
199 return E_OUTOFMEMORY
;
203 pmedium
->tymed
= TYMED_HGLOBAL
;
204 pmedium
->hGlobal
= hGlobal
;
205 pmedium
->pUnkForRelease
= NULL
;
207 hr
= GetDataHere(pformatetcIn
, pmedium
);
216 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
219 wxLogTrace("wxIDataObject::GetDataHere");
221 // put data in caller provided medium
222 if ( pmedium
->tymed
!= TYMED_HGLOBAL
)
226 void *pBuf
= GlobalLock(pmedium
->hGlobal
);
227 if ( pBuf
== NULL
) {
228 wxLogLastError("GlobalLock");
229 return E_OUTOFMEMORY
;
232 m_pDataObject
->GetDataHere(pBuf
);
234 GlobalUnlock(pmedium
->hGlobal
);
239 // set data functions (not implemented)
240 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
244 wxLogTrace("wxIDataObject::SetData");
248 // information functions
249 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
251 // do we accept data in this format?
252 if ( pformatetc
== NULL
) {
253 wxLogTrace("wxIDataObject::QueryGetData: invalid ptr.");
257 // the only one allowed by current COM implementation
258 if ( pformatetc
->lindex
!= -1 ) {
259 wxLogTrace("wxIDataObject::QueryGetData: bad lindex %d",
264 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
265 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
266 wxLogTrace("wxIDataObject::QueryGetData: bad dwAspect %d",
267 pformatetc
->dwAspect
);
268 return DV_E_DVASPECT
;
271 // @@ we only transfer data by global memory (bad for large amounts of it!)
272 if ( !(pformatetc
->tymed
& TYMED_HGLOBAL
) ) {
273 wxLogTrace("wxIDataObject::QueryGetData: %s != TYMED_HGLOBAL.",
274 GetTymedName(pformatetc
->tymed
));
278 // and now check the type of data requested
279 if ( m_pDataObject
->IsSupportedFormat(pformatetc
->cfFormat
) ) {
280 wxLogTrace("wxIDataObject::QueryGetData: %s ok",
281 wxDataObject::GetFormatName(pformatetc
->cfFormat
));
285 wxLogTrace("wxIDataObject::QueryGetData: %s unsupported",
286 wxDataObject::GetFormatName(pformatetc
->cfFormat
));
287 return DV_E_FORMATETC
;
291 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
292 FORMATETC
*pFormatetcOut
)
294 wxLogTrace("wxIDataObject::GetCanonicalFormatEtc");
296 // @@ implementation is trivial, we might want something better here
297 if ( pFormatetcOut
!= NULL
)
298 pFormatetcOut
->ptd
= NULL
;
299 return DATA_S_SAMEFORMATETC
;
302 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDirection
,
303 IEnumFORMATETC
**ppenumFormatEtc
)
305 wxLogTrace("wxIDataObject::EnumFormatEtc");
307 if ( dwDirection
== DATADIR_SET
) {
308 // we don't allow setting of data anyhow
312 wxIEnumFORMATETC
*pEnum
=
313 new wxIEnumFORMATETC(m_pDataObject
->GetPreferredFormat());
315 *ppenumFormatEtc
= pEnum
;
320 // advise sink functions (not implemented)
321 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
323 IAdviseSink
*pAdvSink
,
324 DWORD
*pdwConnection
)
326 return OLE_E_ADVISENOTSUPPORTED
;
329 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
331 return OLE_E_ADVISENOTSUPPORTED
;
334 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
336 return OLE_E_ADVISENOTSUPPORTED
;
339 // ----------------------------------------------------------------------------
341 // ----------------------------------------------------------------------------
343 wxDataObject::wxDataObject()
345 m_pIDataObject
= new wxIDataObject(this);
346 m_pIDataObject
->AddRef();
349 wxDataObject::~wxDataObject()
351 m_pIDataObject
->Release();
354 const char *wxDataObject::GetFormatName(wxDataFormat format
)
357 static char s_szBuf
[128];
359 case CF_TEXT
: return "CF_TEXT";
360 case CF_BITMAP
: return "CF_BITMAP";
361 case CF_METAFILEPICT
: return "CF_METAFILEPICT";
362 case CF_SYLK
: return "CF_SYLK";
363 case CF_DIF
: return "CF_DIF";
364 case CF_TIFF
: return "CF_TIFF";
365 case CF_OEMTEXT
: return "CF_OEMTEXT";
366 case CF_DIB
: return "CF_DIB";
367 case CF_PALETTE
: return "CF_PALETTE";
368 case CF_PENDATA
: return "CF_PENDATA";
369 case CF_RIFF
: return "CF_RIFF";
370 case CF_WAVE
: return "CF_WAVE";
371 case CF_UNICODETEXT
: return "CF_UNICODETEXT";
372 case CF_ENHMETAFILE
: return "CF_ENHMETAFILE";
373 case CF_HDROP
: return "CF_HDROP";
374 case CF_LOCALE
: return "CF_LOCALE";
376 sprintf(s_szBuf
, "clipboard format %d (unknown)", format
);
384 // ----------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------
387 static const char *GetTymedName(DWORD tymed
)
389 static char s_szBuf
[128];
391 case TYMED_HGLOBAL
: return "TYMED_HGLOBAL";
392 case TYMED_FILE
: return "TYMED_FILE";
393 case TYMED_ISTREAM
: return "TYMED_ISTREAM";
394 case TYMED_ISTORAGE
: return "TYMED_ISTORAGE";
395 case TYMED_GDI
: return "TYMED_GDI";
396 case TYMED_MFPICT
: return "TYMED_MFPICT";
397 case TYMED_ENHMF
: return "TYMED_ENHMF";
399 sprintf(s_szBuf
, "type of media format %d (unknown)", tymed
);