1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Clipboard functionality
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/clipbrd.h"
15 #include "wx/dataobj.h"
19 #include "wx/x11/private.h"
21 //-----------------------------------------------------------------------------
23 //-----------------------------------------------------------------------------
26 Atom g_clipboardAtom
= 0;
27 Atom g_targetsAtom
= 0;
30 // the trace mask we use with wxLogTrace() - call
31 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
32 // (there will be a *lot* of them!)
33 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
35 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
39 /* The contents of a selection are returned in a GtkSelectionData
40 structure. selection/target identify the request.
41 type specifies the type of the return; if length < 0, and
42 the data should be ignored. This structure has object semantics -
43 no fields should be modified directly, they should not be created
44 directly, and pointers to them should not be stored beyond the duration of
45 a callback. (If the last is changed, we'll need to add reference
48 struct _GtkSelectionData
60 //-----------------------------------------------------------------------------
61 // "selection_received" for targets
62 //-----------------------------------------------------------------------------
67 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
68 GtkSelectionData
*selection_data
,
69 #if (GTK_MINOR_VERSION > 0)
70 guint32
WXUNUSED(time
),
72 wxClipboard
*clipboard
)
74 if ( wxTheClipboard
&& selection_data
->length
> 0 )
76 /* make sure we got the data in the correct form */
77 GdkAtom type
= selection_data
->type
;
78 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
80 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
82 wxLogTrace( TRACE_CLIPBOARD
,
83 _T("got unsupported clipboard target") );
85 clipboard
->m_waiting
= FALSE
;
91 wxDataFormat
clip( selection_data
->selection
);
92 wxLogTrace( TRACE_CLIPBOARD
,
93 wxT("selection received for targets, clipboard %s"),
94 clip
.GetId().c_str() );
97 // the atoms we received, holding a list of targets (= formats)
98 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
100 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
102 wxDataFormat
format( atoms
[i
] );
104 wxLogTrace( TRACE_CLIPBOARD
,
105 wxT("selection received for targets, format %s"),
106 format
.GetId().c_str() );
108 if (format
== clipboard
->m_targetRequested
)
110 clipboard
->m_waiting
= FALSE
;
111 clipboard
->m_formatSupported
= TRUE
;
117 clipboard
->m_waiting
= FALSE
;
120 //-----------------------------------------------------------------------------
121 // "selection_received" for the actual data
122 //-----------------------------------------------------------------------------
125 selection_received( GtkWidget
*WXUNUSED(widget
),
126 GtkSelectionData
*selection_data
,
127 #if (GTK_MINOR_VERSION > 0)
128 guint32
WXUNUSED(time
),
130 wxClipboard
*clipboard
)
134 clipboard
->m_waiting
= FALSE
;
138 wxDataObject
*data_object
= clipboard
->m_receivedData
;
142 clipboard
->m_waiting
= FALSE
;
146 if (selection_data
->length
<= 0)
148 clipboard
->m_waiting
= FALSE
;
152 wxDataFormat
format( selection_data
->target
);
154 /* make sure we got the data in the correct format */
155 if (!data_object
->IsSupportedFormat( format
) )
157 clipboard
->m_waiting
= FALSE
;
161 /* make sure we got the data in the correct form (selection type).
162 if so, copy data to target object */
163 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
165 clipboard
->m_waiting
= FALSE
;
169 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
171 wxTheClipboard
->m_formatSupported
= TRUE
;
172 clipboard
->m_waiting
= FALSE
;
175 //-----------------------------------------------------------------------------
177 //-----------------------------------------------------------------------------
180 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
182 if (!wxTheClipboard
) return TRUE
;
184 if (event
->selection
== GDK_SELECTION_PRIMARY
)
186 wxTheClipboard
->m_ownsPrimarySelection
= FALSE
;
189 if (event
->selection
== g_clipboardAtom
)
191 wxTheClipboard
->m_ownsClipboard
= FALSE
;
195 wxTheClipboard
->m_waiting
= FALSE
;
199 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
200 (!wxTheClipboard
->m_ownsClipboard
))
202 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
203 if (wxTheClipboard
->m_data
)
205 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
207 delete wxTheClipboard
->m_data
;
208 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
212 wxTheClipboard
->m_waiting
= FALSE
;
216 //-----------------------------------------------------------------------------
217 // selection handler for supplying data
218 //-----------------------------------------------------------------------------
221 selection_handler( GtkWidget
*WXUNUSED(widget
),
222 GtkSelectionData
*selection_data
,
223 guint
WXUNUSED(info
),
224 guint
WXUNUSED(time
),
225 gpointer
WXUNUSED(data
) )
227 if (!wxTheClipboard
) return;
229 if (!wxTheClipboard
->m_data
) return;
231 wxDataObject
*data
= wxTheClipboard
->m_data
;
233 wxDataFormat
format( selection_data
->target
);
235 if (!data
->IsSupportedFormat( format
)) return;
237 int size
= data
->GetDataSize( format
);
239 if (size
== 0) return;
241 void *d
= malloc(size
);
243 data
->GetDataHere( selection_data
->target
, d
);
245 // transform Unicode text into multibyte before putting it on clipboard
247 if ( format
.GetType() == wxDF_TEXT
)
249 const wchar_t *wstr
= (const wchar_t *)d
;
250 size_t len
= wxConvCurrent
->WC2MB(NULL
, wstr
, 0);
251 char *str
= malloc(len
+ 1);
252 wxConvCurrent
->WC2MB(str
, wstr
, len
);
258 #endif // wxUSE_UNICODE
260 gtk_selection_data_set(
262 GDK_SELECTION_TYPE_STRING
,
272 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
276 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
278 wxClipboard::wxClipboard()
282 m_ownsClipboard
= FALSE
;
283 m_ownsPrimarySelection
= FALSE
;
285 m_data
= (wxDataObject
*) NULL
;
286 m_receivedData
= (wxDataObject
*) NULL
;
288 /* we use m_targetsWidget to query what formats are available */
290 /* we use m_clipboardWidget to get and to offer data */
292 if (!g_clipboardAtom
) g_clipboardAtom
= XInternAtom( (Display
*) wxGetDisplay(), "CLIPBOARD", False
);
293 if (!g_targetsAtom
) g_targetsAtom
= XInternAtom( (Display
*) wxGetDisplay(), "TARGETS", False
);
296 m_formatSupported
= FALSE
;
297 m_targetRequested
= 0;
299 m_usePrimary
= FALSE
;
302 wxClipboard::~wxClipboard()
306 // if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
307 // if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
310 void wxClipboard::Clear()
315 /* disable GUI threads */
318 /* As we have data we also own the clipboard. Once we no longer own
319 it, clear_selection is called which will set m_data to zero */
321 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
325 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
326 (guint32
) GDK_CURRENT_TIME
);
328 while (m_waiting
) gtk_main_iteration();
331 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
335 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
336 (guint32
) GDK_CURRENT_TIME
);
338 while (m_waiting
) gtk_main_iteration();
345 m_data
= (wxDataObject
*) NULL
;
349 /* re-enable GUI threads */
353 m_targetRequested
= 0;
354 m_formatSupported
= FALSE
;
357 bool wxClipboard::Open()
359 wxCHECK_MSG( !m_open
, FALSE
, wxT("clipboard already open") );
366 bool wxClipboard::SetData( wxDataObject
*data
)
368 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
370 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
374 return AddData( data
);
377 bool wxClipboard::AddData( wxDataObject
*data
)
382 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
384 wxCHECK_MSG( data
, FALSE
, wxT("data is invalid") );
386 /* we can only store one wxDataObject */
391 /* get formats from wxDataObjects */
392 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
393 m_data
->GetAllFormats( array
);
396 /* primary selection or clipboard */
397 Atom clipboard
= m_usePrimary
? (Atom
) 1 // 1 = primary selection
402 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
404 wxLogTrace( TRACE_CLIPBOARD
,
405 wxT("wxClipboard now supports atom %s"),
406 array
[i
].GetId().c_str() );
409 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
412 0 ); /* what is info ? */
419 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
421 GTK_SIGNAL_FUNC(selection_handler
),
426 /* disable GUI threads */
431 /* Tell the world we offer clipboard data */
432 res
= (gtk_selection_owner_set( m_clipboardWidget
,
434 (guint32
) GDK_CURRENT_TIME
));
438 m_ownsPrimarySelection
= res
;
440 m_ownsClipboard
= res
;
443 /* re-enable GUI threads */
450 void wxClipboard::Close()
452 wxCHECK_RET( m_open
, wxT("clipboard not open") );
457 bool wxClipboard::IsOpened() const
462 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
464 /* reentrance problems */
465 if (m_waiting
) return FALSE
;
467 /* store requested format to be asked for by callbacks */
468 m_targetRequested
= format
;
471 wxLogTrace( TRACE_CLIPBOARD
,
472 wxT("wxClipboard:IsSupported: requested format: %s"),
473 format
.GetId().c_str() );
476 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
478 m_formatSupported
= FALSE
;
480 /* perform query. this will set m_formatSupported to
481 TRUE if m_targetRequested is supported.
482 also, we have to wait for the "answer" from the
483 clipboard owner which is an asynchronous process.
484 therefore we set m_waiting = TRUE here and wait
485 until the callback "targets_selection_received"
491 gtk_selection_convert( m_targetsWidget
,
492 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
495 (guint32
) GDK_CURRENT_TIME
);
497 while (m_waiting
) gtk_main_iteration();
500 if (!m_formatSupported
) return FALSE
;
505 bool wxClipboard::GetData( wxDataObject
& data
)
507 wxCHECK_MSG( m_open
, FALSE
, wxT("clipboard not open") );
509 /* get formats from wxDataObjects */
510 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
511 data
.GetAllFormats( array
);
513 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
515 wxDataFormat
format( array
[i
] );
517 wxLogTrace( TRACE_CLIPBOARD
,
518 wxT("wxClipboard::GetData: requested format: %s"),
519 format
.GetId().c_str() );
521 /* is data supported by clipboard ? */
523 /* store requested format to be asked for by callbacks */
524 m_targetRequested
= format
;
526 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
528 m_formatSupported
= FALSE
;
530 /* perform query. this will set m_formatSupported to
531 TRUE if m_targetRequested is supported.
532 also, we have to wait for the "answer" from the
533 clipboard owner which is an asynchronous process.
534 therefore we set m_waiting = TRUE here and wait
535 until the callback "targets_selection_received"
541 gtk_selection_convert( m_targetsWidget
,
542 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
545 (guint32
) GDK_CURRENT_TIME
);
547 while (m_waiting
) gtk_main_iteration();
550 if (!m_formatSupported
) continue;
552 /* store pointer to data object to be filled up by callbacks */
553 m_receivedData
= &data
;
555 /* store requested format to be asked for by callbacks */
556 m_targetRequested
= format
;
558 wxCHECK_MSG( m_targetRequested
, FALSE
, wxT("invalid clipboard format") );
561 m_formatSupported
= FALSE
;
563 /* ask for clipboard contents. this will set
564 m_formatSupported to TRUE if m_targetRequested
566 also, we have to wait for the "answer" from the
567 clipboard owner which is an asynchronous process.
568 therefore we set m_waiting = TRUE here and wait
569 until the callback "targets_selection_received"
574 wxLogTrace( TRACE_CLIPBOARD
,
575 wxT("wxClipboard::GetData: format found, start convert") );
578 gtk_selection_convert( m_clipboardWidget
,
579 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
582 (guint32
) GDK_CURRENT_TIME
);
584 while (m_waiting
) gtk_main_iteration();
587 /* this is a true error as we checked for the presence of such data before */
588 wxCHECK_MSG( m_formatSupported
, FALSE
, wxT("error retrieving data from clipboard") );
595 wxLogTrace( TRACE_CLIPBOARD
,
596 wxT("wxClipboard::GetData: format not found") );