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