]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
14d98afe93c35f7f6d3271ff5bb79447afb06ef4
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
;
301 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
302 sizeof(METAFILEPICT
));
303 if ( !pmedium
->hGlobal
) {
304 wxLogLastError("GlobalAlloc");
305 return E_OUTOFMEMORY
;
307 pmedium
->tymed
= TYMED_MFPICT
;
312 size_t size
= m_pDataObject
->GetDataSize(format
);
314 // it probably means that the method is just not implemented
315 wxLogDebug(wxT("Invalid data size - can't be 0"));
317 return DV_E_FORMATETC
;
320 if ( !format
.IsStandard() ) {
321 // for custom formats, put the size with the data - alloc the
323 size
+= sizeof(size_t);
326 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
327 if ( hGlobal
== NULL
) {
328 wxLogLastError("GlobalAlloc");
329 return E_OUTOFMEMORY
;
333 pmedium
->tymed
= TYMED_HGLOBAL
;
334 pmedium
->hGlobal
= hGlobal
;
337 pmedium
->pUnkForRelease
= NULL
;
340 hr
= GetDataHere(pformatetcIn
, pmedium
);
342 // free resources we allocated
343 if ( pmedium
->tymed
& (TYMED_HGLOBAL
| TYMED_MFPICT
) ) {
344 GlobalFree(pmedium
->hGlobal
);
353 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
356 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
358 // put data in caller provided medium
359 switch ( pmedium
->tymed
)
362 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
370 HGLOBAL hGlobal
= pmedium
->hGlobal
;
371 void *pBuf
= GlobalLock(hGlobal
);
372 if ( pBuf
== NULL
) {
373 wxLogLastError(wxT("GlobalLock"));
374 return E_OUTOFMEMORY
;
377 if ( !wxDataFormat(pformatetc
->cfFormat
).IsStandard() ) {
378 // for custom formats, put the size with the data
379 size_t *p
= (size_t *)pBuf
;
380 *p
++ = GlobalSize(hGlobal
);
384 wxDataFormat format
= pformatetc
->cfFormat
;
385 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
388 GlobalUnlock(hGlobal
);
399 // set data functions
400 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
404 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
406 switch ( pmedium
->tymed
)
409 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
415 wxDataFormat format
= pformatetc
->cfFormat
;
417 // this is quite weird, but for file drag and drop, explorer
418 // calls our SetData() with the formats we do *not* support!
420 // as we can't fix this bug in explorer (it's a bug because it
421 // should only use formats returned by EnumFormatEtc), do the
423 if ( !m_pDataObject
->IsSupportedFormat(format
) ) {
425 return DV_E_FORMATETC
;
429 void *pBuf
= GlobalLock(pmedium
->hGlobal
);
430 if ( pBuf
== NULL
) {
431 wxLogLastError("GlobalLock");
433 return E_OUTOFMEMORY
;
436 // we've got a problem with SetData() here because the base
437 // class version requires the size parameter which we don't
438 // have anywhere in OLE data transfer - so we need to
439 // synthetise it for known formats and we suppose that all data
440 // in custom formats starts with a DWORD containing the size
446 size
= strlen((const char *)pBuf
);
450 size
= wcslen((const wchar_t *)pBuf
);
455 // these formats don't use size at all, anyhow (but
456 // pass data by handle, which is always a single DWORD)
461 // the handler will calculate size itself (it's too
462 // complicated to do it here)
466 case CF_METAFILEPICT
:
467 size
= sizeof(METAFILEPICT
);
472 // we suppose that the size precedes the data
473 size_t *p
= (size_t *)pBuf
;
479 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
481 GlobalUnlock(pmedium
->hGlobal
);
494 // we own the medium, so we must release it - but do *not* free any
495 // data we pass by handle because we have copied it elsewhere
496 switch ( pmedium
->tymed
)
499 pmedium
->hBitmap
= 0;
503 pmedium
->hMetaFilePict
= 0;
507 ReleaseStgMedium(pmedium
);
513 // information functions
514 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
516 // do we accept data in this format?
517 if ( pformatetc
== NULL
) {
518 wxLogTrace(wxTRACE_OleCalls
,
519 wxT("wxIDataObject::QueryGetData: invalid ptr."));
524 // the only one allowed by current COM implementation
525 if ( pformatetc
->lindex
!= -1 ) {
526 wxLogTrace(wxTRACE_OleCalls
,
527 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
533 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
534 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
535 wxLogTrace(wxTRACE_OleCalls
,
536 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
537 pformatetc
->dwAspect
);
539 return DV_E_DVASPECT
;
542 // and now check the type of data requested
543 wxDataFormat format
= pformatetc
->cfFormat
;
544 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
545 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
546 wxGetFormatName(format
));
549 wxLogTrace(wxTRACE_OleCalls
,
550 wxT("wxIDataObject::QueryGetData: %s unsupported"),
551 wxGetFormatName(format
));
553 return DV_E_FORMATETC
;
556 // we only transfer data by global memory, except for some particular cases
557 DWORD tymed
= pformatetc
->tymed
;
558 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
559 !(tymed
& TYMED_HGLOBAL
) ) {
560 // it's not what we're waiting for
561 wxLogTrace(wxTRACE_OleCalls
,
562 wxT("wxIDataObject::QueryGetData: %s != %s"),
564 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
573 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
574 FORMATETC
*pFormatetcOut
)
576 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
578 // TODO we might want something better than this trivial implementation here
579 if ( pFormatetcOut
!= NULL
)
580 pFormatetcOut
->ptd
= NULL
;
582 return DATA_S_SAMEFORMATETC
;
585 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
586 IEnumFORMATETC
**ppenumFormatEtc
)
588 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
590 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
593 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
594 wxDataFormat format
, *formats
;
595 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
596 m_pDataObject
->GetAllFormats(formats
, dir
);
598 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
600 *ppenumFormatEtc
= pEnum
;
602 if ( formats
!= &format
) {
609 // ----------------------------------------------------------------------------
610 // advise sink functions (not implemented)
611 // ----------------------------------------------------------------------------
613 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
615 IAdviseSink
*pAdvSink
,
616 DWORD
*pdwConnection
)
618 return OLE_E_ADVISENOTSUPPORTED
;
621 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
623 return OLE_E_ADVISENOTSUPPORTED
;
626 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
628 return OLE_E_ADVISENOTSUPPORTED
;
631 // ----------------------------------------------------------------------------
633 // ----------------------------------------------------------------------------
635 wxDataObject::wxDataObject()
637 m_pIDataObject
= new wxIDataObject(this);
638 m_pIDataObject
->AddRef();
641 wxDataObject::~wxDataObject()
643 ReleaseInterface(m_pIDataObject
);
646 void wxDataObject::SetAutoDelete()
648 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
649 m_pIDataObject
->Release();
651 // so that the dtor doesnt' crash
652 m_pIDataObject
= NULL
;
655 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
) const
657 size_t nFormatCount
= GetFormatCount();
658 if ( nFormatCount
== 1 ) {
659 return format
== GetPreferredFormat();
662 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
663 GetAllFormats(formats
);
666 for ( n
= 0; n
< nFormatCount
; n
++ ) {
667 if ( formats
[n
] == format
)
674 return n
< nFormatCount
;
680 const char *wxDataObject::GetFormatName(wxDataFormat format
)
682 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
684 #pragma warning(disable:4063)
687 static char s_szBuf
[128];
689 case CF_TEXT
: return "CF_TEXT";
690 case CF_BITMAP
: return "CF_BITMAP";
691 case CF_METAFILEPICT
: return "CF_METAFILEPICT";
692 case CF_SYLK
: return "CF_SYLK";
693 case CF_DIF
: return "CF_DIF";
694 case CF_TIFF
: return "CF_TIFF";
695 case CF_OEMTEXT
: return "CF_OEMTEXT";
696 case CF_DIB
: return "CF_DIB";
697 case CF_PALETTE
: return "CF_PALETTE";
698 case CF_PENDATA
: return "CF_PENDATA";
699 case CF_RIFF
: return "CF_RIFF";
700 case CF_WAVE
: return "CF_WAVE";
701 case CF_UNICODETEXT
: return "CF_UNICODETEXT";
702 case CF_ENHMETAFILE
: return "CF_ENHMETAFILE";
703 case CF_HDROP
: return "CF_HDROP";
704 case CF_LOCALE
: return "CF_LOCALE";
706 sprintf(s_szBuf
, "clipboard format 0x%x (unknown)", format
);
711 #pragma warning(default:4063)
717 // ----------------------------------------------------------------------------
718 // wxBitmapDataObject supports CF_DIB format
719 // ----------------------------------------------------------------------------
721 size_t wxBitmapDataObject::GetDataSize() const
723 return wxConvertBitmapToDIB(NULL
, GetBitmap());
726 bool wxBitmapDataObject::GetDataHere(void *buf
) const
728 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
731 bool wxBitmapDataObject::SetData(size_t len
, const void *buf
)
733 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
735 if ( !bitmap
.Ok() ) {
736 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
746 // ----------------------------------------------------------------------------
747 // wxBitmapDataObject2 supports CF_BITMAP format
748 // ----------------------------------------------------------------------------
750 // the bitmaps aren't passed by value as other types of data (i.e. by copying
751 // the data into a global memory chunk and passing it to the clipboard or
752 // another application or whatever), but by handle, so these generic functions
753 // don't make much sense to them.
755 size_t wxBitmapDataObject2::GetDataSize() const
760 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
762 // we put a bitmap handle into pBuf
763 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
768 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
770 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
773 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
775 wxLogLastError("GetObject(HBITMAP)");
778 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
779 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
781 if ( !bitmap
.Ok() ) {
782 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
794 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
796 if ( format
.GetFormatId() == CF_DIB
)
801 // shouldn't be selected into a DC or GetDIBits() would fail
802 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
803 wxT("can't copy bitmap selected into wxMemoryDC") );
805 // first get the info
807 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
808 NULL
, &bi
, DIB_RGB_COLORS
) )
810 wxLogLastError("GetDIBits(NULL)");
815 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
819 // no data to copy - we don't pass HBITMAP via global memory
824 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
827 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
829 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
830 if ( format
.GetFormatId() == CF_DIB
)
835 // shouldn't be selected into a DC or GetDIBits() would fail
836 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
837 wxT("can't copy bitmap selected into wxMemoryDC") );
839 // first get the info
840 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
841 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
843 wxLogLastError("GetDIBits(NULL)");
848 // and now copy the bits
849 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
850 pbi
, DIB_RGB_COLORS
) )
852 wxLogLastError("GetDIBits");
859 // we put a bitmap handle into pBuf
860 *(HBITMAP
*)pBuf
= hbmp
;
866 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
867 size_t size
, const void *pBuf
)
870 if ( format
.GetFormatId() == CF_DIB
)
872 // here we get BITMAPINFO struct followed by the actual bitmap bits and
873 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
876 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
877 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
878 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
879 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
882 wxLogLastError("CreateDIBitmap");
885 m_bitmap
.SetWidth(pbmih
->biWidth
);
886 m_bitmap
.SetHeight(pbmih
->biHeight
);
890 // it's easy with bitmaps: we pass them by handle
891 hbmp
= *(HBITMAP
*)pBuf
;
894 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
896 wxLogLastError("GetObject(HBITMAP)");
899 m_bitmap
.SetWidth(bmp
.bmWidth
);
900 m_bitmap
.SetHeight(bmp
.bmHeight
);
901 m_bitmap
.SetDepth(bmp
.bmPlanes
);
904 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
906 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
913 // ----------------------------------------------------------------------------
915 // ----------------------------------------------------------------------------
917 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
921 // the documentation states that the first member of DROPFILES structure is
922 // a "DWORD offset of double NUL terminated file list". What they mean by
923 // this (I wonder if you see it immediately) is that the list starts at
924 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
925 // to use DragQueryFile to work with this structure, but not told where and
927 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
929 // get number of files (magic value -1)
930 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
932 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
934 // for each file get the length, allocate memory and then get the name
937 for ( n
= 0; n
< nFiles
; n
++ ) {
938 // +1 for terminating NUL
939 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
941 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
943 m_filenames
.Add(str
);
945 if ( len2
!= len
- 1 ) {
946 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned"
947 " %d characters, %d expected."), len2
, len
- 1);
954 void wxFileDataObject::AddFile(const wxString
& file
)
956 // just add file to filenames array
957 // all useful data (such as DROPFILES struct) will be
958 // created later as necessary
959 m_filenames
.Add(file
);
962 size_t wxFileDataObject::GetDataSize() const
964 // size returned will be the size of the DROPFILES structure,
965 // plus the list of filesnames (null byte separated), plus
966 // a double null at the end
968 // if no filenames in list, size is 0
969 if ( m_filenames
.GetCount() == 0 )
972 // inital size of DROPFILES struct + null byte
973 size_t sz
= sizeof(DROPFILES
) + 1;
975 size_t count
= m_filenames
.GetCount();
976 for ( size_t i
= 0; i
< count
; i
++ )
978 // add filename length plus null byte
979 sz
+= m_filenames
[i
].Len() + 1;
985 bool wxFileDataObject::GetDataHere(void *pData
) const
987 // pData points to an externally allocated memory block
988 // created using the size returned by GetDataSize()
990 // if pData is NULL, or there are no files, return
991 if ( !pData
|| m_filenames
.GetCount() == 0 )
994 // convert data pointer to a DROPFILES struct pointer
995 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
997 // initialize DROPFILES struct
998 pDrop
->pFiles
= sizeof(DROPFILES
);
999 pDrop
->fNC
= FALSE
; // not non-client coords
1001 pDrop
->fWide
= TRUE
;
1003 pDrop
->fWide
= FALSE
;
1004 #endif // Unicode/Ansi
1006 // set start of filenames list (null separated)
1007 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1009 size_t count
= m_filenames
.GetCount();
1010 for (size_t i
= 0; i
< count
; i
++ )
1012 // copy filename to pbuf and add null terminator
1013 size_t len
= m_filenames
[i
].Len();
1014 memcpy(pbuf
, m_filenames
[i
], len
);
1016 *pbuf
++ = wxT('\0');
1019 *pbuf
= wxT('\0'); // add final null terminator
1024 // ----------------------------------------------------------------------------
1025 // private functions
1026 // ----------------------------------------------------------------------------
1028 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1030 switch ( bitsPerPixel
)
1033 // monochrome bitmap, 2 entries
1043 // may be used with 24bit bitmaps, but we don't use it here - fall
1048 // bmiColors not used at all with these bitmaps
1052 wxFAIL_MSG( wxT("unknown bitmap format") );
1057 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1059 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1061 // shouldn't be selected into a DC or GetDIBits() would fail
1062 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1063 wxT("can't copy bitmap selected into wxMemoryDC") );
1065 // prepare all the info we need
1067 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1068 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1070 wxLogLastError("GetObject(bitmap)");
1075 // calculate the number of bits per pixel and the number of items in
1076 // bmiColors array (whose meaning depends on the bitmap format)
1077 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1078 WORD biColors
= wxGetNumOfBitmapColors(biBits
);
1082 bool wantSizeOnly
= pbi
== NULL
;
1086 // just for convenience
1087 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1089 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1090 bi
.biWidth
= bm
.bmWidth
;
1091 bi
.biHeight
= bm
.bmHeight
;
1093 bi
.biBitCount
= biBits
;
1094 bi
.biCompression
= BI_RGB
;
1096 bi
.biXPelsPerMeter
= 0;
1097 bi
.biYPelsPerMeter
= 0;
1099 bi
.biClrImportant
= 0;
1101 // memory we need for BITMAPINFO only
1102 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1104 // first get the image size
1106 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1108 wxLogLastError("GetDIBits(NULL)");
1115 // size of the header + size of the image
1116 return dwLen
+ bi
.biSizeImage
;
1119 // and now copy the bits
1120 void *image
= (char *)pbi
+ dwLen
;
1121 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1123 wxLogLastError("GetDIBits");
1128 return dwLen
+ bi
.biSizeImage
;
1131 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1133 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1134 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1135 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1137 // offset of image from the beginning of the header
1138 DWORD ofs
= wxGetNumOfBitmapColors(pbmih
->biBitCount
) * sizeof(RGBQUAD
);
1139 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1142 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1143 image
, pbmi
, DIB_RGB_COLORS
);
1146 wxLogLastError("CreateDIBitmap");
1149 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1150 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1157 static const wxChar
*GetTymedName(DWORD tymed
)
1159 static wxChar s_szBuf
[128];
1161 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1162 case TYMED_FILE
: return wxT("TYMED_FILE");
1163 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1164 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1165 case TYMED_GDI
: return wxT("TYMED_GDI");
1166 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1167 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1169 wxSprintf(s_szBuf
, wxT("type of media format %d (unknown)"), tymed
);
1176 #endif // not using OLE at all