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