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