]>
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__)
35 #if defined(__WIN32__) && !defined(__GNUWIN32__) || defined(wxUSE_NORLANDER_HEADERS)
38 #include "wx/dataobj.h"
41 #ifdef wxUSE_NORLANDER_HEADERS
51 #include "wx/msw/ole/oleutils.h"
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
58 static const wxChar
*GetTymedName(DWORD tymed
);
60 #define GetTymedName(tymed) ""
61 #endif // Debug/!Debug
63 // to be moved into wx/msw/bitmap.h
64 extern size_t wxConvertBitmapToDIB(BITMAPINFO
*pbi
, const wxBitmap
& bitmap
);
65 extern wxBitmap
wxConvertDIBToBitmap(const BITMAPINFO
*bmi
);
67 // ----------------------------------------------------------------------------
68 // wxIEnumFORMATETC interface implementation
69 // ----------------------------------------------------------------------------
71 class wxIEnumFORMATETC
: public IEnumFORMATETC
74 wxIEnumFORMATETC(const wxDataFormat
* formats
, ULONG nCount
);
75 ~wxIEnumFORMATETC() { delete [] m_formats
; }
77 DECLARE_IUNKNOWN_METHODS
;
80 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
81 STDMETHODIMP
Skip(ULONG celt
);
83 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
86 CLIPFORMAT
*m_formats
; // formats we can provide data in
87 ULONG m_nCount
, // number of formats we support
88 m_nCurrent
; // current enum position
91 // ----------------------------------------------------------------------------
92 // wxIDataObject implementation of IDataObject interface
93 // ----------------------------------------------------------------------------
95 class wxIDataObject
: public IDataObject
98 wxIDataObject(wxDataObject
*pDataObject
);
101 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
102 // is), but in some cases, the situation is inversed, that is we delete it
103 // when this object is deleted - setting this flag enables such logic
104 void SetDeleteFlag() { m_mustDelete
= TRUE
; }
106 DECLARE_IUNKNOWN_METHODS
;
109 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
110 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
111 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
112 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
113 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
114 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
115 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
116 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
117 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
120 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
125 // ----------------------------------------------------------------------------
126 // small helper class for getting screen DC (we're working with bitmaps and
128 // ----------------------------------------------------------------------------
133 ScreenHDC() { m_hdc
= GetDC(NULL
); }
134 ~ScreenHDC() { ReleaseDC(NULL
, m_hdc
); }
135 operator HDC() const { return m_hdc
; }
141 // ============================================================================
143 // ============================================================================
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 void wxDataFormat::SetId(const wxChar
*format
)
151 m_format
= ::RegisterClipboardFormat(format
);
154 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
158 wxString
wxDataFormat::GetId() const
160 static const int max
= 256;
164 wxCHECK_MSG( !IsStandard(), s
,
165 wxT("name of predefined format cannot be retrieved") );
167 int len
= ::GetClipboardFormatName(m_format
, s
.GetWriteBuf(max
), max
);
172 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
178 // ----------------------------------------------------------------------------
180 // ----------------------------------------------------------------------------
182 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
184 ADD_IID(EnumFORMATETC
)
187 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
189 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
194 m_formats
= new CLIPFORMAT
[nCount
];
195 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
196 m_formats
[n
] = formats
[n
].GetFormatId();
200 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
204 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
207 // we only return 1 element at a time - mainly because I'm too lazy to
208 // implement something which you're never asked for anyhow
212 if ( m_nCurrent
< m_nCount
) {
214 format
.cfFormat
= m_formats
[m_nCurrent
++];
216 format
.dwAspect
= DVASPECT_CONTENT
;
218 format
.tymed
= TYMED_HGLOBAL
;
229 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
231 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
234 if ( m_nCurrent
< m_nCount
)
237 // no, can't skip this many elements
243 STDMETHODIMP
wxIEnumFORMATETC::Reset()
245 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
252 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
254 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
256 // unfortunately, we can't reuse the code in ctor - types are different
257 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
258 pNew
->m_nCount
= m_nCount
;
259 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
260 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
261 pNew
->m_formats
[n
] = m_formats
[n
];
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
273 BEGIN_IID_TABLE(wxIDataObject
)
278 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
280 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
283 m_pDataObject
= pDataObject
;
284 m_mustDelete
= FALSE
;
287 wxIDataObject::~wxIDataObject()
291 delete m_pDataObject
;
295 // get data functions
296 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
298 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
300 // is data is in our format?
301 HRESULT hr
= QueryGetData(pformatetcIn
);
305 // for the bitmaps and metafiles we use the handles instead of global memory
307 wxDataFormat format
= (wxDataFormatId
)pformatetcIn
->cfFormat
;
312 pmedium
->tymed
= TYMED_GDI
;
316 pmedium
->tymed
= TYMED_MFPICT
;
321 size_t size
= m_pDataObject
->GetDataSize(format
);
323 // it probably means that the method is just not implemented
324 wxLogDebug(wxT("Invalid data size - can't be 0"));
326 return DV_E_FORMATETC
;
329 if ( !format
.IsStandard() ) {
330 // for custom formats, put the size with the data - alloc the
332 size
+= sizeof(size_t);
335 HGLOBAL hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
, size
);
336 if ( hGlobal
== NULL
) {
337 wxLogLastError("GlobalAlloc");
338 return E_OUTOFMEMORY
;
342 pmedium
->tymed
= TYMED_HGLOBAL
;
343 pmedium
->hGlobal
= hGlobal
;
346 pmedium
->pUnkForRelease
= NULL
;
349 hr
= GetDataHere(pformatetcIn
, pmedium
);
351 // free resources we allocated
352 if ( pmedium
->tymed
== TYMED_HGLOBAL
) {
353 GlobalFree(pmedium
->hGlobal
);
362 STDMETHODIMP
wxIDataObject::GetDataHere(FORMATETC
*pformatetc
,
365 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetDataHere"));
367 // put data in caller provided medium
368 switch ( pmedium
->tymed
)
371 if ( !m_pDataObject
->GetDataHere(wxDF_BITMAP
, &pmedium
->hBitmap
) )
376 // this should be copied on bitmaps - but I don't have time for
378 wxFAIL_MSG(wxT("TODO - no support for metafiles in wxDataObject"));
384 HGLOBAL hGlobal
= pmedium
->hGlobal
;
385 void *pBuf
= GlobalLock(hGlobal
);
386 if ( pBuf
== NULL
) {
387 wxLogLastError(wxT("GlobalLock"));
388 return E_OUTOFMEMORY
;
391 if ( !wxDataFormat(pformatetc
->cfFormat
).IsStandard() ) {
392 // for custom formats, put the size with the data
393 size_t *p
= (size_t *)pBuf
;
394 *p
++ = GlobalSize(hGlobal
);
398 wxDataFormat format
= pformatetc
->cfFormat
;
399 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
402 GlobalUnlock(hGlobal
);
413 // set data functions
414 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
418 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
420 switch ( pmedium
->tymed
)
423 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
427 // this should be copied on bitmaps - but I don't have time for
429 wxFAIL_MSG(wxT("TODO - no support for metafiles in wxDataObject"));
435 void *pBuf
= GlobalLock(pmedium
->hGlobal
);
436 if ( pBuf
== NULL
) {
437 wxLogLastError("GlobalLock");
439 return E_OUTOFMEMORY
;
442 // we've got a problem with SetData() here because the base
443 // class version requires the size parameter which we don't
444 // have anywhere in OLE data transfer - so we need to
445 // synthetise it for known formats and we suppose that all data
446 // in custom formats starts with a DWORD containing the size
448 switch ( pformatetc
->cfFormat
)
452 size
= strlen((const char *)pBuf
);
456 size
= wcslen((const wchar_t *)pBuf
);
461 // these formats don't use size at all, anyhow (but
462 // pass data by handle, which is always a single DWORD)
467 // the handler will calculate size itself (it's too
468 // complicated to do it here)
474 // we suppose that the size precedes the data
475 size_t *p
= (size_t *)pBuf
;
481 wxDataFormat format
= pformatetc
->cfFormat
;
482 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
484 GlobalUnlock(pmedium
->hGlobal
);
497 // we own the medium, so we must release it - but do *not* free the
498 // bitmap handle fi we have it because we have copied it elsewhere
499 if ( pmedium
->tymed
== TYMED_GDI
)
501 pmedium
->hBitmap
= 0;
504 ReleaseStgMedium(pmedium
);
510 // information functions
511 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
513 // do we accept data in this format?
514 if ( pformatetc
== NULL
) {
515 wxLogTrace(wxTRACE_OleCalls
,
516 wxT("wxIDataObject::QueryGetData: invalid ptr."));
521 // the only one allowed by current COM implementation
522 if ( pformatetc
->lindex
!= -1 ) {
523 wxLogTrace(wxTRACE_OleCalls
,
524 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
530 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
531 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
532 wxLogTrace(wxTRACE_OleCalls
,
533 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
534 pformatetc
->dwAspect
);
536 return DV_E_DVASPECT
;
539 // and now check the type of data requested
540 wxDataFormat format
= pformatetc
->cfFormat
;
541 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
542 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
543 wxGetFormatName(format
));
546 wxLogTrace(wxTRACE_OleCalls
,
547 wxT("wxIDataObject::QueryGetData: %s unsupported"),
548 wxGetFormatName(format
));
550 return DV_E_FORMATETC
;
553 // we only transfer data by global memory, except for some particular cases
554 DWORD tymed
= pformatetc
->tymed
;
555 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
556 !(tymed
& TYMED_HGLOBAL
) ) {
557 // it's not what we're waiting for
558 wxLogTrace(wxTRACE_OleCalls
,
559 wxT("wxIDataObject::QueryGetData: %s != %s"),
561 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
570 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*pFormatetcIn
,
571 FORMATETC
*pFormatetcOut
)
573 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
575 // TODO we might want something better than this trivial implementation here
576 if ( pFormatetcOut
!= NULL
)
577 pFormatetcOut
->ptd
= NULL
;
579 return DATA_S_SAMEFORMATETC
;
582 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
583 IEnumFORMATETC
**ppenumFormatEtc
)
585 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
587 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
590 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
591 wxDataFormat format
, *formats
;
592 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
593 m_pDataObject
->GetAllFormats(formats
, dir
);
595 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
597 *ppenumFormatEtc
= pEnum
;
599 if ( formats
!= &format
) {
606 // ----------------------------------------------------------------------------
607 // advise sink functions (not implemented)
608 // ----------------------------------------------------------------------------
610 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*pformatetc
,
612 IAdviseSink
*pAdvSink
,
613 DWORD
*pdwConnection
)
615 return OLE_E_ADVISENOTSUPPORTED
;
618 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD dwConnection
)
620 return OLE_E_ADVISENOTSUPPORTED
;
623 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
)
625 return OLE_E_ADVISENOTSUPPORTED
;
628 // ----------------------------------------------------------------------------
630 // ----------------------------------------------------------------------------
632 wxDataObject::wxDataObject()
634 m_pIDataObject
= new wxIDataObject(this);
635 m_pIDataObject
->AddRef();
638 wxDataObject::~wxDataObject()
640 ReleaseInterface(m_pIDataObject
);
643 void wxDataObject::SetAutoDelete()
645 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
646 m_pIDataObject
->Release();
648 // so that the dtor doesnt' crash
649 m_pIDataObject
= NULL
;
652 bool wxDataObject::IsSupportedFormat(const wxDataFormat
& format
) const
654 size_t nFormatCount
= GetFormatCount();
655 if ( nFormatCount
== 1 ) {
656 return format
== GetPreferredFormat();
659 wxDataFormat
*formats
= new wxDataFormat
[nFormatCount
];
660 GetAllFormats(formats
);
663 for ( n
= 0; n
< nFormatCount
; n
++ ) {
664 if ( formats
[n
] == format
)
671 return n
< nFormatCount
;
677 const char *wxDataObject::GetFormatName(wxDataFormat format
)
679 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
681 #pragma warning(disable:4063)
684 static char s_szBuf
[128];
686 case CF_TEXT
: return "CF_TEXT";
687 case CF_BITMAP
: return "CF_BITMAP";
688 case CF_METAFILEPICT
: return "CF_METAFILEPICT";
689 case CF_SYLK
: return "CF_SYLK";
690 case CF_DIF
: return "CF_DIF";
691 case CF_TIFF
: return "CF_TIFF";
692 case CF_OEMTEXT
: return "CF_OEMTEXT";
693 case CF_DIB
: return "CF_DIB";
694 case CF_PALETTE
: return "CF_PALETTE";
695 case CF_PENDATA
: return "CF_PENDATA";
696 case CF_RIFF
: return "CF_RIFF";
697 case CF_WAVE
: return "CF_WAVE";
698 case CF_UNICODETEXT
: return "CF_UNICODETEXT";
699 case CF_ENHMETAFILE
: return "CF_ENHMETAFILE";
700 case CF_HDROP
: return "CF_HDROP";
701 case CF_LOCALE
: return "CF_LOCALE";
703 sprintf(s_szBuf
, "clipboard format 0x%x (unknown)", format
);
708 #pragma warning(default:4063)
714 // ----------------------------------------------------------------------------
715 // wxBitmapDataObject supports CF_DIB format
716 // ----------------------------------------------------------------------------
718 size_t wxBitmapDataObject::GetDataSize() const
720 return wxConvertBitmapToDIB(NULL
, GetBitmap());
723 bool wxBitmapDataObject::GetDataHere(void *buf
) const
725 return wxConvertBitmapToDIB((BITMAPINFO
*)buf
, GetBitmap()) != 0;
728 bool wxBitmapDataObject::SetData(size_t len
, const void *buf
)
730 wxBitmap
bitmap(wxConvertDIBToBitmap((const BITMAPINFO
*)buf
));
732 if ( !bitmap
.Ok() ) {
733 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
743 // ----------------------------------------------------------------------------
744 // wxBitmapDataObject2 supports CF_BITMAP format
745 // ----------------------------------------------------------------------------
747 // the bitmaps aren't passed by value as other types of data (i.e. by copying
748 // the data into a global memory chunk and passing it to the clipboard or
749 // another application or whatever), but by handle, so these generic functions
750 // don't make much sense to them.
752 size_t wxBitmapDataObject2::GetDataSize() const
757 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
759 // we put a bitmap handle into pBuf
760 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
765 bool wxBitmapDataObject2::SetData(size_t len
, const void *pBuf
)
767 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
770 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
772 wxLogLastError("GetObject(HBITMAP)");
775 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
776 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
778 if ( !bitmap
.Ok() ) {
779 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
791 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
793 if ( format
.GetFormatId() == CF_DIB
)
798 // shouldn't be selected into a DC or GetDIBits() would fail
799 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
800 wxT("can't copy bitmap selected into wxMemoryDC") );
802 // first get the info
804 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
805 NULL
, &bi
, DIB_RGB_COLORS
) )
807 wxLogLastError("GetDIBits(NULL)");
812 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
816 // no data to copy - we don't pass HBITMAP via global memory
821 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
824 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
826 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
827 if ( format
.GetFormatId() == CF_DIB
)
832 // shouldn't be selected into a DC or GetDIBits() would fail
833 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
834 wxT("can't copy bitmap selected into wxMemoryDC") );
836 // first get the info
837 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
838 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
840 wxLogLastError("GetDIBits(NULL)");
845 // and now copy the bits
846 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
847 pbi
, DIB_RGB_COLORS
) )
849 wxLogLastError("GetDIBits");
856 // we put a bitmap handle into pBuf
857 *(HBITMAP
*)pBuf
= hbmp
;
863 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
864 size_t size
, const void *pBuf
)
867 if ( format
.GetFormatId() == CF_DIB
)
869 // here we get BITMAPINFO struct followed by the actual bitmap bits and
870 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
873 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
874 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
875 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
876 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
879 wxLogLastError("CreateDIBitmap");
882 m_bitmap
.SetWidth(pbmih
->biWidth
);
883 m_bitmap
.SetHeight(pbmih
->biHeight
);
887 // it's easy with bitmaps: we pass them by handle
888 hbmp
= *(HBITMAP
*)pBuf
;
891 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
893 wxLogLastError("GetObject(HBITMAP)");
896 m_bitmap
.SetWidth(bmp
.bmWidth
);
897 m_bitmap
.SetHeight(bmp
.bmHeight
);
898 m_bitmap
.SetDepth(bmp
.bmPlanes
);
901 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
903 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
910 // ----------------------------------------------------------------------------
912 // ----------------------------------------------------------------------------
914 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
918 // the documentation states that the first member of DROPFILES structure is
919 // a "DWORD offset of double NUL terminated file list". What they mean by
920 // this (I wonder if you see it immediately) is that the list starts at
921 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
922 // to use DragQueryFile to work with this structure, but not told where and
924 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
926 // get number of files (magic value -1)
927 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
929 // for each file get the length, allocate memory and then get the name
932 for ( n
= 0; n
< nFiles
; n
++ ) {
933 // +1 for terminating NUL
934 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
936 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
938 m_filenames
.Add(str
);
940 if ( len2
!= len
- 1 ) {
941 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned"
942 " %d characters, %d expected."), len2
, len
- 1);
949 // ----------------------------------------------------------------------------
951 // ----------------------------------------------------------------------------
953 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
955 switch ( bitsPerPixel
)
958 // monochrome bitmap, 2 entries
968 // may be used with 24bit bitmaps, but we don't use it here - fall
973 // bmiColors not used at all with these bitmaps
977 wxFAIL_MSG( wxT("unknown bitmap format") );
982 size_t wxConvertBitmapToDIB(BITMAPINFO
*pbi
, const wxBitmap
& bitmap
)
984 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
986 // shouldn't be selected into a DC or GetDIBits() would fail
987 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
988 wxT("can't copy bitmap selected into wxMemoryDC") );
990 // prepare all the info we need
992 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
993 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
995 wxLogLastError("GetObject(bitmap)");
1000 // calculate the number of bits per pixel and the number of items in
1001 // bmiColors array (whose meaning depends on the bitmap format)
1002 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1003 WORD biColors
= wxGetNumOfBitmapColors(biBits
);
1007 bool wantSizeOnly
= pbi
== NULL
;
1011 // just for convenience
1012 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1014 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1015 bi
.biWidth
= bm
.bmWidth
;
1016 bi
.biHeight
= bm
.bmHeight
;
1018 bi
.biBitCount
= biBits
;
1019 bi
.biCompression
= BI_RGB
;
1021 bi
.biXPelsPerMeter
= 0;
1022 bi
.biYPelsPerMeter
= 0;
1024 bi
.biClrImportant
= 0;
1026 // memory we need for BITMAPINFO only
1027 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1029 // first get the image size
1031 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1033 wxLogLastError("GetDIBits(NULL)");
1040 // size of the header + size of the image
1041 return dwLen
+ bi
.biSizeImage
;
1044 // and now copy the bits
1045 void *image
= (char *)pbi
+ dwLen
;
1046 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1048 wxLogLastError("GetDIBits");
1053 return dwLen
+ bi
.biSizeImage
;
1056 wxBitmap
wxConvertDIBToBitmap(const BITMAPINFO
*pbmi
)
1058 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1059 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1060 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1062 // offset of image from the beginning of the header
1063 DWORD ofs
= wxGetNumOfBitmapColors(pbmih
->biBitCount
) * sizeof(RGBQUAD
);
1064 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1067 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1068 image
, pbmi
, DIB_RGB_COLORS
);
1071 wxLogLastError("CreateDIBitmap");
1074 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1075 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1082 static const wxChar
*GetTymedName(DWORD tymed
)
1084 static wxChar s_szBuf
[128];
1086 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1087 case TYMED_FILE
: return wxT("TYMED_FILE");
1088 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1089 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1090 case TYMED_GDI
: return wxT("TYMED_GDI");
1091 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1092 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1094 wxSprintf(s_szBuf
, wxT("type of media format %d (unknown)"), tymed
);
1101 #endif // not using OLE at all