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