]> git.saurik.com Git - wxWidgets.git/blob - src/common/dobjcmn.cpp
cleanup - reformatting
[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 ) + 1 : 0;
242 }
243
244 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
245 {
246 if ( buf == NULL )
247 return false;
248
249 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
250 if ( buffer == NULL )
251 return false;
252
253 strcpy( (char*)buf, buffer );
254
255 return true;
256 }
257
258 bool wxTextDataObject::SetData(const wxDataFormat& format,
259 size_t WXUNUSED(len), const void *buf)
260 {
261 if ( buf == NULL )
262 return false;
263
264 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
265 if ( buffer == NULL )
266 return false;
267
268 SetText( buffer );
269
270 return true;
271 }
272
273 #elif wxUSE_UNICODE && defined(__WXMAC__)
274
275 static wxMBConvUTF16 sUTF16Converter;
276
277 static inline wxMBConv& GetConv(const wxDataFormat& format)
278 {
279 return
280 format == wxDF_UNICODETEXT
281 ? (wxMBConv&) sUTF16Converter
282 : (wxMBConv&) wxConvLocal;
283 }
284
285 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
286 {
287 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
288 len += (format == wxDF_UNICODETEXT ? 2 : 1);
289
290 return len;
291 }
292
293 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
294 {
295 if ( buf == NULL )
296 return false;
297
298 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
299
300 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
301 len += (format == wxDF_UNICODETEXT ? 2 : 1);
302
303 // trailing (uni)char 0
304 memcpy( (char*)buf, (const char*)buffer, len );
305
306 return true;
307 }
308
309 bool wxTextDataObject::SetData(const wxDataFormat& format,
310 size_t WXUNUSED(len), const void *buf)
311 {
312 if ( buf == NULL )
313 return false;
314
315 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
316
317 SetText( buffer );
318
319 return true;
320 }
321
322 #else
323
324 size_t wxTextDataObject::GetDataSize() const
325 {
326 return GetTextLength() * sizeof(wxChar);
327 }
328
329 bool wxTextDataObject::GetDataHere(void *buf) const
330 {
331 wxStrcpy( (wxChar*)buf, GetText().c_str() );
332
333 return true;
334 }
335
336 bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
337 {
338 SetText( wxString((const wxChar*)buf) );
339
340 return true;
341 }
342
343 #endif
344
345 // ----------------------------------------------------------------------------
346 // wxFileDataObjectBase
347 // ----------------------------------------------------------------------------
348
349 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
350 // be moved to gtk/dataobj.cpp
351 #if 0
352
353 wxString wxFileDataObjectBase::GetFilenames() const
354 {
355 wxString str;
356 size_t count = m_filenames.GetCount();
357 for ( size_t n = 0; n < count; n++ )
358 {
359 str << m_filenames[n] << wxT('\0');
360 }
361
362 return str;
363 }
364
365 void wxFileDataObjectBase::SetFilenames(const wxChar* filenames)
366 {
367 m_filenames.Empty();
368
369 wxString current;
370 for ( const wxChar *pc = filenames; ; pc++ )
371 {
372 if ( *pc )
373 {
374 current += *pc;
375 }
376 else
377 {
378 if ( !current )
379 {
380 // 2 consecutive NULs - this is the end of the string
381 break;
382 }
383
384 m_filenames.Add(current);
385 current.Empty();
386 }
387 }
388 }
389
390 #endif
391
392 // ----------------------------------------------------------------------------
393 // wxCustomDataObject
394 // ----------------------------------------------------------------------------
395
396 wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
397 : wxDataObjectSimple(format)
398 {
399 m_data = NULL;
400 m_size = 0;
401 }
402
403 wxCustomDataObject::~wxCustomDataObject()
404 {
405 Free();
406 }
407
408 void wxCustomDataObject::TakeData(size_t size, void *data)
409 {
410 Free();
411
412 m_size = size;
413 m_data = data;
414 }
415
416 void *wxCustomDataObject::Alloc(size_t size)
417 {
418 return (void *)new char[size];
419 }
420
421 void wxCustomDataObject::Free()
422 {
423 delete [] (char*)m_data;
424 m_size = 0;
425 m_data = (void*)NULL;
426 }
427
428 size_t wxCustomDataObject::GetDataSize() const
429 {
430 return GetSize();
431 }
432
433 bool wxCustomDataObject::GetDataHere(void *buf) const
434 {
435 if ( buf == NULL )
436 return false;
437
438 void *data = GetData();
439 if ( data == NULL )
440 return false;
441
442 memcpy( buf, data, GetSize() );
443
444 return true;
445 }
446
447 bool wxCustomDataObject::SetData(size_t size, const void *buf)
448 {
449 Free();
450
451 m_data = Alloc(size);
452 if ( m_data == NULL )
453 return false;
454
455 m_size = size;
456 memcpy( m_data, buf, m_size );
457
458 return true;
459 }
460
461 // ============================================================================
462 // some common dnd related code
463 // ============================================================================
464
465 #if wxUSE_DRAG_AND_DROP
466
467 #include "wx/dnd.h"
468
469 // ----------------------------------------------------------------------------
470 // wxTextDropTarget
471 // ----------------------------------------------------------------------------
472
473 // NB: we can't use "new" in ctor initializer lists because this provokes an
474 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
475 // so use SetDataObject() instead
476
477 wxTextDropTarget::wxTextDropTarget()
478 {
479 SetDataObject(new wxTextDataObject);
480 }
481
482 wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
483 {
484 if ( !GetData() )
485 return wxDragNone;
486
487 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
488 return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone;
489 }
490
491 // ----------------------------------------------------------------------------
492 // wxFileDropTarget
493 // ----------------------------------------------------------------------------
494
495 wxFileDropTarget::wxFileDropTarget()
496 {
497 SetDataObject(new wxFileDataObject);
498 }
499
500 wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
501 {
502 if ( !GetData() )
503 return wxDragNone;
504
505 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
506 return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone;
507 }
508
509 #endif // wxUSE_DRAG_AND_DROP
510
511 #endif // wxUSE_DATAOBJ