]> git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/clipbrd.cpp
Removed wxHTMLHelpControllerBase (putting the
[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 #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 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
218
219 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
220
221 Clear();
222
223 return AddData( data );
224 }
225
226 bool wxClipboard::AddData( wxDataObject *data )
227 {
228 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
229
230 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
231
232 /* we can only store one wxDataObject */
233 Clear();
234
235 m_data = data;
236
237 /* get formats from wxDataObjects */
238 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
239 m_data->GetAllFormats( array );
240
241 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
242 {
243 wxLogTrace( TRACE_CLIPBOARD,
244 wxT("wxClipboard now supports atom %s"),
245 array[i].GetId().c_str() );
246
247 #if !TARGET_CARBON
248 OSErr err = noErr ;
249 #else
250 OSStatus err = noErr ;
251 #endif
252
253 switch ( array[i].GetType() )
254 {
255 case wxDF_TEXT:
256 case wxDF_OEMTEXT:
257 {
258 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
259 wxString str(textDataObject->GetText());
260 wxCharBuffer buf = wxMacStringToCString( str ) ;
261 err = UMAPutScrap( strlen(buf) , kScrapFlavorTypeText , (void*) buf.data() ) ;
262 }
263 break ;
264 #if wxUSE_UNICODE
265 case wxDF_UNICODETEXT :
266 {
267 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
268 wxString str(textDataObject->GetText());
269 err = UMAPutScrap( str.Length() * sizeof(wxChar) , kScrapFlavorTypeUnicode , (void*) str.wc_str() ) ;
270 }
271 break ;
272 #endif
273 #if wxUSE_DRAG_AND_DROP
274 case wxDF_METAFILE:
275 {
276 wxMetafileDataObject* metaFileDataObject =
277 (wxMetafileDataObject*) data;
278 wxMetafile metaFile = metaFileDataObject->GetMetafile();
279 PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ;
280 HLock( (Handle) pict ) ;
281 err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ;
282 HUnlock( (Handle) pict ) ;
283 }
284 break ;
285 #endif
286 case wxDF_BITMAP:
287 case wxDF_DIB:
288 {
289 bool created = false ;
290 PicHandle pict = NULL ;
291
292 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data ;
293 pict = (PicHandle) bitmapDataObject->GetBitmap().GetPict( &created ) ;
294
295 HLock( (Handle) pict ) ;
296 err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ;
297 HUnlock( (Handle) pict ) ;
298 if ( created )
299 KillPicture( pict ) ;
300 }
301 default:
302 break ;
303 }
304
305 }
306
307 delete[] array;
308
309 return true ;
310 }
311
312 void wxClipboard::Close()
313 {
314 m_open = false ;
315 }
316
317 bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
318 {
319 if ( m_data )
320 {
321 return m_data->IsSupported( dataFormat ) ;
322 }
323 #if TARGET_CARBON
324 OSStatus err = noErr;
325 ScrapRef scrapRef;
326
327 err = GetCurrentScrap( &scrapRef );
328 if ( err != noTypeErr && err != memFullErr )
329 {
330 ScrapFlavorFlags flavorFlags;
331 Size byteCount;
332
333 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
334 {
335 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
336 {
337 return TRUE ;
338 }
339 }
340 }
341 return FALSE;
342
343 #else
344 long offset ;
345 Handle datahandle = NewHandle(0) ;
346 HLock( datahandle ) ;
347 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
348 HUnlock( datahandle ) ;
349 bool hasData = GetHandleSize( datahandle ) > 0 ;
350 DisposeHandle( datahandle ) ;
351 return hasData ;
352 #endif
353 }
354
355 bool wxClipboard::GetData( wxDataObject& data )
356 {
357 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
358
359 size_t formatcount = data.GetFormatCount() + 1 ;
360 wxDataFormat *array = new wxDataFormat[ formatcount ];
361 array[0] = data.GetPreferredFormat();
362 data.GetAllFormats( &array[1] );
363
364 bool transferred = false ;
365
366 if ( m_data )
367 {
368 for (size_t i = 0; !transferred && i < formatcount ; i++)
369 {
370 wxDataFormat format = array[i] ;
371 if ( m_data->IsSupported( format ) )
372 {
373 int size = m_data->GetDataSize( format );
374 transferred = true ;
375
376 if (size == 0)
377 {
378 data.SetData(format , 0 , 0 ) ;
379 }
380 else
381 {
382 char *d = new char[size];
383 m_data->GetDataHere( format , (void*) d );
384 data.SetData( format , size , d ) ;
385 delete[] d ;
386 }
387 }
388 }
389 }
390 /* get formats from wxDataObjects */
391 if ( !transferred )
392 {
393 for (size_t i = 0; !transferred && i < formatcount ; i++)
394 {
395 wxDataFormat format = array[i] ;
396
397 switch ( format.GetType() )
398 {
399 case wxDF_TEXT :
400 case wxDF_OEMTEXT :
401 case wxDF_BITMAP :
402 case wxDF_METAFILE :
403 {
404 long len ;
405 char* s = (char*)wxGetClipboardData(format, &len );
406 if ( s )
407 {
408 data.SetData( format , len , s ) ;
409 delete [] s;
410
411 transferred = true ;
412 }
413 }
414 break ;
415
416 default :
417 break ;
418 }
419 }
420 }
421
422 delete[] array ;
423 return transferred ;
424 }