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