]>
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__)
35 #if defined(__WIN32__) && !defined(__GNUWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
38 #include "wx/dataobj.h"
41 #ifdef wxUSE_NORLANDER_HEADERS
51 #include "wx/msw/ole/oleutils.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
58 static const char *GetTymedName(DWORD tymed
);
61 // ----------------------------------------------------------------------------
62 // wxIEnumFORMATETC interface implementation
63 // ----------------------------------------------------------------------------
65 class wxIEnumFORMATETC
: public IEnumFORMATETC
68 wxIEnumFORMATETC(const wxDataFormat
* formats
, ULONG nCount
);
69 ~wxIEnumFORMATETC() { delete [] m_formats
; }
71 DECLARE_IUNKNOWN_METHODS
;
74 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
75 STDMETHODIMP
Skip(ULONG celt
);
77 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
80 CLIPFORMAT
*m_formats
; // formats we can provide data in
81 ULONG m_nCount
, // number of formats we support
82 m_nCurrent
; // current enum position
85 // ----------------------------------------------------------------------------
86 // wxIDataObject implementation of IDataObject interface
87 // ----------------------------------------------------------------------------
89 class wxIDataObject
: public IDataObject
92 wxIDataObject(wxDataObject
*pDataObject
);
94 DECLARE_IUNKNOWN_METHODS
;
97 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
98 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
99 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
100 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
101 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
102 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
103 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
104 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
105 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
108 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
111 // ============================================================================
113 // ============================================================================
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 void wxDataFormat::SetId(const wxChar
*format
)
121 m_format
= ::RegisterClipboardFormat(format
);
124 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
128 wxString
wxDataFormat::GetId() const
130 static const int max
= 256;
134 wxCHECK_MSG( !IsStandard(), s
,
135 wxT("name of predefined format cannot be retrieved") );
137 int len
= ::GetClipboardFormatName(m_format
, s
.GetWriteBuf(max
), max
);
142 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
154 ADD_IID(EnumFORMATETC
)
157 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
159 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
164 m_formats
= new CLIPFORMAT
[nCount
];
165 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
166 m_formats
[n
] = formats
[n
].GetFormatId();
170 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
174 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
177 // we only return 1 element at a time - mainly because I'm too lazy to
178 // implement something which you're never asked for anyhow
182 if ( m_nCurrent
< m_nCount
) {
184 format
.cfFormat
= m_formats
[m_nCurrent
++];
186 format
.dwAspect
= DVASPECT_CONTENT
;
188 format
.tymed
= TYMED_HGLOBAL
;
199 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
201 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
204 if ( m_nCurrent
< m_nCount
)
207 // no, can't skip this many elements
213 STDMETHODIMP
wxIEnumFORMATETC::Reset()
215 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
222 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
224 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
226 // unfortunately, we can't reuse the code in ctor - types are different
227 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
228 pNew
->m_nCount
= m_nCount
;
229 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
230 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
231 pNew
->m_formats
[n
] = m_formats
[n
];
239 // ----------------------------------------------------------------------------
241 // ----------------------------------------------------------------------------
243 BEGIN_IID_TABLE(wxIDataObject
)
248 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
250 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
253 m_pDataObject
= pDataObject
;
256 // get data functions
257 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
259 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
261 // is data is in our format?
262 HRESULT hr
= QueryGetData(pformatetcIn
);
266 // for the bitmaps and metafiles we use the handles instead of global memory
268 wxDataFormat format
= (wxDataFormatId
)pformatetcIn
->cfFormat
;
273 pmedium
->tymed
= TYMED_GDI
;
277 pmedium
->tymed
= TYMED_MFPICT
;
282 size_t size
= m_pDataObject
->GetDataSize(format
);
284 // it probably means that the method is just not implemented
285 wxLogDebug(wxT("Invalid data size - can't be 0"));
287 return DV_E_FORMATETC
;
290 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
291 if ( hGlobal
== NULL
) {
292 wxLogLastError("GlobalAlloc");
293 return E_OUTOFMEMORY
;
297 pmedium
->tymed
= TYMED_HGLOBAL
;
298 pmedium
->hGlobal
= hGlobal
;
301 pmedium
->pUnkForRelease
= NULL
;
304 hr
= GetDataHere(pformatetcIn
, pmedium
);
306 // free resources we allocated
307 if ( pmedium
->tymed
== TYMED_HGLOBAL
) {
308 GlobalFree(pmedium
->hGlobal
);
317 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
320 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
322 // put data in caller provided medium
323 switch ( pmedium
->tymed
)
326 m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
);
330 // this should be copied on bitmaps - but I don't have time for
332 wxFAIL_MSG(wxT("TODO - no support for metafiles in wxDataObject"));
338 void *pBuf
= GlobalLock(pmedium
->hGlobal
);
339 if ( pBuf
== NULL
) {
340 wxLogLastError(wxT("GlobalLock"));
341 return E_OUTOFMEMORY
;
344 wxDataFormat format
= (wxDataFormatId
)pformatetc
->cfFormat
;
345 m_pDataObject
->GetDataHere(format
, pBuf
);
347 GlobalUnlock(pmedium
->hGlobal
);
358 // set data functions (not implemented)
359 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
363 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
368 // information functions
369 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
371 // do we accept data in this format?
372 if ( pformatetc
== NULL
) {
373 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: invalid ptr."));
378 // the only one allowed by current COM implementation
379 if ( pformatetc
->lindex
!= -1 ) {
380 wxLogTrace(wxTRACE_OleCalls
,
381 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
386 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
387 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
388 wxLogTrace(wxTRACE_OleCalls
,
389 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
390 pformatetc
->dwAspect
);
391 return DV_E_DVASPECT
;
394 // we only transfer data by global memory, except for some particular cases
395 wxDataFormat format
= (wxDataFormatId
)pformatetc
->cfFormat
;
396 DWORD tymed
= pformatetc
->tymed
;
397 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) ||
398 !(tymed
& TYMED_HGLOBAL
) ) {
399 // it's not what we're waiting for
401 wxLogTrace(wxTRACE_OleCalls
,
402 wxT("wxIDataObject::QueryGetData: %s & %s == 0."),
404 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
: TYMED_HGLOBAL
));
409 // and now check the type of data requested
410 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
412 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
413 wxDataObject::GetFormatName(format
));
418 wxLogTrace(wxTRACE_OleCalls
,
419 wxT("wxIDataObject::QueryGetData: %s unsupported"),
420 wxDataObject::GetFormatName((wxDataFormatId
)pformatetc
->cfFormat
));
421 return DV_E_FORMATETC
;
425 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
426 FORMATETC
*pFormatetcOut
)
428 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
430 // TODO we might want something better than this trivial implementation here
431 if ( pFormatetcOut
!= NULL
)
432 pFormatetcOut
->ptd
= NULL
;
434 return DATA_S_SAMEFORMATETC
;
437 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDirection
,
438 IEnumFORMATETC
**ppenumFormatEtc
)
440 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
442 if ( dwDirection
== DATADIR_SET
) {
443 // we don't allow setting of data anyhow
447 size_t nFormatCount
= m_pDataObject
->GetFormatCount();
448 wxDataFormat format
, *formats
;
449 if ( nFormatCount
== 1 ) {
450 // this is the most common case, this is why we consider it separately
452 format
= m_pDataObject
->GetPreferredFormat();
455 // bad luck, build the array with all formats
456 formats
= new wxDataFormat
[nFormatCount
];
457 m_pDataObject
->GetAllFormats(formats
);
460 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
462 *ppenumFormatEtc
= pEnum
;
464 if ( formats
!= &format
) {
471 // advise sink functions (not implemented)
472 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
474 IAdviseSink
*pAdvSink
,
475 DWORD
*pdwConnection
)
477 return OLE_E_ADVISENOTSUPPORTED
;
480 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
482 return OLE_E_ADVISENOTSUPPORTED
;
485 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
487 return OLE_E_ADVISENOTSUPPORTED
;
490 // ----------------------------------------------------------------------------
492 // ----------------------------------------------------------------------------
494 wxDataObject::wxDataObject()
496 m_pIDataObject
= new wxIDataObject(this);
497 m_pIDataObject
->AddRef();
500 wxDataObject::~wxDataObject()
502 m_pIDataObject
->Release();
505 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
) const
507 size_t nFormatCount
= GetFormatCount();
508 if ( nFormatCount
== 1 ) {
509 return format
== GetPreferredFormat();
512 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
513 GetAllFormats(formats
);
516 for ( n
= 0; n
< nFormatCount
; n
++ ) {
517 if ( formats
[n
] == format
)
524 return n
< nFormatCount
;
529 const char *wxDataObject::GetFormatName(wxDataFormat format
)
531 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
533 #pragma warning(disable:4063)
536 static char s_szBuf
[128];
538 case CF_TEXT
: return "CF_TEXT";
539 case CF_BITMAP
: return "CF_BITMAP";
540 case CF_METAFILEPICT
: return "CF_METAFILEPICT";
541 case CF_SYLK
: return "CF_SYLK";
542 case CF_DIF
: return "CF_DIF";
543 case CF_TIFF
: return "CF_TIFF";
544 case CF_OEMTEXT
: return "CF_OEMTEXT";
545 case CF_DIB
: return "CF_DIB";
546 case CF_PALETTE
: return "CF_PALETTE";
547 case CF_PENDATA
: return "CF_PENDATA";
548 case CF_RIFF
: return "CF_RIFF";
549 case CF_WAVE
: return "CF_WAVE";
550 case CF_UNICODETEXT
: return "CF_UNICODETEXT";
551 case CF_ENHMETAFILE
: return "CF_ENHMETAFILE";
552 case CF_HDROP
: return "CF_HDROP";
553 case CF_LOCALE
: return "CF_LOCALE";
555 sprintf(s_szBuf
, "clipboard format %d (unknown)", format
);
560 #pragma warning(default:4063)
565 // ----------------------------------------------------------------------------
566 // wxPrivateDataObject
567 // ----------------------------------------------------------------------------
569 wxPrivateDataObject::wxPrivateDataObject()
575 void wxPrivateDataObject::Free()
581 void wxPrivateDataObject::SetData( const void *data
, size_t size
)
586 m_data
= malloc(size
);
588 memcpy( m_data
, data
, size
);
591 void wxPrivateDataObject::WriteData( void *dest
) const
593 WriteData( m_data
, dest
);
596 size_t wxPrivateDataObject::GetSize() const
601 void wxPrivateDataObject::WriteData( const void *data
, void *dest
) const
603 memcpy( dest
, data
, GetSize() );
606 // ----------------------------------------------------------------------------
607 // wxBitmapDataObject
608 // ----------------------------------------------------------------------------
610 // the bitmaps aren't passed by value as other types of data (i.e. by copyign
611 // the data into a global memory chunk and passing it to the clipboard or
612 // another application or whatever), but by handle, so these generic functions
613 // don't make much sense to them.
615 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
617 // no data to copy anyhow
621 void wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
624 // we put a bitmap handle into pBuf
625 *(WXHBITMAP
*)pBuf
= m_bitmap
.GetHBITMAP();
628 // ----------------------------------------------------------------------------
630 // ----------------------------------------------------------------------------
634 static const char *GetTymedName(DWORD tymed
)
636 static char s_szBuf
[128];
638 case TYMED_HGLOBAL
: return "TYMED_HGLOBAL";
639 case TYMED_FILE
: return "TYMED_FILE";
640 case TYMED_ISTREAM
: return "TYMED_ISTREAM";
641 case TYMED_ISTORAGE
: return "TYMED_ISTORAGE";
642 case TYMED_GDI
: return "TYMED_GDI";
643 case TYMED_MFPICT
: return "TYMED_MFPICT";
644 case TYMED_ENHMF
: return "TYMED_ENHMF";
646 sprintf(s_szBuf
, "type of media format %d (unknown)", tymed
);