[gtk/a11y/atspi] atspi: Implement Selection for listbox



commit 08f57d5c3dd19869b6bdec69c3f46b99e1b757ff
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Oct 13 14:19:53 2020 -0400

    atspi: Implement Selection for listbox
    
    Implement the selection interface for GtkListBox.
    
    This also includes a convenience api for context
    addresses: gtk_at_spi_context_to_ref.

 gtk/a11y/gtkatspicontext.c          |  58 +++++++-
 gtk/a11y/gtkatspicontextprivate.h   |   3 +
 gtk/a11y/gtkatspiselection.c        | 259 ++++++++++++++++++++++++++++++++++++
 gtk/a11y/gtkatspiselectionprivate.h |  38 ++++++
 gtk/a11y/gtkatspitext.c             |  26 ++--
 gtk/a11y/gtkatspitextprivate.h      |   8 +-
 gtk/a11y/meson.build                |   1 +
 7 files changed, 373 insertions(+), 20 deletions(-)
---
diff --git a/gtk/a11y/gtkatspicontext.c b/gtk/a11y/gtkatspicontext.c
index aa9b3ccf05..d9ce313dfd 100644
--- a/gtk/a11y/gtkatspicontext.c
+++ b/gtk/a11y/gtkatspicontext.c
@@ -31,11 +31,13 @@
 #include "gtkatspitextprivate.h"
 #include "gtkatspieditabletextprivate.h"
 #include "gtkatspivalueprivate.h"
+#include "gtkatspiselectionprivate.h"
 
 #include "a11y/atspi/atspi-accessible.h"
 #include "a11y/atspi/atspi-text.h"
 #include "a11y/atspi/atspi-editabletext.h"
 #include "a11y/atspi/atspi-value.h"
+#include "a11y/atspi/atspi-selection.h"
 
 #include "gtkdebug.h"
 #include "gtkeditable.h"
@@ -656,6 +658,21 @@ gtk_at_spi_context_register_object (GtkAtSpiContext *self)
       self->n_registered_objects++;
     }
 
+  vtable = gtk_atspi_get_selection_vtable (widget);
+  if (vtable)
+    {
+      g_variant_builder_add (&interfaces, "s", "org.a11y.atspi.Selection");
+      self->registration_ids[self->n_registered_objects] =
+          g_dbus_connection_register_object (self->connection,
+                                             self->context_path,
+                                             (GDBusInterfaceInfo *) &atspi_selection_interface,
+                                             vtable,
+                                             self,
+                                             NULL,
+                                             NULL);
+      self->n_registered_objects++;
+    }
+
   self->interfaces = g_variant_ref_sink (g_variant_builder_end (&interfaces));
 }
 
@@ -689,9 +706,9 @@ emit_text_changed (GtkAtSpiContext *self,
 }
 
 static void
-emit_selection_changed (GtkAtSpiContext *self,
-                        const char      *kind,
-                        int              cursor_position)
+emit_text_selection_changed (GtkAtSpiContext *self,
+                             const char      *kind,
+                             int              cursor_position)
 {
   g_dbus_connection_emit_signal (self->connection,
                                  NULL,
@@ -703,6 +720,20 @@ emit_selection_changed (GtkAtSpiContext *self,
                                  NULL);
 }
 
+static void
+emit_selection_changed (GtkAtSpiContext *self,
+                        const char      *kind)
+{
+  g_dbus_connection_emit_signal (self->connection,
+                                 NULL,
+                                 self->context_path,
+                                 "org.a11y.atspi.Event.Object",
+                                 "SelectionChanged",
+                                 g_variant_new ("(siiva{sv})",
+                                                "", 0, 0, g_variant_new_string (""), NULL),
+                                 NULL);
+}
+
 static void
 emit_state_changed (GtkAtSpiContext *self,
                     const char      *name,
@@ -946,6 +977,7 @@ gtk_at_spi_context_dispose (GObject *gobject)
 
   gtk_at_spi_context_unregister_object (self);
   gtk_atspi_disconnect_text_signals (GTK_WIDGET (accessible));
+  gtk_atspi_disconnect_selection_signals (GTK_WIDGET (accessible));
 
   G_OBJECT_CLASS (gtk_at_spi_context_parent_class)->dispose (gobject);
 }
@@ -1071,8 +1103,11 @@ gtk_at_spi_context_constructed (GObject *gobject)
   GtkAccessible *accessible = gtk_at_context_get_accessible (GTK_AT_CONTEXT (self));
   gtk_atspi_connect_text_signals (GTK_WIDGET (accessible),
                                   emit_text_changed,
-                                  emit_selection_changed,
+                                  emit_text_selection_changed,
                                   self);
+  gtk_atspi_connect_selection_signals (GTK_WIDGET (accessible),
+                                       emit_selection_changed,
+                                       self);
   gtk_at_spi_context_register_object (self);
 
   G_OBJECT_CLASS (gtk_at_spi_context_parent_class)->constructed (gobject);
@@ -1295,3 +1330,18 @@ gtk_at_spi_context_get_context_path (GtkAtSpiContext *self)
 
   return self->context_path;
 }
+
+/*< private >
+ * gtk_at_spi_context_to_ref:
+ * @self: a #GtkAtSpiContext
+ *
+ * Returns an ATSPI object reference for the #GtkAtSpiContext.
+ *
+ * Returns: (transfer floating): a #GVariant with the reference
+ */
+GVariant *
+gtk_at_spi_context_to_ref (GtkAtSpiContext *self)
+{
+  const char *name = g_dbus_connection_get_unique_name (self->connection);
+  return g_variant_new ("(so)", name, self->context_path);
+}
diff --git a/gtk/a11y/gtkatspicontextprivate.h b/gtk/a11y/gtkatspicontextprivate.h
index 7c02efed7a..b0cd407579 100644
--- a/gtk/a11y/gtkatspicontextprivate.h
+++ b/gtk/a11y/gtkatspicontextprivate.h
@@ -36,4 +36,7 @@ gtk_at_spi_create_context (GtkAccessibleRole  accessible_role,
 const char *
 gtk_at_spi_context_get_context_path (GtkAtSpiContext *self);
 
+GVariant *
+gtk_at_spi_context_to_ref (GtkAtSpiContext *self);
+
 G_END_DECLS
diff --git a/gtk/a11y/gtkatspiselection.c b/gtk/a11y/gtkatspiselection.c
new file mode 100644
index 0000000000..dd95df4fab
--- /dev/null
+++ b/gtk/a11y/gtkatspiselection.c
@@ -0,0 +1,259 @@
+/* gtkatspiselection.c: AT-SPI Selection implementation
+ *
+ * Copyright 2020 Red Hat, Inc.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "gtkatspiselectionprivate.h"
+
+#include "a11y/atspi/atspi-selection.h"
+
+#include "gtkatcontextprivate.h"
+#include "gtkatspicontextprivate.h"
+#include "gtkaccessibleprivate.h"
+#include "gtkdebug.h"
+#include "gtklistbox.h"
+
+#include <gio/gio.h>
+
+typedef struct {
+  int n;
+  GtkListBoxRow *row;
+} RowCounter;
+
+static void
+find_nth (GtkListBox    *box,
+          GtkListBoxRow *row,
+          gpointer       data)
+{
+  RowCounter *counter = data;
+
+  if (counter->n == 0)
+    counter->row = row;
+
+  counter->n--;
+}
+
+static void
+listbox_handle_method (GDBusConnection       *connection,
+                       const gchar           *sender,
+                       const gchar           *object_path,
+                       const gchar           *interface_name,
+                       const gchar           *method_name,
+                       GVariant              *parameters,
+                       GDBusMethodInvocation *invocation,
+                       gpointer               user_data)
+{
+  GtkATContext *self = user_data;
+  GtkAccessible *accessible = gtk_at_context_get_accessible (self);
+  GtkWidget *widget = GTK_WIDGET (accessible);
+
+  if (g_strcmp0 (method_name, "GetSelectedChild") == 0)
+    {
+      RowCounter counter;
+      int idx;
+
+      g_variant_get (parameters, "(i)", &idx);
+
+      counter.n = idx;
+      counter.row = NULL;
+
+      gtk_list_box_selected_foreach (GTK_LIST_BOX (widget), find_nth, &counter);
+
+      if (counter.row == NULL)
+        g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "No 
selected child for %d", idx);
+      else
+        {
+          GtkATContext *ctx = gtk_accessible_get_at_context (GTK_ACCESSIBLE (counter.row));
+          g_dbus_method_invocation_return_value (invocation, g_variant_new ("(@(so))", 
gtk_at_spi_context_to_ref (GTK_AT_SPI_CONTEXT (ctx))));
+        }
+    }
+  else if (g_strcmp0 (method_name, "SelectChild") == 0)
+    {
+      int idx;
+      GtkListBoxRow *row;
+
+      g_variant_get (parameters, "(i)", &idx);
+
+      row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (widget), idx);
+      if (!row)
+        g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "No 
child at position %d", idx);
+      else
+        {
+          gboolean ret;
+
+          gtk_list_box_select_row (GTK_LIST_BOX (widget), row);
+          ret = gtk_list_box_row_is_selected (row);
+          g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", ret));
+        }
+    }
+  else if (g_strcmp0 (method_name, "DeselectChild") == 0)
+    {
+      int idx;
+      GtkListBoxRow *row;
+
+      g_variant_get (parameters, "(i)", &idx);
+
+      row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (widget), idx);
+      if (!row)
+        g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "No 
child at position %d", idx);
+      else
+        {
+          gboolean ret;
+
+          gtk_list_box_unselect_row (GTK_LIST_BOX (widget), row);
+          ret = !gtk_list_box_row_is_selected (row);
+          g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", ret));
+        }
+    }
+  else if (g_strcmp0 (method_name, "DeselectSelectedChild") == 0)
+    {
+      RowCounter counter;
+      int idx;
+
+      g_variant_get (parameters, "(i)", &idx);
+
+      counter.n = idx;
+      counter.row = NULL;
+
+      gtk_list_box_selected_foreach (GTK_LIST_BOX (widget), find_nth, &counter);
+
+      if (counter.row == NULL)
+        g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "No 
selected child for %d", idx);
+      else
+        {
+          gboolean ret;
+
+          gtk_list_box_unselect_row (GTK_LIST_BOX (widget), counter.row);
+          ret = !gtk_list_box_row_is_selected (counter.row);
+          g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", ret));
+        }
+    }
+  else if (g_strcmp0 (method_name, "IsChildSelected") == 0)
+    {
+      int idx;
+      GtkListBoxRow *row;
+
+      g_variant_get (parameters, "(i)", &idx);
+
+      row = gtk_list_box_get_row_at_index (GTK_LIST_BOX (widget), idx);
+      if (!row)
+        g_dbus_method_invocation_return_error (invocation, G_DBUS_ERROR, G_DBUS_ERROR_INVALID_ARGS, "No 
child at position %d", idx);
+      else
+        g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", 
gtk_list_box_row_is_selected (row)));
+    }
+  else if (g_strcmp0 (method_name, "SelectAll") == 0)
+    {
+      gtk_list_box_select_all (GTK_LIST_BOX (widget));
+      g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
+    }
+  else if (g_strcmp0 (method_name, "ClearSelection") == 0)
+    {
+      gtk_list_box_unselect_all (GTK_LIST_BOX (widget));
+      g_dbus_method_invocation_return_value (invocation, g_variant_new ("(b)", TRUE));
+    }
+}
+
+static void
+count_selected (GtkListBox    *box,
+                GtkListBoxRow *row,
+                gpointer       data)
+{
+  *(int *)data += 1;
+}
+
+static GVariant *
+listbox_get_property (GDBusConnection  *connection,
+                      const gchar      *sender,
+                      const gchar      *object_path,
+                      const gchar      *interface_name,
+                      const gchar      *property_name,
+                      GError          **error,
+                      gpointer          user_data)
+{
+  GtkATContext *self = GTK_AT_CONTEXT (user_data);
+  GtkAccessible *accessible = gtk_at_context_get_accessible (self);
+  GtkWidget *widget = GTK_WIDGET (accessible);
+
+  if (g_strcmp0 (property_name, "NSelectedChildren") == 0)
+    {
+      int count = 0;
+
+      gtk_list_box_selected_foreach (GTK_LIST_BOX (widget), count_selected, &count);
+
+      return g_variant_new_int32 (count);
+    }
+
+  return NULL;
+}
+
+static const GDBusInterfaceVTable listbox_vtable = {
+  listbox_handle_method,
+  listbox_get_property,
+  NULL
+};
+
+const GDBusInterfaceVTable *
+gtk_atspi_get_selection_vtable (GtkWidget *widget)
+{
+  if (GTK_IS_LIST_BOX(widget))
+    return &listbox_vtable;
+
+  return NULL;
+}
+
+typedef struct {
+  GtkAtspiSelectionCallback *changed;
+  gpointer data;
+} SelectionChanged;
+
+void
+gtk_atspi_connect_selection_signals (GtkWidget *widget,
+                                     GtkAtspiSelectionCallback selection_changed,
+                                     gpointer   data)
+{
+  if (GTK_IS_LIST_BOX (widget))
+    {
+      SelectionChanged *changed;
+
+      changed = g_new (SelectionChanged, 1);
+      changed->changed = selection_changed;
+      changed->data = data;
+
+      g_object_set_data_full (G_OBJECT (widget), "accessible-selection-data", changed, g_free);
+
+      g_signal_connect_swapped (widget, "selected-rows-changed", G_CALLBACK (selection_changed), data);
+   }
+}
+
+void
+gtk_atspi_disconnect_selection_signals (GtkWidget *widget)
+{
+  if (GTK_IS_LIST_BOX (widget))
+    {
+      SelectionChanged *changed;
+
+      changed = g_object_get_data (G_OBJECT (widget), "accessible-selection-data");
+
+      g_signal_handlers_disconnect_by_func (widget, changed->changed, changed->data);
+
+      g_object_set_data (G_OBJECT (widget), "accessible-selection-data", NULL);
+    }
+}
+
diff --git a/gtk/a11y/gtkatspiselectionprivate.h b/gtk/a11y/gtkatspiselectionprivate.h
new file mode 100644
index 0000000000..f885bec519
--- /dev/null
+++ b/gtk/a11y/gtkatspiselectionprivate.h
@@ -0,0 +1,38 @@
+/* gtkatspiselectionprivate.h: AT-SPI Selection implementation
+ *
+ * Copyright 2020 Red Hat, Inc.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <gio/gio.h>
+#include "gtkwidget.h"
+
+G_BEGIN_DECLS
+
+const GDBusInterfaceVTable *gtk_atspi_get_selection_vtable (GtkWidget *widget);
+
+typedef void (GtkAtspiSelectionCallback) (gpointer data);
+
+void gtk_atspi_connect_selection_signals    (GtkWidget *widget,
+                                             GtkAtspiSelectionCallback selection_changed,
+                                             gpointer   data);
+void gtk_atspi_disconnect_selection_signals (GtkWidget *widget);
+
+
+G_END_DECLS
diff --git a/gtk/a11y/gtkatspitext.c b/gtk/a11y/gtkatspitext.c
index 9a7b37b5a3..893b331110 100644
--- a/gtk/a11y/gtkatspitext.c
+++ b/gtk/a11y/gtkatspitext.c
@@ -1188,11 +1188,11 @@ typedef struct {
 } TextChanged;
 
 static void
-insert_text_cb (GtkEditable     *editable,
-                char            *new_text,
-                int              new_text_length,
-                int             *position,
-                TextChanged     *changed)
+insert_text_cb (GtkEditable *editable,
+                char        *new_text,
+                int          new_text_length,
+                int         *position,
+                TextChanged *changed)
 {
   int length;
 
@@ -1204,10 +1204,10 @@ insert_text_cb (GtkEditable     *editable,
 }
 
 static void
-delete_text_cb (GtkEditable     *editable,
-                int              start,
-                int              end,
-                TextChanged     *changed)
+delete_text_cb (GtkEditable *editable,
+                int          start,
+                int          end,
+                TextChanged *changed)
 {
   char *text;
 
@@ -1381,11 +1381,15 @@ buffer_changed (GtkWidget   *widget,
 void
 gtk_atspi_connect_text_signals (GtkWidget *widget,
                                 GtkAtspiTextChangedCallback text_changed,
-                                GtkAtspiSelectionChangedCallback selection_changed,
+                                GtkAtspiTextSelectionCallback selection_changed,
                                 gpointer   data)
 {
   TextChanged *changed;
 
+  if (!GTK_IS_EDITABLE (widget) &&
+      !GTK_IS_TEXT_VIEW (widget))
+    return;
+
   changed = g_new0 (TextChanged, 1);
   changed->text_changed = text_changed;
   changed->selection_changed = selection_changed;
@@ -1420,8 +1424,6 @@ gtk_atspi_disconnect_text_signals (GtkWidget *widget)
 
   changed = g_object_get_data (G_OBJECT (widget), "accessible-text-data");
 
-  g_assert (changed != NULL);
-
   if (GTK_IS_EDITABLE (widget))
     {
       GtkText *text = gtk_editable_get_text_widget (widget);
diff --git a/gtk/a11y/gtkatspitextprivate.h b/gtk/a11y/gtkatspitextprivate.h
index 14ebf8c2b4..ad1459d128 100644
--- a/gtk/a11y/gtkatspitextprivate.h
+++ b/gtk/a11y/gtkatspitextprivate.h
@@ -32,13 +32,13 @@ typedef void (GtkAtspiTextChangedCallback) (gpointer    data,
                                             int         start,
                                             int         end,
                                             const char *text);
-typedef void (GtkAtspiSelectionChangedCallback) (gpointer    data,
-                                                 const char *kind,
-                                                 int         position);
+typedef void (GtkAtspiTextSelectionCallback) (gpointer    data,
+                                              const char *kind,
+                                              int         position);
 
 void gtk_atspi_connect_text_signals    (GtkWidget *widget,
                                         GtkAtspiTextChangedCallback text_changed,
-                                        GtkAtspiSelectionChangedCallback selection_changed,
+                                        GtkAtspiTextSelectionCallback selection_changed,
                                         gpointer   data);
 void gtk_atspi_disconnect_text_signals (GtkWidget *widget);
 
diff --git a/gtk/a11y/meson.build b/gtk/a11y/meson.build
index 2912746fc7..49359159ae 100644
--- a/gtk/a11y/meson.build
+++ b/gtk/a11y/meson.build
@@ -18,5 +18,6 @@ if gtk_a11y_backends.contains('atspi')
     'gtkatspitext.c',
     'gtkatspivalue.c',
     'gtkatspieditabletext.c',
+    'gtkatspiselection.c',
   ])
 endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]