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