]>
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
);
162 return wxEmptyString
;
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
174 ADD_IID(EnumFORMATETC
)
177 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
179 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
183 m_formats
= new CLIPFORMAT
[nCount
];
184 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
185 m_formats
[n
] = formats
[n
].GetFormatId();
189 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
193 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
195 ULONG numFetched
= 0;
196 while (m_nCurrent
< m_nCount
&& numFetched
< celt
) {
198 format
.cfFormat
= m_formats
[m_nCurrent
++];
200 format
.dwAspect
= DVASPECT_CONTENT
;
202 format
.tymed
= TYMED_HGLOBAL
;
209 *pceltFetched
= numFetched
;
211 return numFetched
== celt
? S_OK
: S_FALSE
;
214 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
216 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
219 if ( m_nCurrent
< m_nCount
)
222 // no, can't skip this many elements
228 STDMETHODIMP
wxIEnumFORMATETC::Reset()
230 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
237 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
239 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
241 // unfortunately, we can't reuse the code in ctor - types are different
242 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
243 pNew
->m_nCount
= m_nCount
;
244 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
245 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
246 pNew
->m_formats
[n
] = m_formats
[n
];
254 // ----------------------------------------------------------------------------
256 // ----------------------------------------------------------------------------
258 BEGIN_IID_TABLE(wxIDataObject
)
263 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
265 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
267 m_pDataObject
= pDataObject
;
268 m_mustDelete
= false;
271 wxIDataObject::~wxIDataObject()
275 delete m_pDataObject
;
279 // get data functions
280 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
282 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
284 // is data is in our format?
285 HRESULT hr
= QueryGetData(pformatetcIn
);
289 // for the bitmaps and metafiles we use the handles instead of global memory
291 wxDataFormat format
= (wxDataFormat::NativeFormat
)pformatetcIn
->cfFormat
;
296 pmedium
->tymed
= TYMED_GDI
;
299 case wxDF_ENHMETAFILE
:
300 pmedium
->tymed
= TYMED_ENHMF
;
305 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
306 sizeof(METAFILEPICT
));
307 if ( !pmedium
->hGlobal
) {
308 wxLogLastError(wxT("GlobalAlloc"));
309 return E_OUTOFMEMORY
;
311 pmedium
->tymed
= TYMED_MFPICT
;
316 size_t size
= m_pDataObject
->GetDataSize(format
);
318 // it probably means that the method is just not implemented
319 wxLogDebug(wxT("Invalid data size - can't be 0"));
321 return DV_E_FORMATETC
;
324 // we may need extra space for the buffer size
325 size
+= m_pDataObject
->GetBufferOffset( format
);
327 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
328 if ( hGlobal
== NULL
) {
329 wxLogLastError(wxT("GlobalAlloc"));
330 return E_OUTOFMEMORY
;
334 pmedium
->tymed
= TYMED_HGLOBAL
;
335 pmedium
->hGlobal
= hGlobal
;
338 pmedium
->pUnkForRelease
= NULL
;
341 hr
= GetDataHere(pformatetcIn
, pmedium
);
343 // free resources we allocated
344 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
345 GlobalFree(pmedium
->hGlobal
);
354 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
357 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
359 // put data in caller provided medium
360 switch ( pmedium
->tymed
)
363 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
368 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
369 &pmedium
->hEnhMetaFile
) )
374 // fall through - we pass METAFILEPICT through HGLOBAL
379 HGLOBAL hGlobal
= pmedium
->hGlobal
;
380 void *pBuf
= GlobalLock(hGlobal
);
381 if ( pBuf
== NULL
) {
382 wxLogLastError(wxT("GlobalLock"));
383 return E_OUTOFMEMORY
;
386 wxDataFormat format
= pformatetc
->cfFormat
;
388 // possibly put the size in the beginning of the buffer
389 pBuf
= m_pDataObject
->SetSizeInBuffer
392 ::GlobalSize(hGlobal
),
396 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
399 GlobalUnlock(hGlobal
);
411 // set data functions
412 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
416 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
418 switch ( pmedium
->tymed
)
421 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
425 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
429 // fall through - we pass METAFILEPICT through HGLOBAL
432 wxDataFormat format
= pformatetc
->cfFormat
;
434 // this is quite weird, but for file drag and drop, explorer
435 // calls our SetData() with the formats we do *not* support!
437 // as we can't fix this bug in explorer (it's a bug because it
438 // should only use formats returned by EnumFormatEtc), do the
440 if ( !m_pDataObject
->IsSupported(format
, wxDataObject::Set
) ) {
442 return DV_E_FORMATETC
;
446 const void *pBuf
= GlobalLock(pmedium
->hGlobal
);
447 if ( pBuf
== NULL
) {
448 wxLogLastError(wxT("GlobalLock"));
450 return E_OUTOFMEMORY
;
453 // we've got a problem with SetData() here because the base
454 // class version requires the size parameter which we don't
455 // have anywhere in OLE data transfer - so we need to
456 // synthetise it for known formats and we suppose that all data
457 // in custom formats starts with a DWORD containing the size
463 size
= strlen((const char *)pBuf
);
465 #if !(defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
467 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
468 || ( defined(__MWERKS__) && defined(__WXMSW__) )
469 size
= std::wcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
471 size
= wxWcslen((const wchar_t *)pBuf
) * sizeof(wchar_t);
478 // these formats don't use size at all, anyhow (but
479 // pass data by handle, which is always a single DWORD)
485 // the handler will calculate size itself (it's too
486 // complicated to do it here)
491 case CF_METAFILEPICT
:
492 size
= sizeof(METAFILEPICT
);
496 pBuf
= m_pDataObject
->
497 GetSizeFromBuffer(pBuf
, &size
, format
);
498 size
-= m_pDataObject
->GetBufferOffset(format
);
501 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
503 GlobalUnlock(pmedium
->hGlobal
);
516 // we own the medium, so we must release it - but do *not* free any
517 // data we pass by handle because we have copied it elsewhere
518 switch ( pmedium
->tymed
)
521 pmedium
->hBitmap
= 0;
525 pmedium
->hMetaFilePict
= 0;
529 pmedium
->hEnhMetaFile
= 0;
533 ReleaseStgMedium(pmedium
);
539 // information functions
540 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
542 // do we accept data in this format?
543 if ( pformatetc
== NULL
) {
544 wxLogTrace(wxTRACE_OleCalls
,
545 wxT("wxIDataObject::QueryGetData: invalid ptr."));
550 // the only one allowed by current COM implementation
551 if ( pformatetc
->lindex
!= -1 ) {
552 wxLogTrace(wxTRACE_OleCalls
,
553 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
559 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
560 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
561 wxLogTrace(wxTRACE_OleCalls
,
562 wxT("wxIDataObject::QueryGetData: bad dwAspect %ld"),
563 pformatetc
->dwAspect
);
565 return DV_E_DVASPECT
;
568 // and now check the type of data requested
569 wxDataFormat format
= pformatetc
->cfFormat
;
570 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
571 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
572 wxGetFormatName(format
));
575 wxLogTrace(wxTRACE_OleCalls
,
576 wxT("wxIDataObject::QueryGetData: %s unsupported"),
577 wxGetFormatName(format
));
579 return DV_E_FORMATETC
;
582 // we only transfer data by global memory, except for some particular cases
583 DWORD tymed
= pformatetc
->tymed
;
584 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
585 !(tymed
& TYMED_HGLOBAL
) ) {
586 // it's not what we're waiting for
587 wxLogTrace(wxTRACE_OleCalls
,
588 wxT("wxIDataObject::QueryGetData: %s != %s"),
590 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
599 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
600 FORMATETC
*pFormatetcOut
)
602 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
604 // TODO we might want something better than this trivial implementation here
605 if ( pFormatetcOut
!= NULL
)
606 pFormatetcOut
->ptd
= NULL
;
608 return DATA_S_SAMEFORMATETC
;
611 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
612 IEnumFORMATETC
**ppenumFormatEtc
)
614 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
616 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
619 ULONG nFormatCount
= wx_truncate_cast(ULONG
, m_pDataObject
->GetFormatCount(dir
));
621 wxDataFormat
*formats
;
622 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
623 m_pDataObject
->GetAllFormats(formats
, dir
);
625 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
627 *ppenumFormatEtc
= pEnum
;
629 if ( formats
!= &format
) {
636 // ----------------------------------------------------------------------------
637 // advise sink functions (not implemented)
638 // ----------------------------------------------------------------------------
640 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
641 DWORD
WXUNUSED(advf
),
642 IAdviseSink
*WXUNUSED(pAdvSink
),
643 DWORD
*WXUNUSED(pdwConnection
))
645 return OLE_E_ADVISENOTSUPPORTED
;
648 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
650 return OLE_E_ADVISENOTSUPPORTED
;
653 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
655 return OLE_E_ADVISENOTSUPPORTED
;
658 // ----------------------------------------------------------------------------
660 // ----------------------------------------------------------------------------
662 wxDataObject::wxDataObject()
664 m_pIDataObject
= new wxIDataObject(this);
665 m_pIDataObject
->AddRef();
668 wxDataObject::~wxDataObject()
670 ReleaseInterface(m_pIDataObject
);
673 void wxDataObject::SetAutoDelete()
675 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
676 m_pIDataObject
->Release();
678 // so that the dtor doesnt' crash
679 m_pIDataObject
= NULL
;
682 size_t wxDataObject::GetBufferOffset(const wxDataFormat
& format
)
684 // if we prepend the size of the data to the buffer itself, account for it
685 return NeedsVerbatimData(format
) ? 0 : sizeof(size_t);
688 const void* wxDataObject::GetSizeFromBuffer( const void* buffer
, size_t* size
,
689 const wxDataFormat
& format
)
691 // hack: the third parameter is declared non-const in Wine's headers so
692 // cast away the const
693 size_t realsz
= ::HeapSize(::GetProcessHeap(), 0,
694 wx_const_cast(void*, buffer
));
695 if ( realsz
== (size_t)-1 )
697 // note that HeapSize() does not set last error
698 wxLogApiError(wxT("HeapSize"), 0);
704 // check if this data has its size prepended (as it was by default for wx
705 // programs prior 2.6.3):
706 size_t *p
= (size_t *)buffer
;
709 if ( NeedsVerbatimData(format
) )
710 wxLogDebug(wxT("Apparent data format mismatch: size not needed"));
712 p
++; // this data has its size prepended; skip first DWORD
718 void* wxDataObject::SetSizeInBuffer( void* buffer
, size_t size
,
719 const wxDataFormat
& format
)
721 size_t* p
= (size_t *)buffer
;
722 if ( !NeedsVerbatimData(format
) )
724 // prepend the size to the data and skip it
733 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
735 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
737 #pragma warning(disable:4063)
740 static wxChar s_szBuf
[256];
742 case CF_TEXT
: return wxT("CF_TEXT");
743 case CF_BITMAP
: return wxT("CF_BITMAP");
744 case CF_SYLK
: return wxT("CF_SYLK");
745 case CF_DIF
: return wxT("CF_DIF");
746 case CF_TIFF
: return wxT("CF_TIFF");
747 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
748 case CF_DIB
: return wxT("CF_DIB");
749 case CF_PALETTE
: return wxT("CF_PALETTE");
750 case CF_PENDATA
: return wxT("CF_PENDATA");
751 case CF_RIFF
: return wxT("CF_RIFF");
752 case CF_WAVE
: return wxT("CF_WAVE");
753 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
755 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
756 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
757 case CF_LOCALE
: return wxT("CF_LOCALE");
758 case CF_HDROP
: return wxT("CF_HDROP");
762 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
764 // it must be a new predefined format we don't know the name of
765 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
772 #pragma warning(default:4063)
778 // ----------------------------------------------------------------------------
779 // wxBitmapDataObject supports CF_DIB format
780 // ----------------------------------------------------------------------------
782 // TODO: support CF_DIB under Windows CE as well
784 size_t wxBitmapDataObject::GetDataSize() const
786 #if wxUSE_WXDIB && !defined(__WXWINCE__)
787 return wxDIB::ConvertFromBitmap(NULL
, GetHbitmapOf(GetBitmap()));
793 bool wxBitmapDataObject::GetDataHere(void *buf
) const
795 #if wxUSE_WXDIB && !defined(__WXWINCE__)
796 BITMAPINFO
* const pbi
= (BITMAPINFO
*)buf
;
798 return wxDIB::ConvertFromBitmap(pbi
, GetHbitmapOf(GetBitmap())) != 0;
805 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
807 #if wxUSE_WXDIB && !defined(__WXWINCE__)
808 const BITMAPINFO
* const pbmi
= (const BITMAPINFO
*)buf
;
810 HBITMAP hbmp
= wxDIB::ConvertToBitmap(pbmi
);
812 wxCHECK_MSG( hbmp
, FALSE
, wxT("pasting/dropping invalid bitmap") );
814 const BITMAPINFOHEADER
* const pbmih
= &pbmi
->bmiHeader
;
815 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
816 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
818 // TODO: create wxPalette if the bitmap has any
829 // ----------------------------------------------------------------------------
830 // wxBitmapDataObject2 supports CF_BITMAP format
831 // ----------------------------------------------------------------------------
833 // the bitmaps aren't passed by value as other types of data (i.e. by copying
834 // the data into a global memory chunk and passing it to the clipboard or
835 // another application or whatever), but by handle, so these generic functions
836 // don't make much sense to them.
838 size_t wxBitmapDataObject2::GetDataSize() const
843 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
845 // we put a bitmap handle into pBuf
846 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
851 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
853 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
856 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
858 wxLogLastError(wxT("GetObject(HBITMAP)"));
861 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
862 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
864 if ( !bitmap
.Ok() ) {
865 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
877 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
879 if ( format
.GetFormatId() == CF_DIB
)
884 // shouldn't be selected into a DC or GetDIBits() would fail
885 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
886 wxT("can't copy bitmap selected into wxMemoryDC") );
888 // first get the info
890 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
891 NULL
, &bi
, DIB_RGB_COLORS
) )
893 wxLogLastError(wxT("GetDIBits(NULL)"));
898 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
902 // no data to copy - we don't pass HBITMAP via global memory
907 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
910 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
912 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
913 if ( format
.GetFormatId() == CF_DIB
)
918 // shouldn't be selected into a DC or GetDIBits() would fail
919 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
920 wxT("can't copy bitmap selected into wxMemoryDC") );
922 // first get the info
923 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
924 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
926 wxLogLastError(wxT("GetDIBits(NULL)"));
931 // and now copy the bits
932 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
933 pbi
, DIB_RGB_COLORS
) )
935 wxLogLastError(wxT("GetDIBits"));
942 // we put a bitmap handle into pBuf
943 *(HBITMAP
*)pBuf
= hbmp
;
949 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
950 size_t size
, const void *pBuf
)
953 if ( format
.GetFormatId() == CF_DIB
)
955 // here we get BITMAPINFO struct followed by the actual bitmap bits and
956 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
959 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
960 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
961 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
962 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
965 wxLogLastError(wxT("CreateDIBitmap"));
968 m_bitmap
.SetWidth(pbmih
->biWidth
);
969 m_bitmap
.SetHeight(pbmih
->biHeight
);
973 // it's easy with bitmaps: we pass them by handle
974 hbmp
= *(HBITMAP
*)pBuf
;
977 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
979 wxLogLastError(wxT("GetObject(HBITMAP)"));
982 m_bitmap
.SetWidth(bmp
.bmWidth
);
983 m_bitmap
.SetHeight(bmp
.bmHeight
);
984 m_bitmap
.SetDepth(bmp
.bmPlanes
);
987 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
989 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
996 // ----------------------------------------------------------------------------
998 // ----------------------------------------------------------------------------
1000 bool wxFileDataObject::SetData(size_t WXUNUSED(size
),
1001 const void *WXUNUSED_IN_WINCE(pData
))
1004 m_filenames
.Empty();
1006 // the documentation states that the first member of DROPFILES structure is
1007 // a "DWORD offset of double NUL terminated file list". What they mean by
1008 // this (I wonder if you see it immediately) is that the list starts at
1009 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
1010 // to use DragQueryFile to work with this structure, but not told where and
1011 // how to get HDROP.
1012 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
1014 // get number of files (magic value -1)
1015 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
1017 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
1019 // for each file get the length, allocate memory and then get the name
1022 for ( n
= 0; n
< nFiles
; n
++ ) {
1023 // +1 for terminating NUL
1024 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
1026 UINT len2
= ::DragQueryFile(hdrop
, n
, wxStringBuffer(str
, len
), len
);
1027 m_filenames
.Add(str
);
1029 if ( len2
!= len
- 1 ) {
1030 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
1031 %d characters, %d expected."), len2
, len
- 1);
1041 void wxFileDataObject::AddFile(const wxString
& file
)
1043 // just add file to filenames array
1044 // all useful data (such as DROPFILES struct) will be
1045 // created later as necessary
1046 m_filenames
.Add(file
);
1049 size_t wxFileDataObject::GetDataSize() const
1052 // size returned will be the size of the DROPFILES structure, plus the list
1053 // of filesnames (null byte separated), plus a double null at the end
1055 // if no filenames in list, size is 0
1056 if ( m_filenames
.empty() )
1059 #if wxUSE_UNICODE_MSLU
1061 if ( wxGetOsVersion() == wxOS_WINDOWS_9X
)
1063 // Win9x always uses ANSI file names and MSLU doesn't help with this
1064 sizeOfChar
= sizeof(char);
1068 sizeOfChar
= sizeof(wxChar
);
1070 #else // !wxUSE_UNICODE_MSLU
1071 static const size_t sizeOfChar
= sizeof(wxChar
);
1072 #endif // wxUSE_UNICODE_MSLU/!wxUSE_UNICODE_MSLU
1074 // inital size of DROPFILES struct + null byte
1075 size_t sz
= sizeof(DROPFILES
) + sizeOfChar
;
1077 const size_t count
= m_filenames
.size();
1078 for ( size_t i
= 0; i
< count
; i
++ )
1080 // add filename length plus null byte
1082 #if wxUSE_UNICODE_MSLU
1083 if ( sizeOfChar
== sizeof(char) )
1084 len
= strlen(wxConvFileName
->cWC2MB(m_filenames
[i
]));
1086 #endif // wxUSE_UNICODE_MSLU
1087 len
= m_filenames
[i
].length();
1089 sz
+= (len
+ 1) * sizeOfChar
;
1098 bool wxFileDataObject::GetDataHere(void *WXUNUSED_IN_WINCE(pData
)) const
1101 // pData points to an externally allocated memory block
1102 // created using the size returned by GetDataSize()
1104 // if pData is NULL, or there are no files, return
1105 if ( !pData
|| m_filenames
.empty() )
1108 // convert data pointer to a DROPFILES struct pointer
1109 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1111 // initialize DROPFILES struct
1112 pDrop
->pFiles
= sizeof(DROPFILES
);
1113 pDrop
->fNC
= FALSE
; // not non-client coords
1114 #if wxUSE_UNICODE_MSLU
1115 pDrop
->fWide
= wxGetOsVersion() != wxOS_WINDOWS_9X
? TRUE
: FALSE
;
1117 pDrop
->fWide
= wxUSE_UNICODE
;
1120 const size_t sizeOfChar
= pDrop
->fWide
? sizeof(wchar_t) : sizeof(char);
1122 // set start of filenames list (null separated)
1123 BYTE
*pbuf
= (BYTE
*)(pDrop
+ 1);
1125 const size_t count
= m_filenames
.size();
1126 for ( size_t i
= 0; i
< count
; i
++ )
1128 // copy filename to pbuf and add null terminator
1130 #if wxUSE_UNICODE_MSLU
1131 if ( sizeOfChar
== sizeof(char) )
1133 wxCharBuffer
buf(wxConvFileName
->cWC2MB(m_filenames
[i
]));
1135 memcpy(pbuf
, buf
, len
*sizeOfChar
);
1138 #endif // wxUSE_UNICODE_MSLU
1140 len
= m_filenames
[i
].length();
1141 memcpy(pbuf
, m_filenames
[i
].c_str(), len
*sizeOfChar
);
1144 pbuf
+= len
*sizeOfChar
;
1146 memset(pbuf
, 0, sizeOfChar
);
1150 // add final null terminator
1151 memset(pbuf
, 0, sizeOfChar
);
1159 // ----------------------------------------------------------------------------
1161 // ----------------------------------------------------------------------------
1163 // Work around bug in Wine headers
1164 #if defined(__WINE__) && defined(CFSTR_SHELLURL) && wxUSE_UNICODE
1165 #undef CFSTR_SHELLURL
1166 #define CFSTR_SHELLURL _T("CFSTR_SHELLURL")
1169 class CFSTR_SHELLURLDataObject
: public wxCustomDataObject
1172 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL
) {}
1174 virtual size_t GetBufferOffset( const wxDataFormat
& WXUNUSED(format
) )
1179 virtual const void* GetSizeFromBuffer( const void* buffer
, size_t* size
,
1180 const wxDataFormat
& WXUNUSED(format
) )
1182 // CFSTR_SHELLURL is _always_ ANSI text
1183 *size
= strlen( (const char*)buffer
);
1188 virtual void* SetSizeInBuffer( void* buffer
, size_t WXUNUSED(size
),
1189 const wxDataFormat
& WXUNUSED(format
) )
1195 virtual bool GetDataHere( void* buffer
) const
1197 // CFSTR_SHELLURL is _always_ ANSI!
1198 wxCharBuffer
char_buffer( GetDataSize() );
1199 wxCustomDataObject::GetDataHere( (void*)char_buffer
.data() );
1200 wxString
unicode_buffer( char_buffer
, wxConvLibc
);
1201 memcpy( buffer
, unicode_buffer
.c_str(),
1202 ( unicode_buffer
.length() + 1 ) * sizeof(wxChar
) );
1206 virtual bool GetDataHere(const wxDataFormat
& WXUNUSED(format
),
1208 { return GetDataHere(buf
); }
1211 DECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject
)
1216 wxURLDataObject::wxURLDataObject(const wxString
& url
)
1218 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1219 // but it seems that some browsers only provide one of them so we have to
1221 Add(new wxTextDataObject
);
1222 Add(new CFSTR_SHELLURLDataObject());
1224 // we don't have any data yet
1225 m_dataObjectLast
= NULL
;
1231 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1235 m_dataObjectLast
= GetObject(format
);
1237 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1238 wxT("unsupported format in wxURLDataObject"));
1240 return m_dataObjectLast
->SetData(len
, buf
);
1243 wxString
wxURLDataObject::GetURL() const
1246 wxCHECK_MSG( m_dataObjectLast
, url
, _T("no data in wxURLDataObject") );
1248 size_t len
= m_dataObjectLast
->GetDataSize();
1250 m_dataObjectLast
->GetDataHere(wxStringBuffer(url
, len
));
1255 void wxURLDataObject::SetURL(const wxString
& url
)
1257 SetData(wxDataFormat(wxUSE_UNICODE
? wxDF_UNICODETEXT
: wxDF_TEXT
),
1258 url
.Length()+1, url
.c_str());
1260 // CFSTR_SHELLURL is always supposed to be ANSI...
1261 wxWX2MBbuf urlA
= (wxWX2MBbuf
)url
.mbc_str();
1262 size_t len
= strlen(urlA
);
1263 SetData(wxDataFormat(CFSTR_SHELLURL
), len
+1, (const char*)urlA
);
1266 // ----------------------------------------------------------------------------
1267 // private functions
1268 // ----------------------------------------------------------------------------
1272 static const wxChar
*GetTymedName(DWORD tymed
)
1274 static wxChar s_szBuf
[128];
1276 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1277 case TYMED_FILE
: return wxT("TYMED_FILE");
1278 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1279 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1280 case TYMED_GDI
: return wxT("TYMED_GDI");
1281 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1282 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1284 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1291 #else // not using OLE at all
1293 // ----------------------------------------------------------------------------
1295 // ----------------------------------------------------------------------------
1299 wxDataObject::wxDataObject()
1303 wxDataObject::~wxDataObject()
1307 void wxDataObject::SetAutoDelete()
1312 const wxChar
*wxDataObject::GetFormatName(wxDataFormat
WXUNUSED(format
))
1316 #endif // __WXDEBUG__
1318 #endif // wxUSE_DATAOBJ
1320 #endif // wxUSE_OLE/!wxUSE_OLE