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