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