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 = G_VALUE_INIT;
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))
228 combo_box = GTK_COMBO_BOX (widget);
229 g_object_get (combo_box, "popup-shown", &popup_shown, NULL);
231 gtk_combo_box_popdown (combo_box);
233 gtk_combo_box_popup (combo_box);
239 gtk_combo_box_accessible_get_n_actions (AtkAction *action)
245 gtk_combo_box_accessible_get_keybinding (AtkAction *action,
248 GtkComboBoxAccessible *combo_box;
252 AtkRelation *relation;
254 gpointer target_object;
256 gchar *return_value = NULL;
261 combo_box = GTK_COMBO_BOX_ACCESSIBLE (action);
262 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (combo_box));
266 set = atk_object_ref_relation_set (ATK_OBJECT (action));
271 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
274 target = atk_relation_get_target (relation);
275 target_object = g_ptr_array_index (target, 0);
276 label = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
278 g_object_unref (set);
279 if (GTK_IS_LABEL (label))
281 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
282 if (key_val != GDK_KEY_VoidSymbol)
283 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
290 gtk_combo_box_accessible_action_get_name (AtkAction *action,
300 atk_action_interface_init (AtkActionIface *iface)
302 iface->do_action = gtk_combo_box_accessible_do_action;
303 iface->get_n_actions = gtk_combo_box_accessible_get_n_actions;
304 iface->get_keybinding = gtk_combo_box_accessible_get_keybinding;
305 iface->get_name = gtk_combo_box_accessible_action_get_name;
309 gtk_combo_box_accessible_add_selection (AtkSelection *selection,
314 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
318 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), i);
324 gtk_combo_box_accessible_clear_selection (AtkSelection *selection)
328 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
332 gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);
338 gtk_combo_box_accessible_ref_selection (AtkSelection *selection,
341 GtkComboBox *combo_box;
346 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
353 combo_box = GTK_COMBO_BOX (widget);
355 obj = gtk_combo_box_get_popup_accessible (combo_box);
356 index = gtk_combo_box_get_active (combo_box);
358 return atk_object_ref_accessible_child (obj, index);
362 gtk_combo_box_accessible_get_selection_count (AtkSelection *selection)
366 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
370 return (gtk_combo_box_get_active (GTK_COMBO_BOX (widget)) == -1) ? 0 : 1;
374 gtk_combo_box_accessible_is_child_selected (AtkSelection *selection,
380 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
385 j = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
391 gtk_combo_box_accessible_remove_selection (AtkSelection *selection,
394 if (atk_selection_is_child_selected (selection, i))
395 atk_selection_clear_selection (selection);
401 atk_selection_interface_init (AtkSelectionIface *iface)
403 iface->add_selection = gtk_combo_box_accessible_add_selection;
404 iface->clear_selection = gtk_combo_box_accessible_clear_selection;
405 iface->ref_selection = gtk_combo_box_accessible_ref_selection;
406 iface->get_selection_count = gtk_combo_box_accessible_get_selection_count;
407 iface->is_child_selected = gtk_combo_box_accessible_is_child_selected;
408 iface->remove_selection = gtk_combo_box_accessible_remove_selection;