]> git.saurik.com Git - wxWidgets.git/blame - src/common/dobjcmn.cpp
Applied patch [ 1382329 ] [msw] SetScrollbar: Set thumbsize before triggering events
[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
3f364be8
VZ
20#include "wx/wxprec.h"
21
22#ifdef __BORLANDC__
23 #pragma hdrstop
24#endif
25
1e6feb95
VZ
26#if wxUSE_DATAOBJ
27
3f364be8
VZ
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
259c43f6 41WX_DEFINE_LIST(wxSimpleDataObjectList)
3f364be8 42
0c2b453f
VZ
43// ----------------------------------------------------------------------------
44// globals
45// ----------------------------------------------------------------------------
46
47static wxDataFormat dataFormatInvalid;
31e78e0c 48WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid;
0c2b453f 49
3f364be8
VZ
50// ============================================================================
51// implementation
52// ============================================================================
53
54// ----------------------------------------------------------------------------
55// wxDataObjectBase
56// ----------------------------------------------------------------------------
57
58wxDataObjectBase::~wxDataObjectBase()
59{
60}
61
d9317fd4
VZ
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
3f364be8
VZ
89// ----------------------------------------------------------------------------
90// wxDataObjectComposite
91// ----------------------------------------------------------------------------
92
e6b01b78
VZ
93wxDataObjectComposite::wxDataObjectComposite()
94{
95 m_preferred = 0;
222ed1d6 96}
e6b01b78 97
222ed1d6
MB
98wxDataObjectComposite::~wxDataObjectComposite()
99{
100 WX_CLEAR_LIST(wxSimpleDataObjectList, m_dataObjects);
e6b01b78
VZ
101}
102
3f364be8
VZ
103wxDataObjectSimple *
104wxDataObjectComposite::GetObject(const wxDataFormat& format) const
105{
222ed1d6 106 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst();
3f364be8
VZ
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{
222ed1d6 133 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred );
3f364be8 134
2ee3ee1b 135 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
3f364be8
VZ
136
137 wxDataObjectSimple* dataObj = node->GetData();
138
139 return dataObj->GetFormat();
140}
141
e1b435af 142#if defined(__WXMSW__)
7d584866 143
e1b435af
MB
144size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format )
145{
146 wxDataObjectSimple *dataObj = GetObject(format);
147
f7f50f49 148 wxCHECK_MSG( dataObj, 0,
e1b435af
MB
149 wxT("unsupported format in wxDataObjectComposite"));
150
151 return dataObj->GetBufferOffset( format );
152}
153
7d584866 154
e1b435af
MB
155const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer,
156 size_t* size,
157 const wxDataFormat& format )
158{
159 wxDataObjectSimple *dataObj = GetObject(format);
160
f7f50f49 161 wxCHECK_MSG( dataObj, NULL,
e1b435af
MB
162 wxT("unsupported format in wxDataObjectComposite"));
163
164 return dataObj->GetSizeFromBuffer( buffer, size, format );
165}
166
7d584866 167
e1b435af
MB
168void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size,
169 const wxDataFormat& format )
170{
171 wxDataObjectSimple *dataObj = GetObject(format);
172
f7f50f49 173 wxCHECK_MSG( dataObj, NULL,
e1b435af
MB
174 wxT("unsupported format in wxDataObjectComposite"));
175
176 return dataObj->SetSizeInBuffer( buffer, size, format );
177}
178
179#endif
180
3f364be8
VZ
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;
222ed1d6 191 wxSimpleDataObjectList::compatibility_iterator node;
3f364be8
VZ
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
68379eaf 214 wxCHECK_MSG( dataObj, false,
3f364be8
VZ
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
68379eaf 226 wxCHECK_MSG( dataObj, false,
3f364be8
VZ
227 wxT("unsupported format in wxDataObjectComposite"));
228
229 return dataObj->SetData(len, buf);
230}
231
232// ----------------------------------------------------------------------------
233// wxTextDataObject
234// ----------------------------------------------------------------------------
235
6a59178a 236#if defined(__WXGTK20__) && wxUSE_UNICODE
c7d6d883 237
f4370741
VZ
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
c7d6d883
RR
244size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
245{
f4370741
VZ
246 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
247 return buffer ? strlen(buffer) + 1 : 0;
c7d6d883
RR
248}
249
250bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
251{
f4370741
VZ
252 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
253 if ( !buffer )
254 return false;
255
256 strcpy( (char*) buf, buffer );
68379eaf
WS
257
258 return true;
c7d6d883
RR
259}
260
261bool wxTextDataObject::SetData(const wxDataFormat& format,
262 size_t WXUNUSED(len), const void *buf)
263{
f4370741
VZ
264 wxWCharBuffer buffer = GetConv(format).cMB2WX((const char *)buf);
265 if ( !buffer )
266 return false;
267
268 SetText(buffer);
68379eaf
WS
269
270 return true;
c7d6d883
RR
271}
272
25758297
SC
273#elif wxUSE_UNICODE && defined(__WXMAC__)
274
671478ee
SC
275static wxMBConvUTF16 sUTF16Converter ;
276
277static inline wxMBConv& GetConv(const wxDataFormat& format)
278{
d1dbe757
VZ
279 return format == wxDF_UNICODETEXT ? (wxMBConv&) sUTF16Converter
280 : (wxMBConv&) wxConvLocal;
671478ee
SC
281}
282
25758297
SC
283size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
284{
671478ee
SC
285 size_t len = GetConv(format).WC2MB( NULL , GetText().c_str() , 0 )
286 + ( format == wxDF_UNICODETEXT ? 2 : 1 ) ;
287 return len ;
25758297
SC
288}
289
290bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
291{
671478ee
SC
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
68379eaf
WS
300
301 return true;
25758297
SC
302}
303
304bool wxTextDataObject::SetData(const wxDataFormat& format,
305 size_t WXUNUSED(len), const void *buf)
306{
671478ee
SC
307 wxWCharBuffer buffer = GetConv(format).cMB2WX((const char *)buf);
308 if ( !buffer )
309 return false;
310
311 SetText(buffer);
312
68379eaf 313 return true;
25758297
SC
314}
315
6a59178a 316#else
c7d6d883
RR
317
318size_t wxTextDataObject::GetDataSize() const
319{
e1b435af 320 return GetTextLength() * sizeof(wxChar);
3f364be8
VZ
321}
322
323bool wxTextDataObject::GetDataHere(void *buf) const
324{
e1b435af 325 wxStrcpy((wxChar *)buf, GetText().c_str());
3f364be8 326
68379eaf 327 return true;
3f364be8
VZ
328}
329
330bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
331{
e1b435af 332 SetText(wxString((const wxChar *)buf));
3f364be8 333
68379eaf 334 return true;
3f364be8
VZ
335}
336
c7d6d883
RR
337#endif
338
3f364be8
VZ
339// ----------------------------------------------------------------------------
340// wxFileDataObjectBase
341// ----------------------------------------------------------------------------
342
9e2896e5
VZ
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
3f364be8
VZ
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
9e2896e5
VZ
384#endif // 0
385
3f364be8
VZ
386// ----------------------------------------------------------------------------
387// wxCustomDataObject
388// ----------------------------------------------------------------------------
389
775e1c62
RD
390wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
391 : wxDataObjectSimple(format)
392{
4e16881a 393 m_data = (void *)NULL;
775e1c62
RD
394}
395
3f364be8
VZ
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{
5bcea60e 416 delete [] (char *)m_data;
3f364be8
VZ
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 )
68379eaf 430 return false;
3f364be8
VZ
431
432 memcpy(buf, data, GetSize());
433
68379eaf 434 return true;
3f364be8
VZ
435}
436
9e2896e5 437bool wxCustomDataObject::SetData(size_t size, const void *buf)
3f364be8
VZ
438{
439 Free();
440
441 m_data = Alloc(size);
442 if ( !m_data )
68379eaf 443 return false;
3f364be8 444
9e2896e5 445 memcpy(m_data, buf, m_size = size);
3f364be8 446
68379eaf 447 return true;
3f364be8
VZ
448}
449
8ee9d618
VZ
450// ============================================================================
451// some common dnd related code
452// ============================================================================
453
8fb3a512
JS
454#if wxUSE_DRAG_AND_DROP
455
8ee9d618
VZ
456#include "wx/dnd.h"
457
458// ----------------------------------------------------------------------------
459// wxTextDropTarget
460// ----------------------------------------------------------------------------
461
f3ac12aa
VZ
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
8ee9d618 466wxTextDropTarget::wxTextDropTarget()
8ee9d618 467{
f3ac12aa 468 SetDataObject(new wxTextDataObject);
8ee9d618
VZ
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()
8ee9d618 485{
f3ac12aa 486 SetDataObject(new wxFileDataObject);
8ee9d618
VZ
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
1e6feb95 498#endif // wxUSE_DRAG_AND_DROP
8fb3a512 499
1e6feb95 500#endif // wxUSE_DATAOBJ