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