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