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