]>
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__)
33 #if defined(__WIN32__) && !defined(__GNUWIN32__)
36 #include <wx/msw/ole/dataobj.h>
46 #include <wx/msw/ole/oleutils.h>
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 static const char *GetTymedName(DWORD tymed
);
54 // ----------------------------------------------------------------------------
55 // wxIEnumFORMATETC interface implementation
56 // ----------------------------------------------------------------------------
57 class wxIEnumFORMATETC
: public IEnumFORMATETC
60 wxIEnumFORMATETC(CLIPFORMAT cf
);
62 DECLARE_IUNKNOWN_METHODS
;
65 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
66 STDMETHODIMP
Skip(ULONG celt
);
68 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
71 FORMATETC m_format
; // (unique @@@) format we can provide data in
72 ULONG m_nCurrent
; // current enum position (currently either 0 or 1)
75 // ----------------------------------------------------------------------------
76 // wxIDataObject implementation of IDataObject interface
77 // ----------------------------------------------------------------------------
78 class wxIDataObject
: public IDataObject
81 wxIDataObject(wxDataObject
*pDataObject
);
83 DECLARE_IUNKNOWN_METHODS
;
86 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
87 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
88 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
89 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
90 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
91 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
92 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
93 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
94 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
97 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
110 ADD_IID(EnumFORMATETC
)
113 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
115 wxIEnumFORMATETC::wxIEnumFORMATETC(CLIPFORMAT cf
)
117 m_format
.cfFormat
= cf
;
119 m_format
.dwAspect
= DVASPECT_CONTENT
;
120 m_format
.lindex
= -1;
121 m_format
.tymed
= TYMED_HGLOBAL
;
126 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
130 wxLogTrace("wxIEnumFORMATETC::Next");
135 if ( m_nCurrent
== 0 ) {
145 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
147 wxLogTrace("wxIEnumFORMATETC::Skip");
149 if ( m_nCurrent
== 0 )
155 STDMETHODIMP
wxIEnumFORMATETC::Reset()
157 wxLogTrace("wxIEnumFORMATETC::Reset");
164 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
166 wxLogTrace("wxIEnumFORMATETC::Clone");
168 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(m_format
.cfFormat
);
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 BEGIN_IID_TABLE(wxIDataObject
)
184 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
186 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
189 m_pDataObject
= pDataObject
;
192 // get data functions
193 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
195 wxLogTrace("wxIDataObject::GetData");
197 // is data is in our format?
198 HRESULT hr
= QueryGetData(pformatetcIn
);
203 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
204 m_pDataObject
->GetDataSize());
205 if ( hGlobal
== NULL
) {
206 wxLogLastError("GlobalAlloc");
207 return E_OUTOFMEMORY
;
211 pmedium
->tymed
= TYMED_HGLOBAL
;
212 pmedium
->hGlobal
= hGlobal
;
213 pmedium
->pUnkForRelease
= NULL
;
215 hr
= GetDataHere(pformatetcIn
, pmedium
);
224 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
227 wxLogTrace("wxIDataObject::GetDataHere");
229 // put data in caller provided medium
230 if ( pmedium
->tymed
!= TYMED_HGLOBAL
)
234 void *pBuf
= GlobalLock(pmedium
->hGlobal
);
235 if ( pBuf
== NULL
) {
236 wxLogLastError("GlobalLock");
237 return E_OUTOFMEMORY
;
240 m_pDataObject
->GetDataHere(pBuf
);
242 GlobalUnlock(pmedium
->hGlobal
);
247 // set data functions (not implemented)
248 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
252 wxLogTrace("wxIDataObject::SetData");
256 // information functions
257 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
259 // do we accept data in this format?
260 if ( pformatetc
== NULL
) {
261 wxLogTrace("wxIDataObject::QueryGetData: invalid ptr.");
265 // the only one allowed by current COM implementation
266 if ( pformatetc
->lindex
!= -1 ) {
267 wxLogTrace("wxIDataObject::QueryGetData: bad lindex %d",
272 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
273 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
274 wxLogTrace("wxIDataObject::QueryGetData: bad dwAspect %d",
275 pformatetc
->dwAspect
);
276 return DV_E_DVASPECT
;
279 // @@ we only transfer data by global memory (bad for large amounts of it!)
280 if ( !(pformatetc
->tymed
& TYMED_HGLOBAL
) ) {
281 wxLogTrace("wxIDataObject::QueryGetData: %s != TYMED_HGLOBAL.",
282 GetTymedName(pformatetc
->tymed
));
286 // and now check the type of data requested
287 if ( m_pDataObject
->IsSupportedFormat((wxDataFormat
) pformatetc
->cfFormat
) ) {
288 wxLogTrace("wxIDataObject::QueryGetData: %s ok",
289 wxDataObject::GetFormatName((wxDataFormat
) pformatetc
->cfFormat
));
293 wxLogTrace("wxIDataObject::QueryGetData: %s unsupported",
294 wxDataObject::GetFormatName((wxDataFormat
) pformatetc
->cfFormat
));
295 return DV_E_FORMATETC
;
299 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
300 FORMATETC
*pFormatetcOut
)
302 wxLogTrace("wxIDataObject::GetCanonicalFormatEtc");
304 // @@ implementation is trivial, we might want something better here
305 if ( pFormatetcOut
!= NULL
)
306 pFormatetcOut
->ptd
= NULL
;
307 return DATA_S_SAMEFORMATETC
;
310 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDirection
,
311 IEnumFORMATETC
**ppenumFormatEtc
)
313 wxLogTrace("wxIDataObject::EnumFormatEtc");
315 if ( dwDirection
== DATADIR_SET
) {
316 // we don't allow setting of data anyhow
320 wxIEnumFORMATETC
*pEnum
=
321 new wxIEnumFORMATETC(m_pDataObject
->GetPreferredFormat());
323 *ppenumFormatEtc
= pEnum
;
328 // advise sink functions (not implemented)
329 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
331 IAdviseSink
*pAdvSink
,
332 DWORD
*pdwConnection
)
334 return OLE_E_ADVISENOTSUPPORTED
;
337 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
339 return OLE_E_ADVISENOTSUPPORTED
;
342 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
344 return OLE_E_ADVISENOTSUPPORTED
;
347 // ----------------------------------------------------------------------------
349 // ----------------------------------------------------------------------------
351 wxDataObject::wxDataObject()
353 m_pIDataObject
= new wxIDataObject(this);
354 m_pIDataObject
->AddRef();
357 wxDataObject::~wxDataObject()
359 m_pIDataObject
->Release();
362 const char *wxDataObject::GetFormatName(wxDataFormat format
)
365 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
367 #pragma warning(disable:4063)
370 static char s_szBuf
[128];
372 case CF_TEXT
: return "CF_TEXT";
373 case CF_BITMAP
: return "CF_BITMAP";
374 case CF_METAFILEPICT
: return "CF_METAFILEPICT";
375 case CF_SYLK
: return "CF_SYLK";
376 case CF_DIF
: return "CF_DIF";
377 case CF_TIFF
: return "CF_TIFF";
378 case CF_OEMTEXT
: return "CF_OEMTEXT";
379 case CF_DIB
: return "CF_DIB";
380 case CF_PALETTE
: return "CF_PALETTE";
381 case CF_PENDATA
: return "CF_PENDATA";
382 case CF_RIFF
: return "CF_RIFF";
383 case CF_WAVE
: return "CF_WAVE";
384 case CF_UNICODETEXT
: return "CF_UNICODETEXT";
385 case CF_ENHMETAFILE
: return "CF_ENHMETAFILE";
386 case CF_HDROP
: return "CF_HDROP";
387 case CF_LOCALE
: return "CF_LOCALE";
389 sprintf(s_szBuf
, "clipboard format %d (unknown)", format
);
394 #pragma warning(default:4063)
402 // ----------------------------------------------------------------------------
404 // ----------------------------------------------------------------------------
405 static const char *GetTymedName(DWORD tymed
)
407 static char s_szBuf
[128];
409 case TYMED_HGLOBAL
: return "TYMED_HGLOBAL";
410 case TYMED_FILE
: return "TYMED_FILE";
411 case TYMED_ISTREAM
: return "TYMED_ISTREAM";
412 case TYMED_ISTORAGE
: return "TYMED_ISTORAGE";
413 case TYMED_GDI
: return "TYMED_GDI";
414 case TYMED_MFPICT
: return "TYMED_MFPICT";
415 case TYMED_ENHMF
: return "TYMED_ENHMF";
417 sprintf(s_szBuf
, "type of media format %d (unknown)", tymed
);
422 // TODO: OLE parts of wxBitmap/File/MetafileDataObject