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