1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
8 // Copyright: (c) AUTHOR
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"
25 #include "wx/mac/private.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
;
48 switch (dataFormat
.GetType())
51 dataFormat
= wxDF_TEXT
;
58 wxLogError(_("Unsupported clipboard format."));
66 err
= GetCurrentScrap( &scrapRef
);
67 if ( err
!= noTypeErr
&& err
!= memFullErr
)
69 ScrapFlavorFlags flavorFlags
;
72 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
74 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
76 if ( dataFormat
.GetType() == wxDF_TEXT
)
79 data
= new char[ byteCount
] ;
80 if (( err
= GetScrapFlavorData( scrapRef
, dataFormat
.GetFormatId(), &byteCount
, data
)) == noErr
)
83 if ( dataFormat
.GetType() == wxDF_TEXT
)
84 ((char*)data
)[byteCount
] = 0 ;
88 delete[] ((char *)data
) ;
97 Handle datahandle
= NewHandle(0) ;
99 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
100 HUnlock( datahandle
) ;
101 if ( GetHandleSize( datahandle
) > 0 )
103 long byteCount
= GetHandleSize( datahandle
) ;
104 if ( dataFormat
.GetType() == wxDF_TEXT
)
105 data
= new char[ byteCount
+ 1] ;
107 data
= new char[ byteCount
] ;
109 memcpy( (char*) data
, (char*) *datahandle
, byteCount
) ;
110 if ( dataFormat
.GetType() == wxDF_TEXT
)
111 ((char*)data
)[byteCount
] = 0 ;
114 DisposeHandle( datahandle
) ;
118 wxLogSysError(_("Failed to get clipboard data."));
122 if ( dataFormat
.GetType() == wxDF_TEXT
&& wxApp::s_macDefaultEncodingIsPC
)
124 wxMacConvertToPC((char*)data
) ;
131 * Generalized clipboard implementation by Matthew Flatt
134 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxClipboardBase
)
136 wxClipboard::wxClipboard()
142 wxClipboard::~wxClipboard()
147 m_data
= (wxDataObject
*) NULL
;
151 void wxClipboard::Clear()
156 m_data
= (wxDataObject
*) NULL
;
160 err
= ClearCurrentScrap( );
167 wxLogSysError(_("Failed to empty the clipboard."));
171 bool wxClipboard::Flush()
176 bool wxClipboard::Open()
178 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
183 bool wxClipboard::IsOpened() const
188 bool wxClipboard::SetData( wxDataObject
*data
)
190 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
192 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
196 return AddData( data
);
199 bool wxClipboard::AddData( wxDataObject
*data
)
201 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
203 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
205 wxDataFormat format
= data
->GetPreferredFormat();
207 /* we can only store one wxDataObject */
212 /* get formats from wxDataObjects */
213 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
214 m_data
->GetAllFormats( array
);
216 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
218 wxLogTrace( TRACE_CLIPBOARD
,
219 wxT("wxClipboard now supports atom %s"),
220 array
[i
].GetId().c_str() );
225 OSStatus err
= noErr
;
228 switch ( array
[i
].GetType() )
233 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
234 wxString
str(textDataObject
->GetText());
236 if ( wxApp::s_macDefaultEncodingIsPC
)
238 mac
= wxMacMakeMacStringFromPC(textDataObject
->GetText()) ;
242 mac
= textDataObject
->GetText() ;
245 err
= PutScrap( mac
.Length() , 'TEXT' , mac
.c_str() ) ;
248 err
= GetCurrentScrap (&scrap
);
251 err
= PutScrapFlavor (scrap
, 'TEXT', 0, mac
.Length(), mac
.c_str());
257 #if wxUSE_DRAG_AND_DROP
260 wxMetafileDataObject
* metaFileDataObject
=
261 (wxMetafileDataObject
*) data
;
262 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
263 PicHandle pict
= (PicHandle
) metaFile
.GetHMETAFILE() ;
264 HLock( (Handle
) pict
) ;
266 err
= PutScrap( GetHandleSize( (Handle
) pict
) , 'PICT' , *pict
) ;
269 err
= GetCurrentScrap (&scrap
);
272 err
= PutScrapFlavor (scrap
, 'PICT', 0, GetHandleSize((Handle
) pict
), *pict
);
275 HUnlock( (Handle
) pict
) ;
292 void wxClipboard::Close()
297 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
301 return m_data
->IsSupported( dataFormat
) ;
304 OSStatus err
= noErr
;
307 err
= GetCurrentScrap( &scrapRef
);
308 if ( err
!= noTypeErr
&& err
!= memFullErr
)
310 ScrapFlavorFlags flavorFlags
;
313 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
315 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
325 Handle datahandle
= NewHandle(0) ;
326 HLock( datahandle
) ;
327 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
328 HUnlock( datahandle
) ;
329 bool hasData
= GetHandleSize( datahandle
) > 0 ;
330 DisposeHandle( datahandle
) ;
335 bool wxClipboard::GetData( wxDataObject
& data
)
337 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
339 int formatcount
= data
.GetFormatCount() + 1 ;
340 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
341 array
[0] = data
.GetPreferredFormat();
342 data
.GetAllFormats( &array
[1] );
344 bool transferred
= false ;
348 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
350 wxDataFormat format
= array
[i
] ;
351 if ( m_data
->IsSupported( format
) )
353 int size
= m_data
->GetDataSize( format
);
358 data
.SetData(format
, 0 , 0 ) ;
362 char *d
= new char[size
];
363 m_data
->GetDataHere( format
, (void*) d
);
364 data
.SetData( format
, size
, d
) ;
370 /* get formats from wxDataObjects */
373 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
375 wxDataFormat format
= array
[i
] ;
377 switch ( format
.GetType() )
383 char* s
= (char*)wxGetClipboardData(format
, &len
);
386 data
.SetData( format
, len
, s
) ;