]>
Commit | Line | Data |
---|---|---|
3f364be8 | 1 | /////////////////////////////////////////////////////////////////////////////// |
8b445796 | 2 | // Name: src/common/dobjcmn.cpp |
3f364be8 VZ |
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$ | |
77ffb593 | 8 | // Copyright: (c) wxWidgets Team |
65571936 | 9 | // Licence: wxWindows licence |
3f364be8 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
8b445796 | 12 | // For compilers that support precompilation, includes "wx.h". |
3f364be8 VZ |
13 | #include "wx/wxprec.h" |
14 | ||
15 | #ifdef __BORLANDC__ | |
16 | #pragma hdrstop | |
17 | #endif | |
18 | ||
1e6feb95 VZ |
19 | #if wxUSE_DATAOBJ |
20 | ||
3f364be8 VZ |
21 | #ifndef WX_PRECOMP |
22 | #include "wx/app.h" | |
23 | #include "wx/debug.h" | |
8b445796 | 24 | #endif |
3f364be8 VZ |
25 | |
26 | #include "wx/dataobj.h" | |
27 | ||
28 | // ---------------------------------------------------------------------------- | |
29 | // lists | |
30 | // ---------------------------------------------------------------------------- | |
31 | ||
32 | #include "wx/listimpl.cpp" | |
33 | ||
259c43f6 | 34 | WX_DEFINE_LIST(wxSimpleDataObjectList) |
3f364be8 | 35 | |
0c2b453f VZ |
36 | // ---------------------------------------------------------------------------- |
37 | // globals | |
38 | // ---------------------------------------------------------------------------- | |
39 | ||
40 | static wxDataFormat dataFormatInvalid; | |
31e78e0c | 41 | WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid; |
0c2b453f | 42 | |
3f364be8 VZ |
43 | // ============================================================================ |
44 | // implementation | |
45 | // ============================================================================ | |
46 | ||
47 | // ---------------------------------------------------------------------------- | |
48 | // wxDataObjectBase | |
49 | // ---------------------------------------------------------------------------- | |
50 | ||
51 | wxDataObjectBase::~wxDataObjectBase() | |
52 | { | |
53 | } | |
54 | ||
d9317fd4 VZ |
55 | bool wxDataObjectBase::IsSupported(const wxDataFormat& format, |
56 | Direction dir) const | |
57 | { | |
8b445796 | 58 | size_t nFormatCount = GetFormatCount( dir ); |
d9317fd4 VZ |
59 | if ( nFormatCount == 1 ) |
60 | { | |
8b445796 | 61 | return format == GetPreferredFormat( dir ); |
d9317fd4 VZ |
62 | } |
63 | else | |
64 | { | |
65 | wxDataFormat *formats = new wxDataFormat[nFormatCount]; | |
8b445796 | 66 | GetAllFormats( formats, dir ); |
d9317fd4 VZ |
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 | ||
3f364be8 VZ |
82 | // ---------------------------------------------------------------------------- |
83 | // wxDataObjectComposite | |
84 | // ---------------------------------------------------------------------------- | |
85 | ||
e6b01b78 VZ |
86 | wxDataObjectComposite::wxDataObjectComposite() |
87 | { | |
88 | m_preferred = 0; | |
c072c757 | 89 | m_receivedFormat = wxFormatInvalid; |
222ed1d6 | 90 | } |
e6b01b78 | 91 | |
222ed1d6 MB |
92 | wxDataObjectComposite::~wxDataObjectComposite() |
93 | { | |
8b445796 | 94 | WX_CLEAR_LIST( wxSimpleDataObjectList, m_dataObjects ); |
e6b01b78 VZ |
95 | } |
96 | ||
3f364be8 VZ |
97 | wxDataObjectSimple * |
98 | wxDataObjectComposite::GetObject(const wxDataFormat& format) const | |
99 | { | |
222ed1d6 | 100 | wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst(); |
3f364be8 VZ |
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 | ||
c072c757 RD |
124 | wxDataFormat wxDataObjectComposite::GetReceivedFormat() const |
125 | { | |
126 | return m_receivedFormat; | |
127 | } | |
128 | ||
3f364be8 VZ |
129 | wxDataFormat |
130 | wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const | |
131 | { | |
222ed1d6 | 132 | wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred ); |
3f364be8 | 133 | |
2ee3ee1b | 134 | wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") ); |
3f364be8 VZ |
135 | |
136 | wxDataObjectSimple* dataObj = node->GetData(); | |
137 | ||
138 | return dataObj->GetFormat(); | |
139 | } | |
140 | ||
e1b435af | 141 | #if defined(__WXMSW__) |
7d584866 | 142 | |
e1b435af MB |
143 | size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format ) |
144 | { | |
145 | wxDataObjectSimple *dataObj = GetObject(format); | |
146 | ||
f7f50f49 | 147 | wxCHECK_MSG( dataObj, 0, |
e1b435af MB |
148 | wxT("unsupported format in wxDataObjectComposite")); |
149 | ||
150 | return dataObj->GetBufferOffset( format ); | |
151 | } | |
152 | ||
7d584866 | 153 | |
e1b435af MB |
154 | const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer, |
155 | size_t* size, | |
156 | const wxDataFormat& format ) | |
157 | { | |
158 | wxDataObjectSimple *dataObj = GetObject(format); | |
159 | ||
f7f50f49 | 160 | wxCHECK_MSG( dataObj, NULL, |
e1b435af MB |
161 | wxT("unsupported format in wxDataObjectComposite")); |
162 | ||
163 | return dataObj->GetSizeFromBuffer( buffer, size, format ); | |
164 | } | |
165 | ||
7d584866 | 166 | |
e1b435af MB |
167 | void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size, |
168 | const wxDataFormat& format ) | |
169 | { | |
8b445796 | 170 | wxDataObjectSimple *dataObj = GetObject( format ); |
e1b435af | 171 | |
f7f50f49 | 172 | wxCHECK_MSG( dataObj, NULL, |
e1b435af MB |
173 | wxT("unsupported format in wxDataObjectComposite")); |
174 | ||
175 | return dataObj->SetSizeInBuffer( buffer, size, format ); | |
176 | } | |
177 | ||
178 | #endif | |
179 | ||
3f364be8 VZ |
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; | |
222ed1d6 | 190 | wxSimpleDataObjectList::compatibility_iterator node; |
3f364be8 VZ |
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 | { | |
8b445796 | 211 | wxDataObjectSimple *dataObj = GetObject( format ); |
3f364be8 | 212 | |
68379eaf | 213 | wxCHECK_MSG( dataObj, false, |
3f364be8 VZ |
214 | wxT("unsupported format in wxDataObjectComposite")); |
215 | ||
8b445796 | 216 | return dataObj->GetDataHere( buf ); |
3f364be8 VZ |
217 | } |
218 | ||
219 | bool wxDataObjectComposite::SetData(const wxDataFormat& format, | |
220 | size_t len, | |
221 | const void *buf) | |
222 | { | |
8b445796 | 223 | wxDataObjectSimple *dataObj = GetObject( format ); |
3f364be8 | 224 | |
68379eaf | 225 | wxCHECK_MSG( dataObj, false, |
3f364be8 VZ |
226 | wxT("unsupported format in wxDataObjectComposite")); |
227 | ||
c072c757 | 228 | m_receivedFormat = format; |
8b445796 | 229 | return dataObj->SetData( len, buf ); |
3f364be8 VZ |
230 | } |
231 | ||
232 | // ---------------------------------------------------------------------------- | |
233 | // wxTextDataObject | |
234 | // ---------------------------------------------------------------------------- | |
235 | ||
6a59178a | 236 | #if defined(__WXGTK20__) && wxUSE_UNICODE |
c7d6d883 | 237 | |
f4370741 VZ |
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 | ||
c7d6d883 RR |
244 | size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const |
245 | { | |
f4370741 | 246 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); |
8b445796 | 247 | |
2c906a49 | 248 | return buffer ? strlen( buffer ) : 0; |
c7d6d883 RR |
249 | } |
250 | ||
251 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
252 | { | |
f7570f57 | 253 | if ( !buf ) |
8b445796 DS |
254 | return false; |
255 | ||
f4370741 | 256 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); |
f7570f57 RR |
257 | if ( !buffer ) |
258 | return false; | |
f4370741 | 259 | |
f7570f57 RR |
260 | memcpy( (char*) buf, buffer, GetDataSize(format) ); |
261 | // strcpy( (char*) buf, buffer ); | |
68379eaf WS |
262 | |
263 | return true; | |
c7d6d883 RR |
264 | } |
265 | ||
266 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
267 | size_t WXUNUSED(len), const void *buf) | |
268 | { | |
8b445796 | 269 | if ( buf == NULL ) |
f4370741 VZ |
270 | return false; |
271 | ||
8b445796 | 272 | wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf ); |
8b445796 DS |
273 | |
274 | SetText( buffer ); | |
68379eaf WS |
275 | |
276 | return true; | |
c7d6d883 RR |
277 | } |
278 | ||
25758297 SC |
279 | #elif wxUSE_UNICODE && defined(__WXMAC__) |
280 | ||
8b445796 | 281 | static wxMBConvUTF16 sUTF16Converter; |
671478ee SC |
282 | |
283 | static inline wxMBConv& GetConv(const wxDataFormat& format) | |
284 | { | |
8b445796 DS |
285 | return |
286 | format == wxDF_UNICODETEXT | |
287 | ? (wxMBConv&) sUTF16Converter | |
288 | : (wxMBConv&) wxConvLocal; | |
671478ee SC |
289 | } |
290 | ||
25758297 SC |
291 | size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const |
292 | { | |
8b445796 DS |
293 | size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 ); |
294 | len += (format == wxDF_UNICODETEXT ? 2 : 1); | |
295 | ||
296 | return len; | |
25758297 SC |
297 | } |
298 | ||
299 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
300 | { | |
8b445796 | 301 | if ( buf == NULL ) |
671478ee SC |
302 | return false; |
303 | ||
8b445796 | 304 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); |
671478ee | 305 | |
8b445796 DS |
306 | size_t len = GetConv(format).WC2MB( NULL, GetText().c_str(), 0 ); |
307 | len += (format == wxDF_UNICODETEXT ? 2 : 1); | |
308 | ||
309 | // trailing (uni)char 0 | |
310 | memcpy( (char*)buf, (const char*)buffer, len ); | |
68379eaf WS |
311 | |
312 | return true; | |
25758297 SC |
313 | } |
314 | ||
315 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
316 | size_t WXUNUSED(len), const void *buf) | |
317 | { | |
8b445796 | 318 | if ( buf == NULL ) |
671478ee | 319 | return false; |
8b445796 DS |
320 | |
321 | wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf ); | |
322 | ||
323 | SetText( buffer ); | |
324 | ||
68379eaf | 325 | return true; |
25758297 SC |
326 | } |
327 | ||
6a59178a | 328 | #else |
c7d6d883 RR |
329 | |
330 | size_t wxTextDataObject::GetDataSize() const | |
331 | { | |
e1b435af | 332 | return GetTextLength() * sizeof(wxChar); |
3f364be8 VZ |
333 | } |
334 | ||
335 | bool wxTextDataObject::GetDataHere(void *buf) const | |
336 | { | |
8b445796 | 337 | wxStrcpy( (wxChar*)buf, GetText().c_str() ); |
3f364be8 | 338 | |
68379eaf | 339 | return true; |
3f364be8 VZ |
340 | } |
341 | ||
342 | bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf) | |
343 | { | |
8b445796 | 344 | SetText( wxString((const wxChar*)buf) ); |
3f364be8 | 345 | |
68379eaf | 346 | return true; |
3f364be8 VZ |
347 | } |
348 | ||
c7d6d883 RR |
349 | #endif |
350 | ||
3f364be8 VZ |
351 | // ---------------------------------------------------------------------------- |
352 | // wxFileDataObjectBase | |
353 | // ---------------------------------------------------------------------------- | |
354 | ||
9e2896e5 VZ |
355 | // VZ: I don't need this in MSW finally, so if it is needed in wxGTK, it should |
356 | // be moved to gtk/dataobj.cpp | |
357 | #if 0 | |
358 | ||
3f364be8 VZ |
359 | wxString wxFileDataObjectBase::GetFilenames() const |
360 | { | |
361 | wxString str; | |
362 | size_t count = m_filenames.GetCount(); | |
363 | for ( size_t n = 0; n < count; n++ ) | |
364 | { | |
365 | str << m_filenames[n] << wxT('\0'); | |
366 | } | |
367 | ||
368 | return str; | |
369 | } | |
370 | ||
371 | void wxFileDataObjectBase::SetFilenames(const wxChar* filenames) | |
372 | { | |
373 | m_filenames.Empty(); | |
374 | ||
375 | wxString current; | |
376 | for ( const wxChar *pc = filenames; ; pc++ ) | |
377 | { | |
378 | if ( *pc ) | |
379 | { | |
380 | current += *pc; | |
381 | } | |
382 | else | |
383 | { | |
384 | if ( !current ) | |
385 | { | |
386 | // 2 consecutive NULs - this is the end of the string | |
387 | break; | |
388 | } | |
389 | ||
390 | m_filenames.Add(current); | |
391 | current.Empty(); | |
392 | } | |
393 | } | |
394 | } | |
395 | ||
8b445796 | 396 | #endif |
9e2896e5 | 397 | |
3f364be8 VZ |
398 | // ---------------------------------------------------------------------------- |
399 | // wxCustomDataObject | |
400 | // ---------------------------------------------------------------------------- | |
401 | ||
775e1c62 RD |
402 | wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format) |
403 | : wxDataObjectSimple(format) | |
404 | { | |
8b445796 DS |
405 | m_data = NULL; |
406 | m_size = 0; | |
775e1c62 RD |
407 | } |
408 | ||
3f364be8 VZ |
409 | wxCustomDataObject::~wxCustomDataObject() |
410 | { | |
411 | Free(); | |
412 | } | |
413 | ||
414 | void wxCustomDataObject::TakeData(size_t size, void *data) | |
415 | { | |
416 | Free(); | |
417 | ||
418 | m_size = size; | |
419 | m_data = data; | |
420 | } | |
421 | ||
422 | void *wxCustomDataObject::Alloc(size_t size) | |
423 | { | |
424 | return (void *)new char[size]; | |
425 | } | |
426 | ||
427 | void wxCustomDataObject::Free() | |
428 | { | |
8b445796 | 429 | delete [] (char*)m_data; |
3f364be8 | 430 | m_size = 0; |
8b445796 | 431 | m_data = (void*)NULL; |
3f364be8 VZ |
432 | } |
433 | ||
434 | size_t wxCustomDataObject::GetDataSize() const | |
435 | { | |
436 | return GetSize(); | |
437 | } | |
438 | ||
439 | bool wxCustomDataObject::GetDataHere(void *buf) const | |
440 | { | |
8b445796 DS |
441 | if ( buf == NULL ) |
442 | return false; | |
443 | ||
3f364be8 | 444 | void *data = GetData(); |
8b445796 | 445 | if ( data == NULL ) |
68379eaf | 446 | return false; |
3f364be8 | 447 | |
8b445796 | 448 | memcpy( buf, data, GetSize() ); |
3f364be8 | 449 | |
68379eaf | 450 | return true; |
3f364be8 VZ |
451 | } |
452 | ||
9e2896e5 | 453 | bool wxCustomDataObject::SetData(size_t size, const void *buf) |
3f364be8 VZ |
454 | { |
455 | Free(); | |
456 | ||
457 | m_data = Alloc(size); | |
8b445796 | 458 | if ( m_data == NULL ) |
68379eaf | 459 | return false; |
3f364be8 | 460 | |
8b445796 DS |
461 | m_size = size; |
462 | memcpy( m_data, buf, m_size ); | |
3f364be8 | 463 | |
68379eaf | 464 | return true; |
3f364be8 VZ |
465 | } |
466 | ||
8ee9d618 VZ |
467 | // ============================================================================ |
468 | // some common dnd related code | |
469 | // ============================================================================ | |
470 | ||
8fb3a512 JS |
471 | #if wxUSE_DRAG_AND_DROP |
472 | ||
8ee9d618 VZ |
473 | #include "wx/dnd.h" |
474 | ||
475 | // ---------------------------------------------------------------------------- | |
476 | // wxTextDropTarget | |
477 | // ---------------------------------------------------------------------------- | |
478 | ||
f3ac12aa VZ |
479 | // NB: we can't use "new" in ctor initializer lists because this provokes an |
480 | // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!), | |
481 | // so use SetDataObject() instead | |
482 | ||
8ee9d618 | 483 | wxTextDropTarget::wxTextDropTarget() |
8ee9d618 | 484 | { |
f3ac12aa | 485 | SetDataObject(new wxTextDataObject); |
8ee9d618 VZ |
486 | } |
487 | ||
488 | wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) | |
489 | { | |
490 | if ( !GetData() ) | |
491 | return wxDragNone; | |
492 | ||
493 | wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject; | |
8b445796 | 494 | return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone; |
8ee9d618 VZ |
495 | } |
496 | ||
497 | // ---------------------------------------------------------------------------- | |
498 | // wxFileDropTarget | |
499 | // ---------------------------------------------------------------------------- | |
500 | ||
501 | wxFileDropTarget::wxFileDropTarget() | |
8ee9d618 | 502 | { |
f3ac12aa | 503 | SetDataObject(new wxFileDataObject); |
8ee9d618 VZ |
504 | } |
505 | ||
506 | wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def) | |
507 | { | |
508 | if ( !GetData() ) | |
509 | return wxDragNone; | |
510 | ||
511 | wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject; | |
8b445796 | 512 | return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone; |
8ee9d618 VZ |
513 | } |
514 | ||
1e6feb95 | 515 | #endif // wxUSE_DRAG_AND_DROP |
8fb3a512 | 516 | |
1e6feb95 | 517 | #endif // wxUSE_DATAOBJ |