1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "clipbrd.h"
16 #include "wx/wxprec.h"
22 #include "wx/bitmap.h"
24 #include "wx/metafile.h"
25 #include "wx/clipbrd.h"
32 #include "wx/mac/uma.h"
34 #define wxUSE_DATAOBJ 1
38 // the trace mask we use with wxLogTrace() - call
39 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
40 // (there will be a *lot* of them!)
41 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
43 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
48 OSStatus err
= noErr
;
53 switch (dataFormat
.GetType())
56 dataFormat
= wxDF_TEXT
;
61 case wxDF_UNICODETEXT
:
68 wxLogError(_("Unsupported clipboard format."));
76 err
= GetCurrentScrap( &scrapRef
);
77 if ( err
!= noTypeErr
&& err
!= memFullErr
)
79 ScrapFlavorFlags flavorFlags
;
81 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
83 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
85 Size allocSize
= byteCount
;
86 if ( dataFormat
.GetType() == wxDF_TEXT
)
88 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
91 data
= new char[ allocSize
] ;
93 if (( err
= GetScrapFlavorData( scrapRef
, dataFormat
.GetFormatId(), &byteCount
, data
)) == noErr
)
96 if ( dataFormat
.GetType() == wxDF_TEXT
)
97 ((char*)data
)[byteCount
] = 0 ;
98 if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
99 ((wxChar
*)data
)[byteCount
/2] = 0 ;
103 delete[] ((char *)data
) ;
112 Handle datahandle
= NewHandle(0) ;
113 HLock( datahandle
) ;
114 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
115 HUnlock( datahandle
) ;
116 if ( GetHandleSize( datahandle
) > 0 )
118 byteCount
= GetHandleSize( datahandle
) ;
119 Size allocSize
= byteCount
;
120 if ( dataFormat
.GetType() == wxDF_TEXT
)
122 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
125 data
= new char[ allocSize
] ;
127 memcpy( (char*) data
, (char*) *datahandle
, byteCount
) ;
128 if ( dataFormat
.GetType() == wxDF_TEXT
)
129 ((char*)data
)[byteCount
] = 0 ;
130 if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
131 ((wxChar
*)data
)[byteCount
/2] = 0 ;
134 DisposeHandle( datahandle
) ;
138 wxLogSysError(_("Failed to get clipboard data."));
143 if ( dataFormat
.GetType() == wxDF_TEXT
)
145 wxMacConvertNewlines10To13( (char*) data
) ;
153 * Generalized clipboard implementation by Matthew Flatt
156 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
158 wxClipboard::wxClipboard()
164 wxClipboard::~wxClipboard()
169 m_data
= (wxDataObject
*) NULL
;
173 void wxClipboard::Clear()
178 m_data
= (wxDataObject
*) NULL
;
182 err
= ClearCurrentScrap( );
189 wxLogSysError(_("Failed to empty the clipboard."));
193 bool wxClipboard::Flush()
198 bool wxClipboard::Open()
200 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
205 bool wxClipboard::IsOpened() const
210 bool wxClipboard::SetData( wxDataObject
*data
)
212 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
214 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
217 // as we can only store one wxDataObject, this is the same in this
219 return AddData( data
);
222 bool wxClipboard::AddData( wxDataObject
*data
)
224 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
226 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
228 /* we can only store one wxDataObject */
233 /* get formats from wxDataObjects */
234 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
235 m_data
->GetAllFormats( array
);
237 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
239 wxLogTrace( TRACE_CLIPBOARD
,
240 wxT("wxClipboard now supports atom %s"),
241 array
[i
].GetId().c_str() );
243 size_t sz
= data
->GetDataSize( array
[i
] ) ;
244 void* buf
= malloc( sz
+ 1 ) ;
247 data
->GetDataHere( array
[i
] , buf
) ;
249 switch ( array
[i
].GetType() )
253 mactype
= kScrapFlavorTypeText
;
257 case wxDF_UNICODETEXT
:
258 mactype
= kScrapFlavorTypeUnicode
;
262 #if wxUSE_DRAG_AND_DROP
264 mactype
= kScrapFlavorTypePicture
;
269 mactype
= kScrapFlavorTypePicture
;
274 UMAPutScrap( sz
, mactype
, buf
) ;
284 void wxClipboard::Close()
286 wxCHECK_RET( m_open
, wxT("clipboard not open") );
290 // Get rid of cached object. If this is not done copying from another application will
295 m_data
= (wxDataObject
*) NULL
;
300 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
304 return m_data
->IsSupported( dataFormat
) ;
307 OSStatus err
= noErr
;
310 err
= GetCurrentScrap( &scrapRef
);
311 if ( err
!= noTypeErr
&& err
!= memFullErr
)
313 ScrapFlavorFlags flavorFlags
;
316 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
318 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
328 Handle datahandle
= NewHandle(0) ;
329 HLock( datahandle
) ;
330 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
331 HUnlock( datahandle
) ;
332 bool hasData
= GetHandleSize( datahandle
) > 0 ;
333 DisposeHandle( datahandle
) ;
338 bool wxClipboard::GetData( wxDataObject
& data
)
340 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
342 size_t formatcount
= data
.GetFormatCount() + 1 ;
343 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
344 array
[0] = data
.GetPreferredFormat();
345 data
.GetAllFormats( &array
[1] );
347 bool transferred
= false ;
351 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
353 wxDataFormat format
= array
[i
] ;
354 if ( m_data
->IsSupported( format
) )
356 int size
= m_data
->GetDataSize( format
);
361 data
.SetData(format
, 0 , 0 ) ;
365 char *d
= new char[size
];
366 m_data
->GetDataHere( format
, (void*) d
);
367 data
.SetData( format
, size
, d
) ;
373 /* get formats from wxDataObjects */
376 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
378 wxDataFormat format
= array
[i
] ;
380 switch ( format
.GetType() )
388 char* s
= (char*)wxGetClipboardData(format
, &len
);
391 data
.SetData( format
, len
, s
) ;