]> git.saurik.com Git - wxWidgets.git/blame - src/common/dobjcmn.cpp
wxUSE_*BOOK checks.
[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
RR
241
242size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
243{
244 if (format == wxDF_UNICODETEXT)
245 {
246 // Use UTF8 not UCS4
247 wxCharBuffer buffer = wxConvUTF8.cWX2MB( GetText().c_str() );
248 return strlen( (const char*) buffer ) + 1;
249 }
250 else // == wxDF_TEXT
251 {
252 wxCharBuffer buffer = wxConvLibc.cWX2MB( GetText().c_str() );
253 return strlen( (const char*) buffer ) + 1;
254 }
255}
256
257bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
258{
259 if (format == wxDF_UNICODETEXT)
260 {
261 // Use UTF8 not UCS4
262 wxCharBuffer buffer = wxConvUTF8.cWX2MB( GetText().c_str() );
263 strcpy( (char*) buf, (const char*) buffer );
264 }
265 else
266 {
267 wxCharBuffer buffer = wxConvLibc.cWX2MB( GetText().c_str() );
268 strcpy( (char*) buf, (const char*) buffer );
269 }
68379eaf
WS
270
271 return true;
c7d6d883
RR
272}
273
274bool wxTextDataObject::SetData(const wxDataFormat& format,
275 size_t WXUNUSED(len), const void *buf)
276{
277 if (format == wxDF_UNICODETEXT)
278 SetText( wxConvUTF8.cMB2WX( (const char*) buf ) );
279 else
280 SetText( wxConvLibc.cMB2WX( (const char*) buf ) );
68379eaf
WS
281
282 return true;
c7d6d883
RR
283}
284
25758297
SC
285#elif wxUSE_UNICODE && defined(__WXMAC__)
286
287size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
288{
289 if (format == wxDF_UNICODETEXT)
290 {
291 // host native is UTF16
68379eaf 292 wxMBConvUTF16BE converter ;
b3370ae5 293 return converter.WC2MB( NULL , GetText().c_str() , 0 ) + 2; // add space for trailing unichar 0
25758297
SC
294 }
295 else // == wxDF_TEXT
296 {
297 wxCharBuffer buffer = wxConvLibc.cWX2MB( GetText().c_str() );
298 return strlen( (const char*) buffer ) + 1;
299 }
300}
301
302bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
303{
304 if (format == wxDF_UNICODETEXT)
305 {
306 // host native is UTF16
68379eaf
WS
307 wxMBConvUTF16BE converter ;
308 size_t len = converter.WC2MB( NULL , GetText().c_str() , 0 ) ;
25758297 309 wxCharBuffer buffer = converter.cWX2MB( GetText().c_str() );
b3370ae5 310 memcpy( (char*) buf, (const char*) buffer , len + 2); // trailing unichar 0
25758297
SC
311 }
312 else
313 {
314 wxCharBuffer buffer = wxConvLibc.cWX2MB( GetText().c_str() );
315 strcpy( (char*) buf, (const char*) buffer );
316 }
68379eaf
WS
317
318 return true;
25758297
SC
319}
320
321bool wxTextDataObject::SetData(const wxDataFormat& format,
322 size_t WXUNUSED(len), const void *buf)
323{
324 if (format == wxDF_UNICODETEXT)
325 {
326 // host native is UTF16
68379eaf 327 wxMBConvUTF16BE converter ;
25758297
SC
328 SetText( converter.cMB2WX( (const char*) buf ) );
329 }
330 else
331 SetText( wxConvLibc.cMB2WX( (const char*) buf ) );
68379eaf
WS
332
333 return true;
25758297
SC
334}
335
6a59178a 336#else
c7d6d883
RR
337
338size_t wxTextDataObject::GetDataSize() const
339{
e1b435af 340 return GetTextLength() * sizeof(wxChar);
3f364be8
VZ
341}
342
343bool wxTextDataObject::GetDataHere(void *buf) const
344{
e1b435af 345 wxStrcpy((wxChar *)buf, GetText().c_str());
3f364be8 346
68379eaf 347 return true;
3f364be8
VZ
348}
349
350bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
351{
e1b435af 352 SetText(wxString((const wxChar *)buf));
3f364be8 353
68379eaf 354 return true;
3f364be8
VZ
355}
356
c7d6d883
RR
357#endif
358
3f364be8
VZ
359// ----------------------------------------------------------------------------
360// wxFileDataObjectBase
361// ----------------------------------------------------------------------------
362
9e2896e5
VZ
363// VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
364// be moved to gtk/dataobj.cpp
365#if 0
366
3f364be8
VZ
367wxString wxFileDataObjectBase::GetFilenames() const
368{
369 wxString str;
370 size_t count = m_filenames.GetCount();
371 for ( size_t n = 0; n < count; n++ )
372 {
373 str << m_filenames[n] << wxT('\0');
374 }
375
376 return str;
377}
378
379void wxFileDataObjectBase::SetFilenames(const wxChar* filenames)
380{
381 m_filenames.Empty();
382
383 wxString current;
384 for ( const wxChar *pc = filenames; ; pc++ )
385 {
386 if ( *pc )
387 {
388 current += *pc;
389 }
390 else
391 {
392 if ( !current )
393 {
394 // 2 consecutive NULs - this is the end of the string
395 break;
396 }
397
398 m_filenames.Add(current);
399 current.Empty();
400 }
401 }
402}
403
9e2896e5
VZ
404#endif // 0
405
3f364be8
VZ
406// ----------------------------------------------------------------------------
407// wxCustomDataObject
408// ----------------------------------------------------------------------------
409
775e1c62
RD
410wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
411 : wxDataObjectSimple(format)
412{
4e16881a 413 m_data = (void *)NULL;
775e1c62
RD
414}
415
3f364be8
VZ
416wxCustomDataObject::~wxCustomDataObject()
417{
418 Free();
419}
420
421void wxCustomDataObject::TakeData(size_t size, void *data)
422{
423 Free();
424
425 m_size = size;
426 m_data = data;
427}
428
429void *wxCustomDataObject::Alloc(size_t size)
430{
431 return (void *)new char[size];
432}
433
434void wxCustomDataObject::Free()
435{
5bcea60e 436 delete [] (char *)m_data;
3f364be8
VZ
437 m_size = 0;
438 m_data = (void *)NULL;
439}
440
441size_t wxCustomDataObject::GetDataSize() const
442{
443 return GetSize();
444}
445
446bool wxCustomDataObject::GetDataHere(void *buf) const
447{
448 void *data = GetData();
449 if ( !data )
68379eaf 450 return false;
3f364be8
VZ
451
452 memcpy(buf, data, GetSize());
453
68379eaf 454 return true;
3f364be8
VZ
455}
456
9e2896e5 457bool wxCustomDataObject::SetData(size_t size, const void *buf)
3f364be8
VZ
458{
459 Free();
460
461 m_data = Alloc(size);
462 if ( !m_data )
68379eaf 463 return false;
3f364be8 464
9e2896e5 465 memcpy(m_data, buf, m_size = size);
3f364be8 466
68379eaf 467 return true;
3f364be8
VZ
468}
469
8ee9d618
VZ
470// ============================================================================
471// some common dnd related code
472// ============================================================================
473
8fb3a512
JS
474#if wxUSE_DRAG_AND_DROP
475
8ee9d618
VZ
476#include "wx/dnd.h"
477
478// ----------------------------------------------------------------------------
479// wxTextDropTarget
480// ----------------------------------------------------------------------------
481
f3ac12aa
VZ
482// NB: we can't use "new" in ctor initializer lists because this provokes an
483// internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
484// so use SetDataObject() instead
485
8ee9d618 486wxTextDropTarget::wxTextDropTarget()
8ee9d618 487{
f3ac12aa 488 SetDataObject(new wxTextDataObject);
8ee9d618
VZ
489}
490
491wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
492{
493 if ( !GetData() )
494 return wxDragNone;
495
496 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
497 return OnDropText(x, y, dobj->GetText()) ? def : wxDragNone;
498}
499
500// ----------------------------------------------------------------------------
501// wxFileDropTarget
502// ----------------------------------------------------------------------------
503
504wxFileDropTarget::wxFileDropTarget()
8ee9d618 505{
f3ac12aa 506 SetDataObject(new wxFileDataObject);
8ee9d618
VZ
507}
508
509wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
510{
511 if ( !GetData() )
512 return wxDragNone;
513
514 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
515 return OnDropFiles(x, y, dobj->GetFilenames()) ? def : wxDragNone;
516}
517
1e6feb95 518#endif // wxUSE_DRAG_AND_DROP
8fb3a512 519
1e6feb95 520#endif // wxUSE_DATAOBJ