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 <gdk/gdkkeysyms.h>
24 #include "gailcombobox.h"
26 static void gail_combo_box_class_init (GailComboBoxClass *klass);
27 static void gail_combo_box_init (GailComboBox *combo_box);
28 static void gail_combo_box_real_initialize (AtkObject *obj,
31 static void gail_combo_box_changed_gtk (GtkWidget *widget);
33 static G_CONST_RETURN gchar* gail_combo_box_get_name (AtkObject *obj);
34 static gint gail_combo_box_get_n_children (AtkObject *obj);
35 static AtkObject* gail_combo_box_ref_child (AtkObject *obj,
37 static void gail_combo_box_finalize (GObject *object);
38 static void atk_action_interface_init (AtkActionIface *iface);
40 static gboolean gail_combo_box_do_action (AtkAction *action,
42 static gboolean idle_do_action (gpointer data);
43 static gint gail_combo_box_get_n_actions (AtkAction *action);
44 static G_CONST_RETURN gchar* gail_combo_box_get_description(AtkAction *action,
46 static G_CONST_RETURN gchar* gail_combo_box_get_keybinding (AtkAction *action,
48 static G_CONST_RETURN gchar* gail_combo_box_action_get_name(AtkAction *action,
50 static gboolean gail_combo_box_set_description(AtkAction *action,
53 static void atk_selection_interface_init (AtkSelectionIface *iface);
54 static gboolean gail_combo_box_add_selection (AtkSelection *selection,
56 static gboolean gail_combo_box_clear_selection (AtkSelection *selection);
57 static AtkObject* gail_combo_box_ref_selection (AtkSelection *selection,
59 static gint gail_combo_box_get_selection_count (AtkSelection *selection);
60 static gboolean gail_combo_box_is_child_selected (AtkSelection *selection,
62 static gboolean gail_combo_box_remove_selection (AtkSelection *selection,
65 G_DEFINE_TYPE_WITH_CODE (GailComboBox, gail_combo_box, GAIL_TYPE_CONTAINER,
66 G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
67 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
70 gail_combo_box_class_init (GailComboBoxClass *klass)
72 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
73 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
75 gobject_class->finalize = gail_combo_box_finalize;
77 class->get_name = gail_combo_box_get_name;
78 class->get_n_children = gail_combo_box_get_n_children;
79 class->ref_child = gail_combo_box_ref_child;
80 class->initialize = gail_combo_box_real_initialize;
84 gail_combo_box_init (GailComboBox *combo_box)
86 combo_box->press_description = NULL;
87 combo_box->press_keybinding = NULL;
88 combo_box->old_selection = -1;
89 combo_box->name = NULL;
90 combo_box->popup_set = FALSE;
94 gail_combo_box_real_initialize (AtkObject *obj,
97 GtkComboBox *combo_box;
98 GailComboBox *gail_combo_box;
101 ATK_OBJECT_CLASS (gail_combo_box_parent_class)->initialize (obj, data);
103 combo_box = GTK_COMBO_BOX (data);
105 gail_combo_box = GAIL_COMBO_BOX (obj);
107 g_signal_connect (combo_box,
109 G_CALLBACK (gail_combo_box_changed_gtk),
111 gail_combo_box->old_selection = gtk_combo_box_get_active (combo_box);
113 popup = gtk_combo_box_get_popup_accessible (combo_box);
116 atk_object_set_parent (popup, obj);
117 gail_combo_box->popup_set = TRUE;
119 if (GTK_IS_COMBO_BOX_ENTRY (combo_box))
120 atk_object_set_parent (gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (combo_box))), obj);
122 obj->role = ATK_ROLE_COMBO_BOX;
126 gail_combo_box_changed_gtk (GtkWidget *widget)
128 GtkComboBox *combo_box;
130 GailComboBox *gail_combo_box;
133 combo_box = GTK_COMBO_BOX (widget);
135 index = gtk_combo_box_get_active (combo_box);
136 obj = gtk_widget_get_accessible (widget);
137 gail_combo_box = GAIL_COMBO_BOX (obj);
138 if (gail_combo_box->old_selection != index)
140 gail_combo_box->old_selection = index;
141 g_object_notify (G_OBJECT (obj), "accessible-name");
142 g_signal_emit_by_name (obj, "selection_changed");
146 static G_CONST_RETURN gchar*
147 gail_combo_box_get_name (AtkObject *obj)
150 GtkComboBox *combo_box;
151 GailComboBox *gail_combo_box;
153 G_CONST_RETURN gchar *name;
158 g_return_val_if_fail (GAIL_IS_COMBO_BOX (obj), NULL);
160 name = ATK_OBJECT_CLASS (gail_combo_box_parent_class)->get_name (obj);
164 widget = GTK_ACCESSIBLE (obj)->widget;
171 combo_box = GTK_COMBO_BOX (widget);
172 gail_combo_box = GAIL_COMBO_BOX (obj);
173 if (gtk_combo_box_get_active_iter (combo_box, &iter))
175 model = gtk_combo_box_get_model (combo_box);
176 n_columns = gtk_tree_model_get_n_columns (model);
177 for (i = 0; i < n_columns; i++)
179 GValue value = { 0, };
181 gtk_tree_model_get_value (model, &iter, i, &value);
182 if (G_VALUE_HOLDS_STRING (&value))
184 if (gail_combo_box->name) g_free (gail_combo_box->name);
185 gail_combo_box->name = g_strdup ((gchar *)
186 g_value_get_string (&value));
187 g_value_unset (&value);
191 g_value_unset (&value);
194 return gail_combo_box->name;
198 * The children of a GailComboBox are the list of items and the entry field
202 gail_combo_box_get_n_children (AtkObject* obj)
207 g_return_val_if_fail (GAIL_IS_COMBO_BOX (obj), 0);
209 widget = GTK_ACCESSIBLE (obj)->widget;
217 if (GTK_IS_COMBO_BOX_ENTRY (widget))
224 gail_combo_box_ref_child (AtkObject *obj,
231 g_return_val_if_fail (GAIL_IS_COMBO_BOX (obj), NULL);
233 widget = GTK_ACCESSIBLE (obj)->widget;
243 child = gtk_combo_box_get_popup_accessible (GTK_COMBO_BOX (widget));
244 box = GAIL_COMBO_BOX (obj);
245 if (box->popup_set == FALSE)
247 atk_object_set_parent (child, obj);
248 box->popup_set = TRUE;
251 else if (i == 1 && GTK_IS_COMBO_BOX_ENTRY (widget))
253 child = gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (widget)));
259 return g_object_ref (child);
263 atk_action_interface_init (AtkActionIface *iface)
265 iface->do_action = gail_combo_box_do_action;
266 iface->get_n_actions = gail_combo_box_get_n_actions;
267 iface->get_description = gail_combo_box_get_description;
268 iface->get_keybinding = gail_combo_box_get_keybinding;
269 iface->get_name = gail_combo_box_action_get_name;
270 iface->set_description = gail_combo_box_set_description;
274 gail_combo_box_do_action (AtkAction *action,
277 GailComboBox *combo_box;
280 widget = GTK_ACCESSIBLE (action)->widget;
287 if (!gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
290 combo_box = GAIL_COMBO_BOX (action);
293 if (combo_box->action_idle_handler)
296 combo_box->action_idle_handler = gdk_threads_add_idle (idle_do_action, combo_box);
304 idle_do_action (gpointer data)
306 GtkComboBox *combo_box;
308 GailComboBox *gail_combo_box;
312 gail_combo_box = GAIL_COMBO_BOX (data);
313 gail_combo_box->action_idle_handler = 0;
314 widget = GTK_ACCESSIBLE (gail_combo_box)->widget;
315 if (widget == NULL || /* State is defunct */
316 !gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
319 combo_box = GTK_COMBO_BOX (widget);
321 popup = gtk_combo_box_get_popup_accessible (combo_box);
322 do_popup = !GTK_WIDGET_MAPPED (GTK_ACCESSIBLE (popup)->widget);
324 gtk_combo_box_popup (combo_box);
326 gtk_combo_box_popdown (combo_box);
332 gail_combo_box_get_n_actions (AtkAction *action)
335 * The default behavior of a combo_box box is to have one action -
340 static G_CONST_RETURN gchar*
341 gail_combo_box_get_description (AtkAction *action,
346 GailComboBox *combo_box;
348 combo_box = GAIL_COMBO_BOX (action);
349 return combo_box->press_description;
355 static G_CONST_RETURN gchar*
356 gail_combo_box_get_keybinding (AtkAction *action,
359 GailComboBox *combo_box;
360 gchar *return_value = NULL;
368 AtkRelation *relation;
370 gpointer target_object;
373 combo_box = GAIL_COMBO_BOX (action);
374 widget = GTK_ACCESSIBLE (combo_box)->widget;
377 set = atk_object_ref_relation_set (ATK_OBJECT (action));
381 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
384 target = atk_relation_get_target (relation);
385 target_object = g_ptr_array_index (target, 0);
386 if (GTK_IS_ACCESSIBLE (target_object))
388 label = GTK_ACCESSIBLE (target_object)->widget;
391 g_object_unref (set);
392 if (GTK_IS_LABEL (label))
394 key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
395 if (key_val != GDK_VoidSymbol)
396 return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
398 g_free (combo_box->press_keybinding);
399 combo_box->press_keybinding = return_value;
409 static G_CONST_RETURN gchar*
410 gail_combo_box_action_get_name (AtkAction *action,
420 gail_combo_box_set_description (AtkAction *action,
426 GailComboBox *combo_box;
428 combo_box = GAIL_COMBO_BOX (action);
429 g_free (combo_box->press_description);
430 combo_box->press_description = g_strdup (desc);
438 atk_selection_interface_init (AtkSelectionIface *iface)
440 iface->add_selection = gail_combo_box_add_selection;
441 iface->clear_selection = gail_combo_box_clear_selection;
442 iface->ref_selection = gail_combo_box_ref_selection;
443 iface->get_selection_count = gail_combo_box_get_selection_count;
444 iface->is_child_selected = gail_combo_box_is_child_selected;
445 iface->remove_selection = gail_combo_box_remove_selection;
447 * select_all_selection does not make sense for a combo_box
448 * so no implementation is provided.
453 gail_combo_box_add_selection (AtkSelection *selection,
456 GtkComboBox *combo_box;
459 widget = GTK_ACCESSIBLE (selection)->widget;
466 combo_box = GTK_COMBO_BOX (widget);
468 gtk_combo_box_set_active (combo_box, i);
473 gail_combo_box_clear_selection (AtkSelection *selection)
475 GtkComboBox *combo_box;
478 widget = GTK_ACCESSIBLE (selection)->widget;
485 combo_box = GTK_COMBO_BOX (widget);
487 gtk_combo_box_set_active (combo_box, -1);
492 gail_combo_box_ref_selection (AtkSelection *selection,
495 GtkComboBox *combo_box;
500 widget = GTK_ACCESSIBLE (selection)->widget;
507 combo_box = GTK_COMBO_BOX (widget);
510 * A combo_box box can have only one selection.
515 obj = gtk_combo_box_get_popup_accessible (combo_box);
516 index = gtk_combo_box_get_active (combo_box);
517 return atk_object_ref_accessible_child (obj, index);
521 gail_combo_box_get_selection_count (AtkSelection *selection)
523 GtkComboBox *combo_box;
526 widget = GTK_ACCESSIBLE (selection)->widget;
533 combo_box = GTK_COMBO_BOX (widget);
535 return (gtk_combo_box_get_active (combo_box) == -1) ? 0 : 1;
539 gail_combo_box_is_child_selected (AtkSelection *selection,
542 GtkComboBox *combo_box;
546 widget = GTK_ACCESSIBLE (selection)->widget;
553 combo_box = GTK_COMBO_BOX (widget);
555 j = gtk_combo_box_get_active (combo_box);
561 gail_combo_box_remove_selection (AtkSelection *selection,
564 if (atk_selection_is_child_selected (selection, i))
565 atk_selection_clear_selection (selection);
571 gail_combo_box_finalize (GObject *object)
573 GailComboBox *combo_box = GAIL_COMBO_BOX (object);
575 g_free (combo_box->press_description);
576 g_free (combo_box->press_keybinding);
577 g_free (combo_box->name);
578 if (combo_box->action_idle_handler)
580 g_source_remove (combo_box->action_idle_handler);
581 combo_box->action_idle_handler = 0;
583 G_OBJECT_CLASS (gail_combo_box_parent_class)->finalize (object);