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