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