]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtkcellaccessible.c
GtkRadioButtonAccessible: add a private struct
[~andy/gtk] / gtk / a11y / gtkcellaccessible.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2001 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 "gtkcontainercellaccessible.h"
22 #include "gtkcellaccessible.h"
23 #include "gtkcellaccessibleparent.h"
24
25 static const struct {
26   AtkState atk_state;
27   GtkCellRendererState renderer_state;
28   gboolean invert;
29 } state_map[] = {
30   { ATK_STATE_SENSITIVE, GTK_CELL_RENDERER_INSENSITIVE, TRUE },
31   { ATK_STATE_ENABLED,   GTK_CELL_RENDERER_INSENSITIVE, TRUE },
32   { ATK_STATE_SELECTED,  GTK_CELL_RENDERER_SELECTED,    FALSE },
33   /* XXX: why do we map ACTIVE here? */
34   { ATK_STATE_ACTIVE,    GTK_CELL_RENDERER_FOCUSED,     FALSE },
35   { ATK_STATE_FOCUSED,   GTK_CELL_RENDERER_FOCUSED,     FALSE },
36   { ATK_STATE_EXPANDABLE,GTK_CELL_RENDERER_EXPANDABLE,  FALSE },
37   { ATK_STATE_EXPANDED,  GTK_CELL_RENDERER_EXPANDED,    FALSE },
38 };
39
40 static void atk_action_interface_init    (AtkActionIface    *iface);
41 static void atk_component_interface_init (AtkComponentIface *iface);
42
43 G_DEFINE_TYPE_WITH_CODE (GtkCellAccessible, _gtk_cell_accessible, GTK_TYPE_ACCESSIBLE,
44                          G_IMPLEMENT_INTERFACE (ATK_TYPE_ACTION, atk_action_interface_init)
45                          G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, atk_component_interface_init))
46
47 static void
48 gtk_cell_accessible_object_finalize (GObject *obj)
49 {
50   AtkRelationSet *relation_set;
51   AtkRelation *relation;
52   GPtrArray *target;
53   gpointer target_object;
54   gint i;
55
56   relation_set = atk_object_ref_relation_set (ATK_OBJECT (obj));
57   if (ATK_IS_RELATION_SET (relation_set))
58     {
59       relation = atk_relation_set_get_relation_by_type (relation_set,
60                                                         ATK_RELATION_NODE_CHILD_OF);
61       if (relation)
62         {
63           target = atk_relation_get_target (relation);
64           for (i = 0; i < target->len; i++)
65             {
66               target_object = g_ptr_array_index (target, i);
67               if (GTK_IS_CELL_ACCESSIBLE (target_object))
68                 g_object_unref (target_object);
69             }
70         }
71       g_object_unref (relation_set);
72     }
73   G_OBJECT_CLASS (_gtk_cell_accessible_parent_class)->finalize (obj);
74 }
75
76 static gint
77 gtk_cell_accessible_get_index_in_parent (AtkObject *obj)
78 {
79   GtkCellAccessible *cell;
80   AtkObject *parent;
81
82   cell = GTK_CELL_ACCESSIBLE (obj);
83
84   parent = atk_object_get_parent (obj);
85   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
86     return g_list_index (_gtk_container_cell_accessible_get_children (GTK_CONTAINER_CELL_ACCESSIBLE (parent)), obj);
87
88   parent = gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)));
89   if (parent == NULL)
90     return -1;
91
92   return _gtk_cell_accessible_parent_get_child_index (GTK_CELL_ACCESSIBLE_PARENT (parent), cell);
93 }
94
95 static AtkStateSet *
96 gtk_cell_accessible_ref_state_set (AtkObject *accessible)
97 {
98   GtkCellAccessible *cell_accessible;
99   AtkStateSet *state_set;
100   GtkCellRendererState flags;
101   guint i;
102
103   cell_accessible = GTK_CELL_ACCESSIBLE (accessible);
104
105   state_set = atk_state_set_new ();
106
107   if (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell_accessible)) == NULL)
108     {
109       atk_state_set_add_state (state_set, ATK_STATE_DEFUNCT);
110       return state_set;
111     }
112
113   flags = _gtk_cell_accessible_get_state (cell_accessible);
114
115   atk_state_set_add_state (state_set, ATK_STATE_FOCUSABLE);
116   atk_state_set_add_state (state_set, ATK_STATE_SELECTABLE);
117   atk_state_set_add_state (state_set, ATK_STATE_TRANSIENT);
118   atk_state_set_add_state (state_set, ATK_STATE_VISIBLE);
119
120   for (i = 0; i < G_N_ELEMENTS (state_map); i++)
121     {
122       if (flags & state_map[i].renderer_state)
123         {
124           if (!state_map[i].invert)
125             atk_state_set_add_state (state_set, state_map[i].atk_state);
126         }
127       else
128         {
129           if (state_map[i].invert)
130             atk_state_set_add_state (state_set, state_map[i].atk_state);
131         }
132     }
133
134   if (gtk_widget_get_mapped (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell_accessible))))
135     atk_state_set_add_state (state_set, ATK_STATE_SHOWING);
136
137   return state_set;
138 }
139
140
141 static void
142 _gtk_cell_accessible_class_init (GtkCellAccessibleClass *klass)
143 {
144   AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
145   GObjectClass *g_object_class = G_OBJECT_CLASS (klass);
146
147   g_object_class->finalize = gtk_cell_accessible_object_finalize;
148
149   class->get_index_in_parent = gtk_cell_accessible_get_index_in_parent;
150   class->ref_state_set = gtk_cell_accessible_ref_state_set;
151 }
152
153 static void
154 _gtk_cell_accessible_init (GtkCellAccessible *cell)
155 {
156 }
157
158 void
159 _gtk_cell_accessible_initialise (GtkCellAccessible *cell,
160                                  GtkWidget         *widget,
161                                  AtkObject         *parent)
162 {
163   gtk_accessible_set_widget (GTK_ACCESSIBLE (cell), widget);
164   atk_object_set_parent (ATK_OBJECT (cell), parent);
165 }
166
167 gboolean
168 _gtk_cell_accessible_add_state (GtkCellAccessible *cell,
169                                 AtkStateType       state_type,
170                                 gboolean           emit_signal)
171 {
172   AtkObject *parent;
173
174   /* The signal should only be generated if the value changed,
175    * not when the cell is set up. So states that are set
176    * initially should pass FALSE as the emit_signal argument.
177    */
178   if (emit_signal)
179     {
180       atk_object_notify_state_change (ATK_OBJECT (cell), state_type, TRUE);
181       /* If state_type is ATK_STATE_VISIBLE, additional notification */
182       if (state_type == ATK_STATE_VISIBLE)
183         g_signal_emit_by_name (cell, "visible-data-changed");
184     }
185
186   /* If the parent is a flyweight container cell, propagate the state
187    * change to it also
188    */
189   parent = atk_object_get_parent (ATK_OBJECT (cell));
190   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
191     _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (parent), state_type, emit_signal);
192
193   return TRUE;
194 }
195
196 gboolean
197 _gtk_cell_accessible_remove_state (GtkCellAccessible *cell,
198                                    AtkStateType       state_type,
199                                    gboolean           emit_signal)
200 {
201   AtkObject *parent;
202
203   parent = atk_object_get_parent (ATK_OBJECT (cell));
204
205   /* The signal should only be generated if the value changed,
206    * not when the cell is set up.  So states that are set
207    * initially should pass FALSE as the emit_signal argument.
208    */
209   if (emit_signal)
210     {
211       atk_object_notify_state_change (ATK_OBJECT (cell), state_type, FALSE);
212       /* If state_type is ATK_STATE_VISIBLE, additional notification */
213       if (state_type == ATK_STATE_VISIBLE)
214         g_signal_emit_by_name (cell, "visible-data-changed");
215     }
216
217   /* If the parent is a flyweight container cell, propagate the state
218    * change to it also
219    */
220   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
221     _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (parent), state_type, emit_signal);
222
223   return TRUE;
224 }
225
226 static gint
227 gtk_cell_accessible_action_get_n_actions (AtkAction *action)
228 {
229   return 3;
230 }
231
232 static const gchar *
233 gtk_cell_accessible_action_get_name (AtkAction *action,
234                                      gint       index)
235 {
236   switch (index)
237     {
238     case 0:
239       return "expand or contract";
240     case 1:
241       return "edit";
242     case 2:
243       return "activate";
244     default:
245       return NULL;
246     }
247 }
248
249 static const gchar *
250 gtk_cell_accessible_action_get_description (AtkAction *action,
251                                             gint       index)
252 {
253   switch (index)
254     {
255     case 0:
256       return "expands or contracts the row in the tree view containing this cell";
257     case 1:
258       return "creates a widget in which the contents of the cell can be edited";
259     case 2:
260       return "activate the cell";
261     default:
262       return NULL;
263     }
264 }
265
266 static const gchar *
267 gtk_cell_accessible_action_get_keybinding (AtkAction *action,
268                                            gint       index)
269 {
270   return NULL;
271 }
272
273 static gboolean
274 gtk_cell_accessible_action_do_action (AtkAction *action,
275                                       gint       index)
276 {
277   GtkCellAccessible *cell = GTK_CELL_ACCESSIBLE (action);
278   GtkCellAccessibleParent *parent;
279
280   cell = GTK_CELL_ACCESSIBLE (action);
281   if (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)) == NULL)
282     return FALSE;
283
284   parent = GTK_CELL_ACCESSIBLE_PARENT (gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell))));
285
286   switch (index)
287     {
288     case 0:
289       _gtk_cell_accessible_parent_expand_collapse (parent, cell);
290     case 1:
291       _gtk_cell_accessible_parent_edit (parent, cell);
292     case 2:
293       _gtk_cell_accessible_parent_activate (parent, cell);
294     default:
295       return FALSE;
296     }
297
298   return TRUE;
299 }
300
301 static void
302 atk_action_interface_init (AtkActionIface *iface)
303 {
304   iface->get_n_actions = gtk_cell_accessible_action_get_n_actions;
305   iface->do_action = gtk_cell_accessible_action_do_action;
306   iface->get_name = gtk_cell_accessible_action_get_name;
307   iface->get_description = gtk_cell_accessible_action_get_description;
308   iface->get_keybinding = gtk_cell_accessible_action_get_keybinding;
309 }
310
311 static void
312 gtk_cell_accessible_get_extents (AtkComponent *component,
313                                  gint         *x,
314                                  gint         *y,
315                                  gint         *width,
316                                  gint         *height,
317                                  AtkCoordType  coord_type)
318 {
319   GtkCellAccessible *cell;
320   AtkObject *parent;
321
322   cell = GTK_CELL_ACCESSIBLE (component);
323   parent = gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)));
324
325   _gtk_cell_accessible_parent_get_cell_extents (GTK_CELL_ACCESSIBLE_PARENT (parent),
326                                                 cell,
327                                                 x, y, width, height, coord_type);
328 }
329
330 static gboolean
331 gtk_cell_accessible_grab_focus (AtkComponent *component)
332 {
333   GtkCellAccessible *cell;
334   AtkObject *parent;
335
336   cell = GTK_CELL_ACCESSIBLE (component);
337   parent = gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)));
338
339   return _gtk_cell_accessible_parent_grab_focus (GTK_CELL_ACCESSIBLE_PARENT (parent), cell);
340 }
341
342 static void
343 atk_component_interface_init (AtkComponentIface *iface)
344 {
345   iface->get_extents = gtk_cell_accessible_get_extents;
346   iface->grab_focus = gtk_cell_accessible_grab_focus;
347 }
348
349 /**
350  * _gtk_cell_accessible_get_state:
351  * @cell: a #GtkCellAccessible
352  *
353  * Gets the state that would be used to render the area referenced by @cell.
354  *
355  * Returns: the #GtkCellRendererState for cell
356  **/
357 GtkCellRendererState
358 _gtk_cell_accessible_get_state (GtkCellAccessible *cell)
359 {
360   AtkObject *parent;
361
362   g_return_val_if_fail (GTK_IS_CELL_ACCESSIBLE (cell), 0);
363
364   parent = gtk_widget_get_accessible (gtk_accessible_get_widget (GTK_ACCESSIBLE (cell)));
365   if (parent == NULL)
366     return 0;
367
368   return _gtk_cell_accessible_parent_get_renderer_state (GTK_CELL_ACCESSIBLE_PARENT (parent), cell);
369 }
370
371 /**
372  * _gtk_cell_accessible_state_changed:
373  * @cell: a #GtkCellAccessible
374  * @added: the flags that were added from @cell
375  * @removed: the flags that were removed from @cell
376  *
377  * Notifies @cell of state changes. Multiple states may be added
378  * or removed at the same time. A state that is @added may not be
379  * @removed at the same time.
380  **/
381 void
382 _gtk_cell_accessible_state_changed (GtkCellAccessible    *cell,
383                                     GtkCellRendererState  added,
384                                     GtkCellRendererState  removed)
385 {
386   AtkObject *object;
387   guint i;
388
389   g_return_if_fail (GTK_IS_CELL_ACCESSIBLE (cell));
390   g_return_if_fail ((added & removed) == 0);
391
392   object = ATK_OBJECT (cell);
393
394   for (i = 0; i < G_N_ELEMENTS (state_map); i++)
395     {
396       if (added & state_map[i].renderer_state)
397         atk_object_notify_state_change (object, 
398                                         state_map[i].atk_state,
399                                         !state_map[i].invert);
400       if (added & state_map[i].renderer_state)
401         atk_object_notify_state_change (object, 
402                                         state_map[i].atk_state,
403                                         state_map[i].invert);
404     }
405 }
406
407 /**
408  * _gtk_cell_accessible_update_cache:
409  * @cell: the cell that is changed
410  *
411  * Notifies the cell that the values in the data in the row that
412  * is used to feed the cell renderer with has changed. The
413  * cell_changed function of @cell is called to send update
414  * notifications for the properties it takes from its cell
415  * renderer.
416  * 
417  * Note that there is no higher granularity available about which
418  * properties changed, so you will need to make do with this
419  * function.
420  **/
421 void
422 _gtk_cell_accessible_update_cache (GtkCellAccessible *cell)
423 {
424   GtkCellAccessibleClass *klass;
425   
426   g_return_if_fail (GTK_CELL_ACCESSIBLE (cell));
427
428   klass = GTK_CELL_ACCESSIBLE_GET_CLASS (cell);
429
430   if (klass->update_cache)
431     klass->update_cache (cell);
432 }
433