- /* As we have data we also own the clipboard. Once we no longer own
- it, clear_selection is called which will set m_data to zero */
-
- if (m_data)
- {
- delete m_data;
- gtk_selection_owner_set( (GtkWidget*) NULL, GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME );
- }
- if (m_clipboardWidget) gtk_widget_destroy( m_clipboardWidget );
-}
-
-void wxClipboard::SetData( wxDataObject *data )
-{
- if (m_data) delete m_data;
- m_data = data;
- if (!m_data) return;
-
- if (!gtk_selection_owner_set( m_clipboardWidget,
- g_clipboardAtom,
- GDK_CURRENT_TIME))
- {
- delete m_data;
- m_data = (wxDataObject*) NULL;
- return;
- }
-
- switch (m_data->GetPreferredFormat())
- {
-/*
- case wxDF_STRING:
- gtk_selection_add_handler( m_clipboardWidget,
- g_clipboardAtom,
- GDK_TARGET_STRING,
- selection_handler,
- NULL );
- break;
- case wxDF_TEXT:
- gtk_selection_add_handler( m_clipboardWidget,
- g_clipboardAtom,
- g_textAtom,
- selection_handler,
- NULL );
- break;
-*/
- default:
- break;
- }
-}
-
-void *wxClipboard::GetData( wxDataFormat format, size_t *length )
-{
- if (!IsAvailable(format))
- {
- if (length) *length = 0;
- return NULL;
- }
- return NULL;
-}
-
-bool wxClipboard::IsAvailable( wxDataFormat WXUNUSED(format) )
-{
- return FALSE;
+ Clear();
+
+ gtk_widget_destroy( m_clipboardWidget );
+ gtk_widget_destroy( m_targetsWidget );
+}
+
+// ----------------------------------------------------------------------------
+// wxClipboard helper functions
+// ----------------------------------------------------------------------------
+
+GdkAtom wxClipboard::GTKGetClipboardAtom() const
+{
+ return m_usePrimary ? (GdkAtom)GDK_SELECTION_PRIMARY
+ : g_clipboardAtom;
+}
+
+void wxClipboard::GTKClearData(Kind kind)
+{
+ wxDataObject *&data = Data(kind);
+ wxDELETE(data);
+}
+
+bool wxClipboard::SetSelectionOwner(bool set)
+{
+ bool rc = gtk_selection_owner_set
+ (
+ set ? m_clipboardWidget : NULL,
+ GTKGetClipboardAtom(),
+ (guint32)GDK_CURRENT_TIME
+ ) != 0;
+
+ if ( !rc )
+ {
+ wxLogTrace(TRACE_CLIPBOARD, wxT("Failed to %sset selection owner"),
+ set ? wxT("") : wxT("un"));
+ }
+
+ return rc;
+}
+
+void wxClipboard::AddSupportedTarget(GdkAtom atom)
+{
+ gtk_selection_add_target
+ (
+ m_clipboardWidget,
+ GTKGetClipboardAtom(),
+ atom,
+ 0 // info (same as client data) unused
+ );
+}
+
+bool wxClipboard::IsSupportedAsync(wxEvtHandler *sink)
+{
+ if (m_sink.get())
+ return false; // currently busy, come back later
+
+ wxCHECK_MSG( sink, false, wxT("no sink given") );
+
+ m_sink = sink;
+ gtk_selection_convert( m_targetsWidgetAsync,
+ GTKGetClipboardAtom(),
+ g_targetsAtom,
+ (guint32) GDK_CURRENT_TIME );
+
+ return true;
+}
+
+bool wxClipboard::DoIsSupported(const wxDataFormat& format)
+{
+ wxCHECK_MSG( format, false, wxT("invalid clipboard format") );
+
+ wxLogTrace(TRACE_CLIPBOARD, wxT("Checking if format %s is available"),
+ format.GetId().c_str());
+
+ // these variables will be used by our GTKOnTargetReceived()
+ m_targetRequested = format;
+ m_formatSupported = false;
+
+ // block until m_formatSupported is set from targets_selection_received
+ // callback
+ {
+ wxClipboardSync sync(*this);
+
+ gtk_selection_convert( m_targetsWidget,
+ GTKGetClipboardAtom(),
+ g_targetsAtom,
+ (guint32) GDK_CURRENT_TIME );
+ }
+
+ return m_formatSupported;
+}
+
+// ----------------------------------------------------------------------------
+// wxClipboard public API implementation
+// ----------------------------------------------------------------------------
+
+void wxClipboard::Clear()
+{
+ gtk_selection_clear_targets( m_clipboardWidget, GTKGetClipboardAtom() );
+
+ if ( gdk_selection_owner_get(GTKGetClipboardAtom()) ==
+ gtk_widget_get_window(m_clipboardWidget) )
+ {
+ wxClipboardSync sync(*this);
+
+ // this will result in selection_clear_clip callback being called and
+ // it will free our data
+ SetSelectionOwner(false);
+ }
+
+ m_targetRequested = 0;
+ m_formatSupported = false;
+}
+
+bool wxClipboard::Open()
+{
+ wxCHECK_MSG( !m_open, false, wxT("clipboard already open") );
+
+ m_open = true;
+
+ return true;
+}
+
+bool wxClipboard::SetData( wxDataObject *data )
+{
+ wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
+
+ wxCHECK_MSG( data, false, wxT("data is invalid") );
+
+ Clear();
+
+ return AddData( data );
+}
+
+bool wxClipboard::AddData( wxDataObject *data )
+{
+ wxCHECK_MSG( m_open, false, wxT("clipboard not open") );
+
+ wxCHECK_MSG( data, false, wxT("data is invalid") );
+
+ // we can only store one wxDataObject so clear the old one
+ Clear();
+
+ Data() = data;
+
+ // get formats from wxDataObjects
+ const size_t count = data->GetFormatCount();
+ wxDataFormatArray formats(new wxDataFormat[count]);
+ data->GetAllFormats(formats.get());
+
+ // always provide TIMESTAMP as a target, see comments in selection_handler
+ // for explanation
+ AddSupportedTarget(g_timestampAtom);
+
+ for ( size_t i = 0; i < count; i++ )
+ {
+ const wxDataFormat format(formats[i]);
+
+ wxLogTrace(TRACE_CLIPBOARD, wxT("Adding support for %s"),
+ format.GetId().c_str());
+
+ AddSupportedTarget(format);
+ }
+
+ g_signal_connect (m_clipboardWidget, "selection_get",
+ G_CALLBACK (selection_handler),
+ GUINT_TO_POINTER (gtk_get_current_event_time()) );
+
+ // tell the world we offer clipboard data
+ return SetSelectionOwner();
+}
+
+void wxClipboard::Close()
+{
+ wxCHECK_RET( m_open, wxT("clipboard not open") );
+
+ m_open = false;