]>
Commit | Line | Data |
---|---|---|
489468fe SC |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/mac/carbon/dataobj.cpp | |
3 | // Purpose: implementation of wxDataObject class | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 10/21/99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1999 Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // For compilers that support precompilation, includes "wx.h". | |
13 | #include "wx/wxprec.h" | |
14 | ||
15 | #if wxUSE_DATAOBJ | |
16 | ||
17 | #include "wx/dataobj.h" | |
18 | ||
19 | #ifndef WX_PRECOMP | |
20 | #include "wx/intl.h" | |
21 | #include "wx/log.h" | |
22 | #include "wx/dcmemory.h" | |
23 | #include "wx/image.h" | |
24 | #endif | |
25 | ||
26 | #include "wx/mstream.h" | |
27 | #include "wx/metafile.h" | |
28 | #include "wx/tokenzr.h" | |
29 | ||
1f0c8f31 | 30 | #include "wx/osx/uma.h" |
489468fe SC |
31 | |
32 | #ifdef __DARWIN__ | |
33 | #include <QuickTime/QuickTime.h> | |
34 | #endif | |
35 | ||
36 | ||
37 | // ---------------------------------------------------------------------------- | |
38 | // wxDataFormat | |
39 | // ---------------------------------------------------------------------------- | |
40 | ||
41 | wxDataFormat::wxDataFormat() | |
42 | { | |
43 | m_type = wxDF_INVALID; | |
44 | m_format = 0; | |
45 | } | |
46 | ||
47 | wxDataFormat::wxDataFormat( wxDataFormatId vType ) | |
48 | { | |
49 | m_format = 0; | |
50 | m_type = wxDF_INVALID; | |
51 | SetType( vType ); | |
52 | } | |
53 | ||
54 | wxDataFormat::wxDataFormat( const wxChar *zId ) | |
55 | { | |
56 | m_format = 0; | |
57 | m_type = wxDF_INVALID; | |
58 | SetId( zId ); | |
59 | } | |
60 | ||
61 | wxDataFormat::wxDataFormat( const wxString& rId ) | |
62 | { | |
63 | m_format = 0; | |
64 | m_type = wxDF_INVALID; | |
65 | SetId( rId ); | |
66 | } | |
67 | ||
68 | wxDataFormat::wxDataFormat(const wxDataFormat& rFormat) | |
69 | { | |
70 | if ( rFormat.m_format ) | |
71 | m_format = (NativeFormat) CFStringCreateCopy(NULL, (CFStringRef)rFormat.m_format); | |
72 | else | |
73 | m_format = 0; | |
74 | m_type = rFormat.m_type; | |
75 | m_id = rFormat.m_id; | |
76 | } | |
77 | ||
78 | wxDataFormat::wxDataFormat( NativeFormat vFormat ) | |
79 | { | |
80 | m_format = 0; | |
81 | m_type = wxDF_INVALID; | |
82 | SetId( vFormat ); | |
83 | } | |
84 | ||
85 | wxDataFormat::~wxDataFormat() | |
86 | { | |
87 | if ( m_format != 0 ) | |
88 | { | |
89 | CFRelease( (CFStringRef) m_format ); | |
90 | m_format = 0; | |
91 | } | |
92 | } | |
93 | ||
94 | // in order to be correct for 10.3 we restrict to the available types there | |
95 | // http://developer.apple.com/qa/qa2005/qa1406.html | |
96 | // TODO : Use UTCoreTypes.h constants once we support 10.4+ only | |
97 | ||
98 | wxDataFormat& wxDataFormat::operator=(const wxDataFormat& rFormat) | |
99 | { | |
100 | if ( m_format != 0 ) | |
101 | { | |
102 | CFRelease( (CFStringRef) m_format ); | |
103 | m_format = 0; | |
104 | } | |
105 | if ( rFormat.m_format ) | |
106 | m_format = (NativeFormat) CFStringCreateCopy(NULL, (CFStringRef)rFormat.m_format); | |
107 | m_type = rFormat.m_type; | |
108 | m_id = rFormat.m_id; | |
109 | return *this; | |
110 | } | |
111 | ||
112 | void wxDataFormat::SetType( wxDataFormatId dataType ) | |
113 | { | |
114 | m_type = dataType; | |
115 | if ( m_format != 0 ) | |
116 | { | |
117 | CFRelease( (CFStringRef) m_format ); | |
118 | m_format = 0; | |
119 | } | |
120 | ||
121 | switch (m_type) | |
122 | { | |
123 | case wxDF_TEXT: | |
124 | m_format = (long) CFStringCreateCopy( NULL, CFSTR("public.plain-text") ); | |
125 | break; | |
126 | ||
127 | case wxDF_UNICODETEXT: | |
128 | m_format = (long) CFStringCreateCopy( NULL, CFSTR("public.utf16-plain-text") ); | |
129 | break; | |
130 | ||
131 | case wxDF_BITMAP: | |
132 | m_format = (long) CFStringCreateCopy( NULL, CFSTR("public.tiff") ); | |
133 | break; | |
134 | case wxDF_METAFILE: | |
135 | m_format = (long) CFStringCreateCopy( NULL, CFSTR("com.adobe.pdf") ); | |
136 | break; | |
137 | ||
138 | case wxDF_FILENAME: | |
139 | m_format = (long) CFStringCreateCopy( NULL, CFSTR("public.file-url") ); | |
140 | break; | |
141 | ||
142 | default: | |
143 | wxFAIL_MSG( wxT("invalid data format") ); | |
144 | break; | |
145 | } | |
146 | } | |
147 | ||
148 | wxString wxDataFormat::GetId() const | |
149 | { | |
150 | return wxCFStringRef(wxCFRetain((CFStringRef)m_format)).AsString(); | |
151 | } | |
152 | ||
153 | void wxDataFormat::SetId( NativeFormat format ) | |
154 | { | |
155 | if ( m_format != 0 ) | |
156 | { | |
157 | CFRelease( (CFStringRef) m_format ); | |
158 | m_format = 0; | |
159 | } | |
160 | m_format = (NativeFormat) CFStringCreateCopy(NULL, (CFStringRef)format); | |
161 | if ( UTTypeConformsTo( (CFStringRef)format, CFSTR("public.utf16-plain-text") ) ) | |
162 | { | |
163 | m_type = wxDF_UNICODETEXT; | |
164 | } | |
165 | else if ( UTTypeConformsTo( (CFStringRef)format, CFSTR("public.plain-text") ) ) | |
166 | { | |
167 | m_type = wxDF_TEXT; | |
168 | } | |
169 | else if ( UTTypeConformsTo( (CFStringRef)format, CFSTR("public.tiff") ) ) | |
170 | { | |
171 | m_type = wxDF_BITMAP; | |
172 | } | |
173 | else if ( UTTypeConformsTo( (CFStringRef)format, CFSTR("com.adobe.pdf") ) ) | |
174 | { | |
175 | m_type = wxDF_METAFILE; | |
176 | } | |
177 | else if ( UTTypeConformsTo( (CFStringRef)format, CFSTR("public.file-url") ) ) | |
178 | { | |
179 | m_type = wxDF_FILENAME; | |
180 | } | |
181 | else | |
182 | { | |
183 | m_type = wxDF_PRIVATE; | |
184 | m_id = wxCFStringRef( (CFStringRef) CFRetain((CFStringRef) format )).AsString(); | |
185 | } | |
186 | } | |
187 | ||
188 | void wxDataFormat::SetId( const wxString& zId ) | |
189 | { | |
190 | m_type = wxDF_PRIVATE; | |
191 | m_id = zId; | |
192 | if ( m_format != 0 ) | |
193 | { | |
194 | CFRelease( (CFStringRef) m_format ); | |
195 | m_format = 0; | |
196 | } | |
197 | // since it is private, no need to conform to anything ... | |
198 | m_format = (long) wxCFRetain( (CFStringRef) wxCFStringRef(m_id) ); | |
199 | } | |
200 | ||
201 | bool wxDataFormat::operator==(const wxDataFormat& format) const | |
202 | { | |
203 | if (IsStandard() || format.IsStandard()) | |
204 | return (format.m_type == m_type); | |
205 | else | |
206 | return ( UTTypeConformsTo( (CFStringRef) m_format , (CFStringRef) format.m_format ) ); | |
207 | } | |
208 | ||
209 | //------------------------------------------------------------------------- | |
210 | // wxDataObject | |
211 | //------------------------------------------------------------------------- | |
212 | ||
213 | wxDataObject::wxDataObject() | |
214 | { | |
215 | } | |
216 | ||
217 | bool wxDataObject::IsSupportedFormat( const wxDataFormat& rFormat, Direction vDir ) const | |
218 | { | |
219 | size_t nFormatCount = GetFormatCount( vDir ); | |
220 | bool found = false; | |
221 | ||
222 | if (nFormatCount == 1) | |
223 | { | |
224 | found = (rFormat == GetPreferredFormat()); | |
225 | } | |
226 | else | |
227 | { | |
228 | wxDataFormat *pFormats = new wxDataFormat[nFormatCount]; | |
229 | GetAllFormats( pFormats, vDir ); | |
230 | ||
231 | for (size_t n = 0; n < nFormatCount; n++) | |
232 | { | |
233 | if (pFormats[n] == rFormat) | |
234 | { | |
235 | found = true; | |
236 | break; | |
237 | } | |
238 | } | |
239 | ||
240 | delete [] pFormats; | |
241 | } | |
242 | ||
243 | return found; | |
244 | } | |
245 | ||
246 | void wxDataObject::AddToPasteboard( void * pb, int itemID ) | |
247 | { | |
248 | PasteboardRef pasteboard = (PasteboardRef) pb; | |
249 | // get formats from wxDataObjects | |
250 | wxDataFormat *array = new wxDataFormat[ GetFormatCount() ]; | |
251 | GetAllFormats( array ); | |
252 | ||
253 | for (size_t i = 0; i < GetFormatCount(); i++) | |
254 | { | |
255 | wxDataFormat thisFormat = array[ i ]; | |
256 | ||
257 | // add four bytes at the end for data objs like text that | |
258 | // have a datasize = strlen but still need a buffer for the | |
259 | // string including trailing zero | |
260 | ||
261 | size_t datasize = GetDataSize( thisFormat ); | |
262 | size_t sz = datasize + 4; | |
263 | void* buf = malloc( sz ); | |
264 | if ( buf != NULL ) | |
265 | { | |
266 | // empty the buffer because in some case GetDataHere does not fill buf | |
267 | memset( buf, 0, sz ); | |
268 | if ( GetDataHere( array[ i ], buf ) ) | |
269 | { | |
270 | int counter = 1 ; | |
271 | if ( thisFormat.GetType() == wxDF_FILENAME ) | |
272 | { | |
273 | // the data is D-normalized UTF8 strings of filenames delimited with \n | |
274 | char *fname = strtok((char*) buf,"\n"); | |
275 | while (fname != NULL) | |
276 | { | |
277 | // translate the filepath into a fileurl and put that into the pasteobard | |
278 | CFStringRef path = CFStringCreateWithBytes(NULL,(UInt8*)fname,strlen(fname),kCFStringEncodingUTF8,false); | |
279 | CFURLRef url = CFURLCreateWithFileSystemPath(NULL, path , kCFURLPOSIXPathStyle, false); | |
280 | CFRelease(path); | |
281 | CFDataRef data = CFURLCreateData(NULL,url,kCFStringEncodingUTF8,true); | |
282 | CFRelease(url); | |
283 | PasteboardPutItemFlavor( pasteboard, (PasteboardItemID) counter, | |
284 | (CFStringRef) thisFormat.GetFormatId() , data, kPasteboardFlavorNoFlags); | |
285 | CFRelease( data ); | |
286 | counter++; | |
287 | fname = strtok (NULL,"\n"); | |
288 | } | |
289 | ||
290 | } | |
291 | else | |
292 | { | |
293 | CFDataRef data = CFDataCreate( kCFAllocatorDefault, (UInt8*)buf, datasize ); | |
294 | if ( thisFormat.GetType() == wxDF_TEXT ) | |
295 | PasteboardPutItemFlavor( pasteboard, (PasteboardItemID) itemID, | |
296 | CFSTR("com.apple.traditional-mac-plain-text") , data, kPasteboardFlavorNoFlags); | |
297 | else | |
298 | PasteboardPutItemFlavor( pasteboard, (PasteboardItemID) itemID, | |
299 | (CFStringRef) thisFormat.GetFormatId() , data, kPasteboardFlavorNoFlags); | |
300 | CFRelease( data ); | |
301 | } | |
302 | } | |
303 | free( buf ); | |
304 | } | |
305 | } | |
306 | ||
307 | delete [] array; | |
308 | } | |
309 | ||
310 | bool wxDataObject::IsFormatInPasteboard( void * pb, const wxDataFormat &dataFormat ) | |
311 | { | |
312 | PasteboardRef pasteboard = (PasteboardRef) pb; | |
313 | bool hasData = false; | |
314 | OSStatus err = noErr; | |
315 | ItemCount itemCount; | |
316 | ||
317 | // we synchronize here once again, so we don't mind which flags get returned | |
318 | PasteboardSynchronize( pasteboard ); | |
319 | ||
320 | err = PasteboardGetItemCount( pasteboard, &itemCount ); | |
321 | if ( err == noErr ) | |
322 | { | |
323 | for( UInt32 itemIndex = 1; itemIndex <= itemCount && hasData == false ; itemIndex++ ) | |
324 | { | |
325 | PasteboardItemID itemID; | |
326 | CFArrayRef flavorTypeArray; | |
327 | CFIndex flavorCount; | |
328 | ||
329 | err = PasteboardGetItemIdentifier( pasteboard, itemIndex, &itemID ); | |
330 | if ( err != noErr ) | |
331 | continue; | |
332 | ||
333 | err = PasteboardCopyItemFlavors( pasteboard, itemID, &flavorTypeArray ); | |
334 | if ( err != noErr ) | |
335 | continue; | |
336 | ||
337 | flavorCount = CFArrayGetCount( flavorTypeArray ); | |
338 | ||
339 | for( CFIndex flavorIndex = 0; flavorIndex < flavorCount && hasData == false ; flavorIndex++ ) | |
340 | { | |
341 | CFStringRef flavorType; | |
342 | ||
343 | flavorType = (CFStringRef)CFArrayGetValueAtIndex( flavorTypeArray, | |
344 | flavorIndex ); | |
345 | ||
346 | wxDataFormat flavorFormat( (wxDataFormat::NativeFormat) flavorType ); | |
347 | if ( dataFormat == flavorFormat ) | |
348 | hasData = true; | |
349 | else if ( dataFormat.GetType() == wxDF_UNICODETEXT && flavorFormat.GetType() == wxDF_TEXT ) | |
350 | hasData = true; | |
351 | } | |
352 | CFRelease (flavorTypeArray); | |
353 | } | |
354 | } | |
355 | ||
356 | return hasData; | |
357 | } | |
358 | ||
359 | bool wxDataObject::GetFromPasteboard( void * pb ) | |
360 | { | |
361 | PasteboardRef pasteboard = (PasteboardRef) pb; | |
362 | size_t formatcount = GetFormatCount() + 1; | |
363 | wxDataFormat *array = new wxDataFormat[ formatcount ]; | |
364 | array[0] = GetPreferredFormat(); | |
365 | GetAllFormats( &array[1] ); | |
366 | ItemCount itemCount = 0; | |
367 | wxString filenamesPassed; | |
368 | bool transferred = false; | |
369 | ||
370 | // we synchronize here once again, so we don't mind which flags get returned | |
371 | PasteboardSynchronize( pasteboard ); | |
372 | ||
373 | OSStatus err = PasteboardGetItemCount( pasteboard, &itemCount ); | |
374 | if ( err == noErr ) | |
375 | { | |
376 | for (size_t i = 0; !transferred && i < formatcount; i++) | |
377 | { | |
378 | // go through the data in our order of preference | |
379 | wxDataFormat dataFormat = array[ i ]; | |
380 | ||
381 | for( UInt32 itemIndex = 1; itemIndex <= itemCount && transferred == false ; itemIndex++ ) | |
382 | { | |
383 | PasteboardItemID itemID = 0; | |
384 | CFArrayRef flavorTypeArray = NULL; | |
385 | CFIndex flavorCount = 0; | |
386 | ||
387 | err = PasteboardGetItemIdentifier( pasteboard, itemIndex, &itemID ); | |
388 | if ( err != noErr ) | |
389 | continue; | |
390 | ||
391 | err = PasteboardCopyItemFlavors( pasteboard, itemID, &flavorTypeArray ); | |
392 | if ( err != noErr ) | |
393 | continue; | |
394 | ||
395 | flavorCount = CFArrayGetCount( flavorTypeArray ); | |
396 | ||
397 | for( CFIndex flavorIndex = 0; !transferred && flavorIndex < flavorCount ; flavorIndex++ ) | |
398 | { | |
399 | CFStringRef flavorType; | |
400 | CFDataRef flavorData; | |
401 | CFIndex flavorDataSize; | |
402 | ||
403 | flavorType = (CFStringRef)CFArrayGetValueAtIndex( flavorTypeArray, | |
404 | flavorIndex ); | |
405 | ||
406 | wxDataFormat flavorFormat( (wxDataFormat::NativeFormat) flavorType ); | |
407 | ||
408 | if ( dataFormat == flavorFormat ) | |
409 | { | |
410 | err = PasteboardCopyItemFlavorData( pasteboard, itemID, flavorType , &flavorData ); | |
411 | if ( err == noErr ) | |
412 | { | |
413 | flavorDataSize = CFDataGetLength( flavorData ); | |
414 | if (dataFormat.GetType() == wxDF_FILENAME ) | |
415 | { | |
416 | // revert the translation and decomposition to arrive at a proper utf8 string again | |
417 | CFURLRef url = CFURLCreateWithBytes( kCFAllocatorDefault, CFDataGetBytePtr( flavorData ), flavorDataSize, kCFStringEncodingUTF8, NULL ); | |
418 | CFStringRef cfString = CFURLCopyFileSystemPath( url, kCFURLPOSIXPathStyle ); | |
419 | CFRelease( url ); | |
420 | CFMutableStringRef cfMutableString = CFStringCreateMutableCopy(NULL, 0, cfString); | |
421 | CFRelease( cfString ); | |
422 | CFStringNormalize(cfMutableString,kCFStringNormalizationFormC); | |
423 | wxString path = wxCFStringRef(cfMutableString).AsString(); | |
424 | if (!path.empty()) | |
425 | filenamesPassed += path + wxT("\n"); | |
426 | } | |
427 | else | |
428 | { | |
429 | // because some data implementation expect trailing a trailing NUL, we add some headroom | |
430 | void *buf = malloc( flavorDataSize + 4 ); | |
431 | if ( buf ) | |
432 | { | |
433 | memset( buf, 0, flavorDataSize + 4 ); | |
434 | memcpy( buf, CFDataGetBytePtr( flavorData ), flavorDataSize ); | |
435 | ||
436 | if (dataFormat.GetType() == wxDF_TEXT) | |
437 | wxMacConvertNewlines10To13( (char*) buf ); | |
438 | SetData( flavorFormat, flavorDataSize, buf ); | |
439 | transferred = true; | |
440 | free( buf ); | |
441 | } | |
442 | } | |
443 | CFRelease (flavorData); | |
444 | } | |
445 | } | |
446 | else if ( dataFormat.GetType() == wxDF_UNICODETEXT && flavorFormat.GetType() == wxDF_TEXT ) | |
447 | { | |
448 | err = PasteboardCopyItemFlavorData( pasteboard, itemID, flavorType, &flavorData ); | |
449 | if ( err == noErr ) | |
450 | { | |
451 | flavorDataSize = CFDataGetLength( flavorData ); | |
452 | void *asciibuf = malloc( flavorDataSize + 1 ); | |
453 | if ( asciibuf ) | |
454 | { | |
455 | memset( asciibuf, 0, flavorDataSize + 1 ); | |
456 | memcpy( asciibuf, CFDataGetBytePtr( flavorData ), flavorDataSize ); | |
457 | CFRelease (flavorData); | |
458 | ||
459 | SetData( wxDF_TEXT, flavorDataSize, asciibuf ); | |
460 | transferred = true; | |
461 | free( asciibuf ); | |
462 | } | |
463 | else | |
464 | CFRelease (flavorData); | |
465 | } | |
466 | } | |
467 | } | |
468 | CFRelease( flavorTypeArray ); | |
469 | } | |
470 | if (filenamesPassed.length() > 0) | |
471 | { | |
472 | wxCharBuffer buf = filenamesPassed.fn_str(); | |
473 | SetData( wxDF_FILENAME, strlen( buf ), (const char*)buf ); | |
474 | transferred = true; | |
475 | } | |
476 | } | |
477 | } | |
478 | return transferred; | |
479 | } | |
480 | ||
481 | bool wxDataObject::HasDataInPasteboard( void * pb ) | |
482 | { | |
483 | PasteboardRef pasteboard = (PasteboardRef) pb; | |
484 | size_t formatcount = GetFormatCount() + 1; | |
485 | wxDataFormat *array = new wxDataFormat[ formatcount ]; | |
486 | array[0] = GetPreferredFormat(); | |
487 | GetAllFormats( &array[1] ); | |
488 | ItemCount itemCount = 0; | |
489 | bool hasData = false; | |
490 | ||
491 | // we synchronize here once again, so we don't mind which flags get returned | |
492 | PasteboardSynchronize( pasteboard ); | |
493 | ||
494 | OSStatus err = PasteboardGetItemCount( pasteboard, &itemCount ); | |
495 | if ( err == noErr ) | |
496 | { | |
497 | for (size_t i = 0; !hasData && i < formatcount; i++) | |
498 | { | |
499 | // go through the data in our order of preference | |
500 | wxDataFormat dataFormat = array[ i ]; | |
501 | ||
502 | for( UInt32 itemIndex = 1; itemIndex <= itemCount && hasData == false ; itemIndex++ ) | |
503 | { | |
504 | PasteboardItemID itemID = 0; | |
505 | CFArrayRef flavorTypeArray = NULL; | |
506 | CFIndex flavorCount = 0; | |
507 | ||
508 | err = PasteboardGetItemIdentifier( pasteboard, itemIndex, &itemID ); | |
509 | if ( err != noErr ) | |
510 | continue; | |
511 | ||
512 | err = PasteboardCopyItemFlavors( pasteboard, itemID, &flavorTypeArray ); | |
513 | if ( err != noErr ) | |
514 | continue; | |
515 | ||
516 | flavorCount = CFArrayGetCount( flavorTypeArray ); | |
517 | ||
518 | for( CFIndex flavorIndex = 0; !hasData && flavorIndex < flavorCount ; flavorIndex++ ) | |
519 | { | |
520 | CFStringRef flavorType; | |
521 | ||
522 | flavorType = (CFStringRef)CFArrayGetValueAtIndex( flavorTypeArray, | |
523 | flavorIndex ); | |
524 | ||
525 | wxDataFormat flavorFormat( (wxDataFormat::NativeFormat) flavorType ); | |
526 | ||
527 | if ( dataFormat == flavorFormat || | |
528 | dataFormat.GetType() == wxDF_UNICODETEXT && flavorFormat.GetType() == wxDF_TEXT ) | |
529 | { | |
530 | hasData = true; | |
531 | } | |
532 | } | |
533 | CFRelease( flavorTypeArray ); | |
534 | } | |
535 | } | |
536 | } | |
537 | return hasData; | |
538 | } | |
539 | ||
540 | // ---------------------------------------------------------------------------- | |
541 | // wxTextDataObject | |
542 | // ---------------------------------------------------------------------------- | |
543 | ||
544 | #if wxUSE_UNICODE | |
545 | void wxTextDataObject::GetAllFormats(wxDataFormat *formats, | |
546 | wxDataObjectBase::Direction WXUNUSED(dir)) const | |
547 | { | |
548 | *formats++ = wxDataFormat( wxDF_TEXT ); | |
549 | *formats = wxDataFormat( wxDF_UNICODETEXT ); | |
550 | } | |
551 | #endif | |
552 | ||
553 | // ---------------------------------------------------------------------------- | |
554 | // wxFileDataObject | |
555 | // ---------------------------------------------------------------------------- | |
556 | ||
557 | void wxFileDataObject::GetFileNames( wxCharBuffer &buf ) const | |
558 | { | |
559 | wxString filenames; | |
560 | ||
561 | for (size_t i = 0; i < m_filenames.GetCount(); i++) | |
562 | { | |
563 | filenames += m_filenames[i]; | |
564 | filenames += wxT('\n'); | |
565 | } | |
566 | ||
567 | buf = filenames.fn_str(); | |
568 | } | |
569 | ||
570 | bool wxFileDataObject::GetDataHere( void *pBuf ) const | |
571 | { | |
572 | if (pBuf == NULL) | |
573 | return false; | |
574 | ||
575 | wxCharBuffer buf; | |
576 | size_t buffLength; | |
577 | ||
578 | GetFileNames( buf ); | |
579 | buffLength = strlen( buf ); | |
580 | memcpy( pBuf, (const char*)buf, buffLength + 1 ); | |
581 | ||
582 | return true; | |
583 | } | |
584 | ||
585 | size_t wxFileDataObject::GetDataSize() const | |
586 | { | |
587 | wxCharBuffer buf; | |
588 | size_t buffLength; | |
589 | ||
590 | GetFileNames( buf ); | |
591 | buffLength = strlen( buf ); | |
592 | // terminating 0 | |
593 | return buffLength + 1; | |
594 | } | |
595 | ||
596 | bool wxFileDataObject::SetData( size_t WXUNUSED(nSize), const void *pBuf ) | |
597 | { | |
598 | wxString filenames; | |
599 | ||
600 | #if wxUSE_UNICODE | |
601 | filenames = wxString( (const char*)pBuf, *wxConvFileName ); | |
602 | #else | |
603 | filenames = wxString (wxConvLocal.cWC2WX(wxConvFileName->cMB2WC( (const char*)pBuf))); | |
604 | #endif | |
605 | ||
606 | m_filenames = wxStringTokenize( filenames, wxT("\n"), wxTOKEN_STRTOK ); | |
607 | ||
608 | return true; | |
609 | } | |
610 | ||
611 | void wxFileDataObject::AddFile( const wxString& rFilename ) | |
612 | { | |
613 | m_filenames.Add( rFilename ); | |
614 | } | |
615 | ||
616 | // ---------------------------------------------------------------------------- | |
617 | // wxBitmapDataObject | |
618 | // ---------------------------------------------------------------------------- | |
619 | ||
620 | wxBitmapDataObject::wxBitmapDataObject() | |
621 | { | |
622 | Init(); | |
623 | } | |
624 | ||
625 | wxBitmapDataObject::wxBitmapDataObject( const wxBitmap& rBitmap ) | |
626 | : wxBitmapDataObjectBase( rBitmap ) | |
627 | { | |
628 | Init(); | |
629 | ||
630 | if (m_bitmap.Ok()) | |
631 | { | |
632 | SetBitmap( rBitmap ); | |
633 | } | |
634 | } | |
635 | ||
636 | wxBitmapDataObject::~wxBitmapDataObject() | |
637 | { | |
638 | Clear(); | |
639 | } | |
640 | ||
641 | void wxBitmapDataObject::SetBitmap( const wxBitmap& rBitmap ) | |
642 | { | |
643 | Clear(); | |
644 | wxBitmapDataObjectBase::SetBitmap( rBitmap ); | |
645 | if (m_bitmap.Ok()) | |
646 | { | |
647 | CGImageRef cgImageRef = (CGImageRef) m_bitmap.CreateCGImage(); | |
648 | ||
649 | CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0); | |
650 | CGImageDestinationRef destination = CGImageDestinationCreateWithData( data , kUTTypeTIFF , 1 , NULL ); | |
651 | if ( destination ) | |
652 | { | |
653 | CGImageDestinationAddImage( destination, cgImageRef, NULL ); | |
654 | CGImageDestinationFinalize( destination ); | |
655 | CFRelease( destination ); | |
656 | } | |
657 | m_pictHandle = NewHandle(CFDataGetLength(data)); | |
658 | if ( m_pictHandle ) | |
659 | { | |
660 | memcpy( *(Handle)m_pictHandle, (const char *)CFDataGetBytePtr(data), CFDataGetLength(data) ); | |
661 | } | |
662 | CFRelease( data ); | |
663 | ||
664 | CGImageRelease(cgImageRef); | |
665 | } | |
666 | } | |
667 | ||
668 | void wxBitmapDataObject::Init() | |
669 | { | |
670 | m_pictHandle = NULL; | |
671 | m_pictCreated = false; | |
672 | } | |
673 | ||
674 | void wxBitmapDataObject::Clear() | |
675 | { | |
676 | if (m_pictHandle != NULL) | |
677 | { | |
678 | DisposeHandle( (Handle) m_pictHandle ); | |
679 | m_pictHandle = NULL; | |
680 | } | |
681 | m_pictCreated = false; | |
682 | } | |
683 | ||
684 | bool wxBitmapDataObject::GetDataHere( void *pBuf ) const | |
685 | { | |
686 | if (m_pictHandle == NULL) | |
687 | { | |
688 | wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") ); | |
689 | return false; | |
690 | } | |
691 | ||
692 | if (pBuf == NULL) | |
693 | return false; | |
694 | ||
695 | memcpy( pBuf, *(Handle)m_pictHandle, GetHandleSize( (Handle)m_pictHandle ) ); | |
696 | ||
697 | return true; | |
698 | } | |
699 | ||
700 | size_t wxBitmapDataObject::GetDataSize() const | |
701 | { | |
702 | if (m_pictHandle != NULL) | |
703 | return GetHandleSize( (Handle)m_pictHandle ); | |
704 | else | |
705 | return 0; | |
706 | } | |
707 | ||
708 | Handle MacCreateDataReferenceHandle(Handle theDataHandle) | |
709 | { | |
710 | Handle dataRef = NULL; | |
711 | OSErr err = noErr; | |
712 | ||
713 | // Create a data reference handle for our data. | |
714 | err = PtrToHand( &theDataHandle, &dataRef, sizeof(Handle)); | |
715 | ||
716 | return dataRef; | |
717 | } | |
718 | ||
719 | bool wxBitmapDataObject::SetData( size_t nSize, const void *pBuf ) | |
720 | { | |
721 | Clear(); | |
722 | ||
723 | if ((pBuf == NULL) || (nSize == 0)) | |
724 | return false; | |
725 | ||
726 | Handle picHandle = NewHandle( nSize ); | |
727 | memcpy( *picHandle, pBuf, nSize ); | |
728 | m_pictHandle = picHandle; | |
729 | CGImageRef cgImageRef = 0; | |
730 | ||
731 | CFDataRef data = CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, (const UInt8*) pBuf, nSize, kCFAllocatorNull); | |
732 | CGImageSourceRef source = CGImageSourceCreateWithData( data, NULL ); | |
733 | if ( source ) | |
734 | { | |
735 | cgImageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL); | |
736 | } | |
737 | CFRelease( source ); | |
738 | CFRelease( data ); | |
739 | ||
740 | if ( cgImageRef ) | |
741 | { | |
742 | m_bitmap.Create( CGImageGetWidth(cgImageRef) , CGImageGetHeight(cgImageRef) ); | |
743 | CGRect r = CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef) , CGImageGetHeight(cgImageRef) ); | |
744 | // since our context is upside down we dont use CGContextDrawImage | |
745 | wxMacDrawCGImage( (CGContextRef) m_bitmap.GetHBITMAP() , &r, cgImageRef ) ; | |
746 | CGImageRelease(cgImageRef); | |
747 | cgImageRef = NULL; | |
748 | } | |
749 | ||
750 | return m_bitmap.Ok(); | |
751 | } | |
752 | ||
753 | #endif |