]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/clipbrd.cpp
unicode savvy conversions
[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
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;
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{
e135f093
VZ
208 // as we can only store one wxDataObject, this is the same in this
209 // implementation
f0822896 210 return AddData( data );
e7549107
SC
211}
212
213bool wxClipboard::AddData( wxDataObject *data )
214{
f0822896 215 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e7549107 216
f0822896 217 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
e7549107 218
f0822896
SC
219 /* we can only store one wxDataObject */
220 Clear();
e7549107 221
f0822896 222 m_data = data;
e7549107 223
f0822896
SC
224 /* get formats from wxDataObjects */
225 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
226 m_data->GetAllFormats( array );
e7549107 227
f0822896
SC
228 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
229 {
230 wxLogTrace( TRACE_CLIPBOARD,
231 wxT("wxClipboard now supports atom %s"),
232 array[i].GetId().c_str() );
e7549107 233
f0822896 234#if !TARGET_CARBON
0cd51b2d 235 OSErr err = noErr ;
f0822896 236#else
0cd51b2d 237 OSStatus err = noErr ;
f0822896 238#endif
e7549107 239
f0822896
SC
240 switch ( array[i].GetType() )
241 {
242 case wxDF_TEXT:
243 case wxDF_OEMTEXT:
244 {
245 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
939fba6c 246 wxCharBuffer buf = textDataObject->GetText().mb_str() ;
427ff662 247 err = UMAPutScrap( strlen(buf) , kScrapFlavorTypeText , (void*) buf.data() ) ;
f0822896 248 }
d1171566 249 break ;
427ff662
SC
250#if wxUSE_UNICODE
251 case wxDF_UNICODETEXT :
252 {
253 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
254 wxString str(textDataObject->GetText());
255 err = UMAPutScrap( str.Length() * sizeof(wxChar) , kScrapFlavorTypeUnicode , (void*) str.wc_str() ) ;
256 }
257 break ;
258#endif
f0822896 259#if wxUSE_DRAG_AND_DROP
e7549107 260 case wxDF_METAFILE:
f0822896 261 {
e40298d5 262 wxMetafileDataObject* metaFileDataObject =
f0822896 263 (wxMetafileDataObject*) data;
e40298d5
JS
264 wxMetafile metaFile = metaFileDataObject->GetMetafile();
265 PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ;
266 HLock( (Handle) pict ) ;
427ff662 267 err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ;
e40298d5 268 HUnlock( (Handle) pict ) ;
f0822896 269 }
d1171566 270 break ;
e7549107 271#endif
f0822896
SC
272 case wxDF_BITMAP:
273 case wxDF_DIB:
9c3c5849 274 {
e40298d5
JS
275 bool created = false ;
276 PicHandle pict = NULL ;
e135f093 277
e40298d5
JS
278 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data ;
279 pict = (PicHandle) bitmapDataObject->GetBitmap().GetPict( &created ) ;
280
281 HLock( (Handle) pict ) ;
427ff662 282 err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ;
e40298d5
JS
283 HUnlock( (Handle) pict ) ;
284 if ( created )
285 KillPicture( pict ) ;
9c3c5849 286 }
f0822896 287 default:
ed60b502 288 break ;
f0822896 289 }
e9576ca5 290
e9576ca5
SC
291 }
292
f0822896 293 delete[] array;
e9576ca5 294
f0822896 295 return true ;
e9576ca5
SC
296}
297
f0822896 298void wxClipboard::Close()
e9576ca5 299{
f0822896 300 m_open = false ;
e9576ca5
SC
301}
302
f0822896 303bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
e7549107 304{
f0822896
SC
305 if ( m_data )
306 {
307 return m_data->IsSupported( dataFormat ) ;
e9576ca5 308 }
f0822896 309#if TARGET_CARBON
ed60b502
RR
310 OSStatus err = noErr;
311 ScrapRef scrapRef;
e135f093 312
ed60b502 313 err = GetCurrentScrap( &scrapRef );
e135f093 314 if ( err != noTypeErr && err != memFullErr )
ed60b502
RR
315 {
316 ScrapFlavorFlags flavorFlags;
317 Size byteCount;
e135f093 318
ed60b502
RR
319 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
320 {
321 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
322 {
323 return TRUE ;
324 }
325 }
326 }
327 return FALSE;
e135f093 328
f0822896 329#else
ed60b502
RR
330 long offset ;
331 Handle datahandle = NewHandle(0) ;
332 HLock( datahandle ) ;
333 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
334 HUnlock( datahandle ) ;
335 bool hasData = GetHandleSize( datahandle ) > 0 ;
336 DisposeHandle( datahandle ) ;
337 return hasData ;
f0822896 338#endif
e9576ca5 339}
f0822896
SC
340
341bool wxClipboard::GetData( wxDataObject& data )
e9576ca5 342{
f0822896 343 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e9576ca5 344
3340066a 345 size_t formatcount = data.GetFormatCount() + 1 ;
f0822896
SC
346 wxDataFormat *array = new wxDataFormat[ formatcount ];
347 array[0] = data.GetPreferredFormat();
348 data.GetAllFormats( &array[1] );
e9576ca5 349
f0822896 350 bool transferred = false ;
e9576ca5 351
f0822896
SC
352 if ( m_data )
353 {
354 for (size_t i = 0; !transferred && i < formatcount ; i++)
355 {
356 wxDataFormat format = array[i] ;
e135f093 357 if ( m_data->IsSupported( format ) )
f0822896
SC
358 {
359 int size = m_data->GetDataSize( format );
360 transferred = true ;
e7549107 361
e135f093 362 if (size == 0)
f0822896
SC
363 {
364 data.SetData(format , 0 , 0 ) ;
365 }
366 else
367 {
368 char *d = new char[size];
369 m_data->GetDataHere( format , (void*) d );
370 data.SetData( format , size , d ) ;
371 delete[] d ;
372 }
373 }
374 }
375 }
376 /* get formats from wxDataObjects */
e135f093 377 if ( !transferred )
f0822896 378 {
f0822896
SC
379 for (size_t i = 0; !transferred && i < formatcount ; i++)
380 {
381 wxDataFormat format = array[i] ;
382
383 switch ( format.GetType() )
384 {
9c3c5849
SC
385 case wxDF_TEXT :
386 case wxDF_OEMTEXT :
387 case wxDF_BITMAP :
388 case wxDF_METAFILE :
f0822896
SC
389 {
390 long len ;
391 char* s = (char*)wxGetClipboardData(format, &len );
392 if ( s )
393 {
394 data.SetData( format , len , s ) ;
395 delete [] s;
396
397 transferred = true ;
398 }
399 }
e40298d5 400 break ;
d1171566 401
f0822896
SC
402 default :
403 break ;
404 }
405 }
406 }
e9576ca5 407
f0822896
SC
408 delete[] array ;
409 return transferred ;
410}