]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/clipbrd.cpp
added test for wxScopeGuard
[wxWidgets.git] / src / mac / carbon / clipbrd.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: clipbrd.cpp
3// Purpose: Clipboard functionality
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
a8e9860d 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
e9576ca5
SC
13#pragma implementation "clipbrd.h"
14#endif
15
a8e9860d
SC
16#include "wx/wxprec.h"
17
179e085f
RN
18#if wxUSE_CLIPBOARD
19
e9576ca5
SC
20#include "wx/app.h"
21#include "wx/frame.h"
22#include "wx/bitmap.h"
23#include "wx/utils.h"
24#include "wx/metafile.h"
25#include "wx/clipbrd.h"
5fde6fcc 26#include "wx/intl.h"
48d2ab90 27#include "wx/log.h"
e9576ca5 28
66a09d47
SC
29#ifndef __DARWIN__
30#include <Scrap.h>
31#endif
9c3c5849 32#include "wx/mac/uma.h"
76a5e5d2 33
2f1ae414
SC
34#define wxUSE_DATAOBJ 1
35
e9576ca5
SC
36#include <string.h>
37
f0822896
SC
38// the trace mask we use with wxLogTrace() - call
39// wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
40// (there will be a *lot* of them!)
41static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
2f1ae414 42
f0822896 43void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
e9576ca5 44{
2f1ae414 45#if !TARGET_CARBON
ed60b502 46 OSErr err = noErr ;
2f1ae414 47#else
ed60b502 48 OSStatus err = noErr ;
2f1ae414 49#endif
e40298d5
JS
50 void * data = NULL ;
51 Size byteCount;
e135f093 52
2f1ae414
SC
53 switch (dataFormat.GetType())
54 {
e40298d5
JS
55 case wxDF_OEMTEXT:
56 dataFormat = wxDF_TEXT;
57 // fall through
e135f093 58
e40298d5
JS
59 case wxDF_TEXT:
60 break;
427ff662
SC
61 case wxDF_UNICODETEXT:
62 break;
e40298d5
JS
63 case wxDF_BITMAP :
64 case wxDF_METAFILE :
65 break ;
66 default:
67 {
68 wxLogError(_("Unsupported clipboard format."));
69 return NULL;
70 }
97af5088 71 }
e135f093 72
97af5088 73#if TARGET_CARBON
ed60b502 74 ScrapRef scrapRef;
e135f093 75
ed60b502 76 err = GetCurrentScrap( &scrapRef );
e135f093 77 if ( err != noTypeErr && err != memFullErr )
ed60b502
RR
78 {
79 ScrapFlavorFlags flavorFlags;
e135f093 80
ed60b502
RR
81 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
82 {
83 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
84 {
427ff662 85 Size allocSize = byteCount ;
e135f093 86 if ( dataFormat.GetType() == wxDF_TEXT )
427ff662 87 allocSize += 1 ;
e135f093 88 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 89 allocSize += 2 ;
e135f093 90
427ff662 91 data = new char[ allocSize ] ;
e135f093 92
e40298d5
JS
93 if (( err = GetScrapFlavorData( scrapRef, dataFormat.GetFormatId(), &byteCount , data )) == noErr )
94 {
427ff662 95 *len = allocSize ;
e135f093 96 if ( dataFormat.GetType() == wxDF_TEXT )
e40298d5 97 ((char*)data)[byteCount] = 0 ;
e135f093 98 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 99 ((wxChar*)data)[byteCount/2] = 0 ;
e40298d5
JS
100 }
101 else
102 {
103 delete[] ((char *)data) ;
104 data = NULL ;
105 }
ed60b502
RR
106 }
107 }
108 }
e135f093 109
97af5088 110#else
ed60b502
RR
111 long offset ;
112 Handle datahandle = NewHandle(0) ;
113 HLock( datahandle ) ;
114 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
115 HUnlock( datahandle ) ;
116 if ( GetHandleSize( datahandle ) > 0 )
117 {
e40298d5 118 byteCount = GetHandleSize( datahandle ) ;
427ff662 119 Size allocSize = byteCount ;
e135f093 120 if ( dataFormat.GetType() == wxDF_TEXT )
427ff662 121 allocSize += 1 ;
e135f093 122 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662
SC
123 allocSize += 2 ;
124
125 data = new char[ allocSize ] ;
126
e40298d5 127 memcpy( (char*) data , (char*) *datahandle , byteCount ) ;
e135f093 128 if ( dataFormat.GetType() == wxDF_TEXT )
e40298d5 129 ((char*)data)[byteCount] = 0 ;
e135f093 130 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 131 ((wxChar*)data)[byteCount/2] = 0 ;
e40298d5 132 *len = byteCount ;
ed60b502
RR
133 }
134 DisposeHandle( datahandle ) ;
97af5088
SC
135#endif
136 if ( err )
137 {
138 wxLogSysError(_("Failed to get clipboard data."));
e135f093 139
97af5088
SC
140 return NULL ;
141 }
427ff662 142
939fba6c 143 if ( dataFormat.GetType() == wxDF_TEXT )
97af5088 144 {
8aa701ed 145 wxMacConvertNewlines10To13( (char*) data ) ;
97af5088 146 }
427ff662 147
97af5088 148 return data;
2f1ae414
SC
149}
150
151
e9576ca5
SC
152/*
153 * Generalized clipboard implementation by Matthew Flatt
154 */
155
528e2293 156IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
7c74e7fe 157
e7549107
SC
158wxClipboard::wxClipboard()
159{
e40298d5
JS
160 m_open = false ;
161 m_data = NULL ;
e7549107 162}
e9576ca5 163
e7549107 164wxClipboard::~wxClipboard()
e9576ca5 165{
f0822896 166 if (m_data)
e7549107 167 {
f0822896
SC
168 delete m_data;
169 m_data = (wxDataObject*) NULL;
e7549107 170 }
e9576ca5
SC
171}
172
e7549107 173void wxClipboard::Clear()
e9576ca5 174{
f0822896
SC
175 if (m_data)
176 {
177 delete m_data;
178 m_data = (wxDataObject*) NULL;
179 }
180#if TARGET_CARBON
ed60b502
RR
181 OSStatus err ;
182 err = ClearCurrentScrap( );
f0822896 183#else
ed60b502
RR
184 OSErr err ;
185 err = ZeroScrap( );
f0822896 186#endif
ed60b502
RR
187 if ( err )
188 {
f0822896 189 wxLogSysError(_("Failed to empty the clipboard."));
ed60b502 190 }
e9576ca5
SC
191}
192
e7549107 193bool wxClipboard::Flush()
e9576ca5 194{
e7549107
SC
195 return FALSE;
196}
197
198bool wxClipboard::Open()
199{
f0822896
SC
200 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
201 m_open = true ;
202 return true ;
e7549107
SC
203}
204
205bool wxClipboard::IsOpened() const
206{
f0822896 207 return m_open;
e9576ca5
SC
208}
209
f0822896 210bool wxClipboard::SetData( wxDataObject *data )
e9576ca5 211{
634bafa9
SC
212 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
213
214 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
215
216 Clear();
e135f093
VZ
217 // as we can only store one wxDataObject, this is the same in this
218 // implementation
f0822896 219 return AddData( data );
e7549107
SC
220}
221
222bool wxClipboard::AddData( wxDataObject *data )
223{
f0822896 224 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e7549107 225
f0822896 226 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
e7549107 227
f0822896
SC
228 /* we can only store one wxDataObject */
229 Clear();
e7549107 230
f0822896 231 m_data = data;
e7549107 232
f0822896
SC
233 /* get formats from wxDataObjects */
234 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
235 m_data->GetAllFormats( array );
e7549107 236
f0822896
SC
237 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
238 {
239 wxLogTrace( TRACE_CLIPBOARD,
240 wxT("wxClipboard now supports atom %s"),
241 array[i].GetId().c_str() );
e7549107 242
634bafa9
SC
243 size_t sz = data->GetDataSize( array[i] ) ;
244 void* buf = malloc( sz + 1 ) ;
245 if ( buf )
246 {
247 data->GetDataHere( array[i] , buf ) ;
248 OSType mactype = 0 ;
249 switch ( array[i].GetType() )
250 {
251 case wxDF_TEXT:
252 case wxDF_OEMTEXT:
253 mactype = kScrapFlavorTypeText ;
0c4f1b2a 254 sz -= 1;
634bafa9
SC
255 break ;
256 #if wxUSE_UNICODE
257 case wxDF_UNICODETEXT :
258 mactype = kScrapFlavorTypeUnicode ;
0c4f1b2a 259 sz -= 2;
634bafa9
SC
260 break ;
261 #endif
262 #if wxUSE_DRAG_AND_DROP
263 case wxDF_METAFILE:
264 mactype = kScrapFlavorTypePicture ;
265 break ;
266 #endif
267 case wxDF_BITMAP:
268 case wxDF_DIB:
269 mactype = kScrapFlavorTypePicture ;
270 break ;
271 default:
272 break ;
273 }
274 UMAPutScrap( sz , mactype , buf ) ;
275 free( buf ) ;
276 }
e9576ca5
SC
277 }
278
f0822896 279 delete[] array;
e9576ca5 280
f0822896 281 return true ;
e9576ca5
SC
282}
283
f0822896 284void wxClipboard::Close()
e9576ca5 285{
634bafa9
SC
286 wxCHECK_RET( m_open, wxT("clipboard not open") );
287
f0822896 288 m_open = false ;
89a69c60
SC
289
290 // Get rid of cached object. If this is not done copying from another application will
291 // only work once
292 if (m_data)
293 {
294 delete m_data;
295 m_data = (wxDataObject*) NULL;
296 }
297
e9576ca5
SC
298}
299
f0822896 300bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
e7549107 301{
f0822896
SC
302 if ( m_data )
303 {
304 return m_data->IsSupported( dataFormat ) ;
e9576ca5 305 }
f0822896 306#if TARGET_CARBON
ed60b502
RR
307 OSStatus err = noErr;
308 ScrapRef scrapRef;
e135f093 309
ed60b502 310 err = GetCurrentScrap( &scrapRef );
e135f093 311 if ( err != noTypeErr && err != memFullErr )
ed60b502
RR
312 {
313 ScrapFlavorFlags flavorFlags;
314 Size byteCount;
e135f093 315
ed60b502
RR
316 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
317 {
318 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
319 {
320 return TRUE ;
321 }
322 }
323 }
324 return FALSE;
e135f093 325
f0822896 326#else
ed60b502
RR
327 long offset ;
328 Handle datahandle = NewHandle(0) ;
329 HLock( datahandle ) ;
330 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
331 HUnlock( datahandle ) ;
332 bool hasData = GetHandleSize( datahandle ) > 0 ;
333 DisposeHandle( datahandle ) ;
334 return hasData ;
f0822896 335#endif
e9576ca5 336}
f0822896
SC
337
338bool wxClipboard::GetData( wxDataObject& data )
e9576ca5 339{
f0822896 340 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e9576ca5 341
3340066a 342 size_t formatcount = data.GetFormatCount() + 1 ;
f0822896
SC
343 wxDataFormat *array = new wxDataFormat[ formatcount ];
344 array[0] = data.GetPreferredFormat();
345 data.GetAllFormats( &array[1] );
e9576ca5 346
f0822896 347 bool transferred = false ;
e9576ca5 348
f0822896
SC
349 if ( m_data )
350 {
351 for (size_t i = 0; !transferred && i < formatcount ; i++)
352 {
353 wxDataFormat format = array[i] ;
e135f093 354 if ( m_data->IsSupported( format ) )
f0822896
SC
355 {
356 int size = m_data->GetDataSize( format );
357 transferred = true ;
e7549107 358
e135f093 359 if (size == 0)
f0822896
SC
360 {
361 data.SetData(format , 0 , 0 ) ;
362 }
363 else
364 {
365 char *d = new char[size];
366 m_data->GetDataHere( format , (void*) d );
367 data.SetData( format , size , d ) ;
368 delete[] d ;
369 }
370 }
371 }
372 }
373 /* get formats from wxDataObjects */
e135f093 374 if ( !transferred )
f0822896 375 {
f0822896
SC
376 for (size_t i = 0; !transferred && i < formatcount ; i++)
377 {
378 wxDataFormat format = array[i] ;
379
380 switch ( format.GetType() )
381 {
9c3c5849
SC
382 case wxDF_TEXT :
383 case wxDF_OEMTEXT :
384 case wxDF_BITMAP :
385 case wxDF_METAFILE :
f0822896
SC
386 {
387 long len ;
388 char* s = (char*)wxGetClipboardData(format, &len );
389 if ( s )
390 {
391 data.SetData( format , len , s ) ;
392 delete [] s;
393
394 transferred = true ;
395 }
396 }
e40298d5 397 break ;
d1171566 398
f0822896
SC
399 default :
400 break ;
401 }
402 }
403 }
e9576ca5 404
f0822896
SC
405 delete[] array ;
406 return transferred ;
407}
179e085f
RN
408
409#endif