]>
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 wxChar
*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
);
95 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
96 // is), but in some cases, the situation is inversed, that is we delete it
97 // when this object is deleted - setting this flag enables such logic
98 void SetDeleteFlag() { m_mustDelete
= TRUE
; }
100 DECLARE_IUNKNOWN_METHODS
;
103 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
104 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
105 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
106 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
107 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
108 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
109 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
110 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
111 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
114 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
119 // ----------------------------------------------------------------------------
120 // small helper class for getting screen DC (we're working with bitmaps and
122 // ----------------------------------------------------------------------------
127 ScreenHDC() { m_hdc
= GetDC(NULL
); }
128 ~ScreenHDC() { ReleaseDC(NULL
, m_hdc
); }
129 operator HDC() const { return m_hdc
; }
135 // ============================================================================
137 // ============================================================================
139 // ----------------------------------------------------------------------------
141 // ----------------------------------------------------------------------------
143 void wxDataFormat::SetId(const wxChar
*format
)
145 m_format
= ::RegisterClipboardFormat(format
);
148 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
152 wxString
wxDataFormat::GetId() const
154 static const int max
= 256;
158 wxCHECK_MSG( !IsStandard(), s
,
159 wxT("name of predefined format cannot be retrieved") );
161 int len
= ::GetClipboardFormatName(m_format
, s
.GetWriteBuf(max
), max
);
166 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
178 ADD_IID(EnumFORMATETC
)
181 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
183 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
188 m_formats
= new CLIPFORMAT
[nCount
];
189 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
190 m_formats
[n
] = formats
[n
].GetFormatId();
194 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
198 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
201 // we only return 1 element at a time - mainly because I'm too lazy to
202 // implement something which you're never asked for anyhow
206 if ( m_nCurrent
< m_nCount
) {
208 format
.cfFormat
= m_formats
[m_nCurrent
++];
210 format
.dwAspect
= DVASPECT_CONTENT
;
212 format
.tymed
= TYMED_HGLOBAL
;
223 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
225 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
228 if ( m_nCurrent
< m_nCount
)
231 // no, can't skip this many elements
237 STDMETHODIMP
wxIEnumFORMATETC::Reset()
239 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
246 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
248 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
250 // unfortunately, we can't reuse the code in ctor - types are different
251 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
252 pNew
->m_nCount
= m_nCount
;
253 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
254 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
255 pNew
->m_formats
[n
] = m_formats
[n
];
263 // ----------------------------------------------------------------------------
265 // ----------------------------------------------------------------------------
267 BEGIN_IID_TABLE(wxIDataObject
)
272 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
274 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
277 m_pDataObject
= pDataObject
;
278 m_mustDelete
= FALSE
;
281 wxIDataObject::~wxIDataObject()
285 delete m_pDataObject
;
289 // get data functions
290 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
292 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
294 // is data is in our format?
295 HRESULT hr
= QueryGetData(pformatetcIn
);
299 // for the bitmaps and metafiles we use the handles instead of global memory
301 wxDataFormat format
= (wxDataFormatId
)pformatetcIn
->cfFormat
;
306 pmedium
->tymed
= TYMED_GDI
;
310 pmedium
->tymed
= TYMED_MFPICT
;
315 size_t size
= m_pDataObject
->GetDataSize(format
);
317 // it probably means that the method is just not implemented
318 wxLogDebug(wxT("Invalid data size - can't be 0"));
320 return DV_E_FORMATETC
;
323 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
324 if ( hGlobal
== NULL
) {
325 wxLogLastError("GlobalAlloc");
326 return E_OUTOFMEMORY
;
330 pmedium
->tymed
= TYMED_HGLOBAL
;
331 pmedium
->hGlobal
= hGlobal
;
334 pmedium
->pUnkForRelease
= NULL
;
337 hr
= GetDataHere(pformatetcIn
, pmedium
);
339 // free resources we allocated
340 if ( pmedium
->tymed
== TYMED_HGLOBAL
) {
341 GlobalFree(pmedium
->hGlobal
);
350 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
353 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
355 // put data in caller provided medium
356 switch ( pmedium
->tymed
)
359 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
364 // this should be copied on bitmaps - but I don't have time for
366 wxFAIL_MSG(wxT("TODO - no support for metafiles in wxDataObject"));
372 void *pBuf
= GlobalLock(pmedium
->hGlobal
);
373 if ( pBuf
== NULL
) {
374 wxLogLastError(wxT("GlobalLock"));
375 return E_OUTOFMEMORY
;
378 wxDataFormat format
= (wxDataFormatId
)pformatetc
->cfFormat
;
379 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
382 GlobalUnlock(pmedium
->hGlobal
);
393 // set data functions (not implemented)
394 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
398 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
400 switch ( pmedium
->tymed
)
403 m_pDataObject
->SetData(wxDF_BITMAP
, &pmedium
->hBitmap
);
407 // this should be copied on bitmaps - but I don't have time for
409 wxFAIL_MSG(wxT("TODO - no support for metafiles in wxDataObject"));
415 void *pBuf
= GlobalLock(pmedium
->hGlobal
);
416 if ( pBuf
== NULL
) {
417 wxLogLastError("GlobalLock");
419 return E_OUTOFMEMORY
;
422 wxDataFormat format
= (wxDataFormatId
)pformatetc
->cfFormat
;
423 m_pDataObject
->SetData(format
, pBuf
);
425 GlobalUnlock(pmedium
->hGlobal
);
434 // we own the medium, so we must release it - but do *not* free the
435 // bitmap handle fi we have it because we have copied it elsewhere
436 if ( pmedium
->tymed
== TYMED_GDI
)
438 pmedium
->hBitmap
= 0;
441 ReleaseStgMedium(pmedium
);
447 // information functions
448 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
450 // do we accept data in this format?
451 if ( pformatetc
== NULL
) {
452 wxLogTrace(wxTRACE_OleCalls
,
453 wxT("wxIDataObject::QueryGetData: invalid ptr."));
458 // the only one allowed by current COM implementation
459 if ( pformatetc
->lindex
!= -1 ) {
460 wxLogTrace(wxTRACE_OleCalls
,
461 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
467 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
468 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
469 wxLogTrace(wxTRACE_OleCalls
,
470 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
471 pformatetc
->dwAspect
);
473 return DV_E_DVASPECT
;
476 // and now check the type of data requested
477 wxDataFormat format
= (wxDataFormatId
)pformatetc
->cfFormat
;
478 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
480 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
481 wxDataObject::GetFormatName(format
));
485 wxLogTrace(wxTRACE_OleCalls
,
486 wxT("wxIDataObject::QueryGetData: %s unsupported"),
487 wxDataObject::GetFormatName(format
));
489 return DV_E_FORMATETC
;
492 // we only transfer data by global memory, except for some particular cases
493 DWORD tymed
= pformatetc
->tymed
;
494 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
495 !(tymed
& TYMED_HGLOBAL
) ) {
496 // it's not what we're waiting for
498 wxLogTrace(wxTRACE_OleCalls
,
499 wxT("wxIDataObject::QueryGetData: %s != %s"),
501 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
511 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
512 FORMATETC
*pFormatetcOut
)
514 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
516 // TODO we might want something better than this trivial implementation here
517 if ( pFormatetcOut
!= NULL
)
518 pFormatetcOut
->ptd
= NULL
;
520 return DATA_S_SAMEFORMATETC
;
523 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDirection
,
524 IEnumFORMATETC
**ppenumFormatEtc
)
526 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
528 bool allowOutputOnly
= dwDirection
== DATADIR_GET
;
530 size_t nFormatCount
= m_pDataObject
->GetFormatCount(allowOutputOnly
);
531 wxDataFormat format
, *formats
;
532 if ( nFormatCount
== 1 ) {
533 // this is the most common case, this is why we consider it separately
535 format
= m_pDataObject
->GetPreferredFormat();
538 // bad luck, build the array with all formats
539 formats
= new wxDataFormat
[nFormatCount
];
540 m_pDataObject
->GetAllFormats(formats
, allowOutputOnly
);
543 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
545 *ppenumFormatEtc
= pEnum
;
547 if ( formats
!= &format
) {
554 // advise sink functions (not implemented)
555 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
557 IAdviseSink
*pAdvSink
,
558 DWORD
*pdwConnection
)
560 return OLE_E_ADVISENOTSUPPORTED
;
563 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
565 return OLE_E_ADVISENOTSUPPORTED
;
568 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
570 return OLE_E_ADVISENOTSUPPORTED
;
573 // ----------------------------------------------------------------------------
575 // ----------------------------------------------------------------------------
577 wxDataObject::wxDataObject()
579 m_pIDataObject
= new wxIDataObject(this);
580 m_pIDataObject
->AddRef();
583 wxDataObject::~wxDataObject()
585 ReleaseInterface(m_pIDataObject
);
588 void wxDataObject::SetAutoDelete()
590 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
591 m_pIDataObject
->Release();
593 // so that the dtor doesnt' crash
594 m_pIDataObject
= NULL
;
597 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
) const
599 size_t nFormatCount
= GetFormatCount();
600 if ( nFormatCount
== 1 ) {
601 return format
== GetPreferredFormat();
604 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
605 GetAllFormats(formats
);
608 for ( n
= 0; n
< nFormatCount
; n
++ ) {
609 if ( formats
[n
] == format
)
616 return n
< nFormatCount
;
621 const char *wxDataObject::GetFormatName(wxDataFormat format
)
623 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
625 #pragma warning(disable:4063)
628 static char s_szBuf
[128];
630 case CF_TEXT
: return "CF_TEXT";
631 case CF_BITMAP
: return "CF_BITMAP";
632 case CF_METAFILEPICT
: return "CF_METAFILEPICT";
633 case CF_SYLK
: return "CF_SYLK";
634 case CF_DIF
: return "CF_DIF";
635 case CF_TIFF
: return "CF_TIFF";
636 case CF_OEMTEXT
: return "CF_OEMTEXT";
637 case CF_DIB
: return "CF_DIB";
638 case CF_PALETTE
: return "CF_PALETTE";
639 case CF_PENDATA
: return "CF_PENDATA";
640 case CF_RIFF
: return "CF_RIFF";
641 case CF_WAVE
: return "CF_WAVE";
642 case CF_UNICODETEXT
: return "CF_UNICODETEXT";
643 case CF_ENHMETAFILE
: return "CF_ENHMETAFILE";
644 case CF_HDROP
: return "CF_HDROP";
645 case CF_LOCALE
: return "CF_LOCALE";
647 sprintf(s_szBuf
, "clipboard format 0x%x (unknown)", format
);
652 #pragma warning(default:4063)
657 // ----------------------------------------------------------------------------
658 // wxPrivateDataObject
659 // ----------------------------------------------------------------------------
661 wxPrivateDataObject::wxPrivateDataObject()
667 void wxPrivateDataObject::Free()
673 void wxPrivateDataObject::SetData( const void *data
, size_t size
)
678 m_data
= malloc(size
);
680 memcpy( m_data
, data
, size
);
683 void wxPrivateDataObject::WriteData( void *dest
) const
685 WriteData( m_data
, dest
);
688 size_t wxPrivateDataObject::GetSize() const
693 void wxPrivateDataObject::WriteData( const void *data
, void *dest
) const
695 memcpy( dest
, data
, GetSize() );
698 // ----------------------------------------------------------------------------
699 // wxBitmapDataObject: it supports standard CF_BITMAP and CF_DIB formats
700 // ----------------------------------------------------------------------------
702 size_t wxBitmapDataObject::GetFormatCount(bool outputOnlyToo
) const
707 void wxBitmapDataObject::GetAllFormats(wxDataFormat
*formats
,
708 bool outputOnlyToo
) const
710 formats
[0] = CF_BITMAP
;
714 // the bitmaps aren't passed by value as other types of data (i.e. by copyign
715 // the data into a global memory chunk and passing it to the clipboard or
716 // another application or whatever), but by handle, so these generic functions
717 // don't make much sense to them.
719 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
721 if ( format
.GetFormatId() == CF_DIB
)
726 // shouldn't be selected into a DC or GetDIBits() would fail
727 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
728 wxT("can't copy bitmap selected into wxMemoryDC") );
730 // first get the info
732 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
733 NULL
, &bi
, DIB_RGB_COLORS
) )
735 wxLogLastError("GetDIBits(NULL)");
740 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
744 // no data to copy - we don't pass HBITMAP via global memory
749 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
752 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
754 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
755 if ( format
.GetFormatId() == CF_DIB
)
760 // shouldn't be selected into a DC or GetDIBits() would fail
761 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
762 wxT("can't copy bitmap selected into wxMemoryDC") );
764 // first get the info
765 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
766 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
768 wxLogLastError("GetDIBits(NULL)");
773 // and now copy the bits
774 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
775 pbi
, DIB_RGB_COLORS
) )
777 wxLogLastError("GetDIBits");
784 // we put a bitmap handle into pBuf
785 *(HBITMAP
*)pBuf
= hbmp
;
791 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
, const void *pBuf
)
794 if ( format
.GetFormatId() == CF_DIB
)
796 // here we get BITMAPINFO struct followed by the actual bitmap bits and
797 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
800 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
801 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
802 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
803 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
806 wxLogLastError("CreateDIBitmap");
809 m_bitmap
.SetWidth(pbmih
->biWidth
);
810 m_bitmap
.SetHeight(pbmih
->biHeight
);
814 // it's easy with bitmaps: we pass them by handle
815 hbmp
= *(HBITMAP
*)pBuf
;
818 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
820 wxLogLastError("GetObject(HBITMAP)");
823 m_bitmap
.SetWidth(bmp
.bmWidth
);
824 m_bitmap
.SetHeight(bmp
.bmHeight
);
825 m_bitmap
.SetDepth(bmp
.bmPlanes
);
828 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
830 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
835 // ----------------------------------------------------------------------------
837 // ----------------------------------------------------------------------------
841 static const wxChar
*GetTymedName(DWORD tymed
)
843 static wxChar s_szBuf
[128];
845 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
846 case TYMED_FILE
: return wxT("TYMED_FILE");
847 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
848 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
849 case TYMED_GDI
: return wxT("TYMED_GDI");
850 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
851 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
853 wxSprintf(s_szBuf
, wxT("type of media format %d (unknown)"), tymed
);
860 #endif // not using OLE at all