]>
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__)
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"
59 #define LPDROPFILES DROPFILES*
62 #ifndef CFSTR_SHELLURL
63 #define CFSTR_SHELLURL _T("UniformResourceLocator")
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
71 static const wxChar
*GetTymedName(DWORD tymed
);
73 #define GetTymedName(tymed) _T("")
74 #endif // Debug/!Debug
76 // ----------------------------------------------------------------------------
77 // wxIEnumFORMATETC interface implementation
78 // ----------------------------------------------------------------------------
80 class wxIEnumFORMATETC
: public IEnumFORMATETC
83 wxIEnumFORMATETC(const wxDataFormat
* formats
, ULONG nCount
);
84 virtual ~wxIEnumFORMATETC() { delete [] m_formats
; }
86 DECLARE_IUNKNOWN_METHODS
;
89 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
90 STDMETHODIMP
Skip(ULONG celt
);
92 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
95 CLIPFORMAT
*m_formats
; // formats we can provide data in
96 ULONG m_nCount
, // number of formats we support
97 m_nCurrent
; // current enum position
99 DECLARE_NO_COPY_CLASS(wxIEnumFORMATETC
)
102 // ----------------------------------------------------------------------------
103 // wxIDataObject implementation of IDataObject interface
104 // ----------------------------------------------------------------------------
106 class wxIDataObject
: public IDataObject
109 wxIDataObject(wxDataObject
*pDataObject
);
110 virtual ~wxIDataObject();
112 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
113 // is), but in some cases, the situation is inversed, that is we delete it
114 // when this object is deleted - setting this flag enables such logic
115 void SetDeleteFlag() { m_mustDelete
= TRUE
; }
117 DECLARE_IUNKNOWN_METHODS
;
120 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
121 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
122 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
123 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
124 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
125 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
126 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
127 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
128 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
131 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
135 DECLARE_NO_COPY_CLASS(wxIDataObject
)
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 void wxDataFormat::SetId(const wxChar
*format
)
148 m_format
= (wxDataFormat::NativeFormat
)::RegisterClipboardFormat(format
);
151 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
155 wxString
wxDataFormat::GetId() const
157 static const int max
= 256;
161 wxCHECK_MSG( !IsStandard(), s
,
162 wxT("name of predefined format cannot be retrieved") );
164 int len
= ::GetClipboardFormatName(m_format
, s
.GetWriteBuf(max
), max
);
169 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
181 ADD_IID(EnumFORMATETC
)
184 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
186 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
190 m_formats
= new CLIPFORMAT
[nCount
];
191 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
192 m_formats
[n
] = formats
[n
].GetFormatId();
196 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
198 ULONG
*WXUNUSED(pceltFetched
))
200 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
203 // we only return 1 element at a time - mainly because I'm too lazy to
204 // implement something which you're never asked for anyhow
208 if ( m_nCurrent
< m_nCount
) {
210 format
.cfFormat
= m_formats
[m_nCurrent
++];
212 format
.dwAspect
= DVASPECT_CONTENT
;
214 format
.tymed
= TYMED_HGLOBAL
;
225 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
227 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
230 if ( m_nCurrent
< m_nCount
)
233 // no, can't skip this many elements
239 STDMETHODIMP
wxIEnumFORMATETC::Reset()
241 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
248 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
250 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
252 // unfortunately, we can't reuse the code in ctor - types are different
253 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
254 pNew
->m_nCount
= m_nCount
;
255 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
256 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
257 pNew
->m_formats
[n
] = m_formats
[n
];
265 // ----------------------------------------------------------------------------
267 // ----------------------------------------------------------------------------
269 BEGIN_IID_TABLE(wxIDataObject
)
274 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
276 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
278 m_pDataObject
= pDataObject
;
279 m_mustDelete
= FALSE
;
282 wxIDataObject::~wxIDataObject()
286 delete m_pDataObject
;
290 // get data functions
291 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
293 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
295 // is data is in our format?
296 HRESULT hr
= QueryGetData(pformatetcIn
);
300 // for the bitmaps and metafiles we use the handles instead of global memory
302 wxDataFormat format
= (wxDataFormat::NativeFormat
)pformatetcIn
->cfFormat
;
307 pmedium
->tymed
= TYMED_GDI
;
310 case wxDF_ENHMETAFILE
:
311 pmedium
->tymed
= TYMED_ENHMF
;
315 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
316 sizeof(METAFILEPICT
));
317 if ( !pmedium
->hGlobal
) {
318 wxLogLastError(wxT("GlobalAlloc"));
319 return E_OUTOFMEMORY
;
321 pmedium
->tymed
= TYMED_MFPICT
;
326 size_t size
= m_pDataObject
->GetDataSize(format
);
328 // it probably means that the method is just not implemented
329 wxLogDebug(wxT("Invalid data size - can't be 0"));
331 return DV_E_FORMATETC
;
334 if ( !format
.IsStandard() ) {
335 // for custom formats, put the size with the data - alloc the
337 // MB: not completely sure this is correct,
338 // even if I can't figure out what's wrong
339 size
+= m_pDataObject
->GetBufferOffset( format
);
342 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
343 if ( hGlobal
== NULL
) {
344 wxLogLastError(wxT("GlobalAlloc"));
345 return E_OUTOFMEMORY
;
349 pmedium
->tymed
= TYMED_HGLOBAL
;
350 pmedium
->hGlobal
= hGlobal
;
353 pmedium
->pUnkForRelease
= NULL
;
356 hr
= GetDataHere(pformatetcIn
, pmedium
);
358 // free resources we allocated
359 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
360 GlobalFree(pmedium
->hGlobal
);
369 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
372 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
374 // put data in caller provided medium
375 switch ( pmedium
->tymed
)
378 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
383 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
384 &pmedium
->hEnhMetaFile
) )
389 // fall through - we pass METAFILEPICT through HGLOBAL
394 HGLOBAL hGlobal
= pmedium
->hGlobal
;
395 void *pBuf
= GlobalLock(hGlobal
);
396 if ( pBuf
== NULL
) {
397 wxLogLastError(wxT("GlobalLock"));
398 return E_OUTOFMEMORY
;
401 wxDataFormat format
= pformatetc
->cfFormat
;
402 if ( !format
.IsStandard() ) {
403 // for custom formats, put the size with the data
404 pBuf
= m_pDataObject
->SetSizeInBuffer( pBuf
, GlobalSize(hGlobal
), format
);
407 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
410 GlobalUnlock(hGlobal
);
421 // set data functions
422 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
426 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
428 switch ( pmedium
->tymed
)
431 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
435 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
439 // fall through - we pass METAFILEPICT through HGLOBAL
442 wxDataFormat format
= pformatetc
->cfFormat
;
444 // this is quite weird, but for file drag and drop, explorer
445 // calls our SetData() with the formats we do *not* support!
447 // as we can't fix this bug in explorer (it's a bug because it
448 // should only use formats returned by EnumFormatEtc), do the
450 if ( !m_pDataObject
->IsSupportedFormat(format
) ) {
452 return DV_E_FORMATETC
;
456 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
457 if ( pBuf
== NULL
) {
458 wxLogLastError(wxT("GlobalLock"));
460 return E_OUTOFMEMORY
;
463 // we've got a problem with SetData() here because the base
464 // class version requires the size parameter which we don't
465 // have anywhere in OLE data transfer - so we need to
466 // synthetise it for known formats and we suppose that all data
467 // in custom formats starts with a DWORD containing the size
473 size
= strlen((const char *)pBuf
);
475 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
477 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
478 || ( defined(__MWERKS__) && defined(__WXMSW__) )
479 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
481 size
= wxWcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
487 // these formats don't use size at all, anyhow (but
488 // pass data by handle, which is always a single DWORD)
493 // the handler will calculate size itself (it's too
494 // complicated to do it here)
498 case CF_METAFILEPICT
:
499 size
= sizeof(METAFILEPICT
);
504 // we suppose that the size precedes the data
505 pBuf
= m_pDataObject
->GetSizeFromBuffer( pBuf
, &size
, format
);
506 if (! format
.IsStandard() ) {
507 // see GetData for coresponding increment
508 size
-= m_pDataObject
->GetBufferOffset( format
);
513 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
515 GlobalUnlock(pmedium
->hGlobal
);
528 // we own the medium, so we must release it - but do *not* free any
529 // data we pass by handle because we have copied it elsewhere
530 switch ( pmedium
->tymed
)
533 pmedium
->hBitmap
= 0;
537 pmedium
->hMetaFilePict
= 0;
541 pmedium
->hEnhMetaFile
= 0;
545 ReleaseStgMedium(pmedium
);
551 // information functions
552 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
554 // do we accept data in this format?
555 if ( pformatetc
== NULL
) {
556 wxLogTrace(wxTRACE_OleCalls
,
557 wxT("wxIDataObject::QueryGetData: invalid ptr."));
562 // the only one allowed by current COM implementation
563 if ( pformatetc
->lindex
!= -1 ) {
564 wxLogTrace(wxTRACE_OleCalls
,
565 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
571 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
572 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
573 wxLogTrace(wxTRACE_OleCalls
,
574 wxT("wxIDataObject::QueryGetData: bad dwAspect %ld"),
575 pformatetc
->dwAspect
);
577 return DV_E_DVASPECT
;
580 // and now check the type of data requested
581 wxDataFormat format
= pformatetc
->cfFormat
;
582 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
583 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
584 wxGetFormatName(format
));
587 wxLogTrace(wxTRACE_OleCalls
,
588 wxT("wxIDataObject::QueryGetData: %s unsupported"),
589 wxGetFormatName(format
));
591 return DV_E_FORMATETC
;
594 // we only transfer data by global memory, except for some particular cases
595 DWORD tymed
= pformatetc
->tymed
;
596 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
597 !(tymed
& TYMED_HGLOBAL
) ) {
598 // it's not what we're waiting for
599 wxLogTrace(wxTRACE_OleCalls
,
600 wxT("wxIDataObject::QueryGetData: %s != %s"),
602 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
611 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
612 FORMATETC
*pFormatetcOut
)
614 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
616 // TODO we might want something better than this trivial implementation here
617 if ( pFormatetcOut
!= NULL
)
618 pFormatetcOut
->ptd
= NULL
;
620 return DATA_S_SAMEFORMATETC
;
623 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
624 IEnumFORMATETC
**ppenumFormatEtc
)
626 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
628 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
631 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
633 wxDataFormat
*formats
;
634 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
635 m_pDataObject
->GetAllFormats(formats
, dir
);
637 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
639 *ppenumFormatEtc
= pEnum
;
641 if ( formats
!= &format
) {
648 // ----------------------------------------------------------------------------
649 // advise sink functions (not implemented)
650 // ----------------------------------------------------------------------------
652 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
653 DWORD
WXUNUSED(advf
),
654 IAdviseSink
*WXUNUSED(pAdvSink
),
655 DWORD
*WXUNUSED(pdwConnection
))
657 return OLE_E_ADVISENOTSUPPORTED
;
660 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
662 return OLE_E_ADVISENOTSUPPORTED
;
665 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
667 return OLE_E_ADVISENOTSUPPORTED
;
670 // ----------------------------------------------------------------------------
672 // ----------------------------------------------------------------------------
674 wxDataObject::wxDataObject()
676 m_pIDataObject
= new wxIDataObject(this);
677 m_pIDataObject
->AddRef();
680 wxDataObject::~wxDataObject()
682 ReleaseInterface(m_pIDataObject
);
685 void wxDataObject::SetAutoDelete()
687 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
688 m_pIDataObject
->Release();
690 // so that the dtor doesnt' crash
691 m_pIDataObject
= NULL
;
694 size_t wxDataObject::GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
696 return sizeof(size_t);
699 const void* wxDataObject::GetSizeFromBuffer( const void* buffer
, size_t* size
,
700 const wxDataFormat
& WXUNUSED(format
) )
702 size_t* p
= (size_t*)buffer
;
708 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
709 const wxDataFormat
& WXUNUSED(format
) )
711 size_t* p
= (size_t*)buffer
;
719 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
721 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
723 #pragma warning(disable:4063)
726 static wxChar s_szBuf
[256];
728 case CF_TEXT
: return wxT("CF_TEXT");
729 case CF_BITMAP
: return wxT("CF_BITMAP");
730 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
731 case CF_SYLK
: return wxT("CF_SYLK");
732 case CF_DIF
: return wxT("CF_DIF");
733 case CF_TIFF
: return wxT("CF_TIFF");
734 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
735 case CF_DIB
: return wxT("CF_DIB");
736 case CF_PALETTE
: return wxT("CF_PALETTE");
737 case CF_PENDATA
: return wxT("CF_PENDATA");
738 case CF_RIFF
: return wxT("CF_RIFF");
739 case CF_WAVE
: return wxT("CF_WAVE");
740 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
741 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
742 case CF_HDROP
: return wxT("CF_HDROP");
743 case CF_LOCALE
: return wxT("CF_LOCALE");
746 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
748 // it must be a new predefined format we don't know the name of
749 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
756 #pragma warning(default:4063)
762 // ----------------------------------------------------------------------------
763 // wxBitmapDataObject supports CF_DIB format
764 // ----------------------------------------------------------------------------
766 size_t wxBitmapDataObject::GetDataSize() const
768 return wxConvertBitmapToDIB(NULL
, GetBitmap());
771 bool wxBitmapDataObject::GetDataHere(void *buf
) const
773 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
776 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
778 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
780 if ( !bitmap
.Ok() ) {
781 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
791 // ----------------------------------------------------------------------------
792 // wxBitmapDataObject2 supports CF_BITMAP format
793 // ----------------------------------------------------------------------------
795 // the bitmaps aren't passed by value as other types of data (i.e. by copying
796 // the data into a global memory chunk and passing it to the clipboard or
797 // another application or whatever), but by handle, so these generic functions
798 // don't make much sense to them.
800 size_t wxBitmapDataObject2::GetDataSize() const
805 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
807 // we put a bitmap handle into pBuf
808 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
813 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
815 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
818 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
820 wxLogLastError(wxT("GetObject(HBITMAP)"));
823 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
824 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
826 if ( !bitmap
.Ok() ) {
827 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
839 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
841 if ( format
.GetFormatId() == CF_DIB
)
846 // shouldn't be selected into a DC or GetDIBits() would fail
847 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
848 wxT("can't copy bitmap selected into wxMemoryDC") );
850 // first get the info
852 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
853 NULL
, &bi
, DIB_RGB_COLORS
) )
855 wxLogLastError(wxT("GetDIBits(NULL)"));
860 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
864 // no data to copy - we don't pass HBITMAP via global memory
869 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
872 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
874 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
875 if ( format
.GetFormatId() == CF_DIB
)
880 // shouldn't be selected into a DC or GetDIBits() would fail
881 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
882 wxT("can't copy bitmap selected into wxMemoryDC") );
884 // first get the info
885 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
886 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
888 wxLogLastError(wxT("GetDIBits(NULL)"));
893 // and now copy the bits
894 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
895 pbi
, DIB_RGB_COLORS
) )
897 wxLogLastError(wxT("GetDIBits"));
904 // we put a bitmap handle into pBuf
905 *(HBITMAP
*)pBuf
= hbmp
;
911 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
912 size_t size
, const void *pBuf
)
915 if ( format
.GetFormatId() == CF_DIB
)
917 // here we get BITMAPINFO struct followed by the actual bitmap bits and
918 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
921 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
922 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
923 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
924 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
927 wxLogLastError(wxT("CreateDIBitmap"));
930 m_bitmap
.SetWidth(pbmih
->biWidth
);
931 m_bitmap
.SetHeight(pbmih
->biHeight
);
935 // it's easy with bitmaps: we pass them by handle
936 hbmp
= *(HBITMAP
*)pBuf
;
939 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
941 wxLogLastError(wxT("GetObject(HBITMAP)"));
944 m_bitmap
.SetWidth(bmp
.bmWidth
);
945 m_bitmap
.SetHeight(bmp
.bmHeight
);
946 m_bitmap
.SetDepth(bmp
.bmPlanes
);
949 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
951 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
958 // ----------------------------------------------------------------------------
960 // ----------------------------------------------------------------------------
962 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
966 // the documentation states that the first member of DROPFILES structure is
967 // a "DWORD offset of double NUL terminated file list". What they mean by
968 // this (I wonder if you see it immediately) is that the list starts at
969 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
970 // to use DragQueryFile to work with this structure, but not told where and
972 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
974 // get number of files (magic value -1)
975 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
977 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
979 // for each file get the length, allocate memory and then get the name
982 for ( n
= 0; n
< nFiles
; n
++ ) {
983 // +1 for terminating NUL
984 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
986 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
988 m_filenames
.Add(str
);
990 if ( len2
!= len
- 1 ) {
991 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
992 %d characters, %d expected."), len2
, len
- 1);
999 void wxFileDataObject::AddFile(const wxString
& file
)
1001 // just add file to filenames array
1002 // all useful data (such as DROPFILES struct) will be
1003 // created later as necessary
1004 m_filenames
.Add(file
);
1007 size_t wxFileDataObject::GetDataSize() const
1009 // size returned will be the size of the DROPFILES structure,
1010 // plus the list of filesnames (null byte separated), plus
1011 // a double null at the end
1013 // if no filenames in list, size is 0
1014 if ( m_filenames
.GetCount() == 0 )
1017 // inital size of DROPFILES struct + null byte
1018 size_t sz
= sizeof(DROPFILES
) + 1;
1020 size_t count
= m_filenames
.GetCount();
1021 for ( size_t i
= 0; i
< count
; i
++ )
1023 // add filename length plus null byte
1024 sz
+= m_filenames
[i
].Len() + 1;
1030 bool wxFileDataObject::GetDataHere(void *pData
) const
1032 // pData points to an externally allocated memory block
1033 // created using the size returned by GetDataSize()
1035 // if pData is NULL, or there are no files, return
1036 if ( !pData
|| m_filenames
.GetCount() == 0 )
1039 // convert data pointer to a DROPFILES struct pointer
1040 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1042 // initialize DROPFILES struct
1043 pDrop
->pFiles
= sizeof(DROPFILES
);
1044 pDrop
->fNC
= FALSE
; // not non-client coords
1046 pDrop
->fWide
= TRUE
;
1048 pDrop
->fWide
= FALSE
;
1049 #endif // Unicode/Ansi
1051 // set start of filenames list (null separated)
1052 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1054 size_t count
= m_filenames
.GetCount();
1055 for (size_t i
= 0; i
< count
; i
++ )
1057 // copy filename to pbuf and add null terminator
1058 size_t len
= m_filenames
[i
].Len();
1059 memcpy(pbuf
, m_filenames
[i
], len
);
1061 *pbuf
++ = wxT('\0');
1064 // add final null terminator
1070 // ----------------------------------------------------------------------------
1072 // ----------------------------------------------------------------------------
1074 class CFSTR_SHELLURLDataObject
: public wxCustomDataObject
1077 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1079 virtual size_t GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
1084 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1085 const wxDataFormat
& WXUNUSED(format
) )
1087 // CFSTR_SHELLURL is _always_ ANSI text
1088 *size
= strlen( (const char*)buffer
);
1093 virtual void* SetSizeInBuffer( void* buffer
, size_t WXUNUSED(size
),
1094 const wxDataFormat
& WXUNUSED(format
) )
1100 virtual bool GetDataHere( void* buffer
) const
1102 // CFSTR_SHELLURL is _always_ ANSI!
1103 wxCharBuffer
char_buffer( GetDataSize() );
1104 wxCustomDataObject::GetDataHere( (void*)char_buffer
.data() );
1105 wxString
unicode_buffer( char_buffer
, wxConvLibc
);
1106 memcpy( buffer
, unicode_buffer
.c_str(),
1107 ( unicode_buffer
.length() + 1 ) * sizeof(wxChar
) );
1116 wxURLDataObject::wxURLDataObject()
1118 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1119 // but it seems that some browsers only provide one of them so we have to
1121 Add(new wxTextDataObject
);
1122 Add(new CFSTR_SHELLURLDataObject());
1124 // we don't have any data yet
1125 m_dataObjectLast
= NULL
;
1128 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1132 m_dataObjectLast
= GetObject(format
);
1134 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1135 wxT("unsupported format in wxURLDataObject"));
1137 return m_dataObjectLast
->SetData(len
, buf
);
1140 wxString
wxURLDataObject::GetURL() const
1143 wxCHECK_MSG( m_dataObjectLast
, url
, _T("no data in wxURLDataObject") );
1145 size_t len
= m_dataObjectLast
->GetDataSize();
1147 m_dataObjectLast
->GetDataHere(url
.GetWriteBuf(len
));
1148 url
.UngetWriteBuf();
1153 void wxURLDataObject::SetURL(const wxString
& url
)
1155 SetData(wxDataFormat(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
),
1156 url
.Length()+1, url
.c_str());
1158 // CFSTR_SHELLURL is always supposed to be ANSI...
1159 wxWX2MBbuf urlA
= (wxWX2MBbuf
)url
.mbc_str();
1160 size_t len
= strlen(urlA
);
1161 SetData(wxDataFormat(CFSTR_SHELLURL
), len
+1, (const char*)urlA
);
1164 // ----------------------------------------------------------------------------
1165 // private functions
1166 // ----------------------------------------------------------------------------
1168 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1170 switch ( bitsPerPixel
)
1173 // monochrome bitmap, 2 entries
1183 // may be used with 24bit bitmaps, but we don't use it here - fall
1188 // bmiColors not used at all with these bitmaps
1192 wxFAIL_MSG( wxT("unknown bitmap format") );
1197 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1199 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1201 // shouldn't be selected into a DC or GetDIBits() would fail
1202 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1203 wxT("can't copy bitmap selected into wxMemoryDC") );
1205 // prepare all the info we need
1207 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1208 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1210 wxLogLastError(wxT("GetObject(bitmap)"));
1215 // calculate the number of bits per pixel and the number of items in
1216 // bmiColors array (whose meaning depends on the bitmap format)
1217 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1218 WORD biColors
= (WORD
)wxGetNumOfBitmapColors(biBits
);
1222 bool wantSizeOnly
= pbi
== NULL
;
1226 // just for convenience
1227 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1229 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1230 bi
.biWidth
= bm
.bmWidth
;
1231 bi
.biHeight
= bm
.bmHeight
;
1233 bi
.biBitCount
= biBits
;
1234 bi
.biCompression
= BI_RGB
;
1236 bi
.biXPelsPerMeter
= 0;
1237 bi
.biYPelsPerMeter
= 0;
1239 bi
.biClrImportant
= 0;
1241 // memory we need for BITMAPINFO only
1242 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1244 // first get the image size
1246 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1248 wxLogLastError(wxT("GetDIBits(NULL)"));
1255 // size of the header + size of the image
1256 return dwLen
+ bi
.biSizeImage
;
1259 // and now copy the bits
1260 void *image
= (char *)pbi
+ dwLen
;
1261 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1263 wxLogLastError(wxT("GetDIBits"));
1268 return dwLen
+ bi
.biSizeImage
;
1271 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1273 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1274 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1275 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1277 // biClrUsed has the number of colors, unless it's 0
1278 int numColors
= pbmih
->biClrUsed
;
1281 numColors
= wxGetNumOfBitmapColors(pbmih
->biBitCount
);
1284 // offset of image from the beginning of the header
1285 DWORD ofs
= numColors
* sizeof(RGBQUAD
);
1286 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1289 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1290 image
, pbmi
, DIB_RGB_COLORS
);
1293 wxLogLastError(wxT("CreateDIBitmap"));
1296 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1297 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1304 static const wxChar
*GetTymedName(DWORD tymed
)
1306 static wxChar s_szBuf
[128];
1308 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1309 case TYMED_FILE
: return wxT("TYMED_FILE");
1310 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1311 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1312 case TYMED_GDI
: return wxT("TYMED_GDI");
1313 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1314 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1316 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1323 #else // not using OLE at all
1324 // ----------------------------------------------------------------------------
1326 // ----------------------------------------------------------------------------
1328 wxDataObject::wxDataObject()
1332 wxDataObject::~wxDataObject()
1336 void wxDataObject::SetAutoDelete()
1341 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)