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