]> git.saurik.com Git - wxWidgets.git/blame - src/mac/clipbrd.cpp
added missing casts after removal of dependancy on mac headers
[wxWidgets.git] / src / mac / clipbrd.cpp
CommitLineData
e9576ca5
SC
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"
5fde6fcc 23#include "wx/intl.h"
e9576ca5 24
76a5e5d2
SC
25#include "wx/mac/private.h"
26
2f1ae414
SC
27#define wxUSE_DATAOBJ 1
28
e9576ca5
SC
29#include <string.h>
30
f0822896
SC
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!)
34static const wxChar *TRACE_CLIPBOARD = _T("clipboard");
2f1ae414 35
f0822896 36void *wxGetClipboardData(wxDataFormat dataFormat, long *len)
e9576ca5 37{
2f1ae414
SC
38#if !TARGET_CARBON
39 OSErr err = noErr ;
40#else
41 OSStatus err = noErr ;
42#endif
f0822896 43 void * data = NULL ;
2f1ae414
SC
44
45 switch (dataFormat.GetType())
46 {
2f1ae414
SC
47 case wxDF_OEMTEXT:
48 dataFormat = wxDF_TEXT;
49 // fall through
50
51 case wxDF_TEXT:
2f1ae414 52 break;
97af5088
SC
53 default:
54 {
55 wxLogError(_("Unsupported clipboard format."));
56 return NULL;
57 }
97af5088
SC
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 {
6d5e7307 85 delete[] ((char *)data) ;
97af5088
SC
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;
2f1ae414
SC
124}
125
126
e9576ca5
SC
127/*
128 * Generalized clipboard implementation by Matthew Flatt
129 */
130
2f1ae414 131IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxClipboardBase)
7c74e7fe 132
e7549107
SC
133wxClipboard::wxClipboard()
134{
f0822896
SC
135 m_open = false ;
136 m_data = NULL ;
e7549107 137}
e9576ca5 138
e7549107 139wxClipboard::~wxClipboard()
e9576ca5 140{
f0822896 141 if (m_data)
e7549107 142 {
f0822896
SC
143 delete m_data;
144 m_data = (wxDataObject*) NULL;
e7549107 145 }
e9576ca5
SC
146}
147
e7549107 148void wxClipboard::Clear()
e9576ca5 149{
f0822896
SC
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 }
e9576ca5
SC
166}
167
e7549107 168bool wxClipboard::Flush()
e9576ca5 169{
e7549107
SC
170 return FALSE;
171}
172
173bool wxClipboard::Open()
174{
f0822896
SC
175 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
176 m_open = true ;
177 return true ;
e7549107
SC
178}
179
180bool wxClipboard::IsOpened() const
181{
f0822896 182 return m_open;
e9576ca5
SC
183}
184
f0822896 185bool wxClipboard::SetData( wxDataObject *data )
e9576ca5 186{
f0822896 187 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e9576ca5 188
f0822896 189 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
e9576ca5 190
f0822896 191 Clear();
e7549107 192
f0822896 193 return AddData( data );
e7549107
SC
194}
195
196bool wxClipboard::AddData( wxDataObject *data )
197{
f0822896 198 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e7549107 199
f0822896 200 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
e7549107 201
2f1ae414 202 wxDataFormat format = data->GetPreferredFormat();
e7549107 203
f0822896
SC
204 /* we can only store one wxDataObject */
205 Clear();
e7549107 206
f0822896 207 m_data = data;
e7549107 208
f0822896
SC
209 /* get formats from wxDataObjects */
210 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
211 m_data->GetAllFormats( array );
e7549107 212
f0822896
SC
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() );
e7549107 218
f0822896
SC
219#if !TARGET_CARBON
220 OSErr err = noErr ;
221#else
222 OSStatus err = noErr ;
223#endif
e7549107 224
f0822896
SC
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
e7549107 254 case wxDF_METAFILE:
f0822896
SC
255 {
256 wxMetafileDataObject* metaFileDataObject =
257 (wxMetafileDataObject*) data;
258 wxMetafile metaFile = metaFileDataObject->GetMetafile();
76a5e5d2 259 PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ;
f0822896
SC
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 }
e7549107 273#endif
f0822896
SC
274 case wxDF_BITMAP:
275 case wxDF_DIB:
276 default:
277 break ;
278 }
e9576ca5 279
e9576ca5
SC
280 }
281
f0822896 282 delete[] array;
e9576ca5 283
f0822896 284 return true ;
e9576ca5
SC
285}
286
f0822896 287void wxClipboard::Close()
e9576ca5 288{
f0822896 289 m_open = false ;
e9576ca5
SC
290}
291
f0822896 292bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
e7549107 293{
f0822896
SC
294 if ( m_data )
295 {
296 return m_data->IsSupported( dataFormat ) ;
e9576ca5 297 }
f0822896
SC
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
e9576ca5 328}
f0822896
SC
329
330bool wxClipboard::GetData( wxDataObject& data )
e9576ca5 331{
f0822896 332 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e9576ca5 333
f0822896
SC
334 int formatcount = data.GetFormatCount() + 1 ;
335 wxDataFormat *array = new wxDataFormat[ formatcount ];
336 array[0] = data.GetPreferredFormat();
337 data.GetAllFormats( &array[1] );
e9576ca5 338
f0822896 339 bool transferred = false ;
e9576ca5 340
f0822896
SC
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 ;
e7549107 350
f0822896
SC
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 {
f0822896
SC
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 }
e9576ca5 392
f0822896
SC
393 delete[] array ;
394 return transferred ;
395}