]>
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__)
36 #include "wx/dataobj.h"
38 #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
40 #include "wx/msw/private.h" // includes <windows.h>
42 #if wxUSE_NORLANDER_HEADERS
54 #include "wx/msw/ole/oleutils.h"
56 #include "wx/msw/dib.h"
58 #ifndef CFSTR_SHELLURL
59 #define CFSTR_SHELLURL _T("UniformResourceLocator")
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
68 static const wxChar
*GetTymedName(DWORD tymed
);
70 #define GetTymedName(tymed) _T("")
71 #endif // Debug/!Debug
73 // ----------------------------------------------------------------------------
74 // wxIEnumFORMATETC interface implementation
75 // ----------------------------------------------------------------------------
77 class wxIEnumFORMATETC
: public IEnumFORMATETC
80 wxIEnumFORMATETC(const wxDataFormat
* formats
, ULONG nCount
);
81 virtual ~wxIEnumFORMATETC() { delete [] m_formats
; }
83 DECLARE_IUNKNOWN_METHODS
;
86 STDMETHODIMP
Next(ULONG celt
, FORMATETC
*rgelt
, ULONG
*pceltFetched
);
87 STDMETHODIMP
Skip(ULONG celt
);
89 STDMETHODIMP
Clone(IEnumFORMATETC
**ppenum
);
92 CLIPFORMAT
*m_formats
; // formats we can provide data in
93 ULONG m_nCount
, // number of formats we support
94 m_nCurrent
; // current enum position
97 // ----------------------------------------------------------------------------
98 // wxIDataObject implementation of IDataObject interface
99 // ----------------------------------------------------------------------------
101 class wxIDataObject
: public IDataObject
104 wxIDataObject(wxDataObject
*pDataObject
);
105 virtual ~wxIDataObject();
107 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
108 // is), but in some cases, the situation is inversed, that is we delete it
109 // when this object is deleted - setting this flag enables such logic
110 void SetDeleteFlag() { m_mustDelete
= TRUE
; }
112 DECLARE_IUNKNOWN_METHODS
;
115 STDMETHODIMP
GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
);
116 STDMETHODIMP
GetDataHere(FORMATETC
*pformatetc
, STGMEDIUM
*pmedium
);
117 STDMETHODIMP
QueryGetData(FORMATETC
*pformatetc
);
118 STDMETHODIMP
GetCanonicalFormatEtc(FORMATETC
*In
, FORMATETC
*pOut
);
119 STDMETHODIMP
SetData(FORMATETC
*pfetc
, STGMEDIUM
*pmedium
, BOOL fRelease
);
120 STDMETHODIMP
EnumFormatEtc(DWORD dwDirection
, IEnumFORMATETC
**ppenumFEtc
);
121 STDMETHODIMP
DAdvise(FORMATETC
*pfetc
, DWORD ad
, IAdviseSink
*p
, DWORD
*pdw
);
122 STDMETHODIMP
DUnadvise(DWORD dwConnection
);
123 STDMETHODIMP
EnumDAdvise(IEnumSTATDATA
**ppenumAdvise
);
126 wxDataObject
*m_pDataObject
; // pointer to C++ class we belong to
131 // ============================================================================
133 // ============================================================================
135 // ----------------------------------------------------------------------------
137 // ----------------------------------------------------------------------------
139 void wxDataFormat::SetId(const wxChar
*format
)
141 m_format
= (wxDataFormat::NativeFormat
)::RegisterClipboardFormat(format
);
144 wxLogError(_("Couldn't register clipboard format '%s'."), format
);
148 wxString
wxDataFormat::GetId() const
150 static const int max
= 256;
154 wxCHECK_MSG( !IsStandard(), s
,
155 wxT("name of predefined format cannot be retrieved") );
157 int len
= ::GetClipboardFormatName(m_format
, s
.GetWriteBuf(max
), max
);
162 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format
);
168 // ----------------------------------------------------------------------------
170 // ----------------------------------------------------------------------------
172 BEGIN_IID_TABLE(wxIEnumFORMATETC
)
174 ADD_IID(EnumFORMATETC
)
177 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC
)
179 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat
*formats
, ULONG nCount
)
184 m_formats
= new CLIPFORMAT
[nCount
];
185 for ( ULONG n
= 0; n
< nCount
; n
++ ) {
186 m_formats
[n
] = formats
[n
].GetFormatId();
190 STDMETHODIMP
wxIEnumFORMATETC::Next(ULONG celt
,
192 ULONG
*WXUNUSED(pceltFetched
))
194 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Next"));
197 // we only return 1 element at a time - mainly because I'm too lazy to
198 // implement something which you're never asked for anyhow
202 if ( m_nCurrent
< m_nCount
) {
204 format
.cfFormat
= m_formats
[m_nCurrent
++];
206 format
.dwAspect
= DVASPECT_CONTENT
;
208 format
.tymed
= TYMED_HGLOBAL
;
219 STDMETHODIMP
wxIEnumFORMATETC::Skip(ULONG celt
)
221 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Skip"));
224 if ( m_nCurrent
< m_nCount
)
227 // no, can't skip this many elements
233 STDMETHODIMP
wxIEnumFORMATETC::Reset()
235 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Reset"));
242 STDMETHODIMP
wxIEnumFORMATETC::Clone(IEnumFORMATETC
**ppenum
)
244 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIEnumFORMATETC::Clone"));
246 // unfortunately, we can't reuse the code in ctor - types are different
247 wxIEnumFORMATETC
*pNew
= new wxIEnumFORMATETC(NULL
, 0);
248 pNew
->m_nCount
= m_nCount
;
249 pNew
->m_formats
= new CLIPFORMAT
[m_nCount
];
250 for ( ULONG n
= 0; n
< m_nCount
; n
++ ) {
251 pNew
->m_formats
[n
] = m_formats
[n
];
259 // ----------------------------------------------------------------------------
261 // ----------------------------------------------------------------------------
263 BEGIN_IID_TABLE(wxIDataObject
)
268 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject
)
270 wxIDataObject::wxIDataObject(wxDataObject
*pDataObject
)
273 m_pDataObject
= pDataObject
;
274 m_mustDelete
= FALSE
;
277 wxIDataObject::~wxIDataObject()
281 delete m_pDataObject
;
285 // get data functions
286 STDMETHODIMP
wxIDataObject::GetData(FORMATETC
*pformatetcIn
, STGMEDIUM
*pmedium
)
288 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetData"));
290 // is data is in our format?
291 HRESULT hr
= QueryGetData(pformatetcIn
);
295 // for the bitmaps and metafiles we use the handles instead of global memory
297 wxDataFormat format
= (wxDataFormatId
)pformatetcIn
->cfFormat
;
302 pmedium
->tymed
= TYMED_GDI
;
305 case wxDF_ENHMETAFILE
:
306 pmedium
->tymed
= TYMED_ENHMF
;
310 pmedium
->hGlobal
= GlobalAlloc(GMEM_MOVEABLE
| GMEM_SHARE
,
311 sizeof(METAFILEPICT
));
312 if ( !pmedium
->hGlobal
) {
313 wxLogLastError(wxT("GlobalAlloc"));
314 return E_OUTOFMEMORY
;
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(wxT("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
| TYMED_MFPICT
) ) {
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 if ( !m_pDataObject
->GetDataHere(wxDF_ENHMETAFILE
,
377 &pmedium
->hEnhMetaFile
) )
382 // fall through - we pass METAFILEPICT through HGLOBAL
387 HGLOBAL hGlobal
= pmedium
->hGlobal
;
388 void *pBuf
= GlobalLock(hGlobal
);
389 if ( pBuf
== NULL
) {
390 wxLogLastError(wxT("GlobalLock"));
391 return E_OUTOFMEMORY
;
394 if ( !wxDataFormat(pformatetc
->cfFormat
).IsStandard() ) {
395 // for custom formats, put the size with the data
396 size_t *p
= (size_t *)pBuf
;
397 *p
++ = GlobalSize(hGlobal
);
401 wxDataFormat format
= pformatetc
->cfFormat
;
402 if ( !m_pDataObject
->GetDataHere(format
, pBuf
) )
405 GlobalUnlock(hGlobal
);
416 // set data functions
417 STDMETHODIMP
wxIDataObject::SetData(FORMATETC
*pformatetc
,
421 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::SetData"));
423 switch ( pmedium
->tymed
)
426 m_pDataObject
->SetData(wxDF_BITMAP
, 0, &pmedium
->hBitmap
);
430 m_pDataObject
->SetData(wxDF_ENHMETAFILE
, 0, &pmedium
->hEnhMetaFile
);
434 // fall through - we pass METAFILEPICT through HGLOBAL
437 wxDataFormat format
= pformatetc
->cfFormat
;
439 // this is quite weird, but for file drag and drop, explorer
440 // calls our SetData() with the formats we do *not* support!
442 // as we can't fix this bug in explorer (it's a bug because it
443 // should only use formats returned by EnumFormatEtc), do the
445 if ( !m_pDataObject
->IsSupportedFormat(format
) ) {
447 return DV_E_FORMATETC
;
451 void *pBuf
= GlobalLock(pmedium
->hGlobal
);
452 if ( pBuf
== NULL
) {
453 wxLogLastError(wxT("GlobalLock"));
455 return E_OUTOFMEMORY
;
458 // we've got a problem with SetData() here because the base
459 // class version requires the size parameter which we don't
460 // have anywhere in OLE data transfer - so we need to
461 // synthetise it for known formats and we suppose that all data
462 // in custom formats starts with a DWORD containing the size
468 size
= strlen((const char *)pBuf
);
470 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
472 #if (defined(__BORLANDC__) && (__BORLANDC__ > 0x530))
473 size
= std::wcslen((const wchar_t *)pBuf
);
475 size
= ::wcslen((const wchar_t *)pBuf
);
481 // these formats don't use size at all, anyhow (but
482 // pass data by handle, which is always a single DWORD)
487 // the handler will calculate size itself (it's too
488 // complicated to do it here)
492 case CF_METAFILEPICT
:
493 size
= sizeof(METAFILEPICT
);
498 // we suppose that the size precedes the data
499 size_t *p
= (size_t *)pBuf
;
502 if (! format
.IsStandard() ) {
503 // see GetData for coresponding increment
504 size
-= sizeof(size_t);
509 bool ok
= m_pDataObject
->SetData(format
, size
, pBuf
);
511 GlobalUnlock(pmedium
->hGlobal
);
524 // we own the medium, so we must release it - but do *not* free any
525 // data we pass by handle because we have copied it elsewhere
526 switch ( pmedium
->tymed
)
529 pmedium
->hBitmap
= 0;
533 pmedium
->hMetaFilePict
= 0;
537 pmedium
->hEnhMetaFile
= 0;
541 ReleaseStgMedium(pmedium
);
547 // information functions
548 STDMETHODIMP
wxIDataObject::QueryGetData(FORMATETC
*pformatetc
)
550 // do we accept data in this format?
551 if ( pformatetc
== NULL
) {
552 wxLogTrace(wxTRACE_OleCalls
,
553 wxT("wxIDataObject::QueryGetData: invalid ptr."));
558 // the only one allowed by current COM implementation
559 if ( pformatetc
->lindex
!= -1 ) {
560 wxLogTrace(wxTRACE_OleCalls
,
561 wxT("wxIDataObject::QueryGetData: bad lindex %d"),
567 // we don't support anything other (THUMBNAIL, ICON, DOCPRINT...)
568 if ( pformatetc
->dwAspect
!= DVASPECT_CONTENT
) {
569 wxLogTrace(wxTRACE_OleCalls
,
570 wxT("wxIDataObject::QueryGetData: bad dwAspect %d"),
571 pformatetc
->dwAspect
);
573 return DV_E_DVASPECT
;
576 // and now check the type of data requested
577 wxDataFormat format
= pformatetc
->cfFormat
;
578 if ( m_pDataObject
->IsSupportedFormat(format
) ) {
579 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::QueryGetData: %s ok"),
580 wxGetFormatName(format
));
583 wxLogTrace(wxTRACE_OleCalls
,
584 wxT("wxIDataObject::QueryGetData: %s unsupported"),
585 wxGetFormatName(format
));
587 return DV_E_FORMATETC
;
590 // we only transfer data by global memory, except for some particular cases
591 DWORD tymed
= pformatetc
->tymed
;
592 if ( (format
== wxDF_BITMAP
&& !(tymed
& TYMED_GDI
)) &&
593 !(tymed
& TYMED_HGLOBAL
) ) {
594 // it's not what we're waiting for
595 wxLogTrace(wxTRACE_OleCalls
,
596 wxT("wxIDataObject::QueryGetData: %s != %s"),
598 GetTymedName(format
== wxDF_BITMAP
? TYMED_GDI
607 STDMETHODIMP
wxIDataObject::GetCanonicalFormatEtc(FORMATETC
*WXUNUSED(pFormatetcIn
),
608 FORMATETC
*pFormatetcOut
)
610 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::GetCanonicalFormatEtc"));
612 // TODO we might want something better than this trivial implementation here
613 if ( pFormatetcOut
!= NULL
)
614 pFormatetcOut
->ptd
= NULL
;
616 return DATA_S_SAMEFORMATETC
;
619 STDMETHODIMP
wxIDataObject::EnumFormatEtc(DWORD dwDir
,
620 IEnumFORMATETC
**ppenumFormatEtc
)
622 wxLogTrace(wxTRACE_OleCalls
, wxT("wxIDataObject::EnumFormatEtc"));
624 wxDataObject::Direction dir
= dwDir
== DATADIR_GET
? wxDataObject::Get
627 size_t nFormatCount
= m_pDataObject
->GetFormatCount(dir
);
629 wxDataFormat
*formats
;
630 formats
= nFormatCount
== 1 ? &format
: new wxDataFormat
[nFormatCount
];
631 m_pDataObject
->GetAllFormats(formats
, dir
);
633 wxIEnumFORMATETC
*pEnum
= new wxIEnumFORMATETC(formats
, nFormatCount
);
635 *ppenumFormatEtc
= pEnum
;
637 if ( formats
!= &format
) {
644 // ----------------------------------------------------------------------------
645 // advise sink functions (not implemented)
646 // ----------------------------------------------------------------------------
648 STDMETHODIMP
wxIDataObject::DAdvise(FORMATETC
*WXUNUSED(pformatetc
),
649 DWORD
WXUNUSED(advf
),
650 IAdviseSink
*WXUNUSED(pAdvSink
),
651 DWORD
*WXUNUSED(pdwConnection
))
653 return OLE_E_ADVISENOTSUPPORTED
;
656 STDMETHODIMP
wxIDataObject::DUnadvise(DWORD
WXUNUSED(dwConnection
))
658 return OLE_E_ADVISENOTSUPPORTED
;
661 STDMETHODIMP
wxIDataObject::EnumDAdvise(IEnumSTATDATA
**WXUNUSED(ppenumAdvise
))
663 return OLE_E_ADVISENOTSUPPORTED
;
666 // ----------------------------------------------------------------------------
668 // ----------------------------------------------------------------------------
670 wxDataObject::wxDataObject()
672 m_pIDataObject
= new wxIDataObject(this);
673 m_pIDataObject
->AddRef();
676 wxDataObject::~wxDataObject()
678 ReleaseInterface(m_pIDataObject
);
681 void wxDataObject::SetAutoDelete()
683 ((wxIDataObject
*)m_pIDataObject
)->SetDeleteFlag();
684 m_pIDataObject
->Release();
686 // so that the dtor doesnt' crash
687 m_pIDataObject
= NULL
;
692 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)
694 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
696 #pragma warning(disable:4063)
699 static wxChar s_szBuf
[256];
701 case CF_TEXT
: return wxT("CF_TEXT");
702 case CF_BITMAP
: return wxT("CF_BITMAP");
703 case CF_METAFILEPICT
: return wxT("CF_METAFILEPICT");
704 case CF_SYLK
: return wxT("CF_SYLK");
705 case CF_DIF
: return wxT("CF_DIF");
706 case CF_TIFF
: return wxT("CF_TIFF");
707 case CF_OEMTEXT
: return wxT("CF_OEMTEXT");
708 case CF_DIB
: return wxT("CF_DIB");
709 case CF_PALETTE
: return wxT("CF_PALETTE");
710 case CF_PENDATA
: return wxT("CF_PENDATA");
711 case CF_RIFF
: return wxT("CF_RIFF");
712 case CF_WAVE
: return wxT("CF_WAVE");
713 case CF_UNICODETEXT
: return wxT("CF_UNICODETEXT");
714 case CF_ENHMETAFILE
: return wxT("CF_ENHMETAFILE");
715 case CF_HDROP
: return wxT("CF_HDROP");
716 case CF_LOCALE
: return wxT("CF_LOCALE");
719 if ( !::GetClipboardFormatName(format
, s_szBuf
, WXSIZEOF(s_szBuf
)) )
721 // it must be a new predefined format we don't know the name of
722 wxSprintf(s_szBuf
, wxT("unknown CF (0x%04x)"), format
.GetFormatId());
729 #pragma warning(default:4063)
735 // ----------------------------------------------------------------------------
736 // wxBitmapDataObject supports CF_DIB format
737 // ----------------------------------------------------------------------------
739 size_t wxBitmapDataObject::GetDataSize() const
741 return wxConvertBitmapToDIB(NULL
, GetBitmap());
744 bool wxBitmapDataObject::GetDataHere(void *buf
) const
746 return wxConvertBitmapToDIB((LPBITMAPINFO
)buf
, GetBitmap()) != 0;
749 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len
), const void *buf
)
751 wxBitmap
bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO
)buf
));
753 if ( !bitmap
.Ok() ) {
754 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
764 // ----------------------------------------------------------------------------
765 // wxBitmapDataObject2 supports CF_BITMAP format
766 // ----------------------------------------------------------------------------
768 // the bitmaps aren't passed by value as other types of data (i.e. by copying
769 // the data into a global memory chunk and passing it to the clipboard or
770 // another application or whatever), but by handle, so these generic functions
771 // don't make much sense to them.
773 size_t wxBitmapDataObject2::GetDataSize() const
778 bool wxBitmapDataObject2::GetDataHere(void *pBuf
) const
780 // we put a bitmap handle into pBuf
781 *(WXHBITMAP
*)pBuf
= GetBitmap().GetHBITMAP();
786 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len
), const void *pBuf
)
788 HBITMAP hbmp
= *(HBITMAP
*)pBuf
;
791 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
793 wxLogLastError(wxT("GetObject(HBITMAP)"));
796 wxBitmap
bitmap(bmp
.bmWidth
, bmp
.bmHeight
, bmp
.bmPlanes
);
797 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
799 if ( !bitmap
.Ok() ) {
800 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
812 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat
& format
) const
814 if ( format
.GetFormatId() == CF_DIB
)
819 // shouldn't be selected into a DC or GetDIBits() would fail
820 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
821 wxT("can't copy bitmap selected into wxMemoryDC") );
823 // first get the info
825 if ( !GetDIBits(hdc
, (HBITMAP
)m_bitmap
.GetHBITMAP(), 0, 0,
826 NULL
, &bi
, DIB_RGB_COLORS
) )
828 wxLogLastError(wxT("GetDIBits(NULL)"));
833 return sizeof(BITMAPINFO
) + bi
.bmiHeader
.biSizeImage
;
837 // no data to copy - we don't pass HBITMAP via global memory
842 bool wxBitmapDataObject::GetDataHere(const wxDataFormat
& format
,
845 wxASSERT_MSG( m_bitmap
.Ok(), wxT("copying invalid bitmap") );
847 HBITMAP hbmp
= (HBITMAP
)m_bitmap
.GetHBITMAP();
848 if ( format
.GetFormatId() == CF_DIB
)
853 // shouldn't be selected into a DC or GetDIBits() would fail
854 wxASSERT_MSG( !m_bitmap
.GetSelectedInto(),
855 wxT("can't copy bitmap selected into wxMemoryDC") );
857 // first get the info
858 BITMAPINFO
*pbi
= (BITMAPINFO
*)pBuf
;
859 if ( !GetDIBits(hdc
, hbmp
, 0, 0, NULL
, pbi
, DIB_RGB_COLORS
) )
861 wxLogLastError(wxT("GetDIBits(NULL)"));
866 // and now copy the bits
867 if ( !GetDIBits(hdc
, hbmp
, 0, pbi
->bmiHeader
.biHeight
, pbi
+ 1,
868 pbi
, DIB_RGB_COLORS
) )
870 wxLogLastError(wxT("GetDIBits"));
877 // we put a bitmap handle into pBuf
878 *(HBITMAP
*)pBuf
= hbmp
;
884 bool wxBitmapDataObject::SetData(const wxDataFormat
& format
,
885 size_t size
, const void *pBuf
)
888 if ( format
.GetFormatId() == CF_DIB
)
890 // here we get BITMAPINFO struct followed by the actual bitmap bits and
891 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
894 BITMAPINFO
*pbmi
= (BITMAPINFO
*)pBuf
;
895 BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
896 hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
897 pbmi
+ 1, pbmi
, DIB_RGB_COLORS
);
900 wxLogLastError(wxT("CreateDIBitmap"));
903 m_bitmap
.SetWidth(pbmih
->biWidth
);
904 m_bitmap
.SetHeight(pbmih
->biHeight
);
908 // it's easy with bitmaps: we pass them by handle
909 hbmp
= *(HBITMAP
*)pBuf
;
912 if ( !GetObject(hbmp
, sizeof(BITMAP
), &bmp
) )
914 wxLogLastError(wxT("GetObject(HBITMAP)"));
917 m_bitmap
.SetWidth(bmp
.bmWidth
);
918 m_bitmap
.SetHeight(bmp
.bmHeight
);
919 m_bitmap
.SetDepth(bmp
.bmPlanes
);
922 m_bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
924 wxASSERT_MSG( m_bitmap
.Ok(), wxT("pasting invalid bitmap") );
931 // ----------------------------------------------------------------------------
933 // ----------------------------------------------------------------------------
935 bool wxFileDataObject::SetData(size_t WXUNUSED(size
), const void *pData
)
939 // the documentation states that the first member of DROPFILES structure is
940 // a "DWORD offset of double NUL terminated file list". What they mean by
941 // this (I wonder if you see it immediately) is that the list starts at
942 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
943 // to use DragQueryFile to work with this structure, but not told where and
945 HDROP hdrop
= (HDROP
)pData
; // NB: it works, but I'm not sure about it
947 // get number of files (magic value -1)
948 UINT nFiles
= ::DragQueryFile(hdrop
, (unsigned)-1, NULL
, 0u);
950 wxCHECK_MSG ( nFiles
!= (UINT
)-1, FALSE
, wxT("wrong HDROP handle") );
952 // for each file get the length, allocate memory and then get the name
955 for ( n
= 0; n
< nFiles
; n
++ ) {
956 // +1 for terminating NUL
957 len
= ::DragQueryFile(hdrop
, n
, NULL
, 0) + 1;
959 UINT len2
= ::DragQueryFile(hdrop
, n
, str
.GetWriteBuf(len
), len
);
961 m_filenames
.Add(str
);
963 if ( len2
!= len
- 1 ) {
964 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
965 %d characters, %d expected."), len2
, len
- 1);
972 void wxFileDataObject::AddFile(const wxString
& file
)
974 // just add file to filenames array
975 // all useful data (such as DROPFILES struct) will be
976 // created later as necessary
977 m_filenames
.Add(file
);
980 size_t wxFileDataObject::GetDataSize() const
982 // size returned will be the size of the DROPFILES structure,
983 // plus the list of filesnames (null byte separated), plus
984 // a double null at the end
986 // if no filenames in list, size is 0
987 if ( m_filenames
.GetCount() == 0 )
990 // inital size of DROPFILES struct + null byte
991 size_t sz
= sizeof(DROPFILES
) + 1;
993 size_t count
= m_filenames
.GetCount();
994 for ( size_t i
= 0; i
< count
; i
++ )
996 // add filename length plus null byte
997 sz
+= m_filenames
[i
].Len() + 1;
1003 bool wxFileDataObject::GetDataHere(void *pData
) const
1005 // pData points to an externally allocated memory block
1006 // created using the size returned by GetDataSize()
1008 // if pData is NULL, or there are no files, return
1009 if ( !pData
|| m_filenames
.GetCount() == 0 )
1012 // convert data pointer to a DROPFILES struct pointer
1013 LPDROPFILES pDrop
= (LPDROPFILES
) pData
;
1015 // initialize DROPFILES struct
1016 pDrop
->pFiles
= sizeof(DROPFILES
);
1017 pDrop
->fNC
= FALSE
; // not non-client coords
1019 pDrop
->fWide
= TRUE
;
1021 pDrop
->fWide
= FALSE
;
1022 #endif // Unicode/Ansi
1024 // set start of filenames list (null separated)
1025 wxChar
*pbuf
= (wxChar
*) ((BYTE
*)pDrop
+ sizeof(DROPFILES
));
1027 size_t count
= m_filenames
.GetCount();
1028 for (size_t i
= 0; i
< count
; i
++ )
1030 // copy filename to pbuf and add null terminator
1031 size_t len
= m_filenames
[i
].Len();
1032 memcpy(pbuf
, m_filenames
[i
], len
);
1034 *pbuf
++ = wxT('\0');
1037 // add final null terminator
1043 // ----------------------------------------------------------------------------
1045 // ----------------------------------------------------------------------------
1047 wxURLDataObject::wxURLDataObject()
1049 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1050 // but it seems that some browsers only provideo ne of them so we have to
1052 Add(new wxCustomDataObject(CFSTR_SHELLURL
));
1053 Add(new wxTextDataObject
);
1055 // we don't have any data yet
1056 m_dataObjectLast
= NULL
;
1059 bool wxURLDataObject::SetData(const wxDataFormat
& format
,
1063 m_dataObjectLast
= GetObject(format
);
1065 wxCHECK_MSG( m_dataObjectLast
, FALSE
,
1066 wxT("unsupported format in wxURLDataObject"));
1068 return m_dataObjectLast
->SetData(len
, buf
);
1071 wxString
wxURLDataObject::GetURL() const
1074 wxCHECK_MSG( m_dataObjectLast
, url
, _T("no data in wxURLDataObject") );
1076 size_t len
= m_dataObjectLast
->GetDataSize();
1078 m_dataObjectLast
->GetDataHere(url
.GetWriteBuf(len
+ 1));
1079 url
.UngetWriteBuf();
1084 // ----------------------------------------------------------------------------
1085 // private functions
1086 // ----------------------------------------------------------------------------
1088 static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel
)
1090 switch ( bitsPerPixel
)
1093 // monochrome bitmap, 2 entries
1103 // may be used with 24bit bitmaps, but we don't use it here - fall
1108 // bmiColors not used at all with these bitmaps
1112 wxFAIL_MSG( wxT("unknown bitmap format") );
1117 size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi
, const wxBitmap
& bitmap
)
1119 wxASSERT_MSG( bitmap
.Ok(), wxT("invalid bmp can't be converted to DIB") );
1121 // shouldn't be selected into a DC or GetDIBits() would fail
1122 wxASSERT_MSG( !bitmap
.GetSelectedInto(),
1123 wxT("can't copy bitmap selected into wxMemoryDC") );
1125 // prepare all the info we need
1127 HBITMAP hbmp
= (HBITMAP
)bitmap
.GetHBITMAP();
1128 if ( !GetObject(hbmp
, sizeof(bm
), &bm
) )
1130 wxLogLastError(wxT("GetObject(bitmap)"));
1135 // calculate the number of bits per pixel and the number of items in
1136 // bmiColors array (whose meaning depends on the bitmap format)
1137 WORD biBits
= bm
.bmPlanes
* bm
.bmBitsPixel
;
1138 WORD biColors
= (WORD
)wxGetNumOfBitmapColors(biBits
);
1142 bool wantSizeOnly
= pbi
== NULL
;
1146 // just for convenience
1147 BITMAPINFOHEADER
& bi
= pbi
->bmiHeader
;
1149 bi
.biSize
= sizeof(BITMAPINFOHEADER
);
1150 bi
.biWidth
= bm
.bmWidth
;
1151 bi
.biHeight
= bm
.bmHeight
;
1153 bi
.biBitCount
= biBits
;
1154 bi
.biCompression
= BI_RGB
;
1156 bi
.biXPelsPerMeter
= 0;
1157 bi
.biYPelsPerMeter
= 0;
1159 bi
.biClrImportant
= 0;
1161 // memory we need for BITMAPINFO only
1162 DWORD dwLen
= bi
.biSize
+ biColors
* sizeof(RGBQUAD
);
1164 // first get the image size
1166 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, NULL
, pbi
, DIB_RGB_COLORS
) )
1168 wxLogLastError(wxT("GetDIBits(NULL)"));
1175 // size of the header + size of the image
1176 return dwLen
+ bi
.biSizeImage
;
1179 // and now copy the bits
1180 void *image
= (char *)pbi
+ dwLen
;
1181 if ( !GetDIBits(hdc
, hbmp
, 0, bi
.biHeight
, image
, pbi
, DIB_RGB_COLORS
) )
1183 wxLogLastError(wxT("GetDIBits"));
1188 return dwLen
+ bi
.biSizeImage
;
1191 wxBitmap
wxConvertDIBToBitmap(const LPBITMAPINFO pbmi
)
1193 // here we get BITMAPINFO struct followed by the actual bitmap bits and
1194 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
1195 const BITMAPINFOHEADER
*pbmih
= &pbmi
->bmiHeader
;
1197 // offset of image from the beginning of the header
1198 DWORD ofs
= wxGetNumOfBitmapColors(pbmih
->biBitCount
) * sizeof(RGBQUAD
);
1199 void *image
= (char *)pbmih
+ sizeof(BITMAPINFOHEADER
) + ofs
;
1202 HBITMAP hbmp
= CreateDIBitmap(hdc
, pbmih
, CBM_INIT
,
1203 image
, pbmi
, DIB_RGB_COLORS
);
1206 wxLogLastError(wxT("CreateDIBitmap"));
1209 wxBitmap
bitmap(pbmih
->biWidth
, pbmih
->biHeight
, pbmih
->biBitCount
);
1210 bitmap
.SetHBITMAP((WXHBITMAP
)hbmp
);
1217 static const wxChar
*GetTymedName(DWORD tymed
)
1219 static wxChar s_szBuf
[128];
1221 case TYMED_HGLOBAL
: return wxT("TYMED_HGLOBAL");
1222 case TYMED_FILE
: return wxT("TYMED_FILE");
1223 case TYMED_ISTREAM
: return wxT("TYMED_ISTREAM");
1224 case TYMED_ISTORAGE
: return wxT("TYMED_ISTORAGE");
1225 case TYMED_GDI
: return wxT("TYMED_GDI");
1226 case TYMED_MFPICT
: return wxT("TYMED_MFPICT");
1227 case TYMED_ENHMF
: return wxT("TYMED_ENHMF");
1229 wxSprintf(s_szBuf
, wxT("type of media format %ld (unknown)"), tymed
);
1236 #else // not using OLE at all
1237 // ----------------------------------------------------------------------------
1239 // ----------------------------------------------------------------------------
1241 wxDataObject::wxDataObject()
1245 wxDataObject::~wxDataObject()
1249 void wxDataObject::SetAutoDelete()
1254 const wxChar
*wxDataObject::GetFormatName(wxDataFormat format
)