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