]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtktreeviewaccessible.c
treeview: Add add() function to accessible
[~andy/gtk] / gtk / a11y / gtktreeviewaccessible.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2001, 2002, 2003 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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include <gtk/gtk.h>
23 #ifdef GDK_WINDOWING_X11
24 #include <gdk/x11/gdkx.h>
25 #endif
26
27 #include "gtktreeprivate.h"
28 #include "gtkwidgetprivate.h"
29
30 #include "gtktreeviewaccessible.h"
31 #include "gtkrenderercellaccessible.h"
32 #include "gtkbooleancellaccessible.h"
33 #include "gtkimagecellaccessible.h"
34 #include "gtkcontainercellaccessible.h"
35 #include "gtktextcellaccessible.h"
36 #include "gtkcellaccessibleparent.h"
37
38 typedef struct _GtkTreeViewAccessibleCellInfo  GtkTreeViewAccessibleCellInfo;
39 struct _GtkTreeViewAccessibleCellInfo
40 {
41   GtkCellAccessible *cell;
42   GtkRBTree *tree;
43   GtkRBNode *node;
44   GtkTreeViewColumn *cell_col_ref;
45   GtkTreeViewAccessible *view;
46 };
47
48 /* signal handling */
49
50 static void     selection_changed_cb (GtkTreeSelection *selection,
51                                       gpointer          data);
52
53 static void     cursor_changed       (GtkTreeView      *tree_view,
54                                       GtkTreeViewAccessible *accessible);
55 static gboolean focus_in             (GtkWidget        *widget);
56 static gboolean focus_out            (GtkWidget        *widget);
57
58 /* Misc */
59
60 static void             set_iter_nth_row                (GtkTreeView            *tree_view,
61                                                          GtkTreeIter            *iter,
62                                                          gint                   row);
63 static gint             get_row_from_tree_path          (GtkTreeView            *tree_view,
64                                                          GtkTreePath            *path);
65 static void             iterate_thru_children           (GtkTreeView            *tree_view,
66                                                          GtkTreeModel           *tree_model,
67                                                          GtkTreePath            *tree_path,
68                                                          GtkTreePath            *orig,
69                                                          gint                   *count,
70                                                          gint                   depth);
71 static int              cell_info_get_index             (GtkTreeView                     *tree_view,
72                                                          GtkTreeViewAccessibleCellInfo   *info);
73 static gboolean         update_cell_value               (GtkRendererCellAccessible       *renderer_cell,
74                                                          GtkTreeViewAccessible           *accessible,
75                                                          gboolean               emit_change_signal);
76 static gboolean         is_cell_showing                 (GtkTreeView            *tree_view,
77                                                          GdkRectangle           *cell_rect);
78
79 static void             cell_destroyed                  (gpointer               data);
80 static void             cell_info_new                   (GtkTreeViewAccessible           *accessible,
81                                                          GtkTreeModel           *tree_model,
82                                                          GtkRBTree              *tree,
83                                                          GtkRBNode              *node,
84                                                          GtkTreeViewColumn      *tv_col,
85                                                          GtkCellAccessible      *cell);
86 static GtkCellAccessible *find_cell                       (GtkTreeViewAccessible           *accessible,
87                                                          gint                   index);
88 static void             connect_model_signals           (GtkTreeView            *view,
89                                                          GtkTreeViewAccessible           *accessible);
90 static void             disconnect_model_signals        (GtkTreeViewAccessible           *accessible);
91 static gint             get_column_number               (GtkTreeView            *tree_view,
92                                                          GtkTreeViewColumn      *column);
93 static gint             get_focus_index                 (GtkTreeView            *tree_view);
94 static gint             get_index                       (GtkTreeView            *tree_view,
95                                                          GtkTreePath            *path,
96                                                          gint                   actual_column);
97 static void             count_rows                      (GtkTreeModel           *model,
98                                                          GtkTreeIter            *iter,
99                                                          GtkTreePath            *end_path,
100                                                          gint                   *count,
101                                                          gint                   level,
102                                                          gint                   depth);
103
104 static gboolean         get_rbtree_column_from_index    (GtkTreeView            *tree_view,
105                                                          gint                   index,
106                                                          GtkRBTree              **tree,
107                                                          GtkRBNode              **node,
108                                                          GtkTreeViewColumn      **column);
109
110 static GtkTreeViewAccessibleCellInfo* find_cell_info    (GtkTreeViewAccessible           *view,
111                                                          GtkCellAccessible               *cell);
112 static AtkObject *       get_header_from_column         (GtkTreeViewColumn      *tv_col);
113
114
115 static void atk_table_interface_init                  (AtkTableIface                *iface);
116 static void atk_selection_interface_init              (AtkSelectionIface            *iface);
117 static void atk_component_interface_init              (AtkComponentIface            *iface);
118 static void gtk_cell_accessible_parent_interface_init (GtkCellAccessibleParentIface *iface);
119
120 G_DEFINE_TYPE_WITH_CODE (GtkTreeViewAccessible, _gtk_tree_view_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
121                          G_IMPLEMENT_INTERFACE (ATK_TYPE_TABLE, atk_table_interface_init)
122                          G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init)
123                          G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, atk_component_interface_init)
124                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_ACCESSIBLE_PARENT, gtk_cell_accessible_parent_interface_init))
125
126
127 static GQuark
128 gtk_tree_view_accessible_get_data_quark (void)
129 {
130   static GQuark quark = 0;
131
132   if (G_UNLIKELY (quark == 0))
133     quark = g_quark_from_static_string ("gtk-tree-view-accessible-data");
134
135   return quark;
136 }
137
138 static void
139 cell_info_free (GtkTreeViewAccessibleCellInfo *cell_info)
140 {
141   if (cell_info->cell)
142     {
143       g_object_steal_qdata (G_OBJECT (cell_info->cell),
144                             gtk_tree_view_accessible_get_data_quark ());
145       _gtk_cell_accessible_add_state (cell_info->cell, ATK_STATE_DEFUNCT, FALSE);
146     }
147
148   g_free (cell_info);
149 }
150
151 static GtkTreePath *
152 cell_info_get_path (GtkTreeViewAccessibleCellInfo *cell_info)
153 {
154   return _gtk_tree_path_new_from_rbtree (cell_info->tree, cell_info->node);
155 }
156
157 static guint
158 cell_info_hash (gconstpointer info)
159 {
160   const GtkTreeViewAccessibleCellInfo *cell_info = info;
161   guint node, col;
162
163   node = GPOINTER_TO_UINT (cell_info->node);
164   col = GPOINTER_TO_UINT (cell_info->cell_col_ref);
165
166   return ((node << sizeof (guint) / 2) | (node >> sizeof (guint) / 2)) ^ col;
167 }
168
169 static gboolean
170 cell_info_equal (gconstpointer a, gconstpointer b)
171 {
172   const GtkTreeViewAccessibleCellInfo *cell_info_a = a;
173   const GtkTreeViewAccessibleCellInfo *cell_info_b = b;
174
175   return cell_info_a->node == cell_info_b->node &&
176          cell_info_a->cell_col_ref == cell_info_b->cell_col_ref;
177 }
178
179 static void
180 gtk_tree_view_accessible_initialize (AtkObject *obj,
181                                      gpointer   data)
182 {
183   GtkTreeViewAccessible *accessible;
184   GtkTreeView *tree_view;
185   GtkTreeModel *tree_model;
186   GtkWidget *widget;
187   GtkTreeSelection *selection;
188
189   ATK_OBJECT_CLASS (_gtk_tree_view_accessible_parent_class)->initialize (obj, data);
190
191   accessible = GTK_TREE_VIEW_ACCESSIBLE (obj);
192   accessible->focus_cell = NULL;
193
194   accessible->cell_infos = g_hash_table_new_full (cell_info_hash,
195       cell_info_equal, NULL, (GDestroyNotify) cell_info_free);
196
197   widget = GTK_WIDGET (data);
198   tree_view = GTK_TREE_VIEW (widget);
199   tree_model = gtk_tree_view_get_model (tree_view);
200   selection = gtk_tree_view_get_selection (tree_view);
201
202   g_signal_connect (selection, "changed",
203                     G_CALLBACK (selection_changed_cb), obj);
204
205   g_signal_connect (tree_view, "cursor-changed",
206                     G_CALLBACK (cursor_changed), accessible);
207   g_signal_connect (tree_view, "focus-in-event",
208                     G_CALLBACK (focus_in), NULL);
209   g_signal_connect (tree_view, "focus-out-event",
210                     G_CALLBACK (focus_out), NULL);
211
212   accessible->tree_model = tree_model;
213   if (tree_model)
214     {
215       g_object_add_weak_pointer (G_OBJECT (accessible->tree_model), (gpointer *)&accessible->tree_model);
216       connect_model_signals (tree_view, accessible);
217
218       if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
219         obj->role = ATK_ROLE_TABLE;
220       else
221         obj->role = ATK_ROLE_TREE_TABLE;
222     }
223 }
224
225 static void
226 gtk_tree_view_accessible_finalize (GObject *object)
227 {
228   GtkTreeViewAccessible *accessible = GTK_TREE_VIEW_ACCESSIBLE (object);
229
230   if (accessible->tree_model)
231     disconnect_model_signals (accessible);
232
233   if (accessible->cell_infos)
234     g_hash_table_destroy (accessible->cell_infos);
235
236   G_OBJECT_CLASS (_gtk_tree_view_accessible_parent_class)->finalize (object);
237 }
238
239 static void
240 gtk_tree_view_accessible_notify_gtk (GObject    *obj,
241                                      GParamSpec *pspec)
242 {
243   GtkWidget *widget;
244   GtkTreeView *tree_view;
245   GtkTreeViewAccessible *accessible;
246
247   widget = GTK_WIDGET (obj);
248   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
249   tree_view = GTK_TREE_VIEW (widget);
250
251   if (g_strcmp0 (pspec->name, "model") == 0)
252     {
253       GtkTreeModel *tree_model;
254       AtkRole role;
255
256       tree_model = gtk_tree_view_get_model (tree_view);
257       if (accessible->tree_model)
258         disconnect_model_signals (accessible);
259       g_hash_table_remove_all (accessible->cell_infos);
260       accessible->tree_model = tree_model;
261
262       if (tree_model)
263         {
264           g_object_add_weak_pointer (G_OBJECT (accessible->tree_model), (gpointer *)&accessible->tree_model);
265           connect_model_signals (tree_view, accessible);
266
267           if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
268             role = ATK_ROLE_TABLE;
269           else
270             role = ATK_ROLE_TREE_TABLE;
271         }
272       else
273         {
274           role = ATK_ROLE_UNKNOWN;
275         }
276       atk_object_set_role (ATK_OBJECT (accessible), role);
277       g_object_freeze_notify (G_OBJECT (accessible));
278       g_signal_emit_by_name (accessible, "model-changed");
279       g_signal_emit_by_name (accessible, "visible-data-changed");
280       g_object_thaw_notify (G_OBJECT (accessible));
281     }
282   else
283     GTK_WIDGET_ACCESSIBLE_CLASS (_gtk_tree_view_accessible_parent_class)->notify_gtk (obj, pspec);
284 }
285
286 static void
287 gtk_tree_view_accessible_destroyed (GtkWidget     *widget,
288                                     GtkAccessible *gtk_accessible)
289 {
290   GtkTreeViewAccessible *accessible;
291
292   if (!GTK_IS_TREE_VIEW (widget))
293     return;
294
295   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_accessible);
296
297   if (accessible->tree_model)
298     {
299       disconnect_model_signals (accessible);
300       accessible->tree_model = NULL;
301     }
302   if (accessible->focus_cell)
303     {
304       g_object_unref (accessible->focus_cell);
305       accessible->focus_cell = NULL;
306     }
307 }
308
309 static void
310 gtk_tree_view_accessible_connect_widget_destroyed (GtkAccessible *accessible)
311 {
312   GtkWidget *widget;
313
314   widget = gtk_accessible_get_widget (accessible);
315   if (widget)
316     g_signal_connect_after (widget, "destroy",
317                             G_CALLBACK (gtk_tree_view_accessible_destroyed), accessible);
318
319   GTK_ACCESSIBLE_CLASS (_gtk_tree_view_accessible_parent_class)->connect_widget_destroyed (accessible);
320 }
321
322 static gint
323 get_n_rows (GtkTreeView *tree_view)
324 {
325   GtkRBTree *tree;
326
327   tree = _gtk_tree_view_get_rbtree (tree_view);
328
329   if (tree == NULL)
330     return 0;
331
332   return tree->root->total_count;
333 }
334
335 static gint
336 get_n_columns (GtkTreeView *tree_view)
337 {
338   guint i, visible_columns;
339
340   visible_columns = 0;
341
342   for (i = 0; i < gtk_tree_view_get_n_columns (tree_view); i++)
343     {
344       GtkTreeViewColumn *column = gtk_tree_view_get_column (tree_view, i);
345
346       if (gtk_tree_view_column_get_visible (column))
347         visible_columns++;
348     }
349
350   return visible_columns;
351 }
352 static gint
353 gtk_tree_view_accessible_get_n_children (AtkObject *obj)
354 {
355   GtkWidget *widget;
356   GtkTreeView *tree_view;
357
358   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
359   if (widget == NULL)
360     return 0;
361
362   tree_view = GTK_TREE_VIEW (widget);
363   return (get_n_rows (tree_view) + 1) * get_n_columns (tree_view);
364 }
365
366 static GtkTreeViewColumn *
367 get_visible_column (GtkTreeView *tree_view,
368                     guint        id)
369 {
370   guint i;
371
372   for (i = 0; i < gtk_tree_view_get_n_columns (tree_view); i++)
373     {
374       GtkTreeViewColumn *column = gtk_tree_view_get_column (tree_view, i);
375
376       if (!gtk_tree_view_column_get_visible (column))
377         continue;
378
379       if (id == 0)
380         return column;
381
382       id--;
383     }
384
385   g_return_val_if_reached (NULL);
386 }
387
388 static AtkObject *
389 gtk_tree_view_accessible_ref_child (AtkObject *obj,
390                                     gint       i)
391 {
392   GtkWidget *widget;
393   GtkTreeViewAccessible *accessible;
394   GtkCellAccessible *cell;
395   GtkTreeView *tree_view;
396   GtkTreeModel *tree_model;
397   GtkCellRenderer *renderer;
398   GtkTreeIter iter;
399   GtkTreeViewColumn *tv_col;
400   GtkTreeSelection *selection;
401   GtkTreePath *path;
402   GtkRBTree *tree;
403   GtkRBNode *node;
404   AtkObject *child;
405   AtkObject *parent;
406   GtkTreeViewColumn *expander_tv;
407   GList *renderer_list;
408   GList *l;
409   GtkContainerCellAccessible *container = NULL;
410   GtkRendererCellAccessible *renderer_cell;
411   gboolean is_expander, is_expanded, retval;
412   gboolean editable = FALSE;
413   gint focus_index;
414
415   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
416   if (widget == NULL)
417     return NULL;
418
419   if (i >= gtk_tree_view_accessible_get_n_children (obj))
420     return NULL;
421
422   accessible = GTK_TREE_VIEW_ACCESSIBLE (obj);
423   tree_view = GTK_TREE_VIEW (widget);
424   if (i < get_n_columns (tree_view))
425     {
426       tv_col = get_visible_column (tree_view, i);
427       child = get_header_from_column (tv_col);
428       if (child)
429         g_object_ref (child);
430       return child;
431     }
432
433   /* Check whether the child is cached */
434   cell = find_cell (accessible, i);
435   if (cell)
436     {
437       g_object_ref (cell);
438       return ATK_OBJECT (cell);
439     }
440
441   if (accessible->focus_cell == NULL)
442       focus_index = get_focus_index (tree_view);
443   else
444       focus_index = -1;
445
446   /* Find the RBTree and GtkTreeViewColumn for the index */
447   if (!get_rbtree_column_from_index (tree_view, i, &tree, &node, &tv_col))
448     return NULL;
449
450   path = _gtk_tree_path_new_from_rbtree (tree, node);
451   tree_model = gtk_tree_view_get_model (tree_view);
452   retval = gtk_tree_model_get_iter (tree_model, &iter, path);
453   if (!retval)
454     return NULL;
455
456   expander_tv = gtk_tree_view_get_expander_column (tree_view);
457   is_expander = FALSE;
458   is_expanded = FALSE;
459   if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_PARENT))
460     {
461       if (expander_tv == tv_col)
462         {
463           is_expander = TRUE;
464           is_expanded = node->children != NULL;
465         }
466     }
467   gtk_tree_view_column_cell_set_cell_data (tv_col, tree_model, &iter,
468                                            is_expander, is_expanded);
469
470   renderer_list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
471
472   /* If there is not exactly one renderer in the list,
473    * make a container
474    */
475   if (renderer_list == NULL || renderer_list->next)
476     {
477       GtkCellAccessible *container_cell;
478
479       container = _gtk_container_cell_accessible_new ();
480
481       container_cell = GTK_CELL_ACCESSIBLE (container);
482       _gtk_cell_accessible_initialise (container_cell, widget, ATK_OBJECT (accessible));
483
484       /* The GtkTreeViewAccessibleCellInfo structure for the container will
485        * be before the ones for the cells so that the first one we find for
486        * a position will be for the container
487        */
488       cell_info_new (accessible, tree_model, tree, node, tv_col, container_cell);
489       parent = ATK_OBJECT (container);
490     }
491   else
492     parent = ATK_OBJECT (accessible);
493
494   child = NULL;
495
496   for (l = renderer_list; l; l = l->next)
497     {
498       renderer = GTK_CELL_RENDERER (l->data);
499
500       if (GTK_IS_CELL_RENDERER_TEXT (renderer))
501         {
502           g_object_get (G_OBJECT (renderer), "editable", &editable, NULL);
503           child = _gtk_text_cell_accessible_new ();
504         }
505       else if (GTK_IS_CELL_RENDERER_TOGGLE (renderer))
506         child = _gtk_boolean_cell_accessible_new ();
507       else if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
508         child = _gtk_image_cell_accessible_new ();
509       else
510         child = _gtk_renderer_cell_accessible_new ();
511
512       cell = GTK_CELL_ACCESSIBLE (child);
513       renderer_cell = GTK_RENDERER_CELL_ACCESSIBLE (child);
514
515       /* Create the GtkTreeViewAccessibleCellInfo for this cell */
516       if (parent == ATK_OBJECT (accessible))
517         cell_info_new (accessible, tree_model, tree, node, tv_col, cell);
518
519       _gtk_cell_accessible_initialise (cell, widget, parent);
520
521       if (container)
522         _gtk_container_cell_accessible_add_child (container, cell);
523
524       update_cell_value (renderer_cell, accessible, FALSE);
525
526       /* If the row is selected, all cells on the row are selected */
527       selection = gtk_tree_view_get_selection (tree_view);
528
529       if (gtk_tree_selection_path_is_selected (selection, path))
530         _gtk_cell_accessible_add_state (cell, ATK_STATE_SELECTED, FALSE);
531
532       _gtk_cell_accessible_add_state (cell, ATK_STATE_FOCUSABLE, FALSE);
533       if (focus_index == i)
534         {
535           accessible->focus_cell = g_object_ref (cell);
536           _gtk_cell_accessible_add_state (cell, ATK_STATE_FOCUSED, FALSE);
537           g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
538         }
539     }
540   g_list_free (renderer_list);
541   if (container)
542     child = ATK_OBJECT (container);
543
544   if (expander_tv == tv_col)
545     {
546       AtkRelationSet *relation_set;
547       AtkObject *accessible_array[1];
548       AtkRelation* relation;
549       AtkObject *parent_node;
550
551       relation_set = atk_object_ref_relation_set (ATK_OBJECT (child));
552
553       gtk_tree_path_up (path);
554       if (gtk_tree_path_get_depth (path) == 0)
555         parent_node = obj;
556       else
557         {
558           gint parent_index;
559
560           parent_index = get_index (tree_view, path, i % get_n_columns (tree_view));
561           parent_node = atk_object_ref_accessible_child (obj, parent_index);
562         }
563       accessible_array[0] = parent_node;
564       relation = atk_relation_new (accessible_array, 1,
565                                    ATK_RELATION_NODE_CHILD_OF);
566       atk_relation_set_add (relation_set, relation);
567       atk_object_add_relationship (parent_node, ATK_RELATION_NODE_PARENT_OF, child);
568       g_object_unref (relation);
569       g_object_unref (relation_set);
570     }
571   gtk_tree_path_free (path);
572
573   /* We do not increase the reference count here; when g_object_unref()
574    * is called for the cell then cell_destroyed() is called and this
575    * removes the cell from the cache.
576    */
577   return child;
578 }
579
580 static AtkStateSet*
581 gtk_tree_view_accessible_ref_state_set (AtkObject *obj)
582 {
583   AtkStateSet *state_set;
584   GtkWidget *widget;
585
586   state_set = ATK_OBJECT_CLASS (_gtk_tree_view_accessible_parent_class)->ref_state_set (obj);
587   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
588
589   if (widget != NULL)
590     atk_state_set_add_state (state_set, ATK_STATE_MANAGES_DESCENDANTS);
591
592   return state_set;
593 }
594
595 static void
596 _gtk_tree_view_accessible_class_init (GtkTreeViewAccessibleClass *klass)
597 {
598   AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
599   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
600   GtkAccessibleClass *accessible_class = (GtkAccessibleClass*)klass;
601   GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
602   GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
603
604   class->get_n_children = gtk_tree_view_accessible_get_n_children;
605   class->ref_child = gtk_tree_view_accessible_ref_child;
606   class->ref_state_set = gtk_tree_view_accessible_ref_state_set;
607   class->initialize = gtk_tree_view_accessible_initialize;
608
609   widget_class->notify_gtk = gtk_tree_view_accessible_notify_gtk;
610
611   accessible_class->connect_widget_destroyed = gtk_tree_view_accessible_connect_widget_destroyed;
612
613   /* The children of a GtkTreeView are the buttons at the top of the columns
614    * we do not represent these as children so we do not want to report
615    * children added or deleted when these changed.
616    */
617   container_class->add_gtk = NULL;
618   container_class->remove_gtk = NULL;
619
620   gobject_class->finalize = gtk_tree_view_accessible_finalize;
621 }
622
623 static void
624 _gtk_tree_view_accessible_init (GtkTreeViewAccessible *view)
625 {
626 }
627
628 gint
629 get_focus_index (GtkTreeView *tree_view)
630 {
631   GtkTreePath *focus_path;
632   GtkTreeViewColumn *focus_column;
633   gint index;
634
635   gtk_tree_view_get_cursor (tree_view, &focus_path, &focus_column);
636   if (focus_path && focus_column)
637     index = get_index (tree_view, focus_path,
638                        get_column_number (tree_view, focus_column));
639   else
640     index = -1;
641
642   if (focus_path)
643     gtk_tree_path_free (focus_path);
644
645   return index;
646 }
647
648 /* This function returns a reference to the accessible object
649  * for the cell in the treeview which has focus, if any
650  */
651 static AtkObject *
652 gtk_tree_view_accessible_ref_focus_cell (GtkTreeView *tree_view)
653 {
654   AtkObject *focus_cell = NULL;
655   AtkObject *atk_obj;
656   gint focus_index;
657
658   focus_index = get_focus_index (tree_view);
659   if (focus_index >= 0)
660     {
661       atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
662       focus_cell = atk_object_ref_accessible_child (atk_obj, focus_index);
663     }
664
665   return focus_cell;
666 }
667
668 /* atkcomponent.h */
669
670 static AtkObject *
671 gtk_tree_view_accessible_ref_accessible_at_point (AtkComponent *component,
672                                                   gint          x,
673                                                   gint          y,
674                                                   AtkCoordType  coord_type)
675 {
676   GtkWidget *widget;
677   GtkTreeView *tree_view;
678   GtkTreePath *path;
679   GtkTreeViewColumn *tv_column;
680   gint x_pos, y_pos;
681   gint bx, by;
682   gboolean ret_val;
683
684   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (component));
685   if (widget == NULL)
686     return NULL;
687
688   tree_view = GTK_TREE_VIEW (widget);
689
690   atk_component_get_extents (component, &x_pos, &y_pos, NULL, NULL, coord_type);
691   gtk_tree_view_convert_widget_to_bin_window_coords (tree_view, x, y, &bx, &by);
692   ret_val = gtk_tree_view_get_path_at_pos (tree_view,
693                                            bx - x_pos, by - y_pos,
694                                            &path, &tv_column, NULL, NULL);
695   if (ret_val)
696     {
697       gint index, column;
698
699       column = get_column_number (tree_view, tv_column);
700       index = get_index (tree_view, path, column);
701       gtk_tree_path_free (path);
702
703       return gtk_tree_view_accessible_ref_child (ATK_OBJECT (component), index);
704     }
705
706   return NULL;
707 }
708
709 static void
710 atk_component_interface_init (AtkComponentIface *iface)
711 {
712   iface->ref_accessible_at_point = gtk_tree_view_accessible_ref_accessible_at_point;
713 }
714
715 /* atktable.h */
716
717 static gint
718 gtk_tree_view_accessible_get_index_at (AtkTable *table,
719                                        gint      row,
720                                        gint      column)
721 {
722   GtkWidget *widget;
723   gint n_cols, n_rows;
724
725   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
726   if (widget == NULL)
727     return -1;
728
729   n_cols = atk_table_get_n_columns (table);
730   n_rows = atk_table_get_n_rows (table);
731
732   if (row >= n_rows || column >= n_cols)
733     return -1;
734
735   return (row + 1) * n_cols + column;
736 }
737
738 static gint
739 gtk_tree_view_accessible_get_column_at_index (AtkTable *table,
740                                               gint      index)
741 {
742   GtkWidget *widget;
743   gint n_columns;
744
745   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
746   if (widget == NULL)
747     return -1;
748
749   if (index >= gtk_tree_view_accessible_get_n_children (ATK_OBJECT (table)))
750     return -1;
751
752   n_columns = get_n_columns (GTK_TREE_VIEW (widget));
753
754   /* checked by the n_children() check above */
755   g_assert (n_columns > 0);
756
757   return index % n_columns;
758 }
759
760 static gint
761 gtk_tree_view_accessible_get_row_at_index (AtkTable *table,
762                                            gint      index)
763 {
764   GtkWidget *widget;
765   GtkTreeView *tree_view;
766
767   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
768   if (widget == NULL)
769     return -1;
770
771   tree_view = GTK_TREE_VIEW (widget);
772
773   index /= get_n_columns (tree_view);
774   index--;
775   if (index >= get_n_rows (tree_view))
776     return -1;
777
778   return index;
779 }
780
781 static AtkObject *
782 gtk_tree_view_accessible_table_ref_at (AtkTable *table,
783                                        gint      row,
784                                        gint      column)
785 {
786   gint index;
787
788   index = gtk_tree_view_accessible_get_index_at (table, row, column);
789   if (index == -1)
790     return NULL;
791
792   return gtk_tree_view_accessible_ref_child (ATK_OBJECT (table), index);
793 }
794
795 static gint
796 gtk_tree_view_accessible_get_n_rows (AtkTable *table)
797 {
798   GtkWidget *widget;
799
800   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
801   if (widget == NULL)
802     return 0;
803
804   return get_n_rows (GTK_TREE_VIEW (widget));
805 }
806
807 static gint
808 gtk_tree_view_accessible_get_n_columns (AtkTable *table)
809 {
810   GtkWidget *widget;
811
812   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
813   if (widget == NULL)
814     return 0;
815
816   return get_n_columns (GTK_TREE_VIEW (widget));
817 }
818
819 static gboolean
820 gtk_tree_view_accessible_is_row_selected (AtkTable *table,
821                                           gint      row)
822 {
823   GtkWidget *widget;
824   GtkTreeView *tree_view;
825   GtkTreeSelection *selection;
826   GtkTreeIter iter;
827
828   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
829   if (widget == NULL)
830     return FALSE;
831
832   if (row < 0)
833     return FALSE;
834
835   tree_view = GTK_TREE_VIEW (widget);
836   selection = gtk_tree_view_get_selection (tree_view);
837
838   set_iter_nth_row (tree_view, &iter, row);
839   return gtk_tree_selection_iter_is_selected (selection, &iter);
840 }
841
842 static gboolean
843 gtk_tree_view_accessible_is_selected (AtkTable *table,
844                                       gint      row,
845                                       gint      column)
846 {
847   return gtk_tree_view_accessible_is_row_selected (table, row);
848 }
849
850 static void
851 get_selected_rows (GtkTreeModel *model,
852                    GtkTreePath  *path,
853                    GtkTreeIter  *iter,
854                    gpointer      data)
855 {
856   GPtrArray *array = (GPtrArray *)data;
857
858   g_ptr_array_add (array, gtk_tree_path_copy (path));
859 }
860
861 static gint
862 gtk_tree_view_accessible_get_selected_rows (AtkTable  *table,
863                                             gint     **rows_selected)
864 {
865   GtkWidget *widget;
866   GtkTreeView *tree_view;
867   GtkTreeModel *tree_model;
868   GtkTreeIter iter;
869   GtkTreeSelection *selection;
870   GtkTreePath *tree_path;
871   gint ret_val = 0;
872
873   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
874   if (widget == NULL)
875     return 0;
876
877   tree_view = GTK_TREE_VIEW (widget);
878   selection = gtk_tree_view_get_selection (tree_view);
879
880   switch (gtk_tree_selection_get_mode (selection))
881     {
882     case GTK_SELECTION_SINGLE:
883     case GTK_SELECTION_BROWSE:
884       if (gtk_tree_selection_get_selected (selection, &tree_model, &iter))
885         {
886           gint row;
887
888           if (rows_selected)
889             {
890               *rows_selected = g_new (gint, 1);
891               tree_path = gtk_tree_model_get_path (tree_model, &iter);
892               row = get_row_from_tree_path (tree_view, tree_path);
893               gtk_tree_path_free (tree_path);
894
895               /* shouldn't ever happen */
896               g_return_val_if_fail (row != -1, 0);
897
898               *rows_selected[0] = row;
899             }
900           ret_val = 1;
901         }
902       break;
903     case GTK_SELECTION_MULTIPLE:
904       {
905         GPtrArray *array = g_ptr_array_new();
906
907         gtk_tree_selection_selected_foreach (selection, get_selected_rows, array);
908         ret_val = array->len;
909
910         if (rows_selected && ret_val)
911           {
912             gint i;
913
914             *rows_selected = g_new (gint, ret_val);
915             for (i = 0; i < ret_val; i++)
916               {
917                 gint row;
918
919                 tree_path = (GtkTreePath *) g_ptr_array_index (array, i);
920                 row = get_row_from_tree_path (tree_view, tree_path);
921                 gtk_tree_path_free (tree_path);
922                 (*rows_selected)[i] = row;
923               }
924           }
925         g_ptr_array_free (array, FALSE);
926       }
927       break;
928     case GTK_SELECTION_NONE:
929       break;
930     }
931   return ret_val;
932 }
933
934 static gboolean
935 gtk_tree_view_accessible_add_row_selection (AtkTable *table,
936                                             gint      row)
937 {
938   GtkWidget *widget;
939   GtkTreeView *tree_view;
940   GtkTreeModel *tree_model;
941   GtkTreeSelection *selection;
942   GtkTreePath *tree_path;
943   GtkTreeIter iter_to_row;
944
945   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
946   if (widget == NULL)
947     return FALSE;
948
949   if (!gtk_tree_view_accessible_is_row_selected (table, row))
950     {
951       tree_view = GTK_TREE_VIEW (widget);
952       tree_model = gtk_tree_view_get_model (tree_view);
953       selection = gtk_tree_view_get_selection (tree_view);
954
955       if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
956         {
957           tree_path = gtk_tree_path_new ();
958           gtk_tree_path_append_index (tree_path, row);
959           gtk_tree_selection_select_path (selection,tree_path);
960           gtk_tree_path_free (tree_path);
961         }
962       else
963         {
964           set_iter_nth_row (tree_view, &iter_to_row, row);
965           gtk_tree_selection_select_iter (selection, &iter_to_row);
966         }
967     }
968
969   return gtk_tree_view_accessible_is_row_selected (table, row);
970 }
971
972 static gboolean
973 gtk_tree_view_accessible_remove_row_selection (AtkTable *table,
974                                                gint      row)
975 {
976   GtkWidget *widget;
977   GtkTreeView *tree_view;
978   GtkTreeSelection *selection;
979
980   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
981   if (widget == NULL)
982     return FALSE;
983
984   tree_view = GTK_TREE_VIEW (widget);
985   selection = gtk_tree_view_get_selection (tree_view);
986
987   if (gtk_tree_view_accessible_is_row_selected (table, row))
988     {
989       gtk_tree_selection_unselect_all (selection);
990       return TRUE;
991     }
992
993   return FALSE;
994 }
995
996 static AtkObject *
997 gtk_tree_view_accessible_get_column_header (AtkTable *table,
998                                             gint      in_col)
999 {
1000   GtkWidget *widget;
1001   GtkTreeView *tree_view;
1002   GtkTreeViewColumn *tv_col;
1003
1004   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1005   if (widget == NULL)
1006     return NULL;
1007
1008   tree_view = GTK_TREE_VIEW (widget);
1009   if (in_col < 0 || in_col >= get_n_columns (tree_view))
1010     return NULL;
1011
1012   tv_col = get_visible_column (tree_view, in_col);
1013   return get_header_from_column (tv_col);
1014 }
1015
1016 static const gchar *
1017 gtk_tree_view_accessible_get_column_description (AtkTable *table,
1018                                                  gint      in_col)
1019 {
1020   GtkWidget *widget;
1021   GtkTreeView *tree_view;
1022   GtkTreeViewColumn *tv_col;
1023
1024   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1025   if (widget == NULL)
1026     return NULL;
1027
1028   tree_view = GTK_TREE_VIEW (widget);
1029   if (in_col < 0 || in_col >= get_n_columns (tree_view))
1030     return NULL;
1031
1032   tv_col = get_visible_column (tree_view, in_col);
1033   return gtk_tree_view_column_get_title (tv_col);
1034 }
1035
1036 static void
1037 atk_table_interface_init (AtkTableIface *iface)
1038 {
1039   iface->ref_at = gtk_tree_view_accessible_table_ref_at;
1040   iface->get_n_rows = gtk_tree_view_accessible_get_n_rows;
1041   iface->get_n_columns = gtk_tree_view_accessible_get_n_columns;
1042   iface->get_index_at = gtk_tree_view_accessible_get_index_at;
1043   iface->get_column_at_index = gtk_tree_view_accessible_get_column_at_index;
1044   iface->get_row_at_index = gtk_tree_view_accessible_get_row_at_index;
1045   iface->is_row_selected = gtk_tree_view_accessible_is_row_selected;
1046   iface->is_selected = gtk_tree_view_accessible_is_selected;
1047   iface->get_selected_rows = gtk_tree_view_accessible_get_selected_rows;
1048   iface->add_row_selection = gtk_tree_view_accessible_add_row_selection;
1049   iface->remove_row_selection = gtk_tree_view_accessible_remove_row_selection;
1050   iface->get_column_extent_at = NULL;
1051   iface->get_row_extent_at = NULL;
1052   iface->get_column_header = gtk_tree_view_accessible_get_column_header;
1053   iface->get_column_description = gtk_tree_view_accessible_get_column_description;
1054 }
1055
1056 /* atkselection.h */
1057
1058 static gboolean
1059 gtk_tree_view_accessible_add_selection (AtkSelection *selection,
1060                                         gint          i)
1061 {
1062   AtkTable *table;
1063   gint n_columns;
1064   gint row;
1065
1066   table = ATK_TABLE (selection);
1067   n_columns = gtk_tree_view_accessible_get_n_columns (table);
1068   if (n_columns != 1)
1069     return FALSE;
1070
1071   row = gtk_tree_view_accessible_get_row_at_index (table, i);
1072   return gtk_tree_view_accessible_add_row_selection (table, row);
1073 }
1074
1075 static gboolean
1076 gtk_tree_view_accessible_clear_selection (AtkSelection *selection)
1077 {
1078   GtkWidget *widget;
1079   GtkTreeView *tree_view;
1080   GtkTreeSelection *tree_selection;
1081
1082   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
1083   if (widget == NULL)
1084     return FALSE;
1085
1086   tree_view = GTK_TREE_VIEW (widget);
1087   tree_selection = gtk_tree_view_get_selection (tree_view);
1088
1089   gtk_tree_selection_unselect_all (tree_selection);
1090   return TRUE;
1091 }
1092
1093 static AtkObject *
1094 gtk_tree_view_accessible_ref_selection (AtkSelection *selection,
1095                                         gint          i)
1096 {
1097   AtkTable *table;
1098   gint row;
1099   gint n_selected;
1100   gint n_columns;
1101   gint *selected;
1102
1103   table = ATK_TABLE (selection);
1104   n_columns = gtk_tree_view_accessible_get_n_columns (table);
1105   n_selected = gtk_tree_view_accessible_get_selected_rows (table, &selected);
1106   if (i >= n_columns * n_selected)
1107     return NULL;
1108
1109   row = selected[i / n_columns];
1110   g_free (selected);
1111
1112   return gtk_tree_view_accessible_table_ref_at (table, row, i % n_columns);
1113 }
1114
1115 static gint
1116 gtk_tree_view_accessible_get_selection_count (AtkSelection *selection)
1117 {
1118   AtkTable *table;
1119   gint n_selected;
1120
1121   table = ATK_TABLE (selection);
1122   n_selected = gtk_tree_view_accessible_get_selected_rows (table, NULL);
1123   if (n_selected > 0)
1124     n_selected *= gtk_tree_view_accessible_get_n_columns (table);
1125   return n_selected;
1126 }
1127
1128 static gboolean
1129 gtk_tree_view_accessible_is_child_selected (AtkSelection *selection,
1130                                             gint          i)
1131 {
1132   GtkWidget *widget;
1133   gint row;
1134
1135   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
1136   if (widget == NULL)
1137     return FALSE;
1138
1139   row = atk_table_get_row_at_index (ATK_TABLE (selection), i);
1140
1141   return gtk_tree_view_accessible_is_row_selected (ATK_TABLE (selection), row);
1142 }
1143
1144 static void atk_selection_interface_init (AtkSelectionIface *iface)
1145 {
1146   iface->add_selection = gtk_tree_view_accessible_add_selection;
1147   iface->clear_selection = gtk_tree_view_accessible_clear_selection;
1148   iface->ref_selection = gtk_tree_view_accessible_ref_selection;
1149   iface->get_selection_count = gtk_tree_view_accessible_get_selection_count;
1150   iface->is_child_selected = gtk_tree_view_accessible_is_child_selected;
1151 }
1152
1153 #define EXTRA_EXPANDER_PADDING 4
1154
1155 static void
1156 gtk_tree_view_accessible_get_cell_area (GtkCellAccessibleParent *parent,
1157                                         GtkCellAccessible       *cell,
1158                                         GdkRectangle            *cell_rect)
1159 {
1160   GtkWidget *widget;
1161   GtkTreeView *tree_view;
1162   GtkTreeViewColumn *tv_col;
1163   GtkTreePath *path;
1164   AtkObject *parent_cell;
1165   GtkTreeViewAccessibleCellInfo *cell_info;
1166   GtkCellAccessible *top_cell;
1167
1168   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1169   if (widget == NULL)
1170     return;
1171
1172   tree_view = GTK_TREE_VIEW (widget);
1173   parent_cell = atk_object_get_parent (ATK_OBJECT (cell));
1174   if (parent_cell != ATK_OBJECT (parent))
1175     top_cell = GTK_CELL_ACCESSIBLE (parent_cell);
1176   else
1177     top_cell = cell;
1178   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), top_cell);
1179   if (!cell_info)
1180     return;
1181   path = cell_info_get_path (cell_info);
1182   tv_col = cell_info->cell_col_ref;
1183   if (path)
1184     {
1185       GtkTreeViewColumn *expander_column;
1186       gint focus_line_width;
1187
1188       gtk_tree_view_get_cell_area (tree_view, path, tv_col, cell_rect);
1189       expander_column = gtk_tree_view_get_expander_column (tree_view);
1190       if (expander_column == tv_col)
1191         {
1192           gint expander_size;
1193           gtk_widget_style_get (widget,
1194                                 "expander-size", &expander_size,
1195                                 NULL);
1196           cell_rect->x += expander_size + EXTRA_EXPANDER_PADDING;
1197           cell_rect->width -= expander_size + EXTRA_EXPANDER_PADDING;
1198         }
1199       gtk_widget_style_get (widget,
1200                             "focus-line-width", &focus_line_width,
1201                             NULL);
1202
1203       cell_rect->x += focus_line_width;
1204       cell_rect->width -= 2 * focus_line_width;
1205
1206       gtk_tree_path_free (path);
1207
1208       /* A column has more than one renderer so we find the position
1209        * and width of each
1210        */
1211       if (top_cell != cell)
1212         {
1213           gint cell_index;
1214           gboolean found;
1215           gint cell_start;
1216           gint cell_width;
1217           GList *renderers;
1218           GtkCellRenderer *renderer;
1219
1220           cell_index = atk_object_get_index_in_parent (ATK_OBJECT (cell));
1221           renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
1222           renderer = g_list_nth_data (renderers, cell_index);
1223
1224           found = gtk_tree_view_column_cell_get_position (tv_col, renderer, &cell_start, &cell_width);
1225           if (found)
1226             {
1227               cell_rect->x += cell_start;
1228               cell_rect->width = cell_width;
1229             }
1230           g_list_free (renderers);
1231         }
1232
1233     }
1234 }
1235
1236 static void
1237 gtk_tree_view_accessible_get_cell_extents (GtkCellAccessibleParent *parent,
1238                                            GtkCellAccessible       *cell,
1239                                            gint                    *x,
1240                                            gint                    *y,
1241                                            gint                    *width,
1242                                            gint                    *height,
1243                                            AtkCoordType             coord_type)
1244 {
1245   GtkWidget *widget;
1246   GtkTreeView *tree_view;
1247   GdkWindow *bin_window;
1248   GdkRectangle cell_rect;
1249   gint w_x, w_y;
1250
1251   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1252   if (widget == NULL)
1253     return;
1254
1255   tree_view = GTK_TREE_VIEW (widget);
1256   gtk_tree_view_accessible_get_cell_area (parent, cell, &cell_rect);
1257   bin_window = gtk_tree_view_get_bin_window (tree_view);
1258   gdk_window_get_origin (bin_window, &w_x, &w_y);
1259
1260   if (coord_type == ATK_XY_WINDOW)
1261     {
1262       GdkWindow *window;
1263       gint x_toplevel, y_toplevel;
1264
1265       window = gdk_window_get_toplevel (bin_window);
1266       gdk_window_get_origin (window, &x_toplevel, &y_toplevel);
1267
1268       w_x -= x_toplevel;
1269       w_y -= y_toplevel;
1270     }
1271
1272   *width = cell_rect.width;
1273   *height = cell_rect.height;
1274   if (is_cell_showing (tree_view, &cell_rect))
1275     {
1276       *x = cell_rect.x + w_x;
1277       *y = cell_rect.y + w_y;
1278     }
1279   else
1280     {
1281       *x = G_MININT;
1282       *y = G_MININT;
1283     }
1284 }
1285
1286 static gboolean
1287 gtk_tree_view_accessible_grab_cell_focus (GtkCellAccessibleParent *parent,
1288                                           GtkCellAccessible       *cell)
1289 {
1290   GtkWidget *widget;
1291   GtkTreeView *tree_view;
1292   GtkTreeViewColumn *tv_col;
1293   GtkTreePath *path;
1294   AtkObject *parent_cell;
1295   AtkObject *cell_object;
1296   GtkTreeViewAccessibleCellInfo *cell_info;
1297   GtkCellRenderer *renderer = NULL;
1298   GtkWidget *toplevel;
1299   gint index;
1300
1301   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1302   if (widget == NULL)
1303     return FALSE;
1304
1305   tree_view = GTK_TREE_VIEW (widget);
1306
1307   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1308   if (!cell_info)
1309     return FALSE;
1310   cell_object = ATK_OBJECT (cell);
1311   parent_cell = atk_object_get_parent (cell_object);
1312   tv_col = cell_info->cell_col_ref;
1313   if (parent_cell != ATK_OBJECT (parent))
1314     {
1315       /* GtkCellAccessible is in a GtkContainerCellAccessible.
1316        * The GtkTreeViewColumn has multiple renderers;
1317        * find the corresponding one.
1318        */
1319       GList *renderers;
1320
1321       renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
1322       index = atk_object_get_index_in_parent (cell_object);
1323       renderer = g_list_nth_data (renderers, index);
1324       g_list_free (renderers);
1325     }
1326   path = cell_info_get_path (cell_info);
1327   if (path)
1328     {
1329       if (renderer)
1330         gtk_tree_view_set_cursor_on_cell (tree_view, path, tv_col, renderer, FALSE);
1331       else
1332         gtk_tree_view_set_cursor (tree_view, path, tv_col, FALSE);
1333
1334       gtk_tree_path_free (path);
1335       gtk_widget_grab_focus (widget);
1336       toplevel = gtk_widget_get_toplevel (widget);
1337       if (gtk_widget_is_toplevel (toplevel))
1338         {
1339 #ifdef GDK_WINDOWING_X11
1340           gtk_window_present_with_time (GTK_WINDOW (toplevel),
1341                                         gdk_x11_get_server_time (gtk_widget_get_window (widget)));
1342 #else
1343           gtk_window_present (GTK_WINDOW (toplevel));
1344 #endif
1345         }
1346
1347       return TRUE;
1348     }
1349   else
1350       return FALSE;
1351 }
1352
1353 static int
1354 gtk_tree_view_accessible_get_child_index (GtkCellAccessibleParent *parent,
1355                                           GtkCellAccessible       *cell)
1356 {
1357   GtkTreeViewAccessibleCellInfo *cell_info;
1358   GtkTreeView *tree_view;
1359
1360   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1361   if (!cell_info)
1362     return -1;
1363
1364   tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
1365
1366   return cell_info_get_index (tree_view, cell_info);
1367 }
1368
1369 static GtkCellRendererState
1370 gtk_tree_view_accessible_get_renderer_state (GtkCellAccessibleParent *parent,
1371                                              GtkCellAccessible       *cell)
1372 {
1373   GtkTreeViewAccessibleCellInfo *cell_info;
1374   GtkTreeView *treeview;
1375   GtkCellRendererState flags;
1376
1377   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1378   if (!cell_info)
1379     return 0;
1380
1381   flags = 0;
1382
1383   if (GTK_RBNODE_FLAG_SET (cell_info->node, GTK_RBNODE_IS_SELECTED))
1384     flags |= GTK_CELL_RENDERER_SELECTED;
1385
1386   if (GTK_RBNODE_FLAG_SET (cell_info->node, GTK_RBNODE_IS_PRELIT))
1387     flags |= GTK_CELL_RENDERER_PRELIT;
1388
1389   if (gtk_tree_view_column_get_sort_indicator (cell_info->cell_col_ref))
1390     flags |= GTK_CELL_RENDERER_SORTED;
1391
1392   treeview = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
1393
1394   if (cell_info->cell_col_ref == gtk_tree_view_get_expander_column (treeview))
1395     {
1396       if (GTK_RBNODE_FLAG_SET (cell_info->node, GTK_RBNODE_IS_PARENT))
1397         flags |= GTK_CELL_RENDERER_EXPANDABLE;
1398
1399       if (cell_info->node->children)
1400         flags |= GTK_CELL_RENDERER_EXPANDED;
1401     }
1402
1403   if (gtk_widget_has_focus (GTK_WIDGET (treeview)))
1404     {
1405       GtkTreeViewColumn *column;
1406       GtkTreePath *path;
1407       GtkRBTree *tree;
1408       GtkRBNode *node;
1409       
1410       gtk_tree_view_get_cursor (treeview, &path, &column);
1411       if (path)
1412         {
1413           _gtk_tree_view_find_node (treeview, path, &tree, &node);
1414           gtk_tree_path_free (path);
1415         }
1416       else
1417         tree = NULL;
1418
1419       if (cell_info->cell_col_ref == column
1420           && cell_info->tree == tree
1421           && cell_info->node == node)
1422         flags |= GTK_CELL_RENDERER_FOCUSED;
1423     }
1424
1425   return flags;
1426 }
1427
1428 static void
1429 gtk_tree_view_accessible_set_cell_data (GtkCellAccessibleParent *parent,
1430                                         GtkCellAccessible       *cell)
1431 {
1432   GtkTreeViewAccessibleCellInfo *cell_info;
1433   GtkTreeView *treeview;
1434   gboolean is_expander, is_expanded;
1435   GtkTreeModel *model;
1436   GtkTreeIter iter;
1437   GtkTreePath *path;
1438
1439   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1440   if (!cell_info)
1441     return;
1442
1443   treeview = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
1444   model = gtk_tree_view_get_model (treeview);
1445
1446   if (GTK_RBNODE_FLAG_SET (cell_info->node, GTK_RBNODE_IS_PARENT) &&
1447       cell_info->cell_col_ref == gtk_tree_view_get_expander_column (treeview))
1448     {
1449       is_expander = TRUE;
1450       is_expanded = cell_info->node->children != NULL;
1451     }
1452   else
1453     {
1454       is_expander = FALSE;
1455       is_expanded = FALSE;
1456     }
1457
1458   path = cell_info_get_path (cell_info);
1459   if (path == NULL ||
1460       !gtk_tree_model_get_iter (model, &iter, path))
1461     {
1462       /* We only track valid cells, this should never happen */
1463       g_return_if_reached ();
1464     }
1465
1466   gtk_tree_view_column_cell_set_cell_data (cell_info->cell_col_ref,
1467                                            model,
1468                                            &iter,
1469                                            is_expander,
1470                                            is_expanded);
1471 }
1472
1473 static void
1474 gtk_tree_view_accessible_expand_collapse (GtkCellAccessibleParent *parent,
1475                                           GtkCellAccessible       *cell)
1476 {
1477   GtkTreeViewAccessibleCellInfo *cell_info;
1478   GtkTreeView *treeview;
1479   GtkTreePath *path;
1480
1481   treeview = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
1482
1483   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1484   if (!cell_info ||
1485       cell_info->cell_col_ref != gtk_tree_view_get_expander_column (treeview))
1486     return;
1487
1488   path = cell_info_get_path (cell_info);
1489
1490   if (cell_info->node->children)
1491     gtk_tree_view_collapse_row (treeview, path);
1492   else
1493     gtk_tree_view_expand_row (treeview, path, FALSE);
1494
1495   gtk_tree_path_free (path);
1496 }
1497
1498 static void
1499 gtk_tree_view_accessible_activate (GtkCellAccessibleParent *parent,
1500                                    GtkCellAccessible       *cell)
1501 {
1502   GtkTreeViewAccessibleCellInfo *cell_info;
1503   GtkTreeView *treeview;
1504   GtkTreePath *path;
1505
1506   treeview = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
1507
1508   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1509   if (!cell_info)
1510     return;
1511
1512   path = cell_info_get_path (cell_info);
1513
1514   gtk_tree_view_row_activated (treeview, path, cell_info->cell_col_ref);
1515
1516   gtk_tree_path_free (path);
1517 }
1518
1519 static void
1520 gtk_tree_view_accessible_edit (GtkCellAccessibleParent *parent,
1521                                GtkCellAccessible       *cell)
1522 {
1523   GtkTreeView *treeview;
1524
1525   if (!gtk_tree_view_accessible_grab_cell_focus (parent, cell))
1526     return;
1527
1528   treeview = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
1529
1530   g_signal_emit_by_name (treeview,
1531                          "real-select-cursor-row",
1532                          TRUE);
1533 }
1534
1535 static void
1536 gtk_cell_accessible_parent_interface_init (GtkCellAccessibleParentIface *iface)
1537 {
1538   iface->get_cell_extents = gtk_tree_view_accessible_get_cell_extents;
1539   iface->get_cell_area = gtk_tree_view_accessible_get_cell_area;
1540   iface->grab_focus = gtk_tree_view_accessible_grab_cell_focus;
1541   iface->get_child_index = gtk_tree_view_accessible_get_child_index;
1542   iface->get_renderer_state = gtk_tree_view_accessible_get_renderer_state;
1543   iface->set_cell_data = gtk_tree_view_accessible_set_cell_data;
1544   iface->expand_collapse = gtk_tree_view_accessible_expand_collapse;
1545   iface->activate = gtk_tree_view_accessible_activate;
1546   iface->edit = gtk_tree_view_accessible_edit;
1547 }
1548
1549 /* signal handling */
1550
1551 static void
1552 selection_changed_cb (GtkTreeSelection *selection,
1553                       gpointer          data)
1554 {
1555   GtkTreeViewAccessible *accessible;
1556   GtkTreeView *tree_view;
1557   GtkWidget *widget;
1558   GtkTreeViewAccessibleCellInfo *info;
1559   GtkTreeSelection *tree_selection;
1560   GtkTreePath *path;
1561   GHashTableIter iter;
1562
1563   accessible = GTK_TREE_VIEW_ACCESSIBLE (data);
1564   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
1565   if (widget == NULL)
1566     return;
1567
1568   tree_view = GTK_TREE_VIEW (widget);
1569   tree_selection = gtk_tree_view_get_selection (tree_view);
1570
1571   /* FIXME: clean rows iterates through all cells too */
1572   g_hash_table_iter_init (&iter, accessible->cell_infos);
1573   while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&info))
1574     {
1575       _gtk_cell_accessible_remove_state (info->cell, ATK_STATE_SELECTED, TRUE);
1576
1577       path = cell_info_get_path (info);
1578       if (path && gtk_tree_selection_path_is_selected (tree_selection, path))
1579         _gtk_cell_accessible_add_state (info->cell, ATK_STATE_SELECTED, TRUE);
1580       gtk_tree_path_free (path);
1581     }
1582   if (gtk_widget_get_realized (widget))
1583     g_signal_emit_by_name (accessible, "selection-changed");
1584 }
1585
1586 static void
1587 cursor_changed (GtkTreeView           *tree_view,
1588                 GtkTreeViewAccessible *accessible)
1589 {
1590   AtkObject *cell;
1591
1592   cell = gtk_tree_view_accessible_ref_focus_cell (tree_view);
1593   if (cell)
1594     {
1595       if (cell != accessible->focus_cell)
1596         {
1597           if (accessible->focus_cell)
1598             {
1599               _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_ACTIVE, FALSE);
1600               _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_FOCUSED, FALSE);
1601               g_object_unref (accessible->focus_cell);
1602               accessible->focus_cell = cell;
1603             }
1604
1605           if (gtk_widget_has_focus (GTK_WIDGET (tree_view)))
1606             {
1607               _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_ACTIVE, FALSE);
1608               _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_FOCUSED, FALSE);
1609             }
1610
1611           g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
1612         }
1613       else
1614         g_object_unref (cell);
1615     }
1616 }
1617
1618 static gboolean
1619 focus_in (GtkWidget *widget)
1620 {
1621   GtkTreeView *tree_view;
1622   GtkTreeViewAccessible *accessible;
1623   AtkStateSet *state_set;
1624   AtkObject *cell;
1625
1626   tree_view = GTK_TREE_VIEW (widget);
1627   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
1628
1629   if (accessible->focus_cell == NULL)
1630     {
1631       cell = gtk_tree_view_accessible_ref_focus_cell (tree_view);
1632       if (cell)
1633         {
1634           state_set = atk_object_ref_state_set (cell);
1635           if (state_set)
1636             {
1637               if (!atk_state_set_contains_state (state_set, ATK_STATE_FOCUSED))
1638                 {
1639                   _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_ACTIVE, FALSE);
1640                   accessible->focus_cell = cell;
1641                   _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_FOCUSED, FALSE);
1642                   g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
1643                 }
1644               g_object_unref (state_set);
1645             }
1646         }
1647     }
1648   return FALSE;
1649 }
1650
1651 static gboolean
1652 focus_out (GtkWidget *widget)
1653 {
1654   GtkTreeViewAccessible *accessible;
1655
1656   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
1657   if (accessible->focus_cell)
1658     {
1659       _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_ACTIVE, FALSE);
1660       _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_FOCUSED, FALSE);
1661       g_object_unref (accessible->focus_cell);
1662       accessible->focus_cell = NULL;
1663     }
1664   return FALSE;
1665 }
1666
1667 static void
1668 model_row_changed (GtkTreeModel *tree_model,
1669                    GtkTreePath  *path,
1670                    GtkTreeIter  *iter,
1671                    gpointer      user_data)
1672 {
1673   GtkTreeView *tree_view = GTK_TREE_VIEW (user_data);
1674   GtkTreeViewAccessible *accessible;
1675   GtkTreePath *cell_path;
1676   GtkTreeViewAccessibleCellInfo *cell_info;
1677   GHashTableIter hash_iter;
1678
1679   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (tree_view)));
1680
1681   /* Loop through our cached cells */
1682   /* Must loop through them all */
1683   g_hash_table_iter_init (&hash_iter, accessible->cell_infos);
1684   while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer *)&cell_info))
1685     {
1686       cell_path = cell_info_get_path (cell_info);
1687
1688       if (cell_path != NULL)
1689         {
1690           if (path && gtk_tree_path_compare (cell_path, path) == 0)
1691             {
1692               if (GTK_IS_RENDERER_CELL_ACCESSIBLE (cell_info->cell))
1693                 update_cell_value (GTK_RENDERER_CELL_ACCESSIBLE (cell_info->cell),
1694                                    accessible, TRUE);
1695             }
1696           gtk_tree_path_free (cell_path);
1697         }
1698     }
1699   g_signal_emit_by_name (accessible, "visible-data-changed");
1700 }
1701
1702 static void
1703 model_row_inserted (GtkTreeModel *tree_model,
1704                     GtkTreePath  *path,
1705                     GtkTreeIter  *iter,
1706                     gpointer      user_data)
1707 {
1708   GtkTreeView *tree_view = (GtkTreeView *)user_data;
1709   AtkObject *atk_obj;
1710   gint row, n_inserted, child_row;
1711
1712   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1713
1714   /* Check to see if row is visible */
1715   row = get_row_from_tree_path (tree_view, path);
1716
1717  /* A row insert is not necessarily visible.  For example,
1718   * a row can be draged & dropped into another row, which
1719   * causes an insert on the model that isn't visible in the
1720   * view.  Only generate a signal if the inserted row is
1721   * visible.
1722   */
1723   if (row != -1)
1724     {
1725       GtkTreeIter tmp_iter;
1726       gint n_cols, col;
1727
1728       gtk_tree_model_get_iter (tree_model, &tmp_iter, path);
1729
1730       /* Figure out number of visible children. */
1731       if (gtk_tree_model_iter_has_child (tree_model, &tmp_iter))
1732         {
1733           GtkTreePath *path2;
1734          /*
1735           * By passing path into this function, we find the number of
1736           * visible children of path.
1737           */
1738           n_inserted = 0;
1739           /* iterate_thru_children modifies path, we don't want that, so give
1740            * it a copy */
1741           path2 = gtk_tree_path_copy (path);
1742           iterate_thru_children (tree_view, tree_model,
1743                                  path2, NULL, &n_inserted, 0);
1744           gtk_tree_path_free (path2);
1745
1746           /* Must add one to include the row that is being added */
1747           n_inserted++;
1748         }
1749       else
1750         n_inserted = 1;
1751
1752       /* Generate children-changed signals */
1753       n_cols = get_n_columns (tree_view);
1754       for (child_row = row; child_row < (row + n_inserted); child_row++)
1755         {
1756           for (col = 0; col < n_cols; col++)
1757             {
1758              /* Pass NULL as the child object, i.e. 4th argument */
1759               g_signal_emit_by_name (atk_obj, "children-changed::add",
1760                                     ((row * n_cols) + col), NULL, NULL);
1761             }
1762         }
1763     }
1764 }
1765
1766 void
1767 _gtk_tree_view_accessible_reorder (GtkTreeView *treeview)
1768 {
1769   GtkTreeViewAccessible *accessible;
1770
1771   accessible = GTK_TREE_VIEW_ACCESSIBLE (_gtk_widget_peek_accessible (GTK_WIDGET (treeview)));
1772   if (accessible == NULL)
1773     return;
1774
1775   g_signal_emit_by_name (accessible, "row-reordered");
1776 }
1777
1778 static gboolean
1779 is_cell_showing (GtkTreeView  *tree_view,
1780                  GdkRectangle *cell_rect)
1781 {
1782   GdkRectangle rect, *visible_rect;
1783   GdkRectangle rect1, *tree_cell_rect;
1784   gint bx, by;
1785   gboolean is_showing;
1786
1787  /* A cell is considered "SHOWING" if any part of the cell is
1788   * in the visible area. Other ways we could do this is by a
1789   * cell's midpoint or if the cell is fully in the visible range.
1790   * Since we have the cell_rect x, y, width, height of the cell,
1791   * any of these is easy to compute.
1792   *
1793   * It is assumed that cell's rectangle is in widget coordinates
1794   * so we must transform to tree cordinates.
1795   */
1796   visible_rect = &rect;
1797   tree_cell_rect = &rect1;
1798   tree_cell_rect->x = cell_rect->x;
1799   tree_cell_rect->y = cell_rect->y;
1800   tree_cell_rect->width = cell_rect->width;
1801   tree_cell_rect->height = cell_rect->height;
1802
1803   gtk_tree_view_get_visible_rect (tree_view, visible_rect);
1804   gtk_tree_view_convert_tree_to_bin_window_coords (tree_view, visible_rect->x,
1805                                                    visible_rect->y, &bx, &by);
1806
1807   if (((tree_cell_rect->x + tree_cell_rect->width) < bx) ||
1808      ((tree_cell_rect->y + tree_cell_rect->height) < by) ||
1809      (tree_cell_rect->x > (bx + visible_rect->width)) ||
1810      (tree_cell_rect->y > (by + visible_rect->height)))
1811     is_showing =  FALSE;
1812   else
1813     is_showing = TRUE;
1814
1815   return is_showing;
1816 }
1817
1818 /* Misc Public */
1819
1820 /* This function is called when a cell's flyweight is created in
1821  * gtk_tree_view_accessible_table_ref_at with emit_change_signal
1822  * set to FALSE and in model_row_changed() on receipt of "row-changed"
1823  * signal when emit_change_signal is set to TRUE
1824  */
1825 static gboolean
1826 update_cell_value (GtkRendererCellAccessible      *renderer_cell,
1827                    GtkTreeViewAccessible *accessible,
1828                    gboolean               emit_change_signal)
1829 {
1830   GtkTreeViewAccessibleCellInfo *cell_info;
1831   GtkTreeView *tree_view;
1832   GtkTreeModel *tree_model;
1833   GtkTreePath *path;
1834   GtkTreeIter iter;
1835   GList *renderers, *cur_renderer;
1836   GParamSpec *spec;
1837   GtkRendererCellAccessibleClass *renderer_cell_class;
1838   GtkCellRendererClass *gtk_cell_renderer_class;
1839   GtkCellAccessible *cell;
1840   gchar **prop_list;
1841   AtkObject *parent;
1842   gboolean is_expander, is_expanded;
1843
1844   renderer_cell_class = GTK_RENDERER_CELL_ACCESSIBLE_GET_CLASS (renderer_cell);
1845   if (renderer_cell->renderer)
1846     gtk_cell_renderer_class = GTK_CELL_RENDERER_GET_CLASS (renderer_cell->renderer);
1847   else
1848     gtk_cell_renderer_class = NULL;
1849
1850   prop_list = renderer_cell_class->property_list;
1851
1852   cell = GTK_CELL_ACCESSIBLE (renderer_cell);
1853   cell_info = find_cell_info (accessible, cell);
1854   if (!cell_info)
1855     return FALSE;
1856
1857   if (emit_change_signal)
1858     {
1859       tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
1860       tree_model = gtk_tree_view_get_model (tree_view);
1861       path = cell_info_get_path (cell_info);
1862       if (path == NULL)
1863         return FALSE;
1864
1865       gtk_tree_model_get_iter (tree_model, &iter, path);
1866       is_expander = FALSE;
1867       is_expanded = FALSE;
1868       if (gtk_tree_model_iter_has_child (tree_model, &iter))
1869         {
1870           GtkTreeViewColumn *expander_tv;
1871
1872           expander_tv = gtk_tree_view_get_expander_column (tree_view);
1873           if (expander_tv == cell_info->cell_col_ref)
1874             {
1875               is_expander = TRUE;
1876               is_expanded = gtk_tree_view_row_expanded (tree_view, path);
1877             }
1878         }
1879       gtk_tree_path_free (path);
1880       gtk_tree_view_column_cell_set_cell_data (cell_info->cell_col_ref,
1881                                                tree_model, &iter,
1882                                                is_expander, is_expanded);
1883     }
1884   renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_info->cell_col_ref));
1885   if (!renderers)
1886     return FALSE;
1887
1888   /* If the cell is in a container, its index is used to find the renderer
1889    * in the list. Otherwise, we assume that the cell is represented
1890    * by the first renderer in the list
1891    */
1892   parent = atk_object_get_parent (ATK_OBJECT (cell));
1893
1894   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
1895     cur_renderer = g_list_nth (renderers, atk_object_get_index_in_parent (ATK_OBJECT (cell)));
1896   else
1897     cur_renderer = renderers;
1898
1899   if (cur_renderer == NULL)
1900     return FALSE;
1901
1902   if (gtk_cell_renderer_class)
1903     {
1904       while (*prop_list)
1905         {
1906           spec = g_object_class_find_property
1907                            (G_OBJECT_CLASS (gtk_cell_renderer_class), *prop_list);
1908
1909           if (spec != NULL)
1910             {
1911               GValue value = G_VALUE_INIT;
1912
1913               g_value_init (&value, spec->value_type);
1914               g_object_get_property (cur_renderer->data, *prop_list, &value);
1915               g_object_set_property (G_OBJECT (renderer_cell->renderer),
1916                                      *prop_list, &value);
1917               g_value_unset (&value);
1918             }
1919           else
1920             g_warning ("Invalid property: %s\n", *prop_list);
1921           prop_list++;
1922         }
1923     }
1924   g_list_free (renderers);
1925
1926   return _gtk_renderer_cell_accessible_update_cache (renderer_cell, emit_change_signal);
1927 }
1928
1929 static gint
1930 get_row_from_tree_path (GtkTreeView *tree_view,
1931                         GtkTreePath *path)
1932 {
1933   GtkTreeModel *tree_model;
1934   GtkTreePath *root_tree;
1935   gint row;
1936
1937   tree_model = gtk_tree_view_get_model (tree_view);
1938
1939   if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
1940     row = gtk_tree_path_get_indices (path)[0];
1941   else
1942     {
1943       root_tree = gtk_tree_path_new_first ();
1944       row = 0;
1945       iterate_thru_children (tree_view, tree_model, root_tree, path, &row, 0);
1946       gtk_tree_path_free (root_tree);
1947     }
1948
1949   return row;
1950 }
1951
1952 /* Misc Private */
1953
1954 /* Helper recursive function that returns an iter to nth row
1955  */
1956 static GtkTreeIter *
1957 return_iter_nth_row (GtkTreeView  *tree_view,
1958                      GtkTreeModel *tree_model,
1959                      GtkTreeIter  *iter,
1960                      gint          increment,
1961                      gint          row)
1962 {
1963   GtkTreePath *current_path;
1964   GtkTreeIter new_iter;
1965   gboolean row_expanded;
1966
1967   current_path = gtk_tree_model_get_path (tree_model, iter);
1968   if (increment == row)
1969     {
1970       gtk_tree_path_free (current_path);
1971       return iter;
1972     }
1973
1974   row_expanded = gtk_tree_view_row_expanded (tree_view, current_path);
1975   gtk_tree_path_free (current_path);
1976
1977   new_iter = *iter;
1978   if ((row_expanded && gtk_tree_model_iter_children (tree_model, iter, &new_iter)) ||
1979       (gtk_tree_model_iter_next (tree_model, iter)) ||
1980       (gtk_tree_model_iter_parent (tree_model, iter, &new_iter) &&
1981           (gtk_tree_model_iter_next (tree_model, iter))))
1982     return return_iter_nth_row (tree_view, tree_model, iter,
1983       ++increment, row);
1984
1985   return NULL;
1986 }
1987
1988 static void
1989 set_iter_nth_row (GtkTreeView *tree_view,
1990                   GtkTreeIter *iter,
1991                   gint         row)
1992 {
1993   GtkTreeModel *tree_model;
1994
1995   tree_model = gtk_tree_view_get_model (tree_view);
1996   gtk_tree_model_get_iter_first (tree_model, iter);
1997   iter = return_iter_nth_row (tree_view, tree_model, iter, 0, row);
1998 }
1999
2000 /* Recursively called until the row specified by orig is found.
2001  *
2002  * *count will be set to the visible row number of the child
2003  * relative to the row that was initially passed in as tree_path.
2004  * tree_path could be modified by this function.
2005  *
2006  * *count will be -1 if orig is not found as a child (a row that is
2007  * not visible will not be found, e.g. if the row is inside a
2008  * collapsed row).  If NULL is passed in as orig, *count will
2009  * be a count of the visible children.
2010  *
2011  * NOTE: the value for depth must be 0 when this recursive function
2012  * is initially called, or it may not function as expected.
2013  */
2014 static void
2015 iterate_thru_children (GtkTreeView  *tree_view,
2016                        GtkTreeModel *tree_model,
2017                        GtkTreePath  *tree_path,
2018                        GtkTreePath  *orig,
2019                        gint         *count,
2020                        gint          depth)
2021 {
2022   GtkTreeIter iter;
2023
2024   if (!gtk_tree_model_get_iter (tree_model, &iter, tree_path))
2025     return;
2026
2027   if (tree_path && orig && !gtk_tree_path_compare (tree_path, orig))
2028     /* Found it! */
2029     return;
2030
2031   if (tree_path && orig && gtk_tree_path_compare (tree_path, orig) > 0)
2032     {
2033       /* Past it, so return -1 */
2034       *count = -1;
2035       return;
2036     }
2037   else if (gtk_tree_view_row_expanded (tree_view, tree_path) &&
2038     gtk_tree_model_iter_has_child (tree_model, &iter))
2039     {
2040       (*count)++;
2041       gtk_tree_path_append_index (tree_path, 0);
2042       iterate_thru_children (tree_view, tree_model, tree_path,
2043                              orig, count, (depth + 1));
2044       return;
2045     }
2046   else if (gtk_tree_model_iter_next (tree_model, &iter))
2047     {
2048       (*count)++;
2049       tree_path = gtk_tree_model_get_path (tree_model, &iter);
2050        if (tree_path)
2051          {
2052            iterate_thru_children (tree_view, tree_model, tree_path,
2053                                  orig, count, depth);
2054            gtk_tree_path_free (tree_path);
2055          }
2056       return;
2057   }
2058   else if (gtk_tree_path_up (tree_path))
2059     {
2060       GtkTreeIter temp_iter;
2061       gboolean exit_loop = FALSE;
2062       gint new_depth = depth - 1;
2063
2064       (*count)++;
2065
2066      /* Make sure that we back up until we find a row
2067       * where gtk_tree_path_next does not return NULL.
2068       */
2069       while (!exit_loop)
2070         {
2071           if (gtk_tree_path_get_depth (tree_path) == 0)
2072               /* depth is now zero so */
2073             return;
2074           gtk_tree_path_next (tree_path);
2075
2076           /* Verify that the next row is a valid row! */
2077           exit_loop = gtk_tree_model_get_iter (tree_model, &temp_iter, tree_path);
2078
2079           if (!exit_loop)
2080             {
2081               /* Keep going up until we find a row that has a valid next */
2082               if (gtk_tree_path_get_depth(tree_path) > 1)
2083                 {
2084                   new_depth--;
2085                   gtk_tree_path_up (tree_path);
2086                 }
2087               else
2088                 {
2089                  /* If depth is 1 and gtk_tree_model_get_iter returns FALSE,
2090                   * then we are at the last row, so just return.
2091                   */
2092                   if (orig != NULL)
2093                     *count = -1;
2094
2095                   return;
2096                 }
2097             }
2098         }
2099
2100      /* This guarantees that we will stop when we hit the end of the
2101       * children.
2102       */
2103       if (new_depth < 0)
2104         return;
2105
2106       iterate_thru_children (tree_view, tree_model, tree_path,
2107                              orig, count, new_depth);
2108       return;
2109     }
2110
2111  /* If it gets here, then the path wasn't found.  Situations
2112   * that would cause this would be if the path passed in is
2113   * invalid or contained within the last row, but not visible
2114   * because the last row is not expanded.  If NULL was passed
2115   * in then a row count is desired, so only set count to -1
2116   * if orig is not NULL.
2117   */
2118   if (orig != NULL)
2119     *count = -1;
2120
2121   return;
2122 }
2123
2124 static void
2125 cell_destroyed (gpointer data)
2126 {
2127   GtkTreeViewAccessibleCellInfo *cell_info = data;
2128
2129   cell_info->cell = NULL;
2130
2131   g_hash_table_remove (cell_info->view->cell_infos, cell_info);
2132 }
2133
2134 static int
2135 cell_info_get_index (GtkTreeView                     *tree_view,
2136                      GtkTreeViewAccessibleCellInfo   *info)
2137 {
2138   int index;
2139
2140   index = _gtk_rbtree_node_get_index (info->tree, info->node) + 1;
2141   index *= get_n_columns (tree_view);
2142   index += get_column_number (tree_view, info->cell_col_ref);
2143
2144   return index;
2145 }
2146
2147 static void
2148 cell_info_new (GtkTreeViewAccessible *accessible,
2149                GtkTreeModel          *tree_model,
2150                GtkRBTree             *tree,
2151                GtkRBNode             *node,
2152                GtkTreeViewColumn     *tv_col,
2153                GtkCellAccessible     *cell)
2154 {
2155   GtkTreeViewAccessibleCellInfo *cell_info;
2156
2157   cell_info = g_new (GtkTreeViewAccessibleCellInfo, 1);
2158
2159   cell_info->tree = tree;
2160   cell_info->node = node;
2161   cell_info->cell_col_ref = tv_col;
2162   cell_info->cell = cell;
2163   cell_info->view = accessible;
2164
2165   g_object_set_qdata_full (G_OBJECT (cell), 
2166                            gtk_tree_view_accessible_get_data_quark (),
2167                            cell_info,
2168                            cell_destroyed);
2169
2170   g_hash_table_replace (accessible->cell_infos, cell_info, cell_info);
2171 }
2172
2173 static GtkCellAccessible *
2174 peek_cell (GtkTreeViewAccessible *accessible,
2175            GtkRBTree             *tree,
2176            GtkRBNode             *node,
2177            GtkTreeViewColumn     *column)
2178 {
2179   GtkTreeViewAccessibleCellInfo lookup, *cell_info;
2180
2181   lookup.tree = tree;
2182   lookup.node = node;
2183   lookup.cell_col_ref = column;
2184
2185   cell_info = g_hash_table_lookup (accessible->cell_infos, &lookup);
2186   if (cell_info == NULL)
2187     return NULL;
2188
2189   return cell_info->cell;
2190 }
2191
2192 static GtkCellAccessible *
2193 find_cell (GtkTreeViewAccessible *accessible,
2194            gint                   index)
2195 {
2196   GtkTreeView *tree_view;
2197   GtkRBTree *tree;
2198   GtkRBNode *node;
2199
2200   tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
2201
2202   if (!_gtk_rbtree_find_index (_gtk_tree_view_get_rbtree (tree_view),
2203                                index / get_n_columns (tree_view) - 1,
2204                                &tree,
2205                                &node))
2206     {
2207       g_assert_not_reached ();
2208     }
2209
2210   return peek_cell (accessible,
2211                     tree, node, 
2212                     get_visible_column (tree_view, index % get_n_columns (tree_view)));
2213 }
2214
2215 static void
2216 connect_model_signals (GtkTreeView           *view,
2217                        GtkTreeViewAccessible *accessible)
2218 {
2219   GObject *obj;
2220
2221   obj = G_OBJECT (accessible->tree_model);
2222   g_signal_connect_data (obj, "row-changed",
2223                          G_CALLBACK (model_row_changed), view, NULL, 0);
2224   g_signal_connect_data (obj, "row-inserted",
2225                          G_CALLBACK (model_row_inserted), view, NULL,
2226                          G_CONNECT_AFTER);
2227 }
2228
2229 static void
2230 disconnect_model_signals (GtkTreeViewAccessible *accessible)
2231 {
2232   GObject *obj;
2233   GtkWidget *widget;
2234
2235   obj = G_OBJECT (accessible->tree_model);
2236   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
2237   g_signal_handlers_disconnect_by_func (obj, model_row_changed, widget);
2238   g_signal_handlers_disconnect_by_func (obj, model_row_inserted, widget);
2239 }
2240
2241 /* Returns the column number of the specified GtkTreeViewColumn
2242  * The column must be visible.
2243  */
2244 static gint
2245 get_column_number (GtkTreeView       *treeview,
2246                    GtkTreeViewColumn *column)
2247 {
2248   GtkTreeViewColumn *cur;
2249   guint i, number;
2250
2251   number = 0;
2252
2253   for (i = 0; i < gtk_tree_view_get_n_columns (treeview); i++)
2254     {
2255       cur = gtk_tree_view_get_column (treeview, i);
2256       
2257       if (!gtk_tree_view_column_get_visible (cur))
2258         continue;
2259
2260       if (cur == column)
2261         break;
2262
2263       number++;
2264     }
2265
2266   g_return_val_if_fail (i < gtk_tree_view_get_n_columns (treeview), 0);
2267
2268   return number;
2269 }
2270
2271 static gint
2272 get_index (GtkTreeView *tree_view,
2273            GtkTreePath *path,
2274            gint         actual_column)
2275 {
2276   gint depth = 0;
2277   gint index = 1;
2278   gint *indices = NULL;
2279
2280   if (path)
2281     {
2282       depth = gtk_tree_path_get_depth (path);
2283       indices = gtk_tree_path_get_indices (path);
2284     }
2285
2286   if (depth > 1)
2287     {
2288       GtkTreePath *copy_path;
2289       GtkTreeModel *model;
2290
2291       model = gtk_tree_view_get_model (tree_view);
2292       copy_path = gtk_tree_path_copy (path);
2293       gtk_tree_path_up (copy_path);
2294       count_rows (model, NULL, copy_path, &index, 0, depth);
2295       gtk_tree_path_free (copy_path);
2296     }
2297
2298   if (path)
2299     index += indices[depth - 1];
2300   index *= get_n_columns (tree_view);
2301   index +=  actual_column;
2302   return index;
2303 }
2304
2305 /* The function count_rows counts the number of rows starting at iter
2306  * and ending at end_path. The value of level is the depth of iter and
2307  * the value of depth is the depth of end_path. Rows at depth before
2308  * end_path are counted. This functions counts rows which are not visible
2309  * because an ancestor is collapsed.
2310  */
2311 static void
2312 count_rows (GtkTreeModel *model,
2313             GtkTreeIter  *iter,
2314             GtkTreePath  *end_path,
2315             gint         *count,
2316             gint          level,
2317             gint          depth)
2318 {
2319   GtkTreeIter child_iter;
2320
2321   if (!model)
2322     return;
2323
2324   level++;
2325   *count += gtk_tree_model_iter_n_children (model, iter);
2326
2327   if (gtk_tree_model_get_flags (model) & GTK_TREE_MODEL_LIST_ONLY)
2328     return;
2329
2330   if (level >= depth)
2331     return;
2332
2333   if (gtk_tree_model_iter_children (model, &child_iter, iter))
2334     {
2335       gboolean ret_val = TRUE;
2336
2337       while (ret_val)
2338         {
2339           if (level == depth - 1)
2340             {
2341               GtkTreePath *iter_path;
2342               gboolean finished = FALSE;
2343
2344               iter_path = gtk_tree_model_get_path (model, &child_iter);
2345               if (end_path && gtk_tree_path_compare (iter_path, end_path) >= 0)
2346                 finished = TRUE;
2347               gtk_tree_path_free (iter_path);
2348               if (finished)
2349                 break;
2350             }
2351           if (gtk_tree_model_iter_has_child (model, &child_iter))
2352             count_rows (model, &child_iter, end_path, count, level, depth);
2353           ret_val = gtk_tree_model_iter_next (model, &child_iter);
2354         }
2355     }
2356 }
2357
2358 static gboolean
2359 get_rbtree_column_from_index (GtkTreeView        *tree_view,
2360                               gint                index,
2361                               GtkRBTree         **tree,
2362                               GtkRBNode         **node,
2363                               GtkTreeViewColumn **column)
2364 {
2365   guint n_columns = get_n_columns (tree_view);
2366
2367   if (n_columns == 0)
2368     return FALSE;
2369   /* First row is the column headers */
2370   index -= n_columns;
2371   if (index < 0)
2372     return FALSE;
2373
2374   if (tree)
2375     {
2376       g_return_val_if_fail (node != NULL, FALSE);
2377
2378       if (!_gtk_rbtree_find_index (_gtk_tree_view_get_rbtree (tree_view),
2379                                    index / n_columns,
2380                                    tree,
2381                                    node))
2382         return FALSE;
2383     }
2384
2385   if (column)
2386     {
2387       *column = get_visible_column (tree_view, index % n_columns);
2388       if (*column == NULL)
2389         return FALSE;
2390   }
2391   return TRUE;
2392 }
2393
2394 static GtkTreeViewAccessibleCellInfo *
2395 find_cell_info (GtkTreeViewAccessible *accessible,
2396                 GtkCellAccessible     *cell)
2397 {
2398   AtkObject *parent;
2399   
2400   parent = atk_object_get_parent (ATK_OBJECT (cell));
2401   while (parent != ATK_OBJECT (accessible))
2402     {
2403       cell = GTK_CELL_ACCESSIBLE (parent);
2404       parent = atk_object_get_parent (ATK_OBJECT (cell));
2405     }
2406
2407   return g_object_get_qdata (G_OBJECT (cell),
2408                              gtk_tree_view_accessible_get_data_quark ());
2409 }
2410
2411 static AtkObject *
2412 get_header_from_column (GtkTreeViewColumn *tv_col)
2413 {
2414   AtkObject *rc;
2415   GtkWidget *header_widget;
2416
2417   if (tv_col == NULL)
2418     return NULL;
2419
2420   header_widget = gtk_tree_view_column_get_button (tv_col);
2421
2422   if (header_widget)
2423     rc = gtk_widget_get_accessible (header_widget);
2424   else
2425     rc = NULL;
2426
2427   return rc;
2428 }
2429
2430 void
2431 _gtk_tree_view_accessible_add (GtkTreeView *treeview,
2432                                GtkRBTree   *tree,
2433                                GtkRBNode   *node)
2434 {
2435   GtkTreeViewAccessible *accessible;
2436   guint row, n_rows;
2437
2438   accessible = GTK_TREE_VIEW_ACCESSIBLE (_gtk_widget_peek_accessible (GTK_WIDGET (treeview)));
2439   if (accessible == NULL)
2440     return;
2441
2442   if (node == NULL)
2443     {
2444       row = tree->parent_tree ? _gtk_rbtree_node_get_index (tree->parent_tree, tree->parent_node) : 0;
2445       n_rows = tree->root->total_count;
2446     }
2447   else
2448     {
2449       row = _gtk_rbtree_node_get_index (tree, node);
2450       n_rows = 1 + (node->children ? node->children->root->total_count : 0);
2451     }
2452
2453   g_signal_emit_by_name (accessible, "row-inserted", row, n_rows);
2454 }
2455
2456 void
2457 _gtk_tree_view_accessible_remove (GtkTreeView *treeview,
2458                                   GtkRBTree   *tree,
2459                                   GtkRBNode   *node)
2460 {
2461   GtkTreeViewAccessibleCellInfo *cell_info;
2462   GHashTableIter iter;
2463   GtkTreeViewAccessible *accessible;
2464   guint row, n_rows;
2465
2466   accessible = GTK_TREE_VIEW_ACCESSIBLE (_gtk_widget_peek_accessible (GTK_WIDGET (treeview)));
2467   if (accessible == NULL)
2468     return;
2469
2470   /* if this shows up in profiles, special-case node->children == NULL */
2471
2472   if (node == NULL)
2473     {
2474       row = tree->parent_tree ? _gtk_rbtree_node_get_index (tree->parent_tree, tree->parent_node) : 0;
2475       n_rows = tree->root->total_count + 1;
2476     }
2477   else
2478     {
2479       row = _gtk_rbtree_node_get_index (tree, node);
2480       n_rows = 1 + (node->children ? node->children->root->total_count : 0);
2481
2482       tree = node->children;
2483     }
2484
2485   g_signal_emit_by_name (accessible, "row-deleted", row, n_rows);
2486
2487   g_hash_table_iter_init (&iter, accessible->cell_infos);
2488   while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&cell_info))
2489     {
2490       if (node == cell_info->node ||
2491           tree == cell_info->tree ||
2492           (tree && _gtk_rbtree_contains (tree, cell_info->tree)))
2493         g_hash_table_iter_remove (&iter);
2494     }
2495 }
2496
2497 /* NB: id is not checked, only columns < id are.
2498  * This is important so the function works for notification of removal of a column */
2499 static guint
2500 to_visible_column_id (GtkTreeView *treeview,
2501                       guint        id)
2502 {
2503   guint i;
2504   guint invisible;
2505
2506   invisible = 0;
2507
2508   for (i = 0; i < id; i++)
2509     {
2510       GtkTreeViewColumn *column = gtk_tree_view_get_column (treeview, i);
2511
2512       if (!gtk_tree_view_column_get_visible (column))
2513         invisible++;
2514     }
2515
2516   return id - invisible;
2517 }
2518
2519 void
2520 _gtk_tree_view_accessible_do_add_column (GtkTreeViewAccessible *accessible,
2521                                          GtkTreeView           *treeview,
2522                                          GtkTreeViewColumn     *column,
2523                                          guint                  id)
2524 {
2525   guint row, n_rows, n_cols;
2526
2527   /* Generate column-inserted signal */
2528   g_signal_emit_by_name (accessible, "column-inserted", id, 1);
2529
2530   n_rows = get_n_rows (treeview);
2531   n_cols = get_n_columns (treeview);
2532
2533   /* Generate children-changed signals */
2534   for (row = 0; row <= n_rows; row++)
2535     {
2536      /* Pass NULL as the child object, i.e. 4th argument */
2537       g_signal_emit_by_name (accessible, "children-changed::add",
2538                              (row * n_cols) + id, NULL, NULL);
2539     }
2540 }
2541
2542 void
2543 _gtk_tree_view_accessible_add_column (GtkTreeView       *treeview,
2544                                       GtkTreeViewColumn *column,
2545                                       guint              id)
2546 {
2547   AtkObject *obj;
2548
2549   if (!gtk_tree_view_column_get_visible (column))
2550     return;
2551
2552   obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
2553   if (obj == NULL)
2554     return;
2555
2556   _gtk_tree_view_accessible_do_add_column (GTK_TREE_VIEW_ACCESSIBLE (obj),
2557                                            treeview,
2558                                            column,
2559                                            to_visible_column_id (treeview, id));
2560 }
2561
2562 void
2563 _gtk_tree_view_accessible_do_remove_column (GtkTreeViewAccessible *accessible,
2564                                             GtkTreeView           *treeview,
2565                                             GtkTreeViewColumn     *column,
2566                                             guint                  id)
2567 {
2568   GtkTreeViewAccessibleCellInfo *cell_info;
2569   GHashTableIter iter;
2570   gpointer value;
2571   guint row, n_rows, n_cols;
2572
2573   /* Clean column from cache */
2574   g_hash_table_iter_init (&iter, accessible->cell_infos);
2575   while (g_hash_table_iter_next (&iter, NULL, &value))
2576     {
2577       cell_info = value;
2578       if (cell_info->cell_col_ref == column)
2579         g_hash_table_iter_remove (&iter);
2580     }
2581
2582   /* Generate column-deleted signal */
2583   g_signal_emit_by_name (accessible, "column-deleted", id, 1);
2584
2585   n_rows = get_n_rows (treeview);
2586   n_cols = get_n_columns (treeview);
2587
2588   /* Generate children-changed signals */
2589   for (row = 0; row <= n_rows; row++)
2590     {
2591       /* Pass NULL as the child object, 4th argument */
2592       g_signal_emit_by_name (accessible, "children-changed::remove",
2593                              (row * n_cols) + id, NULL, NULL);
2594     }
2595 }
2596
2597 void
2598 _gtk_tree_view_accessible_remove_column (GtkTreeView       *treeview,
2599                                          GtkTreeViewColumn *column,
2600                                          guint              id)
2601 {
2602   AtkObject *obj;
2603
2604   if (!gtk_tree_view_column_get_visible (column))
2605     return;
2606
2607   obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
2608   if (obj == NULL)
2609     return;
2610
2611   _gtk_tree_view_accessible_do_remove_column (GTK_TREE_VIEW_ACCESSIBLE (obj),
2612                                               treeview,
2613                                               column,
2614                                               to_visible_column_id (treeview, id));
2615 }
2616
2617 void
2618 _gtk_tree_view_accessible_reorder_column (GtkTreeView       *treeview,
2619                                           GtkTreeViewColumn *column)
2620 {
2621   AtkObject *obj;
2622
2623   obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
2624   if (obj == NULL)
2625     return;
2626
2627   g_signal_emit_by_name (obj, "column-reordered");
2628 }
2629
2630 void
2631 _gtk_tree_view_accessible_toggle_visibility (GtkTreeView       *treeview,
2632                                              GtkTreeViewColumn *column)
2633 {
2634   AtkObject *obj;
2635   guint id;
2636
2637   obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
2638   if (obj == NULL)
2639     return;
2640
2641   id = get_column_number (treeview, column);
2642
2643   if (gtk_tree_view_column_get_visible (column))
2644     _gtk_tree_view_accessible_do_add_column (GTK_TREE_VIEW_ACCESSIBLE (obj),
2645                                              treeview,
2646                                              column,
2647                                              id);
2648   else
2649     _gtk_tree_view_accessible_do_remove_column (GTK_TREE_VIEW_ACCESSIBLE (obj),
2650                                                 treeview,
2651                                                 column,
2652                                                 id);
2653 }
2654
2655 void
2656 _gtk_tree_view_accessible_add_state (GtkTreeView          *treeview,
2657                                      GtkRBTree            *tree,
2658                                      GtkRBNode            *node,
2659                                      GtkCellRendererState  state)
2660 {
2661   GtkTreeViewAccessible *accessible;
2662   AtkObject *obj;
2663   guint i;
2664
2665   obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
2666   if (obj == NULL)
2667     return;
2668
2669   accessible = GTK_TREE_VIEW_ACCESSIBLE (obj);
2670
2671   for (i = 0; i < gtk_tree_view_get_n_columns (treeview); i++)
2672     {
2673       GtkCellAccessible *cell = peek_cell (accessible,
2674                                            tree, node,
2675                                            gtk_tree_view_get_column (treeview, i));
2676
2677       if (cell == NULL)
2678         continue;
2679
2680       _gtk_cell_accessible_state_changed (cell, state, 0);
2681     }
2682 }
2683
2684 void
2685 _gtk_tree_view_accessible_remove_state (GtkTreeView          *treeview,
2686                                         GtkRBTree            *tree,
2687                                         GtkRBNode            *node,
2688                                         GtkCellRendererState  state)
2689 {
2690   GtkTreeViewAccessible *accessible;
2691   AtkObject *obj;
2692   guint i;
2693
2694   obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
2695   if (obj == NULL)
2696     return;
2697
2698   accessible = GTK_TREE_VIEW_ACCESSIBLE (obj);
2699
2700   for (i = 0; i < gtk_tree_view_get_n_columns (treeview); i++)
2701     {
2702       GtkCellAccessible *cell = peek_cell (accessible,
2703                                            tree, node,
2704                                            gtk_tree_view_get_column (treeview, i));
2705
2706       if (cell == NULL)
2707         continue;
2708
2709       _gtk_cell_accessible_state_changed (cell, 0, state);
2710     }
2711 }
2712
2713 void
2714 _gtk_tree_view_accessible_expanded (GtkTreeView *treeview, 
2715                                     GtkRBTree   *tree,
2716                                     GtkRBNode   *node)
2717 {
2718   AtkObject *obj;
2719
2720   obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
2721   if (obj == NULL)
2722     return;
2723
2724   _gtk_tree_view_accessible_add_state (treeview,
2725                                        tree, node,
2726                                        GTK_CELL_RENDERER_EXPANDED);
2727 }
2728