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