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"
20 #include "wx/bitmap.h"
22 #include "wx/metafile.h"
23 #include "wx/clipbrd.h"
30 #include "wx/mac/uma.h"
32 #define wxUSE_DATAOBJ 1
36 // the trace mask we use with wxLogTrace() - call
37 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
38 // (there will be a *lot* of them!)
39 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
41 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
46 OSStatus err
= noErr
;
51 switch (dataFormat
.GetType())
54 dataFormat
= wxDF_TEXT
;
59 case wxDF_UNICODETEXT
:
66 wxLogError(_("Unsupported clipboard format."));
74 err
= GetCurrentScrap( &scrapRef
);
75 if ( err
!= noTypeErr
&& err
!= memFullErr
)
77 ScrapFlavorFlags flavorFlags
;
79 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
81 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
83 Size allocSize
= byteCount
;
84 if ( dataFormat
.GetType() == wxDF_TEXT
)
86 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
89 data
= new char[ allocSize
] ;
91 if (( err
= GetScrapFlavorData( scrapRef
, dataFormat
.GetFormatId(), &byteCount
, data
)) == noErr
)
94 if ( dataFormat
.GetType() == wxDF_TEXT
)
95 ((char*)data
)[byteCount
] = 0 ;
96 if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
97 ((wxChar
*)data
)[byteCount
/2] = 0 ;
101 delete[] ((char *)data
) ;
110 Handle datahandle
= NewHandle(0) ;
111 HLock( datahandle
) ;
112 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
113 HUnlock( datahandle
) ;
114 if ( GetHandleSize( datahandle
) > 0 )
116 byteCount
= GetHandleSize( datahandle
) ;
117 Size allocSize
= byteCount
;
118 if ( dataFormat
.GetType() == wxDF_TEXT
)
120 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
123 data
= new char[ allocSize
] ;
125 memcpy( (char*) data
, (char*) *datahandle
, byteCount
) ;
126 if ( dataFormat
.GetType() == wxDF_TEXT
)
127 ((char*)data
)[byteCount
] = 0 ;
128 if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
129 ((wxChar
*)data
)[byteCount
/2] = 0 ;
132 DisposeHandle( datahandle
) ;
136 wxLogSysError(_("Failed to get clipboard data."));
141 if ( dataFormat
.GetType() == wxDF_TEXT
)
143 wxMacConvertNewlines10To13( (char*) data
) ;
151 * Generalized clipboard implementation by Matthew Flatt
154 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
156 wxClipboard::wxClipboard()
162 wxClipboard::~wxClipboard()
167 m_data
= (wxDataObject
*) NULL
;
171 void wxClipboard::Clear()
176 m_data
= (wxDataObject
*) NULL
;
180 err
= ClearCurrentScrap( );
187 wxLogSysError(_("Failed to empty the clipboard."));
191 bool wxClipboard::Flush()
196 bool wxClipboard::Open()
198 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
203 bool wxClipboard::IsOpened() const
208 bool wxClipboard::SetData( wxDataObject
*data
)
210 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
212 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
215 // as we can only store one wxDataObject, this is the same in this
217 return AddData( data
);
220 bool wxClipboard::AddData( wxDataObject
*data
)
222 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
224 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
226 /* we can only store one wxDataObject */
231 /* get formats from wxDataObjects */
232 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
233 m_data
->GetAllFormats( array
);
235 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
237 wxLogTrace( TRACE_CLIPBOARD
,
238 wxT("wxClipboard now supports atom %s"),
239 array
[i
].GetId().c_str() );
241 size_t sz
= data
->GetDataSize( array
[i
] ) ;
242 void* buf
= malloc( sz
+ 1 ) ;
245 data
->GetDataHere( array
[i
] , buf
) ;
247 switch ( array
[i
].GetType() )
251 mactype
= kScrapFlavorTypeText
;
254 case wxDF_UNICODETEXT
:
255 mactype
= kScrapFlavorTypeUnicode
;
258 #if wxUSE_DRAG_AND_DROP
260 mactype
= kScrapFlavorTypePicture
;
265 mactype
= kScrapFlavorTypePicture
;
270 UMAPutScrap( sz
, mactype
, buf
) ;
280 void wxClipboard::Close()
282 wxCHECK_RET( m_open
, wxT("clipboard not open") );
286 // Get rid of cached object. If this is not done copying from another application will
291 m_data
= (wxDataObject
*) NULL
;
296 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
300 return m_data
->IsSupported( dataFormat
) ;
303 OSStatus err
= noErr
;
306 err
= GetCurrentScrap( &scrapRef
);
307 if ( err
!= noTypeErr
&& err
!= memFullErr
)
309 ScrapFlavorFlags flavorFlags
;
312 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
314 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
324 Handle datahandle
= NewHandle(0) ;
325 HLock( datahandle
) ;
326 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
327 HUnlock( datahandle
) ;
328 bool hasData
= GetHandleSize( datahandle
) > 0 ;
329 DisposeHandle( datahandle
) ;
334 bool wxClipboard::GetData( wxDataObject
& data
)
336 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
338 size_t formatcount
= data
.GetFormatCount() + 1 ;
339 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
340 array
[0] = data
.GetPreferredFormat();
341 data
.GetAllFormats( &array
[1] );
343 bool transferred
= false ;
347 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
349 wxDataFormat format
= array
[i
] ;
350 if ( m_data
->IsSupported( format
) )
352 int size
= m_data
->GetDataSize( format
);
357 data
.SetData(format
, 0 , 0 ) ;
361 char *d
= new char[size
];
362 m_data
->GetDataHere( format
, (void*) d
);
363 data
.SetData( format
, size
, d
) ;
369 /* get formats from wxDataObjects */
372 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
374 wxDataFormat format
= array
[i
] ;
376 switch ( format
.GetType() )
384 char* s
= (char*)wxGetClipboardData(format
, &len
);
387 data
.SetData( format
, len
, s
) ;