[gtk/matthiasc/a11y] atspi: Break out the Value implementation



commit 68e85d08b9dd778777c0506696ba410b1a19859b
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Oct 11 16:18:56 2020 -0400

    atspi: Break out the Value implementation
    
    This isn't necessarily very big, but it keeps
    the widget checks out of gtkatspicontext.c, and
    it is a nice pattern.

 gtk/a11y/gtkatspicontext.c      |  95 ++---------------------------
 gtk/a11y/gtkatspivalue.c        | 129 ++++++++++++++++++++++++++++++++++++++++
 gtk/a11y/gtkatspivalueprivate.h |  30 ++++++++++
 gtk/a11y/meson.build            |   3 +-
 4 files changed, 165 insertions(+), 92 deletions(-)
---
diff --git a/gtk/a11y/gtkatspicontext.c b/gtk/a11y/gtkatspicontext.c
index 9ba86750ba..2600565b99 100644
--- a/gtk/a11y/gtkatspicontext.c
+++ b/gtk/a11y/gtkatspicontext.c
@@ -27,19 +27,14 @@
 #include "gtkatspiprivate.h"
 #include "gtkatspiutilsprivate.h"
 #include "gtkatspitextprivate.h"
+#include "gtkatspivalueprivate.h"
 
 #include "a11y/atspi/atspi-accessible.h"
 #include "a11y/atspi/atspi-text.h"
 #include "a11y/atspi/atspi-value.h"
 
 #include "gtkdebug.h"
-#include "gtklevelbar.h"
-#include "gtkpaned.h"
-#include "gtkprogressbar.h"
-#include "gtkrange.h"
 #include "gtkroot.h"
-#include "gtkscalebutton.h"
-#include "gtkspinbutton.h"
 #include "gtkwindow.h"
 
 #include <gio/gio.h>
@@ -514,84 +509,6 @@ static const GDBusInterfaceVTable accessible_vtable = {
   NULL,
 };
 
-static GVariant *
-handle_value_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 *ctx = GTK_AT_CONTEXT (user_data);
-  struct {
-    const char *name;
-    GtkAccessibleProperty property;
-  } properties[] = {
-    { "MinimumValue", GTK_ACCESSIBLE_PROPERTY_VALUE_MIN },
-    { "MaximumValue", GTK_ACCESSIBLE_PROPERTY_VALUE_MAX },
-    { "CurrentValue", GTK_ACCESSIBLE_PROPERTY_VALUE_NOW },
-  };
-  int i;
-
-  for (i = 0; i < G_N_ELEMENTS (properties); i++)
-    {
-      if (g_strcmp0 (property_name,  properties[i].name) == 0)
-        {
-          if (gtk_at_context_has_accessible_property (ctx, properties[i].property))
-            {
-              GtkAccessibleValue *value;
-
-              value = gtk_at_context_get_accessible_property (ctx, properties[i].property);
-              return g_variant_new_double (gtk_number_accessible_value_get (value));
-            }
-        }
-    }
-
-  /* fall back for a) MinimumIncrement b) widgets that should have the
-   * properties but don't
-   */
-  return g_variant_new_double (0.0);
-}
-
-static gboolean
-handle_value_set_property (GDBusConnection  *connection,
-                           const gchar      *sender,
-                           const gchar      *object_path,
-                           const gchar      *interface_name,
-                           const gchar      *property_name,
-                           GVariant         *value,
-                           GError          **error,
-                           gpointer          user_data)
-{
-  GtkAtSpiContext *self = user_data;
-  GtkWidget *widget = GTK_WIDGET (gtk_at_context_get_accessible (GTK_AT_CONTEXT (self)));
-
-  if (g_strcmp0 (property_name, "CurrentValue") == 0)
-    {
-      /* we only allow setting values if that is part of the user-exposed
-       * functionality of the widget.
-       */
-      if (GTK_IS_RANGE (widget))
-        gtk_range_set_value (GTK_RANGE (widget), g_variant_get_double (value));
-      else if (GTK_IS_PANED (widget))
-        gtk_paned_set_position (GTK_PANED (widget), (int)(g_variant_get_double (value) + 0.5));
-      else if (GTK_IS_SPIN_BUTTON (widget))
-        gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), g_variant_get_double (value));
-      else if (GTK_IS_SCALE_BUTTON (widget))
-        gtk_scale_button_set_value (GTK_SCALE_BUTTON (widget), g_variant_get_double (value));
-      return TRUE;
-    }
-
-  return FALSE;
-}
-
-static const GDBusInterfaceVTable value_vtable = {
-  NULL,
-  handle_value_get_property,
-  handle_value_set_property,
-};
-
 static void
 gtk_at_spi_context_register_object (GtkAtSpiContext *self)
 {
@@ -621,18 +538,14 @@ gtk_at_spi_context_register_object (GtkAtSpiContext *self)
                                          NULL);
     }
 
-  if (GTK_IS_LEVEL_BAR (widget) ||
-      GTK_IS_PANED (widget) ||
-      GTK_IS_PROGRESS_BAR (widget) ||
-      GTK_IS_RANGE (widget) ||
-      GTK_IS_SCALE_BUTTON (widget) ||
-      GTK_IS_SPIN_BUTTON (widget))
+  vtable = gtk_atspi_get_value_vtable (widget);
+  if (vtable)
     {
       g_variant_builder_add (&interfaces, "s", "org.a11y.atspi.Value");
       g_dbus_connection_register_object (self->connection,
                                          self->context_path,
                                          (GDBusInterfaceInfo *) &atspi_value_interface,
-                                         &value_vtable,
+                                         vtable,
                                          self,
                                          NULL,
                                          NULL);
diff --git a/gtk/a11y/gtkatspivalue.c b/gtk/a11y/gtkatspivalue.c
new file mode 100644
index 0000000000..2c8e9366de
--- /dev/null
+++ b/gtk/a11y/gtkatspivalue.c
@@ -0,0 +1,129 @@
+/* gtkatspicontext.c: AT-SPI Value implementation
+ *
+ * Copyright 2020  GNOME Foundation
+ *
+ * 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 "gtkatspivalueprivate.h"
+
+#include "a11y/atspi/atspi-value.h"
+
+#include "gtkatcontextprivate.h"
+#include "gtkdebug.h"
+#include "gtklevelbar.h"
+#include "gtkpaned.h"
+#include "gtkprogressbar.h"
+#include "gtkrange.h"
+#include "gtkscalebutton.h"
+#include "gtkspinbutton.h"
+
+#include <gio/gio.h>
+
+static GVariant *
+handle_value_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 *ctx = GTK_AT_CONTEXT (user_data);
+  struct {
+    const char *name;
+    GtkAccessibleProperty property;
+  } properties[] = {
+    { "MinimumValue", GTK_ACCESSIBLE_PROPERTY_VALUE_MIN },
+    { "MaximumValue", GTK_ACCESSIBLE_PROPERTY_VALUE_MAX },
+    { "CurrentValue", GTK_ACCESSIBLE_PROPERTY_VALUE_NOW },
+  };
+  int i;
+
+  for (i = 0; i < G_N_ELEMENTS (properties); i++)
+    {
+      if (g_strcmp0 (property_name,  properties[i].name) == 0)
+        {
+          if (gtk_at_context_has_accessible_property (ctx, properties[i].property))
+            {
+              GtkAccessibleValue *value;
+
+              value = gtk_at_context_get_accessible_property (ctx, properties[i].property);
+              return g_variant_new_double (gtk_number_accessible_value_get (value));
+            }
+        }
+    }
+
+  /* fall back for a) MinimumIncrement b) widgets that should have the
+   * properties but don't
+   */
+  return g_variant_new_double (0.0);
+}
+
+static gboolean
+handle_value_set_property (GDBusConnection  *connection,
+                           const gchar      *sender,
+                           const gchar      *object_path,
+                           const gchar      *interface_name,
+                           const gchar      *property_name,
+                           GVariant         *value,
+                           GError          **error,
+                           gpointer          user_data)
+{
+  GtkATContext *self = user_data;
+  GtkWidget *widget = GTK_WIDGET (gtk_at_context_get_accessible (self));
+
+  if (g_strcmp0 (property_name, "CurrentValue") == 0)
+    {
+      /* we only allow setting values if that is part of the user-exposed
+       * functionality of the widget.
+       */
+      if (GTK_IS_RANGE (widget))
+        gtk_range_set_value (GTK_RANGE (widget), g_variant_get_double (value));
+      else if (GTK_IS_PANED (widget))
+        gtk_paned_set_position (GTK_PANED (widget), (int)(g_variant_get_double (value) + 0.5));
+      else if (GTK_IS_SPIN_BUTTON (widget))
+        gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), g_variant_get_double (value));
+      else if (GTK_IS_SCALE_BUTTON (widget))
+        gtk_scale_button_set_value (GTK_SCALE_BUTTON (widget), g_variant_get_double (value));
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+static const GDBusInterfaceVTable value_vtable = {
+  NULL,
+  handle_value_get_property,
+  handle_value_set_property,
+};
+
+const GDBusInterfaceVTable *
+gtk_atspi_get_value_vtable (GtkWidget *widget)
+{
+  if (GTK_IS_LEVEL_BAR (widget) ||
+      GTK_IS_PANED (widget) ||
+      GTK_IS_PROGRESS_BAR (widget) ||
+      GTK_IS_RANGE (widget) ||
+      GTK_IS_SCALE_BUTTON (widget) ||
+      GTK_IS_SPIN_BUTTON (widget))
+    return &value_vtable;
+
+  return NULL;
+}
+
diff --git a/gtk/a11y/gtkatspivalueprivate.h b/gtk/a11y/gtkatspivalueprivate.h
new file mode 100644
index 0000000000..a248b27203
--- /dev/null
+++ b/gtk/a11y/gtkatspivalueprivate.h
@@ -0,0 +1,30 @@
+/* gtkatspivalueprivate.h: AT-SPI Value 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_value_vtable (GtkWidget *widget);
+
+G_END_DECLS
diff --git a/gtk/a11y/meson.build b/gtk/a11y/meson.build
index 24d5caa6f9..aaf54cdce9 100644
--- a/gtk/a11y/meson.build
+++ b/gtk/a11y/meson.build
@@ -15,6 +15,7 @@ if gtk_a11y_backends.contains('atspi')
     'gtkatspiutils.c',
     'gtkatspipango.c',
     'gtkatspitextbuffer.c',
-    'gtkatspitext.c'
+    'gtkatspitext.c',
+    'gtkatspivalue.c'
   ])
 endif


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