]>
Commit | Line | Data |
---|---|---|
3f364be8 VZ |
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$ | |
77ffb593 | 8 | // Copyright: (c) wxWidgets Team |
65571936 | 9 | // Licence: wxWindows licence |
3f364be8 VZ |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
3f364be8 VZ |
20 | #include "wx/wxprec.h" |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
1e6feb95 VZ |
26 | #if wxUSE_DATAOBJ |
27 | ||
3f364be8 VZ |
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 | ||
259c43f6 | 41 | WX_DEFINE_LIST(wxSimpleDataObjectList) |
3f364be8 | 42 | |
0c2b453f VZ |
43 | // ---------------------------------------------------------------------------- |
44 | // globals | |
45 | // ---------------------------------------------------------------------------- | |
46 | ||
47 | static wxDataFormat dataFormatInvalid; | |
31e78e0c | 48 | WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid; |
0c2b453f | 49 | |
3f364be8 VZ |
50 | // ============================================================================ |
51 | // implementation | |
52 | // ============================================================================ | |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // wxDataObjectBase | |
56 | // ---------------------------------------------------------------------------- | |
57 | ||
58 | wxDataObjectBase::~wxDataObjectBase() | |
59 | { | |
60 | } | |
61 | ||
d9317fd4 VZ |
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 | ||
3f364be8 VZ |
89 | // ---------------------------------------------------------------------------- |
90 | // wxDataObjectComposite | |
91 | // ---------------------------------------------------------------------------- | |
92 | ||
e6b01b78 VZ |
93 | wxDataObjectComposite::wxDataObjectComposite() |
94 | { | |
95 | m_preferred = 0; | |
222ed1d6 | 96 | } |
e6b01b78 | 97 | |
222ed1d6 MB |
98 | wxDataObjectComposite::~wxDataObjectComposite() |
99 | { | |
100 | WX_CLEAR_LIST(wxSimpleDataObjectList, m_dataObjects); | |
e6b01b78 VZ |
101 | } |
102 | ||
3f364be8 VZ |
103 | wxDataObjectSimple * |
104 | wxDataObjectComposite::GetObject(const wxDataFormat& format) const | |
105 | { | |
222ed1d6 | 106 | wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst(); |
3f364be8 VZ |
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 | { | |
222ed1d6 | 133 | wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred ); |
3f364be8 | 134 | |
2ee3ee1b | 135 | wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") ); |
3f364be8 VZ |
136 | |
137 | wxDataObjectSimple* dataObj = node->GetData(); | |
138 | ||
139 | return dataObj->GetFormat(); | |
140 | } | |
141 | ||
e1b435af | 142 | #if defined(__WXMSW__) |
7d584866 | 143 | |
e1b435af MB |
144 | size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format ) |
145 | { | |
146 | wxDataObjectSimple *dataObj = GetObject(format); | |
147 | ||
f7f50f49 | 148 | wxCHECK_MSG( dataObj, 0, |
e1b435af MB |
149 | wxT("unsupported format in wxDataObjectComposite")); |
150 | ||
151 | return dataObj->GetBufferOffset( format ); | |
152 | } | |
153 | ||
7d584866 | 154 | |
e1b435af MB |
155 | const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer, |
156 | size_t* size, | |
157 | const wxDataFormat& format ) | |
158 | { | |
159 | wxDataObjectSimple *dataObj = GetObject(format); | |
160 | ||
f7f50f49 | 161 | wxCHECK_MSG( dataObj, NULL, |
e1b435af MB |
162 | wxT("unsupported format in wxDataObjectComposite")); |
163 | ||
164 | return dataObj->GetSizeFromBuffer( buffer, size, format ); | |
165 | } | |
166 | ||
7d584866 | 167 | |
e1b435af MB |
168 | void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size, |
169 | const wxDataFormat& format ) | |
170 | { | |
171 | wxDataObjectSimple *dataObj = GetObject(format); | |
172 | ||
f7f50f49 | 173 | wxCHECK_MSG( dataObj, NULL, |
e1b435af MB |
174 | wxT("unsupported format in wxDataObjectComposite")); |
175 | ||
176 | return dataObj->SetSizeInBuffer( buffer, size, format ); | |
177 | } | |
178 | ||
179 | #endif | |
180 | ||
3f364be8 VZ |
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; | |
222ed1d6 | 191 | wxSimpleDataObjectList::compatibility_iterator node; |
3f364be8 VZ |
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 | ||
68379eaf | 214 | wxCHECK_MSG( dataObj, false, |
3f364be8 VZ |
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 | ||
68379eaf | 226 | wxCHECK_MSG( dataObj, false, |
3f364be8 VZ |
227 | wxT("unsupported format in wxDataObjectComposite")); |
228 | ||
229 | return dataObj->SetData(len, buf); | |
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 VZ |
246 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); |
247 | return buffer ? strlen(buffer) + 1 : 0; | |
c7d6d883 RR |
248 | } |
249 | ||
250 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
251 | { | |
f4370741 VZ |
252 | wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() ); |
253 | if ( !buffer ) | |
254 | return false; | |
255 | ||
256 | strcpy( (char*) buf, buffer ); | |
68379eaf WS |
257 | |
258 | return true; | |
c7d6d883 RR |
259 | } |
260 | ||
261 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
262 | size_t WXUNUSED(len), const void *buf) | |
263 | { | |
f4370741 VZ |
264 | wxWCharBuffer buffer = GetConv(format).cMB2WX((const char *)buf); |
265 | if ( !buffer ) | |
266 | return false; | |
267 | ||
268 | SetText(buffer); | |
68379eaf WS |
269 | |
270 | return true; | |
c7d6d883 RR |
271 | } |
272 | ||
25758297 SC |
273 | #elif wxUSE_UNICODE && defined(__WXMAC__) |
274 | ||
671478ee SC |
275 | static wxMBConvUTF16 sUTF16Converter ; |
276 | ||
277 | static inline wxMBConv& GetConv(const wxDataFormat& format) | |
278 | { | |
279 | return format == wxDF_UNICODETEXT ? sUTF16Converter : (wxMBConv&) wxConvLocal; | |
280 | } | |
281 | ||
25758297 SC |
282 | size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const |
283 | { | |
671478ee SC |
284 | size_t len = GetConv(format).WC2MB( NULL , GetText().c_str() , 0 ) |
285 | + ( format == wxDF_UNICODETEXT ? 2 : 1 ) ; | |
286 | return len ; | |
25758297 SC |
287 | } |
288 | ||
289 | bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const | |
290 | { | |
671478ee SC |
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 | |
68379eaf WS |
299 | |
300 | return true; | |
25758297 SC |
301 | } |
302 | ||
303 | bool wxTextDataObject::SetData(const wxDataFormat& format, | |
304 | size_t WXUNUSED(len), const void *buf) | |
305 | { | |
671478ee SC |
306 | wxWCharBuffer buffer = GetConv(format).cMB2WX((const char *)buf); |
307 | if ( !buffer ) | |
308 | return false; | |
309 | ||
310 | SetText(buffer); | |
311 | ||
68379eaf | 312 | return true; |
25758297 SC |
313 | } |
314 | ||
6a59178a | 315 | #else |
c7d6d883 RR |
316 | |
317 | size_t wxTextDataObject::GetDataSize() const | |
318 | { | |
e1b435af | 319 | return GetTextLength() * sizeof(wxChar); |
3f364be8 VZ |
320 | } |
321 | ||
322 | bool wxTextDataObject::GetDataHere(void *buf) const | |
323 | { | |
e1b435af | 324 | wxStrcpy((wxChar *)buf, GetText().c_str()); |
3f364be8 | 325 | |
68379eaf | 326 | return true; |
3f364be8 VZ |
327 | } |
328 | ||
329 | bool wxTextDataObject::SetData(size_t WXUNUSED(len), const void *buf) | |
330 | { | |
e1b435af | 331 | SetText(wxString((const wxChar *)buf)); |
3f364be8 | 332 | |
68379eaf | 333 | return true; |
3f364be8 VZ |
334 | } |
335 | ||
c7d6d883 RR |
336 | #endif |
337 | ||
3f364be8 VZ |
338 | // ---------------------------------------------------------------------------- |
339 | // wxFileDataObjectBase | |
340 | // ---------------------------------------------------------------------------- | |
341 | ||
9e2896e5 VZ |
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 | ||
3f364be8 VZ |
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 | ||
9e2896e5 VZ |
383 | #endif // 0 |
384 | ||
3f364be8 VZ |
385 | // ---------------------------------------------------------------------------- |
386 | // wxCustomDataObject | |
387 | // ---------------------------------------------------------------------------- | |
388 | ||
775e1c62 RD |
389 | wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format) |
390 | : wxDataObjectSimple(format) | |
391 | { | |
4e16881a | 392 | m_data = (void *)NULL; |
775e1c62 RD |
393 | } |
394 | ||
3f364be8 VZ |
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 | { | |
5bcea60e | 415 | delete [] (char *)m_data; |
3f364be8 VZ |
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 ) | |
68379eaf | 429 | return false; |
3f364be8 VZ |
430 | |
431 | memcpy(buf, data, GetSize()); | |
432 | ||
68379eaf | 433 | return true; |
3f364be8 VZ |
434 | } |
435 | ||
9e2896e5 | 436 | bool wxCustomDataObject::SetData(size_t size, const void *buf) |
3f364be8 VZ |
437 | { |
438 | Free(); | |
439 | ||
440 | m_data = Alloc(size); | |
441 | if ( !m_data ) | |
68379eaf | 442 | return false; |
3f364be8 | 443 | |
9e2896e5 | 444 | memcpy(m_data, buf, m_size = size); |
3f364be8 | 445 | |
68379eaf | 446 | return true; |
3f364be8 VZ |
447 | } |
448 | ||
8ee9d618 VZ |
449 | // ============================================================================ |
450 | // some common dnd related code | |
451 | // ============================================================================ | |
452 | ||
8fb3a512 JS |
453 | #if wxUSE_DRAG_AND_DROP |
454 | ||
8ee9d618 VZ |
455 | #include "wx/dnd.h" |
456 | ||
457 | // ---------------------------------------------------------------------------- | |
458 | // wxTextDropTarget | |
459 | // ---------------------------------------------------------------------------- | |
460 | ||
f3ac12aa VZ |
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 | ||
8ee9d618 | 465 | wxTextDropTarget::wxTextDropTarget() |
8ee9d618 | 466 | { |
f3ac12aa | 467 | SetDataObject(new wxTextDataObject); |
8ee9d618 VZ |
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() | |
8ee9d618 | 484 | { |
f3ac12aa | 485 | SetDataObject(new wxFileDataObject); |
8ee9d618 VZ |
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 | ||
1e6feb95 | 497 | #endif // wxUSE_DRAG_AND_DROP |
8fb3a512 | 498 | |
1e6feb95 | 499 | #endif // wxUSE_DATAOBJ |