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