]>
Commit | Line | Data |
---|---|---|
e9576ca5 | 1 | ///////////////////////////////////////////////////////////////////////////// |
3e822cd8 | 2 | // Name: src/mac/carbon/clipbrd.cpp |
e9576ca5 | 3 | // Purpose: Clipboard functionality |
3e822cd8 DS |
4 | // Author: Stefan Csomor; |
5 | // Generalized clipboard implementation by Matthew Flatt | |
e9576ca5 | 6 | // Modified by: |
a31a5f85 | 7 | // Created: 1998-01-01 |
e9576ca5 | 8 | // RCS-ID: $Id$ |
a31a5f85 | 9 | // Copyright: (c) Stefan Csomor |
65571936 | 10 | // Licence: wxWindows licence |
e9576ca5 SC |
11 | ///////////////////////////////////////////////////////////////////////////// |
12 | ||
a8e9860d SC |
13 | #include "wx/wxprec.h" |
14 | ||
179e085f RN |
15 | #if wxUSE_CLIPBOARD |
16 | ||
e9576ca5 SC |
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" |
48d2ab90 | 24 | #include "wx/log.h" |
e9576ca5 | 25 | |
66a09d47 SC |
26 | #ifndef __DARWIN__ |
27 | #include <Scrap.h> | |
28 | #endif | |
3e822cd8 | 29 | |
9c3c5849 | 30 | #include "wx/mac/uma.h" |
76a5e5d2 | 31 | |
2f1ae414 SC |
32 | #define wxUSE_DATAOBJ 1 |
33 | ||
e9576ca5 SC |
34 | #include <string.h> |
35 | ||
3e822cd8 | 36 | |
f0822896 SC |
37 | // the trace mask we use with wxLogTrace() - call |
38 | // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here | |
39 | // (there will be a *lot* of them!) | |
3e822cd8 DS |
40 | static const wxChar *TRACE_CLIPBOARD = wxT("clipboard"); |
41 | ||
2f1ae414 | 42 | |
3e822cd8 | 43 | void * wxGetClipboardData( wxDataFormat dataFormat, long *len ) |
e9576ca5 | 44 | { |
3e822cd8 DS |
45 | OSStatus err = noErr; |
46 | void * data = NULL; | |
e40298d5 | 47 | Size byteCount; |
e135f093 | 48 | |
2f1ae414 SC |
49 | switch (dataFormat.GetType()) |
50 | { | |
e40298d5 JS |
51 | case wxDF_OEMTEXT: |
52 | dataFormat = wxDF_TEXT; | |
3e822cd8 | 53 | break; |
e135f093 | 54 | |
e40298d5 | 55 | case wxDF_TEXT: |
427ff662 SC |
56 | case wxDF_UNICODETEXT: |
57 | break; | |
3e822cd8 DS |
58 | |
59 | case wxDF_BITMAP: | |
60 | case wxDF_METAFILE: | |
61 | break; | |
62 | ||
e40298d5 | 63 | default: |
21ec3bf9 | 64 | // custom datatype |
3e822cd8 | 65 | break; |
97af5088 | 66 | } |
e135f093 | 67 | |
97af5088 | 68 | #if TARGET_CARBON |
ed60b502 | 69 | ScrapRef scrapRef; |
e135f093 | 70 | |
ed60b502 | 71 | err = GetCurrentScrap( &scrapRef ); |
e135f093 | 72 | if ( err != noTypeErr && err != memFullErr ) |
ed60b502 RR |
73 | { |
74 | ScrapFlavorFlags flavorFlags; | |
e135f093 | 75 | |
3e822cd8 DS |
76 | err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags ); |
77 | if (err == noErr) | |
ed60b502 | 78 | { |
3e822cd8 DS |
79 | err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount ); |
80 | if (err == noErr) | |
ed60b502 | 81 | { |
3e822cd8 | 82 | Size allocSize = byteCount; |
e135f093 | 83 | if ( dataFormat.GetType() == wxDF_TEXT ) |
3e822cd8 | 84 | allocSize += 1; |
e135f093 | 85 | else if ( dataFormat.GetType() == wxDF_UNICODETEXT ) |
3e822cd8 | 86 | allocSize += 2; |
e135f093 | 87 | |
3e822cd8 | 88 | data = new char[ allocSize ]; |
e135f093 | 89 | |
e40298d5 JS |
90 | if (( err = GetScrapFlavorData( scrapRef, dataFormat.GetFormatId(), &byteCount , data )) == noErr ) |
91 | { | |
3e822cd8 | 92 | *len = allocSize; |
e135f093 | 93 | if ( dataFormat.GetType() == wxDF_TEXT ) |
3e822cd8 | 94 | ((char*)data)[ byteCount ] = 0; |
e135f093 | 95 | if ( dataFormat.GetType() == wxDF_UNICODETEXT ) |
99b62b5f KH |
96 | { |
97 | // "data" format is UTF16, so 2 bytes = one character | |
98 | // wxChar size depends on platform, so just clear last 2 bytes | |
3e822cd8 DS |
99 | ((char*)data)[ byteCount + 0 ] = |
100 | ((char*)data)[ byteCount + 1 ] = 0; | |
99b62b5f | 101 | } |
e40298d5 JS |
102 | } |
103 | else | |
104 | { | |
3e822cd8 DS |
105 | delete [] (char*)data; |
106 | data = NULL; | |
e40298d5 | 107 | } |
ed60b502 RR |
108 | } |
109 | } | |
110 | } | |
e135f093 | 111 | |
97af5088 | 112 | #else |
3e822cd8 DS |
113 | long offset; |
114 | Handle datahandle = NewHandle( 0 ); | |
115 | HLock( datahandle ); | |
116 | err = (OSStatus)GetScrap( datahandle, dataFormat.GetFormatId(), &offset ); | |
117 | HUnlock( datahandle ); | |
ed60b502 RR |
118 | if ( GetHandleSize( datahandle ) > 0 ) |
119 | { | |
3e822cd8 DS |
120 | byteCount = GetHandleSize( datahandle ); |
121 | Size allocSize = byteCount; | |
e135f093 | 122 | if ( dataFormat.GetType() == wxDF_TEXT ) |
3e822cd8 | 123 | allocSize += 1; |
e135f093 | 124 | else if ( dataFormat.GetType() == wxDF_UNICODETEXT ) |
3e822cd8 | 125 | allocSize += 2; |
427ff662 | 126 | |
3e822cd8 | 127 | data = new char[ allocSize ]; |
427ff662 | 128 | |
3e822cd8 | 129 | memcpy( (char*) data, (char*) *datahandle, byteCount ); |
e135f093 | 130 | if ( dataFormat.GetType() == wxDF_TEXT ) |
3e822cd8 DS |
131 | ((char*)data)[ byteCount ] = 0; |
132 | else if ( dataFormat.GetType() == wxDF_UNICODETEXT ) | |
133 | ((wxChar*)data)[ byteCount / 2 ] = 0; | |
134 | *len = byteCount; | |
ed60b502 | 135 | } |
3e822cd8 DS |
136 | |
137 | DisposeHandle( datahandle ); | |
97af5088 | 138 | #endif |
3e822cd8 DS |
139 | |
140 | if (err != noErr) | |
97af5088 | 141 | { |
3e822cd8 | 142 | wxLogSysError(wxT("Failed to get clipboard data.")); |
e135f093 | 143 | |
3e822cd8 | 144 | return NULL; |
97af5088 | 145 | } |
427ff662 | 146 | |
939fba6c | 147 | if ( dataFormat.GetType() == wxDF_TEXT ) |
3e822cd8 | 148 | wxMacConvertNewlines10To13( (char*)data ); |
427ff662 | 149 | |
97af5088 | 150 | return data; |
2f1ae414 SC |
151 | } |
152 | ||
528e2293 | 153 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject) |
7c74e7fe | 154 | |
e7549107 SC |
155 | wxClipboard::wxClipboard() |
156 | { | |
3e822cd8 DS |
157 | m_open = false; |
158 | m_data = NULL; | |
e7549107 | 159 | } |
e9576ca5 | 160 | |
e7549107 | 161 | wxClipboard::~wxClipboard() |
e9576ca5 | 162 | { |
f0822896 | 163 | if (m_data) |
e7549107 | 164 | { |
f0822896 | 165 | delete m_data; |
3e822cd8 | 166 | m_data = (wxDataObject*)NULL; |
e7549107 | 167 | } |
e9576ca5 SC |
168 | } |
169 | ||
e7549107 | 170 | void wxClipboard::Clear() |
e9576ca5 | 171 | { |
f0822896 SC |
172 | if (m_data) |
173 | { | |
174 | delete m_data; | |
3e822cd8 | 175 | m_data = (wxDataObject*)NULL; |
f0822896 | 176 | } |
3e822cd8 | 177 | |
f0822896 | 178 | #if TARGET_CARBON |
3e822cd8 DS |
179 | OSStatus err; |
180 | err = ClearCurrentScrap(); | |
f0822896 | 181 | #else |
3e822cd8 DS |
182 | OSErr err; |
183 | err = ZeroScrap(); | |
f0822896 | 184 | #endif |
3e822cd8 DS |
185 | |
186 | if (err != noErr) | |
ed60b502 | 187 | { |
3e822cd8 | 188 | wxLogSysError( wxT("Failed to empty the clipboard.") ); |
ed60b502 | 189 | } |
e9576ca5 SC |
190 | } |
191 | ||
e7549107 | 192 | bool wxClipboard::Flush() |
e9576ca5 | 193 | { |
3dee36ae | 194 | return false; |
e7549107 SC |
195 | } |
196 | ||
197 | bool wxClipboard::Open() | |
198 | { | |
3dee36ae | 199 | wxCHECK_MSG( !m_open, false, wxT("clipboard already open") ); |
3e822cd8 DS |
200 | |
201 | m_open = true; | |
202 | ||
203 | return true; | |
e7549107 SC |
204 | } |
205 | ||
206 | bool wxClipboard::IsOpened() const | |
207 | { | |
f0822896 | 208 | return m_open; |
e9576ca5 SC |
209 | } |
210 | ||
f0822896 | 211 | bool wxClipboard::SetData( wxDataObject *data ) |
e9576ca5 | 212 | { |
3dee36ae | 213 | wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); |
3dee36ae | 214 | wxCHECK_MSG( data, false, wxT("data is invalid") ); |
634bafa9 SC |
215 | |
216 | Clear(); | |
3e822cd8 DS |
217 | |
218 | // as we can only store one wxDataObject, | |
219 | // this is the same in this implementation | |
f0822896 | 220 | return AddData( data ); |
e7549107 SC |
221 | } |
222 | ||
223 | bool wxClipboard::AddData( wxDataObject *data ) | |
224 | { | |
3dee36ae | 225 | wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); |
3dee36ae | 226 | wxCHECK_MSG( data, false, wxT("data is invalid") ); |
e7549107 | 227 | |
3e822cd8 | 228 | // we can only store one wxDataObject |
f0822896 | 229 | Clear(); |
e7549107 | 230 | |
f0822896 | 231 | m_data = data; |
e7549107 | 232 | |
21ec3bf9 | 233 | // get formats from wxDataObjects |
f0822896 SC |
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 | |
3e822cd8 DS |
243 | size_t sz = data->GetDataSize( array[ i ] ); |
244 | void* buf = malloc( sz + 1 ); | |
634bafa9 | 245 | if ( buf ) |
3dee36ae | 246 | { |
99b62b5f | 247 | // empty the buffer because in some case GetDataHere does not fill buf |
3e822cd8 DS |
248 | memset(buf, 0, sz + 1); |
249 | data->GetDataHere( array[ i ] , buf ); | |
250 | OSType mactype = 0; | |
634bafa9 SC |
251 | switch ( array[i].GetType() ) |
252 | { | |
3e822cd8 DS |
253 | case wxDF_TEXT: |
254 | case wxDF_OEMTEXT: | |
255 | mactype = kScrapFlavorTypeText; | |
256 | sz -= 1; | |
257 | break; | |
258 | ||
259 | #if wxUSE_UNICODE | |
260 | case wxDF_UNICODETEXT: | |
261 | mactype = kScrapFlavorTypeUnicode; | |
262 | sz -= 2; | |
263 | break; | |
264 | #endif | |
265 | ||
266 | #if wxUSE_DRAG_AND_DROP | |
634bafa9 | 267 | case wxDF_METAFILE: |
3e822cd8 DS |
268 | mactype = kScrapFlavorTypePicture; |
269 | break; | |
270 | #endif | |
271 | ||
272 | case wxDF_BITMAP: | |
273 | case wxDF_DIB: | |
274 | mactype = kScrapFlavorTypePicture; | |
275 | break; | |
276 | ||
277 | default: | |
278 | mactype = (OSType)(array[i].GetFormatId()); | |
279 | break; | |
634bafa9 | 280 | } |
3e822cd8 DS |
281 | |
282 | UMAPutScrap( sz , mactype , buf ); | |
283 | free( buf ); | |
634bafa9 | 284 | } |
e9576ca5 SC |
285 | } |
286 | ||
3e822cd8 | 287 | delete [] array; |
e9576ca5 | 288 | |
3e822cd8 | 289 | return true; |
e9576ca5 SC |
290 | } |
291 | ||
f0822896 | 292 | void wxClipboard::Close() |
e9576ca5 | 293 | { |
634bafa9 SC |
294 | wxCHECK_RET( m_open, wxT("clipboard not open") ); |
295 | ||
3e822cd8 | 296 | m_open = false; |
3dee36ae | 297 | |
3e822cd8 DS |
298 | // Get rid of cached object. |
299 | // If this is not done, copying data from | |
300 | // another application will only work once | |
89a69c60 SC |
301 | if (m_data) |
302 | { | |
303 | delete m_data; | |
304 | m_data = (wxDataObject*) NULL; | |
3dee36ae | 305 | } |
e9576ca5 SC |
306 | } |
307 | ||
f0822896 | 308 | bool wxClipboard::IsSupported( const wxDataFormat &dataFormat ) |
e7549107 | 309 | { |
3dee36ae | 310 | if ( m_data ) |
3e822cd8 DS |
311 | return m_data->IsSupported( dataFormat ); |
312 | ||
f0822896 | 313 | #if TARGET_CARBON |
ed60b502 RR |
314 | OSStatus err = noErr; |
315 | ScrapRef scrapRef; | |
e135f093 | 316 | |
ed60b502 | 317 | err = GetCurrentScrap( &scrapRef ); |
e135f093 | 318 | if ( err != noTypeErr && err != memFullErr ) |
ed60b502 | 319 | { |
3e822cd8 DS |
320 | ScrapFlavorFlags flavorFlags; |
321 | Size byteCount; | |
e135f093 | 322 | |
3e822cd8 DS |
323 | err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags ); |
324 | if (err == noErr) | |
ed60b502 | 325 | { |
3e822cd8 DS |
326 | err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount ); |
327 | if (err == noErr) | |
328 | return true; | |
ed60b502 RR |
329 | } |
330 | } | |
3e822cd8 | 331 | |
3dee36ae | 332 | return false; |
e135f093 | 333 | |
f0822896 | 334 | #else |
3e822cd8 DS |
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 | ||
343 | return hasData; | |
f0822896 | 344 | #endif |
e9576ca5 | 345 | } |
f0822896 SC |
346 | |
347 | bool wxClipboard::GetData( wxDataObject& data ) | |
e9576ca5 | 348 | { |
3dee36ae | 349 | wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); |
e9576ca5 | 350 | |
3e822cd8 DS |
351 | size_t formatcount = data.GetFormatCount() + 1; |
352 | wxDataFormat *array = new wxDataFormat[ formatcount ]; | |
f0822896 SC |
353 | array[0] = data.GetPreferredFormat(); |
354 | data.GetAllFormats( &array[1] ); | |
e9576ca5 | 355 | |
3e822cd8 | 356 | bool transferred = false; |
e9576ca5 | 357 | |
f0822896 SC |
358 | if ( m_data ) |
359 | { | |
3e822cd8 | 360 | for (size_t i = 0; !transferred && i < formatcount; i++) |
21ec3bf9 | 361 | { |
3e822cd8 | 362 | wxDataFormat format = array[ i ]; |
21ec3bf9 | 363 | if ( m_data->IsSupported( format ) ) |
f0822896 | 364 | { |
3e822cd8 DS |
365 | int dataSize = m_data->GetDataSize( format ); |
366 | transferred = true; | |
21ec3bf9 | 367 | |
3e822cd8 | 368 | if (dataSize == 0) |
21ec3bf9 | 369 | { |
3e822cd8 | 370 | data.SetData( format, 0, 0 ); |
21ec3bf9 SC |
371 | } |
372 | else | |
373 | { | |
3e822cd8 DS |
374 | char *d = new char[ dataSize ]; |
375 | m_data->GetDataHere( format, (void*) d ); | |
376 | data.SetData( format, dataSize, d ); | |
377 | delete [] d; | |
21ec3bf9 | 378 | } |
f0822896 | 379 | } |
21ec3bf9 | 380 | } |
f0822896 | 381 | } |
3e822cd8 DS |
382 | |
383 | // get formats from wxDataObjects | |
e135f093 | 384 | if ( !transferred ) |
f0822896 | 385 | { |
3e822cd8 | 386 | for (size_t i = 0; !transferred && i < formatcount; i++) |
21ec3bf9 | 387 | { |
3e822cd8 | 388 | wxDataFormat format = array[ i ]; |
21ec3bf9 SC |
389 | |
390 | switch ( format.GetType() ) | |
391 | { | |
392 | // NOTE: this is usable for all data types | |
3e822cd8 | 393 | case wxDF_TEXT: |
21ec3bf9 | 394 | case wxDF_UNICODETEXT: |
3e822cd8 DS |
395 | case wxDF_OEMTEXT: |
396 | case wxDF_BITMAP: | |
397 | case wxDF_METAFILE: | |
398 | default: | |
21ec3bf9 | 399 | { |
3e822cd8 DS |
400 | long len; |
401 | char* s = (char*)wxGetClipboardData( format, &len ); | |
402 | if (s != NULL) | |
21ec3bf9 | 403 | { |
3e822cd8 | 404 | data.SetData( format, len, s ); |
21ec3bf9 SC |
405 | delete [] s; |
406 | ||
3e822cd8 | 407 | transferred = true; |
21ec3bf9 SC |
408 | } |
409 | } | |
3e822cd8 | 410 | break; |
21ec3bf9 SC |
411 | } |
412 | } | |
f0822896 | 413 | } |
e9576ca5 | 414 | |
3e822cd8 DS |
415 | delete [] array; |
416 | return transferred; | |
f0822896 | 417 | } |
179e085f RN |
418 | |
419 | #endif |