From 11d235d16578e13d9ddd33d78b9c46e88dcad7b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Tyrychtr?= Date: Mon, 16 Jan 2023 11:39:41 +0100 Subject: [PATCH] a11y: Simplify GtkAccessible implementation for editables This commit implements the idea brought up in #5032, and provides a simple function for GtkEditable implementations using a delegate object. The accessible state is proxied from the outher GtkEditable to the delegate. --- gtk/gtkeditable.c | 55 ++++++++++++++++++++++++++++++++++++++++-- gtk/gtkeditable.h | 5 +++- gtk/gtkentry.c | 15 +----------- gtk/gtkpasswordentry.c | 14 +---------- gtk/gtksearchentry.c | 14 +---------- gtk/gtkspinbutton.c | 15 ++---------- 6 files changed, 62 insertions(+), 56 deletions(-) diff --git a/gtk/gtkeditable.c b/gtk/gtkeditable.c index a43e34550a..ce2e9a9b5a 100644 --- a/gtk/gtkeditable.c +++ b/gtk/gtkeditable.c @@ -19,7 +19,7 @@ * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS * file for a list of people on the GTK+ Team. See the ChangeLog * files for a list of changes. These files are distributed with - * GTK+ at ftp://ftp.gtk.org/pub/gtk/. + * GTK+ at ftp://ftp.gtk.org/pub/gtk/. */ /** @@ -485,7 +485,7 @@ gtk_editable_insert_text (GtkEditable *editable, if (length < 0) length = strlen (text); - + GTK_EDITABLE_GET_IFACE (editable)->do_insert_text (editable, text, length, position); } @@ -1181,3 +1181,54 @@ gtk_editable_delegate_get_property (GObject *object, return TRUE; } + +/** + * gtk_editable_delegate_get_accessible_platform_state: + * @editable: a `GtkEditable` implementation + * @state: what kind of accessible state to retrieve + * + * Retrieves the accessible platform state from the editable delegate. + * + * This is an helper function to retrieve the accessible state for + * `GtkEditable` interface implementations using a delegate pattern. + * + * You should call this function in your editable widget implementation + * of the [vfunc@Gtk.Accessible.get_platform_state] virtual function, for + * instance: + * + * ```c + * static void + * accessible_interface_init (GtkAccessibleInterface *iface) + * { + * iface->get_platform_state = your_editable_get_accessible_platform_state; + * } + * + * static gboolean + * your_editable_get_accessible_platform_state (GtkAccessible *accessible, + * GtkAccessiblePlatformState state) + * { + * return gtk_editable_delegate_get_accessible_platform_state (GTK_EDITABLE (accessible), state); + * } + * ``` + * + * Since: 4.10 + */ +gboolean +gtk_editable_delegate_get_accessible_platform_state (GtkEditable *editable, + GtkAccessiblePlatformState state) +{ + GtkWidget *delegate = GTK_WIDGET (gtk_editable_get_delegate (editable)); + + switch (state) + { + case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSABLE: + return gtk_widget_get_focusable (delegate); + case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSED: + return gtk_widget_has_focus (delegate); + case GTK_ACCESSIBLE_PLATFORM_STATE_ACTIVE: + return FALSE; + default: + g_assert_not_reached (); + return FALSE; + } +} diff --git a/gtk/gtkeditable.h b/gtk/gtkeditable.h index c7ca5caf04..7ca3da4034 100644 --- a/gtk/gtkeditable.h +++ b/gtk/gtkeditable.h @@ -30,9 +30,9 @@ #error "Only can be included directly." #endif +#include #include - G_BEGIN_DECLS #define GTK_TYPE_EDITABLE (gtk_editable_get_type ()) @@ -194,6 +194,9 @@ gboolean gtk_editable_delegate_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec); +GDK_AVAILABLE_IN_4_10 +gboolean gtk_editable_delegate_get_accessible_platform_state (GtkEditable *editable, + GtkAccessiblePlatformState state); G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkEditable, g_object_unref) diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c index 6fd837a276..23b6868064 100644 --- a/gtk/gtkentry.c +++ b/gtk/gtkentry.c @@ -339,20 +339,7 @@ static gboolean gtk_entry_accessible_get_platform_state (GtkAccessible *self, GtkAccessiblePlatformState state) { - GtkEntry *entry = GTK_ENTRY (self); - GtkEntryPrivate *priv = gtk_entry_get_instance_private (entry); - - switch (state) - { - case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSABLE: - return gtk_widget_get_focusable (GTK_WIDGET (priv->text)); - case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSED: - return gtk_widget_has_focus (GTK_WIDGET (priv->text)); - case GTK_ACCESSIBLE_PLATFORM_STATE_ACTIVE: - return FALSE; - default: - g_assert_not_reached (); - } + return gtk_editable_delegate_get_accessible_platform_state (GTK_EDITABLE (self), state); } static void diff --git a/gtk/gtkpasswordentry.c b/gtk/gtkpasswordentry.c index b540a27fca..72c3fd5189 100644 --- a/gtk/gtkpasswordentry.c +++ b/gtk/gtkpasswordentry.c @@ -550,19 +550,7 @@ static gboolean gtk_password_entry_accessible_get_platform_state (GtkAccessible *self, GtkAccessiblePlatformState state) { - GtkPasswordEntry *entry = GTK_PASSWORD_ENTRY (self); - - switch (state) - { - case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSABLE: - return gtk_widget_get_focusable (GTK_WIDGET (entry->entry)); - case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSED: - return gtk_widget_has_focus (GTK_WIDGET (entry->entry)); - case GTK_ACCESSIBLE_PLATFORM_STATE_ACTIVE: - return FALSE; - default: - g_assert_not_reached (); - } + return gtk_editable_delegate_get_accessible_platform_state (GTK_EDITABLE (self), state); } static void diff --git a/gtk/gtksearchentry.c b/gtk/gtksearchentry.c index 5b5b3b4d25..d11c659431 100644 --- a/gtk/gtksearchentry.c +++ b/gtk/gtksearchentry.c @@ -484,19 +484,7 @@ static gboolean gtk_search_entry_accessible_get_platform_state (GtkAccessible *self, GtkAccessiblePlatformState state) { - GtkSearchEntry *entry = GTK_SEARCH_ENTRY (self); - - switch (state) - { - case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSABLE: - return gtk_widget_get_focusable (GTK_WIDGET (entry->entry)); - case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSED: - return gtk_widget_has_focus (GTK_WIDGET (entry->entry)); - case GTK_ACCESSIBLE_PLATFORM_STATE_ACTIVE: - return FALSE; - default: - g_assert_not_reached (); - } + return gtk_editable_delegate_get_accessible_platform_state (GTK_EDITABLE (self), state); } static void diff --git a/gtk/gtkspinbutton.c b/gtk/gtkspinbutton.c index 27efc3c837..75d60b0000 100644 --- a/gtk/gtkspinbutton.c +++ b/gtk/gtkspinbutton.c @@ -614,25 +614,14 @@ static gboolean gtk_spin_button_accessible_get_platform_state (GtkAccessible *self, GtkAccessiblePlatformState state) { - GtkSpinButton *spin_button = GTK_SPIN_BUTTON (self); - - switch (state) - { - case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSABLE: - return gtk_widget_get_focusable (spin_button->entry); - case GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSED: - return gtk_widget_has_focus (spin_button->entry); - case GTK_ACCESSIBLE_PLATFORM_STATE_ACTIVE: - return FALSE; - default: - g_assert_not_reached (); - } + return gtk_editable_delegate_get_accessible_platform_state (GTK_EDITABLE (self), state); } static void gtk_spin_button_accessible_init (GtkAccessibleInterface *iface) { GtkAccessibleInterface *parent_iface = g_type_interface_peek_parent (iface); + iface->get_at_context = parent_iface->get_at_context; iface->get_platform_state = gtk_spin_button_accessible_get_platform_state; }