]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/clipbrd.cpp
TransferFrom/ToWindow() were reverted, fixed
[wxWidgets.git] / src / mac / carbon / clipbrd.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: clipbrd.cpp
3// Purpose: Clipboard functionality
a31a5f85 4// Author: Stefan Csomor
e9576ca5 5// Modified by:
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
ed60b502 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
e9576ca5
SC
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"
5fde6fcc 22#include "wx/intl.h"
48d2ab90 23#include "wx/log.h"
e9576ca5 24
66a09d47
SC
25#ifndef __DARWIN__
26#include <Scrap.h>
27#endif
9c3c5849 28#include "wx/mac/uma.h"
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
e40298d5
JS
46 void * data = NULL ;
47 Size byteCount;
e135f093 48
2f1ae414
SC
49 switch (dataFormat.GetType())
50 {
e40298d5
JS
51 case wxDF_OEMTEXT:
52 dataFormat = wxDF_TEXT;
53 // fall through
e135f093 54
e40298d5
JS
55 case wxDF_TEXT:
56 break;
427ff662
SC
57 case wxDF_UNICODETEXT:
58 break;
e40298d5
JS
59 case wxDF_BITMAP :
60 case wxDF_METAFILE :
61 break ;
62 default:
63 {
64 wxLogError(_("Unsupported clipboard format."));
65 return NULL;
66 }
97af5088 67 }
e135f093 68
97af5088 69#if TARGET_CARBON
ed60b502 70 ScrapRef scrapRef;
e135f093 71
ed60b502 72 err = GetCurrentScrap( &scrapRef );
e135f093 73 if ( err != noTypeErr && err != memFullErr )
ed60b502
RR
74 {
75 ScrapFlavorFlags flavorFlags;
e135f093 76
ed60b502
RR
77 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
78 {
79 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
80 {
427ff662 81 Size allocSize = byteCount ;
e135f093 82 if ( dataFormat.GetType() == wxDF_TEXT )
427ff662 83 allocSize += 1 ;
e135f093 84 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 85 allocSize += 2 ;
e135f093 86
427ff662 87 data = new char[ allocSize ] ;
e135f093 88
e40298d5
JS
89 if (( err = GetScrapFlavorData( scrapRef, dataFormat.GetFormatId(), &byteCount , data )) == noErr )
90 {
427ff662 91 *len = allocSize ;
e135f093 92 if ( dataFormat.GetType() == wxDF_TEXT )
e40298d5 93 ((char*)data)[byteCount] = 0 ;
e135f093 94 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 95 ((wxChar*)data)[byteCount/2] = 0 ;
e40298d5
JS
96 }
97 else
98 {
99 delete[] ((char *)data) ;
100 data = NULL ;
101 }
ed60b502
RR
102 }
103 }
104 }
e135f093 105
97af5088 106#else
ed60b502
RR
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 {
e40298d5 114 byteCount = GetHandleSize( datahandle ) ;
427ff662 115 Size allocSize = byteCount ;
e135f093 116 if ( dataFormat.GetType() == wxDF_TEXT )
427ff662 117 allocSize += 1 ;
e135f093 118 else if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662
SC
119 allocSize += 2 ;
120
121 data = new char[ allocSize ] ;
122
e40298d5 123 memcpy( (char*) data , (char*) *datahandle , byteCount ) ;
e135f093 124 if ( dataFormat.GetType() == wxDF_TEXT )
e40298d5 125 ((char*)data)[byteCount] = 0 ;
e135f093 126 if ( dataFormat.GetType() == wxDF_UNICODETEXT )
427ff662 127 ((wxChar*)data)[byteCount/2] = 0 ;
e40298d5 128 *len = byteCount ;
ed60b502
RR
129 }
130 DisposeHandle( datahandle ) ;
97af5088
SC
131#endif
132 if ( err )
133 {
134 wxLogSysError(_("Failed to get clipboard data."));
e135f093 135
97af5088
SC
136 return NULL ;
137 }
427ff662 138
939fba6c 139 if ( dataFormat.GetType() == wxDF_TEXT )
97af5088 140 {
939fba6c
SC
141 char * buf = (char*) data ;
142 while( (buf=strchr(buf,0x0a)) != NULL )
143 {
144 *buf = 13 ;
145 buf++ ;
146 }
97af5088 147 }
427ff662 148
97af5088 149 return data;
2f1ae414
SC
150}
151
152
e9576ca5
SC
153/*
154 * Generalized clipboard implementation by Matthew Flatt
155 */
156
528e2293 157IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject)
7c74e7fe 158
e7549107
SC
159wxClipboard::wxClipboard()
160{
e40298d5
JS
161 m_open = false ;
162 m_data = NULL ;
e7549107 163}
e9576ca5 164
e7549107 165wxClipboard::~wxClipboard()
e9576ca5 166{
f0822896 167 if (m_data)
e7549107 168 {
f0822896
SC
169 delete m_data;
170 m_data = (wxDataObject*) NULL;
e7549107 171 }
e9576ca5
SC
172}
173
e7549107 174void wxClipboard::Clear()
e9576ca5 175{
f0822896
SC
176 if (m_data)
177 {
178 delete m_data;
179 m_data = (wxDataObject*) NULL;
180 }
181#if TARGET_CARBON
ed60b502
RR
182 OSStatus err ;
183 err = ClearCurrentScrap( );
f0822896 184#else
ed60b502
RR
185 OSErr err ;
186 err = ZeroScrap( );
f0822896 187#endif
ed60b502
RR
188 if ( err )
189 {
f0822896 190 wxLogSysError(_("Failed to empty the clipboard."));
ed60b502 191 }
e9576ca5
SC
192}
193
e7549107 194bool wxClipboard::Flush()
e9576ca5 195{
e7549107
SC
196 return FALSE;
197}
198
199bool wxClipboard::Open()
200{
f0822896
SC
201 wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") );
202 m_open = true ;
203 return true ;
e7549107
SC
204}
205
206bool wxClipboard::IsOpened() const
207{
f0822896 208 return m_open;
e9576ca5
SC
209}
210
f0822896 211bool wxClipboard::SetData( wxDataObject *data )
e9576ca5 212{
e135f093
VZ
213 // as we can only store one wxDataObject, this is the same in this
214 // implementation
f0822896 215 return AddData( data );
e7549107
SC
216}
217
218bool wxClipboard::AddData( wxDataObject *data )
219{
f0822896 220 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e7549107 221
f0822896 222 wxCHECK_MSG( data, FALSE, wxT("data is invalid") );
e7549107 223
f0822896
SC
224 /* we can only store one wxDataObject */
225 Clear();
e7549107 226
f0822896 227 m_data = data;
e7549107 228
f0822896
SC
229 /* get formats from wxDataObjects */
230 wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ];
231 m_data->GetAllFormats( array );
e7549107 232
f0822896
SC
233 for (size_t i = 0; i < m_data->GetFormatCount(); i++)
234 {
235 wxLogTrace( TRACE_CLIPBOARD,
236 wxT("wxClipboard now supports atom %s"),
237 array[i].GetId().c_str() );
e7549107 238
f0822896 239#if !TARGET_CARBON
0cd51b2d 240 OSErr err = noErr ;
f0822896 241#else
0cd51b2d 242 OSStatus err = noErr ;
f0822896 243#endif
e7549107 244
f0822896
SC
245 switch ( array[i].GetType() )
246 {
247 case wxDF_TEXT:
248 case wxDF_OEMTEXT:
249 {
250 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
939fba6c 251 wxCharBuffer buf = textDataObject->GetText().mb_str() ;
427ff662 252 err = UMAPutScrap( strlen(buf) , kScrapFlavorTypeText , (void*) buf.data() ) ;
f0822896 253 }
d1171566 254 break ;
427ff662
SC
255#if wxUSE_UNICODE
256 case wxDF_UNICODETEXT :
257 {
258 wxTextDataObject* textDataObject = (wxTextDataObject*) data;
259 wxString str(textDataObject->GetText());
260 err = UMAPutScrap( str.Length() * sizeof(wxChar) , kScrapFlavorTypeUnicode , (void*) str.wc_str() ) ;
261 }
262 break ;
263#endif
f0822896 264#if wxUSE_DRAG_AND_DROP
e7549107 265 case wxDF_METAFILE:
f0822896 266 {
e40298d5 267 wxMetafileDataObject* metaFileDataObject =
f0822896 268 (wxMetafileDataObject*) data;
e40298d5
JS
269 wxMetafile metaFile = metaFileDataObject->GetMetafile();
270 PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ;
271 HLock( (Handle) pict ) ;
427ff662 272 err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ;
e40298d5 273 HUnlock( (Handle) pict ) ;
f0822896 274 }
d1171566 275 break ;
e7549107 276#endif
f0822896
SC
277 case wxDF_BITMAP:
278 case wxDF_DIB:
9c3c5849 279 {
e40298d5
JS
280 bool created = false ;
281 PicHandle pict = NULL ;
e135f093 282
e40298d5
JS
283 wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data ;
284 pict = (PicHandle) bitmapDataObject->GetBitmap().GetPict( &created ) ;
285
286 HLock( (Handle) pict ) ;
427ff662 287 err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ;
e40298d5
JS
288 HUnlock( (Handle) pict ) ;
289 if ( created )
290 KillPicture( pict ) ;
9c3c5849 291 }
f0822896 292 default:
ed60b502 293 break ;
f0822896 294 }
e9576ca5 295
e9576ca5
SC
296 }
297
f0822896 298 delete[] array;
e9576ca5 299
f0822896 300 return true ;
e9576ca5
SC
301}
302
f0822896 303void wxClipboard::Close()
e9576ca5 304{
f0822896 305 m_open = false ;
e9576ca5
SC
306}
307
f0822896 308bool wxClipboard::IsSupported( const wxDataFormat &dataFormat )
e7549107 309{
f0822896
SC
310 if ( m_data )
311 {
312 return m_data->IsSupported( dataFormat ) ;
e9576ca5 313 }
f0822896 314#if TARGET_CARBON
ed60b502
RR
315 OSStatus err = noErr;
316 ScrapRef scrapRef;
e135f093 317
ed60b502 318 err = GetCurrentScrap( &scrapRef );
e135f093 319 if ( err != noTypeErr && err != memFullErr )
ed60b502
RR
320 {
321 ScrapFlavorFlags flavorFlags;
322 Size byteCount;
e135f093 323
ed60b502
RR
324 if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr)
325 {
326 if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr)
327 {
328 return TRUE ;
329 }
330 }
331 }
332 return FALSE;
e135f093 333
f0822896 334#else
ed60b502
RR
335 long offset ;
336 Handle datahandle = NewHandle(0) ;
337 HLock( datahandle ) ;
338 GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ;
339 HUnlock( datahandle ) ;
340 bool hasData = GetHandleSize( datahandle ) > 0 ;
341 DisposeHandle( datahandle ) ;
342 return hasData ;
f0822896 343#endif
e9576ca5 344}
f0822896
SC
345
346bool wxClipboard::GetData( wxDataObject& data )
e9576ca5 347{
f0822896 348 wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") );
e9576ca5 349
3340066a 350 size_t formatcount = data.GetFormatCount() + 1 ;
f0822896
SC
351 wxDataFormat *array = new wxDataFormat[ formatcount ];
352 array[0] = data.GetPreferredFormat();
353 data.GetAllFormats( &array[1] );
e9576ca5 354
f0822896 355 bool transferred = false ;
e9576ca5 356
f0822896
SC
357 if ( m_data )
358 {
359 for (size_t i = 0; !transferred && i < formatcount ; i++)
360 {
361 wxDataFormat format = array[i] ;
e135f093 362 if ( m_data->IsSupported( format ) )
f0822896
SC
363 {
364 int size = m_data->GetDataSize( format );
365 transferred = true ;
e7549107 366
e135f093 367 if (size == 0)
f0822896
SC
368 {
369 data.SetData(format , 0 , 0 ) ;
370 }
371 else
372 {
373 char *d = new char[size];
374 m_data->GetDataHere( format , (void*) d );
375 data.SetData( format , size , d ) ;
376 delete[] d ;
377 }
378 }
379 }
380 }
381 /* get formats from wxDataObjects */
e135f093 382 if ( !transferred )
f0822896 383 {
f0822896
SC
384 for (size_t i = 0; !transferred && i < formatcount ; i++)
385 {
386 wxDataFormat format = array[i] ;
387
388 switch ( format.GetType() )
389 {
9c3c5849
SC
390 case wxDF_TEXT :
391 case wxDF_OEMTEXT :
392 case wxDF_BITMAP :
393 case wxDF_METAFILE :
f0822896
SC
394 {
395 long len ;
396 char* s = (char*)wxGetClipboardData(format, &len );
397 if ( s )
398 {
399 data.SetData( format , len , s ) ;
400 delete [] s;
401
402 transferred = true ;
403 }
404 }
e40298d5 405 break ;
d1171566 406
f0822896
SC
407 default :
408 break ;
409 }
410 }
411 }
e9576ca5 412
f0822896
SC
413 delete[] array ;
414 return transferred ;
415}