]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/dataobj.cpp
Inital fill in background, removed tabs, -1->wxID_ANY, TRUE->true, FALSE->false
[wxWidgets.git] / src / msw / ole / dataobj.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: msw/ole/dataobj.cpp
3 // Purpose: implementation of wx[I]DataObject class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 10.05.98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "dataobj.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #if defined(__BORLANDC__)
28 #pragma hdrstop
29 #endif
30
31 #ifndef WX_PRECOMP
32 #include "wx/intl.h"
33 #include "wx/log.h"
34 #endif
35
36 #include "wx/dataobj.h"
37
38 #if wxUSE_OLE && defined(__WIN32__) && !defined(__GNUWIN32_OLD__)
39
40 #include "wx/msw/private.h" // includes <windows.h>
41
42 #ifdef __WXWINCE__
43 #include <winreg.h>
44 #endif
45
46 // for some compilers, the entire ole2.h must be included, not only oleauto.h
47 #if wxUSE_NORLANDER_HEADERS || defined(__WATCOMC__) || defined(__WXWINCE__)
48 #include <ole2.h>
49 #endif
50
51 #include <oleauto.h>
52 #include <shlobj.h>
53
54 #include "wx/msw/ole/oleutils.h"
55
56 #include "wx/msw/dib.h"
57
58 #ifndef CFSTR_SHELLURL
59 #define CFSTR_SHELLURL _T("UniformResourceLocator")
60 #endif
61
62 // ----------------------------------------------------------------------------
63 // functions
64 // ----------------------------------------------------------------------------
65
66 #ifdef __WXDEBUG__
67 static const wxChar *GetTymedName(DWORD tymed);
68 #else // !Debug
69 #define GetTymedName(tymed) wxEmptyString
70 #endif // Debug/!Debug
71
72 // ----------------------------------------------------------------------------
73 // wxIEnumFORMATETC interface implementation
74 // ----------------------------------------------------------------------------
75
76 class wxIEnumFORMATETC : public IEnumFORMATETC
77 {
78 public:
79 wxIEnumFORMATETC(const wxDataFormat* formats, ULONG nCount);
80 virtual ~wxIEnumFORMATETC() { delete [] m_formats; }
81
82 DECLARE_IUNKNOWN_METHODS;
83
84 // IEnumFORMATETC
85 STDMETHODIMP Next(ULONG celt, FORMATETC *rgelt, ULONG *pceltFetched);
86 STDMETHODIMP Skip(ULONG celt);
87 STDMETHODIMP Reset();
88 STDMETHODIMP Clone(IEnumFORMATETC **ppenum);
89
90 private:
91 CLIPFORMAT *m_formats; // formats we can provide data in
92 ULONG m_nCount, // number of formats we support
93 m_nCurrent; // current enum position
94
95 DECLARE_NO_COPY_CLASS(wxIEnumFORMATETC)
96 };
97
98 // ----------------------------------------------------------------------------
99 // wxIDataObject implementation of IDataObject interface
100 // ----------------------------------------------------------------------------
101
102 class wxIDataObject : public IDataObject
103 {
104 public:
105 wxIDataObject(wxDataObject *pDataObject);
106 virtual ~wxIDataObject();
107
108 // normally, wxDataObject controls our lifetime (i.e. we're deleted when it
109 // is), but in some cases, the situation is inversed, that is we delete it
110 // when this object is deleted - setting this flag enables such logic
111 void SetDeleteFlag() { m_mustDelete = TRUE; }
112
113 DECLARE_IUNKNOWN_METHODS;
114
115 // IDataObject
116 STDMETHODIMP GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium);
117 STDMETHODIMP GetDataHere(FORMATETC *pformatetc, STGMEDIUM *pmedium);
118 STDMETHODIMP QueryGetData(FORMATETC *pformatetc);
119 STDMETHODIMP GetCanonicalFormatEtc(FORMATETC *In, FORMATETC *pOut);
120 STDMETHODIMP SetData(FORMATETC *pfetc, STGMEDIUM *pmedium, BOOL fRelease);
121 STDMETHODIMP EnumFormatEtc(DWORD dwDirection, IEnumFORMATETC **ppenumFEtc);
122 STDMETHODIMP DAdvise(FORMATETC *pfetc, DWORD ad, IAdviseSink *p, DWORD *pdw);
123 STDMETHODIMP DUnadvise(DWORD dwConnection);
124 STDMETHODIMP EnumDAdvise(IEnumSTATDATA **ppenumAdvise);
125
126 private:
127 wxDataObject *m_pDataObject; // pointer to C++ class we belong to
128
129 bool m_mustDelete;
130
131 DECLARE_NO_COPY_CLASS(wxIDataObject)
132 };
133
134 // ============================================================================
135 // implementation
136 // ============================================================================
137
138 // ----------------------------------------------------------------------------
139 // wxDataFormat
140 // ----------------------------------------------------------------------------
141
142 void wxDataFormat::SetId(const wxChar *format)
143 {
144 m_format = (wxDataFormat::NativeFormat)::RegisterClipboardFormat(format);
145 if ( !m_format )
146 {
147 wxLogError(_("Couldn't register clipboard format '%s'."), format);
148 }
149 }
150
151 wxString wxDataFormat::GetId() const
152 {
153 static const int max = 256;
154
155 wxString s;
156
157 wxCHECK_MSG( !IsStandard(), s,
158 wxT("name of predefined format cannot be retrieved") );
159
160 int len = ::GetClipboardFormatName(m_format, wxStringBuffer(s, max), max);
161
162 if ( !len )
163 {
164 wxLogError(_("The clipboard format '%d' doesn't exist."), m_format);
165 }
166
167 return s;
168 }
169
170 // ----------------------------------------------------------------------------
171 // wxIEnumFORMATETC
172 // ----------------------------------------------------------------------------
173
174 BEGIN_IID_TABLE(wxIEnumFORMATETC)
175 ADD_IID(Unknown)
176 ADD_IID(EnumFORMATETC)
177 END_IID_TABLE;
178
179 IMPLEMENT_IUNKNOWN_METHODS(wxIEnumFORMATETC)
180
181 wxIEnumFORMATETC::wxIEnumFORMATETC(const wxDataFormat *formats, ULONG nCount)
182 {
183 m_nCurrent = 0;
184 m_nCount = nCount;
185 m_formats = new CLIPFORMAT[nCount];
186 for ( ULONG n = 0; n < nCount; n++ ) {
187 m_formats[n] = formats[n].GetFormatId();
188 }
189 }
190
191 STDMETHODIMP wxIEnumFORMATETC::Next(ULONG celt,
192 FORMATETC *rgelt,
193 ULONG *pceltFetched)
194 {
195 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Next"));
196
197 ULONG numFetched = 0;
198 while (m_nCurrent < m_nCount && numFetched < celt) {
199 FORMATETC format;
200 format.cfFormat = m_formats[m_nCurrent++];
201 format.ptd = NULL;
202 format.dwAspect = DVASPECT_CONTENT;
203 format.lindex = -1;
204 format.tymed = TYMED_HGLOBAL;
205
206 *rgelt++ = format;
207 numFetched++;
208 }
209
210 if (pceltFetched)
211 *pceltFetched = numFetched;
212
213 return numFetched == celt ? S_OK : S_FALSE;
214 }
215
216 STDMETHODIMP wxIEnumFORMATETC::Skip(ULONG celt)
217 {
218 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Skip"));
219
220 m_nCurrent += celt;
221 if ( m_nCurrent < m_nCount )
222 return S_OK;
223
224 // no, can't skip this many elements
225 m_nCurrent -= celt;
226
227 return S_FALSE;
228 }
229
230 STDMETHODIMP wxIEnumFORMATETC::Reset()
231 {
232 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Reset"));
233
234 m_nCurrent = 0;
235
236 return S_OK;
237 }
238
239 STDMETHODIMP wxIEnumFORMATETC::Clone(IEnumFORMATETC **ppenum)
240 {
241 wxLogTrace(wxTRACE_OleCalls, wxT("wxIEnumFORMATETC::Clone"));
242
243 // unfortunately, we can't reuse the code in ctor - types are different
244 wxIEnumFORMATETC *pNew = new wxIEnumFORMATETC(NULL, 0);
245 pNew->m_nCount = m_nCount;
246 pNew->m_formats = new CLIPFORMAT[m_nCount];
247 for ( ULONG n = 0; n < m_nCount; n++ ) {
248 pNew->m_formats[n] = m_formats[n];
249 }
250 pNew->AddRef();
251 *ppenum = pNew;
252
253 return S_OK;
254 }
255
256 // ----------------------------------------------------------------------------
257 // wxIDataObject
258 // ----------------------------------------------------------------------------
259
260 BEGIN_IID_TABLE(wxIDataObject)
261 ADD_IID(Unknown)
262 ADD_IID(DataObject)
263 END_IID_TABLE;
264
265 IMPLEMENT_IUNKNOWN_METHODS(wxIDataObject)
266
267 wxIDataObject::wxIDataObject(wxDataObject *pDataObject)
268 {
269 m_pDataObject = pDataObject;
270 m_mustDelete = FALSE;
271 }
272
273 wxIDataObject::~wxIDataObject()
274 {
275 if ( m_mustDelete )
276 {
277 delete m_pDataObject;
278 }
279 }
280
281 // get data functions
282 STDMETHODIMP wxIDataObject::GetData(FORMATETC *pformatetcIn, STGMEDIUM *pmedium)
283 {
284 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetData"));
285
286 // is data is in our format?
287 HRESULT hr = QueryGetData(pformatetcIn);
288 if ( FAILED(hr) )
289 return hr;
290
291 // for the bitmaps and metafiles we use the handles instead of global memory
292 // to pass the data
293 wxDataFormat format = (wxDataFormat::NativeFormat)pformatetcIn->cfFormat;
294
295 switch ( format )
296 {
297 case wxDF_BITMAP:
298 pmedium->tymed = TYMED_GDI;
299 break;
300
301 case wxDF_ENHMETAFILE:
302 pmedium->tymed = TYMED_ENHMF;
303 break;
304
305 #ifndef __WXWINCE__
306 case wxDF_METAFILE:
307 pmedium->hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE,
308 sizeof(METAFILEPICT));
309 if ( !pmedium->hGlobal ) {
310 wxLogLastError(wxT("GlobalAlloc"));
311 return E_OUTOFMEMORY;
312 }
313 pmedium->tymed = TYMED_MFPICT;
314 break;
315 #endif
316 default:
317 // alloc memory
318 size_t size = m_pDataObject->GetDataSize(format);
319 if ( !size ) {
320 // it probably means that the method is just not implemented
321 wxLogDebug(wxT("Invalid data size - can't be 0"));
322
323 return DV_E_FORMATETC;
324 }
325
326 if ( !format.IsStandard() ) {
327 // for custom formats, put the size with the data - alloc the
328 // space for it
329 // MB: not completely sure this is correct,
330 // even if I can't figure out what's wrong
331 size += m_pDataObject->GetBufferOffset( format );
332 }
333
334 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_SHARE, size);
335 if ( hGlobal == NULL ) {
336 wxLogLastError(wxT("GlobalAlloc"));
337 return E_OUTOFMEMORY;
338 }
339
340 // copy data
341 pmedium->tymed = TYMED_HGLOBAL;
342 pmedium->hGlobal = hGlobal;
343 }
344
345 pmedium->pUnkForRelease = NULL;
346
347 // do copy the data
348 hr = GetDataHere(pformatetcIn, pmedium);
349 if ( FAILED(hr) ) {
350 // free resources we allocated
351 if ( pmedium->tymed & (TYMED_HGLOBAL | TYMED_MFPICT) ) {
352 GlobalFree(pmedium->hGlobal);
353 }
354
355 return hr;
356 }
357
358 return S_OK;
359 }
360
361 STDMETHODIMP wxIDataObject::GetDataHere(FORMATETC *pformatetc,
362 STGMEDIUM *pmedium)
363 {
364 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetDataHere"));
365
366 // put data in caller provided medium
367 switch ( pmedium->tymed )
368 {
369 case TYMED_GDI:
370 if ( !m_pDataObject->GetDataHere(wxDF_BITMAP, &pmedium->hBitmap) )
371 return E_UNEXPECTED;
372 break;
373
374 case TYMED_ENHMF:
375 if ( !m_pDataObject->GetDataHere(wxDF_ENHMETAFILE,
376 &pmedium->hEnhMetaFile) )
377 return E_UNEXPECTED;
378 break;
379
380 case TYMED_MFPICT:
381 // fall through - we pass METAFILEPICT through HGLOBAL
382
383 case TYMED_HGLOBAL:
384 {
385 // copy data
386 HGLOBAL hGlobal = pmedium->hGlobal;
387 void *pBuf = GlobalLock(hGlobal);
388 if ( pBuf == NULL ) {
389 wxLogLastError(wxT("GlobalLock"));
390 return E_OUTOFMEMORY;
391 }
392
393 wxDataFormat format = pformatetc->cfFormat;
394 if ( !format.IsStandard() ) {
395 // for custom formats, put the size with the data
396 pBuf = m_pDataObject->SetSizeInBuffer( pBuf, GlobalSize(hGlobal), format );
397 }
398
399 if ( !m_pDataObject->GetDataHere(format, pBuf) )
400 return E_UNEXPECTED;
401
402 GlobalUnlock(hGlobal);
403 }
404 break;
405
406 default:
407 return DV_E_TYMED;
408 }
409
410 return S_OK;
411 }
412
413
414 // set data functions
415 STDMETHODIMP wxIDataObject::SetData(FORMATETC *pformatetc,
416 STGMEDIUM *pmedium,
417 BOOL fRelease)
418 {
419 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::SetData"));
420
421 switch ( pmedium->tymed )
422 {
423 case TYMED_GDI:
424 m_pDataObject->SetData(wxDF_BITMAP, 0, &pmedium->hBitmap);
425 break;
426
427 case TYMED_ENHMF:
428 m_pDataObject->SetData(wxDF_ENHMETAFILE, 0, &pmedium->hEnhMetaFile);
429 break;
430
431 case TYMED_MFPICT:
432 // fall through - we pass METAFILEPICT through HGLOBAL
433 case TYMED_HGLOBAL:
434 {
435 wxDataFormat format = pformatetc->cfFormat;
436
437 // this is quite weird, but for file drag and drop, explorer
438 // calls our SetData() with the formats we do *not* support!
439 //
440 // as we can't fix this bug in explorer (it's a bug because it
441 // should only use formats returned by EnumFormatEtc), do the
442 // check here
443 if ( !m_pDataObject->IsSupported(format, wxDataObject::Set) ) {
444 // go away!
445 return DV_E_FORMATETC;
446 }
447
448 // copy data
449 const void *pBuf = GlobalLock(pmedium->hGlobal);
450 if ( pBuf == NULL ) {
451 wxLogLastError(wxT("GlobalLock"));
452
453 return E_OUTOFMEMORY;
454 }
455
456 // we've got a problem with SetData() here because the base
457 // class version requires the size parameter which we don't
458 // have anywhere in OLE data transfer - so we need to
459 // synthetise it for known formats and we suppose that all data
460 // in custom formats starts with a DWORD containing the size
461 size_t size;
462 switch ( format )
463 {
464 case CF_TEXT:
465 case CF_OEMTEXT:
466 size = strlen((const char *)pBuf);
467 break;
468 #if !defined(__WATCOMC__) && ! (defined(__BORLANDC__) && (__BORLANDC__ < 0x500))
469 case CF_UNICODETEXT:
470 #if ( defined(__BORLANDC__) && (__BORLANDC__ > 0x530) ) \
471 || ( defined(__MWERKS__) && defined(__WXMSW__) )
472 size = std::wcslen((const wchar_t *)pBuf) * sizeof(wchar_t);
473 #else
474 size = wxWcslen((const wchar_t *)pBuf) * sizeof(wchar_t);
475 #endif
476 break;
477 #endif
478 case CF_BITMAP:
479 #ifndef __WXWINCE__
480 case CF_HDROP:
481 // these formats don't use size at all, anyhow (but
482 // pass data by handle, which is always a single DWORD)
483 size = 0;
484 break;
485 #endif
486
487 case CF_DIB:
488 // the handler will calculate size itself (it's too
489 // complicated to do it here)
490 size = 0;
491 break;
492
493 #ifndef __WXWINCE__
494 case CF_METAFILEPICT:
495 size = sizeof(METAFILEPICT);
496 break;
497 #endif
498 default:
499 {
500 // we suppose that the size precedes the data
501 pBuf = m_pDataObject->GetSizeFromBuffer( pBuf, &size, format );
502 if (! format.IsStandard() ) {
503 // see GetData for coresponding increment
504 size -= m_pDataObject->GetBufferOffset( format );
505 }
506 }
507 }
508
509 bool ok = m_pDataObject->SetData(format, size, pBuf);
510
511 GlobalUnlock(pmedium->hGlobal);
512
513 if ( !ok ) {
514 return E_UNEXPECTED;
515 }
516 }
517 break;
518
519 default:
520 return DV_E_TYMED;
521 }
522
523 if ( fRelease ) {
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 )
527 {
528 case TYMED_GDI:
529 pmedium->hBitmap = 0;
530 break;
531
532 case TYMED_MFPICT:
533 pmedium->hMetaFilePict = 0;
534 break;
535
536 case TYMED_ENHMF:
537 pmedium->hEnhMetaFile = 0;
538 break;
539 }
540
541 ReleaseStgMedium(pmedium);
542 }
543
544 return S_OK;
545 }
546
547 // information functions
548 STDMETHODIMP wxIDataObject::QueryGetData(FORMATETC *pformatetc)
549 {
550 // do we accept data in this format?
551 if ( pformatetc == NULL ) {
552 wxLogTrace(wxTRACE_OleCalls,
553 wxT("wxIDataObject::QueryGetData: invalid ptr."));
554
555 return E_INVALIDARG;
556 }
557
558 // the only one allowed by current COM implementation
559 if ( pformatetc->lindex != -1 ) {
560 wxLogTrace(wxTRACE_OleCalls,
561 wxT("wxIDataObject::QueryGetData: bad lindex %ld"),
562 pformatetc->lindex);
563
564 return DV_E_LINDEX;
565 }
566
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 %ld"),
571 pformatetc->dwAspect);
572
573 return DV_E_DVASPECT;
574 }
575
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));
581 }
582 else {
583 wxLogTrace(wxTRACE_OleCalls,
584 wxT("wxIDataObject::QueryGetData: %s unsupported"),
585 wxGetFormatName(format));
586
587 return DV_E_FORMATETC;
588 }
589
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"),
597 GetTymedName(tymed),
598 GetTymedName(format == wxDF_BITMAP ? TYMED_GDI
599 : TYMED_HGLOBAL));
600
601 return DV_E_TYMED;
602 }
603
604 return S_OK;
605 }
606
607 STDMETHODIMP wxIDataObject::GetCanonicalFormatEtc(FORMATETC *WXUNUSED(pFormatetcIn),
608 FORMATETC *pFormatetcOut)
609 {
610 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::GetCanonicalFormatEtc"));
611
612 // TODO we might want something better than this trivial implementation here
613 if ( pFormatetcOut != NULL )
614 pFormatetcOut->ptd = NULL;
615
616 return DATA_S_SAMEFORMATETC;
617 }
618
619 STDMETHODIMP wxIDataObject::EnumFormatEtc(DWORD dwDir,
620 IEnumFORMATETC **ppenumFormatEtc)
621 {
622 wxLogTrace(wxTRACE_OleCalls, wxT("wxIDataObject::EnumFormatEtc"));
623
624 wxDataObject::Direction dir = dwDir == DATADIR_GET ? wxDataObject::Get
625 : wxDataObject::Set;
626
627 size_t nFormatCount = m_pDataObject->GetFormatCount(dir);
628 wxDataFormat format;
629 wxDataFormat *formats;
630 formats = nFormatCount == 1 ? &format : new wxDataFormat[nFormatCount];
631 m_pDataObject->GetAllFormats(formats, dir);
632
633 wxIEnumFORMATETC *pEnum = new wxIEnumFORMATETC(formats, nFormatCount);
634 pEnum->AddRef();
635 *ppenumFormatEtc = pEnum;
636
637 if ( formats != &format ) {
638 delete [] formats;
639 }
640
641 return S_OK;
642 }
643
644 // ----------------------------------------------------------------------------
645 // advise sink functions (not implemented)
646 // ----------------------------------------------------------------------------
647
648 STDMETHODIMP wxIDataObject::DAdvise(FORMATETC *WXUNUSED(pformatetc),
649 DWORD WXUNUSED(advf),
650 IAdviseSink *WXUNUSED(pAdvSink),
651 DWORD *WXUNUSED(pdwConnection))
652 {
653 return OLE_E_ADVISENOTSUPPORTED;
654 }
655
656 STDMETHODIMP wxIDataObject::DUnadvise(DWORD WXUNUSED(dwConnection))
657 {
658 return OLE_E_ADVISENOTSUPPORTED;
659 }
660
661 STDMETHODIMP wxIDataObject::EnumDAdvise(IEnumSTATDATA **WXUNUSED(ppenumAdvise))
662 {
663 return OLE_E_ADVISENOTSUPPORTED;
664 }
665
666 // ----------------------------------------------------------------------------
667 // wxDataObject
668 // ----------------------------------------------------------------------------
669
670 wxDataObject::wxDataObject()
671 {
672 m_pIDataObject = new wxIDataObject(this);
673 m_pIDataObject->AddRef();
674 }
675
676 wxDataObject::~wxDataObject()
677 {
678 ReleaseInterface(m_pIDataObject);
679 }
680
681 void wxDataObject::SetAutoDelete()
682 {
683 ((wxIDataObject *)m_pIDataObject)->SetDeleteFlag();
684 m_pIDataObject->Release();
685
686 // so that the dtor doesnt' crash
687 m_pIDataObject = NULL;
688 }
689
690 size_t wxDataObject::GetBufferOffset( const wxDataFormat& WXUNUSED(format) )
691 {
692 return sizeof(size_t);
693 }
694
695 const void* wxDataObject::GetSizeFromBuffer( const void* buffer, size_t* size,
696 const wxDataFormat& WXUNUSED(format) )
697 {
698 size_t* p = (size_t*)buffer;
699 *size = *p;
700
701 return p + 1;
702 }
703
704 void* wxDataObject::SetSizeInBuffer( void* buffer, size_t size,
705 const wxDataFormat& WXUNUSED(format) )
706 {
707 size_t* p = (size_t*)buffer;
708 *p = size;
709
710 return p + 1;
711 }
712
713 #ifdef __WXDEBUG__
714
715 const wxChar *wxDataObject::GetFormatName(wxDataFormat format)
716 {
717 // case 'xxx' is not a valid value for switch of enum 'wxDataFormat'
718 #ifdef __VISUALC__
719 #pragma warning(disable:4063)
720 #endif // VC++
721
722 static wxChar s_szBuf[256];
723 switch ( format ) {
724 case CF_TEXT: return wxT("CF_TEXT");
725 case CF_BITMAP: return wxT("CF_BITMAP");
726 case CF_SYLK: return wxT("CF_SYLK");
727 case CF_DIF: return wxT("CF_DIF");
728 case CF_TIFF: return wxT("CF_TIFF");
729 case CF_OEMTEXT: return wxT("CF_OEMTEXT");
730 case CF_DIB: return wxT("CF_DIB");
731 case CF_PALETTE: return wxT("CF_PALETTE");
732 case CF_PENDATA: return wxT("CF_PENDATA");
733 case CF_RIFF: return wxT("CF_RIFF");
734 case CF_WAVE: return wxT("CF_WAVE");
735 case CF_UNICODETEXT: return wxT("CF_UNICODETEXT");
736 #ifndef __WXWINCE__
737 case CF_METAFILEPICT: return wxT("CF_METAFILEPICT");
738 case CF_ENHMETAFILE: return wxT("CF_ENHMETAFILE");
739 case CF_LOCALE: return wxT("CF_LOCALE");
740 case CF_HDROP: return wxT("CF_HDROP");
741 #endif
742
743 default:
744 if ( !::GetClipboardFormatName(format, s_szBuf, WXSIZEOF(s_szBuf)) )
745 {
746 // it must be a new predefined format we don't know the name of
747 wxSprintf(s_szBuf, wxT("unknown CF (0x%04x)"), format.GetFormatId());
748 }
749
750 return s_szBuf;
751 }
752
753 #ifdef __VISUALC__
754 #pragma warning(default:4063)
755 #endif // VC++
756 }
757
758 #endif // Debug
759
760 // ----------------------------------------------------------------------------
761 // wxBitmapDataObject supports CF_DIB format
762 // ----------------------------------------------------------------------------
763
764 // TODO: support CF_DIB under Windows CE as well
765
766 size_t wxBitmapDataObject::GetDataSize() const
767 {
768 #if wxUSE_WXDIB && !defined(__WXWINCE__)
769 return wxDIB::ConvertFromBitmap(NULL, GetHbitmapOf(GetBitmap()));
770 #else
771 return 0;
772 #endif
773 }
774
775 bool wxBitmapDataObject::GetDataHere(void *buf) const
776 {
777 #if wxUSE_WXDIB && !defined(__WXWINCE__)
778 BITMAPINFO * const pbi = (BITMAPINFO *)buf;
779
780 return wxDIB::ConvertFromBitmap(pbi, GetHbitmapOf(GetBitmap())) != 0;
781 #else
782 return FALSE;
783 #endif
784 }
785
786 bool wxBitmapDataObject::SetData(size_t WXUNUSED(len), const void *buf)
787 {
788 #if wxUSE_WXDIB && !defined(__WXWINCE__)
789 const BITMAPINFO * const pbmi = (const BITMAPINFO *)buf;
790
791 HBITMAP hbmp = wxDIB::ConvertToBitmap(pbmi);
792
793 wxCHECK_MSG( hbmp, FALSE, wxT("pasting/dropping invalid bitmap") );
794
795 const BITMAPINFOHEADER * const pbmih = &pbmi->bmiHeader;
796 wxBitmap bitmap(pbmih->biWidth, pbmih->biHeight, pbmih->biBitCount);
797 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
798
799 // TODO: create wxPalette if the bitmap has any
800
801 SetBitmap(bitmap);
802
803 return TRUE;
804 #else
805 return FALSE;
806 #endif
807 }
808
809 // ----------------------------------------------------------------------------
810 // wxBitmapDataObject2 supports CF_BITMAP format
811 // ----------------------------------------------------------------------------
812
813 // the bitmaps aren't passed by value as other types of data (i.e. by copying
814 // the data into a global memory chunk and passing it to the clipboard or
815 // another application or whatever), but by handle, so these generic functions
816 // don't make much sense to them.
817
818 size_t wxBitmapDataObject2::GetDataSize() const
819 {
820 return 0;
821 }
822
823 bool wxBitmapDataObject2::GetDataHere(void *pBuf) const
824 {
825 // we put a bitmap handle into pBuf
826 *(WXHBITMAP *)pBuf = GetBitmap().GetHBITMAP();
827
828 return TRUE;
829 }
830
831 bool wxBitmapDataObject2::SetData(size_t WXUNUSED(len), const void *pBuf)
832 {
833 HBITMAP hbmp = *(HBITMAP *)pBuf;
834
835 BITMAP bmp;
836 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
837 {
838 wxLogLastError(wxT("GetObject(HBITMAP)"));
839 }
840
841 wxBitmap bitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes);
842 bitmap.SetHBITMAP((WXHBITMAP)hbmp);
843
844 if ( !bitmap.Ok() ) {
845 wxFAIL_MSG(wxT("pasting/dropping invalid bitmap"));
846
847 return FALSE;
848 }
849
850 SetBitmap(bitmap);
851
852 return TRUE;
853 }
854
855 #if 0
856
857 size_t wxBitmapDataObject::GetDataSize(const wxDataFormat& format) const
858 {
859 if ( format.GetFormatId() == CF_DIB )
860 {
861 // create the DIB
862 ScreenHDC hdc;
863
864 // shouldn't be selected into a DC or GetDIBits() would fail
865 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
866 wxT("can't copy bitmap selected into wxMemoryDC") );
867
868 // first get the info
869 BITMAPINFO bi;
870 if ( !GetDIBits(hdc, (HBITMAP)m_bitmap.GetHBITMAP(), 0, 0,
871 NULL, &bi, DIB_RGB_COLORS) )
872 {
873 wxLogLastError(wxT("GetDIBits(NULL)"));
874
875 return 0;
876 }
877
878 return sizeof(BITMAPINFO) + bi.bmiHeader.biSizeImage;
879 }
880 else // CF_BITMAP
881 {
882 // no data to copy - we don't pass HBITMAP via global memory
883 return 0;
884 }
885 }
886
887 bool wxBitmapDataObject::GetDataHere(const wxDataFormat& format,
888 void *pBuf) const
889 {
890 wxASSERT_MSG( m_bitmap.Ok(), wxT("copying invalid bitmap") );
891
892 HBITMAP hbmp = (HBITMAP)m_bitmap.GetHBITMAP();
893 if ( format.GetFormatId() == CF_DIB )
894 {
895 // create the DIB
896 ScreenHDC hdc;
897
898 // shouldn't be selected into a DC or GetDIBits() would fail
899 wxASSERT_MSG( !m_bitmap.GetSelectedInto(),
900 wxT("can't copy bitmap selected into wxMemoryDC") );
901
902 // first get the info
903 BITMAPINFO *pbi = (BITMAPINFO *)pBuf;
904 if ( !GetDIBits(hdc, hbmp, 0, 0, NULL, pbi, DIB_RGB_COLORS) )
905 {
906 wxLogLastError(wxT("GetDIBits(NULL)"));
907
908 return 0;
909 }
910
911 // and now copy the bits
912 if ( !GetDIBits(hdc, hbmp, 0, pbi->bmiHeader.biHeight, pbi + 1,
913 pbi, DIB_RGB_COLORS) )
914 {
915 wxLogLastError(wxT("GetDIBits"));
916
917 return FALSE;
918 }
919 }
920 else // CF_BITMAP
921 {
922 // we put a bitmap handle into pBuf
923 *(HBITMAP *)pBuf = hbmp;
924 }
925
926 return TRUE;
927 }
928
929 bool wxBitmapDataObject::SetData(const wxDataFormat& format,
930 size_t size, const void *pBuf)
931 {
932 HBITMAP hbmp;
933 if ( format.GetFormatId() == CF_DIB )
934 {
935 // here we get BITMAPINFO struct followed by the actual bitmap bits and
936 // BITMAPINFO starts with BITMAPINFOHEADER followed by colour info
937 ScreenHDC hdc;
938
939 BITMAPINFO *pbmi = (BITMAPINFO *)pBuf;
940 BITMAPINFOHEADER *pbmih = &pbmi->bmiHeader;
941 hbmp = CreateDIBitmap(hdc, pbmih, CBM_INIT,
942 pbmi + 1, pbmi, DIB_RGB_COLORS);
943 if ( !hbmp )
944 {
945 wxLogLastError(wxT("CreateDIBitmap"));
946 }
947
948 m_bitmap.SetWidth(pbmih->biWidth);
949 m_bitmap.SetHeight(pbmih->biHeight);
950 }
951 else // CF_BITMAP
952 {
953 // it's easy with bitmaps: we pass them by handle
954 hbmp = *(HBITMAP *)pBuf;
955
956 BITMAP bmp;
957 if ( !GetObject(hbmp, sizeof(BITMAP), &bmp) )
958 {
959 wxLogLastError(wxT("GetObject(HBITMAP)"));
960 }
961
962 m_bitmap.SetWidth(bmp.bmWidth);
963 m_bitmap.SetHeight(bmp.bmHeight);
964 m_bitmap.SetDepth(bmp.bmPlanes);
965 }
966
967 m_bitmap.SetHBITMAP((WXHBITMAP)hbmp);
968
969 wxASSERT_MSG( m_bitmap.Ok(), wxT("pasting invalid bitmap") );
970
971 return TRUE;
972 }
973
974 #endif // 0
975
976 // ----------------------------------------------------------------------------
977 // wxFileDataObject
978 // ----------------------------------------------------------------------------
979
980 bool wxFileDataObject::SetData(size_t WXUNUSED(size), const void *pData)
981 {
982 #ifndef __WXWINCE__
983 m_filenames.Empty();
984
985 // the documentation states that the first member of DROPFILES structure is
986 // a "DWORD offset of double NUL terminated file list". What they mean by
987 // this (I wonder if you see it immediately) is that the list starts at
988 // ((char *)&(pDropFiles.pFiles)) + pDropFiles.pFiles. We're also advised
989 // to use DragQueryFile to work with this structure, but not told where and
990 // how to get HDROP.
991 HDROP hdrop = (HDROP)pData; // NB: it works, but I'm not sure about it
992
993 // get number of files (magic value -1)
994 UINT nFiles = ::DragQueryFile(hdrop, (unsigned)-1, NULL, 0u);
995
996 wxCHECK_MSG ( nFiles != (UINT)-1, FALSE, wxT("wrong HDROP handle") );
997
998 // for each file get the length, allocate memory and then get the name
999 wxString str;
1000 UINT len, n;
1001 for ( n = 0; n < nFiles; n++ ) {
1002 // +1 for terminating NUL
1003 len = ::DragQueryFile(hdrop, n, NULL, 0) + 1;
1004
1005 UINT len2 = ::DragQueryFile(hdrop, n, wxStringBuffer(str, len), len);
1006 m_filenames.Add(str);
1007
1008 if ( len2 != len - 1 ) {
1009 wxLogDebug(wxT("In wxFileDropTarget::OnDrop DragQueryFile returned\
1010 %d characters, %d expected."), len2, len - 1);
1011 }
1012 }
1013
1014 return TRUE;
1015 #else
1016 return FALSE;
1017 #endif
1018 }
1019
1020 void wxFileDataObject::AddFile(const wxString& file)
1021 {
1022 // just add file to filenames array
1023 // all useful data (such as DROPFILES struct) will be
1024 // created later as necessary
1025 m_filenames.Add(file);
1026 }
1027
1028 size_t wxFileDataObject::GetDataSize() const
1029 {
1030 #ifndef __WXWINCE__
1031 // size returned will be the size of the DROPFILES structure,
1032 // plus the list of filesnames (null byte separated), plus
1033 // a double null at the end
1034
1035 // if no filenames in list, size is 0
1036 if ( m_filenames.GetCount() == 0 )
1037 return 0;
1038
1039 // inital size of DROPFILES struct + null byte
1040 size_t sz = sizeof(DROPFILES) + 1;
1041
1042 size_t count = m_filenames.GetCount();
1043 for ( size_t i = 0; i < count; i++ )
1044 {
1045 // add filename length plus null byte
1046 sz += m_filenames[i].Len() + 1;
1047 }
1048
1049 return sz;
1050 #else
1051 return 0;
1052 #endif
1053 }
1054
1055 bool wxFileDataObject::GetDataHere(void *pData) const
1056 {
1057 #ifndef __WXWINCE__
1058 // pData points to an externally allocated memory block
1059 // created using the size returned by GetDataSize()
1060
1061 // if pData is NULL, or there are no files, return
1062 if ( !pData || m_filenames.GetCount() == 0 )
1063 return FALSE;
1064
1065 // convert data pointer to a DROPFILES struct pointer
1066 LPDROPFILES pDrop = (LPDROPFILES) pData;
1067
1068 // initialize DROPFILES struct
1069 pDrop->pFiles = sizeof(DROPFILES);
1070 pDrop->fNC = FALSE; // not non-client coords
1071 #if wxUSE_UNICODE
1072 pDrop->fWide = TRUE;
1073 #else // ANSI
1074 pDrop->fWide = FALSE;
1075 #endif // Unicode/Ansi
1076
1077 // set start of filenames list (null separated)
1078 wxChar *pbuf = (wxChar*) ((BYTE *)pDrop + sizeof(DROPFILES));
1079
1080 size_t count = m_filenames.GetCount();
1081 for (size_t i = 0; i < count; i++ )
1082 {
1083 // copy filename to pbuf and add null terminator
1084 size_t len = m_filenames[i].Len();
1085 memcpy(pbuf, m_filenames[i], len*sizeof(wxChar));
1086 pbuf += len;
1087 *pbuf++ = wxT('\0');
1088 }
1089
1090 // add final null terminator
1091 *pbuf = wxT('\0');
1092
1093 return TRUE;
1094 #else
1095 return FALSE;
1096 #endif
1097 }
1098
1099 // ----------------------------------------------------------------------------
1100 // wxURLDataObject
1101 // ----------------------------------------------------------------------------
1102
1103 class CFSTR_SHELLURLDataObject : public wxCustomDataObject
1104 {
1105 public:
1106 CFSTR_SHELLURLDataObject() : wxCustomDataObject(CFSTR_SHELLURL) {}
1107 protected:
1108 virtual size_t GetBufferOffset( const wxDataFormat& WXUNUSED(format) )
1109 {
1110 return 0;
1111 }
1112
1113 virtual const void* GetSizeFromBuffer( const void* buffer, size_t* size,
1114 const wxDataFormat& WXUNUSED(format) )
1115 {
1116 // CFSTR_SHELLURL is _always_ ANSI text
1117 *size = strlen( (const char*)buffer );
1118
1119 return buffer;
1120 }
1121
1122 virtual void* SetSizeInBuffer( void* buffer, size_t WXUNUSED(size),
1123 const wxDataFormat& WXUNUSED(format) )
1124 {
1125 return buffer;
1126 }
1127
1128 #if wxUSE_UNICODE
1129 virtual bool GetDataHere( void* buffer ) const
1130 {
1131 // CFSTR_SHELLURL is _always_ ANSI!
1132 wxCharBuffer char_buffer( GetDataSize() );
1133 wxCustomDataObject::GetDataHere( (void*)char_buffer.data() );
1134 wxString unicode_buffer( char_buffer, wxConvLibc );
1135 memcpy( buffer, unicode_buffer.c_str(),
1136 ( unicode_buffer.length() + 1 ) * sizeof(wxChar) );
1137
1138 return TRUE;
1139 }
1140 virtual bool GetDataHere(const wxDataFormat& WXUNUSED(format),
1141 void *buf) const
1142 { return GetDataHere(buf); }
1143 #endif
1144
1145 DECLARE_NO_COPY_CLASS(CFSTR_SHELLURLDataObject)
1146 };
1147
1148
1149
1150 wxURLDataObject::wxURLDataObject()
1151 {
1152 // we support CF_TEXT and CFSTR_SHELLURL formats which are basicly the same
1153 // but it seems that some browsers only provide one of them so we have to
1154 // support both
1155 Add(new wxTextDataObject);
1156 Add(new CFSTR_SHELLURLDataObject());
1157
1158 // we don't have any data yet
1159 m_dataObjectLast = NULL;
1160 }
1161
1162 bool wxURLDataObject::SetData(const wxDataFormat& format,
1163 size_t len,
1164 const void *buf)
1165 {
1166 m_dataObjectLast = GetObject(format);
1167
1168 wxCHECK_MSG( m_dataObjectLast, FALSE,
1169 wxT("unsupported format in wxURLDataObject"));
1170
1171 return m_dataObjectLast->SetData(len, buf);
1172 }
1173
1174 wxString wxURLDataObject::GetURL() const
1175 {
1176 wxString url;
1177 wxCHECK_MSG( m_dataObjectLast, url, _T("no data in wxURLDataObject") );
1178
1179 size_t len = m_dataObjectLast->GetDataSize();
1180
1181 m_dataObjectLast->GetDataHere(wxStringBuffer(url, len));
1182
1183 return url;
1184 }
1185
1186 void wxURLDataObject::SetURL(const wxString& url)
1187 {
1188 SetData(wxDataFormat(wxUSE_UNICODE ? wxDF_UNICODETEXT : wxDF_TEXT),
1189 url.Length()+1, url.c_str());
1190
1191 // CFSTR_SHELLURL is always supposed to be ANSI...
1192 wxWX2MBbuf urlA = (wxWX2MBbuf)url.mbc_str();
1193 size_t len = strlen(urlA);
1194 SetData(wxDataFormat(CFSTR_SHELLURL), len+1, (const char*)urlA);
1195 }
1196
1197 // ----------------------------------------------------------------------------
1198 // private functions
1199 // ----------------------------------------------------------------------------
1200
1201 #ifdef __WXDEBUG__
1202
1203 static const wxChar *GetTymedName(DWORD tymed)
1204 {
1205 static wxChar s_szBuf[128];
1206 switch ( tymed ) {
1207 case TYMED_HGLOBAL: return wxT("TYMED_HGLOBAL");
1208 case TYMED_FILE: return wxT("TYMED_FILE");
1209 case TYMED_ISTREAM: return wxT("TYMED_ISTREAM");
1210 case TYMED_ISTORAGE: return wxT("TYMED_ISTORAGE");
1211 case TYMED_GDI: return wxT("TYMED_GDI");
1212 case TYMED_MFPICT: return wxT("TYMED_MFPICT");
1213 case TYMED_ENHMF: return wxT("TYMED_ENHMF");
1214 default:
1215 wxSprintf(s_szBuf, wxT("type of media format %ld (unknown)"), tymed);
1216 return s_szBuf;
1217 }
1218 }
1219
1220 #endif // Debug
1221
1222 #else // not using OLE at all
1223 // ----------------------------------------------------------------------------
1224 // wxDataObject
1225 // ----------------------------------------------------------------------------
1226
1227 #if wxUSE_DATAOBJ
1228
1229 wxDataObject::wxDataObject()
1230 {
1231 }
1232
1233 wxDataObject::~wxDataObject()
1234 {
1235 }
1236
1237 void wxDataObject::SetAutoDelete()
1238 {
1239 }
1240
1241 #ifdef __WXDEBUG__
1242 const wxChar *wxDataObject::GetFormatName(wxDataFormat format)
1243 {
1244 return NULL;
1245 }
1246 #endif
1247
1248 #endif
1249
1250 #endif
1251