]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
14ae6ed6a880334eb1101f26f169ffa63e1e0166
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
));
486 wxLogTrace(wxTRACE_OleCalls
,
487 wxT("wxIDataObject::QueryGetData: %s unsupported"),
488 wxDataObject::GetFormatName(format
));
490 return DV_E_FORMATETC
;
493 // we only transfer data by global memory, except for some particular cases
494 DWORD tymed
= pformatetc
->tymed
;
495 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
496 !(tymed
& TYMED_HGLOBAL
) ) {
497 // it's not what we're waiting for
499 wxLogTrace(wxTRACE_OleCalls
,
500 wxT("wxIDataObject::QueryGetData: %s != %s"),
502 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
512 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
513 FORMATETC
*pFormatetcOut
)
515 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
517 // TODO we might want something better than this trivial implementation here
518 if ( pFormatetcOut
!= NULL
)
519 pFormatetcOut
->ptd
= NULL
;
521 return DATA_S_SAMEFORMATETC
;
524 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDirection
,
525 IEnumFORMATETC
**ppenumFormatEtc
)
527 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
529 bool allowOutputOnly
= dwDirection
== DATADIR_GET
;
531 size_t nFormatCount
= m_pDataObject
->GetFormatCount(allowOutputOnly
);
532 wxDataFormat format
, *formats
;
533 if ( nFormatCount
== 1 ) {
534 // this is the most common case, this is why we consider it separately
536 format
= m_pDataObject
->GetPreferredFormat();
539 // bad luck, build the array with all formats
540 formats
= new wxDataFormat
[nFormatCount
];
541 m_pDataObject
->GetAllFormats(formats
, allowOutputOnly
);
544 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
546 *ppenumFormatEtc
= pEnum
;
548 if ( formats
!= &format
) {
555 // advise sink functions (not implemented)
556 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
558 IAdviseSink
*pAdvSink
,
559 DWORD
*pdwConnection
)
561 return OLE_E_ADVISENOTSUPPORTED
;
564 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
566 return OLE_E_ADVISENOTSUPPORTED
;
569 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
571 return OLE_E_ADVISENOTSUPPORTED
;
574 // ----------------------------------------------------------------------------
576 // ----------------------------------------------------------------------------
578 wxDataObject::wxDataObject()
580 m_pIDataObject
= new wxIDataObject(this);
581 m_pIDataObject
->AddRef();
584 wxDataObject::~wxDataObject()
586 ReleaseInterface(m_pIDataObject
);
589 void wxDataObject::SetAutoDelete()
591 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
592 m_pIDataObject
->Release();
594 // so that the dtor doesnt' crash
595 m_pIDataObject
= NULL
;
598 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
) const
600 size_t nFormatCount
= GetFormatCount();
601 if ( nFormatCount
== 1 ) {
602 return format
== GetPreferredFormat();
605 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
606 GetAllFormats(formats
);
609 for ( n
= 0; n
< nFormatCount
; n
++ ) {
610 if ( formats
[n
] == format
)
617 return n
< nFormatCount
;
622 const char *wxDataObject::GetFormatName(wxDataFormat format
)
624 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
626 #pragma warning(disable:4063)
629 static char s_szBuf
[128];
631 case CF_TEXT
: return "CF_TEXT";
632 case CF_BITMAP
: return "CF_BITMAP";
633 case CF_METAFILEPICT
: return "CF_METAFILEPICT";
634 case CF_SYLK
: return "CF_SYLK";
635 case CF_DIF
: return "CF_DIF";
636 case CF_TIFF
: return "CF_TIFF";
637 case CF_OEMTEXT
: return "CF_OEMTEXT";
638 case CF_DIB
: return "CF_DIB";
639 case CF_PALETTE
: return "CF_PALETTE";
640 case CF_PENDATA
: return "CF_PENDATA";
641 case CF_RIFF
: return "CF_RIFF";
642 case CF_WAVE
: return "CF_WAVE";
643 case CF_UNICODETEXT
: return "CF_UNICODETEXT";
644 case CF_ENHMETAFILE
: return "CF_ENHMETAFILE";
645 case CF_HDROP
: return "CF_HDROP";
646 case CF_LOCALE
: return "CF_LOCALE";
648 sprintf(s_szBuf
, "clipboard format 0x%x (unknown)", format
);
653 #pragma warning(default:4063)
658 // ----------------------------------------------------------------------------
659 // wxPrivateDataObject
660 // ----------------------------------------------------------------------------
662 wxPrivateDataObject::wxPrivateDataObject()
668 void wxPrivateDataObject::Free()
674 void wxPrivateDataObject::SetData( const void *data
, size_t size
)
679 m_data
= malloc(size
);
681 memcpy( m_data
, data
, size
);
684 void wxPrivateDataObject::WriteData( void *dest
) const
686 WriteData( m_data
, dest
);
689 size_t wxPrivateDataObject::GetSize() const
694 void wxPrivateDataObject::WriteData( const void *data
, void *dest
) const
696 memcpy( dest
, data
, GetSize() );
699 // ----------------------------------------------------------------------------
700 // wxBitmapDataObject: it supports standard CF_BITMAP and CF_DIB formats
701 // ----------------------------------------------------------------------------
703 size_t wxBitmapDataObject::GetFormatCount(bool outputOnlyToo
) const
708 void wxBitmapDataObject::GetAllFormats(wxDataFormat
*formats
,
709 bool outputOnlyToo
) const
711 formats
[0] = CF_BITMAP
;
715 // the bitmaps aren't passed by value as other types of data (i.e. by copyign
716 // the data into a global memory chunk and passing it to the clipboard or
717 // another application or whatever), but by handle, so these generic functions
718 // don't make much sense to them.
720 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
722 if ( format
.GetFormatId() == CF_DIB
)
727 // shouldn't be selected into a DC or GetDIBits() would fail
728 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
729 wxT("can't copy bitmap selected into wxMemoryDC") );
731 // first get the info
733 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
734 NULL
, &bi
, DIB_RGB_COLORS
) )
736 wxLogLastError("GetDIBits(NULL)");
741 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
745 // no data to copy - we don't pass HBITMAP via global memory
750 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
753 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
755 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
756 if ( format
.GetFormatId() == CF_DIB
)
761 // shouldn't be selected into a DC or GetDIBits() would fail
762 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
763 wxT("can't copy bitmap selected into wxMemoryDC") );
765 // first get the info
766 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
767 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
769 wxLogLastError("GetDIBits(NULL)");
774 // and now copy the bits
775 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
776 pbi
, DIB_RGB_COLORS
) )
778 wxLogLastError("GetDIBits");
785 // we put a bitmap handle into pBuf
786 *(HBITMAP
*)pBuf
= hbmp
;
792 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
, const void *pBuf
)
795 if ( format
.GetFormatId() == CF_DIB
)
797 // here we get BITMAPINFO struct followed by the actual bitmap bits and
798 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
801 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
802 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
803 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
804 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
807 wxLogLastError("CreateDIBitmap");
810 m_bitmap
.SetWidth(pbmih
->biWidth
);
811 m_bitmap
.SetHeight(pbmih
->biHeight
);
815 // it's easy with bitmaps: we pass them by handle
816 hbmp
= *(HBITMAP
*)pBuf
;
819 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
821 wxLogLastError("GetObject(HBITMAP)");
824 m_bitmap
.SetWidth(bmp
.bmWidth
);
825 m_bitmap
.SetHeight(bmp
.bmHeight
);
826 m_bitmap
.SetDepth(bmp
.bmPlanes
);
829 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
831 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
836 // ----------------------------------------------------------------------------
838 // ----------------------------------------------------------------------------
842 static const wxChar
*GetTymedName(DWORD tymed
)
844 static wxChar s_szBuf
[128];
846 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
847 case TYMED_FILE
: return wxT("TYMED_FILE");
848 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
849 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
850 case TYMED_GDI
: return wxT("TYMED_GDI");
851 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
852 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
854 wxSprintf(s_szBuf
, wxT("type of media format %d (unknown)"), tymed
);
861 #endif // not using OLE at all