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