]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/clipbrd.cpp
wxPalmOS build fix.
[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
SC
12#include "wx/wxprec.h"
13
179e085f
RN
14#if wxUSE_CLIPBOARD
15
e9576ca5
SC
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:
21ec3bf9
SC
63 // custom datatype
64 break ;
97af5088 65 }
e135f093 66
97af5088 67#if TARGET_CARBON
ed60b502 68 ScrapRef scrapRef;
e135f093 69
ed60b502 70 err = GetCurrentScrap( &scrapRef );
e135f093 71 if ( err != noTypeErr && err != memFullErr )
ed60b502
RR
72 {
73 ScrapFlavorFlags flavorFlags;
e135f093 74
ed60b502
RR
75 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
76 {
77 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
78 {
427ff662 79 Size allocSize = byteCount ;
e135f093 80 if ( dataFormat.GetType() == wxDF_TEXT )
427ff662 81 allocSize += 1 ;
e135f093 82 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 83 allocSize += 2 ;
e135f093 84
427ff662 85 data = new char[ allocSize ] ;
e135f093 86
e40298d5
JS
87 if (( err = GetScrapFlavorData( scrapRef, dataFormat.GetFormatId(), &byteCount , data )) == noErr )
88 {
427ff662 89 *len = allocSize ;
e135f093 90 if ( dataFormat.GetType() == wxDF_TEXT )
e40298d5 91 ((char*)data)[byteCount] = 0 ;
e135f093 92 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
99b62b5f
KH
93 {
94 // "data" format is UTF16, so 2 bytes = one character
95 // wxChar size depends on platform, so just clear last 2 bytes
96 ((char*)data)[byteCount] = 0;
97 ((char*)data)[byteCount+1] = 0;
98 }
e40298d5
JS
99 }
100 else
101 {
102 delete[] ((char *)data) ;
103 data = NULL ;
104 }
ed60b502
RR
105 }
106 }
107 }
e135f093 108
97af5088 109#else
ed60b502
RR
110 long offset ;
111 Handle datahandle = NewHandle(0) ;
112 HLock( datahandle ) ;
113 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
114 HUnlock( datahandle ) ;
115 if ( GetHandleSize( datahandle ) > 0 )
116 {
e40298d5 117 byteCount = GetHandleSize( datahandle ) ;
427ff662 118 Size allocSize = byteCount ;
e135f093 119 if ( dataFormat.GetType() == wxDF_TEXT )
427ff662 120 allocSize += 1 ;
e135f093 121 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662
SC
122 allocSize += 2 ;
123
124 data = new char[ allocSize ] ;
125
e40298d5 126 memcpy( (char*) data , (char*) *datahandle , byteCount ) ;
e135f093 127 if ( dataFormat.GetType() == wxDF_TEXT )
e40298d5 128 ((char*)data)[byteCount] = 0 ;
e135f093 129 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 130 ((wxChar*)data)[byteCount/2] = 0 ;
e40298d5 131 *len = byteCount ;
ed60b502
RR
132 }
133 DisposeHandle( datahandle ) ;
97af5088
SC
134#endif
135 if ( err )
136 {
137 wxLogSysError(_("Failed to get clipboard data."));
e135f093 138
97af5088
SC
139 return NULL ;
140 }
427ff662 141
939fba6c 142 if ( dataFormat.GetType() == wxDF_TEXT )
97af5088 143 {
8aa701ed 144 wxMacConvertNewlines10To13( (char*) data ) ;
97af5088 145 }
427ff662 146
97af5088 147 return data;
2f1ae414
SC
148}
149
150
e9576ca5
SC
151/*
152 * Generalized clipboard implementation by Matthew Flatt
153 */
154
528e2293 155IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
7c74e7fe 156
e7549107
SC
157wxClipboard::wxClipboard()
158{
e40298d5
JS
159 m_open = false ;
160 m_data = NULL ;
e7549107 161}
e9576ca5 162
e7549107 163wxClipboard::~wxClipboard()
e9576ca5 164{
f0822896 165 if (m_data)
e7549107 166 {
f0822896
SC
167 delete m_data;
168 m_data = (wxDataObject*) NULL;
e7549107 169 }
e9576ca5
SC
170}
171
e7549107 172void wxClipboard::Clear()
e9576ca5 173{
f0822896
SC
174 if (m_data)
175 {
176 delete m_data;
177 m_data = (wxDataObject*) NULL;
178 }
179#if TARGET_CARBON
ed60b502
RR
180 OSStatus err ;
181 err = ClearCurrentScrap( );
f0822896 182#else
ed60b502
RR
183 OSErr err ;
184 err = ZeroScrap( );
f0822896 185#endif
ed60b502
RR
186 if ( err )
187 {
f0822896 188 wxLogSysError(_("Failed to empty the clipboard."));
ed60b502 189 }
e9576ca5
SC
190}
191
e7549107 192bool wxClipboard::Flush()
e9576ca5 193{
3dee36ae 194 return false;
e7549107
SC
195}
196
197bool wxClipboard::Open()
198{
3dee36ae 199 wxCHECK_MSG( !m_open, false, wxT("clipboard already open") );
f0822896
SC
200 m_open = true ;
201 return true ;
e7549107
SC
202}
203
204bool wxClipboard::IsOpened() const
205{
f0822896 206 return m_open;
e9576ca5
SC
207}
208
f0822896 209bool wxClipboard::SetData( wxDataObject *data )
e9576ca5 210{
3dee36ae 211 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
634bafa9 212
3dee36ae 213 wxCHECK_MSG( data, false, wxT("data is invalid") );
634bafa9
SC
214
215 Clear();
e135f093
VZ
216 // as we can only store one wxDataObject, this is the same in this
217 // implementation
f0822896 218 return AddData( data );
e7549107
SC
219}
220
221bool wxClipboard::AddData( wxDataObject *data )
222{
3dee36ae 223 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
e7549107 224
3dee36ae 225 wxCHECK_MSG( data, false, wxT("data is invalid") );
e7549107 226
21ec3bf9 227 // we can only store one wxDataObject
f0822896 228 Clear();
e7549107 229
f0822896 230 m_data = data;
e7549107 231
21ec3bf9 232 // get formats from wxDataObjects
f0822896
SC
233 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
234 m_data->GetAllFormats( array );
e7549107 235
f0822896
SC
236 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
237 {
238 wxLogTrace( TRACE_CLIPBOARD,
239 wxT("wxClipboard now supports atom %s"),
240 array[i].GetId().c_str() );
e7549107 241
634bafa9
SC
242 size_t sz = data->GetDataSize( array[i] ) ;
243 void* buf = malloc( sz + 1 ) ;
244 if ( buf )
3dee36ae 245 {
99b62b5f
KH
246 // empty the buffer because in some case GetDataHere does not fill buf
247 memset(buf, 0, sz+1);
634bafa9
SC
248 data->GetDataHere( array[i] , buf ) ;
249 OSType mactype = 0 ;
250 switch ( array[i].GetType() )
251 {
252 case wxDF_TEXT:
253 case wxDF_OEMTEXT:
254 mactype = kScrapFlavorTypeText ;
0c4f1b2a 255 sz -= 1;
634bafa9
SC
256 break ;
257 #if wxUSE_UNICODE
258 case wxDF_UNICODETEXT :
259 mactype = kScrapFlavorTypeUnicode ;
0c4f1b2a 260 sz -= 2;
634bafa9
SC
261 break ;
262 #endif
263 #if wxUSE_DRAG_AND_DROP
264 case wxDF_METAFILE:
265 mactype = kScrapFlavorTypePicture ;
266 break ;
267 #endif
268 case wxDF_BITMAP:
269 case wxDF_DIB:
270 mactype = kScrapFlavorTypePicture ;
271 break ;
272 default:
21ec3bf9 273 mactype = (OSType)(array[i].GetFormatId());
634bafa9
SC
274 break ;
275 }
276 UMAPutScrap( sz , mactype , buf ) ;
277 free( buf ) ;
278 }
e9576ca5
SC
279 }
280
f0822896 281 delete[] array;
e9576ca5 282
f0822896 283 return true ;
e9576ca5
SC
284}
285
f0822896 286void wxClipboard::Close()
e9576ca5 287{
634bafa9
SC
288 wxCHECK_RET( m_open, wxT("clipboard not open") );
289
f0822896 290 m_open = false ;
3dee36ae
WS
291
292 // Get rid of cached object. If this is not done copying from another application will
293 // only work once
89a69c60
SC
294 if (m_data)
295 {
296 delete m_data;
297 m_data = (wxDataObject*) NULL;
3dee36ae 298 }
e9576ca5
SC
299}
300
f0822896 301bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
e7549107 302{
3dee36ae
WS
303 if ( m_data )
304 {
305 return m_data->IsSupported( dataFormat ) ;
306 }
f0822896 307#if TARGET_CARBON
ed60b502
RR
308 OSStatus err = noErr;
309 ScrapRef scrapRef;
e135f093 310
ed60b502 311 err = GetCurrentScrap( &scrapRef );
e135f093 312 if ( err != noTypeErr && err != memFullErr )
ed60b502
RR
313 {
314 ScrapFlavorFlags flavorFlags;
315 Size byteCount;
e135f093 316
ed60b502
RR
317 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
318 {
319 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
320 {
3dee36ae 321 return true ;
ed60b502
RR
322 }
323 }
324 }
3dee36ae 325 return false;
e135f093 326
f0822896 327#else
ed60b502
RR
328 long offset ;
329 Handle datahandle = NewHandle(0) ;
330 HLock( datahandle ) ;
331 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
332 HUnlock( datahandle ) ;
333 bool hasData = GetHandleSize( datahandle ) > 0 ;
334 DisposeHandle( datahandle ) ;
335 return hasData ;
f0822896 336#endif
e9576ca5 337}
f0822896
SC
338
339bool wxClipboard::GetData( wxDataObject& data )
e9576ca5 340{
3dee36ae 341 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
e9576ca5 342
3340066a 343 size_t formatcount = data.GetFormatCount() + 1 ;
f0822896
SC
344 wxDataFormat *array = new wxDataFormat[ formatcount ];
345 array[0] = data.GetPreferredFormat();
346 data.GetAllFormats( &array[1] );
e9576ca5 347
f0822896 348 bool transferred = false ;
e9576ca5 349
f0822896
SC
350 if ( m_data )
351 {
21ec3bf9
SC
352 for (size_t i = 0; !transferred && i < formatcount ; i++)
353 {
354 wxDataFormat format = array[i] ;
355 if ( m_data->IsSupported( format ) )
f0822896 356 {
21ec3bf9
SC
357 int size = m_data->GetDataSize( format );
358 transferred = true ;
359
360 if (size == 0)
361 {
362 data.SetData(format , 0 , 0 ) ;
363 }
364 else
365 {
366 char *d = new char[size];
367 m_data->GetDataHere( format , (void*) d );
368 data.SetData( format , size , d ) ;
369 delete[] d ;
370 }
f0822896 371 }
21ec3bf9 372 }
f0822896 373 }
21ec3bf9
SC
374
375 // get formats from wxDataObjects
e135f093 376 if ( !transferred )
f0822896 377 {
21ec3bf9
SC
378 for (size_t i = 0; !transferred && i < formatcount ; i++)
379 {
380 wxDataFormat format = array[i] ;
381
382 switch ( format.GetType() )
383 {
384 // NOTE: this is usable for all data types
385 case wxDF_TEXT :
386 case wxDF_UNICODETEXT:
387 case wxDF_OEMTEXT :
388 case wxDF_BITMAP :
389 case wxDF_METAFILE :
390 default :
391 {
392 long len ;
393 char* s = (char*)wxGetClipboardData(format, &len );
394 if ( s )
395 {
396 data.SetData( format , len , s ) ;
397 delete [] s;
398
399 transferred = true ;
400 }
401 }
f0822896 402 break ;
21ec3bf9
SC
403 }
404 }
f0822896 405 }
e9576ca5 406
f0822896
SC
407 delete[] array ;
408 return transferred ;
409}
179e085f
RN
410
411#endif