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