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