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"
27 #define wxUSE_DATAOBJ 1
31 // the trace mask we use with wxLogTrace() - call
32 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
33 // (there will be a *lot* of them!)
34 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
36 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
41 OSStatus err
= noErr
;
45 switch (dataFormat
.GetType())
48 dataFormat
= wxDF_TEXT
;
55 wxLogError(_("Unsupported clipboard format."));
63 err
= GetCurrentScrap( &scrapRef
);
64 if ( err
!= noTypeErr
&& err
!= memFullErr
)
66 ScrapFlavorFlags flavorFlags
;
69 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
71 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
73 if ( dataFormat
.GetType() == wxDF_TEXT
)
76 data
= new char[ byteCount
] ;
77 if (( err
= GetScrapFlavorData( scrapRef
, dataFormat
.GetFormatId(), &byteCount
, data
)) == noErr
)
80 if ( dataFormat
.GetType() == wxDF_TEXT
)
81 ((char*)data
)[byteCount
] = 0 ;
85 delete[] ((char *)data
) ;
94 Handle datahandle
= NewHandle(0) ;
96 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
97 HUnlock( datahandle
) ;
98 if ( GetHandleSize( datahandle
) > 0 )
100 long byteCount
= GetHandleSize( datahandle
) ;
101 if ( dataFormat
.GetType() == wxDF_TEXT
)
102 data
= new char[ byteCount
+ 1] ;
104 data
= new char[ byteCount
] ;
106 memcpy( (char*) data
, (char*) *datahandle
, byteCount
) ;
107 if ( dataFormat
.GetType() == wxDF_TEXT
)
108 ((char*)data
)[byteCount
] = 0 ;
111 DisposeHandle( datahandle
) ;
115 wxLogSysError(_("Failed to get clipboard data."));
119 if ( dataFormat
.GetType() == wxDF_TEXT
&& wxApp::s_macDefaultEncodingIsPC
)
121 wxMacConvertToPC((char*)data
) ;
128 * Generalized clipboard implementation by Matthew Flatt
131 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxClipboardBase
)
133 wxClipboard::wxClipboard()
139 wxClipboard::~wxClipboard()
144 m_data
= (wxDataObject
*) NULL
;
148 void wxClipboard::Clear()
153 m_data
= (wxDataObject
*) NULL
;
157 err
= ClearCurrentScrap( );
164 wxLogSysError(_("Failed to empty the clipboard."));
168 bool wxClipboard::Flush()
173 bool wxClipboard::Open()
175 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
180 bool wxClipboard::IsOpened() const
185 bool wxClipboard::SetData( wxDataObject
*data
)
187 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
189 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
193 return AddData( data
);
196 bool wxClipboard::AddData( wxDataObject
*data
)
198 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
200 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
202 wxDataFormat format
= data
->GetPreferredFormat();
204 /* we can only store one wxDataObject */
209 /* get formats from wxDataObjects */
210 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
211 m_data
->GetAllFormats( array
);
213 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
215 wxLogTrace( TRACE_CLIPBOARD
,
216 wxT("wxClipboard now supports atom %s"),
217 array
[i
].GetId().c_str() );
222 OSStatus err
= noErr
;
225 switch ( array
[i
].GetType() )
230 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
231 wxString
str(textDataObject
->GetText());
233 if ( wxApp::s_macDefaultEncodingIsPC
)
235 mac
= wxMacMakeMacStringFromPC(textDataObject
->GetText()) ;
239 mac
= textDataObject
->GetText() ;
242 err
= PutScrap( mac
.Length() , 'TEXT' , mac
.c_str() ) ;
245 err
= GetCurrentScrap (&scrap
);
248 err
= PutScrapFlavor (scrap
, 'TEXT', 0, mac
.Length(), mac
.c_str());
253 #if wxUSE_DRAG_AND_DROP
256 wxMetafileDataObject
* metaFileDataObject
=
257 (wxMetafileDataObject
*) data
;
258 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
259 PicHandle pict
= (PicHandle
) metaFile
.GetHMETAFILE() ;
260 HLock( (Handle
) pict
) ;
262 err
= PutScrap( GetHandleSize( (Handle
) pict
) , 'PICT' , *pict
) ;
265 err
= GetCurrentScrap (&scrap
);
268 err
= PutScrapFlavor (scrap
, 'PICT', 0, GetHandleSize((Handle
) pict
), *pict
);
271 HUnlock( (Handle
) pict
) ;
287 void wxClipboard::Close()
292 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
296 return m_data
->IsSupported( dataFormat
) ;
299 OSStatus err
= noErr
;
302 err
= GetCurrentScrap( &scrapRef
);
303 if ( err
!= noTypeErr
&& err
!= memFullErr
)
305 ScrapFlavorFlags flavorFlags
;
308 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
310 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
320 Handle datahandle
= NewHandle(0) ;
321 HLock( datahandle
) ;
322 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
323 HUnlock( datahandle
) ;
324 bool hasData
= GetHandleSize( datahandle
) > 0 ;
325 DisposeHandle( datahandle
) ;
330 bool wxClipboard::GetData( wxDataObject
& data
)
332 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
334 int formatcount
= data
.GetFormatCount() + 1 ;
335 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
336 array
[0] = data
.GetPreferredFormat();
337 data
.GetAllFormats( &array
[1] );
339 bool transferred
= false ;
343 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
345 wxDataFormat format
= array
[i
] ;
346 if ( m_data
->IsSupported( format
) )
348 int size
= m_data
->GetDataSize( format
);
353 data
.SetData(format
, 0 , 0 ) ;
357 char *d
= new char[size
];
358 m_data
->GetDataHere( format
, (void*) d
);
359 data
.SetData( format
, size
, d
) ;
365 /* get formats from wxDataObjects */
368 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
370 wxDataFormat format
= array
[i
] ;
372 switch ( format
.GetType() )
378 char* s
= (char*)wxGetClipboardData(format
, &len
);
381 data
.SetData( format
, len
, s
) ;