]>
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 | #ifdef __GNUG__ | |
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" | |
22 | #include "wx/intl.h" | |
23 | #include "wx/log.h" | |
24 | ||
25 | #ifndef __DARWIN__ | |
26 | #include <Scrap.h> | |
27 | #endif | |
28 | #include "wx/mac/uma.h" | |
29 | ||
30 | #define wxUSE_DATAOBJ 1 | |
31 | ||
32 | #include <string.h> | |
33 | ||
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"); | |
38 | ||
39 | void *wxGetClipboardData(wxDataFormat dataFormat, long *len) | |
40 | { | |
41 | #if !TARGET_CARBON | |
42 | OSErr err = noErr ; | |
43 | #else | |
44 | OSStatus err = noErr ; | |
45 | #endif | |
46 | void * data = NULL ; | |
47 | Size byteCount; | |
48 | ||
49 | switch (dataFormat.GetType()) | |
50 | { | |
51 | case wxDF_OEMTEXT: | |
52 | dataFormat = wxDF_TEXT; | |
53 | // fall through | |
54 | ||
55 | case wxDF_TEXT: | |
56 | break; | |
57 | case wxDF_UNICODETEXT: | |
58 | break; | |
59 | case wxDF_BITMAP : | |
60 | case wxDF_METAFILE : | |
61 | break ; | |
62 | default: | |
63 | { | |
64 | wxLogError(_("Unsupported clipboard format.")); | |
65 | return NULL; | |
66 | } | |
67 | } | |
68 | ||
69 | #if TARGET_CARBON | |
70 | ScrapRef scrapRef; | |
71 | ||
72 | err = GetCurrentScrap( &scrapRef ); | |
73 | if ( err != noTypeErr && err != memFullErr ) | |
74 | { | |
75 | ScrapFlavorFlags flavorFlags; | |
76 | ||
77 | if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr) | |
78 | { | |
79 | if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr) | |
80 | { | |
81 | Size allocSize = byteCount ; | |
82 | if ( dataFormat.GetType() == wxDF_TEXT ) | |
83 | allocSize += 1 ; | |
84 | else if ( dataFormat.GetType() == wxDF_UNICODETEXT ) | |
85 | allocSize += 2 ; | |
86 | ||
87 | data = new char[ allocSize ] ; | |
88 | ||
89 | if (( err = GetScrapFlavorData( scrapRef, dataFormat.GetFormatId(), &byteCount , data )) == noErr ) | |
90 | { | |
91 | *len = allocSize ; | |
92 | if ( dataFormat.GetType() == wxDF_TEXT ) | |
93 | ((char*)data)[byteCount] = 0 ; | |
94 | if ( dataFormat.GetType() == wxDF_UNICODETEXT ) | |
95 | ((wxChar*)data)[byteCount/2] = 0 ; | |
96 | } | |
97 | else | |
98 | { | |
99 | delete[] ((char *)data) ; | |
100 | data = NULL ; | |
101 | } | |
102 | } | |
103 | } | |
104 | } | |
105 | ||
106 | #else | |
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 | { | |
114 | byteCount = GetHandleSize( datahandle ) ; | |
115 | Size allocSize = byteCount ; | |
116 | if ( dataFormat.GetType() == wxDF_TEXT ) | |
117 | allocSize += 1 ; | |
118 | else if ( dataFormat.GetType() == wxDF_UNICODETEXT ) | |
119 | allocSize += 2 ; | |
120 | ||
121 | data = new char[ allocSize ] ; | |
122 | ||
123 | memcpy( (char*) data , (char*) *datahandle , byteCount ) ; | |
124 | if ( dataFormat.GetType() == wxDF_TEXT ) | |
125 | ((char*)data)[byteCount] = 0 ; | |
126 | if ( dataFormat.GetType() == wxDF_UNICODETEXT ) | |
127 | ((wxChar*)data)[byteCount/2] = 0 ; | |
128 | *len = byteCount ; | |
129 | } | |
130 | DisposeHandle( datahandle ) ; | |
131 | #endif | |
132 | if ( err ) | |
133 | { | |
134 | wxLogSysError(_("Failed to get clipboard data.")); | |
135 | ||
136 | return NULL ; | |
137 | } | |
138 | ||
139 | if ( dataFormat.GetType() == wxDF_TEXT ) | |
140 | { | |
141 | wxMacConvertNewlines10To13( (char*) data ) ; | |
142 | } | |
143 | ||
144 | return data; | |
145 | } | |
146 | ||
147 | ||
148 | /* | |
149 | * Generalized clipboard implementation by Matthew Flatt | |
150 | */ | |
151 | ||
152 | IMPLEMENT_DYNAMIC_CLASS(wxClipboard, wxObject) | |
153 | ||
154 | wxClipboard::wxClipboard() | |
155 | { | |
156 | m_open = false ; | |
157 | m_data = NULL ; | |
158 | } | |
159 | ||
160 | wxClipboard::~wxClipboard() | |
161 | { | |
162 | if (m_data) | |
163 | { | |
164 | delete m_data; | |
165 | m_data = (wxDataObject*) NULL; | |
166 | } | |
167 | } | |
168 | ||
169 | void wxClipboard::Clear() | |
170 | { | |
171 | if (m_data) | |
172 | { | |
173 | delete m_data; | |
174 | m_data = (wxDataObject*) NULL; | |
175 | } | |
176 | #if TARGET_CARBON | |
177 | OSStatus err ; | |
178 | err = ClearCurrentScrap( ); | |
179 | #else | |
180 | OSErr err ; | |
181 | err = ZeroScrap( ); | |
182 | #endif | |
183 | if ( err ) | |
184 | { | |
185 | wxLogSysError(_("Failed to empty the clipboard.")); | |
186 | } | |
187 | } | |
188 | ||
189 | bool wxClipboard::Flush() | |
190 | { | |
191 | return FALSE; | |
192 | } | |
193 | ||
194 | bool wxClipboard::Open() | |
195 | { | |
196 | wxCHECK_MSG( !m_open, FALSE, wxT("clipboard already open") ); | |
197 | m_open = true ; | |
198 | return true ; | |
199 | } | |
200 | ||
201 | bool wxClipboard::IsOpened() const | |
202 | { | |
203 | return m_open; | |
204 | } | |
205 | ||
206 | bool wxClipboard::SetData( wxDataObject *data ) | |
207 | { | |
208 | // as we can only store one wxDataObject, this is the same in this | |
209 | // implementation | |
210 | return AddData( data ); | |
211 | } | |
212 | ||
213 | bool wxClipboard::AddData( wxDataObject *data ) | |
214 | { | |
215 | wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") ); | |
216 | ||
217 | wxCHECK_MSG( data, FALSE, wxT("data is invalid") ); | |
218 | ||
219 | /* we can only store one wxDataObject */ | |
220 | Clear(); | |
221 | ||
222 | m_data = data; | |
223 | ||
224 | /* get formats from wxDataObjects */ | |
225 | wxDataFormat *array = new wxDataFormat[ m_data->GetFormatCount() ]; | |
226 | m_data->GetAllFormats( array ); | |
227 | ||
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() ); | |
233 | ||
234 | #if !TARGET_CARBON | |
235 | OSErr err = noErr ; | |
236 | #else | |
237 | OSStatus err = noErr ; | |
238 | #endif | |
239 | ||
240 | switch ( array[i].GetType() ) | |
241 | { | |
242 | case wxDF_TEXT: | |
243 | case wxDF_OEMTEXT: | |
244 | { | |
245 | wxTextDataObject* textDataObject = (wxTextDataObject*) data; | |
246 | wxCharBuffer buf = textDataObject->GetText().mb_str() ; | |
247 | err = UMAPutScrap( strlen(buf) , kScrapFlavorTypeText , (void*) buf.data() ) ; | |
248 | } | |
249 | break ; | |
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 | |
259 | #if wxUSE_DRAG_AND_DROP | |
260 | case wxDF_METAFILE: | |
261 | { | |
262 | wxMetafileDataObject* metaFileDataObject = | |
263 | (wxMetafileDataObject*) data; | |
264 | wxMetafile metaFile = metaFileDataObject->GetMetafile(); | |
265 | PicHandle pict = (PicHandle) metaFile.GetHMETAFILE() ; | |
266 | HLock( (Handle) pict ) ; | |
267 | err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ; | |
268 | HUnlock( (Handle) pict ) ; | |
269 | } | |
270 | break ; | |
271 | #endif | |
272 | case wxDF_BITMAP: | |
273 | case wxDF_DIB: | |
274 | { | |
275 | bool created = false ; | |
276 | PicHandle pict = NULL ; | |
277 | ||
278 | wxBitmapDataObject* bitmapDataObject = (wxBitmapDataObject*) data ; | |
279 | pict = (PicHandle) bitmapDataObject->GetBitmap().GetPict( &created ) ; | |
280 | ||
281 | HLock( (Handle) pict ) ; | |
282 | err = UMAPutScrap( GetHandleSize( (Handle) pict ) , kScrapFlavorTypePicture , *pict ) ; | |
283 | HUnlock( (Handle) pict ) ; | |
284 | if ( created ) | |
285 | KillPicture( pict ) ; | |
286 | } | |
287 | default: | |
288 | break ; | |
289 | } | |
290 | ||
291 | } | |
292 | ||
293 | delete[] array; | |
294 | ||
295 | return true ; | |
296 | } | |
297 | ||
298 | void wxClipboard::Close() | |
299 | { | |
300 | m_open = false ; | |
301 | } | |
302 | ||
303 | bool wxClipboard::IsSupported( const wxDataFormat &dataFormat ) | |
304 | { | |
305 | if ( m_data ) | |
306 | { | |
307 | return m_data->IsSupported( dataFormat ) ; | |
308 | } | |
309 | #if TARGET_CARBON | |
310 | OSStatus err = noErr; | |
311 | ScrapRef scrapRef; | |
312 | ||
313 | err = GetCurrentScrap( &scrapRef ); | |
314 | if ( err != noTypeErr && err != memFullErr ) | |
315 | { | |
316 | ScrapFlavorFlags flavorFlags; | |
317 | Size byteCount; | |
318 | ||
319 | if (( err = GetScrapFlavorFlags( scrapRef, dataFormat.GetFormatId(), &flavorFlags )) == noErr) | |
320 | { | |
321 | if (( err = GetScrapFlavorSize( scrapRef, dataFormat.GetFormatId(), &byteCount )) == noErr) | |
322 | { | |
323 | return TRUE ; | |
324 | } | |
325 | } | |
326 | } | |
327 | return FALSE; | |
328 | ||
329 | #else | |
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 ; | |
338 | #endif | |
339 | } | |
340 | ||
341 | bool wxClipboard::GetData( wxDataObject& data ) | |
342 | { | |
343 | wxCHECK_MSG( m_open, FALSE, wxT("clipboard not open") ); | |
344 | ||
345 | size_t formatcount = data.GetFormatCount() + 1 ; | |
346 | wxDataFormat *array = new wxDataFormat[ formatcount ]; | |
347 | array[0] = data.GetPreferredFormat(); | |
348 | data.GetAllFormats( &array[1] ); | |
349 | ||
350 | bool transferred = false ; | |
351 | ||
352 | if ( m_data ) | |
353 | { | |
354 | for (size_t i = 0; !transferred && i < formatcount ; i++) | |
355 | { | |
356 | wxDataFormat format = array[i] ; | |
357 | if ( m_data->IsSupported( format ) ) | |
358 | { | |
359 | int size = m_data->GetDataSize( format ); | |
360 | transferred = true ; | |
361 | ||
362 | if (size == 0) | |
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 */ | |
377 | if ( !transferred ) | |
378 | { | |
379 | for (size_t i = 0; !transferred && i < formatcount ; i++) | |
380 | { | |
381 | wxDataFormat format = array[i] ; | |
382 | ||
383 | switch ( format.GetType() ) | |
384 | { | |
385 | case wxDF_TEXT : | |
386 | case wxDF_OEMTEXT : | |
387 | case wxDF_BITMAP : | |
388 | case wxDF_METAFILE : | |
389 | { | |
390 | long len ; | |
391 | char* s = (char*)wxGetClipboardData(format, &len ); | |
392 | if ( s ) | |
393 | { | |
394 | data.SetData( format , len , s ) ; | |
395 | delete [] s; | |
396 | ||
397 | transferred = true ; | |
398 | } | |
399 | } | |
400 | break ; | |
401 | ||
402 | default : | |
403 | break ; | |
404 | } | |
405 | } | |
406 | } | |
407 | ||
408 | delete[] array ; | |
409 | return transferred ; | |
410 | } |