]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/clipbrd.cpp
Fix for menu items not being disabled after a modal dialog is shown
[wxWidgets.git] / src / mac / carbon / 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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "clipbrd.h"
14 #endif
15
16 #include "wx/wxprec.h"
17
18 #include "wx/app.h"
19 #include "wx/frame.h"
20 #include "wx/bitmap.h"
21 #include "wx/utils.h"
22 #include "wx/metafile.h"
23 #include "wx/clipbrd.h"
24 #include "wx/intl.h"
25 #include "wx/log.h"
26
27 #ifndef __DARWIN__
28 #include <Scrap.h>
29 #endif
30 #include "wx/mac/uma.h"
31
32 #define wxUSE_DATAOBJ 1
33
34 #include <string.h>
35
36 // the trace mask we use with wxLogTrace() - call
37 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
38 // (there will be a *lot* of them!)
39 static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
40
41 void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
42 {
43 #if !TARGET_CARBON
44 OSErr err = noErr ;
45 #else
46 OSStatus err = noErr ;
47 #endif
48 void * data = NULL ;
49 Size byteCount;
50
51 switch (dataFormat.GetType())
52 {
53 case wxDF_OEMTEXT:
54 dataFormat = wxDF_TEXT;
55 // fall through
56
57 case wxDF_TEXT:
58 break;
59 case wxDF_UNICODETEXT:
60 break;
61 case wxDF_BITMAP :
62 case wxDF_METAFILE :
63 break ;
64 default:
65 {
66 wxLogError(_("Unsupported clipboard format."));
67 return NULL;
68 }
69 }
70
71 #if TARGET_CARBON
72 ScrapRef scrapRef;
73
74 err = GetCurrentScrap( &scrapRef );
75 if ( err != noTypeErr && err != memFullErr )
76 {
77 ScrapFlavorFlags flavorFlags;
78
79 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
80 {
81 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
82 {
83 Size allocSize = byteCount ;
84 if ( dataFormat.GetType() == wxDF_TEXT )
85 allocSize += 1 ;
86 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
87 allocSize += 2 ;
88
89 data = new char[ allocSize ] ;
90
91 if (( err = GetScrapFlavorData( scrapRef, dataFormat.GetFormatId(), &byteCount , data )) == noErr )
92 {
93 *len = allocSize ;
94 if ( dataFormat.GetType() == wxDF_TEXT )
95 ((char*)data)[byteCount] = 0 ;
96 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
97 ((wxChar*)data)[byteCount/2] = 0 ;
98 }
99 else
100 {
101 delete[] ((char *)data) ;
102 data = NULL ;
103 }
104 }
105 }
106 }
107
108 #else
109 long offset ;
110 Handle datahandle = NewHandle(0) ;
111 HLock( datahandle ) ;
112 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
113 HUnlock( datahandle ) ;
114 if ( GetHandleSize( datahandle ) > 0 )
115 {
116 byteCount = GetHandleSize( datahandle ) ;
117 Size allocSize = byteCount ;
118 if ( dataFormat.GetType() == wxDF_TEXT )
119 allocSize += 1 ;
120 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
121 allocSize += 2 ;
122
123 data = new char[ allocSize ] ;
124
125 memcpy( (char*) data , (char*) *datahandle , byteCount ) ;
126 if ( dataFormat.GetType() == wxDF_TEXT )
127 ((char*)data)[byteCount] = 0 ;
128 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
129 ((wxChar*)data)[byteCount/2] = 0 ;
130 *len = byteCount ;
131 }
132 DisposeHandle( datahandle ) ;
133 #endif
134 if ( err )
135 {
136 wxLogSysError(_("Failed to get clipboard data."));
137
138 return NULL ;
139 }
140
141 if ( dataFormat.GetType() == wxDF_TEXT )
142 {
143 wxMacConvertNewlines10To13( (char*) data ) ;
144 }
145
146 return data;
147 }
148
149
150 /*
151 * Generalized clipboard implementation by Matthew Flatt
152 */
153
154 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
155
156 wxClipboard::wxClipboard()
157 {
158 m_open = false ;
159 m_data = NULL ;
160 }
161
162 wxClipboard::~wxClipboard()
163 {
164 if (m_data)
165 {
166 delete m_data;
167 m_data = (wxDataObject*) NULL;
168 }
169 }
170
171 void wxClipboard::Clear()
172 {
173 if (m_data)
174 {
175 delete m_data;
176 m_data = (wxDataObject*) NULL;
177 }
178 #if TARGET_CARBON
179 OSStatus err ;
180 err = ClearCurrentScrap( );
181 #else
182 OSErr err ;
183 err = ZeroScrap( );
184 #endif
185 if ( err )
186 {
187 wxLogSysError(_("Failed to empty the clipboard."));
188 }
189 }
190
191 bool wxClipboard::Flush()
192 {
193 return FALSE;
194 }
195
196 bool wxClipboard::Open()
197 {
198 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
199 m_open = true ;
200 return true ;
201 }
202
203 bool wxClipboard::IsOpened() const
204 {
205 return m_open;
206 }
207
208 bool wxClipboard::SetData( wxDataObject *data )
209 {
210 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
211
212 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
213
214 Clear();
215 // as we can only store one wxDataObject, this is the same in this
216 // implementation
217 return AddData( data );
218 }
219
220 bool wxClipboard::AddData( wxDataObject *data )
221 {
222 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
223
224 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
225
226 /* we can only store one wxDataObject */
227 Clear();
228
229 m_data = data;
230
231 /* get formats from wxDataObjects */
232 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
233 m_data->GetAllFormats( array );
234
235 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
236 {
237 wxLogTrace( TRACE_CLIPBOARD,
238 wxT("wxClipboard now supports atom %s"),
239 array[i].GetId().c_str() );
240
241 size_t sz = data->GetDataSize( array[i] ) ;
242 void* buf = malloc( sz + 1 ) ;
243 if ( buf )
244 {
245 data->GetDataHere( array[i] , buf ) ;
246 OSType mactype = 0 ;
247 switch ( array[i].GetType() )
248 {
249 case wxDF_TEXT:
250 case wxDF_OEMTEXT:
251 mactype = kScrapFlavorTypeText ;
252 break ;
253 #if wxUSE_UNICODE
254 case wxDF_UNICODETEXT :
255 mactype = kScrapFlavorTypeUnicode ;
256 break ;
257 #endif
258 #if wxUSE_DRAG_AND_DROP
259 case wxDF_METAFILE:
260 mactype = kScrapFlavorTypePicture ;
261 break ;
262 #endif
263 case wxDF_BITMAP:
264 case wxDF_DIB:
265 mactype = kScrapFlavorTypePicture ;
266 break ;
267 default:
268 break ;
269 }
270 UMAPutScrap( sz , mactype , buf ) ;
271 free( buf ) ;
272 }
273 }
274
275 delete[] array;
276
277 return true ;
278 }
279
280 void wxClipboard::Close()
281 {
282 wxCHECK_RET( m_open, wxT("clipboard not open") );
283
284 m_open = false ;
285
286 // Get rid of cached object. If this is not done copying from another application will
287 // only work once
288 if (m_data)
289 {
290 delete m_data;
291 m_data = (wxDataObject*) NULL;
292 }
293
294 }
295
296 bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
297 {
298 if ( m_data )
299 {
300 return m_data->IsSupported( dataFormat ) ;
301 }
302 #if TARGET_CARBON
303 OSStatus err = noErr;
304 ScrapRef scrapRef;
305
306 err = GetCurrentScrap( &scrapRef );
307 if ( err != noTypeErr && err != memFullErr )
308 {
309 ScrapFlavorFlags flavorFlags;
310 Size byteCount;
311
312 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
313 {
314 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
315 {
316 return TRUE ;
317 }
318 }
319 }
320 return FALSE;
321
322 #else
323 long offset ;
324 Handle datahandle = NewHandle(0) ;
325 HLock( datahandle ) ;
326 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
327 HUnlock( datahandle ) ;
328 bool hasData = GetHandleSize( datahandle ) > 0 ;
329 DisposeHandle( datahandle ) ;
330 return hasData ;
331 #endif
332 }
333
334 bool wxClipboard::GetData( wxDataObject& data )
335 {
336 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
337
338 size_t formatcount = data.GetFormatCount() + 1 ;
339 wxDataFormat *array = new wxDataFormat[ formatcount ];
340 array[0] = data.GetPreferredFormat();
341 data.GetAllFormats( &array[1] );
342
343 bool transferred = false ;
344
345 if ( m_data )
346 {
347 for (size_t i = 0; !transferred && i < formatcount ; i++)
348 {
349 wxDataFormat format = array[i] ;
350 if ( m_data->IsSupported( format ) )
351 {
352 int size = m_data->GetDataSize( format );
353 transferred = true ;
354
355 if (size == 0)
356 {
357 data.SetData(format , 0 , 0 ) ;
358 }
359 else
360 {
361 char *d = new char[size];
362 m_data->GetDataHere( format , (void*) d );
363 data.SetData( format , size , d ) ;
364 delete[] d ;
365 }
366 }
367 }
368 }
369 /* get formats from wxDataObjects */
370 if ( !transferred )
371 {
372 for (size_t i = 0; !transferred && i < formatcount ; i++)
373 {
374 wxDataFormat format = array[i] ;
375
376 switch ( format.GetType() )
377 {
378 case wxDF_TEXT :
379 case wxDF_OEMTEXT :
380 case wxDF_BITMAP :
381 case wxDF_METAFILE :
382 {
383 long len ;
384 char* s = (char*)wxGetClipboardData(format, &len );
385 if ( s )
386 {
387 data.SetData( format , len , s ) ;
388 delete [] s;
389
390 transferred = true ;
391 }
392 }
393 break ;
394
395 default :
396 break ;
397 }
398 }
399 }
400
401 delete[] array ;
402 return transferred ;
403 }