summaryrefslogtreecommitdiff
path: root/gtk/gtkatcontext.c
diff options
context:
space:
mode:
authorEmmanuele Bassi <ebassi@gnome.org>2020-10-09 21:23:19 +0100
committerEmmanuele Bassi <ebassi@gnome.org>2020-10-12 16:19:32 +0100
commit21482e8264b1cb8f5c4fc4a8209a38804f1dd088 (patch)
treed502f68ebfd03655873cbdaf8d78a7545f5787b4 /gtk/gtkatcontext.c
parent88113956a30cdb546ca71d4901d94de93140f3e2 (diff)
downloadgtk+-21482e8264b1cb8f5c4fc4a8209a38804f1dd088.tar.gz
a11y: Compute the label for a GtkATContext
The ARIA spec defines the mechanism for determining the name of an accessible element—see §4.3 of the WAI-ARIA spec. We follow the specification as much as it makes sense for GTK to do so: 1. if the element is hidden, return an empty string 1. if the element has a labelled-by relation set, retrieve the label of the related element 2. if the element has a label property set, use the value of the property 3. if neither labelled-by nor label attributes are set, we use the role to compute the name: - for a `range` role, we return the contents of the value of the `value-text` or `value-now` properties - for any other role, we return a textual representation of the GtkAccessibleRole enumeration value
Diffstat (limited to 'gtk/gtkatcontext.c')
-rw-r--r--gtk/gtkatcontext.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/gtk/gtkatcontext.c b/gtk/gtkatcontext.c
index 9b586c26bb..fe770cd5c2 100644
--- a/gtk/gtkatcontext.c
+++ b/gtk/gtkatcontext.c
@@ -725,3 +725,83 @@ gtk_at_context_get_accessible_relation (GtkATContext *self,
return gtk_accessible_attribute_set_get_value (self->relations, relation);
}
+
+/*< private >
+ * gtk_at_context_get_label:
+ * @self: a #GtkATContext
+ *
+ * Retrieves the accessible label of the #GtkATContext.
+ *
+ * This is a convenience function meant to be used by #GtkATContext implementations.
+ *
+ * Returns: (transfer full): the label of the #GtkATContext
+ */
+char *
+gtk_at_context_get_label (GtkATContext *self)
+{
+ g_return_val_if_fail (GTK_IS_AT_CONTEXT (self), NULL);
+
+ GtkAccessibleValue *value = NULL;
+
+ if (gtk_accessible_attribute_set_contains (self->states, GTK_ACCESSIBLE_STATE_HIDDEN))
+ {
+ value = gtk_accessible_attribute_set_get_value (self->states, GTK_ACCESSIBLE_STATE_HIDDEN);
+
+ if (gtk_boolean_accessible_value_get (value))
+ return g_strdup ("");
+ }
+
+ if (gtk_accessible_attribute_set_contains (self->properties, GTK_ACCESSIBLE_PROPERTY_LABEL))
+ {
+ value = gtk_accessible_attribute_set_get_value (self->properties, GTK_ACCESSIBLE_PROPERTY_LABEL);
+
+ return g_strdup (gtk_string_accessible_value_get (value));
+ }
+
+ if (gtk_accessible_attribute_set_contains (self->relations, GTK_ACCESSIBLE_RELATION_LABELLED_BY))
+ {
+ value = gtk_accessible_attribute_set_get_value (self->relations, GTK_ACCESSIBLE_RELATION_LABELLED_BY);
+
+ GtkAccessible *rel = gtk_reference_accessible_value_get (value);
+ GtkATContext *rel_context = gtk_accessible_get_at_context (rel);
+
+ return gtk_at_context_get_label (rel_context);
+ }
+
+ GtkAccessibleRole role = gtk_at_context_get_accessible_role (self);
+
+ switch ((int) role)
+ {
+ case GTK_ACCESSIBLE_ROLE_RANGE:
+ {
+ int range_attrs[] = {
+ GTK_ACCESSIBLE_PROPERTY_VALUE_TEXT,
+ GTK_ACCESSIBLE_PROPERTY_VALUE_NOW,
+ };
+
+ for (int i = 0; i < G_N_ELEMENTS (range_attrs); i++)
+ {
+ if (gtk_accessible_attribute_set_contains (self->properties, range_attrs[i]))
+ {
+ value = gtk_accessible_attribute_set_get_value (self->properties, range_attrs[i]);
+ break;
+ }
+ }
+
+ if (value != NULL)
+ return g_strdup (gtk_string_accessible_value_get (value));
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ GEnumClass *enum_class = g_type_class_peek (GTK_TYPE_ACCESSIBLE_ROLE);
+ GEnumValue *enum_value = g_enum_get_value (enum_class, role);
+
+ if (enum_value != NULL)
+ return g_strdup (enum_value->value_nick);
+
+ return g_strdup ("widget");
+}