1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/clipbrd.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/clipbrd.h"
20 #include "wx/dataobj.h"
23 #include "wx/gtk/private.h"
25 //-----------------------------------------------------------------------------
27 //-----------------------------------------------------------------------------
29 GdkAtom g_clipboardAtom
= 0;
30 GdkAtom g_targetsAtom
= 0;
31 GdkAtom g_timestampAtom
= 0;
34 extern GdkAtom g_altTextAtom
;
37 // the trace mask we use with wxLogTrace() - call
38 // wxLog::AddTraceMask(TRACE_CLIPBOARD) to enable the trace messages from here
39 // (there will be a *lot* of them!)
40 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 //-----------------------------------------------------------------------------
73 targets_selection_received( GtkWidget
*WXUNUSED(widget
),
74 GtkSelectionData
*selection_data
,
75 guint32
WXUNUSED(time
),
76 wxClipboard
*clipboard
)
78 if ( wxTheClipboard
&& selection_data
->length
> 0 )
80 // make sure we got the data in the correct form
81 GdkAtom type
= selection_data
->type
;
82 if ( type
!= GDK_SELECTION_TYPE_ATOM
)
84 if ( strcmp(wxGtkString(gdk_atom_name(type
)), "TARGETS") )
86 wxLogTrace( TRACE_CLIPBOARD
,
87 _T("got unsupported clipboard target") );
89 clipboard
->m_waiting
= false;
95 wxDataFormat
clip( selection_data
->selection
);
96 wxLogTrace( TRACE_CLIPBOARD
,
97 wxT("selection received for targets, clipboard %s"),
98 clip
.GetId().c_str() );
101 // the atoms we received, holding a list of targets (= formats)
102 GdkAtom
*atoms
= (GdkAtom
*)selection_data
->data
;
104 for (unsigned int i
=0; i
<selection_data
->length
/sizeof(GdkAtom
); i
++)
106 wxDataFormat
format( atoms
[i
] );
108 wxLogTrace( TRACE_CLIPBOARD
,
109 wxT("selection received for targets, format %s"),
110 format
.GetId().c_str() );
112 // printf( "format %s requested %s\n",
113 // gdk_atom_name( atoms[i] ),
114 // gdk_atom_name( clipboard->m_targetRequested ) );
116 if (format
== clipboard
->m_targetRequested
)
118 clipboard
->m_waiting
= false;
119 clipboard
->m_formatSupported
= true;
125 clipboard
->m_waiting
= false;
129 //-----------------------------------------------------------------------------
130 // "selection_received" for the actual data
131 //-----------------------------------------------------------------------------
135 selection_received( GtkWidget
*WXUNUSED(widget
),
136 GtkSelectionData
*selection_data
,
137 guint32
WXUNUSED(time
),
138 wxClipboard
*clipboard
)
142 clipboard
->m_waiting
= false;
146 wxDataObject
*data_object
= clipboard
->m_receivedData
;
150 clipboard
->m_waiting
= false;
154 if (selection_data
->length
<= 0)
156 clipboard
->m_waiting
= false;
160 wxDataFormat
format( selection_data
->target
);
162 // make sure we got the data in the correct format
163 if (!data_object
->IsSupportedFormat( format
) )
165 clipboard
->m_waiting
= false;
170 This seems to cause problems somehow
171 // make sure we got the data in the correct form (selection type).
172 // if so, copy data to target object
173 if (selection_data
->type
!= GDK_SELECTION_TYPE_STRING
)
175 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;
187 //-----------------------------------------------------------------------------
189 //-----------------------------------------------------------------------------
193 selection_clear_clip( GtkWidget
*WXUNUSED(widget
), GdkEventSelection
*event
)
195 if (!wxTheClipboard
) return true;
197 if (event
->selection
== GDK_SELECTION_PRIMARY
)
199 wxTheClipboard
->m_ownsPrimarySelection
= false;
202 if (event
->selection
== g_clipboardAtom
)
204 wxTheClipboard
->m_ownsClipboard
= false;
208 wxTheClipboard
->m_waiting
= false;
212 if ((!wxTheClipboard
->m_ownsPrimarySelection
) &&
213 (!wxTheClipboard
->m_ownsClipboard
))
215 /* the clipboard is no longer in our hands. we can the delete clipboard data. */
216 if (wxTheClipboard
->m_data
)
218 wxLogTrace(TRACE_CLIPBOARD
, wxT("wxClipboard will get cleared" ));
220 delete wxTheClipboard
->m_data
;
221 wxTheClipboard
->m_data
= (wxDataObject
*) NULL
;
225 wxTheClipboard
->m_waiting
= false;
230 //-----------------------------------------------------------------------------
231 // selection handler for supplying data
232 //-----------------------------------------------------------------------------
236 selection_handler( GtkWidget
*WXUNUSED(widget
),
237 GtkSelectionData
*selection_data
,
238 guint
WXUNUSED(info
),
239 guint
WXUNUSED(time
),
240 gpointer signal_data
)
242 if (!wxTheClipboard
) return;
244 if (!wxTheClipboard
->m_data
) return;
246 wxDataObject
*data
= wxTheClipboard
->m_data
;
248 // ICCCM says that TIMESTAMP is a required atom.
249 // In particular, it satisfies Klipper, which polls
250 // TIMESTAMP to see if the clipboards content has changed.
251 // It shall return the time which was used to set the data.
252 if (selection_data
->target
== g_timestampAtom
)
254 guint timestamp
= GPOINTER_TO_UINT (signal_data
);
255 gtk_selection_data_set(selection_data
,
256 GDK_SELECTION_TYPE_INTEGER
,
258 (guchar
*)&(timestamp
),
260 wxLogTrace(TRACE_CLIPBOARD
,
261 _T("Clipboard TIMESTAMP requested, returning timestamp=%u"),
266 wxDataFormat
format( selection_data
->target
);
269 wxLogTrace(TRACE_CLIPBOARD
,
270 _T("clipboard data in format %s, GtkSelectionData is target=%s type=%s selection=%s timestamp=%u"),
271 format
.GetId().c_str(),
272 wxString::FromAscii(wxGtkString(gdk_atom_name(selection_data
->target
))).c_str(),
273 wxString::FromAscii(wxGtkString(gdk_atom_name(selection_data
->type
))).c_str(),
274 wxString::FromAscii(wxGtkString(gdk_atom_name(selection_data
->selection
))).c_str(),
275 GPOINTER_TO_UINT( signal_data
)
279 if (!data
->IsSupportedFormat( format
)) return;
281 int size
= data
->GetDataSize( format
);
283 if (size
== 0) return;
285 void *d
= malloc(size
);
287 // Text data will be in UTF8 in Unicode mode.
288 data
->GetDataHere( selection_data
->target
, d
);
290 // NB: GTK+ requires special treatment of UTF8_STRING data, the text
291 // would show as UTF-8 data interpreted as latin1 (?) in other
292 // GTK+ apps if we used gtk_selection_data_set()
293 if (format
== wxDataFormat(wxDF_UNICODETEXT
))
295 gtk_selection_data_set_text(
302 gtk_selection_data_set(
304 GDK_SELECTION_TYPE_STRING
,
314 //-----------------------------------------------------------------------------
316 //-----------------------------------------------------------------------------
318 IMPLEMENT_DYNAMIC_CLASS(wxClipboard
,wxObject
)
320 wxClipboard::wxClipboard()
325 m_ownsClipboard
= false;
326 m_ownsPrimarySelection
= false;
328 m_data
= (wxDataObject
*) NULL
;
329 m_receivedData
= (wxDataObject
*) NULL
;
331 /* we use m_targetsWidget to query what formats are available */
333 m_targetsWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
334 gtk_widget_realize( m_targetsWidget
);
336 g_signal_connect (m_targetsWidget
, "selection_received",
337 G_CALLBACK (targets_selection_received
), this);
339 /* we use m_clipboardWidget to get and to offer data */
341 m_clipboardWidget
= gtk_window_new( GTK_WINDOW_POPUP
);
342 gtk_widget_realize( m_clipboardWidget
);
344 g_signal_connect (m_clipboardWidget
, "selection_received",
345 G_CALLBACK (selection_received
), this);
347 g_signal_connect (m_clipboardWidget
, "selection_clear_event",
348 G_CALLBACK (selection_clear_clip
), NULL
);
350 if (!g_clipboardAtom
) g_clipboardAtom
= gdk_atom_intern( "CLIPBOARD", FALSE
);
351 if (!g_targetsAtom
) g_targetsAtom
= gdk_atom_intern ("TARGETS", FALSE
);
352 if (!g_timestampAtom
) g_timestampAtom
= gdk_atom_intern ("TIMESTAMP", FALSE
);
354 m_formatSupported
= false;
355 m_targetRequested
= 0;
357 m_usePrimary
= false;
360 wxClipboard::~wxClipboard()
364 if (m_clipboardWidget
) gtk_widget_destroy( m_clipboardWidget
);
365 if (m_targetsWidget
) gtk_widget_destroy( m_targetsWidget
);
368 void wxClipboard::Clear()
373 /* disable GUI threads */
376 // As we have data we also own the clipboard. Once we no longer own
377 // it, clear_selection is called which will set m_data to zero
378 if (gdk_selection_owner_get( g_clipboardAtom
) == m_clipboardWidget
->window
)
382 gtk_selection_owner_set( (GtkWidget
*) NULL
, g_clipboardAtom
,
383 (guint32
) GDK_CURRENT_TIME
);
385 while (m_waiting
) gtk_main_iteration();
388 if (gdk_selection_owner_get( GDK_SELECTION_PRIMARY
) == m_clipboardWidget
->window
)
392 gtk_selection_owner_set( (GtkWidget
*) NULL
, GDK_SELECTION_PRIMARY
,
393 (guint32
) GDK_CURRENT_TIME
);
395 while (m_waiting
) gtk_main_iteration();
401 m_data
= (wxDataObject
*) NULL
;
405 /* re-enable GUI threads */
409 m_targetRequested
= 0;
410 m_formatSupported
= false;
413 bool wxClipboard::Open()
415 wxCHECK_MSG( !m_open
, false, wxT("clipboard already open") );
422 bool wxClipboard::SetData( wxDataObject
*data
)
424 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
426 wxCHECK_MSG( data
, false, wxT("data is invalid") );
430 return AddData( data
);
433 bool wxClipboard::AddData( wxDataObject
*data
)
435 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
437 wxCHECK_MSG( data
, false, wxT("data is invalid") );
439 // we can only store one wxDataObject
444 // get formats from wxDataObjects
445 wxDataFormat
*array
= new wxDataFormat
[ m_data
->GetFormatCount() ];
446 m_data
->GetAllFormats( array
);
448 // primary selection or clipboard
449 GdkAtom clipboard
= m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
452 // by default provide TIMESTAMP as a target
453 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
458 for (size_t i
= 0; i
< m_data
->GetFormatCount(); i
++)
460 wxLogTrace( TRACE_CLIPBOARD
,
461 wxT("wxClipboard now supports atom %s"),
462 array
[i
].GetId().c_str() );
464 // printf( "added %s\n",
465 // gdk_atom_name( array[i].GetFormatId() ) );
467 gtk_selection_add_target( GTK_WIDGET(m_clipboardWidget
),
470 0 ); /* what is info ? */
475 g_signal_connect (m_clipboardWidget
, "selection_get",
476 G_CALLBACK (selection_handler
),
477 GUINT_TO_POINTER (gtk_get_current_event_time()) );
480 /* disable GUI threads */
483 /* Tell the world we offer clipboard data */
484 bool res
= (gtk_selection_owner_set( m_clipboardWidget
,
486 (guint32
) GDK_CURRENT_TIME
));
489 m_ownsPrimarySelection
= res
;
491 m_ownsClipboard
= res
;
494 /* re-enable GUI threads */
500 void wxClipboard::Close()
502 wxCHECK_RET( m_open
, wxT("clipboard not open") );
507 bool wxClipboard::IsOpened() const
512 bool wxClipboard::IsSupported( const wxDataFormat
& format
)
514 /* reentrance problems */
515 if (m_waiting
) return false;
517 /* store requested format to be asked for by callbacks */
518 m_targetRequested
= format
;
520 wxLogTrace( TRACE_CLIPBOARD
,
521 wxT("wxClipboard:IsSupported: requested format: %s"),
522 format
.GetId().c_str() );
524 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
526 m_formatSupported
= false;
528 /* perform query. this will set m_formatSupported to
529 true if m_targetRequested is supported.
530 also, we have to wait for the "answer" from the
531 clipboard owner which is an asynchronous process.
532 therefore we set m_waiting = true here and wait
533 until the callback "targets_selection_received"
538 gtk_selection_convert( m_targetsWidget
,
539 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
542 (guint32
) GDK_CURRENT_TIME
);
544 while (m_waiting
) gtk_main_iteration();
547 if (!m_formatSupported
&& format
== wxDataFormat(wxDF_UNICODETEXT
))
549 // Another try with plain STRING format
550 extern GdkAtom g_altTextAtom
;
551 return IsSupported(g_altTextAtom
);
555 return m_formatSupported
;
558 bool wxClipboard::GetData( wxDataObject
& data
)
560 wxCHECK_MSG( m_open
, false, wxT("clipboard not open") );
562 /* get formats from wxDataObjects */
563 wxDataFormat
*array
= new wxDataFormat
[ data
.GetFormatCount() ];
564 data
.GetAllFormats( array
);
566 for (size_t i
= 0; i
< data
.GetFormatCount(); i
++)
568 wxDataFormat
format( array
[i
] );
570 wxLogTrace( TRACE_CLIPBOARD
,
571 wxT("wxClipboard::GetData: requested format: %s"),
572 format
.GetId().c_str() );
574 /* is data supported by clipboard ? */
576 /* store requested format to be asked for by callbacks */
577 m_targetRequested
= format
;
579 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
581 m_formatSupported
= false;
583 /* perform query. this will set m_formatSupported to
584 true if m_targetRequested is supported.
585 also, we have to wait for the "answer" from the
586 clipboard owner which is an asynchronous process.
587 therefore we set m_waiting = true here and wait
588 until the callback "targets_selection_received"
593 gtk_selection_convert( m_targetsWidget
,
594 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
597 (guint32
) GDK_CURRENT_TIME
);
599 while (m_waiting
) gtk_main_iteration();
601 if (!m_formatSupported
) continue;
603 /* store pointer to data object to be filled up by callbacks */
604 m_receivedData
= &data
;
606 /* store requested format to be asked for by callbacks */
607 m_targetRequested
= format
;
609 wxCHECK_MSG( m_targetRequested
, false, wxT("invalid clipboard format") );
612 m_formatSupported
= false;
614 /* ask for clipboard contents. this will set
615 m_formatSupported to true if m_targetRequested
617 also, we have to wait for the "answer" from the
618 clipboard owner which is an asynchronous process.
619 therefore we set m_waiting = true here and wait
620 until the callback "targets_selection_received"
625 wxLogTrace( TRACE_CLIPBOARD
,
626 wxT("wxClipboard::GetData: format found, start convert") );
628 gtk_selection_convert( m_clipboardWidget
,
629 m_usePrimary
? (GdkAtom
)GDK_SELECTION_PRIMARY
632 (guint32
) GDK_CURRENT_TIME
);
634 while (m_waiting
) gtk_main_iteration();
636 /* this is a true error as we checked for the presence of such data before */
637 wxCHECK_MSG( m_formatSupported
, false, wxT("error retrieving data from clipboard") );
644 wxLogTrace( TRACE_CLIPBOARD
,
645 wxT("wxClipboard::GetData: format not found") );