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
&& wxApp::s_macDefaultEncodingIsPC
)
141 wxString st
= wxMacMakeStringFromCString( (char*) data
) ;
143 wxCharBuffer buf
= st
.ToAscii() ;
145 const char* buf
= st
;
147 char* newdata
= new char[strlen(buf
)+1] ;
148 memcpy( newdata
, buf
, strlen(buf
)+1 ) ;
149 delete[] ((char*) data
) ;
158 * Generalized clipboard implementation by Matthew Flatt
161 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
163 wxClipboard::wxClipboard()
169 wxClipboard::~wxClipboard()
174 m_data
= (wxDataObject
*) NULL
;
178 void wxClipboard::Clear()
183 m_data
= (wxDataObject
*) NULL
;
187 err
= ClearCurrentScrap( );
194 wxLogSysError(_("Failed to empty the clipboard."));
198 bool wxClipboard::Flush()
203 bool wxClipboard::Open()
205 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
210 bool wxClipboard::IsOpened() const
215 bool wxClipboard::SetData( wxDataObject
*data
)
217 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
219 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
223 return AddData( data
);
226 bool wxClipboard::AddData( wxDataObject
*data
)
228 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
230 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
232 /* we can only store one wxDataObject */
237 /* get formats from wxDataObjects */
238 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
239 m_data
->GetAllFormats( array
);
241 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
243 wxLogTrace( TRACE_CLIPBOARD
,
244 wxT("wxClipboard now supports atom %s"),
245 array
[i
].GetId().c_str() );
250 OSStatus err
= noErr
;
253 switch ( array
[i
].GetType() )
258 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
259 wxString
str(textDataObject
->GetText());
260 wxCharBuffer buf
= wxMacStringToCString( str
) ;
261 err
= UMAPutScrap( strlen(buf
) , kScrapFlavorTypeText
, (void*) buf
.data() ) ;
265 case wxDF_UNICODETEXT
:
267 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
268 wxString
str(textDataObject
->GetText());
269 err
= UMAPutScrap( str
.Length() * sizeof(wxChar
) , kScrapFlavorTypeUnicode
, (void*) str
.wc_str() ) ;
273 #if wxUSE_DRAG_AND_DROP
276 wxMetafileDataObject
* metaFileDataObject
=
277 (wxMetafileDataObject
*) data
;
278 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
279 PicHandle pict
= (PicHandle
) metaFile
.GetHMETAFILE() ;
280 HLock( (Handle
) pict
) ;
281 err
= UMAPutScrap( GetHandleSize( (Handle
) pict
) , kScrapFlavorTypePicture
, *pict
) ;
282 HUnlock( (Handle
) pict
) ;
289 bool created
= false ;
290 PicHandle pict
= NULL
;
292 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
293 pict
= (PicHandle
) bitmapDataObject
->GetBitmap().GetPict( &created
) ;
295 HLock( (Handle
) pict
) ;
296 err
= UMAPutScrap( GetHandleSize( (Handle
) pict
) , kScrapFlavorTypePicture
, *pict
) ;
297 HUnlock( (Handle
) pict
) ;
299 KillPicture( pict
) ;
312 void wxClipboard::Close()
317 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
321 return m_data
->IsSupported( dataFormat
) ;
324 OSStatus err
= noErr
;
327 err
= GetCurrentScrap( &scrapRef
);
328 if ( err
!= noTypeErr
&& err
!= memFullErr
)
330 ScrapFlavorFlags flavorFlags
;
333 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
335 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
345 Handle datahandle
= NewHandle(0) ;
346 HLock( datahandle
) ;
347 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
348 HUnlock( datahandle
) ;
349 bool hasData
= GetHandleSize( datahandle
) > 0 ;
350 DisposeHandle( datahandle
) ;
355 bool wxClipboard::GetData( wxDataObject
& data
)
357 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
359 size_t formatcount
= data
.GetFormatCount() + 1 ;
360 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
361 array
[0] = data
.GetPreferredFormat();
362 data
.GetAllFormats( &array
[1] );
364 bool transferred
= false ;
368 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
370 wxDataFormat format
= array
[i
] ;
371 if ( m_data
->IsSupported( format
) )
373 int size
= m_data
->GetDataSize( format
);
378 data
.SetData(format
, 0 , 0 ) ;
382 char *d
= new char[size
];
383 m_data
->GetDataHere( format
, (void*) d
);
384 data
.SetData( format
, size
, d
) ;
390 /* get formats from wxDataObjects */
393 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
395 wxDataFormat format
= array
[i
] ;
397 switch ( format
.GetType() )
405 char* s
= (char*)wxGetClipboardData(format
, &len
);
408 data
.SetData( format
, len
, s
) ;