]> git.saurik.com Git - wxWidgets.git/blob - src/common/dobjcmn.cpp
set error to GSOCK_TIMEOUT if the socket timed out (modified and extended patch 1303554)
[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) wxWidgets Team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_DATAOBJ
27
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
41 WX_DEFINE_LIST(wxSimpleDataObjectList);
42
43 // ----------------------------------------------------------------------------
44 // globals
45 // ----------------------------------------------------------------------------
46
47 static wxDataFormat dataFormatInvalid;
48 WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid;
49
50 // ============================================================================
51 // implementation
52 // ============================================================================
53
54 // ----------------------------------------------------------------------------
55 // wxDataObjectBase
56 // ----------------------------------------------------------------------------
57
58 wxDataObjectBase::~wxDataObjectBase()
59 {
60 }
61
62 bool 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
89 // ----------------------------------------------------------------------------
90 // wxDataObjectComposite
91 // ----------------------------------------------------------------------------
92
93 wxDataObjectComposite::wxDataObjectComposite()
94 {
95 m_preferred = 0;
96 }
97
98 wxDataObjectComposite::~wxDataObjectComposite()
99 {
100 WX_CLEAR_LIST(wxSimpleDataObjectList, m_dataObjects);
101 }
102
103 wxDataObjectSimple *
104 wxDataObjectComposite::GetObject(const wxDataFormat& format) const
105 {
106 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst();
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
122 void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
123 {
124 if ( preferred )
125 m_preferred = m_dataObjects.GetCount();
126
127 m_dataObjects.Append( dataObject );
128 }
129
130 wxDataFormat
131 wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
132 {
133 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred );
134
135 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
136
137 wxDataObjectSimple* dataObj = node->GetData();
138
139 return dataObj->GetFormat();
140 }
141
142 #if defined(__WXMSW__)
143
144 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format )
145 {
146 wxDataObjectSimple *dataObj = GetObject(format);
147
148 wxCHECK_MSG( dataObj, 0,
149 wxT("unsupported format in wxDataObjectComposite"));
150
151 return dataObj->GetBufferOffset( format );
152 }
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
168 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size,
169 const wxDataFormat& format )
170 {
171 wxDataObjectSimple *dataObj = GetObject(format);
172
173 wxCHECK_MSG( dataObj, NULL,
174 wxT("unsupported format in wxDataObjectComposite"));
175
176 return dataObj->SetSizeInBuffer( buffer, size, format );
177 }
178
179 #endif
180
181 size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const
182 {
183 // TODO what about the Get/Set only formats?
184 return m_dataObjects.GetCount();
185 }
186
187 void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
188 Direction WXUNUSED(dir)) const
189 {
190 size_t n = 0;
191 wxSimpleDataObjectList::compatibility_iterator node;
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
199 size_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
209 bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
210 void *buf) const
211 {
212 wxDataObjectSimple *dataObj = GetObject(format);
213
214 wxCHECK_MSG( dataObj, false,
215 wxT("unsupported format in wxDataObjectComposite"));
216
217 return dataObj->GetDataHere(buf);
218 }
219
220 bool wxDataObjectComposite::SetData(const wxDataFormat& format,
221 size_t len,
222 const void *buf)
223 {
224 wxDataObjectSimple *dataObj = GetObject(format);
225
226 wxCHECK_MSG( dataObj, false,
227 wxT("unsupported format in wxDataObjectComposite"));
228
229 return dataObj->SetData(len, buf);
230 }
231
232 // ----------------------------------------------------------------------------
233 // wxTextDataObject
234 // ----------------------------------------------------------------------------
235
236 #if defined(__WXGTK20__) && wxUSE_UNICODE
237
238 static 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
244 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
245 {
246 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
247 return buffer ? strlen(buffer) + 1 : 0;
248 }
249
250 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
251 {
252 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
253 if ( !buffer )
254 return false;
255
256 strcpy( (char*) buf, buffer );
257
258 return true;
259 }
260
261 bool wxTextDataObject::SetData(const wxDataFormat& format,
262 size_t WXUNUSED(len), const void *buf)
263 {
264 wxWCharBuffer buffer = GetConv(format).cMB2WX((const char *)buf);
265 if ( !buffer )
266 return false;
267
268 SetText(buffer);
269
270 return true;
271 }
272
273 #elif wxUSE_UNICODE && defined(__WXMAC__)
274
275 static wxMBConvUTF16 sUTF16Converter ;
276
277 static inline wxMBConv& GetConv(const wxDataFormat& format)
278 {
279 return format == wxDF_UNICODETEXT ? sUTF16Converter : (wxMBConv&) wxConvLocal;
280 }
281
282 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
283 {
284 size_t len = GetConv(format).WC2MB( NULL , GetText().c_str() , 0 )
285 + ( format == wxDF_UNICODETEXT ? 2 : 1 ) ;
286 return len ;
287 }
288
289 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
290 {
291 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
292 if ( !buffer )
293 return false;
294
295 size_t len = GetConv(format).WC2MB( NULL , GetText().c_str() , 0 )
296 + ( format == wxDF_UNICODETEXT ? 2 : 1 ) ;
297
298 memcpy( (char*) buf, (const char*) buffer , len ); // trailing (uni)char 0
299
300 return true;
301 }
302
303 bool wxTextDataObject::SetData(const wxDataFormat& format,
304 size_t WXUNUSED(len), const void *buf)
305 {
306 wxWCharBuffer buffer = GetConv(format).cMB2WX((const char *)buf);
307 if ( !buffer )
308 return false;
309
310 SetText(buffer);
311
312 return true;
313 }
314
315 #else
316
317 size_t wxTextDataObject::GetDataSize() const
318 {
319 return GetTextLength() * sizeof(wxChar);
320 }
321
322 bool wxTextDataObject::GetDataHere(void *buf) const
323 {
324 wxStrcpy((wxChar *)buf, GetText().c_str());
325
326 return true;
327 }
328
329 bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf)
330 {
331 SetText(wxString((const wxChar *)buf));
332
333 return true;
334 }
335
336 #endif
337
338 // ----------------------------------------------------------------------------
339 // wxFileDataObjectBase
340 // ----------------------------------------------------------------------------
341
342 // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should
343 // be moved to gtk/dataobj.cpp
344 #if 0
345
346 wxString wxFileDataObjectBase::GetFilenames() const
347 {
348 wxString str;
349 size_t count = m_filenames.GetCount();
350 for ( size_t n = 0; n < count; n++ )
351 {
352 str << m_filenames[n] << wxT('\0');
353 }
354
355 return str;
356 }
357
358 void wxFileDataObjectBase::SetFilenames(const wxChar* filenames)
359 {
360 m_filenames.Empty();
361
362 wxString current;
363 for ( const wxChar *pc = filenames; ; pc++ )
364 {
365 if ( *pc )
366 {
367 current += *pc;
368 }
369 else
370 {
371 if ( !current )
372 {
373 // 2 consecutive NULs - this is the end of the string
374 break;
375 }
376
377 m_filenames.Add(current);
378 current.Empty();
379 }
380 }
381 }
382
383 #endif // 0
384
385 // ----------------------------------------------------------------------------
386 // wxCustomDataObject
387 // ----------------------------------------------------------------------------
388
389 wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
390 : wxDataObjectSimple(format)
391 {
392 m_data = (void *)NULL;
393 }
394
395 wxCustomDataObject::~wxCustomDataObject()
396 {
397 Free();
398 }
399
400 void wxCustomDataObject::TakeData(size_t size, void *data)
401 {
402 Free();
403
404 m_size = size;
405 m_data = data;
406 }
407
408 void *wxCustomDataObject::Alloc(size_t size)
409 {
410 return (void *)new char[size];
411 }
412
413 void wxCustomDataObject::Free()
414 {
415 delete [] (char *)m_data;
416 m_size = 0;
417 m_data = (void *)NULL;
418 }
419
420 size_t wxCustomDataObject::GetDataSize() const
421 {
422 return GetSize();
423 }
424
425 bool wxCustomDataObject::GetDataHere(void *buf) const
426 {
427 void *data = GetData();
428 if ( !data )
429 return false;
430
431 memcpy(buf, data, GetSize());
432
433 return true;
434 }
435
436 bool wxCustomDataObject::SetData(size_t size, const void *buf)
437 {
438 Free();
439
440 m_data = Alloc(size);
441 if ( !m_data )
442 return false;
443
444 memcpy(m_data, buf, m_size = size);
445
446 return true;
447 }
448
449 // ============================================================================
450 // some common dnd related code
451 // ============================================================================
452
453 #if wxUSE_DRAG_AND_DROP
454
455 #include "wx/dnd.h"
456
457 // ----------------------------------------------------------------------------
458 // wxTextDropTarget
459 // ----------------------------------------------------------------------------
460
461 // NB: we can't use "new" in ctor initializer lists because this provokes an
462 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
463 // so use SetDataObject() instead
464
465 wxTextDropTarget::wxTextDropTarget()
466 {
467 SetDataObject(new wxTextDataObject);
468 }
469
470 wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
471 {
472 if ( !GetData() )
473 return wxDragNone;
474
475 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
476 return OnDropText(x, y, dobj->GetText()) ? def : wxDragNone;
477 }
478
479 // ----------------------------------------------------------------------------
480 // wxFileDropTarget
481 // ----------------------------------------------------------------------------
482
483 wxFileDropTarget::wxFileDropTarget()
484 {
485 SetDataObject(new wxFileDataObject);
486 }
487
488 wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
489 {
490 if ( !GetData() )
491 return wxDragNone;
492
493 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
494 return OnDropFiles(x, y, dobj->GetFilenames()) ? def : wxDragNone;
495 }
496
497 #endif // wxUSE_DRAG_AND_DROP
498
499 #endif // wxUSE_DATAOBJ