1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/carbon/dataobj.cpp
3 // Purpose: implementation of wxDataObject class
4 // Author: Stefan Csomor
8 // Copyright: (c) 1999 Stefan Csomor
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
17 #include "wx/dataobj.h"
22 #include "wx/dcmemory.h"
26 #include "wx/mstream.h"
27 #include "wx/metafile.h"
28 #include "wx/tokenzr.h"
30 #include "wx/osx/private.h"
32 #if wxOSX_USE_COCOA_OR_CARBON
33 #include <QuickTime/QuickTime.h>
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 wxDataFormat::wxDataFormat()
43 m_type
= wxDF_INVALID
;
47 wxDataFormat::wxDataFormat( wxDataFormatId vType
)
50 m_type
= wxDF_INVALID
;
54 wxDataFormat::wxDataFormat( const wxChar
*zId
)
57 m_type
= wxDF_INVALID
;
61 wxDataFormat::wxDataFormat( const wxString
& rId
)
64 m_type
= wxDF_INVALID
;
68 wxDataFormat::wxDataFormat(const wxDataFormat
& rFormat
)
70 if ( rFormat
.m_format
)
71 m_format
= (NativeFormat
) CFStringCreateCopy(NULL
, (CFStringRef
)rFormat
.m_format
);
74 m_type
= rFormat
.m_type
;
78 wxDataFormat::wxDataFormat( NativeFormat vFormat
)
81 m_type
= wxDF_INVALID
;
85 wxDataFormat::~wxDataFormat()
89 CFRelease( (CFStringRef
) m_format
);
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
98 wxDataFormat
& wxDataFormat::operator=(const wxDataFormat
& rFormat
)
102 CFRelease( (CFStringRef
) m_format
);
105 if ( rFormat
.m_format
)
106 m_format
= (NativeFormat
) CFStringCreateCopy(NULL
, (CFStringRef
)rFormat
.m_format
);
107 m_type
= rFormat
.m_type
;
112 void wxDataFormat::SetType( wxDataFormatId dataType
)
117 CFRelease( (CFStringRef
) m_format
);
124 m_format
= (long) CFStringCreateCopy( NULL
, CFSTR("public.plain-text") );
127 case wxDF_UNICODETEXT
:
128 m_format
= (long) CFStringCreateCopy( NULL
, CFSTR("public.utf16-plain-text") );
132 m_format
= (long) CFStringCreateCopy( NULL
, CFSTR("public.tiff") );
135 m_format
= (long) CFStringCreateCopy( NULL
, CFSTR("com.adobe.pdf") );
139 m_format
= (long) CFStringCreateCopy( NULL
, CFSTR("public.file-url") );
143 wxFAIL_MSG( wxT("invalid data format") );
148 wxString
wxDataFormat::GetId() const
150 return wxCFStringRef(wxCFRetain((CFStringRef
)m_format
)).AsString();
153 void wxDataFormat::SetId( NativeFormat format
)
157 CFRelease( (CFStringRef
) m_format
);
160 m_format
= (NativeFormat
) CFStringCreateCopy(NULL
, (CFStringRef
)format
);
161 if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.utf16-plain-text") ) )
163 m_type
= wxDF_UNICODETEXT
;
165 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.utf16-external-plain-text") ) )
167 m_type
= wxDF_UNICODETEXT
;
169 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.plain-text") ) )
173 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.tiff") ) )
175 m_type
= wxDF_BITMAP
;
177 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("com.adobe.pdf") ) )
179 m_type
= wxDF_METAFILE
;
181 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.file-url") ) )
183 m_type
= wxDF_FILENAME
;
187 m_type
= wxDF_PRIVATE
;
188 m_id
= wxCFStringRef( (CFStringRef
) CFRetain((CFStringRef
) format
)).AsString();
192 void wxDataFormat::SetId( const wxString
& zId
)
194 m_type
= wxDF_PRIVATE
;
198 CFRelease( (CFStringRef
) m_format
);
201 // since it is private, no need to conform to anything ...
202 m_format
= (long) wxCFRetain( (CFStringRef
) wxCFStringRef(m_id
) );
205 bool wxDataFormat::operator==(const wxDataFormat
& format
) const
207 if (IsStandard() || format
.IsStandard())
208 return (format
.m_type
== m_type
);
210 return ( UTTypeConformsTo( (CFStringRef
) m_format
, (CFStringRef
) format
.m_format
) );
213 //-------------------------------------------------------------------------
215 //-------------------------------------------------------------------------
217 wxDataObject::wxDataObject()
221 bool wxDataObject::IsSupportedFormat( const wxDataFormat
& rFormat
, Direction vDir
) const
223 size_t nFormatCount
= GetFormatCount( vDir
);
226 if (nFormatCount
== 1)
228 found
= (rFormat
== GetPreferredFormat());
232 wxDataFormat
*pFormats
= new wxDataFormat
[nFormatCount
];
233 GetAllFormats( pFormats
, vDir
);
235 for (size_t n
= 0; n
< nFormatCount
; n
++)
237 if (pFormats
[n
] == rFormat
)
250 void wxDataObject::AddToPasteboard( void * pb
, int itemID
)
252 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
253 // get formats from wxDataObjects
254 wxDataFormat
*array
= new wxDataFormat
[ GetFormatCount() ];
255 GetAllFormats( array
);
257 for (size_t i
= 0; i
< GetFormatCount(); i
++)
259 wxDataFormat thisFormat
= array
[ i
];
261 // add four bytes at the end for data objs like text that
262 // have a datasize = strlen but still need a buffer for the
263 // string including trailing zero
265 size_t datasize
= GetDataSize( thisFormat
);
266 size_t sz
= datasize
+ 4;
267 void* buf
= malloc( sz
);
270 // empty the buffer because in some case GetDataHere does not fill buf
271 memset( buf
, 0, sz
);
272 if ( GetDataHere( array
[ i
], buf
) )
275 if ( thisFormat
.GetType() == wxDF_FILENAME
)
277 // the data is D-normalized UTF8 strings of filenames delimited with \n
278 char *fname
= strtok((char*) buf
,"\n");
279 while (fname
!= NULL
)
281 // translate the filepath into a fileurl and put that into the pasteobard
282 CFStringRef path
= CFStringCreateWithBytes(NULL
,(UInt8
*)fname
,strlen(fname
),kCFStringEncodingUTF8
,false);
283 CFURLRef url
= CFURLCreateWithFileSystemPath(NULL
, path
, kCFURLPOSIXPathStyle
, false);
285 CFDataRef data
= CFURLCreateData(NULL
,url
,kCFStringEncodingUTF8
,true);
287 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) counter
,
288 (CFStringRef
) thisFormat
.GetFormatId() , data
, kPasteboardFlavorNoFlags
);
291 fname
= strtok (NULL
,"\n");
297 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*)buf
, datasize
);
298 if ( thisFormat
.GetType() == wxDF_TEXT
)
299 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) itemID
,
300 CFSTR("com.apple.traditional-mac-plain-text") , data
, kPasteboardFlavorNoFlags
);
302 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) itemID
,
303 (CFStringRef
) thisFormat
.GetFormatId() , data
, kPasteboardFlavorNoFlags
);
314 bool wxDataObject::IsFormatInPasteboard( void * pb
, const wxDataFormat
&dataFormat
)
316 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
317 bool hasData
= false;
318 OSStatus err
= noErr
;
321 // we synchronize here once again, so we don't mind which flags get returned
322 PasteboardSynchronize( pasteboard
);
324 err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
327 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& hasData
== false ; itemIndex
++ )
329 PasteboardItemID itemID
;
330 CFArrayRef flavorTypeArray
;
333 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
337 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
341 flavorCount
= CFArrayGetCount( flavorTypeArray
);
343 for( CFIndex flavorIndex
= 0; flavorIndex
< flavorCount
&& hasData
== false ; flavorIndex
++ )
345 CFStringRef flavorType
;
347 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
350 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
351 if ( dataFormat
== flavorFormat
)
353 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
356 CFRelease (flavorTypeArray
);
363 bool wxDataObject::GetFromPasteboard( void * pb
)
365 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
366 size_t formatcount
= GetFormatCount() + 1;
367 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
368 array
[0] = GetPreferredFormat();
369 GetAllFormats( &array
[1] );
370 ItemCount itemCount
= 0;
371 wxString filenamesPassed
;
372 bool transferred
= false;
374 // we synchronize here once again, so we don't mind which flags get returned
375 PasteboardSynchronize( pasteboard
);
377 OSStatus err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
380 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
382 // go through the data in our order of preference
383 wxDataFormat dataFormat
= array
[ i
];
385 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& transferred
== false ; itemIndex
++ )
387 PasteboardItemID itemID
= 0;
388 CFArrayRef flavorTypeArray
= NULL
;
389 CFIndex flavorCount
= 0;
391 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
395 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
399 flavorCount
= CFArrayGetCount( flavorTypeArray
);
401 for( CFIndex flavorIndex
= 0; !transferred
&& flavorIndex
< flavorCount
; flavorIndex
++ )
403 CFStringRef flavorType
;
404 CFDataRef flavorData
;
405 CFIndex flavorDataSize
;
407 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
410 // avoid utf8 being treated closer to plain-text than unicode by forcing a conversion
411 if ( UTTypeConformsTo(flavorType
, CFSTR("public.utf8-plain-text") ) )
413 flavorType
= CFSTR("public.utf16-plain-text");
415 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
417 if ( dataFormat
== flavorFormat
)
419 err
= PasteboardCopyItemFlavorData( pasteboard
, itemID
, flavorType
, &flavorData
);
422 flavorDataSize
= CFDataGetLength( flavorData
);
423 if (dataFormat
.GetType() == wxDF_FILENAME
)
425 // revert the translation and decomposition to arrive at a proper utf8 string again
426 CFURLRef url
= CFURLCreateWithBytes( kCFAllocatorDefault
, CFDataGetBytePtr( flavorData
), flavorDataSize
, kCFStringEncodingUTF8
, NULL
);
427 CFStringRef cfString
= CFURLCopyFileSystemPath( url
, kCFURLPOSIXPathStyle
);
429 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, cfString
);
430 CFRelease( cfString
);
431 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormC
);
432 wxString path
= wxCFStringRef(cfMutableString
).AsString();
434 filenamesPassed
+= path
+ wxT("\n");
438 // because some data implementation expect trailing a trailing NUL, we add some headroom
439 void *buf
= malloc( flavorDataSize
+ 4 );
442 memset( buf
, 0, flavorDataSize
+ 4 );
443 memcpy( buf
, CFDataGetBytePtr( flavorData
), flavorDataSize
);
445 if (dataFormat
.GetType() == wxDF_TEXT
)
446 wxMacConvertNewlines10To13( (char*) buf
);
447 SetData( flavorFormat
, flavorDataSize
, buf
);
452 CFRelease (flavorData
);
455 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
457 err
= PasteboardCopyItemFlavorData( pasteboard
, itemID
, flavorType
, &flavorData
);
460 flavorDataSize
= CFDataGetLength( flavorData
);
461 void *asciibuf
= malloc( flavorDataSize
+ 1 );
464 memset( asciibuf
, 0, flavorDataSize
+ 1 );
465 memcpy( asciibuf
, CFDataGetBytePtr( flavorData
), flavorDataSize
);
466 CFRelease (flavorData
);
468 SetData( wxDF_TEXT
, flavorDataSize
, asciibuf
);
473 CFRelease (flavorData
);
477 CFRelease( flavorTypeArray
);
479 if ( !filenamesPassed
.empty() )
481 wxCharBuffer buf
= filenamesPassed
.fn_str();
482 SetData( wxDF_FILENAME
, strlen( buf
), (const char*)buf
);
490 bool wxDataObject::HasDataInPasteboard( void * pb
)
492 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
493 size_t formatcount
= GetFormatCount() + 1;
494 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
495 array
[0] = GetPreferredFormat();
496 GetAllFormats( &array
[1] );
497 ItemCount itemCount
= 0;
498 bool hasData
= false;
500 // we synchronize here once again, so we don't mind which flags get returned
501 PasteboardSynchronize( pasteboard
);
503 OSStatus err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
506 for (size_t i
= 0; !hasData
&& i
< formatcount
; i
++)
508 // go through the data in our order of preference
509 wxDataFormat dataFormat
= array
[ i
];
511 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& hasData
== false ; itemIndex
++ )
513 PasteboardItemID itemID
= 0;
514 CFArrayRef flavorTypeArray
= NULL
;
515 CFIndex flavorCount
= 0;
517 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
521 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
525 flavorCount
= CFArrayGetCount( flavorTypeArray
);
527 for( CFIndex flavorIndex
= 0; !hasData
&& flavorIndex
< flavorCount
; flavorIndex
++ )
529 CFStringRef flavorType
;
531 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
534 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
536 if ( dataFormat
== flavorFormat
||
537 dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
542 CFRelease( flavorTypeArray
);
549 // ----------------------------------------------------------------------------
551 // ----------------------------------------------------------------------------
554 void wxTextDataObject::GetAllFormats(wxDataFormat
*formats
,
555 wxDataObjectBase::Direction
WXUNUSED(dir
)) const
557 *formats
++ = wxDataFormat( wxDF_TEXT
);
558 *formats
= wxDataFormat( wxDF_UNICODETEXT
);
562 // ----------------------------------------------------------------------------
564 // ----------------------------------------------------------------------------
566 void wxFileDataObject::GetFileNames( wxCharBuffer
&buf
) const
570 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
572 filenames
+= m_filenames
[i
];
573 filenames
+= wxT('\n');
576 buf
= filenames
.fn_str();
579 bool wxFileDataObject::GetDataHere( void *pBuf
) const
588 buffLength
= strlen( buf
);
589 memcpy( pBuf
, (const char*)buf
, buffLength
+ 1 );
594 size_t wxFileDataObject::GetDataSize() const
600 buffLength
= strlen( buf
);
602 return buffLength
+ 1;
605 bool wxFileDataObject::SetData( size_t WXUNUSED(nSize
), const void *pBuf
)
610 filenames
= wxString( (const char*)pBuf
, *wxConvFileName
);
612 filenames
= wxString (wxConvLocal
.cWC2WX(wxConvFileName
->cMB2WC( (const char*)pBuf
)));
615 m_filenames
= wxStringTokenize( filenames
, wxT("\n"), wxTOKEN_STRTOK
);
620 void wxFileDataObject::AddFile( const wxString
& rFilename
)
622 m_filenames
.Add( rFilename
);
625 // ----------------------------------------------------------------------------
626 // wxBitmapDataObject
627 // ----------------------------------------------------------------------------
629 wxBitmapDataObject::wxBitmapDataObject()
634 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap
)
635 : wxBitmapDataObjectBase( rBitmap
)
641 SetBitmap( rBitmap
);
645 wxBitmapDataObject::~wxBitmapDataObject()
650 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap
)
653 wxBitmapDataObjectBase::SetBitmap( rBitmap
);
656 CGImageRef cgImageRef
= (CGImageRef
) m_bitmap
.CreateCGImage();
658 CFMutableDataRef data
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
659 CGImageDestinationRef destination
= CGImageDestinationCreateWithData( data
, kUTTypeTIFF
, 1 , NULL
);
662 CGImageDestinationAddImage( destination
, cgImageRef
, NULL
);
663 CGImageDestinationFinalize( destination
);
664 CFRelease( destination
);
666 m_pictHandle
= NewHandle(CFDataGetLength(data
));
669 memcpy( *(Handle
)m_pictHandle
, (const char *)CFDataGetBytePtr(data
), CFDataGetLength(data
) );
673 CGImageRelease(cgImageRef
);
677 void wxBitmapDataObject::Init()
680 m_pictCreated
= false;
683 void wxBitmapDataObject::Clear()
685 if (m_pictHandle
!= NULL
)
687 DisposeHandle( (Handle
) m_pictHandle
);
690 m_pictCreated
= false;
693 bool wxBitmapDataObject::GetDataHere( void *pBuf
) const
695 if (m_pictHandle
== NULL
)
697 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
704 memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle
) );
709 size_t wxBitmapDataObject::GetDataSize() const
711 if (m_pictHandle
!= NULL
)
712 return GetHandleSize( (Handle
)m_pictHandle
);
717 Handle
MacCreateDataReferenceHandle(Handle theDataHandle
)
719 Handle dataRef
= NULL
;
722 // Create a data reference handle for our data.
723 err
= PtrToHand( &theDataHandle
, &dataRef
, sizeof(Handle
));
728 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf
)
732 if ((pBuf
== NULL
) || (nSize
== 0))
735 Handle picHandle
= NewHandle( nSize
);
736 memcpy( *picHandle
, pBuf
, nSize
);
737 m_pictHandle
= picHandle
;
738 CGImageRef cgImageRef
= 0;
740 CFDataRef data
= CFDataCreateWithBytesNoCopy( kCFAllocatorDefault
, (const UInt8
*) pBuf
, nSize
, kCFAllocatorNull
);
741 CGImageSourceRef source
= CGImageSourceCreateWithData( data
, NULL
);
744 cgImageRef
= CGImageSourceCreateImageAtIndex(source
, 0, NULL
);
751 m_bitmap
.Create( CGImageGetWidth(cgImageRef
) , CGImageGetHeight(cgImageRef
) );
752 CGRect r
= CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef
) , CGImageGetHeight(cgImageRef
) );
753 // since our context is upside down we dont use CGContextDrawImage
754 wxMacDrawCGImage( (CGContextRef
) m_bitmap
.GetHBITMAP() , &r
, cgImageRef
) ;
755 CGImageRelease(cgImageRef
);
759 return m_bitmap
.Ok();