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