]> git.saurik.com Git - wxWidgets.git/blob - src/mac/clipbrd.cpp
explicitly return NFR_UNICODE from WM_NOTIFYFORMAT when using MSLU
[wxWidgets.git] / src / mac / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: Stefan Csomor
5 // Modified by:
6 // Created: 1998-01-01
7 // RCS-ID: $Id$
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
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"
22 #include "wx/intl.h"
23 #include "wx/log.h"
24
25 #ifndef __DARWIN__
26 #include <Scrap.h>
27 #endif
28 #include "wx/mac/uma.h"
29
30 #define wxUSE_DATAOBJ 1
31
32 #include <string.h>
33
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!)
37 static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
38
39 void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
40 {
41 #if !TARGET_CARBON
42 OSErr err = noErr ;
43 #else
44 OSStatus err = noErr ;
45 #endif
46 void * data = NULL ;
47 Size byteCount;
48
49 switch (dataFormat.GetType())
50 {
51 case wxDF_OEMTEXT:
52 dataFormat = wxDF_TEXT;
53 // fall through
54
55 case wxDF_TEXT:
56 break;
57 case wxDF_UNICODETEXT:
58 break;
59 case wxDF_BITMAP :
60 case wxDF_METAFILE :
61 break ;
62 default:
63 {
64 wxLogError(_("Unsupported clipboard format."));
65 return NULL;
66 }
67 }
68
69 #if TARGET_CARBON
70 ScrapRef scrapRef;
71
72 err = GetCurrentScrap( &scrapRef );
73 if ( err != noTypeErr && err != memFullErr )
74 {
75 ScrapFlavorFlags flavorFlags;
76
77 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
78 {
79 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
80 {
81 Size allocSize = byteCount ;
82 if ( dataFormat.GetType() == wxDF_TEXT )
83 allocSize += 1 ;
84 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
85 allocSize += 2 ;
86
87 data = new char[ allocSize ] ;
88
89 if (( err = GetScrapFlavorData( scrapRef, dataFormat.GetFormatId(), &byteCount , data )) == noErr )
90 {
91 *len = allocSize ;
92 if ( dataFormat.GetType() == wxDF_TEXT )
93 ((char*)data)[byteCount] = 0 ;
94 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
95 ((wxChar*)data)[byteCount/2] = 0 ;
96 }
97 else
98 {
99 delete[] ((char *)data) ;
100 data = NULL ;
101 }
102 }
103 }
104 }
105
106 #else
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 {
114 byteCount = GetHandleSize( datahandle ) ;
115 Size allocSize = byteCount ;
116 if ( dataFormat.GetType() == wxDF_TEXT )
117 allocSize += 1 ;
118 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
119 allocSize += 2 ;
120
121 data = new char[ allocSize ] ;
122
123 memcpy( (char*) data , (char*) *datahandle , byteCount ) ;
124 if ( dataFormat.GetType() == wxDF_TEXT )
125 ((char*)data)[byteCount] = 0 ;
126 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
127 ((wxChar*)data)[byteCount/2] = 0 ;
128 *len = byteCount ;
129 }
130 DisposeHandle( datahandle ) ;
131 #endif
132 if ( err )
133 {
134 wxLogSysError(_("Failed to get clipboard data."));
135
136 return NULL ;
137 }
138
139 if ( dataFormat.GetType() == wxDF_TEXT )
140 {
141 wxMacConvertNewlines10To13( (char*) data ) ;
142 }
143
144 return data;
145 }
146
147
148 /*
149 * Generalized clipboard implementation by Matthew Flatt
150 */
151
152 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
153
154 wxClipboard::wxClipboard()
155 {
156 m_open = false ;
157 m_data = NULL ;
158 }
159
160 wxClipboard::~wxClipboard()
161 {
162 if (m_data)
163 {
164 delete m_data;
165 m_data = (wxDataObject*) NULL;
166 }
167 }
168
169 void wxClipboard::Clear()
170 {
171 if (m_data)
172 {
173 delete m_data;
174 m_data = (wxDataObject*) NULL;
175 }
176 #if TARGET_CARBON
177 OSStatus err ;
178 err = ClearCurrentScrap( );
179 #else
180 OSErr err ;
181 err = ZeroScrap( );
182 #endif
183 if ( err )
184 {
185 wxLogSysError(_("Failed to empty the clipboard."));
186 }
187 }
188
189 bool wxClipboard::Flush()
190 {
191 return FALSE;
192 }
193
194 bool wxClipboard::Open()
195 {
196 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
197 m_open = true ;
198 return true ;
199 }
200
201 bool wxClipboard::IsOpened() const
202 {
203 return m_open;
204 }
205
206 bool wxClipboard::SetData( wxDataObject *data )
207 {
208 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
209
210 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
211
212 Clear();
213 // as we can only store one wxDataObject, this is the same in this
214 // implementation
215 return AddData( data );
216 }
217
218 bool wxClipboard::AddData( wxDataObject *data )
219 {
220 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
221
222 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
223
224 /* we can only store one wxDataObject */
225 Clear();
226
227 m_data = data;
228
229 /* get formats from wxDataObjects */
230 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
231 m_data->GetAllFormats( array );
232
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() );
238
239 #if !TARGET_CARBON
240 OSErr err = noErr ;
241 #else
242 OSStatus err = noErr ;
243 #endif
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 }
276 }
277
278 delete[] array;
279
280 return true ;
281 }
282
283 void wxClipboard::Close()
284 {
285 wxCHECK_RET( m_open, wxT("clipboard not open") );
286
287 m_open = false ;
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
297 }
298
299 bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
300 {
301 if ( m_data )
302 {
303 return m_data->IsSupported( dataFormat ) ;
304 }
305 #if TARGET_CARBON
306 OSStatus err = noErr;
307 ScrapRef scrapRef;
308
309 err = GetCurrentScrap( &scrapRef );
310 if ( err != noTypeErr && err != memFullErr )
311 {
312 ScrapFlavorFlags flavorFlags;
313 Size byteCount;
314
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;
324
325 #else
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 ;
334 #endif
335 }
336
337 bool wxClipboard::GetData( wxDataObject& data )
338 {
339 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
340
341 size_t formatcount = data.GetFormatCount() + 1 ;
342 wxDataFormat *array = new wxDataFormat[ formatcount ];
343 array[0] = data.GetPreferredFormat();
344 data.GetAllFormats( &array[1] );
345
346 bool transferred = false ;
347
348 if ( m_data )
349 {
350 for (size_t i = 0; !transferred && i < formatcount ; i++)
351 {
352 wxDataFormat format = array[i] ;
353 if ( m_data->IsSupported( format ) )
354 {
355 int size = m_data->GetDataSize( format );
356 transferred = true ;
357
358 if (size == 0)
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 */
373 if ( !transferred )
374 {
375 for (size_t i = 0; !transferred && i < formatcount ; i++)
376 {
377 wxDataFormat format = array[i] ;
378
379 switch ( format.GetType() )
380 {
381 case wxDF_TEXT :
382 case wxDF_OEMTEXT :
383 case wxDF_BITMAP :
384 case wxDF_METAFILE :
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 }
396 break ;
397
398 default :
399 break ;
400 }
401 }
402 }
403
404 delete[] array ;
405 return transferred ;
406 }