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
, wxObject
)
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 /* we can only store one wxDataObject */
210 /* get formats from wxDataObjects */
211 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
212 m_data
->GetAllFormats( array
);
214 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
216 wxLogTrace( TRACE_CLIPBOARD
,
217 wxT("wxClipboard now supports atom %s"),
218 array
[i
].GetId().c_str() );
223 OSStatus err
= noErr
;
226 switch ( array
[i
].GetType() )
231 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
232 wxString
str(textDataObject
->GetText());
234 if ( wxApp::s_macDefaultEncodingIsPC
)
236 mac
= wxMacMakeMacStringFromPC(textDataObject
->GetText()) ;
240 mac
= textDataObject
->GetText() ;
243 err
= PutScrap( mac
.Length() , 'TEXT' , mac
.c_str() ) ;
246 err
= GetCurrentScrap (&scrap
);
249 err
= PutScrapFlavor (scrap
, 'TEXT', 0, mac
.Length(), mac
.c_str());
255 #if wxUSE_DRAG_AND_DROP
258 wxMetafileDataObject
* metaFileDataObject
=
259 (wxMetafileDataObject
*) data
;
260 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
261 PicHandle pict
= (PicHandle
) metaFile
.GetHMETAFILE() ;
262 HLock( (Handle
) pict
) ;
264 err
= PutScrap( GetHandleSize( (Handle
) pict
) , 'PICT' , *pict
) ;
267 err
= GetCurrentScrap (&scrap
);
270 err
= PutScrapFlavor (scrap
, 'PICT', 0, GetHandleSize((Handle
) pict
), *pict
);
273 HUnlock( (Handle
) pict
) ;
290 void wxClipboard::Close()
295 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
299 return m_data
->IsSupported( dataFormat
) ;
302 OSStatus err
= noErr
;
305 err
= GetCurrentScrap( &scrapRef
);
306 if ( err
!= noTypeErr
&& err
!= memFullErr
)
308 ScrapFlavorFlags flavorFlags
;
311 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
313 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
323 Handle datahandle
= NewHandle(0) ;
324 HLock( datahandle
) ;
325 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
326 HUnlock( datahandle
) ;
327 bool hasData
= GetHandleSize( datahandle
) > 0 ;
328 DisposeHandle( datahandle
) ;
333 bool wxClipboard::GetData( wxDataObject
& data
)
335 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
337 size_t formatcount
= data
.GetFormatCount() + 1 ;
338 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
339 array
[0] = data
.GetPreferredFormat();
340 data
.GetAllFormats( &array
[1] );
342 bool transferred
= false ;
346 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
348 wxDataFormat format
= array
[i
] ;
349 if ( m_data
->IsSupported( format
) )
351 int size
= m_data
->GetDataSize( format
);
356 data
.SetData(format
, 0 , 0 ) ;
360 char *d
= new char[size
];
361 m_data
->GetDataHere( format
, (void*) d
);
362 data
.SetData( format
, size
, d
) ;
368 /* get formats from wxDataObjects */
371 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
373 wxDataFormat format
= array
[i
] ;
375 switch ( format
.GetType() )
381 char* s
= (char*)wxGetClipboardData(format
, &len
);
384 data
.SetData( format
, len
, s
) ;