]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/dobjcmn.cpp | |
3 | // Purpose: implementation of data object methods common to all platforms | |
4 | // Author: Vadim Zeitlin, Robert Roebling | |
5 | // Modified by: | |
6 | // Created: 19.10.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) wxWidgets Team | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if wxUSE_DATAOBJ | |
20 | ||
21 | #include "wx/dataobj.h" | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/app.h" | |
25 | #endif | |
26 | ||
27 | // ---------------------------------------------------------------------------- | |
28 | // lists | |
29 | // ---------------------------------------------------------------------------- | |
30 | ||
31 | #include "wx/listimpl.cpp" | |
32 | ||
33 | WX_DEFINE_LIST(wxSimpleDataObjectList) | |
34 | ||
35 | // ---------------------------------------------------------------------------- | |
36 | // globals | |
37 | // ---------------------------------------------------------------------------- | |
38 | ||
39 | static wxDataFormat dataFormatInvalid; | |
40 | WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid; | |
41 | ||
42 | // ============================================================================ | |
43 | // implementation | |
44 | // ============================================================================ | |
45 | ||
46 | // ---------------------------------------------------------------------------- | |
47 | // wxDataObjectBase | |
48 | // ---------------------------------------------------------------------------- | |
49 | ||
50 | wxDataObjectBase::~wxDataObjectBase() | |
51 | { | |
52 | } | |
53 | ||
54 | bool wxDataObjectBase::IsSupported(const wxDataFormat& format, | |
55 | Direction dir) const | |
56 | { | |
57 | size_t nFormatCount = GetFormatCount( dir ); | |
58 | if ( nFormatCount == 1 ) | |
59 | { | |
60 | return format == GetPreferredFormat( dir ); | |
61 | } | |
62 | else | |
63 | { | |
64 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
65 | GetAllFormats( formats, dir ); | |
66 | ||
67 | size_t n; | |
68 | for ( n = 0; n < nFormatCount; n++ ) | |
69 | { | |
70 | if ( formats[n] == format ) | |
71 | break; | |
72 | } | |
73 | ||
74 | delete [] formats; | |
75 | ||
76 | // found? | |
77 | return n < nFormatCount; | |
78 | } | |
79 | } | |
80 | ||
81 | // ---------------------------------------------------------------------------- | |
82 | // wxDataObjectComposite | |
83 | // ---------------------------------------------------------------------------- | |
84 | ||
85 | wxDataObjectComposite::wxDataObjectComposite() | |
86 | { | |
87 | m_preferred = 0; | |
88 | m_receivedFormat = wxFormatInvalid; | |
89 | } | |
90 | ||
91 | wxDataObjectComposite::~wxDataObjectComposite() | |
92 | { | |
93 | WX_CLEAR_LIST( wxSimpleDataObjectList, m_dataObjects ); | |
94 | } | |
95 | ||
96 | wxDataObjectSimple * | |
97 | wxDataObjectComposite::GetObject(const wxDataFormat& format, wxDataObjectBase::Direction dir) const | |
98 | { | |
99 | wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst(); | |
100 | ||
101 | while ( node ) | |
102 | { | |
103 | wxDataObjectSimple *dataObj = node->GetData(); | |
104 | ||
105 | if (dataObj->IsSupported(format,dir)) | |
106 | return dataObj; | |
107 | node = node->GetNext(); | |
108 | } | |
109 | return NULL; | |
110 | } | |
111 | ||
112 | void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred) | |
113 | { | |
114 | if ( preferred ) | |
115 | m_preferred = m_dataObjects.GetCount(); | |
116 | ||
117 | m_dataObjects.Append( dataObject ); | |
118 | } | |
119 | ||
120 | wxDataFormat wxDataObjectComposite::GetReceivedFormat() const | |
121 | { | |
122 | return m_receivedFormat; | |
123 | } | |
124 | ||
125 | wxDataFormat | |
126 | wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const | |
127 | { | |
128 | wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred ); | |
129 | ||
130 | wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") ); | |
131 | ||
132 | wxDataObjectSimple* dataObj = node->GetData(); | |
133 | ||
134 | return dataObj->GetFormat(); | |
135 | } | |
136 | ||
137 | #if defined(__WXMSW__) | |
138 | ||
139 | size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format ) | |
140 | { | |
141 | wxDataObjectSimple *dataObj = GetObject(format); | |
142 | ||
143 | wxCHECK_MSG( dataObj, 0, | |
144 | wxT("unsupported format in wxDataObjectComposite")); | |
145 | ||
146 | return dataObj->GetBufferOffset( format ); | |
147 | } | |
148 | ||
149 | ||
150 | const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer, | |
151 | size_t* size, | |
152 | const wxDataFormat& format ) | |
153 | { | |
154 | wxDataObjectSimple *dataObj = GetObject(format); | |
155 | ||
156 | wxCHECK_MSG( dataObj, NULL, | |
157 | wxT("unsupported format in wxDataObjectComposite")); | |
158 | ||
159 | return dataObj->GetSizeFromBuffer( buffer, size, format ); | |
160 | } | |
161 | ||
162 | ||
163 | void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size, | |
164 | const wxDataFormat& format ) | |
165 | { | |
166 | wxDataObjectSimple *dataObj = GetObject( format ); | |
167 | ||
168 | wxCHECK_MSG( dataObj, NULL, | |
169 | wxT("unsupported format in wxDataObjectComposite")); | |
170 | ||
171 | return dataObj->SetSizeInBuffer( buffer, size, format ); | |
172 | } | |
173 | ||
174 | #endif | |
175 | ||
176 | size_t wxDataObjectComposite::GetFormatCount(Direction dir) const | |
177 | { | |
178 | size_t n = 0; | |
179 | ||
180 | // NOTE: some wxDataObjectSimple objects may return a number greater than 1 | |
181 | // from GetFormatCount(): this is the case of e.g. wxTextDataObject | |
182 | // under wxMac and wxGTK | |
183 | wxSimpleDataObjectList::compatibility_iterator node; | |
184 | for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() ) | |
185 | n += node->GetData()->GetFormatCount(dir); | |
186 | ||
187 | return n; | |
188 | } | |
189 | ||
190 | void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats, | |
191 | Direction dir) const | |
192 | { | |
193 | size_t index(0); | |
194 | wxSimpleDataObjectList::compatibility_iterator node; | |
195 | ||
196 | for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() ) | |
197 | { | |
198 | // NOTE: some wxDataObjectSimple objects may return more than 1 format | |
199 | // from GetAllFormats(): this is the case of e.g. wxTextDataObject | |
200 | // under wxMac and wxGTK | |
201 | node->GetData()->GetAllFormats(formats+index, dir); | |
202 | index += node->GetData()->GetFormatCount(dir); | |
203 | } | |
204 | } | |
205 | ||
206 | size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const | |
207 | { | |
208 | wxDataObjectSimple *dataObj = GetObject(format); | |
209 | ||
210 | wxCHECK_MSG( dataObj, 0, | |
211 | wxT("unsupported format in wxDataObjectComposite")); | |
212 | ||
213 | return dataObj->GetDataSize(); | |
214 | } | |
215 | ||
216 | bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format, | |
217 | void *buf) const | |
218 | { | |
219 | wxDataObjectSimple *dataObj = GetObject( format ); | |
220 | ||
221 | wxCHECK_MSG( dataObj, false, | |
222 | wxT("unsupported format in wxDataObjectComposite")); | |
223 | ||
224 | return dataObj->GetDataHere( buf ); | |
225 | } | |
226 | ||
227 | bool wxDataObjectComposite::SetData(const wxDataFormat& format, | |
228 | size_t len, | |
229 | const void *buf) | |
230 | { | |
231 | wxDataObjectSimple *dataObj = GetObject( format ); | |
232 | ||
233 | wxCHECK_MSG( dataObj, false, | |
234 | wxT("unsupported format in wxDataObjectComposite")); | |
235 | ||
236 | m_receivedFormat = format; | |
237 | return dataObj->SetData( len, buf ); | |
238 | } | |
239 | ||
240 | // ---------------------------------------------------------------------------- | |
241 | // wxTextDataObject | |
242 | // ---------------------------------------------------------------------------- | |
243 | ||
244 | #ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ | |
245 | ||
246 | // FIXME-UTF8: we should be able to merge wchar_t and UTF-8 versions once we | |
247 | // have a way to get UTF-8 string (and its length) in both builds | |
248 | // without loss of efficiency (i.e. extra buffer copy/strlen call) | |
249 | ||
250 | #if wxUSE_UNICODE_WCHAR | |
251 | ||
252 | static inline wxMBConv& GetConv(const wxDataFormat& format) | |
253 | { | |
254 | // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT | |
255 | return format == wxDF_UNICODETEXT ? wxConvUTF8 : wxConvLibc; | |
256 | } | |
257 | ||
258 | size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const | |
259 | { | |
260 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); | |
261 | ||
262 | return buffer ? strlen( buffer ) : 0; | |
263 | } | |
264 | ||
265 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
266 | { | |
267 | if ( !buf ) | |
268 | return false; | |
269 | ||
270 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); | |
271 | if ( !buffer ) | |
272 | return false; | |
273 | ||
274 | memcpy( (char*) buf, buffer, GetDataSize(format) ); | |
275 | // strcpy( (char*) buf, buffer ); | |
276 | ||
277 | return true; | |
278 | } | |
279 | ||
280 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
281 | size_t WXUNUSED(len), const void *buf) | |
282 | { | |
283 | if ( buf == NULL ) | |
284 | return false; | |
285 | ||
286 | wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf ); | |
287 | ||
288 | SetText( buffer ); | |
289 | ||
290 | return true; | |
291 | } | |
292 | ||
293 | #else // wxUSE_UNICODE_UTF8 | |
294 | ||
295 | size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const | |
296 | { | |
297 | if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 ) | |
298 | { | |
299 | return m_text.utf8_length(); | |
300 | } | |
301 | else // wxDF_TEXT | |
302 | { | |
303 | const wxCharBuffer buf(wxConvLocal.cWC2MB(m_text.wc_str())); | |
304 | return buf ? strlen(buf) : 0; | |
305 | } | |
306 | } | |
307 | ||
308 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
309 | { | |
310 | if ( !buf ) | |
311 | return false; | |
312 | ||
313 | if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 ) | |
314 | { | |
315 | memcpy(buf, m_text.utf8_str(), m_text.utf8_length()); | |
316 | } | |
317 | else // wxDF_TEXT | |
318 | { | |
319 | const wxCharBuffer bufLocal(wxConvLocal.cWC2MB(m_text.wc_str())); | |
320 | if ( !bufLocal ) | |
321 | return false; | |
322 | ||
323 | memcpy(buf, bufLocal, strlen(bufLocal)); | |
324 | } | |
325 | ||
326 | return true; | |
327 | } | |
328 | ||
329 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
330 | size_t len, const void *buf_) | |
331 | { | |
332 | const char * const buf = static_cast<const char *>(buf_); | |
333 | ||
334 | if ( buf == NULL ) | |
335 | return false; | |
336 | ||
337 | if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 ) | |
338 | { | |
339 | // normally the data is in UTF-8 so we could use FromUTF8Unchecked() | |
340 | // but it's not absolutely clear what GTK+ does if the clipboard data | |
341 | // is not in UTF-8 so do an extra check for tranquility, it shouldn't | |
342 | // matter much if we lose a bit of performance when pasting from | |
343 | // clipboard | |
344 | m_text = wxString::FromUTF8(buf, len); | |
345 | } | |
346 | else // wxDF_TEXT, convert from current (non-UTF8) locale | |
347 | { | |
348 | m_text = wxConvLocal.cMB2WC(buf, len, NULL); | |
349 | } | |
350 | ||
351 | return true; | |
352 | } | |
353 | ||
354 | #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8 | |
355 | ||
356 | #elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ) | |
357 | ||
358 | namespace | |
359 | { | |
360 | ||
361 | inline wxMBConv& GetConv(const wxDataFormat& format) | |
362 | { | |
363 | static wxMBConvUTF16 s_UTF16Converter; | |
364 | ||
365 | return format == wxDF_UNICODETEXT ? static_cast<wxMBConv&>(s_UTF16Converter) | |
366 | : static_cast<wxMBConv&>(wxConvLocal); | |
367 | } | |
368 | ||
369 | } // anonymous namespace | |
370 | ||
371 | size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const | |
372 | { | |
373 | return GetConv(format).WC2MB(NULL, GetText().wc_str(), 0); | |
374 | } | |
375 | ||
376 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
377 | { | |
378 | if ( buf == NULL ) | |
379 | return false; | |
380 | ||
381 | wxCharBuffer buffer(GetConv(format).cWX2MB(GetText().c_str())); | |
382 | ||
383 | memcpy(buf, buffer.data(), buffer.length()); | |
384 | ||
385 | return true; | |
386 | } | |
387 | ||
388 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
389 | size_t WXUNUSED(len), | |
390 | const void *buf) | |
391 | { | |
392 | if ( buf == NULL ) | |
393 | return false; | |
394 | ||
395 | SetText(GetConv(format).cMB2WX(static_cast<const char*>(buf))); | |
396 | ||
397 | return true; | |
398 | } | |
399 | ||
400 | #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ | |
401 | ||
402 | size_t wxTextDataObject::GetDataSize() const | |
403 | { | |
404 | return GetTextLength() * sizeof(wxChar); | |
405 | } | |
406 | ||
407 | bool wxTextDataObject::GetDataHere(void *buf) const | |
408 | { | |
409 | // NOTE: use wxTmemcpy() instead of wxStrncpy() to allow | |
410 | // retrieval of strings with embedded NULLs | |
411 | wxTmemcpy( (wxChar*)buf, GetText().c_str(), GetTextLength() ); | |
412 | ||
413 | return true; | |
414 | } | |
415 | ||
416 | bool wxTextDataObject::SetData(size_t len, const void *buf) | |
417 | { | |
418 | SetText( wxString((const wxChar*)buf, len/sizeof(wxChar)) ); | |
419 | ||
420 | return true; | |
421 | } | |
422 | ||
423 | #endif // different wxTextDataObject implementations | |
424 | ||
425 | // ---------------------------------------------------------------------------- | |
426 | // wxCustomDataObject | |
427 | // ---------------------------------------------------------------------------- | |
428 | ||
429 | wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format) | |
430 | : wxDataObjectSimple(format) | |
431 | { | |
432 | m_data = NULL; | |
433 | m_size = 0; | |
434 | } | |
435 | ||
436 | wxCustomDataObject::~wxCustomDataObject() | |
437 | { | |
438 | Free(); | |
439 | } | |
440 | ||
441 | void wxCustomDataObject::TakeData(size_t size, void *data) | |
442 | { | |
443 | Free(); | |
444 | ||
445 | m_size = size; | |
446 | m_data = data; | |
447 | } | |
448 | ||
449 | void *wxCustomDataObject::Alloc(size_t size) | |
450 | { | |
451 | return (void *)new char[size]; | |
452 | } | |
453 | ||
454 | void wxCustomDataObject::Free() | |
455 | { | |
456 | delete [] (char*)m_data; | |
457 | m_size = 0; | |
458 | m_data = NULL; | |
459 | } | |
460 | ||
461 | size_t wxCustomDataObject::GetDataSize() const | |
462 | { | |
463 | return GetSize(); | |
464 | } | |
465 | ||
466 | bool wxCustomDataObject::GetDataHere(void *buf) const | |
467 | { | |
468 | if ( buf == NULL ) | |
469 | return false; | |
470 | ||
471 | void *data = GetData(); | |
472 | if ( data == NULL ) | |
473 | return false; | |
474 | ||
475 | memcpy( buf, data, GetSize() ); | |
476 | ||
477 | return true; | |
478 | } | |
479 | ||
480 | bool wxCustomDataObject::SetData(size_t size, const void *buf) | |
481 | { | |
482 | Free(); | |
483 | ||
484 | m_data = Alloc(size); | |
485 | if ( m_data == NULL ) | |
486 | return false; | |
487 | ||
488 | m_size = size; | |
489 | memcpy( m_data, buf, m_size ); | |
490 | ||
491 | return true; | |
492 | } | |
493 | ||
494 | // ============================================================================ | |
495 | // some common dnd related code | |
496 | // ============================================================================ | |
497 | ||
498 | #if wxUSE_DRAG_AND_DROP | |
499 | ||
500 | #include "wx/dnd.h" | |
501 | ||
502 | // ---------------------------------------------------------------------------- | |
503 | // wxTextDropTarget | |
504 | // ---------------------------------------------------------------------------- | |
505 | ||
506 | // NB: we can't use "new" in ctor initializer lists because this provokes an | |
507 | // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!), | |
508 | // so use SetDataObject() instead | |
509 | ||
510 | wxTextDropTarget::wxTextDropTarget() | |
511 | { | |
512 | SetDataObject(new wxTextDataObject); | |
513 | } | |
514 | ||
515 | wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) | |
516 | { | |
517 | if ( !GetData() ) | |
518 | return wxDragNone; | |
519 | ||
520 | wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject; | |
521 | return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone; | |
522 | } | |
523 | ||
524 | // ---------------------------------------------------------------------------- | |
525 | // wxFileDropTarget | |
526 | // ---------------------------------------------------------------------------- | |
527 | ||
528 | wxFileDropTarget::wxFileDropTarget() | |
529 | { | |
530 | SetDataObject(new wxFileDataObject); | |
531 | } | |
532 | ||
533 | wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) | |
534 | { | |
535 | if ( !GetData() ) | |
536 | return wxDragNone; | |
537 | ||
538 | wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject; | |
539 | return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone; | |
540 | } | |
541 | ||
542 | #endif // wxUSE_DRAG_AND_DROP | |
543 | ||
544 | #endif // wxUSE_DATAOBJ |