]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/clipbrd.cpp
making returns in a line to always be \n (13 in CFM (mpwnewline) and 10 in Mach)
[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
12#ifdef __GNUG__
e9576ca5
SC
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"
5fde6fcc 22#include "wx/intl.h"
48d2ab90 23#include "wx/log.h"
e9576ca5 24
66a09d47
SC
25#ifndef __DARWIN__
26#include <Scrap.h>
27#endif
9c3c5849 28#include "wx/mac/uma.h"
76a5e5d2 29
2f1ae414
SC
30#define wxUSE_DATAOBJ 1
31
e9576ca5
SC
32#include <string.h>
33
f0822896
SC
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");
2f1ae414 38
f0822896 39void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
e9576ca5 40{
2f1ae414 41#if !TARGET_CARBON
ed60b502 42 OSErr err = noErr ;
2f1ae414 43#else
ed60b502 44 OSStatus err = noErr ;
2f1ae414 45#endif
e40298d5
JS
46 void * data = NULL ;
47 Size byteCount;
e135f093 48
2f1ae414
SC
49 switch (dataFormat.GetType())
50 {
e40298d5
JS
51 case wxDF_OEMTEXT:
52 dataFormat = wxDF_TEXT;
53 // fall through
e135f093 54
e40298d5
JS
55 case wxDF_TEXT:
56 break;
427ff662
SC
57 case wxDF_UNICODETEXT:
58 break;
e40298d5
JS
59 case wxDF_BITMAP :
60 case wxDF_METAFILE :
61 break ;
62 default:
63 {
64 wxLogError(_("Unsupported clipboard format."));
65 return NULL;
66 }
97af5088 67 }
e135f093 68
97af5088 69#if TARGET_CARBON
ed60b502 70 ScrapRef scrapRef;
e135f093 71
ed60b502 72 err = GetCurrentScrap( &scrapRef );
e135f093 73 if ( err != noTypeErr && err != memFullErr )
ed60b502
RR
74 {
75 ScrapFlavorFlags flavorFlags;
e135f093 76
ed60b502
RR
77 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
78 {
79 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
80 {
427ff662 81 Size allocSize = byteCount ;
e135f093 82 if ( dataFormat.GetType() == wxDF_TEXT )
427ff662 83 allocSize += 1 ;
e135f093 84 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 85 allocSize += 2 ;
e135f093 86
427ff662 87 data = new char[ allocSize ] ;
e135f093 88
e40298d5
JS
89 if (( err = GetScrapFlavorData( scrapRef, dataFormat.GetFormatId(), &byteCount , data )) == noErr )
90 {
427ff662 91 *len = allocSize ;
e135f093 92 if ( dataFormat.GetType() == wxDF_TEXT )
e40298d5 93 ((char*)data)[byteCount] = 0 ;
e135f093 94 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 95 ((wxChar*)data)[byteCount/2] = 0 ;
e40298d5
JS
96 }
97 else
98 {
99 delete[] ((char *)data) ;
100 data = NULL ;
101 }
ed60b502
RR
102 }
103 }
104 }
e135f093 105
97af5088 106#else
ed60b502
RR
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 {
e40298d5 114 byteCount = GetHandleSize( datahandle ) ;
427ff662 115 Size allocSize = byteCount ;
e135f093 116 if ( dataFormat.GetType() == wxDF_TEXT )
427ff662 117 allocSize += 1 ;
e135f093 118 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662
SC
119 allocSize += 2 ;
120
121 data = new char[ allocSize ] ;
122
e40298d5 123 memcpy( (char*) data , (char*) *datahandle , byteCount ) ;
e135f093 124 if ( dataFormat.GetType() == wxDF_TEXT )
e40298d5 125 ((char*)data)[byteCount] = 0 ;
e135f093 126 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 127 ((wxChar*)data)[byteCount/2] = 0 ;
e40298d5 128 *len = byteCount ;
ed60b502
RR
129 }
130 DisposeHandle( datahandle ) ;
97af5088
SC
131#endif
132 if ( err )
133 {
134 wxLogSysError(_("Failed to get clipboard data."));
e135f093 135
97af5088
SC
136 return NULL ;
137 }
427ff662 138
939fba6c 139 if ( dataFormat.GetType() == wxDF_TEXT )
97af5088 140 {
8aa701ed 141 wxMacConvertNewlines10To13( (char*) data ) ;
97af5088 142 }
427ff662 143
97af5088 144 return data;
2f1ae414
SC
145}
146
147
e9576ca5
SC
148/*
149 * Generalized clipboard implementation by Matthew Flatt
150 */
151
528e2293 152IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
7c74e7fe 153
e7549107
SC
154wxClipboard::wxClipboard()
155{
e40298d5
JS
156 m_open = false ;
157 m_data = NULL ;
e7549107 158}
e9576ca5 159
e7549107 160wxClipboard::~wxClipboard()
e9576ca5 161{
f0822896 162 if (m_data)
e7549107 163 {
f0822896
SC
164 delete m_data;
165 m_data = (wxDataObject*) NULL;
e7549107 166 }
e9576ca5
SC
167}
168
e7549107 169void wxClipboard::Clear()
e9576ca5 170{
f0822896
SC
171 if (m_data)
172 {
173 delete m_data;
174 m_data = (wxDataObject*) NULL;
175 }
176#if TARGET_CARBON
ed60b502
RR
177 OSStatus err ;
178 err = ClearCurrentScrap( );
f0822896 179#else
ed60b502
RR
180 OSErr err ;
181 err = ZeroScrap( );
f0822896 182#endif
ed60b502
RR
183 if ( err )
184 {
f0822896 185 wxLogSysError(_("Failed to empty the clipboard."));
ed60b502 186 }
e9576ca5
SC
187}
188
e7549107 189bool wxClipboard::Flush()
e9576ca5 190{
e7549107
SC
191 return FALSE;
192}
193
194bool wxClipboard::Open()
195{
f0822896
SC
196 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
197 m_open = true ;
198 return true ;
e7549107
SC
199}
200
201bool wxClipboard::IsOpened() const
202{
f0822896 203 return m_open;
e9576ca5
SC
204}
205
f0822896 206bool wxClipboard::SetData( wxDataObject *data )
e9576ca5 207{
634bafa9
SC
208 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
209
210 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
211
212 Clear();
e135f093
VZ
213 // as we can only store one wxDataObject, this is the same in this
214 // implementation
f0822896 215 return AddData( data );
e7549107
SC
216}
217
218bool wxClipboard::AddData( wxDataObject *data )
219{
f0822896 220 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e7549107 221
f0822896 222 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
e7549107 223
f0822896
SC
224 /* we can only store one wxDataObject */
225 Clear();
e7549107 226
f0822896 227 m_data = data;
e7549107 228
f0822896
SC
229 /* get formats from wxDataObjects */
230 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
231 m_data->GetAllFormats( array );
e7549107 232
f0822896
SC
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() );
e7549107 238
634bafa9
SC
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 }
e9576ca5
SC
271 }
272
f0822896 273 delete[] array;
e9576ca5 274
f0822896 275 return true ;
e9576ca5
SC
276}
277
f0822896 278void wxClipboard::Close()
e9576ca5 279{
634bafa9
SC
280 wxCHECK_RET( m_open, wxT("clipboard not open") );
281
f0822896 282 m_open = false ;
89a69c60
SC
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
e9576ca5
SC
292}
293
f0822896 294bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
e7549107 295{
f0822896
SC
296 if ( m_data )
297 {
298 return m_data->IsSupported( dataFormat ) ;
e9576ca5 299 }
f0822896 300#if TARGET_CARBON
ed60b502
RR
301 OSStatus err = noErr;
302 ScrapRef scrapRef;
e135f093 303
ed60b502 304 err = GetCurrentScrap( &scrapRef );
e135f093 305 if ( err != noTypeErr && err != memFullErr )
ed60b502
RR
306 {
307 ScrapFlavorFlags flavorFlags;
308 Size byteCount;
e135f093 309
ed60b502
RR
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;
e135f093 319
f0822896 320#else
ed60b502
RR
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 ;
f0822896 329#endif
e9576ca5 330}
f0822896
SC
331
332bool wxClipboard::GetData( wxDataObject& data )
e9576ca5 333{
f0822896 334 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e9576ca5 335
3340066a 336 size_t formatcount = data.GetFormatCount() + 1 ;
f0822896
SC
337 wxDataFormat *array = new wxDataFormat[ formatcount ];
338 array[0] = data.GetPreferredFormat();
339 data.GetAllFormats( &array[1] );
e9576ca5 340
f0822896 341 bool transferred = false ;
e9576ca5 342
f0822896
SC
343 if ( m_data )
344 {
345 for (size_t i = 0; !transferred && i < formatcount ; i++)
346 {
347 wxDataFormat format = array[i] ;
e135f093 348 if ( m_data->IsSupported( format ) )
f0822896
SC
349 {
350 int size = m_data->GetDataSize( format );
351 transferred = true ;
e7549107 352
e135f093 353 if (size == 0)
f0822896
SC
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 */
e135f093 368 if ( !transferred )
f0822896 369 {
f0822896
SC
370 for (size_t i = 0; !transferred && i < formatcount ; i++)
371 {
372 wxDataFormat format = array[i] ;
373
374 switch ( format.GetType() )
375 {
9c3c5849
SC
376 case wxDF_TEXT :
377 case wxDF_OEMTEXT :
378 case wxDF_BITMAP :
379 case wxDF_METAFILE :
f0822896
SC
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 }
e40298d5 391 break ;
d1171566 392
f0822896
SC
393 default :
394 break ;
395 }
396 }
397 }
e9576ca5 398
f0822896
SC
399 delete[] array ;
400 return transferred ;
401}