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