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());
256 #if wxUSE_DRAG_AND_DROP
259 wxMetafileDataObject
* metaFileDataObject
=
260 (wxMetafileDataObject
*) data
;
261 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
262 PicHandle pict
= (PicHandle
) metaFile
.GetHMETAFILE() ;
263 HLock( (Handle
) pict
) ;
265 err
= PutScrap( GetHandleSize( (Handle
) pict
) , 'PICT' , *pict
) ;
268 err
= GetCurrentScrap (&scrap
);
271 err
= PutScrapFlavor (scrap
, 'PICT', 0, GetHandleSize((Handle
) pict
), *pict
);
274 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 int 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
) ;