1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/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/mac/uma.h"
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 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
408 if ( dataFormat
== flavorFormat
)
410 err
= PasteboardCopyItemFlavorData( pasteboard
, itemID
, flavorType
, &flavorData
);
413 flavorDataSize
= CFDataGetLength( flavorData
);
414 if (dataFormat
.GetType() == wxDF_FILENAME
)
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
);
420 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, cfString
);
421 CFRelease( cfString
);
422 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormC
);
423 wxString path
= wxCFStringRef(cfMutableString
).AsString();
425 filenamesPassed
+= path
+ wxT("\n");
429 // because some data implementation expect trailing a trailing NUL, we add some headroom
430 void *buf
= malloc( flavorDataSize
+ 4 );
433 memset( buf
, 0, flavorDataSize
+ 4 );
434 memcpy( buf
, CFDataGetBytePtr( flavorData
), flavorDataSize
);
436 if (dataFormat
.GetType() == wxDF_TEXT
)
437 wxMacConvertNewlines10To13( (char*) buf
);
438 SetData( flavorFormat
, flavorDataSize
, buf
);
443 CFRelease (flavorData
);
446 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
448 err
= PasteboardCopyItemFlavorData( pasteboard
, itemID
, flavorType
, &flavorData
);
451 flavorDataSize
= CFDataGetLength( flavorData
);
452 void *asciibuf
= malloc( flavorDataSize
+ 1 );
455 memset( asciibuf
, 0, flavorDataSize
+ 1 );
456 memcpy( asciibuf
, CFDataGetBytePtr( flavorData
), flavorDataSize
);
457 CFRelease (flavorData
);
459 SetData( wxDF_TEXT
, flavorDataSize
, asciibuf
);
464 CFRelease (flavorData
);
468 CFRelease( flavorTypeArray
);
470 if (filenamesPassed
.length() > 0)
472 wxCharBuffer buf
= filenamesPassed
.fn_str();
473 SetData( wxDF_FILENAME
, strlen( buf
), (const char*)buf
);
481 bool wxDataObject::HasDataInPasteboard( void * pb
)
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;
491 // we synchronize here once again, so we don't mind which flags get returned
492 PasteboardSynchronize( pasteboard
);
494 OSStatus err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
497 for (size_t i
= 0; !hasData
&& i
< formatcount
; i
++)
499 // go through the data in our order of preference
500 wxDataFormat dataFormat
= array
[ i
];
502 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& hasData
== false ; itemIndex
++ )
504 PasteboardItemID itemID
= 0;
505 CFArrayRef flavorTypeArray
= NULL
;
506 CFIndex flavorCount
= 0;
508 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
512 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
516 flavorCount
= CFArrayGetCount( flavorTypeArray
);
518 for( CFIndex flavorIndex
= 0; !hasData
&& flavorIndex
< flavorCount
; flavorIndex
++ )
520 CFStringRef flavorType
;
522 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
525 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
527 if ( dataFormat
== flavorFormat
||
528 dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
533 CFRelease( flavorTypeArray
);
540 // ----------------------------------------------------------------------------
542 // ----------------------------------------------------------------------------
545 void wxTextDataObject::GetAllFormats(wxDataFormat
*formats
,
546 wxDataObjectBase::Direction
WXUNUSED(dir
)) const
548 *formats
++ = wxDataFormat( wxDF_TEXT
);
549 *formats
= wxDataFormat( wxDF_UNICODETEXT
);
553 // ----------------------------------------------------------------------------
555 // ----------------------------------------------------------------------------
557 void wxFileDataObject::GetFileNames( wxCharBuffer
&buf
) const
561 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
563 filenames
+= m_filenames
[i
];
564 filenames
+= wxT('\n');
567 buf
= filenames
.fn_str();
570 bool wxFileDataObject::GetDataHere( void *pBuf
) const
579 buffLength
= strlen( buf
);
580 memcpy( pBuf
, (const char*)buf
, buffLength
+ 1 );
585 size_t wxFileDataObject::GetDataSize() const
591 buffLength
= strlen( buf
);
593 return buffLength
+ 1;
596 bool wxFileDataObject::SetData( size_t WXUNUSED(nSize
), const void *pBuf
)
601 filenames
= wxString( (const char*)pBuf
, *wxConvFileName
);
603 filenames
= wxString (wxConvLocal
.cWC2WX(wxConvFileName
->cMB2WC( (const char*)pBuf
)));
606 m_filenames
= wxStringTokenize( filenames
, wxT("\n"), wxTOKEN_STRTOK
);
611 void wxFileDataObject::AddFile( const wxString
& rFilename
)
613 m_filenames
.Add( rFilename
);
616 // ----------------------------------------------------------------------------
617 // wxBitmapDataObject
618 // ----------------------------------------------------------------------------
620 wxBitmapDataObject::wxBitmapDataObject()
625 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap
)
626 : wxBitmapDataObjectBase( rBitmap
)
632 SetBitmap( rBitmap
);
636 wxBitmapDataObject::~wxBitmapDataObject()
641 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap
)
644 wxBitmapDataObjectBase::SetBitmap( rBitmap
);
647 CGImageRef cgImageRef
= (CGImageRef
) m_bitmap
.CreateCGImage();
649 CFMutableDataRef data
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
650 CGImageDestinationRef destination
= CGImageDestinationCreateWithData( data
, kUTTypeTIFF
, 1 , NULL
);
653 CGImageDestinationAddImage( destination
, cgImageRef
, NULL
);
654 CGImageDestinationFinalize( destination
);
655 CFRelease( destination
);
657 m_pictHandle
= NewHandle(CFDataGetLength(data
));
660 memcpy( *(Handle
)m_pictHandle
, (const char *)CFDataGetBytePtr(data
), CFDataGetLength(data
) );
664 CGImageRelease(cgImageRef
);
668 void wxBitmapDataObject::Init()
671 m_pictCreated
= false;
674 void wxBitmapDataObject::Clear()
676 if (m_pictHandle
!= NULL
)
678 DisposeHandle( (Handle
) m_pictHandle
);
681 m_pictCreated
= false;
684 bool wxBitmapDataObject::GetDataHere( void *pBuf
) const
686 if (m_pictHandle
== NULL
)
688 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
695 memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle
) );
700 size_t wxBitmapDataObject::GetDataSize() const
702 if (m_pictHandle
!= NULL
)
703 return GetHandleSize( (Handle
)m_pictHandle
);
708 Handle
MacCreateDataReferenceHandle(Handle theDataHandle
)
710 Handle dataRef
= NULL
;
713 // Create a data reference handle for our data.
714 err
= PtrToHand( &theDataHandle
, &dataRef
, sizeof(Handle
));
719 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf
)
723 if ((pBuf
== NULL
) || (nSize
== 0))
726 Handle picHandle
= NewHandle( nSize
);
727 memcpy( *picHandle
, pBuf
, nSize
);
728 m_pictHandle
= picHandle
;
729 CGImageRef cgImageRef
= 0;
731 CFDataRef data
= CFDataCreateWithBytesNoCopy( kCFAllocatorDefault
, (const UInt8
*) pBuf
, nSize
, kCFAllocatorNull
);
732 CGImageSourceRef source
= CGImageSourceCreateWithData( data
, NULL
);
735 cgImageRef
= CGImageSourceCreateImageAtIndex(source
, 0, NULL
);
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
);
750 return m_bitmap
.Ok();