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