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