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