]> git.saurik.com Git - wxWidgets.git/blob - src/common/dobjcmn.cpp
4c1acaa8378b2fdc36bce1e45d732d901d05741c
[wxWidgets.git] / src / common / dobjcmn.cpp
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 #include "wx/dataobj.h"
22
23 #ifndef WX_PRECOMP
24 #include "wx/app.h"
25 #endif
26
27 // ----------------------------------------------------------------------------
28 // lists
29 // ----------------------------------------------------------------------------
30
31 #include "wx/listimpl.cpp"
32
33 WX_DEFINE_LIST(wxSimpleDataObjectList)
34
35 // ----------------------------------------------------------------------------
36 // globals
37 // ----------------------------------------------------------------------------
38
39 static wxDataFormat dataFormatInvalid;
40 WXDLLEXPORT const wxDataFormat& wxFormatInvalid = dataFormatInvalid;
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // wxDataObjectBase
48 // ----------------------------------------------------------------------------
49
50 wxDataObjectBase::~wxDataObjectBase()
51 {
52 }
53
54 bool wxDataObjectBase::IsSupported(const wxDataFormat& format,
55 Direction dir) const
56 {
57 size_t nFormatCount = GetFormatCount( dir );
58 if ( nFormatCount == 1 )
59 {
60 return format == GetPreferredFormat( dir );
61 }
62 else
63 {
64 wxDataFormat *formats = new wxDataFormat[nFormatCount];
65 GetAllFormats( formats, dir );
66
67 size_t n;
68 for ( n = 0; n < nFormatCount; n++ )
69 {
70 if ( formats[n] == format )
71 break;
72 }
73
74 delete [] formats;
75
76 // found?
77 return n < nFormatCount;
78 }
79 }
80
81 // ----------------------------------------------------------------------------
82 // wxDataObjectComposite
83 // ----------------------------------------------------------------------------
84
85 wxDataObjectComposite::wxDataObjectComposite()
86 {
87 m_preferred = 0;
88 m_receivedFormat = wxFormatInvalid;
89 }
90
91 wxDataObjectComposite::~wxDataObjectComposite()
92 {
93 WX_CLEAR_LIST( wxSimpleDataObjectList, m_dataObjects );
94 }
95
96 wxDataObjectSimple *
97 wxDataObjectComposite::GetObject(const wxDataFormat& format, wxDataObjectBase::Direction dir) const
98 {
99 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.GetFirst();
100
101 while ( node )
102 {
103 wxDataObjectSimple *dataObj = node->GetData();
104
105 if (dataObj->IsSupported(format,dir))
106 return dataObj;
107 node = node->GetNext();
108 }
109 return NULL;
110 }
111
112 void wxDataObjectComposite::Add(wxDataObjectSimple *dataObject, bool preferred)
113 {
114 if ( preferred )
115 m_preferred = m_dataObjects.GetCount();
116
117 m_dataObjects.Append( dataObject );
118 }
119
120 wxDataFormat wxDataObjectComposite::GetReceivedFormat() const
121 {
122 return m_receivedFormat;
123 }
124
125 wxDataFormat
126 wxDataObjectComposite::GetPreferredFormat(Direction WXUNUSED(dir)) const
127 {
128 wxSimpleDataObjectList::compatibility_iterator node = m_dataObjects.Item( m_preferred );
129
130 wxCHECK_MSG( node, wxFormatInvalid, wxT("no preferred format") );
131
132 wxDataObjectSimple* dataObj = node->GetData();
133
134 return dataObj->GetFormat();
135 }
136
137 #if defined(__WXMSW__)
138
139 size_t wxDataObjectComposite::GetBufferOffset( const wxDataFormat& format )
140 {
141 wxDataObjectSimple *dataObj = GetObject(format);
142
143 wxCHECK_MSG( dataObj, 0,
144 wxT("unsupported format in wxDataObjectComposite"));
145
146 return dataObj->GetBufferOffset( format );
147 }
148
149
150 const void* wxDataObjectComposite::GetSizeFromBuffer( const void* buffer,
151 size_t* size,
152 const wxDataFormat& format )
153 {
154 wxDataObjectSimple *dataObj = GetObject(format);
155
156 wxCHECK_MSG( dataObj, NULL,
157 wxT("unsupported format in wxDataObjectComposite"));
158
159 return dataObj->GetSizeFromBuffer( buffer, size, format );
160 }
161
162
163 void* wxDataObjectComposite::SetSizeInBuffer( void* buffer, size_t size,
164 const wxDataFormat& format )
165 {
166 wxDataObjectSimple *dataObj = GetObject( format );
167
168 wxCHECK_MSG( dataObj, NULL,
169 wxT("unsupported format in wxDataObjectComposite"));
170
171 return dataObj->SetSizeInBuffer( buffer, size, format );
172 }
173
174 #endif
175
176 size_t wxDataObjectComposite::GetFormatCount(Direction dir) const
177 {
178 size_t n = 0;
179
180 // NOTE: some wxDataObjectSimple objects may return a number greater than 1
181 // from GetFormatCount(): this is the case of e.g. wxTextDataObject
182 // under wxMac and wxGTK
183 wxSimpleDataObjectList::compatibility_iterator node;
184 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
185 n += node->GetData()->GetFormatCount(dir);
186
187 return n;
188 }
189
190 void wxDataObjectComposite::GetAllFormats(wxDataFormat *formats,
191 Direction dir) const
192 {
193 size_t index(0);
194 wxSimpleDataObjectList::compatibility_iterator node;
195
196 for ( node = m_dataObjects.GetFirst(); node; node = node->GetNext() )
197 {
198 // NOTE: some wxDataObjectSimple objects may return more than 1 format
199 // from GetAllFormats(): this is the case of e.g. wxTextDataObject
200 // under wxMac and wxGTK
201 node->GetData()->GetAllFormats(formats+index, dir);
202 index += node->GetData()->GetFormatCount(dir);
203 }
204 }
205
206 size_t wxDataObjectComposite::GetDataSize(const wxDataFormat& format) const
207 {
208 wxDataObjectSimple *dataObj = GetObject(format);
209
210 wxCHECK_MSG( dataObj, 0,
211 wxT("unsupported format in wxDataObjectComposite"));
212
213 return dataObj->GetDataSize();
214 }
215
216 bool wxDataObjectComposite::GetDataHere(const wxDataFormat& format,
217 void *buf) const
218 {
219 wxDataObjectSimple *dataObj = GetObject( format );
220
221 wxCHECK_MSG( dataObj, false,
222 wxT("unsupported format in wxDataObjectComposite"));
223
224 return dataObj->GetDataHere( buf );
225 }
226
227 bool wxDataObjectComposite::SetData(const wxDataFormat& format,
228 size_t len,
229 const void *buf)
230 {
231 wxDataObjectSimple *dataObj = GetObject( format );
232
233 wxCHECK_MSG( dataObj, false,
234 wxT("unsupported format in wxDataObjectComposite"));
235
236 m_receivedFormat = format;
237
238 // Notice that we must pass "format" here as wxTextDataObject, that we can
239 // have as one of our "simple" sub-objects actually is not that simple and
240 // can support multiple formats (ASCII/UTF-8/UTF-16/...) and so needs to
241 // know which one it is given.
242 return dataObj->SetData( format, len, buf );
243 }
244
245 // ----------------------------------------------------------------------------
246 // wxTextDataObject
247 // ----------------------------------------------------------------------------
248
249 #ifdef wxNEEDS_UTF8_FOR_TEXT_DATAOBJ
250
251 // FIXME-UTF8: we should be able to merge wchar_t and UTF-8 versions once we
252 // have a way to get UTF-8 string (and its length) in both builds
253 // without loss of efficiency (i.e. extra buffer copy/strlen call)
254
255 #if wxUSE_UNICODE_WCHAR
256
257 static inline wxMBConv& GetConv(const wxDataFormat& format)
258 {
259 // use UTF8 for wxDF_UNICODETEXT and UCS4 for wxDF_TEXT
260 return format == wxDF_UNICODETEXT ? wxConvUTF8 : wxConvLibc;
261 }
262
263 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
264 {
265 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
266
267 return buffer ? strlen( buffer ) : 0;
268 }
269
270 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
271 {
272 if ( !buf )
273 return false;
274
275 wxCharBuffer buffer = GetConv(format).cWX2MB( GetText().c_str() );
276 if ( !buffer )
277 return false;
278
279 memcpy( (char*) buf, buffer, GetDataSize(format) );
280 // strcpy( (char*) buf, buffer );
281
282 return true;
283 }
284
285 bool wxTextDataObject::SetData(const wxDataFormat& format,
286 size_t WXUNUSED(len), const void *buf)
287 {
288 if ( buf == NULL )
289 return false;
290
291 wxWCharBuffer buffer = GetConv(format).cMB2WX( (const char*)buf );
292
293 SetText( buffer );
294
295 return true;
296 }
297
298 #else // wxUSE_UNICODE_UTF8
299
300 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
301 {
302 const wxString& text = GetText();
303 if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
304 {
305 return text.utf8_length();
306 }
307 else // wxDF_TEXT
308 {
309 const wxCharBuffer buf(wxConvLocal.cWC2MB(text.wc_str()));
310 return buf ? strlen(buf) : 0;
311 }
312 }
313
314 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
315 {
316 if ( !buf )
317 return false;
318
319 const wxString& text = GetText();
320 if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
321 {
322 memcpy(buf, text.utf8_str(), text.utf8_length());
323 }
324 else // wxDF_TEXT
325 {
326 const wxCharBuffer bufLocal(wxConvLocal.cWC2MB(text.wc_str()));
327 if ( !bufLocal )
328 return false;
329
330 memcpy(buf, bufLocal, strlen(bufLocal));
331 }
332
333 return true;
334 }
335
336 bool wxTextDataObject::SetData(const wxDataFormat& format,
337 size_t len, const void *buf_)
338 {
339 const char * const buf = static_cast<const char *>(buf_);
340
341 if ( buf == NULL )
342 return false;
343
344 if ( format == wxDF_UNICODETEXT || wxLocaleIsUtf8 )
345 {
346 // normally the data is in UTF-8 so we could use FromUTF8Unchecked()
347 // but it's not absolutely clear what GTK+ does if the clipboard data
348 // is not in UTF-8 so do an extra check for tranquility, it shouldn't
349 // matter much if we lose a bit of performance when pasting from
350 // clipboard
351 SetText(wxString::FromUTF8(buf, len));
352 }
353 else // wxDF_TEXT, convert from current (non-UTF8) locale
354 {
355 SetText(wxConvLocal.cMB2WC(buf, len, NULL));
356 }
357
358 return true;
359 }
360
361 #endif // wxUSE_UNICODE_WCHAR/wxUSE_UNICODE_UTF8
362
363 #elif defined(wxNEEDS_UTF16_FOR_TEXT_DATAOBJ)
364
365 namespace
366 {
367
368 inline wxMBConv& GetConv(const wxDataFormat& format)
369 {
370 static wxMBConvUTF16 s_UTF16Converter;
371
372 return format == wxDF_UNICODETEXT ? static_cast<wxMBConv&>(s_UTF16Converter)
373 : static_cast<wxMBConv&>(wxConvLocal);
374 }
375
376 } // anonymous namespace
377
378 size_t wxTextDataObject::GetDataSize(const wxDataFormat& format) const
379 {
380 return GetConv(format).WC2MB(NULL, GetText().wc_str(), 0);
381 }
382
383 bool wxTextDataObject::GetDataHere(const wxDataFormat& format, void *buf) const
384 {
385 if ( buf == NULL )
386 return false;
387
388 wxCharBuffer buffer(GetConv(format).cWX2MB(GetText().c_str()));
389
390 memcpy(buf, buffer.data(), buffer.length());
391
392 return true;
393 }
394
395 bool wxTextDataObject::SetData(const wxDataFormat& format,
396 size_t WXUNUSED(len),
397 const void *buf)
398 {
399 if ( buf == NULL )
400 return false;
401
402 SetText(GetConv(format).cMB2WX(static_cast<const char*>(buf)));
403
404 return true;
405 }
406
407 #else // !wxNEEDS_UTF{8,16}_FOR_TEXT_DATAOBJ
408
409 size_t wxTextDataObject::GetDataSize() const
410 {
411 return GetTextLength() * sizeof(wxChar);
412 }
413
414 bool wxTextDataObject::GetDataHere(void *buf) const
415 {
416 // NOTE: use wxTmemcpy() instead of wxStrncpy() to allow
417 // retrieval of strings with embedded NULLs
418 wxTmemcpy( (wxChar*)buf, GetText().c_str(), GetTextLength() );
419
420 return true;
421 }
422
423 bool wxTextDataObject::SetData(size_t len, const void *buf)
424 {
425 SetText( wxString((const wxChar*)buf, len/sizeof(wxChar)) );
426
427 return true;
428 }
429
430 #endif // different wxTextDataObject implementations
431
432 size_t wxHTMLDataObject::GetDataSize() const
433 {
434 const wxScopedCharBuffer buffer(GetHTML().utf8_str());
435
436 size_t size = buffer.length();
437
438 #ifdef __WXMSW__
439 // On Windows we need to add some stuff to the string to satisfy
440 // its clipboard format requirements.
441 size += 400;
442 #endif
443
444 return size;
445 }
446
447 bool wxHTMLDataObject::GetDataHere(void *buf) const
448 {
449 if ( !buf )
450 return false;
451
452 // Windows and Mac always use UTF-8, and docs suggest GTK does as well.
453 const wxScopedCharBuffer html(GetHTML().utf8_str());
454 if ( !html )
455 return false;
456
457 char* const buffer = static_cast<char*>(buf);
458
459 #ifdef __WXMSW__
460 // add the extra info that the MSW clipboard format requires.
461
462 // Create a template string for the HTML header...
463 strcpy(buffer,
464 "Version:0.9\r\n"
465 "StartHTML:00000000\r\n"
466 "EndHTML:00000000\r\n"
467 "StartFragment:00000000\r\n"
468 "EndFragment:00000000\r\n"
469 "<html><body>\r\n"
470 "<!--StartFragment -->\r\n");
471
472 // Append the HTML...
473 strcat(buffer, html);
474 strcat(buffer, "\r\n");
475 // Finish up the HTML format...
476 strcat(buffer,
477 "<!--EndFragment-->\r\n"
478 "</body>\r\n"
479 "</html>");
480
481 // Now go back, calculate all the lengths, and write out the
482 // necessary header information. Note, wsprintf() truncates the
483 // string when you overwrite it so you follow up with code to replace
484 // the 0 appended at the end with a '\r'...
485 char *ptr = strstr(buffer, "StartHTML");
486 sprintf(ptr+10, "%08u", (unsigned)(strstr(buffer, "<html>") - buffer));
487 *(ptr+10+8) = '\r';
488
489 ptr = strstr(buffer, "EndHTML");
490 sprintf(ptr+8, "%08u", (unsigned)strlen(buffer));
491 *(ptr+8+8) = '\r';
492
493 ptr = strstr(buffer, "StartFragment");
494 sprintf(ptr+14, "%08u", (unsigned)(strstr(buffer, "<!--StartFrag") - buffer));
495 *(ptr+14+8) = '\r';
496
497 ptr = strstr(buffer, "EndFragment");
498 sprintf(ptr+12, "%08u", (unsigned)(strstr(buffer, "<!--EndFrag") - buffer));
499 *(ptr+12+8) = '\r';
500 #else
501 strcpy(buffer, html);
502 #endif // __WXMSW__
503
504 return true;
505 }
506
507 bool wxHTMLDataObject::SetData(size_t WXUNUSED(len), const void *buf)
508 {
509 if ( buf == NULL )
510 return false;
511
512 // Windows and Mac always use UTF-8, and docs suggest GTK does as well.
513 wxString html = wxString::FromUTF8(static_cast<const char*>(buf));
514
515 #ifdef __WXMSW__
516 // To be consistent with other platforms, we only add the Fragment part
517 // of the Windows HTML clipboard format to the data object.
518 int fragmentStart = html.rfind("StartFragment");
519 int fragmentEnd = html.rfind("EndFragment");
520
521 if (fragmentStart != wxNOT_FOUND && fragmentEnd != wxNOT_FOUND)
522 {
523 int startCommentEnd = html.find("-->", fragmentStart) + 3;
524 int endCommentStart = html.rfind("<!--", fragmentEnd);
525
526 if (startCommentEnd != wxNOT_FOUND && endCommentStart != wxNOT_FOUND)
527 html = html.Mid(startCommentEnd, endCommentStart - startCommentEnd);
528 }
529 #endif // __WXMSW__
530
531 SetHTML( html );
532
533 return true;
534 }
535
536
537 // ----------------------------------------------------------------------------
538 // wxCustomDataObject
539 // ----------------------------------------------------------------------------
540
541 wxCustomDataObject::wxCustomDataObject(const wxDataFormat& format)
542 : wxDataObjectSimple(format)
543 {
544 m_data = NULL;
545 m_size = 0;
546 }
547
548 wxCustomDataObject::~wxCustomDataObject()
549 {
550 Free();
551 }
552
553 void wxCustomDataObject::TakeData(size_t size, void *data)
554 {
555 Free();
556
557 m_size = size;
558 m_data = data;
559 }
560
561 void *wxCustomDataObject::Alloc(size_t size)
562 {
563 return (void *)new char[size];
564 }
565
566 void wxCustomDataObject::Free()
567 {
568 delete [] (char*)m_data;
569 m_size = 0;
570 m_data = NULL;
571 }
572
573 size_t wxCustomDataObject::GetDataSize() const
574 {
575 return GetSize();
576 }
577
578 bool wxCustomDataObject::GetDataHere(void *buf) const
579 {
580 if ( buf == NULL )
581 return false;
582
583 void *data = GetData();
584 if ( data == NULL )
585 return false;
586
587 memcpy( buf, data, GetSize() );
588
589 return true;
590 }
591
592 bool wxCustomDataObject::SetData(size_t size, const void *buf)
593 {
594 Free();
595
596 m_data = Alloc(size);
597 if ( m_data == NULL )
598 return false;
599
600 m_size = size;
601 memcpy( m_data, buf, m_size );
602
603 return true;
604 }
605
606 // ============================================================================
607 // some common dnd related code
608 // ============================================================================
609
610 #if wxUSE_DRAG_AND_DROP
611
612 #include "wx/dnd.h"
613
614 // ----------------------------------------------------------------------------
615 // wxTextDropTarget
616 // ----------------------------------------------------------------------------
617
618 // NB: we can't use "new" in ctor initializer lists because this provokes an
619 // internal compiler error with VC++ 5.0 (hey, even gcc compiles this!),
620 // so use SetDataObject() instead
621
622 wxTextDropTarget::wxTextDropTarget()
623 {
624 SetDataObject(new wxTextDataObject);
625 }
626
627 wxDragResult wxTextDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
628 {
629 if ( !GetData() )
630 return wxDragNone;
631
632 wxTextDataObject *dobj = (wxTextDataObject *)m_dataObject;
633 return OnDropText( x, y, dobj->GetText() ) ? def : wxDragNone;
634 }
635
636 // ----------------------------------------------------------------------------
637 // wxFileDropTarget
638 // ----------------------------------------------------------------------------
639
640 wxFileDropTarget::wxFileDropTarget()
641 {
642 SetDataObject(new wxFileDataObject);
643 }
644
645 wxDragResult wxFileDropTarget::OnData(wxCoord x, wxCoord y, wxDragResult def)
646 {
647 if ( !GetData() )
648 return wxDragNone;
649
650 wxFileDataObject *dobj = (wxFileDataObject *)m_dataObject;
651 return OnDropFiles( x, y, dobj->GetFilenames() ) ? def : wxDragNone;
652 }
653
654 #endif // wxUSE_DRAG_AND_DROP
655
656 #endif // wxUSE_DATAOBJ