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