]> Pileus Git - ~andy/gtk/blob - gtk/a11y/gtktreeviewaccessible.c
a11y: Redo indexing
[~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 gboolean row_expanded_cb      (GtkTreeView      *tree_view,
51                                       GtkTreeIter      *iter,
52                                       GtkTreePath      *path);
53 static gboolean row_collapsed_cb     (GtkTreeView      *tree_view,
54                                       GtkTreeIter      *iter,
55                                       GtkTreePath      *path);
56 static void     size_allocate_cb     (GtkWidget        *widget,
57                                       GtkAllocation    *allocation);
58 static void     selection_changed_cb (GtkTreeSelection *selection,
59                                       gpointer          data);
60
61 static void     columns_changed      (GtkTreeView      *tree_view);
62 static void     cursor_changed       (GtkTreeView      *tree_view,
63                                       GtkTreeViewAccessible *accessible);
64 static gboolean focus_in             (GtkWidget        *widget);
65 static gboolean focus_out            (GtkWidget        *widget);
66
67 static void     column_visibility_changed
68                                      (GObject          *object,
69                                       GParamSpec       *param,
70                                       gpointer          user_data);
71 static void     destroy_count_func   (GtkTreeView      *tree_view,
72                                       GtkTreePath      *path,
73                                       gint              count,
74                                       gpointer          user_data);
75
76 /* Misc */
77
78 static void             set_iter_nth_row                (GtkTreeView            *tree_view,
79                                                          GtkTreeIter            *iter,
80                                                          gint                   row);
81 static gint             get_row_from_tree_path          (GtkTreeView            *tree_view,
82                                                          GtkTreePath            *path);
83 static GtkTreeViewColumn* get_column                    (GtkTreeView            *tree_view,
84                                                          gint                   in_col);
85 static gint             get_actual_column_number        (GtkTreeView            *tree_view,
86                                                          gint                   visible_column);
87 static gint             get_visible_column_number       (GtkTreeView            *tree_view,
88                                                          gint                   actual_column);
89 static void             iterate_thru_children           (GtkTreeView            *tree_view,
90                                                          GtkTreeModel           *tree_model,
91                                                          GtkTreePath            *tree_path,
92                                                          GtkTreePath            *orig,
93                                                          gint                   *count,
94                                                          gint                   depth);
95 static int              cell_info_get_index             (GtkTreeView                     *tree_view,
96                                                          GtkTreeViewAccessibleCellInfo   *info);
97 static void             clean_rows                      (GtkTreeViewAccessible           *tree_view);
98 static void             clean_cols                      (GtkTreeViewAccessible           *tree_view,
99                                                          GtkTreeViewColumn      *tv_col);
100 static void             traverse_cells                  (GtkTreeViewAccessible           *tree_view,
101                                                          GtkTreePath            *tree_path,
102                                                          gboolean               inc_row);
103 static gboolean         update_cell_value               (GtkRendererCellAccessible       *renderer_cell,
104                                                          GtkTreeViewAccessible           *accessible,
105                                                          gboolean               emit_change_signal);
106 static void             set_cell_visibility             (GtkTreeView            *tree_view,
107                                                          GtkCellAccessible      *cell,
108                                                          GtkTreeViewColumn      *tv_col,
109                                                          GtkTreePath            *tree_path,
110                                                          gboolean               emit_signal);
111 static gboolean         is_cell_showing                 (GtkTreeView            *tree_view,
112                                                          GdkRectangle           *cell_rect);
113 static void             set_expand_state                (GtkTreeView            *tree_view,
114                                                          GtkTreeModel           *tree_model,
115                                                          GtkTreeViewAccessible           *accessible,
116                                                          GtkTreePath            *tree_path,
117                                                          gboolean               set_on_ancestor);
118 static void             set_cell_expandable             (GtkCellAccessible     *cell);
119 static void             add_cell_actions                (GtkCellAccessible     *cell,
120                                                          gboolean               editable);
121
122 static void             toggle_cell_toggled             (GtkCellAccessible     *cell);
123 static void             edit_cell                       (GtkCellAccessible     *cell);
124 static void             activate_cell                   (GtkCellAccessible     *cell);
125 static void             cell_destroyed                  (gpointer               data);
126 static void             cell_info_new                   (GtkTreeViewAccessible           *accessible,
127                                                          GtkTreeModel           *tree_model,
128                                                          GtkRBTree              *tree,
129                                                          GtkRBNode              *node,
130                                                          GtkTreeViewColumn      *tv_col,
131                                                          GtkCellAccessible      *cell);
132 static GtkCellAccessible *find_cell                       (GtkTreeViewAccessible           *accessible,
133                                                          gint                   index);
134 static void             connect_model_signals           (GtkTreeView            *view,
135                                                          GtkTreeViewAccessible           *accessible);
136 static void             disconnect_model_signals        (GtkTreeViewAccessible           *accessible);
137 static gint             get_column_number               (GtkTreeView            *tree_view,
138                                                          GtkTreeViewColumn      *column,
139                                                          gboolean               visible);
140 static gint             get_focus_index                 (GtkTreeView            *tree_view);
141 static gint             get_index                       (GtkTreeView            *tree_view,
142                                                          GtkTreePath            *path,
143                                                          gint                   actual_column);
144 static void             count_rows                      (GtkTreeModel           *model,
145                                                          GtkTreeIter            *iter,
146                                                          GtkTreePath            *end_path,
147                                                          gint                   *count,
148                                                          gint                   level,
149                                                          gint                   depth);
150
151 static gboolean         get_rbtree_column_from_index    (GtkTreeView            *tree_view,
152                                                          gint                   index,
153                                                          GtkRBTree              **tree,
154                                                          GtkRBNode              **node,
155                                                          GtkTreeViewColumn      **column);
156
157 static GtkTreeViewAccessibleCellInfo* find_cell_info    (GtkTreeViewAccessible           *view,
158                                                          GtkCellAccessible               *cell,
159                                                          gboolean                live_only);
160 static AtkObject *       get_header_from_column         (GtkTreeViewColumn      *tv_col);
161
162
163 static void atk_table_interface_init                  (AtkTableIface                *iface);
164 static void atk_selection_interface_init              (AtkSelectionIface            *iface);
165 static void atk_component_interface_init              (AtkComponentIface            *iface);
166 static void gtk_cell_accessible_parent_interface_init (GtkCellAccessibleParentIface *iface);
167
168 G_DEFINE_TYPE_WITH_CODE (GtkTreeViewAccessible, _gtk_tree_view_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
169                          G_IMPLEMENT_INTERFACE (ATK_TYPE_TABLE, atk_table_interface_init)
170                          G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init)
171                          G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, atk_component_interface_init)
172                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_ACCESSIBLE_PARENT, gtk_cell_accessible_parent_interface_init))
173
174
175 static void
176 adjustment_changed (GtkAdjustment *adjustment,
177                     GtkWidget     *widget)
178 {
179   GtkTreeViewAccessible *accessible;
180
181   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
182   traverse_cells (accessible, NULL, FALSE);
183 }
184
185 static void
186 hadjustment_set_cb (GObject    *widget,
187                     GParamSpec *pspec,
188                     gpointer    data)
189 {
190   GtkTreeViewAccessible *accessible = data;
191   GtkAdjustment *adj;
192
193   g_object_get (widget, "hadjustment", &adj, NULL);
194   accessible->old_hadj = adj;
195   g_object_add_weak_pointer (G_OBJECT (accessible->old_hadj), (gpointer *)&accessible->old_hadj);
196   g_signal_connect (adj, "value-changed", G_CALLBACK (adjustment_changed), widget);
197 }
198
199 static void
200 vadjustment_set_cb (GObject    *widget,
201                     GParamSpec *pspec,
202                     gpointer    data)
203 {
204   GtkTreeViewAccessible *accessible = data;
205   GtkAdjustment *adj;
206
207   g_object_get (widget, "vadjustment", &adj, NULL);
208   accessible->old_vadj = adj;
209   g_object_add_weak_pointer (G_OBJECT (accessible->old_vadj), (gpointer *)&accessible->old_vadj);
210   g_signal_connect (adj, "value-changed",
211                     G_CALLBACK (adjustment_changed), widget);
212 }
213
214 static GQuark
215 gtk_tree_view_accessible_get_data_quark (void)
216 {
217   static GQuark quark = 0;
218
219   if (G_UNLIKELY (quark == 0))
220     quark = g_quark_from_static_string ("gtk-tree-view-accessible-data");
221
222   return quark;
223 }
224
225 static void
226 cell_info_free (GtkTreeViewAccessibleCellInfo *cell_info)
227 {
228   if (cell_info->cell)
229     {
230       g_object_steal_qdata (G_OBJECT (cell_info->cell),
231                             gtk_tree_view_accessible_get_data_quark ());
232       _gtk_cell_accessible_add_state (cell_info->cell, ATK_STATE_DEFUNCT, FALSE);
233     }
234
235   g_free (cell_info);
236 }
237
238 static GtkTreePath *
239 cell_info_get_path (GtkTreeViewAccessibleCellInfo *cell_info)
240 {
241   return _gtk_tree_view_find_path (NULL,
242                                    cell_info->tree,
243                                    cell_info->node);
244 }
245
246 static void
247 gtk_tree_view_accessible_initialize (AtkObject *obj,
248                                      gpointer   data)
249 {
250   GtkTreeViewAccessible *accessible;
251   GtkTreeView *tree_view;
252   GtkTreeModel *tree_model;
253   GList *tv_cols, *tmp_list;
254   GtkWidget *widget;
255   GtkTreeSelection *selection;
256
257   ATK_OBJECT_CLASS (_gtk_tree_view_accessible_parent_class)->initialize (obj, data);
258
259   accessible = GTK_TREE_VIEW_ACCESSIBLE (obj);
260   accessible->col_data = NULL;
261   accessible->focus_cell = NULL;
262   accessible->old_hadj = NULL;
263   accessible->old_vadj = NULL;
264   accessible->idle_expand_id = 0;
265   accessible->idle_expand_path = NULL;
266   accessible->n_children_deleted = 0;
267
268   accessible->cell_infos = g_hash_table_new_full (g_direct_hash,
269       g_direct_equal, NULL, (GDestroyNotify) cell_info_free);
270
271   widget = GTK_WIDGET (data);
272   tree_view = GTK_TREE_VIEW (widget);
273   tree_model = gtk_tree_view_get_model (tree_view);
274   selection = gtk_tree_view_get_selection (tree_view);
275
276   g_signal_connect_after (widget, "row-collapsed",
277                           G_CALLBACK (row_collapsed_cb), NULL);
278   g_signal_connect (widget, "row-expanded",
279                     G_CALLBACK (row_expanded_cb), NULL);
280   g_signal_connect (widget, "size-allocate",
281                     G_CALLBACK (size_allocate_cb), NULL);
282   g_signal_connect (selection, "changed",
283                     G_CALLBACK (selection_changed_cb), obj);
284
285   g_signal_connect (tree_view, "columns-changed",
286                     G_CALLBACK (columns_changed), NULL);
287   g_signal_connect (tree_view, "cursor-changed",
288                     G_CALLBACK (cursor_changed), accessible);
289   g_signal_connect (tree_view, "focus-in-event",
290                     G_CALLBACK (focus_in), NULL);
291   g_signal_connect (tree_view, "focus-out-event",
292                     G_CALLBACK (focus_out), NULL);
293
294   accessible->tree_model = tree_model;
295   accessible->n_cols = 0;
296   if (tree_model)
297     {
298       g_object_add_weak_pointer (G_OBJECT (accessible->tree_model), (gpointer *)&accessible->tree_model);
299       connect_model_signals (tree_view, accessible);
300
301       if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
302         obj->role = ATK_ROLE_TABLE;
303       else
304         obj->role = ATK_ROLE_TREE_TABLE;
305     }
306
307   hadjustment_set_cb (G_OBJECT (widget), NULL, accessible);
308   vadjustment_set_cb (G_OBJECT (widget), NULL, accessible);
309   g_signal_connect (widget, "notify::hadjustment",
310                     G_CALLBACK (hadjustment_set_cb), accessible);
311   g_signal_connect (widget, "notify::vadjustment",
312                     G_CALLBACK (vadjustment_set_cb), accessible);
313
314   accessible->col_data = g_array_sized_new (FALSE, TRUE,
315                                             sizeof (GtkTreeViewColumn *), 0);
316
317   tv_cols = gtk_tree_view_get_columns (tree_view);
318   for (tmp_list = tv_cols; tmp_list; tmp_list = tmp_list->next)
319     {
320       accessible->n_cols++;
321       g_signal_connect (tmp_list->data, "notify::visible",
322                         G_CALLBACK (column_visibility_changed), tree_view);
323       g_array_append_val (accessible->col_data, tmp_list->data);
324     }
325   g_list_free (tv_cols);
326
327   gtk_tree_view_set_destroy_count_func (tree_view,
328                                         destroy_count_func,
329                                         NULL, NULL);
330 }
331
332 static void
333 gtk_tree_view_accessible_finalize (GObject *object)
334 {
335   GtkTreeViewAccessible *accessible = GTK_TREE_VIEW_ACCESSIBLE (object);
336
337   /* remove any idle handlers still pending */
338   if (accessible->idle_expand_id)
339     g_source_remove (accessible->idle_expand_id);
340
341   if (accessible->tree_model)
342     disconnect_model_signals (accessible);
343
344   if (accessible->cell_infos)
345     g_hash_table_destroy (accessible->cell_infos);
346
347   if (accessible->col_data)
348     {
349       GArray *array = accessible->col_data;
350
351      /* No need to free the contents of the array since it
352       * just contains pointers to the GtkTreeViewColumn
353       * objects that are in the GtkTreeView.
354       */
355       g_array_free (array, TRUE);
356     }
357
358   G_OBJECT_CLASS (_gtk_tree_view_accessible_parent_class)->finalize (object);
359 }
360
361 static void
362 gtk_tree_view_accessible_notify_gtk (GObject    *obj,
363                                      GParamSpec *pspec)
364 {
365   GtkWidget *widget;
366   GtkTreeView *tree_view;
367   GtkTreeViewAccessible *accessible;
368   GtkAdjustment *adj;
369
370   widget = GTK_WIDGET (obj);
371   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
372   tree_view = GTK_TREE_VIEW (widget);
373
374   if (g_strcmp0 (pspec->name, "model") == 0)
375     {
376       GtkTreeModel *tree_model;
377       AtkRole role;
378
379       tree_model = gtk_tree_view_get_model (tree_view);
380       if (accessible->tree_model)
381         disconnect_model_signals (accessible);
382       g_hash_table_remove_all (accessible->cell_infos);
383       accessible->tree_model = tree_model;
384
385       if (tree_model)
386         {
387           g_object_add_weak_pointer (G_OBJECT (accessible->tree_model), (gpointer *)&accessible->tree_model);
388           connect_model_signals (tree_view, accessible);
389
390           if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
391             role = ATK_ROLE_TABLE;
392           else
393             role = ATK_ROLE_TREE_TABLE;
394         }
395       else
396         {
397           role = ATK_ROLE_UNKNOWN;
398         }
399       atk_object_set_role (ATK_OBJECT (accessible), role);
400       g_object_freeze_notify (G_OBJECT (accessible));
401       g_signal_emit_by_name (accessible, "model-changed");
402       g_signal_emit_by_name (accessible, "visible-data-changed");
403       g_object_thaw_notify (G_OBJECT (accessible));
404     }
405   else if (g_strcmp0 (pspec->name, "hadjustment") == 0)
406     {
407       g_object_get (tree_view, "hadjustment", &adj, NULL);
408       g_signal_handlers_disconnect_by_func (accessible->old_hadj,
409                                             (gpointer) adjustment_changed,
410                                             widget);
411       accessible->old_hadj = adj;
412       g_object_add_weak_pointer (G_OBJECT (accessible->old_hadj), (gpointer *)&accessible->old_hadj);
413       g_signal_connect (adj, "value-changed", G_CALLBACK (adjustment_changed), tree_view);
414     }
415   else if (g_strcmp0 (pspec->name, "vadjustment") == 0)
416     {
417       g_object_get (tree_view, "vadjustment", &adj, NULL);
418       g_signal_handlers_disconnect_by_func (accessible->old_vadj,
419                                             (gpointer) adjustment_changed,
420                                             widget);
421       accessible->old_vadj = adj;
422       g_object_add_weak_pointer (G_OBJECT (accessible->old_hadj), (gpointer *)&accessible->old_vadj);
423       g_signal_connect (adj, "value-changed", G_CALLBACK (adjustment_changed), tree_view);
424     }
425   else
426     GTK_WIDGET_ACCESSIBLE_CLASS (_gtk_tree_view_accessible_parent_class)->notify_gtk (obj, pspec);
427 }
428
429 static void
430 gtk_tree_view_accessible_destroyed (GtkWidget     *widget,
431                                     GtkAccessible *gtk_accessible)
432 {
433   GtkAdjustment *adj;
434   GtkTreeViewAccessible *accessible;
435
436   if (!GTK_IS_TREE_VIEW (widget))
437     return;
438
439   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_accessible);
440   adj = accessible->old_hadj;
441   if (adj)
442     g_signal_handlers_disconnect_by_func (adj,
443                                           (gpointer) adjustment_changed,
444                                           widget);
445   adj = accessible->old_vadj;
446   if (adj)
447     g_signal_handlers_disconnect_by_func (adj,
448                                           (gpointer) adjustment_changed,
449                                           widget);
450   if (accessible->tree_model)
451     {
452       disconnect_model_signals (accessible);
453       accessible->tree_model = NULL;
454     }
455   if (accessible->focus_cell)
456     {
457       g_object_unref (accessible->focus_cell);
458       accessible->focus_cell = NULL;
459     }
460   if (accessible->idle_expand_id)
461     {
462       g_source_remove (accessible->idle_expand_id);
463       accessible->idle_expand_id = 0;
464     }
465 }
466
467 static void
468 gtk_tree_view_accessible_connect_widget_destroyed (GtkAccessible *accessible)
469 {
470   GtkWidget *widget;
471
472   widget = gtk_accessible_get_widget (accessible);
473   if (widget)
474     g_signal_connect_after (widget, "destroy",
475                             G_CALLBACK (gtk_tree_view_accessible_destroyed), accessible);
476
477   GTK_ACCESSIBLE_CLASS (_gtk_tree_view_accessible_parent_class)->connect_widget_destroyed (accessible);
478 }
479
480 static gint
481 get_n_rows (GtkTreeView *tree_view)
482 {
483   GtkRBTree *tree;
484
485   tree = _gtk_tree_view_get_rbtree (tree_view);
486
487   if (tree == NULL)
488     return 0;
489
490   return tree->root->total_count;
491 }
492
493 static gint
494 gtk_tree_view_accessible_get_n_children (AtkObject *obj)
495 {
496   GtkWidget *widget;
497   GtkTreeViewAccessible *accessible;
498   GtkTreeView *tree_view;
499
500   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
501   if (widget == NULL)
502     return 0;
503
504   tree_view = GTK_TREE_VIEW (widget);
505   accessible = GTK_TREE_VIEW_ACCESSIBLE (obj);
506   return (get_n_rows (tree_view) + 1) * accessible->n_cols;
507 }
508
509 static AtkObject *
510 gtk_tree_view_accessible_ref_child (AtkObject *obj,
511                                     gint       i)
512 {
513   GtkWidget *widget;
514   GtkTreeViewAccessible *accessible;
515   GtkCellAccessible *cell;
516   GtkTreeView *tree_view;
517   GtkTreeModel *tree_model;
518   GtkCellRenderer *renderer;
519   GtkTreeIter iter;
520   GtkTreeViewColumn *tv_col;
521   GtkTreeSelection *selection;
522   GtkTreePath *path;
523   GtkRBTree *tree;
524   GtkRBNode *node;
525   AtkObject *child;
526   AtkObject *parent;
527   GtkTreeViewColumn *expander_tv;
528   GList *renderer_list;
529   GList *l;
530   GtkContainerCellAccessible *container = NULL;
531   GtkRendererCellAccessible *renderer_cell;
532   gboolean is_expander, is_expanded, retval;
533   gboolean editable = FALSE;
534   gint focus_index;
535
536   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
537   if (widget == NULL)
538     return NULL;
539
540   if (i >= gtk_tree_view_accessible_get_n_children (obj))
541     return NULL;
542
543   accessible = GTK_TREE_VIEW_ACCESSIBLE (obj);
544   tree_view = GTK_TREE_VIEW (widget);
545   if (i < accessible->n_cols)
546     {
547       tv_col = gtk_tree_view_get_column (tree_view, i);
548       child = get_header_from_column (tv_col);
549       if (child)
550         g_object_ref (child);
551       return child;
552     }
553
554   /* Check whether the child is cached */
555   cell = find_cell (accessible, i);
556   if (cell)
557     {
558       g_object_ref (cell);
559       return ATK_OBJECT (cell);
560     }
561
562   if (accessible->focus_cell == NULL)
563       focus_index = get_focus_index (tree_view);
564   else
565       focus_index = -1;
566
567   /* Find the RBTree and GtkTreeViewColumn for the index */
568   if (!get_rbtree_column_from_index (tree_view, i, &tree, &node, &tv_col))
569     return NULL;
570
571   path = _gtk_tree_view_find_path (tree_view, tree, node);
572   tree_model = gtk_tree_view_get_model (tree_view);
573   retval = gtk_tree_model_get_iter (tree_model, &iter, path);
574   if (!retval)
575     return NULL;
576
577   expander_tv = gtk_tree_view_get_expander_column (tree_view);
578   is_expander = FALSE;
579   is_expanded = FALSE;
580   if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_PARENT))
581     {
582       if (expander_tv == tv_col)
583         {
584           is_expander = TRUE;
585           is_expanded = node->children != NULL;
586         }
587     }
588   gtk_tree_view_column_cell_set_cell_data (tv_col, tree_model, &iter,
589                                            is_expander, is_expanded);
590
591   renderer_list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
592
593   /* If there are more than one renderer in the list,
594    * make a container
595    */
596   if (renderer_list && renderer_list->next)
597     {
598       GtkCellAccessible *container_cell;
599
600       container = _gtk_container_cell_accessible_new ();
601
602       container_cell = GTK_CELL_ACCESSIBLE (container);
603       _gtk_cell_accessible_initialise (container_cell, widget, ATK_OBJECT (accessible));
604
605       /* The GtkTreeViewAccessibleCellInfo structure for the container will
606        * be before the ones for the cells so that the first one we find for
607        * a position will be for the container
608        */
609       cell_info_new (accessible, tree_model, tree, node, tv_col, container_cell);
610       parent = ATK_OBJECT (container);
611     }
612   else
613     parent = ATK_OBJECT (accessible);
614
615   child = NULL;
616
617   /* Now we make a fake cell_renderer if there is no cell
618    * in renderer_list
619    */
620   if (renderer_list == NULL)
621     {
622       GtkCellRenderer *fake_renderer;
623
624       fake_renderer = g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
625       child = _gtk_text_cell_accessible_new ();
626       cell = GTK_CELL_ACCESSIBLE (child);
627       renderer_cell = GTK_RENDERER_CELL_ACCESSIBLE (child);
628       renderer_cell->renderer = fake_renderer;
629
630       /* Create the GtkTreeViewAccessibleCellInfo structure for this cell */
631       cell_info_new (accessible, tree_model, tree, node, tv_col, cell);
632
633       _gtk_cell_accessible_initialise (cell, widget, parent);
634
635       /* Set state if it is expandable */
636       if (is_expander)
637         {
638           set_cell_expandable (cell);
639           if (is_expanded)
640             _gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDED, FALSE);
641         }
642     }
643   else
644     {
645       for (l = renderer_list; l; l = l->next)
646         {
647           renderer = GTK_CELL_RENDERER (l->data);
648
649           if (GTK_IS_CELL_RENDERER_TEXT (renderer))
650             {
651               g_object_get (G_OBJECT (renderer), "editable", &editable, NULL);
652               child = _gtk_text_cell_accessible_new ();
653             }
654           else if (GTK_IS_CELL_RENDERER_TOGGLE (renderer))
655             child = _gtk_boolean_cell_accessible_new ();
656           else if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
657             child = _gtk_image_cell_accessible_new ();
658           else
659             child = _gtk_renderer_cell_accessible_new ();
660
661           cell = GTK_CELL_ACCESSIBLE (child);
662           renderer_cell = GTK_RENDERER_CELL_ACCESSIBLE (child);
663
664           /* Create the GtkTreeViewAccessibleCellInfo for this cell */
665           cell_info_new (accessible, tree_model, tree, node, tv_col, cell);
666
667           _gtk_cell_accessible_initialise (cell, widget, parent);
668
669           if (container)
670             _gtk_container_cell_accessible_add_child (container, cell);
671
672           update_cell_value (renderer_cell, accessible, FALSE);
673
674           /* Add the actions appropriate for this cell */
675           add_cell_actions (cell, editable);
676
677           /* Set state if it is expandable */
678           if (is_expander)
679             {
680               set_cell_expandable (cell);
681               if (is_expanded)
682                 _gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDED, FALSE);
683             }
684
685           /* If the column is visible, sets the cell's state */
686           if (gtk_tree_view_column_get_visible (tv_col))
687             set_cell_visibility (tree_view, cell, tv_col, path, FALSE);
688
689           /* If the row is selected, all cells on the row are selected */
690           selection = gtk_tree_view_get_selection (tree_view);
691
692           if (gtk_tree_selection_path_is_selected (selection, path))
693             _gtk_cell_accessible_add_state (cell, ATK_STATE_SELECTED, FALSE);
694
695           _gtk_cell_accessible_add_state (cell, ATK_STATE_FOCUSABLE, FALSE);
696           if (focus_index == i)
697             {
698               accessible->focus_cell = g_object_ref (cell);
699               _gtk_cell_accessible_add_state (cell, ATK_STATE_FOCUSED, FALSE);
700               g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
701             }
702         }
703       g_list_free (renderer_list);
704       if (container)
705         child = ATK_OBJECT (container);
706     }
707
708   if (expander_tv == tv_col)
709     {
710       AtkRelationSet *relation_set;
711       AtkObject *accessible_array[1];
712       AtkRelation* relation;
713       AtkObject *parent_node;
714
715       relation_set = atk_object_ref_relation_set (ATK_OBJECT (child));
716
717       gtk_tree_path_up (path);
718       if (gtk_tree_path_get_depth (path) == 0)
719         parent_node = obj;
720       else
721         {
722           gint parent_index;
723
724           parent_index = get_index (tree_view, path, i % accessible->n_cols);
725           parent_node = atk_object_ref_accessible_child (obj, parent_index);
726         }
727       accessible_array[0] = parent_node;
728       relation = atk_relation_new (accessible_array, 1,
729                                    ATK_RELATION_NODE_CHILD_OF);
730       atk_relation_set_add (relation_set, relation);
731       atk_object_add_relationship (parent_node, ATK_RELATION_NODE_PARENT_OF, child);
732       g_object_unref (relation);
733       g_object_unref (relation_set);
734     }
735   gtk_tree_path_free (path);
736
737   /* We do not increase the reference count here; when g_object_unref()
738    * is called for the cell then cell_destroyed() is called and this
739    * removes the cell from the cache.
740    */
741   return child;
742 }
743
744 static AtkStateSet*
745 gtk_tree_view_accessible_ref_state_set (AtkObject *obj)
746 {
747   AtkStateSet *state_set;
748   GtkWidget *widget;
749
750   state_set = ATK_OBJECT_CLASS (_gtk_tree_view_accessible_parent_class)->ref_state_set (obj);
751   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
752
753   if (widget != NULL)
754     atk_state_set_add_state (state_set, ATK_STATE_MANAGES_DESCENDANTS);
755
756   return state_set;
757 }
758
759 static void
760 _gtk_tree_view_accessible_class_init (GtkTreeViewAccessibleClass *klass)
761 {
762   AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
763   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
764   GtkAccessibleClass *accessible_class = (GtkAccessibleClass*)klass;
765   GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
766   GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
767
768   class->get_n_children = gtk_tree_view_accessible_get_n_children;
769   class->ref_child = gtk_tree_view_accessible_ref_child;
770   class->ref_state_set = gtk_tree_view_accessible_ref_state_set;
771   class->initialize = gtk_tree_view_accessible_initialize;
772
773   widget_class->notify_gtk = gtk_tree_view_accessible_notify_gtk;
774
775   accessible_class->connect_widget_destroyed = gtk_tree_view_accessible_connect_widget_destroyed;
776
777   /* The children of a GtkTreeView are the buttons at the top of the columns
778    * we do not represent these as children so we do not want to report
779    * children added or deleted when these changed.
780    */
781   container_class->add_gtk = NULL;
782   container_class->remove_gtk = NULL;
783
784   gobject_class->finalize = gtk_tree_view_accessible_finalize;
785 }
786
787 static void
788 _gtk_tree_view_accessible_init (GtkTreeViewAccessible *view)
789 {
790 }
791
792 gint
793 get_focus_index (GtkTreeView *tree_view)
794 {
795   GtkTreePath *focus_path;
796   GtkTreeViewColumn *focus_column;
797   gint index;
798
799   gtk_tree_view_get_cursor (tree_view, &focus_path, &focus_column);
800   if (focus_path && focus_column)
801     index = get_index (tree_view, focus_path,
802                        get_column_number (tree_view, focus_column, FALSE));
803   else
804     index = -1;
805
806   if (focus_path)
807     gtk_tree_path_free (focus_path);
808
809   return index;
810 }
811
812 /* This function returns a reference to the accessible object
813  * for the cell in the treeview which has focus, if any
814  */
815 static AtkObject *
816 gtk_tree_view_accessible_ref_focus_cell (GtkTreeView *tree_view)
817 {
818   AtkObject *focus_cell = NULL;
819   AtkObject *atk_obj;
820   gint focus_index;
821
822   focus_index = get_focus_index (tree_view);
823   if (focus_index >= 0)
824     {
825       atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
826       focus_cell = atk_object_ref_accessible_child (atk_obj, focus_index);
827     }
828
829   return focus_cell;
830 }
831
832 /* atkcomponent.h */
833
834 static AtkObject *
835 gtk_tree_view_accessible_ref_accessible_at_point (AtkComponent *component,
836                                                   gint          x,
837                                                   gint          y,
838                                                   AtkCoordType  coord_type)
839 {
840   GtkWidget *widget;
841   GtkTreeView *tree_view;
842   GtkTreePath *path;
843   GtkTreeViewColumn *tv_column;
844   gint x_pos, y_pos;
845   gint bx, by;
846   gboolean ret_val;
847
848   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (component));
849   if (widget == NULL)
850     return NULL;
851
852   tree_view = GTK_TREE_VIEW (widget);
853
854   atk_component_get_extents (component, &x_pos, &y_pos, NULL, NULL, coord_type);
855   gtk_tree_view_convert_widget_to_bin_window_coords (tree_view, x, y, &bx, &by);
856   ret_val = gtk_tree_view_get_path_at_pos (tree_view,
857                                            bx - x_pos, by - y_pos,
858                                            &path, &tv_column, NULL, NULL);
859   if (ret_val)
860     {
861       gint index, column;
862
863       column = get_column_number (tree_view, tv_column, FALSE);
864       index = get_index (tree_view, path, column);
865       gtk_tree_path_free (path);
866
867       return gtk_tree_view_accessible_ref_child (ATK_OBJECT (component), index);
868     }
869
870   return NULL;
871 }
872
873 static void
874 atk_component_interface_init (AtkComponentIface *iface)
875 {
876   iface->ref_accessible_at_point = gtk_tree_view_accessible_ref_accessible_at_point;
877 }
878
879 /* atktable.h */
880
881 static gint
882 gtk_tree_view_accessible_get_index_at (AtkTable *table,
883                                        gint      row,
884                                        gint      column)
885 {
886   GtkWidget *widget;
887   GtkTreeView *tree_view;
888   gint actual_column;
889   gint n_cols, n_rows;
890   GtkTreeIter iter;
891   GtkTreePath *path;
892   gint index;
893
894   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
895   if (widget == NULL)
896     return -1;
897
898   n_cols = atk_table_get_n_columns (table);
899   n_rows = atk_table_get_n_rows (table);
900
901   if (row >= n_rows || column >= n_cols)
902     return -1;
903
904   tree_view = GTK_TREE_VIEW (widget);
905   actual_column = get_actual_column_number (tree_view, column);
906
907   set_iter_nth_row (tree_view, &iter, row);
908   path = gtk_tree_model_get_path (gtk_tree_view_get_model (tree_view), &iter);
909
910   index = get_index (tree_view, path, actual_column);
911   gtk_tree_path_free (path);
912
913   return index;
914 }
915
916 static gint
917 gtk_tree_view_accessible_get_column_at_index (AtkTable *table,
918                                               gint      index)
919 {
920   GtkWidget *widget;
921   GtkTreeView *tree_view;
922   gint n_columns;
923
924   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
925   if (widget == NULL)
926     return -1;
927
928   tree_view = GTK_TREE_VIEW (widget);
929   n_columns = GTK_TREE_VIEW_ACCESSIBLE (table)->n_cols;
930
931   if (n_columns == 0)
932     return 0;
933
934   index = index % n_columns;
935
936   return get_visible_column_number (tree_view, index);
937 }
938
939 static gint
940 gtk_tree_view_accessible_get_row_at_index (AtkTable *table,
941                                            gint      index)
942 {
943   GtkWidget *widget;
944   GtkTreeView *tree_view;
945
946   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
947   if (widget == NULL)
948     return -1;
949
950   tree_view = GTK_TREE_VIEW (widget);
951
952   index /= GTK_TREE_VIEW_ACCESSIBLE (table)->n_cols;
953   index--;
954   if (index >= get_n_rows (tree_view))
955     return -1;
956
957   return index;
958 }
959
960 static AtkObject *
961 gtk_tree_view_accessible_table_ref_at (AtkTable *table,
962                                        gint      row,
963                                        gint      column)
964 {
965   gint index;
966
967   index = gtk_tree_view_accessible_get_index_at (table, row, column);
968   if (index == -1)
969     return NULL;
970
971   return gtk_tree_view_accessible_ref_child (ATK_OBJECT (table), index);
972 }
973
974 static gint
975 gtk_tree_view_accessible_get_n_rows (AtkTable *table)
976 {
977   GtkWidget *widget;
978
979   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
980   if (widget == NULL)
981     return 0;
982
983   return get_n_rows (GTK_TREE_VIEW (widget));
984 }
985
986 static gint
987 gtk_tree_view_accessible_get_n_columns (AtkTable *table)
988 {
989   GtkWidget *widget;
990   GtkTreeView *tree_view;
991   GtkTreeViewColumn *tv_col;
992   gint n_cols = 0;
993   gint i = 0;
994
995   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
996   if (widget == NULL)
997     return 0;
998
999   tree_view = GTK_TREE_VIEW (widget);
1000   tv_col = gtk_tree_view_get_column (tree_view, i);
1001
1002   while (tv_col != NULL)
1003     {
1004       if (gtk_tree_view_column_get_visible (tv_col))
1005         n_cols++;
1006
1007       i++;
1008       tv_col = gtk_tree_view_get_column (tree_view, i);
1009     }
1010
1011   return n_cols;
1012 }
1013
1014 static gboolean
1015 gtk_tree_view_accessible_is_row_selected (AtkTable *table,
1016                                           gint      row)
1017 {
1018   GtkWidget *widget;
1019   GtkTreeView *tree_view;
1020   GtkTreeSelection *selection;
1021   GtkTreeIter iter;
1022
1023   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1024   if (widget == NULL)
1025     return FALSE;
1026
1027   if (row < 0)
1028     return FALSE;
1029
1030   tree_view = GTK_TREE_VIEW (widget);
1031   selection = gtk_tree_view_get_selection (tree_view);
1032
1033   set_iter_nth_row (tree_view, &iter, row);
1034   return gtk_tree_selection_iter_is_selected (selection, &iter);
1035 }
1036
1037 static gboolean
1038 gtk_tree_view_accessible_is_selected (AtkTable *table,
1039                                       gint      row,
1040                                       gint      column)
1041 {
1042   return gtk_tree_view_accessible_is_row_selected (table, row);
1043 }
1044
1045 static void
1046 get_selected_rows (GtkTreeModel *model,
1047                    GtkTreePath  *path,
1048                    GtkTreeIter  *iter,
1049                    gpointer      data)
1050 {
1051   GPtrArray *array = (GPtrArray *)data;
1052
1053   g_ptr_array_add (array, gtk_tree_path_copy (path));
1054 }
1055
1056 static gint
1057 gtk_tree_view_accessible_get_selected_rows (AtkTable  *table,
1058                                             gint     **rows_selected)
1059 {
1060   GtkWidget *widget;
1061   GtkTreeView *tree_view;
1062   GtkTreeModel *tree_model;
1063   GtkTreeIter iter;
1064   GtkTreeSelection *selection;
1065   GtkTreePath *tree_path;
1066   gint ret_val = 0;
1067
1068   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1069   if (widget == NULL)
1070     return 0;
1071
1072   tree_view = GTK_TREE_VIEW (widget);
1073   selection = gtk_tree_view_get_selection (tree_view);
1074
1075   switch (gtk_tree_selection_get_mode (selection))
1076     {
1077     case GTK_SELECTION_SINGLE:
1078     case GTK_SELECTION_BROWSE:
1079       if (gtk_tree_selection_get_selected (selection, &tree_model, &iter))
1080         {
1081           gint row;
1082
1083           if (rows_selected)
1084             {
1085               *rows_selected = g_new (gint, 1);
1086               tree_path = gtk_tree_model_get_path (tree_model, &iter);
1087               row = get_row_from_tree_path (tree_view, tree_path);
1088               gtk_tree_path_free (tree_path);
1089
1090               /* shouldn't ever happen */
1091               g_return_val_if_fail (row != -1, 0);
1092
1093               *rows_selected[0] = row;
1094             }
1095           ret_val = 1;
1096         }
1097       break;
1098     case GTK_SELECTION_MULTIPLE:
1099       {
1100         GPtrArray *array = g_ptr_array_new();
1101
1102         gtk_tree_selection_selected_foreach (selection, get_selected_rows, array);
1103         ret_val = array->len;
1104
1105         if (rows_selected && ret_val)
1106           {
1107             gint i;
1108
1109             *rows_selected = g_new (gint, ret_val);
1110             for (i = 0; i < ret_val; i++)
1111               {
1112                 gint row;
1113
1114                 tree_path = (GtkTreePath *) g_ptr_array_index (array, i);
1115                 row = get_row_from_tree_path (tree_view, tree_path);
1116                 gtk_tree_path_free (tree_path);
1117                 (*rows_selected)[i] = row;
1118               }
1119           }
1120         g_ptr_array_free (array, FALSE);
1121       }
1122       break;
1123     case GTK_SELECTION_NONE:
1124       break;
1125     }
1126   return ret_val;
1127 }
1128
1129 static gboolean
1130 gtk_tree_view_accessible_add_row_selection (AtkTable *table,
1131                                             gint      row)
1132 {
1133   GtkWidget *widget;
1134   GtkTreeView *tree_view;
1135   GtkTreeModel *tree_model;
1136   GtkTreeSelection *selection;
1137   GtkTreePath *tree_path;
1138   GtkTreeIter iter_to_row;
1139
1140   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1141   if (widget == NULL)
1142     return FALSE;
1143
1144   if (!gtk_tree_view_accessible_is_row_selected (table, row))
1145     {
1146       tree_view = GTK_TREE_VIEW (widget);
1147       tree_model = gtk_tree_view_get_model (tree_view);
1148       selection = gtk_tree_view_get_selection (tree_view);
1149
1150       if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
1151         {
1152           tree_path = gtk_tree_path_new ();
1153           gtk_tree_path_append_index (tree_path, row);
1154           gtk_tree_selection_select_path (selection,tree_path);
1155           gtk_tree_path_free (tree_path);
1156         }
1157       else
1158         {
1159           set_iter_nth_row (tree_view, &iter_to_row, row);
1160           gtk_tree_selection_select_iter (selection, &iter_to_row);
1161         }
1162     }
1163
1164   return gtk_tree_view_accessible_is_row_selected (table, row);
1165 }
1166
1167 static gboolean
1168 gtk_tree_view_accessible_remove_row_selection (AtkTable *table,
1169                                                gint      row)
1170 {
1171   GtkWidget *widget;
1172   GtkTreeView *tree_view;
1173   GtkTreeSelection *selection;
1174
1175   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1176   if (widget == NULL)
1177     return FALSE;
1178
1179   tree_view = GTK_TREE_VIEW (widget);
1180   selection = gtk_tree_view_get_selection (tree_view);
1181
1182   if (gtk_tree_view_accessible_is_row_selected (table, row))
1183     {
1184       gtk_tree_selection_unselect_all (selection);
1185       return TRUE;
1186     }
1187
1188   return FALSE;
1189 }
1190
1191 static AtkObject *
1192 gtk_tree_view_accessible_get_column_header (AtkTable *table,
1193                                             gint      in_col)
1194 {
1195   GtkWidget *widget;
1196   GtkTreeView *tree_view;
1197   GtkTreeViewColumn *tv_col;
1198
1199   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1200   if (widget == NULL)
1201     return NULL;
1202
1203   tree_view = GTK_TREE_VIEW (widget);
1204   tv_col = get_column (tree_view, in_col);
1205   return get_header_from_column (tv_col);
1206 }
1207
1208 static const gchar *
1209 gtk_tree_view_accessible_get_column_description (AtkTable *table,
1210                                                  gint      in_col)
1211 {
1212   GtkWidget *widget;
1213   GtkTreeView *tree_view;
1214   GtkTreeViewColumn *tv_col;
1215
1216   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1217   if (widget == NULL)
1218     return NULL;
1219
1220   tree_view = GTK_TREE_VIEW (widget);
1221   tv_col = get_column (tree_view, in_col);
1222   if (tv_col == NULL)
1223      return NULL;
1224
1225   return gtk_tree_view_column_get_title (tv_col);
1226 }
1227
1228 static void
1229 atk_table_interface_init (AtkTableIface *iface)
1230 {
1231   iface->ref_at = gtk_tree_view_accessible_table_ref_at;
1232   iface->get_n_rows = gtk_tree_view_accessible_get_n_rows;
1233   iface->get_n_columns = gtk_tree_view_accessible_get_n_columns;
1234   iface->get_index_at = gtk_tree_view_accessible_get_index_at;
1235   iface->get_column_at_index = gtk_tree_view_accessible_get_column_at_index;
1236   iface->get_row_at_index = gtk_tree_view_accessible_get_row_at_index;
1237   iface->is_row_selected = gtk_tree_view_accessible_is_row_selected;
1238   iface->is_selected = gtk_tree_view_accessible_is_selected;
1239   iface->get_selected_rows = gtk_tree_view_accessible_get_selected_rows;
1240   iface->add_row_selection = gtk_tree_view_accessible_add_row_selection;
1241   iface->remove_row_selection = gtk_tree_view_accessible_remove_row_selection;
1242   iface->get_column_extent_at = NULL;
1243   iface->get_row_extent_at = NULL;
1244   iface->get_column_header = gtk_tree_view_accessible_get_column_header;
1245   iface->get_column_description = gtk_tree_view_accessible_get_column_description;
1246 }
1247
1248 /* atkselection.h */
1249
1250 static gboolean
1251 gtk_tree_view_accessible_add_selection (AtkSelection *selection,
1252                                         gint          i)
1253 {
1254   AtkTable *table;
1255   gint n_columns;
1256   gint row;
1257
1258   table = ATK_TABLE (selection);
1259   n_columns = gtk_tree_view_accessible_get_n_columns (table);
1260   if (n_columns != 1)
1261     return FALSE;
1262
1263   row = gtk_tree_view_accessible_get_row_at_index (table, i);
1264   return gtk_tree_view_accessible_add_row_selection (table, row);
1265 }
1266
1267 static gboolean
1268 gtk_tree_view_accessible_clear_selection (AtkSelection *selection)
1269 {
1270   GtkWidget *widget;
1271   GtkTreeView *tree_view;
1272   GtkTreeSelection *tree_selection;
1273
1274   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
1275   if (widget == NULL)
1276     return FALSE;
1277
1278   tree_view = GTK_TREE_VIEW (widget);
1279   tree_selection = gtk_tree_view_get_selection (tree_view);
1280
1281   gtk_tree_selection_unselect_all (tree_selection);
1282   return TRUE;
1283 }
1284
1285 static AtkObject *
1286 gtk_tree_view_accessible_ref_selection (AtkSelection *selection,
1287                                         gint          i)
1288 {
1289   AtkTable *table;
1290   gint row;
1291   gint n_selected;
1292   gint n_columns;
1293   gint *selected;
1294
1295   table = ATK_TABLE (selection);
1296   n_columns = gtk_tree_view_accessible_get_n_columns (table);
1297   n_selected = gtk_tree_view_accessible_get_selected_rows (table, &selected);
1298   if (i >= n_columns * n_selected)
1299     return NULL;
1300
1301   row = selected[i / n_columns];
1302   g_free (selected);
1303
1304   return gtk_tree_view_accessible_table_ref_at (table, row, i % n_columns);
1305 }
1306
1307 static gint
1308 gtk_tree_view_accessible_get_selection_count (AtkSelection *selection)
1309 {
1310   AtkTable *table;
1311   gint n_selected;
1312
1313   table = ATK_TABLE (selection);
1314   n_selected = gtk_tree_view_accessible_get_selected_rows (table, NULL);
1315   if (n_selected > 0)
1316     n_selected *= gtk_tree_view_accessible_get_n_columns (table);
1317   return n_selected;
1318 }
1319
1320 static gboolean
1321 gtk_tree_view_accessible_is_child_selected (AtkSelection *selection,
1322                                             gint          i)
1323 {
1324   GtkWidget *widget;
1325   gint row;
1326
1327   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
1328   if (widget == NULL)
1329     return FALSE;
1330
1331   row = atk_table_get_row_at_index (ATK_TABLE (selection), i);
1332
1333   return gtk_tree_view_accessible_is_row_selected (ATK_TABLE (selection), row);
1334 }
1335
1336 static void atk_selection_interface_init (AtkSelectionIface *iface)
1337 {
1338   iface->add_selection = gtk_tree_view_accessible_add_selection;
1339   iface->clear_selection = gtk_tree_view_accessible_clear_selection;
1340   iface->ref_selection = gtk_tree_view_accessible_ref_selection;
1341   iface->get_selection_count = gtk_tree_view_accessible_get_selection_count;
1342   iface->is_child_selected = gtk_tree_view_accessible_is_child_selected;
1343 }
1344
1345 #define EXTRA_EXPANDER_PADDING 4
1346
1347 static void
1348 gtk_tree_view_accessible_get_cell_area (GtkCellAccessibleParent *parent,
1349                                         GtkCellAccessible       *cell,
1350                                         GdkRectangle            *cell_rect)
1351 {
1352   GtkWidget *widget;
1353   GtkTreeView *tree_view;
1354   GtkTreeViewColumn *tv_col;
1355   GtkTreePath *path;
1356   AtkObject *parent_cell;
1357   GtkTreeViewAccessibleCellInfo *cell_info;
1358   GtkCellAccessible *top_cell;
1359
1360   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1361   if (widget == NULL)
1362     return;
1363
1364   tree_view = GTK_TREE_VIEW (widget);
1365   parent_cell = atk_object_get_parent (ATK_OBJECT (cell));
1366   if (parent_cell != ATK_OBJECT (parent))
1367     top_cell = GTK_CELL_ACCESSIBLE (parent_cell);
1368   else
1369     top_cell = cell;
1370   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), top_cell, TRUE);
1371   if (!cell_info)
1372     return;
1373   path = cell_info_get_path (cell_info);
1374   tv_col = cell_info->cell_col_ref;
1375   if (path)
1376     {
1377       GtkTreeViewColumn *expander_column;
1378       gint focus_line_width;
1379
1380       gtk_tree_view_get_cell_area (tree_view, path, tv_col, cell_rect);
1381       expander_column = gtk_tree_view_get_expander_column (tree_view);
1382       if (expander_column == tv_col)
1383         {
1384           gint expander_size;
1385           gtk_widget_style_get (widget,
1386                                 "expander-size", &expander_size,
1387                                 NULL);
1388           cell_rect->x += expander_size + EXTRA_EXPANDER_PADDING;
1389           cell_rect->width -= expander_size + EXTRA_EXPANDER_PADDING;
1390         }
1391       gtk_widget_style_get (widget,
1392                             "focus-line-width", &focus_line_width,
1393                             NULL);
1394
1395       cell_rect->x += focus_line_width;
1396       cell_rect->width -= 2 * focus_line_width;
1397
1398       gtk_tree_path_free (path);
1399
1400       /* A column has more than one renderer so we find the position
1401        * and width of each
1402        */
1403       if (top_cell != cell)
1404         {
1405           gint cell_index;
1406           gboolean found;
1407           gint cell_start;
1408           gint cell_width;
1409           GList *renderers;
1410           GtkCellRenderer *renderer;
1411
1412           cell_index = atk_object_get_index_in_parent (ATK_OBJECT (cell));
1413           renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
1414           renderer = g_list_nth_data (renderers, cell_index);
1415
1416           found = gtk_tree_view_column_cell_get_position (tv_col, renderer, &cell_start, &cell_width);
1417           if (found)
1418             {
1419               cell_rect->x += cell_start;
1420               cell_rect->width = cell_width;
1421             }
1422           g_list_free (renderers);
1423         }
1424
1425     }
1426 }
1427
1428 static void
1429 gtk_tree_view_accessible_get_cell_extents (GtkCellAccessibleParent *parent,
1430                                            GtkCellAccessible       *cell,
1431                                            gint                    *x,
1432                                            gint                    *y,
1433                                            gint                    *width,
1434                                            gint                    *height,
1435                                            AtkCoordType             coord_type)
1436 {
1437   GtkWidget *widget;
1438   GtkTreeView *tree_view;
1439   GdkWindow *bin_window;
1440   GdkRectangle cell_rect;
1441   gint w_x, w_y;
1442
1443   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1444   if (widget == NULL)
1445     return;
1446
1447   tree_view = GTK_TREE_VIEW (widget);
1448   gtk_tree_view_accessible_get_cell_area (parent, cell, &cell_rect);
1449   bin_window = gtk_tree_view_get_bin_window (tree_view);
1450   gdk_window_get_origin (bin_window, &w_x, &w_y);
1451
1452   if (coord_type == ATK_XY_WINDOW)
1453     {
1454       GdkWindow *window;
1455       gint x_toplevel, y_toplevel;
1456
1457       window = gdk_window_get_toplevel (bin_window);
1458       gdk_window_get_origin (window, &x_toplevel, &y_toplevel);
1459
1460       w_x -= x_toplevel;
1461       w_y -= y_toplevel;
1462     }
1463
1464   *width = cell_rect.width;
1465   *height = cell_rect.height;
1466   if (is_cell_showing (tree_view, &cell_rect))
1467     {
1468       *x = cell_rect.x + w_x;
1469       *y = cell_rect.y + w_y;
1470     }
1471   else
1472     {
1473       *x = G_MININT;
1474       *y = G_MININT;
1475     }
1476 }
1477
1478 static gboolean
1479 gtk_tree_view_accessible_grab_cell_focus (GtkCellAccessibleParent *parent,
1480                                           GtkCellAccessible       *cell)
1481 {
1482   GtkWidget *widget;
1483   GtkTreeView *tree_view;
1484   GtkTreeViewColumn *tv_col;
1485   GtkTreePath *path;
1486   AtkObject *parent_cell;
1487   AtkObject *cell_object;
1488   GtkTreeViewAccessibleCellInfo *cell_info;
1489   GtkCellRenderer *renderer = NULL;
1490   GtkWidget *toplevel;
1491   gint index;
1492
1493   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1494   if (widget == NULL)
1495     return FALSE;
1496
1497   tree_view = GTK_TREE_VIEW (widget);
1498
1499   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell, TRUE);
1500   if (!cell_info)
1501     return FALSE;
1502   cell_object = ATK_OBJECT (cell);
1503   parent_cell = atk_object_get_parent (cell_object);
1504   tv_col = cell_info->cell_col_ref;
1505   if (parent_cell != ATK_OBJECT (parent))
1506     {
1507       /* GtkCellAccessible is in a GtkContainerCellAccessible.
1508        * The GtkTreeViewColumn has multiple renderers;
1509        * find the corresponding one.
1510        */
1511       GList *renderers;
1512
1513       renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
1514       index = atk_object_get_index_in_parent (cell_object);
1515       renderer = g_list_nth_data (renderers, index);
1516       g_list_free (renderers);
1517     }
1518   path = cell_info_get_path (cell_info);
1519   if (path)
1520     {
1521       if (renderer)
1522         gtk_tree_view_set_cursor_on_cell (tree_view, path, tv_col, renderer, FALSE);
1523       else
1524         gtk_tree_view_set_cursor (tree_view, path, tv_col, FALSE);
1525
1526       gtk_tree_path_free (path);
1527       gtk_widget_grab_focus (widget);
1528       toplevel = gtk_widget_get_toplevel (widget);
1529       if (gtk_widget_is_toplevel (toplevel))
1530         {
1531 #ifdef GDK_WINDOWING_X11
1532           gtk_window_present_with_time (GTK_WINDOW (toplevel),
1533                                         gdk_x11_get_server_time (gtk_widget_get_window (widget)));
1534 #else
1535           gtk_window_present (GTK_WINDOW (toplevel));
1536 #endif
1537         }
1538
1539       return TRUE;
1540     }
1541   else
1542       return FALSE;
1543 }
1544
1545 static int
1546 gtk_tree_view_accessible_get_child_index (GtkCellAccessibleParent *parent,
1547                                           GtkCellAccessible       *cell)
1548 {
1549   GtkTreeViewAccessibleCellInfo *cell_info;
1550   GtkTreeView *tree_view;
1551
1552   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell, TRUE);
1553   if (!cell_info)
1554     return -1;
1555
1556   tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
1557
1558   return cell_info_get_index (tree_view, cell_info);
1559 }
1560
1561 static void
1562 gtk_cell_accessible_parent_interface_init (GtkCellAccessibleParentIface *iface)
1563 {
1564   iface->get_cell_extents = gtk_tree_view_accessible_get_cell_extents;
1565   iface->get_cell_area = gtk_tree_view_accessible_get_cell_area;
1566   iface->grab_focus = gtk_tree_view_accessible_grab_cell_focus;
1567   iface->get_child_index = gtk_tree_view_accessible_get_child_index;
1568 }
1569
1570 /* signal handling */
1571
1572 static gboolean
1573 idle_expand_row (gpointer data)
1574 {
1575   GtkTreeViewAccessible *accessible = data;
1576   GtkTreePath *path;
1577   GtkTreeView *tree_view;
1578   GtkTreeIter iter;
1579   GtkTreeModel *tree_model;
1580   gint n_inserted, row;
1581
1582   accessible->idle_expand_id = 0;
1583
1584   path = accessible->idle_expand_path;
1585   tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
1586
1587   tree_model = gtk_tree_view_get_model (tree_view);
1588   if (!tree_model)
1589     return FALSE;
1590
1591   if (!path || !gtk_tree_model_get_iter (tree_model, &iter, path))
1592     return FALSE;
1593
1594   /* Update visibility of cells below expansion row */
1595   traverse_cells (accessible, path, FALSE);
1596
1597   /* Figure out number of visible children, the following test
1598    * should not fail
1599    */
1600   if (gtk_tree_model_iter_has_child (tree_model, &iter))
1601     {
1602       GtkTreePath *path_copy;
1603
1604       /* By passing path into this function, we find the number of
1605        * visible children of path.
1606        */
1607       path_copy = gtk_tree_path_copy (path);
1608       gtk_tree_path_append_index (path_copy, 0);
1609
1610       n_inserted = 0;
1611       iterate_thru_children (tree_view, tree_model,
1612                              path_copy, NULL, &n_inserted, 0);
1613       gtk_tree_path_free (path_copy);
1614     }
1615   else
1616     {
1617       /* We can get here if the row expanded callback deleted the row */
1618       return FALSE;
1619     }
1620
1621   /* Set expand state */
1622   set_expand_state (tree_view, tree_model, accessible, path, TRUE);
1623
1624   row = get_row_from_tree_path (tree_view, path);
1625
1626   /* shouldn't ever happen */
1627   if (row == -1)
1628     g_assert_not_reached ();
1629
1630   /* Must add 1 because the "added rows" are below the row being expanded */
1631   row += 1;
1632
1633   g_signal_emit_by_name (accessible, "row-inserted", row, n_inserted);
1634
1635   accessible->idle_expand_path = NULL;
1636
1637   gtk_tree_path_free (path);
1638
1639   return FALSE;
1640 }
1641
1642 static gboolean
1643 row_expanded_cb (GtkTreeView *tree_view,
1644                  GtkTreeIter *iter,
1645                  GtkTreePath *path)
1646 {
1647   AtkObject *atk_obj;
1648   GtkTreeViewAccessible *accessible;
1649
1650   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1651   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1652
1653   /*
1654    * The visible rectangle has not been updated when this signal is emitted
1655    * so we process the signal when the GTK processing is completed
1656    */
1657   /* this seems wrong since it overwrites any other pending expand handlers... */
1658   accessible->idle_expand_path = gtk_tree_path_copy (path);
1659   if (accessible->idle_expand_id)
1660     g_source_remove (accessible->idle_expand_id);
1661   accessible->idle_expand_id = gdk_threads_add_idle (idle_expand_row, accessible);
1662
1663   return FALSE;
1664 }
1665
1666 static gboolean
1667 row_collapsed_cb (GtkTreeView *tree_view,
1668                   GtkTreeIter *iter,
1669                   GtkTreePath *path)
1670 {
1671   GtkTreeModel *tree_model;
1672   AtkObject *atk_obj;
1673   GtkTreeViewAccessible *accessible;
1674   gint row;
1675
1676   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1677   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1678   tree_model = gtk_tree_view_get_model (tree_view);
1679
1680   clean_rows (accessible);
1681
1682   /* Update visibility of cells below collapsed row */
1683   traverse_cells (accessible, path, FALSE);
1684
1685   /* Set collapse state */
1686   set_expand_state (tree_view, tree_model, accessible, path, FALSE);
1687   if (accessible->n_children_deleted == 0)
1688     return FALSE;
1689   row = get_row_from_tree_path (tree_view, path);
1690   if (row == -1)
1691     return FALSE;
1692   g_signal_emit_by_name (atk_obj, "row-deleted", row,
1693                          accessible->n_children_deleted);
1694   accessible->n_children_deleted = 0;
1695   return FALSE;
1696 }
1697
1698 static void
1699 size_allocate_cb (GtkWidget     *widget,
1700                   GtkAllocation *allocation)
1701 {
1702   AtkObject *atk_obj;
1703   GtkTreeViewAccessible *accessible;
1704
1705   atk_obj = gtk_widget_get_accessible (widget);
1706   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1707
1708   /* If the size allocation changes, the visibility of cells
1709    * may change so update the cells visibility.
1710    */
1711   traverse_cells (accessible, NULL, FALSE);
1712 }
1713
1714 static void
1715 selection_changed_cb (GtkTreeSelection *selection,
1716                       gpointer          data)
1717 {
1718   GtkTreeViewAccessible *accessible;
1719   GtkTreeView *tree_view;
1720   GtkWidget *widget;
1721   GtkTreeViewAccessibleCellInfo *info;
1722   GtkTreeSelection *tree_selection;
1723   GtkTreePath *path;
1724   GHashTableIter iter;
1725
1726   accessible = GTK_TREE_VIEW_ACCESSIBLE (data);
1727   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
1728   if (widget == NULL)
1729     return;
1730
1731   tree_view = GTK_TREE_VIEW (widget);
1732   tree_selection = gtk_tree_view_get_selection (tree_view);
1733
1734   clean_rows (accessible);
1735
1736   /* FIXME: clean rows iterates through all cells too */
1737   g_hash_table_iter_init (&iter, accessible->cell_infos);
1738   while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&info))
1739     {
1740       _gtk_cell_accessible_remove_state (info->cell, ATK_STATE_SELECTED, TRUE);
1741
1742       path = cell_info_get_path (info);
1743       if (path && gtk_tree_selection_path_is_selected (tree_selection, path))
1744         _gtk_cell_accessible_add_state (info->cell, ATK_STATE_SELECTED, TRUE);
1745       gtk_tree_path_free (path);
1746     }
1747   if (gtk_widget_get_realized (widget))
1748     g_signal_emit_by_name (accessible, "selection-changed");
1749 }
1750
1751 static void
1752 columns_changed (GtkTreeView *tree_view)
1753 {
1754   AtkObject *atk_obj;
1755   GtkTreeViewAccessible *accessible;
1756   GList *tv_cols, *tmp_list;
1757   gboolean column_found;
1758   gboolean move_found = FALSE;
1759   gint column_count = 0;
1760   gint i;
1761
1762   atk_obj = gtk_widget_get_accessible (GTK_WIDGET(tree_view));
1763   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1764
1765   /* This function must determine if the change is an add, delete
1766    * or a move based upon its cache of TreeViewColumns in
1767    * accessible->col_data
1768    */
1769   tv_cols = gtk_tree_view_get_columns (tree_view);
1770   accessible->n_cols = g_list_length (tv_cols);
1771
1772   /* check for adds or moves */
1773   for (tmp_list = tv_cols; tmp_list; tmp_list = tmp_list->next)
1774     {
1775       column_found = FALSE;
1776
1777       for (i = 0; i < accessible->col_data->len; i++)
1778         {
1779
1780           if ((GtkTreeViewColumn *)tmp_list->data ==
1781               (GtkTreeViewColumn *)g_array_index (accessible->col_data,
1782                GtkTreeViewColumn *, i))
1783             {
1784               column_found = TRUE;
1785
1786               /* If the column isn't in the same position, a move happened */
1787               if (!move_found && i != column_count)
1788                 {
1789                   /* Just emit one column reordered signal when a move happens */
1790                   g_signal_emit_by_name (atk_obj, "column-reordered");
1791                   move_found = TRUE;
1792                 }
1793
1794               break;
1795             }
1796         }
1797
1798      /* If column_found is FALSE, then an insert happened for column
1799       * number column_count
1800       */
1801       if (!column_found)
1802         {
1803           gint row;
1804
1805           /* Generate column-inserted signal */
1806           g_signal_emit_by_name (atk_obj, "column-inserted", column_count, 1);
1807
1808           /* Generate children-changed signals */
1809           for (row = 0; row < get_n_rows (tree_view); row++)
1810             {
1811              /* Pass NULL as the child object, i.e. 4th argument */
1812               g_signal_emit_by_name (atk_obj, "children-changed::add",
1813                                     ((row * accessible->n_cols) + column_count), NULL, NULL);
1814             }
1815         }
1816
1817       column_count++;
1818     }
1819
1820   /* check for deletes */
1821   for (i = 0; i < accessible->col_data->len; i++)
1822     {
1823       column_found = FALSE;
1824
1825       for (tmp_list = tv_cols; tmp_list; tmp_list = tmp_list->next)
1826         {
1827             if ((GtkTreeViewColumn *)tmp_list->data ==
1828                 (GtkTreeViewColumn *)g_array_index (accessible->col_data,
1829                  GtkTreeViewColumn *, i))
1830               {
1831                 column_found = TRUE;
1832                 break;
1833               }
1834         }
1835
1836        /* If column_found is FALSE, then a delete happened for column
1837         * number i
1838         */
1839       if (!column_found)
1840         {
1841           gint row;
1842
1843           clean_cols (accessible,
1844                       (GtkTreeViewColumn *)g_array_index (accessible->col_data,
1845                       GtkTreeViewColumn *, i));
1846
1847           /* Generate column-deleted signal */
1848           g_signal_emit_by_name (atk_obj, "column-deleted", i, 1);
1849
1850           /* Generate children-changed signals */
1851           for (row = 0; row < get_n_rows (tree_view); row++)
1852             {
1853               /* Pass NULL as the child object, 4th argument */
1854               g_signal_emit_by_name (atk_obj, "children-changed::remove",
1855                                      ((row * accessible->n_cols) + column_count), NULL, NULL);
1856             }
1857         }
1858     }
1859
1860   traverse_cells (accessible, NULL, FALSE);
1861
1862   /* rebuild the array */
1863   g_array_free (accessible->col_data, TRUE);
1864   accessible->col_data = g_array_sized_new (FALSE, TRUE, sizeof (GtkTreeViewColumn *), 0);
1865
1866   for (tmp_list = tv_cols; tmp_list; tmp_list = tmp_list->next)
1867     g_array_append_val (accessible->col_data, tmp_list->data);
1868   g_list_free (tv_cols);
1869 }
1870
1871 static void
1872 cursor_changed (GtkTreeView           *tree_view,
1873                 GtkTreeViewAccessible *accessible)
1874 {
1875   AtkObject *cell;
1876
1877   cell = gtk_tree_view_accessible_ref_focus_cell (tree_view);
1878   if (cell)
1879     {
1880       if (cell != accessible->focus_cell)
1881         {
1882           if (accessible->focus_cell)
1883             {
1884               _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_ACTIVE, FALSE);
1885               _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_FOCUSED, FALSE);
1886               g_object_unref (accessible->focus_cell);
1887               accessible->focus_cell = cell;
1888             }
1889
1890           if (gtk_widget_has_focus (GTK_WIDGET (tree_view)))
1891             {
1892               _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_ACTIVE, FALSE);
1893               _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_FOCUSED, FALSE);
1894             }
1895
1896           g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
1897         }
1898       else
1899         g_object_unref (cell);
1900     }
1901 }
1902
1903 static gboolean
1904 focus_in (GtkWidget *widget)
1905 {
1906   GtkTreeView *tree_view;
1907   GtkTreeViewAccessible *accessible;
1908   AtkStateSet *state_set;
1909   AtkObject *cell;
1910
1911   tree_view = GTK_TREE_VIEW (widget);
1912   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
1913
1914   if (accessible->focus_cell == NULL)
1915     {
1916       cell = gtk_tree_view_accessible_ref_focus_cell (tree_view);
1917       if (cell)
1918         {
1919           state_set = atk_object_ref_state_set (cell);
1920           if (state_set)
1921             {
1922               if (!atk_state_set_contains_state (state_set, ATK_STATE_FOCUSED))
1923                 {
1924                   _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_ACTIVE, FALSE);
1925                   accessible->focus_cell = cell;
1926                   _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_FOCUSED, FALSE);
1927                   g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
1928                 }
1929               g_object_unref (state_set);
1930             }
1931         }
1932     }
1933   return FALSE;
1934 }
1935
1936 static gboolean
1937 focus_out (GtkWidget *widget)
1938 {
1939   GtkTreeViewAccessible *accessible;
1940
1941   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
1942   if (accessible->focus_cell)
1943     {
1944       _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_ACTIVE, FALSE);
1945       _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_FOCUSED, FALSE);
1946       g_object_unref (accessible->focus_cell);
1947       accessible->focus_cell = NULL;
1948     }
1949   return FALSE;
1950 }
1951
1952 static void
1953 model_row_changed (GtkTreeModel *tree_model,
1954                    GtkTreePath  *path,
1955                    GtkTreeIter  *iter,
1956                    gpointer      user_data)
1957 {
1958   GtkTreeView *tree_view = GTK_TREE_VIEW (user_data);
1959   GtkTreeViewAccessible *accessible;
1960   GtkTreePath *cell_path;
1961   GtkTreeViewAccessibleCellInfo *cell_info;
1962   GHashTableIter hash_iter;
1963
1964   accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (tree_view)));
1965
1966   /* Loop through our cached cells */
1967   /* Must loop through them all */
1968   g_hash_table_iter_init (&hash_iter, accessible->cell_infos);
1969   while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer *)&cell_info))
1970     {
1971       cell_path = cell_info_get_path (cell_info);
1972
1973       if (cell_path != NULL)
1974         {
1975           if (path && gtk_tree_path_compare (cell_path, path) == 0)
1976             {
1977               if (GTK_IS_RENDERER_CELL_ACCESSIBLE (cell_info->cell))
1978                 update_cell_value (GTK_RENDERER_CELL_ACCESSIBLE (cell_info->cell),
1979                                    accessible, TRUE);
1980             }
1981           gtk_tree_path_free (cell_path);
1982         }
1983     }
1984   g_signal_emit_by_name (accessible, "visible-data-changed");
1985 }
1986
1987 static void
1988 column_visibility_changed (GObject    *object,
1989                            GParamSpec *pspec,
1990                            gpointer    user_data)
1991 {
1992   if (g_strcmp0 (pspec->name, "visible") == 0)
1993     {
1994       /* A column has been made visible or invisible
1995        * We update our cache of cells and emit model_changed signal
1996        */
1997       GtkTreeView *tree_view = (GtkTreeView *)user_data;
1998       GtkTreeViewAccessible *accessible;
1999       GtkTreeViewAccessibleCellInfo *cell_info;
2000       GtkTreeViewColumn *this_col = GTK_TREE_VIEW_COLUMN (object);
2001       GtkTreeViewColumn *tv_col;
2002       GHashTableIter iter;
2003
2004       accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (tree_view))
2005 );
2006       g_signal_emit_by_name (accessible, "model-changed");
2007
2008       g_hash_table_iter_init (&iter, accessible->cell_infos);
2009       while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&cell_info))
2010         {
2011           tv_col = cell_info->cell_col_ref;
2012           if (tv_col == this_col)
2013             {
2014               GtkTreePath *row_path;
2015               row_path = cell_info_get_path (cell_info);
2016               if (GTK_IS_RENDERER_CELL_ACCESSIBLE (cell_info->cell))
2017                 {
2018                   if (gtk_tree_view_column_get_visible (tv_col))
2019                       set_cell_visibility (tree_view,
2020                                            cell_info->cell,
2021                                            tv_col, row_path, FALSE);
2022                   else
2023                     {
2024                       _gtk_cell_accessible_remove_state (cell_info->cell, ATK_STATE_VISIBLE, TRUE);
2025                       _gtk_cell_accessible_remove_state (cell_info->cell, ATK_STATE_SHOWING, TRUE);
2026                     }
2027                 }
2028               gtk_tree_path_free (row_path);
2029             }
2030         }
2031     }
2032 }
2033
2034 static void
2035 model_row_inserted (GtkTreeModel *tree_model,
2036                     GtkTreePath  *path,
2037                     GtkTreeIter  *iter,
2038                     gpointer      user_data)
2039 {
2040   GtkTreeView *tree_view = (GtkTreeView *)user_data;
2041   AtkObject *atk_obj;
2042   GtkTreeViewAccessible *accessible;
2043   GtkTreePath *path_copy;
2044   gint row, n_inserted, child_row;
2045
2046   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2047   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
2048
2049   if (accessible->idle_expand_id)
2050     {
2051       g_source_remove (accessible->idle_expand_id);
2052       accessible->idle_expand_id = 0;
2053
2054       /* don't do this if the insertion precedes the idle path,
2055        * since it will now be invalid
2056        */
2057       if (path && accessible->idle_expand_path &&
2058           (gtk_tree_path_compare (path, accessible->idle_expand_path) > 0))
2059           set_expand_state (tree_view, tree_model, accessible, accessible->idle_expand_path, FALSE);
2060       if (accessible->idle_expand_path)
2061           gtk_tree_path_free (accessible->idle_expand_path);
2062     }
2063   /* Check to see if row is visible */
2064   row = get_row_from_tree_path (tree_view, path);
2065
2066  /* A row insert is not necessarily visible.  For example,
2067   * a row can be draged & dropped into another row, which
2068   * causes an insert on the model that isn't visible in the
2069   * view.  Only generate a signal if the inserted row is
2070   * visible.
2071   */
2072   if (row != -1)
2073     {
2074       GtkTreeIter tmp_iter;
2075       gint n_cols, col;
2076
2077       gtk_tree_model_get_iter (tree_model, &tmp_iter, path);
2078
2079       /* Figure out number of visible children. */
2080       if (gtk_tree_model_iter_has_child (tree_model, &tmp_iter))
2081         {
2082           GtkTreePath *path2;
2083          /*
2084           * By passing path into this function, we find the number of
2085           * visible children of path.
2086           */
2087           n_inserted = 0;
2088           /* iterate_thru_children modifies path, we don't want that, so give
2089            * it a copy */
2090           path2 = gtk_tree_path_copy (path);
2091           iterate_thru_children (tree_view, tree_model,
2092                                  path2, NULL, &n_inserted, 0);
2093           gtk_tree_path_free (path2);
2094
2095           /* Must add one to include the row that is being added */
2096           n_inserted++;
2097         }
2098       else
2099         n_inserted = 1;
2100
2101       traverse_cells (accessible, path, TRUE);
2102
2103       /* Generate row-inserted signal */
2104       g_signal_emit_by_name (atk_obj, "row-inserted", row, n_inserted);
2105
2106       /* Generate children-changed signals */
2107       n_cols = gtk_tree_view_accessible_get_n_columns (ATK_TABLE (atk_obj));
2108       for (child_row = row; child_row < (row + n_inserted); child_row++)
2109         {
2110           for (col = 0; col < n_cols; col++)
2111             {
2112              /* Pass NULL as the child object, i.e. 4th argument */
2113               g_signal_emit_by_name (atk_obj, "children-changed::add",
2114                                     ((row * n_cols) + col), NULL, NULL);
2115             }
2116         }
2117     }
2118   else
2119     {
2120      /* The row has been inserted inside another row.  This can
2121       * cause a row that previously couldn't be expanded to now
2122       * be expandable.
2123       */
2124       path_copy = gtk_tree_path_copy (path);
2125       gtk_tree_path_up (path_copy);
2126       set_expand_state (tree_view, tree_model, accessible, path_copy, TRUE);
2127       gtk_tree_path_free (path_copy);
2128     }
2129 }
2130
2131 static void
2132 model_row_deleted (GtkTreeModel *tree_model,
2133                    GtkTreePath  *path,
2134                    gpointer      user_data)
2135 {
2136   GtkTreeView *tree_view = (GtkTreeView *)user_data;
2137   GtkTreePath *path_copy;
2138   AtkObject *atk_obj;
2139   GtkTreeViewAccessible *accessible;
2140   gint row, col;
2141
2142   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2143   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
2144
2145   if (accessible->idle_expand_id)
2146     {
2147       g_source_remove (accessible->idle_expand_id);
2148       gtk_tree_path_free (accessible->idle_expand_path);
2149       accessible->idle_expand_id = 0;
2150     }
2151
2152   /* Check to see if row is visible */
2153   clean_rows (accessible);
2154
2155   traverse_cells (accessible, path, TRUE);
2156
2157   /* If deleting a row with a depth > 1, then this may affect the
2158    * expansion/contraction of its parent(s). Make sure this is
2159    * handled.
2160    */
2161   if (gtk_tree_path_get_depth (path) > 1)
2162     {
2163       path_copy = gtk_tree_path_copy (path);
2164       gtk_tree_path_up (path_copy);
2165       set_expand_state (tree_view, tree_model, accessible, path_copy, TRUE);
2166       gtk_tree_path_free (path_copy);
2167     }
2168   row = get_row_from_tree_path (tree_view, path);
2169
2170   /* If the row which is deleted is not visible because it is a child of
2171    * a collapsed row then row will be -1
2172    */
2173   if (row > 0)
2174     g_signal_emit_by_name (atk_obj, "row-deleted", row,
2175                            accessible->n_children_deleted + 1);
2176   accessible->n_children_deleted = 0;
2177
2178   /* Generate children-changed signals */
2179   for (col = 0; col < accessible->n_cols; col++)
2180     {
2181       /* Pass NULL as the child object, 4th argument */
2182       g_signal_emit_by_name (atk_obj, "children-changed::remove",
2183                              ((row * accessible->n_cols) + col), NULL, NULL);
2184     }
2185 }
2186
2187 /* This function gets called when a row is deleted or when rows are
2188  * removed from the view due to a collapse event. Note that the
2189  * count is the number of visible *children* of the deleted row,
2190  * so it does not include the row being deleted.
2191  *
2192  * As this function is called before the rows are removed we just note
2193  * the number of rows and then deal with it when we get a notification
2194  * that rows were deleted or collapsed.
2195  */
2196 static void
2197 destroy_count_func (GtkTreeView *tree_view,
2198                     GtkTreePath *path,
2199                     gint         count,
2200                     gpointer     user_data)
2201 {
2202   AtkObject *atk_obj;
2203   GtkTreeViewAccessible *accessible;
2204
2205   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2206   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
2207
2208   if (accessible->n_children_deleted != 0)
2209     return;
2210
2211   accessible->n_children_deleted = count;
2212 }
2213
2214 static void
2215 model_rows_reordered (GtkTreeModel *tree_model,
2216                       GtkTreePath  *path,
2217                       GtkTreeIter  *iter,
2218                       gint         *new_order,
2219                       gpointer      user_data)
2220 {
2221   GtkTreeView *tree_view = (GtkTreeView *)user_data;
2222   AtkObject *atk_obj;
2223   GtkTreeViewAccessible *accessible;
2224
2225   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2226   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
2227
2228   if (accessible->idle_expand_id)
2229     {
2230       g_source_remove (accessible->idle_expand_id);
2231       gtk_tree_path_free (accessible->idle_expand_path);
2232       accessible->idle_expand_id = 0;
2233     }
2234   traverse_cells (accessible, NULL, FALSE);
2235
2236   g_signal_emit_by_name (atk_obj, "row-reordered");
2237 }
2238
2239 static void
2240 set_cell_visibility (GtkTreeView       *tree_view,
2241                      GtkCellAccessible *cell,
2242                      GtkTreeViewColumn *tv_col,
2243                      GtkTreePath       *tree_path,
2244                      gboolean           emit_signal)
2245 {
2246   GdkRectangle cell_rect;
2247
2248   /* Get these three values in tree coords */
2249   if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
2250     gtk_tree_view_get_cell_area (tree_view, tree_path, tv_col, &cell_rect);
2251   else
2252     cell_rect.height = 0;
2253
2254   if (cell_rect.height > 0)
2255     {
2256       /* The height will be zero for a cell for which an antecedent
2257        * is not expanded
2258        */
2259       _gtk_cell_accessible_add_state (cell, ATK_STATE_VISIBLE, emit_signal);
2260       if (is_cell_showing (tree_view, &cell_rect))
2261         _gtk_cell_accessible_add_state (cell, ATK_STATE_SHOWING, emit_signal);
2262       else
2263         _gtk_cell_accessible_remove_state (cell, ATK_STATE_SHOWING, emit_signal);
2264     }
2265   else
2266     {
2267       _gtk_cell_accessible_remove_state (cell, ATK_STATE_VISIBLE, emit_signal);
2268       _gtk_cell_accessible_remove_state (cell, ATK_STATE_SHOWING, emit_signal);
2269     }
2270 }
2271
2272 static gboolean
2273 is_cell_showing (GtkTreeView  *tree_view,
2274                  GdkRectangle *cell_rect)
2275 {
2276   GdkRectangle rect, *visible_rect;
2277   GdkRectangle rect1, *tree_cell_rect;
2278   gint bx, by;
2279   gboolean is_showing;
2280
2281  /* A cell is considered "SHOWING" if any part of the cell is
2282   * in the visible area. Other ways we could do this is by a
2283   * cell's midpoint or if the cell is fully in the visible range.
2284   * Since we have the cell_rect x, y, width, height of the cell,
2285   * any of these is easy to compute.
2286   *
2287   * It is assumed that cell's rectangle is in widget coordinates
2288   * so we must transform to tree cordinates.
2289   */
2290   visible_rect = &rect;
2291   tree_cell_rect = &rect1;
2292   tree_cell_rect->x = cell_rect->x;
2293   tree_cell_rect->y = cell_rect->y;
2294   tree_cell_rect->width = cell_rect->width;
2295   tree_cell_rect->height = cell_rect->height;
2296
2297   gtk_tree_view_get_visible_rect (tree_view, visible_rect);
2298   gtk_tree_view_convert_tree_to_bin_window_coords (tree_view, visible_rect->x,
2299                                                    visible_rect->y, &bx, &by);
2300
2301   if (((tree_cell_rect->x + tree_cell_rect->width) < bx) ||
2302      ((tree_cell_rect->y + tree_cell_rect->height) < by) ||
2303      (tree_cell_rect->x > (bx + visible_rect->width)) ||
2304      (tree_cell_rect->y > (by + visible_rect->height)))
2305     is_showing =  FALSE;
2306   else
2307     is_showing = TRUE;
2308
2309   return is_showing;
2310 }
2311
2312 /* Misc Public */
2313
2314 /* This function is called when a cell's flyweight is created in
2315  * gtk_tree_view_accessible_table_ref_at with emit_change_signal
2316  * set to FALSE and in model_row_changed() on receipt of "row-changed"
2317  * signal when emit_change_signal is set to TRUE
2318  */
2319 static gboolean
2320 update_cell_value (GtkRendererCellAccessible      *renderer_cell,
2321                    GtkTreeViewAccessible *accessible,
2322                    gboolean               emit_change_signal)
2323 {
2324   GtkTreeViewAccessibleCellInfo *cell_info;
2325   GtkTreeView *tree_view;
2326   GtkTreeModel *tree_model;
2327   GtkTreePath *path;
2328   GtkTreeIter iter;
2329   GList *renderers, *cur_renderer;
2330   GParamSpec *spec;
2331   GtkRendererCellAccessibleClass *renderer_cell_class;
2332   GtkCellRendererClass *gtk_cell_renderer_class;
2333   GtkCellAccessible *cell;
2334   gchar **prop_list;
2335   AtkObject *parent;
2336   gboolean is_expander, is_expanded;
2337
2338   renderer_cell_class = GTK_RENDERER_CELL_ACCESSIBLE_GET_CLASS (renderer_cell);
2339   if (renderer_cell->renderer)
2340     gtk_cell_renderer_class = GTK_CELL_RENDERER_GET_CLASS (renderer_cell->renderer);
2341   else
2342     gtk_cell_renderer_class = NULL;
2343
2344   prop_list = renderer_cell_class->property_list;
2345
2346   cell = GTK_CELL_ACCESSIBLE (renderer_cell);
2347   cell_info = find_cell_info (accessible, cell, TRUE);
2348   if (!cell_info)
2349     return FALSE;
2350
2351   if (emit_change_signal)
2352     {
2353       tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
2354       tree_model = gtk_tree_view_get_model (tree_view);
2355       path = cell_info_get_path (cell_info);
2356       if (path == NULL)
2357         return FALSE;
2358
2359       gtk_tree_model_get_iter (tree_model, &iter, path);
2360       is_expander = FALSE;
2361       is_expanded = FALSE;
2362       if (gtk_tree_model_iter_has_child (tree_model, &iter))
2363         {
2364           GtkTreeViewColumn *expander_tv;
2365
2366           expander_tv = gtk_tree_view_get_expander_column (tree_view);
2367           if (expander_tv == cell_info->cell_col_ref)
2368             {
2369               is_expander = TRUE;
2370               is_expanded = gtk_tree_view_row_expanded (tree_view, path);
2371             }
2372         }
2373       gtk_tree_path_free (path);
2374       gtk_tree_view_column_cell_set_cell_data (cell_info->cell_col_ref,
2375                                                tree_model, &iter,
2376                                                is_expander, is_expanded);
2377     }
2378   renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_info->cell_col_ref));
2379   if (!renderers)
2380     return FALSE;
2381
2382   /* If the cell is in a container, its index is used to find the renderer
2383    * in the list. Otherwise, we assume that the cell is represented
2384    * by the first renderer in the list
2385    */
2386   parent = atk_object_get_parent (ATK_OBJECT (cell));
2387
2388   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2389     cur_renderer = g_list_nth (renderers, atk_object_get_index_in_parent (ATK_OBJECT (cell)));
2390   else
2391     cur_renderer = renderers;
2392
2393   if (cur_renderer == NULL)
2394     return FALSE;
2395
2396   if (gtk_cell_renderer_class)
2397     {
2398       while (*prop_list)
2399         {
2400           spec = g_object_class_find_property
2401                            (G_OBJECT_CLASS (gtk_cell_renderer_class), *prop_list);
2402
2403           if (spec != NULL)
2404             {
2405               GValue value = G_VALUE_INIT;
2406
2407               g_value_init (&value, spec->value_type);
2408               g_object_get_property (cur_renderer->data, *prop_list, &value);
2409               g_object_set_property (G_OBJECT (renderer_cell->renderer),
2410                                      *prop_list, &value);
2411               g_value_unset (&value);
2412             }
2413           else
2414             g_warning ("Invalid property: %s\n", *prop_list);
2415           prop_list++;
2416         }
2417     }
2418   g_list_free (renderers);
2419
2420   return _gtk_renderer_cell_accessible_update_cache (renderer_cell, emit_change_signal);
2421 }
2422
2423 static gint
2424 get_row_from_tree_path (GtkTreeView *tree_view,
2425                         GtkTreePath *path)
2426 {
2427   GtkTreeModel *tree_model;
2428   GtkTreePath *root_tree;
2429   gint row;
2430
2431   tree_model = gtk_tree_view_get_model (tree_view);
2432
2433   if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
2434     row = gtk_tree_path_get_indices (path)[0];
2435   else
2436     {
2437       root_tree = gtk_tree_path_new_first ();
2438       row = 0;
2439       iterate_thru_children (tree_view, tree_model, root_tree, path, &row, 0);
2440       gtk_tree_path_free (root_tree);
2441     }
2442
2443   return row;
2444 }
2445
2446 /* Misc Private */
2447
2448 /*
2449  * Get the specified GtkTreeViewColumn in the GtkTreeView.
2450  * Only visible columns are considered.
2451  */
2452 static GtkTreeViewColumn *
2453 get_column (GtkTreeView *tree_view,
2454             gint         in_col)
2455 {
2456   GtkTreeViewColumn *tv_col;
2457   gint n_cols = -1;
2458   gint i = 0;
2459
2460   if (in_col < 0)
2461     {
2462        g_warning ("Request for invalid column %d\n", in_col);
2463        return NULL;
2464     }
2465
2466   tv_col = gtk_tree_view_get_column (tree_view, i);
2467   while (tv_col != NULL)
2468     {
2469       if (gtk_tree_view_column_get_visible (tv_col))
2470         n_cols++;
2471       if (in_col == n_cols)
2472         break;
2473       tv_col = gtk_tree_view_get_column (tree_view, ++i);
2474     }
2475
2476   if (in_col != n_cols)
2477     {
2478        g_warning ("Request for invalid column %d\n", in_col);
2479        return NULL;
2480     }
2481   return tv_col;
2482 }
2483
2484 static gint
2485 get_actual_column_number (GtkTreeView *tree_view,
2486                           gint         visible_column)
2487 {
2488   GtkTreeViewColumn *tv_col;
2489   gint actual_column = 0;
2490   gint visible_columns = -1;
2491
2492   /* This function calculates the column number which corresponds
2493    * to the specified visible column number
2494    */
2495   tv_col = gtk_tree_view_get_column (tree_view, actual_column);
2496   while (tv_col != NULL)
2497     {
2498       if (gtk_tree_view_column_get_visible (tv_col))
2499         visible_columns++;
2500       if (visible_columns == visible_column)
2501         return actual_column;
2502       tv_col = gtk_tree_view_get_column (tree_view, ++actual_column);
2503     }
2504   g_warning ("get_actual_column_number failed for %d\n", visible_column);
2505   return -1;
2506 }
2507
2508 static gint
2509 get_visible_column_number (GtkTreeView *tree_view,
2510                            gint         actual_column)
2511 {
2512   GtkTreeViewColumn *tv_col;
2513   gint column = 0;
2514   gint visible_columns = -1;
2515
2516   /* This function calculates the visible column number
2517    * which corresponds to the specified actual column number
2518    */
2519   tv_col = gtk_tree_view_get_column (tree_view, column);
2520
2521   while (tv_col != NULL)
2522     {
2523       if (gtk_tree_view_column_get_visible (tv_col))
2524         {
2525           visible_columns++;
2526           if (actual_column == column)
2527             return visible_columns;
2528         }
2529       else
2530         if (actual_column == column)
2531           return -1;
2532       tv_col = gtk_tree_view_get_column (tree_view, ++column);
2533     }
2534   g_warning ("get_visible_column_number failed for %d\n", actual_column);
2535   return -1;
2536 }
2537
2538 /* Helper recursive function that returns an iter to nth row
2539  */
2540 static GtkTreeIter *
2541 return_iter_nth_row (GtkTreeView  *tree_view,
2542                      GtkTreeModel *tree_model,
2543                      GtkTreeIter  *iter,
2544                      gint          increment,
2545                      gint          row)
2546 {
2547   GtkTreePath *current_path;
2548   GtkTreeIter new_iter;
2549   gboolean row_expanded;
2550
2551   current_path = gtk_tree_model_get_path (tree_model, iter);
2552   if (increment == row)
2553     {
2554       gtk_tree_path_free (current_path);
2555       return iter;
2556     }
2557
2558   row_expanded = gtk_tree_view_row_expanded (tree_view, current_path);
2559   gtk_tree_path_free (current_path);
2560
2561   new_iter = *iter;
2562   if ((row_expanded && gtk_tree_model_iter_children (tree_model, iter, &new_iter)) ||
2563       (gtk_tree_model_iter_next (tree_model, iter)) ||
2564       (gtk_tree_model_iter_parent (tree_model, iter, &new_iter) &&
2565           (gtk_tree_model_iter_next (tree_model, iter))))
2566     return return_iter_nth_row (tree_view, tree_model, iter,
2567       ++increment, row);
2568
2569   return NULL;
2570 }
2571
2572 static void
2573 set_iter_nth_row (GtkTreeView *tree_view,
2574                   GtkTreeIter *iter,
2575                   gint         row)
2576 {
2577   GtkTreeModel *tree_model;
2578
2579   tree_model = gtk_tree_view_get_model (tree_view);
2580   gtk_tree_model_get_iter_first (tree_model, iter);
2581   iter = return_iter_nth_row (tree_view, tree_model, iter, 0, row);
2582 }
2583
2584 /* Recursively called until the row specified by orig is found.
2585  *
2586  * *count will be set to the visible row number of the child
2587  * relative to the row that was initially passed in as tree_path.
2588  * tree_path could be modified by this function.
2589  *
2590  * *count will be -1 if orig is not found as a child (a row that is
2591  * not visible will not be found, e.g. if the row is inside a
2592  * collapsed row).  If NULL is passed in as orig, *count will
2593  * be a count of the visible children.
2594  *
2595  * NOTE: the value for depth must be 0 when this recursive function
2596  * is initially called, or it may not function as expected.
2597  */
2598 static void
2599 iterate_thru_children (GtkTreeView  *tree_view,
2600                        GtkTreeModel *tree_model,
2601                        GtkTreePath  *tree_path,
2602                        GtkTreePath  *orig,
2603                        gint         *count,
2604                        gint          depth)
2605 {
2606   GtkTreeIter iter;
2607
2608   if (!gtk_tree_model_get_iter (tree_model, &iter, tree_path))
2609     return;
2610
2611   if (tree_path && orig && !gtk_tree_path_compare (tree_path, orig))
2612     /* Found it! */
2613     return;
2614
2615   if (tree_path && orig && gtk_tree_path_compare (tree_path, orig) > 0)
2616     {
2617       /* Past it, so return -1 */
2618       *count = -1;
2619       return;
2620     }
2621   else if (gtk_tree_view_row_expanded (tree_view, tree_path) &&
2622     gtk_tree_model_iter_has_child (tree_model, &iter))
2623     {
2624       (*count)++;
2625       gtk_tree_path_append_index (tree_path, 0);
2626       iterate_thru_children (tree_view, tree_model, tree_path,
2627                              orig, count, (depth + 1));
2628       return;
2629     }
2630   else if (gtk_tree_model_iter_next (tree_model, &iter))
2631     {
2632       (*count)++;
2633       tree_path = gtk_tree_model_get_path (tree_model, &iter);
2634        if (tree_path)
2635          {
2636            iterate_thru_children (tree_view, tree_model, tree_path,
2637                                  orig, count, depth);
2638            gtk_tree_path_free (tree_path);
2639          }
2640       return;
2641   }
2642   else if (gtk_tree_path_up (tree_path))
2643     {
2644       GtkTreeIter temp_iter;
2645       gboolean exit_loop = FALSE;
2646       gint new_depth = depth - 1;
2647
2648       (*count)++;
2649
2650      /* Make sure that we back up until we find a row
2651       * where gtk_tree_path_next does not return NULL.
2652       */
2653       while (!exit_loop)
2654         {
2655           if (gtk_tree_path_get_depth (tree_path) == 0)
2656               /* depth is now zero so */
2657             return;
2658           gtk_tree_path_next (tree_path);
2659
2660           /* Verify that the next row is a valid row! */
2661           exit_loop = gtk_tree_model_get_iter (tree_model, &temp_iter, tree_path);
2662
2663           if (!exit_loop)
2664             {
2665               /* Keep going up until we find a row that has a valid next */
2666               if (gtk_tree_path_get_depth(tree_path) > 1)
2667                 {
2668                   new_depth--;
2669                   gtk_tree_path_up (tree_path);
2670                 }
2671               else
2672                 {
2673                  /* If depth is 1 and gtk_tree_model_get_iter returns FALSE,
2674                   * then we are at the last row, so just return.
2675                   */
2676                   if (orig != NULL)
2677                     *count = -1;
2678
2679                   return;
2680                 }
2681             }
2682         }
2683
2684      /* This guarantees that we will stop when we hit the end of the
2685       * children.
2686       */
2687       if (new_depth < 0)
2688         return;
2689
2690       iterate_thru_children (tree_view, tree_model, tree_path,
2691                              orig, count, new_depth);
2692       return;
2693     }
2694
2695  /* If it gets here, then the path wasn't found.  Situations
2696   * that would cause this would be if the path passed in is
2697   * invalid or contained within the last row, but not visible
2698   * because the last row is not expanded.  If NULL was passed
2699   * in then a row count is desired, so only set count to -1
2700   * if orig is not NULL.
2701   */
2702   if (orig != NULL)
2703     *count = -1;
2704
2705   return;
2706 }
2707
2708 static void
2709 clean_rows (GtkTreeViewAccessible *accessible)
2710 {
2711   GtkTreeViewAccessibleCellInfo *cell_info;
2712   GHashTableIter iter;
2713
2714   /* Clean GtkTreeViewAccessibleCellInfo data */
2715   g_hash_table_iter_init (&iter, accessible->cell_infos);
2716   while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&cell_info))
2717     {
2718       GtkTreePath *row_path;
2719
2720       row_path = cell_info_get_path (cell_info);
2721
2722       /* If the cell has become invalid because the row has been removed,
2723        * then set the cell's state to ATK_STATE_DEFUNCT and schedule
2724        * its removal.  If row_path is NULL then the row has
2725        * been removed.
2726        */
2727       if (row_path == NULL)
2728         g_hash_table_iter_remove (&iter);
2729       else
2730         gtk_tree_path_free (row_path);
2731     }
2732 }
2733
2734 static void
2735 clean_cols (GtkTreeViewAccessible *accessible,
2736             GtkTreeViewColumn     *tv_col)
2737 {
2738   GtkTreeViewAccessibleCellInfo *cell_info;
2739   GHashTableIter iter;
2740
2741   /* Clean GtkTreeViewAccessibleCellInfo data */
2742   g_hash_table_iter_init (&iter, accessible->cell_infos);
2743   while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &cell_info))
2744     {
2745       /* If the cell has become invalid because the column tv_col
2746        * has been removed, then set the cell's state to ATK_STATE_DEFUNCT
2747        * and remove the cell from accessible->cell_data.
2748        */
2749       if (cell_info->cell_col_ref == tv_col)
2750         g_hash_table_iter_remove (&iter);
2751     }
2752 }
2753
2754 /* If tree_path is passed in as NULL, then all cells are acted on.
2755  * Otherwise, just act on those cells that are on a row greater than
2756  * the specified tree_path. If inc_row is passed in as TRUE, then rows
2757  * greater and equal to the specified tree_path are acted on.
2758  *
2759  * The function set_cell_visibility() is called on all cells to be
2760  * acted on to update the visibility of the cell.
2761  */
2762 static void
2763 traverse_cells (GtkTreeViewAccessible *accessible,
2764                 GtkTreePath           *tree_path,
2765                 gboolean               inc_row)
2766 {
2767   GtkTreeViewAccessibleCellInfo *cell_info;
2768   GtkWidget *widget;
2769   GHashTableIter iter;
2770
2771   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
2772   if (!widget)
2773     return;
2774
2775   /* Must loop through them all */
2776   g_hash_table_iter_init (&iter, accessible->cell_infos);
2777   while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&cell_info))
2778     {
2779       GtkTreePath *row_path;
2780       gboolean act_on_cell;
2781
2782       row_path = cell_info_get_path (cell_info);
2783       g_return_if_fail (row_path != NULL);
2784       if (tree_path == NULL)
2785         act_on_cell = TRUE;
2786       else
2787         {
2788           gint comparison;
2789
2790           comparison =  gtk_tree_path_compare (row_path, tree_path);
2791           if ((comparison > 0) ||
2792               (comparison == 0 && inc_row))
2793             act_on_cell = TRUE;
2794           else
2795             act_on_cell = FALSE;
2796         }
2797
2798       if (act_on_cell)
2799         {
2800           set_cell_visibility (GTK_TREE_VIEW (widget),
2801                                cell_info->cell,
2802                                cell_info->cell_col_ref,
2803                                row_path, TRUE);
2804         }
2805       gtk_tree_path_free (row_path);
2806     }
2807
2808   g_signal_emit_by_name (accessible, "visible-data-changed");
2809 }
2810
2811 /* If the tree_path passed in has children, then
2812  * ATK_STATE_EXPANDABLE is set.  If the row is expanded
2813  * ATK_STATE_EXPANDED is turned on.  If the row is
2814  * collapsed, then ATK_STATE_EXPANDED is removed.
2815  *
2816  * If the tree_path passed in has no children, then
2817  * ATK_STATE_EXPANDABLE and ATK_STATE_EXPANDED are removed.
2818  *
2819  * If set_on_ancestor is TRUE, then this function will also
2820  * update all cells that are ancestors of the tree_path.
2821  */
2822 static void
2823 set_expand_state (GtkTreeView           *tree_view,
2824                   GtkTreeModel          *tree_model,
2825                   GtkTreeViewAccessible *accessible,
2826                   GtkTreePath           *tree_path,
2827                   gboolean               set_on_ancestor)
2828 {
2829   GtkTreeViewColumn *expander_tv;
2830   GtkTreeViewAccessibleCellInfo *cell_info;
2831   GtkTreePath *cell_path;
2832   GtkTreeIter iter;
2833   gboolean found;
2834   GHashTableIter hash_iter;
2835
2836   g_hash_table_iter_init (&hash_iter, accessible->cell_infos);
2837   while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer *) &cell_info))
2838     {
2839       cell_path = cell_info_get_path (cell_info);
2840       found = FALSE;
2841
2842       if (cell_path != NULL)
2843         {
2844           GtkCellAccessible *cell = GTK_CELL_ACCESSIBLE (cell_info->cell);
2845
2846           expander_tv = gtk_tree_view_get_expander_column (tree_view);
2847
2848           /* Only set state for the cell that is in the column with the
2849            * expander toggle
2850            */
2851           if (expander_tv == cell_info->cell_col_ref)
2852             {
2853               if (tree_path && gtk_tree_path_compare (cell_path, tree_path) == 0)
2854                 found = TRUE;
2855               else if (set_on_ancestor &&
2856                        gtk_tree_path_get_depth (cell_path) <
2857                        gtk_tree_path_get_depth (tree_path) &&
2858                        gtk_tree_path_is_ancestor (cell_path, tree_path) == 1)
2859                 /* Only set if set_on_ancestor was passed in as TRUE */
2860                 found = TRUE;
2861             }
2862
2863           /* Set ATK_STATE_EXPANDABLE and ATK_STATE_EXPANDED
2864            * for ancestors and found cells.
2865            */
2866           if (found)
2867             {
2868               /* Must check against cell_path since cell_path
2869                * can be equal to or an ancestor of tree_path.
2870                */
2871               gtk_tree_model_get_iter (tree_model, &iter, cell_path);
2872
2873               /* Set or unset ATK_STATE_EXPANDABLE as appropriate */
2874               if (gtk_tree_model_iter_has_child (tree_model, &iter))
2875                 {
2876                   set_cell_expandable (cell);
2877
2878                   if (gtk_tree_view_row_expanded (tree_view, cell_path))
2879                     _gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDED, TRUE);
2880                   else
2881                     _gtk_cell_accessible_remove_state (cell, ATK_STATE_EXPANDED, TRUE);
2882                 }
2883               else
2884                 {
2885                   _gtk_cell_accessible_remove_state (cell, ATK_STATE_EXPANDED, TRUE);
2886                   if (_gtk_cell_accessible_remove_state (cell, ATK_STATE_EXPANDABLE, TRUE))
2887                   /* The state may have been propagated to the container cell */
2888                   if (!GTK_IS_CONTAINER_CELL_ACCESSIBLE (cell))
2889                     _gtk_cell_accessible_remove_action_by_name (cell,
2890                                                                 "expand or contract");
2891                 }
2892
2893               /* We assume that each cell in the cache once and
2894                * a container cell is before its child cells so we are
2895                * finished if set_on_ancestor is not set to TRUE.
2896                */
2897               if (!set_on_ancestor)
2898                 break;
2899             }
2900         }
2901       gtk_tree_path_free (cell_path);
2902     }
2903 }
2904
2905 static void
2906 add_cell_actions (GtkCellAccessible *cell,
2907                   gboolean           editable)
2908 {
2909   if (GTK_IS_BOOLEAN_CELL_ACCESSIBLE (cell))
2910     _gtk_cell_accessible_add_action (cell,
2911                                      "toggle", "toggles the cell",
2912                                      NULL, toggle_cell_toggled);
2913   if (editable)
2914     _gtk_cell_accessible_add_action (cell,
2915                                      "edit", "creates a widget in which the contents of the cell can be edited",
2916                                      NULL, edit_cell);
2917   _gtk_cell_accessible_add_action (cell,
2918                                    "activate", "activate the cell",
2919                                    NULL, activate_cell);
2920 }
2921
2922 static void
2923 toggle_cell_expanded (GtkCellAccessible *cell)
2924 {
2925   GtkTreeViewAccessibleCellInfo *cell_info;
2926   GtkTreeView *tree_view;
2927   GtkTreePath *path;
2928   AtkObject *parent;
2929   AtkStateSet *stateset;
2930
2931   parent = atk_object_get_parent (ATK_OBJECT (cell));
2932   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2933     parent = atk_object_get_parent (parent);
2934
2935   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell, TRUE);
2936   if (!cell_info)
2937     return;
2938
2939   tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
2940   path = cell_info_get_path (cell_info);
2941   if (!path)
2942     return;
2943
2944   stateset = atk_object_ref_state_set (ATK_OBJECT (cell));
2945   if (atk_state_set_contains_state (stateset, ATK_STATE_EXPANDED))
2946     gtk_tree_view_collapse_row (tree_view, path);
2947   else
2948     gtk_tree_view_expand_row (tree_view, path, TRUE);
2949   g_object_unref (stateset);
2950   gtk_tree_path_free (path);
2951 }
2952
2953 static void
2954 toggle_cell_toggled (GtkCellAccessible *cell)
2955 {
2956   GtkTreeViewAccessibleCellInfo *cell_info;
2957   GtkTreePath *path;
2958   gchar *pathstring;
2959   GList *renderers, *cur_renderer;
2960   AtkObject *parent;
2961   gboolean is_container_cell = FALSE;
2962
2963   parent = atk_object_get_parent (ATK_OBJECT (cell));
2964   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2965     {
2966       is_container_cell = TRUE;
2967       parent = atk_object_get_parent (parent);
2968     }
2969
2970   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell, TRUE);
2971   if (!cell_info)
2972     return;
2973
2974   path = cell_info_get_path (cell_info);
2975   if (!path)
2976     return;
2977
2978   /* If the cell is in a container, its index is used to find the
2979    * renderer in the list. Otherwise, we assume that the cell is
2980    * represented by the first renderer in the list
2981    */
2982   renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_info->cell_col_ref));
2983   if (is_container_cell)
2984     cur_renderer = g_list_nth (renderers, atk_object_get_index_in_parent (ATK_OBJECT (cell)));
2985   else
2986     cur_renderer = renderers;
2987
2988   if (cur_renderer)
2989     {
2990       pathstring = gtk_tree_path_to_string (path);
2991       g_signal_emit_by_name (cur_renderer->data, "toggled", pathstring);
2992       g_free (pathstring);
2993     }
2994
2995   g_list_free (renderers);
2996   gtk_tree_path_free (path);
2997 }
2998
2999 static void
3000 edit_cell (GtkCellAccessible *cell)
3001 {
3002   GtkTreeViewAccessibleCellInfo *cell_info;
3003   GtkTreeView *tree_view;
3004   GtkTreePath *path;
3005   AtkObject *parent;
3006
3007   parent = atk_object_get_parent (ATK_OBJECT (cell));
3008   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
3009     parent = atk_object_get_parent (parent);
3010
3011   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell, TRUE);
3012   if (!cell_info)
3013     return;
3014
3015   tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
3016   path = cell_info_get_path (cell_info);
3017   if (!path)
3018     return;
3019   gtk_tree_view_set_cursor (tree_view, path, cell_info->cell_col_ref, TRUE);
3020   gtk_tree_path_free (path);
3021 }
3022
3023 static void
3024 activate_cell (GtkCellAccessible *cell)
3025 {
3026   GtkTreeViewAccessibleCellInfo *cell_info;
3027   GtkTreeView *tree_view;
3028   GtkTreePath *path;
3029   AtkObject *parent;
3030
3031   parent = atk_object_get_parent (ATK_OBJECT (cell));
3032   if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
3033     parent = atk_object_get_parent (parent);
3034
3035   cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell, TRUE);
3036   if (!cell_info)
3037     return;
3038
3039   tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
3040   path = cell_info_get_path (cell_info);
3041   if (!path)
3042     return;
3043   gtk_tree_view_row_activated (tree_view, path, cell_info->cell_col_ref);
3044   gtk_tree_path_free (path);
3045 }
3046
3047 static void
3048 cell_destroyed (gpointer data)
3049 {
3050   GtkTreeViewAccessibleCellInfo *cell_info = data;
3051
3052   cell_info->cell = NULL;
3053
3054   g_hash_table_remove (cell_info->view->cell_infos, cell_info);
3055 }
3056
3057 static int
3058 cell_info_get_index (GtkTreeView                     *tree_view,
3059                      GtkTreeViewAccessibleCellInfo   *info)
3060 {
3061   GtkTreePath *path;
3062   gint column_number;
3063   int index;
3064
3065   path = cell_info_get_path (info);
3066   if (!path)
3067     return -1;
3068
3069   column_number = get_column_number (tree_view, info->cell_col_ref, FALSE);
3070   index = get_index (tree_view, path, column_number);
3071   gtk_tree_path_free (path);
3072
3073   return index;
3074 }
3075
3076 static void
3077 cell_info_new (GtkTreeViewAccessible *accessible,
3078                GtkTreeModel          *tree_model,
3079                GtkRBTree             *tree,
3080                GtkRBNode             *node,
3081                GtkTreeViewColumn     *tv_col,
3082                GtkCellAccessible     *cell)
3083 {
3084   GtkTreeViewAccessibleCellInfo *cell_info;
3085
3086   cell_info = g_new (GtkTreeViewAccessibleCellInfo, 1);
3087
3088   cell_info->tree = tree;
3089   cell_info->node = node;
3090   cell_info->cell_col_ref = tv_col;
3091   cell_info->cell = cell;
3092   cell_info->view = accessible;
3093
3094   g_object_set_qdata_full (G_OBJECT (cell), 
3095                            gtk_tree_view_accessible_get_data_quark (),
3096                            cell_info,
3097                            cell_destroyed);
3098
3099   g_hash_table_replace (accessible->cell_infos, cell_info, cell_info);
3100 }
3101
3102 static GtkCellAccessible *
3103 find_cell (GtkTreeViewAccessible *accessible,
3104            gint                   index)
3105 {
3106   GtkTreeViewAccessibleCellInfo *info;
3107   GHashTableIter iter;
3108   GtkTreeView *tree_view;
3109
3110   tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
3111
3112   g_hash_table_iter_init (&iter, accessible->cell_infos);
3113   while (g_hash_table_iter_next (&iter, NULL, (gpointer *) &info))
3114     {
3115       if (index == cell_info_get_index (tree_view, info))
3116         return info->cell;
3117     }
3118
3119   return NULL;
3120 }
3121
3122 static void
3123 connect_model_signals (GtkTreeView           *view,
3124                        GtkTreeViewAccessible *accessible)
3125 {
3126   GObject *obj;
3127
3128   obj = G_OBJECT (accessible->tree_model);
3129   g_signal_connect_data (obj, "row-changed",
3130                          G_CALLBACK (model_row_changed), view, NULL, 0);
3131   g_signal_connect_data (obj, "row-inserted",
3132                          G_CALLBACK (model_row_inserted), view, NULL,
3133                          G_CONNECT_AFTER);
3134   g_signal_connect_data (obj, "row-deleted",
3135                          G_CALLBACK (model_row_deleted), view, NULL,
3136                          G_CONNECT_AFTER);
3137   g_signal_connect_data (obj, "rows-reordered",
3138                          G_CALLBACK (model_rows_reordered), view, NULL,
3139                          G_CONNECT_AFTER);
3140 }
3141
3142 static void
3143 disconnect_model_signals (GtkTreeViewAccessible *accessible)
3144 {
3145   GObject *obj;
3146   GtkWidget *widget;
3147
3148   obj = G_OBJECT (accessible->tree_model);
3149   widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
3150   g_signal_handlers_disconnect_by_func (obj, model_row_changed, widget);
3151   g_signal_handlers_disconnect_by_func (obj, model_row_inserted, widget);
3152   g_signal_handlers_disconnect_by_func (obj, model_row_deleted, widget);
3153   g_signal_handlers_disconnect_by_func (obj, model_rows_reordered, widget);
3154 }
3155
3156 /* Returns the column number of the specified GtkTreeViewColumn
3157  *
3158  * If visible is set, the value returned will be the visible column number,
3159  * i.e. suitable for use in AtkTable function. If visible is not set, the
3160  * value returned is the actual column number, which is suitable for use in
3161  * getting an index value.
3162  */
3163 static gint
3164 get_column_number (GtkTreeView       *tree_view,
3165                    GtkTreeViewColumn *column,
3166                    gboolean           visible)
3167 {
3168   GtkTreeViewColumn *tv_column;
3169   gint ret_val;
3170   gint i;
3171   AtkObject *atk_obj;
3172   GtkTreeViewAccessible *accessible;
3173
3174   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
3175   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
3176
3177   ret_val = 0;
3178   for (i = 0; i < accessible->col_data->len; i++)
3179     {
3180       tv_column = g_array_index (accessible->col_data, GtkTreeViewColumn *, i);
3181       if (tv_column == column)
3182         break;
3183       if (!visible || gtk_tree_view_column_get_visible (tv_column))
3184         ret_val++;
3185     }
3186   if (i == accessible->col_data->len)
3187     ret_val = -1;
3188
3189   return ret_val;
3190 }
3191
3192 static gint
3193 get_index (GtkTreeView *tree_view,
3194            GtkTreePath *path,
3195            gint         actual_column)
3196 {
3197   AtkObject *atk_obj;
3198   GtkTreeViewAccessible *accessible;
3199   gint depth = 0;
3200   gint index = 1;
3201   gint *indices = NULL;
3202
3203   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
3204   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
3205
3206   if (path)
3207     {
3208       depth = gtk_tree_path_get_depth (path);
3209       indices = gtk_tree_path_get_indices (path);
3210     }
3211
3212   if (depth > 1)
3213     {
3214       GtkTreePath *copy_path;
3215       GtkTreeModel *model;
3216
3217       model = gtk_tree_view_get_model (tree_view);
3218       copy_path = gtk_tree_path_copy (path);
3219       gtk_tree_path_up (copy_path);
3220       count_rows (model, NULL, copy_path, &index, 0, depth);
3221       gtk_tree_path_free (copy_path);
3222     }
3223
3224   if (path)
3225     index += indices[depth - 1];
3226   index *= accessible->n_cols;
3227   index +=  actual_column;
3228   return index;
3229 }
3230
3231 /* The function count_rows counts the number of rows starting at iter
3232  * and ending at end_path. The value of level is the depth of iter and
3233  * the value of depth is the depth of end_path. Rows at depth before
3234  * end_path are counted. This functions counts rows which are not visible
3235  * because an ancestor is collapsed.
3236  */
3237 static void
3238 count_rows (GtkTreeModel *model,
3239             GtkTreeIter  *iter,
3240             GtkTreePath  *end_path,
3241             gint         *count,
3242             gint          level,
3243             gint          depth)
3244 {
3245   GtkTreeIter child_iter;
3246
3247   if (!model)
3248     return;
3249
3250   level++;
3251   *count += gtk_tree_model_iter_n_children (model, iter);
3252
3253   if (gtk_tree_model_get_flags (model) & GTK_TREE_MODEL_LIST_ONLY)
3254     return;
3255
3256   if (level >= depth)
3257     return;
3258
3259   if (gtk_tree_model_iter_children (model, &child_iter, iter))
3260     {
3261       gboolean ret_val = TRUE;
3262
3263       while (ret_val)
3264         {
3265           if (level == depth - 1)
3266             {
3267               GtkTreePath *iter_path;
3268               gboolean finished = FALSE;
3269
3270               iter_path = gtk_tree_model_get_path (model, &child_iter);
3271               if (end_path && gtk_tree_path_compare (iter_path, end_path) >= 0)
3272                 finished = TRUE;
3273               gtk_tree_path_free (iter_path);
3274               if (finished)
3275                 break;
3276             }
3277           if (gtk_tree_model_iter_has_child (model, &child_iter))
3278             count_rows (model, &child_iter, end_path, count, level, depth);
3279           ret_val = gtk_tree_model_iter_next (model, &child_iter);
3280         }
3281     }
3282 }
3283
3284 static gboolean
3285 get_rbtree_column_from_index (GtkTreeView        *tree_view,
3286                               gint                index,
3287                               GtkRBTree         **tree,
3288                               GtkRBNode         **node,
3289                               GtkTreeViewColumn **column)
3290 {
3291   AtkObject *atk_obj;
3292   GtkTreeViewAccessible *accessible;
3293
3294   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
3295   accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
3296
3297   if (accessible->n_cols == 0)
3298     return FALSE;
3299   /* First row is the column headers */
3300   index -= accessible->n_cols;
3301   if (index < 0)
3302     return FALSE;
3303
3304   if (tree)
3305     {
3306       g_return_val_if_fail (node != NULL, FALSE);
3307
3308       if (!_gtk_rbtree_find_index (_gtk_tree_view_get_rbtree (tree_view),
3309                                    index / accessible->n_cols,
3310                                    tree,
3311                                    node))
3312         return FALSE;
3313     }
3314
3315   if (column)
3316     {
3317       *column = gtk_tree_view_get_column (tree_view, index % accessible->n_cols);
3318       if (*column == NULL)
3319         return FALSE;
3320   }
3321   return TRUE;
3322 }
3323
3324 static void
3325 set_cell_expandable (GtkCellAccessible *cell)
3326 {
3327   if (_gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDABLE, FALSE))
3328     _gtk_cell_accessible_add_action (cell,
3329                                      "expand or contract",
3330                                      "expands or contracts the row in the tree view containing this cell",
3331                                      NULL, toggle_cell_expanded);
3332 }
3333
3334 static GtkTreeViewAccessibleCellInfo *
3335 find_cell_info (GtkTreeViewAccessible *accessible,
3336                 GtkCellAccessible     *cell,
3337                 gboolean               live_only)
3338 {
3339   return g_object_get_qdata (G_OBJECT (cell),
3340                                   gtk_tree_view_accessible_get_data_quark ());
3341 }
3342
3343 static AtkObject *
3344 get_header_from_column (GtkTreeViewColumn *tv_col)
3345 {
3346   AtkObject *rc;
3347   GtkWidget *header_widget;
3348
3349   if (tv_col == NULL)
3350     return NULL;
3351
3352   header_widget = gtk_tree_view_column_get_button (tv_col);
3353
3354   if (header_widget)
3355     rc = gtk_widget_get_accessible (header_widget);
3356   else
3357     rc = NULL;
3358
3359   return rc;
3360 }
3361
3362 /**
3363  * _gtk_rbtree_get_ancestor_node:
3364  * @ancestor: the ancestor tree
3365  * @child_tree: the potential child's tree
3366  * @child_node: the potential child's node
3367  *
3368  * Finds the node that is the ancestor of @child_tree and @child_node
3369  * and belongs to @ancestor. If @ancestor is not an ancestor tree
3370  * of @child_node, %NULL is returned.
3371  *
3372  * Returns: the ancestor node or %NULL if @ancestor is not an ancestor.
3373  **/
3374 static GtkRBNode *
3375 _gtk_rbtree_get_ancestor_node (GtkRBTree *ancestor,
3376                                GtkRBTree *child_tree,
3377                                GtkRBNode *child_node)
3378 {
3379   while (child_tree != NULL)
3380     {
3381       if (child_tree == ancestor)
3382         return child_node;
3383
3384       child_node = child_tree->parent_node;
3385       child_tree = child_tree->parent_tree;
3386     }
3387
3388   return NULL;
3389 }
3390
3391 void
3392 _gtk_tree_view_accessible_remove (GtkTreeView *treeview,
3393                                   GtkRBTree   *tree,
3394                                   GtkRBNode   *node)
3395 {
3396   GtkTreeViewAccessibleCellInfo *cell_info;
3397   GHashTableIter iter;
3398   GtkTreeViewAccessible *accessible;
3399
3400   accessible = GTK_TREE_VIEW_ACCESSIBLE (_gtk_widget_peek_accessible (GTK_WIDGET (treeview)));
3401   if (accessible == NULL)
3402     return;
3403
3404   /* if this shows up in profiles, special-case node->children == NULL */
3405
3406   g_hash_table_iter_init (&iter, accessible->cell_infos);
3407   while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&cell_info))
3408     {
3409       GtkRBNode *child_node = _gtk_rbtree_get_ancestor_node (tree,
3410                                                              cell_info->tree,
3411                                                              cell_info->node);
3412
3413       if (child_node == NULL)
3414         continue;
3415
3416       if (node == NULL || node == child_node)
3417         g_hash_table_iter_remove (&iter);
3418     }
3419 }
3420