1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation
14 #pragma implementation "clipbrd.h"
19 #include "wx/bitmap.h"
21 #include "wx/metafile.h"
22 #include "wx/clipbrd.h"
29 #include "wx/mac/uma.h"
31 #define wxUSE_DATAOBJ 1
35 // the trace mask we use with wxLogTrace() - call
36 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
37 // (there will be a *lot* of them!)
38 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
40 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
45 OSStatus err
= noErr
;
50 switch (dataFormat
.GetType())
53 dataFormat
= wxDF_TEXT
;
63 wxLogError(_("Unsupported clipboard format."));
71 err
= GetCurrentScrap( &scrapRef
);
72 if ( err
!= noTypeErr
&& err
!= memFullErr
)
74 ScrapFlavorFlags flavorFlags
;
76 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
78 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
80 if ( dataFormat
.GetType() == wxDF_TEXT
)
83 data
= new char[ byteCount
] ;
84 if (( err
= GetScrapFlavorData( scrapRef
, dataFormat
.GetFormatId(), &byteCount
, data
)) == noErr
)
87 if ( dataFormat
.GetType() == wxDF_TEXT
)
88 ((char*)data
)[byteCount
] = 0 ;
92 delete[] ((char *)data
) ;
101 Handle datahandle
= NewHandle(0) ;
102 HLock( datahandle
) ;
103 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
104 HUnlock( datahandle
) ;
105 if ( GetHandleSize( datahandle
) > 0 )
107 byteCount
= GetHandleSize( datahandle
) ;
108 if ( dataFormat
.GetType() == wxDF_TEXT
)
109 data
= new char[ byteCount
+ 1] ;
111 data
= new char[ byteCount
] ;
113 memcpy( (char*) data
, (char*) *datahandle
, byteCount
) ;
114 if ( dataFormat
.GetType() == wxDF_TEXT
)
115 ((char*)data
)[byteCount
] = 0 ;
118 DisposeHandle( datahandle
) ;
122 wxLogSysError(_("Failed to get clipboard data."));
126 if ( dataFormat
.GetType() == wxDF_TEXT
&& wxApp::s_macDefaultEncodingIsPC
)
128 wxMacConvertToPC((char*)data
,(char*)data
,byteCount
) ;
135 * Generalized clipboard implementation by Matthew Flatt
138 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
140 wxClipboard::wxClipboard()
146 wxClipboard::~wxClipboard()
151 m_data
= (wxDataObject
*) NULL
;
155 void wxClipboard::Clear()
160 m_data
= (wxDataObject
*) NULL
;
164 err
= ClearCurrentScrap( );
171 wxLogSysError(_("Failed to empty the clipboard."));
175 bool wxClipboard::Flush()
180 bool wxClipboard::Open()
182 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
187 bool wxClipboard::IsOpened() const
192 bool wxClipboard::SetData( wxDataObject
*data
)
194 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
196 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
200 return AddData( data
);
203 bool wxClipboard::AddData( wxDataObject
*data
)
205 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
207 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
209 /* we can only store one wxDataObject */
214 /* get formats from wxDataObjects */
215 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
216 m_data
->GetAllFormats( array
);
218 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
220 wxLogTrace( TRACE_CLIPBOARD
,
221 wxT("wxClipboard now supports atom %s"),
222 array
[i
].GetId().c_str() );
227 OSStatus err
= noErr
;
230 switch ( array
[i
].GetType() )
235 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
236 wxString
str(textDataObject
->GetText());
238 if ( wxApp::s_macDefaultEncodingIsPC
)
240 mac
= wxMacMakeMacStringFromPC(textDataObject
->GetText()) ;
244 mac
= textDataObject
->GetText() ;
246 err
= UMAPutScrap( mac
.Length() , 'TEXT' , (void*) mac
.c_str() ) ;
250 #if wxUSE_DRAG_AND_DROP
253 wxMetafileDataObject
* metaFileDataObject
=
254 (wxMetafileDataObject
*) data
;
255 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
256 PicHandle pict
= (PicHandle
) metaFile
.GetHMETAFILE() ;
257 HLock( (Handle
) pict
) ;
258 err
= UMAPutScrap( GetHandleSize( (Handle
) pict
) , 'PICT' , *pict
) ;
259 HUnlock( (Handle
) pict
) ;
266 bool created
= false ;
267 PicHandle pict
= NULL
;
269 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
270 pict
= (PicHandle
) bitmapDataObject
->GetBitmap().GetPict( &created
) ;
272 HLock( (Handle
) pict
) ;
273 err
= UMAPutScrap( GetHandleSize( (Handle
) pict
) , 'PICT' , *pict
) ;
274 HUnlock( (Handle
) pict
) ;
276 KillPicture( pict
) ;
289 void wxClipboard::Close()
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
) ;