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