]> git.saurik.com Git - wxWidgets.git/blame - src/mac/clipbrd.cpp
Acutally use the m_maxChars parameter value instead of ignoring it
[wxWidgets.git] / src / mac / 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
ed60b502 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;
ed60b502 48
2f1ae414
SC
49 switch (dataFormat.GetType())
50 {
e40298d5
JS
51 case wxDF_OEMTEXT:
52 dataFormat = wxDF_TEXT;
53 // fall through
54
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 }
e40298d5 68
97af5088 69#if TARGET_CARBON
ed60b502
RR
70 ScrapRef scrapRef;
71
72 err = GetCurrentScrap( &scrapRef );
73 if ( err != noTypeErr && err != memFullErr )
74 {
75 ScrapFlavorFlags flavorFlags;
ed60b502
RR
76
77 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
78 {
79 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
80 {
427ff662
SC
81 Size allocSize = byteCount ;
82 if ( dataFormat.GetType() == wxDF_TEXT )
83 allocSize += 1 ;
84 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
85 allocSize += 2 ;
ed60b502 86
427ff662
SC
87 data = new char[ allocSize ] ;
88
e40298d5
JS
89 if (( err = GetScrapFlavorData( scrapRef, dataFormat.GetFormatId(), &byteCount , data )) == noErr )
90 {
427ff662 91 *len = allocSize ;
e40298d5
JS
92 if ( dataFormat.GetType() == wxDF_TEXT )
93 ((char*)data)[byteCount] = 0 ;
427ff662
SC
94 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
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 }
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 ;
e40298d5 116 if ( dataFormat.GetType() == wxDF_TEXT )
427ff662
SC
117 allocSize += 1 ;
118 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
119 allocSize += 2 ;
120
121 data = new char[ allocSize ] ;
122
e40298d5
JS
123 memcpy( (char*) data , (char*) *datahandle , byteCount ) ;
124 if ( dataFormat.GetType() == wxDF_TEXT )
125 ((char*)data)[byteCount] = 0 ;
427ff662
SC
126 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
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."));
e40298d5 135
97af5088
SC
136 return NULL ;
137 }
427ff662 138
97af5088
SC
139 if ( dataFormat.GetType() == wxDF_TEXT && wxApp::s_macDefaultEncodingIsPC )
140 {
427ff662
SC
141 wxString st = wxMacMakeStringFromCString( (char*) data ) ;
142#if wxUSE_UNICODE
143 wxCharBuffer buf = st.ToAscii() ;
144#else
692db919 145 const char* buf = st ;
427ff662
SC
146#endif
147 char* newdata = new char[strlen(buf)+1] ;
148 memcpy( newdata , buf , strlen(buf)+1 ) ;
149 delete[] ((char*) data ) ;
150 data = newdata ;
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{
e7549107
SC
200 return FALSE;
201}
202
203bool wxClipboard::Open()
204{
f0822896
SC
205 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
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{
f0822896 217 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e9576ca5 218
f0822896 219 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
e9576ca5 220
f0822896 221 Clear();
e7549107 222
f0822896 223 return AddData( data );
e7549107
SC
224}
225
226bool wxClipboard::AddData( wxDataObject *data )
227{
f0822896 228 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e7549107 229
f0822896 230 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
e7549107 231
f0822896
SC
232 /* we can only store one wxDataObject */
233 Clear();
e7549107 234
f0822896 235 m_data = data;
e7549107 236
f0822896
SC
237 /* get formats from wxDataObjects */
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 {
243 wxLogTrace( TRACE_CLIPBOARD,
244 wxT("wxClipboard now supports atom %s"),
245 array[i].GetId().c_str() );
e7549107 246
f0822896 247#if !TARGET_CARBON
0cd51b2d 248 OSErr err = noErr ;
f0822896 249#else
0cd51b2d 250 OSStatus err = noErr ;
f0822896 251#endif
e7549107 252
f0822896
SC
253 switch ( array[i].GetType() )
254 {
255 case wxDF_TEXT:
256 case wxDF_OEMTEXT:
257 {
258 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
259 wxString str(textDataObject->GetText());
427ff662
SC
260 wxCharBuffer buf = wxMacStringToCString( str ) ;
261 err = UMAPutScrap( strlen(buf) , kScrapFlavorTypeText , (void*) buf.data() ) ;
f0822896 262 }
d1171566 263 break ;
427ff662
SC
264#if wxUSE_UNICODE
265 case wxDF_UNICODETEXT :
266 {
267 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
268 wxString str(textDataObject->GetText());
269 err = UMAPutScrap( str.Length() * sizeof(wxChar) , kScrapFlavorTypeUnicode , (void*) str.wc_str() ) ;
270 }
271 break ;
272#endif
f0822896 273#if wxUSE_DRAG_AND_DROP
e7549107 274 case wxDF_METAFILE:
f0822896 275 {
e40298d5 276 wxMetafileDataObject* metaFileDataObject =
f0822896 277 (wxMetafileDataObject*) data;
e40298d5
JS
278 wxMetafile metaFile = metaFileDataObject->GetMetafile();
279 PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ;
280 HLock( (Handle) pict ) ;
427ff662 281 err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ;
e40298d5 282 HUnlock( (Handle) pict ) ;
f0822896 283 }
d1171566 284 break ;
e7549107 285#endif
f0822896
SC
286 case wxDF_BITMAP:
287 case wxDF_DIB:
9c3c5849 288 {
e40298d5
JS
289 bool created = false ;
290 PicHandle pict = NULL ;
291
292 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data ;
293 pict = (PicHandle) bitmapDataObject->GetBitmap().GetPict( &created ) ;
294
295 HLock( (Handle) pict ) ;
427ff662 296 err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ;
e40298d5
JS
297 HUnlock( (Handle) pict ) ;
298 if ( created )
299 KillPicture( pict ) ;
9c3c5849 300 }
f0822896 301 default:
ed60b502 302 break ;
f0822896 303 }
e9576ca5 304
e9576ca5
SC
305 }
306
f0822896 307 delete[] array;
e9576ca5 308
f0822896 309 return true ;
e9576ca5
SC
310}
311
f0822896 312void wxClipboard::Close()
e9576ca5 313{
f0822896 314 m_open = false ;
e9576ca5
SC
315}
316
f0822896 317bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
e7549107 318{
f0822896
SC
319 if ( m_data )
320 {
321 return m_data->IsSupported( dataFormat ) ;
e9576ca5 322 }
f0822896 323#if TARGET_CARBON
ed60b502
RR
324 OSStatus err = noErr;
325 ScrapRef scrapRef;
326
327 err = GetCurrentScrap( &scrapRef );
328 if ( err != noTypeErr && err != memFullErr )
329 {
330 ScrapFlavorFlags flavorFlags;
331 Size byteCount;
332
333 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
334 {
335 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
336 {
337 return TRUE ;
338 }
339 }
340 }
341 return FALSE;
342
f0822896 343#else
ed60b502
RR
344 long offset ;
345 Handle datahandle = NewHandle(0) ;
346 HLock( datahandle ) ;
347 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
348 HUnlock( datahandle ) ;
349 bool hasData = GetHandleSize( datahandle ) > 0 ;
350 DisposeHandle( datahandle ) ;
351 return hasData ;
f0822896 352#endif
e9576ca5 353}
f0822896
SC
354
355bool wxClipboard::GetData( wxDataObject& data )
e9576ca5 356{
f0822896 357 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e9576ca5 358
3340066a 359 size_t formatcount = data.GetFormatCount() + 1 ;
f0822896
SC
360 wxDataFormat *array = new wxDataFormat[ formatcount ];
361 array[0] = data.GetPreferredFormat();
362 data.GetAllFormats( &array[1] );
e9576ca5 363
f0822896 364 bool transferred = false ;
e9576ca5 365
f0822896
SC
366 if ( m_data )
367 {
368 for (size_t i = 0; !transferred && i < formatcount ; i++)
369 {
370 wxDataFormat format = array[i] ;
371 if ( m_data->IsSupported( format ) )
372 {
373 int size = m_data->GetDataSize( format );
374 transferred = true ;
e7549107 375
f0822896
SC
376 if (size == 0)
377 {
378 data.SetData(format , 0 , 0 ) ;
379 }
380 else
381 {
382 char *d = new char[size];
383 m_data->GetDataHere( format , (void*) d );
384 data.SetData( format , size , d ) ;
385 delete[] d ;
386 }
387 }
388 }
389 }
390 /* get formats from wxDataObjects */
391 if ( !transferred )
392 {
f0822896
SC
393 for (size_t i = 0; !transferred && i < formatcount ; i++)
394 {
395 wxDataFormat format = array[i] ;
396
397 switch ( format.GetType() )
398 {
9c3c5849
SC
399 case wxDF_TEXT :
400 case wxDF_OEMTEXT :
401 case wxDF_BITMAP :
402 case wxDF_METAFILE :
f0822896
SC
403 {
404 long len ;
405 char* s = (char*)wxGetClipboardData(format, &len );
406 if ( s )
407 {
408 data.SetData( format , len , s ) ;
409 delete [] s;
410
411 transferred = true ;
412 }
413 }
e40298d5 414 break ;
d1171566 415
f0822896
SC
416 default :
417 break ;
418 }
419 }
420 }
e9576ca5 421
f0822896
SC
422 delete[] array ;
423 return transferred ;
424}