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