/[pcsx2_0.9.7]/trunk/3rdparty/wxWidgets/src/common/dobjcmn.cpp
ViewVC logotype

Contents of /trunk/3rdparty/wxWidgets/src/common/dobjcmn.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (10 years, 2 months ago) by william
File size: 13118 byte(s)
committing r3113 initial commit again...
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: dobjcmn.cpp 49036 2007-10-04 10:10:06Z SC $
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 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
293 if ( !buffer )
294 return 0;
295
296 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 );
297 return len;
298 }
299
300 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
301 {
302 if ( buf == NULL )
303 return false;
304
305 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
306 if ( !buffer )
307 return false;
308
309 size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 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

  ViewVC Help
Powered by ViewVC 1.1.22