]>
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 | #ifndef WX_PRECOMP | |
22 | #include "wx/app.h" | |
23 | #include "wx/debug.h" | |
24 | #endif | |
25 | ||
26 | #include "wx/dataobj.h" | |
27 | ||
28 | // ---------------------------------------------------------------------------- | |
29 | // lists | |
30 | // ---------------------------------------------------------------------------- | |
31 | ||
32 | #include "wx/listimpl.cpp" | |
33 | ||
34 | WX_DEFINE_LIST(wxSimpleDataObjectList) | |
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // globals | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | static wxDataFormat dataFormatInvalid; | |
41 | WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid; | |
42 | ||
43 | // ============================================================================ | |
44 | // implementation | |
45 | // ============================================================================ | |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // wxDataObjectBase | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | wxDataObjectBase::~wxDataObjectBase() | |
52 | { | |
53 | } | |
54 | ||
55 | bool wxDataObjectBase::IsSupported(const wxDataFormat& format, | |
56 | Direction dir) const | |
57 | { | |
58 | size_t nFormatCount = GetFormatCount( dir ); | |
59 | if ( nFormatCount == 1 ) | |
60 | { | |
61 | return format == GetPreferredFormat( dir ); | |
62 | } | |
63 | else | |
64 | { | |
65 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
66 | GetAllFormats( formats, dir ); | |
67 | ||
68 | size_t n; | |
69 | for ( n = 0; n < nFormatCount; n++ ) | |
70 | { | |
71 | if ( formats[n] == format ) | |
72 | break; | |
73 | } | |
74 | ||
75 | delete [] formats; | |
76 | ||
77 | // found? | |
78 | return n < nFormatCount; | |
79 | } | |
80 | } | |
81 | ||
82 | // ---------------------------------------------------------------------------- | |
83 | // wxDataObjectComposite | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | wxDataObjectComposite::wxDataObjectComposite() | |
87 | { | |
88 | m_preferred = 0; | |
89 | m_receivedFormat = wxFormatInvalid; | |
90 | } | |
91 | ||
92 | wxDataObjectComposite::~wxDataObjectComposite() | |
93 | { | |
94 | WX_CLEAR_LIST( wxSimpleDataObjectList, m_dataObjects ); | |
95 | } | |
96 | ||
97 | wxDataObjectSimple * | |
98 | wxDataObjectComposite::GetObject(const wxDataFormat& format) const | |
99 | { | |
100 | wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst(); | |
101 | while ( node ) | |
102 | { | |
103 | wxDataObjectSimple *dataObj = node->GetData(); | |
104 | ||
105 | if ( dataObj->GetFormat() == format ) | |
106 | { | |
107 | return dataObj; | |
108 | } | |
109 | ||
110 | node = node->GetNext(); | |
111 | } | |
112 | ||
113 | return (wxDataObjectSimple *)NULL; | |
114 | } | |
115 | ||
116 | void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred) | |
117 | { | |
118 | if ( preferred ) | |
119 | m_preferred = m_dataObjects.GetCount(); | |
120 | ||
121 | m_dataObjects.Append( dataObject ); | |
122 | } | |
123 | ||
124 | wxDataFormat wxDataObjectComposite::GetReceivedFormat() const | |
125 | { | |
126 | return m_receivedFormat; | |
127 | } | |
128 | ||
129 | wxDataFormat | |
130 | wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const | |
131 | { | |
132 | wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred ); | |
133 | ||
134 | wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") ); | |
135 | ||
136 | wxDataObjectSimple* dataObj = node->GetData(); | |
137 | ||
138 | return dataObj->GetFormat(); | |
139 | } | |
140 | ||
141 | #if defined(__WXMSW__) | |
142 | ||
143 | size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format ) | |
144 | { | |
145 | wxDataObjectSimple *dataObj = GetObject(format); | |
146 | ||
147 | wxCHECK_MSG( dataObj, 0, | |
148 | wxT("unsupported format in wxDataObjectComposite")); | |
149 | ||
150 | return dataObj->GetBufferOffset( format ); | |
151 | } | |
152 | ||
153 | ||
154 | const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer, | |
155 | size_t* size, | |
156 | const wxDataFormat& format ) | |
157 | { | |
158 | wxDataObjectSimple *dataObj = GetObject(format); | |
159 | ||
160 | wxCHECK_MSG( dataObj, NULL, | |
161 | wxT("unsupported format in wxDataObjectComposite")); | |
162 | ||
163 | return dataObj->GetSizeFromBuffer( buffer, size, format ); | |
164 | } | |
165 | ||
166 | ||
167 | void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size, | |
168 | const wxDataFormat& format ) | |
169 | { | |
170 | wxDataObjectSimple *dataObj = GetObject( format ); | |
171 | ||
172 | wxCHECK_MSG( dataObj, NULL, | |
173 | wxT("unsupported format in wxDataObjectComposite")); | |
174 | ||
175 | return dataObj->SetSizeInBuffer( buffer, size, format ); | |
176 | } | |
177 | ||
178 | #endif | |
179 | ||
180 | size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const | |
181 | { | |
182 | // TODO what about the Get/Set only formats? | |
183 | return m_dataObjects.GetCount(); | |
184 | } | |
185 | ||
186 | void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats, | |
187 | Direction WXUNUSED(dir)) const | |
188 | { | |
189 | size_t n = 0; | |
190 | wxSimpleDataObjectList::compatibility_iterator node; | |
191 | for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() ) | |
192 | { | |
193 | // TODO if ( !outputOnlyToo ) && this one counts ... | |
194 | formats[n++] = node->GetData()->GetFormat(); | |
195 | } | |
196 | } | |
197 | ||
198 | size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const | |
199 | { | |
200 | wxDataObjectSimple *dataObj = GetObject(format); | |
201 | ||
202 | wxCHECK_MSG( dataObj, 0, | |
203 | wxT("unsupported format in wxDataObjectComposite")); | |
204 | ||
205 | return dataObj->GetDataSize(); | |
206 | } | |
207 | ||
208 | bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format, | |
209 | void *buf) const | |
210 | { | |
211 | wxDataObjectSimple *dataObj = GetObject( format ); | |
212 | ||
213 | wxCHECK_MSG( dataObj, false, | |
214 | wxT("unsupported format in wxDataObjectComposite")); | |
215 | ||
216 | return dataObj->GetDataHere( buf ); | |
217 | } | |
218 | ||
219 | bool wxDataObjectComposite::SetData(const wxDataFormat& format, | |
220 | size_t len, | |
221 | const void *buf) | |
222 | { | |
223 | wxDataObjectSimple *dataObj = GetObject( format ); | |
224 | ||
225 | wxCHECK_MSG( dataObj, false, | |
226 | wxT("unsupported format in wxDataObjectComposite")); | |
227 | ||
228 | m_receivedFormat = format; | |
229 | return dataObj->SetData( len, buf ); | |
230 | } | |
231 | ||
232 | // ---------------------------------------------------------------------------- | |
233 | // wxTextDataObject | |
234 | // ---------------------------------------------------------------------------- | |
235 | ||
236 | #if defined(__WXGTK20__) && wxUSE_UNICODE | |
237 | ||
238 | static inline wxMBConv& GetConv(const wxDataFormat& format) | |
239 | { | |
240 | // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT | |
241 | return format == wxDF_UNICODETEXT ? wxConvUTF8 : wxConvLibc; | |
242 | } | |
243 | ||
244 | size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const | |
245 | { | |
246 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); | |
247 | ||
248 | return buffer ? strlen( buffer ) : 0; | |
249 | } | |
250 | ||
251 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
252 | { | |
253 | if ( !buf ) | |
254 | return false; | |
255 | ||
256 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); | |
257 | if ( !buffer ) | |
258 | return false; | |
259 | ||
260 | memcpy( (char*) buf, buffer, GetDataSize(format) ); | |
261 | // strcpy( (char*) buf, buffer ); | |
262 | ||
263 | return true; | |
264 | } | |
265 | ||
266 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
267 | size_t WXUNUSED(len), const void *buf) | |
268 | { | |
269 | if ( buf == NULL ) | |
270 | return false; | |
271 | ||
272 | wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf ); | |
273 | ||
274 | SetText( buffer ); | |
275 | ||
276 | return true; | |
277 | } | |
278 | ||
279 | #elif wxUSE_UNICODE && defined(__WXMAC__) | |
280 | ||
281 | static wxMBConvUTF16 sUTF16Converter; | |
282 | ||
283 | static inline wxMBConv& GetConv(const wxDataFormat& format) | |
284 | { | |
285 | return | |
286 | format == wxDF_UNICODETEXT | |
287 | ? (wxMBConv&) sUTF16Converter | |
288 | : (wxMBConv&) wxConvLocal; | |
289 | } | |
290 | ||
291 | size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const | |
292 | { | |
293 | size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 ); | |
294 | len += (format == wxDF_UNICODETEXT ? 2 : 1); | |
295 | ||
296 | return len; | |
297 | } | |
298 | ||
299 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
300 | { | |
301 | if ( buf == NULL ) | |
302 | return false; | |
303 | ||
304 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); | |
305 | ||
306 | size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 ); | |
307 | len += (format == wxDF_UNICODETEXT ? 2 : 1); | |
308 | ||
309 | // trailing (uni)char 0 | |
310 | memcpy( (char*)buf, (const char*)buffer, len ); | |
311 | ||
312 | return true; | |
313 | } | |
314 | ||
315 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
316 | size_t WXUNUSED(len), const void *buf) | |
317 | { | |
318 | if ( buf == NULL ) | |
319 | return false; | |
320 | ||
321 | wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf ); | |
322 | ||
323 | SetText( buffer ); | |
324 | ||
325 | return true; | |
326 | } | |
327 | ||
328 | #else | |
329 | ||
330 | size_t wxTextDataObject::GetDataSize() const | |
331 | { | |
332 | return GetTextLength() * sizeof(wxChar); | |
333 | } | |
334 | ||
335 | bool wxTextDataObject::GetDataHere(void *buf) const | |
336 | { | |
337 | wxStrcpy( (wxChar*)buf, GetText().c_str() ); | |
338 | ||
339 | return true; | |
340 | } | |
341 | ||
342 | bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf) | |
343 | { | |
344 | SetText( wxString((const wxChar*)buf) ); | |
345 | ||
346 | return true; | |
347 | } | |
348 | ||
349 | #endif | |
350 | ||
351 | // ---------------------------------------------------------------------------- | |
352 | // wxFileDataObjectBase | |
353 | // ---------------------------------------------------------------------------- | |
354 | ||
355 | // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should | |
356 | // be moved to gtk/dataobj.cpp | |
357 | #if 0 | |
358 | ||
359 | wxString wxFileDataObjectBase::GetFilenames() const | |
360 | { | |
361 | wxString str; | |
362 | size_t count = m_filenames.GetCount(); | |
363 | for ( size_t n = 0; n < count; n++ ) | |
364 | { | |
365 | str << m_filenames[n] << wxT('\0'); | |
366 | } | |
367 | ||
368 | return str; | |
369 | } | |
370 | ||
371 | void wxFileDataObjectBase::SetFilenames(const wxChar* filenames) | |
372 | { | |
373 | m_filenames.Empty(); | |
374 | ||
375 | wxString current; | |
376 | for ( const wxChar *pc = filenames; ; pc++ ) | |
377 | { | |
378 | if ( *pc ) | |
379 | { | |
380 | current += *pc; | |
381 | } | |
382 | else | |
383 | { | |
384 | if ( !current ) | |
385 | { | |
386 | // 2 consecutive NULs - this is the end of the string | |
387 | break; | |
388 | } | |
389 | ||
390 | m_filenames.Add(current); | |
391 | current.Empty(); | |
392 | } | |
393 | } | |
394 | } | |
395 | ||
396 | #endif | |
397 | ||
398 | // ---------------------------------------------------------------------------- | |
399 | // wxCustomDataObject | |
400 | // ---------------------------------------------------------------------------- | |
401 | ||
402 | wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format) | |
403 | : wxDataObjectSimple(format) | |
404 | { | |
405 | m_data = NULL; | |
406 | m_size = 0; | |
407 | } | |
408 | ||
409 | wxCustomDataObject::~wxCustomDataObject() | |
410 | { | |
411 | Free(); | |
412 | } | |
413 | ||
414 | void wxCustomDataObject::TakeData(size_t size, void *data) | |
415 | { | |
416 | Free(); | |
417 | ||
418 | m_size = size; | |
419 | m_data = data; | |
420 | } | |
421 | ||
422 | void *wxCustomDataObject::Alloc(size_t size) | |
423 | { | |
424 | return (void *)new char[size]; | |
425 | } | |
426 | ||
427 | void wxCustomDataObject::Free() | |
428 | { | |
429 | delete [] (char*)m_data; | |
430 | m_size = 0; | |
431 | m_data = (void*)NULL; | |
432 | } | |
433 | ||
434 | size_t wxCustomDataObject::GetDataSize() const | |
435 | { | |
436 | return GetSize(); | |
437 | } | |
438 | ||
439 | bool wxCustomDataObject::GetDataHere(void *buf) const | |
440 | { | |
441 | if ( buf == NULL ) | |
442 | return false; | |
443 | ||
444 | void *data = GetData(); | |
445 | if ( data == NULL ) | |
446 | return false; | |
447 | ||
448 | memcpy( buf, data, GetSize() ); | |
449 | ||
450 | return true; | |
451 | } | |
452 | ||
453 | bool wxCustomDataObject::SetData(size_t size, const void *buf) | |
454 | { | |
455 | Free(); | |
456 | ||
457 | m_data = Alloc(size); | |
458 | if ( m_data == NULL ) | |
459 | return false; | |
460 | ||
461 | m_size = size; | |
462 | memcpy( m_data, buf, m_size ); | |
463 | ||
464 | return true; | |
465 | } | |
466 | ||
467 | // ============================================================================ | |
468 | // some common dnd related code | |
469 | // ============================================================================ | |
470 | ||
471 | #if wxUSE_DRAG_AND_DROP | |
472 | ||
473 | #include "wx/dnd.h" | |
474 | ||
475 | // ---------------------------------------------------------------------------- | |
476 | // wxTextDropTarget | |
477 | // ---------------------------------------------------------------------------- | |
478 | ||
479 | // NB: we can't use "new" in ctor initializer lists because this provokes an | |
480 | // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!), | |
481 | // so use SetDataObject() instead | |
482 | ||
483 | wxTextDropTarget::wxTextDropTarget() | |
484 | { | |
485 | SetDataObject(new wxTextDataObject); | |
486 | } | |
487 | ||
488 | wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) | |
489 | { | |
490 | if ( !GetData() ) | |
491 | return wxDragNone; | |
492 | ||
493 | wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject; | |
494 | return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone; | |
495 | } | |
496 | ||
497 | // ---------------------------------------------------------------------------- | |
498 | // wxFileDropTarget | |
499 | // ---------------------------------------------------------------------------- | |
500 | ||
501 | wxFileDropTarget::wxFileDropTarget() | |
502 | { | |
503 | SetDataObject(new wxFileDataObject); | |
504 | } | |
505 | ||
506 | wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) | |
507 | { | |
508 | if ( !GetData() ) | |
509 | return wxDragNone; | |
510 | ||
511 | wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject; | |
512 | return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone; | |
513 | } | |
514 | ||
515 | #endif // wxUSE_DRAG_AND_DROP | |
516 | ||
517 | #endif // wxUSE_DATAOBJ |