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