]>
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"
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
)
183 m_formats
= new CLIPFORMAT
[nCount
];
184 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
185 m_formats
[n
] = formats
[n
].GetFormatId();
189 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
191 ULONG
*WXUNUSED(pceltFetched
))
193 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
196 // we only return 1 element at a time - mainly because I'm too lazy to
197 // implement something which you're never asked for anyhow
201 if ( m_nCurrent
< m_nCount
) {
203 format
.cfFormat
= m_formats
[m_nCurrent
++];
205 format
.dwAspect
= DVASPECT_CONTENT
;
207 format
.tymed
= TYMED_HGLOBAL
;
218 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
220 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
223 if ( m_nCurrent
< m_nCount
)
226 // no, can't skip this many elements
232 STDMETHODIMP
wxIEnumFORMATETC::Reset()
234 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
241 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
243 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
245 // unfortunately, we can't reuse the code in ctor - types are different
246 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
247 pNew
->m_nCount
= m_nCount
;
248 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
249 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
250 pNew
->m_formats
[n
] = m_formats
[n
];
258 // ----------------------------------------------------------------------------
260 // ----------------------------------------------------------------------------
262 BEGIN_IID_TABLE(wxIDataObject
)
267 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
269 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
272 m_pDataObject
= pDataObject
;
273 m_mustDelete
= FALSE
;
276 wxIDataObject::~wxIDataObject()
280 delete m_pDataObject
;
284 // get data functions
285 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
287 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
289 // is data is in our format?
290 HRESULT hr
= QueryGetData(pformatetcIn
);
294 // for the bitmaps and metafiles we use the handles instead of global memory
296 wxDataFormat format
= (wxDataFormatId
)pformatetcIn
->cfFormat
;
301 pmedium
->tymed
= TYMED_GDI
;
304 case wxDF_ENHMETAFILE
:
305 pmedium
->tymed
= TYMED_ENHMF
;
309 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
310 sizeof(METAFILEPICT
));
311 if ( !pmedium
->hGlobal
) {
312 wxLogLastError(wxT("GlobalAlloc"));
313 return E_OUTOFMEMORY
;
315 pmedium
->tymed
= TYMED_MFPICT
;
320 size_t size
= m_pDataObject
->GetDataSize(format
);
322 // it probably means that the method is just not implemented
323 wxLogDebug(wxT("Invalid data size - can't be 0"));
325 return DV_E_FORMATETC
;
328 if ( !format
.IsStandard() ) {
329 // for custom formats, put the size with the data - alloc the
331 // MB: not completely sure this is correct,
332 // even if I can't figure out what's wrong
333 size
+= m_pDataObject
->GetBufferOffset( format
);
336 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
337 if ( hGlobal
== NULL
) {
338 wxLogLastError(wxT("GlobalAlloc"));
339 return E_OUTOFMEMORY
;
343 pmedium
->tymed
= TYMED_HGLOBAL
;
344 pmedium
->hGlobal
= hGlobal
;
347 pmedium
->pUnkForRelease
= NULL
;
350 hr
= GetDataHere(pformatetcIn
, pmedium
);
352 // free resources we allocated
353 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
354 GlobalFree(pmedium
->hGlobal
);
363 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
366 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
368 // put data in caller provided medium
369 switch ( pmedium
->tymed
)
372 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
377 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
378 &pmedium
->hEnhMetaFile
) )
383 // fall through - we pass METAFILEPICT through HGLOBAL
388 HGLOBAL hGlobal
= pmedium
->hGlobal
;
389 void *pBuf
= GlobalLock(hGlobal
);
390 if ( pBuf
== NULL
) {
391 wxLogLastError(wxT("GlobalLock"));
392 return E_OUTOFMEMORY
;
395 wxDataFormat format
= pformatetc
->cfFormat
;
396 if ( !format
.IsStandard() ) {
397 // for custom formats, put the size with the data
398 pBuf
= m_pDataObject
->SetSizeInBuffer( pBuf
, GlobalSize(hGlobal
), format
);
401 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
404 GlobalUnlock(hGlobal
);
415 // set data functions
416 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
420 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
422 switch ( pmedium
->tymed
)
425 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
429 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
433 // fall through - we pass METAFILEPICT through HGLOBAL
436 wxDataFormat format
= pformatetc
->cfFormat
;
438 // this is quite weird, but for file drag and drop, explorer
439 // calls our SetData() with the formats we do *not* support!
441 // as we can't fix this bug in explorer (it's a bug because it
442 // should only use formats returned by EnumFormatEtc), do the
444 if ( !m_pDataObject
->IsSupportedFormat(format
) ) {
446 return DV_E_FORMATETC
;
450 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
451 if ( pBuf
== NULL
) {
452 wxLogLastError(wxT("GlobalLock"));
454 return E_OUTOFMEMORY
;
457 // we've got a problem with SetData() here because the base
458 // class version requires the size parameter which we don't
459 // have anywhere in OLE data transfer - so we need to
460 // synthetise it for known formats and we suppose that all data
461 // in custom formats starts with a DWORD containing the size
467 size
= strlen((const char *)pBuf
);
469 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
471 #if (defined(__BORLANDC__) && (__BORLANDC__ > 0x530))
472 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
474 size
= ::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
480 // these formats don't use size at all, anyhow (but
481 // pass data by handle, which is always a single DWORD)
486 // the handler will calculate size itself (it's too
487 // complicated to do it here)
491 case CF_METAFILEPICT
:
492 size
= sizeof(METAFILEPICT
);
497 // we suppose that the size precedes the data
498 pBuf
= m_pDataObject
->GetSizeFromBuffer( pBuf
, &size
, format
);
499 if (! format
.IsStandard() ) {
500 // see GetData for coresponding increment
501 size
-= m_pDataObject
->GetBufferOffset( format
);
506 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
508 GlobalUnlock(pmedium
->hGlobal
);
521 // we own the medium, so we must release it - but do *not* free any
522 // data we pass by handle because we have copied it elsewhere
523 switch ( pmedium
->tymed
)
526 pmedium
->hBitmap
= 0;
530 pmedium
->hMetaFilePict
= 0;
534 pmedium
->hEnhMetaFile
= 0;
538 ReleaseStgMedium(pmedium
);
544 // information functions
545 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
547 // do we accept data in this format?
548 if ( pformatetc
== NULL
) {
549 wxLogTrace(wxTRACE_OleCalls
,
550 wxT("wxIDataObject::QueryGetData: invalid ptr."));
555 // the only one allowed by current COM implementation
556 if ( pformatetc
->lindex
!= -1 ) {
557 wxLogTrace(wxTRACE_OleCalls
,
558 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
564 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
565 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
566 wxLogTrace(wxTRACE_OleCalls
,
567 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
568 pformatetc
->dwAspect
);
570 return DV_E_DVASPECT
;
573 // and now check the type of data requested
574 wxDataFormat format
= pformatetc
->cfFormat
;
575 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
576 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
577 wxGetFormatName(format
));
580 wxLogTrace(wxTRACE_OleCalls
,
581 wxT("wxIDataObject::QueryGetData: %s unsupported"),
582 wxGetFormatName(format
));
584 return DV_E_FORMATETC
;
587 // we only transfer data by global memory, except for some particular cases
588 DWORD tymed
= pformatetc
->tymed
;
589 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
590 !(tymed
& TYMED_HGLOBAL
) ) {
591 // it's not what we're waiting for
592 wxLogTrace(wxTRACE_OleCalls
,
593 wxT("wxIDataObject::QueryGetData: %s != %s"),
595 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
604 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
605 FORMATETC
*pFormatetcOut
)
607 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
609 // TODO we might want something better than this trivial implementation here
610 if ( pFormatetcOut
!= NULL
)
611 pFormatetcOut
->ptd
= NULL
;
613 return DATA_S_SAMEFORMATETC
;
616 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
617 IEnumFORMATETC
**ppenumFormatEtc
)
619 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
621 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
624 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
626 wxDataFormat
*formats
;
627 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
628 m_pDataObject
->GetAllFormats(formats
, dir
);
630 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
632 *ppenumFormatEtc
= pEnum
;
634 if ( formats
!= &format
) {
641 // ----------------------------------------------------------------------------
642 // advise sink functions (not implemented)
643 // ----------------------------------------------------------------------------
645 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
646 DWORD
WXUNUSED(advf
),
647 IAdviseSink
*WXUNUSED(pAdvSink
),
648 DWORD
*WXUNUSED(pdwConnection
))
650 return OLE_E_ADVISENOTSUPPORTED
;
653 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
655 return OLE_E_ADVISENOTSUPPORTED
;
658 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
660 return OLE_E_ADVISENOTSUPPORTED
;
663 // ----------------------------------------------------------------------------
665 // ----------------------------------------------------------------------------
667 wxDataObject::wxDataObject()
669 m_pIDataObject
= new wxIDataObject(this);
670 m_pIDataObject
->AddRef();
673 wxDataObject::~wxDataObject()
675 ReleaseInterface(m_pIDataObject
);
678 void wxDataObject::SetAutoDelete()
680 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
681 m_pIDataObject
->Release();
683 // so that the dtor doesnt' crash
684 m_pIDataObject
= NULL
;
687 size_t wxDataObject::GetBufferOffset( const wxDataFormat
& format
)
689 return sizeof(size_t);
692 const void* wxDataObject::GetSizeFromBuffer( const void* buffer
, size_t* size
,
693 const wxDataFormat
& format
)
695 size_t* p
= (size_t*)buffer
;
701 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
702 const wxDataFormat
& format
)
704 size_t* p
= (size_t*)buffer
;
712 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
714 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
716 #pragma warning(disable:4063)
719 static wxChar s_szBuf
[256];
721 case CF_TEXT
: return wxT("CF_TEXT");
722 case CF_BITMAP
: return wxT("CF_BITMAP");
723 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
724 case CF_SYLK
: return wxT("CF_SYLK");
725 case CF_DIF
: return wxT("CF_DIF");
726 case CF_TIFF
: return wxT("CF_TIFF");
727 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
728 case CF_DIB
: return wxT("CF_DIB");
729 case CF_PALETTE
: return wxT("CF_PALETTE");
730 case CF_PENDATA
: return wxT("CF_PENDATA");
731 case CF_RIFF
: return wxT("CF_RIFF");
732 case CF_WAVE
: return wxT("CF_WAVE");
733 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
734 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
735 case CF_HDROP
: return wxT("CF_HDROP");
736 case CF_LOCALE
: return wxT("CF_LOCALE");
739 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
741 // it must be a new predefined format we don't know the name of
742 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
749 #pragma warning(default:4063)
755 // ----------------------------------------------------------------------------
756 // wxBitmapDataObject supports CF_DIB format
757 // ----------------------------------------------------------------------------
759 size_t wxBitmapDataObject::GetDataSize() const
761 return wxConvertBitmapToDIB(NULL
, GetBitmap());
764 bool wxBitmapDataObject::GetDataHere(void *buf
) const
766 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
769 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
771 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
773 if ( !bitmap
.Ok() ) {
774 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
784 // ----------------------------------------------------------------------------
785 // wxBitmapDataObject2 supports CF_BITMAP format
786 // ----------------------------------------------------------------------------
788 // the bitmaps aren't passed by value as other types of data (i.e. by copying
789 // the data into a global memory chunk and passing it to the clipboard or
790 // another application or whatever), but by handle, so these generic functions
791 // don't make much sense to them.
793 size_t wxBitmapDataObject2::GetDataSize() const
798 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
800 // we put a bitmap handle into pBuf
801 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
806 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
808 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
811 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
813 wxLogLastError(wxT("GetObject(HBITMAP)"));
816 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
817 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
819 if ( !bitmap
.Ok() ) {
820 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
832 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
834 if ( format
.GetFormatId() == CF_DIB
)
839 // shouldn't be selected into a DC or GetDIBits() would fail
840 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
841 wxT("can't copy bitmap selected into wxMemoryDC") );
843 // first get the info
845 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
846 NULL
, &bi
, DIB_RGB_COLORS
) )
848 wxLogLastError(wxT("GetDIBits(NULL)"));
853 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
857 // no data to copy - we don't pass HBITMAP via global memory
862 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
865 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
867 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
868 if ( format
.GetFormatId() == CF_DIB
)
873 // shouldn't be selected into a DC or GetDIBits() would fail
874 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
875 wxT("can't copy bitmap selected into wxMemoryDC") );
877 // first get the info
878 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
879 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
881 wxLogLastError(wxT("GetDIBits(NULL)"));
886 // and now copy the bits
887 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
888 pbi
, DIB_RGB_COLORS
) )
890 wxLogLastError(wxT("GetDIBits"));
897 // we put a bitmap handle into pBuf
898 *(HBITMAP
*)pBuf
= hbmp
;
904 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
905 size_t size
, const void *pBuf
)
908 if ( format
.GetFormatId() == CF_DIB
)
910 // here we get BITMAPINFO struct followed by the actual bitmap bits and
911 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
914 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
915 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
916 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
917 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
920 wxLogLastError(wxT("CreateDIBitmap"));
923 m_bitmap
.SetWidth(pbmih
->biWidth
);
924 m_bitmap
.SetHeight(pbmih
->biHeight
);
928 // it's easy with bitmaps: we pass them by handle
929 hbmp
= *(HBITMAP
*)pBuf
;
932 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
934 wxLogLastError(wxT("GetObject(HBITMAP)"));
937 m_bitmap
.SetWidth(bmp
.bmWidth
);
938 m_bitmap
.SetHeight(bmp
.bmHeight
);
939 m_bitmap
.SetDepth(bmp
.bmPlanes
);
942 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
944 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
951 // ----------------------------------------------------------------------------
953 // ----------------------------------------------------------------------------
955 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
959 // the documentation states that the first member of DROPFILES structure is
960 // a "DWORD offset of double NUL terminated file list". What they mean by
961 // this (I wonder if you see it immediately) is that the list starts at
962 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
963 // to use DragQueryFile to work with this structure, but not told where and
965 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
967 // get number of files (magic value -1)
968 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
970 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
972 // for each file get the length, allocate memory and then get the name
975 for ( n
= 0; n
< nFiles
; n
++ ) {
976 // +1 for terminating NUL
977 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
979 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
981 m_filenames
.Add(str
);
983 if ( len2
!= len
- 1 ) {
984 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
985 %d characters, %d expected."), len2
, len
- 1);
992 void wxFileDataObject::AddFile(const wxString
& file
)
994 // just add file to filenames array
995 // all useful data (such as DROPFILES struct) will be
996 // created later as necessary
997 m_filenames
.Add(file
);
1000 size_t wxFileDataObject::GetDataSize() const
1002 // size returned will be the size of the DROPFILES structure,
1003 // plus the list of filesnames (null byte separated), plus
1004 // a double null at the end
1006 // if no filenames in list, size is 0
1007 if ( m_filenames
.GetCount() == 0 )
1010 // inital size of DROPFILES struct + null byte
1011 size_t sz
= sizeof(DROPFILES
) + 1;
1013 size_t count
= m_filenames
.GetCount();
1014 for ( size_t i
= 0; i
< count
; i
++ )
1016 // add filename length plus null byte
1017 sz
+= m_filenames
[i
].Len() + 1;
1023 bool wxFileDataObject::GetDataHere(void *pData
) const
1025 // pData points to an externally allocated memory block
1026 // created using the size returned by GetDataSize()
1028 // if pData is NULL, or there are no files, return
1029 if ( !pData
|| m_filenames
.GetCount() == 0 )
1032 // convert data pointer to a DROPFILES struct pointer
1033 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1035 // initialize DROPFILES struct
1036 pDrop
->pFiles
= sizeof(DROPFILES
);
1037 pDrop
->fNC
= FALSE
; // not non-client coords
1039 pDrop
->fWide
= TRUE
;
1041 pDrop
->fWide
= FALSE
;
1042 #endif // Unicode/Ansi
1044 // set start of filenames list (null separated)
1045 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1047 size_t count
= m_filenames
.GetCount();
1048 for (size_t i
= 0; i
< count
; i
++ )
1050 // copy filename to pbuf and add null terminator
1051 size_t len
= m_filenames
[i
].Len();
1052 memcpy(pbuf
, m_filenames
[i
], len
);
1054 *pbuf
++ = wxT('\0');
1057 // add final null terminator
1063 // ----------------------------------------------------------------------------
1065 // ----------------------------------------------------------------------------
1067 class CFSTR_SHELLURLDataObject
:public wxCustomDataObject
1070 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1072 virtual size_t GetBufferOffset( const wxDataFormat
& format
)
1077 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1078 const wxDataFormat
& format
)
1080 // CFSTR_SHELLURL is _always_ ANSI text
1081 *size
= strlen( (const char*)buffer
);
1086 virtual void* SetSizeInBuffer( void* buffer
, size_t size
,
1087 const wxDataFormat
& format
)
1092 virtual bool GetDataHere( void* buffer
) const
1094 // CFSTR_SHELLURL is _always_ ANSI!
1095 wxCharBuffer
char_buffer( GetDataSize() );
1096 wxCustomDataObject::GetDataHere( (void*)char_buffer
.data() );
1097 wxString
unicode_buffer( char_buffer
);
1098 memcpy( buffer
, unicode_buffer
.c_str(),
1099 ( unicode_buffer
.length() + 1 ) * sizeof(wxChar
) );
1106 wxURLDataObject::wxURLDataObject()
1108 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1109 // but it seems that some browsers only provide one of them so we have to
1111 Add(new CFSTR_SHELLURLDataObject());
1112 Add(new wxTextDataObject
);
1114 // we don't have any data yet
1115 m_dataObjectLast
= NULL
;
1118 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1122 m_dataObjectLast
= GetObject(format
);
1124 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1125 wxT("unsupported format in wxURLDataObject"));
1127 return m_dataObjectLast
->SetData(len
, buf
);
1130 wxString
wxURLDataObject::GetURL() const
1133 wxCHECK_MSG( m_dataObjectLast
, url
, _T("no data in wxURLDataObject") );
1135 size_t len
= m_dataObjectLast
->GetDataSize();
1137 m_dataObjectLast
->GetDataHere(url
.GetWriteBuf(len
+ 1));
1138 url
.UngetWriteBuf();
1143 // ----------------------------------------------------------------------------
1144 // private functions
1145 // ----------------------------------------------------------------------------
1147 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1149 switch ( bitsPerPixel
)
1152 // monochrome bitmap, 2 entries
1162 // may be used with 24bit bitmaps, but we don't use it here - fall
1167 // bmiColors not used at all with these bitmaps
1171 wxFAIL_MSG( wxT("unknown bitmap format") );
1176 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1178 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1180 // shouldn't be selected into a DC or GetDIBits() would fail
1181 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1182 wxT("can't copy bitmap selected into wxMemoryDC") );
1184 // prepare all the info we need
1186 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1187 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1189 wxLogLastError(wxT("GetObject(bitmap)"));
1194 // calculate the number of bits per pixel and the number of items in
1195 // bmiColors array (whose meaning depends on the bitmap format)
1196 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1197 WORD biColors
= (WORD
)wxGetNumOfBitmapColors(biBits
);
1201 bool wantSizeOnly
= pbi
== NULL
;
1205 // just for convenience
1206 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1208 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1209 bi
.biWidth
= bm
.bmWidth
;
1210 bi
.biHeight
= bm
.bmHeight
;
1212 bi
.biBitCount
= biBits
;
1213 bi
.biCompression
= BI_RGB
;
1215 bi
.biXPelsPerMeter
= 0;
1216 bi
.biYPelsPerMeter
= 0;
1218 bi
.biClrImportant
= 0;
1220 // memory we need for BITMAPINFO only
1221 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1223 // first get the image size
1225 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1227 wxLogLastError(wxT("GetDIBits(NULL)"));
1234 // size of the header + size of the image
1235 return dwLen
+ bi
.biSizeImage
;
1238 // and now copy the bits
1239 void *image
= (char *)pbi
+ dwLen
;
1240 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1242 wxLogLastError(wxT("GetDIBits"));
1247 return dwLen
+ bi
.biSizeImage
;
1250 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1252 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1253 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1254 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1256 // offset of image from the beginning of the header
1257 DWORD ofs
= wxGetNumOfBitmapColors(pbmih
->biBitCount
) * sizeof(RGBQUAD
);
1258 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1261 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1262 image
, pbmi
, DIB_RGB_COLORS
);
1265 wxLogLastError(wxT("CreateDIBitmap"));
1268 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1269 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1276 static const wxChar
*GetTymedName(DWORD tymed
)
1278 static wxChar s_szBuf
[128];
1280 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1281 case TYMED_FILE
: return wxT("TYMED_FILE");
1282 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1283 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1284 case TYMED_GDI
: return wxT("TYMED_GDI");
1285 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1286 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1288 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1295 #else // not using OLE at all
1296 // ----------------------------------------------------------------------------
1298 // ----------------------------------------------------------------------------
1300 wxDataObject::wxDataObject()
1304 wxDataObject::~wxDataObject()
1308 void wxDataObject::SetAutoDelete()
1313 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)