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.html") );
136 m_format
= (long) CFStringCreateCopy( NULL
, CFSTR("public.tiff") );
139 m_format
= (long) CFStringCreateCopy( NULL
, CFSTR("com.adobe.pdf") );
143 m_format
= (long) CFStringCreateCopy( NULL
, CFSTR("public.file-url") );
147 wxFAIL_MSG( wxT("invalid data format") );
152 wxString
wxDataFormat::GetId() const
154 return wxCFStringRef(wxCFRetain((CFStringRef
)m_format
)).AsString();
157 void wxDataFormat::SetId( NativeFormat format
)
161 CFRelease( (CFStringRef
) m_format
);
164 m_format
= (NativeFormat
) CFStringCreateCopy(NULL
, (CFStringRef
)format
);
165 if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.html") ) )
169 if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.utf16-plain-text") ) )
171 m_type
= wxDF_UNICODETEXT
;
173 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.utf16-external-plain-text") ) )
175 m_type
= wxDF_UNICODETEXT
;
177 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.plain-text") ) )
181 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.tiff") ) )
183 m_type
= wxDF_BITMAP
;
185 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("com.adobe.pdf") ) )
187 m_type
= wxDF_METAFILE
;
189 else if ( UTTypeConformsTo( (CFStringRef
)format
, CFSTR("public.file-url") ) )
191 m_type
= wxDF_FILENAME
;
195 m_type
= wxDF_PRIVATE
;
196 m_id
= wxCFStringRef( (CFStringRef
) CFRetain((CFStringRef
) format
)).AsString();
200 void wxDataFormat::SetId( const wxString
& zId
)
202 m_type
= wxDF_PRIVATE
;
206 CFRelease( (CFStringRef
) m_format
);
209 // since it is private, no need to conform to anything ...
210 m_format
= (long) wxCFRetain( (CFStringRef
) wxCFStringRef(m_id
) );
213 bool wxDataFormat::operator==(const wxDataFormat
& format
) const
215 if (IsStandard() || format
.IsStandard())
216 return (format
.m_type
== m_type
);
218 return ( UTTypeConformsTo( (CFStringRef
) m_format
, (CFStringRef
) format
.m_format
) );
221 //-------------------------------------------------------------------------
223 //-------------------------------------------------------------------------
225 wxDataObject::wxDataObject()
229 bool wxDataObject::IsSupportedFormat( const wxDataFormat
& rFormat
, Direction vDir
) const
231 size_t nFormatCount
= GetFormatCount( vDir
);
234 if (nFormatCount
== 1)
236 found
= (rFormat
== GetPreferredFormat());
240 wxDataFormat
*pFormats
= new wxDataFormat
[nFormatCount
];
241 GetAllFormats( pFormats
, vDir
);
243 for (size_t n
= 0; n
< nFormatCount
; n
++)
245 if (pFormats
[n
] == rFormat
)
258 void wxDataObject::AddToPasteboard( void * pb
, int itemID
)
260 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
261 // get formats from wxDataObjects
262 wxDataFormat
*array
= new wxDataFormat
[ GetFormatCount() ];
263 GetAllFormats( array
);
265 for (size_t i
= 0; i
< GetFormatCount(); i
++)
267 wxDataFormat thisFormat
= array
[ i
];
269 // add four bytes at the end for data objs like text that
270 // have a datasize = strlen but still need a buffer for the
271 // string including trailing zero
273 size_t datasize
= GetDataSize( thisFormat
);
274 size_t sz
= datasize
+ 4;
275 void* buf
= malloc( sz
);
278 // empty the buffer because in some case GetDataHere does not fill buf
279 memset( buf
, 0, sz
);
280 if ( GetDataHere( array
[ i
], buf
) )
283 if ( thisFormat
.GetType() == wxDF_FILENAME
)
285 // the data is D-normalized UTF8 strings of filenames delimited with \n
286 char *fname
= strtok((char*) buf
,"\n");
287 while (fname
!= NULL
)
289 // translate the filepath into a fileurl and put that into the pasteobard
290 CFStringRef path
= CFStringCreateWithBytes(NULL
,(UInt8
*)fname
,strlen(fname
),kCFStringEncodingUTF8
,false);
291 CFURLRef url
= CFURLCreateWithFileSystemPath(NULL
, path
, kCFURLPOSIXPathStyle
, false);
293 CFDataRef data
= CFURLCreateData(NULL
,url
,kCFStringEncodingUTF8
,true);
295 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) counter
,
296 (CFStringRef
) thisFormat
.GetFormatId() , data
, kPasteboardFlavorNoFlags
);
299 fname
= strtok (NULL
,"\n");
305 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*)buf
, datasize
);
306 if ( thisFormat
.GetType() == wxDF_TEXT
)
307 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) itemID
,
308 CFSTR("com.apple.traditional-mac-plain-text") , data
, kPasteboardFlavorNoFlags
);
310 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) itemID
,
311 (CFStringRef
) thisFormat
.GetFormatId() , data
, kPasteboardFlavorNoFlags
);
322 bool wxDataObject::IsFormatInPasteboard( void * pb
, const wxDataFormat
&dataFormat
)
324 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
325 bool hasData
= false;
326 OSStatus err
= noErr
;
329 // we synchronize here once again, so we don't mind which flags get returned
330 PasteboardSynchronize( pasteboard
);
332 err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
335 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& hasData
== false ; itemIndex
++ )
337 PasteboardItemID itemID
;
338 CFArrayRef flavorTypeArray
;
341 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
345 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
349 flavorCount
= CFArrayGetCount( flavorTypeArray
);
351 for( CFIndex flavorIndex
= 0; flavorIndex
< flavorCount
&& hasData
== false ; flavorIndex
++ )
353 CFStringRef flavorType
;
355 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
358 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
359 if ( dataFormat
== flavorFormat
)
361 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
364 CFRelease (flavorTypeArray
);
371 bool wxDataObject::GetFromPasteboard( void * pb
)
373 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
374 size_t formatcount
= GetFormatCount() + 1;
375 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
376 array
[0] = GetPreferredFormat();
377 GetAllFormats( &array
[1] );
378 ItemCount itemCount
= 0;
379 wxString filenamesPassed
;
380 bool transferred
= false;
382 // we synchronize here once again, so we don't mind which flags get returned
383 PasteboardSynchronize( pasteboard
);
385 OSStatus err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
388 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
390 // go through the data in our order of preference
391 wxDataFormat dataFormat
= array
[ i
];
393 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& transferred
== false ; itemIndex
++ )
395 PasteboardItemID itemID
= 0;
396 CFArrayRef flavorTypeArray
= NULL
;
397 CFIndex flavorCount
= 0;
399 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
403 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
407 flavorCount
= CFArrayGetCount( flavorTypeArray
);
409 for( CFIndex flavorIndex
= 0; !transferred
&& flavorIndex
< flavorCount
; flavorIndex
++ )
411 CFStringRef flavorType
;
412 CFDataRef flavorData
;
413 CFIndex flavorDataSize
;
415 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
418 // avoid utf8 being treated closer to plain-text than unicode by forcing a conversion
419 if ( UTTypeConformsTo(flavorType
, CFSTR("public.utf8-plain-text") ) )
421 flavorType
= CFSTR("public.utf16-plain-text");
423 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
425 if ( dataFormat
== flavorFormat
)
427 err
= PasteboardCopyItemFlavorData( pasteboard
, itemID
, flavorType
, &flavorData
);
430 flavorDataSize
= CFDataGetLength( flavorData
);
431 if (dataFormat
.GetType() == wxDF_FILENAME
)
433 // revert the translation and decomposition to arrive at a proper utf8 string again
434 CFURLRef url
= CFURLCreateWithBytes( kCFAllocatorDefault
, CFDataGetBytePtr( flavorData
), flavorDataSize
, kCFStringEncodingUTF8
, NULL
);
435 CFStringRef cfString
= CFURLCopyFileSystemPath( url
, kCFURLPOSIXPathStyle
);
437 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, cfString
);
438 CFRelease( cfString
);
439 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormC
);
440 wxString path
= wxCFStringRef(cfMutableString
).AsString();
442 filenamesPassed
+= path
+ wxT("\n");
446 // because some data implementation expect trailing a trailing NUL, we add some headroom
447 void *buf
= malloc( flavorDataSize
+ 4 );
450 memset( buf
, 0, flavorDataSize
+ 4 );
451 memcpy( buf
, CFDataGetBytePtr( flavorData
), flavorDataSize
);
453 if (dataFormat
.GetType() == wxDF_TEXT
)
454 wxMacConvertNewlines10To13( (char*) buf
);
455 SetData( flavorFormat
, flavorDataSize
, buf
);
460 CFRelease (flavorData
);
463 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
465 err
= PasteboardCopyItemFlavorData( pasteboard
, itemID
, flavorType
, &flavorData
);
468 flavorDataSize
= CFDataGetLength( flavorData
);
469 void *asciibuf
= malloc( flavorDataSize
+ 1 );
472 memset( asciibuf
, 0, flavorDataSize
+ 1 );
473 memcpy( asciibuf
, CFDataGetBytePtr( flavorData
), flavorDataSize
);
474 CFRelease (flavorData
);
476 SetData( wxDF_TEXT
, flavorDataSize
, asciibuf
);
481 CFRelease (flavorData
);
485 CFRelease( flavorTypeArray
);
487 if ( !filenamesPassed
.empty() )
489 wxCharBuffer buf
= filenamesPassed
.fn_str();
490 SetData( wxDF_FILENAME
, strlen( buf
), (const char*)buf
);
498 bool wxDataObject::HasDataInPasteboard( void * pb
)
500 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
501 size_t formatcount
= GetFormatCount() + 1;
502 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
503 array
[0] = GetPreferredFormat();
504 GetAllFormats( &array
[1] );
505 ItemCount itemCount
= 0;
506 bool hasData
= false;
508 // we synchronize here once again, so we don't mind which flags get returned
509 PasteboardSynchronize( pasteboard
);
511 OSStatus err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
514 for (size_t i
= 0; !hasData
&& i
< formatcount
; i
++)
516 // go through the data in our order of preference
517 wxDataFormat dataFormat
= array
[ i
];
519 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& hasData
== false ; itemIndex
++ )
521 PasteboardItemID itemID
= 0;
522 CFArrayRef flavorTypeArray
= NULL
;
523 CFIndex flavorCount
= 0;
525 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
529 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
533 flavorCount
= CFArrayGetCount( flavorTypeArray
);
535 for( CFIndex flavorIndex
= 0; !hasData
&& flavorIndex
< flavorCount
; flavorIndex
++ )
537 CFStringRef flavorType
;
539 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
542 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
544 if ( dataFormat
== flavorFormat
||
545 (dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
) )
550 CFRelease( flavorTypeArray
);
557 // ----------------------------------------------------------------------------
559 // ----------------------------------------------------------------------------
562 void wxTextDataObject::GetAllFormats(wxDataFormat
*formats
,
563 wxDataObjectBase::Direction
WXUNUSED(dir
)) const
565 *formats
++ = wxDataFormat( wxDF_TEXT
);
566 *formats
= wxDataFormat( wxDF_UNICODETEXT
);
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
574 void wxFileDataObject::GetFileNames( wxCharBuffer
&buf
) const
578 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
580 filenames
+= m_filenames
[i
];
581 filenames
+= wxT('\n');
584 buf
= filenames
.fn_str();
587 bool wxFileDataObject::GetDataHere( void *pBuf
) const
596 buffLength
= strlen( buf
);
597 memcpy( pBuf
, (const char*)buf
, buffLength
+ 1 );
602 size_t wxFileDataObject::GetDataSize() const
608 buffLength
= strlen( buf
);
610 return buffLength
+ 1;
613 bool wxFileDataObject::SetData( size_t WXUNUSED(nSize
), const void *pBuf
)
618 filenames
= wxString( (const char*)pBuf
, *wxConvFileName
);
620 filenames
= wxString (wxConvLocal
.cWC2WX(wxConvFileName
->cMB2WC( (const char*)pBuf
)));
623 m_filenames
= wxStringTokenize( filenames
, wxT("\n"), wxTOKEN_STRTOK
);
628 void wxFileDataObject::AddFile( const wxString
& rFilename
)
630 m_filenames
.Add( rFilename
);
633 // ----------------------------------------------------------------------------
634 // wxBitmapDataObject
635 // ----------------------------------------------------------------------------
637 wxBitmapDataObject::wxBitmapDataObject()
642 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap
)
643 : wxBitmapDataObjectBase( rBitmap
)
649 SetBitmap( rBitmap
);
653 wxBitmapDataObject::~wxBitmapDataObject()
658 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap
)
661 wxBitmapDataObjectBase::SetBitmap( rBitmap
);
664 CGImageRef cgImageRef
= (CGImageRef
) m_bitmap
.CreateCGImage();
666 CFMutableDataRef data
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
667 CGImageDestinationRef destination
= CGImageDestinationCreateWithData( data
, kUTTypeTIFF
, 1 , NULL
);
670 CGImageDestinationAddImage( destination
, cgImageRef
, NULL
);
671 CGImageDestinationFinalize( destination
);
672 CFRelease( destination
);
674 m_pictHandle
= NewHandle(CFDataGetLength(data
));
677 memcpy( *(Handle
)m_pictHandle
, (const char *)CFDataGetBytePtr(data
), CFDataGetLength(data
) );
681 CGImageRelease(cgImageRef
);
685 void wxBitmapDataObject::Init()
688 m_pictCreated
= false;
691 void wxBitmapDataObject::Clear()
693 if (m_pictHandle
!= NULL
)
695 DisposeHandle( (Handle
) m_pictHandle
);
698 m_pictCreated
= false;
701 bool wxBitmapDataObject::GetDataHere( void *pBuf
) const
703 if (m_pictHandle
== NULL
)
705 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
712 memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle
) );
717 size_t wxBitmapDataObject::GetDataSize() const
719 if (m_pictHandle
!= NULL
)
720 return GetHandleSize( (Handle
)m_pictHandle
);
725 Handle
MacCreateDataReferenceHandle(Handle theDataHandle
)
727 Handle dataRef
= NULL
;
730 // Create a data reference handle for our data.
731 err
= PtrToHand( &theDataHandle
, &dataRef
, sizeof(Handle
));
736 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf
)
740 if ((pBuf
== NULL
) || (nSize
== 0))
743 Handle picHandle
= NewHandle( nSize
);
744 memcpy( *picHandle
, pBuf
, nSize
);
745 m_pictHandle
= picHandle
;
746 CGImageRef cgImageRef
= 0;
748 CFDataRef data
= CFDataCreateWithBytesNoCopy( kCFAllocatorDefault
, (const UInt8
*) pBuf
, nSize
, kCFAllocatorNull
);
749 CGImageSourceRef source
= CGImageSourceCreateWithData( data
, NULL
);
752 cgImageRef
= CGImageSourceCreateImageAtIndex(source
, 0, NULL
);
759 m_bitmap
.Create( CGImageGetWidth(cgImageRef
) , CGImageGetHeight(cgImageRef
) );
760 CGRect r
= CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef
) , CGImageGetHeight(cgImageRef
) );
761 // since our context is upside down we dont use CGContextDrawImage
762 wxMacDrawCGImage( (CGContextRef
) m_bitmap
.GetHBITMAP() , &r
, cgImageRef
) ;
763 CGImageRelease(cgImageRef
);
767 return m_bitmap
.IsOk();