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.plain-text") ) )
169 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.tiff") ) )
171 m_type
= wxDF_BITMAP
;
173 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("com.adobe.pdf") ) )
175 m_type
= wxDF_METAFILE
;
177 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.file-url") ) )
179 m_type
= wxDF_FILENAME
;
183 m_type
= wxDF_PRIVATE
;
184 m_id
= wxCFStringRef( (CFStringRef
) CFRetain((CFStringRef
) format
)).AsString();
188 void wxDataFormat::SetId( const wxString
& zId
)
190 m_type
= wxDF_PRIVATE
;
194 CFRelease( (CFStringRef
) m_format
);
197 // since it is private, no need to conform to anything ...
198 m_format
= (long) wxCFRetain( (CFStringRef
) wxCFStringRef(m_id
) );
201 bool wxDataFormat::operator==(const wxDataFormat
& format
) const
203 if (IsStandard() || format
.IsStandard())
204 return (format
.m_type
== m_type
);
206 return ( UTTypeConformsTo( (CFStringRef
) m_format
, (CFStringRef
) format
.m_format
) );
209 //-------------------------------------------------------------------------
211 //-------------------------------------------------------------------------
213 wxDataObject::wxDataObject()
217 bool wxDataObject::IsSupportedFormat( const wxDataFormat
& rFormat
, Direction vDir
) const
219 size_t nFormatCount
= GetFormatCount( vDir
);
222 if (nFormatCount
== 1)
224 found
= (rFormat
== GetPreferredFormat());
228 wxDataFormat
*pFormats
= new wxDataFormat
[nFormatCount
];
229 GetAllFormats( pFormats
, vDir
);
231 for (size_t n
= 0; n
< nFormatCount
; n
++)
233 if (pFormats
[n
] == rFormat
)
246 void wxDataObject::AddToPasteboard( void * pb
, int itemID
)
248 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
249 // get formats from wxDataObjects
250 wxDataFormat
*array
= new wxDataFormat
[ GetFormatCount() ];
251 GetAllFormats( array
);
253 for (size_t i
= 0; i
< GetFormatCount(); i
++)
255 wxDataFormat thisFormat
= array
[ i
];
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
261 size_t datasize
= GetDataSize( thisFormat
);
262 size_t sz
= datasize
+ 4;
263 void* buf
= malloc( sz
);
266 // empty the buffer because in some case GetDataHere does not fill buf
267 memset( buf
, 0, sz
);
268 if ( GetDataHere( array
[ i
], buf
) )
271 if ( thisFormat
.GetType() == wxDF_FILENAME
)
273 // the data is D-normalized UTF8 strings of filenames delimited with \n
274 char *fname
= strtok((char*) buf
,"\n");
275 while (fname
!= NULL
)
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);
281 CFDataRef data
= CFURLCreateData(NULL
,url
,kCFStringEncodingUTF8
,true);
283 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) counter
,
284 (CFStringRef
) thisFormat
.GetFormatId() , data
, kPasteboardFlavorNoFlags
);
287 fname
= strtok (NULL
,"\n");
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
);
298 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) itemID
,
299 (CFStringRef
) thisFormat
.GetFormatId() , data
, kPasteboardFlavorNoFlags
);
310 bool wxDataObject::IsFormatInPasteboard( void * pb
, const wxDataFormat
&dataFormat
)
312 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
313 bool hasData
= false;
314 OSStatus err
= noErr
;
317 // we synchronize here once again, so we don't mind which flags get returned
318 PasteboardSynchronize( pasteboard
);
320 err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
323 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& hasData
== false ; itemIndex
++ )
325 PasteboardItemID itemID
;
326 CFArrayRef flavorTypeArray
;
329 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
333 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
337 flavorCount
= CFArrayGetCount( flavorTypeArray
);
339 for( CFIndex flavorIndex
= 0; flavorIndex
< flavorCount
&& hasData
== false ; flavorIndex
++ )
341 CFStringRef flavorType
;
343 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
346 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
347 if ( dataFormat
== flavorFormat
)
349 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
352 CFRelease (flavorTypeArray
);
359 bool wxDataObject::GetFromPasteboard( void * pb
)
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;
370 // we synchronize here once again, so we don't mind which flags get returned
371 PasteboardSynchronize( pasteboard
);
373 OSStatus err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
376 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
378 // go through the data in our order of preference
379 wxDataFormat dataFormat
= array
[ i
];
381 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& transferred
== false ; itemIndex
++ )
383 PasteboardItemID itemID
= 0;
384 CFArrayRef flavorTypeArray
= NULL
;
385 CFIndex flavorCount
= 0;
387 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
391 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
395 flavorCount
= CFArrayGetCount( flavorTypeArray
);
397 for( CFIndex flavorIndex
= 0; !transferred
&& flavorIndex
< flavorCount
; flavorIndex
++ )
399 CFStringRef flavorType
;
400 CFDataRef flavorData
;
401 CFIndex flavorDataSize
;
403 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
406 // avoid utf8 being treated closer to plain-text than unicode by forcing a conversion
407 if ( UTTypeConformsTo(flavorType
, CFSTR("public.utf8-plain-text") ) )
409 flavorType
= CFSTR("public.utf16-plain-text");
411 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
413 if ( dataFormat
== flavorFormat
)
415 err
= PasteboardCopyItemFlavorData( pasteboard
, itemID
, flavorType
, &flavorData
);
418 flavorDataSize
= CFDataGetLength( flavorData
);
419 if (dataFormat
.GetType() == wxDF_FILENAME
)
421 // revert the translation and decomposition to arrive at a proper utf8 string again
422 CFURLRef url
= CFURLCreateWithBytes( kCFAllocatorDefault
, CFDataGetBytePtr( flavorData
), flavorDataSize
, kCFStringEncodingUTF8
, NULL
);
423 CFStringRef cfString
= CFURLCopyFileSystemPath( url
, kCFURLPOSIXPathStyle
);
425 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, cfString
);
426 CFRelease( cfString
);
427 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormC
);
428 wxString path
= wxCFStringRef(cfMutableString
).AsString();
430 filenamesPassed
+= path
+ wxT("\n");
434 // because some data implementation expect trailing a trailing NUL, we add some headroom
435 void *buf
= malloc( flavorDataSize
+ 4 );
438 memset( buf
, 0, flavorDataSize
+ 4 );
439 memcpy( buf
, CFDataGetBytePtr( flavorData
), flavorDataSize
);
441 if (dataFormat
.GetType() == wxDF_TEXT
)
442 wxMacConvertNewlines10To13( (char*) buf
);
443 SetData( flavorFormat
, flavorDataSize
, buf
);
448 CFRelease (flavorData
);
451 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
453 err
= PasteboardCopyItemFlavorData( pasteboard
, itemID
, flavorType
, &flavorData
);
456 flavorDataSize
= CFDataGetLength( flavorData
);
457 void *asciibuf
= malloc( flavorDataSize
+ 1 );
460 memset( asciibuf
, 0, flavorDataSize
+ 1 );
461 memcpy( asciibuf
, CFDataGetBytePtr( flavorData
), flavorDataSize
);
462 CFRelease (flavorData
);
464 SetData( wxDF_TEXT
, flavorDataSize
, asciibuf
);
469 CFRelease (flavorData
);
473 CFRelease( flavorTypeArray
);
475 if (filenamesPassed
.length() > 0)
477 wxCharBuffer buf
= filenamesPassed
.fn_str();
478 SetData( wxDF_FILENAME
, strlen( buf
), (const char*)buf
);
486 bool wxDataObject::HasDataInPasteboard( void * pb
)
488 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
489 size_t formatcount
= GetFormatCount() + 1;
490 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
491 array
[0] = GetPreferredFormat();
492 GetAllFormats( &array
[1] );
493 ItemCount itemCount
= 0;
494 bool hasData
= false;
496 // we synchronize here once again, so we don't mind which flags get returned
497 PasteboardSynchronize( pasteboard
);
499 OSStatus err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
502 for (size_t i
= 0; !hasData
&& i
< formatcount
; i
++)
504 // go through the data in our order of preference
505 wxDataFormat dataFormat
= array
[ i
];
507 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& hasData
== false ; itemIndex
++ )
509 PasteboardItemID itemID
= 0;
510 CFArrayRef flavorTypeArray
= NULL
;
511 CFIndex flavorCount
= 0;
513 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
517 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
521 flavorCount
= CFArrayGetCount( flavorTypeArray
);
523 for( CFIndex flavorIndex
= 0; !hasData
&& flavorIndex
< flavorCount
; flavorIndex
++ )
525 CFStringRef flavorType
;
527 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
530 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
532 if ( dataFormat
== flavorFormat
||
533 dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
538 CFRelease( flavorTypeArray
);
545 // ----------------------------------------------------------------------------
547 // ----------------------------------------------------------------------------
550 void wxTextDataObject::GetAllFormats(wxDataFormat
*formats
,
551 wxDataObjectBase::Direction
WXUNUSED(dir
)) const
553 *formats
++ = wxDataFormat( wxDF_TEXT
);
554 *formats
= wxDataFormat( wxDF_UNICODETEXT
);
558 // ----------------------------------------------------------------------------
560 // ----------------------------------------------------------------------------
562 void wxFileDataObject::GetFileNames( wxCharBuffer
&buf
) const
566 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
568 filenames
+= m_filenames
[i
];
569 filenames
+= wxT('\n');
572 buf
= filenames
.fn_str();
575 bool wxFileDataObject::GetDataHere( void *pBuf
) const
584 buffLength
= strlen( buf
);
585 memcpy( pBuf
, (const char*)buf
, buffLength
+ 1 );
590 size_t wxFileDataObject::GetDataSize() const
596 buffLength
= strlen( buf
);
598 return buffLength
+ 1;
601 bool wxFileDataObject::SetData( size_t WXUNUSED(nSize
), const void *pBuf
)
606 filenames
= wxString( (const char*)pBuf
, *wxConvFileName
);
608 filenames
= wxString (wxConvLocal
.cWC2WX(wxConvFileName
->cMB2WC( (const char*)pBuf
)));
611 m_filenames
= wxStringTokenize( filenames
, wxT("\n"), wxTOKEN_STRTOK
);
616 void wxFileDataObject::AddFile( const wxString
& rFilename
)
618 m_filenames
.Add( rFilename
);
621 // ----------------------------------------------------------------------------
622 // wxBitmapDataObject
623 // ----------------------------------------------------------------------------
625 wxBitmapDataObject::wxBitmapDataObject()
630 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap
)
631 : wxBitmapDataObjectBase( rBitmap
)
637 SetBitmap( rBitmap
);
641 wxBitmapDataObject::~wxBitmapDataObject()
646 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap
)
649 wxBitmapDataObjectBase::SetBitmap( rBitmap
);
652 CGImageRef cgImageRef
= (CGImageRef
) m_bitmap
.CreateCGImage();
654 CFMutableDataRef data
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
655 CGImageDestinationRef destination
= CGImageDestinationCreateWithData( data
, kUTTypeTIFF
, 1 , NULL
);
658 CGImageDestinationAddImage( destination
, cgImageRef
, NULL
);
659 CGImageDestinationFinalize( destination
);
660 CFRelease( destination
);
662 m_pictHandle
= NewHandle(CFDataGetLength(data
));
665 memcpy( *(Handle
)m_pictHandle
, (const char *)CFDataGetBytePtr(data
), CFDataGetLength(data
) );
669 CGImageRelease(cgImageRef
);
673 void wxBitmapDataObject::Init()
676 m_pictCreated
= false;
679 void wxBitmapDataObject::Clear()
681 if (m_pictHandle
!= NULL
)
683 DisposeHandle( (Handle
) m_pictHandle
);
686 m_pictCreated
= false;
689 bool wxBitmapDataObject::GetDataHere( void *pBuf
) const
691 if (m_pictHandle
== NULL
)
693 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
700 memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle
) );
705 size_t wxBitmapDataObject::GetDataSize() const
707 if (m_pictHandle
!= NULL
)
708 return GetHandleSize( (Handle
)m_pictHandle
);
713 Handle
MacCreateDataReferenceHandle(Handle theDataHandle
)
715 Handle dataRef
= NULL
;
718 // Create a data reference handle for our data.
719 err
= PtrToHand( &theDataHandle
, &dataRef
, sizeof(Handle
));
724 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf
)
728 if ((pBuf
== NULL
) || (nSize
== 0))
731 Handle picHandle
= NewHandle( nSize
);
732 memcpy( *picHandle
, pBuf
, nSize
);
733 m_pictHandle
= picHandle
;
734 CGImageRef cgImageRef
= 0;
736 CFDataRef data
= CFDataCreateWithBytesNoCopy( kCFAllocatorDefault
, (const UInt8
*) pBuf
, nSize
, kCFAllocatorNull
);
737 CGImageSourceRef source
= CGImageSourceCreateWithData( data
, NULL
);
740 cgImageRef
= CGImageSourceCreateImageAtIndex(source
, 0, NULL
);
747 m_bitmap
.Create( CGImageGetWidth(cgImageRef
) , CGImageGetHeight(cgImageRef
) );
748 CGRect r
= CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef
) , CGImageGetHeight(cgImageRef
) );
749 // since our context is upside down we dont use CGContextDrawImage
750 wxMacDrawCGImage( (CGContextRef
) m_bitmap
.GetHBITMAP() , &r
, cgImageRef
) ;
751 CGImageRelease(cgImageRef
);
755 return m_bitmap
.Ok();