From 9aa90f8df08ffa79270b6c12c911ece99465f5a3 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Tue, 5 Mar 2024 11:00:20 +0000 Subject: [PATCH 1/2] Rename GtkAccessibleText private header Follow the existing naming convention, and drop the hyphen. --- gtk/a11y/gtkatspicontext.c | 2 +- gtk/a11y/gtkatspitext.c | 2 +- gtk/gtkaccessibletext.c | 2 +- ...t-private.h => gtkaccessibletextprivate.h} | 2 +- gtk/gtkinscription.c | 2 +- gtk/gtklabel.c | 21 ++++---- gtk/gtktestatcontext.c | 2 +- gtk/gtktext.c | 18 +++---- gtk/gtktextview.c | 49 +++++++++---------- testsuite/a11y/inscription.c | 2 +- testsuite/a11y/label.c | 2 +- testsuite/a11y/text.c | 2 +- testsuite/a11y/textview.c | 2 +- 13 files changed, 53 insertions(+), 55 deletions(-) rename gtk/{gtkaccessibletext-private.h => gtkaccessibletextprivate.h} (95%) diff --git a/gtk/a11y/gtkatspicontext.c b/gtk/a11y/gtkatspicontext.c index e754c7c5c5..b6557148b4 100644 --- a/gtk/a11y/gtkatspicontext.c +++ b/gtk/a11y/gtkatspicontext.c @@ -23,7 +23,7 @@ #include "gtkatspicontextprivate.h" #include "gtkaccessibleprivate.h" -#include "gtkaccessibletext-private.h" +#include "gtkaccessibletextprivate.h" #include "gtkatspiactionprivate.h" #include "gtkatspieditabletextprivate.h" diff --git a/gtk/a11y/gtkatspitext.c b/gtk/a11y/gtkatspitext.c index ec6e859933..9ef2538d36 100644 --- a/gtk/a11y/gtkatspitext.c +++ b/gtk/a11y/gtkatspitext.c @@ -29,6 +29,7 @@ #include "a11y/atspi/atspi-text.h" +#include "gtkaccessibletextprivate.h" #include "gtkatcontextprivate.h" #include "gtkdebug.h" #include "gtkeditable.h" @@ -41,7 +42,6 @@ #include "gtkspinbuttonprivate.h" #include "gtktextbufferprivate.h" #include "gtktextviewprivate.h" -#include "gtkaccessibletext-private.h" #include diff --git a/gtk/gtkaccessibletext.c b/gtk/gtkaccessibletext.c index 163d6f908f..7ceef4fd1f 100644 --- a/gtk/gtkaccessibletext.c +++ b/gtk/gtkaccessibletext.c @@ -6,7 +6,7 @@ #include "config.h" -#include "gtkaccessibletext-private.h" +#include "gtkaccessibletextprivate.h" #include "gtkatcontextprivate.h" diff --git a/gtk/gtkaccessibletext-private.h b/gtk/gtkaccessibletextprivate.h similarity index 95% rename from gtk/gtkaccessibletext-private.h rename to gtk/gtkaccessibletextprivate.h index 0126d27a99..8617055dd1 100644 --- a/gtk/gtkaccessibletext-private.h +++ b/gtk/gtkaccessibletextprivate.h @@ -1,4 +1,4 @@ -/* gtkaccessibletext-private.h: Private definitions for GtkAccessibleText +/* gtkaccessibletextprivate.h: Private definitions for GtkAccessibleText * * SPDX-FileCopyrightText: 2023 Emmanuele Bassi * SPDX-License-Identifier: LGPL-2.1-or-later diff --git a/gtk/gtkinscription.c b/gtk/gtkinscription.c index a5e5ecfbfc..bbd8790150 100644 --- a/gtk/gtkinscription.c +++ b/gtk/gtkinscription.c @@ -21,7 +21,7 @@ #include "gtkinscriptionprivate.h" -#include "gtkaccessibletext-private.h" +#include "gtkaccessibletextprivate.h" #include "gtkcssnodeprivate.h" #include "gtkcssstylechangeprivate.h" #include "gtkpangoprivate.h" diff --git a/gtk/gtklabel.c b/gtk/gtklabel.c index fc00253a69..3f457369ba 100644 --- a/gtk/gtklabel.c +++ b/gtk/gtklabel.c @@ -26,37 +26,36 @@ #include "gtklabelprivate.h" +#include "gtkaccessibletextprivate.h" #include "gtkbuildable.h" +#include "gtkcsscolorvalueprivate.h" +#include "gtkdragsourceprivate.h" +#include "gtkdragicon.h" #include "gtkeventcontrollermotion.h" #include "gtkeventcontrollerfocus.h" #include "gtkfilelauncher.h" #include "gtkgesturedrag.h" #include "gtkgestureclick.h" #include "gtkgesturesingle.h" +#include "gtkjoinedmenuprivate.h" #include "gtkmarshalers.h" +#include "gtknative.h" #include "gtknotebook.h" #include "gtkpangoprivate.h" +#include "gtkpopovermenu.h" #include "gtkprivate.h" +#include "gtkrenderbackgroundprivate.h" +#include "gtkrenderborderprivate.h" +#include "gtkrenderlayoutprivate.h" #include "gtkshortcut.h" #include "gtkshortcutcontroller.h" #include "gtkshortcuttrigger.h" #include "gtksnapshot.h" -#include "gtkrenderbackgroundprivate.h" -#include "gtkrenderborderprivate.h" -#include "gtkrenderlayoutprivate.h" #include "gtktextutilprivate.h" #include "gtktooltip.h" #include "gtktypebuiltins.h" #include "gtkurilauncher.h" #include "gtkwidgetprivate.h" -#include "gtkpopovermenu.h" -#include "gtknative.h" -#include "gtkdragsourceprivate.h" -#include "gtkdragicon.h" -#include "gtkcsscolorvalueprivate.h" -#include "gtkjoinedmenuprivate.h" -#include "gtkaccessibletext-private.h" -#include "gtkpangoprivate.h" #include #include diff --git a/gtk/gtktestatcontext.c b/gtk/gtktestatcontext.c index 662310fa3c..4e448ba374 100644 --- a/gtk/gtktestatcontext.c +++ b/gtk/gtktestatcontext.c @@ -24,11 +24,11 @@ #include "gtkatcontextprivate.h" #include "gtkaccessibleprivate.h" +#include "gtkaccessibletextprivate.h" #include "gtkdebug.h" #include "gtkenums.h" #include "gtkprivate.h" #include "gtktypebuiltins.h" -#include "gtkaccessibletext-private.h" struct _GtkTestATContext { diff --git a/gtk/gtktext.c b/gtk/gtktext.c index 6da0ecc9d7..2dbdf9ecc1 100644 --- a/gtk/gtktext.c +++ b/gtk/gtktext.c @@ -23,8 +23,9 @@ #include "gtktextprivate.h" -#include "gtkaccessibletext-private.h" +#include "gtkaccessibletextprivate.h" #include "gtkactionable.h" +#include "gtkactionmuxerprivate.h" #include "gtkadjustment.h" #include "gtkbox.h" #include "gtkbutton.h" @@ -46,19 +47,20 @@ #include "gtkimcontextprivate.h" #include "gtkimcontextsimple.h" #include "gtkimmulticontext.h" -#include +#include "gtkjoinedmenuprivate.h" #include "gtklabel.h" #include "gtkmagnifierprivate.h" #include "gtkmain.h" #include "gtkmarshalers.h" +#include "gtknative.h" #include "gtkpangoprivate.h" #include "gtkpopovermenu.h" #include "gtkprivate.h" -#include "gtksettings.h" -#include "gtksnapshot.h" #include "gtkrenderbackgroundprivate.h" #include "gtkrenderborderprivate.h" #include "gtkrenderlayoutprivate.h" +#include "gtksettings.h" +#include "gtksnapshot.h" #include "gtktexthandleprivate.h" #include "gtktexthistoryprivate.h" #include "gtktextutilprivate.h" @@ -66,15 +68,13 @@ #include "gtktypebuiltins.h" #include "gtkwidgetprivate.h" #include "gtkwindow.h" -#include "gtknative.h" -#include "gtkactionmuxerprivate.h" -#include "gtkjoinedmenuprivate.h" + #include "deprecated/gtkrender.h" -#include "gtkaccessibletext-private.h" #include "a11y/gtkatspipangoprivate.h" -#include #include +#include +#include /** * GtkText: diff --git a/gtk/gtktextview.c b/gtk/gtktextview.c index 7473117635..b64de40895 100644 --- a/gtk/gtktextview.c +++ b/gtk/gtktextview.c @@ -29,41 +29,40 @@ #include +#include + +#include "gtkaccessibletextprivate.h" #include "gtkadjustmentprivate.h" #include "gtkcsscolorvalueprivate.h" +#include "gtkcssenumvalueprivate.h" +#include "gtkcsslineheightvalueprivate.h" #include "gtkdebug.h" #include "gtkdragsourceprivate.h" #include "gtkdropcontrollermotion.h" -#include -#include "gtkmain.h" -#include "gtkmarshalers.h" -#include "gtkrenderbackgroundprivate.h" -#include "gtksettings.h" -#include "gtktextiterprivate.h" +#include "gtkemojichooser.h" #include "gtkimcontextprivate.h" #include "gtkimmulticontext.h" -#include "gtkprivate.h" -#include "gtktextutilprivate.h" -#include "gtktextbufferprivate.h" -#include "gtkwidgetprivate.h" -#include "gtkwindow.h" -#include "gtkscrollable.h" -#include "gtktypebuiltins.h" -#include "gtktextviewchildprivate.h" -#include "gtktexthandleprivate.h" -#include "gtkpopover.h" -#include "gtkmagnifierprivate.h" -#include "gtkemojichooser.h" -#include "gtkpangoprivate.h" -#include "gtknative.h" -#include "gtkwidgetprivate.h" #include "gtkjoinedmenuprivate.h" -#include "gtkcsslineheightvalueprivate.h" -#include "gtkcssenumvalueprivate.h" -#include "gtksnapshot.h" +#include "gtkmagnifierprivate.h" +#include "gtkmain.h" +#include "gtkmarshalers.h" +#include "gtknative.h" +#include "gtkpangoprivate.h" #include "gtkrenderbackgroundprivate.h" #include "gtkrenderborderprivate.h" -#include "gtkaccessibletext-private.h" +#include "gtkscrollable.h" +#include "gtksettings.h" +#include "gtksnapshot.h" +#include "gtktextiterprivate.h" +#include "gtktexthandleprivate.h" +#include "gtktextviewchildprivate.h" +#include "gtkpopover.h" +#include "gtkprivate.h" +#include "gtktextbufferprivate.h" +#include "gtktextutilprivate.h" +#include "gtktypebuiltins.h" +#include "gtkwidgetprivate.h" +#include "gtkwindow.h" /** * GtkTextView: diff --git a/testsuite/a11y/inscription.c b/testsuite/a11y/inscription.c index 96844958bf..3cf48d374c 100644 --- a/testsuite/a11y/inscription.c +++ b/testsuite/a11y/inscription.c @@ -1,6 +1,6 @@ #include -#include "gtk/gtkaccessibletext-private.h" +#include "gtk/gtkaccessibletextprivate.h" static void inscription_text_interface (void) diff --git a/testsuite/a11y/label.c b/testsuite/a11y/label.c index 5f296580ee..9a54adbc72 100644 --- a/testsuite/a11y/label.c +++ b/testsuite/a11y/label.c @@ -1,6 +1,6 @@ #include -#include "gtk/gtkaccessibletext-private.h" +#include "gtk/gtkaccessibletextprivate.h" static void label_role (void) diff --git a/testsuite/a11y/text.c b/testsuite/a11y/text.c index 8d30acf840..454e18c469 100644 --- a/testsuite/a11y/text.c +++ b/testsuite/a11y/text.c @@ -1,7 +1,7 @@ #include #include "gtk/gtkatcontextprivate.h" -#include "gtk/gtkaccessibletext-private.h" +#include "gtk/gtkaccessibletextprivate.h" typedef struct { diff --git a/testsuite/a11y/textview.c b/testsuite/a11y/textview.c index 615dc41693..5eb0d40659 100644 --- a/testsuite/a11y/textview.c +++ b/testsuite/a11y/textview.c @@ -1,7 +1,7 @@ #include #include "gtk/gtkatcontextprivate.h" -#include "gtk/gtkaccessibletext-private.h" +#include "gtk/gtkaccessibletextprivate.h" static void textview_role (void) From 06262463670cb332249c061d59eed98b8e0a5074 Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Tue, 5 Mar 2024 12:23:17 +0000 Subject: [PATCH 2/2] a11y: Add utilify method for text attributes Simplify the AT-SPI implementation by having a convenience function to extract the text attributes of a GtkAccessibleText, with or without the default attributes. --- gtk/a11y/gtkatspitext.c | 46 +++++-------- gtk/gtkaccessibletext.c | 117 +++++++++++++++++++++++++++++++++ gtk/gtkaccessibletextprivate.h | 9 +++ 3 files changed, 142 insertions(+), 30 deletions(-) diff --git a/gtk/a11y/gtkatspitext.c b/gtk/a11y/gtkatspitext.c index 9ef2538d36..f2eb4acb9c 100644 --- a/gtk/a11y/gtkatspitext.c +++ b/gtk/a11y/gtkatspitext.c @@ -225,56 +225,42 @@ accessible_text_handle_method (GDBusConnection *connection, GVariantBuilder builder = G_VARIANT_BUILDER_INIT (G_VARIANT_TYPE ("a{ss}")); gboolean include_defaults = FALSE; int offset; - gsize n_attrs = 0; + gsize n_ranges = 0; GtkAccessibleTextRange *ranges = NULL; int start, end; char **attr_names = NULL; char **attr_values = NULL; - GHashTable *attrs; + gboolean res; g_variant_get (parameters, "(ib)", &offset, &include_defaults); - attrs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - - if (include_defaults) + res = gtk_accessible_text_get_attributes_run (accessible_text, + offset, + include_defaults, + &n_ranges, + &ranges, + &attr_names, + &attr_values); + if (!res) { - gtk_accessible_text_get_default_attributes (accessible_text, - &attr_names, - &attr_values); - - for (int i = 0; attr_names[i] != NULL; i++) - g_hash_table_insert (attrs, g_strdup (attr_names[i]), g_strdup (attr_values[i])); - - g_strfreev (attr_names); - g_strfreev (attr_values); + /* No attributes */ + g_dbus_method_invocation_return_value (invocation, g_variant_new ("(a{ss}ii)", &builder, 0, 0)); + return; } - gtk_accessible_text_get_attributes (accessible_text, - offset, - &n_attrs, - &ranges, - &attr_names, - &attr_values); + for (unsigned i = 0; attr_names[i] != NULL; i++) + g_variant_builder_add (&builder, "{ss}", attr_names[i], attr_values[i]); start = 0; end = G_MAXINT; - - for (int i = 0; i < n_attrs; i++) + for (unsigned i = 0; i < n_ranges; i++) { - g_hash_table_insert (attrs, g_strdup (attr_names[i]), g_strdup (attr_values[i])); start = MAX (start, ranges[i].start); end = MIN (end, start + ranges[i].length); } - GHashTableIter attr_iter; - gpointer key, value; - g_hash_table_iter_init (&attr_iter, attrs); - while (g_hash_table_iter_next (&attr_iter, &key, &value)) - g_variant_builder_add (&builder, "{ss}", key, value); - g_dbus_method_invocation_return_value (invocation, g_variant_new ("(a{ss}ii)", &builder, start, end)); - g_clear_pointer (&attrs, g_hash_table_unref); g_clear_pointer (&ranges, g_free); g_strfreev (attr_names); g_strfreev (attr_values); diff --git a/gtk/gtkaccessibletext.c b/gtk/gtkaccessibletext.c index 7ceef4fd1f..6d3e9a929c 100644 --- a/gtk/gtkaccessibletext.c +++ b/gtk/gtkaccessibletext.c @@ -309,6 +309,123 @@ gtk_accessible_text_get_default_attributes (GtkAccessibleText *self, attribute_values); } +/*< private > + * gtk_accessible_text_get_attributes_run: + * @self: the accessible object + * @offset: the offset, in characters + * @include_defaults: whether to include the default attributes in the + * returned array + * @n_ranges: (out): the number of attributes + * @ranges: (out) (array length=n_attributes) (optional): the ranges of the attributes + * inside the accessible object + * @attribute_names: (out) (array zero-terminated=1) (element-type utf8) (optional) (transfer full): + * the names of the attributes inside the accessible object + * @attribute_values: (out) (array zero-terminated=1) (element-type utf8) (optional) (transfer full): + * the values of the attributes inside the accessible object + * + * Retrieves the text attributes inside the accessible object. + * + * Each attribute is composed by: + * + * - a range + * - a name, typically in the form of a reverse DNS identifier + * - a value + * + * If this function returns true, `n_ranges` will be set to a value + * greater than or equal to one, @ranges will be set to a newly + * allocated array of [struct#Gtk.AccessibleTextRange] which should + * be freed with g_free(), @attribute_names and @attribute_values + * will be set to string arrays that should be freed with g_strfreev(). + * + * Returns: true if the accessible object has at least an attribute, + * and false otherwise + * + * Since: 4.14 + */ +gboolean +gtk_accessible_text_get_attributes_run (GtkAccessibleText *self, + unsigned int offset, + gboolean include_defaults, + gsize *n_ranges, + GtkAccessibleTextRange **ranges, + char ***attribute_names, + char ***attribute_values) +{ + GHashTable *attrs; + GHashTableIter attr_iter; + gpointer key, value; + char **attr_names, **attr_values; + gboolean res; + GStrvBuilder *names_builder; + GStrvBuilder *values_builder; + + g_return_val_if_fail (GTK_IS_ACCESSIBLE_TEXT (self), FALSE); + + attrs = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); + + if (include_defaults) + { + gtk_accessible_text_get_default_attributes (self, + &attr_names, + &attr_values); + + for (unsigned i = 0; attr_names[i] != NULL; i++) + { + g_hash_table_insert (attrs, + g_steal_pointer (&attr_names[i]), + g_steal_pointer (&attr_values[i])); + } + + g_free (attr_names); + g_free (attr_values); + } + + res = gtk_accessible_text_get_attributes (self, + offset, + n_ranges, + ranges, + &attr_names, + &attr_values); + + /* If there are no attributes, we can bail out early */ + if (!res && !include_defaults) + { + g_hash_table_unref (attrs); + *attribute_names = NULL; + *attribute_values = NULL; + return FALSE; + } + + /* The text attributes override the default ones */ + for (unsigned i = 0; i < *n_ranges; i++) + { + g_hash_table_insert (attrs, + g_steal_pointer (&attr_names[i]), + g_steal_pointer (&attr_values[i])); + } + + g_free (attr_names); + g_free (attr_values); + + names_builder = g_strv_builder_new (); + values_builder = g_strv_builder_new (); + g_hash_table_iter_init (&attr_iter, attrs); + while (g_hash_table_iter_next (&attr_iter, &key, &value)) + { + g_strv_builder_add (names_builder, key); + g_strv_builder_add (values_builder, value); + } + + *attribute_names = g_strv_builder_end (names_builder); + *attribute_values = g_strv_builder_end (values_builder); + + g_strv_builder_unref (names_builder); + g_strv_builder_unref (values_builder); + g_hash_table_unref (attrs); + + return TRUE; +} + /** * gtk_accessible_text_update_caret_position: * @self: the accessible object diff --git a/gtk/gtkaccessibletextprivate.h b/gtk/gtkaccessibletextprivate.h index 8617055dd1..2b72b69f4b 100644 --- a/gtk/gtkaccessibletextprivate.h +++ b/gtk/gtkaccessibletextprivate.h @@ -43,4 +43,13 @@ gtk_accessible_text_get_default_attributes (GtkAccessibleText *self, char ***attribute_names, char ***attribute_values); +gboolean +gtk_accessible_text_get_attributes_run (GtkAccessibleText *self, + unsigned int offset, + gboolean include_defaults, + gsize *n_ranges, + GtkAccessibleTextRange **ranges, + char ***attribute_names, + char ***attribute_values); + G_END_DECLS