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