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