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