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