]>
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 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
32 #include "wx/dataobj.h"
34 #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
36 #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__) || defined(__WXWINCE__)
50 #include "wx/msw/ole/oleutils.h"
52 #include "wx/msw/dib.h"
54 #ifndef CFSTR_SHELLURL
55 #define CFSTR_SHELLURL _T("UniformResourceLocator")
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
63 static const wxChar
*GetTymedName(DWORD tymed
);
65 #define GetTymedName(tymed) wxEmptyString
66 #endif // Debug/!Debug
68 // ----------------------------------------------------------------------------
69 // wxIEnumFORMATETC interface implementation
70 // ----------------------------------------------------------------------------
72 class wxIEnumFORMATETC
: public IEnumFORMATETC
75 wxIEnumFORMATETC(const wxDataFormat
* formats
, ULONG nCount
);
76 virtual ~wxIEnumFORMATETC() { delete [] m_formats
; }
79 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
80 STDMETHODIMP
Skip(ULONG celt
);
82 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
84 DECLARE_IUNKNOWN_METHODS
;
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 reversed, that is we delete it
106 // when this object is deleted - setting this flag enables such logic
107 void SetDeleteFlag() { m_mustDelete
= true; }
110 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
111 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
112 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
113 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
114 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
115 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
116 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
117 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
118 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
120 DECLARE_IUNKNOWN_METHODS
;
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
, wxStringBuffer(s
, max
), max
);
160 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
172 ADD_IID(EnumFORMATETC
)
175 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
177 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
181 m_formats
= new CLIPFORMAT
[nCount
];
182 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
183 m_formats
[n
] = formats
[n
].GetFormatId();
187 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
191 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
193 ULONG numFetched
= 0;
194 while (m_nCurrent
< m_nCount
&& numFetched
< celt
) {
196 format
.cfFormat
= m_formats
[m_nCurrent
++];
198 format
.dwAspect
= DVASPECT_CONTENT
;
200 format
.tymed
= TYMED_HGLOBAL
;
207 *pceltFetched
= numFetched
;
209 return numFetched
== celt
? S_OK
: S_FALSE
;
212 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
214 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
217 if ( m_nCurrent
< m_nCount
)
220 // no, can't skip this many elements
226 STDMETHODIMP
wxIEnumFORMATETC::Reset()
228 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
235 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
237 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
239 // unfortunately, we can't reuse the code in ctor - types are different
240 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
241 pNew
->m_nCount
= m_nCount
;
242 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
243 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
244 pNew
->m_formats
[n
] = m_formats
[n
];
252 // ----------------------------------------------------------------------------
254 // ----------------------------------------------------------------------------
256 BEGIN_IID_TABLE(wxIDataObject
)
261 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
263 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
265 m_pDataObject
= pDataObject
;
266 m_mustDelete
= false;
269 wxIDataObject::~wxIDataObject()
273 delete m_pDataObject
;
277 // get data functions
278 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
280 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
282 // is data is in our format?
283 HRESULT hr
= QueryGetData(pformatetcIn
);
287 // for the bitmaps and metafiles we use the handles instead of global memory
289 wxDataFormat format
= (wxDataFormat::NativeFormat
)pformatetcIn
->cfFormat
;
294 pmedium
->tymed
= TYMED_GDI
;
297 case wxDF_ENHMETAFILE
:
298 pmedium
->tymed
= TYMED_ENHMF
;
303 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
304 sizeof(METAFILEPICT
));
305 if ( !pmedium
->hGlobal
) {
306 wxLogLastError(wxT("GlobalAlloc"));
307 return E_OUTOFMEMORY
;
309 pmedium
->tymed
= TYMED_MFPICT
;
314 size_t size
= m_pDataObject
->GetDataSize(format
);
316 // it probably means that the method is just not implemented
317 wxLogDebug(wxT("Invalid data size - can't be 0"));
319 return DV_E_FORMATETC
;
322 // we may need extra space for the buffer size
323 size
+= m_pDataObject
->GetBufferOffset( format
);
325 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
326 if ( hGlobal
== NULL
) {
327 wxLogLastError(wxT("GlobalAlloc"));
328 return E_OUTOFMEMORY
;
332 pmedium
->tymed
= TYMED_HGLOBAL
;
333 pmedium
->hGlobal
= hGlobal
;
336 pmedium
->pUnkForRelease
= NULL
;
339 hr
= GetDataHere(pformatetcIn
, pmedium
);
341 // free resources we allocated
342 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
343 GlobalFree(pmedium
->hGlobal
);
352 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
355 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
357 // put data in caller provided medium
358 switch ( pmedium
->tymed
)
361 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
366 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
367 &pmedium
->hEnhMetaFile
) )
372 // fall through - we pass METAFILEPICT through HGLOBAL
377 HGLOBAL hGlobal
= pmedium
->hGlobal
;
378 void *pBuf
= GlobalLock(hGlobal
);
379 if ( pBuf
== NULL
) {
380 wxLogLastError(wxT("GlobalLock"));
381 return E_OUTOFMEMORY
;
384 wxDataFormat format
= pformatetc
->cfFormat
;
386 // possibly put the size in the beginning of the buffer
387 pBuf
= m_pDataObject
->SetSizeInBuffer
390 ::GlobalSize(hGlobal
),
394 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
397 GlobalUnlock(hGlobal
);
409 // set data functions
410 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
414 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
416 switch ( pmedium
->tymed
)
419 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
423 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
427 // fall through - we pass METAFILEPICT through HGLOBAL
430 wxDataFormat format
= pformatetc
->cfFormat
;
432 // this is quite weird, but for file drag and drop, explorer
433 // calls our SetData() with the formats we do *not* support!
435 // as we can't fix this bug in explorer (it's a bug because it
436 // should only use formats returned by EnumFormatEtc), do the
438 if ( !m_pDataObject
->IsSupported(format
, wxDataObject::Set
) ) {
440 return DV_E_FORMATETC
;
444 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
445 if ( pBuf
== NULL
) {
446 wxLogLastError(wxT("GlobalLock"));
448 return E_OUTOFMEMORY
;
451 // we've got a problem with SetData() here because the base
452 // class version requires the size parameter which we don't
453 // have anywhere in OLE data transfer - so we need to
454 // synthetise it for known formats and we suppose that all data
455 // in custom formats starts with a DWORD containing the size
461 size
= strlen((const char *)pBuf
);
463 #if !(defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
465 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
466 || ( defined(__MWERKS__) && defined(__WXMSW__) )
467 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
469 size
= wxWcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
476 // these formats don't use size at all, anyhow (but
477 // pass data by handle, which is always a single DWORD)
483 // the handler will calculate size itself (it's too
484 // complicated to do it here)
489 case CF_METAFILEPICT
:
490 size
= sizeof(METAFILEPICT
);
494 pBuf
= m_pDataObject
->
495 GetSizeFromBuffer(pBuf
, &size
, format
);
496 size
-= m_pDataObject
->GetBufferOffset(format
);
499 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
501 GlobalUnlock(pmedium
->hGlobal
);
514 // we own the medium, so we must release it - but do *not* free any
515 // data we pass by handle because we have copied it elsewhere
516 switch ( pmedium
->tymed
)
519 pmedium
->hBitmap
= 0;
523 pmedium
->hMetaFilePict
= 0;
527 pmedium
->hEnhMetaFile
= 0;
531 ReleaseStgMedium(pmedium
);
537 // information functions
538 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
540 // do we accept data in this format?
541 if ( pformatetc
== NULL
) {
542 wxLogTrace(wxTRACE_OleCalls
,
543 wxT("wxIDataObject::QueryGetData: invalid ptr."));
548 // the only one allowed by current COM implementation
549 if ( pformatetc
->lindex
!= -1 ) {
550 wxLogTrace(wxTRACE_OleCalls
,
551 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
557 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
558 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
559 wxLogTrace(wxTRACE_OleCalls
,
560 wxT("wxIDataObject::QueryGetData: bad dwAspect %ld"),
561 pformatetc
->dwAspect
);
563 return DV_E_DVASPECT
;
566 // and now check the type of data requested
567 wxDataFormat format
= pformatetc
->cfFormat
;
568 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
569 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
570 wxGetFormatName(format
));
573 wxLogTrace(wxTRACE_OleCalls
,
574 wxT("wxIDataObject::QueryGetData: %s unsupported"),
575 wxGetFormatName(format
));
577 return DV_E_FORMATETC
;
580 // we only transfer data by global memory, except for some particular cases
581 DWORD tymed
= pformatetc
->tymed
;
582 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
583 !(tymed
& TYMED_HGLOBAL
) ) {
584 // it's not what we're waiting for
585 wxLogTrace(wxTRACE_OleCalls
,
586 wxT("wxIDataObject::QueryGetData: %s != %s"),
588 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
597 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
598 FORMATETC
*pFormatetcOut
)
600 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
602 // TODO we might want something better than this trivial implementation here
603 if ( pFormatetcOut
!= NULL
)
604 pFormatetcOut
->ptd
= NULL
;
606 return DATA_S_SAMEFORMATETC
;
609 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
610 IEnumFORMATETC
**ppenumFormatEtc
)
612 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
614 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
617 ULONG nFormatCount
= wx_truncate_cast(ULONG
, m_pDataObject
->GetFormatCount(dir
));
619 wxDataFormat
*formats
;
620 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
621 m_pDataObject
->GetAllFormats(formats
, dir
);
623 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
625 *ppenumFormatEtc
= pEnum
;
627 if ( formats
!= &format
) {
634 // ----------------------------------------------------------------------------
635 // advise sink functions (not implemented)
636 // ----------------------------------------------------------------------------
638 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
639 DWORD
WXUNUSED(advf
),
640 IAdviseSink
*WXUNUSED(pAdvSink
),
641 DWORD
*WXUNUSED(pdwConnection
))
643 return OLE_E_ADVISENOTSUPPORTED
;
646 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
648 return OLE_E_ADVISENOTSUPPORTED
;
651 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
653 return OLE_E_ADVISENOTSUPPORTED
;
656 // ----------------------------------------------------------------------------
658 // ----------------------------------------------------------------------------
660 wxDataObject::wxDataObject()
662 m_pIDataObject
= new wxIDataObject(this);
663 m_pIDataObject
->AddRef();
666 wxDataObject::~wxDataObject()
668 ReleaseInterface(m_pIDataObject
);
671 void wxDataObject::SetAutoDelete()
673 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
674 m_pIDataObject
->Release();
676 // so that the dtor doesnt' crash
677 m_pIDataObject
= NULL
;
680 size_t wxDataObject::GetBufferOffset(const wxDataFormat
& format
)
682 // if we prepend the size of the data to the buffer itself, account for it
683 return NeedsVerbatimData(format
) ? 0 : sizeof(size_t);
686 const void* wxDataObject::GetSizeFromBuffer( const void* buffer
, size_t* size
,
687 const wxDataFormat
& format
)
689 // hack: the third parameter is declared non-const in Wine's headers so
690 // cast away the const
691 size_t realsz
= ::HeapSize(::GetProcessHeap(), 0,
692 wx_const_cast(void*, buffer
));
693 if ( realsz
== (size_t)-1 )
695 // note that HeapSize() does not set last error
696 wxLogApiError(wxT("HeapSize"), 0);
702 // check if this data has its size prepended (as it was by default for wx
703 // programs prior 2.6.3):
704 size_t *p
= (size_t *)buffer
;
707 if ( NeedsVerbatimData(format
) )
708 wxLogDebug(wxT("Apparent data format mismatch: size not needed"));
710 p
++; // this data has its size prepended; skip first DWORD
716 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
717 const wxDataFormat
& format
)
719 size_t* p
= (size_t *)buffer
;
720 if ( !NeedsVerbatimData(format
) )
722 // prepend the size to the data and skip it
731 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
733 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
735 #pragma warning(disable:4063)
738 static wxChar s_szBuf
[256];
740 case CF_TEXT
: return wxT("CF_TEXT");
741 case CF_BITMAP
: return wxT("CF_BITMAP");
742 case CF_SYLK
: return wxT("CF_SYLK");
743 case CF_DIF
: return wxT("CF_DIF");
744 case CF_TIFF
: return wxT("CF_TIFF");
745 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
746 case CF_DIB
: return wxT("CF_DIB");
747 case CF_PALETTE
: return wxT("CF_PALETTE");
748 case CF_PENDATA
: return wxT("CF_PENDATA");
749 case CF_RIFF
: return wxT("CF_RIFF");
750 case CF_WAVE
: return wxT("CF_WAVE");
751 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
753 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
754 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
755 case CF_LOCALE
: return wxT("CF_LOCALE");
756 case CF_HDROP
: return wxT("CF_HDROP");
760 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
762 // it must be a new predefined format we don't know the name of
763 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
770 #pragma warning(default:4063)
776 // ----------------------------------------------------------------------------
777 // wxBitmapDataObject supports CF_DIB format
778 // ----------------------------------------------------------------------------
780 // TODO: support CF_DIB under Windows CE as well
782 size_t wxBitmapDataObject::GetDataSize() const
784 #if wxUSE_WXDIB && !defined(__WXWINCE__)
785 return wxDIB::ConvertFromBitmap(NULL
, GetHbitmapOf(GetBitmap()));
791 bool wxBitmapDataObject::GetDataHere(void *buf
) const
793 #if wxUSE_WXDIB && !defined(__WXWINCE__)
794 BITMAPINFO
* const pbi
= (BITMAPINFO
*)buf
;
796 return wxDIB::ConvertFromBitmap(pbi
, GetHbitmapOf(GetBitmap())) != 0;
803 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
805 #if wxUSE_WXDIB && !defined(__WXWINCE__)
806 const BITMAPINFO
* const pbmi
= (const BITMAPINFO
*)buf
;
808 HBITMAP hbmp
= wxDIB::ConvertToBitmap(pbmi
);
810 wxCHECK_MSG( hbmp
, FALSE
, wxT("pasting/dropping invalid bitmap") );
812 const BITMAPINFOHEADER
* const pbmih
= &pbmi
->bmiHeader
;
813 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
814 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
816 // TODO: create wxPalette if the bitmap has any
827 // ----------------------------------------------------------------------------
828 // wxBitmapDataObject2 supports CF_BITMAP format
829 // ----------------------------------------------------------------------------
831 // the bitmaps aren't passed by value as other types of data (i.e. by copying
832 // the data into a global memory chunk and passing it to the clipboard or
833 // another application or whatever), but by handle, so these generic functions
834 // don't make much sense to them.
836 size_t wxBitmapDataObject2::GetDataSize() const
841 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
843 // we put a bitmap handle into pBuf
844 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
849 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
851 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
854 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
856 wxLogLastError(wxT("GetObject(HBITMAP)"));
859 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
860 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
862 if ( !bitmap
.Ok() ) {
863 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
875 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
877 if ( format
.GetFormatId() == CF_DIB
)
882 // shouldn't be selected into a DC or GetDIBits() would fail
883 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
884 wxT("can't copy bitmap selected into wxMemoryDC") );
886 // first get the info
888 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
889 NULL
, &bi
, DIB_RGB_COLORS
) )
891 wxLogLastError(wxT("GetDIBits(NULL)"));
896 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
900 // no data to copy - we don't pass HBITMAP via global memory
905 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
908 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
910 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
911 if ( format
.GetFormatId() == CF_DIB
)
916 // shouldn't be selected into a DC or GetDIBits() would fail
917 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
918 wxT("can't copy bitmap selected into wxMemoryDC") );
920 // first get the info
921 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
922 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
924 wxLogLastError(wxT("GetDIBits(NULL)"));
929 // and now copy the bits
930 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
931 pbi
, DIB_RGB_COLORS
) )
933 wxLogLastError(wxT("GetDIBits"));
940 // we put a bitmap handle into pBuf
941 *(HBITMAP
*)pBuf
= hbmp
;
947 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
948 size_t size
, const void *pBuf
)
951 if ( format
.GetFormatId() == CF_DIB
)
953 // here we get BITMAPINFO struct followed by the actual bitmap bits and
954 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
957 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
958 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
959 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
960 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
963 wxLogLastError(wxT("CreateDIBitmap"));
966 m_bitmap
.SetWidth(pbmih
->biWidth
);
967 m_bitmap
.SetHeight(pbmih
->biHeight
);
971 // it's easy with bitmaps: we pass them by handle
972 hbmp
= *(HBITMAP
*)pBuf
;
975 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
977 wxLogLastError(wxT("GetObject(HBITMAP)"));
980 m_bitmap
.SetWidth(bmp
.bmWidth
);
981 m_bitmap
.SetHeight(bmp
.bmHeight
);
982 m_bitmap
.SetDepth(bmp
.bmPlanes
);
985 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
987 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
994 // ----------------------------------------------------------------------------
996 // ----------------------------------------------------------------------------
998 bool wxFileDataObject::SetData(size_t WXUNUSED(size
),
999 const void *WXUNUSED_IN_WINCE(pData
))
1002 m_filenames
.Empty();
1004 // the documentation states that the first member of DROPFILES structure is
1005 // a "DWORD offset of double NUL terminated file list". What they mean by
1006 // this (I wonder if you see it immediately) is that the list starts at
1007 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
1008 // to use DragQueryFile to work with this structure, but not told where and
1009 // how to get HDROP.
1010 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
1012 // get number of files (magic value -1)
1013 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
1015 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
1017 // for each file get the length, allocate memory and then get the name
1020 for ( n
= 0; n
< nFiles
; n
++ ) {
1021 // +1 for terminating NUL
1022 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
1024 UINT len2
= ::DragQueryFile(hdrop
, n
, wxStringBuffer(str
, len
), len
);
1025 m_filenames
.Add(str
);
1027 if ( len2
!= len
- 1 ) {
1028 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
1029 %d characters, %d expected."), len2
, len
- 1);
1039 void wxFileDataObject::AddFile(const wxString
& file
)
1041 // just add file to filenames array
1042 // all useful data (such as DROPFILES struct) will be
1043 // created later as necessary
1044 m_filenames
.Add(file
);
1047 size_t wxFileDataObject::GetDataSize() const
1050 // size returned will be the size of the DROPFILES structure,
1051 // plus the list of filesnames (null byte separated), plus
1052 // a double null at the end
1054 // if no filenames in list, size is 0
1055 if ( m_filenames
.GetCount() == 0 )
1058 // inital size of DROPFILES struct + null byte
1059 size_t sz
= sizeof(DROPFILES
) + (1 * sizeof(wxChar
));
1061 size_t count
= m_filenames
.GetCount();
1062 for ( size_t i
= 0; i
< count
; i
++ )
1064 // add filename length plus null byte
1065 sz
+= (m_filenames
[i
].Len() + 1) * sizeof(wxChar
);
1074 bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData
)) const
1077 // pData points to an externally allocated memory block
1078 // created using the size returned by GetDataSize()
1080 // if pData is NULL, or there are no files, return
1081 if ( !pData
|| m_filenames
.GetCount() == 0 )
1084 // convert data pointer to a DROPFILES struct pointer
1085 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1087 // initialize DROPFILES struct
1088 pDrop
->pFiles
= sizeof(DROPFILES
);
1089 pDrop
->fNC
= FALSE
; // not non-client coords
1091 pDrop
->fWide
= TRUE
;
1093 pDrop
->fWide
= FALSE
;
1094 #endif // Unicode/Ansi
1096 // set start of filenames list (null separated)
1097 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1099 size_t count
= m_filenames
.GetCount();
1100 for (size_t i
= 0; i
< count
; i
++ )
1102 // copy filename to pbuf and add null terminator
1103 size_t len
= m_filenames
[i
].Len();
1104 memcpy(pbuf
, m_filenames
[i
], len
*sizeof(wxChar
));
1106 *pbuf
++ = wxT('\0');
1109 // add final null terminator
1118 // ----------------------------------------------------------------------------
1120 // ----------------------------------------------------------------------------
1122 class CFSTR_SHELLURLDataObject
: public wxCustomDataObject
1125 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1127 virtual size_t GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
1132 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1133 const wxDataFormat
& WXUNUSED(format
) )
1135 // CFSTR_SHELLURL is _always_ ANSI text
1136 *size
= strlen( (const char*)buffer
);
1141 virtual void* SetSizeInBuffer( void* buffer
, size_t WXUNUSED(size
),
1142 const wxDataFormat
& WXUNUSED(format
) )
1148 virtual bool GetDataHere( void* buffer
) const
1150 // CFSTR_SHELLURL is _always_ ANSI!
1151 wxCharBuffer
char_buffer( GetDataSize() );
1152 wxCustomDataObject::GetDataHere( (void*)char_buffer
.data() );
1153 wxString
unicode_buffer( char_buffer
, wxConvLibc
);
1154 memcpy( buffer
, unicode_buffer
.c_str(),
1155 ( unicode_buffer
.length() + 1 ) * sizeof(wxChar
) );
1159 virtual bool GetDataHere(const wxDataFormat
& WXUNUSED(format
),
1161 { return GetDataHere(buf
); }
1164 DECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject
)
1169 wxURLDataObject::wxURLDataObject(const wxString
& url
)
1171 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1172 // but it seems that some browsers only provide one of them so we have to
1174 Add(new wxTextDataObject
);
1175 Add(new CFSTR_SHELLURLDataObject());
1177 // we don't have any data yet
1178 m_dataObjectLast
= NULL
;
1184 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1188 m_dataObjectLast
= GetObject(format
);
1190 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1191 wxT("unsupported format in wxURLDataObject"));
1193 return m_dataObjectLast
->SetData(len
, buf
);
1196 wxString
wxURLDataObject::GetURL() const
1199 wxCHECK_MSG( m_dataObjectLast
, url
, _T("no data in wxURLDataObject") );
1201 size_t len
= m_dataObjectLast
->GetDataSize();
1203 m_dataObjectLast
->GetDataHere(wxStringBuffer(url
, len
));
1208 void wxURLDataObject::SetURL(const wxString
& url
)
1210 SetData(wxDataFormat(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
),
1211 url
.Length()+1, url
.c_str());
1213 // CFSTR_SHELLURL is always supposed to be ANSI...
1214 wxWX2MBbuf urlA
= (wxWX2MBbuf
)url
.mbc_str();
1215 size_t len
= strlen(urlA
);
1216 SetData(wxDataFormat(CFSTR_SHELLURL
), len
+1, (const char*)urlA
);
1219 // ----------------------------------------------------------------------------
1220 // private functions
1221 // ----------------------------------------------------------------------------
1225 static const wxChar
*GetTymedName(DWORD tymed
)
1227 static wxChar s_szBuf
[128];
1229 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1230 case TYMED_FILE
: return wxT("TYMED_FILE");
1231 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1232 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1233 case TYMED_GDI
: return wxT("TYMED_GDI");
1234 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1235 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1237 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1244 #else // not using OLE at all
1246 // ----------------------------------------------------------------------------
1248 // ----------------------------------------------------------------------------
1252 wxDataObject::wxDataObject()
1256 wxDataObject::~wxDataObject()
1260 void wxDataObject::SetAutoDelete()
1265 const wxChar
*wxDataObject::GetFormatName(wxDataFormat
WXUNUSED(format
))
1269 #endif // __WXDEBUG__
1271 #endif // wxUSE_DATAOBJ
1273 #endif // wxUSE_OLE/!wxUSE_OLE