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 wxDELETE(wxTheClipboard
->m_data
);
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;
296 m_receivedData
= 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;
310 wxClipboard::~wxClipboard()
314 // if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
315 // if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
318 void wxClipboard::Clear()
323 /* disable GUI threads */
326 /* As we have data we also own the clipboard. Once we no longer own
327 it, clear_selection is called which will set m_data to zero */
329 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
333 gtk_selection_owner_set( NULL
, g_clipboardAtom
,
334 (guint32
) GDK_CURRENT_TIME
);
336 while (m_waiting
) gtk_main_iteration();
339 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
343 gtk_selection_owner_set( NULL
, GDK_SELECTION_PRIMARY
,
344 (guint32
) GDK_CURRENT_TIME
);
346 while (m_waiting
) gtk_main_iteration();
353 /* re-enable GUI threads */
357 m_targetRequested
= 0;
358 m_formatSupported
= false;
361 bool wxClipboard::Open()
363 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
370 bool wxClipboard::SetData( wxDataObject
*data
)
372 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
374 wxCHECK_MSG( data
, false, wxT("data is invalid") );
378 return AddData( data
);
381 bool wxClipboard::AddData( wxDataObject
*data
)
386 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
388 wxCHECK_MSG( data
, false, wxT("data is invalid") );
390 /* we can only store one wxDataObject */
395 /* get formats from wxDataObjects */
396 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
397 m_data
->GetAllFormats( array
);
400 /* primary selection or clipboard */
401 Atom clipboard
= m_usePrimary
? (Atom
) 1 // 1 = primary selection
406 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
408 wxLogTrace( TRACE_CLIPBOARD
,
409 wxT("wxClipboard now supports atom %s"),
410 array
[i
].GetId().c_str() );
413 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
416 0 ); /* what is info ? */
423 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
425 GTK_SIGNAL_FUNC(selection_handler
),
430 /* disable GUI threads */
435 /* Tell the world we offer clipboard data */
436 res
= (gtk_selection_owner_set( m_clipboardWidget
,
438 (guint32
) GDK_CURRENT_TIME
));
442 m_ownsPrimarySelection
= res
;
444 m_ownsClipboard
= res
;
447 /* re-enable GUI threads */
454 void wxClipboard::Close()
456 wxCHECK_RET( m_open
, wxT("clipboard not open") );
461 bool wxClipboard::IsOpened() const
466 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
468 /* reentrance problems */
469 if (m_waiting
) return false;
471 /* store requested format to be asked for by callbacks */
472 m_targetRequested
= format
;
475 wxLogTrace( TRACE_CLIPBOARD
,
476 wxT("wxClipboard:IsSupported: requested format: %s"),
477 format
.GetId().c_str() );
480 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
482 m_formatSupported
= false;
484 /* perform query. this will set m_formatSupported to
485 true if m_targetRequested is supported.
486 also, we have to wait for the "answer" from the
487 clipboard owner which is an asynchronous process.
488 therefore we set m_waiting = true here and wait
489 until the callback "targets_selection_received"
495 gtk_selection_convert( m_targetsWidget
,
496 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
499 (guint32
) GDK_CURRENT_TIME
);
501 while (m_waiting
) gtk_main_iteration();
504 if (!m_formatSupported
) return false;
509 bool wxClipboard::GetData( wxDataObject
& data
)
511 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
513 /* get formats from wxDataObjects */
514 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
515 data
.GetAllFormats( array
);
517 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
519 wxDataFormat
format( array
[i
] );
521 wxLogTrace( TRACE_CLIPBOARD
,
522 wxT("wxClipboard::GetData: requested format: %s"),
523 format
.GetId().c_str() );
525 /* is data supported by clipboard ? */
527 /* store requested format to be asked for by callbacks */
528 m_targetRequested
= format
;
530 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
532 m_formatSupported
= false;
534 /* perform query. this will set m_formatSupported to
535 true if m_targetRequested is supported.
536 also, we have to wait for the "answer" from the
537 clipboard owner which is an asynchronous process.
538 therefore we set m_waiting = true here and wait
539 until the callback "targets_selection_received"
545 gtk_selection_convert( m_targetsWidget
,
546 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
549 (guint32
) GDK_CURRENT_TIME
);
551 while (m_waiting
) gtk_main_iteration();
554 if (!m_formatSupported
) continue;
556 /* store pointer to data object to be filled up by callbacks */
557 m_receivedData
= &data
;
559 /* store requested format to be asked for by callbacks */
560 m_targetRequested
= format
;
562 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
565 m_formatSupported
= false;
567 /* ask for clipboard contents. this will set
568 m_formatSupported to true if m_targetRequested
570 also, we have to wait for the "answer" from the
571 clipboard owner which is an asynchronous process.
572 therefore we set m_waiting = true here and wait
573 until the callback "targets_selection_received"
578 wxLogTrace( TRACE_CLIPBOARD
,
579 wxT("wxClipboard::GetData: format found, start convert") );
582 gtk_selection_convert( m_clipboardWidget
,
583 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
586 (guint32
) GDK_CURRENT_TIME
);
588 while (m_waiting
) gtk_main_iteration();
591 /* this is a true error as we checked for the presence of such data before */
592 wxCHECK_MSG( m_formatSupported
, false, wxT("error retrieving data from clipboard") );
599 wxLogTrace( TRACE_CLIPBOARD
,
600 wxT("wxClipboard::GetData: format not found") );