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