1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
18 #include "wx/bitmap.h"
20 #include "wx/metafile.h"
21 #include "wx/clipbrd.h"
28 #include "wx/mac/uma.h"
30 #define wxUSE_DATAOBJ 1
34 // the trace mask we use with wxLogTrace() - call
35 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
36 // (there will be a *lot* of them!)
37 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
39 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
44 OSStatus err
= noErr
;
49 switch (dataFormat
.GetType())
52 dataFormat
= wxDF_TEXT
;
57 case wxDF_UNICODETEXT
:
64 wxLogError(_("Unsupported clipboard format."));
72 err
= GetCurrentScrap( &scrapRef
);
73 if ( err
!= noTypeErr
&& err
!= memFullErr
)
75 ScrapFlavorFlags flavorFlags
;
77 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
79 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
81 Size allocSize
= byteCount
;
82 if ( dataFormat
.GetType() == wxDF_TEXT
)
84 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
87 data
= new char[ allocSize
] ;
89 if (( err
= GetScrapFlavorData( scrapRef
, dataFormat
.GetFormatId(), &byteCount
, data
)) == noErr
)
92 if ( dataFormat
.GetType() == wxDF_TEXT
)
93 ((char*)data
)[byteCount
] = 0 ;
94 if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
96 // "data" format is UTF16, so 2 bytes = one character
97 // wxChar size depends on platform, so just clear last 2 bytes
98 ((char*)data
)[byteCount
] = 0;
99 ((char*)data
)[byteCount
+1] = 0;
104 delete[] ((char *)data
) ;
113 Handle datahandle
= NewHandle(0) ;
114 HLock( datahandle
) ;
115 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
116 HUnlock( datahandle
) ;
117 if ( GetHandleSize( datahandle
) > 0 )
119 byteCount
= GetHandleSize( datahandle
) ;
120 Size allocSize
= byteCount
;
121 if ( dataFormat
.GetType() == wxDF_TEXT
)
123 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
126 data
= new char[ allocSize
] ;
128 memcpy( (char*) data
, (char*) *datahandle
, byteCount
) ;
129 if ( dataFormat
.GetType() == wxDF_TEXT
)
130 ((char*)data
)[byteCount
] = 0 ;
131 if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
132 ((wxChar
*)data
)[byteCount
/2] = 0 ;
135 DisposeHandle( datahandle
) ;
139 wxLogSysError(_("Failed to get clipboard data."));
144 if ( dataFormat
.GetType() == wxDF_TEXT
)
146 wxMacConvertNewlines10To13( (char*) data
) ;
154 * Generalized clipboard implementation by Matthew Flatt
157 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
159 wxClipboard::wxClipboard()
165 wxClipboard::~wxClipboard()
170 m_data
= (wxDataObject
*) NULL
;
174 void wxClipboard::Clear()
179 m_data
= (wxDataObject
*) NULL
;
183 err
= ClearCurrentScrap( );
190 wxLogSysError(_("Failed to empty the clipboard."));
194 bool wxClipboard::Flush()
199 bool wxClipboard::Open()
201 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
206 bool wxClipboard::IsOpened() const
211 bool wxClipboard::SetData( wxDataObject
*data
)
213 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
215 wxCHECK_MSG( data
, false, wxT("data is invalid") );
218 // as we can only store one wxDataObject, this is the same in this
220 return AddData( data
);
223 bool wxClipboard::AddData( wxDataObject
*data
)
225 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
227 wxCHECK_MSG( data
, false, wxT("data is invalid") );
229 /* we can only store one wxDataObject */
234 /* get formats from wxDataObjects */
235 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
236 m_data
->GetAllFormats( array
);
238 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
240 wxLogTrace( TRACE_CLIPBOARD
,
241 wxT("wxClipboard now supports atom %s"),
242 array
[i
].GetId().c_str() );
244 size_t sz
= data
->GetDataSize( array
[i
] ) ;
245 void* buf
= malloc( sz
+ 1 ) ;
248 // empty the buffer because in some case GetDataHere does not fill buf
249 memset(buf
, 0, sz
+1);
250 data
->GetDataHere( array
[i
] , buf
) ;
252 switch ( array
[i
].GetType() )
256 mactype
= kScrapFlavorTypeText
;
260 case wxDF_UNICODETEXT
:
261 mactype
= kScrapFlavorTypeUnicode
;
265 #if wxUSE_DRAG_AND_DROP
267 mactype
= kScrapFlavorTypePicture
;
272 mactype
= kScrapFlavorTypePicture
;
277 UMAPutScrap( sz
, mactype
, buf
) ;
287 void wxClipboard::Close()
289 wxCHECK_RET( m_open
, wxT("clipboard not open") );
293 // Get rid of cached object. If this is not done copying from another application will
298 m_data
= (wxDataObject
*) NULL
;
303 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
307 return m_data
->IsSupported( dataFormat
) ;
310 OSStatus err
= noErr
;
313 err
= GetCurrentScrap( &scrapRef
);
314 if ( err
!= noTypeErr
&& err
!= memFullErr
)
316 ScrapFlavorFlags flavorFlags
;
319 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
321 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
331 Handle datahandle
= NewHandle(0) ;
332 HLock( datahandle
) ;
333 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
334 HUnlock( datahandle
) ;
335 bool hasData
= GetHandleSize( datahandle
) > 0 ;
336 DisposeHandle( datahandle
) ;
341 bool wxClipboard::GetData( wxDataObject
& data
)
343 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
345 size_t formatcount
= data
.GetFormatCount() + 1 ;
346 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
347 array
[0] = data
.GetPreferredFormat();
348 data
.GetAllFormats( &array
[1] );
350 bool transferred
= false ;
354 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
356 wxDataFormat format
= array
[i
] ;
357 if ( m_data
->IsSupported( format
) )
359 int size
= m_data
->GetDataSize( format
);
364 data
.SetData(format
, 0 , 0 ) ;
368 char *d
= new char[size
];
369 m_data
->GetDataHere( format
, (void*) d
);
370 data
.SetData( format
, size
, d
) ;
376 /* get formats from wxDataObjects */
379 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
381 wxDataFormat format
= array
[i
] ;
383 switch ( format
.GetType() )
386 case wxDF_UNICODETEXT
:
392 char* s
= (char*)wxGetClipboardData(format
, &len
);
395 data
.SetData( format
, len
, s
) ;