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