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