]> git.saurik.com Git - wxWidgets.git/blob - src/motif/clipbrd.cpp
fixes #13557
[wxWidgets.git] / src / motif / clipbrd.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: Julian Smart
5 // Modified by: Mattia Barbon (added support for generic wxDataObjects)
6 // Created: 17/09/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __VMS
16 #include "wx/vms_x_fix.h"
17 #define XtWindow XTWINDOW
18 #define XtScreen XTSCREEN
19 #define XtParent XTPARENT
20 #define XtIsRealized XTISREALIZED
21 #define XtDisplay XTDISPLAY
22 #endif
23
24 #if wxUSE_CLIPBOARD
25
26 #include "wx/clipbrd.h"
27
28 #ifndef WX_PRECOMP
29 #include "wx/app.h"
30 #include "wx/utils.h"
31 #include "wx/bitmap.h"
32 #include "wx/dataobj.h"
33 #endif
34
35 #include "wx/scopedarray.h"
36
37 typedef wxScopedArray<wxDataFormat> wxDataFormatScopedArray;
38
39 #ifdef __VMS__
40 #pragma message disable nosimpint
41
42 #endif
43 #include <Xm/Xm.h>
44 #include <Xm/CutPaste.h>
45 #ifdef __VMS__
46 #pragma message enable nosimpint
47 #endif
48
49 #include "wx/motif/private.h"
50
51 bool wxOpenClipboard()
52 {
53 return wxTheClipboard->Open();
54 }
55
56 bool wxCloseClipboard()
57 {
58 wxTheClipboard->Close();
59
60 return true;
61 }
62
63 bool wxEmptyClipboard()
64 {
65 wxTheClipboard->Clear();
66 return true;
67 }
68
69 bool wxClipboardOpen()
70 {
71 return wxTheClipboard->IsOpened();
72 }
73
74 bool wxIsClipboardFormatAvailable(const wxDataFormat& dataFormat)
75 {
76 return wxTheClipboard->IsSupported( dataFormat );
77 }
78
79 bool wxSetClipboardData(wxDataFormat dataFormat, wxObject *obj,
80 int WXUNUSED(width), int WXUNUSED(height))
81 {
82 wxDataObject* dobj = NULL;
83
84 if( dataFormat == wxDF_TEXT )
85 {
86 wxChar* data = (wxChar*)obj;
87 dobj = new wxTextDataObject( data );
88 }
89 else if( dataFormat = wxDF_BITMAP )
90 {
91 wxBitmap* data = (wxBitmap*)obj;
92 dobj = new wxBitmapDataObject( *data );
93 }
94
95 if( !dobj )
96 return false;
97
98 return wxTheClipboard->SetData( dobj );
99 }
100
101 wxObject *wxGetClipboardData(wxDataFormat dataFormat, long *len)
102 {
103 wxDataObject* dobj = NULL;
104 wxTextDataObject* tobj = NULL;
105 wxBitmapDataObject* bobj = NULL;
106
107 if( dataFormat == wxDF_TEXT )
108 {
109 dobj = tobj = new wxTextDataObject;
110 }
111 else if( dataFormat = wxDF_BITMAP )
112 {
113 dobj = bobj = new wxBitmapDataObject;
114 }
115
116 if( !dobj || !wxTheClipboard->GetData( *dobj ) )
117 return NULL;
118
119 if( tobj )
120 {
121 wxString text = tobj->GetText();
122 wxChar* buf = new wxChar[text.length() + 1];
123
124 if( len ) *len = text.length();
125 return (wxObject*)wxStrcpy( buf, text.c_str() );
126 }
127 else if( bobj )
128 {
129 if( len ) *len = 0;
130 return new wxBitmap( bobj->GetBitmap() );
131 }
132
133 return NULL; // just in case...
134 }
135
136 wxDataFormat wxEnumClipboardFormats(const wxDataFormat& dataFormat)
137 {
138 // Only wxDF_TEXT supported
139 if (dataFormat == wxDF_TEXT)
140 return wxDF_TEXT;
141 else
142 return wxDF_INVALID;
143 }
144
145 wxDataFormat wxRegisterClipboardFormat(char *WXUNUSED(formatName))
146 {
147 // Not supported
148 return wxDF_INVALID;
149 }
150
151 bool wxGetClipboardFormatName(const wxDataFormat& dataFormat, char *formatName,
152 int maxCount)
153 {
154 wxStrlcpy( formatName, dataFormat.GetId().c_str(), maxCount );
155
156 return true;
157 }
158
159 //-----------------------------------------------------------------------------
160 // wxClipboard
161 //-----------------------------------------------------------------------------
162
163 struct wxDataIdToDataObject
164 {
165 wxDataIdToDataObject( wxDataObject* o, long d, size_t s )
166 : object( o ), size( s ), dataId( d ) { }
167
168 wxDataObject* object;
169 size_t size;
170 long dataId;
171 };
172
173 #include "wx/listimpl.cpp"
174
175 WX_DEFINE_LIST(wxDataObjectList)
176 WX_DEFINE_LIST(wxDataIdToDataObjectList)
177
178 extern "C"
179 {
180 #if wxCHECK_LESSTIF()
181 static void wxClipboardCallback( Widget widget, int* data_id,
182 int* priv, int* reason );
183 #else // Motif
184 static void wxClipboardCallback( Widget widget, long* data_id,
185 long* priv, int* reason );
186 #endif // Less/Motif
187 }
188
189 IMPLEMENT_DYNAMIC_CLASS(wxClipboard,wxObject)
190
191 wxClipboard::wxClipboard()
192 {
193 m_open = false;
194 }
195
196 wxClipboard::~wxClipboard()
197 {
198 Clear();
199 }
200
201 void wxClipboard::Clear()
202 {
203 wxDataObjectList::compatibility_iterator node = m_data.GetFirst();
204 while (node)
205 {
206 delete node->GetData();
207 node = node->GetNext();
208 }
209 m_data.Clear();
210
211 for( wxDataIdToDataObjectList::compatibility_iterator node2 = m_idToObject.GetFirst();
212 node2; node2 = node2->GetNext() )
213 {
214 delete node2->GetData();
215 }
216 m_idToObject.Clear();
217 }
218
219 bool wxClipboard::Open()
220 {
221 wxCHECK_MSG( !m_open, false, "clipboard already open" );
222
223 m_open = true;
224
225 return true;
226 }
227
228 bool wxClipboard::SetData( wxDataObject *data )
229 {
230 wxCHECK_MSG( data, false, "data is invalid" );
231 wxCHECK_MSG( m_open, false, "clipboard not open" );
232
233 Clear();
234
235 return AddData( data );
236 }
237
238 #if wxCHECK_LESSTIF()
239 void wxClipboardCallback( Widget xwidget, int* data_id,
240 int* priv, int* WXUNUSED(reason) )
241 #else
242 void wxClipboardCallback( Widget xwidget, long* data_id,
243 long* priv, int* WXUNUSED(reason) )
244 #endif
245 {
246 Display* xdisplay = XtDisplay( xwidget );
247 Window xwindow = XtWindow( xwidget );
248 wxDataObject* dobj = NULL;
249 size_t size = 0;
250
251 for( wxDataIdToDataObjectList::compatibility_iterator node2 =
252 wxTheClipboard->m_idToObject.GetFirst();
253 node2; node2 = node2->GetNext() )
254 {
255 wxDataIdToDataObject* dido = node2->GetData();
256 if( dido->dataId == *data_id )
257 {
258 dobj = dido->object;
259 size = dido->size;
260 break;
261 }
262 }
263
264 if( !dobj ) return;
265
266 wxCharBuffer buffer(size);
267 size_t count = dobj->GetFormatCount( wxDataObject::Get );
268 wxDataFormatScopedArray dfarr( new wxDataFormat[count] );
269 dobj->GetAllFormats( dfarr.get(), wxDataObject::Get );
270
271 if( !dobj->GetDataHere( dfarr[*priv], buffer.data() ) )
272 return;
273
274 while( XmClipboardCopyByName( xdisplay, xwindow, *data_id,
275 buffer.data(), size, 0 )
276 == XmClipboardLocked );
277 }
278
279 bool wxClipboard::AddData( wxDataObject *data )
280 {
281 wxCHECK_MSG( data, false, "data is invalid" );
282 wxCHECK_MSG( m_open, false, "clipboard not open" );
283
284 m_data.Append( data );
285
286 Display* xdisplay = wxGlobalDisplay();
287 Widget xwidget = (Widget)wxTheApp->GetTopLevelRealizedWidget();
288 Window xwindow = XtWindow( xwidget );
289 wxXmString label( wxTheApp->GetAppDisplayName() );
290 Time timestamp = XtLastTimestampProcessed( xdisplay );
291 long itemId;
292
293 int retval;
294
295 while( ( retval = XmClipboardStartCopy( xdisplay, xwindow, label(),
296 timestamp, xwidget,
297 wxClipboardCallback,
298 &itemId ) )
299 == XmClipboardLocked );
300 if( retval != XmClipboardSuccess )
301 return false;
302
303 size_t count = data->GetFormatCount( wxDataObject::Get );
304 wxDataFormatScopedArray dfarr( new wxDataFormat[count] );
305 data->GetAllFormats( dfarr.get(), wxDataObject::Get );
306
307 for( size_t i = 0; i < count; ++i )
308 {
309 size_t size = data->GetDataSize( dfarr[i] );
310 long data_id;
311 wxString id = dfarr[i].GetId();
312
313 while( ( retval = XmClipboardCopy( xdisplay, xwindow, itemId,
314 id.char_str(),
315 NULL, size, i, &data_id ) )
316 == XmClipboardLocked );
317
318 m_idToObject.Append( new wxDataIdToDataObject( data, data_id, size ) );
319 }
320
321 while( XmClipboardEndCopy( xdisplay, xwindow, itemId )
322 == XmClipboardLocked );
323
324 return true;
325 }
326
327 void wxClipboard::Close()
328 {
329 wxCHECK_RET( m_open, "clipboard not open" );
330
331 m_open = false;
332 }
333
334 bool wxClipboard::IsSupported(const wxDataFormat& format)
335 {
336 Display* xdisplay = wxGlobalDisplay();
337 Window xwindow = XtWindow( (Widget)wxTheApp->GetTopLevelRealizedWidget() );
338 bool isSupported = false;
339 int retval, count;
340 unsigned long max_name_length;
341 wxString id = format.GetId();
342
343 while( ( retval = XmClipboardLock( xdisplay, xwindow ) )
344 == XmClipboardLocked );
345 if( retval != XmClipboardSuccess )
346 return false;
347
348 if( XmClipboardInquireCount( xdisplay, xwindow, &count, &max_name_length )
349 == XmClipboardSuccess )
350 {
351 wxCharBuffer buf( max_name_length + 1 );
352 unsigned long copied;
353
354 for( int i = 0; i < count; ++i )
355 {
356 if( XmClipboardInquireFormat( xdisplay, xwindow, i + 1,
357 (XtPointer)buf.data(),
358 max_name_length, &copied )
359 != XmClipboardSuccess )
360 continue;
361
362 buf.data()[copied] = '\0';
363
364 if( buf == id )
365 {
366 isSupported = true;
367 break;
368 }
369 }
370 }
371
372 XmClipboardUnlock( xdisplay, xwindow, False );
373
374 return isSupported;
375 }
376
377 class wxClipboardEndRetrieve
378 {
379 public:
380 wxClipboardEndRetrieve( Display* display, Window window )
381 : m_display( display ), m_window( window ) { }
382 ~wxClipboardEndRetrieve()
383 {
384 while( XmClipboardEndRetrieve( m_display, m_window )
385 == XmClipboardLocked );
386 }
387 private:
388 Display* m_display;
389 Window m_window;
390 };
391
392 bool wxClipboard::GetData( wxDataObject& data )
393 {
394 wxCHECK_MSG( m_open, false, "clipboard not open" );
395
396 Display* xdisplay = wxGlobalDisplay();
397 Window xwindow = XtWindow( (Widget)wxTheApp->GetTopLevelRealizedWidget() );
398 Time timestamp = XtLastTimestampProcessed( xdisplay );
399
400 wxDataFormat chosenFormat;
401 int retval;
402
403 ///////////////////////////////////////////////////////////////////////////
404 // determine if the cliboard holds any format we like
405 ///////////////////////////////////////////////////////////////////////////
406 while( ( retval = XmClipboardStartRetrieve( xdisplay, xwindow,
407 timestamp ) )
408 == XmClipboardLocked );
409 if( retval != XmClipboardSuccess )
410 return false;
411
412 wxClipboardEndRetrieve endRetrieve( xdisplay, xwindow );
413
414 int count;
415 unsigned long max_name_length;
416 size_t dfcount = data.GetFormatCount( wxDataObject::Set );
417 wxDataFormatScopedArray dfarr( new wxDataFormat[dfcount] );
418 data.GetAllFormats( dfarr.get(), wxDataObject::Set );
419
420 if( XmClipboardInquireCount( xdisplay, xwindow, &count, &max_name_length )
421 == XmClipboardSuccess )
422 {
423 wxCharBuffer buf( max_name_length + 1 );
424 unsigned long copied;
425
426 for( int i = 0; i < count; ++i )
427 {
428 if( XmClipboardInquireFormat( xdisplay, xwindow, i + 1,
429 (XtPointer)buf.data(),
430 max_name_length, &copied )
431 != XmClipboardSuccess )
432 continue;
433
434 buf.data()[copied] = '\0';
435
436 // try preferred format
437 if( buf == data.GetPreferredFormat( wxDataObject::Set ).GetId() )
438 {
439 chosenFormat = data.GetPreferredFormat( wxDataObject::Set );
440 break;
441 }
442
443 // try all other formats
444 for( size_t i = 0; i < dfcount; ++i )
445 {
446 if( buf == dfarr[i].GetId() )
447 chosenFormat = dfarr[i];
448 }
449 }
450 }
451
452 if( chosenFormat == wxDF_INVALID )
453 return false;
454
455 ///////////////////////////////////////////////////////////////////////////
456 // now retrieve the data
457 ///////////////////////////////////////////////////////////////////////////
458 unsigned long length, dummy1;
459 long dummy2;
460 wxString id = chosenFormat.GetId();
461
462 while( ( retval = XmClipboardInquireLength( xdisplay, xwindow,
463 id.char_str(),
464 &length ) )
465 == XmClipboardLocked );
466 if( retval != XmClipboardSuccess )
467 return false;
468
469 wxCharBuffer buf(length);
470
471 while( ( retval = XmClipboardRetrieve( xdisplay, xwindow,
472 id.char_str(),
473 (XtPointer)buf.data(),
474 length, &dummy1, &dummy2 ) )
475 == XmClipboardLocked );
476 if( retval != XmClipboardSuccess )
477 return false;
478
479 if( !data.SetData( chosenFormat, length, buf.data() ) )
480 return false;
481
482 return true;
483 }
484
485 #endif // wxUSE_CLIPBOARD