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"
23 #include "wx/dataobj.h"
25 #include "wx/x11/private.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
32 Atom g_clipboardAtom
= 0;
33 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");
41 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
45 /* The contents of a selection are returned in a GtkSelectionData
46 structure. selection/target identify the request.
47 type specifies the type of the return; if length < 0, and
48 the data should be ignored. This structure has object semantics -
49 no fields should be modified directly, they should not be created
50 directly, and pointers to them should not be stored beyond the duration of
51 a callback. (If the last is changed, we'll need to add reference
54 struct _GtkSelectionData
66 //-----------------------------------------------------------------------------
67 // "selection_received" for targets
68 //-----------------------------------------------------------------------------
73 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
74 GtkSelectionData
*selection_data
,
75 #if (GTK_MINOR_VERSION > 0)
76 guint32
WXUNUSED(time
),
78 wxClipboard
*clipboard
)
80 if ( wxTheClipboard
&& selection_data
->length
> 0 )
82 /* make sure we got the data in the correct form */
83 GdkAtom type
= selection_data
->type
;
84 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
86 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
88 wxLogTrace( TRACE_CLIPBOARD
,
89 _T("got unsupported clipboard target") );
91 clipboard
->m_waiting
= false;
97 wxDataFormat
clip( selection_data
->selection
);
98 wxLogTrace( TRACE_CLIPBOARD
,
99 wxT("selection received for targets, clipboard %s"),
100 clip
.GetId().c_str() );
101 #endif // __WXDEBUG__
103 // the atoms we received, holding a list of targets (= formats)
104 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
106 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
108 wxDataFormat
format( atoms
[i
] );
110 wxLogTrace( TRACE_CLIPBOARD
,
111 wxT("selection received for targets, format %s"),
112 format
.GetId().c_str() );
114 if (format
== clipboard
->m_targetRequested
)
116 clipboard
->m_waiting
= false;
117 clipboard
->m_formatSupported
= true;
123 clipboard
->m_waiting
= false;
126 //-----------------------------------------------------------------------------
127 // "selection_received" for the actual data
128 //-----------------------------------------------------------------------------
131 selection_received( GtkWidget
*WXUNUSED(widget
),
132 GtkSelectionData
*selection_data
,
133 #if (GTK_MINOR_VERSION > 0)
134 guint32
WXUNUSED(time
),
136 wxClipboard
*clipboard
)
140 clipboard
->m_waiting
= false;
144 wxDataObject
*data_object
= clipboard
->m_receivedData
;
148 clipboard
->m_waiting
= false;
152 if (selection_data
->length
<= 0)
154 clipboard
->m_waiting
= false;
158 wxDataFormat
format( selection_data
->target
);
160 /* make sure we got the data in the correct format */
161 if (!data_object
->IsSupportedFormat( format
) )
163 clipboard
->m_waiting
= false;
167 /* make sure we got the data in the correct form (selection type).
168 if so, copy data to target object */
169 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
171 clipboard
->m_waiting
= false;
175 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
177 wxTheClipboard
->m_formatSupported
= true;
178 clipboard
->m_waiting
= false;
181 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
186 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
188 if (!wxTheClipboard
) return TRUE
;
190 if (event
->selection
== GDK_SELECTION_PRIMARY
)
192 wxTheClipboard
->m_ownsPrimarySelection
= false;
195 if (event
->selection
== g_clipboardAtom
)
197 wxTheClipboard
->m_ownsClipboard
= false;
201 wxTheClipboard
->m_waiting
= false;
205 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
206 (!wxTheClipboard
->m_ownsClipboard
))
208 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
209 if (wxTheClipboard
->m_data
)
211 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
213 delete wxTheClipboard
->m_data
;
214 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
218 wxTheClipboard
->m_waiting
= false;
222 //-----------------------------------------------------------------------------
223 // selection handler for supplying data
224 //-----------------------------------------------------------------------------
227 selection_handler( GtkWidget
*WXUNUSED(widget
),
228 GtkSelectionData
*selection_data
,
229 guint
WXUNUSED(info
),
230 guint
WXUNUSED(time
),
231 gpointer
WXUNUSED(data
) )
233 if (!wxTheClipboard
) return;
235 if (!wxTheClipboard
->m_data
) return;
237 wxDataObject
*data
= wxTheClipboard
->m_data
;
239 wxDataFormat
format( selection_data
->target
);
241 if (!data
->IsSupportedFormat( format
)) return;
243 int size
= data
->GetDataSize( format
);
245 if (size
== 0) return;
247 void *d
= malloc(size
);
249 data
->GetDataHere( selection_data
->target
, d
);
251 // transform Unicode text into multibyte before putting it on clipboard
253 if ( format
.GetType() == wxDF_TEXT
)
255 const wchar_t *wstr
= (const wchar_t *)d
;
256 size_t len
= wxConvCurrent
->WC2MB(NULL
, wstr
, 0);
257 char *str
= malloc(len
+ 1);
258 wxConvCurrent
->WC2MB(str
, wstr
, len
);
264 #endif // wxUSE_UNICODE
266 gtk_selection_data_set(
268 GDK_SELECTION_TYPE_STRING
,
278 //-----------------------------------------------------------------------------
280 //-----------------------------------------------------------------------------
282 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
284 wxClipboard::wxClipboard()
288 m_ownsClipboard
= false;
289 m_ownsPrimarySelection
= false;
291 m_data
= (wxDataObject
*) NULL
;
292 m_receivedData
= (wxDataObject
*) NULL
;
294 /* we use m_targetsWidget to query what formats are available */
296 /* we use m_clipboardWidget to get and to offer data */
298 if (!g_clipboardAtom
) g_clipboardAtom
= XInternAtom( (Display
*) wxGetDisplay(), "CLIPBOARD", False
);
299 if (!g_targetsAtom
) g_targetsAtom
= XInternAtom( (Display
*) wxGetDisplay(), "TARGETS", False
);
302 m_formatSupported
= false;
303 m_targetRequested
= 0;
305 m_usePrimary
= false;
308 wxClipboard::~wxClipboard()
312 // if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
313 // if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
316 void wxClipboard::Clear()
321 /* disable GUI threads */
324 /* As we have data we also own the clipboard. Once we no longer own
325 it, clear_selection is called which will set m_data to zero */
327 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
331 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
332 (guint32
) GDK_CURRENT_TIME
);
334 while (m_waiting
) gtk_main_iteration();
337 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
341 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
342 (guint32
) GDK_CURRENT_TIME
);
344 while (m_waiting
) gtk_main_iteration();
351 m_data
= (wxDataObject
*) NULL
;
355 /* re-enable GUI threads */
359 m_targetRequested
= 0;
360 m_formatSupported
= false;
363 bool wxClipboard::Open()
365 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
372 bool wxClipboard::SetData( wxDataObject
*data
)
374 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
376 wxCHECK_MSG( data
, false, wxT("data is invalid") );
380 return AddData( data
);
383 bool wxClipboard::AddData( wxDataObject
*data
)
388 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
390 wxCHECK_MSG( data
, false, wxT("data is invalid") );
392 /* we can only store one wxDataObject */
397 /* get formats from wxDataObjects */
398 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
399 m_data
->GetAllFormats( array
);
402 /* primary selection or clipboard */
403 Atom clipboard
= m_usePrimary
? (Atom
) 1 // 1 = primary selection
408 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
410 wxLogTrace( TRACE_CLIPBOARD
,
411 wxT("wxClipboard now supports atom %s"),
412 array
[i
].GetId().c_str() );
415 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
418 0 ); /* what is info ? */
425 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
427 GTK_SIGNAL_FUNC(selection_handler
),
432 /* disable GUI threads */
437 /* Tell the world we offer clipboard data */
438 res
= (gtk_selection_owner_set( m_clipboardWidget
,
440 (guint32
) GDK_CURRENT_TIME
));
444 m_ownsPrimarySelection
= res
;
446 m_ownsClipboard
= res
;
449 /* re-enable GUI threads */
456 void wxClipboard::Close()
458 wxCHECK_RET( m_open
, wxT("clipboard not open") );
463 bool wxClipboard::IsOpened() const
468 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
470 /* reentrance problems */
471 if (m_waiting
) return false;
473 /* store requested format to be asked for by callbacks */
474 m_targetRequested
= format
;
477 wxLogTrace( TRACE_CLIPBOARD
,
478 wxT("wxClipboard:IsSupported: requested format: %s"),
479 format
.GetId().c_str() );
482 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
484 m_formatSupported
= false;
486 /* perform query. this will set m_formatSupported to
487 true if m_targetRequested is supported.
488 also, we have to wait for the "answer" from the
489 clipboard owner which is an asynchronous process.
490 therefore we set m_waiting = true here and wait
491 until the callback "targets_selection_received"
497 gtk_selection_convert( m_targetsWidget
,
498 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
501 (guint32
) GDK_CURRENT_TIME
);
503 while (m_waiting
) gtk_main_iteration();
506 if (!m_formatSupported
) return false;
511 bool wxClipboard::GetData( wxDataObject
& data
)
513 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
515 /* get formats from wxDataObjects */
516 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
517 data
.GetAllFormats( array
);
519 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
521 wxDataFormat
format( array
[i
] );
523 wxLogTrace( TRACE_CLIPBOARD
,
524 wxT("wxClipboard::GetData: requested format: %s"),
525 format
.GetId().c_str() );
527 /* is data supported by clipboard ? */
529 /* store requested format to be asked for by callbacks */
530 m_targetRequested
= format
;
532 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
534 m_formatSupported
= false;
536 /* perform query. this will set m_formatSupported to
537 true if m_targetRequested is supported.
538 also, we have to wait for the "answer" from the
539 clipboard owner which is an asynchronous process.
540 therefore we set m_waiting = true here and wait
541 until the callback "targets_selection_received"
547 gtk_selection_convert( m_targetsWidget
,
548 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
551 (guint32
) GDK_CURRENT_TIME
);
553 while (m_waiting
) gtk_main_iteration();
556 if (!m_formatSupported
) continue;
558 /* store pointer to data object to be filled up by callbacks */
559 m_receivedData
= &data
;
561 /* store requested format to be asked for by callbacks */
562 m_targetRequested
= format
;
564 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
567 m_formatSupported
= false;
569 /* ask for clipboard contents. this will set
570 m_formatSupported to true if m_targetRequested
572 also, we have to wait for the "answer" from the
573 clipboard owner which is an asynchronous process.
574 therefore we set m_waiting = true here and wait
575 until the callback "targets_selection_received"
580 wxLogTrace( TRACE_CLIPBOARD
,
581 wxT("wxClipboard::GetData: format found, start convert") );
584 gtk_selection_convert( m_clipboardWidget
,
585 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
588 (guint32
) GDK_CURRENT_TIME
);
590 while (m_waiting
) gtk_main_iteration();
593 /* this is a true error as we checked for the presence of such data before */
594 wxCHECK_MSG( m_formatSupported
, false, wxT("error retrieving data from clipboard") );
601 wxLogTrace( TRACE_CLIPBOARD
,
602 wxT("wxClipboard::GetData: format not found") );