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 (notice that we still use it
36 // even in release build if the compiler doesn't support variadic macros)
37 #if defined(__WXDEBUG__) || !defined(HAVE_VARIADIC_MACROS)
39 // the trace mask we use with wxLogTrace() - call
40 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
41 // (there will be a *lot* of them!)
42 static const wxChar
*TRACE_CLIPBOARD
= wxT("clipboard");
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 /* The contents of a selection are returned in a GtkSelectionData
51 structure. selection/target identify the request.
52 type specifies the type of the return; if length < 0, and
53 the data should be ignored. This structure has object semantics -
54 no fields should be modified directly, they should not be created
55 directly, and pointers to them should not be stored beyond the duration of
56 a callback. (If the last is changed, we'll need to add reference
59 struct _GtkSelectionData
71 //-----------------------------------------------------------------------------
72 // "selection_received" for targets
73 //-----------------------------------------------------------------------------
78 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
79 GtkSelectionData
*selection_data
,
80 #if (GTK_MINOR_VERSION > 0)
81 guint32
WXUNUSED(time
),
83 wxClipboard
*clipboard
)
85 if ( wxTheClipboard
&& selection_data
->length
> 0 )
87 /* make sure we got the data in the correct form */
88 GdkAtom type
= selection_data
->type
;
89 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
91 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
93 wxLogTrace( TRACE_CLIPBOARD
,
94 wxT("got unsupported clipboard target") );
96 clipboard
->m_waiting
= false;
102 wxDataFormat
clip( selection_data
->selection
);
103 wxLogTrace( TRACE_CLIPBOARD
,
104 wxT("selection received for targets, clipboard %s"),
105 clip
.GetId().c_str() );
106 #endif // __WXDEBUG__
108 // the atoms we received, holding a list of targets (= formats)
109 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
111 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
113 wxDataFormat
format( atoms
[i
] );
115 wxLogTrace( TRACE_CLIPBOARD
,
116 wxT("selection received for targets, format %s"),
117 format
.GetId().c_str() );
119 if (format
== clipboard
->m_targetRequested
)
121 clipboard
->m_waiting
= false;
122 clipboard
->m_formatSupported
= true;
128 clipboard
->m_waiting
= false;
131 //-----------------------------------------------------------------------------
132 // "selection_received" for the actual data
133 //-----------------------------------------------------------------------------
136 selection_received( GtkWidget
*WXUNUSED(widget
),
137 GtkSelectionData
*selection_data
,
138 #if (GTK_MINOR_VERSION > 0)
139 guint32
WXUNUSED(time
),
141 wxClipboard
*clipboard
)
145 clipboard
->m_waiting
= false;
149 wxDataObject
*data_object
= clipboard
->m_receivedData
;
153 clipboard
->m_waiting
= false;
157 if (selection_data
->length
<= 0)
159 clipboard
->m_waiting
= false;
163 wxDataFormat
format( selection_data
->target
);
165 /* make sure we got the data in the correct format */
166 if (!data_object
->IsSupportedFormat( format
) )
168 clipboard
->m_waiting
= false;
172 /* make sure we got the data in the correct form (selection type).
173 if so, copy data to target object */
174 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
176 clipboard
->m_waiting
= false;
180 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
182 wxTheClipboard
->m_formatSupported
= true;
183 clipboard
->m_waiting
= false;
186 //-----------------------------------------------------------------------------
188 //-----------------------------------------------------------------------------
191 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
193 if (!wxTheClipboard
) return TRUE
;
195 if (event
->selection
== GDK_SELECTION_PRIMARY
)
197 wxTheClipboard
->m_ownsPrimarySelection
= false;
200 if (event
->selection
== g_clipboardAtom
)
202 wxTheClipboard
->m_ownsClipboard
= false;
206 wxTheClipboard
->m_waiting
= false;
210 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
211 (!wxTheClipboard
->m_ownsClipboard
))
213 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
214 if (wxTheClipboard
->m_data
)
216 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
218 delete wxTheClipboard
->m_data
;
219 wxTheClipboard
->m_data
= NULL
;
223 wxTheClipboard
->m_waiting
= false;
227 //-----------------------------------------------------------------------------
228 // selection handler for supplying data
229 //-----------------------------------------------------------------------------
232 selection_handler( GtkWidget
*WXUNUSED(widget
),
233 GtkSelectionData
*selection_data
,
234 guint
WXUNUSED(info
),
235 guint
WXUNUSED(time
),
236 gpointer
WXUNUSED(data
) )
238 if (!wxTheClipboard
) return;
240 if (!wxTheClipboard
->m_data
) return;
242 wxDataObject
*data
= wxTheClipboard
->m_data
;
244 wxDataFormat
format( selection_data
->target
);
246 if (!data
->IsSupportedFormat( format
)) return;
248 int size
= data
->GetDataSize( format
);
250 if (size
== 0) return;
252 void *d
= malloc(size
);
254 data
->GetDataHere( selection_data
->target
, d
);
256 // transform Unicode text into multibyte before putting it on clipboard
258 if ( format
.GetType() == wxDF_TEXT
|| format
.GetType() == wxDF_UNICODETEXT
)
260 const wchar_t *wstr
= (const wchar_t *)d
;
261 size_t len
= wxConvCurrent
->WC2MB(NULL
, wstr
, 0);
262 char *str
= malloc(len
+ 1);
263 wxConvCurrent
->WC2MB(str
, wstr
, len
);
269 #endif // wxUSE_UNICODE
271 gtk_selection_data_set(
273 GDK_SELECTION_TYPE_STRING
,
283 //-----------------------------------------------------------------------------
285 //-----------------------------------------------------------------------------
287 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
289 wxClipboard::wxClipboard()
293 m_ownsClipboard
= false;
294 m_ownsPrimarySelection
= false;
297 m_receivedData
= NULL
;
299 /* we use m_targetsWidget to query what formats are available */
301 /* we use m_clipboardWidget to get and to offer data */
303 if (!g_clipboardAtom
) g_clipboardAtom
= XInternAtom( (Display
*) wxGetDisplay(), "CLIPBOARD", False
);
304 if (!g_targetsAtom
) g_targetsAtom
= XInternAtom( (Display
*) wxGetDisplay(), "TARGETS", False
);
307 m_formatSupported
= false;
308 m_targetRequested
= 0;
311 wxClipboard::~wxClipboard()
315 // if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
316 // if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
319 void wxClipboard::Clear()
324 /* disable GUI threads */
327 /* As we have data we also own the clipboard. Once we no longer own
328 it, clear_selection is called which will set m_data to zero */
330 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
334 gtk_selection_owner_set( NULL
, g_clipboardAtom
,
335 (guint32
) GDK_CURRENT_TIME
);
337 while (m_waiting
) gtk_main_iteration();
340 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
344 gtk_selection_owner_set( NULL
, GDK_SELECTION_PRIMARY
,
345 (guint32
) GDK_CURRENT_TIME
);
347 while (m_waiting
) gtk_main_iteration();
358 /* re-enable GUI threads */
362 m_targetRequested
= 0;
363 m_formatSupported
= false;
366 bool wxClipboard::Open()
368 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
375 bool wxClipboard::SetData( wxDataObject
*data
)
377 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
379 wxCHECK_MSG( data
, false, wxT("data is invalid") );
383 return AddData( data
);
386 bool wxClipboard::AddData( wxDataObject
*data
)
391 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
393 wxCHECK_MSG( data
, false, wxT("data is invalid") );
395 /* we can only store one wxDataObject */
400 /* get formats from wxDataObjects */
401 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
402 m_data
->GetAllFormats( array
);
405 /* primary selection or clipboard */
406 Atom clipboard
= m_usePrimary
? (Atom
) 1 // 1 = primary selection
411 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
413 wxLogTrace( TRACE_CLIPBOARD
,
414 wxT("wxClipboard now supports atom %s"),
415 array
[i
].GetId().c_str() );
418 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
421 0 ); /* what is info ? */
428 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
430 GTK_SIGNAL_FUNC(selection_handler
),
435 /* disable GUI threads */
440 /* Tell the world we offer clipboard data */
441 res
= (gtk_selection_owner_set( m_clipboardWidget
,
443 (guint32
) GDK_CURRENT_TIME
));
447 m_ownsPrimarySelection
= res
;
449 m_ownsClipboard
= res
;
452 /* re-enable GUI threads */
459 void wxClipboard::Close()
461 wxCHECK_RET( m_open
, wxT("clipboard not open") );
466 bool wxClipboard::IsOpened() const
471 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
473 /* reentrance problems */
474 if (m_waiting
) return false;
476 /* store requested format to be asked for by callbacks */
477 m_targetRequested
= format
;
480 wxLogTrace( TRACE_CLIPBOARD
,
481 wxT("wxClipboard:IsSupported: requested format: %s"),
482 format
.GetId().c_str() );
485 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
487 m_formatSupported
= false;
489 /* perform query. this will set m_formatSupported to
490 true if m_targetRequested is supported.
491 also, we have to wait for the "answer" from the
492 clipboard owner which is an asynchronous process.
493 therefore we set m_waiting = true here and wait
494 until the callback "targets_selection_received"
500 gtk_selection_convert( m_targetsWidget
,
501 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
504 (guint32
) GDK_CURRENT_TIME
);
506 while (m_waiting
) gtk_main_iteration();
509 if (!m_formatSupported
) return false;
514 bool wxClipboard::GetData( wxDataObject
& data
)
516 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
518 /* get formats from wxDataObjects */
519 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
520 data
.GetAllFormats( array
);
522 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
524 wxDataFormat
format( array
[i
] );
526 wxLogTrace( TRACE_CLIPBOARD
,
527 wxT("wxClipboard::GetData: requested format: %s"),
528 format
.GetId().c_str() );
530 /* is data supported by clipboard ? */
532 /* store requested format to be asked for by callbacks */
533 m_targetRequested
= format
;
535 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
537 m_formatSupported
= false;
539 /* perform query. this will set m_formatSupported to
540 true if m_targetRequested is supported.
541 also, we have to wait for the "answer" from the
542 clipboard owner which is an asynchronous process.
543 therefore we set m_waiting = true here and wait
544 until the callback "targets_selection_received"
550 gtk_selection_convert( m_targetsWidget
,
551 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
554 (guint32
) GDK_CURRENT_TIME
);
556 while (m_waiting
) gtk_main_iteration();
559 if (!m_formatSupported
) continue;
561 /* store pointer to data object to be filled up by callbacks */
562 m_receivedData
= &data
;
564 /* store requested format to be asked for by callbacks */
565 m_targetRequested
= format
;
567 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
570 m_formatSupported
= false;
572 /* ask for clipboard contents. this will set
573 m_formatSupported to true if m_targetRequested
575 also, we have to wait for the "answer" from the
576 clipboard owner which is an asynchronous process.
577 therefore we set m_waiting = true here and wait
578 until the callback "targets_selection_received"
583 wxLogTrace( TRACE_CLIPBOARD
,
584 wxT("wxClipboard::GetData: format found, start convert") );
587 gtk_selection_convert( m_clipboardWidget
,
588 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
591 (guint32
) GDK_CURRENT_TIME
);
593 while (m_waiting
) gtk_main_iteration();
596 /* this is a true error as we checked for the presence of such data before */
597 wxCHECK_MSG( m_formatSupported
, false, wxT("error retrieving data from clipboard") );
604 wxLogTrace( TRACE_CLIPBOARD
,
605 wxT("wxClipboard::GetData: format not found") );