]>
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
);
76 ~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
);
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
= ::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
,
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("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("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("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
);
467 size
= wcslen((const wchar_t *)pBuf
);
472 // these formats don't use size at all, anyhow (but
473 // pass data by handle, which is always a single DWORD)
478 // the handler will calculate size itself (it's too
479 // complicated to do it here)
483 case CF_METAFILEPICT
:
484 size
= sizeof(METAFILEPICT
);
489 // we suppose that the size precedes the data
490 size_t *p
= (size_t *)pBuf
;
496 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
498 GlobalUnlock(pmedium
->hGlobal
);
511 // we own the medium, so we must release it - but do *not* free any
512 // data we pass by handle because we have copied it elsewhere
513 switch ( pmedium
->tymed
)
516 pmedium
->hBitmap
= 0;
520 pmedium
->hMetaFilePict
= 0;
524 pmedium
->hEnhMetaFile
= 0;
528 ReleaseStgMedium(pmedium
);
534 // information functions
535 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
537 // do we accept data in this format?
538 if ( pformatetc
== NULL
) {
539 wxLogTrace(wxTRACE_OleCalls
,
540 wxT("wxIDataObject::QueryGetData: invalid ptr."));
545 // the only one allowed by current COM implementation
546 if ( pformatetc
->lindex
!= -1 ) {
547 wxLogTrace(wxTRACE_OleCalls
,
548 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
554 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
555 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
556 wxLogTrace(wxTRACE_OleCalls
,
557 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
558 pformatetc
->dwAspect
);
560 return DV_E_DVASPECT
;
563 // and now check the type of data requested
564 wxDataFormat format
= pformatetc
->cfFormat
;
565 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
566 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
567 wxGetFormatName(format
));
570 wxLogTrace(wxTRACE_OleCalls
,
571 wxT("wxIDataObject::QueryGetData: %s unsupported"),
572 wxGetFormatName(format
));
574 return DV_E_FORMATETC
;
577 // we only transfer data by global memory, except for some particular cases
578 DWORD tymed
= pformatetc
->tymed
;
579 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
580 !(tymed
& TYMED_HGLOBAL
) ) {
581 // it's not what we're waiting for
582 wxLogTrace(wxTRACE_OleCalls
,
583 wxT("wxIDataObject::QueryGetData: %s != %s"),
585 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
594 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
595 FORMATETC
*pFormatetcOut
)
597 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
599 // TODO we might want something better than this trivial implementation here
600 if ( pFormatetcOut
!= NULL
)
601 pFormatetcOut
->ptd
= NULL
;
603 return DATA_S_SAMEFORMATETC
;
606 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
607 IEnumFORMATETC
**ppenumFormatEtc
)
609 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
611 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
614 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
615 wxDataFormat format
, *formats
;
616 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
617 m_pDataObject
->GetAllFormats(formats
, dir
);
619 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
621 *ppenumFormatEtc
= pEnum
;
623 if ( formats
!= &format
) {
630 // ----------------------------------------------------------------------------
631 // advise sink functions (not implemented)
632 // ----------------------------------------------------------------------------
634 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
636 IAdviseSink
*pAdvSink
,
637 DWORD
*pdwConnection
)
639 return OLE_E_ADVISENOTSUPPORTED
;
642 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
644 return OLE_E_ADVISENOTSUPPORTED
;
647 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
649 return OLE_E_ADVISENOTSUPPORTED
;
652 // ----------------------------------------------------------------------------
654 // ----------------------------------------------------------------------------
656 wxDataObject::wxDataObject()
658 m_pIDataObject
= new wxIDataObject(this);
659 m_pIDataObject
->AddRef();
662 wxDataObject::~wxDataObject()
664 ReleaseInterface(m_pIDataObject
);
667 void wxDataObject::SetAutoDelete()
669 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
670 m_pIDataObject
->Release();
672 // so that the dtor doesnt' crash
673 m_pIDataObject
= NULL
;
678 const char *wxDataObject::GetFormatName(wxDataFormat format
)
680 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
682 #pragma warning(disable:4063)
685 static char s_szBuf
[256];
687 case CF_TEXT
: return "CF_TEXT";
688 case CF_BITMAP
: return "CF_BITMAP";
689 case CF_METAFILEPICT
: return "CF_METAFILEPICT";
690 case CF_SYLK
: return "CF_SYLK";
691 case CF_DIF
: return "CF_DIF";
692 case CF_TIFF
: return "CF_TIFF";
693 case CF_OEMTEXT
: return "CF_OEMTEXT";
694 case CF_DIB
: return "CF_DIB";
695 case CF_PALETTE
: return "CF_PALETTE";
696 case CF_PENDATA
: return "CF_PENDATA";
697 case CF_RIFF
: return "CF_RIFF";
698 case CF_WAVE
: return "CF_WAVE";
699 case CF_UNICODETEXT
: return "CF_UNICODETEXT";
700 case CF_ENHMETAFILE
: return "CF_ENHMETAFILE";
701 case CF_HDROP
: return "CF_HDROP";
702 case CF_LOCALE
: return "CF_LOCALE";
705 if ( !GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
707 // it must be a new predefined format we don't know the name of
708 sprintf(s_szBuf
, "unknown CF (0x%04x)", format
);
715 #pragma warning(default:4063)
721 // ----------------------------------------------------------------------------
722 // wxBitmapDataObject supports CF_DIB format
723 // ----------------------------------------------------------------------------
725 size_t wxBitmapDataObject::GetDataSize() const
727 return wxConvertBitmapToDIB(NULL
, GetBitmap());
730 bool wxBitmapDataObject::GetDataHere(void *buf
) const
732 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
735 bool wxBitmapDataObject::SetData(size_t len
, const void *buf
)
737 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
739 if ( !bitmap
.Ok() ) {
740 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
750 // ----------------------------------------------------------------------------
751 // wxBitmapDataObject2 supports CF_BITMAP format
752 // ----------------------------------------------------------------------------
754 // the bitmaps aren't passed by value as other types of data (i.e. by copying
755 // the data into a global memory chunk and passing it to the clipboard or
756 // another application or whatever), but by handle, so these generic functions
757 // don't make much sense to them.
759 size_t wxBitmapDataObject2::GetDataSize() const
764 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
766 // we put a bitmap handle into pBuf
767 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
772 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
774 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
777 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
779 wxLogLastError("GetObject(HBITMAP)");
782 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
783 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
785 if ( !bitmap
.Ok() ) {
786 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
798 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
800 if ( format
.GetFormatId() == CF_DIB
)
805 // shouldn't be selected into a DC or GetDIBits() would fail
806 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
807 wxT("can't copy bitmap selected into wxMemoryDC") );
809 // first get the info
811 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
812 NULL
, &bi
, DIB_RGB_COLORS
) )
814 wxLogLastError("GetDIBits(NULL)");
819 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
823 // no data to copy - we don't pass HBITMAP via global memory
828 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
831 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
833 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
834 if ( format
.GetFormatId() == CF_DIB
)
839 // shouldn't be selected into a DC or GetDIBits() would fail
840 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
841 wxT("can't copy bitmap selected into wxMemoryDC") );
843 // first get the info
844 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
845 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
847 wxLogLastError("GetDIBits(NULL)");
852 // and now copy the bits
853 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
854 pbi
, DIB_RGB_COLORS
) )
856 wxLogLastError("GetDIBits");
863 // we put a bitmap handle into pBuf
864 *(HBITMAP
*)pBuf
= hbmp
;
870 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
871 size_t size
, const void *pBuf
)
874 if ( format
.GetFormatId() == CF_DIB
)
876 // here we get BITMAPINFO struct followed by the actual bitmap bits and
877 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
880 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
881 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
882 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
883 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
886 wxLogLastError("CreateDIBitmap");
889 m_bitmap
.SetWidth(pbmih
->biWidth
);
890 m_bitmap
.SetHeight(pbmih
->biHeight
);
894 // it's easy with bitmaps: we pass them by handle
895 hbmp
= *(HBITMAP
*)pBuf
;
898 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
900 wxLogLastError("GetObject(HBITMAP)");
903 m_bitmap
.SetWidth(bmp
.bmWidth
);
904 m_bitmap
.SetHeight(bmp
.bmHeight
);
905 m_bitmap
.SetDepth(bmp
.bmPlanes
);
908 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
910 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
917 // ----------------------------------------------------------------------------
919 // ----------------------------------------------------------------------------
921 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
925 // the documentation states that the first member of DROPFILES structure is
926 // a "DWORD offset of double NUL terminated file list". What they mean by
927 // this (I wonder if you see it immediately) is that the list starts at
928 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
929 // to use DragQueryFile to work with this structure, but not told where and
931 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
933 // get number of files (magic value -1)
934 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
936 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
938 // for each file get the length, allocate memory and then get the name
941 for ( n
= 0; n
< nFiles
; n
++ ) {
942 // +1 for terminating NUL
943 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
945 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
947 m_filenames
.Add(str
);
949 if ( len2
!= len
- 1 ) {
950 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned"
951 " %d characters, %d expected."), len2
, len
- 1);
958 void wxFileDataObject::AddFile(const wxString
& file
)
960 // just add file to filenames array
961 // all useful data (such as DROPFILES struct) will be
962 // created later as necessary
963 m_filenames
.Add(file
);
966 size_t wxFileDataObject::GetDataSize() const
968 // size returned will be the size of the DROPFILES structure,
969 // plus the list of filesnames (null byte separated), plus
970 // a double null at the end
972 // if no filenames in list, size is 0
973 if ( m_filenames
.GetCount() == 0 )
976 // inital size of DROPFILES struct + null byte
977 size_t sz
= sizeof(DROPFILES
) + 1;
979 size_t count
= m_filenames
.GetCount();
980 for ( size_t i
= 0; i
< count
; i
++ )
982 // add filename length plus null byte
983 sz
+= m_filenames
[i
].Len() + 1;
989 bool wxFileDataObject::GetDataHere(void *pData
) const
991 // pData points to an externally allocated memory block
992 // created using the size returned by GetDataSize()
994 // if pData is NULL, or there are no files, return
995 if ( !pData
|| m_filenames
.GetCount() == 0 )
998 // convert data pointer to a DROPFILES struct pointer
999 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1001 // initialize DROPFILES struct
1002 pDrop
->pFiles
= sizeof(DROPFILES
);
1003 pDrop
->fNC
= FALSE
; // not non-client coords
1005 pDrop
->fWide
= TRUE
;
1007 pDrop
->fWide
= FALSE
;
1008 #endif // Unicode/Ansi
1010 // set start of filenames list (null separated)
1011 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1013 size_t count
= m_filenames
.GetCount();
1014 for (size_t i
= 0; i
< count
; i
++ )
1016 // copy filename to pbuf and add null terminator
1017 size_t len
= m_filenames
[i
].Len();
1018 memcpy(pbuf
, m_filenames
[i
], len
);
1020 *pbuf
++ = wxT('\0');
1023 *pbuf
= wxT('\0'); // add final null terminator
1028 // ----------------------------------------------------------------------------
1029 // private functions
1030 // ----------------------------------------------------------------------------
1032 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1034 switch ( bitsPerPixel
)
1037 // monochrome bitmap, 2 entries
1047 // may be used with 24bit bitmaps, but we don't use it here - fall
1052 // bmiColors not used at all with these bitmaps
1056 wxFAIL_MSG( wxT("unknown bitmap format") );
1061 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1063 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1065 // shouldn't be selected into a DC or GetDIBits() would fail
1066 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1067 wxT("can't copy bitmap selected into wxMemoryDC") );
1069 // prepare all the info we need
1071 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1072 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1074 wxLogLastError("GetObject(bitmap)");
1079 // calculate the number of bits per pixel and the number of items in
1080 // bmiColors array (whose meaning depends on the bitmap format)
1081 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1082 WORD biColors
= wxGetNumOfBitmapColors(biBits
);
1086 bool wantSizeOnly
= pbi
== NULL
;
1090 // just for convenience
1091 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1093 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1094 bi
.biWidth
= bm
.bmWidth
;
1095 bi
.biHeight
= bm
.bmHeight
;
1097 bi
.biBitCount
= biBits
;
1098 bi
.biCompression
= BI_RGB
;
1100 bi
.biXPelsPerMeter
= 0;
1101 bi
.biYPelsPerMeter
= 0;
1103 bi
.biClrImportant
= 0;
1105 // memory we need for BITMAPINFO only
1106 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1108 // first get the image size
1110 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1112 wxLogLastError("GetDIBits(NULL)");
1119 // size of the header + size of the image
1120 return dwLen
+ bi
.biSizeImage
;
1123 // and now copy the bits
1124 void *image
= (char *)pbi
+ dwLen
;
1125 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1127 wxLogLastError("GetDIBits");
1132 return dwLen
+ bi
.biSizeImage
;
1135 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1137 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1138 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1139 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1141 // offset of image from the beginning of the header
1142 DWORD ofs
= wxGetNumOfBitmapColors(pbmih
->biBitCount
) * sizeof(RGBQUAD
);
1143 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1146 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1147 image
, pbmi
, DIB_RGB_COLORS
);
1150 wxLogLastError("CreateDIBitmap");
1153 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1154 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1161 static const wxChar
*GetTymedName(DWORD tymed
)
1163 static wxChar s_szBuf
[128];
1165 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1166 case TYMED_FILE
: return wxT("TYMED_FILE");
1167 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1168 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1169 case TYMED_GDI
: return wxT("TYMED_GDI");
1170 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1171 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1173 wxSprintf(s_szBuf
, wxT("type of media format %d (unknown)"), tymed
);
1180 #endif // not using OLE at all