1 /* GAIL - The GNOME Accessibility Implementation Library
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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #include "gtkcomboboxaccessible.h"
26 static void atk_action_interface_init (AtkActionIface *iface);
27 static void atk_selection_interface_init (AtkSelectionIface *iface);
29 G_DEFINE_TYPE_WITH_CODE (GtkComboBoxAccessible, gtk_combo_box_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
30 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
31 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
34 changed_cb (GtkWidget *widget)
36 GtkComboBox *combo_box;
38 GtkComboBoxAccessible *accessible;
41 combo_box = GTK_COMBO_BOX (widget);
43 index = gtk_combo_box_get_active (combo_box);
44 obj = gtk_widget_get_accessible (widget);
45 accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
46 if (accessible->old_selection != index)
48 accessible->old_selection = index;
49 g_object_notify (G_OBJECT (obj), "accessible-name");
50 g_signal_emit_by_name (obj, "selection_changed");
55 gtk_combo_box_accessible_initialize (AtkObject *obj,
58 GtkComboBox *combo_box;
59 GtkComboBoxAccessible *accessible;
62 ATK_OBJECT_CLASS (gtk_combo_box_accessible_parent_class)->initialize (obj, data);
64 combo_box = GTK_COMBO_BOX (data);
65 accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
67 g_signal_connect (combo_box, "changed", G_CALLBACK (changed_cb), NULL);
68 accessible->old_selection = gtk_combo_box_get_active (combo_box);
70 popup = gtk_combo_box_get_popup_accessible (combo_box);
73 atk_object_set_parent (popup, obj);
74 accessible->popup_set = TRUE;
76 if (gtk_combo_box_get_has_entry (combo_box))
77 atk_object_set_parent (gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (combo_box))), obj);
79 obj->role = ATK_ROLE_COMBO_BOX;
83 gtk_combo_box_accessible_finalize (GObject *object)
85 GtkComboBoxAccessible *combo_box = GTK_COMBO_BOX_ACCESSIBLE (object);
87 g_free (combo_box->name);
89 G_OBJECT_CLASS (gtk_combo_box_accessible_parent_class)->finalize (object);
93 gtk_combo_box_accessible_get_name (AtkObject *obj)
96 GtkComboBox *combo_box;
97 GtkComboBoxAccessible *accessible;
104 name = ATK_OBJECT_CLASS (gtk_combo_box_accessible_parent_class)->get_name (obj);
108 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
112 combo_box = GTK_COMBO_BOX (widget);
113 accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
114 if (gtk_combo_box_get_active_iter (combo_box, &iter))
116 model = gtk_combo_box_get_model (combo_box);
117 n_columns = gtk_tree_model_get_n_columns (model);
118 for (i = 0; i < n_columns; i++)
120 GValue value = { 0, };
122 gtk_tree_model_get_value (model, &iter, i, &value);
123 if (G_VALUE_HOLDS_STRING (&value))
125 g_free (accessible->name);
126 accessible->name = g_strdup (g_value_get_string (&value));
127 g_value_unset (&value);
131 g_value_unset (&value);
134 return accessible->name;
138 gtk_combo_box_accessible_get_n_children (AtkObject* obj)
143 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
148 if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (widget)))
155 gtk_combo_box_accessible_ref_child (AtkObject *obj,
160 GtkComboBoxAccessible *box;
162 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
168 child = gtk_combo_box_get_popup_accessible (GTK_COMBO_BOX (widget));
169 box = GTK_COMBO_BOX_ACCESSIBLE (obj);
170 if (box->popup_set == FALSE)
172 atk_object_set_parent (child, obj);
173 box->popup_set = TRUE;
176 else if (i == 1 && gtk_combo_box_get_has_entry (GTK_COMBO_BOX (widget)))
178 child = gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (widget)));
185 return g_object_ref (child);
189 gtk_combo_box_accessible_class_init (GtkComboBoxAccessibleClass *klass)
191 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
192 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
194 gobject_class->finalize = gtk_combo_box_accessible_finalize;
196 class->get_name = gtk_combo_box_accessible_get_name;
197 class->get_n_children = gtk_combo_box_accessible_get_n_children;
198 class->ref_child = gtk_combo_box_accessible_ref_child;
199 class->initialize = gtk_combo_box_accessible_initialize;
203 gtk_combo_box_accessible_init (GtkComboBoxAccessible *combo_box)
205 combo_box->old_selection = -1;
206 combo_box->name = NULL;
207 combo_box->popup_set = FALSE;
211 gtk_combo_box_accessible_do_action (AtkAction *action,
214 GtkComboBox *combo_box;
216 gboolean popup_shown;
218 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
222 if (!gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
227 combo_box = GTK_COMBO_BOX (widget);
228 g_object_get (combo_box, "popup-shown", &popup_shown, NULL);
230 gtk_combo_box_popdown (combo_box);
232 gtk_combo_box_popup (combo_box);
240 gtk_combo_box_accessible_get_n_actions (AtkAction *action)
246 gtk_combo_box_accessible_get_keybinding (AtkAction *action,
249 GtkComboBoxAccessible *combo_box;
253 AtkRelation *relation;
255 gpointer target_object;
257 gchar *return_value = NULL;
262 combo_box = GTK_COMBO_BOX_ACCESSIBLE (action);
263 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (combo_box));
267 set = atk_object_ref_relation_set (ATK_OBJECT (action));
272 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
275 target = atk_relation_get_target (relation);
276 target_object = g_ptr_array_index (target, 0);
277 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
279 g_object_unref (set);
280 if (GTK_IS_LABEL (label))
282 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
283 if (key_val != GDK_KEY_VoidSymbol)
284 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
291 gtk_combo_box_accessible_action_get_name (AtkAction *action,
301 atk_action_interface_init (AtkActionIface *iface)
303 iface->do_action = gtk_combo_box_accessible_do_action;
304 iface->get_n_actions = gtk_combo_box_accessible_get_n_actions;
305 iface->get_keybinding = gtk_combo_box_accessible_get_keybinding;
306 iface->get_name = gtk_combo_box_accessible_action_get_name;
310 gtk_combo_box_accessible_add_selection (AtkSelection *selection,
315 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
319 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), i);
325 gtk_combo_box_accessible_clear_selection (AtkSelection *selection)
329 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
333 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);
339 gtk_combo_box_accessible_ref_selection (AtkSelection *selection,
342 GtkComboBox *combo_box;
347 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
354 combo_box = GTK_COMBO_BOX (widget);
356 obj = gtk_combo_box_get_popup_accessible (combo_box);
357 index = gtk_combo_box_get_active (combo_box);
359 return atk_object_ref_accessible_child (obj, index);
363 gtk_combo_box_accessible_get_selection_count (AtkSelection *selection)
367 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
371 return (gtk_combo_box_get_active (GTK_COMBO_BOX (widget)) == -1) ? 0 : 1;
375 gtk_combo_box_accessible_is_child_selected (AtkSelection *selection,
381 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
386 j = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
392 gtk_combo_box_accessible_remove_selection (AtkSelection *selection,
395 if (atk_selection_is_child_selected (selection, i))
396 atk_selection_clear_selection (selection);
402 atk_selection_interface_init (AtkSelectionIface *iface)
404 iface->add_selection = gtk_combo_box_accessible_add_selection;
405 iface->clear_selection = gtk_combo_box_accessible_clear_selection;
406 iface->ref_selection = gtk_combo_box_accessible_ref_selection;
407 iface->get_selection_count = gtk_combo_box_accessible_get_selection_count;
408 iface->is_child_selected = gtk_combo_box_accessible_is_child_selected;
409 iface->remove_selection = gtk_combo_box_accessible_remove_selection;