]> git.saurik.com Git - wxWidgets.git/blob - src/mac/clipbrd.cpp
wxCocoa: Added preliminary Blit support
[wxWidgets.git] / src / mac / 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 #ifdef __GNUG__
13 #pragma implementation "clipbrd.h"
14 #endif
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 ((wxChar*)data)[byteCount/2] = 0 ;
96 }
97 else
98 {
99 delete[] ((char *)data) ;
100 data = NULL ;
101 }
102 }
103 }
104 }
105
106 #else
107 long offset ;
108 Handle datahandle = NewHandle(0) ;
109 HLock( datahandle ) ;
110 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
111 HUnlock( datahandle ) ;
112 if ( GetHandleSize( datahandle ) > 0 )
113 {
114 byteCount = GetHandleSize( datahandle ) ;
115 Size allocSize = byteCount ;
116 if ( dataFormat.GetType() == wxDF_TEXT )
117 allocSize += 1 ;
118 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
119 allocSize += 2 ;
120
121 data = new char[ allocSize ] ;
122
123 memcpy( (char*) data , (char*) *datahandle , byteCount ) ;
124 if ( dataFormat.GetType() == wxDF_TEXT )
125 ((char*)data)[byteCount] = 0 ;
126 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
127 ((wxChar*)data)[byteCount/2] = 0 ;
128 *len = byteCount ;
129 }
130 DisposeHandle( datahandle ) ;
131 #endif
132 if ( err )
133 {
134 wxLogSysError(_("Failed to get clipboard data."));
135
136 return NULL ;
137 }
138
139 if ( dataFormat.GetType() == wxDF_TEXT && wxApp::s_macDefaultEncodingIsPC )
140 {
141 wxString st = wxMacMakeStringFromCString( (char*) data ) ;
142 #if wxUSE_UNICODE
143 wxCharBuffer buf = st.ToAscii() ;
144 #else
145 const char* buf = st ;
146 #endif
147 char* newdata = new char[strlen(buf)+1] ;
148 memcpy( newdata , buf , strlen(buf)+1 ) ;
149 delete[] ((char*) data ) ;
150 data = newdata ;
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 // 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 #if !TARGET_CARBON
244 OSErr err = noErr ;
245 #else
246 OSStatus err = noErr ;
247 #endif
248
249 switch ( array[i].GetType() )
250 {
251 case wxDF_TEXT:
252 case wxDF_OEMTEXT:
253 {
254 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
255 wxString str(textDataObject->GetText());
256 wxCharBuffer buf = wxMacStringToCString( str ) ;
257 err = UMAPutScrap( strlen(buf) , kScrapFlavorTypeText , (void*) buf.data() ) ;
258 }
259 break ;
260 #if wxUSE_UNICODE
261 case wxDF_UNICODETEXT :
262 {
263 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
264 wxString str(textDataObject->GetText());
265 err = UMAPutScrap( str.Length() * sizeof(wxChar) , kScrapFlavorTypeUnicode , (void*) str.wc_str() ) ;
266 }
267 break ;
268 #endif
269 #if wxUSE_DRAG_AND_DROP
270 case wxDF_METAFILE:
271 {
272 wxMetafileDataObject* metaFileDataObject =
273 (wxMetafileDataObject*) data;
274 wxMetafile metaFile = metaFileDataObject->GetMetafile();
275 PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ;
276 HLock( (Handle) pict ) ;
277 err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ;
278 HUnlock( (Handle) pict ) ;
279 }
280 break ;
281 #endif
282 case wxDF_BITMAP:
283 case wxDF_DIB:
284 {
285 bool created = false ;
286 PicHandle pict = NULL ;
287
288 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data ;
289 pict = (PicHandle) bitmapDataObject->GetBitmap().GetPict( &created ) ;
290
291 HLock( (Handle) pict ) ;
292 err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ;
293 HUnlock( (Handle) pict ) ;
294 if ( created )
295 KillPicture( pict ) ;
296 }
297 default:
298 break ;
299 }
300
301 }
302
303 delete[] array;
304
305 return true ;
306 }
307
308 void wxClipboard::Close()
309 {
310 m_open = false ;
311 }
312
313 bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
314 {
315 if ( m_data )
316 {
317 return m_data->IsSupported( dataFormat ) ;
318 }
319 #if TARGET_CARBON
320 OSStatus err = noErr;
321 ScrapRef scrapRef;
322
323 err = GetCurrentScrap( &scrapRef );
324 if ( err != noTypeErr && err != memFullErr )
325 {
326 ScrapFlavorFlags flavorFlags;
327 Size byteCount;
328
329 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
330 {
331 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
332 {
333 return TRUE ;
334 }
335 }
336 }
337 return FALSE;
338
339 #else
340 long offset ;
341 Handle datahandle = NewHandle(0) ;
342 HLock( datahandle ) ;
343 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
344 HUnlock( datahandle ) ;
345 bool hasData = GetHandleSize( datahandle ) > 0 ;
346 DisposeHandle( datahandle ) ;
347 return hasData ;
348 #endif
349 }
350
351 bool wxClipboard::GetData( wxDataObject& data )
352 {
353 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
354
355 size_t formatcount = data.GetFormatCount() + 1 ;
356 wxDataFormat *array = new wxDataFormat[ formatcount ];
357 array[0] = data.GetPreferredFormat();
358 data.GetAllFormats( &array[1] );
359
360 bool transferred = false ;
361
362 if ( m_data )
363 {
364 for (size_t i = 0; !transferred && i < formatcount ; i++)
365 {
366 wxDataFormat format = array[i] ;
367 if ( m_data->IsSupported( format ) )
368 {
369 int size = m_data->GetDataSize( format );
370 transferred = true ;
371
372 if (size == 0)
373 {
374 data.SetData(format , 0 , 0 ) ;
375 }
376 else
377 {
378 char *d = new char[size];
379 m_data->GetDataHere( format , (void*) d );
380 data.SetData( format , size , d ) ;
381 delete[] d ;
382 }
383 }
384 }
385 }
386 /* get formats from wxDataObjects */
387 if ( !transferred )
388 {
389 for (size_t i = 0; !transferred && i < formatcount ; i++)
390 {
391 wxDataFormat format = array[i] ;
392
393 switch ( format.GetType() )
394 {
395 case wxDF_TEXT :
396 case wxDF_OEMTEXT :
397 case wxDF_BITMAP :
398 case wxDF_METAFILE :
399 {
400 long len ;
401 char* s = (char*)wxGetClipboardData(format, &len );
402 if ( s )
403 {
404 data.SetData( format , len , s ) ;
405 delete [] s;
406
407 transferred = true ;
408 }
409 }
410 break ;
411
412 default :
413 break ;
414 }
415 }
416 }
417
418 delete[] array ;
419 return transferred ;
420 }