]> git.saurik.com Git - wxWidgets.git/blame - src/msw/ole/dataobj.cpp
fix for red to blue mapping in toolbar buttons
[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
300 case wxDF_METAFILE:
265b0c07
VZ
301 pmedium->hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
302 sizeof(METAFILEPICT));
303 if ( !pmedium->hGlobal ) {
304 wxLogLastError("GlobalAlloc");
305 return E_OUTOFMEMORY;
306 }
8e193f38
VZ
307 pmedium->tymed = TYMED_MFPICT;
308 break;
309
310 default:
311 // alloc memory
312 size_t size = m_pDataObject->GetDataSize(format);
313 if ( !size ) {
314 // it probably means that the method is just not implemented
315 wxLogDebug(wxT("Invalid data size - can't be 0"));
316
317 return DV_E_FORMATETC;
318 }
319
9e2896e5
VZ
320 if ( !format.IsStandard() ) {
321 // for custom formats, put the size with the data - alloc the
322 // space for it
323 size += sizeof(size_t);
324 }
325
8e193f38
VZ
326 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
327 if ( hGlobal == NULL ) {
328 wxLogLastError("GlobalAlloc");
329 return E_OUTOFMEMORY;
330 }
331
332 // copy data
333 pmedium->tymed = TYMED_HGLOBAL;
334 pmedium->hGlobal = hGlobal;
335 }
269a5a34 336
8e193f38
VZ
337 pmedium->pUnkForRelease = NULL;
338
339 // do copy the data
340 hr = GetDataHere(pformatetcIn, pmedium);
341 if ( FAILED(hr) ) {
342 // free resources we allocated
265b0c07 343 if ( pmedium->tymed & (TYMED_HGLOBAL | TYMED_MFPICT) ) {
8e193f38
VZ
344 GlobalFree(pmedium->hGlobal);
345 }
346
347 return hr;
348 }
269a5a34 349
8e193f38 350 return S_OK;
269a5a34
VZ
351}
352
353STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc,
354 STGMEDIUM *pmedium)
355{
8e193f38 356 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetDataHere"));
269a5a34 357
8e193f38
VZ
358 // put data in caller provided medium
359 switch ( pmedium->tymed )
360 {
361 case TYMED_GDI:
d59ceba5
VZ
362 if ( !m_pDataObject->GetDataHere(wxDF_BITMAP, &pmedium->hBitmap) )
363 return E_UNEXPECTED;
8e193f38
VZ
364 break;
365
366 case TYMED_MFPICT:
8e193f38
VZ
367 case TYMED_HGLOBAL:
368 {
369 // copy data
9e2896e5
VZ
370 HGLOBAL hGlobal = pmedium->hGlobal;
371 void *pBuf = GlobalLock(hGlobal);
8e193f38
VZ
372 if ( pBuf == NULL ) {
373 wxLogLastError(wxT("GlobalLock"));
374 return E_OUTOFMEMORY;
375 }
376
9e2896e5
VZ
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);
381 pBuf = p;
382 }
383
384 wxDataFormat format = pformatetc->cfFormat;
d59ceba5
VZ
385 if ( !m_pDataObject->GetDataHere(format, pBuf) )
386 return E_UNEXPECTED;
8e193f38 387
9e2896e5 388 GlobalUnlock(hGlobal);
8e193f38
VZ
389 }
390 break;
391
392 default:
393 return DV_E_TYMED;
394 }
269a5a34 395
8e193f38 396 return S_OK;
269a5a34
VZ
397}
398
9e2896e5 399// set data functions
269a5a34
VZ
400STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
401 STGMEDIUM *pmedium,
402 BOOL fRelease)
403{
d59ceba5 404 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::SetData"));
8e193f38 405
d59ceba5
VZ
406 switch ( pmedium->tymed )
407 {
408 case TYMED_GDI:
9e2896e5 409 m_pDataObject->SetData(wxDF_BITMAP, 0, &pmedium->hBitmap);
d59ceba5
VZ
410 break;
411
412 case TYMED_MFPICT:
d59ceba5
VZ
413 case TYMED_HGLOBAL:
414 {
51edda6a
VZ
415 wxDataFormat format = pformatetc->cfFormat;
416
417 // this is quite weird, but for file drag and drop, explorer
418 // calls our SetData() with the formats we do *not* support!
419 //
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
422 // check here
423 if ( !m_pDataObject->IsSupportedFormat(format) ) {
424 // go away!
425 return DV_E_FORMATETC;
426 }
427
d59ceba5
VZ
428 // copy data
429 void *pBuf = GlobalLock(pmedium->hGlobal);
430 if ( pBuf == NULL ) {
431 wxLogLastError("GlobalLock");
432
433 return E_OUTOFMEMORY;
434 }
435
9e2896e5
VZ
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
441 size_t size;
51edda6a 442 switch ( format )
9e2896e5
VZ
443 {
444 case CF_TEXT:
445 case CF_OEMTEXT:
446 size = strlen((const char *)pBuf);
447 break;
4d85bcd1 448#ifndef __WATCOMC__
9e2896e5
VZ
449 case CF_UNICODETEXT:
450 size = wcslen((const wchar_t *)pBuf);
451 break;
4d85bcd1 452#endif
9e2896e5
VZ
453 case CF_BITMAP:
454 case CF_HDROP:
455 // these formats don't use size at all, anyhow (but
456 // pass data by handle, which is always a single DWORD)
457 size = 0;
458 break;
459
8ee9d618
VZ
460 case CF_DIB:
461 // the handler will calculate size itself (it's too
462 // complicated to do it here)
463 size = 0;
464 break;
465
265b0c07
VZ
466 case CF_METAFILEPICT:
467 size = sizeof(METAFILEPICT);
468 break;
469
9e2896e5
VZ
470 default:
471 {
472 // we suppose that the size precedes the data
473 size_t *p = (size_t *)pBuf;
474 size = *p++;
475 pBuf = p;
476 }
477 }
478
9e2896e5 479 bool ok = m_pDataObject->SetData(format, size, pBuf);
d59ceba5
VZ
480
481 GlobalUnlock(pmedium->hGlobal);
9e2896e5
VZ
482
483 if ( !ok ) {
484 return E_UNEXPECTED;
485 }
d59ceba5
VZ
486 }
487 break;
488
489 default:
490 return DV_E_TYMED;
491 }
492
493 if ( fRelease ) {
265b0c07
VZ
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 )
d59ceba5 497 {
265b0c07
VZ
498 case TYMED_GDI:
499 pmedium->hBitmap = 0;
500 break;
501
502 case TYMED_MFPICT:
503 pmedium->hMetaFilePict = 0;
504 break;
d59ceba5
VZ
505 }
506
507 ReleaseStgMedium(pmedium);
508 }
509
510 return S_OK;
269a5a34
VZ
511}
512
513// information functions
514STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc)
515{
d59ceba5
VZ
516 // do we accept data in this format?
517 if ( pformatetc == NULL ) {
518 wxLogTrace(wxTRACE_OleCalls,
519 wxT("wxIDataObject::QueryGetData: invalid ptr."));
8e193f38 520
d59ceba5
VZ
521 return E_INVALIDARG;
522 }
269a5a34 523
d59ceba5
VZ
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"),
528 pformatetc->lindex);
269a5a34 529
d59ceba5
VZ
530 return DV_E_LINDEX;
531 }
269a5a34 532
d59ceba5
VZ
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);
538
539 return DV_E_DVASPECT;
540 }
541
542 // and now check the type of data requested
9e2896e5 543 wxDataFormat format = pformatetc->cfFormat;
d59ceba5 544 if ( m_pDataObject->IsSupportedFormat(format) ) {
d59ceba5 545 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::QueryGetData: %s ok"),
1e8335b0 546 wxGetFormatName(format));
d59ceba5
VZ
547 }
548 else {
549 wxLogTrace(wxTRACE_OleCalls,
550 wxT("wxIDataObject::QueryGetData: %s unsupported"),
1e8335b0 551 wxGetFormatName(format));
9e2896e5 552
d59ceba5
VZ
553 return DV_E_FORMATETC;
554 }
555
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
d59ceba5
VZ
561 wxLogTrace(wxTRACE_OleCalls,
562 wxT("wxIDataObject::QueryGetData: %s != %s"),
563 GetTymedName(tymed),
564 GetTymedName(format == wxDF_BITMAP ? TYMED_GDI
565 : TYMED_HGLOBAL));
d59ceba5
VZ
566
567 return DV_E_TYMED;
568 }
569
269a5a34 570 return S_OK;
269a5a34
VZ
571}
572
573STDMETHODIMP wxIDataObject::GetCanonicalFormatEtc(FORMATETC *pFormatetcIn,
574 FORMATETC *pFormatetcOut)
575{
8e193f38
VZ
576 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetCanonicalFormatEtc"));
577
578 // TODO we might want something better than this trivial implementation here
579 if ( pFormatetcOut != NULL )
580 pFormatetcOut->ptd = NULL;
269a5a34 581
8e193f38 582 return DATA_S_SAMEFORMATETC;
269a5a34
VZ
583}
584
9e2896e5 585STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir,
269a5a34
VZ
586 IEnumFORMATETC **ppenumFormatEtc)
587{
8e193f38 588 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::EnumFormatEtc"));
269a5a34 589
9e2896e5
VZ
590 wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get
591 : wxDataObject::Set;
269a5a34 592
9e2896e5 593 size_t nFormatCount = m_pDataObject->GetFormatCount(dir);
8e193f38 594 wxDataFormat format, *formats;
9e2896e5
VZ
595 formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount];
596 m_pDataObject->GetAllFormats(formats, dir);
8e193f38
VZ
597
598 wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats, nFormatCount);
599 pEnum->AddRef();
600 *ppenumFormatEtc = pEnum;
601
602 if ( formats != &format ) {
603 delete [] formats;
604 }
269a5a34 605
8e193f38 606 return S_OK;
269a5a34
VZ
607}
608
9e2896e5 609// ----------------------------------------------------------------------------
269a5a34 610// advise sink functions (not implemented)
9e2896e5
VZ
611// ----------------------------------------------------------------------------
612
269a5a34
VZ
613STDMETHODIMP wxIDataObject::DAdvise(FORMATETC *pformatetc,
614 DWORD advf,
615 IAdviseSink *pAdvSink,
616 DWORD *pdwConnection)
617{
618 return OLE_E_ADVISENOTSUPPORTED;
619}
620
621STDMETHODIMP wxIDataObject::DUnadvise(DWORD dwConnection)
622{
623 return OLE_E_ADVISENOTSUPPORTED;
624}
625
626STDMETHODIMP wxIDataObject::EnumDAdvise(IEnumSTATDATA **ppenumAdvise)
627{
628 return OLE_E_ADVISENOTSUPPORTED;
629}
630
631// ----------------------------------------------------------------------------
632// wxDataObject
633// ----------------------------------------------------------------------------
634
635wxDataObject::wxDataObject()
636{
d59ceba5
VZ
637 m_pIDataObject = new wxIDataObject(this);
638 m_pIDataObject->AddRef();
269a5a34
VZ
639}
640
641wxDataObject::~wxDataObject()
642{
d59ceba5
VZ
643 ReleaseInterface(m_pIDataObject);
644}
645
646void wxDataObject::SetAutoDelete()
647{
648 ((wxIDataObject *)m_pIDataObject)->SetDeleteFlag();
649 m_pIDataObject->Release();
650
651 // so that the dtor doesnt' crash
652 m_pIDataObject = NULL;
269a5a34
VZ
653}
654
8e193f38 655bool wxDataObject::IsSupportedFormat(const wxDataFormat& format) const
269a5a34 656{
8e193f38
VZ
657 size_t nFormatCount = GetFormatCount();
658 if ( nFormatCount == 1 ) {
659 return format == GetPreferredFormat();
660 }
661 else {
662 wxDataFormat *formats = new wxDataFormat[nFormatCount];
663 GetAllFormats(formats);
664
665 size_t n;
666 for ( n = 0; n < nFormatCount; n++ ) {
667 if ( formats[n] == format )
668 break;
669 }
670
671 delete [] formats;
672
673 // found?
674 return n < nFormatCount;
675 }
676}
677
1e8335b0
VZ
678#ifdef __WXDEBUG__
679
8e193f38
VZ
680const char *wxDataObject::GetFormatName(wxDataFormat format)
681{
fd3f686c 682 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
3f4a0c5b 683 #ifdef __VISUALC__
fd3f686c
VZ
684 #pragma warning(disable:4063)
685 #endif // VC++
686
269a5a34
VZ
687 static char s_szBuf[128];
688 switch ( format ) {
9b64e798 689 case CF_TEXT: return "CF_TEXT";
269a5a34 690 case CF_BITMAP: return "CF_BITMAP";
9b64e798 691 case CF_METAFILEPICT: return "CF_METAFILEPICT";
269a5a34
VZ
692 case CF_SYLK: return "CF_SYLK";
693 case CF_DIF: return "CF_DIF";
694 case CF_TIFF: return "CF_TIFF";
9b64e798 695 case CF_OEMTEXT: return "CF_OEMTEXT";
269a5a34
VZ
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";
9b64e798 701 case CF_UNICODETEXT: return "CF_UNICODETEXT";
269a5a34
VZ
702 case CF_ENHMETAFILE: return "CF_ENHMETAFILE";
703 case CF_HDROP: return "CF_HDROP";
704 case CF_LOCALE: return "CF_LOCALE";
705 default:
d59ceba5 706 sprintf(s_szBuf, "clipboard format 0x%x (unknown)", format);
269a5a34
VZ
707 return s_szBuf;
708 }
fd3f686c 709
3f4a0c5b 710 #ifdef __VISUALC__
fd3f686c
VZ
711 #pragma warning(default:4063)
712 #endif // VC++
269a5a34
VZ
713}
714
1e8335b0
VZ
715#endif // Debug
716
3f480da3 717// ----------------------------------------------------------------------------
9e2896e5 718// wxBitmapDataObject supports CF_DIB format
3f480da3
VZ
719// ----------------------------------------------------------------------------
720
9e2896e5 721size_t wxBitmapDataObject::GetDataSize() const
3f480da3 722{
9e2896e5 723 return wxConvertBitmapToDIB(NULL, GetBitmap());
3f480da3
VZ
724}
725
9e2896e5 726bool wxBitmapDataObject::GetDataHere(void *buf) const
3f480da3 727{
0d0512bd 728 return wxConvertBitmapToDIB((LPBITMAPINFO)buf, GetBitmap()) != 0;
3f480da3
VZ
729}
730
9e2896e5 731bool wxBitmapDataObject::SetData(size_t len, const void *buf)
3f480da3 732{
0d0512bd 733 wxBitmap bitmap(wxConvertDIBToBitmap((const LPBITMAPINFO)buf));
9e2896e5
VZ
734
735 if ( !bitmap.Ok() ) {
736 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
3f480da3 737
9e2896e5
VZ
738 return FALSE;
739 }
740
741 SetBitmap(bitmap);
3f480da3 742
9e2896e5 743 return TRUE;
3f480da3
VZ
744}
745
9e2896e5
VZ
746// ----------------------------------------------------------------------------
747// wxBitmapDataObject2 supports CF_BITMAP format
748// ----------------------------------------------------------------------------
749
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.
754
755size_t wxBitmapDataObject2::GetDataSize() const
3f480da3 756{
9e2896e5 757 return 0;
3f480da3
VZ
758}
759
9e2896e5 760bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
3f480da3 761{
9e2896e5
VZ
762 // we put a bitmap handle into pBuf
763 *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
764
765 return TRUE;
3f480da3
VZ
766}
767
265b0c07 768bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf)
3f480da3 769{
9e2896e5 770 HBITMAP hbmp = *(HBITMAP *)pBuf;
3f480da3 771
9e2896e5
VZ
772 BITMAP bmp;
773 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
774 {
775 wxLogLastError("GetObject(HBITMAP)");
776 }
8e193f38 777
9e2896e5
VZ
778 wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
779 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
d59ceba5 780
9e2896e5
VZ
781 if ( !bitmap.Ok() ) {
782 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
783
784 return FALSE;
785 }
786
787 SetBitmap(bitmap);
788
789 return TRUE;
d59ceba5
VZ
790}
791
9e2896e5 792#if 0
8e193f38
VZ
793
794size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
795{
d59ceba5
VZ
796 if ( format.GetFormatId() == CF_DIB )
797 {
798 // create the DIB
799 ScreenHDC hdc;
800
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") );
804
805 // first get the info
806 BITMAPINFO bi;
807 if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0,
808 NULL, &bi, DIB_RGB_COLORS) )
809 {
810 wxLogLastError("GetDIBits(NULL)");
811
812 return 0;
813 }
814
815 return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
816 }
817 else // CF_BITMAP
818 {
819 // no data to copy - we don't pass HBITMAP via global memory
820 return 0;
821 }
8e193f38
VZ
822}
823
d59ceba5 824bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
8e193f38
VZ
825 void *pBuf) const
826{
d59ceba5
VZ
827 wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") );
828
829 HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP();
830 if ( format.GetFormatId() == CF_DIB )
831 {
832 // create the DIB
833 ScreenHDC hdc;
834
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") );
838
839 // first get the info
840 BITMAPINFO *pbi = (BITMAPINFO *)pBuf;
841 if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) )
842 {
843 wxLogLastError("GetDIBits(NULL)");
844
845 return 0;
846 }
847
848 // and now copy the bits
849 if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
850 pbi, DIB_RGB_COLORS) )
851 {
852 wxLogLastError("GetDIBits");
853
854 return FALSE;
855 }
856 }
857 else // CF_BITMAP
858 {
859 // we put a bitmap handle into pBuf
860 *(HBITMAP *)pBuf = hbmp;
861 }
862
863 return TRUE;
864}
865
9e2896e5
VZ
866bool wxBitmapDataObject::SetData(const wxDataFormat& format,
867 size_t size, const void *pBuf)
d59ceba5
VZ
868{
869 HBITMAP hbmp;
870 if ( format.GetFormatId() == CF_DIB )
871 {
872 // here we get BITMAPINFO struct followed by the actual bitmap bits and
873 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
874 ScreenHDC hdc;
875
876 BITMAPINFO *pbmi = (BITMAPINFO *)pBuf;
877 BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
878 hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
879 pbmi + 1, pbmi, DIB_RGB_COLORS);
880 if ( !hbmp )
881 {
882 wxLogLastError("CreateDIBitmap");
883 }
884
885 m_bitmap.SetWidth(pbmih->biWidth);
886 m_bitmap.SetHeight(pbmih->biHeight);
887 }
888 else // CF_BITMAP
889 {
890 // it's easy with bitmaps: we pass them by handle
891 hbmp = *(HBITMAP *)pBuf;
892
893 BITMAP bmp;
894 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
895 {
896 wxLogLastError("GetObject(HBITMAP)");
897 }
898
899 m_bitmap.SetWidth(bmp.bmWidth);
900 m_bitmap.SetHeight(bmp.bmHeight);
901 m_bitmap.SetDepth(bmp.bmPlanes);
902 }
903
904 m_bitmap.SetHBITMAP((WXHBITMAP)hbmp);
905
906 wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") );
907
908 return TRUE;
8e193f38
VZ
909}
910
9e2896e5
VZ
911#endif // 0
912
913// ----------------------------------------------------------------------------
914// wxFileDataObject
915// ----------------------------------------------------------------------------
916
917bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData)
918{
919 m_filenames.Empty();
920
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
926 // how to get HDROP.
927 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
928
929 // get number of files (magic value -1)
930 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
931
51edda6a
VZ
932 wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") );
933
9e2896e5
VZ
934 // for each file get the length, allocate memory and then get the name
935 wxString str;
936 UINT len, n;
937 for ( n = 0; n < nFiles; n++ ) {
938 // +1 for terminating NUL
939 len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
940
941 UINT len2 = ::DragQueryFile(hdrop, n, str.GetWriteBuf(len), len);
942 str.UngetWriteBuf();
943 m_filenames.Add(str);
944
945 if ( len2 != len - 1 ) {
946 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned"
947 " %d characters, %d expected."), len2, len - 1);
948 }
949 }
950
951 return TRUE;
952}
953
8b85d24e
VZ
954void wxFileDataObject::AddFile(const wxString& file)
955{
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);
960}
961
962size_t wxFileDataObject::GetDataSize() const
963{
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
967
968 // if no filenames in list, size is 0
dd10a646
VZ
969 if ( m_filenames.GetCount() == 0 )
970 return 0;
8b85d24e
VZ
971
972 // inital size of DROPFILES struct + null byte
973 size_t sz = sizeof(DROPFILES) + 1;
974
dd10a646
VZ
975 size_t count = m_filenames.GetCount();
976 for ( size_t i = 0; i < count; i++ )
8b85d24e
VZ
977 {
978 // add filename length plus null byte
979 sz += m_filenames[i].Len() + 1;
980 }
dd10a646 981
8b85d24e
VZ
982 return sz;
983}
984
985bool wxFileDataObject::GetDataHere(void *pData) const
986{
987 // pData points to an externally allocated memory block
988 // created using the size returned by GetDataSize()
989
990 // if pData is NULL, or there are no files, return
dd10a646
VZ
991 if ( !pData || m_filenames.GetCount() == 0 )
992 return FALSE;
8b85d24e
VZ
993
994 // convert data pointer to a DROPFILES struct pointer
995 LPDROPFILES pDrop = (LPDROPFILES) pData;
996
997 // initialize DROPFILES struct
998 pDrop->pFiles = sizeof(DROPFILES);
dd10a646
VZ
999 pDrop->fNC = FALSE; // not non-client coords
1000#if wxUSE_UNICODE
1001 pDrop->fWide = TRUE;
1002#else // ANSI
8b85d24e 1003 pDrop->fWide = FALSE;
dd10a646 1004#endif // Unicode/Ansi
8b85d24e
VZ
1005
1006 // set start of filenames list (null separated)
51babd09 1007 wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES));
8b85d24e 1008
dd10a646
VZ
1009 size_t count = m_filenames.GetCount();
1010 for (size_t i = 0; i < count; i++ )
8b85d24e
VZ
1011 {
1012 // copy filename to pbuf and add null terminator
1013 size_t len = m_filenames[i].Len();
1014 memcpy(pbuf, m_filenames[i], len);
1015 pbuf += len;
dd10a646 1016 *pbuf++ = wxT('\0');
8b85d24e 1017 }
dd10a646
VZ
1018
1019 *pbuf = wxT('\0'); // add final null terminator
8b85d24e
VZ
1020
1021 return TRUE;
1022}
1023
269a5a34
VZ
1024// ----------------------------------------------------------------------------
1025// private functions
1026// ----------------------------------------------------------------------------
8e193f38 1027
8ee9d618
VZ
1028static size_t wxGetNumOfBitmapColors(size_t bitsPerPixel)
1029{
1030 switch ( bitsPerPixel )
1031 {
1032 case 1:
1033 // monochrome bitmap, 2 entries
1034 return 2;
1035
1036 case 4:
1037 return 16;
1038
1039 case 8:
1040 return 256;
1041
1042 case 24:
1043 // may be used with 24bit bitmaps, but we don't use it here - fall
1044 // through
1045
1046 case 16:
1047 case 32:
1048 // bmiColors not used at all with these bitmaps
1049 return 0;
1050
1051 default:
1052 wxFAIL_MSG( wxT("unknown bitmap format") );
1053 return 0;
1054 }
1055}
9e2896e5 1056
0d0512bd 1057size_t wxConvertBitmapToDIB(LPBITMAPINFO pbi, const wxBitmap& bitmap)
9e2896e5 1058{
8ee9d618
VZ
1059 wxASSERT_MSG( bitmap.Ok(), wxT("invalid bmp can't be converted to DIB") );
1060
9e2896e5
VZ
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") );
1064
8ee9d618
VZ
1065 // prepare all the info we need
1066 BITMAP bm;
9e2896e5 1067 HBITMAP hbmp = (HBITMAP)bitmap.GetHBITMAP();
8ee9d618
VZ
1068 if ( !GetObject(hbmp, sizeof(bm), &bm) )
1069 {
1070 wxLogLastError("GetObject(bitmap)");
1071
1072 return 0;
1073 }
1074
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);
9e2896e5 1079
8ee9d618 1080 BITMAPINFO bi2;
9e2896e5 1081
8ee9d618
VZ
1082 bool wantSizeOnly = pbi == NULL;
1083 if ( wantSizeOnly )
1084 pbi = &bi2;
1085
1086 // just for convenience
1087 BITMAPINFOHEADER& bi = pbi->bmiHeader;
1088
1089 bi.biSize = sizeof(BITMAPINFOHEADER);
1090 bi.biWidth = bm.bmWidth;
1091 bi.biHeight = bm.bmHeight;
1092 bi.biPlanes = 1;
1093 bi.biBitCount = biBits;
1094 bi.biCompression = BI_RGB;
1095 bi.biSizeImage = 0;
1096 bi.biXPelsPerMeter = 0;
1097 bi.biYPelsPerMeter = 0;
1098 bi.biClrUsed = 0;
1099 bi.biClrImportant = 0;
1100
1101 // memory we need for BITMAPINFO only
1102 DWORD dwLen = bi.biSize + biColors * sizeof(RGBQUAD);
1103
1104 // first get the image size
9e2896e5 1105 ScreenHDC hdc;
8ee9d618 1106 if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, NULL, pbi, DIB_RGB_COLORS) )
9e2896e5
VZ
1107 {
1108 wxLogLastError("GetDIBits(NULL)");
1109
1110 return 0;
1111 }
1112
8ee9d618 1113 if ( wantSizeOnly )
9e2896e5 1114 {
8ee9d618
VZ
1115 // size of the header + size of the image
1116 return dwLen + bi.biSizeImage;
9e2896e5
VZ
1117 }
1118
1119 // and now copy the bits
8ee9d618
VZ
1120 void *image = (char *)pbi + dwLen;
1121 if ( !GetDIBits(hdc, hbmp, 0, bi.biHeight, image, pbi, DIB_RGB_COLORS) )
9e2896e5
VZ
1122 {
1123 wxLogLastError("GetDIBits");
1124
1125 return 0;
1126 }
1127
8ee9d618 1128 return dwLen + bi.biSizeImage;
9e2896e5
VZ
1129}
1130
0d0512bd 1131wxBitmap wxConvertDIBToBitmap(const LPBITMAPINFO pbmi)
9e2896e5
VZ
1132{
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;
1136
8ee9d618
VZ
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;
1140
9e2896e5
VZ
1141 ScreenHDC hdc;
1142 HBITMAP hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
8ee9d618 1143 image, pbmi, DIB_RGB_COLORS);
9e2896e5
VZ
1144 if ( !hbmp )
1145 {
1146 wxLogLastError("CreateDIBitmap");
1147 }
1148
1149 wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
1150 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
1151
1152 return bitmap;
1153}
1154
8e193f38
VZ
1155#ifdef __WXDEBUG__
1156
d59ceba5
VZ
1157static const wxChar *GetTymedName(DWORD tymed)
1158{
1159 static wxChar s_szBuf[128];
1160 switch ( tymed ) {
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");
1168 default:
1169 wxSprintf(s_szBuf, wxT("type of media format %d (unknown)"), tymed);
1170 return s_szBuf;
1171 }
269a5a34 1172}
5260b1c5 1173
8e193f38 1174#endif // Debug
2845ddc2 1175
d59ceba5 1176#endif // not using OLE at all
5260b1c5 1177