]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/dataobj.cpp
more backwards compatible kbd handling in wxScrolledWindow
[wxWidgets.git] / src / msw / ole / dataobj.cpp
CommitLineData
269a5a34
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: msw/ole/dataobj.cpp
3// Purpose: implementation of wx[I]DataObject class
4// Author: Vadim Zeitlin
3f4a0c5b 5// Modified by:
269a5a34
VZ
6// Created: 10.05.98
7// RCS-ID: $Id$
8// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
0d0512bd 21 #pragma implementation "dataobj.h"
269a5a34
VZ
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#if defined(__BORLANDC__)
0d0512bd 28 #pragma hdrstop
269a5a34 29#endif
0d0512bd
VZ
30
31#if defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
32
7dee726c 33#ifndef WX_PRECOMP
0d0512bd
VZ
34 #include "wx/intl.h"
35 #include "wx/log.h"
7dee726c 36#endif
5260b1c5 37
3f480da3 38#include "wx/dataobj.h"
17b74d79 39
0d0512bd
VZ
40#include "wx/msw/private.h" // includes <windows.h>
41
7dee726c
RS
42#ifdef wxUSE_NORLANDER_HEADERS
43 #include <ole2.h>
44#endif
17b74d79 45#include <oleauto.h>
269a5a34 46
f6aa3903 47#ifndef __WIN32__
269a5a34
VZ
48 #include <ole2.h>
49 #include <olestd.h>
50#endif
51
8b85d24e
VZ
52#include <shlobj.h>
53
0d0512bd
VZ
54#include "wx/msw/ole/oleutils.h"
55
56#include "wx/msw/dib.h"
17b74d79 57
269a5a34
VZ
58// ----------------------------------------------------------------------------
59// functions
60// ----------------------------------------------------------------------------
61
8e193f38 62#ifdef __WXDEBUG__
d59ceba5 63 static const wxChar *GetTymedName(DWORD tymed);
1e8335b0
VZ
64#else // !Debug
65 #define GetTymedName(tymed) ""
66#endif // Debug/!Debug
269a5a34
VZ
67
68// ----------------------------------------------------------------------------
69// wxIEnumFORMATETC interface implementation
70// ----------------------------------------------------------------------------
8e193f38 71
269a5a34
VZ
72class wxIEnumFORMATETC : public IEnumFORMATETC
73{
74public:
8e193f38
VZ
75 wxIEnumFORMATETC(const wxDataFormat* formats, ULONG nCount);
76 ~wxIEnumFORMATETC() { delete [] m_formats; }
269a5a34 77
8e193f38 78 DECLARE_IUNKNOWN_METHODS;
269a5a34 79
8e193f38
VZ
80 // IEnumFORMATETC
81 STDMETHODIMP Next(ULONG celt, FORMATETC *rgelt, ULONG *pceltFetched);
82 STDMETHODIMP Skip(ULONG celt);
83 STDMETHODIMP Reset();
84 STDMETHODIMP Clone(IEnumFORMATETC **ppenum);
269a5a34
VZ
85
86private:
8e193f38
VZ
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
269a5a34
VZ
90};
91
92// ----------------------------------------------------------------------------
93// wxIDataObject implementation of IDataObject interface
94// ----------------------------------------------------------------------------
8e193f38 95
269a5a34
VZ
96class wxIDataObject : public IDataObject
97{
98public:
8e193f38 99 wxIDataObject(wxDataObject *pDataObject);
d59ceba5
VZ
100 ~wxIDataObject();
101
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; }
269a5a34 106
8e193f38 107 DECLARE_IUNKNOWN_METHODS;
269a5a34 108
8e193f38
VZ
109 // IDataObject
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);
269a5a34
VZ
119
120private:
8e193f38 121 wxDataObject *m_pDataObject; // pointer to C++ class we belong to
d59ceba5
VZ
122
123 bool m_mustDelete;
124};
125
269a5a34
VZ
126// ============================================================================
127// implementation
128// ============================================================================
129
3f480da3
VZ
130// ----------------------------------------------------------------------------
131// wxDataFormat
132// ----------------------------------------------------------------------------
133
134void wxDataFormat::SetId(const wxChar *format)
135{
136 m_format = ::RegisterClipboardFormat(format);
137 if ( !m_format )
138 {
139 wxLogError(_("Couldn't register clipboard format '%s'."), format);
140 }
141}
142
143wxString wxDataFormat::GetId() const
144{
145 static const int max = 256;
146
147 wxString s;
148
149 wxCHECK_MSG( !IsStandard(), s,
223d09f6 150 wxT("name of predefined format cannot be retrieved") );
3f480da3
VZ
151
152 int len = ::GetClipboardFormatName(m_format, s.GetWriteBuf(max), max);
153 s.UngetWriteBuf();
154
155 if ( !len )
156 {
157 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format);
158 }
159
160 return s;
161}
162
269a5a34
VZ
163// ----------------------------------------------------------------------------
164// wxIEnumFORMATETC
165// ----------------------------------------------------------------------------
166
167BEGIN_IID_TABLE(wxIEnumFORMATETC)
8e193f38
VZ
168 ADD_IID(Unknown)
169 ADD_IID(EnumFORMATETC)
269a5a34
VZ
170END_IID_TABLE;
171
172IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC)
173
8e193f38 174wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat *formats, ULONG nCount)
269a5a34 175{
8e193f38
VZ
176 m_cRef = 0;
177 m_nCurrent = 0;
178 m_nCount = nCount;
179 m_formats = new CLIPFORMAT[nCount];
180 for ( ULONG n = 0; n < nCount; n++ ) {
181 m_formats[n] = formats[n].GetFormatId();
182 }
269a5a34
VZ
183}
184
185STDMETHODIMP wxIEnumFORMATETC::Next(ULONG celt,
186 FORMATETC *rgelt,
187 ULONG *pceltFetched)
188{
8e193f38 189 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Next"));
269a5a34 190
8e193f38
VZ
191 if ( celt > 1 ) {
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
194 return S_FALSE;
195 }
269a5a34 196
8e193f38
VZ
197 if ( m_nCurrent < m_nCount ) {
198 FORMATETC format;
199 format.cfFormat = m_formats[m_nCurrent++];
200 format.ptd = NULL;
201 format.dwAspect = DVASPECT_CONTENT;
202 format.lindex = -1;
203 format.tymed = TYMED_HGLOBAL;
204 *rgelt = format;
269a5a34 205
8e193f38
VZ
206 return S_OK;
207 }
208 else {
209 // bad index
210 return S_FALSE;
211 }
269a5a34
VZ
212}
213
214STDMETHODIMP wxIEnumFORMATETC::Skip(ULONG celt)
215{
8e193f38
VZ
216 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Skip"));
217
218 m_nCurrent += celt;
219 if ( m_nCurrent < m_nCount )
220 return S_OK;
269a5a34 221
8e193f38
VZ
222 // no, can't skip this many elements
223 m_nCurrent -= celt;
269a5a34 224
8e193f38 225 return S_FALSE;
269a5a34
VZ
226}
227
228STDMETHODIMP wxIEnumFORMATETC::Reset()
229{
8e193f38 230 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Reset"));
269a5a34 231
8e193f38 232 m_nCurrent = 0;
269a5a34 233
8e193f38 234 return S_OK;
269a5a34
VZ
235}
236
237STDMETHODIMP wxIEnumFORMATETC::Clone(IEnumFORMATETC **ppenum)
238{
8e193f38
VZ
239 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Clone"));
240
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];
247 }
248 pNew->AddRef();
249 *ppenum = pNew;
269a5a34 250
8e193f38 251 return S_OK;
269a5a34
VZ
252}
253
254// ----------------------------------------------------------------------------
255// wxIDataObject
256// ----------------------------------------------------------------------------
257
258BEGIN_IID_TABLE(wxIDataObject)
8e193f38
VZ
259 ADD_IID(Unknown)
260 ADD_IID(DataObject)
269a5a34
VZ
261END_IID_TABLE;
262
263IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject)
264
265wxIDataObject::wxIDataObject(wxDataObject *pDataObject)
266{
8e193f38
VZ
267 m_cRef = 0;
268 m_pDataObject = pDataObject;
d59ceba5
VZ
269 m_mustDelete = FALSE;
270}
271
272wxIDataObject::~wxIDataObject()
273{
274 if ( m_mustDelete )
275 {
276 delete m_pDataObject;
277 }
269a5a34
VZ
278}
279
280// get data functions
281STDMETHODIMP wxIDataObject::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium)
282{
8e193f38 283 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetData"));
269a5a34 284
8e193f38
VZ
285 // is data is in our format?
286 HRESULT hr = QueryGetData(pformatetcIn);
287 if ( FAILED(hr) )
288 return hr;
269a5a34 289
8e193f38
VZ
290 // for the bitmaps and metafiles we use the handles instead of global memory
291 // to pass the data
292 wxDataFormat format = (wxDataFormatId)pformatetcIn->cfFormat;
269a5a34 293
8e193f38
VZ
294 switch ( format )
295 {
296 case wxDF_BITMAP:
297 pmedium->tymed = TYMED_GDI;
298 break;
299
d9317fd4
VZ
300 case wxDF_ENHMETAFILE:
301 pmedium->tymed = TYMED_ENHMF;
302 break;
303
8e193f38 304 case wxDF_METAFILE:
265b0c07
VZ
305 pmedium->hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
306 sizeof(METAFILEPICT));
307 if ( !pmedium->hGlobal ) {
308 wxLogLastError("GlobalAlloc");
309 return E_OUTOFMEMORY;
310 }
8e193f38
VZ
311 pmedium->tymed = TYMED_MFPICT;
312 break;
313
314 default:
315 // alloc memory
316 size_t size = m_pDataObject->GetDataSize(format);
317 if ( !size ) {
318 // it probably means that the method is just not implemented
319 wxLogDebug(wxT("Invalid data size - can't be 0"));
320
321 return DV_E_FORMATETC;
322 }
323
9e2896e5
VZ
324 if ( !format.IsStandard() ) {
325 // for custom formats, put the size with the data - alloc the
326 // space for it
327 size += sizeof(size_t);
328 }
329
8e193f38
VZ
330 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
331 if ( hGlobal == NULL ) {
332 wxLogLastError("GlobalAlloc");
333 return E_OUTOFMEMORY;
334 }
335
336 // copy data
337 pmedium->tymed = TYMED_HGLOBAL;
338 pmedium->hGlobal = hGlobal;
339 }
269a5a34 340
8e193f38
VZ
341 pmedium->pUnkForRelease = NULL;
342
343 // do copy the data
344 hr = GetDataHere(pformatetcIn, pmedium);
345 if ( FAILED(hr) ) {
346 // free resources we allocated
265b0c07 347 if ( pmedium->tymed & (TYMED_HGLOBAL | TYMED_MFPICT) ) {
8e193f38
VZ
348 GlobalFree(pmedium->hGlobal);
349 }
350
351 return hr;
352 }
269a5a34 353
8e193f38 354 return S_OK;
269a5a34
VZ
355}
356
357STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc,
358 STGMEDIUM *pmedium)
359{
8e193f38 360 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetDataHere"));
269a5a34 361
8e193f38
VZ
362 // put data in caller provided medium
363 switch ( pmedium->tymed )
364 {
365 case TYMED_GDI:
d59ceba5
VZ
366 if ( !m_pDataObject->GetDataHere(wxDF_BITMAP, &pmedium->hBitmap) )
367 return E_UNEXPECTED;
8e193f38
VZ
368 break;
369
d9317fd4
VZ
370 case TYMED_ENHMF:
371 if ( !m_pDataObject->GetDataHere(wxDF_ENHMETAFILE,
372 &pmedium->hEnhMetaFile) )
373 return E_UNEXPECTED;
374 break;
375
8e193f38 376 case TYMED_MFPICT:
d9317fd4
VZ
377 // fall through - we pass METAFILEPICT through HGLOBAL
378
8e193f38
VZ
379 case TYMED_HGLOBAL:
380 {
381 // copy data
9e2896e5
VZ
382 HGLOBAL hGlobal = pmedium->hGlobal;
383 void *pBuf = GlobalLock(hGlobal);
8e193f38
VZ
384 if ( pBuf == NULL ) {
385 wxLogLastError(wxT("GlobalLock"));
386 return E_OUTOFMEMORY;
387 }
388
9e2896e5
VZ
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);
393 pBuf = p;
394 }
395
396 wxDataFormat format = pformatetc->cfFormat;
d59ceba5
VZ
397 if ( !m_pDataObject->GetDataHere(format, pBuf) )
398 return E_UNEXPECTED;
8e193f38 399
9e2896e5 400 GlobalUnlock(hGlobal);
8e193f38
VZ
401 }
402 break;
403
404 default:
405 return DV_E_TYMED;
406 }
269a5a34 407
8e193f38 408 return S_OK;
269a5a34
VZ
409}
410
9e2896e5 411// set data functions
269a5a34
VZ
412STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
413 STGMEDIUM *pmedium,
414 BOOL fRelease)
415{
d59ceba5 416 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::SetData"));
8e193f38 417
d59ceba5
VZ
418 switch ( pmedium->tymed )
419 {
420 case TYMED_GDI:
9e2896e5 421 m_pDataObject->SetData(wxDF_BITMAP, 0, &pmedium->hBitmap);
d59ceba5
VZ
422 break;
423
d9317fd4
VZ
424 case TYMED_ENHMF:
425 m_pDataObject->SetData(wxDF_ENHMETAFILE, 0, &pmedium->hEnhMetaFile);
426 break;
427
d59ceba5 428 case TYMED_MFPICT:
d9317fd4 429 // fall through - we pass METAFILEPICT through HGLOBAL
d59ceba5
VZ
430 case TYMED_HGLOBAL:
431 {
51edda6a
VZ
432 wxDataFormat format = pformatetc->cfFormat;
433
434 // this is quite weird, but for file drag and drop, explorer
435 // calls our SetData() with the formats we do *not* support!
436 //
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
439 // check here
440 if ( !m_pDataObject->IsSupportedFormat(format) ) {
441 // go away!
442 return DV_E_FORMATETC;
443 }
444
d59ceba5
VZ
445 // copy data
446 void *pBuf = GlobalLock(pmedium->hGlobal);
447 if ( pBuf == NULL ) {
448 wxLogLastError("GlobalLock");
449
450 return E_OUTOFMEMORY;
451 }
452
9e2896e5
VZ
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
458 size_t size;
51edda6a 459 switch ( format )
9e2896e5
VZ
460 {
461 case CF_TEXT:
462 case CF_OEMTEXT:
463 size = strlen((const char *)pBuf);
464 break;
4d85bcd1 465#ifndef __WATCOMC__
9e2896e5
VZ
466 case CF_UNICODETEXT:
467 size = wcslen((const wchar_t *)pBuf);
468 break;
4d85bcd1 469#endif
9e2896e5
VZ
470 case CF_BITMAP:
471 case CF_HDROP:
472 // these formats don't use size at all, anyhow (but
473 // pass data by handle, which is always a single DWORD)
474 size = 0;
475 break;
476
8ee9d618
VZ
477 case CF_DIB:
478 // the handler will calculate size itself (it's too
479 // complicated to do it here)
480 size = 0;
481 break;
482
265b0c07
VZ
483 case CF_METAFILEPICT:
484 size = sizeof(METAFILEPICT);
485 break;
486
9e2896e5
VZ
487 default:
488 {
489 // we suppose that the size precedes the data
490 size_t *p = (size_t *)pBuf;
491 size = *p++;
492 pBuf = p;
493 }
494 }
495
9e2896e5 496 bool ok = m_pDataObject->SetData(format, size, pBuf);
d59ceba5
VZ
497
498 GlobalUnlock(pmedium->hGlobal);
9e2896e5
VZ
499
500 if ( !ok ) {
501 return E_UNEXPECTED;
502 }
d59ceba5
VZ
503 }
504 break;
505
506 default:
507 return DV_E_TYMED;
508 }
509
510 if ( fRelease ) {
265b0c07
VZ
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 )
d59ceba5 514 {
265b0c07
VZ
515 case TYMED_GDI:
516 pmedium->hBitmap = 0;
517 break;
518
519 case TYMED_MFPICT:
520 pmedium->hMetaFilePict = 0;
521 break;
d9317fd4
VZ
522
523 case TYMED_ENHMF:
524 pmedium->hEnhMetaFile = 0;
525 break;
d59ceba5
VZ
526 }
527
528 ReleaseStgMedium(pmedium);
529 }
530
531 return S_OK;
269a5a34
VZ
532}
533
534// information functions
535STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc)
536{
d59ceba5
VZ
537 // do we accept data in this format?
538 if ( pformatetc == NULL ) {
539 wxLogTrace(wxTRACE_OleCalls,
540 wxT("wxIDataObject::QueryGetData: invalid ptr."));
8e193f38 541
d59ceba5
VZ
542 return E_INVALIDARG;
543 }
269a5a34 544
d59ceba5
VZ
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"),
549 pformatetc->lindex);
269a5a34 550
d59ceba5
VZ
551 return DV_E_LINDEX;
552 }
269a5a34 553
d59ceba5
VZ
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);
559
560 return DV_E_DVASPECT;
561 }
562
563 // and now check the type of data requested
9e2896e5 564 wxDataFormat format = pformatetc->cfFormat;
d59ceba5 565 if ( m_pDataObject->IsSupportedFormat(format) ) {
d59ceba5 566 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s ok"),
1e8335b0 567 wxGetFormatName(format));
d59ceba5
VZ
568 }
569 else {
570 wxLogTrace(wxTRACE_OleCalls,
571 wxT("wxIDataObject::QueryGetData: %s unsupported"),
1e8335b0 572 wxGetFormatName(format));
9e2896e5 573
d59ceba5
VZ
574 return DV_E_FORMATETC;
575 }
576
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
d59ceba5
VZ
582 wxLogTrace(wxTRACE_OleCalls,
583 wxT("wxIDataObject::QueryGetData: %s != %s"),
584 GetTymedName(tymed),
585 GetTymedName(format == wxDF_BITMAP ? TYMED_GDI
586 : TYMED_HGLOBAL));
d59ceba5
VZ
587
588 return DV_E_TYMED;
589 }
590
269a5a34 591 return S_OK;
269a5a34
VZ
592}
593
594STDMETHODIMP wxIDataObject::GetCanonicalFormatEtc(FORMATETC *pFormatetcIn,
595 FORMATETC *pFormatetcOut)
596{
8e193f38
VZ
597 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetCanonicalFormatEtc"));
598
599 // TODO we might want something better than this trivial implementation here
600 if ( pFormatetcOut != NULL )
601 pFormatetcOut->ptd = NULL;
269a5a34 602
8e193f38 603 return DATA_S_SAMEFORMATETC;
269a5a34
VZ
604}
605
9e2896e5 606STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir,
269a5a34
VZ
607 IEnumFORMATETC **ppenumFormatEtc)
608{
8e193f38 609 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::EnumFormatEtc"));
269a5a34 610
9e2896e5
VZ
611 wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get
612 : wxDataObject::Set;
269a5a34 613
9e2896e5 614 size_t nFormatCount = m_pDataObject->GetFormatCount(dir);
8e193f38 615 wxDataFormat format, *formats;
9e2896e5
VZ
616 formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount];
617 m_pDataObject->GetAllFormats(formats, dir);
8e193f38
VZ
618
619 wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats, nFormatCount);
620 pEnum->AddRef();
621 *ppenumFormatEtc = pEnum;
622
623 if ( formats != &format ) {
624 delete [] formats;
625 }
269a5a34 626
8e193f38 627 return S_OK;
269a5a34
VZ
628}
629
9e2896e5 630// ----------------------------------------------------------------------------
269a5a34 631// advise sink functions (not implemented)
9e2896e5
VZ
632// ----------------------------------------------------------------------------
633
269a5a34
VZ
634STDMETHODIMP wxIDataObject::DAdvise(FORMATETC *pformatetc,
635 DWORD advf,
636 IAdviseSink *pAdvSink,
637 DWORD *pdwConnection)
638{
639 return OLE_E_ADVISENOTSUPPORTED;
640}
641
642STDMETHODIMP wxIDataObject::DUnadvise(DWORD dwConnection)
643{
644 return OLE_E_ADVISENOTSUPPORTED;
645}
646
647STDMETHODIMP wxIDataObject::EnumDAdvise(IEnumSTATDATA **ppenumAdvise)
648{
649 return OLE_E_ADVISENOTSUPPORTED;
650}
651
652// ----------------------------------------------------------------------------
653// wxDataObject
654// ----------------------------------------------------------------------------
655
656wxDataObject::wxDataObject()
657{
d59ceba5
VZ
658 m_pIDataObject = new wxIDataObject(this);
659 m_pIDataObject->AddRef();
269a5a34
VZ
660}
661
662wxDataObject::~wxDataObject()
663{
d59ceba5
VZ
664 ReleaseInterface(m_pIDataObject);
665}
666
667void wxDataObject::SetAutoDelete()
668{
669 ((wxIDataObject *)m_pIDataObject)->SetDeleteFlag();
670 m_pIDataObject->Release();
671
672 // so that the dtor doesnt' crash
673 m_pIDataObject = NULL;
269a5a34
VZ
674}
675
d9317fd4 676#ifdef __WXDEBUG__
8e193f38 677
d9317fd4
VZ
678const char *wxDataObject::GetFormatName(wxDataFormat format)
679{
680 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
681 #ifdef __VISUALC__
682 #pragma warning(disable:4063)
683 #endif // VC++
684
685 static char s_szBuf[256];
686 switch ( format ) {
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";
8e193f38 703
d9317fd4
VZ
704 default:
705 if ( !GetClipboardFormatName(format, s_szBuf, WXSIZEOF(s_szBuf)) )
706 {
707 // it must be a new predefined format we don't know the name of
708 sprintf(s_szBuf, "unknown CF (0x%04x)", format);
709 }
8e193f38 710
d9317fd4 711 return s_szBuf;
8e193f38 712 }
1e8335b0 713
d9317fd4
VZ
714 #ifdef __VISUALC__
715 #pragma warning(default:4063)
716 #endif // VC++
269a5a34
VZ
717}
718
1e8335b0
VZ
719#endif // Debug
720
3f480da3 721// ----------------------------------------------------------------------------
9e2896e5 722// wxBitmapDataObject supports CF_DIB format
3f480da3
VZ
723// ----------------------------------------------------------------------------
724
9e2896e5 725size_t wxBitmapDataObject::GetDataSize() const
3f480da3 726{
9e2896e5 727 return wxConvertBitmapToDIB(NULL, GetBitmap());
3f480da3
VZ
728}
729
9e2896e5 730bool wxBitmapDataObject::GetDataHere(void *buf) const
3f480da3 731{
0d0512bd 732 return wxConvertBitmapToDIB((LPBITMAPINFO)buf, GetBitmap()) != 0;
3f480da3
VZ
733}
734
9e2896e5 735bool wxBitmapDataObject::SetData(size_t len, const void *buf)
3f480da3 736{
0d0512bd 737 wxBitmap bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO)buf));
9e2896e5
VZ
738
739 if ( !bitmap.Ok() ) {
740 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
3f480da3 741
9e2896e5
VZ
742 return FALSE;
743 }
744
745 SetBitmap(bitmap);
3f480da3 746
9e2896e5 747 return TRUE;
3f480da3
VZ
748}
749
9e2896e5
VZ
750// ----------------------------------------------------------------------------
751// wxBitmapDataObject2 supports CF_BITMAP format
752// ----------------------------------------------------------------------------
753
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.
758
759size_t wxBitmapDataObject2::GetDataSize() const
3f480da3 760{
9e2896e5 761 return 0;
3f480da3
VZ
762}
763
9e2896e5 764bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
3f480da3 765{
9e2896e5
VZ
766 // we put a bitmap handle into pBuf
767 *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
768
769 return TRUE;
3f480da3
VZ
770}
771
265b0c07 772bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf)
3f480da3 773{
9e2896e5 774 HBITMAP hbmp = *(HBITMAP *)pBuf;
3f480da3 775
9e2896e5
VZ
776 BITMAP bmp;
777 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
778 {
779 wxLogLastError("GetObject(HBITMAP)");
780 }
8e193f38 781
9e2896e5
VZ
782 wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
783 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
d59ceba5 784
9e2896e5
VZ
785 if ( !bitmap.Ok() ) {
786 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
787
788 return FALSE;
789 }
790
791 SetBitmap(bitmap);
792
793 return TRUE;
d59ceba5
VZ
794}
795
9e2896e5 796#if 0
8e193f38
VZ
797
798size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
799{
d59ceba5
VZ
800 if ( format.GetFormatId() == CF_DIB )
801 {
802 // create the DIB
803 ScreenHDC hdc;
804
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") );
808
809 // first get the info
810 BITMAPINFO bi;
811 if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0,
812 NULL, &bi, DIB_RGB_COLORS) )
813 {
814 wxLogLastError("GetDIBits(NULL)");
815
816 return 0;
817 }
818
819 return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
820 }
821 else // CF_BITMAP
822 {
823 // no data to copy - we don't pass HBITMAP via global memory
824 return 0;
825 }
8e193f38
VZ
826}
827
d59ceba5 828bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
8e193f38
VZ
829 void *pBuf) const
830{
d59ceba5
VZ
831 wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") );
832
833 HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP();
834 if ( format.GetFormatId() == CF_DIB )
835 {
836 // create the DIB
837 ScreenHDC hdc;
838
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") );
842
843 // first get the info
844 BITMAPINFO *pbi = (BITMAPINFO *)pBuf;
845 if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) )
846 {
847 wxLogLastError("GetDIBits(NULL)");
848
849 return 0;
850 }
851
852 // and now copy the bits
853 if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
854 pbi, DIB_RGB_COLORS) )
855 {
856 wxLogLastError("GetDIBits");
857
858 return FALSE;
859 }
860 }
861 else // CF_BITMAP
862 {
863 // we put a bitmap handle into pBuf
864 *(HBITMAP *)pBuf = hbmp;
865 }
866
867 return TRUE;
868}
869
9e2896e5
VZ
870bool wxBitmapDataObject::SetData(const wxDataFormat& format,
871 size_t size, const void *pBuf)
d59ceba5
VZ
872{
873 HBITMAP hbmp;
874 if ( format.GetFormatId() == CF_DIB )
875 {
876 // here we get BITMAPINFO struct followed by the actual bitmap bits and
877 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
878 ScreenHDC hdc;
879
880 BITMAPINFO *pbmi = (BITMAPINFO *)pBuf;
881 BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
882 hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
883 pbmi + 1, pbmi, DIB_RGB_COLORS);
884 if ( !hbmp )
885 {
886 wxLogLastError("CreateDIBitmap");
887 }
888
889 m_bitmap.SetWidth(pbmih->biWidth);
890 m_bitmap.SetHeight(pbmih->biHeight);
891 }
892 else // CF_BITMAP
893 {
894 // it's easy with bitmaps: we pass them by handle
895 hbmp = *(HBITMAP *)pBuf;
896
897 BITMAP bmp;
898 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
899 {
900 wxLogLastError("GetObject(HBITMAP)");
901 }
902
903 m_bitmap.SetWidth(bmp.bmWidth);
904 m_bitmap.SetHeight(bmp.bmHeight);
905 m_bitmap.SetDepth(bmp.bmPlanes);
906 }
907
908 m_bitmap.SetHBITMAP((WXHBITMAP)hbmp);
909
910 wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") );
911
912 return TRUE;
8e193f38
VZ
913}
914
9e2896e5
VZ
915#endif // 0
916
917// ----------------------------------------------------------------------------
918// wxFileDataObject
919// ----------------------------------------------------------------------------
920
921bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData)
922{
923 m_filenames.Empty();
924
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
930 // how to get HDROP.
931 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
932
933 // get number of files (magic value -1)
934 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
935
51edda6a
VZ
936 wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") );
937
9e2896e5
VZ
938 // for each file get the length, allocate memory and then get the name
939 wxString str;
940 UINT len, n;
941 for ( n = 0; n < nFiles; n++ ) {
942 // +1 for terminating NUL
943 len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
944
945 UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len);
946 str.UngetWriteBuf();
947 m_filenames.Add(str);
948
949 if ( len2 != len - 1 ) {
950 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned"
951 " %d characters, %d expected."), len2, len - 1);
952 }
953 }
954
955 return TRUE;
956}
957
8b85d24e
VZ
958void wxFileDataObject::AddFile(const wxString& file)
959{
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);
964}
965
966size_t wxFileDataObject::GetDataSize() const
967{
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
971
972 // if no filenames in list, size is 0
dd10a646
VZ
973 if ( m_filenames.GetCount() == 0 )
974 return 0;
8b85d24e
VZ
975
976 // inital size of DROPFILES struct + null byte
977 size_t sz = sizeof(DROPFILES) + 1;
978
dd10a646
VZ
979 size_t count = m_filenames.GetCount();
980 for ( size_t i = 0; i < count; i++ )
8b85d24e
VZ
981 {
982 // add filename length plus null byte
983 sz += m_filenames[i].Len() + 1;
984 }
dd10a646 985
8b85d24e
VZ
986 return sz;
987}
988
989bool wxFileDataObject::GetDataHere(void *pData) const
990{
991 // pData points to an externally allocated memory block
992 // created using the size returned by GetDataSize()
993
994 // if pData is NULL, or there are no files, return
dd10a646
VZ
995 if ( !pData || m_filenames.GetCount() == 0 )
996 return FALSE;
8b85d24e
VZ
997
998 // convert data pointer to a DROPFILES struct pointer
999 LPDROPFILES pDrop = (LPDROPFILES) pData;
1000
1001 // initialize DROPFILES struct
1002 pDrop->pFiles = sizeof(DROPFILES);
dd10a646
VZ
1003 pDrop->fNC = FALSE; // not non-client coords
1004#if wxUSE_UNICODE
1005 pDrop->fWide = TRUE;
1006#else // ANSI
8b85d24e 1007 pDrop->fWide = FALSE;
dd10a646 1008#endif // Unicode/Ansi
8b85d24e
VZ
1009
1010 // set start of filenames list (null separated)
51babd09 1011 wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES));
8b85d24e 1012
dd10a646
VZ
1013 size_t count = m_filenames.GetCount();
1014 for (size_t i = 0; i < count; i++ )
8b85d24e
VZ
1015 {
1016 // copy filename to pbuf and add null terminator
1017 size_t len = m_filenames[i].Len();
1018 memcpy(pbuf, m_filenames[i], len);
1019 pbuf += len;
dd10a646 1020 *pbuf++ = wxT('\0');
8b85d24e 1021 }
dd10a646
VZ
1022
1023 *pbuf = wxT('\0'); // add final null terminator
8b85d24e
VZ
1024
1025 return TRUE;
1026}
1027
269a5a34
VZ
1028// ----------------------------------------------------------------------------
1029// private functions
1030// ----------------------------------------------------------------------------
8e193f38 1031
8ee9d618
VZ
1032static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel)
1033{
1034 switch ( bitsPerPixel )
1035 {
1036 case 1:
1037 // monochrome bitmap, 2 entries
1038 return 2;
1039
1040 case 4:
1041 return 16;
1042
1043 case 8:
1044 return 256;
1045
1046 case 24:
1047 // may be used with 24bit bitmaps, but we don't use it here - fall
1048 // through
1049
1050 case 16:
1051 case 32:
1052 // bmiColors not used at all with these bitmaps
1053 return 0;
1054
1055 default:
1056 wxFAIL_MSG( wxT("unknown bitmap format") );
1057 return 0;
1058 }
1059}
9e2896e5 1060
0d0512bd 1061size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi, const wxBitmap& bitmap)
9e2896e5 1062{
8ee9d618
VZ
1063 wxASSERT_MSG( bitmap.Ok(), wxT("invalid bmp can't be converted to DIB") );
1064
9e2896e5
VZ
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") );
1068
8ee9d618
VZ
1069 // prepare all the info we need
1070 BITMAP bm;
9e2896e5 1071 HBITMAP hbmp = (HBITMAP)bitmap.GetHBITMAP();
8ee9d618
VZ
1072 if ( !GetObject(hbmp, sizeof(bm), &bm) )
1073 {
1074 wxLogLastError("GetObject(bitmap)");
1075
1076 return 0;
1077 }
1078
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);
9e2896e5 1083
8ee9d618 1084 BITMAPINFO bi2;
9e2896e5 1085
8ee9d618
VZ
1086 bool wantSizeOnly = pbi == NULL;
1087 if ( wantSizeOnly )
1088 pbi = &bi2;
1089
1090 // just for convenience
1091 BITMAPINFOHEADER& bi = pbi->bmiHeader;
1092
1093 bi.biSize = sizeof(BITMAPINFOHEADER);
1094 bi.biWidth = bm.bmWidth;
1095 bi.biHeight = bm.bmHeight;
1096 bi.biPlanes = 1;
1097 bi.biBitCount = biBits;
1098 bi.biCompression = BI_RGB;
1099 bi.biSizeImage = 0;
1100 bi.biXPelsPerMeter = 0;
1101 bi.biYPelsPerMeter = 0;
1102 bi.biClrUsed = 0;
1103 bi.biClrImportant = 0;
1104
1105 // memory we need for BITMAPINFO only
1106 DWORD dwLen = bi.biSize + biColors * sizeof(RGBQUAD);
1107
1108 // first get the image size
9e2896e5 1109 ScreenHDC hdc;
8ee9d618 1110 if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, NULL, pbi, DIB_RGB_COLORS) )
9e2896e5
VZ
1111 {
1112 wxLogLastError("GetDIBits(NULL)");
1113
1114 return 0;
1115 }
1116
8ee9d618 1117 if ( wantSizeOnly )
9e2896e5 1118 {
8ee9d618
VZ
1119 // size of the header + size of the image
1120 return dwLen + bi.biSizeImage;
9e2896e5
VZ
1121 }
1122
1123 // and now copy the bits
8ee9d618
VZ
1124 void *image = (char *)pbi + dwLen;
1125 if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, image, pbi, DIB_RGB_COLORS) )
9e2896e5
VZ
1126 {
1127 wxLogLastError("GetDIBits");
1128
1129 return 0;
1130 }
1131
8ee9d618 1132 return dwLen + bi.biSizeImage;
9e2896e5
VZ
1133}
1134
0d0512bd 1135wxBitmap wxConvertDIBToBitmap(const LPBITMAPINFO pbmi)
9e2896e5
VZ
1136{
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;
1140
8ee9d618
VZ
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;
1144
9e2896e5
VZ
1145 ScreenHDC hdc;
1146 HBITMAP hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
8ee9d618 1147 image, pbmi, DIB_RGB_COLORS);
9e2896e5
VZ
1148 if ( !hbmp )
1149 {
1150 wxLogLastError("CreateDIBitmap");
1151 }
1152
1153 wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
1154 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
1155
1156 return bitmap;
1157}
1158
8e193f38
VZ
1159#ifdef __WXDEBUG__
1160
d59ceba5
VZ
1161static const wxChar *GetTymedName(DWORD tymed)
1162{
1163 static wxChar s_szBuf[128];
1164 switch ( tymed ) {
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");
1172 default:
1173 wxSprintf(s_szBuf, wxT("type of media format %d (unknown)"), tymed);
1174 return s_szBuf;
1175 }
269a5a34 1176}
5260b1c5 1177
8e193f38 1178#endif // Debug
2845ddc2 1179
d59ceba5 1180#endif // not using OLE at all
5260b1c5 1181