]>
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__)
33 #include "wx/dataobj.h"
35 #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
37 #include "wx/msw/private.h" // includes <windows.h>
43 // for some compilers, the entire ole2.h must be included, not only oleauto.h
44 #if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__) || defined(__WXWINCE__)
51 #include "wx/msw/ole/oleutils.h"
53 #include "wx/msw/dib.h"
55 #ifndef CFSTR_SHELLURL
56 #define CFSTR_SHELLURL _T("UniformResourceLocator")
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
64 static const wxChar
*GetTymedName(DWORD tymed
);
66 #define GetTymedName(tymed) wxEmptyString
67 #endif // Debug/!Debug
69 // ----------------------------------------------------------------------------
70 // wxIEnumFORMATETC interface implementation
71 // ----------------------------------------------------------------------------
73 class wxIEnumFORMATETC
: public IEnumFORMATETC
76 wxIEnumFORMATETC(const wxDataFormat
* formats
, ULONG nCount
);
77 virtual ~wxIEnumFORMATETC() { delete [] m_formats
; }
80 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
81 STDMETHODIMP
Skip(ULONG celt
);
83 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
85 DECLARE_IUNKNOWN_METHODS
;
88 CLIPFORMAT
*m_formats
; // formats we can provide data in
89 ULONG m_nCount
, // number of formats we support
90 m_nCurrent
; // current enum position
92 DECLARE_NO_COPY_CLASS(wxIEnumFORMATETC
)
95 // ----------------------------------------------------------------------------
96 // wxIDataObject implementation of IDataObject interface
97 // ----------------------------------------------------------------------------
99 class wxIDataObject
: public IDataObject
102 wxIDataObject(wxDataObject
*pDataObject
);
103 virtual ~wxIDataObject();
105 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
106 // is), but in some cases, the situation is reversed, that is we delete it
107 // when this object is deleted - setting this flag enables such logic
108 void SetDeleteFlag() { m_mustDelete
= true; }
111 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
112 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
113 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
114 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
115 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
116 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
117 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
118 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
119 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
121 DECLARE_IUNKNOWN_METHODS
;
124 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
128 DECLARE_NO_COPY_CLASS(wxIDataObject
)
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 void wxDataFormat::SetId(const wxChar
*format
)
141 m_format
= (wxDataFormat::NativeFormat
)::RegisterClipboardFormat(format
);
144 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
148 wxString
wxDataFormat::GetId() const
150 static const int max
= 256;
154 wxCHECK_MSG( !IsStandard(), s
,
155 wxT("name of predefined format cannot be retrieved") );
157 int len
= ::GetClipboardFormatName(m_format
, wxStringBuffer(s
, 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
,
192 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
194 ULONG numFetched
= 0;
195 while (m_nCurrent
< m_nCount
&& numFetched
< celt
) {
197 format
.cfFormat
= m_formats
[m_nCurrent
++];
199 format
.dwAspect
= DVASPECT_CONTENT
;
201 format
.tymed
= TYMED_HGLOBAL
;
208 *pceltFetched
= numFetched
;
210 return numFetched
== celt
? S_OK
: S_FALSE
;
213 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
215 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
218 if ( m_nCurrent
< m_nCount
)
221 // no, can't skip this many elements
227 STDMETHODIMP
wxIEnumFORMATETC::Reset()
229 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
236 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
238 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
240 // unfortunately, we can't reuse the code in ctor - types are different
241 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
242 pNew
->m_nCount
= m_nCount
;
243 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
244 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
245 pNew
->m_formats
[n
] = m_formats
[n
];
253 // ----------------------------------------------------------------------------
255 // ----------------------------------------------------------------------------
257 BEGIN_IID_TABLE(wxIDataObject
)
262 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
264 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
266 m_pDataObject
= pDataObject
;
267 m_mustDelete
= false;
270 wxIDataObject::~wxIDataObject()
274 delete m_pDataObject
;
278 // get data functions
279 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
281 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
283 // is data is in our format?
284 HRESULT hr
= QueryGetData(pformatetcIn
);
288 // for the bitmaps and metafiles we use the handles instead of global memory
290 wxDataFormat format
= (wxDataFormat::NativeFormat
)pformatetcIn
->cfFormat
;
295 pmedium
->tymed
= TYMED_GDI
;
298 case wxDF_ENHMETAFILE
:
299 pmedium
->tymed
= TYMED_ENHMF
;
304 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
305 sizeof(METAFILEPICT
));
306 if ( !pmedium
->hGlobal
) {
307 wxLogLastError(wxT("GlobalAlloc"));
308 return E_OUTOFMEMORY
;
310 pmedium
->tymed
= TYMED_MFPICT
;
315 size_t size
= m_pDataObject
->GetDataSize(format
);
317 // it probably means that the method is just not implemented
318 wxLogDebug(wxT("Invalid data size - can't be 0"));
320 return DV_E_FORMATETC
;
323 // we may need extra space for the buffer size
324 size
+= m_pDataObject
->GetBufferOffset( format
);
326 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
327 if ( hGlobal
== NULL
) {
328 wxLogLastError(wxT("GlobalAlloc"));
329 return E_OUTOFMEMORY
;
333 pmedium
->tymed
= TYMED_HGLOBAL
;
334 pmedium
->hGlobal
= hGlobal
;
337 pmedium
->pUnkForRelease
= NULL
;
340 hr
= GetDataHere(pformatetcIn
, pmedium
);
342 // free resources we allocated
343 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
344 GlobalFree(pmedium
->hGlobal
);
353 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
356 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
358 // put data in caller provided medium
359 switch ( pmedium
->tymed
)
362 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
367 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
368 &pmedium
->hEnhMetaFile
) )
373 // fall through - we pass METAFILEPICT through HGLOBAL
378 HGLOBAL hGlobal
= pmedium
->hGlobal
;
379 void *pBuf
= GlobalLock(hGlobal
);
380 if ( pBuf
== NULL
) {
381 wxLogLastError(wxT("GlobalLock"));
382 return E_OUTOFMEMORY
;
385 wxDataFormat format
= pformatetc
->cfFormat
;
387 // possibly put the size in the beginning of the buffer
388 pBuf
= m_pDataObject
->SetSizeInBuffer
391 ::GlobalSize(hGlobal
),
395 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
398 GlobalUnlock(hGlobal
);
410 // set data functions
411 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
415 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
417 switch ( pmedium
->tymed
)
420 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
424 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
428 // fall through - we pass METAFILEPICT through HGLOBAL
431 wxDataFormat format
= pformatetc
->cfFormat
;
433 // this is quite weird, but for file drag and drop, explorer
434 // calls our SetData() with the formats we do *not* support!
436 // as we can't fix this bug in explorer (it's a bug because it
437 // should only use formats returned by EnumFormatEtc), do the
439 if ( !m_pDataObject
->IsSupported(format
, wxDataObject::Set
) ) {
441 return DV_E_FORMATETC
;
445 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
446 if ( pBuf
== NULL
) {
447 wxLogLastError(wxT("GlobalLock"));
449 return E_OUTOFMEMORY
;
452 // we've got a problem with SetData() here because the base
453 // class version requires the size parameter which we don't
454 // have anywhere in OLE data transfer - so we need to
455 // synthetise it for known formats and we suppose that all data
456 // in custom formats starts with a DWORD containing the size
462 size
= strlen((const char *)pBuf
);
464 #if !(defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
466 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
467 || ( defined(__MWERKS__) && defined(__WXMSW__) )
468 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
470 size
= wxWcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
477 // these formats don't use size at all, anyhow (but
478 // pass data by handle, which is always a single DWORD)
484 // the handler will calculate size itself (it's too
485 // complicated to do it here)
490 case CF_METAFILEPICT
:
491 size
= sizeof(METAFILEPICT
);
495 pBuf
= m_pDataObject
->
496 GetSizeFromBuffer(pBuf
, &size
, format
);
497 size
-= m_pDataObject
->GetBufferOffset(format
);
500 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
502 GlobalUnlock(pmedium
->hGlobal
);
515 // we own the medium, so we must release it - but do *not* free any
516 // data we pass by handle because we have copied it elsewhere
517 switch ( pmedium
->tymed
)
520 pmedium
->hBitmap
= 0;
524 pmedium
->hMetaFilePict
= 0;
528 pmedium
->hEnhMetaFile
= 0;
532 ReleaseStgMedium(pmedium
);
538 // information functions
539 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
541 // do we accept data in this format?
542 if ( pformatetc
== NULL
) {
543 wxLogTrace(wxTRACE_OleCalls
,
544 wxT("wxIDataObject::QueryGetData: invalid ptr."));
549 // the only one allowed by current COM implementation
550 if ( pformatetc
->lindex
!= -1 ) {
551 wxLogTrace(wxTRACE_OleCalls
,
552 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
558 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
559 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
560 wxLogTrace(wxTRACE_OleCalls
,
561 wxT("wxIDataObject::QueryGetData: bad dwAspect %ld"),
562 pformatetc
->dwAspect
);
564 return DV_E_DVASPECT
;
567 // and now check the type of data requested
568 wxDataFormat format
= pformatetc
->cfFormat
;
569 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
570 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
571 wxGetFormatName(format
));
574 wxLogTrace(wxTRACE_OleCalls
,
575 wxT("wxIDataObject::QueryGetData: %s unsupported"),
576 wxGetFormatName(format
));
578 return DV_E_FORMATETC
;
581 // we only transfer data by global memory, except for some particular cases
582 DWORD tymed
= pformatetc
->tymed
;
583 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
584 !(tymed
& TYMED_HGLOBAL
) ) {
585 // it's not what we're waiting for
586 wxLogTrace(wxTRACE_OleCalls
,
587 wxT("wxIDataObject::QueryGetData: %s != %s"),
589 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
598 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
599 FORMATETC
*pFormatetcOut
)
601 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
603 // TODO we might want something better than this trivial implementation here
604 if ( pFormatetcOut
!= NULL
)
605 pFormatetcOut
->ptd
= NULL
;
607 return DATA_S_SAMEFORMATETC
;
610 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
611 IEnumFORMATETC
**ppenumFormatEtc
)
613 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
615 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
618 ULONG nFormatCount
= wx_truncate_cast(ULONG
, m_pDataObject
->GetFormatCount(dir
));
620 wxDataFormat
*formats
;
621 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
622 m_pDataObject
->GetAllFormats(formats
, dir
);
624 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
626 *ppenumFormatEtc
= pEnum
;
628 if ( formats
!= &format
) {
635 // ----------------------------------------------------------------------------
636 // advise sink functions (not implemented)
637 // ----------------------------------------------------------------------------
639 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
640 DWORD
WXUNUSED(advf
),
641 IAdviseSink
*WXUNUSED(pAdvSink
),
642 DWORD
*WXUNUSED(pdwConnection
))
644 return OLE_E_ADVISENOTSUPPORTED
;
647 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
649 return OLE_E_ADVISENOTSUPPORTED
;
652 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
654 return OLE_E_ADVISENOTSUPPORTED
;
657 // ----------------------------------------------------------------------------
659 // ----------------------------------------------------------------------------
661 wxDataObject::wxDataObject()
663 m_pIDataObject
= new wxIDataObject(this);
664 m_pIDataObject
->AddRef();
667 wxDataObject::~wxDataObject()
669 ReleaseInterface(m_pIDataObject
);
672 void wxDataObject::SetAutoDelete()
674 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
675 m_pIDataObject
->Release();
677 // so that the dtor doesnt' crash
678 m_pIDataObject
= NULL
;
681 size_t wxDataObject::GetBufferOffset(const wxDataFormat
& format
)
683 // if we prepend the size of the data to the buffer itself, account for it
684 return NeedsVerbatimData(format
) ? 0 : sizeof(size_t);
687 const void* wxDataObject::GetSizeFromBuffer( const void* buffer
, size_t* size
,
688 const wxDataFormat
& format
)
690 // hack: the third parameter is declared non-const in Wine's headers so
691 // cast away the const
692 size_t realsz
= ::HeapSize(::GetProcessHeap(), 0,
693 wx_const_cast(void*, buffer
));
694 if ( realsz
== (size_t)-1 )
696 // note that HeapSize() does not set last error
697 wxLogApiError(wxT("HeapSize"), 0);
703 // check if this data has its size prepended (as it was by default for wx
704 // programs prior 2.6.3):
705 size_t *p
= (size_t *)buffer
;
708 if ( NeedsVerbatimData(format
) )
709 wxLogDebug(wxT("Apparent data format mismatch: size not needed"));
711 p
++; // this data has its size prepended; skip first DWORD
717 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
718 const wxDataFormat
& format
)
720 size_t* p
= (size_t *)buffer
;
721 if ( !NeedsVerbatimData(format
) )
723 // prepend the size to the data and skip it
732 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
734 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
736 #pragma warning(disable:4063)
739 static wxChar s_szBuf
[256];
741 case CF_TEXT
: return wxT("CF_TEXT");
742 case CF_BITMAP
: return wxT("CF_BITMAP");
743 case CF_SYLK
: return wxT("CF_SYLK");
744 case CF_DIF
: return wxT("CF_DIF");
745 case CF_TIFF
: return wxT("CF_TIFF");
746 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
747 case CF_DIB
: return wxT("CF_DIB");
748 case CF_PALETTE
: return wxT("CF_PALETTE");
749 case CF_PENDATA
: return wxT("CF_PENDATA");
750 case CF_RIFF
: return wxT("CF_RIFF");
751 case CF_WAVE
: return wxT("CF_WAVE");
752 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
754 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
755 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
756 case CF_LOCALE
: return wxT("CF_LOCALE");
757 case CF_HDROP
: return wxT("CF_HDROP");
761 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
763 // it must be a new predefined format we don't know the name of
764 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
771 #pragma warning(default:4063)
777 // ----------------------------------------------------------------------------
778 // wxBitmapDataObject supports CF_DIB format
779 // ----------------------------------------------------------------------------
781 // TODO: support CF_DIB under Windows CE as well
783 size_t wxBitmapDataObject::GetDataSize() const
785 #if wxUSE_WXDIB && !defined(__WXWINCE__)
786 return wxDIB::ConvertFromBitmap(NULL
, GetHbitmapOf(GetBitmap()));
792 bool wxBitmapDataObject::GetDataHere(void *buf
) const
794 #if wxUSE_WXDIB && !defined(__WXWINCE__)
795 BITMAPINFO
* const pbi
= (BITMAPINFO
*)buf
;
797 return wxDIB::ConvertFromBitmap(pbi
, GetHbitmapOf(GetBitmap())) != 0;
804 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
806 #if wxUSE_WXDIB && !defined(__WXWINCE__)
807 const BITMAPINFO
* const pbmi
= (const BITMAPINFO
*)buf
;
809 HBITMAP hbmp
= wxDIB::ConvertToBitmap(pbmi
);
811 wxCHECK_MSG( hbmp
, FALSE
, wxT("pasting/dropping invalid bitmap") );
813 const BITMAPINFOHEADER
* const pbmih
= &pbmi
->bmiHeader
;
814 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
815 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
817 // TODO: create wxPalette if the bitmap has any
828 // ----------------------------------------------------------------------------
829 // wxBitmapDataObject2 supports CF_BITMAP format
830 // ----------------------------------------------------------------------------
832 // the bitmaps aren't passed by value as other types of data (i.e. by copying
833 // the data into a global memory chunk and passing it to the clipboard or
834 // another application or whatever), but by handle, so these generic functions
835 // don't make much sense to them.
837 size_t wxBitmapDataObject2::GetDataSize() const
842 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
844 // we put a bitmap handle into pBuf
845 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
850 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
852 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
855 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
857 wxLogLastError(wxT("GetObject(HBITMAP)"));
860 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
861 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
863 if ( !bitmap
.Ok() ) {
864 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
876 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
878 if ( format
.GetFormatId() == CF_DIB
)
883 // shouldn't be selected into a DC or GetDIBits() would fail
884 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
885 wxT("can't copy bitmap selected into wxMemoryDC") );
887 // first get the info
889 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
890 NULL
, &bi
, DIB_RGB_COLORS
) )
892 wxLogLastError(wxT("GetDIBits(NULL)"));
897 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
901 // no data to copy - we don't pass HBITMAP via global memory
906 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
909 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
911 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
912 if ( format
.GetFormatId() == CF_DIB
)
917 // shouldn't be selected into a DC or GetDIBits() would fail
918 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
919 wxT("can't copy bitmap selected into wxMemoryDC") );
921 // first get the info
922 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
923 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
925 wxLogLastError(wxT("GetDIBits(NULL)"));
930 // and now copy the bits
931 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
932 pbi
, DIB_RGB_COLORS
) )
934 wxLogLastError(wxT("GetDIBits"));
941 // we put a bitmap handle into pBuf
942 *(HBITMAP
*)pBuf
= hbmp
;
948 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
949 size_t size
, const void *pBuf
)
952 if ( format
.GetFormatId() == CF_DIB
)
954 // here we get BITMAPINFO struct followed by the actual bitmap bits and
955 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
958 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
959 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
960 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
961 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
964 wxLogLastError(wxT("CreateDIBitmap"));
967 m_bitmap
.SetWidth(pbmih
->biWidth
);
968 m_bitmap
.SetHeight(pbmih
->biHeight
);
972 // it's easy with bitmaps: we pass them by handle
973 hbmp
= *(HBITMAP
*)pBuf
;
976 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
978 wxLogLastError(wxT("GetObject(HBITMAP)"));
981 m_bitmap
.SetWidth(bmp
.bmWidth
);
982 m_bitmap
.SetHeight(bmp
.bmHeight
);
983 m_bitmap
.SetDepth(bmp
.bmPlanes
);
986 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
988 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
995 // ----------------------------------------------------------------------------
997 // ----------------------------------------------------------------------------
999 bool wxFileDataObject::SetData(size_t WXUNUSED(size
),
1000 const void *WXUNUSED_IN_WINCE(pData
))
1003 m_filenames
.Empty();
1005 // the documentation states that the first member of DROPFILES structure is
1006 // a "DWORD offset of double NUL terminated file list". What they mean by
1007 // this (I wonder if you see it immediately) is that the list starts at
1008 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
1009 // to use DragQueryFile to work with this structure, but not told where and
1010 // how to get HDROP.
1011 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
1013 // get number of files (magic value -1)
1014 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
1016 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
1018 // for each file get the length, allocate memory and then get the name
1021 for ( n
= 0; n
< nFiles
; n
++ ) {
1022 // +1 for terminating NUL
1023 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
1025 UINT len2
= ::DragQueryFile(hdrop
, n
, wxStringBuffer(str
, len
), len
);
1026 m_filenames
.Add(str
);
1028 if ( len2
!= len
- 1 ) {
1029 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
1030 %d characters, %d expected."), len2
, len
- 1);
1040 void wxFileDataObject::AddFile(const wxString
& file
)
1042 // just add file to filenames array
1043 // all useful data (such as DROPFILES struct) will be
1044 // created later as necessary
1045 m_filenames
.Add(file
);
1048 size_t wxFileDataObject::GetDataSize() const
1051 // size returned will be the size of the DROPFILES structure, plus the list
1052 // of filesnames (null byte separated), plus a double null at the end
1054 // if no filenames in list, size is 0
1055 if ( m_filenames
.empty() )
1058 #if wxUSE_UNICODE_MSLU
1060 if ( wxGetOsVersion() == wxOS_WINDOWS_9X
)
1062 // Win9x always uses ANSI file names and MSLU doesn't help with this
1063 sizeOfChar
= sizeof(char);
1067 sizeOfChar
= sizeof(wxChar
);
1069 #else // !wxUSE_UNICODE_MSLU
1070 static const size_t sizeOfChar
= sizeof(wxChar
);
1071 #endif // wxUSE_UNICODE_MSLU/!wxUSE_UNICODE_MSLU
1073 // inital size of DROPFILES struct + null byte
1074 size_t sz
= sizeof(DROPFILES
) + sizeOfChar
;
1076 const size_t count
= m_filenames
.size();
1077 for ( size_t i
= 0; i
< count
; i
++ )
1079 // add filename length plus null byte
1081 #if wxUSE_UNICODE_MSLU
1082 if ( sizeOfChar
== sizeof(char) )
1083 len
= strlen(wxConvFileName
->cWC2MB(m_filenames
[i
]));
1085 #endif // wxUSE_UNICODE_MSLU
1086 len
= m_filenames
[i
].length();
1088 sz
+= (len
+ 1) * sizeOfChar
;
1097 bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData
)) const
1100 // pData points to an externally allocated memory block
1101 // created using the size returned by GetDataSize()
1103 // if pData is NULL, or there are no files, return
1104 if ( !pData
|| m_filenames
.empty() )
1107 // convert data pointer to a DROPFILES struct pointer
1108 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1110 // initialize DROPFILES struct
1111 pDrop
->pFiles
= sizeof(DROPFILES
);
1112 pDrop
->fNC
= FALSE
; // not non-client coords
1113 #if wxUSE_UNICODE_MSLU
1114 pDrop
->fWide
= wxGetOsVersion() != wxOS_WINDOWS_9X
? TRUE
: FALSE
;
1116 pDrop
->fWide
= wxUSE_UNICODE
;
1119 const size_t sizeOfChar
= pDrop
->fWide
? sizeof(wchar_t) : sizeof(char);
1121 // set start of filenames list (null separated)
1122 BYTE
*pbuf
= (BYTE
*)(pDrop
+ 1);
1124 const size_t count
= m_filenames
.size();
1125 for ( size_t i
= 0; i
< count
; i
++ )
1127 // copy filename to pbuf and add null terminator
1129 #if wxUSE_UNICODE_MSLU
1130 if ( sizeOfChar
== sizeof(char) )
1132 wxCharBuffer
buf(wxConvFileName
->cWC2MB(m_filenames
[i
]));
1134 memcpy(pbuf
, buf
, len
*sizeOfChar
);
1137 #endif // wxUSE_UNICODE_MSLU
1139 len
= m_filenames
[i
].length();
1140 memcpy(pbuf
, m_filenames
[i
].c_str(), len
*sizeOfChar
);
1143 pbuf
+= len
*sizeOfChar
;
1145 memset(pbuf
, 0, sizeOfChar
);
1149 // add final null terminator
1150 memset(pbuf
, 0, sizeOfChar
);
1158 // ----------------------------------------------------------------------------
1160 // ----------------------------------------------------------------------------
1162 class CFSTR_SHELLURLDataObject
: public wxCustomDataObject
1165 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1167 virtual size_t GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
1172 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1173 const wxDataFormat
& WXUNUSED(format
) )
1175 // CFSTR_SHELLURL is _always_ ANSI text
1176 *size
= strlen( (const char*)buffer
);
1181 virtual void* SetSizeInBuffer( void* buffer
, size_t WXUNUSED(size
),
1182 const wxDataFormat
& WXUNUSED(format
) )
1188 virtual bool GetDataHere( void* buffer
) const
1190 // CFSTR_SHELLURL is _always_ ANSI!
1191 wxCharBuffer
char_buffer( GetDataSize() );
1192 wxCustomDataObject::GetDataHere( (void*)char_buffer
.data() );
1193 wxString
unicode_buffer( char_buffer
, wxConvLibc
);
1194 memcpy( buffer
, unicode_buffer
.c_str(),
1195 ( unicode_buffer
.length() + 1 ) * sizeof(wxChar
) );
1199 virtual bool GetDataHere(const wxDataFormat
& WXUNUSED(format
),
1201 { return GetDataHere(buf
); }
1204 DECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject
)
1209 wxURLDataObject::wxURLDataObject(const wxString
& url
)
1211 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1212 // but it seems that some browsers only provide one of them so we have to
1214 Add(new wxTextDataObject
);
1215 Add(new CFSTR_SHELLURLDataObject());
1217 // we don't have any data yet
1218 m_dataObjectLast
= NULL
;
1224 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1228 m_dataObjectLast
= GetObject(format
);
1230 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1231 wxT("unsupported format in wxURLDataObject"));
1233 return m_dataObjectLast
->SetData(len
, buf
);
1236 wxString
wxURLDataObject::GetURL() const
1239 wxCHECK_MSG( m_dataObjectLast
, url
, _T("no data in wxURLDataObject") );
1241 size_t len
= m_dataObjectLast
->GetDataSize();
1243 m_dataObjectLast
->GetDataHere(wxStringBuffer(url
, len
));
1248 void wxURLDataObject::SetURL(const wxString
& url
)
1250 SetData(wxDataFormat(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
),
1251 url
.Length()+1, url
.c_str());
1253 // CFSTR_SHELLURL is always supposed to be ANSI...
1254 wxWX2MBbuf urlA
= (wxWX2MBbuf
)url
.mbc_str();
1255 size_t len
= strlen(urlA
);
1256 SetData(wxDataFormat(CFSTR_SHELLURL
), len
+1, (const char*)urlA
);
1259 // ----------------------------------------------------------------------------
1260 // private functions
1261 // ----------------------------------------------------------------------------
1265 static const wxChar
*GetTymedName(DWORD tymed
)
1267 static wxChar s_szBuf
[128];
1269 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1270 case TYMED_FILE
: return wxT("TYMED_FILE");
1271 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1272 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1273 case TYMED_GDI
: return wxT("TYMED_GDI");
1274 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1275 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1277 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1284 #else // not using OLE at all
1286 // ----------------------------------------------------------------------------
1288 // ----------------------------------------------------------------------------
1292 wxDataObject::wxDataObject()
1296 wxDataObject::~wxDataObject()
1300 void wxDataObject::SetAutoDelete()
1305 const wxChar
*wxDataObject::GetFormatName(wxDataFormat
WXUNUSED(format
))
1309 #endif // __WXDEBUG__
1311 #endif // wxUSE_DATAOBJ
1313 #endif // wxUSE_OLE/!wxUSE_OLE