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 #define wxUSE_DATAOBJ 1
29 // the trace mask we use with wxLogTrace() - call
30 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
31 // (there will be a *lot* of them!)
32 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
34 void *wxGetClipboardData(wxDataFormat dataFormat
, long *len
)
39 OSStatus err
= noErr
;
43 switch (dataFormat
.GetType())
46 dataFormat
= wxDF_TEXT
;
53 wxLogError(_("Unsupported clipboard format."));
61 err
= GetCurrentScrap( &scrapRef
);
62 if ( err
!= noTypeErr
&& err
!= memFullErr
)
64 ScrapFlavorFlags flavorFlags
;
67 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
69 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
71 if ( dataFormat
.GetType() == wxDF_TEXT
)
74 data
= new char[ byteCount
] ;
75 if (( err
= GetScrapFlavorData( scrapRef
, dataFormat
.GetFormatId(), &byteCount
, data
)) == noErr
)
78 if ( dataFormat
.GetType() == wxDF_TEXT
)
79 ((char*)data
)[byteCount
] = 0 ;
83 delete[] ((char *)data
) ;
92 Handle datahandle
= NewHandle(0) ;
94 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
95 HUnlock( datahandle
) ;
96 if ( GetHandleSize( datahandle
) > 0 )
98 long byteCount
= GetHandleSize( datahandle
) ;
99 if ( dataFormat
.GetType() == wxDF_TEXT
)
100 data
= new char[ byteCount
+ 1] ;
102 data
= new char[ byteCount
] ;
104 memcpy( (char*) data
, (char*) *datahandle
, byteCount
) ;
105 if ( dataFormat
.GetType() == wxDF_TEXT
)
106 ((char*)data
)[byteCount
] = 0 ;
109 DisposeHandle( datahandle
) ;
113 wxLogSysError(_("Failed to get clipboard data."));
117 if ( dataFormat
.GetType() == wxDF_TEXT
&& wxApp::s_macDefaultEncodingIsPC
)
119 wxMacConvertToPC((char*)data
) ;
126 * Generalized clipboard implementation by Matthew Flatt
129 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
, wxClipboardBase
)
131 wxClipboard::wxClipboard()
137 wxClipboard::~wxClipboard()
142 m_data
= (wxDataObject
*) NULL
;
146 void wxClipboard::Clear()
151 m_data
= (wxDataObject
*) NULL
;
155 err
= ClearCurrentScrap( );
162 wxLogSysError(_("Failed to empty the clipboard."));
166 bool wxClipboard::Flush()
171 bool wxClipboard::Open()
173 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
178 bool wxClipboard::IsOpened() const
183 bool wxClipboard::SetData( wxDataObject
*data
)
185 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
187 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
191 return AddData( data
);
194 bool wxClipboard::AddData( wxDataObject
*data
)
196 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
198 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
200 wxDataFormat format
= data
->GetPreferredFormat();
202 /* we can only store one wxDataObject */
207 /* get formats from wxDataObjects */
208 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
209 m_data
->GetAllFormats( array
);
211 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
213 wxLogTrace( TRACE_CLIPBOARD
,
214 wxT("wxClipboard now supports atom %s"),
215 array
[i
].GetId().c_str() );
220 OSStatus err
= noErr
;
223 switch ( array
[i
].GetType() )
228 wxTextDataObject
* textDataObject
= (wxTextDataObject
*) data
;
229 wxString
str(textDataObject
->GetText());
231 if ( wxApp::s_macDefaultEncodingIsPC
)
233 mac
= wxMacMakeMacStringFromPC(textDataObject
->GetText()) ;
237 mac
= textDataObject
->GetText() ;
240 err
= PutScrap( mac
.Length() , 'TEXT' , mac
.c_str() ) ;
243 err
= GetCurrentScrap (&scrap
);
246 err
= PutScrapFlavor (scrap
, 'TEXT', 0, mac
.Length(), mac
.c_str());
251 #if wxUSE_DRAG_AND_DROP
254 wxMetafileDataObject
* metaFileDataObject
=
255 (wxMetafileDataObject
*) data
;
256 wxMetafile metaFile
= metaFileDataObject
->GetMetafile();
257 PicHandle pict
= metaFile
->GetHMETAFILE() ;
258 HLock( (Handle
) pict
) ;
260 err
= PutScrap( GetHandleSize( (Handle
) pict
) , 'PICT' , *pict
) ;
263 err
= GetCurrentScrap (&scrap
);
266 err
= PutScrapFlavor (scrap
, 'PICT', 0, GetHandleSize((Handle
) pict
), *pict
);
269 HUnlock( (Handle
) pict
) ;
285 void wxClipboard::Close()
290 bool wxClipboard::IsSupported( const wxDataFormat
&dataFormat
)
294 return m_data
->IsSupported( dataFormat
) ;
297 OSStatus err
= noErr
;
300 err
= GetCurrentScrap( &scrapRef
);
301 if ( err
!= noTypeErr
&& err
!= memFullErr
)
303 ScrapFlavorFlags flavorFlags
;
306 if (( err
= GetScrapFlavorFlags( scrapRef
, dataFormat
.GetFormatId(), &flavorFlags
)) == noErr
)
308 if (( err
= GetScrapFlavorSize( scrapRef
, dataFormat
.GetFormatId(), &byteCount
)) == noErr
)
318 Handle datahandle
= NewHandle(0) ;
319 HLock( datahandle
) ;
320 GetScrap( datahandle
, dataFormat
.GetFormatId() , &offset
) ;
321 HUnlock( datahandle
) ;
322 bool hasData
= GetHandleSize( datahandle
) > 0 ;
323 DisposeHandle( datahandle
) ;
328 bool wxClipboard::GetData( wxDataObject
& data
)
330 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
332 int formatcount
= data
.GetFormatCount() + 1 ;
333 wxDataFormat
*array
= new wxDataFormat
[ formatcount
];
334 array
[0] = data
.GetPreferredFormat();
335 data
.GetAllFormats( &array
[1] );
337 bool transferred
= false ;
341 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
343 wxDataFormat format
= array
[i
] ;
344 if ( m_data
->IsSupported( format
) )
346 int size
= m_data
->GetDataSize( format
);
351 data
.SetData(format
, 0 , 0 ) ;
355 char *d
= new char[size
];
356 m_data
->GetDataHere( format
, (void*) d
);
357 data
.SetData( format
, size
, d
) ;
363 /* get formats from wxDataObjects */
366 for (size_t i
= 0; !transferred
&& i
< formatcount
; i
++)
368 wxDataFormat format
= array
[i
] ;
370 switch ( format
.GetType() )
376 char* s
= (char*)wxGetClipboardData(format
, &len
);
379 data
.SetData( format
, len
, s
) ;