]>
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 #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
;
512 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
514 GlobalUnlock(pmedium
->hGlobal
);
527 // we own the medium, so we must release it - but do *not* free any
528 // data we pass by handle because we have copied it elsewhere
529 switch ( pmedium
->tymed
)
532 pmedium
->hBitmap
= 0;
536 pmedium
->hMetaFilePict
= 0;
540 pmedium
->hEnhMetaFile
= 0;
544 ReleaseStgMedium(pmedium
);
550 // information functions
551 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
553 // do we accept data in this format?
554 if ( pformatetc
== NULL
) {
555 wxLogTrace(wxTRACE_OleCalls
,
556 wxT("wxIDataObject::QueryGetData: invalid ptr."));
561 // the only one allowed by current COM implementation
562 if ( pformatetc
->lindex
!= -1 ) {
563 wxLogTrace(wxTRACE_OleCalls
,
564 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
570 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
571 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
572 wxLogTrace(wxTRACE_OleCalls
,
573 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
574 pformatetc
->dwAspect
);
576 return DV_E_DVASPECT
;
579 // and now check the type of data requested
580 wxDataFormat format
= pformatetc
->cfFormat
;
581 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
582 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
583 wxGetFormatName(format
));
586 wxLogTrace(wxTRACE_OleCalls
,
587 wxT("wxIDataObject::QueryGetData: %s unsupported"),
588 wxGetFormatName(format
));
590 return DV_E_FORMATETC
;
593 // we only transfer data by global memory, except for some particular cases
594 DWORD tymed
= pformatetc
->tymed
;
595 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
596 !(tymed
& TYMED_HGLOBAL
) ) {
597 // it's not what we're waiting for
598 wxLogTrace(wxTRACE_OleCalls
,
599 wxT("wxIDataObject::QueryGetData: %s != %s"),
601 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
610 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
611 FORMATETC
*pFormatetcOut
)
613 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
615 // TODO we might want something better than this trivial implementation here
616 if ( pFormatetcOut
!= NULL
)
617 pFormatetcOut
->ptd
= NULL
;
619 return DATA_S_SAMEFORMATETC
;
622 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
623 IEnumFORMATETC
**ppenumFormatEtc
)
625 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
627 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
630 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
631 wxDataFormat format
, *formats
;
632 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
633 m_pDataObject
->GetAllFormats(formats
, dir
);
635 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
637 *ppenumFormatEtc
= pEnum
;
639 if ( formats
!= &format
) {
646 // ----------------------------------------------------------------------------
647 // advise sink functions (not implemented)
648 // ----------------------------------------------------------------------------
650 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
652 IAdviseSink
*pAdvSink
,
653 DWORD
*pdwConnection
)
655 return OLE_E_ADVISENOTSUPPORTED
;
658 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
660 return OLE_E_ADVISENOTSUPPORTED
;
663 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
665 return OLE_E_ADVISENOTSUPPORTED
;
668 // ----------------------------------------------------------------------------
670 // ----------------------------------------------------------------------------
672 wxDataObject::wxDataObject()
674 m_pIDataObject
= new wxIDataObject(this);
675 m_pIDataObject
->AddRef();
678 wxDataObject::~wxDataObject()
680 ReleaseInterface(m_pIDataObject
);
683 void wxDataObject::SetAutoDelete()
685 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
686 m_pIDataObject
->Release();
688 // so that the dtor doesnt' crash
689 m_pIDataObject
= NULL
;
694 const char *wxDataObject::GetFormatName(wxDataFormat format
)
696 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
698 #pragma warning(disable:4063)
701 static char s_szBuf
[256];
703 case CF_TEXT
: return "CF_TEXT";
704 case CF_BITMAP
: return "CF_BITMAP";
705 case CF_METAFILEPICT
: return "CF_METAFILEPICT";
706 case CF_SYLK
: return "CF_SYLK";
707 case CF_DIF
: return "CF_DIF";
708 case CF_TIFF
: return "CF_TIFF";
709 case CF_OEMTEXT
: return "CF_OEMTEXT";
710 case CF_DIB
: return "CF_DIB";
711 case CF_PALETTE
: return "CF_PALETTE";
712 case CF_PENDATA
: return "CF_PENDATA";
713 case CF_RIFF
: return "CF_RIFF";
714 case CF_WAVE
: return "CF_WAVE";
715 case CF_UNICODETEXT
: return "CF_UNICODETEXT";
716 case CF_ENHMETAFILE
: return "CF_ENHMETAFILE";
717 case CF_HDROP
: return "CF_HDROP";
718 case CF_LOCALE
: return "CF_LOCALE";
721 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
723 // it must be a new predefined format we don't know the name of
724 sprintf(s_szBuf
, "unknown CF (0x%04x)", format
.GetFormatId());
731 #pragma warning(default:4063)
737 // ----------------------------------------------------------------------------
738 // wxBitmapDataObject supports CF_DIB format
739 // ----------------------------------------------------------------------------
741 size_t wxBitmapDataObject::GetDataSize() const
743 return wxConvertBitmapToDIB(NULL
, GetBitmap());
746 bool wxBitmapDataObject::GetDataHere(void *buf
) const
748 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
751 bool wxBitmapDataObject::SetData(size_t len
, const void *buf
)
753 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
755 if ( !bitmap
.Ok() ) {
756 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
766 // ----------------------------------------------------------------------------
767 // wxBitmapDataObject2 supports CF_BITMAP format
768 // ----------------------------------------------------------------------------
770 // the bitmaps aren't passed by value as other types of data (i.e. by copying
771 // the data into a global memory chunk and passing it to the clipboard or
772 // another application or whatever), but by handle, so these generic functions
773 // don't make much sense to them.
775 size_t wxBitmapDataObject2::GetDataSize() const
780 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
782 // we put a bitmap handle into pBuf
783 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
788 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
790 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
793 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
795 wxLogLastError("GetObject(HBITMAP)");
798 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
799 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
801 if ( !bitmap
.Ok() ) {
802 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
814 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
816 if ( format
.GetFormatId() == CF_DIB
)
821 // shouldn't be selected into a DC or GetDIBits() would fail
822 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
823 wxT("can't copy bitmap selected into wxMemoryDC") );
825 // first get the info
827 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
828 NULL
, &bi
, DIB_RGB_COLORS
) )
830 wxLogLastError("GetDIBits(NULL)");
835 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
839 // no data to copy - we don't pass HBITMAP via global memory
844 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
847 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
849 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
850 if ( format
.GetFormatId() == CF_DIB
)
855 // shouldn't be selected into a DC or GetDIBits() would fail
856 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
857 wxT("can't copy bitmap selected into wxMemoryDC") );
859 // first get the info
860 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
861 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
863 wxLogLastError("GetDIBits(NULL)");
868 // and now copy the bits
869 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
870 pbi
, DIB_RGB_COLORS
) )
872 wxLogLastError("GetDIBits");
879 // we put a bitmap handle into pBuf
880 *(HBITMAP
*)pBuf
= hbmp
;
886 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
887 size_t size
, const void *pBuf
)
890 if ( format
.GetFormatId() == CF_DIB
)
892 // here we get BITMAPINFO struct followed by the actual bitmap bits and
893 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
896 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
897 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
898 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
899 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
902 wxLogLastError("CreateDIBitmap");
905 m_bitmap
.SetWidth(pbmih
->biWidth
);
906 m_bitmap
.SetHeight(pbmih
->biHeight
);
910 // it's easy with bitmaps: we pass them by handle
911 hbmp
= *(HBITMAP
*)pBuf
;
914 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
916 wxLogLastError("GetObject(HBITMAP)");
919 m_bitmap
.SetWidth(bmp
.bmWidth
);
920 m_bitmap
.SetHeight(bmp
.bmHeight
);
921 m_bitmap
.SetDepth(bmp
.bmPlanes
);
924 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
926 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
933 // ----------------------------------------------------------------------------
935 // ----------------------------------------------------------------------------
937 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
941 // the documentation states that the first member of DROPFILES structure is
942 // a "DWORD offset of double NUL terminated file list". What they mean by
943 // this (I wonder if you see it immediately) is that the list starts at
944 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
945 // to use DragQueryFile to work with this structure, but not told where and
947 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
949 // get number of files (magic value -1)
950 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
952 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
954 // for each file get the length, allocate memory and then get the name
957 for ( n
= 0; n
< nFiles
; n
++ ) {
958 // +1 for terminating NUL
959 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
961 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
963 m_filenames
.Add(str
);
965 if ( len2
!= len
- 1 ) {
966 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned"
967 " %d characters, %d expected."), len2
, len
- 1);
974 void wxFileDataObject::AddFile(const wxString
& file
)
976 // just add file to filenames array
977 // all useful data (such as DROPFILES struct) will be
978 // created later as necessary
979 m_filenames
.Add(file
);
982 size_t wxFileDataObject::GetDataSize() const
984 // size returned will be the size of the DROPFILES structure,
985 // plus the list of filesnames (null byte separated), plus
986 // a double null at the end
988 // if no filenames in list, size is 0
989 if ( m_filenames
.GetCount() == 0 )
992 // inital size of DROPFILES struct + null byte
993 size_t sz
= sizeof(DROPFILES
) + 1;
995 size_t count
= m_filenames
.GetCount();
996 for ( size_t i
= 0; i
< count
; i
++ )
998 // add filename length plus null byte
999 sz
+= m_filenames
[i
].Len() + 1;
1005 bool wxFileDataObject::GetDataHere(void *pData
) const
1007 // pData points to an externally allocated memory block
1008 // created using the size returned by GetDataSize()
1010 // if pData is NULL, or there are no files, return
1011 if ( !pData
|| m_filenames
.GetCount() == 0 )
1014 // convert data pointer to a DROPFILES struct pointer
1015 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1017 // initialize DROPFILES struct
1018 pDrop
->pFiles
= sizeof(DROPFILES
);
1019 pDrop
->fNC
= FALSE
; // not non-client coords
1021 pDrop
->fWide
= TRUE
;
1023 pDrop
->fWide
= FALSE
;
1024 #endif // Unicode/Ansi
1026 // set start of filenames list (null separated)
1027 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1029 size_t count
= m_filenames
.GetCount();
1030 for (size_t i
= 0; i
< count
; i
++ )
1032 // copy filename to pbuf and add null terminator
1033 size_t len
= m_filenames
[i
].Len();
1034 memcpy(pbuf
, m_filenames
[i
], len
);
1036 *pbuf
++ = wxT('\0');
1039 *pbuf
= wxT('\0'); // add final null terminator
1044 // ----------------------------------------------------------------------------
1045 // private functions
1046 // ----------------------------------------------------------------------------
1048 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1050 switch ( bitsPerPixel
)
1053 // monochrome bitmap, 2 entries
1063 // may be used with 24bit bitmaps, but we don't use it here - fall
1068 // bmiColors not used at all with these bitmaps
1072 wxFAIL_MSG( wxT("unknown bitmap format") );
1077 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1079 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1081 // shouldn't be selected into a DC or GetDIBits() would fail
1082 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1083 wxT("can't copy bitmap selected into wxMemoryDC") );
1085 // prepare all the info we need
1087 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1088 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1090 wxLogLastError("GetObject(bitmap)");
1095 // calculate the number of bits per pixel and the number of items in
1096 // bmiColors array (whose meaning depends on the bitmap format)
1097 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1098 WORD biColors
= wxGetNumOfBitmapColors(biBits
);
1102 bool wantSizeOnly
= pbi
== NULL
;
1106 // just for convenience
1107 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1109 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1110 bi
.biWidth
= bm
.bmWidth
;
1111 bi
.biHeight
= bm
.bmHeight
;
1113 bi
.biBitCount
= biBits
;
1114 bi
.biCompression
= BI_RGB
;
1116 bi
.biXPelsPerMeter
= 0;
1117 bi
.biYPelsPerMeter
= 0;
1119 bi
.biClrImportant
= 0;
1121 // memory we need for BITMAPINFO only
1122 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1124 // first get the image size
1126 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1128 wxLogLastError("GetDIBits(NULL)");
1135 // size of the header + size of the image
1136 return dwLen
+ bi
.biSizeImage
;
1139 // and now copy the bits
1140 void *image
= (char *)pbi
+ dwLen
;
1141 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1143 wxLogLastError("GetDIBits");
1148 return dwLen
+ bi
.biSizeImage
;
1151 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1153 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1154 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1155 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1157 // offset of image from the beginning of the header
1158 DWORD ofs
= wxGetNumOfBitmapColors(pbmih
->biBitCount
) * sizeof(RGBQUAD
);
1159 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1162 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1163 image
, pbmi
, DIB_RGB_COLORS
);
1166 wxLogLastError("CreateDIBitmap");
1169 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1170 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1177 static const wxChar
*GetTymedName(DWORD tymed
)
1179 static wxChar s_szBuf
[128];
1181 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1182 case TYMED_FILE
: return wxT("TYMED_FILE");
1183 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1184 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1185 case TYMED_GDI
: return wxT("TYMED_GDI");
1186 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1187 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1189 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1196 #endif // not using OLE at all