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