]> git.saurik.com Git - wxWidgets.git/blob - src/common/dobjcmn.cpp
warning in Unicode compilation fixed
[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 wxDataObjectSimple *
98 wxDataObjectComposite::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
116 void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
117 {
118 if ( preferred )
119 m_preferred = m_dataObjects.GetCount();
120
121 m_dataObjects.Append( dataObject );
122 }
123
124 wxDataFormat
125 wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
126 {
127 wxSimpleDataObjectList::Node *node = m_dataObjects.Item( m_preferred );
128
129 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
130
131 wxDataObjectSimple* dataObj = node->GetData();
132
133 return dataObj->GetFormat();
134 }
135
136 #if defined(__WXMSW__)
137
138 size_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
148 const 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
160 void* 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
173 size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const
174 {
175 // TODO what about the Get/Set only formats?
176 return m_dataObjects.GetCount();
177 }
178
179 void 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
191 size_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
201 bool 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
212 bool 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
228 size_t wxTextDataObject::GetDataSize() const
229 {
230 return GetTextLength() * sizeof(wxChar);
231 }
232
233 bool wxTextDataObject::GetDataHere(void *buf) const
234 {
235 wxStrcpy((wxChar *)buf, GetText().c_str());
236
237 return TRUE;
238 }
239
240 bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
241 {
242 SetText(wxString((const wxChar *)buf));
243
244 return TRUE;
245 }
246
247 // ----------------------------------------------------------------------------
248 // wxFileDataObjectBase
249 // ----------------------------------------------------------------------------
250
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
255 wxString 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
267 void 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
292 #endif // 0
293
294 // ----------------------------------------------------------------------------
295 // wxCustomDataObject
296 // ----------------------------------------------------------------------------
297
298 wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
299 : wxDataObjectSimple(format)
300 {
301 m_data = (void *)NULL;
302 }
303
304 wxCustomDataObject::~wxCustomDataObject()
305 {
306 Free();
307 }
308
309 void wxCustomDataObject::TakeData(size_t size, void *data)
310 {
311 Free();
312
313 m_size = size;
314 m_data = data;
315 }
316
317 void *wxCustomDataObject::Alloc(size_t size)
318 {
319 return (void *)new char[size];
320 }
321
322 void wxCustomDataObject::Free()
323 {
324 delete [] (char *)m_data;
325 m_size = 0;
326 m_data = (void *)NULL;
327 }
328
329 size_t wxCustomDataObject::GetDataSize() const
330 {
331 return GetSize();
332 }
333
334 bool 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
345 bool wxCustomDataObject::SetData(size_t size, const void *buf)
346 {
347 Free();
348
349 m_data = Alloc(size);
350 if ( !m_data )
351 return FALSE;
352
353 memcpy(m_data, buf, m_size = size);
354
355 return TRUE;
356 }
357
358 // ============================================================================
359 // some common dnd related code
360 // ============================================================================
361
362 #if wxUSE_DRAG_AND_DROP
363
364 #include "wx/dnd.h"
365
366 // ----------------------------------------------------------------------------
367 // wxTextDropTarget
368 // ----------------------------------------------------------------------------
369
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
374 wxTextDropTarget::wxTextDropTarget()
375 {
376 SetDataObject(new wxTextDataObject);
377 }
378
379 wxDragResult 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
392 wxFileDropTarget::wxFileDropTarget()
393 {
394 SetDataObject(new wxFileDataObject);
395 }
396
397 wxDragResult 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
406 #endif // wxUSE_DRAG_AND_DROP
407
408 #endif // wxUSE_DATAOBJ