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 // the trace mask we use with wxLogTrace() - call
36 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
37 // (there will be a *lot* of them!)
38 static const wxChar
*TRACE_CLIPBOARD
= _T("clipboard");
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 /* The contents of a selection are returned in a GtkSelectionData
45 structure. selection/target identify the request.
46 type specifies the type of the return; if length < 0, and
47 the data should be ignored. This structure has object semantics -
48 no fields should be modified directly, they should not be created
49 directly, and pointers to them should not be stored beyond the duration of
50 a callback. (If the last is changed, we'll need to add reference
53 struct _GtkSelectionData
65 //-----------------------------------------------------------------------------
66 // "selection_received" for targets
67 //-----------------------------------------------------------------------------
72 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
73 GtkSelectionData
*selection_data
,
74 #if (GTK_MINOR_VERSION > 0)
75 guint32
WXUNUSED(time
),
77 wxClipboard
*clipboard
)
79 if ( wxTheClipboard
&& selection_data
->length
> 0 )
81 /* make sure we got the data in the correct form */
82 GdkAtom type
= selection_data
->type
;
83 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
85 if ( strcmp(gdk_atom_name(type
), "TARGETS") )
87 wxLogTrace( TRACE_CLIPBOARD
,
88 _T("got unsupported clipboard target") );
90 clipboard
->m_waiting
= false;
96 wxDataFormat
clip( selection_data
->selection
);
97 wxLogTrace( TRACE_CLIPBOARD
,
98 wxT("selection received for targets, clipboard %s"),
99 clip
.GetId().c_str() );
100 #endif // __WXDEBUG__
102 // the atoms we received, holding a list of targets (= formats)
103 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
105 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
107 wxDataFormat
format( atoms
[i
] );
109 wxLogTrace( TRACE_CLIPBOARD
,
110 wxT("selection received for targets, format %s"),
111 format
.GetId().c_str() );
113 if (format
== clipboard
->m_targetRequested
)
115 clipboard
->m_waiting
= false;
116 clipboard
->m_formatSupported
= true;
122 clipboard
->m_waiting
= false;
125 //-----------------------------------------------------------------------------
126 // "selection_received" for the actual data
127 //-----------------------------------------------------------------------------
130 selection_received( GtkWidget
*WXUNUSED(widget
),
131 GtkSelectionData
*selection_data
,
132 #if (GTK_MINOR_VERSION > 0)
133 guint32
WXUNUSED(time
),
135 wxClipboard
*clipboard
)
139 clipboard
->m_waiting
= false;
143 wxDataObject
*data_object
= clipboard
->m_receivedData
;
147 clipboard
->m_waiting
= false;
151 if (selection_data
->length
<= 0)
153 clipboard
->m_waiting
= false;
157 wxDataFormat
format( selection_data
->target
);
159 /* make sure we got the data in the correct format */
160 if (!data_object
->IsSupportedFormat( format
) )
162 clipboard
->m_waiting
= false;
166 /* make sure we got the data in the correct form (selection type).
167 if so, copy data to target object */
168 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
170 clipboard
->m_waiting
= false;
174 data_object
->SetData( format
, (size_t) selection_data
->length
, (const char*) selection_data
->data
);
176 wxTheClipboard
->m_formatSupported
= true;
177 clipboard
->m_waiting
= false;
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
185 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
187 if (!wxTheClipboard
) return TRUE
;
189 if (event
->selection
== GDK_SELECTION_PRIMARY
)
191 wxTheClipboard
->m_ownsPrimarySelection
= false;
194 if (event
->selection
== g_clipboardAtom
)
196 wxTheClipboard
->m_ownsClipboard
= false;
200 wxTheClipboard
->m_waiting
= false;
204 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
205 (!wxTheClipboard
->m_ownsClipboard
))
207 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
208 if (wxTheClipboard
->m_data
)
210 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
212 delete wxTheClipboard
->m_data
;
213 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
217 wxTheClipboard
->m_waiting
= false;
221 //-----------------------------------------------------------------------------
222 // selection handler for supplying data
223 //-----------------------------------------------------------------------------
226 selection_handler( GtkWidget
*WXUNUSED(widget
),
227 GtkSelectionData
*selection_data
,
228 guint
WXUNUSED(info
),
229 guint
WXUNUSED(time
),
230 gpointer
WXUNUSED(data
) )
232 if (!wxTheClipboard
) return;
234 if (!wxTheClipboard
->m_data
) return;
236 wxDataObject
*data
= wxTheClipboard
->m_data
;
238 wxDataFormat
format( selection_data
->target
);
240 if (!data
->IsSupportedFormat( format
)) return;
242 int size
= data
->GetDataSize( format
);
244 if (size
== 0) return;
246 void *d
= malloc(size
);
248 data
->GetDataHere( selection_data
->target
, d
);
250 // transform Unicode text into multibyte before putting it on clipboard
252 if ( format
.GetType() == wxDF_TEXT
)
254 const wchar_t *wstr
= (const wchar_t *)d
;
255 size_t len
= wxConvCurrent
->WC2MB(NULL
, wstr
, 0);
256 char *str
= malloc(len
+ 1);
257 wxConvCurrent
->WC2MB(str
, wstr
, len
);
263 #endif // wxUSE_UNICODE
265 gtk_selection_data_set(
267 GDK_SELECTION_TYPE_STRING
,
277 //-----------------------------------------------------------------------------
279 //-----------------------------------------------------------------------------
281 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
283 wxClipboard::wxClipboard()
287 m_ownsClipboard
= false;
288 m_ownsPrimarySelection
= false;
290 m_data
= (wxDataObject
*) NULL
;
291 m_receivedData
= (wxDataObject
*) NULL
;
293 /* we use m_targetsWidget to query what formats are available */
295 /* we use m_clipboardWidget to get and to offer data */
297 if (!g_clipboardAtom
) g_clipboardAtom
= XInternAtom( (Display
*) wxGetDisplay(), "CLIPBOARD", False
);
298 if (!g_targetsAtom
) g_targetsAtom
= XInternAtom( (Display
*) wxGetDisplay(), "TARGETS", False
);
301 m_formatSupported
= false;
302 m_targetRequested
= 0;
304 m_usePrimary
= false;
307 wxClipboard::~wxClipboard()
311 // if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
312 // if (m_targetsWidget) gtk_widget_destroy( m_targetsWidget );
315 void wxClipboard::Clear()
320 /* disable GUI threads */
323 /* As we have data we also own the clipboard. Once we no longer own
324 it, clear_selection is called which will set m_data to zero */
326 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
330 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
331 (guint32
) GDK_CURRENT_TIME
);
333 while (m_waiting
) gtk_main_iteration();
336 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
340 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
341 (guint32
) GDK_CURRENT_TIME
);
343 while (m_waiting
) gtk_main_iteration();
350 m_data
= (wxDataObject
*) NULL
;
354 /* re-enable GUI threads */
358 m_targetRequested
= 0;
359 m_formatSupported
= false;
362 bool wxClipboard::Open()
364 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
371 bool wxClipboard::SetData( wxDataObject
*data
)
373 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
375 wxCHECK_MSG( data
, false, wxT("data is invalid") );
379 return AddData( data
);
382 bool wxClipboard::AddData( wxDataObject
*data
)
387 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
389 wxCHECK_MSG( data
, false, wxT("data is invalid") );
391 /* we can only store one wxDataObject */
396 /* get formats from wxDataObjects */
397 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
398 m_data
->GetAllFormats( array
);
401 /* primary selection or clipboard */
402 Atom clipboard
= m_usePrimary
? (Atom
) 1 // 1 = primary selection
407 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
409 wxLogTrace( TRACE_CLIPBOARD
,
410 wxT("wxClipboard now supports atom %s"),
411 array
[i
].GetId().c_str() );
414 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
417 0 ); /* what is info ? */
424 gtk_signal_connect( GTK_OBJECT(m_clipboardWidget
),
426 GTK_SIGNAL_FUNC(selection_handler
),
431 /* disable GUI threads */
436 /* Tell the world we offer clipboard data */
437 res
= (gtk_selection_owner_set( m_clipboardWidget
,
439 (guint32
) GDK_CURRENT_TIME
));
443 m_ownsPrimarySelection
= res
;
445 m_ownsClipboard
= res
;
448 /* re-enable GUI threads */
455 void wxClipboard::Close()
457 wxCHECK_RET( m_open
, wxT("clipboard not open") );
462 bool wxClipboard::IsOpened() const
467 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
469 /* reentrance problems */
470 if (m_waiting
) return false;
472 /* store requested format to be asked for by callbacks */
473 m_targetRequested
= format
;
476 wxLogTrace( TRACE_CLIPBOARD
,
477 wxT("wxClipboard:IsSupported: requested format: %s"),
478 format
.GetId().c_str() );
481 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
483 m_formatSupported
= false;
485 /* perform query. this will set m_formatSupported to
486 true if m_targetRequested is supported.
487 also, we have to wait for the "answer" from the
488 clipboard owner which is an asynchronous process.
489 therefore we set m_waiting = true here and wait
490 until the callback "targets_selection_received"
496 gtk_selection_convert( m_targetsWidget
,
497 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
500 (guint32
) GDK_CURRENT_TIME
);
502 while (m_waiting
) gtk_main_iteration();
505 if (!m_formatSupported
) return false;
510 bool wxClipboard::GetData( wxDataObject
& data
)
512 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
514 /* get formats from wxDataObjects */
515 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
516 data
.GetAllFormats( array
);
518 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
520 wxDataFormat
format( array
[i
] );
522 wxLogTrace( TRACE_CLIPBOARD
,
523 wxT("wxClipboard::GetData: requested format: %s"),
524 format
.GetId().c_str() );
526 /* is data supported by clipboard ? */
528 /* store requested format to be asked for by callbacks */
529 m_targetRequested
= format
;
531 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
533 m_formatSupported
= false;
535 /* perform query. this will set m_formatSupported to
536 true if m_targetRequested is supported.
537 also, we have to wait for the "answer" from the
538 clipboard owner which is an asynchronous process.
539 therefore we set m_waiting = true here and wait
540 until the callback "targets_selection_received"
546 gtk_selection_convert( m_targetsWidget
,
547 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
550 (guint32
) GDK_CURRENT_TIME
);
552 while (m_waiting
) gtk_main_iteration();
555 if (!m_formatSupported
) continue;
557 /* store pointer to data object to be filled up by callbacks */
558 m_receivedData
= &data
;
560 /* store requested format to be asked for by callbacks */
561 m_targetRequested
= format
;
563 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
566 m_formatSupported
= false;
568 /* ask for clipboard contents. this will set
569 m_formatSupported to true if m_targetRequested
571 also, we have to wait for the "answer" from the
572 clipboard owner which is an asynchronous process.
573 therefore we set m_waiting = true here and wait
574 until the callback "targets_selection_received"
579 wxLogTrace( TRACE_CLIPBOARD
,
580 wxT("wxClipboard::GetData: format found, start convert") );
583 gtk_selection_convert( m_clipboardWidget
,
584 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
587 (guint32
) GDK_CURRENT_TIME
);
589 while (m_waiting
) gtk_main_iteration();
592 /* this is a true error as we checked for the presence of such data before */
593 wxCHECK_MSG( m_formatSupported
, false, wxT("error retrieving data from clipboard") );
600 wxLogTrace( TRACE_CLIPBOARD
,
601 wxT("wxClipboard::GetData: format not found") );