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