]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/clipbrd.cpp
fix wxExecute() return code checks and removed not working code to open URLs in new...
[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 #include "wx/wxprec.h"
13
14 #if wxUSE_CLIPBOARD
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 {
96 // "data" format is UTF16, so 2 bytes = one character
97 // wxChar size depends on platform, so just clear last 2 bytes
98 ((char*)data)[byteCount] = 0;
99 ((char*)data)[byteCount+1] = 0;
100 }
101 }
102 else
103 {
104 delete[] ((char *)data) ;
105 data = NULL ;
106 }
107 }
108 }
109 }
110
111 #else
112 long offset ;
113 Handle datahandle = NewHandle(0) ;
114 HLock( datahandle ) ;
115 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
116 HUnlock( datahandle ) ;
117 if ( GetHandleSize( datahandle ) > 0 )
118 {
119 byteCount = GetHandleSize( datahandle ) ;
120 Size allocSize = byteCount ;
121 if ( dataFormat.GetType() == wxDF_TEXT )
122 allocSize += 1 ;
123 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
124 allocSize += 2 ;
125
126 data = new char[ allocSize ] ;
127
128 memcpy( (char*) data , (char*) *datahandle , byteCount ) ;
129 if ( dataFormat.GetType() == wxDF_TEXT )
130 ((char*)data)[byteCount] = 0 ;
131 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
132 ((wxChar*)data)[byteCount/2] = 0 ;
133 *len = byteCount ;
134 }
135 DisposeHandle( datahandle ) ;
136 #endif
137 if ( err )
138 {
139 wxLogSysError(_("Failed to get clipboard data."));
140
141 return NULL ;
142 }
143
144 if ( dataFormat.GetType() == wxDF_TEXT )
145 {
146 wxMacConvertNewlines10To13( (char*) data ) ;
147 }
148
149 return data;
150 }
151
152
153 /*
154 * Generalized clipboard implementation by Matthew Flatt
155 */
156
157 IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
158
159 wxClipboard::wxClipboard()
160 {
161 m_open = false ;
162 m_data = NULL ;
163 }
164
165 wxClipboard::~wxClipboard()
166 {
167 if (m_data)
168 {
169 delete m_data;
170 m_data = (wxDataObject*) NULL;
171 }
172 }
173
174 void wxClipboard::Clear()
175 {
176 if (m_data)
177 {
178 delete m_data;
179 m_data = (wxDataObject*) NULL;
180 }
181 #if TARGET_CARBON
182 OSStatus err ;
183 err = ClearCurrentScrap( );
184 #else
185 OSErr err ;
186 err = ZeroScrap( );
187 #endif
188 if ( err )
189 {
190 wxLogSysError(_("Failed to empty the clipboard."));
191 }
192 }
193
194 bool wxClipboard::Flush()
195 {
196 return false;
197 }
198
199 bool wxClipboard::Open()
200 {
201 wxCHECK_MSG( !m_open, false, wxT("clipboard already open") );
202 m_open = true ;
203 return true ;
204 }
205
206 bool wxClipboard::IsOpened() const
207 {
208 return m_open;
209 }
210
211 bool wxClipboard::SetData( wxDataObject *data )
212 {
213 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
214
215 wxCHECK_MSG( data, false, wxT("data is invalid") );
216
217 Clear();
218 // as we can only store one wxDataObject, this is the same in this
219 // implementation
220 return AddData( data );
221 }
222
223 bool wxClipboard::AddData( wxDataObject *data )
224 {
225 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
226
227 wxCHECK_MSG( data, false, wxT("data is invalid") );
228
229 /* we can only store one wxDataObject */
230 Clear();
231
232 m_data = data;
233
234 /* get formats from wxDataObjects */
235 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
236 m_data->GetAllFormats( array );
237
238 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
239 {
240 wxLogTrace( TRACE_CLIPBOARD,
241 wxT("wxClipboard now supports atom %s"),
242 array[i].GetId().c_str() );
243
244 size_t sz = data->GetDataSize( array[i] ) ;
245 void* buf = malloc( sz + 1 ) ;
246 if ( buf )
247 {
248 // empty the buffer because in some case GetDataHere does not fill buf
249 memset(buf, 0, sz+1);
250 data->GetDataHere( array[i] , buf ) ;
251 OSType mactype = 0 ;
252 switch ( array[i].GetType() )
253 {
254 case wxDF_TEXT:
255 case wxDF_OEMTEXT:
256 mactype = kScrapFlavorTypeText ;
257 sz -= 1;
258 break ;
259 #if wxUSE_UNICODE
260 case wxDF_UNICODETEXT :
261 mactype = kScrapFlavorTypeUnicode ;
262 sz -= 2;
263 break ;
264 #endif
265 #if wxUSE_DRAG_AND_DROP
266 case wxDF_METAFILE:
267 mactype = kScrapFlavorTypePicture ;
268 break ;
269 #endif
270 case wxDF_BITMAP:
271 case wxDF_DIB:
272 mactype = kScrapFlavorTypePicture ;
273 break ;
274 default:
275 break ;
276 }
277 UMAPutScrap( sz , mactype , buf ) ;
278 free( buf ) ;
279 }
280 }
281
282 delete[] array;
283
284 return true ;
285 }
286
287 void wxClipboard::Close()
288 {
289 wxCHECK_RET( m_open, wxT("clipboard not open") );
290
291 m_open = false ;
292
293 // Get rid of cached object. If this is not done copying from another application will
294 // only work once
295 if (m_data)
296 {
297 delete m_data;
298 m_data = (wxDataObject*) NULL;
299 }
300
301 }
302
303 bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
304 {
305 if ( m_data )
306 {
307 return m_data->IsSupported( dataFormat ) ;
308 }
309 #if TARGET_CARBON
310 OSStatus err = noErr;
311 ScrapRef scrapRef;
312
313 err = GetCurrentScrap( &scrapRef );
314 if ( err != noTypeErr && err != memFullErr )
315 {
316 ScrapFlavorFlags flavorFlags;
317 Size byteCount;
318
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;
328
329 #else
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 ;
338 #endif
339 }
340
341 bool wxClipboard::GetData( wxDataObject& data )
342 {
343 wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
344
345 size_t formatcount = data.GetFormatCount() + 1 ;
346 wxDataFormat *array = new wxDataFormat[ formatcount ];
347 array[0] = data.GetPreferredFormat();
348 data.GetAllFormats( &array[1] );
349
350 bool transferred = false ;
351
352 if ( m_data )
353 {
354 for (size_t i = 0; !transferred && i < formatcount ; i++)
355 {
356 wxDataFormat format = array[i] ;
357 if ( m_data->IsSupported( format ) )
358 {
359 int size = m_data->GetDataSize( format );
360 transferred = true ;
361
362 if (size == 0)
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 */
377 if ( !transferred )
378 {
379 for (size_t i = 0; !transferred && i < formatcount ; i++)
380 {
381 wxDataFormat format = array[i] ;
382
383 switch ( format.GetType() )
384 {
385 case wxDF_TEXT :
386 case wxDF_UNICODETEXT:
387 case wxDF_OEMTEXT :
388 case wxDF_BITMAP :
389 case wxDF_METAFILE :
390 {
391 long len ;
392 char* s = (char*)wxGetClipboardData(format, &len );
393 if ( s )
394 {
395 data.SetData( format , len , s ) ;
396 delete [] s;
397
398 transferred = true ;
399 }
400 }
401 break ;
402
403 default :
404 break ;
405 }
406 }
407 }
408
409 delete[] array ;
410 return transferred ;
411 }
412
413 #endif