]> git.saurik.com Git - wxWidgets.git/blame - src/common/dobjcmn.cpp
more synonyms for UCS-2/4
[wxWidgets.git] / src / common / dobjcmn.cpp
CommitLineData
3f364be8
VZ
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$
77ffb593 8// Copyright: (c) wxWidgets Team
65571936 9// Licence: wxWindows licence
3f364be8
VZ
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
3f364be8
VZ
21 #pragma implementation "dataobjbase.h"
22#endif
23
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
29
1e6feb95
VZ
30#if wxUSE_DATAOBJ
31
3f364be8
VZ
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
45WX_DEFINE_LIST(wxSimpleDataObjectList);
46
0c2b453f
VZ
47// ----------------------------------------------------------------------------
48// globals
49// ----------------------------------------------------------------------------
50
51static wxDataFormat dataFormatInvalid;
31e78e0c 52WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid;
0c2b453f 53
3f364be8
VZ
54// ============================================================================
55// implementation
56// ============================================================================
57
58// ----------------------------------------------------------------------------
59// wxDataObjectBase
60// ----------------------------------------------------------------------------
61
62wxDataObjectBase::~wxDataObjectBase()
63{
64}
65
d9317fd4
VZ
66bool 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
3f364be8
VZ
93// ----------------------------------------------------------------------------
94// wxDataObjectComposite
95// ----------------------------------------------------------------------------
96
e6b01b78
VZ
97wxDataObjectComposite::wxDataObjectComposite()
98{
99 m_preferred = 0;
222ed1d6 100}
e6b01b78 101
222ed1d6
MB
102wxDataObjectComposite::~wxDataObjectComposite()
103{
104 WX_CLEAR_LIST(wxSimpleDataObjectList, m_dataObjects);
e6b01b78
VZ
105}
106
3f364be8
VZ
107wxDataObjectSimple *
108wxDataObjectComposite::GetObject(const wxDataFormat& format) const
109{
222ed1d6 110 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst();
3f364be8
VZ
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
126void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
127{
128 if ( preferred )
129 m_preferred = m_dataObjects.GetCount();
130
131 m_dataObjects.Append( dataObject );
132}
133
134wxDataFormat
135wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
136{
222ed1d6 137 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred );
3f364be8 138
2ee3ee1b 139 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
3f364be8
VZ
140
141 wxDataObjectSimple* dataObj = node->GetData();
142
143 return dataObj->GetFormat();
144}
145
e1b435af 146#if defined(__WXMSW__)
7d584866 147
e1b435af
MB
148size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format )
149{
150 wxDataObjectSimple *dataObj = GetObject(format);
151
f7f50f49 152 wxCHECK_MSG( dataObj, 0,
e1b435af
MB
153 wxT("unsupported format in wxDataObjectComposite"));
154
155 return dataObj->GetBufferOffset( format );
156}
157
7d584866 158
e1b435af
MB
159const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer,
160 size_t* size,
161 const wxDataFormat& format )
162{
163 wxDataObjectSimple *dataObj = GetObject(format);
164
f7f50f49 165 wxCHECK_MSG( dataObj, NULL,
e1b435af
MB
166 wxT("unsupported format in wxDataObjectComposite"));
167
168 return dataObj->GetSizeFromBuffer( buffer, size, format );
169}
170
7d584866 171
e1b435af
MB
172void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size,
173 const wxDataFormat& format )
174{
175 wxDataObjectSimple *dataObj = GetObject(format);
176
f7f50f49 177 wxCHECK_MSG( dataObj, NULL,
e1b435af
MB
178 wxT("unsupported format in wxDataObjectComposite"));
179
180 return dataObj->SetSizeInBuffer( buffer, size, format );
181}
182
183#endif
184
3f364be8
VZ
185size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const
186{
187 // TODO what about the Get/Set only formats?
188 return m_dataObjects.GetCount();
189}
190
191void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
192 Direction WXUNUSED(dir)) const
193{
194 size_t n = 0;
222ed1d6 195 wxSimpleDataObjectList::compatibility_iterator node;
3f364be8
VZ
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
203size_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
213bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
214 void *buf) const
215{
216 wxDataObjectSimple *dataObj = GetObject(format);
217
68379eaf 218 wxCHECK_MSG( dataObj, false,
3f364be8
VZ
219 wxT("unsupported format in wxDataObjectComposite"));
220
221 return dataObj->GetDataHere(buf);
222}
223
224bool wxDataObjectComposite::SetData(const wxDataFormat& format,
225 size_t len,
226 const void *buf)
227{
228 wxDataObjectSimple *dataObj = GetObject(format);
229
68379eaf 230 wxCHECK_MSG( dataObj, false,
3f364be8
VZ
231 wxT("unsupported format in wxDataObjectComposite"));
232
233 return dataObj->SetData(len, buf);
234}
235
236// ----------------------------------------------------------------------------
237// wxTextDataObject
238// ----------------------------------------------------------------------------
239
6a59178a 240#if defined(__WXGTK20__) && wxUSE_UNICODE
c7d6d883 241
f4370741
VZ
242static 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
c7d6d883
RR
248size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
249{
f4370741
VZ
250 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
251 return buffer ? strlen(buffer) + 1 : 0;
c7d6d883
RR
252}
253
254bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
255{
f4370741
VZ
256 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
257 if ( !buffer )
258 return false;
259
260 strcpy( (char*) buf, buffer );
68379eaf
WS
261
262 return true;
c7d6d883
RR
263}
264
265bool wxTextDataObject::SetData(const wxDataFormat& format,
266 size_t WXUNUSED(len), const void *buf)
267{
f4370741
VZ
268 wxWCharBuffer buffer = GetConv(format).cMB2WX((const char *)buf);
269 if ( !buffer )
270 return false;
271
272 SetText(buffer);
68379eaf
WS
273
274 return true;
c7d6d883
RR
275}
276
25758297
SC
277#elif wxUSE_UNICODE && defined(__WXMAC__)
278
671478ee
SC
279static wxMBConvUTF16 sUTF16Converter ;
280
281static inline wxMBConv& GetConv(const wxDataFormat& format)
282{
283 return format == wxDF_UNICODETEXT ? sUTF16Converter : (wxMBConv&) wxConvLocal;
284}
285
25758297
SC
286size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
287{
671478ee
SC
288 size_t len = GetConv(format).WC2MB( NULL , GetText().c_str() , 0 )
289 + ( format == wxDF_UNICODETEXT ? 2 : 1 ) ;
290 return len ;
25758297
SC
291}
292
293bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
294{
671478ee
SC
295 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
296 if ( !buffer )
297 return false;
298
299 size_t len = GetConv(format).WC2MB( NULL , GetText().c_str() , 0 )
300 + ( format == wxDF_UNICODETEXT ? 2 : 1 ) ;
301
302 memcpy( (char*) buf, (const char*) buffer , len ); // trailing (uni)char 0
68379eaf
WS
303
304 return true;
25758297
SC
305}
306
307bool wxTextDataObject::SetData(const wxDataFormat& format,
308 size_t WXUNUSED(len), const void *buf)
309{
671478ee
SC
310 wxWCharBuffer buffer = GetConv(format).cMB2WX((const char *)buf);
311 if ( !buffer )
312 return false;
313
314 SetText(buffer);
315
68379eaf 316 return true;
25758297
SC
317}
318
6a59178a 319#else
c7d6d883
RR
320
321size_t wxTextDataObject::GetDataSize() const
322{
e1b435af 323 return GetTextLength() * sizeof(wxChar);
3f364be8
VZ
324}
325
326bool wxTextDataObject::GetDataHere(void *buf) const
327{
e1b435af 328 wxStrcpy((wxChar *)buf, GetText().c_str());
3f364be8 329
68379eaf 330 return true;
3f364be8
VZ
331}
332
333bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
334{
e1b435af 335 SetText(wxString((const wxChar *)buf));
3f364be8 336
68379eaf 337 return true;
3f364be8
VZ
338}
339
c7d6d883
RR
340#endif
341
3f364be8
VZ
342// ----------------------------------------------------------------------------
343// wxFileDataObjectBase
344// ----------------------------------------------------------------------------
345
9e2896e5
VZ
346// VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
347// be moved to gtk/dataobj.cpp
348#if 0
349
3f364be8
VZ
350wxString wxFileDataObjectBase::GetFilenames() const
351{
352 wxString str;
353 size_t count = m_filenames.GetCount();
354 for ( size_t n = 0; n < count; n++ )
355 {
356 str << m_filenames[n] << wxT('\0');
357 }
358
359 return str;
360}
361
362void wxFileDataObjectBase::SetFilenames(const wxChar* filenames)
363{
364 m_filenames.Empty();
365
366 wxString current;
367 for ( const wxChar *pc = filenames; ; pc++ )
368 {
369 if ( *pc )
370 {
371 current += *pc;
372 }
373 else
374 {
375 if ( !current )
376 {
377 // 2 consecutive NULs - this is the end of the string
378 break;
379 }
380
381 m_filenames.Add(current);
382 current.Empty();
383 }
384 }
385}
386
9e2896e5
VZ
387#endif // 0
388
3f364be8
VZ
389// ----------------------------------------------------------------------------
390// wxCustomDataObject
391// ----------------------------------------------------------------------------
392
775e1c62
RD
393wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
394 : wxDataObjectSimple(format)
395{
4e16881a 396 m_data = (void *)NULL;
775e1c62
RD
397}
398
3f364be8
VZ
399wxCustomDataObject::~wxCustomDataObject()
400{
401 Free();
402}
403
404void wxCustomDataObject::TakeData(size_t size, void *data)
405{
406 Free();
407
408 m_size = size;
409 m_data = data;
410}
411
412void *wxCustomDataObject::Alloc(size_t size)
413{
414 return (void *)new char[size];
415}
416
417void wxCustomDataObject::Free()
418{
5bcea60e 419 delete [] (char *)m_data;
3f364be8
VZ
420 m_size = 0;
421 m_data = (void *)NULL;
422}
423
424size_t wxCustomDataObject::GetDataSize() const
425{
426 return GetSize();
427}
428
429bool wxCustomDataObject::GetDataHere(void *buf) const
430{
431 void *data = GetData();
432 if ( !data )
68379eaf 433 return false;
3f364be8
VZ
434
435 memcpy(buf, data, GetSize());
436
68379eaf 437 return true;
3f364be8
VZ
438}
439
9e2896e5 440bool wxCustomDataObject::SetData(size_t size, const void *buf)
3f364be8
VZ
441{
442 Free();
443
444 m_data = Alloc(size);
445 if ( !m_data )
68379eaf 446 return false;
3f364be8 447
9e2896e5 448 memcpy(m_data, buf, m_size = size);
3f364be8 449
68379eaf 450 return true;
3f364be8
VZ
451}
452
8ee9d618
VZ
453// ============================================================================
454// some common dnd related code
455// ============================================================================
456
8fb3a512
JS
457#if wxUSE_DRAG_AND_DROP
458
8ee9d618
VZ
459#include "wx/dnd.h"
460
461// ----------------------------------------------------------------------------
462// wxTextDropTarget
463// ----------------------------------------------------------------------------
464
f3ac12aa
VZ
465// NB: we can't use "new" in ctor initializer lists because this provokes an
466// internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
467// so use SetDataObject() instead
468
8ee9d618 469wxTextDropTarget::wxTextDropTarget()
8ee9d618 470{
f3ac12aa 471 SetDataObject(new wxTextDataObject);
8ee9d618
VZ
472}
473
474wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
475{
476 if ( !GetData() )
477 return wxDragNone;
478
479 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
480 return OnDropText(x, y, dobj->GetText()) ? def : wxDragNone;
481}
482
483// ----------------------------------------------------------------------------
484// wxFileDropTarget
485// ----------------------------------------------------------------------------
486
487wxFileDropTarget::wxFileDropTarget()
8ee9d618 488{
f3ac12aa 489 SetDataObject(new wxFileDataObject);
8ee9d618
VZ
490}
491
492wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
493{
494 if ( !GetData() )
495 return wxDragNone;
496
497 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
498 return OnDropFiles(x, y, dobj->GetFilenames()) ? def : wxDragNone;
499}
500
1e6feb95 501#endif // wxUSE_DRAG_AND_DROP
8fb3a512 502
1e6feb95 503#endif // wxUSE_DATAOBJ