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 char * buf
= (char*) data
;
142 while( (buf
=strchr(buf
,0x0a)) != NULL
)
154 * Generalized clipboard implementation by Matthew Flatt
157 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxObject
)
159 wxClipboard::wxClipboard()
165 wxClipboard::~wxClipboard()
170 m_data
= (wxDataObject
*) NULL
;
174 void wxClipboard::Clear()
179 m_data
= (wxDataObject
*) NULL
;
183 err
= ClearCurrentScrap( );
190 wxLogSysError(_("Failed to empty the clipboard."));
194 bool wxClipboard::Flush()
199 bool wxClipboard::Open()
201 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
206 bool wxClipboard::IsOpened() const
211 bool wxClipboard::SetData( wxDataObject
*data
)
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() );
242 OSStatus err
= noErr
;
245 switch ( array
[i
].GetType() )
250 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
251 wxCharBuffer buf
= textDataObject
->GetText().mb_str() ;
252 err
= UMAPutScrap( strlen(buf
) , kScrapFlavorTypeText
, (void*) buf
.data() ) ;
256 case wxDF_UNICODETEXT
:
258 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
259 wxString
str(textDataObject
->GetText());
260 err
= UMAPutScrap( str
.Length() * sizeof(wxChar
) , kScrapFlavorTypeUnicode
, (void*) str
.wc_str() ) ;
264 #if wxUSE_DRAG_AND_DROP
267 wxMetafileDataObject
* metaFileDataObject
=
268 (wxMetafileDataObject
*) data
;
269 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
270 PicHandle pict
= (PicHandle
) metaFile
.GetHMETAFILE() ;
271 HLock( (Handle
) pict
) ;
272 err
= UMAPutScrap( GetHandleSize( (Handle
) pict
) , kScrapFlavorTypePicture
, *pict
) ;
273 HUnlock( (Handle
) pict
) ;
280 bool created
= false ;
281 PicHandle pict
= NULL
;
283 wxBitmapDataObject
* bitmapDataObject
= (wxBitmapDataObject
*) data
;
284 pict
= (PicHandle
) bitmapDataObject
->GetBitmap().GetPict( &created
) ;
286 HLock( (Handle
) pict
) ;
287 err
= UMAPutScrap( GetHandleSize( (Handle
) pict
) , kScrapFlavorTypePicture
, *pict
) ;
288 HUnlock( (Handle
) pict
) ;
290 KillPicture( pict
) ;
303 void wxClipboard::Close()
308 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
312 return m_data
->IsSupported( dataFormat
) ;
315 OSStatus err
= noErr
;
318 err
= GetCurrentScrap( &scrapRef
);
319 if ( err
!= noTypeErr
&& err
!= memFullErr
)
321 ScrapFlavorFlags flavorFlags
;
324 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
326 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
336 Handle datahandle
= NewHandle(0) ;
337 HLock( datahandle
) ;
338 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
339 HUnlock( datahandle
) ;
340 bool hasData
= GetHandleSize( datahandle
) > 0 ;
341 DisposeHandle( datahandle
) ;
346 bool wxClipboard::GetData( wxDataObject
& data
)
348 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
350 size_t formatcount
= data
.GetFormatCount() + 1 ;
351 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
352 array
[0] = data
.GetPreferredFormat();
353 data
.GetAllFormats( &array
[1] );
355 bool transferred
= false ;
359 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
361 wxDataFormat format
= array
[i
] ;
362 if ( m_data
->IsSupported( format
) )
364 int size
= m_data
->GetDataSize( format
);
369 data
.SetData(format
, 0 , 0 ) ;
373 char *d
= new char[size
];
374 m_data
->GetDataHere( format
, (void*) d
);
375 data
.SetData( format
, size
, d
) ;
381 /* get formats from wxDataObjects */
384 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
386 wxDataFormat format
= array
[i
] ;
388 switch ( format
.GetType() )
396 char* s
= (char*)wxGetClipboardData(format
, &len
);
399 data
.SetData( format
, len
, s
) ;