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