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_get_widget (GTK_ACCESSIBLE (obj));
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_get_widget (GTK_ACCESSIBLE (obj));
218 if (GTK_IS_COMBO_BOX_ENTRY (widget))
225 gail_combo_box_ref_child (AtkObject *obj,
232 g_return_val_if_fail (GAIL_IS_COMBO_BOX (obj), NULL);
234 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
244 child = gtk_combo_box_get_popup_accessible (GTK_COMBO_BOX (widget));
245 box = GAIL_COMBO_BOX (obj);
246 if (box->popup_set == FALSE)
248 atk_object_set_parent (child, obj);
249 box->popup_set = TRUE;
252 else if (i == 1 && GTK_IS_COMBO_BOX_ENTRY (widget))
254 child = gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (widget)));
260 return g_object_ref (child);
264 atk_action_interface_init (AtkActionIface *iface)
266 iface->do_action = gail_combo_box_do_action;
267 iface->get_n_actions = gail_combo_box_get_n_actions;
268 iface->get_description = gail_combo_box_get_description;
269 iface->get_keybinding = gail_combo_box_get_keybinding;
270 iface->get_name = gail_combo_box_action_get_name;
271 iface->set_description = gail_combo_box_set_description;
275 gail_combo_box_do_action (AtkAction *action,
278 GailComboBox *combo_box;
281 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
289 if (!gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
292 combo_box = GAIL_COMBO_BOX (action);
295 if (combo_box->action_idle_handler)
298 combo_box->action_idle_handler = gdk_threads_add_idle (idle_do_action, combo_box);
306 idle_do_action (gpointer data)
308 GtkComboBox *combo_box;
310 GailComboBox *gail_combo_box;
314 gail_combo_box = GAIL_COMBO_BOX (data);
315 gail_combo_box->action_idle_handler = 0;
316 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (gail_combo_box));
318 if (widget == NULL || /* State is defunct */
319 !gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
322 combo_box = GTK_COMBO_BOX (widget);
324 popup = gtk_combo_box_get_popup_accessible (combo_box);
325 do_popup = !gtk_widget_get_mapped (gtk_accessible_get_widget (GTK_ACCESSIBLE (popup)));
327 gtk_combo_box_popup (combo_box);
329 gtk_combo_box_popdown (combo_box);
335 gail_combo_box_get_n_actions (AtkAction *action)
338 * The default behavior of a combo_box box is to have one action -
343 static G_CONST_RETURN gchar*
344 gail_combo_box_get_description (AtkAction *action,
349 GailComboBox *combo_box;
351 combo_box = GAIL_COMBO_BOX (action);
352 return combo_box->press_description;
358 static G_CONST_RETURN gchar*
359 gail_combo_box_get_keybinding (AtkAction *action,
362 GailComboBox *combo_box;
363 gchar *return_value = NULL;
371 AtkRelation *relation;
373 gpointer target_object;
376 combo_box = GAIL_COMBO_BOX (action);
377 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (combo_box));
380 set = atk_object_ref_relation_set (ATK_OBJECT (action));
384 relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
387 target = atk_relation_get_target (relation);
388 target_object = g_ptr_array_index (target, 0);
389 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
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_KEY_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_get_widget (GTK_ACCESSIBLE (selection));
467 combo_box = GTK_COMBO_BOX (widget);
469 gtk_combo_box_set_active (combo_box, i);
474 gail_combo_box_clear_selection (AtkSelection *selection)
476 GtkComboBox *combo_box;
479 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
487 combo_box = GTK_COMBO_BOX (widget);
489 gtk_combo_box_set_active (combo_box, -1);
494 gail_combo_box_ref_selection (AtkSelection *selection,
497 GtkComboBox *combo_box;
502 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
510 combo_box = GTK_COMBO_BOX (widget);
513 * A combo_box box can have only one selection.
518 obj = gtk_combo_box_get_popup_accessible (combo_box);
519 index = gtk_combo_box_get_active (combo_box);
520 return atk_object_ref_accessible_child (obj, index);
524 gail_combo_box_get_selection_count (AtkSelection *selection)
526 GtkComboBox *combo_box;
529 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
537 combo_box = GTK_COMBO_BOX (widget);
539 return (gtk_combo_box_get_active (combo_box) == -1) ? 0 : 1;
543 gail_combo_box_is_child_selected (AtkSelection *selection,
546 GtkComboBox *combo_box;
550 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
558 combo_box = GTK_COMBO_BOX (widget);
560 j = gtk_combo_box_get_active (combo_box);
566 gail_combo_box_remove_selection (AtkSelection *selection,
569 if (atk_selection_is_child_selected (selection, i))
570 atk_selection_clear_selection (selection);
576 gail_combo_box_finalize (GObject *object)
578 GailComboBox *combo_box = GAIL_COMBO_BOX (object);
580 g_free (combo_box->press_description);
581 g_free (combo_box->press_keybinding);
582 g_free (combo_box->name);
583 if (combo_box->action_idle_handler)
585 g_source_remove (combo_box->action_idle_handler);
586 combo_box->action_idle_handler = 0;
588 G_OBJECT_CLASS (gail_combo_box_parent_class)->finalize (object);