]> git.saurik.com Git - wxWidgets.git/blame - src/common/dobjcmn.cpp
added wxSockAddress copy ctor which, in particular, fixes Clone() and fixed its opera...
[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$
8// Copyright: (c) wxWindows Team
9// Licence: wxWindows license
10///////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
20#ifdef __GNUG__
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
97wxDataObjectSimple *
98wxDataObjectComposite::GetObject(const wxDataFormat& format) const
99{
100 wxSimpleDataObjectList::Node *node = m_dataObjects.GetFirst();
101 while ( node )
102 {
103 wxDataObjectSimple *dataObj = node->GetData();
104
105 if ( dataObj->GetFormat() == format )
106 {
107 return dataObj;
108 }
109
110 node = node->GetNext();
111 }
112
113 return (wxDataObjectSimple *)NULL;
114}
115
116void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
117{
118 if ( preferred )
119 m_preferred = m_dataObjects.GetCount();
120
121 m_dataObjects.Append( dataObject );
122}
123
124wxDataFormat
125wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
126{
127 wxSimpleDataObjectList::Node *node = m_dataObjects.Item( m_preferred );
128
2ee3ee1b 129 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
3f364be8
VZ
130
131 wxDataObjectSimple* dataObj = node->GetData();
132
133 return dataObj->GetFormat();
134}
135
e1b435af
MB
136#if defined(__WXMSW__)
137
138size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format )
139{
140 wxDataObjectSimple *dataObj = GetObject(format);
141
142 wxCHECK_MSG( dataObj, FALSE,
143 wxT("unsupported format in wxDataObjectComposite"));
144
145 return dataObj->GetBufferOffset( format );
146}
147
148const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer,
149 size_t* size,
150 const wxDataFormat& format )
151{
152 wxDataObjectSimple *dataObj = GetObject(format);
153
154 wxCHECK_MSG( dataObj, FALSE,
155 wxT("unsupported format in wxDataObjectComposite"));
156
157 return dataObj->GetSizeFromBuffer( buffer, size, format );
158}
159
160void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size,
161 const wxDataFormat& format )
162{
163 wxDataObjectSimple *dataObj = GetObject(format);
164
165 wxCHECK_MSG( dataObj, FALSE,
166 wxT("unsupported format in wxDataObjectComposite"));
167
168 return dataObj->SetSizeInBuffer( buffer, size, format );
169}
170
171#endif
172
3f364be8
VZ
173size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const
174{
175 // TODO what about the Get/Set only formats?
176 return m_dataObjects.GetCount();
177}
178
179void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
180 Direction WXUNUSED(dir)) const
181{
182 size_t n = 0;
183 wxSimpleDataObjectList::Node *node;
184 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
185 {
186 // TODO if ( !outputOnlyToo ) && this one counts ...
187 formats[n++] = node->GetData()->GetFormat();
188 }
189}
190
191size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const
192{
193 wxDataObjectSimple *dataObj = GetObject(format);
194
195 wxCHECK_MSG( dataObj, 0,
196 wxT("unsupported format in wxDataObjectComposite"));
197
198 return dataObj->GetDataSize();
199}
200
201bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
202 void *buf) const
203{
204 wxDataObjectSimple *dataObj = GetObject(format);
205
206 wxCHECK_MSG( dataObj, FALSE,
207 wxT("unsupported format in wxDataObjectComposite"));
208
209 return dataObj->GetDataHere(buf);
210}
211
212bool wxDataObjectComposite::SetData(const wxDataFormat& format,
213 size_t len,
214 const void *buf)
215{
216 wxDataObjectSimple *dataObj = GetObject(format);
217
218 wxCHECK_MSG( dataObj, FALSE,
219 wxT("unsupported format in wxDataObjectComposite"));
220
221 return dataObj->SetData(len, buf);
222}
223
224// ----------------------------------------------------------------------------
225// wxTextDataObject
226// ----------------------------------------------------------------------------
227
228size_t wxTextDataObject::GetDataSize() const
229{
e1b435af 230 return GetTextLength() * sizeof(wxChar);
3f364be8
VZ
231}
232
233bool wxTextDataObject::GetDataHere(void *buf) const
234{
e1b435af 235 wxStrcpy((wxChar *)buf, GetText().c_str());
3f364be8
VZ
236
237 return TRUE;
238}
239
240bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
241{
e1b435af 242 SetText(wxString((const wxChar *)buf));
3f364be8
VZ
243
244 return TRUE;
245}
246
247// ----------------------------------------------------------------------------
248// wxFileDataObjectBase
249// ----------------------------------------------------------------------------
250
9e2896e5
VZ
251// VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
252// be moved to gtk/dataobj.cpp
253#if 0
254
3f364be8
VZ
255wxString wxFileDataObjectBase::GetFilenames() const
256{
257 wxString str;
258 size_t count = m_filenames.GetCount();
259 for ( size_t n = 0; n < count; n++ )
260 {
261 str << m_filenames[n] << wxT('\0');
262 }
263
264 return str;
265}
266
267void wxFileDataObjectBase::SetFilenames(const wxChar* filenames)
268{
269 m_filenames.Empty();
270
271 wxString current;
272 for ( const wxChar *pc = filenames; ; pc++ )
273 {
274 if ( *pc )
275 {
276 current += *pc;
277 }
278 else
279 {
280 if ( !current )
281 {
282 // 2 consecutive NULs - this is the end of the string
283 break;
284 }
285
286 m_filenames.Add(current);
287 current.Empty();
288 }
289 }
290}
291
9e2896e5
VZ
292#endif // 0
293
3f364be8
VZ
294// ----------------------------------------------------------------------------
295// wxCustomDataObject
296// ----------------------------------------------------------------------------
297
775e1c62
RD
298wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
299 : wxDataObjectSimple(format)
300{
4e16881a 301 m_data = (void *)NULL;
775e1c62
RD
302}
303
3f364be8
VZ
304wxCustomDataObject::~wxCustomDataObject()
305{
306 Free();
307}
308
309void wxCustomDataObject::TakeData(size_t size, void *data)
310{
311 Free();
312
313 m_size = size;
314 m_data = data;
315}
316
317void *wxCustomDataObject::Alloc(size_t size)
318{
319 return (void *)new char[size];
320}
321
322void wxCustomDataObject::Free()
323{
5bcea60e 324 delete [] (char *)m_data;
3f364be8
VZ
325 m_size = 0;
326 m_data = (void *)NULL;
327}
328
329size_t wxCustomDataObject::GetDataSize() const
330{
331 return GetSize();
332}
333
334bool wxCustomDataObject::GetDataHere(void *buf) const
335{
336 void *data = GetData();
337 if ( !data )
338 return FALSE;
339
340 memcpy(buf, data, GetSize());
341
342 return TRUE;
343}
344
9e2896e5 345bool wxCustomDataObject::SetData(size_t size, const void *buf)
3f364be8
VZ
346{
347 Free();
348
349 m_data = Alloc(size);
350 if ( !m_data )
351 return FALSE;
352
9e2896e5 353 memcpy(m_data, buf, m_size = size);
3f364be8
VZ
354
355 return TRUE;
356}
357
8ee9d618
VZ
358// ============================================================================
359// some common dnd related code
360// ============================================================================
361
8fb3a512
JS
362#if wxUSE_DRAG_AND_DROP
363
8ee9d618
VZ
364#include "wx/dnd.h"
365
366// ----------------------------------------------------------------------------
367// wxTextDropTarget
368// ----------------------------------------------------------------------------
369
f3ac12aa
VZ
370// NB: we can't use "new" in ctor initializer lists because this provokes an
371// internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
372// so use SetDataObject() instead
373
8ee9d618 374wxTextDropTarget::wxTextDropTarget()
8ee9d618 375{
f3ac12aa 376 SetDataObject(new wxTextDataObject);
8ee9d618
VZ
377}
378
379wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
380{
381 if ( !GetData() )
382 return wxDragNone;
383
384 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
385 return OnDropText(x, y, dobj->GetText()) ? def : wxDragNone;
386}
387
388// ----------------------------------------------------------------------------
389// wxFileDropTarget
390// ----------------------------------------------------------------------------
391
392wxFileDropTarget::wxFileDropTarget()
8ee9d618 393{
f3ac12aa 394 SetDataObject(new wxFileDataObject);
8ee9d618
VZ
395}
396
397wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
398{
399 if ( !GetData() )
400 return wxDragNone;
401
402 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
403 return OnDropFiles(x, y, dobj->GetFilenames()) ? def : wxDragNone;
404}
405
1e6feb95 406#endif // wxUSE_DRAG_AND_DROP
8fb3a512 407
1e6feb95 408#endif // wxUSE_DATAOBJ