]> git.saurik.com Git - wxWidgets.git/blob - src/common/dobjcmn.cpp
Fixed my sloppy fix for redundant path separators
[wxWidgets.git] / src / common / dobjcmn.cpp
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
30 #if wxUSE_DATAOBJ
31
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
45 WX_DEFINE_LIST(wxSimpleDataObjectList);
46
47 // ----------------------------------------------------------------------------
48 // globals
49 // ----------------------------------------------------------------------------
50
51 static wxDataFormat dataFormatInvalid;
52 WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid;
53
54 // ============================================================================
55 // implementation
56 // ============================================================================
57
58 // ----------------------------------------------------------------------------
59 // wxDataObjectBase
60 // ----------------------------------------------------------------------------
61
62 wxDataObjectBase::~wxDataObjectBase()
63 {
64 }
65
66 bool 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
93 // ----------------------------------------------------------------------------
94 // wxDataObjectComposite
95 // ----------------------------------------------------------------------------
96
97 wxDataObjectComposite::wxDataObjectComposite()
98 {
99 m_preferred = 0;
100
101 m_dataObjects.DeleteContents(TRUE);
102 }
103
104 wxDataObjectSimple *
105 wxDataObjectComposite::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
123 void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
124 {
125 if ( preferred )
126 m_preferred = m_dataObjects.GetCount();
127
128 m_dataObjects.Append( dataObject );
129 }
130
131 wxDataFormat
132 wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
133 {
134 wxSimpleDataObjectList::Node *node = m_dataObjects.Item( m_preferred );
135
136 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
137
138 wxDataObjectSimple* dataObj = node->GetData();
139
140 return dataObj->GetFormat();
141 }
142
143 #if defined(__WXMSW__)
144
145 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format )
146 {
147 wxDataObjectSimple *dataObj = GetObject(format);
148
149 wxCHECK_MSG( dataObj, 0,
150 wxT("unsupported format in wxDataObjectComposite"));
151
152 return dataObj->GetBufferOffset( format );
153 }
154
155 const 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, NULL,
162 wxT("unsupported format in wxDataObjectComposite"));
163
164 return dataObj->GetSizeFromBuffer( buffer, size, format );
165 }
166
167 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size,
168 const wxDataFormat& format )
169 {
170 wxDataObjectSimple *dataObj = GetObject(format);
171
172 wxCHECK_MSG( dataObj, NULL,
173 wxT("unsupported format in wxDataObjectComposite"));
174
175 return dataObj->SetSizeInBuffer( buffer, size, format );
176 }
177
178 #endif
179
180 size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const
181 {
182 // TODO what about the Get/Set only formats?
183 return m_dataObjects.GetCount();
184 }
185
186 void 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
198 size_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
208 bool 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
219 bool 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
235 size_t wxTextDataObject::GetDataSize() const
236 {
237 return GetTextLength() * sizeof(wxChar);
238 }
239
240 bool wxTextDataObject::GetDataHere(void *buf) const
241 {
242 wxStrcpy((wxChar *)buf, GetText().c_str());
243
244 return TRUE;
245 }
246
247 bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
248 {
249 SetText(wxString((const wxChar *)buf));
250
251 return TRUE;
252 }
253
254 // ----------------------------------------------------------------------------
255 // wxFileDataObjectBase
256 // ----------------------------------------------------------------------------
257
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
262 wxString 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
274 void 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
299 #endif // 0
300
301 // ----------------------------------------------------------------------------
302 // wxCustomDataObject
303 // ----------------------------------------------------------------------------
304
305 wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
306 : wxDataObjectSimple(format)
307 {
308 m_data = (void *)NULL;
309 }
310
311 wxCustomDataObject::~wxCustomDataObject()
312 {
313 Free();
314 }
315
316 void wxCustomDataObject::TakeData(size_t size, void *data)
317 {
318 Free();
319
320 m_size = size;
321 m_data = data;
322 }
323
324 void *wxCustomDataObject::Alloc(size_t size)
325 {
326 return (void *)new char[size];
327 }
328
329 void wxCustomDataObject::Free()
330 {
331 delete [] (char *)m_data;
332 m_size = 0;
333 m_data = (void *)NULL;
334 }
335
336 size_t wxCustomDataObject::GetDataSize() const
337 {
338 return GetSize();
339 }
340
341 bool 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
352 bool wxCustomDataObject::SetData(size_t size, const void *buf)
353 {
354 Free();
355
356 m_data = Alloc(size);
357 if ( !m_data )
358 return FALSE;
359
360 memcpy(m_data, buf, m_size = size);
361
362 return TRUE;
363 }
364
365 // ============================================================================
366 // some common dnd related code
367 // ============================================================================
368
369 #if wxUSE_DRAG_AND_DROP
370
371 #include "wx/dnd.h"
372
373 // ----------------------------------------------------------------------------
374 // wxTextDropTarget
375 // ----------------------------------------------------------------------------
376
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
381 wxTextDropTarget::wxTextDropTarget()
382 {
383 SetDataObject(new wxTextDataObject);
384 }
385
386 wxDragResult 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
399 wxFileDropTarget::wxFileDropTarget()
400 {
401 SetDataObject(new wxFileDataObject);
402 }
403
404 wxDragResult 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
413 #endif // wxUSE_DRAG_AND_DROP
414
415 #endif // wxUSE_DATAOBJ