1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/x11/clipbrd.cpp
3 // Purpose: Clipboard functionality
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // for compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
16 #include "wx/clipbrd.h"
21 #include "wx/dataobj.h"
24 #include "wx/x11/private.h"
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
31 Atom g_clipboardAtom
= 0;
32 Atom g_targetsAtom
= 0;
35 // avoid warnings about unused static variable
36 #if defined(__WXDEBUG__) && defined(HAVE_VARIADIC_MACROS)
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");
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 /* The contents of a selection are returned in a GtkSelectionData
50 structure. selection/target identify the request.
51 type specifies the type of the return; if length < 0, and
52 the data should be ignored. This structure has object semantics -
53 no fields should be modified directly, they should not be created
54 directly, and pointers to them should not be stored beyond the duration of
55 a callback. (If the last is changed, we'll need to add reference
58 struct _GtkSelectionData
70 //-----------------------------------------------------------------------------
71 // "selection_received" for targets
72 //-----------------------------------------------------------------------------
77 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
78 GtkSelectionData
*selection_data
,
79 #if (GTK_MINOR_VERSION > 0)
80 guint32
WXUNUSED(time
),
82 wxClipboard
*clipboard
)
84 if ( wxTheClipboard
&& selection_data
->length
> 0 )
86 /* make sure we got the data in the correct form */
87 GdkAtom type
= selection_data
->type
;
88 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
90 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
92 wxLogTrace( TRACE_CLIPBOARD
,
93 _T("got unsupported clipboard target") );
95 clipboard
->m_waiting
= false;
101 wxDataFormat
clip( selection_data
->selection
);
102 wxLogTrace( TRACE_CLIPBOARD
,
103 wxT("selection received for targets, clipboard %s"),
104 clip
.GetId().c_str() );
105 #endif // __WXDEBUG__
107 // the atoms we received, holding a list of targets (= formats)
108 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
110 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
112 wxDataFormat
format( atoms
[i
] );
114 wxLogTrace( TRACE_CLIPBOARD
,
115 wxT("selection received for targets, format %s"),
116 format
.GetId().c_str() );
118 if (format
== clipboard
->m_targetRequested
)
120 clipboard
->m_waiting
= false;
121 clipboard
->m_formatSupported
= true;
127 clipboard
->m_waiting
= false;
130 //-----------------------------------------------------------------------------
131 // "selection_received" for the actual data
132 //-----------------------------------------------------------------------------
135 selection_received( GtkWidget
*WXUNUSED(widget
),
136 GtkSelectionData
*selection_data
,
137 #if (GTK_MINOR_VERSION > 0)
138 guint32
WXUNUSED(time
),
140 wxClipboard
*clipboard
)
144 clipboard
->m_waiting
= false;
148 wxDataObject
*data_object
= clipboard
->m_receivedData
;
152 clipboard
->m_waiting
= false;
156 if (selection_data
->length
<= 0)
158 clipboard
->m_waiting
= false;
162 wxDataFormat
format( selection_data
->target
);
164 /* make sure we got the data in the correct format */
165 if (!data_object
->IsSupportedFormat( format
) )
167 clipboard
->m_waiting
= false;
171 /* make sure we got the data in the correct form (selection type).
172 if so, copy data to target object */
173 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
175 clipboard
->m_waiting
= false;
179 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
181 wxTheClipboard
->m_formatSupported
= true;
182 clipboard
->m_waiting
= false;
185 //-----------------------------------------------------------------------------
187 //-----------------------------------------------------------------------------
190 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
192 if (!wxTheClipboard
) return TRUE
;
194 if (event
->selection
== GDK_SELECTION_PRIMARY
)
196 wxTheClipboard
->m_ownsPrimarySelection
= false;
199 if (event
->selection
== g_clipboardAtom
)
201 wxTheClipboard
->m_ownsClipboard
= false;
205 wxTheClipboard
->m_waiting
= false;
209 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
210 (!wxTheClipboard
->m_ownsClipboard
))
212 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
213 if (wxTheClipboard
->m_data
)
215 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
217 delete wxTheClipboard
->m_data
;
218 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
222 wxTheClipboard
->m_waiting
= false;
226 //-----------------------------------------------------------------------------
227 // selection handler for supplying data
228 //-----------------------------------------------------------------------------
231 selection_handler( GtkWidget
*WXUNUSED(widget
),
232 GtkSelectionData
*selection_data
,
233 guint
WXUNUSED(info
),
234 guint
WXUNUSED(time
),
235 gpointer
WXUNUSED(data
) )
237 if (!wxTheClipboard
) return;
239 if (!wxTheClipboard
->m_data
) return;
241 wxDataObject
*data
= wxTheClipboard
->m_data
;
243 wxDataFormat
format( selection_data
->target
);
245 if (!data
->IsSupportedFormat( format
)) return;
247 int size
= data
->GetDataSize( format
);
249 if (size
== 0) return;
251 void *d
= malloc(size
);
253 data
->GetDataHere( selection_data
->target
, d
);
255 // transform Unicode text into multibyte before putting it on clipboard
257 if ( format
.GetType() == wxDF_TEXT
|| format
.GetType() == wxDF_UNICODETEXT
)
259 const wchar_t *wstr
= (const wchar_t *)d
;
260 size_t len
= wxConvCurrent
->WC2MB(NULL
, wstr
, 0);
261 char *str
= malloc(len
+ 1);
262 wxConvCurrent
->WC2MB(str
, wstr
, len
);
268 #endif // wxUSE_UNICODE
270 gtk_selection_data_set(
272 GDK_SELECTION_TYPE_STRING
,
282 //-----------------------------------------------------------------------------
284 //-----------------------------------------------------------------------------
286 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
288 wxClipboard::wxClipboard()
292 m_ownsClipboard
= false;
293 m_ownsPrimarySelection
= false;
295 m_data
= (wxDataObject
*) NULL
;
296 m_receivedData
= (wxDataObject
*) NULL
;
298 /* we use m_targetsWidget to query what formats are available */
300 /* we use m_clipboardWidget to get and to offer data */
302 if (!g_clipboardAtom
) g_clipboardAtom
= XInternAtom( (Display
*) wxGetDisplay(), "CLIPBOARD", False
);
303 if (!g_targetsAtom
) g_targetsAtom
= XInternAtom( (Display
*) wxGetDisplay(), "TARGETS", False
);
306 m_formatSupported
= false;
307 m_targetRequested
= 0;
309 m_usePrimary
= false;
312 wxClipboard::~wxClipboard()
316 // if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
317 // if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
320 void wxClipboard::Clear()
325 /* disable GUI threads */
328 /* As we have data we also own the clipboard. Once we no longer own
329 it, clear_selection is called which will set m_data to zero */
331 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
335 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
336 (guint32
) GDK_CURRENT_TIME
);
338 while (m_waiting
) gtk_main_iteration();
341 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
345 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
346 (guint32
) GDK_CURRENT_TIME
);
348 while (m_waiting
) gtk_main_iteration();
355 m_data
= (wxDataObject
*) NULL
;
359 /* re-enable GUI threads */
363 m_targetRequested
= 0;
364 m_formatSupported
= false;
367 bool wxClipboard::Open()
369 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
376 bool wxClipboard::SetData( wxDataObject
*data
)
378 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
380 wxCHECK_MSG( data
, false, wxT("data is invalid") );
384 return AddData( data
);
387 bool wxClipboard::AddData( wxDataObject
*data
)
392 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
394 wxCHECK_MSG( data
, false, wxT("data is invalid") );
396 /* we can only store one wxDataObject */
401 /* get formats from wxDataObjects */
402 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
403 m_data
->GetAllFormats( array
);
406 /* primary selection or clipboard */
407 Atom clipboard
= m_usePrimary
? (Atom
) 1 // 1 = primary selection
412 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
414 wxLogTrace( TRACE_CLIPBOARD
,
415 wxT("wxClipboard now supports atom %s"),
416 array
[i
].GetId().c_str() );
419 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
422 0 ); /* what is info ? */
429 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
431 GTK_SIGNAL_FUNC(selection_handler
),
436 /* disable GUI threads */
441 /* Tell the world we offer clipboard data */
442 res
= (gtk_selection_owner_set( m_clipboardWidget
,
444 (guint32
) GDK_CURRENT_TIME
));
448 m_ownsPrimarySelection
= res
;
450 m_ownsClipboard
= res
;
453 /* re-enable GUI threads */
460 void wxClipboard::Close()
462 wxCHECK_RET( m_open
, wxT("clipboard not open") );
467 bool wxClipboard::IsOpened() const
472 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
474 /* reentrance problems */
475 if (m_waiting
) return false;
477 /* store requested format to be asked for by callbacks */
478 m_targetRequested
= format
;
481 wxLogTrace( TRACE_CLIPBOARD
,
482 wxT("wxClipboard:IsSupported: requested format: %s"),
483 format
.GetId().c_str() );
486 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
488 m_formatSupported
= false;
490 /* perform query. this will set m_formatSupported to
491 true if m_targetRequested is supported.
492 also, we have to wait for the "answer" from the
493 clipboard owner which is an asynchronous process.
494 therefore we set m_waiting = true here and wait
495 until the callback "targets_selection_received"
501 gtk_selection_convert( m_targetsWidget
,
502 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
505 (guint32
) GDK_CURRENT_TIME
);
507 while (m_waiting
) gtk_main_iteration();
510 if (!m_formatSupported
) return false;
515 bool wxClipboard::GetData( wxDataObject
& data
)
517 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
519 /* get formats from wxDataObjects */
520 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
521 data
.GetAllFormats( array
);
523 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
525 wxDataFormat
format( array
[i
] );
527 wxLogTrace( TRACE_CLIPBOARD
,
528 wxT("wxClipboard::GetData: requested format: %s"),
529 format
.GetId().c_str() );
531 /* is data supported by clipboard ? */
533 /* store requested format to be asked for by callbacks */
534 m_targetRequested
= format
;
536 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
538 m_formatSupported
= false;
540 /* perform query. this will set m_formatSupported to
541 true if m_targetRequested is supported.
542 also, we have to wait for the "answer" from the
543 clipboard owner which is an asynchronous process.
544 therefore we set m_waiting = true here and wait
545 until the callback "targets_selection_received"
551 gtk_selection_convert( m_targetsWidget
,
552 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
555 (guint32
) GDK_CURRENT_TIME
);
557 while (m_waiting
) gtk_main_iteration();
560 if (!m_formatSupported
) continue;
562 /* store pointer to data object to be filled up by callbacks */
563 m_receivedData
= &data
;
565 /* store requested format to be asked for by callbacks */
566 m_targetRequested
= format
;
568 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
571 m_formatSupported
= false;
573 /* ask for clipboard contents. this will set
574 m_formatSupported to true if m_targetRequested
576 also, we have to wait for the "answer" from the
577 clipboard owner which is an asynchronous process.
578 therefore we set m_waiting = true here and wait
579 until the callback "targets_selection_received"
584 wxLogTrace( TRACE_CLIPBOARD
,
585 wxT("wxClipboard::GetData: format found, start convert") );
588 gtk_selection_convert( m_clipboardWidget
,
589 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
592 (guint32
) GDK_CURRENT_TIME
);
594 while (m_waiting
) gtk_main_iteration();
597 /* this is a true error as we checked for the presence of such data before */
598 wxCHECK_MSG( m_formatSupported
, false, wxT("error retrieving data from clipboard") );
605 wxLogTrace( TRACE_CLIPBOARD
,
606 wxT("wxClipboard::GetData: format not found") );