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