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