]>
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__)
31 #if defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
38 #include "wx/dataobj.h"
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
);
77 // to suppress the gcc warning about "class has virtual functions but non
82 ~wxIEnumFORMATETC() { delete [] m_formats
; }
84 DECLARE_IUNKNOWN_METHODS
;
87 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
88 STDMETHODIMP
Skip(ULONG celt
);
90 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
93 CLIPFORMAT
*m_formats
; // formats we can provide data in
94 ULONG m_nCount
, // number of formats we support
95 m_nCurrent
; // current enum position
98 // ----------------------------------------------------------------------------
99 // wxIDataObject implementation of IDataObject interface
100 // ----------------------------------------------------------------------------
102 class wxIDataObject
: public IDataObject
105 wxIDataObject(wxDataObject
*pDataObject
);
107 // to suppress the gcc warning about "class has virtual functions but non
114 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
115 // is), but in some cases, the situation is inversed, that is we delete it
116 // when this object is deleted - setting this flag enables such logic
117 void SetDeleteFlag() { m_mustDelete
= TRUE
; }
119 DECLARE_IUNKNOWN_METHODS
;
122 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
123 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
124 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
125 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
126 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
127 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
128 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
129 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
130 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
133 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
138 // ============================================================================
140 // ============================================================================
142 // ----------------------------------------------------------------------------
144 // ----------------------------------------------------------------------------
146 void wxDataFormat::SetId(const wxChar
*format
)
148 m_format
= ::RegisterClipboardFormat(format
);
151 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
155 wxString
wxDataFormat::GetId() const
157 static const int max
= 256;
161 wxCHECK_MSG( !IsStandard(), s
,
162 wxT("name of predefined format cannot be retrieved") );
164 int len
= ::GetClipboardFormatName(m_format
, s
.GetWriteBuf(max
), max
);
169 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
181 ADD_IID(EnumFORMATETC
)
184 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
186 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
191 m_formats
= new CLIPFORMAT
[nCount
];
192 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
193 m_formats
[n
] = formats
[n
].GetFormatId();
197 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
201 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
204 // we only return 1 element at a time - mainly because I'm too lazy to
205 // implement something which you're never asked for anyhow
209 if ( m_nCurrent
< m_nCount
) {
211 format
.cfFormat
= m_formats
[m_nCurrent
++];
213 format
.dwAspect
= DVASPECT_CONTENT
;
215 format
.tymed
= TYMED_HGLOBAL
;
226 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
228 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
231 if ( m_nCurrent
< m_nCount
)
234 // no, can't skip this many elements
240 STDMETHODIMP
wxIEnumFORMATETC::Reset()
242 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
249 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
251 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
253 // unfortunately, we can't reuse the code in ctor - types are different
254 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
255 pNew
->m_nCount
= m_nCount
;
256 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
257 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
258 pNew
->m_formats
[n
] = m_formats
[n
];
266 // ----------------------------------------------------------------------------
268 // ----------------------------------------------------------------------------
270 BEGIN_IID_TABLE(wxIDataObject
)
275 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
277 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
280 m_pDataObject
= pDataObject
;
281 m_mustDelete
= FALSE
;
284 wxIDataObject::~wxIDataObject()
288 delete m_pDataObject
;
292 // get data functions
293 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
295 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
297 // is data is in our format?
298 HRESULT hr
= QueryGetData(pformatetcIn
);
302 // for the bitmaps and metafiles we use the handles instead of global memory
304 wxDataFormat format
= (wxDataFormatId
)pformatetcIn
->cfFormat
;
309 pmedium
->tymed
= TYMED_GDI
;
312 case wxDF_ENHMETAFILE
:
313 pmedium
->tymed
= TYMED_ENHMF
;
317 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
318 sizeof(METAFILEPICT
));
319 if ( !pmedium
->hGlobal
) {
320 wxLogLastError(wxT("GlobalAlloc"));
321 return E_OUTOFMEMORY
;
323 pmedium
->tymed
= TYMED_MFPICT
;
328 size_t size
= m_pDataObject
->GetDataSize(format
);
330 // it probably means that the method is just not implemented
331 wxLogDebug(wxT("Invalid data size - can't be 0"));
333 return DV_E_FORMATETC
;
336 if ( !format
.IsStandard() ) {
337 // for custom formats, put the size with the data - alloc the
339 size
+= sizeof(size_t);
342 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
343 if ( hGlobal
== NULL
) {
344 wxLogLastError(wxT("GlobalAlloc"));
345 return E_OUTOFMEMORY
;
349 pmedium
->tymed
= TYMED_HGLOBAL
;
350 pmedium
->hGlobal
= hGlobal
;
353 pmedium
->pUnkForRelease
= NULL
;
356 hr
= GetDataHere(pformatetcIn
, pmedium
);
358 // free resources we allocated
359 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
360 GlobalFree(pmedium
->hGlobal
);
369 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
372 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
374 // put data in caller provided medium
375 switch ( pmedium
->tymed
)
378 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
383 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
384 &pmedium
->hEnhMetaFile
) )
389 // fall through - we pass METAFILEPICT through HGLOBAL
394 HGLOBAL hGlobal
= pmedium
->hGlobal
;
395 void *pBuf
= GlobalLock(hGlobal
);
396 if ( pBuf
== NULL
) {
397 wxLogLastError(wxT("GlobalLock"));
398 return E_OUTOFMEMORY
;
401 if ( !wxDataFormat(pformatetc
->cfFormat
).IsStandard() ) {
402 // for custom formats, put the size with the data
403 size_t *p
= (size_t *)pBuf
;
404 *p
++ = GlobalSize(hGlobal
);
408 wxDataFormat format
= pformatetc
->cfFormat
;
409 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
412 GlobalUnlock(hGlobal
);
423 // set data functions
424 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
428 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
430 switch ( pmedium
->tymed
)
433 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
437 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
441 // fall through - we pass METAFILEPICT through HGLOBAL
444 wxDataFormat format
= pformatetc
->cfFormat
;
446 // this is quite weird, but for file drag and drop, explorer
447 // calls our SetData() with the formats we do *not* support!
449 // as we can't fix this bug in explorer (it's a bug because it
450 // should only use formats returned by EnumFormatEtc), do the
452 if ( !m_pDataObject
->IsSupportedFormat(format
) ) {
454 return DV_E_FORMATETC
;
458 void *pBuf
= GlobalLock(pmedium
->hGlobal
);
459 if ( pBuf
== NULL
) {
460 wxLogLastError(wxT("GlobalLock"));
462 return E_OUTOFMEMORY
;
465 // we've got a problem with SetData() here because the base
466 // class version requires the size parameter which we don't
467 // have anywhere in OLE data transfer - so we need to
468 // synthetise it for known formats and we suppose that all data
469 // in custom formats starts with a DWORD containing the size
475 size
= strlen((const char *)pBuf
);
477 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
479 #if (defined(__BORLANDC__) && (__BORLANDC__ > 0x530))
480 size
= std::wcslen((const wchar_t *)pBuf
);
482 size
= ::wcslen((const wchar_t *)pBuf
);
488 // these formats don't use size at all, anyhow (but
489 // pass data by handle, which is always a single DWORD)
494 // the handler will calculate size itself (it's too
495 // complicated to do it here)
499 case CF_METAFILEPICT
:
500 size
= sizeof(METAFILEPICT
);
505 // we suppose that the size precedes the data
506 size_t *p
= (size_t *)pBuf
;
509 if (! format
.IsStandard() ) {
510 // see GetData for coresponding increment
511 size
-= sizeof(size_t);
516 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
518 GlobalUnlock(pmedium
->hGlobal
);
531 // we own the medium, so we must release it - but do *not* free any
532 // data we pass by handle because we have copied it elsewhere
533 switch ( pmedium
->tymed
)
536 pmedium
->hBitmap
= 0;
540 pmedium
->hMetaFilePict
= 0;
544 pmedium
->hEnhMetaFile
= 0;
548 ReleaseStgMedium(pmedium
);
554 // information functions
555 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
557 // do we accept data in this format?
558 if ( pformatetc
== NULL
) {
559 wxLogTrace(wxTRACE_OleCalls
,
560 wxT("wxIDataObject::QueryGetData: invalid ptr."));
565 // the only one allowed by current COM implementation
566 if ( pformatetc
->lindex
!= -1 ) {
567 wxLogTrace(wxTRACE_OleCalls
,
568 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
574 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
575 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
576 wxLogTrace(wxTRACE_OleCalls
,
577 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
578 pformatetc
->dwAspect
);
580 return DV_E_DVASPECT
;
583 // and now check the type of data requested
584 wxDataFormat format
= pformatetc
->cfFormat
;
585 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
586 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
587 wxGetFormatName(format
));
590 wxLogTrace(wxTRACE_OleCalls
,
591 wxT("wxIDataObject::QueryGetData: %s unsupported"),
592 wxGetFormatName(format
));
594 return DV_E_FORMATETC
;
597 // we only transfer data by global memory, except for some particular cases
598 DWORD tymed
= pformatetc
->tymed
;
599 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
600 !(tymed
& TYMED_HGLOBAL
) ) {
601 // it's not what we're waiting for
602 wxLogTrace(wxTRACE_OleCalls
,
603 wxT("wxIDataObject::QueryGetData: %s != %s"),
605 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
614 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
615 FORMATETC
*pFormatetcOut
)
617 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
619 // TODO we might want something better than this trivial implementation here
620 if ( pFormatetcOut
!= NULL
)
621 pFormatetcOut
->ptd
= NULL
;
623 return DATA_S_SAMEFORMATETC
;
626 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
627 IEnumFORMATETC
**ppenumFormatEtc
)
629 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
631 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
634 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
635 wxDataFormat format
, *formats
;
636 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
637 m_pDataObject
->GetAllFormats(formats
, dir
);
639 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
641 *ppenumFormatEtc
= pEnum
;
643 if ( formats
!= &format
) {
650 // ----------------------------------------------------------------------------
651 // advise sink functions (not implemented)
652 // ----------------------------------------------------------------------------
654 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
656 IAdviseSink
*pAdvSink
,
657 DWORD
*pdwConnection
)
659 return OLE_E_ADVISENOTSUPPORTED
;
662 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
664 return OLE_E_ADVISENOTSUPPORTED
;
667 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
669 return OLE_E_ADVISENOTSUPPORTED
;
672 // ----------------------------------------------------------------------------
674 // ----------------------------------------------------------------------------
676 wxDataObject::wxDataObject()
678 m_pIDataObject
= new wxIDataObject(this);
679 m_pIDataObject
->AddRef();
682 wxDataObject::~wxDataObject()
684 ReleaseInterface(m_pIDataObject
);
687 void wxDataObject::SetAutoDelete()
689 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
690 m_pIDataObject
->Release();
692 // so that the dtor doesnt' crash
693 m_pIDataObject
= NULL
;
698 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
700 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
702 #pragma warning(disable:4063)
705 static wxChar s_szBuf
[256];
707 case CF_TEXT
: return wxT("CF_TEXT");
708 case CF_BITMAP
: return wxT("CF_BITMAP");
709 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
710 case CF_SYLK
: return wxT("CF_SYLK");
711 case CF_DIF
: return wxT("CF_DIF");
712 case CF_TIFF
: return wxT("CF_TIFF");
713 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
714 case CF_DIB
: return wxT("CF_DIB");
715 case CF_PALETTE
: return wxT("CF_PALETTE");
716 case CF_PENDATA
: return wxT("CF_PENDATA");
717 case CF_RIFF
: return wxT("CF_RIFF");
718 case CF_WAVE
: return wxT("CF_WAVE");
719 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
720 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
721 case CF_HDROP
: return wxT("CF_HDROP");
722 case CF_LOCALE
: return wxT("CF_LOCALE");
725 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
727 // it must be a new predefined format we don't know the name of
728 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
735 #pragma warning(default:4063)
741 // ----------------------------------------------------------------------------
742 // wxBitmapDataObject supports CF_DIB format
743 // ----------------------------------------------------------------------------
745 size_t wxBitmapDataObject::GetDataSize() const
747 return wxConvertBitmapToDIB(NULL
, GetBitmap());
750 bool wxBitmapDataObject::GetDataHere(void *buf
) const
752 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
755 bool wxBitmapDataObject::SetData(size_t len
, const void *buf
)
757 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
759 if ( !bitmap
.Ok() ) {
760 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
770 // ----------------------------------------------------------------------------
771 // wxBitmapDataObject2 supports CF_BITMAP format
772 // ----------------------------------------------------------------------------
774 // the bitmaps aren't passed by value as other types of data (i.e. by copying
775 // the data into a global memory chunk and passing it to the clipboard or
776 // another application or whatever), but by handle, so these generic functions
777 // don't make much sense to them.
779 size_t wxBitmapDataObject2::GetDataSize() const
784 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
786 // we put a bitmap handle into pBuf
787 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
792 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
794 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
797 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
799 wxLogLastError(wxT("GetObject(HBITMAP)"));
802 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
803 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
805 if ( !bitmap
.Ok() ) {
806 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
818 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
820 if ( format
.GetFormatId() == CF_DIB
)
825 // shouldn't be selected into a DC or GetDIBits() would fail
826 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
827 wxT("can't copy bitmap selected into wxMemoryDC") );
829 // first get the info
831 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
832 NULL
, &bi
, DIB_RGB_COLORS
) )
834 wxLogLastError(wxT("GetDIBits(NULL)"));
839 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
843 // no data to copy - we don't pass HBITMAP via global memory
848 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
851 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
853 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
854 if ( format
.GetFormatId() == CF_DIB
)
859 // shouldn't be selected into a DC or GetDIBits() would fail
860 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
861 wxT("can't copy bitmap selected into wxMemoryDC") );
863 // first get the info
864 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
865 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
867 wxLogLastError(wxT("GetDIBits(NULL)"));
872 // and now copy the bits
873 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
874 pbi
, DIB_RGB_COLORS
) )
876 wxLogLastError(wxT("GetDIBits"));
883 // we put a bitmap handle into pBuf
884 *(HBITMAP
*)pBuf
= hbmp
;
890 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
891 size_t size
, const void *pBuf
)
894 if ( format
.GetFormatId() == CF_DIB
)
896 // here we get BITMAPINFO struct followed by the actual bitmap bits and
897 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
900 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
901 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
902 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
903 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
906 wxLogLastError(wxT("CreateDIBitmap"));
909 m_bitmap
.SetWidth(pbmih
->biWidth
);
910 m_bitmap
.SetHeight(pbmih
->biHeight
);
914 // it's easy with bitmaps: we pass them by handle
915 hbmp
= *(HBITMAP
*)pBuf
;
918 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
920 wxLogLastError(wxT("GetObject(HBITMAP)"));
923 m_bitmap
.SetWidth(bmp
.bmWidth
);
924 m_bitmap
.SetHeight(bmp
.bmHeight
);
925 m_bitmap
.SetDepth(bmp
.bmPlanes
);
928 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
930 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
937 // ----------------------------------------------------------------------------
939 // ----------------------------------------------------------------------------
941 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
945 // the documentation states that the first member of DROPFILES structure is
946 // a "DWORD offset of double NUL terminated file list". What they mean by
947 // this (I wonder if you see it immediately) is that the list starts at
948 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
949 // to use DragQueryFile to work with this structure, but not told where and
951 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
953 // get number of files (magic value -1)
954 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
956 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
958 // for each file get the length, allocate memory and then get the name
961 for ( n
= 0; n
< nFiles
; n
++ ) {
962 // +1 for terminating NUL
963 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
965 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
967 m_filenames
.Add(str
);
969 if ( len2
!= len
- 1 ) {
970 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
971 %d characters, %d expected."), len2
, len
- 1);
978 void wxFileDataObject::AddFile(const wxString
& file
)
980 // just add file to filenames array
981 // all useful data (such as DROPFILES struct) will be
982 // created later as necessary
983 m_filenames
.Add(file
);
986 size_t wxFileDataObject::GetDataSize() const
988 // size returned will be the size of the DROPFILES structure,
989 // plus the list of filesnames (null byte separated), plus
990 // a double null at the end
992 // if no filenames in list, size is 0
993 if ( m_filenames
.GetCount() == 0 )
996 // inital size of DROPFILES struct + null byte
997 size_t sz
= sizeof(DROPFILES
) + 1;
999 size_t count
= m_filenames
.GetCount();
1000 for ( size_t i
= 0; i
< count
; i
++ )
1002 // add filename length plus null byte
1003 sz
+= m_filenames
[i
].Len() + 1;
1009 bool wxFileDataObject::GetDataHere(void *pData
) const
1011 // pData points to an externally allocated memory block
1012 // created using the size returned by GetDataSize()
1014 // if pData is NULL, or there are no files, return
1015 if ( !pData
|| m_filenames
.GetCount() == 0 )
1018 // convert data pointer to a DROPFILES struct pointer
1019 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1021 // initialize DROPFILES struct
1022 pDrop
->pFiles
= sizeof(DROPFILES
);
1023 pDrop
->fNC
= FALSE
; // not non-client coords
1025 pDrop
->fWide
= TRUE
;
1027 pDrop
->fWide
= FALSE
;
1028 #endif // Unicode/Ansi
1030 // set start of filenames list (null separated)
1031 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1033 size_t count
= m_filenames
.GetCount();
1034 for (size_t i
= 0; i
< count
; i
++ )
1036 // copy filename to pbuf and add null terminator
1037 size_t len
= m_filenames
[i
].Len();
1038 memcpy(pbuf
, m_filenames
[i
], len
);
1040 *pbuf
++ = wxT('\0');
1043 *pbuf
= wxT('\0'); // add final null terminator
1048 // ----------------------------------------------------------------------------
1049 // private functions
1050 // ----------------------------------------------------------------------------
1052 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1054 switch ( bitsPerPixel
)
1057 // monochrome bitmap, 2 entries
1067 // may be used with 24bit bitmaps, but we don't use it here - fall
1072 // bmiColors not used at all with these bitmaps
1076 wxFAIL_MSG( wxT("unknown bitmap format") );
1081 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1083 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1085 // shouldn't be selected into a DC or GetDIBits() would fail
1086 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1087 wxT("can't copy bitmap selected into wxMemoryDC") );
1089 // prepare all the info we need
1091 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1092 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1094 wxLogLastError(wxT("GetObject(bitmap)"));
1099 // calculate the number of bits per pixel and the number of items in
1100 // bmiColors array (whose meaning depends on the bitmap format)
1101 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1102 WORD biColors
= wxGetNumOfBitmapColors(biBits
);
1106 bool wantSizeOnly
= pbi
== NULL
;
1110 // just for convenience
1111 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1113 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1114 bi
.biWidth
= bm
.bmWidth
;
1115 bi
.biHeight
= bm
.bmHeight
;
1117 bi
.biBitCount
= biBits
;
1118 bi
.biCompression
= BI_RGB
;
1120 bi
.biXPelsPerMeter
= 0;
1121 bi
.biYPelsPerMeter
= 0;
1123 bi
.biClrImportant
= 0;
1125 // memory we need for BITMAPINFO only
1126 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1128 // first get the image size
1130 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1132 wxLogLastError(wxT("GetDIBits(NULL)"));
1139 // size of the header + size of the image
1140 return dwLen
+ bi
.biSizeImage
;
1143 // and now copy the bits
1144 void *image
= (char *)pbi
+ dwLen
;
1145 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1147 wxLogLastError(wxT("GetDIBits"));
1152 return dwLen
+ bi
.biSizeImage
;
1155 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1157 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1158 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1159 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1161 // offset of image from the beginning of the header
1162 DWORD ofs
= wxGetNumOfBitmapColors(pbmih
->biBitCount
) * sizeof(RGBQUAD
);
1163 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1166 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1167 image
, pbmi
, DIB_RGB_COLORS
);
1170 wxLogLastError(wxT("CreateDIBitmap"));
1173 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1174 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1181 static const wxChar
*GetTymedName(DWORD tymed
)
1183 static wxChar s_szBuf
[128];
1185 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1186 case TYMED_FILE
: return wxT("TYMED_FILE");
1187 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1188 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1189 case TYMED_GDI
: return wxT("TYMED_GDI");
1190 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1191 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1193 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1200 #endif // not using OLE at all