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