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"
29 #include "wx/filename.h"
31 #include "wx/osx/private.h"
33 #if wxOSX_USE_COCOA_OR_CARBON
34 #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") ) ||
190 UTTypeConformsTo( (CFStringRef
)format
, kPasteboardTypeFileURLPromise
))
192 m_type
= wxDF_FILENAME
;
196 m_type
= wxDF_PRIVATE
;
197 m_id
= wxCFStringRef( (CFStringRef
) CFRetain((CFStringRef
) format
)).AsString();
201 void wxDataFormat::SetId( const wxString
& zId
)
203 m_type
= wxDF_PRIVATE
;
207 CFRelease( (CFStringRef
) m_format
);
210 // since it is private, no need to conform to anything ...
211 m_format
= (long) wxCFRetain( (CFStringRef
) wxCFStringRef(m_id
) );
214 bool wxDataFormat::operator==(const wxDataFormat
& format
) const
216 if (IsStandard() || format
.IsStandard())
217 return (format
.m_type
== m_type
);
219 return ( UTTypeConformsTo( (CFStringRef
) m_format
, (CFStringRef
) format
.m_format
) );
222 //-------------------------------------------------------------------------
224 //-------------------------------------------------------------------------
226 wxDataObject::wxDataObject()
230 bool wxDataObject::IsSupportedFormat( const wxDataFormat
& rFormat
, Direction vDir
) const
232 size_t nFormatCount
= GetFormatCount( vDir
);
235 if (nFormatCount
== 1)
237 found
= (rFormat
== GetPreferredFormat());
241 wxDataFormat
*pFormats
= new wxDataFormat
[nFormatCount
];
242 GetAllFormats( pFormats
, vDir
);
244 for (size_t n
= 0; n
< nFormatCount
; n
++)
246 if (pFormats
[n
] == rFormat
)
259 void wxDataObject::AddToPasteboard( void * pb
, int itemID
)
261 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
262 // get formats from wxDataObjects
263 wxDataFormat
*array
= new wxDataFormat
[ GetFormatCount() ];
264 GetAllFormats( array
);
266 for (size_t i
= 0; i
< GetFormatCount(); i
++)
268 wxDataFormat thisFormat
= array
[ i
];
270 // add four bytes at the end for data objs like text that
271 // have a datasize = strlen but still need a buffer for the
272 // string including trailing zero
274 size_t datasize
= GetDataSize( thisFormat
);
275 size_t sz
= datasize
+ 4;
276 void* buf
= malloc( sz
);
279 // empty the buffer because in some case GetDataHere does not fill buf
280 memset( buf
, 0, sz
);
281 if ( GetDataHere( array
[ i
], buf
) )
284 if ( thisFormat
.GetType() == wxDF_FILENAME
)
286 // the data is D-normalized UTF8 strings of filenames delimited with \n
287 char *fname
= strtok((char*) buf
,"\n");
288 while (fname
!= NULL
)
290 // translate the filepath into a fileurl and put that into the pasteobard
291 CFStringRef path
= CFStringCreateWithBytes(NULL
,(UInt8
*)fname
,strlen(fname
),kCFStringEncodingUTF8
,false);
292 CFURLRef url
= CFURLCreateWithFileSystemPath(NULL
, path
, kCFURLPOSIXPathStyle
, false);
294 CFDataRef data
= CFURLCreateData(NULL
,url
,kCFStringEncodingUTF8
,true);
296 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) counter
,
297 (CFStringRef
) thisFormat
.GetFormatId() , data
, kPasteboardFlavorNoFlags
);
300 fname
= strtok (NULL
,"\n");
306 CFDataRef data
= CFDataCreate( kCFAllocatorDefault
, (UInt8
*)buf
, datasize
);
307 if ( thisFormat
.GetType() == wxDF_TEXT
)
308 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) itemID
,
309 CFSTR("com.apple.traditional-mac-plain-text") , data
, kPasteboardFlavorNoFlags
);
311 PasteboardPutItemFlavor( pasteboard
, (PasteboardItemID
) itemID
,
312 (CFStringRef
) thisFormat
.GetFormatId() , data
, kPasteboardFlavorNoFlags
);
323 bool wxDataObject::IsFormatInPasteboard( void * pb
, const wxDataFormat
&dataFormat
)
325 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
326 bool hasData
= false;
327 OSStatus err
= noErr
;
330 // we synchronize here once again, so we don't mind which flags get returned
331 PasteboardSynchronize( pasteboard
);
333 err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
336 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& hasData
== false ; itemIndex
++ )
338 PasteboardItemID itemID
;
339 CFArrayRef flavorTypeArray
;
342 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
346 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
350 flavorCount
= CFArrayGetCount( flavorTypeArray
);
352 for( CFIndex flavorIndex
= 0; flavorIndex
< flavorCount
&& hasData
== false ; flavorIndex
++ )
354 CFStringRef flavorType
;
356 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
359 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
360 if ( dataFormat
== flavorFormat
)
362 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
365 CFRelease (flavorTypeArray
);
372 bool wxDataObject::GetFromPasteboard( void * pb
)
374 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
375 size_t formatcount
= GetFormatCount() + 1;
376 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
377 array
[0] = GetPreferredFormat();
378 GetAllFormats( &array
[1] );
379 ItemCount itemCount
= 0;
380 wxString filenamesPassed
;
381 bool transferred
= false;
383 // we synchronize here once again, so we don't mind which flags get returned
384 PasteboardSynchronize( pasteboard
);
386 OSStatus err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
389 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
391 // go through the data in our order of preference
392 wxDataFormat dataFormat
= array
[ i
];
394 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& transferred
== false ; itemIndex
++ )
396 PasteboardItemID itemID
= 0;
397 CFArrayRef flavorTypeArray
= NULL
;
398 CFIndex flavorCount
= 0;
400 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
404 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
408 flavorCount
= CFArrayGetCount( flavorTypeArray
);
410 for( CFIndex flavorIndex
= 0; !transferred
&& flavorIndex
< flavorCount
; flavorIndex
++ )
412 CFStringRef flavorType
;
413 CFDataRef flavorData
;
414 CFIndex flavorDataSize
;
416 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
419 // avoid utf8 being treated closer to plain-text than unicode by forcing a conversion
420 if ( UTTypeConformsTo(flavorType
, CFSTR("public.utf8-plain-text") ) )
422 flavorType
= CFSTR("public.utf16-plain-text");
424 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
426 if ( dataFormat
== flavorFormat
)
428 if ( UTTypeConformsTo( (CFStringRef
)flavorType
, kPasteboardTypeFileURLPromise
) )
430 wxString tempdir
= wxFileName::GetTempDir() + wxFILE_SEP_PATH
+ "wxtemp.XXXXXX";
431 char* result
= mkdtemp((char*)tempdir
.fn_str().data());
436 wxCFRef
<CFURLRef
> dest(CFURLCreateFromFileSystemRepresentation(NULL
,(const UInt8
*)result
,strlen(result
),true));
437 PasteboardSetPasteLocation(pasteboard
, dest
);
439 err
= PasteboardCopyItemFlavorData( pasteboard
, itemID
, flavorType
, &flavorData
);
442 flavorDataSize
= CFDataGetLength( flavorData
);
443 if (dataFormat
.GetType() == wxDF_FILENAME
)
445 // revert the translation and decomposition to arrive at a proper utf8 string again
446 CFURLRef url
= CFURLCreateWithBytes( kCFAllocatorDefault
, CFDataGetBytePtr( flavorData
), flavorDataSize
, kCFStringEncodingUTF8
, NULL
);
447 CFStringRef cfString
= CFURLCopyFileSystemPath( url
, kCFURLPOSIXPathStyle
);
449 CFMutableStringRef cfMutableString
= CFStringCreateMutableCopy(NULL
, 0, cfString
);
450 CFRelease( cfString
);
451 CFStringNormalize(cfMutableString
,kCFStringNormalizationFormC
);
452 wxString path
= wxCFStringRef(cfMutableString
).AsString();
454 filenamesPassed
+= path
+ wxT("\n");
458 // because some data implementation expect trailing a trailing NUL, we add some headroom
459 void *buf
= malloc( flavorDataSize
+ 4 );
462 memset( buf
, 0, flavorDataSize
+ 4 );
463 memcpy( buf
, CFDataGetBytePtr( flavorData
), flavorDataSize
);
465 if (dataFormat
.GetType() == wxDF_TEXT
)
466 wxMacConvertNewlines10To13( (char*) buf
);
467 SetData( flavorFormat
, flavorDataSize
, buf
);
472 CFRelease (flavorData
);
475 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
)
477 err
= PasteboardCopyItemFlavorData( pasteboard
, itemID
, flavorType
, &flavorData
);
480 flavorDataSize
= CFDataGetLength( flavorData
);
481 void *asciibuf
= malloc( flavorDataSize
+ 1 );
484 memset( asciibuf
, 0, flavorDataSize
+ 1 );
485 memcpy( asciibuf
, CFDataGetBytePtr( flavorData
), flavorDataSize
);
486 CFRelease (flavorData
);
488 SetData( wxDF_TEXT
, flavorDataSize
, asciibuf
);
493 CFRelease (flavorData
);
497 CFRelease( flavorTypeArray
);
499 if ( !filenamesPassed
.empty() )
501 wxCharBuffer buf
= filenamesPassed
.fn_str();
502 SetData( wxDF_FILENAME
, strlen( buf
), (const char*)buf
);
510 bool wxDataObject::HasDataInPasteboard( void * pb
)
512 PasteboardRef pasteboard
= (PasteboardRef
) pb
;
513 size_t formatcount
= GetFormatCount() + 1;
514 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
515 array
[0] = GetPreferredFormat();
516 GetAllFormats( &array
[1] );
517 ItemCount itemCount
= 0;
518 bool hasData
= false;
520 // we synchronize here once again, so we don't mind which flags get returned
521 PasteboardSynchronize( pasteboard
);
523 OSStatus err
= PasteboardGetItemCount( pasteboard
, &itemCount
);
526 for (size_t i
= 0; !hasData
&& i
< formatcount
; i
++)
528 // go through the data in our order of preference
529 wxDataFormat dataFormat
= array
[ i
];
531 for( UInt32 itemIndex
= 1; itemIndex
<= itemCount
&& hasData
== false ; itemIndex
++ )
533 PasteboardItemID itemID
= 0;
534 CFArrayRef flavorTypeArray
= NULL
;
535 CFIndex flavorCount
= 0;
537 err
= PasteboardGetItemIdentifier( pasteboard
, itemIndex
, &itemID
);
541 err
= PasteboardCopyItemFlavors( pasteboard
, itemID
, &flavorTypeArray
);
545 flavorCount
= CFArrayGetCount( flavorTypeArray
);
547 for( CFIndex flavorIndex
= 0; !hasData
&& flavorIndex
< flavorCount
; flavorIndex
++ )
549 CFStringRef flavorType
;
551 flavorType
= (CFStringRef
)CFArrayGetValueAtIndex( flavorTypeArray
,
554 wxDataFormat
flavorFormat( (wxDataFormat::NativeFormat
) flavorType
);
556 if ( dataFormat
== flavorFormat
||
557 (dataFormat
.GetType() == wxDF_UNICODETEXT
&& flavorFormat
.GetType() == wxDF_TEXT
) )
562 CFRelease( flavorTypeArray
);
569 // ----------------------------------------------------------------------------
571 // ----------------------------------------------------------------------------
574 void wxTextDataObject::GetAllFormats(wxDataFormat
*formats
,
575 wxDataObjectBase::Direction
WXUNUSED(dir
)) const
577 *formats
++ = wxDataFormat( wxDF_TEXT
);
578 *formats
= wxDataFormat( wxDF_UNICODETEXT
);
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
586 void wxFileDataObject::GetFileNames( wxCharBuffer
&buf
) const
590 for (size_t i
= 0; i
< m_filenames
.GetCount(); i
++)
592 filenames
+= m_filenames
[i
];
593 filenames
+= wxT('\n');
596 buf
= filenames
.fn_str();
599 bool wxFileDataObject::GetDataHere( void *pBuf
) const
608 buffLength
= strlen( buf
);
609 memcpy( pBuf
, (const char*)buf
, buffLength
+ 1 );
614 size_t wxFileDataObject::GetDataSize() const
620 buffLength
= strlen( buf
);
622 return buffLength
+ 1;
625 bool wxFileDataObject::SetData( size_t WXUNUSED(nSize
), const void *pBuf
)
630 filenames
= wxString( (const char*)pBuf
, *wxConvFileName
);
632 filenames
= wxString (wxConvLocal
.cWC2WX(wxConvFileName
->cMB2WC( (const char*)pBuf
)));
635 m_filenames
= wxStringTokenize( filenames
, wxT("\n"), wxTOKEN_STRTOK
);
640 void wxFileDataObject::AddFile( const wxString
& rFilename
)
642 m_filenames
.Add( rFilename
);
645 // ----------------------------------------------------------------------------
646 // wxBitmapDataObject
647 // ----------------------------------------------------------------------------
649 wxBitmapDataObject::wxBitmapDataObject()
654 wxBitmapDataObject::wxBitmapDataObject( const wxBitmap
& rBitmap
)
655 : wxBitmapDataObjectBase( rBitmap
)
661 SetBitmap( rBitmap
);
665 wxBitmapDataObject::~wxBitmapDataObject()
670 void wxBitmapDataObject::SetBitmap( const wxBitmap
& rBitmap
)
673 wxBitmapDataObjectBase::SetBitmap( rBitmap
);
676 CGImageRef cgImageRef
= (CGImageRef
) m_bitmap
.CreateCGImage();
678 CFMutableDataRef data
= CFDataCreateMutable(kCFAllocatorDefault
, 0);
679 CGImageDestinationRef destination
= CGImageDestinationCreateWithData( data
, kUTTypeTIFF
, 1 , NULL
);
682 CGImageDestinationAddImage( destination
, cgImageRef
, NULL
);
683 CGImageDestinationFinalize( destination
);
684 CFRelease( destination
);
686 m_pictHandle
= NewHandle(CFDataGetLength(data
));
689 memcpy( *(Handle
)m_pictHandle
, (const char *)CFDataGetBytePtr(data
), CFDataGetLength(data
) );
693 CGImageRelease(cgImageRef
);
697 void wxBitmapDataObject::Init()
700 m_pictCreated
= false;
703 void wxBitmapDataObject::Clear()
705 if (m_pictHandle
!= NULL
)
707 DisposeHandle( (Handle
) m_pictHandle
);
710 m_pictCreated
= false;
713 bool wxBitmapDataObject::GetDataHere( void *pBuf
) const
715 if (m_pictHandle
== NULL
)
717 wxFAIL_MSG( wxT("attempt to copy empty bitmap failed") );
724 memcpy( pBuf
, *(Handle
)m_pictHandle
, GetHandleSize( (Handle
)m_pictHandle
) );
729 size_t wxBitmapDataObject::GetDataSize() const
731 if (m_pictHandle
!= NULL
)
732 return GetHandleSize( (Handle
)m_pictHandle
);
737 Handle
MacCreateDataReferenceHandle(Handle theDataHandle
)
739 Handle dataRef
= NULL
;
742 // Create a data reference handle for our data.
743 err
= PtrToHand( &theDataHandle
, &dataRef
, sizeof(Handle
));
748 bool wxBitmapDataObject::SetData( size_t nSize
, const void *pBuf
)
752 if ((pBuf
== NULL
) || (nSize
== 0))
755 Handle picHandle
= NewHandle( nSize
);
756 memcpy( *picHandle
, pBuf
, nSize
);
757 m_pictHandle
= picHandle
;
758 CGImageRef cgImageRef
= 0;
760 CFDataRef data
= CFDataCreateWithBytesNoCopy( kCFAllocatorDefault
, (const UInt8
*) pBuf
, nSize
, kCFAllocatorNull
);
761 CGImageSourceRef source
= CGImageSourceCreateWithData( data
, NULL
);
764 cgImageRef
= CGImageSourceCreateImageAtIndex(source
, 0, NULL
);
771 m_bitmap
.Create( CGImageGetWidth(cgImageRef
) , CGImageGetHeight(cgImageRef
) );
772 CGRect r
= CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef
) , CGImageGetHeight(cgImageRef
) );
773 // since our context is upside down we dont use CGContextDrawImage
774 wxMacDrawCGImage( (CGContextRef
) m_bitmap
.GetHBITMAP() , &r
, cgImageRef
) ;
775 CGImageRelease(cgImageRef
);
779 return m_bitmap
.IsOk();