]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/clipbrd.cpp
alpha support for Scale added
[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 ;
254 break ;
255 #if wxUSE_UNICODE
256 case wxDF_UNICODETEXT :
257 mactype = kScrapFlavorTypeUnicode ;
258 break ;
259 #endif
260 #if wxUSE_DRAG_AND_DROP
261 case wxDF_METAFILE:
262 mactype = kScrapFlavorTypePicture ;
263 break ;
264 #endif
265 case wxDF_BITMAP:
266 case wxDF_DIB:
267 mactype = kScrapFlavorTypePicture ;
268 break ;
269 default:
270 break ;
271 }
272 UMAPutScrap( sz , mactype , buf ) ;
273 free( buf ) ;
274 }
e9576ca5
SC
275 }
276
f0822896 277 delete[] array;
e9576ca5 278
f0822896 279 return true ;
e9576ca5
SC
280}
281
f0822896 282void wxClipboard::Close()
e9576ca5 283{
634bafa9
SC
284 wxCHECK_RET( m_open, wxT("clipboard not open") );
285
f0822896 286 m_open = false ;
89a69c60
SC
287
288 // Get rid of cached object. If this is not done copying from another application will
289 // only work once
290 if (m_data)
291 {
292 delete m_data;
293 m_data = (wxDataObject*) NULL;
294 }
295
e9576ca5
SC
296}
297
f0822896 298bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
e7549107 299{
f0822896
SC
300 if ( m_data )
301 {
302 return m_data->IsSupported( dataFormat ) ;
e9576ca5 303 }
f0822896 304#if TARGET_CARBON
ed60b502
RR
305 OSStatus err = noErr;
306 ScrapRef scrapRef;
e135f093 307
ed60b502 308 err = GetCurrentScrap( &scrapRef );
e135f093 309 if ( err != noTypeErr && err != memFullErr )
ed60b502
RR
310 {
311 ScrapFlavorFlags flavorFlags;
312 Size byteCount;
e135f093 313
ed60b502
RR
314 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
315 {
316 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
317 {
318 return TRUE ;
319 }
320 }
321 }
322 return FALSE;
e135f093 323
f0822896 324#else
ed60b502
RR
325 long offset ;
326 Handle datahandle = NewHandle(0) ;
327 HLock( datahandle ) ;
328 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
329 HUnlock( datahandle ) ;
330 bool hasData = GetHandleSize( datahandle ) > 0 ;
331 DisposeHandle( datahandle ) ;
332 return hasData ;
f0822896 333#endif
e9576ca5 334}
f0822896
SC
335
336bool wxClipboard::GetData( wxDataObject& data )
e9576ca5 337{
f0822896 338 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e9576ca5 339
3340066a 340 size_t formatcount = data.GetFormatCount() + 1 ;
f0822896
SC
341 wxDataFormat *array = new wxDataFormat[ formatcount ];
342 array[0] = data.GetPreferredFormat();
343 data.GetAllFormats( &array[1] );
e9576ca5 344
f0822896 345 bool transferred = false ;
e9576ca5 346
f0822896
SC
347 if ( m_data )
348 {
349 for (size_t i = 0; !transferred && i < formatcount ; i++)
350 {
351 wxDataFormat format = array[i] ;
e135f093 352 if ( m_data->IsSupported( format ) )
f0822896
SC
353 {
354 int size = m_data->GetDataSize( format );
355 transferred = true ;
e7549107 356
e135f093 357 if (size == 0)
f0822896
SC
358 {
359 data.SetData(format , 0 , 0 ) ;
360 }
361 else
362 {
363 char *d = new char[size];
364 m_data->GetDataHere( format , (void*) d );
365 data.SetData( format , size , d ) ;
366 delete[] d ;
367 }
368 }
369 }
370 }
371 /* get formats from wxDataObjects */
e135f093 372 if ( !transferred )
f0822896 373 {
f0822896
SC
374 for (size_t i = 0; !transferred && i < formatcount ; i++)
375 {
376 wxDataFormat format = array[i] ;
377
378 switch ( format.GetType() )
379 {
9c3c5849
SC
380 case wxDF_TEXT :
381 case wxDF_OEMTEXT :
382 case wxDF_BITMAP :
383 case wxDF_METAFILE :
f0822896
SC
384 {
385 long len ;
386 char* s = (char*)wxGetClipboardData(format, &len );
387 if ( s )
388 {
389 data.SetData( format , len , s ) ;
390 delete [] s;
391
392 transferred = true ;
393 }
394 }
e40298d5 395 break ;
d1171566 396
f0822896
SC
397 default :
398 break ;
399 }
400 }
401 }
e9576ca5 402
f0822896
SC
403 delete[] array ;
404 return transferred ;
405}
179e085f
RN
406
407#endif