]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/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 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
19 | #if wxUSE_DATAOBJ | |
20 | ||
21 | #ifndef WX_PRECOMP | |
22 | #include "wx/app.h" | |
23 | #include "wx/debug.h" | |
24 | #endif | |
25 | ||
26 | #include "wx/dataobj.h" | |
27 | ||
28 | // ---------------------------------------------------------------------------- | |
29 | // lists | |
30 | // ---------------------------------------------------------------------------- | |
31 | ||
32 | #include "wx/listimpl.cpp" | |
33 | ||
34 | WX_DEFINE_LIST(wxSimpleDataObjectList) | |
35 | ||
36 | // ---------------------------------------------------------------------------- | |
37 | // globals | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | static wxDataFormat dataFormatInvalid; | |
41 | WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid; | |
42 | ||
43 | // ============================================================================ | |
44 | // implementation | |
45 | // ============================================================================ | |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // wxDataObjectBase | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | wxDataObjectBase::~wxDataObjectBase() | |
52 | { | |
53 | } | |
54 | ||
55 | bool wxDataObjectBase::IsSupported(const wxDataFormat& format, | |
56 | Direction dir) const | |
57 | { | |
58 | size_t nFormatCount = GetFormatCount( dir ); | |
59 | if ( nFormatCount == 1 ) | |
60 | { | |
61 | return format == GetPreferredFormat( dir ); | |
62 | } | |
63 | else | |
64 | { | |
65 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
66 | GetAllFormats( formats, dir ); | |
67 | ||
68 | size_t n; | |
69 | for ( n = 0; n < nFormatCount; n++ ) | |
70 | { | |
71 | if ( formats[n] == format ) | |
72 | break; | |
73 | } | |
74 | ||
75 | delete [] formats; | |
76 | ||
77 | // found? | |
78 | return n < nFormatCount; | |
79 | } | |
80 | } | |
81 | ||
82 | // ---------------------------------------------------------------------------- | |
83 | // wxDataObjectComposite | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
86 | wxDataObjectComposite::wxDataObjectComposite() | |
87 | { | |
88 | m_preferred = 0; | |
89 | } | |
90 | ||
91 | wxDataObjectComposite::~wxDataObjectComposite() | |
92 | { | |
93 | WX_CLEAR_LIST( wxSimpleDataObjectList, m_dataObjects ); | |
94 | } | |
95 | ||
96 | wxDataObjectSimple * | |
97 | wxDataObjectComposite::GetObject(const wxDataFormat& format) const | |
98 | { | |
99 | wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst(); | |
100 | while ( node ) | |
101 | { | |
102 | wxDataObjectSimple *dataObj = node->GetData(); | |
103 | ||
104 | if ( dataObj->GetFormat() == format ) | |
105 | { | |
106 | return dataObj; | |
107 | } | |
108 | ||
109 | node = node->GetNext(); | |
110 | } | |
111 | ||
112 | return (wxDataObjectSimple *)NULL; | |
113 | } | |
114 | ||
115 | void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred) | |
116 | { | |
117 | if ( preferred ) | |
118 | m_preferred = m_dataObjects.GetCount(); | |
119 | ||
120 | m_dataObjects.Append( dataObject ); | |
121 | } | |
122 | ||
123 | wxDataFormat | |
124 | wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const | |
125 | { | |
126 | wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred ); | |
127 | ||
128 | wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") ); | |
129 | ||
130 | wxDataObjectSimple* dataObj = node->GetData(); | |
131 | ||
132 | return dataObj->GetFormat(); | |
133 | } | |
134 | ||
135 | #if defined(__WXMSW__) | |
136 | ||
137 | size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format ) | |
138 | { | |
139 | wxDataObjectSimple *dataObj = GetObject(format); | |
140 | ||
141 | wxCHECK_MSG( dataObj, 0, | |
142 | wxT("unsupported format in wxDataObjectComposite")); | |
143 | ||
144 | return dataObj->GetBufferOffset( format ); | |
145 | } | |
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, NULL, | |
155 | wxT("unsupported format in wxDataObjectComposite")); | |
156 | ||
157 | return dataObj->GetSizeFromBuffer( buffer, size, format ); | |
158 | } | |
159 | ||
160 | ||
161 | void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size, | |
162 | const wxDataFormat& format ) | |
163 | { | |
164 | wxDataObjectSimple *dataObj = GetObject( format ); | |
165 | ||
166 | wxCHECK_MSG( dataObj, NULL, | |
167 | wxT("unsupported format in wxDataObjectComposite")); | |
168 | ||
169 | return dataObj->SetSizeInBuffer( buffer, size, format ); | |
170 | } | |
171 | ||
172 | #endif | |
173 | ||
174 | size_t wxDataObjectComposite::GetFormatCount(Direction WXUNUSED(dir)) const | |
175 | { | |
176 | // TODO what about the Get/Set only formats? | |
177 | return m_dataObjects.GetCount(); | |
178 | } | |
179 | ||
180 | void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats, | |
181 | Direction WXUNUSED(dir)) const | |
182 | { | |
183 | size_t n = 0; | |
184 | wxSimpleDataObjectList::compatibility_iterator node; | |
185 | for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() ) | |
186 | { | |
187 | // TODO if ( !outputOnlyToo ) && this one counts ... | |
188 | formats[n++] = node->GetData()->GetFormat(); | |
189 | } | |
190 | } | |
191 | ||
192 | size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const | |
193 | { | |
194 | wxDataObjectSimple *dataObj = GetObject(format); | |
195 | ||
196 | wxCHECK_MSG( dataObj, 0, | |
197 | wxT("unsupported format in wxDataObjectComposite")); | |
198 | ||
199 | return dataObj->GetDataSize(); | |
200 | } | |
201 | ||
202 | bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format, | |
203 | void *buf) const | |
204 | { | |
205 | wxDataObjectSimple *dataObj = GetObject( format ); | |
206 | ||
207 | wxCHECK_MSG( dataObj, false, | |
208 | wxT("unsupported format in wxDataObjectComposite")); | |
209 | ||
210 | return dataObj->GetDataHere( buf ); | |
211 | } | |
212 | ||
213 | bool wxDataObjectComposite::SetData(const wxDataFormat& format, | |
214 | size_t len, | |
215 | const void *buf) | |
216 | { | |
217 | wxDataObjectSimple *dataObj = GetObject( format ); | |
218 | ||
219 | wxCHECK_MSG( dataObj, false, | |
220 | wxT("unsupported format in wxDataObjectComposite")); | |
221 | ||
222 | return dataObj->SetData( len, buf ); | |
223 | } | |
224 | ||
225 | // ---------------------------------------------------------------------------- | |
226 | // wxTextDataObject | |
227 | // ---------------------------------------------------------------------------- | |
228 | ||
229 | #if defined(__WXGTK20__) && wxUSE_UNICODE | |
230 | ||
231 | static inline wxMBConv& GetConv(const wxDataFormat& format) | |
232 | { | |
233 | // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT | |
234 | return format == wxDF_UNICODETEXT ? wxConvUTF8 : wxConvLibc; | |
235 | } | |
236 | ||
237 | size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const | |
238 | { | |
239 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); | |
240 | ||
241 | return buffer ? strlen( buffer ) : 0; | |
242 | } | |
243 | ||
244 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
245 | { | |
246 | if ( buf == NULL ) | |
247 | return false; | |
248 | ||
249 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); | |
250 | ||
251 | strcpy( (char*)buf, buffer ); | |
252 | ||
253 | return true; | |
254 | } | |
255 | ||
256 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
257 | size_t WXUNUSED(len), const void *buf) | |
258 | { | |
259 | if ( buf == NULL ) | |
260 | return false; | |
261 | ||
262 | wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf ); | |
263 | ||
264 | SetText( buffer ); | |
265 | ||
266 | return true; | |
267 | } | |
268 | ||
269 | #elif wxUSE_UNICODE && defined(__WXMAC__) | |
270 | ||
271 | static wxMBConvUTF16 sUTF16Converter; | |
272 | ||
273 | static inline wxMBConv& GetConv(const wxDataFormat& format) | |
274 | { | |
275 | return | |
276 | format == wxDF_UNICODETEXT | |
277 | ? (wxMBConv&) sUTF16Converter | |
278 | : (wxMBConv&) wxConvLocal; | |
279 | } | |
280 | ||
281 | size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const | |
282 | { | |
283 | size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 ); | |
284 | len += (format == wxDF_UNICODETEXT ? 2 : 1); | |
285 | ||
286 | return len; | |
287 | } | |
288 | ||
289 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
290 | { | |
291 | if ( buf == NULL ) | |
292 | return false; | |
293 | ||
294 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); | |
295 | ||
296 | size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 ); | |
297 | len += (format == wxDF_UNICODETEXT ? 2 : 1); | |
298 | ||
299 | // trailing (uni)char 0 | |
300 | memcpy( (char*)buf, (const char*)buffer, len ); | |
301 | ||
302 | return true; | |
303 | } | |
304 | ||
305 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
306 | size_t WXUNUSED(len), const void *buf) | |
307 | { | |
308 | if ( buf == NULL ) | |
309 | return false; | |
310 | ||
311 | wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf ); | |
312 | ||
313 | SetText( buffer ); | |
314 | ||
315 | return true; | |
316 | } | |
317 | ||
318 | #else | |
319 | ||
320 | size_t wxTextDataObject::GetDataSize() const | |
321 | { | |
322 | return GetTextLength() * sizeof(wxChar); | |
323 | } | |
324 | ||
325 | bool wxTextDataObject::GetDataHere(void *buf) const | |
326 | { | |
327 | wxStrcpy( (wxChar*)buf, GetText().c_str() ); | |
328 | ||
329 | return true; | |
330 | } | |
331 | ||
332 | bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf) | |
333 | { | |
334 | SetText( wxString((const wxChar*)buf) ); | |
335 | ||
336 | return true; | |
337 | } | |
338 | ||
339 | #endif | |
340 | ||
341 | // ---------------------------------------------------------------------------- | |
342 | // wxFileDataObjectBase | |
343 | // ---------------------------------------------------------------------------- | |
344 | ||
345 | // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should | |
346 | // be moved to gtk/dataobj.cpp | |
347 | #if 0 | |
348 | ||
349 | wxString wxFileDataObjectBase::GetFilenames() const | |
350 | { | |
351 | wxString str; | |
352 | size_t count = m_filenames.GetCount(); | |
353 | for ( size_t n = 0; n < count; n++ ) | |
354 | { | |
355 | str << m_filenames[n] << wxT('\0'); | |
356 | } | |
357 | ||
358 | return str; | |
359 | } | |
360 | ||
361 | void wxFileDataObjectBase::SetFilenames(const wxChar* filenames) | |
362 | { | |
363 | m_filenames.Empty(); | |
364 | ||
365 | wxString current; | |
366 | for ( const wxChar *pc = filenames; ; pc++ ) | |
367 | { | |
368 | if ( *pc ) | |
369 | { | |
370 | current += *pc; | |
371 | } | |
372 | else | |
373 | { | |
374 | if ( !current ) | |
375 | { | |
376 | // 2 consecutive NULs - this is the end of the string | |
377 | break; | |
378 | } | |
379 | ||
380 | m_filenames.Add(current); | |
381 | current.Empty(); | |
382 | } | |
383 | } | |
384 | } | |
385 | ||
386 | #endif | |
387 | ||
388 | // ---------------------------------------------------------------------------- | |
389 | // wxCustomDataObject | |
390 | // ---------------------------------------------------------------------------- | |
391 | ||
392 | wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format) | |
393 | : wxDataObjectSimple(format) | |
394 | { | |
395 | m_data = NULL; | |
396 | m_size = 0; | |
397 | } | |
398 | ||
399 | wxCustomDataObject::~wxCustomDataObject() | |
400 | { | |
401 | Free(); | |
402 | } | |
403 | ||
404 | void wxCustomDataObject::TakeData(size_t size, void *data) | |
405 | { | |
406 | Free(); | |
407 | ||
408 | m_size = size; | |
409 | m_data = data; | |
410 | } | |
411 | ||
412 | void *wxCustomDataObject::Alloc(size_t size) | |
413 | { | |
414 | return (void *)new char[size]; | |
415 | } | |
416 | ||
417 | void wxCustomDataObject::Free() | |
418 | { | |
419 | delete [] (char*)m_data; | |
420 | m_size = 0; | |
421 | m_data = (void*)NULL; | |
422 | } | |
423 | ||
424 | size_t wxCustomDataObject::GetDataSize() const | |
425 | { | |
426 | return GetSize(); | |
427 | } | |
428 | ||
429 | bool wxCustomDataObject::GetDataHere(void *buf) const | |
430 | { | |
431 | if ( buf == NULL ) | |
432 | return false; | |
433 | ||
434 | void *data = GetData(); | |
435 | if ( data == NULL ) | |
436 | return false; | |
437 | ||
438 | memcpy( buf, data, GetSize() ); | |
439 | ||
440 | return true; | |
441 | } | |
442 | ||
443 | bool wxCustomDataObject::SetData(size_t size, const void *buf) | |
444 | { | |
445 | Free(); | |
446 | ||
447 | m_data = Alloc(size); | |
448 | if ( m_data == NULL ) | |
449 | return false; | |
450 | ||
451 | m_size = size; | |
452 | memcpy( m_data, buf, m_size ); | |
453 | ||
454 | return true; | |
455 | } | |
456 | ||
457 | // ============================================================================ | |
458 | // some common dnd related code | |
459 | // ============================================================================ | |
460 | ||
461 | #if wxUSE_DRAG_AND_DROP | |
462 | ||
463 | #include "wx/dnd.h" | |
464 | ||
465 | // ---------------------------------------------------------------------------- | |
466 | // wxTextDropTarget | |
467 | // ---------------------------------------------------------------------------- | |
468 | ||
469 | // NB: we can't use "new" in ctor initializer lists because this provokes an | |
470 | // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!), | |
471 | // so use SetDataObject() instead | |
472 | ||
473 | wxTextDropTarget::wxTextDropTarget() | |
474 | { | |
475 | SetDataObject(new wxTextDataObject); | |
476 | } | |
477 | ||
478 | wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) | |
479 | { | |
480 | if ( !GetData() ) | |
481 | return wxDragNone; | |
482 | ||
483 | wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject; | |
484 | return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone; | |
485 | } | |
486 | ||
487 | // ---------------------------------------------------------------------------- | |
488 | // wxFileDropTarget | |
489 | // ---------------------------------------------------------------------------- | |
490 | ||
491 | wxFileDropTarget::wxFileDropTarget() | |
492 | { | |
493 | SetDataObject(new wxFileDataObject); | |
494 | } | |
495 | ||
496 | wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) | |
497 | { | |
498 | if ( !GetData() ) | |
499 | return wxDragNone; | |
500 | ||
501 | wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject; | |
502 | return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone; | |
503 | } | |
504 | ||
505 | #endif // wxUSE_DRAG_AND_DROP | |
506 | ||
507 | #endif // wxUSE_DATAOBJ |