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