1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "clipbrd.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
)
95 ((wxChar
*)data
)[byteCount
/2] = 0 ;
99 delete[] ((char *)data
) ;
108 Handle datahandle
= NewHandle(0) ;
109 HLock( datahandle
) ;
110 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
111 HUnlock( datahandle
) ;
112 if ( GetHandleSize( datahandle
) > 0 )
114 byteCount
= GetHandleSize( datahandle
) ;
115 Size allocSize
= byteCount
;
116 if ( dataFormat
.GetType() == wxDF_TEXT
)
118 else if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
121 data
= new char[ allocSize
] ;
123 memcpy( (char*) data
, (char*) *datahandle
, byteCount
) ;
124 if ( dataFormat
.GetType() == wxDF_TEXT
)
125 ((char*)data
)[byteCount
] = 0 ;
126 if ( dataFormat
.GetType() == wxDF_UNICODETEXT
)
127 ((wxChar
*)data
)[byteCount
/2] = 0 ;
130 DisposeHandle( datahandle
) ;
134 wxLogSysError(_("Failed to get clipboard data."));
139 if ( dataFormat
.GetType() == wxDF_TEXT
)
141 wxMacConvertNewlines10To13( (char*) data
) ;
149 * Generalized clipboard implementation by Matthew Flatt
152 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
154 wxClipboard::wxClipboard()
160 wxClipboard::~wxClipboard()
165 m_data
= (wxDataObject
*) NULL
;
169 void wxClipboard::Clear()
174 m_data
= (wxDataObject
*) NULL
;
178 err
= ClearCurrentScrap( );
185 wxLogSysError(_("Failed to empty the clipboard."));
189 bool wxClipboard::Flush()
194 bool wxClipboard::Open()
196 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
201 bool wxClipboard::IsOpened() const
206 bool wxClipboard::SetData( wxDataObject
*data
)
208 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
210 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
213 // as we can only store one wxDataObject, this is the same in this
215 return AddData( data
);
218 bool wxClipboard::AddData( wxDataObject
*data
)
220 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
222 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
224 /* we can only store one wxDataObject */
229 /* get formats from wxDataObjects */
230 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
231 m_data
->GetAllFormats( array
);
233 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
235 wxLogTrace( TRACE_CLIPBOARD
,
236 wxT("wxClipboard now supports atom %s"),
237 array
[i
].GetId().c_str() );
239 size_t sz
= data
->GetDataSize( array
[i
] ) ;
240 void* buf
= malloc( sz
+ 1 ) ;
243 data
->GetDataHere( array
[i
] , buf
) ;
245 switch ( array
[i
].GetType() )
249 mactype
= kScrapFlavorTypeText
;
252 case wxDF_UNICODETEXT
:
253 mactype
= kScrapFlavorTypeUnicode
;
256 #if wxUSE_DRAG_AND_DROP
258 mactype
= kScrapFlavorTypePicture
;
263 mactype
= kScrapFlavorTypePicture
;
268 UMAPutScrap( sz
, mactype
, buf
) ;
278 void wxClipboard::Close()
280 wxCHECK_RET( m_open
, wxT("clipboard not open") );
284 // Get rid of cached object. If this is not done copying from another application will
289 m_data
= (wxDataObject
*) NULL
;
294 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
298 return m_data
->IsSupported( dataFormat
) ;
301 OSStatus err
= noErr
;
304 err
= GetCurrentScrap( &scrapRef
);
305 if ( err
!= noTypeErr
&& err
!= memFullErr
)
307 ScrapFlavorFlags flavorFlags
;
310 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
312 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
322 Handle datahandle
= NewHandle(0) ;
323 HLock( datahandle
) ;
324 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
325 HUnlock( datahandle
) ;
326 bool hasData
= GetHandleSize( datahandle
) > 0 ;
327 DisposeHandle( datahandle
) ;
332 bool wxClipboard::GetData( wxDataObject
& data
)
334 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
336 size_t formatcount
= data
.GetFormatCount() + 1 ;
337 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
338 array
[0] = data
.GetPreferredFormat();
339 data
.GetAllFormats( &array
[1] );
341 bool transferred
= false ;
345 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
347 wxDataFormat format
= array
[i
] ;
348 if ( m_data
->IsSupported( format
) )
350 int size
= m_data
->GetDataSize( format
);
355 data
.SetData(format
, 0 , 0 ) ;
359 char *d
= new char[size
];
360 m_data
->GetDataHere( format
, (void*) d
);
361 data
.SetData( format
, size
, d
) ;
367 /* get formats from wxDataObjects */
370 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
372 wxDataFormat format
= array
[i
] ;
374 switch ( format
.GetType() )
382 char* s
= (char*)wxGetClipboardData(format
, &len
);
385 data
.SetData( format
, len
, s
) ;