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