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