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
= _T("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 _T("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
= (wxDataObject
*) 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;
296 m_data
= (wxDataObject
*) NULL
;
297 m_receivedData
= (wxDataObject
*) 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;
310 m_usePrimary
= false;
313 wxClipboard::~wxClipboard()
317 // if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
318 // if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
321 void wxClipboard::Clear()
326 /* disable GUI threads */
329 /* As we have data we also own the clipboard. Once we no longer own
330 it, clear_selection is called which will set m_data to zero */
332 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
336 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
337 (guint32
) GDK_CURRENT_TIME
);
339 while (m_waiting
) gtk_main_iteration();
342 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
346 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
347 (guint32
) GDK_CURRENT_TIME
);
349 while (m_waiting
) gtk_main_iteration();
356 m_data
= (wxDataObject
*) NULL
;
360 /* re-enable GUI threads */
364 m_targetRequested
= 0;
365 m_formatSupported
= false;
368 bool wxClipboard::Open()
370 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
377 bool wxClipboard::SetData( wxDataObject
*data
)
379 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
381 wxCHECK_MSG( data
, false, wxT("data is invalid") );
385 return AddData( data
);
388 bool wxClipboard::AddData( wxDataObject
*data
)
393 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
395 wxCHECK_MSG( data
, false, wxT("data is invalid") );
397 /* we can only store one wxDataObject */
402 /* get formats from wxDataObjects */
403 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
404 m_data
->GetAllFormats( array
);
407 /* primary selection or clipboard */
408 Atom clipboard
= m_usePrimary
? (Atom
) 1 // 1 = primary selection
413 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
415 wxLogTrace( TRACE_CLIPBOARD
,
416 wxT("wxClipboard now supports atom %s"),
417 array
[i
].GetId().c_str() );
420 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
423 0 ); /* what is info ? */
430 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
432 GTK_SIGNAL_FUNC(selection_handler
),
437 /* disable GUI threads */
442 /* Tell the world we offer clipboard data */
443 res
= (gtk_selection_owner_set( m_clipboardWidget
,
445 (guint32
) GDK_CURRENT_TIME
));
449 m_ownsPrimarySelection
= res
;
451 m_ownsClipboard
= res
;
454 /* re-enable GUI threads */
461 void wxClipboard::Close()
463 wxCHECK_RET( m_open
, wxT("clipboard not open") );
468 bool wxClipboard::IsOpened() const
473 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
475 /* reentrance problems */
476 if (m_waiting
) return false;
478 /* store requested format to be asked for by callbacks */
479 m_targetRequested
= format
;
482 wxLogTrace( TRACE_CLIPBOARD
,
483 wxT("wxClipboard:IsSupported: requested format: %s"),
484 format
.GetId().c_str() );
487 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
489 m_formatSupported
= false;
491 /* perform query. this will set m_formatSupported to
492 true if m_targetRequested is supported.
493 also, we have to wait for the "answer" from the
494 clipboard owner which is an asynchronous process.
495 therefore we set m_waiting = true here and wait
496 until the callback "targets_selection_received"
502 gtk_selection_convert( m_targetsWidget
,
503 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
506 (guint32
) GDK_CURRENT_TIME
);
508 while (m_waiting
) gtk_main_iteration();
511 if (!m_formatSupported
) return false;
516 bool wxClipboard::GetData( wxDataObject
& data
)
518 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
520 /* get formats from wxDataObjects */
521 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
522 data
.GetAllFormats( array
);
524 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
526 wxDataFormat
format( array
[i
] );
528 wxLogTrace( TRACE_CLIPBOARD
,
529 wxT("wxClipboard::GetData: requested format: %s"),
530 format
.GetId().c_str() );
532 /* is data supported by clipboard ? */
534 /* store requested format to be asked for by callbacks */
535 m_targetRequested
= format
;
537 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
539 m_formatSupported
= false;
541 /* perform query. this will set m_formatSupported to
542 true if m_targetRequested is supported.
543 also, we have to wait for the "answer" from the
544 clipboard owner which is an asynchronous process.
545 therefore we set m_waiting = true here and wait
546 until the callback "targets_selection_received"
552 gtk_selection_convert( m_targetsWidget
,
553 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
556 (guint32
) GDK_CURRENT_TIME
);
558 while (m_waiting
) gtk_main_iteration();
561 if (!m_formatSupported
) continue;
563 /* store pointer to data object to be filled up by callbacks */
564 m_receivedData
= &data
;
566 /* store requested format to be asked for by callbacks */
567 m_targetRequested
= format
;
569 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
572 m_formatSupported
= false;
574 /* ask for clipboard contents. this will set
575 m_formatSupported to true if m_targetRequested
577 also, we have to wait for the "answer" from the
578 clipboard owner which is an asynchronous process.
579 therefore we set m_waiting = true here and wait
580 until the callback "targets_selection_received"
585 wxLogTrace( TRACE_CLIPBOARD
,
586 wxT("wxClipboard::GetData: format found, start convert") );
589 gtk_selection_convert( m_clipboardWidget
,
590 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
593 (guint32
) GDK_CURRENT_TIME
);
595 while (m_waiting
) gtk_main_iteration();
598 /* this is a true error as we checked for the presence of such data before */
599 wxCHECK_MSG( m_formatSupported
, false, wxT("error retrieving data from clipboard") );
606 wxLogTrace( TRACE_CLIPBOARD
,
607 wxT("wxClipboard::GetData: format not found") );