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