1 /* GTK+ - accessibility implementations
2 * Copyright 2004 Sun Microsystems Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20 #include <glib/gi18n-lib.h>
22 #include "gtkcomboboxaccessible.h"
24 struct _GtkComboBoxAccessiblePrivate
31 static void atk_action_interface_init (AtkActionIface *iface);
32 static void atk_selection_interface_init (AtkSelectionIface *iface);
34 G_DEFINE_TYPE_WITH_CODE (GtkComboBoxAccessible, gtk_combo_box_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
35 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
36 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
39 changed_cb (GtkWidget *widget)
41 GtkComboBox *combo_box;
43 GtkComboBoxAccessible *accessible;
46 combo_box = GTK_COMBO_BOX (widget);
48 index = gtk_combo_box_get_active (combo_box);
49 obj = gtk_widget_get_accessible (widget);
50 accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
51 if (accessible->priv->old_selection != index)
53 accessible->priv->old_selection = index;
54 g_object_notify (G_OBJECT (obj), "accessible-name");
55 g_signal_emit_by_name (obj, "selection-changed");
60 gtk_combo_box_accessible_initialize (AtkObject *obj,
63 GtkComboBox *combo_box;
64 GtkComboBoxAccessible *accessible;
67 ATK_OBJECT_CLASS (gtk_combo_box_accessible_parent_class)->initialize (obj, data);
69 combo_box = GTK_COMBO_BOX (data);
70 accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
72 g_signal_connect (combo_box, "changed", G_CALLBACK (changed_cb), NULL);
73 accessible->priv->old_selection = gtk_combo_box_get_active (combo_box);
75 popup = gtk_combo_box_get_popup_accessible (combo_box);
78 atk_object_set_parent (popup, obj);
79 accessible->priv->popup_set = TRUE;
81 if (gtk_combo_box_get_has_entry (combo_box))
82 atk_object_set_parent (gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (combo_box))), obj);
84 obj->role = ATK_ROLE_COMBO_BOX;
88 gtk_combo_box_accessible_finalize (GObject *object)
90 GtkComboBoxAccessible *combo_box = GTK_COMBO_BOX_ACCESSIBLE (object);
92 g_free (combo_box->priv->name);
94 G_OBJECT_CLASS (gtk_combo_box_accessible_parent_class)->finalize (object);
98 gtk_combo_box_accessible_get_name (AtkObject *obj)
101 GtkComboBox *combo_box;
102 GtkComboBoxAccessible *accessible;
109 name = ATK_OBJECT_CLASS (gtk_combo_box_accessible_parent_class)->get_name (obj);
113 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
117 combo_box = GTK_COMBO_BOX (widget);
118 accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
119 if (gtk_combo_box_get_active_iter (combo_box, &iter))
121 model = gtk_combo_box_get_model (combo_box);
122 n_columns = gtk_tree_model_get_n_columns (model);
123 for (i = 0; i < n_columns; i++)
125 GValue value = G_VALUE_INIT;
127 gtk_tree_model_get_value (model, &iter, i, &value);
128 if (G_VALUE_HOLDS_STRING (&value))
130 g_free (accessible->priv->name);
131 accessible->priv->name = g_strdup (g_value_get_string (&value));
132 g_value_unset (&value);
136 g_value_unset (&value);
139 return accessible->priv->name;
143 gtk_combo_box_accessible_get_n_children (AtkObject* obj)
148 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
153 if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (widget)))
160 gtk_combo_box_accessible_ref_child (AtkObject *obj,
165 GtkComboBoxAccessible *box;
167 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
173 child = gtk_combo_box_get_popup_accessible (GTK_COMBO_BOX (widget));
174 box = GTK_COMBO_BOX_ACCESSIBLE (obj);
175 if (!box->priv->popup_set)
177 atk_object_set_parent (child, obj);
178 box->priv->popup_set = TRUE;
181 else if (i == 1 && gtk_combo_box_get_has_entry (GTK_COMBO_BOX (widget)))
183 child = gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (widget)));
190 return g_object_ref (child);
194 gtk_combo_box_accessible_class_init (GtkComboBoxAccessibleClass *klass)
196 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
197 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
199 gobject_class->finalize = gtk_combo_box_accessible_finalize;
201 class->get_name = gtk_combo_box_accessible_get_name;
202 class->get_n_children = gtk_combo_box_accessible_get_n_children;
203 class->ref_child = gtk_combo_box_accessible_ref_child;
204 class->initialize = gtk_combo_box_accessible_initialize;
206 g_type_class_add_private (klass, sizeof (GtkComboBoxAccessiblePrivate));
210 gtk_combo_box_accessible_init (GtkComboBoxAccessible *combo_box)
212 combo_box->priv = G_TYPE_INSTANCE_GET_PRIVATE (combo_box,
213 GTK_TYPE_COMBO_BOX_ACCESSIBLE,
214 GtkComboBoxAccessiblePrivate);
215 combo_box->priv->old_selection = -1;
216 combo_box->priv->name = NULL;
217 combo_box->priv->popup_set = FALSE;
221 gtk_combo_box_accessible_do_action (AtkAction *action,
224 GtkComboBox *combo_box;
226 gboolean popup_shown;
228 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
232 if (!gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
238 combo_box = GTK_COMBO_BOX (widget);
239 g_object_get (combo_box, "popup-shown", &popup_shown, NULL);
241 gtk_combo_box_popdown (combo_box);
243 gtk_combo_box_popup (combo_box);
249 gtk_combo_box_accessible_get_n_actions (AtkAction *action)
255 gtk_combo_box_accessible_get_keybinding (AtkAction *action,
258 GtkComboBoxAccessible *combo_box;
262 AtkRelation *relation;
264 gpointer target_object;
266 gchar *return_value = NULL;
271 combo_box = GTK_COMBO_BOX_ACCESSIBLE (action);
272 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (combo_box));
276 set = atk_object_ref_relation_set (ATK_OBJECT (action));
281 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
284 target = atk_relation_get_target (relation);
285 target_object = g_ptr_array_index (target, 0);
286 label = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
288 g_object_unref (set);
289 if (GTK_IS_LABEL (label))
291 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
292 if (key_val != GDK_KEY_VoidSymbol)
293 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
300 gtk_combo_box_accessible_action_get_name (AtkAction *action,
309 gtk_combo_box_accessible_action_get_localized_name (AtkAction *action,
313 return C_("Action name", "Press");
318 gtk_combo_box_accessible_action_get_description (AtkAction *action,
322 return C_("Action description", "Presses the combobox");
327 atk_action_interface_init (AtkActionIface *iface)
329 iface->do_action = gtk_combo_box_accessible_do_action;
330 iface->get_n_actions = gtk_combo_box_accessible_get_n_actions;
331 iface->get_keybinding = gtk_combo_box_accessible_get_keybinding;
332 iface->get_name = gtk_combo_box_accessible_action_get_name;
333 iface->get_localized_name = gtk_combo_box_accessible_action_get_localized_name;
334 iface->get_description = gtk_combo_box_accessible_action_get_description;
338 gtk_combo_box_accessible_add_selection (AtkSelection *selection,
343 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
347 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), i);
353 gtk_combo_box_accessible_clear_selection (AtkSelection *selection)
357 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
361 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);
367 gtk_combo_box_accessible_ref_selection (AtkSelection *selection,
370 GtkComboBox *combo_box;
375 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
382 combo_box = GTK_COMBO_BOX (widget);
384 obj = gtk_combo_box_get_popup_accessible (combo_box);
385 index = gtk_combo_box_get_active (combo_box);
387 return atk_object_ref_accessible_child (obj, index);
391 gtk_combo_box_accessible_get_selection_count (AtkSelection *selection)
395 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
399 return (gtk_combo_box_get_active (GTK_COMBO_BOX (widget)) == -1) ? 0 : 1;
403 gtk_combo_box_accessible_is_child_selected (AtkSelection *selection,
409 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
414 j = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
420 gtk_combo_box_accessible_remove_selection (AtkSelection *selection,
423 if (atk_selection_is_child_selected (selection, i))
424 atk_selection_clear_selection (selection);
430 atk_selection_interface_init (AtkSelectionIface *iface)
432 iface->add_selection = gtk_combo_box_accessible_add_selection;
433 iface->clear_selection = gtk_combo_box_accessible_clear_selection;
434 iface->ref_selection = gtk_combo_box_accessible_ref_selection;
435 iface->get_selection_count = gtk_combo_box_accessible_get_selection_count;
436 iface->is_child_selected = gtk_combo_box_accessible_is_child_selected;
437 iface->remove_selection = gtk_combo_box_accessible_remove_selection;