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
;
256 case wxDF_UNICODETEXT
:
257 mactype
= kScrapFlavorTypeUnicode
;
260 #if wxUSE_DRAG_AND_DROP
262 mactype
= kScrapFlavorTypePicture
;
267 mactype
= kScrapFlavorTypePicture
;
272 UMAPutScrap( sz
, mactype
, buf
) ;
282 void wxClipboard::Close()
284 wxCHECK_RET( m_open
, wxT("clipboard not open") );
288 // Get rid of cached object. If this is not done copying from another application will
293 m_data
= (wxDataObject
*) NULL
;
298 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
302 return m_data
->IsSupported( dataFormat
) ;
305 OSStatus err
= noErr
;
308 err
= GetCurrentScrap( &scrapRef
);
309 if ( err
!= noTypeErr
&& err
!= memFullErr
)
311 ScrapFlavorFlags flavorFlags
;
314 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
316 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
326 Handle datahandle
= NewHandle(0) ;
327 HLock( datahandle
) ;
328 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
329 HUnlock( datahandle
) ;
330 bool hasData
= GetHandleSize( datahandle
) > 0 ;
331 DisposeHandle( datahandle
) ;
336 bool wxClipboard::GetData( wxDataObject
& data
)
338 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
340 size_t formatcount
= data
.GetFormatCount() + 1 ;
341 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
342 array
[0] = data
.GetPreferredFormat();
343 data
.GetAllFormats( &array
[1] );
345 bool transferred
= false ;
349 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
351 wxDataFormat format
= array
[i
] ;
352 if ( m_data
->IsSupported( format
) )
354 int size
= m_data
->GetDataSize( format
);
359 data
.SetData(format
, 0 , 0 ) ;
363 char *d
= new char[size
];
364 m_data
->GetDataHere( format
, (void*) d
);
365 data
.SetData( format
, size
, d
) ;
371 /* get formats from wxDataObjects */
374 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
376 wxDataFormat format
= array
[i
] ;
378 switch ( format
.GetType() )
386 char* s
= (char*)wxGetClipboardData(format
, &len
);
389 data
.SetData( format
, len
, s
) ;