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