]>
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 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) _T("")
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
,
190 ULONG
*WXUNUSED(pceltFetched
))
192 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
195 // we only return 1 element at a time - mainly because I'm too lazy to
196 // implement something which you're never asked for anyhow
200 if ( m_nCurrent
< m_nCount
) {
202 format
.cfFormat
= m_formats
[m_nCurrent
++];
204 format
.dwAspect
= DVASPECT_CONTENT
;
206 format
.tymed
= TYMED_HGLOBAL
;
217 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
219 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
222 if ( m_nCurrent
< m_nCount
)
225 // no, can't skip this many elements
231 STDMETHODIMP
wxIEnumFORMATETC::Reset()
233 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
240 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
242 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
244 // unfortunately, we can't reuse the code in ctor - types are different
245 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
246 pNew
->m_nCount
= m_nCount
;
247 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
248 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
249 pNew
->m_formats
[n
] = m_formats
[n
];
257 // ----------------------------------------------------------------------------
259 // ----------------------------------------------------------------------------
261 BEGIN_IID_TABLE(wxIDataObject
)
266 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
268 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
270 m_pDataObject
= pDataObject
;
271 m_mustDelete
= FALSE
;
274 wxIDataObject::~wxIDataObject()
278 delete m_pDataObject
;
282 // get data functions
283 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
285 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
287 // is data is in our format?
288 HRESULT hr
= QueryGetData(pformatetcIn
);
292 // for the bitmaps and metafiles we use the handles instead of global memory
294 wxDataFormat format
= (wxDataFormat::NativeFormat
)pformatetcIn
->cfFormat
;
299 pmedium
->tymed
= TYMED_GDI
;
302 case wxDF_ENHMETAFILE
:
303 pmedium
->tymed
= TYMED_ENHMF
;
307 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
308 sizeof(METAFILEPICT
));
309 if ( !pmedium
->hGlobal
) {
310 wxLogLastError(wxT("GlobalAlloc"));
311 return E_OUTOFMEMORY
;
313 pmedium
->tymed
= TYMED_MFPICT
;
318 size_t size
= m_pDataObject
->GetDataSize(format
);
320 // it probably means that the method is just not implemented
321 wxLogDebug(wxT("Invalid data size - can't be 0"));
323 return DV_E_FORMATETC
;
326 if ( !format
.IsStandard() ) {
327 // for custom formats, put the size with the data - alloc the
329 // MB: not completely sure this is correct,
330 // even if I can't figure out what's wrong
331 size
+= m_pDataObject
->GetBufferOffset( format
);
334 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
335 if ( hGlobal
== NULL
) {
336 wxLogLastError(wxT("GlobalAlloc"));
337 return E_OUTOFMEMORY
;
341 pmedium
->tymed
= TYMED_HGLOBAL
;
342 pmedium
->hGlobal
= hGlobal
;
345 pmedium
->pUnkForRelease
= NULL
;
348 hr
= GetDataHere(pformatetcIn
, pmedium
);
350 // free resources we allocated
351 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
352 GlobalFree(pmedium
->hGlobal
);
361 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
364 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
366 // put data in caller provided medium
367 switch ( pmedium
->tymed
)
370 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
375 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
376 &pmedium
->hEnhMetaFile
) )
381 // fall through - we pass METAFILEPICT through HGLOBAL
386 HGLOBAL hGlobal
= pmedium
->hGlobal
;
387 void *pBuf
= GlobalLock(hGlobal
);
388 if ( pBuf
== NULL
) {
389 wxLogLastError(wxT("GlobalLock"));
390 return E_OUTOFMEMORY
;
393 wxDataFormat format
= pformatetc
->cfFormat
;
394 if ( !format
.IsStandard() ) {
395 // for custom formats, put the size with the data
396 pBuf
= m_pDataObject
->SetSizeInBuffer( pBuf
, GlobalSize(hGlobal
), format
);
399 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
402 GlobalUnlock(hGlobal
);
413 #ifdef __DIGITALMARS__
415 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat
& format
);
418 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer
,
420 const wxDataFormat
& format
) ;
422 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer
, size_t size
,
423 const wxDataFormat
& format
) ;
427 // set data functions
428 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
432 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
434 switch ( pmedium
->tymed
)
437 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
441 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
445 // fall through - we pass METAFILEPICT through HGLOBAL
448 wxDataFormat format
= pformatetc
->cfFormat
;
450 // this is quite weird, but for file drag and drop, explorer
451 // calls our SetData() with the formats we do *not* support!
453 // as we can't fix this bug in explorer (it's a bug because it
454 // should only use formats returned by EnumFormatEtc), do the
456 if ( !m_pDataObject
->IsSupported(format
, wxDataObject::Set
) ) {
458 return DV_E_FORMATETC
;
462 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
463 if ( pBuf
== NULL
) {
464 wxLogLastError(wxT("GlobalLock"));
466 return E_OUTOFMEMORY
;
469 // we've got a problem with SetData() here because the base
470 // class version requires the size parameter which we don't
471 // have anywhere in OLE data transfer - so we need to
472 // synthetise it for known formats and we suppose that all data
473 // in custom formats starts with a DWORD containing the size
479 size
= strlen((const char *)pBuf
);
481 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
483 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
484 || ( defined(__MWERKS__) && defined(__WXMSW__) )
485 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
487 size
= wxWcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
493 // these formats don't use size at all, anyhow (but
494 // pass data by handle, which is always a single DWORD)
499 // the handler will calculate size itself (it's too
500 // complicated to do it here)
504 case CF_METAFILEPICT
:
505 size
= sizeof(METAFILEPICT
);
510 // we suppose that the size precedes the data
511 pBuf
= m_pDataObject
->GetSizeFromBuffer( pBuf
, &size
, format
);
512 if (! format
.IsStandard() ) {
513 // see GetData for coresponding increment
514 size
-= m_pDataObject
->GetBufferOffset( format
);
519 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
521 GlobalUnlock(pmedium
->hGlobal
);
534 // we own the medium, so we must release it - but do *not* free any
535 // data we pass by handle because we have copied it elsewhere
536 switch ( pmedium
->tymed
)
539 pmedium
->hBitmap
= 0;
543 pmedium
->hMetaFilePict
= 0;
547 pmedium
->hEnhMetaFile
= 0;
551 ReleaseStgMedium(pmedium
);
557 // information functions
558 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
560 // do we accept data in this format?
561 if ( pformatetc
== NULL
) {
562 wxLogTrace(wxTRACE_OleCalls
,
563 wxT("wxIDataObject::QueryGetData: invalid ptr."));
568 // the only one allowed by current COM implementation
569 if ( pformatetc
->lindex
!= -1 ) {
570 wxLogTrace(wxTRACE_OleCalls
,
571 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
577 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
578 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
579 wxLogTrace(wxTRACE_OleCalls
,
580 wxT("wxIDataObject::QueryGetData: bad dwAspect %ld"),
581 pformatetc
->dwAspect
);
583 return DV_E_DVASPECT
;
586 // and now check the type of data requested
587 wxDataFormat format
= pformatetc
->cfFormat
;
588 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
589 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
590 wxGetFormatName(format
));
593 wxLogTrace(wxTRACE_OleCalls
,
594 wxT("wxIDataObject::QueryGetData: %s unsupported"),
595 wxGetFormatName(format
));
597 return DV_E_FORMATETC
;
600 // we only transfer data by global memory, except for some particular cases
601 DWORD tymed
= pformatetc
->tymed
;
602 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
603 !(tymed
& TYMED_HGLOBAL
) ) {
604 // it's not what we're waiting for
605 wxLogTrace(wxTRACE_OleCalls
,
606 wxT("wxIDataObject::QueryGetData: %s != %s"),
608 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
617 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
618 FORMATETC
*pFormatetcOut
)
620 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
622 // TODO we might want something better than this trivial implementation here
623 if ( pFormatetcOut
!= NULL
)
624 pFormatetcOut
->ptd
= NULL
;
626 return DATA_S_SAMEFORMATETC
;
629 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
630 IEnumFORMATETC
**ppenumFormatEtc
)
632 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
634 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
637 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
639 wxDataFormat
*formats
;
640 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
641 m_pDataObject
->GetAllFormats(formats
, dir
);
643 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
645 *ppenumFormatEtc
= pEnum
;
647 if ( formats
!= &format
) {
654 // ----------------------------------------------------------------------------
655 // advise sink functions (not implemented)
656 // ----------------------------------------------------------------------------
658 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
659 DWORD
WXUNUSED(advf
),
660 IAdviseSink
*WXUNUSED(pAdvSink
),
661 DWORD
*WXUNUSED(pdwConnection
))
663 return OLE_E_ADVISENOTSUPPORTED
;
666 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
668 return OLE_E_ADVISENOTSUPPORTED
;
671 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
673 return OLE_E_ADVISENOTSUPPORTED
;
676 // ----------------------------------------------------------------------------
678 // ----------------------------------------------------------------------------
680 wxDataObject::wxDataObject()
682 m_pIDataObject
= new wxIDataObject(this);
683 m_pIDataObject
->AddRef();
686 wxDataObject::~wxDataObject()
688 ReleaseInterface(m_pIDataObject
);
691 void wxDataObject::SetAutoDelete()
693 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
694 m_pIDataObject
->Release();
696 // so that the dtor doesnt' crash
697 m_pIDataObject
= NULL
;
700 size_t wxDataObject::GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
702 return sizeof(size_t);
705 const void* wxDataObject::GetSizeFromBuffer( const void* buffer
, size_t* size
,
706 const wxDataFormat
& WXUNUSED(format
) )
708 size_t* p
= (size_t*)buffer
;
714 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
715 const wxDataFormat
& WXUNUSED(format
) )
717 size_t* p
= (size_t*)buffer
;
725 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
727 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
729 #pragma warning(disable:4063)
732 static wxChar s_szBuf
[256];
734 case CF_TEXT
: return wxT("CF_TEXT");
735 case CF_BITMAP
: return wxT("CF_BITMAP");
736 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
737 case CF_SYLK
: return wxT("CF_SYLK");
738 case CF_DIF
: return wxT("CF_DIF");
739 case CF_TIFF
: return wxT("CF_TIFF");
740 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
741 case CF_DIB
: return wxT("CF_DIB");
742 case CF_PALETTE
: return wxT("CF_PALETTE");
743 case CF_PENDATA
: return wxT("CF_PENDATA");
744 case CF_RIFF
: return wxT("CF_RIFF");
745 case CF_WAVE
: return wxT("CF_WAVE");
746 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
747 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
748 case CF_HDROP
: return wxT("CF_HDROP");
749 case CF_LOCALE
: return wxT("CF_LOCALE");
752 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
754 // it must be a new predefined format we don't know the name of
755 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
762 #pragma warning(default:4063)
768 // ----------------------------------------------------------------------------
769 // wxBitmapDataObject supports CF_DIB format
770 // ----------------------------------------------------------------------------
772 size_t wxBitmapDataObject::GetDataSize() const
774 return wxDIB::ConvertFromBitmap(NULL
, GetHbitmapOf(GetBitmap()));
777 bool wxBitmapDataObject::GetDataHere(void *buf
) const
779 BITMAPINFO
* const pbi
= (BITMAPINFO
*)buf
;
781 return wxDIB::ConvertFromBitmap(pbi
, GetHbitmapOf(GetBitmap())) != 0;
784 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
786 const BITMAPINFO
* const pbmi
= (const BITMAPINFO
*)buf
;
788 HBITMAP hbmp
= wxDIB::ConvertToBitmap(pbmi
);
790 wxCHECK_MSG( hbmp
, FALSE
, wxT("pasting/dropping invalid bitmap") );
792 const BITMAPINFOHEADER
* const pbmih
= &pbmi
->bmiHeader
;
793 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
794 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
796 // TODO: create wxPalette if the bitmap has any
803 // ----------------------------------------------------------------------------
804 // wxBitmapDataObject2 supports CF_BITMAP format
805 // ----------------------------------------------------------------------------
807 // the bitmaps aren't passed by value as other types of data (i.e. by copying
808 // the data into a global memory chunk and passing it to the clipboard or
809 // another application or whatever), but by handle, so these generic functions
810 // don't make much sense to them.
812 size_t wxBitmapDataObject2::GetDataSize() const
817 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
819 // we put a bitmap handle into pBuf
820 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
825 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
827 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
830 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
832 wxLogLastError(wxT("GetObject(HBITMAP)"));
835 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
836 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
838 if ( !bitmap
.Ok() ) {
839 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
851 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
853 if ( format
.GetFormatId() == CF_DIB
)
858 // shouldn't be selected into a DC or GetDIBits() would fail
859 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
860 wxT("can't copy bitmap selected into wxMemoryDC") );
862 // first get the info
864 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
865 NULL
, &bi
, DIB_RGB_COLORS
) )
867 wxLogLastError(wxT("GetDIBits(NULL)"));
872 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
876 // no data to copy - we don't pass HBITMAP via global memory
881 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
884 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
886 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
887 if ( format
.GetFormatId() == CF_DIB
)
892 // shouldn't be selected into a DC or GetDIBits() would fail
893 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
894 wxT("can't copy bitmap selected into wxMemoryDC") );
896 // first get the info
897 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
898 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
900 wxLogLastError(wxT("GetDIBits(NULL)"));
905 // and now copy the bits
906 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
907 pbi
, DIB_RGB_COLORS
) )
909 wxLogLastError(wxT("GetDIBits"));
916 // we put a bitmap handle into pBuf
917 *(HBITMAP
*)pBuf
= hbmp
;
923 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
924 size_t size
, const void *pBuf
)
927 if ( format
.GetFormatId() == CF_DIB
)
929 // here we get BITMAPINFO struct followed by the actual bitmap bits and
930 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
933 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
934 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
935 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
936 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
939 wxLogLastError(wxT("CreateDIBitmap"));
942 m_bitmap
.SetWidth(pbmih
->biWidth
);
943 m_bitmap
.SetHeight(pbmih
->biHeight
);
947 // it's easy with bitmaps: we pass them by handle
948 hbmp
= *(HBITMAP
*)pBuf
;
951 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
953 wxLogLastError(wxT("GetObject(HBITMAP)"));
956 m_bitmap
.SetWidth(bmp
.bmWidth
);
957 m_bitmap
.SetHeight(bmp
.bmHeight
);
958 m_bitmap
.SetDepth(bmp
.bmPlanes
);
961 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
963 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
970 // ----------------------------------------------------------------------------
972 // ----------------------------------------------------------------------------
974 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
976 //FIX ME __DIGITALMARS__
977 #if defined (__DIGITALMARS__)
978 //DragQueryFile not in any library
979 wxLogDebug(wxT("In wxFileDataObject::SetData code not executed - no Digital Mars library "));
984 // the documentation states that the first member of DROPFILES structure is
985 // a "DWORD offset of double NUL terminated file list". What they mean by
986 // this (I wonder if you see it immediately) is that the list starts at
987 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
988 // to use DragQueryFile to work with this structure, but not told where and
990 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
992 // get number of files (magic value -1)
993 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
995 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
997 // for each file get the length, allocate memory and then get the name
1000 for ( n
= 0; n
< nFiles
; n
++ ) {
1001 // +1 for terminating NUL
1002 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
1004 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
1005 str
.UngetWriteBuf();
1006 m_filenames
.Add(str
);
1008 if ( len2
!= len
- 1 ) {
1009 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
1010 %d characters, %d expected."), len2
, len
- 1);
1017 void wxFileDataObject::AddFile(const wxString
& file
)
1019 // just add file to filenames array
1020 // all useful data (such as DROPFILES struct) will be
1021 // created later as necessary
1022 m_filenames
.Add(file
);
1025 size_t wxFileDataObject::GetDataSize() const
1027 // size returned will be the size of the DROPFILES structure,
1028 // plus the list of filesnames (null byte separated), plus
1029 // a double null at the end
1031 // if no filenames in list, size is 0
1032 if ( m_filenames
.GetCount() == 0 )
1035 // inital size of DROPFILES struct + null byte
1036 size_t sz
= sizeof(DROPFILES
) + 1;
1038 size_t count
= m_filenames
.GetCount();
1039 for ( size_t i
= 0; i
< count
; i
++ )
1041 // add filename length plus null byte
1042 sz
+= m_filenames
[i
].Len() + 1;
1048 bool wxFileDataObject::GetDataHere(void *pData
) const
1050 // pData points to an externally allocated memory block
1051 // created using the size returned by GetDataSize()
1053 // if pData is NULL, or there are no files, return
1054 if ( !pData
|| m_filenames
.GetCount() == 0 )
1057 // convert data pointer to a DROPFILES struct pointer
1058 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1060 // initialize DROPFILES struct
1061 pDrop
->pFiles
= sizeof(DROPFILES
);
1062 pDrop
->fNC
= FALSE
; // not non-client coords
1064 pDrop
->fWide
= TRUE
;
1066 pDrop
->fWide
= FALSE
;
1067 #endif // Unicode/Ansi
1069 // set start of filenames list (null separated)
1070 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1072 size_t count
= m_filenames
.GetCount();
1073 for (size_t i
= 0; i
< count
; i
++ )
1075 // copy filename to pbuf and add null terminator
1076 size_t len
= m_filenames
[i
].Len();
1077 memcpy(pbuf
, m_filenames
[i
], len
);
1079 *pbuf
++ = wxT('\0');
1082 // add final null terminator
1088 // ----------------------------------------------------------------------------
1090 // ----------------------------------------------------------------------------
1092 class CFSTR_SHELLURLDataObject
: public wxCustomDataObject
1095 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1097 virtual size_t GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
1102 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1103 const wxDataFormat
& WXUNUSED(format
) )
1105 // CFSTR_SHELLURL is _always_ ANSI text
1106 *size
= strlen( (const char*)buffer
);
1111 virtual void* SetSizeInBuffer( void* buffer
, size_t WXUNUSED(size
),
1112 const wxDataFormat
& WXUNUSED(format
) )
1118 virtual bool GetDataHere( void* buffer
) const
1120 // CFSTR_SHELLURL is _always_ ANSI!
1121 wxCharBuffer
char_buffer( GetDataSize() );
1122 wxCustomDataObject::GetDataHere( (void*)char_buffer
.data() );
1123 wxString
unicode_buffer( char_buffer
, wxConvLibc
);
1124 memcpy( buffer
, unicode_buffer
.c_str(),
1125 ( unicode_buffer
.length() + 1 ) * sizeof(wxChar
) );
1134 wxURLDataObject::wxURLDataObject()
1136 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1137 // but it seems that some browsers only provide one of them so we have to
1139 Add(new wxTextDataObject
);
1140 Add(new CFSTR_SHELLURLDataObject());
1142 // we don't have any data yet
1143 m_dataObjectLast
= NULL
;
1146 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1150 m_dataObjectLast
= GetObject(format
);
1152 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1153 wxT("unsupported format in wxURLDataObject"));
1155 return m_dataObjectLast
->SetData(len
, buf
);
1158 wxString
wxURLDataObject::GetURL() const
1161 wxCHECK_MSG( m_dataObjectLast
, url
, _T("no data in wxURLDataObject") );
1163 size_t len
= m_dataObjectLast
->GetDataSize();
1165 m_dataObjectLast
->GetDataHere(url
.GetWriteBuf(len
));
1166 url
.UngetWriteBuf();
1171 void wxURLDataObject::SetURL(const wxString
& url
)
1173 SetData(wxDataFormat(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
),
1174 url
.Length()+1, url
.c_str());
1176 // CFSTR_SHELLURL is always supposed to be ANSI...
1177 wxWX2MBbuf urlA
= (wxWX2MBbuf
)url
.mbc_str();
1178 size_t len
= strlen(urlA
);
1179 SetData(wxDataFormat(CFSTR_SHELLURL
), len
+1, (const char*)urlA
);
1182 // ----------------------------------------------------------------------------
1183 // private functions
1184 // ----------------------------------------------------------------------------
1188 static const wxChar
*GetTymedName(DWORD tymed
)
1190 static wxChar s_szBuf
[128];
1192 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1193 case TYMED_FILE
: return wxT("TYMED_FILE");
1194 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1195 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1196 case TYMED_GDI
: return wxT("TYMED_GDI");
1197 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1198 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1200 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1207 #else // not using OLE at all
1208 // ----------------------------------------------------------------------------
1210 // ----------------------------------------------------------------------------
1212 wxDataObject::wxDataObject()
1216 wxDataObject::~wxDataObject()
1220 void wxDataObject::SetAutoDelete()
1225 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)