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