]>
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
100 // ----------------------------------------------------------------------------
101 // wxIDataObject implementation of IDataObject interface
102 // ----------------------------------------------------------------------------
104 class wxIDataObject
: public IDataObject
107 wxIDataObject(wxDataObject
*pDataObject
);
108 virtual ~wxIDataObject();
110 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
111 // is), but in some cases, the situation is inversed, that is we delete it
112 // when this object is deleted - setting this flag enables such logic
113 void SetDeleteFlag() { m_mustDelete
= TRUE
; }
115 DECLARE_IUNKNOWN_METHODS
;
118 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
119 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
120 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
121 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
122 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
123 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
124 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
125 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
126 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
129 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
134 // ============================================================================
136 // ============================================================================
138 // ----------------------------------------------------------------------------
140 // ----------------------------------------------------------------------------
142 void wxDataFormat::SetId(const wxChar
*format
)
144 m_format
= (wxDataFormat::NativeFormat
)::RegisterClipboardFormat(format
);
147 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
151 wxString
wxDataFormat::GetId() const
153 static const int max
= 256;
157 wxCHECK_MSG( !IsStandard(), s
,
158 wxT("name of predefined format cannot be retrieved") );
160 int len
= ::GetClipboardFormatName(m_format
, s
.GetWriteBuf(max
), max
);
165 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
177 ADD_IID(EnumFORMATETC
)
180 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
182 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
186 m_formats
= new CLIPFORMAT
[nCount
];
187 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
188 m_formats
[n
] = formats
[n
].GetFormatId();
192 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
194 ULONG
*WXUNUSED(pceltFetched
))
196 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
199 // we only return 1 element at a time - mainly because I'm too lazy to
200 // implement something which you're never asked for anyhow
204 if ( m_nCurrent
< m_nCount
) {
206 format
.cfFormat
= m_formats
[m_nCurrent
++];
208 format
.dwAspect
= DVASPECT_CONTENT
;
210 format
.tymed
= TYMED_HGLOBAL
;
221 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
223 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
226 if ( m_nCurrent
< m_nCount
)
229 // no, can't skip this many elements
235 STDMETHODIMP
wxIEnumFORMATETC::Reset()
237 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
244 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
246 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
248 // unfortunately, we can't reuse the code in ctor - types are different
249 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
250 pNew
->m_nCount
= m_nCount
;
251 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
252 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
253 pNew
->m_formats
[n
] = m_formats
[n
];
261 // ----------------------------------------------------------------------------
263 // ----------------------------------------------------------------------------
265 BEGIN_IID_TABLE(wxIDataObject
)
270 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
272 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
274 m_pDataObject
= pDataObject
;
275 m_mustDelete
= FALSE
;
278 wxIDataObject::~wxIDataObject()
282 delete m_pDataObject
;
286 // get data functions
287 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
289 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
291 // is data is in our format?
292 HRESULT hr
= QueryGetData(pformatetcIn
);
296 // for the bitmaps and metafiles we use the handles instead of global memory
298 wxDataFormat format
= (wxDataFormat::NativeFormat
)pformatetcIn
->cfFormat
;
303 pmedium
->tymed
= TYMED_GDI
;
306 case wxDF_ENHMETAFILE
:
307 pmedium
->tymed
= TYMED_ENHMF
;
311 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
312 sizeof(METAFILEPICT
));
313 if ( !pmedium
->hGlobal
) {
314 wxLogLastError(wxT("GlobalAlloc"));
315 return E_OUTOFMEMORY
;
317 pmedium
->tymed
= TYMED_MFPICT
;
322 size_t size
= m_pDataObject
->GetDataSize(format
);
324 // it probably means that the method is just not implemented
325 wxLogDebug(wxT("Invalid data size - can't be 0"));
327 return DV_E_FORMATETC
;
330 if ( !format
.IsStandard() ) {
331 // for custom formats, put the size with the data - alloc the
333 // MB: not completely sure this is correct,
334 // even if I can't figure out what's wrong
335 size
+= m_pDataObject
->GetBufferOffset( format
);
338 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
339 if ( hGlobal
== NULL
) {
340 wxLogLastError(wxT("GlobalAlloc"));
341 return E_OUTOFMEMORY
;
345 pmedium
->tymed
= TYMED_HGLOBAL
;
346 pmedium
->hGlobal
= hGlobal
;
349 pmedium
->pUnkForRelease
= NULL
;
352 hr
= GetDataHere(pformatetcIn
, pmedium
);
354 // free resources we allocated
355 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
356 GlobalFree(pmedium
->hGlobal
);
365 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
368 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
370 // put data in caller provided medium
371 switch ( pmedium
->tymed
)
374 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
379 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
380 &pmedium
->hEnhMetaFile
) )
385 // fall through - we pass METAFILEPICT through HGLOBAL
390 HGLOBAL hGlobal
= pmedium
->hGlobal
;
391 void *pBuf
= GlobalLock(hGlobal
);
392 if ( pBuf
== NULL
) {
393 wxLogLastError(wxT("GlobalLock"));
394 return E_OUTOFMEMORY
;
397 wxDataFormat format
= pformatetc
->cfFormat
;
398 if ( !format
.IsStandard() ) {
399 // for custom formats, put the size with the data
400 pBuf
= m_pDataObject
->SetSizeInBuffer( pBuf
, GlobalSize(hGlobal
), format
);
403 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
406 GlobalUnlock(hGlobal
);
417 // set data functions
418 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
422 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
424 switch ( pmedium
->tymed
)
427 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
431 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
435 // fall through - we pass METAFILEPICT through HGLOBAL
438 wxDataFormat format
= pformatetc
->cfFormat
;
440 // this is quite weird, but for file drag and drop, explorer
441 // calls our SetData() with the formats we do *not* support!
443 // as we can't fix this bug in explorer (it's a bug because it
444 // should only use formats returned by EnumFormatEtc), do the
446 if ( !m_pDataObject
->IsSupportedFormat(format
) ) {
448 return DV_E_FORMATETC
;
452 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
453 if ( pBuf
== NULL
) {
454 wxLogLastError(wxT("GlobalLock"));
456 return E_OUTOFMEMORY
;
459 // we've got a problem with SetData() here because the base
460 // class version requires the size parameter which we don't
461 // have anywhere in OLE data transfer - so we need to
462 // synthetise it for known formats and we suppose that all data
463 // in custom formats starts with a DWORD containing the size
469 size
= strlen((const char *)pBuf
);
471 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
473 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
474 || ( defined(__MWERKS__) && defined(__WXMSW__) )
475 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
477 size
= ::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
483 // these formats don't use size at all, anyhow (but
484 // pass data by handle, which is always a single DWORD)
489 // the handler will calculate size itself (it's too
490 // complicated to do it here)
494 case CF_METAFILEPICT
:
495 size
= sizeof(METAFILEPICT
);
500 // we suppose that the size precedes the data
501 pBuf
= m_pDataObject
->GetSizeFromBuffer( pBuf
, &size
, format
);
502 if (! format
.IsStandard() ) {
503 // see GetData for coresponding increment
504 size
-= m_pDataObject
->GetBufferOffset( format
);
509 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
511 GlobalUnlock(pmedium
->hGlobal
);
524 // we own the medium, so we must release it - but do *not* free any
525 // data we pass by handle because we have copied it elsewhere
526 switch ( pmedium
->tymed
)
529 pmedium
->hBitmap
= 0;
533 pmedium
->hMetaFilePict
= 0;
537 pmedium
->hEnhMetaFile
= 0;
541 ReleaseStgMedium(pmedium
);
547 // information functions
548 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
550 // do we accept data in this format?
551 if ( pformatetc
== NULL
) {
552 wxLogTrace(wxTRACE_OleCalls
,
553 wxT("wxIDataObject::QueryGetData: invalid ptr."));
558 // the only one allowed by current COM implementation
559 if ( pformatetc
->lindex
!= -1 ) {
560 wxLogTrace(wxTRACE_OleCalls
,
561 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
567 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
568 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
569 wxLogTrace(wxTRACE_OleCalls
,
570 wxT("wxIDataObject::QueryGetData: bad dwAspect %ld"),
571 pformatetc
->dwAspect
);
573 return DV_E_DVASPECT
;
576 // and now check the type of data requested
577 wxDataFormat format
= pformatetc
->cfFormat
;
578 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
579 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
580 wxGetFormatName(format
));
583 wxLogTrace(wxTRACE_OleCalls
,
584 wxT("wxIDataObject::QueryGetData: %s unsupported"),
585 wxGetFormatName(format
));
587 return DV_E_FORMATETC
;
590 // we only transfer data by global memory, except for some particular cases
591 DWORD tymed
= pformatetc
->tymed
;
592 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
593 !(tymed
& TYMED_HGLOBAL
) ) {
594 // it's not what we're waiting for
595 wxLogTrace(wxTRACE_OleCalls
,
596 wxT("wxIDataObject::QueryGetData: %s != %s"),
598 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
607 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
608 FORMATETC
*pFormatetcOut
)
610 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
612 // TODO we might want something better than this trivial implementation here
613 if ( pFormatetcOut
!= NULL
)
614 pFormatetcOut
->ptd
= NULL
;
616 return DATA_S_SAMEFORMATETC
;
619 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
620 IEnumFORMATETC
**ppenumFormatEtc
)
622 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
624 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
627 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
629 wxDataFormat
*formats
;
630 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
631 m_pDataObject
->GetAllFormats(formats
, dir
);
633 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
635 *ppenumFormatEtc
= pEnum
;
637 if ( formats
!= &format
) {
644 // ----------------------------------------------------------------------------
645 // advise sink functions (not implemented)
646 // ----------------------------------------------------------------------------
648 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
649 DWORD
WXUNUSED(advf
),
650 IAdviseSink
*WXUNUSED(pAdvSink
),
651 DWORD
*WXUNUSED(pdwConnection
))
653 return OLE_E_ADVISENOTSUPPORTED
;
656 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
658 return OLE_E_ADVISENOTSUPPORTED
;
661 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
663 return OLE_E_ADVISENOTSUPPORTED
;
666 // ----------------------------------------------------------------------------
668 // ----------------------------------------------------------------------------
670 wxDataObject::wxDataObject()
672 m_pIDataObject
= new wxIDataObject(this);
673 m_pIDataObject
->AddRef();
676 wxDataObject::~wxDataObject()
678 ReleaseInterface(m_pIDataObject
);
681 void wxDataObject::SetAutoDelete()
683 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
684 m_pIDataObject
->Release();
686 // so that the dtor doesnt' crash
687 m_pIDataObject
= NULL
;
690 size_t wxDataObject::GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
692 return sizeof(size_t);
695 const void* wxDataObject::GetSizeFromBuffer( const void* buffer
, size_t* size
,
696 const wxDataFormat
& WXUNUSED(format
) )
698 size_t* p
= (size_t*)buffer
;
704 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
705 const wxDataFormat
& WXUNUSED(format
) )
707 size_t* p
= (size_t*)buffer
;
715 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
717 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
719 #pragma warning(disable:4063)
722 static wxChar s_szBuf
[256];
724 case CF_TEXT
: return wxT("CF_TEXT");
725 case CF_BITMAP
: return wxT("CF_BITMAP");
726 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
727 case CF_SYLK
: return wxT("CF_SYLK");
728 case CF_DIF
: return wxT("CF_DIF");
729 case CF_TIFF
: return wxT("CF_TIFF");
730 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
731 case CF_DIB
: return wxT("CF_DIB");
732 case CF_PALETTE
: return wxT("CF_PALETTE");
733 case CF_PENDATA
: return wxT("CF_PENDATA");
734 case CF_RIFF
: return wxT("CF_RIFF");
735 case CF_WAVE
: return wxT("CF_WAVE");
736 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
737 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
738 case CF_HDROP
: return wxT("CF_HDROP");
739 case CF_LOCALE
: return wxT("CF_LOCALE");
742 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
744 // it must be a new predefined format we don't know the name of
745 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
752 #pragma warning(default:4063)
758 // ----------------------------------------------------------------------------
759 // wxBitmapDataObject supports CF_DIB format
760 // ----------------------------------------------------------------------------
762 size_t wxBitmapDataObject::GetDataSize() const
764 return wxConvertBitmapToDIB(NULL
, GetBitmap());
767 bool wxBitmapDataObject::GetDataHere(void *buf
) const
769 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
772 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
774 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
776 if ( !bitmap
.Ok() ) {
777 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
787 // ----------------------------------------------------------------------------
788 // wxBitmapDataObject2 supports CF_BITMAP format
789 // ----------------------------------------------------------------------------
791 // the bitmaps aren't passed by value as other types of data (i.e. by copying
792 // the data into a global memory chunk and passing it to the clipboard or
793 // another application or whatever), but by handle, so these generic functions
794 // don't make much sense to them.
796 size_t wxBitmapDataObject2::GetDataSize() const
801 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
803 // we put a bitmap handle into pBuf
804 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
809 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
811 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
814 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
816 wxLogLastError(wxT("GetObject(HBITMAP)"));
819 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
820 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
822 if ( !bitmap
.Ok() ) {
823 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
835 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
837 if ( format
.GetFormatId() == CF_DIB
)
842 // shouldn't be selected into a DC or GetDIBits() would fail
843 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
844 wxT("can't copy bitmap selected into wxMemoryDC") );
846 // first get the info
848 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
849 NULL
, &bi
, DIB_RGB_COLORS
) )
851 wxLogLastError(wxT("GetDIBits(NULL)"));
856 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
860 // no data to copy - we don't pass HBITMAP via global memory
865 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
868 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
870 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
871 if ( format
.GetFormatId() == CF_DIB
)
876 // shouldn't be selected into a DC or GetDIBits() would fail
877 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
878 wxT("can't copy bitmap selected into wxMemoryDC") );
880 // first get the info
881 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
882 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
884 wxLogLastError(wxT("GetDIBits(NULL)"));
889 // and now copy the bits
890 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
891 pbi
, DIB_RGB_COLORS
) )
893 wxLogLastError(wxT("GetDIBits"));
900 // we put a bitmap handle into pBuf
901 *(HBITMAP
*)pBuf
= hbmp
;
907 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
908 size_t size
, const void *pBuf
)
911 if ( format
.GetFormatId() == CF_DIB
)
913 // here we get BITMAPINFO struct followed by the actual bitmap bits and
914 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
917 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
918 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
919 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
920 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
923 wxLogLastError(wxT("CreateDIBitmap"));
926 m_bitmap
.SetWidth(pbmih
->biWidth
);
927 m_bitmap
.SetHeight(pbmih
->biHeight
);
931 // it's easy with bitmaps: we pass them by handle
932 hbmp
= *(HBITMAP
*)pBuf
;
935 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
937 wxLogLastError(wxT("GetObject(HBITMAP)"));
940 m_bitmap
.SetWidth(bmp
.bmWidth
);
941 m_bitmap
.SetHeight(bmp
.bmHeight
);
942 m_bitmap
.SetDepth(bmp
.bmPlanes
);
945 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
947 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
954 // ----------------------------------------------------------------------------
956 // ----------------------------------------------------------------------------
958 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
962 // the documentation states that the first member of DROPFILES structure is
963 // a "DWORD offset of double NUL terminated file list". What they mean by
964 // this (I wonder if you see it immediately) is that the list starts at
965 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
966 // to use DragQueryFile to work with this structure, but not told where and
968 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
970 // get number of files (magic value -1)
971 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
973 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
975 // for each file get the length, allocate memory and then get the name
978 for ( n
= 0; n
< nFiles
; n
++ ) {
979 // +1 for terminating NUL
980 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
982 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
984 m_filenames
.Add(str
);
986 if ( len2
!= len
- 1 ) {
987 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
988 %d characters, %d expected."), len2
, len
- 1);
995 void wxFileDataObject::AddFile(const wxString
& file
)
997 // just add file to filenames array
998 // all useful data (such as DROPFILES struct) will be
999 // created later as necessary
1000 m_filenames
.Add(file
);
1003 size_t wxFileDataObject::GetDataSize() const
1005 // size returned will be the size of the DROPFILES structure,
1006 // plus the list of filesnames (null byte separated), plus
1007 // a double null at the end
1009 // if no filenames in list, size is 0
1010 if ( m_filenames
.GetCount() == 0 )
1013 // inital size of DROPFILES struct + null byte
1014 size_t sz
= sizeof(DROPFILES
) + 1;
1016 size_t count
= m_filenames
.GetCount();
1017 for ( size_t i
= 0; i
< count
; i
++ )
1019 // add filename length plus null byte
1020 sz
+= m_filenames
[i
].Len() + 1;
1026 bool wxFileDataObject::GetDataHere(void *pData
) const
1028 // pData points to an externally allocated memory block
1029 // created using the size returned by GetDataSize()
1031 // if pData is NULL, or there are no files, return
1032 if ( !pData
|| m_filenames
.GetCount() == 0 )
1035 // convert data pointer to a DROPFILES struct pointer
1036 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1038 // initialize DROPFILES struct
1039 pDrop
->pFiles
= sizeof(DROPFILES
);
1040 pDrop
->fNC
= FALSE
; // not non-client coords
1042 pDrop
->fWide
= TRUE
;
1044 pDrop
->fWide
= FALSE
;
1045 #endif // Unicode/Ansi
1047 // set start of filenames list (null separated)
1048 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1050 size_t count
= m_filenames
.GetCount();
1051 for (size_t i
= 0; i
< count
; i
++ )
1053 // copy filename to pbuf and add null terminator
1054 size_t len
= m_filenames
[i
].Len();
1055 memcpy(pbuf
, m_filenames
[i
], len
);
1057 *pbuf
++ = wxT('\0');
1060 // add final null terminator
1066 // ----------------------------------------------------------------------------
1068 // ----------------------------------------------------------------------------
1071 class CFSTR_SHELLURLDataObject
: public wxCustomDataObject
1074 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1076 virtual size_t GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
1081 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1082 const wxDataFormat
& WXUNUSED(format
) )
1084 // CFSTR_SHELLURL is _always_ ANSI text
1085 *size
= strlen( (const char*)buffer
);
1090 virtual void* SetSizeInBuffer( void* buffer
, size_t WXUNUSED(size
),
1091 const wxDataFormat
& WXUNUSED(format
) )
1097 virtual bool GetDataHere( void* buffer
) const
1099 // CFSTR_SHELLURL is _always_ ANSI!
1100 wxCharBuffer
char_buffer( GetDataSize() );
1101 wxCustomDataObject::GetDataHere( (void*)char_buffer
.data() );
1102 wxString
unicode_buffer( char_buffer
);
1103 memcpy( buffer
, unicode_buffer
.c_str(),
1104 ( unicode_buffer
.length() + 1 ) * sizeof(wxChar
) );
1113 wxURLDataObject::wxURLDataObject()
1115 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1116 // but it seems that some browsers only provide one of them so we have to
1118 Add(new wxTextDataObject
);
1119 Add(new CFSTR_SHELLURLDataObject());
1121 // we don't have any data yet
1122 m_dataObjectLast
= NULL
;
1125 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1129 m_dataObjectLast
= GetObject(format
);
1131 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1132 wxT("unsupported format in wxURLDataObject"));
1134 return m_dataObjectLast
->SetData(len
, buf
);
1137 wxString
wxURLDataObject::GetURL() const
1140 wxCHECK_MSG( m_dataObjectLast
, url
, _T("no data in wxURLDataObject") );
1142 size_t len
= m_dataObjectLast
->GetDataSize();
1144 m_dataObjectLast
->GetDataHere(url
.GetWriteBuf(len
));
1145 url
.UngetWriteBuf();
1150 void wxURLDataObject::SetURL(const wxString
& url
)
1152 SetData(wxDataFormat(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
),
1153 url
.Length()+1, url
.c_str());
1155 // CFSTR_SHELLURL is always supposed to be ANSI...
1156 wxWX2MBbuf urlA
= (wxWX2MBbuf
)url
.mbc_str();
1157 size_t len
= strlen(urlA
);
1158 SetData(wxDataFormat(CFSTR_SHELLURL
), len
+1, (const char*)urlA
);
1161 // ----------------------------------------------------------------------------
1162 // private functions
1163 // ----------------------------------------------------------------------------
1165 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1167 switch ( bitsPerPixel
)
1170 // monochrome bitmap, 2 entries
1180 // may be used with 24bit bitmaps, but we don't use it here - fall
1185 // bmiColors not used at all with these bitmaps
1189 wxFAIL_MSG( wxT("unknown bitmap format") );
1194 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1196 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1198 // shouldn't be selected into a DC or GetDIBits() would fail
1199 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1200 wxT("can't copy bitmap selected into wxMemoryDC") );
1202 // prepare all the info we need
1204 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1205 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1207 wxLogLastError(wxT("GetObject(bitmap)"));
1212 // calculate the number of bits per pixel and the number of items in
1213 // bmiColors array (whose meaning depends on the bitmap format)
1214 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1215 WORD biColors
= (WORD
)wxGetNumOfBitmapColors(biBits
);
1219 bool wantSizeOnly
= pbi
== NULL
;
1223 // just for convenience
1224 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1226 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1227 bi
.biWidth
= bm
.bmWidth
;
1228 bi
.biHeight
= bm
.bmHeight
;
1230 bi
.biBitCount
= biBits
;
1231 bi
.biCompression
= BI_RGB
;
1233 bi
.biXPelsPerMeter
= 0;
1234 bi
.biYPelsPerMeter
= 0;
1236 bi
.biClrImportant
= 0;
1238 // memory we need for BITMAPINFO only
1239 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1241 // first get the image size
1243 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1245 wxLogLastError(wxT("GetDIBits(NULL)"));
1252 // size of the header + size of the image
1253 return dwLen
+ bi
.biSizeImage
;
1256 // and now copy the bits
1257 void *image
= (char *)pbi
+ dwLen
;
1258 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1260 wxLogLastError(wxT("GetDIBits"));
1265 return dwLen
+ bi
.biSizeImage
;
1268 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1270 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1271 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1272 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1274 // biClrUsed has the number of colors, unless it's 0
1275 int numColors
= pbmih
->biClrUsed
;
1278 numColors
= wxGetNumOfBitmapColors(pbmih
->biBitCount
);
1281 // offset of image from the beginning of the header
1282 DWORD ofs
= numColors
* sizeof(RGBQUAD
);
1283 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1286 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1287 image
, pbmi
, DIB_RGB_COLORS
);
1290 wxLogLastError(wxT("CreateDIBitmap"));
1293 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1294 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1301 static const wxChar
*GetTymedName(DWORD tymed
)
1303 static wxChar s_szBuf
[128];
1305 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1306 case TYMED_FILE
: return wxT("TYMED_FILE");
1307 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1308 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1309 case TYMED_GDI
: return wxT("TYMED_GDI");
1310 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1311 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1313 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1320 #else // not using OLE at all
1321 // ----------------------------------------------------------------------------
1323 // ----------------------------------------------------------------------------
1325 wxDataObject::wxDataObject()
1329 wxDataObject::~wxDataObject()
1333 void wxDataObject::SetAutoDelete()
1338 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)