]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtkcomboboxaccessible.c
Change FSF Address
[~andy/gtk] / gtk / a11y / gtkcomboboxaccessible.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2004 Sun Microsystems Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 #include "config.h"
19
20 #include <gtk/gtk.h>
21 #include "gtkcomboboxaccessible.h"
22
23
24 static void atk_action_interface_init    (AtkActionIface    *iface);
25 static void atk_selection_interface_init (AtkSelectionIface *iface);
26
27 G_DEFINE_TYPE_WITH_CODE (GtkComboBoxAccessible, _gtk_combo_box_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
28                          G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
29                          G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init))
30
31 static void
32 changed_cb (GtkWidget *widget)
33 {
34   GtkComboBox *combo_box;
35   AtkObject *obj;
36   GtkComboBoxAccessible *accessible;
37   gint index;
38
39   combo_box = GTK_COMBO_BOX (widget);
40
41   index = gtk_combo_box_get_active (combo_box);
42   obj = gtk_widget_get_accessible (widget);
43   accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
44   if (accessible->old_selection != index)
45     {
46       accessible->old_selection = index;
47       g_object_notify (G_OBJECT (obj), "accessible-name");
48       g_signal_emit_by_name (obj, "selection-changed");
49     }
50 }
51
52 static void
53 gtk_combo_box_accessible_initialize (AtkObject *obj,
54                                      gpointer   data)
55 {
56   GtkComboBox *combo_box;
57   GtkComboBoxAccessible *accessible;
58   AtkObject *popup;
59
60   ATK_OBJECT_CLASS (_gtk_combo_box_accessible_parent_class)->initialize (obj, data);
61
62   combo_box = GTK_COMBO_BOX (data);
63   accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
64
65   g_signal_connect (combo_box, "changed", G_CALLBACK (changed_cb), NULL);
66   accessible->old_selection = gtk_combo_box_get_active (combo_box);
67
68   popup = gtk_combo_box_get_popup_accessible (combo_box);
69   if (popup)
70     {
71       atk_object_set_parent (popup, obj);
72       accessible->popup_set = TRUE;
73     }
74   if (gtk_combo_box_get_has_entry (combo_box))
75     atk_object_set_parent (gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (combo_box))), obj);
76
77   obj->role = ATK_ROLE_COMBO_BOX;
78 }
79
80 static void
81 gtk_combo_box_accessible_finalize (GObject *object)
82 {
83   GtkComboBoxAccessible *combo_box = GTK_COMBO_BOX_ACCESSIBLE (object);
84
85   g_free (combo_box->name);
86
87   G_OBJECT_CLASS (_gtk_combo_box_accessible_parent_class)->finalize (object);
88 }
89
90 static const gchar *
91 gtk_combo_box_accessible_get_name (AtkObject *obj)
92 {
93   GtkWidget *widget;
94   GtkComboBox *combo_box;
95   GtkComboBoxAccessible *accessible;
96   GtkTreeIter iter;
97   const gchar *name;
98   GtkTreeModel *model;
99   gint n_columns;
100   gint i;
101
102   name = ATK_OBJECT_CLASS (_gtk_combo_box_accessible_parent_class)->get_name (obj);
103   if (name)
104     return name;
105
106   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
107   if (widget == NULL)
108     return NULL;
109
110   combo_box = GTK_COMBO_BOX (widget);
111   accessible = GTK_COMBO_BOX_ACCESSIBLE (obj);
112   if (gtk_combo_box_get_active_iter (combo_box, &iter))
113     {
114       model = gtk_combo_box_get_model (combo_box);
115       n_columns = gtk_tree_model_get_n_columns (model);
116       for (i = 0; i < n_columns; i++)
117         {
118           GValue value = G_VALUE_INIT;
119
120           gtk_tree_model_get_value (model, &iter, i, &value);
121           if (G_VALUE_HOLDS_STRING (&value))
122             {
123               g_free (accessible->name);
124               accessible->name =  g_strdup (g_value_get_string (&value));
125               g_value_unset (&value);
126               break;
127             }
128           else
129             g_value_unset (&value);
130         }
131     }
132   return accessible->name;
133 }
134
135 static gint
136 gtk_combo_box_accessible_get_n_children (AtkObject* obj)
137 {
138   gint n_children = 0;
139   GtkWidget *widget;
140
141   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
142   if (widget == NULL)
143     return 0;
144
145   n_children++;
146   if (gtk_combo_box_get_has_entry (GTK_COMBO_BOX (widget)))
147     n_children++;
148
149   return n_children;
150 }
151
152 static AtkObject *
153 gtk_combo_box_accessible_ref_child (AtkObject *obj,
154                                     gint       i)
155 {
156   GtkWidget *widget;
157   AtkObject *child;
158   GtkComboBoxAccessible *box;
159
160   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
161   if (widget == NULL)
162     return NULL;
163
164   if (i == 0)
165     {
166       child = gtk_combo_box_get_popup_accessible (GTK_COMBO_BOX (widget));
167       box = GTK_COMBO_BOX_ACCESSIBLE (obj);
168       if (box->popup_set == FALSE)
169         {
170           atk_object_set_parent (child, obj);
171           box->popup_set = TRUE;
172         }
173     }
174   else if (i == 1 && gtk_combo_box_get_has_entry (GTK_COMBO_BOX (widget)))
175     {
176       child = gtk_widget_get_accessible (gtk_bin_get_child (GTK_BIN (widget)));
177     }
178   else
179     {
180       return NULL;
181     }
182
183   return g_object_ref (child);
184 }
185
186 static void
187 _gtk_combo_box_accessible_class_init (GtkComboBoxAccessibleClass *klass)
188 {
189   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
190   AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
191
192   gobject_class->finalize = gtk_combo_box_accessible_finalize;
193
194   class->get_name = gtk_combo_box_accessible_get_name;
195   class->get_n_children = gtk_combo_box_accessible_get_n_children;
196   class->ref_child = gtk_combo_box_accessible_ref_child;
197   class->initialize = gtk_combo_box_accessible_initialize;
198 }
199
200 static void
201 _gtk_combo_box_accessible_init (GtkComboBoxAccessible *combo_box)
202 {
203   combo_box->old_selection = -1;
204   combo_box->name = NULL;
205   combo_box->popup_set = FALSE;
206 }
207
208 static gboolean
209 gtk_combo_box_accessible_do_action (AtkAction *action,
210                                     gint       i)
211 {
212   GtkComboBox *combo_box;
213   GtkWidget *widget;
214   gboolean popup_shown;
215
216   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (action));
217   if (widget == NULL)
218     return FALSE;
219
220   if (!gtk_widget_get_sensitive (widget) || !gtk_widget_get_visible (widget))
221     return FALSE;
222
223   if (i != 0)
224     return FALSE;
225
226   combo_box = GTK_COMBO_BOX (widget);
227   g_object_get (combo_box, "popup-shown", &popup_shown, NULL);
228   if (popup_shown)
229     gtk_combo_box_popdown (combo_box);
230   else
231     gtk_combo_box_popup (combo_box);
232
233   return TRUE;
234 }
235
236 static gint
237 gtk_combo_box_accessible_get_n_actions (AtkAction *action)
238 {
239   return 1;
240 }
241
242 static const gchar *
243 gtk_combo_box_accessible_get_keybinding (AtkAction *action,
244                                          gint       i)
245 {
246   GtkComboBoxAccessible *combo_box;
247   GtkWidget *widget;
248   GtkWidget *label;
249   AtkRelationSet *set;
250   AtkRelation *relation;
251   GPtrArray *target;
252   gpointer target_object;
253   guint key_val;
254   gchar *return_value = NULL;
255
256   if (i != 0)
257     return NULL;
258
259   combo_box = GTK_COMBO_BOX_ACCESSIBLE (action);
260   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (combo_box));
261   if (widget == NULL)
262     return NULL;
263
264   set = atk_object_ref_relation_set (ATK_OBJECT (action));
265   if (set == NULL)
266     return NULL;
267
268   label = NULL;
269   relation = atk_relation_set_get_relation_by_type (set, ATK_RELATION_LABELLED_BY);
270   if (relation)
271     {
272       target = atk_relation_get_target (relation);
273       target_object = g_ptr_array_index (target, 0);
274       label = gtk_accessible_get_widget (GTK_ACCESSIBLE (target_object));
275     }
276   g_object_unref (set);
277   if (GTK_IS_LABEL (label))
278     {
279       key_val = gtk_label_get_mnemonic_keyval (GTK_LABEL (label));
280       if (key_val != GDK_KEY_VoidSymbol)
281         return_value = gtk_accelerator_name (key_val, GDK_MOD1_MASK);
282     }
283
284   return return_value;
285 }
286
287 static const gchar *
288 gtk_combo_box_accessible_action_get_name (AtkAction *action,
289                                           gint       i)
290 {
291   if (i != 0)
292     return NULL;
293
294   return "press";
295 }
296
297 static void
298 atk_action_interface_init (AtkActionIface *iface)
299 {
300   iface->do_action = gtk_combo_box_accessible_do_action;
301   iface->get_n_actions = gtk_combo_box_accessible_get_n_actions;
302   iface->get_keybinding = gtk_combo_box_accessible_get_keybinding;
303   iface->get_name = gtk_combo_box_accessible_action_get_name;
304 }
305
306 static gboolean
307 gtk_combo_box_accessible_add_selection (AtkSelection *selection,
308                                         gint          i)
309 {
310   GtkWidget *widget;
311
312   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
313   if (widget == NULL)
314     return FALSE;
315
316   gtk_combo_box_set_active (GTK_COMBO_BOX (widget), i);
317
318   return TRUE;
319 }
320
321 static gboolean
322 gtk_combo_box_accessible_clear_selection (AtkSelection *selection)
323 {
324   GtkWidget *widget;
325
326   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
327   if (widget == NULL)
328     return FALSE;
329
330   gtk_combo_box_set_active (GTK_COMBO_BOX (widget), -1);
331
332   return TRUE;
333 }
334
335 static AtkObject *
336 gtk_combo_box_accessible_ref_selection (AtkSelection *selection,
337                                         gint          i)
338 {
339   GtkComboBox *combo_box;
340   GtkWidget *widget;
341   AtkObject *obj;
342   gint index;
343
344   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
345   if (widget == NULL)
346     return NULL;
347
348   if (i != 0)
349     return NULL;
350
351   combo_box = GTK_COMBO_BOX (widget);
352
353   obj = gtk_combo_box_get_popup_accessible (combo_box);
354   index = gtk_combo_box_get_active (combo_box);
355
356   return atk_object_ref_accessible_child (obj, index);
357 }
358
359 static gint
360 gtk_combo_box_accessible_get_selection_count (AtkSelection *selection)
361 {
362   GtkWidget *widget;
363
364   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
365   if (widget == NULL)
366     return 0;
367
368   return (gtk_combo_box_get_active (GTK_COMBO_BOX (widget)) == -1) ? 0 : 1;
369 }
370
371 static gboolean
372 gtk_combo_box_accessible_is_child_selected (AtkSelection *selection,
373                                             gint          i)
374 {
375   GtkWidget *widget;
376   gint j;
377
378   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
379
380   if (widget == NULL)
381     return FALSE;
382
383   j = gtk_combo_box_get_active (GTK_COMBO_BOX (widget));
384
385   return (j == i);
386 }
387
388 static gboolean
389 gtk_combo_box_accessible_remove_selection (AtkSelection *selection,
390                                            gint          i)
391 {
392   if (atk_selection_is_child_selected (selection, i))
393     atk_selection_clear_selection (selection);
394
395   return TRUE;
396 }
397
398 static void
399 atk_selection_interface_init (AtkSelectionIface *iface)
400 {
401   iface->add_selection = gtk_combo_box_accessible_add_selection;
402   iface->clear_selection = gtk_combo_box_accessible_clear_selection;
403   iface->ref_selection = gtk_combo_box_accessible_ref_selection;
404   iface->get_selection_count = gtk_combo_box_accessible_get_selection_count;
405   iface->is_child_selected = gtk_combo_box_accessible_is_child_selected;
406   iface->remove_selection = gtk_combo_box_accessible_remove_selection;
407 }