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