]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ole/dataobj.cpp
3 // Purpose: implementation of wx[I]DataObject class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dataobj.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
27 #if defined(__BORLANDC__)
36 #include "wx/dataobj.h"
38 #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
40 #include "wx/msw/private.h" // includes <windows.h>
42 #if wxUSE_NORLANDER_HEADERS
54 #include "wx/msw/ole/oleutils.h"
56 #include "wx/msw/dib.h"
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
63 static const wxChar
*GetTymedName(DWORD tymed
);
65 #define GetTymedName(tymed) ""
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
92 // ----------------------------------------------------------------------------
93 // wxIDataObject implementation of IDataObject interface
94 // ----------------------------------------------------------------------------
96 class wxIDataObject
: public IDataObject
99 wxIDataObject(wxDataObject
*pDataObject
);
100 virtual ~wxIDataObject();
102 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
103 // is), but in some cases, the situation is inversed, that is we delete it
104 // when this object is deleted - setting this flag enables such logic
105 void SetDeleteFlag() { m_mustDelete
= TRUE
; }
107 DECLARE_IUNKNOWN_METHODS
;
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
);
121 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
126 // ============================================================================
128 // ============================================================================
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 void wxDataFormat::SetId(const wxChar
*format
)
136 m_format
= (wxDataFormat::NativeFormat
)::RegisterClipboardFormat(format
);
139 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
143 wxString
wxDataFormat::GetId() const
145 static const int max
= 256;
149 wxCHECK_MSG( !IsStandard(), s
,
150 wxT("name of predefined format cannot be retrieved") );
152 int len
= ::GetClipboardFormatName(m_format
, s
.GetWriteBuf(max
), max
);
157 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
163 // ----------------------------------------------------------------------------
165 // ----------------------------------------------------------------------------
167 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
169 ADD_IID(EnumFORMATETC
)
172 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
174 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
179 m_formats
= new CLIPFORMAT
[nCount
];
180 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
181 m_formats
[n
] = formats
[n
].GetFormatId();
185 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
187 ULONG
*WXUNUSED(pceltFetched
))
189 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
192 // we only return 1 element at a time - mainly because I'm too lazy to
193 // implement something which you're never asked for anyhow
197 if ( m_nCurrent
< m_nCount
) {
199 format
.cfFormat
= m_formats
[m_nCurrent
++];
201 format
.dwAspect
= DVASPECT_CONTENT
;
203 format
.tymed
= TYMED_HGLOBAL
;
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
)
268 m_pDataObject
= pDataObject
;
269 m_mustDelete
= FALSE
;
272 wxIDataObject::~wxIDataObject()
276 delete m_pDataObject
;
280 // get data functions
281 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
283 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
285 // is data is in our format?
286 HRESULT hr
= QueryGetData(pformatetcIn
);
290 // for the bitmaps and metafiles we use the handles instead of global memory
292 wxDataFormat format
= (wxDataFormatId
)pformatetcIn
->cfFormat
;
297 pmedium
->tymed
= TYMED_GDI
;
300 case wxDF_ENHMETAFILE
:
301 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 if ( !format
.IsStandard() ) {
325 // for custom formats, put the size with the data - alloc the
327 size
+= sizeof(size_t);
330 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
331 if ( hGlobal
== NULL
) {
332 wxLogLastError(wxT("GlobalAlloc"));
333 return E_OUTOFMEMORY
;
337 pmedium
->tymed
= TYMED_HGLOBAL
;
338 pmedium
->hGlobal
= hGlobal
;
341 pmedium
->pUnkForRelease
= NULL
;
344 hr
= GetDataHere(pformatetcIn
, pmedium
);
346 // free resources we allocated
347 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
348 GlobalFree(pmedium
->hGlobal
);
357 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
360 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
362 // put data in caller provided medium
363 switch ( pmedium
->tymed
)
366 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
371 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
372 &pmedium
->hEnhMetaFile
) )
377 // fall through - we pass METAFILEPICT through HGLOBAL
382 HGLOBAL hGlobal
= pmedium
->hGlobal
;
383 void *pBuf
= GlobalLock(hGlobal
);
384 if ( pBuf
== NULL
) {
385 wxLogLastError(wxT("GlobalLock"));
386 return E_OUTOFMEMORY
;
389 if ( !wxDataFormat(pformatetc
->cfFormat
).IsStandard() ) {
390 // for custom formats, put the size with the data
391 size_t *p
= (size_t *)pBuf
;
392 *p
++ = GlobalSize(hGlobal
);
396 wxDataFormat format
= pformatetc
->cfFormat
;
397 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
400 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
->IsSupportedFormat(format
) ) {
442 return DV_E_FORMATETC
;
446 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(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
467 #if (defined(__BORLANDC__) && (__BORLANDC__ > 0x530))
468 size
= std::wcslen((const wchar_t *)pBuf
);
470 size
= ::wcslen((const wchar_t *)pBuf
);
476 // these formats don't use size at all, anyhow (but
477 // pass data by handle, which is always a single DWORD)
482 // the handler will calculate size itself (it's too
483 // complicated to do it here)
487 case CF_METAFILEPICT
:
488 size
= sizeof(METAFILEPICT
);
493 // we suppose that the size precedes the data
494 size_t *p
= (size_t *)pBuf
;
497 if (! format
.IsStandard() ) {
498 // see GetData for coresponding increment
499 size
-= sizeof(size_t);
504 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
506 GlobalUnlock(pmedium
->hGlobal
);
519 // we own the medium, so we must release it - but do *not* free any
520 // data we pass by handle because we have copied it elsewhere
521 switch ( pmedium
->tymed
)
524 pmedium
->hBitmap
= 0;
528 pmedium
->hMetaFilePict
= 0;
532 pmedium
->hEnhMetaFile
= 0;
536 ReleaseStgMedium(pmedium
);
542 // information functions
543 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
545 // do we accept data in this format?
546 if ( pformatetc
== NULL
) {
547 wxLogTrace(wxTRACE_OleCalls
,
548 wxT("wxIDataObject::QueryGetData: invalid ptr."));
553 // the only one allowed by current COM implementation
554 if ( pformatetc
->lindex
!= -1 ) {
555 wxLogTrace(wxTRACE_OleCalls
,
556 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
562 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
563 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
564 wxLogTrace(wxTRACE_OleCalls
,
565 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
566 pformatetc
->dwAspect
);
568 return DV_E_DVASPECT
;
571 // and now check the type of data requested
572 wxDataFormat format
= pformatetc
->cfFormat
;
573 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
574 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
575 wxGetFormatName(format
));
578 wxLogTrace(wxTRACE_OleCalls
,
579 wxT("wxIDataObject::QueryGetData: %s unsupported"),
580 wxGetFormatName(format
));
582 return DV_E_FORMATETC
;
585 // we only transfer data by global memory, except for some particular cases
586 DWORD tymed
= pformatetc
->tymed
;
587 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
588 !(tymed
& TYMED_HGLOBAL
) ) {
589 // it's not what we're waiting for
590 wxLogTrace(wxTRACE_OleCalls
,
591 wxT("wxIDataObject::QueryGetData: %s != %s"),
593 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
602 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
603 FORMATETC
*pFormatetcOut
)
605 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
607 // TODO we might want something better than this trivial implementation here
608 if ( pFormatetcOut
!= NULL
)
609 pFormatetcOut
->ptd
= NULL
;
611 return DATA_S_SAMEFORMATETC
;
614 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
615 IEnumFORMATETC
**ppenumFormatEtc
)
617 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
619 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
622 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
624 wxDataFormat
*formats
;
625 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
626 m_pDataObject
->GetAllFormats(formats
, dir
);
628 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
630 *ppenumFormatEtc
= pEnum
;
632 if ( formats
!= &format
) {
639 // ----------------------------------------------------------------------------
640 // advise sink functions (not implemented)
641 // ----------------------------------------------------------------------------
643 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
644 DWORD
WXUNUSED(advf
),
645 IAdviseSink
*WXUNUSED(pAdvSink
),
646 DWORD
*WXUNUSED(pdwConnection
))
648 return OLE_E_ADVISENOTSUPPORTED
;
651 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
653 return OLE_E_ADVISENOTSUPPORTED
;
656 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
658 return OLE_E_ADVISENOTSUPPORTED
;
661 // ----------------------------------------------------------------------------
663 // ----------------------------------------------------------------------------
665 wxDataObject::wxDataObject()
667 m_pIDataObject
= new wxIDataObject(this);
668 m_pIDataObject
->AddRef();
671 wxDataObject::~wxDataObject()
673 ReleaseInterface(m_pIDataObject
);
676 void wxDataObject::SetAutoDelete()
678 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
679 m_pIDataObject
->Release();
681 // so that the dtor doesnt' crash
682 m_pIDataObject
= NULL
;
687 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
689 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
691 #pragma warning(disable:4063)
694 static wxChar s_szBuf
[256];
696 case CF_TEXT
: return wxT("CF_TEXT");
697 case CF_BITMAP
: return wxT("CF_BITMAP");
698 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
699 case CF_SYLK
: return wxT("CF_SYLK");
700 case CF_DIF
: return wxT("CF_DIF");
701 case CF_TIFF
: return wxT("CF_TIFF");
702 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
703 case CF_DIB
: return wxT("CF_DIB");
704 case CF_PALETTE
: return wxT("CF_PALETTE");
705 case CF_PENDATA
: return wxT("CF_PENDATA");
706 case CF_RIFF
: return wxT("CF_RIFF");
707 case CF_WAVE
: return wxT("CF_WAVE");
708 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
709 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
710 case CF_HDROP
: return wxT("CF_HDROP");
711 case CF_LOCALE
: return wxT("CF_LOCALE");
714 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
716 // it must be a new predefined format we don't know the name of
717 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
724 #pragma warning(default:4063)
730 // ----------------------------------------------------------------------------
731 // wxBitmapDataObject supports CF_DIB format
732 // ----------------------------------------------------------------------------
734 size_t wxBitmapDataObject::GetDataSize() const
736 return wxConvertBitmapToDIB(NULL
, GetBitmap());
739 bool wxBitmapDataObject::GetDataHere(void *buf
) const
741 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
744 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
746 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
748 if ( !bitmap
.Ok() ) {
749 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
759 // ----------------------------------------------------------------------------
760 // wxBitmapDataObject2 supports CF_BITMAP format
761 // ----------------------------------------------------------------------------
763 // the bitmaps aren't passed by value as other types of data (i.e. by copying
764 // the data into a global memory chunk and passing it to the clipboard or
765 // another application or whatever), but by handle, so these generic functions
766 // don't make much sense to them.
768 size_t wxBitmapDataObject2::GetDataSize() const
773 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
775 // we put a bitmap handle into pBuf
776 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
781 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
783 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
786 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
788 wxLogLastError(wxT("GetObject(HBITMAP)"));
791 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
792 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
794 if ( !bitmap
.Ok() ) {
795 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
807 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
809 if ( format
.GetFormatId() == CF_DIB
)
814 // shouldn't be selected into a DC or GetDIBits() would fail
815 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
816 wxT("can't copy bitmap selected into wxMemoryDC") );
818 // first get the info
820 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
821 NULL
, &bi
, DIB_RGB_COLORS
) )
823 wxLogLastError(wxT("GetDIBits(NULL)"));
828 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
832 // no data to copy - we don't pass HBITMAP via global memory
837 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
840 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
842 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
843 if ( format
.GetFormatId() == CF_DIB
)
848 // shouldn't be selected into a DC or GetDIBits() would fail
849 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
850 wxT("can't copy bitmap selected into wxMemoryDC") );
852 // first get the info
853 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
854 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
856 wxLogLastError(wxT("GetDIBits(NULL)"));
861 // and now copy the bits
862 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
863 pbi
, DIB_RGB_COLORS
) )
865 wxLogLastError(wxT("GetDIBits"));
872 // we put a bitmap handle into pBuf
873 *(HBITMAP
*)pBuf
= hbmp
;
879 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
880 size_t size
, const void *pBuf
)
883 if ( format
.GetFormatId() == CF_DIB
)
885 // here we get BITMAPINFO struct followed by the actual bitmap bits and
886 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
889 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
890 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
891 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
892 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
895 wxLogLastError(wxT("CreateDIBitmap"));
898 m_bitmap
.SetWidth(pbmih
->biWidth
);
899 m_bitmap
.SetHeight(pbmih
->biHeight
);
903 // it's easy with bitmaps: we pass them by handle
904 hbmp
= *(HBITMAP
*)pBuf
;
907 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
909 wxLogLastError(wxT("GetObject(HBITMAP)"));
912 m_bitmap
.SetWidth(bmp
.bmWidth
);
913 m_bitmap
.SetHeight(bmp
.bmHeight
);
914 m_bitmap
.SetDepth(bmp
.bmPlanes
);
917 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
919 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
926 // ----------------------------------------------------------------------------
928 // ----------------------------------------------------------------------------
930 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
934 // the documentation states that the first member of DROPFILES structure is
935 // a "DWORD offset of double NUL terminated file list". What they mean by
936 // this (I wonder if you see it immediately) is that the list starts at
937 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
938 // to use DragQueryFile to work with this structure, but not told where and
940 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
942 // get number of files (magic value -1)
943 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
945 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
947 // for each file get the length, allocate memory and then get the name
950 for ( n
= 0; n
< nFiles
; n
++ ) {
951 // +1 for terminating NUL
952 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
954 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
956 m_filenames
.Add(str
);
958 if ( len2
!= len
- 1 ) {
959 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
960 %d characters, %d expected."), len2
, len
- 1);
967 void wxFileDataObject::AddFile(const wxString
& file
)
969 // just add file to filenames array
970 // all useful data (such as DROPFILES struct) will be
971 // created later as necessary
972 m_filenames
.Add(file
);
975 size_t wxFileDataObject::GetDataSize() const
977 // size returned will be the size of the DROPFILES structure,
978 // plus the list of filesnames (null byte separated), plus
979 // a double null at the end
981 // if no filenames in list, size is 0
982 if ( m_filenames
.GetCount() == 0 )
985 // inital size of DROPFILES struct + null byte
986 size_t sz
= sizeof(DROPFILES
) + 1;
988 size_t count
= m_filenames
.GetCount();
989 for ( size_t i
= 0; i
< count
; i
++ )
991 // add filename length plus null byte
992 sz
+= m_filenames
[i
].Len() + 1;
998 bool wxFileDataObject::GetDataHere(void *pData
) const
1000 // pData points to an externally allocated memory block
1001 // created using the size returned by GetDataSize()
1003 // if pData is NULL, or there are no files, return
1004 if ( !pData
|| m_filenames
.GetCount() == 0 )
1007 // convert data pointer to a DROPFILES struct pointer
1008 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1010 // initialize DROPFILES struct
1011 pDrop
->pFiles
= sizeof(DROPFILES
);
1012 pDrop
->fNC
= FALSE
; // not non-client coords
1014 pDrop
->fWide
= TRUE
;
1016 pDrop
->fWide
= FALSE
;
1017 #endif // Unicode/Ansi
1019 // set start of filenames list (null separated)
1020 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1022 size_t count
= m_filenames
.GetCount();
1023 for (size_t i
= 0; i
< count
; i
++ )
1025 // copy filename to pbuf and add null terminator
1026 size_t len
= m_filenames
[i
].Len();
1027 memcpy(pbuf
, m_filenames
[i
], len
);
1029 *pbuf
++ = wxT('\0');
1032 *pbuf
= wxT('\0'); // add final null terminator
1037 // ----------------------------------------------------------------------------
1038 // private functions
1039 // ----------------------------------------------------------------------------
1041 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1043 switch ( bitsPerPixel
)
1046 // monochrome bitmap, 2 entries
1056 // may be used with 24bit bitmaps, but we don't use it here - fall
1061 // bmiColors not used at all with these bitmaps
1065 wxFAIL_MSG( wxT("unknown bitmap format") );
1070 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1072 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1074 // shouldn't be selected into a DC or GetDIBits() would fail
1075 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1076 wxT("can't copy bitmap selected into wxMemoryDC") );
1078 // prepare all the info we need
1080 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1081 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1083 wxLogLastError(wxT("GetObject(bitmap)"));
1088 // calculate the number of bits per pixel and the number of items in
1089 // bmiColors array (whose meaning depends on the bitmap format)
1090 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1091 WORD biColors
= (WORD
)wxGetNumOfBitmapColors(biBits
);
1095 bool wantSizeOnly
= pbi
== NULL
;
1099 // just for convenience
1100 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1102 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1103 bi
.biWidth
= bm
.bmWidth
;
1104 bi
.biHeight
= bm
.bmHeight
;
1106 bi
.biBitCount
= biBits
;
1107 bi
.biCompression
= BI_RGB
;
1109 bi
.biXPelsPerMeter
= 0;
1110 bi
.biYPelsPerMeter
= 0;
1112 bi
.biClrImportant
= 0;
1114 // memory we need for BITMAPINFO only
1115 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1117 // first get the image size
1119 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1121 wxLogLastError(wxT("GetDIBits(NULL)"));
1128 // size of the header + size of the image
1129 return dwLen
+ bi
.biSizeImage
;
1132 // and now copy the bits
1133 void *image
= (char *)pbi
+ dwLen
;
1134 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1136 wxLogLastError(wxT("GetDIBits"));
1141 return dwLen
+ bi
.biSizeImage
;
1144 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1146 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1147 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1148 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1150 // offset of image from the beginning of the header
1151 DWORD ofs
= wxGetNumOfBitmapColors(pbmih
->biBitCount
) * sizeof(RGBQUAD
);
1152 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1155 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1156 image
, pbmi
, DIB_RGB_COLORS
);
1159 wxLogLastError(wxT("CreateDIBitmap"));
1162 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1163 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1170 static const wxChar
*GetTymedName(DWORD tymed
)
1172 static wxChar s_szBuf
[128];
1174 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1175 case TYMED_FILE
: return wxT("TYMED_FILE");
1176 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1177 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1178 case TYMED_GDI
: return wxT("TYMED_GDI");
1179 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1180 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1182 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1189 #else // not using OLE at all
1190 // ----------------------------------------------------------------------------
1192 // ----------------------------------------------------------------------------
1194 wxDataObject::wxDataObject()
1198 wxDataObject::~wxDataObject()
1202 void wxDataObject::SetAutoDelete()
1207 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)