]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
9e7b0363d15babf4de340b87745a23119863d0e9
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 licence
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 // for some compilers, the entire ole2.h must be included, not only oleauto.h
43 #if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__)
50 #include "wx/msw/ole/oleutils.h"
52 #include "wx/msw/dib.h"
54 #ifndef CFSTR_SHELLURL
55 #define CFSTR_SHELLURL _T("UniformResourceLocator")
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
63 static const wxChar
*GetTymedName(DWORD tymed
);
65 #define GetTymedName(tymed) wxEmptyString
66 #endif // Debug/!Debug
68 // ----------------------------------------------------------------------------
69 // wxIEnumFORMATETC interface implementation
70 // ----------------------------------------------------------------------------
72 class wxIEnumFORMATETC
: public IEnumFORMATETC
75 wxIEnumFORMATETC(const wxDataFormat
* formats
, ULONG nCount
);
76 virtual ~wxIEnumFORMATETC() { delete [] m_formats
; }
78 DECLARE_IUNKNOWN_METHODS
;
81 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
82 STDMETHODIMP
Skip(ULONG celt
);
84 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
87 CLIPFORMAT
*m_formats
; // formats we can provide data in
88 ULONG m_nCount
, // number of formats we support
89 m_nCurrent
; // current enum position
91 DECLARE_NO_COPY_CLASS(wxIEnumFORMATETC
)
94 // ----------------------------------------------------------------------------
95 // wxIDataObject implementation of IDataObject interface
96 // ----------------------------------------------------------------------------
98 class wxIDataObject
: public IDataObject
101 wxIDataObject(wxDataObject
*pDataObject
);
102 virtual ~wxIDataObject();
104 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
105 // is), but in some cases, the situation is inversed, that is we delete it
106 // when this object is deleted - setting this flag enables such logic
107 void SetDeleteFlag() { m_mustDelete
= TRUE
; }
109 DECLARE_IUNKNOWN_METHODS
;
112 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
113 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
114 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
115 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
116 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
117 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
118 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
119 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
120 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
123 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
127 DECLARE_NO_COPY_CLASS(wxIDataObject
)
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
,
192 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
194 ULONG numFetched
= 0;
195 while (m_nCurrent
< m_nCount
&& numFetched
< celt
) {
197 format
.cfFormat
= m_formats
[m_nCurrent
++];
199 format
.dwAspect
= DVASPECT_CONTENT
;
201 format
.tymed
= TYMED_HGLOBAL
;
208 *pceltFetched
= numFetched
;
210 return numFetched
== celt
? S_OK
: S_FALSE
;
213 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
215 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
218 if ( m_nCurrent
< m_nCount
)
221 // no, can't skip this many elements
227 STDMETHODIMP
wxIEnumFORMATETC::Reset()
229 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
236 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
238 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
240 // unfortunately, we can't reuse the code in ctor - types are different
241 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
242 pNew
->m_nCount
= m_nCount
;
243 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
244 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
245 pNew
->m_formats
[n
] = m_formats
[n
];
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 BEGIN_IID_TABLE(wxIDataObject
)
262 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
264 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
266 m_pDataObject
= pDataObject
;
267 m_mustDelete
= FALSE
;
270 wxIDataObject::~wxIDataObject()
274 delete m_pDataObject
;
278 // get data functions
279 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
281 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
283 // is data is in our format?
284 HRESULT hr
= QueryGetData(pformatetcIn
);
288 // for the bitmaps and metafiles we use the handles instead of global memory
290 wxDataFormat format
= (wxDataFormat::NativeFormat
)pformatetcIn
->cfFormat
;
295 pmedium
->tymed
= TYMED_GDI
;
298 case wxDF_ENHMETAFILE
:
299 pmedium
->tymed
= TYMED_ENHMF
;
303 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
304 sizeof(METAFILEPICT
));
305 if ( !pmedium
->hGlobal
) {
306 wxLogLastError(wxT("GlobalAlloc"));
307 return E_OUTOFMEMORY
;
309 pmedium
->tymed
= TYMED_MFPICT
;
314 size_t size
= m_pDataObject
->GetDataSize(format
);
316 // it probably means that the method is just not implemented
317 wxLogDebug(wxT("Invalid data size - can't be 0"));
319 return DV_E_FORMATETC
;
322 if ( !format
.IsStandard() ) {
323 // for custom formats, put the size with the data - alloc the
325 // MB: not completely sure this is correct,
326 // even if I can't figure out what's wrong
327 size
+= m_pDataObject
->GetBufferOffset( format
);
330 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
331 if ( hGlobal
== NULL
) {
332 wxLogLastError(wxT("GlobalAlloc"));
333 return E_OUTOFMEMORY
;
337 pmedium
->tymed
= TYMED_HGLOBAL
;
338 pmedium
->hGlobal
= hGlobal
;
341 pmedium
->pUnkForRelease
= NULL
;
344 hr
= GetDataHere(pformatetcIn
, pmedium
);
346 // free resources we allocated
347 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
348 GlobalFree(pmedium
->hGlobal
);
357 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
360 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
362 // put data in caller provided medium
363 switch ( pmedium
->tymed
)
366 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
371 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
372 &pmedium
->hEnhMetaFile
) )
377 // fall through - we pass METAFILEPICT through HGLOBAL
382 HGLOBAL hGlobal
= pmedium
->hGlobal
;
383 void *pBuf
= GlobalLock(hGlobal
);
384 if ( pBuf
== NULL
) {
385 wxLogLastError(wxT("GlobalLock"));
386 return E_OUTOFMEMORY
;
389 wxDataFormat format
= pformatetc
->cfFormat
;
390 if ( !format
.IsStandard() ) {
391 // for custom formats, put the size with the data
392 pBuf
= m_pDataObject
->SetSizeInBuffer( pBuf
, GlobalSize(hGlobal
), format
);
395 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
398 GlobalUnlock(hGlobal
);
410 // set data functions
411 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
415 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
417 switch ( pmedium
->tymed
)
420 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
424 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
428 // fall through - we pass METAFILEPICT through HGLOBAL
431 wxDataFormat format
= pformatetc
->cfFormat
;
433 // this is quite weird, but for file drag and drop, explorer
434 // calls our SetData() with the formats we do *not* support!
436 // as we can't fix this bug in explorer (it's a bug because it
437 // should only use formats returned by EnumFormatEtc), do the
439 if ( !m_pDataObject
->IsSupported(format
, wxDataObject::Set
) ) {
441 return DV_E_FORMATETC
;
445 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
446 if ( pBuf
== NULL
) {
447 wxLogLastError(wxT("GlobalLock"));
449 return E_OUTOFMEMORY
;
452 // we've got a problem with SetData() here because the base
453 // class version requires the size parameter which we don't
454 // have anywhere in OLE data transfer - so we need to
455 // synthetise it for known formats and we suppose that all data
456 // in custom formats starts with a DWORD containing the size
462 size
= strlen((const char *)pBuf
);
464 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
466 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
467 || ( defined(__MWERKS__) && defined(__WXMSW__) )
468 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
470 size
= wxWcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
476 // these formats don't use size at all, anyhow (but
477 // pass data by handle, which is always a single DWORD)
482 // the handler will calculate size itself (it's too
483 // complicated to do it here)
487 case CF_METAFILEPICT
:
488 size
= sizeof(METAFILEPICT
);
493 // we suppose that the size precedes the data
494 pBuf
= m_pDataObject
->GetSizeFromBuffer( pBuf
, &size
, format
);
495 if (! format
.IsStandard() ) {
496 // see GetData for coresponding increment
497 size
-= m_pDataObject
->GetBufferOffset( format
);
502 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
504 GlobalUnlock(pmedium
->hGlobal
);
517 // we own the medium, so we must release it - but do *not* free any
518 // data we pass by handle because we have copied it elsewhere
519 switch ( pmedium
->tymed
)
522 pmedium
->hBitmap
= 0;
526 pmedium
->hMetaFilePict
= 0;
530 pmedium
->hEnhMetaFile
= 0;
534 ReleaseStgMedium(pmedium
);
540 // information functions
541 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
543 // do we accept data in this format?
544 if ( pformatetc
== NULL
) {
545 wxLogTrace(wxTRACE_OleCalls
,
546 wxT("wxIDataObject::QueryGetData: invalid ptr."));
551 // the only one allowed by current COM implementation
552 if ( pformatetc
->lindex
!= -1 ) {
553 wxLogTrace(wxTRACE_OleCalls
,
554 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
560 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
561 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
562 wxLogTrace(wxTRACE_OleCalls
,
563 wxT("wxIDataObject::QueryGetData: bad dwAspect %ld"),
564 pformatetc
->dwAspect
);
566 return DV_E_DVASPECT
;
569 // and now check the type of data requested
570 wxDataFormat format
= pformatetc
->cfFormat
;
571 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
572 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
573 wxGetFormatName(format
));
576 wxLogTrace(wxTRACE_OleCalls
,
577 wxT("wxIDataObject::QueryGetData: %s unsupported"),
578 wxGetFormatName(format
));
580 return DV_E_FORMATETC
;
583 // we only transfer data by global memory, except for some particular cases
584 DWORD tymed
= pformatetc
->tymed
;
585 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
586 !(tymed
& TYMED_HGLOBAL
) ) {
587 // it's not what we're waiting for
588 wxLogTrace(wxTRACE_OleCalls
,
589 wxT("wxIDataObject::QueryGetData: %s != %s"),
591 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
600 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
601 FORMATETC
*pFormatetcOut
)
603 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
605 // TODO we might want something better than this trivial implementation here
606 if ( pFormatetcOut
!= NULL
)
607 pFormatetcOut
->ptd
= NULL
;
609 return DATA_S_SAMEFORMATETC
;
612 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
613 IEnumFORMATETC
**ppenumFormatEtc
)
615 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
617 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
620 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
622 wxDataFormat
*formats
;
623 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
624 m_pDataObject
->GetAllFormats(formats
, dir
);
626 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
628 *ppenumFormatEtc
= pEnum
;
630 if ( formats
!= &format
) {
637 // ----------------------------------------------------------------------------
638 // advise sink functions (not implemented)
639 // ----------------------------------------------------------------------------
641 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
642 DWORD
WXUNUSED(advf
),
643 IAdviseSink
*WXUNUSED(pAdvSink
),
644 DWORD
*WXUNUSED(pdwConnection
))
646 return OLE_E_ADVISENOTSUPPORTED
;
649 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
651 return OLE_E_ADVISENOTSUPPORTED
;
654 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
656 return OLE_E_ADVISENOTSUPPORTED
;
659 // ----------------------------------------------------------------------------
661 // ----------------------------------------------------------------------------
663 wxDataObject::wxDataObject()
665 m_pIDataObject
= new wxIDataObject(this);
666 m_pIDataObject
->AddRef();
669 wxDataObject::~wxDataObject()
671 ReleaseInterface(m_pIDataObject
);
674 void wxDataObject::SetAutoDelete()
676 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
677 m_pIDataObject
->Release();
679 // so that the dtor doesnt' crash
680 m_pIDataObject
= NULL
;
683 size_t wxDataObject::GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
685 return sizeof(size_t);
688 const void* wxDataObject::GetSizeFromBuffer( const void* buffer
, size_t* size
,
689 const wxDataFormat
& WXUNUSED(format
) )
691 size_t* p
= (size_t*)buffer
;
697 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
698 const wxDataFormat
& WXUNUSED(format
) )
700 size_t* p
= (size_t*)buffer
;
708 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
710 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
712 #pragma warning(disable:4063)
715 static wxChar s_szBuf
[256];
717 case CF_TEXT
: return wxT("CF_TEXT");
718 case CF_BITMAP
: return wxT("CF_BITMAP");
719 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
720 case CF_SYLK
: return wxT("CF_SYLK");
721 case CF_DIF
: return wxT("CF_DIF");
722 case CF_TIFF
: return wxT("CF_TIFF");
723 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
724 case CF_DIB
: return wxT("CF_DIB");
725 case CF_PALETTE
: return wxT("CF_PALETTE");
726 case CF_PENDATA
: return wxT("CF_PENDATA");
727 case CF_RIFF
: return wxT("CF_RIFF");
728 case CF_WAVE
: return wxT("CF_WAVE");
729 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
730 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
731 case CF_HDROP
: return wxT("CF_HDROP");
732 case CF_LOCALE
: return wxT("CF_LOCALE");
735 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
737 // it must be a new predefined format we don't know the name of
738 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
745 #pragma warning(default:4063)
751 // ----------------------------------------------------------------------------
752 // wxBitmapDataObject supports CF_DIB format
753 // ----------------------------------------------------------------------------
755 size_t wxBitmapDataObject::GetDataSize() const
757 return wxDIB::ConvertFromBitmap(NULL
, GetHbitmapOf(GetBitmap()));
760 bool wxBitmapDataObject::GetDataHere(void *buf
) const
762 BITMAPINFO
* const pbi
= (BITMAPINFO
*)buf
;
764 return wxDIB::ConvertFromBitmap(pbi
, GetHbitmapOf(GetBitmap())) != 0;
767 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
769 const BITMAPINFO
* const pbmi
= (const BITMAPINFO
*)buf
;
771 HBITMAP hbmp
= wxDIB::ConvertToBitmap(pbmi
);
773 wxCHECK_MSG( hbmp
, FALSE
, wxT("pasting/dropping invalid bitmap") );
775 const BITMAPINFOHEADER
* const pbmih
= &pbmi
->bmiHeader
;
776 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
777 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
779 // TODO: create wxPalette if the bitmap has any
786 // ----------------------------------------------------------------------------
787 // wxBitmapDataObject2 supports CF_BITMAP format
788 // ----------------------------------------------------------------------------
790 // the bitmaps aren't passed by value as other types of data (i.e. by copying
791 // the data into a global memory chunk and passing it to the clipboard or
792 // another application or whatever), but by handle, so these generic functions
793 // don't make much sense to them.
795 size_t wxBitmapDataObject2::GetDataSize() const
800 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
802 // we put a bitmap handle into pBuf
803 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
808 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
810 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
813 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
815 wxLogLastError(wxT("GetObject(HBITMAP)"));
818 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
819 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
821 if ( !bitmap
.Ok() ) {
822 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
834 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
836 if ( format
.GetFormatId() == CF_DIB
)
841 // shouldn't be selected into a DC or GetDIBits() would fail
842 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
843 wxT("can't copy bitmap selected into wxMemoryDC") );
845 // first get the info
847 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
848 NULL
, &bi
, DIB_RGB_COLORS
) )
850 wxLogLastError(wxT("GetDIBits(NULL)"));
855 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
859 // no data to copy - we don't pass HBITMAP via global memory
864 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
867 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
869 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
870 if ( format
.GetFormatId() == CF_DIB
)
875 // shouldn't be selected into a DC or GetDIBits() would fail
876 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
877 wxT("can't copy bitmap selected into wxMemoryDC") );
879 // first get the info
880 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
881 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
883 wxLogLastError(wxT("GetDIBits(NULL)"));
888 // and now copy the bits
889 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
890 pbi
, DIB_RGB_COLORS
) )
892 wxLogLastError(wxT("GetDIBits"));
899 // we put a bitmap handle into pBuf
900 *(HBITMAP
*)pBuf
= hbmp
;
906 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
907 size_t size
, const void *pBuf
)
910 if ( format
.GetFormatId() == CF_DIB
)
912 // here we get BITMAPINFO struct followed by the actual bitmap bits and
913 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
916 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
917 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
918 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
919 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
922 wxLogLastError(wxT("CreateDIBitmap"));
925 m_bitmap
.SetWidth(pbmih
->biWidth
);
926 m_bitmap
.SetHeight(pbmih
->biHeight
);
930 // it's easy with bitmaps: we pass them by handle
931 hbmp
= *(HBITMAP
*)pBuf
;
934 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
936 wxLogLastError(wxT("GetObject(HBITMAP)"));
939 m_bitmap
.SetWidth(bmp
.bmWidth
);
940 m_bitmap
.SetHeight(bmp
.bmHeight
);
941 m_bitmap
.SetDepth(bmp
.bmPlanes
);
944 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
946 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
953 // ----------------------------------------------------------------------------
955 // ----------------------------------------------------------------------------
957 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
961 // the documentation states that the first member of DROPFILES structure is
962 // a "DWORD offset of double NUL terminated file list". What they mean by
963 // this (I wonder if you see it immediately) is that the list starts at
964 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
965 // to use DragQueryFile to work with this structure, but not told where and
967 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
969 // get number of files (magic value -1)
970 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
972 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
974 // for each file get the length, allocate memory and then get the name
977 for ( n
= 0; n
< nFiles
; n
++ ) {
978 // +1 for terminating NUL
979 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
981 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
983 m_filenames
.Add(str
);
985 if ( len2
!= len
- 1 ) {
986 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
987 %d characters, %d expected."), len2
, len
- 1);
994 void wxFileDataObject::AddFile(const wxString
& file
)
996 // just add file to filenames array
997 // all useful data (such as DROPFILES struct) will be
998 // created later as necessary
999 m_filenames
.Add(file
);
1002 size_t wxFileDataObject::GetDataSize() const
1004 // size returned will be the size of the DROPFILES structure,
1005 // plus the list of filesnames (null byte separated), plus
1006 // a double null at the end
1008 // if no filenames in list, size is 0
1009 if ( m_filenames
.GetCount() == 0 )
1012 // inital size of DROPFILES struct + null byte
1013 size_t sz
= sizeof(DROPFILES
) + 1;
1015 size_t count
= m_filenames
.GetCount();
1016 for ( size_t i
= 0; i
< count
; i
++ )
1018 // add filename length plus null byte
1019 sz
+= m_filenames
[i
].Len() + 1;
1025 bool wxFileDataObject::GetDataHere(void *pData
) const
1027 // pData points to an externally allocated memory block
1028 // created using the size returned by GetDataSize()
1030 // if pData is NULL, or there are no files, return
1031 if ( !pData
|| m_filenames
.GetCount() == 0 )
1034 // convert data pointer to a DROPFILES struct pointer
1035 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1037 // initialize DROPFILES struct
1038 pDrop
->pFiles
= sizeof(DROPFILES
);
1039 pDrop
->fNC
= FALSE
; // not non-client coords
1041 pDrop
->fWide
= TRUE
;
1043 pDrop
->fWide
= FALSE
;
1044 #endif // Unicode/Ansi
1046 // set start of filenames list (null separated)
1047 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1049 size_t count
= m_filenames
.GetCount();
1050 for (size_t i
= 0; i
< count
; i
++ )
1052 // copy filename to pbuf and add null terminator
1053 size_t len
= m_filenames
[i
].Len();
1054 memcpy(pbuf
, m_filenames
[i
], len
);
1056 *pbuf
++ = wxT('\0');
1059 // add final null terminator
1065 // ----------------------------------------------------------------------------
1067 // ----------------------------------------------------------------------------
1069 class CFSTR_SHELLURLDataObject
: public wxCustomDataObject
1072 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1074 virtual size_t GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
1079 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1080 const wxDataFormat
& WXUNUSED(format
) )
1082 // CFSTR_SHELLURL is _always_ ANSI text
1083 *size
= strlen( (const char*)buffer
);
1088 virtual void* SetSizeInBuffer( void* buffer
, size_t WXUNUSED(size
),
1089 const wxDataFormat
& WXUNUSED(format
) )
1095 virtual bool GetDataHere( void* buffer
) const
1097 // CFSTR_SHELLURL is _always_ ANSI!
1098 wxCharBuffer
char_buffer( GetDataSize() );
1099 wxCustomDataObject::GetDataHere( (void*)char_buffer
.data() );
1100 wxString
unicode_buffer( char_buffer
, wxConvLibc
);
1101 memcpy( buffer
, unicode_buffer
.c_str(),
1102 ( unicode_buffer
.length() + 1 ) * sizeof(wxChar
) );
1111 wxURLDataObject::wxURLDataObject()
1113 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1114 // but it seems that some browsers only provide one of them so we have to
1116 Add(new wxTextDataObject
);
1117 Add(new CFSTR_SHELLURLDataObject());
1119 // we don't have any data yet
1120 m_dataObjectLast
= NULL
;
1123 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1127 m_dataObjectLast
= GetObject(format
);
1129 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1130 wxT("unsupported format in wxURLDataObject"));
1132 return m_dataObjectLast
->SetData(len
, buf
);
1135 wxString
wxURLDataObject::GetURL() const
1138 wxCHECK_MSG( m_dataObjectLast
, url
, _T("no data in wxURLDataObject") );
1140 size_t len
= m_dataObjectLast
->GetDataSize();
1142 m_dataObjectLast
->GetDataHere(url
.GetWriteBuf(len
));
1143 url
.UngetWriteBuf();
1148 void wxURLDataObject::SetURL(const wxString
& url
)
1150 SetData(wxDataFormat(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
),
1151 url
.Length()+1, url
.c_str());
1153 // CFSTR_SHELLURL is always supposed to be ANSI...
1154 wxWX2MBbuf urlA
= (wxWX2MBbuf
)url
.mbc_str();
1155 size_t len
= strlen(urlA
);
1156 SetData(wxDataFormat(CFSTR_SHELLURL
), len
+1, (const char*)urlA
);
1159 // ----------------------------------------------------------------------------
1160 // private functions
1161 // ----------------------------------------------------------------------------
1165 static const wxChar
*GetTymedName(DWORD tymed
)
1167 static wxChar s_szBuf
[128];
1169 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1170 case TYMED_FILE
: return wxT("TYMED_FILE");
1171 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1172 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1173 case TYMED_GDI
: return wxT("TYMED_GDI");
1174 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1175 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1177 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1184 #else // not using OLE at all
1185 // ----------------------------------------------------------------------------
1187 // ----------------------------------------------------------------------------
1189 wxDataObject::wxDataObject()
1193 wxDataObject::~wxDataObject()
1197 void wxDataObject::SetAutoDelete()
1202 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)