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;
36 // the trace mask we use with wxLogTrace() - call
37 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
38 // (there will be a *lot* of them!)
39 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 /* The contents of a selection are returned in a GtkSelectionData
47 structure. selection/target identify the request.
48 type specifies the type of the return; if length < 0, and
49 the data should be ignored. This structure has object semantics -
50 no fields should be modified directly, they should not be created
51 directly, and pointers to them should not be stored beyond the duration of
52 a callback. (If the last is changed, we'll need to add reference
55 struct _GtkSelectionData
67 //-----------------------------------------------------------------------------
68 // "selection_received" for targets
69 //-----------------------------------------------------------------------------
74 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
75 GtkSelectionData
*selection_data
,
76 #if (GTK_MINOR_VERSION > 0)
77 guint32
WXUNUSED(time
),
79 wxClipboard
*clipboard
)
81 if ( wxTheClipboard
&& selection_data
->length
> 0 )
83 /* make sure we got the data in the correct form */
84 GdkAtom type
= selection_data
->type
;
85 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
87 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
89 wxLogTrace( TRACE_CLIPBOARD
,
90 _T("got unsupported clipboard target") );
92 clipboard
->m_waiting
= false;
98 wxDataFormat
clip( selection_data
->selection
);
99 wxLogTrace( TRACE_CLIPBOARD
,
100 wxT("selection received for targets, clipboard %s"),
101 clip
.GetId().c_str() );
102 #endif // __WXDEBUG__
104 // the atoms we received, holding a list of targets (= formats)
105 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
107 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
109 wxDataFormat
format( atoms
[i
] );
111 wxLogTrace( TRACE_CLIPBOARD
,
112 wxT("selection received for targets, format %s"),
113 format
.GetId().c_str() );
115 if (format
== clipboard
->m_targetRequested
)
117 clipboard
->m_waiting
= false;
118 clipboard
->m_formatSupported
= true;
124 clipboard
->m_waiting
= false;
127 //-----------------------------------------------------------------------------
128 // "selection_received" for the actual data
129 //-----------------------------------------------------------------------------
132 selection_received( GtkWidget
*WXUNUSED(widget
),
133 GtkSelectionData
*selection_data
,
134 #if (GTK_MINOR_VERSION > 0)
135 guint32
WXUNUSED(time
),
137 wxClipboard
*clipboard
)
141 clipboard
->m_waiting
= false;
145 wxDataObject
*data_object
= clipboard
->m_receivedData
;
149 clipboard
->m_waiting
= false;
153 if (selection_data
->length
<= 0)
155 clipboard
->m_waiting
= false;
159 wxDataFormat
format( selection_data
->target
);
161 /* make sure we got the data in the correct format */
162 if (!data_object
->IsSupportedFormat( format
) )
164 clipboard
->m_waiting
= false;
168 /* make sure we got the data in the correct form (selection type).
169 if so, copy data to target object */
170 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
172 clipboard
->m_waiting
= false;
176 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
178 wxTheClipboard
->m_formatSupported
= true;
179 clipboard
->m_waiting
= false;
182 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
187 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
189 if (!wxTheClipboard
) return TRUE
;
191 if (event
->selection
== GDK_SELECTION_PRIMARY
)
193 wxTheClipboard
->m_ownsPrimarySelection
= false;
196 if (event
->selection
== g_clipboardAtom
)
198 wxTheClipboard
->m_ownsClipboard
= false;
202 wxTheClipboard
->m_waiting
= false;
206 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
207 (!wxTheClipboard
->m_ownsClipboard
))
209 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
210 if (wxTheClipboard
->m_data
)
212 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
214 delete wxTheClipboard
->m_data
;
215 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
219 wxTheClipboard
->m_waiting
= false;
223 //-----------------------------------------------------------------------------
224 // selection handler for supplying data
225 //-----------------------------------------------------------------------------
228 selection_handler( GtkWidget
*WXUNUSED(widget
),
229 GtkSelectionData
*selection_data
,
230 guint
WXUNUSED(info
),
231 guint
WXUNUSED(time
),
232 gpointer
WXUNUSED(data
) )
234 if (!wxTheClipboard
) return;
236 if (!wxTheClipboard
->m_data
) return;
238 wxDataObject
*data
= wxTheClipboard
->m_data
;
240 wxDataFormat
format( selection_data
->target
);
242 if (!data
->IsSupportedFormat( format
)) return;
244 int size
= data
->GetDataSize( format
);
246 if (size
== 0) return;
248 void *d
= malloc(size
);
250 data
->GetDataHere( selection_data
->target
, d
);
252 // transform Unicode text into multibyte before putting it on clipboard
254 if ( format
.GetType() == wxDF_TEXT
|| format
.GetType() == wxDF_UNICODETEXT
)
256 const wchar_t *wstr
= (const wchar_t *)d
;
257 size_t len
= wxConvCurrent
->WC2MB(NULL
, wstr
, 0);
258 char *str
= malloc(len
+ 1);
259 wxConvCurrent
->WC2MB(str
, wstr
, len
);
265 #endif // wxUSE_UNICODE
267 gtk_selection_data_set(
269 GDK_SELECTION_TYPE_STRING
,
279 //-----------------------------------------------------------------------------
281 //-----------------------------------------------------------------------------
283 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
285 wxClipboard::wxClipboard()
289 m_ownsClipboard
= false;
290 m_ownsPrimarySelection
= false;
292 m_data
= (wxDataObject
*) NULL
;
293 m_receivedData
= (wxDataObject
*) NULL
;
295 /* we use m_targetsWidget to query what formats are available */
297 /* we use m_clipboardWidget to get and to offer data */
299 if (!g_clipboardAtom
) g_clipboardAtom
= XInternAtom( (Display
*) wxGetDisplay(), "CLIPBOARD", False
);
300 if (!g_targetsAtom
) g_targetsAtom
= XInternAtom( (Display
*) wxGetDisplay(), "TARGETS", False
);
303 m_formatSupported
= false;
304 m_targetRequested
= 0;
306 m_usePrimary
= false;
309 wxClipboard::~wxClipboard()
313 // if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
314 // if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
317 void wxClipboard::Clear()
322 /* disable GUI threads */
325 /* As we have data we also own the clipboard. Once we no longer own
326 it, clear_selection is called which will set m_data to zero */
328 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
332 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
333 (guint32
) GDK_CURRENT_TIME
);
335 while (m_waiting
) gtk_main_iteration();
338 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
342 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
343 (guint32
) GDK_CURRENT_TIME
);
345 while (m_waiting
) gtk_main_iteration();
352 m_data
= (wxDataObject
*) NULL
;
356 /* re-enable GUI threads */
360 m_targetRequested
= 0;
361 m_formatSupported
= false;
364 bool wxClipboard::Open()
366 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
373 bool wxClipboard::SetData( wxDataObject
*data
)
375 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
377 wxCHECK_MSG( data
, false, wxT("data is invalid") );
381 return AddData( data
);
384 bool wxClipboard::AddData( wxDataObject
*data
)
389 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
391 wxCHECK_MSG( data
, false, wxT("data is invalid") );
393 /* we can only store one wxDataObject */
398 /* get formats from wxDataObjects */
399 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
400 m_data
->GetAllFormats( array
);
403 /* primary selection or clipboard */
404 Atom clipboard
= m_usePrimary
? (Atom
) 1 // 1 = primary selection
409 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
411 wxLogTrace( TRACE_CLIPBOARD
,
412 wxT("wxClipboard now supports atom %s"),
413 array
[i
].GetId().c_str() );
416 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
419 0 ); /* what is info ? */
426 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
428 GTK_SIGNAL_FUNC(selection_handler
),
433 /* disable GUI threads */
438 /* Tell the world we offer clipboard data */
439 res
= (gtk_selection_owner_set( m_clipboardWidget
,
441 (guint32
) GDK_CURRENT_TIME
));
445 m_ownsPrimarySelection
= res
;
447 m_ownsClipboard
= res
;
450 /* re-enable GUI threads */
457 void wxClipboard::Close()
459 wxCHECK_RET( m_open
, wxT("clipboard not open") );
464 bool wxClipboard::IsOpened() const
469 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
471 /* reentrance problems */
472 if (m_waiting
) return false;
474 /* store requested format to be asked for by callbacks */
475 m_targetRequested
= format
;
478 wxLogTrace( TRACE_CLIPBOARD
,
479 wxT("wxClipboard:IsSupported: requested format: %s"),
480 format
.GetId().c_str() );
483 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
485 m_formatSupported
= false;
487 /* perform query. this will set m_formatSupported to
488 true if m_targetRequested is supported.
489 also, we have to wait for the "answer" from the
490 clipboard owner which is an asynchronous process.
491 therefore we set m_waiting = true here and wait
492 until the callback "targets_selection_received"
498 gtk_selection_convert( m_targetsWidget
,
499 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
502 (guint32
) GDK_CURRENT_TIME
);
504 while (m_waiting
) gtk_main_iteration();
507 if (!m_formatSupported
) return false;
512 bool wxClipboard::GetData( wxDataObject
& data
)
514 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
516 /* get formats from wxDataObjects */
517 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
518 data
.GetAllFormats( array
);
520 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
522 wxDataFormat
format( array
[i
] );
524 wxLogTrace( TRACE_CLIPBOARD
,
525 wxT("wxClipboard::GetData: requested format: %s"),
526 format
.GetId().c_str() );
528 /* is data supported by clipboard ? */
530 /* store requested format to be asked for by callbacks */
531 m_targetRequested
= format
;
533 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
535 m_formatSupported
= false;
537 /* perform query. this will set m_formatSupported to
538 true if m_targetRequested is supported.
539 also, we have to wait for the "answer" from the
540 clipboard owner which is an asynchronous process.
541 therefore we set m_waiting = true here and wait
542 until the callback "targets_selection_received"
548 gtk_selection_convert( m_targetsWidget
,
549 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
552 (guint32
) GDK_CURRENT_TIME
);
554 while (m_waiting
) gtk_main_iteration();
557 if (!m_formatSupported
) continue;
559 /* store pointer to data object to be filled up by callbacks */
560 m_receivedData
= &data
;
562 /* store requested format to be asked for by callbacks */
563 m_targetRequested
= format
;
565 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
568 m_formatSupported
= false;
570 /* ask for clipboard contents. this will set
571 m_formatSupported to true if m_targetRequested
573 also, we have to wait for the "answer" from the
574 clipboard owner which is an asynchronous process.
575 therefore we set m_waiting = true here and wait
576 until the callback "targets_selection_received"
581 wxLogTrace( TRACE_CLIPBOARD
,
582 wxT("wxClipboard::GetData: format found, start convert") );
585 gtk_selection_convert( m_clipboardWidget
,
586 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
589 (guint32
) GDK_CURRENT_TIME
);
591 while (m_waiting
) gtk_main_iteration();
594 /* this is a true error as we checked for the presence of such data before */
595 wxCHECK_MSG( m_formatSupported
, false, wxT("error retrieving data from clipboard") );
602 wxLogTrace( TRACE_CLIPBOARD
,
603 wxT("wxClipboard::GetData: format not found") );