]>
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 #ifdef 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("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("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("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 size
= ::wcslen((const wchar_t *)pBuf
);
484 // these formats don't use size at all, anyhow (but
485 // pass data by handle, which is always a single DWORD)
490 // the handler will calculate size itself (it's too
491 // complicated to do it here)
495 case CF_METAFILEPICT
:
496 size
= sizeof(METAFILEPICT
);
501 // we suppose that the size precedes the data
502 size_t *p
= (size_t *)pBuf
;
508 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
510 GlobalUnlock(pmedium
->hGlobal
);
523 // we own the medium, so we must release it - but do *not* free any
524 // data we pass by handle because we have copied it elsewhere
525 switch ( pmedium
->tymed
)
528 pmedium
->hBitmap
= 0;
532 pmedium
->hMetaFilePict
= 0;
536 pmedium
->hEnhMetaFile
= 0;
540 ReleaseStgMedium(pmedium
);
546 // information functions
547 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
549 // do we accept data in this format?
550 if ( pformatetc
== NULL
) {
551 wxLogTrace(wxTRACE_OleCalls
,
552 wxT("wxIDataObject::QueryGetData: invalid ptr."));
557 // the only one allowed by current COM implementation
558 if ( pformatetc
->lindex
!= -1 ) {
559 wxLogTrace(wxTRACE_OleCalls
,
560 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
566 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
567 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
568 wxLogTrace(wxTRACE_OleCalls
,
569 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
570 pformatetc
->dwAspect
);
572 return DV_E_DVASPECT
;
575 // and now check the type of data requested
576 wxDataFormat format
= pformatetc
->cfFormat
;
577 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
578 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
579 wxGetFormatName(format
));
582 wxLogTrace(wxTRACE_OleCalls
,
583 wxT("wxIDataObject::QueryGetData: %s unsupported"),
584 wxGetFormatName(format
));
586 return DV_E_FORMATETC
;
589 // we only transfer data by global memory, except for some particular cases
590 DWORD tymed
= pformatetc
->tymed
;
591 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
592 !(tymed
& TYMED_HGLOBAL
) ) {
593 // it's not what we're waiting for
594 wxLogTrace(wxTRACE_OleCalls
,
595 wxT("wxIDataObject::QueryGetData: %s != %s"),
597 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
606 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
607 FORMATETC
*pFormatetcOut
)
609 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
611 // TODO we might want something better than this trivial implementation here
612 if ( pFormatetcOut
!= NULL
)
613 pFormatetcOut
->ptd
= NULL
;
615 return DATA_S_SAMEFORMATETC
;
618 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
619 IEnumFORMATETC
**ppenumFormatEtc
)
621 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
623 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
626 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
627 wxDataFormat format
, *formats
;
628 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
629 m_pDataObject
->GetAllFormats(formats
, dir
);
631 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
633 *ppenumFormatEtc
= pEnum
;
635 if ( formats
!= &format
) {
642 // ----------------------------------------------------------------------------
643 // advise sink functions (not implemented)
644 // ----------------------------------------------------------------------------
646 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
648 IAdviseSink
*pAdvSink
,
649 DWORD
*pdwConnection
)
651 return OLE_E_ADVISENOTSUPPORTED
;
654 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
656 return OLE_E_ADVISENOTSUPPORTED
;
659 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
661 return OLE_E_ADVISENOTSUPPORTED
;
664 // ----------------------------------------------------------------------------
666 // ----------------------------------------------------------------------------
668 wxDataObject::wxDataObject()
670 m_pIDataObject
= new wxIDataObject(this);
671 m_pIDataObject
->AddRef();
674 wxDataObject::~wxDataObject()
676 ReleaseInterface(m_pIDataObject
);
679 void wxDataObject::SetAutoDelete()
681 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
682 m_pIDataObject
->Release();
684 // so that the dtor doesnt' crash
685 m_pIDataObject
= NULL
;
690 const char *wxDataObject::GetFormatName(wxDataFormat format
)
692 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
694 #pragma warning(disable:4063)
697 static char s_szBuf
[256];
699 case CF_TEXT
: return "CF_TEXT";
700 case CF_BITMAP
: return "CF_BITMAP";
701 case CF_METAFILEPICT
: return "CF_METAFILEPICT";
702 case CF_SYLK
: return "CF_SYLK";
703 case CF_DIF
: return "CF_DIF";
704 case CF_TIFF
: return "CF_TIFF";
705 case CF_OEMTEXT
: return "CF_OEMTEXT";
706 case CF_DIB
: return "CF_DIB";
707 case CF_PALETTE
: return "CF_PALETTE";
708 case CF_PENDATA
: return "CF_PENDATA";
709 case CF_RIFF
: return "CF_RIFF";
710 case CF_WAVE
: return "CF_WAVE";
711 case CF_UNICODETEXT
: return "CF_UNICODETEXT";
712 case CF_ENHMETAFILE
: return "CF_ENHMETAFILE";
713 case CF_HDROP
: return "CF_HDROP";
714 case CF_LOCALE
: return "CF_LOCALE";
717 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
719 // it must be a new predefined format we don't know the name of
720 sprintf(s_szBuf
, "unknown CF (0x%04x)", format
.GetFormatId());
727 #pragma warning(default:4063)
733 // ----------------------------------------------------------------------------
734 // wxBitmapDataObject supports CF_DIB format
735 // ----------------------------------------------------------------------------
737 size_t wxBitmapDataObject::GetDataSize() const
739 return wxConvertBitmapToDIB(NULL
, GetBitmap());
742 bool wxBitmapDataObject::GetDataHere(void *buf
) const
744 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
747 bool wxBitmapDataObject::SetData(size_t len
, const void *buf
)
749 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
751 if ( !bitmap
.Ok() ) {
752 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
762 // ----------------------------------------------------------------------------
763 // wxBitmapDataObject2 supports CF_BITMAP format
764 // ----------------------------------------------------------------------------
766 // the bitmaps aren't passed by value as other types of data (i.e. by copying
767 // the data into a global memory chunk and passing it to the clipboard or
768 // another application or whatever), but by handle, so these generic functions
769 // don't make much sense to them.
771 size_t wxBitmapDataObject2::GetDataSize() const
776 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
778 // we put a bitmap handle into pBuf
779 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
784 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
786 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
789 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
791 wxLogLastError("GetObject(HBITMAP)");
794 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
795 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
797 if ( !bitmap
.Ok() ) {
798 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
810 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
812 if ( format
.GetFormatId() == CF_DIB
)
817 // shouldn't be selected into a DC or GetDIBits() would fail
818 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
819 wxT("can't copy bitmap selected into wxMemoryDC") );
821 // first get the info
823 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
824 NULL
, &bi
, DIB_RGB_COLORS
) )
826 wxLogLastError("GetDIBits(NULL)");
831 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
835 // no data to copy - we don't pass HBITMAP via global memory
840 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
843 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
845 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
846 if ( format
.GetFormatId() == CF_DIB
)
851 // shouldn't be selected into a DC or GetDIBits() would fail
852 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
853 wxT("can't copy bitmap selected into wxMemoryDC") );
855 // first get the info
856 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
857 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
859 wxLogLastError("GetDIBits(NULL)");
864 // and now copy the bits
865 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
866 pbi
, DIB_RGB_COLORS
) )
868 wxLogLastError("GetDIBits");
875 // we put a bitmap handle into pBuf
876 *(HBITMAP
*)pBuf
= hbmp
;
882 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
883 size_t size
, const void *pBuf
)
886 if ( format
.GetFormatId() == CF_DIB
)
888 // here we get BITMAPINFO struct followed by the actual bitmap bits and
889 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
892 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
893 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
894 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
895 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
898 wxLogLastError("CreateDIBitmap");
901 m_bitmap
.SetWidth(pbmih
->biWidth
);
902 m_bitmap
.SetHeight(pbmih
->biHeight
);
906 // it's easy with bitmaps: we pass them by handle
907 hbmp
= *(HBITMAP
*)pBuf
;
910 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
912 wxLogLastError("GetObject(HBITMAP)");
915 m_bitmap
.SetWidth(bmp
.bmWidth
);
916 m_bitmap
.SetHeight(bmp
.bmHeight
);
917 m_bitmap
.SetDepth(bmp
.bmPlanes
);
920 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
922 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
929 // ----------------------------------------------------------------------------
931 // ----------------------------------------------------------------------------
933 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
937 // the documentation states that the first member of DROPFILES structure is
938 // a "DWORD offset of double NUL terminated file list". What they mean by
939 // this (I wonder if you see it immediately) is that the list starts at
940 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
941 // to use DragQueryFile to work with this structure, but not told where and
943 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
945 // get number of files (magic value -1)
946 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
948 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
950 // for each file get the length, allocate memory and then get the name
953 for ( n
= 0; n
< nFiles
; n
++ ) {
954 // +1 for terminating NUL
955 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
957 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
959 m_filenames
.Add(str
);
961 if ( len2
!= len
- 1 ) {
962 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned"
963 " %d characters, %d expected."), len2
, len
- 1);
970 void wxFileDataObject::AddFile(const wxString
& file
)
972 // just add file to filenames array
973 // all useful data (such as DROPFILES struct) will be
974 // created later as necessary
975 m_filenames
.Add(file
);
978 size_t wxFileDataObject::GetDataSize() const
980 // size returned will be the size of the DROPFILES structure,
981 // plus the list of filesnames (null byte separated), plus
982 // a double null at the end
984 // if no filenames in list, size is 0
985 if ( m_filenames
.GetCount() == 0 )
988 // inital size of DROPFILES struct + null byte
989 size_t sz
= sizeof(DROPFILES
) + 1;
991 size_t count
= m_filenames
.GetCount();
992 for ( size_t i
= 0; i
< count
; i
++ )
994 // add filename length plus null byte
995 sz
+= m_filenames
[i
].Len() + 1;
1001 bool wxFileDataObject::GetDataHere(void *pData
) const
1003 // pData points to an externally allocated memory block
1004 // created using the size returned by GetDataSize()
1006 // if pData is NULL, or there are no files, return
1007 if ( !pData
|| m_filenames
.GetCount() == 0 )
1010 // convert data pointer to a DROPFILES struct pointer
1011 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1013 // initialize DROPFILES struct
1014 pDrop
->pFiles
= sizeof(DROPFILES
);
1015 pDrop
->fNC
= FALSE
; // not non-client coords
1017 pDrop
->fWide
= TRUE
;
1019 pDrop
->fWide
= FALSE
;
1020 #endif // Unicode/Ansi
1022 // set start of filenames list (null separated)
1023 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1025 size_t count
= m_filenames
.GetCount();
1026 for (size_t i
= 0; i
< count
; i
++ )
1028 // copy filename to pbuf and add null terminator
1029 size_t len
= m_filenames
[i
].Len();
1030 memcpy(pbuf
, m_filenames
[i
], len
);
1032 *pbuf
++ = wxT('\0');
1035 *pbuf
= wxT('\0'); // add final null terminator
1040 // ----------------------------------------------------------------------------
1041 // private functions
1042 // ----------------------------------------------------------------------------
1044 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1046 switch ( bitsPerPixel
)
1049 // monochrome bitmap, 2 entries
1059 // may be used with 24bit bitmaps, but we don't use it here - fall
1064 // bmiColors not used at all with these bitmaps
1068 wxFAIL_MSG( wxT("unknown bitmap format") );
1073 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1075 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1077 // shouldn't be selected into a DC or GetDIBits() would fail
1078 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1079 wxT("can't copy bitmap selected into wxMemoryDC") );
1081 // prepare all the info we need
1083 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1084 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1086 wxLogLastError("GetObject(bitmap)");
1091 // calculate the number of bits per pixel and the number of items in
1092 // bmiColors array (whose meaning depends on the bitmap format)
1093 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1094 WORD biColors
= wxGetNumOfBitmapColors(biBits
);
1098 bool wantSizeOnly
= pbi
== NULL
;
1102 // just for convenience
1103 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1105 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1106 bi
.biWidth
= bm
.bmWidth
;
1107 bi
.biHeight
= bm
.bmHeight
;
1109 bi
.biBitCount
= biBits
;
1110 bi
.biCompression
= BI_RGB
;
1112 bi
.biXPelsPerMeter
= 0;
1113 bi
.biYPelsPerMeter
= 0;
1115 bi
.biClrImportant
= 0;
1117 // memory we need for BITMAPINFO only
1118 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1120 // first get the image size
1122 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1124 wxLogLastError("GetDIBits(NULL)");
1131 // size of the header + size of the image
1132 return dwLen
+ bi
.biSizeImage
;
1135 // and now copy the bits
1136 void *image
= (char *)pbi
+ dwLen
;
1137 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1139 wxLogLastError("GetDIBits");
1144 return dwLen
+ bi
.biSizeImage
;
1147 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1149 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1150 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1151 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1153 // offset of image from the beginning of the header
1154 DWORD ofs
= wxGetNumOfBitmapColors(pbmih
->biBitCount
) * sizeof(RGBQUAD
);
1155 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1158 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1159 image
, pbmi
, DIB_RGB_COLORS
);
1162 wxLogLastError("CreateDIBitmap");
1165 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1166 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1173 static const wxChar
*GetTymedName(DWORD tymed
)
1175 static wxChar s_szBuf
[128];
1177 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1178 case TYMED_FILE
: return wxT("TYMED_FILE");
1179 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1180 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1181 case TYMED_GDI
: return wxT("TYMED_GDI");
1182 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1183 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1185 wxSprintf(s_szBuf
, wxT("type of media format %d (unknown)"), tymed
);
1192 #endif // not using OLE at all