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