]>
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 |
65571936 | 9 | // Licence: wxWindows licence |
e9576ca5 SC |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
a8e9860d SC |
12 | #include "wx/wxprec.h" |
13 | ||
179e085f RN |
14 | #if wxUSE_CLIPBOARD |
15 | ||
e9576ca5 SC |
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 ) |
99b62b5f KH |
95 | { |
96 | // "data" format is UTF16, so 2 bytes = one character | |
97 | // wxChar size depends on platform, so just clear last 2 bytes | |
98 | ((char*)data)[byteCount] = 0; | |
99 | ((char*)data)[byteCount+1] = 0; | |
100 | } | |
e40298d5 JS |
101 | } |
102 | else | |
103 | { | |
104 | delete[] ((char *)data) ; | |
105 | data = NULL ; | |
106 | } | |
ed60b502 RR |
107 | } |
108 | } | |
109 | } | |
e135f093 | 110 | |
97af5088 | 111 | #else |
ed60b502 RR |
112 | long offset ; |
113 | Handle datahandle = NewHandle(0) ; | |
114 | HLock( datahandle ) ; | |
115 | GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ; | |
116 | HUnlock( datahandle ) ; | |
117 | if ( GetHandleSize( datahandle ) > 0 ) | |
118 | { | |
e40298d5 | 119 | byteCount = GetHandleSize( datahandle ) ; |
427ff662 | 120 | Size allocSize = byteCount ; |
e135f093 | 121 | if ( dataFormat.GetType() == wxDF_TEXT ) |
427ff662 | 122 | allocSize += 1 ; |
e135f093 | 123 | else if ( dataFormat.GetType() == wxDF_UNICODETEXT ) |
427ff662 SC |
124 | allocSize += 2 ; |
125 | ||
126 | data = new char[ allocSize ] ; | |
127 | ||
e40298d5 | 128 | memcpy( (char*) data , (char*) *datahandle , byteCount ) ; |
e135f093 | 129 | if ( dataFormat.GetType() == wxDF_TEXT ) |
e40298d5 | 130 | ((char*)data)[byteCount] = 0 ; |
e135f093 | 131 | if ( dataFormat.GetType() == wxDF_UNICODETEXT ) |
427ff662 | 132 | ((wxChar*)data)[byteCount/2] = 0 ; |
e40298d5 | 133 | *len = byteCount ; |
ed60b502 RR |
134 | } |
135 | DisposeHandle( datahandle ) ; | |
97af5088 SC |
136 | #endif |
137 | if ( err ) | |
138 | { | |
139 | wxLogSysError(_("Failed to get clipboard data.")); | |
e135f093 | 140 | |
97af5088 SC |
141 | return NULL ; |
142 | } | |
427ff662 | 143 | |
939fba6c | 144 | if ( dataFormat.GetType() == wxDF_TEXT ) |
97af5088 | 145 | { |
8aa701ed | 146 | wxMacConvertNewlines10To13( (char*) data ) ; |
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 | 157 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject) |
7c74e7fe | 158 | |
e7549107 SC |
159 | wxClipboard::wxClipboard() |
160 | { | |
e40298d5 JS |
161 | m_open = false ; |
162 | m_data = NULL ; | |
e7549107 | 163 | } |
e9576ca5 | 164 | |
e7549107 | 165 | wxClipboard::~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 | 174 | void 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 | 194 | bool wxClipboard::Flush() |
e9576ca5 | 195 | { |
3dee36ae | 196 | return false; |
e7549107 SC |
197 | } |
198 | ||
199 | bool wxClipboard::Open() | |
200 | { | |
3dee36ae | 201 | wxCHECK_MSG( !m_open, false, wxT("clipboard already open") ); |
f0822896 SC |
202 | m_open = true ; |
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") ); |
634bafa9 | 214 | |
3dee36ae | 215 | wxCHECK_MSG( data, false, wxT("data is invalid") ); |
634bafa9 SC |
216 | |
217 | Clear(); | |
e135f093 VZ |
218 | // as we can only store one wxDataObject, this is the same in this |
219 | // 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") ); |
e7549107 | 226 | |
3dee36ae | 227 | wxCHECK_MSG( data, false, wxT("data is invalid") ); |
e7549107 | 228 | |
f0822896 SC |
229 | /* we can only store one wxDataObject */ |
230 | Clear(); | |
e7549107 | 231 | |
f0822896 | 232 | m_data = data; |
e7549107 | 233 | |
f0822896 SC |
234 | /* get formats from wxDataObjects */ |
235 | wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; | |
236 | m_data->GetAllFormats( array ); | |
e7549107 | 237 | |
f0822896 SC |
238 | for (size_t i = 0; i < m_data->GetFormatCount(); i++) |
239 | { | |
240 | wxLogTrace( TRACE_CLIPBOARD, | |
241 | wxT("wxClipboard now supports atom %s"), | |
242 | array[i].GetId().c_str() ); | |
e7549107 | 243 | |
634bafa9 SC |
244 | size_t sz = data->GetDataSize( array[i] ) ; |
245 | void* buf = malloc( sz + 1 ) ; | |
246 | if ( buf ) | |
3dee36ae | 247 | { |
99b62b5f KH |
248 | // empty the buffer because in some case GetDataHere does not fill buf |
249 | memset(buf, 0, sz+1); | |
634bafa9 SC |
250 | data->GetDataHere( array[i] , buf ) ; |
251 | OSType mactype = 0 ; | |
252 | switch ( array[i].GetType() ) | |
253 | { | |
254 | case wxDF_TEXT: | |
255 | case wxDF_OEMTEXT: | |
256 | mactype = kScrapFlavorTypeText ; | |
0c4f1b2a | 257 | sz -= 1; |
634bafa9 SC |
258 | break ; |
259 | #if wxUSE_UNICODE | |
260 | case wxDF_UNICODETEXT : | |
261 | mactype = kScrapFlavorTypeUnicode ; | |
0c4f1b2a | 262 | sz -= 2; |
634bafa9 SC |
263 | break ; |
264 | #endif | |
265 | #if wxUSE_DRAG_AND_DROP | |
266 | case wxDF_METAFILE: | |
267 | mactype = kScrapFlavorTypePicture ; | |
268 | break ; | |
269 | #endif | |
270 | case wxDF_BITMAP: | |
271 | case wxDF_DIB: | |
272 | mactype = kScrapFlavorTypePicture ; | |
273 | break ; | |
274 | default: | |
275 | break ; | |
276 | } | |
277 | UMAPutScrap( sz , mactype , buf ) ; | |
278 | free( buf ) ; | |
279 | } | |
e9576ca5 SC |
280 | } |
281 | ||
f0822896 | 282 | delete[] array; |
e9576ca5 | 283 | |
f0822896 | 284 | return true ; |
e9576ca5 SC |
285 | } |
286 | ||
f0822896 | 287 | void wxClipboard::Close() |
e9576ca5 | 288 | { |
634bafa9 SC |
289 | wxCHECK_RET( m_open, wxT("clipboard not open") ); |
290 | ||
f0822896 | 291 | m_open = false ; |
3dee36ae WS |
292 | |
293 | // Get rid of cached object. If this is not done copying from another application will | |
294 | // only work once | |
89a69c60 SC |
295 | if (m_data) |
296 | { | |
297 | delete m_data; | |
298 | m_data = (wxDataObject*) NULL; | |
3dee36ae WS |
299 | } |
300 | ||
e9576ca5 SC |
301 | } |
302 | ||
f0822896 | 303 | bool wxClipboard::IsSupported( const wxDataFormat &dataFormat ) |
e7549107 | 304 | { |
3dee36ae WS |
305 | if ( m_data ) |
306 | { | |
307 | return m_data->IsSupported( dataFormat ) ; | |
308 | } | |
f0822896 | 309 | #if TARGET_CARBON |
ed60b502 RR |
310 | OSStatus err = noErr; |
311 | ScrapRef scrapRef; | |
e135f093 | 312 | |
ed60b502 | 313 | err = GetCurrentScrap( &scrapRef ); |
e135f093 | 314 | if ( err != noTypeErr && err != memFullErr ) |
ed60b502 RR |
315 | { |
316 | ScrapFlavorFlags flavorFlags; | |
317 | Size byteCount; | |
e135f093 | 318 | |
ed60b502 RR |
319 | if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr) |
320 | { | |
321 | if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr) | |
322 | { | |
3dee36ae | 323 | return true ; |
ed60b502 RR |
324 | } |
325 | } | |
326 | } | |
3dee36ae | 327 | return false; |
e135f093 | 328 | |
f0822896 | 329 | #else |
ed60b502 RR |
330 | long offset ; |
331 | Handle datahandle = NewHandle(0) ; | |
332 | HLock( datahandle ) ; | |
333 | GetScrap( datahandle , dataFormat.GetFormatId() , &offset ) ; | |
334 | HUnlock( datahandle ) ; | |
335 | bool hasData = GetHandleSize( datahandle ) > 0 ; | |
336 | DisposeHandle( datahandle ) ; | |
337 | return hasData ; | |
f0822896 | 338 | #endif |
e9576ca5 | 339 | } |
f0822896 SC |
340 | |
341 | bool wxClipboard::GetData( wxDataObject& data ) | |
e9576ca5 | 342 | { |
3dee36ae | 343 | wxCHECK_MSG( m_open, false, wxT("clipboard not open") ); |
e9576ca5 | 344 | |
3340066a | 345 | size_t formatcount = data.GetFormatCount() + 1 ; |
f0822896 SC |
346 | wxDataFormat *array = new wxDataFormat[ formatcount ]; |
347 | array[0] = data.GetPreferredFormat(); | |
348 | data.GetAllFormats( &array[1] ); | |
e9576ca5 | 349 | |
f0822896 | 350 | bool transferred = false ; |
e9576ca5 | 351 | |
f0822896 SC |
352 | if ( m_data ) |
353 | { | |
354 | for (size_t i = 0; !transferred && i < formatcount ; i++) | |
355 | { | |
356 | wxDataFormat format = array[i] ; | |
e135f093 | 357 | if ( m_data->IsSupported( format ) ) |
f0822896 SC |
358 | { |
359 | int size = m_data->GetDataSize( format ); | |
360 | transferred = true ; | |
e7549107 | 361 | |
e135f093 | 362 | if (size == 0) |
f0822896 SC |
363 | { |
364 | data.SetData(format , 0 , 0 ) ; | |
365 | } | |
366 | else | |
367 | { | |
368 | char *d = new char[size]; | |
369 | m_data->GetDataHere( format , (void*) d ); | |
370 | data.SetData( format , size , d ) ; | |
371 | delete[] d ; | |
372 | } | |
373 | } | |
374 | } | |
375 | } | |
376 | /* get formats from wxDataObjects */ | |
e135f093 | 377 | if ( !transferred ) |
f0822896 | 378 | { |
f0822896 SC |
379 | for (size_t i = 0; !transferred && i < formatcount ; i++) |
380 | { | |
381 | wxDataFormat format = array[i] ; | |
382 | ||
383 | switch ( format.GetType() ) | |
384 | { | |
9c3c5849 | 385 | case wxDF_TEXT : |
99b62b5f | 386 | case wxDF_UNICODETEXT: |
9c3c5849 SC |
387 | case wxDF_OEMTEXT : |
388 | case wxDF_BITMAP : | |
389 | case wxDF_METAFILE : | |
f0822896 SC |
390 | { |
391 | long len ; | |
392 | char* s = (char*)wxGetClipboardData(format, &len ); | |
393 | if ( s ) | |
394 | { | |
395 | data.SetData( format , len , s ) ; | |
396 | delete [] s; | |
397 | ||
398 | transferred = true ; | |
399 | } | |
400 | } | |
e40298d5 | 401 | break ; |
d1171566 | 402 | |
f0822896 SC |
403 | default : |
404 | break ; | |
405 | } | |
406 | } | |
407 | } | |
e9576ca5 | 408 | |
f0822896 SC |
409 | delete[] array ; |
410 | return transferred ; | |
411 | } | |
179e085f RN |
412 | |
413 | #endif |