]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
927950ca11713ff6b71296650cdff1b091d0dfad
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__)
36 #include "wx/dataobj.h"
38 #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
40 #include "wx/msw/private.h" // includes <windows.h>
42 #if wxUSE_NORLANDER_HEADERS
54 #include "wx/msw/ole/oleutils.h"
56 #include "wx/msw/dib.h"
58 #ifndef CFSTR_SHELLURL
59 #define CFSTR_SHELLURL _T("UniformResourceLocator")
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
67 static const wxChar
*GetTymedName(DWORD tymed
);
69 #define GetTymedName(tymed) _T("")
70 #endif // Debug/!Debug
72 // ----------------------------------------------------------------------------
73 // wxIEnumFORMATETC interface implementation
74 // ----------------------------------------------------------------------------
76 class wxIEnumFORMATETC
: public IEnumFORMATETC
79 wxIEnumFORMATETC(const wxDataFormat
* formats
, ULONG nCount
);
80 virtual ~wxIEnumFORMATETC() { delete [] m_formats
; }
82 DECLARE_IUNKNOWN_METHODS
;
85 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
86 STDMETHODIMP
Skip(ULONG celt
);
88 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
91 CLIPFORMAT
*m_formats
; // formats we can provide data in
92 ULONG m_nCount
, // number of formats we support
93 m_nCurrent
; // current enum position
96 // ----------------------------------------------------------------------------
97 // wxIDataObject implementation of IDataObject interface
98 // ----------------------------------------------------------------------------
100 class wxIDataObject
: public IDataObject
103 wxIDataObject(wxDataObject
*pDataObject
);
104 virtual ~wxIDataObject();
106 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
107 // is), but in some cases, the situation is inversed, that is we delete it
108 // when this object is deleted - setting this flag enables such logic
109 void SetDeleteFlag() { m_mustDelete
= TRUE
; }
111 DECLARE_IUNKNOWN_METHODS
;
114 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
115 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
116 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
117 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
118 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
119 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
120 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
121 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
122 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
125 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
130 // ============================================================================
132 // ============================================================================
134 // ----------------------------------------------------------------------------
136 // ----------------------------------------------------------------------------
138 void wxDataFormat::SetId(const wxChar
*format
)
140 m_format
= (wxDataFormat::NativeFormat
)::RegisterClipboardFormat(format
);
143 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
147 wxString
wxDataFormat::GetId() const
149 static const int max
= 256;
153 wxCHECK_MSG( !IsStandard(), s
,
154 wxT("name of predefined format cannot be retrieved") );
156 int len
= ::GetClipboardFormatName(m_format
, s
.GetWriteBuf(max
), max
);
161 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
167 // ----------------------------------------------------------------------------
169 // ----------------------------------------------------------------------------
171 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
173 ADD_IID(EnumFORMATETC
)
176 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
178 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
182 m_formats
= new CLIPFORMAT
[nCount
];
183 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
184 m_formats
[n
] = formats
[n
].GetFormatId();
188 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
190 ULONG
*WXUNUSED(pceltFetched
))
192 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
195 // we only return 1 element at a time - mainly because I'm too lazy to
196 // implement something which you're never asked for anyhow
200 if ( m_nCurrent
< m_nCount
) {
202 format
.cfFormat
= m_formats
[m_nCurrent
++];
204 format
.dwAspect
= DVASPECT_CONTENT
;
206 format
.tymed
= TYMED_HGLOBAL
;
217 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
219 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
222 if ( m_nCurrent
< m_nCount
)
225 // no, can't skip this many elements
231 STDMETHODIMP
wxIEnumFORMATETC::Reset()
233 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
240 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
242 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
244 // unfortunately, we can't reuse the code in ctor - types are different
245 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
246 pNew
->m_nCount
= m_nCount
;
247 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
248 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
249 pNew
->m_formats
[n
] = m_formats
[n
];
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
261 BEGIN_IID_TABLE(wxIDataObject
)
266 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
268 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
270 m_pDataObject
= pDataObject
;
271 m_mustDelete
= FALSE
;
274 wxIDataObject::~wxIDataObject()
278 delete m_pDataObject
;
282 // get data functions
283 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
285 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
287 // is data is in our format?
288 HRESULT hr
= QueryGetData(pformatetcIn
);
292 // for the bitmaps and metafiles we use the handles instead of global memory
294 wxDataFormat format
= (wxDataFormat::NativeFormat
)pformatetcIn
->cfFormat
;
299 pmedium
->tymed
= TYMED_GDI
;
302 case wxDF_ENHMETAFILE
:
303 pmedium
->tymed
= TYMED_ENHMF
;
307 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
308 sizeof(METAFILEPICT
));
309 if ( !pmedium
->hGlobal
) {
310 wxLogLastError(wxT("GlobalAlloc"));
311 return E_OUTOFMEMORY
;
313 pmedium
->tymed
= TYMED_MFPICT
;
318 size_t size
= m_pDataObject
->GetDataSize(format
);
320 // it probably means that the method is just not implemented
321 wxLogDebug(wxT("Invalid data size - can't be 0"));
323 return DV_E_FORMATETC
;
326 if ( !format
.IsStandard() ) {
327 // for custom formats, put the size with the data - alloc the
329 // MB: not completely sure this is correct,
330 // even if I can't figure out what's wrong
331 size
+= m_pDataObject
->GetBufferOffset( format
);
334 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
335 if ( hGlobal
== NULL
) {
336 wxLogLastError(wxT("GlobalAlloc"));
337 return E_OUTOFMEMORY
;
341 pmedium
->tymed
= TYMED_HGLOBAL
;
342 pmedium
->hGlobal
= hGlobal
;
345 pmedium
->pUnkForRelease
= NULL
;
348 hr
= GetDataHere(pformatetcIn
, pmedium
);
350 // free resources we allocated
351 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
352 GlobalFree(pmedium
->hGlobal
);
361 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
364 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
366 // put data in caller provided medium
367 switch ( pmedium
->tymed
)
370 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
375 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
376 &pmedium
->hEnhMetaFile
) )
381 // fall through - we pass METAFILEPICT through HGLOBAL
386 HGLOBAL hGlobal
= pmedium
->hGlobal
;
387 void *pBuf
= GlobalLock(hGlobal
);
388 if ( pBuf
== NULL
) {
389 wxLogLastError(wxT("GlobalLock"));
390 return E_OUTOFMEMORY
;
393 wxDataFormat format
= pformatetc
->cfFormat
;
394 if ( !format
.IsStandard() ) {
395 // for custom formats, put the size with the data
396 pBuf
= m_pDataObject
->SetSizeInBuffer( pBuf
, GlobalSize(hGlobal
), format
);
399 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
402 GlobalUnlock(hGlobal
);
413 // set data functions
414 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
418 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
420 switch ( pmedium
->tymed
)
423 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
427 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
431 // fall through - we pass METAFILEPICT through HGLOBAL
434 wxDataFormat format
= pformatetc
->cfFormat
;
436 // this is quite weird, but for file drag and drop, explorer
437 // calls our SetData() with the formats we do *not* support!
439 // as we can't fix this bug in explorer (it's a bug because it
440 // should only use formats returned by EnumFormatEtc), do the
442 if ( !m_pDataObject
->IsSupportedFormat(format
) ) {
444 return DV_E_FORMATETC
;
448 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
449 if ( pBuf
== NULL
) {
450 wxLogLastError(wxT("GlobalLock"));
452 return E_OUTOFMEMORY
;
455 // we've got a problem with SetData() here because the base
456 // class version requires the size parameter which we don't
457 // have anywhere in OLE data transfer - so we need to
458 // synthetise it for known formats and we suppose that all data
459 // in custom formats starts with a DWORD containing the size
465 size
= strlen((const char *)pBuf
);
467 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
469 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
470 || ( defined(__MWERKS__) && defined(__WXMSW__) )
471 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
473 size
= ::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
479 // these formats don't use size at all, anyhow (but
480 // pass data by handle, which is always a single DWORD)
485 // the handler will calculate size itself (it's too
486 // complicated to do it here)
490 case CF_METAFILEPICT
:
491 size
= sizeof(METAFILEPICT
);
496 // we suppose that the size precedes the data
497 pBuf
= m_pDataObject
->GetSizeFromBuffer( pBuf
, &size
, format
);
498 if (! format
.IsStandard() ) {
499 // see GetData for coresponding increment
500 size
-= m_pDataObject
->GetBufferOffset( format
);
505 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
507 GlobalUnlock(pmedium
->hGlobal
);
520 // we own the medium, so we must release it - but do *not* free any
521 // data we pass by handle because we have copied it elsewhere
522 switch ( pmedium
->tymed
)
525 pmedium
->hBitmap
= 0;
529 pmedium
->hMetaFilePict
= 0;
533 pmedium
->hEnhMetaFile
= 0;
537 ReleaseStgMedium(pmedium
);
543 // information functions
544 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
546 // do we accept data in this format?
547 if ( pformatetc
== NULL
) {
548 wxLogTrace(wxTRACE_OleCalls
,
549 wxT("wxIDataObject::QueryGetData: invalid ptr."));
554 // the only one allowed by current COM implementation
555 if ( pformatetc
->lindex
!= -1 ) {
556 wxLogTrace(wxTRACE_OleCalls
,
557 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
563 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
564 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
565 wxLogTrace(wxTRACE_OleCalls
,
566 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
567 pformatetc
->dwAspect
);
569 return DV_E_DVASPECT
;
572 // and now check the type of data requested
573 wxDataFormat format
= pformatetc
->cfFormat
;
574 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
575 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
576 wxGetFormatName(format
));
579 wxLogTrace(wxTRACE_OleCalls
,
580 wxT("wxIDataObject::QueryGetData: %s unsupported"),
581 wxGetFormatName(format
));
583 return DV_E_FORMATETC
;
586 // we only transfer data by global memory, except for some particular cases
587 DWORD tymed
= pformatetc
->tymed
;
588 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
589 !(tymed
& TYMED_HGLOBAL
) ) {
590 // it's not what we're waiting for
591 wxLogTrace(wxTRACE_OleCalls
,
592 wxT("wxIDataObject::QueryGetData: %s != %s"),
594 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
603 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
604 FORMATETC
*pFormatetcOut
)
606 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
608 // TODO we might want something better than this trivial implementation here
609 if ( pFormatetcOut
!= NULL
)
610 pFormatetcOut
->ptd
= NULL
;
612 return DATA_S_SAMEFORMATETC
;
615 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
616 IEnumFORMATETC
**ppenumFormatEtc
)
618 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
620 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
623 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
625 wxDataFormat
*formats
;
626 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
627 m_pDataObject
->GetAllFormats(formats
, dir
);
629 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
631 *ppenumFormatEtc
= pEnum
;
633 if ( formats
!= &format
) {
640 // ----------------------------------------------------------------------------
641 // advise sink functions (not implemented)
642 // ----------------------------------------------------------------------------
644 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
645 DWORD
WXUNUSED(advf
),
646 IAdviseSink
*WXUNUSED(pAdvSink
),
647 DWORD
*WXUNUSED(pdwConnection
))
649 return OLE_E_ADVISENOTSUPPORTED
;
652 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
654 return OLE_E_ADVISENOTSUPPORTED
;
657 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
659 return OLE_E_ADVISENOTSUPPORTED
;
662 // ----------------------------------------------------------------------------
664 // ----------------------------------------------------------------------------
666 wxDataObject::wxDataObject()
668 m_pIDataObject
= new wxIDataObject(this);
669 m_pIDataObject
->AddRef();
672 wxDataObject::~wxDataObject()
674 ReleaseInterface(m_pIDataObject
);
677 void wxDataObject::SetAutoDelete()
679 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
680 m_pIDataObject
->Release();
682 // so that the dtor doesnt' crash
683 m_pIDataObject
= NULL
;
686 size_t wxDataObject::GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
688 return sizeof(size_t);
691 const void* wxDataObject::GetSizeFromBuffer( const void* buffer
, size_t* size
,
692 const wxDataFormat
& WXUNUSED(format
) )
694 size_t* p
= (size_t*)buffer
;
700 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
701 const wxDataFormat
& WXUNUSED(format
) )
703 size_t* p
= (size_t*)buffer
;
711 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
713 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
715 #pragma warning(disable:4063)
718 static wxChar s_szBuf
[256];
720 case CF_TEXT
: return wxT("CF_TEXT");
721 case CF_BITMAP
: return wxT("CF_BITMAP");
722 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
723 case CF_SYLK
: return wxT("CF_SYLK");
724 case CF_DIF
: return wxT("CF_DIF");
725 case CF_TIFF
: return wxT("CF_TIFF");
726 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
727 case CF_DIB
: return wxT("CF_DIB");
728 case CF_PALETTE
: return wxT("CF_PALETTE");
729 case CF_PENDATA
: return wxT("CF_PENDATA");
730 case CF_RIFF
: return wxT("CF_RIFF");
731 case CF_WAVE
: return wxT("CF_WAVE");
732 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
733 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
734 case CF_HDROP
: return wxT("CF_HDROP");
735 case CF_LOCALE
: return wxT("CF_LOCALE");
738 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
740 // it must be a new predefined format we don't know the name of
741 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
748 #pragma warning(default:4063)
754 // ----------------------------------------------------------------------------
755 // wxBitmapDataObject supports CF_DIB format
756 // ----------------------------------------------------------------------------
758 size_t wxBitmapDataObject::GetDataSize() const
760 return wxConvertBitmapToDIB(NULL
, GetBitmap());
763 bool wxBitmapDataObject::GetDataHere(void *buf
) const
765 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
768 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
770 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
772 if ( !bitmap
.Ok() ) {
773 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
783 // ----------------------------------------------------------------------------
784 // wxBitmapDataObject2 supports CF_BITMAP format
785 // ----------------------------------------------------------------------------
787 // the bitmaps aren't passed by value as other types of data (i.e. by copying
788 // the data into a global memory chunk and passing it to the clipboard or
789 // another application or whatever), but by handle, so these generic functions
790 // don't make much sense to them.
792 size_t wxBitmapDataObject2::GetDataSize() const
797 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
799 // we put a bitmap handle into pBuf
800 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
805 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
807 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
810 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
812 wxLogLastError(wxT("GetObject(HBITMAP)"));
815 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
816 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
818 if ( !bitmap
.Ok() ) {
819 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
831 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
833 if ( format
.GetFormatId() == CF_DIB
)
838 // shouldn't be selected into a DC or GetDIBits() would fail
839 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
840 wxT("can't copy bitmap selected into wxMemoryDC") );
842 // first get the info
844 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
845 NULL
, &bi
, DIB_RGB_COLORS
) )
847 wxLogLastError(wxT("GetDIBits(NULL)"));
852 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
856 // no data to copy - we don't pass HBITMAP via global memory
861 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
864 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
866 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
867 if ( format
.GetFormatId() == CF_DIB
)
872 // shouldn't be selected into a DC or GetDIBits() would fail
873 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
874 wxT("can't copy bitmap selected into wxMemoryDC") );
876 // first get the info
877 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
878 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
880 wxLogLastError(wxT("GetDIBits(NULL)"));
885 // and now copy the bits
886 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
887 pbi
, DIB_RGB_COLORS
) )
889 wxLogLastError(wxT("GetDIBits"));
896 // we put a bitmap handle into pBuf
897 *(HBITMAP
*)pBuf
= hbmp
;
903 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
904 size_t size
, const void *pBuf
)
907 if ( format
.GetFormatId() == CF_DIB
)
909 // here we get BITMAPINFO struct followed by the actual bitmap bits and
910 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
913 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
914 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
915 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
916 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
919 wxLogLastError(wxT("CreateDIBitmap"));
922 m_bitmap
.SetWidth(pbmih
->biWidth
);
923 m_bitmap
.SetHeight(pbmih
->biHeight
);
927 // it's easy with bitmaps: we pass them by handle
928 hbmp
= *(HBITMAP
*)pBuf
;
931 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
933 wxLogLastError(wxT("GetObject(HBITMAP)"));
936 m_bitmap
.SetWidth(bmp
.bmWidth
);
937 m_bitmap
.SetHeight(bmp
.bmHeight
);
938 m_bitmap
.SetDepth(bmp
.bmPlanes
);
941 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
943 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
950 // ----------------------------------------------------------------------------
952 // ----------------------------------------------------------------------------
954 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
958 // the documentation states that the first member of DROPFILES structure is
959 // a "DWORD offset of double NUL terminated file list". What they mean by
960 // this (I wonder if you see it immediately) is that the list starts at
961 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
962 // to use DragQueryFile to work with this structure, but not told where and
964 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
966 // get number of files (magic value -1)
967 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
969 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
971 // for each file get the length, allocate memory and then get the name
974 for ( n
= 0; n
< nFiles
; n
++ ) {
975 // +1 for terminating NUL
976 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
978 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
980 m_filenames
.Add(str
);
982 if ( len2
!= len
- 1 ) {
983 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
984 %d characters, %d expected."), len2
, len
- 1);
991 void wxFileDataObject::AddFile(const wxString
& file
)
993 // just add file to filenames array
994 // all useful data (such as DROPFILES struct) will be
995 // created later as necessary
996 m_filenames
.Add(file
);
999 size_t wxFileDataObject::GetDataSize() const
1001 // size returned will be the size of the DROPFILES structure,
1002 // plus the list of filesnames (null byte separated), plus
1003 // a double null at the end
1005 // if no filenames in list, size is 0
1006 if ( m_filenames
.GetCount() == 0 )
1009 // inital size of DROPFILES struct + null byte
1010 size_t sz
= sizeof(DROPFILES
) + 1;
1012 size_t count
= m_filenames
.GetCount();
1013 for ( size_t i
= 0; i
< count
; i
++ )
1015 // add filename length plus null byte
1016 sz
+= m_filenames
[i
].Len() + 1;
1022 bool wxFileDataObject::GetDataHere(void *pData
) const
1024 // pData points to an externally allocated memory block
1025 // created using the size returned by GetDataSize()
1027 // if pData is NULL, or there are no files, return
1028 if ( !pData
|| m_filenames
.GetCount() == 0 )
1031 // convert data pointer to a DROPFILES struct pointer
1032 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1034 // initialize DROPFILES struct
1035 pDrop
->pFiles
= sizeof(DROPFILES
);
1036 pDrop
->fNC
= FALSE
; // not non-client coords
1038 pDrop
->fWide
= TRUE
;
1040 pDrop
->fWide
= FALSE
;
1041 #endif // Unicode/Ansi
1043 // set start of filenames list (null separated)
1044 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1046 size_t count
= m_filenames
.GetCount();
1047 for (size_t i
= 0; i
< count
; i
++ )
1049 // copy filename to pbuf and add null terminator
1050 size_t len
= m_filenames
[i
].Len();
1051 memcpy(pbuf
, m_filenames
[i
], len
);
1053 *pbuf
++ = wxT('\0');
1056 // add final null terminator
1062 // ----------------------------------------------------------------------------
1064 // ----------------------------------------------------------------------------
1067 class CFSTR_SHELLURLDataObject
: public wxCustomDataObject
1070 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1072 virtual size_t GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
1077 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1078 const wxDataFormat
& WXUNUSED(format
) )
1080 // CFSTR_SHELLURL is _always_ ANSI text
1081 *size
= strlen( (const char*)buffer
);
1086 virtual void* SetSizeInBuffer( void* buffer
, size_t WXUNUSED(size
),
1087 const wxDataFormat
& WXUNUSED(format
) )
1093 virtual bool GetDataHere( void* buffer
) const
1095 // CFSTR_SHELLURL is _always_ ANSI!
1096 wxCharBuffer
char_buffer( GetDataSize() );
1097 wxCustomDataObject::GetDataHere( (void*)char_buffer
.data() );
1098 wxString
unicode_buffer( char_buffer
);
1099 memcpy( buffer
, unicode_buffer
.c_str(),
1100 ( unicode_buffer
.length() + 1 ) * sizeof(wxChar
) );
1109 wxURLDataObject::wxURLDataObject()
1111 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1112 // but it seems that some browsers only provide one of them so we have to
1114 Add(new wxTextDataObject
);
1115 Add(new CFSTR_SHELLURLDataObject());
1117 // we don't have any data yet
1118 m_dataObjectLast
= NULL
;
1121 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1125 m_dataObjectLast
= GetObject(format
);
1127 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1128 wxT("unsupported format in wxURLDataObject"));
1130 return m_dataObjectLast
->SetData(len
, buf
);
1133 wxString
wxURLDataObject::GetURL() const
1136 wxCHECK_MSG( m_dataObjectLast
, url
, _T("no data in wxURLDataObject") );
1138 size_t len
= m_dataObjectLast
->GetDataSize();
1140 m_dataObjectLast
->GetDataHere(url
.GetWriteBuf(len
));
1141 url
.UngetWriteBuf();
1146 void wxURLDataObject::SetURL(const wxString
& url
)
1148 SetData(wxDataFormat(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
),
1149 url
.Length()+1, url
.c_str());
1151 // CFSTR_SHELLURL is always supposed to be ANSI...
1152 wxWX2MBbuf urlA
= (wxWX2MBbuf
)url
.mbc_str();
1153 size_t len
= strlen(urlA
);
1154 SetData(wxDataFormat(CFSTR_SHELLURL
), len
+1, (const char*)urlA
);
1157 // ----------------------------------------------------------------------------
1158 // private functions
1159 // ----------------------------------------------------------------------------
1161 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1163 switch ( bitsPerPixel
)
1166 // monochrome bitmap, 2 entries
1176 // may be used with 24bit bitmaps, but we don't use it here - fall
1181 // bmiColors not used at all with these bitmaps
1185 wxFAIL_MSG( wxT("unknown bitmap format") );
1190 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1192 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1194 // shouldn't be selected into a DC or GetDIBits() would fail
1195 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1196 wxT("can't copy bitmap selected into wxMemoryDC") );
1198 // prepare all the info we need
1200 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1201 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1203 wxLogLastError(wxT("GetObject(bitmap)"));
1208 // calculate the number of bits per pixel and the number of items in
1209 // bmiColors array (whose meaning depends on the bitmap format)
1210 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1211 WORD biColors
= (WORD
)wxGetNumOfBitmapColors(biBits
);
1215 bool wantSizeOnly
= pbi
== NULL
;
1219 // just for convenience
1220 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1222 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1223 bi
.biWidth
= bm
.bmWidth
;
1224 bi
.biHeight
= bm
.bmHeight
;
1226 bi
.biBitCount
= biBits
;
1227 bi
.biCompression
= BI_RGB
;
1229 bi
.biXPelsPerMeter
= 0;
1230 bi
.biYPelsPerMeter
= 0;
1232 bi
.biClrImportant
= 0;
1234 // memory we need for BITMAPINFO only
1235 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1237 // first get the image size
1239 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1241 wxLogLastError(wxT("GetDIBits(NULL)"));
1248 // size of the header + size of the image
1249 return dwLen
+ bi
.biSizeImage
;
1252 // and now copy the bits
1253 void *image
= (char *)pbi
+ dwLen
;
1254 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1256 wxLogLastError(wxT("GetDIBits"));
1261 return dwLen
+ bi
.biSizeImage
;
1264 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1266 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1267 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1268 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1270 // biClrUsed has the number of colors, unless it's 0
1271 int numColors
= pbmih
->biClrUsed
;
1274 numColors
= wxGetNumOfBitmapColors(pbmih
->biBitCount
);
1277 // offset of image from the beginning of the header
1278 DWORD ofs
= numColors
* sizeof(RGBQUAD
);
1279 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1282 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1283 image
, pbmi
, DIB_RGB_COLORS
);
1286 wxLogLastError(wxT("CreateDIBitmap"));
1289 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1290 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1297 static const wxChar
*GetTymedName(DWORD tymed
)
1299 static wxChar s_szBuf
[128];
1301 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1302 case TYMED_FILE
: return wxT("TYMED_FILE");
1303 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1304 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1305 case TYMED_GDI
: return wxT("TYMED_GDI");
1306 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1307 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1309 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1316 #else // not using OLE at all
1317 // ----------------------------------------------------------------------------
1319 // ----------------------------------------------------------------------------
1321 wxDataObject::wxDataObject()
1325 wxDataObject::~wxDataObject()
1329 void wxDataObject::SetAutoDelete()
1334 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)