]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailtreeview.c
Deprecate flag macros for toplevel, state, no window and composite child
[~andy/gtk] / modules / other / gail / gailtreeview.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 <string.h>
23 #include <gtk/gtk.h>
24 #ifdef GDK_WINDOWING_X11
25 #include <gdk/x11/gdkx.h>
26 #endif
27 #include "gailtreeview.h"
28 #include "gailrenderercell.h"
29 #include "gailbooleancell.h"
30 #include "gailcontainercell.h"
31 #include "gailtextcell.h"
32 #include "gailcellparent.h"
33 #include "gail-private-macros.h"
34
35 typedef struct _GailTreeViewRowInfo    GailTreeViewRowInfo;
36 typedef struct _GailTreeViewCellInfo   GailTreeViewCellInfo;
37
38 static void             gail_tree_view_class_init       (GailTreeViewClass      *klass);
39 static void             gail_tree_view_init             (GailTreeView           *view);
40 static void             gail_tree_view_real_initialize  (AtkObject              *obj,
41                                                          gpointer               data);
42 static void             gail_tree_view_real_notify_gtk  (GObject                *obj,
43                                                          GParamSpec             *pspec);
44 static void             gail_tree_view_finalize         (GObject                *object);
45
46 static void             gail_tree_view_connect_widget_destroyed 
47                                                         (GtkAccessible          *accessible);
48 static void             gail_tree_view_destroyed        (GtkWidget              *widget,
49                                                          GtkAccessible          *accessible); 
50 /* atkobject.h */
51
52 static gint             gail_tree_view_get_n_children   (AtkObject              *obj);
53 static AtkObject*       gail_tree_view_ref_child        (AtkObject              *obj,
54                                                          gint                   i);
55 static AtkStateSet*     gail_tree_view_ref_state_set    (AtkObject              *obj);
56
57 /* atkcomponent.h */
58
59 static void             atk_component_interface_init    (AtkComponentIface      *iface);
60
61 static AtkObject*       gail_tree_view_ref_accessible_at_point
62                                                         (AtkComponent           *component,
63                                                          gint                   x,
64                                                          gint                   y,
65                                                          AtkCoordType           coord_type);
66            
67 /* atktable.h */
68
69 static void             atk_table_interface_init        (AtkTableIface          *iface);
70
71 static gint             gail_tree_view_get_index_at     (AtkTable               *table,
72                                                          gint                   row,
73                                                          gint                   column);
74 static gint             gail_tree_view_get_column_at_index
75                                                         (AtkTable               *table,
76                                                          gint                   index);
77 static gint             gail_tree_view_get_row_at_index (AtkTable               *table,
78                                                          gint                   index);
79
80 static AtkObject*       gail_tree_view_table_ref_at     (AtkTable               *table,
81                                                          gint                   row,
82                                                          gint                   column);
83 static gint             gail_tree_view_get_n_rows       (AtkTable               *table);
84 static gint             gail_tree_view_get_n_columns    (AtkTable               *table);
85 static gint             get_n_actual_columns            (GtkTreeView            *tree_view);
86 static gboolean         gail_tree_view_is_row_selected  (AtkTable               *table,
87                                                          gint                   row);
88 static gboolean         gail_tree_view_is_selected      (AtkTable               *table,
89                                                          gint                   row,
90                                                          gint                   column);
91 static gint             gail_tree_view_get_selected_rows 
92                                                         (AtkTable               *table, 
93                                                          gint                   **selected);
94 static gboolean         gail_tree_view_add_row_selection 
95                                                         (AtkTable               *table, 
96                                                          gint                   row);
97 static gboolean         gail_tree_view_remove_row_selection 
98                                                         (AtkTable               *table, 
99                                                          gint                   row);
100 static AtkObject*       gail_tree_view_get_row_header   (AtkTable               *table,
101                                                          gint                   row);
102 static AtkObject*       gail_tree_view_get_column_header 
103                                                         (AtkTable               *table,
104                                                          gint                   column);
105 static void             gail_tree_view_set_row_header   (AtkTable               *table,
106                                                          gint                   row,
107                                                          AtkObject              *header);
108 static void             gail_tree_view_set_column_header 
109                                                         (AtkTable               *table,
110                                                          gint                   column,
111                                                          AtkObject              *header);
112 static AtkObject*
113                         gail_tree_view_get_caption      (AtkTable               *table);
114 static void             gail_tree_view_set_caption      (AtkTable               *table,
115                                                          AtkObject              *caption);
116 static AtkObject*       gail_tree_view_get_summary      (AtkTable               *table);
117 static void             gail_tree_view_set_summary      (AtkTable               *table,
118                                                          AtkObject              *accessible);
119 static G_CONST_RETURN gchar*
120                         gail_tree_view_get_row_description 
121                                                         (AtkTable               *table,
122                                                          gint                   row);
123 static void             gail_tree_view_set_row_description 
124                                                         (AtkTable               *table,
125                                                          gint                   row,
126                                                          const gchar            *description);
127 static G_CONST_RETURN gchar*
128                         gail_tree_view_get_column_description
129                                                         (AtkTable               *table,
130                                                          gint                   column);
131 static void             gail_tree_view_set_column_description
132                                                         (AtkTable               *table,
133                                                          gint                   column,
134                                                          const gchar            *description);
135
136 static void             set_row_data                    (AtkTable               *table,
137                                                          gint                   row,
138                                                          AtkObject              *header,
139                                                          const gchar            *description,
140                                                          gboolean               is_header);
141 static GailTreeViewRowInfo* 
142                         get_row_info                    (AtkTable               *table,
143                                                          gint                   row);
144
145 /* atkselection.h */
146
147 static void             atk_selection_interface_init    (AtkSelectionIface      *iface);
148 static gboolean         gail_tree_view_add_selection    (AtkSelection           *selection,
149                                                          gint                   i);
150 static gboolean         gail_tree_view_clear_selection  (AtkSelection           *selection);
151 static AtkObject*       gail_tree_view_ref_selection    (AtkSelection           *selection,
152                                                          gint                   i);
153 static gint             gail_tree_view_get_selection_count 
154                                                         (AtkSelection           *selection);
155 static gboolean         gail_tree_view_is_child_selected 
156                                                         (AtkSelection           *selection,
157                                                          gint                   i);
158
159 /* gailcellparent.h */
160
161 static void             gail_cell_parent_interface_init (GailCellParentIface    *iface);
162 static void             gail_tree_view_get_cell_extents (GailCellParent         *parent,
163                                                          GailCell               *cell,
164                                                          gint                   *x,
165                                                          gint                   *y,
166                                                          gint                   *width,
167                                                          gint                   *height,
168                                                          AtkCoordType           coord_type);
169 static void             gail_tree_view_get_cell_area    (GailCellParent         *parent,
170                                                          GailCell               *cell,
171                                                          GdkRectangle           *cell_rect);
172 static gboolean         gail_tree_view_grab_cell_focus  (GailCellParent         *parent,
173                                                          GailCell               *cell);
174
175 /* signal handling */
176
177 static gboolean         gail_tree_view_expand_row_gtk   (GtkTreeView            *tree_view,
178                                                          GtkTreeIter            *iter,
179                                                          GtkTreePath            *path);
180 static gint             idle_expand_row                 (gpointer               data);
181 static gboolean         gail_tree_view_collapse_row_gtk (GtkTreeView            *tree_view,
182                                                          GtkTreeIter            *iter,
183                                                          GtkTreePath            *path);
184 static void             gail_tree_view_size_allocate_gtk (GtkWidget             *widget,
185                                                          GtkAllocation          *allocation);
186 static void             gail_tree_view_set_scroll_adjustments
187                                                         (GtkWidget              *widget,
188                                                          GtkAdjustment          *hadj,
189                                                          GtkAdjustment          *vadj);
190 static void             gail_tree_view_changed_gtk      (GtkTreeSelection       *selection,
191                                                          gpointer               data);
192
193 static void             columns_changed                 (GtkTreeView            *tree_view);
194 static void             cursor_changed                  (GtkTreeView            *tree_view);
195 static gint             idle_cursor_changed             (gpointer               data);
196 static void             focus_in                        (GtkWidget              *widget);
197 static void             focus_out                       (GtkWidget              *widget);
198
199 static void             model_row_changed               (GtkTreeModel           *tree_model,
200                                                          GtkTreePath            *path,
201                                                          GtkTreeIter            *iter,
202                                                          gpointer               user_data);
203 static void             column_visibility_changed       (GObject                *object,
204                                                          GParamSpec             *param,
205                                                          gpointer               user_data);
206 static void             column_destroy                  (GtkObject              *obj); 
207 static void             model_row_inserted              (GtkTreeModel           *tree_model,
208                                                          GtkTreePath            *path,
209                                                          GtkTreeIter            *iter,
210                                                          gpointer               user_data);
211 static void             model_row_deleted               (GtkTreeModel           *tree_model,
212                                                          GtkTreePath            *path,
213                                                          gpointer               user_data);
214 static void             destroy_count_func              (GtkTreeView            *tree_view,
215                                                          GtkTreePath            *path,
216                                                          gint                   count,
217                                                          gpointer               user_data);
218 static void             model_rows_reordered            (GtkTreeModel           *tree_model,
219                                                          GtkTreePath            *path,
220                                                          GtkTreeIter            *iter,
221                                                          gint                   *new_order,
222                                                          gpointer               user_data);
223 static void             adjustment_changed              (GtkAdjustment          *adjustment,
224                                                          GtkTreeView            *tree_view);
225
226 /* Misc */
227
228 static void             set_iter_nth_row                (GtkTreeView            *tree_view,
229                                                          GtkTreeIter            *iter,
230                                                          gint                   row);
231 static gint             get_row_from_tree_path          (GtkTreeView            *tree_view,
232                                                          GtkTreePath            *path);
233 static GtkTreeViewColumn* get_column                    (GtkTreeView            *tree_view,
234                                                          gint                   in_col);
235 static gint             get_actual_column_number        (GtkTreeView            *tree_view,
236                                                          gint                   visible_column);
237 static gint             get_visible_column_number       (GtkTreeView            *tree_view,
238                                                          gint                   actual_column);
239 static void             iterate_thru_children           (GtkTreeView            *tree_view,
240                                                          GtkTreeModel           *tree_model,
241                                                          GtkTreePath            *tree_path,
242                                                          GtkTreePath            *orig,
243                                                          gint                   *count,
244                                                          gint                   depth);
245 static GtkTreeIter*     return_iter_nth_row             (GtkTreeView            *tree_view,
246                                                          GtkTreeModel           *tree_model,
247                                                          GtkTreeIter            *iter,
248                                                          gint                   increment,
249                                                          gint                   row);
250 static void             free_row_info                   (GArray                 *array,
251                                                          gint                   array_idx,
252                                                          gboolean               shift);
253 static void             clean_cell_info                 (GailTreeView           *tree_view,
254                                                          GList                  *list); 
255 static void             clean_rows                      (GailTreeView           *tree_view);
256 static void             clean_cols                      (GailTreeView           *tree_view,
257                                                          GtkTreeViewColumn      *tv_col);
258 static void             traverse_cells                  (GailTreeView           *tree_view,
259                                                          GtkTreePath            *tree_path,
260                                                          gboolean               set_stale,
261                                                          gboolean               inc_row);
262 static gboolean         update_cell_value               (GailRendererCell       *renderer_cell,
263                                                          GailTreeView           *gailview,
264                                                          gboolean               emit_change_signal);
265 static void             set_cell_visibility             (GtkTreeView            *tree_view,
266                                                          GailCell               *cell,
267                                                          GtkTreeViewColumn      *tv_col,
268                                                          GtkTreePath            *tree_path,
269                                                          gboolean               emit_signal);
270 static gboolean         is_cell_showing                 (GtkTreeView            *tree_view,
271                                                          GdkRectangle           *cell_rect);
272 static void             set_expand_state                (GtkTreeView            *tree_view,
273                                                          GtkTreeModel           *tree_model,
274                                                          GailTreeView           *gailview,
275                                                          GtkTreePath            *tree_path,
276                                                          gboolean               set_on_ancestor);
277 static void             add_cell_actions                (GailCell               *cell,
278                                                          gboolean               editable);
279
280 static void             toggle_cell_expanded            (GailCell               *cell);
281 static void             toggle_cell_toggled             (GailCell               *cell);
282 static void             edit_cell                       (GailCell               *cell);
283 static void             activate_cell                   (GailCell               *cell);
284 static void             cell_destroyed                  (gpointer               data);
285 #if 0
286 static void             cell_info_remove                (GailTreeView           *tree_view, 
287                                                          GailCell               *cell);
288 #endif
289 static void             cell_info_get_index             (GtkTreeView            *tree_view, 
290                                                          GailTreeViewCellInfo   *info,
291                                                          gint                   *index);
292 static void             cell_info_new                   (GailTreeView           *gailview, 
293                                                          GtkTreeModel           *tree_model,
294                                                          GtkTreePath            *path,
295                                                          GtkTreeViewColumn      *tv_col,
296                                                          GailCell               *cell);
297 static GailCell*        find_cell                       (GailTreeView           *gailview, 
298                                                          gint                   index);
299 static void             refresh_cell_index              (GailCell               *cell);
300 static void             get_selected_rows               (GtkTreeModel           *model,
301                                                          GtkTreePath            *path,
302                                                          GtkTreeIter            *iter,
303                                                          gpointer               data);
304 static void             connect_model_signals           (GtkTreeView            *view,
305                                                          GailTreeView           *gailview); 
306 static void             disconnect_model_signals        (GailTreeView           *gailview); 
307 static void             clear_cached_data               (GailTreeView           *view);
308 static gint             get_column_number               (GtkTreeView            *tree_view,
309                                                          GtkTreeViewColumn      *column,
310                                                          gboolean               visible); 
311 static gint             get_focus_index                 (GtkTreeView            *tree_view);
312 static gint             get_index                       (GtkTreeView            *tree_view,
313                                                          GtkTreePath            *path,
314                                                          gint                   actual_column);
315 static void             count_rows                      (GtkTreeModel           *model,
316                                                          GtkTreeIter            *iter,
317                                                          GtkTreePath            *end_path,
318                                                          gint                   *count,
319                                                          gint                   level,
320                                                          gint                   depth);
321
322 static gboolean         get_next_node_with_child_at_depth 
323                                                         (GtkTreeModel           *model,
324                                                          GtkTreeIter            *iter,
325                                                          GtkTreePath            **path,
326                                                          gint                   level,
327                                                          gint                   depth);
328 static gboolean         get_next_node_with_child        (GtkTreeModel           *model,
329                                                          GtkTreePath            *path,
330                                                          GtkTreePath            **return_path);
331 static gboolean         get_tree_path_from_row_index    (GtkTreeModel           *model,
332                                                          gint                   row_index,
333                                                          GtkTreePath            **tree_path);
334 static gint             get_row_count                   (GtkTreeModel           *model);
335 static gboolean         get_path_column_from_index      (GtkTreeView            *tree_view,
336                                                          gint                   index,
337                                                          GtkTreePath            **path,
338                                                          GtkTreeViewColumn      **column);
339 static void             set_cell_expandable             (GailCell               *cell);
340
341 static GailTreeViewCellInfo* find_cell_info             (GailTreeView           *view,
342                                                          GailCell               *cell,
343                                                           GList**                list,
344                                                          gboolean                live_only);
345 static AtkObject *       get_header_from_column         (GtkTreeViewColumn      *tv_col);
346 static gboolean          idle_garbage_collect_cell_data (gpointer data);
347 static gboolean          garbage_collect_cell_data      (gpointer data);
348
349 static GQuark quark_column_desc_object = 0;
350 static GQuark quark_column_header_object = 0;
351 static gboolean editing = FALSE;
352 static const gchar* hadjustment = "hadjustment";
353 static const gchar* vadjustment = "vadjustment";
354
355 struct _GailTreeViewRowInfo
356 {
357   GtkTreeRowReference *row_ref;
358   gchar *description;
359   AtkObject *header;
360 };
361
362 struct _GailTreeViewCellInfo
363 {
364   GailCell *cell;
365   GtkTreeRowReference *cell_row_ref;
366   GtkTreeViewColumn *cell_col_ref;
367   GailTreeView *view;
368   gboolean in_use;
369 };
370
371 G_DEFINE_TYPE_WITH_CODE (GailTreeView, gail_tree_view, GAIL_TYPE_CONTAINER,
372                          G_IMPLEMENT_INTERFACE (ATK_TYPE_TABLE, atk_table_interface_init)
373                          G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init)
374                          G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, atk_component_interface_init)
375                          G_IMPLEMENT_INTERFACE (GAIL_TYPE_CELL_PARENT, gail_cell_parent_interface_init))
376
377 static void
378 gail_tree_view_class_init (GailTreeViewClass *klass)
379 {
380   AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
381   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
382   GtkAccessibleClass *accessible_class;
383   GailWidgetClass *widget_class;
384   GailContainerClass *container_class;
385
386   accessible_class = (GtkAccessibleClass*)klass;
387   widget_class = (GailWidgetClass*)klass;
388   container_class = (GailContainerClass*)klass;
389
390   class->get_n_children = gail_tree_view_get_n_children;
391   class->ref_child = gail_tree_view_ref_child;
392   class->ref_state_set = gail_tree_view_ref_state_set;
393   class->initialize = gail_tree_view_real_initialize;
394
395   widget_class->notify_gtk = gail_tree_view_real_notify_gtk;
396
397   accessible_class->connect_widget_destroyed = gail_tree_view_connect_widget_destroyed;
398
399   /*
400    * The children of a GtkTreeView are the buttons at the top of the columns
401    * we do not represent these as children so we do not want to report
402    * children added or deleted when these changed.
403    */
404   container_class->add_gtk = NULL;
405   container_class->remove_gtk = NULL;
406
407   gobject_class->finalize = gail_tree_view_finalize;
408
409   quark_column_desc_object = g_quark_from_static_string ("gtk-column-object");
410   quark_column_header_object = g_quark_from_static_string ("gtk-header-object");
411 }
412
413 static void
414 gail_tree_view_init (GailTreeView *view)
415 {
416 }
417
418 static void
419 gail_tree_view_real_initialize (AtkObject *obj,
420                                 gpointer  data)
421 {
422   GailTreeView *view;
423   GtkTreeView *tree_view;
424   GtkTreeModel *tree_model; 
425   GtkAdjustment *adj;
426   GList *tv_cols, *tmp_list;
427   GtkWidget *widget;
428
429   ATK_OBJECT_CLASS (gail_tree_view_parent_class)->initialize (obj, data);
430
431   view = GAIL_TREE_VIEW (obj);
432   view->caption = NULL;
433   view->summary = NULL;
434   view->row_data = NULL;
435   view->col_data = NULL;
436   view->cell_data = NULL;
437   view->focus_cell = NULL;
438   view->old_hadj = NULL;
439   view->old_vadj = NULL;
440   view->idle_expand_id = 0;
441   view->idle_expand_path = NULL;
442
443   view->n_children_deleted = 0;
444
445   widget = GTK_WIDGET (data);
446   g_signal_connect_after (widget,
447                           "row-collapsed",
448                           G_CALLBACK (gail_tree_view_collapse_row_gtk),
449                           NULL);
450   g_signal_connect (widget,
451                     "row-expanded",
452                     G_CALLBACK (gail_tree_view_expand_row_gtk),
453                     NULL);
454   g_signal_connect (widget,
455                     "size-allocate",
456                     G_CALLBACK (gail_tree_view_size_allocate_gtk),
457                     NULL);
458
459   tree_view = GTK_TREE_VIEW (widget);
460   tree_model = gtk_tree_view_get_model (tree_view);
461
462   /* Set up signal handling */
463
464   g_signal_connect_data (gtk_tree_view_get_selection (tree_view),
465                          "changed",
466                          (GCallback) gail_tree_view_changed_gtk,
467                          obj, NULL, 0);
468
469   g_signal_connect_data (tree_view, "columns-changed",
470     (GCallback) columns_changed, NULL, NULL, 0);
471   g_signal_connect_data (tree_view, "cursor-changed",
472     (GCallback) cursor_changed, NULL, NULL, 0);
473   g_signal_connect_data (GTK_WIDGET (tree_view), "focus-in-event",
474     (GCallback) focus_in, NULL, NULL, 0);
475   g_signal_connect_data (GTK_WIDGET (tree_view), "focus-out-event",
476     (GCallback) focus_out, NULL, NULL, 0);
477
478   view->tree_model = tree_model;
479   if (tree_model)
480     {
481       g_object_add_weak_pointer (G_OBJECT (view->tree_model), (gpointer *)&view->tree_model);
482       connect_model_signals (tree_view, view);
483
484       if (GTK_IS_TREE_STORE (tree_model))
485         obj->role = ATK_ROLE_TREE_TABLE;
486       else
487         obj->role = ATK_ROLE_TABLE;
488     }
489   else
490     {
491       obj->role = ATK_ROLE_UNKNOWN;
492     }
493
494   /* adjustment callbacks */
495
496   g_object_get (tree_view, hadjustment, &adj, NULL);
497   view->old_hadj = adj;
498   g_object_add_weak_pointer (G_OBJECT (view->old_hadj), (gpointer *)&view->old_hadj);
499   g_signal_connect (adj, 
500                     "value_changed",
501                     G_CALLBACK (adjustment_changed),
502                     tree_view);
503
504   g_object_get (tree_view, vadjustment, &adj, NULL);
505   view->old_vadj = adj;
506   g_object_add_weak_pointer (G_OBJECT (view->old_vadj), (gpointer *)&view->old_vadj);
507   g_signal_connect (adj, 
508                     "value_changed",
509                     G_CALLBACK (adjustment_changed),
510                     tree_view);
511   g_signal_connect_after (widget,
512                           "set_scroll_adjustments",
513                           G_CALLBACK (gail_tree_view_set_scroll_adjustments),
514                           NULL);
515
516   view->col_data = g_array_sized_new (FALSE, TRUE, 
517                                       sizeof(GtkTreeViewColumn *), 0);
518
519   tv_cols = gtk_tree_view_get_columns (tree_view);
520
521   for (tmp_list = tv_cols; tmp_list; tmp_list = tmp_list->next)
522     {
523       g_signal_connect_data (tmp_list->data, "notify::visible",
524        (GCallback)column_visibility_changed, 
525         tree_view, NULL, FALSE);
526       g_signal_connect_data (tmp_list->data, "destroy",
527        (GCallback)column_destroy, 
528         NULL, NULL, FALSE);
529       g_array_append_val (view->col_data, tmp_list->data);
530     }
531
532   gtk_tree_view_set_destroy_count_func (tree_view, 
533                                         destroy_count_func,
534                                         NULL, NULL);
535   g_list_free (tv_cols);
536 }
537
538 static void
539 gail_tree_view_real_notify_gtk (GObject             *obj,
540                                 GParamSpec          *pspec)
541 {
542   GtkWidget *widget;
543   AtkObject* atk_obj;
544   GtkTreeView *tree_view;
545   GailTreeView *gailview;
546   GtkAdjustment *adj;
547
548   widget = GTK_WIDGET (obj);
549   atk_obj = gtk_widget_get_accessible (widget);
550   tree_view = GTK_TREE_VIEW (widget);
551   gailview = GAIL_TREE_VIEW (atk_obj);
552
553   if (strcmp (pspec->name, "model") == 0)
554     {
555       GtkTreeModel *tree_model;
556       AtkRole role;
557
558       tree_model = gtk_tree_view_get_model (tree_view);
559       if (gailview->tree_model)
560         disconnect_model_signals (gailview);
561       clear_cached_data (gailview);
562       gailview->tree_model = tree_model;
563       /*
564        * if there is no model the GtkTreeView is probably being destroyed
565        */
566       if (tree_model)
567         {
568           g_object_add_weak_pointer (G_OBJECT (gailview->tree_model), (gpointer *)&gailview->tree_model);
569           connect_model_signals (tree_view, gailview);
570
571           if (GTK_IS_TREE_STORE (tree_model))
572             role = ATK_ROLE_TREE_TABLE;
573           else
574             role = ATK_ROLE_TABLE;
575         }
576       else
577         {
578           role = ATK_ROLE_UNKNOWN;
579         }
580       atk_object_set_role (atk_obj, role);
581       g_object_freeze_notify (G_OBJECT (atk_obj));
582       g_signal_emit_by_name (atk_obj, "model_changed");
583       g_signal_emit_by_name (atk_obj, "visible_data_changed");
584       g_object_thaw_notify (G_OBJECT (atk_obj));
585     }
586   else if (strcmp (pspec->name, hadjustment) == 0)
587     {
588       g_object_get (tree_view, hadjustment, &adj, NULL);
589       g_signal_handlers_disconnect_by_func (gailview->old_hadj, 
590                                            (gpointer) adjustment_changed,
591                                            widget);
592       gailview->old_hadj = adj;
593       g_object_add_weak_pointer (G_OBJECT (gailview->old_hadj), (gpointer *)&gailview->old_hadj);
594       g_signal_connect (adj, 
595                         "value_changed",
596                         G_CALLBACK (adjustment_changed),
597                         tree_view);
598     }
599   else if (strcmp (pspec->name, vadjustment) == 0)
600     {
601       g_object_get (tree_view, vadjustment, &adj, NULL);
602       g_signal_handlers_disconnect_by_func (gailview->old_vadj, 
603                                            (gpointer) adjustment_changed,
604                                            widget);
605       gailview->old_vadj = adj;
606       g_object_add_weak_pointer (G_OBJECT (gailview->old_hadj), (gpointer *)&gailview->old_vadj);
607       g_signal_connect (adj, 
608                         "value_changed",
609                         G_CALLBACK (adjustment_changed),
610                         tree_view);
611     }
612   else
613     GAIL_WIDGET_CLASS (gail_tree_view_parent_class)->notify_gtk (obj, pspec);
614 }
615
616 static void
617 gail_tree_view_finalize (GObject            *object)
618 {
619   GailTreeView *view = GAIL_TREE_VIEW (object);
620
621   clear_cached_data (view);
622
623   /* remove any idle handlers still pending */
624   if (view->idle_garbage_collect_id)
625     g_source_remove (view->idle_garbage_collect_id);
626   if (view->idle_cursor_changed_id)
627     g_source_remove (view->idle_cursor_changed_id);
628   if (view->idle_expand_id)
629     g_source_remove (view->idle_expand_id);
630
631   if (view->caption)
632     g_object_unref (view->caption);
633   if (view->summary)
634     g_object_unref (view->summary);
635
636   if (view->tree_model)
637     disconnect_model_signals (view);
638
639   if (view->col_data)
640     {
641       GArray *array = view->col_data;
642
643      /*
644       * No need to free the contents of the array since it
645       * just contains pointers to the GtkTreeViewColumn
646       * objects that are in the GtkTreeView.
647       */
648       g_array_free (array, TRUE);
649     }
650
651   G_OBJECT_CLASS (gail_tree_view_parent_class)->finalize (object);
652 }
653
654 static void
655 gail_tree_view_connect_widget_destroyed (GtkAccessible *accessible)
656 {
657   if (accessible->widget)
658     {
659       g_signal_connect_after (accessible->widget,
660                               "destroy",
661                               G_CALLBACK (gail_tree_view_destroyed),
662                               accessible);
663     }
664   GTK_ACCESSIBLE_CLASS (gail_tree_view_parent_class)->connect_widget_destroyed (accessible);
665 }
666
667 static void
668 gail_tree_view_destroyed (GtkWidget *widget,
669                           GtkAccessible *accessible)
670 {
671   GtkAdjustment *adj;
672   GailTreeView *gailview;
673
674   gail_return_if_fail (GTK_IS_TREE_VIEW (widget));
675
676   gailview = GAIL_TREE_VIEW (accessible);
677   adj = gailview->old_hadj;
678   if (adj)
679     g_signal_handlers_disconnect_by_func (adj, 
680                                           (gpointer) adjustment_changed,
681                                           widget);
682   adj = gailview->old_vadj;
683   if (adj)
684     g_signal_handlers_disconnect_by_func (adj, 
685                                           (gpointer) adjustment_changed,
686                                           widget);
687   if (gailview->tree_model)
688     {
689       disconnect_model_signals (gailview);
690       gailview->tree_model = NULL;
691     }
692   if (gailview->focus_cell)
693     {
694       g_object_unref (gailview->focus_cell);
695       gailview->focus_cell = NULL;
696     }
697   if (gailview->idle_expand_id) 
698     {
699       g_source_remove (gailview->idle_expand_id);
700       gailview->idle_expand_id = 0;
701     }
702 }
703
704 gint 
705 get_focus_index (GtkTreeView *tree_view)
706 {
707   GtkTreePath *focus_path;
708   GtkTreeViewColumn *focus_column;
709   gint index;
710
711   gtk_tree_view_get_cursor (tree_view, &focus_path, &focus_column);
712   if (focus_path && focus_column)
713     {
714
715       index = get_index (tree_view, focus_path,
716                          get_column_number (tree_view, focus_column, FALSE));
717     }
718   else
719     index = -1;
720
721   if (focus_path)
722     gtk_tree_path_free (focus_path);
723
724   return index;
725 }
726
727 AtkObject *
728 gail_tree_view_ref_focus_cell (GtkTreeView *tree_view)
729 {
730   /*
731    * This function returns a reference to the accessible object for the cell
732    * in the treeview which has focus, if a cell has focus.
733    */
734   AtkObject *focus_cell = NULL;
735   AtkObject *atk_obj;
736   gint focus_index;
737
738   focus_index = get_focus_index (tree_view);
739   if (focus_index >= 0)
740     {
741       atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
742       focus_cell = atk_object_ref_accessible_child (atk_obj, focus_index);
743     }
744
745   return focus_cell;
746 }
747
748 /* atkobject.h */
749
750 static gint
751 gail_tree_view_get_n_children (AtkObject *obj)
752 {
753   GtkWidget *widget;
754   GtkTreeView *tree_view;
755   GtkTreeModel *tree_model;
756   gint n_rows, n_cols;
757
758   gail_return_val_if_fail (GAIL_IS_TREE_VIEW (obj), 0);
759
760   widget = GTK_ACCESSIBLE (obj)->widget;
761   if (widget == NULL)
762     /*
763      * State is defunct
764      */
765     return 0;
766
767   tree_view = GTK_TREE_VIEW (widget);
768   tree_model = gtk_tree_view_get_model (tree_view);
769
770   /*
771    * We get the total number of rows including those which are collapsed
772    */
773   n_rows = get_row_count (tree_model);
774   /*
775    * We get the total number of columns including those which are not visible
776    */
777   n_cols = get_n_actual_columns (tree_view);
778   return (n_rows * n_cols);
779 }
780
781 static AtkObject*
782 gail_tree_view_ref_child (AtkObject *obj, 
783                           gint      i)
784 {
785   GtkWidget *widget;
786   GailTreeView *gailview;
787   GailCell *cell;
788   GtkTreeView *tree_view;
789   GtkTreeModel *tree_model; 
790   GtkCellRenderer *renderer;
791   GtkTreeIter iter;
792   GtkTreeViewColumn *tv_col;
793   GtkTreeSelection *selection;
794   GtkTreePath *path;
795   AtkRegistry *default_registry;
796   AtkObjectFactory *factory;
797   AtkObject *child;
798   AtkObject *parent;
799   GtkTreeViewColumn *expander_tv;
800   GList *renderer_list;
801   GList *l;
802   GailContainerCell *container = NULL;
803   GailRendererCell *renderer_cell;
804   gboolean is_expander, is_expanded, retval;
805   gboolean editable = FALSE;
806   gint focus_index;
807
808   g_return_val_if_fail (GAIL_IS_TREE_VIEW (obj), NULL);
809   g_return_val_if_fail (i >= 0, NULL);
810
811   widget = GTK_ACCESSIBLE (obj)->widget;
812   if (widget == NULL)
813     /*
814      * State is defunct
815      */
816     return NULL;
817
818   if (i >= gail_tree_view_get_n_children (obj))
819     return NULL;
820
821   tree_view = GTK_TREE_VIEW (widget);
822   if (i < get_n_actual_columns (tree_view))
823     {
824       tv_col = gtk_tree_view_get_column (tree_view, i);
825       child = get_header_from_column (tv_col);
826       if (child)
827         g_object_ref (child);
828       return child;
829     }
830
831   gailview = GAIL_TREE_VIEW (obj);
832   /*
833    * Check whether the child is cached
834    */
835   cell = find_cell (gailview, i);
836   if (cell)
837     {
838       g_object_ref (cell);
839       return ATK_OBJECT (cell);
840     }
841
842   if (gailview->focus_cell == NULL)
843       focus_index = get_focus_index (tree_view);
844   else
845       focus_index = -1;
846   /*
847    * Find the TreePath and GtkTreeViewColumn for the index
848    */
849   if (!get_path_column_from_index (tree_view, i, &path, &tv_col))
850     return NULL;
851  
852   tree_model = gtk_tree_view_get_model (tree_view);
853   retval = gtk_tree_model_get_iter (tree_model, &iter, path);
854   gail_return_val_if_fail (retval, NULL);
855
856   expander_tv = gtk_tree_view_get_expander_column (tree_view);
857   is_expander = FALSE;
858   is_expanded = FALSE;
859   if (gtk_tree_model_iter_has_child (tree_model, &iter))
860     {
861       if (expander_tv == tv_col)
862         {
863           is_expander = TRUE;
864           is_expanded = gtk_tree_view_row_expanded (tree_view, path);
865         }
866     } 
867   gtk_tree_view_column_cell_set_cell_data (tv_col, tree_model, &iter, 
868                                            is_expander, is_expanded);
869
870   renderer_list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
871
872   /* If there are more than one renderer in the list, make a container */
873
874   if (renderer_list && renderer_list->next)
875     {
876       GailCell *container_cell;
877
878       container = gail_container_cell_new ();
879       gail_return_val_if_fail (container, NULL);
880
881       container_cell = GAIL_CELL (container);
882       gail_cell_initialise (container_cell,
883                             widget, ATK_OBJECT (gailview), 
884                             i);
885       /*
886        * The GailTreeViewCellInfo structure for the container will be before
887        * the ones for the cells so that the first one we find for a position
888        * will be for the container
889        */
890       cell_info_new (gailview, tree_model, path, tv_col, container_cell);
891       container_cell->refresh_index = refresh_cell_index;
892       parent = ATK_OBJECT (container);
893     }
894   else
895     parent = ATK_OBJECT (gailview);
896
897   child = NULL;
898
899   /*
900    * Now we make a fake cell_renderer if there is no cell in renderer_list
901    */
902
903   if (renderer_list == NULL)
904   {
905     GtkCellRenderer *fake_renderer;
906     fake_renderer = g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
907     default_registry = atk_get_default_registry ();
908     factory = atk_registry_get_factory (default_registry,
909                                         G_OBJECT_TYPE (fake_renderer));
910     child = atk_object_factory_create_accessible (factory,
911                                                   G_OBJECT (fake_renderer));
912     gail_return_val_if_fail (GAIL_IS_RENDERER_CELL (child), NULL);
913     cell = GAIL_CELL (child);
914     renderer_cell = GAIL_RENDERER_CELL (child);
915     renderer_cell->renderer = fake_renderer;
916
917     /* Create the GailTreeViewCellInfo structure for this cell */
918     cell_info_new (gailview, tree_model, path, tv_col, cell);
919
920     gail_cell_initialise (cell,
921                           widget, parent, 
922                           i);
923
924     cell->refresh_index = refresh_cell_index;
925
926     /* set state if it is expandable */
927     if (is_expander)
928     {
929       set_cell_expandable (cell);
930       if (is_expanded)
931         gail_cell_add_state (cell, 
932                              ATK_STATE_EXPANDED,
933                              FALSE);
934     }
935   } else {
936     for (l = renderer_list; l; l = l->next)
937       {
938         renderer = GTK_CELL_RENDERER (l->data);
939
940         if (GTK_IS_CELL_RENDERER_TEXT (renderer))
941           g_object_get (G_OBJECT (renderer), "editable", &editable, NULL);
942
943         default_registry = atk_get_default_registry ();
944         factory = atk_registry_get_factory (default_registry,
945                                             G_OBJECT_TYPE (renderer));
946         child = atk_object_factory_create_accessible (factory,
947                                                       G_OBJECT (renderer));
948         gail_return_val_if_fail (GAIL_IS_RENDERER_CELL (child), NULL);
949         cell = GAIL_CELL (child);
950         renderer_cell = GAIL_RENDERER_CELL (child);
951
952         /* Create the GailTreeViewCellInfo structure for this cell */
953         cell_info_new (gailview, tree_model, path, tv_col, cell);
954
955         gail_cell_initialise (cell,
956                               widget, parent, 
957                               i);
958
959         if (container)
960           gail_container_cell_add_child (container, cell);
961         else
962           cell->refresh_index = refresh_cell_index;
963
964         update_cell_value (renderer_cell, gailview, FALSE);
965         /* Add the actions appropriate for this cell */
966         add_cell_actions (cell, editable);
967
968         /* set state if it is expandable */
969         if (is_expander)
970           {
971             set_cell_expandable (cell);
972             if (is_expanded)
973               gail_cell_add_state (cell, 
974                                    ATK_STATE_EXPANDED,
975                                    FALSE);
976           }
977         /*
978          * If the column is visible, sets the cell's state
979          */
980         if (gtk_tree_view_column_get_visible (tv_col))
981           set_cell_visibility (tree_view, cell, tv_col, path, FALSE);
982         /*
983          * If the row is selected, all cells on the row are selected
984          */
985         selection = gtk_tree_view_get_selection (tree_view);
986
987         if (gtk_tree_selection_path_is_selected (selection, path))
988           gail_cell_add_state (cell, ATK_STATE_SELECTED, FALSE);
989
990         gail_cell_add_state (cell, ATK_STATE_FOCUSABLE, FALSE);
991         if (focus_index == i)
992           {
993             gailview->focus_cell = g_object_ref (cell);
994             gail_cell_add_state (cell, ATK_STATE_FOCUSED, FALSE);
995             g_signal_emit_by_name (gailview,
996                                    "active-descendant-changed",
997                                    cell);
998           }
999       }
1000     g_list_free (renderer_list); 
1001     if (container)
1002       child =  ATK_OBJECT (container);
1003   } 
1004
1005   if (expander_tv == tv_col)
1006     {
1007       AtkRelationSet *relation_set;
1008       AtkObject *accessible_array[1];
1009       AtkRelation* relation;
1010       AtkObject *parent_node;
1011
1012       relation_set = atk_object_ref_relation_set (ATK_OBJECT (child));
1013
1014       gtk_tree_path_up (path);
1015       if (gtk_tree_path_get_depth (path) == 0)
1016         parent_node = obj;
1017       else
1018         {
1019           gint parent_index;
1020           gint n_columns;
1021
1022           n_columns = get_n_actual_columns (tree_view);
1023           parent_index = get_index (tree_view, path, i % n_columns);
1024           parent_node = atk_object_ref_accessible_child (obj, parent_index);
1025         }
1026       accessible_array[0] = parent_node;
1027       relation = atk_relation_new (accessible_array, 1,
1028                                    ATK_RELATION_NODE_CHILD_OF);
1029       atk_relation_set_add (relation_set, relation);
1030       atk_object_add_relationship (parent_node, ATK_RELATION_NODE_PARENT_OF,
1031                                    child);
1032       g_object_unref (relation);
1033       g_object_unref (relation_set);
1034     }
1035   gtk_tree_path_free (path);
1036
1037   /*
1038    * We do not increase the reference count here; when g_object_unref() is 
1039    * called for the cell then cell_destroyed() is called and
1040    * this removes the cell from the cache.
1041    */
1042   return child;
1043 }
1044
1045 static AtkStateSet*
1046 gail_tree_view_ref_state_set (AtkObject *obj)
1047 {
1048   AtkStateSet *state_set;
1049   GtkWidget *widget;
1050
1051   state_set = ATK_OBJECT_CLASS (gail_tree_view_parent_class)->ref_state_set (obj);
1052   widget = GTK_ACCESSIBLE (obj)->widget;
1053
1054   if (widget != NULL)
1055     atk_state_set_add_state (state_set, ATK_STATE_MANAGES_DESCENDANTS);
1056
1057   return state_set;
1058 }
1059
1060 /* atkcomponent.h */
1061
1062 static void
1063 atk_component_interface_init (AtkComponentIface *iface)
1064 {
1065   iface->ref_accessible_at_point = gail_tree_view_ref_accessible_at_point;
1066 }
1067
1068 static AtkObject*
1069 gail_tree_view_ref_accessible_at_point (AtkComponent           *component,
1070                                         gint                   x,
1071                                         gint                   y,
1072                                         AtkCoordType           coord_type)
1073 {
1074   GtkWidget *widget;
1075   GtkTreeView *tree_view;
1076   GtkTreePath *path;
1077   GtkTreeViewColumn *tv_column;
1078   gint x_pos, y_pos;
1079   gint bx, by;
1080   gboolean ret_val;
1081
1082   widget = GTK_ACCESSIBLE (component)->widget;
1083   if (widget == NULL)
1084     /* State is defunct */
1085     return NULL;
1086
1087   tree_view = GTK_TREE_VIEW (widget);
1088
1089   atk_component_get_extents (component, &x_pos, &y_pos, NULL, NULL, coord_type);
1090   gtk_tree_view_convert_widget_to_bin_window_coords (tree_view, x, y, &bx, &by);
1091   ret_val = gtk_tree_view_get_path_at_pos (tree_view, 
1092                                            bx - x_pos, by - y_pos, 
1093                                            &path, &tv_column, NULL, NULL);
1094   if (ret_val)
1095     {
1096       gint index, column;
1097
1098       column = get_column_number (tree_view, tv_column, FALSE);
1099       index = get_index (tree_view, path, column);
1100       gtk_tree_path_free (path);
1101
1102       return gail_tree_view_ref_child (ATK_OBJECT (component), index);
1103     } 
1104   else
1105     {
1106       g_warning ("gail_tree_view_ref_accessible_at_point: gtk_tree_view_get_path_at_pos () failed\n");
1107     }
1108   return NULL;
1109 }
1110            
1111 /* atktable.h */
1112
1113 static void 
1114 atk_table_interface_init (AtkTableIface *iface)
1115 {
1116   iface->ref_at = gail_tree_view_table_ref_at;
1117   iface->get_n_rows = gail_tree_view_get_n_rows;        
1118   iface->get_n_columns = gail_tree_view_get_n_columns;  
1119   iface->get_index_at = gail_tree_view_get_index_at;    
1120   iface->get_column_at_index = gail_tree_view_get_column_at_index;      
1121   iface->get_row_at_index = gail_tree_view_get_row_at_index;    
1122   iface->is_row_selected = gail_tree_view_is_row_selected;
1123   iface->is_selected = gail_tree_view_is_selected;
1124   iface->get_selected_rows = gail_tree_view_get_selected_rows;
1125   iface->add_row_selection = gail_tree_view_add_row_selection;
1126   iface->remove_row_selection = gail_tree_view_remove_row_selection;
1127   iface->get_column_extent_at = NULL;
1128   iface->get_row_extent_at = NULL;
1129   iface->get_row_header = gail_tree_view_get_row_header;
1130   iface->set_row_header = gail_tree_view_set_row_header;
1131   iface->get_column_header = gail_tree_view_get_column_header;
1132   iface->set_column_header = gail_tree_view_set_column_header;
1133   iface->get_caption = gail_tree_view_get_caption;
1134   iface->set_caption = gail_tree_view_set_caption;
1135   iface->get_summary = gail_tree_view_get_summary;
1136   iface->set_summary = gail_tree_view_set_summary;
1137   iface->get_row_description = gail_tree_view_get_row_description;
1138   iface->set_row_description = gail_tree_view_set_row_description;
1139   iface->get_column_description = gail_tree_view_get_column_description;
1140   iface->set_column_description = gail_tree_view_set_column_description;
1141 }
1142
1143 static gint
1144 gail_tree_view_get_index_at (AtkTable *table,
1145                              gint     row,
1146                              gint     column)
1147 {
1148   GtkWidget *widget;
1149   GtkTreeView *tree_view;
1150   gint actual_column;
1151   gint n_cols, n_rows;
1152   GtkTreeIter iter;
1153   GtkTreePath *path;
1154   gint index;
1155
1156   n_cols = atk_table_get_n_columns (table);
1157   n_rows = atk_table_get_n_rows (table);
1158
1159   if (row >= n_rows ||
1160       column >= n_cols)
1161     return -1;
1162
1163   widget = GTK_ACCESSIBLE (table)->widget;
1164   if (widget == NULL)
1165     /* State is defunct */
1166     return -1;
1167
1168   tree_view = GTK_TREE_VIEW (widget);
1169   actual_column = get_actual_column_number (tree_view, column);
1170
1171   set_iter_nth_row (tree_view, &iter, row);
1172   path = gtk_tree_model_get_path (gtk_tree_view_get_model (tree_view), &iter);
1173
1174   index = get_index (tree_view, path, actual_column);
1175   gtk_tree_path_free (path);
1176
1177   return index;
1178 }
1179
1180 static gint
1181 gail_tree_view_get_column_at_index (AtkTable *table,
1182                                     gint     index)
1183 {
1184   GtkWidget *widget;
1185   GtkTreeView *tree_view;
1186   gint n_columns;
1187
1188   widget = GTK_ACCESSIBLE (table)->widget;
1189   if (widget == NULL)
1190     /* State is defunct */
1191     return -1;
1192
1193   tree_view = GTK_TREE_VIEW (widget);
1194   n_columns = get_n_actual_columns (tree_view);
1195
1196   if (n_columns == 0)
1197     return 0;
1198   index = index % n_columns;
1199
1200   return get_visible_column_number (tree_view, index);
1201 }
1202
1203 static gint
1204 gail_tree_view_get_row_at_index (AtkTable *table,
1205                                  gint     index)
1206 {
1207   GtkWidget *widget;
1208   GtkTreeView *tree_view;
1209   GtkTreePath *path;
1210
1211   widget = GTK_ACCESSIBLE (table)->widget;
1212   if (widget == NULL)
1213     /* State is defunct */
1214     return -1;
1215
1216   tree_view = GTK_TREE_VIEW (widget);
1217   if (get_path_column_from_index (tree_view, index, &path, NULL))
1218     {
1219       gint row = get_row_from_tree_path (tree_view, path);
1220       gtk_tree_path_free (path);
1221       return row;
1222     }
1223   else
1224     return -1;
1225 }
1226
1227 static AtkObject* 
1228 gail_tree_view_table_ref_at (AtkTable *table,
1229                              gint     row, 
1230                              gint     column)
1231 {
1232   gint index;
1233
1234   index = gail_tree_view_get_index_at (table, row, column);
1235   if (index == -1)
1236     return NULL;
1237   
1238   return gail_tree_view_ref_child (ATK_OBJECT (table), index);
1239 }
1240
1241 static gint 
1242 gail_tree_view_get_n_rows (AtkTable *table)
1243 {
1244   GtkWidget *widget;
1245   GtkTreeView *tree_view;
1246   GtkTreeModel *tree_model;
1247   gint n_rows;
1248
1249   widget = GTK_ACCESSIBLE (table)->widget;
1250   if (widget == NULL)
1251     /* State is defunct */
1252     return 0;
1253
1254   tree_view = GTK_TREE_VIEW (widget);
1255   tree_model = gtk_tree_view_get_model (tree_view);
1256
1257   if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
1258    /* 
1259     * If working with a LIST store, then this is a faster way
1260     * to get the number of rows.
1261     */
1262     n_rows = gtk_tree_model_iter_n_children (tree_model, NULL);
1263   else
1264     {
1265       GtkTreePath *root_tree;
1266
1267       n_rows = 0;
1268       root_tree = gtk_tree_path_new_first ();
1269       iterate_thru_children (tree_view, tree_model,
1270                              root_tree, NULL, &n_rows, 0);
1271       gtk_tree_path_free (root_tree);
1272     }
1273
1274   return n_rows;
1275 }
1276
1277 /*
1278  * The function get_n_actual_columns returns the number of columns in the 
1279  * GtkTreeView. i.e. it include both visible and non-visible columns.
1280  */
1281 static gint 
1282 get_n_actual_columns (GtkTreeView *tree_view)
1283 {
1284   GList *columns;
1285   gint n_cols;
1286
1287   columns = gtk_tree_view_get_columns (tree_view);
1288   n_cols = g_list_length (columns);
1289   g_list_free (columns);
1290   return n_cols;
1291 }
1292
1293 static gint 
1294 gail_tree_view_get_n_columns (AtkTable *table)
1295 {
1296   GtkWidget *widget;
1297   GtkTreeView *tree_view;
1298   GtkTreeViewColumn *tv_col;
1299   gint n_cols = 0;
1300   gint i = 0;
1301
1302   widget = GTK_ACCESSIBLE (table)->widget;
1303   if (widget == NULL)
1304     /* State is defunct */
1305     return 0;
1306
1307   tree_view = GTK_TREE_VIEW (widget);
1308   tv_col = gtk_tree_view_get_column (tree_view, i);
1309
1310   while (tv_col != NULL) 
1311     {
1312       if (gtk_tree_view_column_get_visible (tv_col)) 
1313         n_cols++;
1314
1315       i++;
1316       tv_col = gtk_tree_view_get_column (tree_view, i);
1317     }
1318
1319   return n_cols;
1320 }
1321
1322 static gboolean 
1323 gail_tree_view_is_row_selected (AtkTable *table,
1324                                 gint     row)
1325 {
1326   GtkWidget *widget;
1327   GtkTreeView *tree_view;
1328   GtkTreeSelection *selection;
1329   GtkTreeIter iter;
1330
1331   widget = GTK_ACCESSIBLE (table)->widget;
1332   if (widget == NULL)
1333     /* State is defunct */
1334     return FALSE;
1335
1336   if (row < 0)
1337     return FALSE;
1338
1339   tree_view = GTK_TREE_VIEW (widget);
1340
1341   selection = gtk_tree_view_get_selection (tree_view);
1342
1343   set_iter_nth_row (tree_view, &iter, row);
1344
1345   return gtk_tree_selection_iter_is_selected (selection, &iter);
1346 }
1347
1348 static gboolean 
1349 gail_tree_view_is_selected (AtkTable *table, 
1350                             gint     row, 
1351                             gint     column)
1352 {
1353   return gail_tree_view_is_row_selected (table, row);
1354 }
1355
1356 static gint 
1357 gail_tree_view_get_selected_rows (AtkTable *table,
1358                                   gint     **rows_selected)
1359 {
1360   GtkWidget *widget;
1361   GtkTreeView *tree_view;
1362   GtkTreeModel *tree_model;
1363   GtkTreeIter iter;
1364   GtkTreeSelection *selection;
1365   GtkTreePath *tree_path;
1366   gint ret_val = 0;
1367
1368   widget = GTK_ACCESSIBLE (table)->widget;
1369   if (widget == NULL)
1370     /* State is defunct */
1371     return 0;
1372
1373   tree_view = GTK_TREE_VIEW (widget);
1374
1375   selection = gtk_tree_view_get_selection (tree_view);
1376
1377   switch (selection->type)
1378     {
1379     case GTK_SELECTION_SINGLE:
1380     case GTK_SELECTION_BROWSE:
1381       if (gtk_tree_selection_get_selected (selection, &tree_model, &iter))
1382         {
1383           gint row;
1384
1385           if (rows_selected)
1386             {
1387               *rows_selected = (gint *)g_malloc (sizeof(gint));
1388               tree_path = gtk_tree_model_get_path (tree_model, &iter);
1389               row = get_row_from_tree_path (tree_view, tree_path);
1390               gtk_tree_path_free (tree_path);
1391
1392               /* shouldn't ever happen */
1393               g_return_val_if_fail (row != -1, 0);
1394
1395               *rows_selected[0] = row;
1396             }
1397           ret_val = 1;
1398         }
1399       break;
1400     case GTK_SELECTION_MULTIPLE:
1401       {
1402         GPtrArray *array = g_ptr_array_new();
1403
1404         gtk_tree_selection_selected_foreach (selection,
1405                                              get_selected_rows,
1406                                              array);
1407         ret_val = array->len;
1408
1409         if (rows_selected && ret_val)
1410           {
1411             gint i;
1412             *rows_selected = (gint *) g_malloc (ret_val * sizeof (gint));
1413
1414             for (i = 0; i < ret_val; i++)
1415               {
1416                 gint row;
1417
1418                 tree_path = (GtkTreePath *) g_ptr_array_index (array, i);
1419                 row = get_row_from_tree_path (tree_view, tree_path);
1420                 gtk_tree_path_free (tree_path);
1421                 (*rows_selected)[i] = row;
1422               }
1423           }
1424         g_ptr_array_free (array, FALSE);
1425       }
1426       break;
1427     case GTK_SELECTION_NONE:
1428       break; 
1429     }
1430   return ret_val;
1431 }
1432
1433 static gboolean 
1434 gail_tree_view_add_row_selection (AtkTable *table, 
1435                                   gint     row)
1436 {
1437   GtkWidget *widget;
1438   GtkTreeView *tree_view;
1439   GtkTreeModel *tree_model;
1440   GtkTreeSelection *selection;
1441   GtkTreePath *tree_path;
1442   GtkTreeIter iter_to_row;
1443
1444   widget = GTK_ACCESSIBLE (table)->widget;
1445   if (widget == NULL)
1446     /* State is defunct */
1447     return FALSE;
1448   
1449   if (!gail_tree_view_is_row_selected (table, row))
1450     {
1451       tree_view = GTK_TREE_VIEW (widget);
1452       tree_model = gtk_tree_view_get_model (tree_view);
1453       selection = gtk_tree_view_get_selection (tree_view);
1454
1455       if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
1456         {
1457           tree_path = gtk_tree_path_new ();
1458           gtk_tree_path_append_index (tree_path, row);
1459           gtk_tree_selection_select_path (selection,tree_path);
1460           gtk_tree_path_free (tree_path);
1461         }
1462       else
1463         { 
1464           set_iter_nth_row (tree_view, &iter_to_row, row);
1465           if (&iter_to_row != NULL)
1466             gtk_tree_selection_select_iter (selection, &iter_to_row);
1467           else
1468             return FALSE;
1469         }
1470     }
1471
1472   return gail_tree_view_is_row_selected (table, row);
1473 }
1474
1475 static gboolean 
1476 gail_tree_view_remove_row_selection (AtkTable *table, 
1477                                      gint     row)
1478 {
1479   GtkWidget *widget;
1480   GtkTreeView *tree_view;
1481   GtkTreeSelection *selection;
1482
1483   widget = GTK_ACCESSIBLE (table)->widget;
1484   if (widget == NULL)
1485     /* State is defunct */
1486     return FALSE;
1487
1488   tree_view = GTK_TREE_VIEW (widget);
1489
1490   selection = gtk_tree_view_get_selection (tree_view);
1491
1492   if (gail_tree_view_is_row_selected (table, row)) 
1493     {
1494       gtk_tree_selection_unselect_all (selection);
1495       return TRUE;
1496     }
1497   else return FALSE;
1498 }
1499
1500 static AtkObject* 
1501 gail_tree_view_get_row_header (AtkTable *table, 
1502                                gint     row)
1503 {
1504   GailTreeViewRowInfo *row_info;
1505
1506   row_info = get_row_info (table, row);
1507   if (row_info)
1508     return row_info->header;
1509   else
1510     return NULL;
1511 }
1512
1513 static void
1514 gail_tree_view_set_row_header (AtkTable  *table, 
1515                                gint      row, 
1516                                AtkObject *header)
1517 {
1518   set_row_data (table, row, header, NULL, TRUE);
1519 }
1520
1521 static AtkObject* 
1522 gail_tree_view_get_column_header (AtkTable *table, 
1523                                   gint     in_col)
1524 {
1525   GtkWidget *widget;
1526   GtkTreeView *tree_view;
1527   GtkTreeViewColumn *tv_col;
1528
1529   widget = GTK_ACCESSIBLE (table)->widget;
1530   if (widget == NULL)
1531     /* State is defunct */
1532     return NULL;
1533
1534   tree_view = GTK_TREE_VIEW (widget);
1535   tv_col = get_column (tree_view, in_col);
1536   return get_header_from_column (tv_col);
1537 }
1538
1539 static void
1540 gail_tree_view_set_column_header (AtkTable  *table, 
1541                                   gint      in_col,
1542                                   AtkObject *header)
1543 {
1544   GtkWidget *widget;
1545   GtkTreeView *tree_view;
1546   GtkTreeViewColumn *tv_col;
1547   AtkObject *rc;
1548   AtkPropertyValues values = { NULL };
1549
1550   widget = GTK_ACCESSIBLE (table)->widget;
1551   if (widget == NULL)
1552     /* State is defunct */
1553     return;
1554
1555   tree_view = GTK_TREE_VIEW (widget);
1556   tv_col = get_column (tree_view, in_col);
1557   if (tv_col == NULL)
1558      return;
1559
1560   rc = g_object_get_qdata (G_OBJECT (tv_col),
1561                           quark_column_header_object);
1562   if (rc)
1563     g_object_unref (rc);
1564
1565   g_object_set_qdata (G_OBJECT (tv_col),
1566                         quark_column_header_object,
1567                         header);
1568   if (header)
1569     g_object_ref (header);
1570   g_value_init (&values.new_value, G_TYPE_INT);
1571   g_value_set_int (&values.new_value, in_col);
1572
1573   values.property_name = "accessible-table-column-header";
1574   g_signal_emit_by_name (table, 
1575                          "property_change::accessible-table-column-header",
1576                          &values, NULL);
1577 }
1578
1579 static AtkObject*
1580 gail_tree_view_get_caption (AtkTable    *table)
1581 {
1582   GailTreeView* obj = GAIL_TREE_VIEW (table);
1583
1584   return obj->caption;
1585 }
1586
1587 static void
1588 gail_tree_view_set_caption (AtkTable    *table,
1589                             AtkObject   *caption)
1590 {
1591   GailTreeView* obj = GAIL_TREE_VIEW (table);
1592   AtkPropertyValues values = { NULL };
1593   AtkObject *old_caption;
1594
1595   old_caption = obj->caption;
1596   obj->caption = caption;
1597   if (obj->caption)
1598     g_object_ref (obj->caption);
1599   g_value_init (&values.old_value, G_TYPE_POINTER);
1600   g_value_set_pointer (&values.old_value, old_caption);
1601   g_value_init (&values.new_value, G_TYPE_POINTER);
1602   g_value_set_pointer (&values.new_value, obj->caption);
1603
1604   values.property_name = "accessible-table-caption-object";
1605   g_signal_emit_by_name (table, 
1606                          "property_change::accessible-table-caption-object", 
1607                          &values, NULL);
1608   if (old_caption)
1609     g_object_unref (old_caption);
1610 }
1611
1612 static G_CONST_RETURN gchar*
1613 gail_tree_view_get_column_description (AtkTable   *table,
1614                                        gint       in_col)
1615 {
1616   GtkWidget *widget;
1617   GtkTreeView *tree_view;
1618   GtkTreeViewColumn *tv_col;
1619   gchar *rc;
1620
1621   widget = GTK_ACCESSIBLE (table)->widget;
1622   if (widget == NULL)
1623     /* State is defunct */
1624     return NULL;
1625
1626   tree_view = GTK_TREE_VIEW (widget);
1627   tv_col = get_column (tree_view, in_col);
1628   if (tv_col == NULL)
1629      return NULL;
1630
1631   rc = g_object_get_qdata (G_OBJECT (tv_col),
1632                            quark_column_desc_object);
1633
1634   if (rc != NULL)
1635     return rc;
1636   else
1637     {
1638       gchar *title_text;
1639
1640       g_object_get (tv_col, "title", &title_text, NULL);
1641       return title_text;
1642     }
1643 }
1644
1645 static void
1646 gail_tree_view_set_column_description (AtkTable    *table,
1647                                        gint        in_col,
1648                                        const gchar *description)
1649 {
1650   GtkWidget *widget;
1651   GtkTreeView *tree_view;
1652   GtkTreeViewColumn *tv_col;
1653   AtkPropertyValues values = { NULL };
1654
1655   widget = GTK_ACCESSIBLE (table)->widget;
1656   if (widget == NULL)
1657     /* State is defunct */
1658     return;
1659
1660   tree_view = GTK_TREE_VIEW (widget);
1661   tv_col = get_column (tree_view, in_col);
1662   if (tv_col == NULL)
1663      return;
1664
1665   g_object_set_qdata (G_OBJECT (tv_col),
1666                       quark_column_desc_object,
1667                       g_strdup (description));
1668   g_value_init (&values.new_value, G_TYPE_INT);
1669   g_value_set_int (&values.new_value, in_col);
1670
1671   values.property_name = "accessible-table-column-description";
1672   g_signal_emit_by_name (table, 
1673                          "property_change::accessible-table-column-description",
1674                          &values, NULL);
1675 }
1676
1677 static G_CONST_RETURN gchar*
1678 gail_tree_view_get_row_description (AtkTable    *table,
1679                                     gint        row)
1680 {
1681   GailTreeViewRowInfo *row_info;
1682
1683   row_info = get_row_info (table, row);
1684   if (row_info)
1685     return row_info->description;
1686   else
1687     return NULL;
1688 }
1689
1690 static void
1691 gail_tree_view_set_row_description (AtkTable    *table,
1692                                     gint        row,
1693                                     const gchar *description)
1694 {
1695   set_row_data (table, row, NULL, description, FALSE);
1696 }
1697
1698 static AtkObject*
1699 gail_tree_view_get_summary (AtkTable    *table)
1700 {
1701   GailTreeView* obj = GAIL_TREE_VIEW (table);
1702
1703   return obj->summary;
1704 }
1705
1706 static void
1707 gail_tree_view_set_summary (AtkTable    *table,
1708                             AtkObject   *accessible)
1709 {
1710   GailTreeView* obj = GAIL_TREE_VIEW (table);
1711   AtkPropertyValues values = { NULL };
1712   AtkObject *old_summary;
1713
1714   old_summary = obj->summary;
1715   obj->summary = accessible;
1716   if (obj->summary)
1717     g_object_ref (obj->summary);
1718   g_value_init (&values.old_value, G_TYPE_POINTER);
1719   g_value_set_pointer (&values.old_value, old_summary);
1720   g_value_init (&values.new_value, G_TYPE_POINTER);
1721   g_value_set_pointer (&values.new_value, obj->summary);
1722
1723   values.property_name = "accessible-table-summary";
1724   g_signal_emit_by_name (table, 
1725                          "property_change::accessible-table-ummary",
1726                          &values, NULL);
1727   if (old_summary)
1728     g_object_unref (old_summary);
1729 }
1730
1731 static void
1732 set_row_data (AtkTable    *table, 
1733               gint        row, 
1734               AtkObject   *header,
1735               const gchar *description,
1736               gboolean    is_header)
1737 {
1738   GtkWidget *widget;
1739   GtkTreeView *tree_view;
1740   GtkTreeModel *tree_model;
1741   GailTreeView* obj = GAIL_TREE_VIEW (table);
1742   GailTreeViewRowInfo* row_info;
1743   GtkTreePath *path;
1744   GtkTreeIter iter;
1745   GArray *array;
1746   gboolean found = FALSE;
1747   gint i;
1748   AtkPropertyValues values = { NULL };
1749   gchar *signal_name;
1750
1751   widget = GTK_ACCESSIBLE (table)->widget;
1752   if (widget == NULL)
1753     /* State is defunct */
1754     return;
1755
1756   tree_view = GTK_TREE_VIEW (widget);
1757   tree_model = gtk_tree_view_get_model (tree_view);
1758
1759   set_iter_nth_row (tree_view, &iter, row);
1760   path = gtk_tree_model_get_path (tree_model, &iter);
1761
1762   if (obj->row_data == NULL)
1763     obj->row_data = g_array_sized_new (FALSE, TRUE,
1764                                        sizeof(GailTreeViewRowInfo *), 0);
1765
1766   array = obj->row_data;
1767
1768   for (i = 0; i < array->len; i++)
1769     {
1770       GtkTreePath *row_path;
1771
1772       row_info = g_array_index (array, GailTreeViewRowInfo*, i);
1773       row_path = gtk_tree_row_reference_get_path (row_info->row_ref);
1774
1775       if (row_path != NULL)
1776         {
1777           if (path && gtk_tree_path_compare (row_path, path) == 0)
1778             found = TRUE;
1779
1780           gtk_tree_path_free (row_path);
1781
1782           if (found)
1783             {
1784               if (is_header)
1785                 {
1786                   if (row_info->header)
1787                     g_object_unref (row_info->header);
1788                   row_info->header = header;
1789                   if (row_info->header)
1790                     g_object_ref (row_info->header);
1791                 }
1792               else
1793                 {
1794                   g_free (row_info->description);
1795                   row_info->description = g_strdup (description);
1796                 }
1797               break;
1798             }
1799         }
1800     }
1801
1802   if (!found)
1803     {
1804       /* if not found */
1805       row_info = g_malloc (sizeof(GailTreeViewRowInfo));
1806       row_info->row_ref = gtk_tree_row_reference_new (tree_model, path);
1807       if (is_header)
1808         {
1809           row_info->header = header;
1810           if (row_info->header)
1811             g_object_ref (row_info->header);
1812           row_info->description = NULL;
1813         }
1814       else
1815         {
1816           row_info->header = NULL;
1817           row_info->description = g_strdup (description);
1818         }
1819       g_array_append_val (array, row_info);
1820     }
1821   g_value_init (&values.new_value, G_TYPE_INT);
1822   g_value_set_int (&values.new_value, row);
1823
1824   if (is_header)
1825     {
1826       values.property_name = "accessible-table-row-header";
1827       signal_name = "property_change::accessible-table-row-header";
1828     }
1829   else
1830     {
1831       values.property_name = "accessible-table-row-description";
1832       signal_name = "property-change::accessible-table-row-description";
1833     }
1834   g_signal_emit_by_name (table, 
1835                          signal_name,
1836                          &values, NULL);
1837
1838   gtk_tree_path_free (path);
1839 }
1840
1841
1842 static GailTreeViewRowInfo*
1843 get_row_info (AtkTable    *table,
1844               gint        row)
1845 {
1846   GtkWidget *widget;
1847   GtkTreeView *tree_view;
1848   GtkTreeModel *tree_model;
1849   GailTreeView* obj = GAIL_TREE_VIEW (table);
1850   GtkTreePath *path;
1851   GtkTreeIter iter;
1852   GArray *array;
1853   GailTreeViewRowInfo *rc = NULL;
1854
1855   widget = GTK_ACCESSIBLE (table)->widget;
1856   if (widget == NULL)
1857     /* State is defunct */
1858     return NULL;
1859
1860   tree_view = GTK_TREE_VIEW (widget);
1861   tree_model = gtk_tree_view_get_model (tree_view);
1862
1863   set_iter_nth_row (tree_view, &iter, row);
1864   path = gtk_tree_model_get_path (tree_model, &iter);
1865   array = obj->row_data;
1866
1867   if (array != NULL)
1868     {
1869       GailTreeViewRowInfo *row_info;
1870       GtkTreePath *row_path;
1871       gint i;
1872
1873       for (i = 0; i < array->len; i++)
1874         {
1875           row_info = g_array_index (array, GailTreeViewRowInfo*, i);
1876           row_path = gtk_tree_row_reference_get_path (row_info->row_ref);
1877           if (row_path != NULL)
1878             {
1879               if (path && gtk_tree_path_compare (row_path, path) == 0)
1880                 rc = row_info;
1881
1882               gtk_tree_path_free (row_path);
1883
1884               if (rc != NULL)
1885                 break;
1886             }
1887         }
1888     }
1889
1890   gtk_tree_path_free (path);
1891   return rc;
1892 }
1893 /* atkselection.h */
1894
1895 static void atk_selection_interface_init (AtkSelectionIface *iface)
1896 {
1897   iface->add_selection = gail_tree_view_add_selection;
1898   iface->clear_selection = gail_tree_view_clear_selection;
1899   iface->ref_selection = gail_tree_view_ref_selection;
1900   iface->get_selection_count = gail_tree_view_get_selection_count;
1901   iface->is_child_selected = gail_tree_view_is_child_selected;
1902 }
1903
1904 static gboolean
1905 gail_tree_view_add_selection (AtkSelection *selection,
1906                               gint         i)
1907 {
1908   AtkTable *table;
1909   gint n_columns;
1910   gint row;
1911
1912   table = ATK_TABLE (selection);
1913   n_columns = gail_tree_view_get_n_columns (table);
1914   if (n_columns != 1)
1915     return FALSE;
1916
1917   row = gail_tree_view_get_row_at_index (table, i);
1918   return gail_tree_view_add_row_selection (table, row);
1919 }
1920
1921 static gboolean
1922 gail_tree_view_clear_selection (AtkSelection *selection)
1923 {
1924   GtkWidget *widget;
1925   GtkTreeView *tree_view;
1926   GtkTreeSelection *tree_selection;
1927
1928   widget = GTK_ACCESSIBLE (selection)->widget;
1929   if (widget == NULL)
1930     /* State is defunct */
1931     return FALSE;
1932  
1933   tree_view = GTK_TREE_VIEW (widget);
1934
1935   tree_selection = gtk_tree_view_get_selection (tree_view);
1936   gtk_tree_selection_unselect_all (tree_selection);  
1937
1938   return TRUE;
1939 }
1940
1941 static AtkObject*  
1942 gail_tree_view_ref_selection (AtkSelection *selection, 
1943                               gint         i)
1944 {
1945   AtkTable *table;
1946   gint row;
1947   gint n_selected;
1948   gint n_columns;
1949   gint *selected;
1950
1951   table = ATK_TABLE (selection);
1952   n_columns = gail_tree_view_get_n_columns (table);
1953   n_selected = gail_tree_view_get_selected_rows (table, &selected);
1954   if (i >= n_columns * n_selected)
1955     return NULL;
1956
1957   row = selected[i / n_columns];
1958   g_free (selected);
1959
1960   return gail_tree_view_table_ref_at (table, row, i % n_columns);
1961 }
1962
1963 static gint
1964 gail_tree_view_get_selection_count (AtkSelection *selection)
1965 {
1966   AtkTable *table;
1967   gint n_selected;
1968
1969   table = ATK_TABLE (selection);
1970   n_selected = gail_tree_view_get_selected_rows (table, NULL);
1971   if (n_selected > 0)
1972     n_selected *= gail_tree_view_get_n_columns (table);
1973   return n_selected;
1974 }
1975
1976 static gboolean
1977 gail_tree_view_is_child_selected (AtkSelection *selection, 
1978                                   gint         i)
1979 {
1980   GtkWidget *widget;
1981   gint row;
1982
1983   widget = GTK_ACCESSIBLE (selection)->widget;
1984   if (widget == NULL)
1985     /* State is defunct */
1986     return FALSE;
1987
1988   row = atk_table_get_row_at_index (ATK_TABLE (selection), i);
1989
1990   return gail_tree_view_is_row_selected (ATK_TABLE (selection), row);
1991 }
1992
1993
1994 static void gail_cell_parent_interface_init (GailCellParentIface *iface)
1995 {
1996   iface->get_cell_extents = gail_tree_view_get_cell_extents;
1997   iface->get_cell_area = gail_tree_view_get_cell_area;
1998   iface->grab_focus = gail_tree_view_grab_cell_focus;
1999 }
2000
2001 static void
2002 gail_tree_view_get_cell_extents (GailCellParent *parent,
2003                                  GailCell       *cell,
2004                                  gint           *x,
2005                                  gint           *y,
2006                                  gint           *width,
2007                                  gint           *height,
2008                                  AtkCoordType   coord_type)
2009 {
2010   GtkWidget *widget;
2011   GtkTreeView *tree_view;
2012   GdkWindow *bin_window;
2013   GdkRectangle cell_rect;
2014   gint w_x, w_y;
2015
2016   widget = GTK_ACCESSIBLE (parent)->widget;
2017   if (widget == NULL)
2018     /* State is defunct */
2019     return;
2020
2021   tree_view = GTK_TREE_VIEW (widget);
2022   gail_tree_view_get_cell_area (parent, cell, &cell_rect);
2023   bin_window = gtk_tree_view_get_bin_window (tree_view);
2024   gdk_window_get_origin (bin_window, &w_x, &w_y);
2025
2026   if (coord_type == ATK_XY_WINDOW)
2027     {
2028       GdkWindow *window;
2029       gint x_toplevel, y_toplevel;
2030
2031       window = gdk_window_get_toplevel (bin_window);
2032       gdk_window_get_origin (window, &x_toplevel, &y_toplevel);
2033
2034       w_x -= x_toplevel;
2035       w_y -= y_toplevel;
2036     }
2037
2038   *width = cell_rect.width;
2039   *height = cell_rect.height;
2040   if (is_cell_showing (tree_view, &cell_rect))
2041     {
2042       *x = cell_rect.x + w_x;
2043       *y = cell_rect.y + w_y;
2044     }
2045   else
2046     {
2047       *x = G_MININT;
2048       *y = G_MININT;
2049     }
2050 }
2051
2052 #define EXTRA_EXPANDER_PADDING 4
2053
2054 static void
2055 gail_tree_view_get_cell_area (GailCellParent *parent,
2056                               GailCell       *cell,
2057                               GdkRectangle   *cell_rect)
2058 {
2059   GtkWidget *widget;
2060   GtkTreeView *tree_view;
2061   GtkTreeViewColumn *tv_col;
2062   GtkTreePath *path;
2063   AtkObject *parent_cell;
2064   GailTreeViewCellInfo *cell_info;
2065   GailCell *top_cell;
2066
2067   widget = GTK_ACCESSIBLE (parent)->widget;
2068   if (widget == NULL)
2069     /* State is defunct */
2070     return;
2071
2072   tree_view = GTK_TREE_VIEW (widget);
2073   parent_cell = atk_object_get_parent (ATK_OBJECT (cell));
2074   if (parent_cell != ATK_OBJECT (parent))
2075     {
2076       /*
2077        * GailCell is in a GailContainerCell
2078        */
2079       top_cell = GAIL_CELL (parent_cell);
2080     }
2081   else
2082     {
2083       top_cell = cell;
2084     }
2085   cell_info = find_cell_info (GAIL_TREE_VIEW (parent), top_cell, NULL, TRUE);
2086   gail_return_if_fail (cell_info);
2087   gail_return_if_fail (cell_info->cell_col_ref);
2088   gail_return_if_fail (cell_info->cell_row_ref);
2089   path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
2090   tv_col = cell_info->cell_col_ref;
2091   if (path && cell_info->in_use)
2092     {
2093       GtkTreeViewColumn *expander_column;
2094       gint focus_line_width;
2095
2096       gtk_tree_view_get_cell_area (tree_view, path, tv_col, cell_rect);
2097       expander_column = gtk_tree_view_get_expander_column (tree_view);
2098       if (expander_column == tv_col)
2099         {
2100           gint expander_size;
2101
2102           gtk_widget_style_get (widget,
2103                                 "expander_size", &expander_size,
2104                                 NULL);
2105
2106           cell_rect->x += expander_size + EXTRA_EXPANDER_PADDING;
2107           cell_rect->width -= expander_size + EXTRA_EXPANDER_PADDING;
2108         }
2109       gtk_widget_style_get (widget,
2110                             "focus-line-width", &focus_line_width,
2111                             NULL);
2112
2113       cell_rect->x += focus_line_width;
2114       cell_rect->width -= 2 * focus_line_width; 
2115
2116       gtk_tree_path_free (path);
2117
2118       /*
2119        * A column has more than one renderer so we find the position and width
2120        * of each.
2121        */
2122       if (top_cell != cell)
2123         {
2124           gint cell_index;
2125           gboolean found;
2126           gint cell_start;
2127           gint cell_width;
2128           GList *renderers;
2129           GtkCellRenderer *renderer;
2130
2131           cell_index = atk_object_get_index_in_parent (ATK_OBJECT (cell));
2132           renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
2133           renderer = g_list_nth_data (renderers, cell_index);
2134
2135           found = gtk_tree_view_column_cell_get_position (tv_col, renderer, &cell_start, &cell_width);
2136           if (found)
2137             {
2138               cell_rect->x += cell_start;
2139               cell_rect->width = cell_width;
2140             }
2141           g_list_free (renderers);
2142         }
2143
2144     }
2145 }
2146
2147 static gboolean
2148 gail_tree_view_grab_cell_focus  (GailCellParent *parent,
2149                                  GailCell       *cell)
2150 {
2151   GtkWidget *widget;
2152   GtkTreeView *tree_view;
2153   GtkTreeViewColumn *tv_col;
2154   GtkTreePath *path;
2155   AtkObject *parent_cell;
2156   AtkObject *cell_object;
2157   GailTreeViewCellInfo *cell_info;
2158   GtkCellRenderer *renderer = NULL;
2159   GtkWidget *toplevel;
2160   gint index;
2161
2162   widget = GTK_ACCESSIBLE (parent)->widget;
2163   if (widget == NULL)
2164     /* State is defunct */
2165     return FALSE;
2166
2167   tree_view = GTK_TREE_VIEW (widget);
2168
2169   cell_info = find_cell_info (GAIL_TREE_VIEW (parent), cell, NULL, TRUE);
2170   gail_return_val_if_fail (cell_info, FALSE);
2171   gail_return_val_if_fail (cell_info->cell_col_ref, FALSE);
2172   gail_return_val_if_fail (cell_info->cell_row_ref, FALSE);
2173   cell_object = ATK_OBJECT (cell);
2174   parent_cell = atk_object_get_parent (cell_object);
2175   tv_col = cell_info->cell_col_ref;
2176   if (parent_cell != ATK_OBJECT (parent))
2177     {
2178       /*
2179        * GailCell is in a GailContainerCell.
2180        * The GtkTreeViewColumn has multiple renderers; 
2181        * find the corresponding one.
2182        */
2183       GList *renderers;
2184
2185       renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
2186       if (cell_info->in_use) {
2187           index = atk_object_get_index_in_parent (cell_object);
2188           renderer = g_list_nth_data (renderers, index);
2189       }
2190       g_list_free (renderers);
2191     }
2192   path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
2193   if (path && cell_info->in_use)
2194     {
2195       if (renderer)
2196         gtk_tree_view_set_cursor_on_cell (tree_view, path, tv_col, renderer, FALSE);
2197       else
2198         gtk_tree_view_set_cursor (tree_view, path, tv_col, FALSE);
2199
2200       gtk_tree_path_free (path);
2201       gtk_widget_grab_focus (widget);
2202       toplevel = gtk_widget_get_toplevel (widget);
2203       if (gtk_widget_is_toplevel (toplevel))
2204         {
2205 #ifdef GDK_WINDOWING_X11
2206           gtk_window_present_with_time (GTK_WINDOW (toplevel), gdk_x11_get_server_time (widget->window));
2207 #else
2208           gtk_window_present (GTK_WINDOW (toplevel));
2209 #endif
2210         }
2211
2212       return TRUE;
2213     }
2214   else
2215       return FALSE; 
2216 }
2217
2218 /* signal handling */
2219
2220 static gboolean
2221 gail_tree_view_expand_row_gtk (GtkTreeView       *tree_view,
2222                                GtkTreeIter        *iter,
2223                                GtkTreePath        *path)
2224 {
2225   AtkObject *atk_obj;
2226   GailTreeView *gailview;
2227
2228   g_assert (GTK_IS_TREE_VIEW (tree_view));
2229
2230   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2231
2232   g_assert (GAIL_IS_TREE_VIEW (atk_obj));
2233
2234   gailview = GAIL_TREE_VIEW (atk_obj);
2235
2236   /*
2237    * The visible rectangle has not been updated when this signal is emitted
2238    * so we process the signal when the GTK processing is completed
2239    */
2240   /* this seems wrong since it overwrites any other pending expand handlers... */
2241   gailview->idle_expand_path = gtk_tree_path_copy (path);
2242   if (gailview->idle_expand_id)
2243     g_source_remove (gailview->idle_expand_id);
2244   gailview->idle_expand_id = gdk_threads_add_idle (idle_expand_row, gailview);
2245   return FALSE;
2246 }
2247
2248 static gint
2249 idle_expand_row (gpointer data)
2250 {
2251   GailTreeView *gailview = data;
2252   GtkTreePath *path;
2253   GtkTreeView *tree_view;
2254   GtkTreeIter iter;
2255   GtkTreeModel *tree_model;
2256   gint n_inserted, row;
2257
2258   gailview->idle_expand_id = 0;
2259
2260   path = gailview->idle_expand_path;
2261   tree_view = GTK_TREE_VIEW (GTK_ACCESSIBLE (gailview)->widget);
2262
2263   g_assert (GTK_IS_TREE_VIEW (tree_view));
2264
2265   tree_model = gtk_tree_view_get_model(tree_view);
2266   if (!tree_model)
2267     return FALSE;
2268
2269   if (!path || !gtk_tree_model_get_iter (tree_model, &iter, path))
2270     return FALSE;
2271
2272   /*
2273    * Update visibility of cells below expansion row
2274    */
2275   traverse_cells (gailview, path, FALSE, FALSE);
2276   /*
2277    * Figure out number of visible children, the following test
2278    * should not fail
2279    */
2280   if (gtk_tree_model_iter_has_child (tree_model, &iter))
2281     {
2282       GtkTreePath *path_copy;
2283
2284      /*
2285       * By passing path into this function, we find the number of
2286       * visible children of path.
2287       */
2288       path_copy = gtk_tree_path_copy (path);
2289       gtk_tree_path_append_index(path_copy, 0);
2290
2291       n_inserted = 0;
2292       iterate_thru_children (tree_view, tree_model,
2293                              path_copy, NULL, &n_inserted, 0);
2294       gtk_tree_path_free (path_copy);
2295     }
2296   else
2297     {
2298       /* We can get here if the row expanded callback deleted the row */
2299       return FALSE;
2300     }
2301
2302   /* Set expand state */
2303   set_expand_state (tree_view, tree_model, gailview, path, TRUE);
2304
2305   row = get_row_from_tree_path (tree_view, path);
2306
2307   /* shouldn't ever happen */
2308   if (row == -1)
2309     g_assert_not_reached ();
2310
2311   /* Must add 1 because the "added rows" are below the row being expanded */
2312   row += 1;
2313   
2314   g_signal_emit_by_name (gailview, "row_inserted", row, n_inserted);
2315
2316   gailview->idle_expand_path = NULL;
2317
2318   gtk_tree_path_free (path);
2319
2320   return FALSE;
2321 }
2322
2323 static gboolean
2324 gail_tree_view_collapse_row_gtk (GtkTreeView       *tree_view,
2325                                  GtkTreeIter        *iter,
2326                                  GtkTreePath        *path)
2327 {
2328   GtkTreeModel *tree_model;
2329   AtkObject *atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2330   GailTreeView *gailview = GAIL_TREE_VIEW (atk_obj);
2331   gint row;
2332
2333   tree_model = gtk_tree_view_get_model (tree_view);
2334
2335   clean_rows (gailview);
2336
2337   /*
2338    * Update visibility of cells below collapsed row
2339    */
2340   traverse_cells (gailview, path, FALSE, FALSE);
2341   /* Set collapse state */
2342   set_expand_state (tree_view, tree_model, gailview, path, FALSE);
2343
2344   gail_return_val_if_fail (gailview->n_children_deleted, FALSE);
2345   row = get_row_from_tree_path (tree_view, path);
2346   gail_return_val_if_fail (row != -1, FALSE);
2347   g_signal_emit_by_name (atk_obj, "row_deleted", row, 
2348                          gailview->n_children_deleted);
2349   gailview->n_children_deleted = 0;
2350   return FALSE;
2351 }
2352
2353 static void
2354 gail_tree_view_size_allocate_gtk (GtkWidget     *widget,
2355                                   GtkAllocation *allocation)
2356 {
2357   AtkObject *atk_obj = gtk_widget_get_accessible (widget);
2358   GailTreeView *gailview = GAIL_TREE_VIEW (atk_obj);
2359
2360   /*
2361    * If the size allocation changes, the visibility of cells may change so
2362    * update the cells visibility.
2363    */
2364   traverse_cells (gailview, NULL, FALSE, FALSE);
2365 }
2366
2367 static void
2368 gail_tree_view_set_scroll_adjustments (GtkWidget     *widget,
2369                                        GtkAdjustment *hadj,
2370                                        GtkAdjustment *vadj)
2371 {
2372   AtkObject *atk_obj = gtk_widget_get_accessible (widget);
2373   GailTreeView *gailview = GAIL_TREE_VIEW (atk_obj);
2374   GtkAdjustment *adj;
2375
2376   g_object_get (widget, hadjustment, &adj, NULL);
2377   if (gailview->old_hadj != adj)
2378      {
2379         g_signal_handlers_disconnect_by_func (gailview->old_hadj, 
2380                                               (gpointer) adjustment_changed,
2381                                               widget);
2382         gailview->old_hadj = adj;
2383         g_object_add_weak_pointer (G_OBJECT (gailview->old_hadj), (gpointer *)&gailview->old_hadj);
2384         g_signal_connect (adj, 
2385                           "value_changed",
2386                           G_CALLBACK (adjustment_changed),
2387                           widget);
2388      } 
2389   g_object_get (widget, vadjustment, &adj, NULL);
2390   if (gailview->old_vadj != adj)
2391      {
2392         g_signal_handlers_disconnect_by_func (gailview->old_vadj, 
2393                                               (gpointer) adjustment_changed,
2394                                               widget);
2395         gailview->old_vadj = adj;
2396         g_object_add_weak_pointer (G_OBJECT (gailview->old_vadj), (gpointer *)&gailview->old_vadj);
2397         g_signal_connect (adj, 
2398                           "value_changed",
2399                           G_CALLBACK (adjustment_changed),
2400                           widget);
2401      } 
2402 }
2403
2404 static void
2405 gail_tree_view_changed_gtk (GtkTreeSelection *selection,
2406                             gpointer         data)
2407 {
2408   GailTreeView *gailview;
2409   GtkTreeView *tree_view;
2410   GtkWidget *widget;
2411   GList *cell_list;
2412   GList *l;
2413   GailTreeViewCellInfo *info;
2414   GtkTreeSelection *tree_selection;
2415   GtkTreePath *path;
2416
2417   gailview = GAIL_TREE_VIEW (data);
2418   cell_list = gailview->cell_data;
2419   widget = GTK_ACCESSIBLE (gailview)->widget;
2420   if (widget == NULL)
2421     /*
2422      * destroy signal emitted for widget
2423      */
2424     return;
2425   tree_view = GTK_TREE_VIEW (widget);
2426
2427   tree_selection = gtk_tree_view_get_selection (tree_view);
2428
2429   clean_rows (gailview);
2430
2431   for (l = cell_list; l; l = l->next)
2432     {
2433       info = (GailTreeViewCellInfo *) (l->data);
2434
2435       if (info->in_use)
2436       {
2437           gail_cell_remove_state (info->cell, ATK_STATE_SELECTED, TRUE); 
2438           
2439           path = gtk_tree_row_reference_get_path (info->cell_row_ref);
2440           if (path && gtk_tree_selection_path_is_selected (tree_selection, path))
2441               gail_cell_add_state (info->cell, ATK_STATE_SELECTED, TRUE); 
2442           gtk_tree_path_free (path);
2443       }
2444     }
2445   if (GTK_WIDGET_REALIZED (widget))
2446     g_signal_emit_by_name (gailview, "selection_changed");
2447 }
2448
2449 static void
2450 columns_changed (GtkTreeView *tree_view)
2451 {
2452   AtkObject *atk_obj = gtk_widget_get_accessible (GTK_WIDGET(tree_view));
2453   GailTreeView *gailview = GAIL_TREE_VIEW (atk_obj);
2454   GList *tv_cols, *tmp_list;
2455   gboolean column_found;
2456   gboolean move_found = FALSE;
2457   gboolean stale_set = FALSE;
2458   gint column_count = 0;
2459   gint i;
2460
2461  /*
2462   * This function must determine if the change is an add, delete or
2463   * a move based upon its cache of TreeViewColumns in
2464   * gailview->col_data
2465   */
2466   tv_cols = gtk_tree_view_get_columns (tree_view);
2467
2468   /* check for adds or moves */
2469   for (tmp_list = tv_cols; tmp_list; tmp_list = tmp_list->next)
2470     {
2471       column_found = FALSE;
2472
2473       for (i = 0; i < gailview->col_data->len; i++)
2474         {
2475
2476           if ((GtkTreeViewColumn *)tmp_list->data ==
2477               (GtkTreeViewColumn *)g_array_index (gailview->col_data,
2478                GtkTreeViewColumn *, i))
2479             {
2480               column_found = TRUE;
2481
2482               /* If the column isn't in the same position, a move happened */
2483               if (!move_found && i != column_count)
2484                 {
2485                   if (!stale_set)
2486                     {
2487                       /* Set all rows to ATK_STATE_STALE */
2488                       traverse_cells (gailview, NULL, TRUE, FALSE);
2489                       stale_set = TRUE;
2490                     }
2491   
2492                   /* Just emit one column reordered signal when a move happens */
2493                   g_signal_emit_by_name (atk_obj, "column_reordered");
2494                   move_found = TRUE;
2495                 }
2496
2497               break;
2498             }
2499         }
2500
2501      /*
2502       * If column_found is FALSE, then an insert happened for column
2503       * number column_count
2504       */
2505       if (!column_found)
2506         {
2507           gint n_cols, n_rows, row;
2508
2509           if (!stale_set)
2510             {
2511               /* Set all rows to ATK_STATE_STALE */
2512               traverse_cells (gailview, NULL, TRUE, FALSE);
2513               stale_set = TRUE;
2514             }
2515
2516           /* Generate column-inserted signal */
2517           g_signal_emit_by_name (atk_obj, "column_inserted", column_count, 1);
2518
2519           /* Generate children-changed signals */
2520           n_rows = get_row_count (gtk_tree_view_get_model (tree_view));
2521           n_cols = get_n_actual_columns (tree_view);
2522           for (row = 0; row < n_rows; row++)
2523             {
2524              /*
2525               * Pass NULL as the child object, i.e. 4th argument.
2526               */
2527               g_signal_emit_by_name (atk_obj, "children_changed::add",
2528                                     ((row * n_cols) + column_count), NULL, NULL);
2529             }
2530         }
2531
2532       column_count++;
2533     }
2534
2535   /* check for deletes */
2536   for (i = 0; i < gailview->col_data->len; i++)
2537     {
2538       column_found = FALSE;
2539
2540       for (tmp_list = tv_cols; tmp_list; tmp_list = tmp_list->next)
2541         {
2542             if ((GtkTreeViewColumn *)tmp_list->data ==
2543                 (GtkTreeViewColumn *)g_array_index (gailview->col_data,
2544                  GtkTreeViewColumn *, i))
2545               {
2546                 column_found = TRUE;
2547                 break;
2548               }
2549         }
2550
2551        /*
2552         * If column_found is FALSE, then a delete happened for column
2553         * number i
2554         */
2555       if (!column_found)
2556         {
2557           gint n_rows, n_cols, row;
2558
2559           clean_cols (gailview,
2560                       (GtkTreeViewColumn *)g_array_index (gailview->col_data,
2561                       GtkTreeViewColumn *, i));
2562
2563           if (!stale_set)
2564             {
2565               /* Set all rows to ATK_STATE_STALE */
2566               traverse_cells (gailview, NULL, TRUE, FALSE);
2567               stale_set = TRUE;
2568             }
2569
2570           /* Generate column-deleted signal */
2571           g_signal_emit_by_name (atk_obj, "column_deleted", i, 1);
2572
2573           /* Generate children-changed signals */
2574           n_rows = get_row_count (gtk_tree_view_get_model (tree_view));
2575           n_cols = get_n_actual_columns (tree_view);
2576           for (row = 0; row < n_rows; row++)
2577             {
2578              /*
2579               * Pass NULL as the child object, 4th argument.
2580               */
2581               g_signal_emit_by_name (atk_obj, "children_changed::remove",
2582                                     ((row * n_cols) + column_count), NULL, NULL);
2583             }
2584         }
2585     }
2586    
2587   /* rebuild the array */
2588
2589   g_array_free (gailview->col_data, TRUE);
2590   gailview->col_data = g_array_sized_new (FALSE, TRUE,
2591     sizeof(GtkTreeViewColumn *), 0);
2592
2593   for (tmp_list = tv_cols; tmp_list; tmp_list = tmp_list->next)
2594      g_array_append_val (gailview->col_data, tmp_list->data);
2595   g_list_free (tv_cols);
2596 }
2597
2598 static void
2599 cursor_changed (GtkTreeView *tree_view)
2600 {
2601   GailTreeView *gailview;
2602
2603   gailview = GAIL_TREE_VIEW (gtk_widget_get_accessible (GTK_WIDGET (tree_view)));
2604   if (gailview->idle_cursor_changed_id != 0)
2605     return;
2606
2607   /*
2608    * We notify the focus change in a idle handler so that the processing
2609    * of the cursor change is completed when the focus handler is called.
2610    * This will allow actions to be called in the focus handler
2611    */ 
2612   gailview->idle_cursor_changed_id = gdk_threads_add_idle (idle_cursor_changed, gailview);
2613 }
2614
2615 static gint
2616 idle_cursor_changed (gpointer data)
2617 {
2618   GailTreeView *gail_tree_view = GAIL_TREE_VIEW (data);
2619   GtkTreeView *tree_view;
2620   GtkWidget *widget;
2621   AtkObject *cell;
2622
2623   gail_tree_view->idle_cursor_changed_id = 0;
2624
2625   widget = GTK_ACCESSIBLE (gail_tree_view)->widget;
2626   /*
2627    * Widget has been deleted
2628    */
2629   if (widget == NULL)
2630     return FALSE;
2631
2632   tree_view = GTK_TREE_VIEW (widget);
2633
2634   cell = gail_tree_view_ref_focus_cell (tree_view);
2635   if (cell)
2636     {
2637       if (cell != gail_tree_view->focus_cell)
2638         {
2639           if (gail_tree_view->focus_cell)
2640             {
2641               gail_cell_remove_state (GAIL_CELL (gail_tree_view->focus_cell), ATK_STATE_ACTIVE, FALSE); 
2642               gail_cell_remove_state (GAIL_CELL (gail_tree_view->focus_cell), ATK_STATE_FOCUSED, FALSE);
2643               g_object_unref (gail_tree_view->focus_cell);
2644             }
2645           gail_tree_view->focus_cell = cell;
2646
2647           if (GTK_WIDGET_HAS_FOCUS (widget))
2648             {
2649               gail_cell_add_state (GAIL_CELL (cell), ATK_STATE_ACTIVE, FALSE);
2650               gail_cell_add_state (GAIL_CELL (cell), ATK_STATE_FOCUSED, FALSE);
2651             }
2652           g_signal_emit_by_name (gail_tree_view,
2653                                  "active-descendant-changed",
2654                                  cell);
2655         }
2656       else
2657         g_object_unref (cell);
2658     }
2659
2660   return FALSE;
2661 }
2662
2663 static void
2664 focus_in (GtkWidget *widget)
2665 {
2666   GtkTreeView *tree_view;
2667   GailTreeView *gail_tree_view;
2668   AtkStateSet *state_set;
2669   AtkObject *cell;
2670
2671   tree_view = GTK_TREE_VIEW (widget);
2672   gail_tree_view = GAIL_TREE_VIEW (gtk_widget_get_accessible (widget));
2673
2674   if (gail_tree_view->focus_cell == NULL)
2675     {
2676       cell = gail_tree_view_ref_focus_cell (tree_view);
2677       if (cell)
2678         {
2679           state_set = atk_object_ref_state_set (cell);
2680           if (state_set)
2681             {
2682               if (!atk_state_set_contains_state (state_set, ATK_STATE_FOCUSED))
2683                 {
2684                   gail_cell_add_state (GAIL_CELL (cell), ATK_STATE_ACTIVE, FALSE);
2685                   gail_tree_view->focus_cell = cell;
2686                   gail_cell_add_state (GAIL_CELL (cell), ATK_STATE_FOCUSED, FALSE);
2687                   g_signal_emit_by_name (gail_tree_view,
2688                                          "active-descendant-changed",
2689                                          cell);
2690                 }
2691               g_object_unref (state_set);
2692             }
2693         }
2694     }
2695 }
2696
2697 static void
2698 focus_out (GtkWidget *widget)
2699 {
2700   GailTreeView *gail_tree_view;
2701
2702   gail_tree_view = GAIL_TREE_VIEW (gtk_widget_get_accessible (widget));
2703   if (gail_tree_view->focus_cell)
2704   {
2705     gail_cell_remove_state (GAIL_CELL (gail_tree_view->focus_cell), ATK_STATE_ACTIVE, FALSE);
2706     gail_cell_remove_state (GAIL_CELL (gail_tree_view->focus_cell), ATK_STATE_FOCUSED, FALSE);
2707     g_object_unref (gail_tree_view->focus_cell);
2708     gail_tree_view->focus_cell = NULL;
2709   }
2710 }
2711
2712 static void
2713 model_row_changed (GtkTreeModel *tree_model,
2714                    GtkTreePath  *path, 
2715                    GtkTreeIter  *iter,
2716                    gpointer     user_data)
2717 {
2718   GtkTreeView *tree_view = GTK_TREE_VIEW(user_data);
2719   GailTreeView *gailview;
2720   GtkTreePath *cell_path;
2721   GList *l;
2722   GailTreeViewCellInfo *cell_info;
2723  
2724   gailview = GAIL_TREE_VIEW (gtk_widget_get_accessible (GTK_WIDGET (tree_view)));
2725
2726   /* Loop through our cached cells */
2727   /* Must loop through them all */
2728   for (l = gailview->cell_data; l; l = l->next)
2729     {
2730       cell_info = (GailTreeViewCellInfo *) l->data;
2731       if (cell_info->in_use) 
2732       {
2733           cell_path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
2734
2735           if (cell_path != NULL)
2736           {
2737               if (path && gtk_tree_path_compare (cell_path, path) == 0)
2738               {
2739                   if (GAIL_IS_RENDERER_CELL (cell_info->cell))
2740                   {
2741                       update_cell_value (GAIL_RENDERER_CELL (cell_info->cell),
2742                                          gailview, TRUE);
2743                   }
2744               }
2745               gtk_tree_path_free (cell_path);
2746           }
2747       }
2748     }
2749   g_signal_emit_by_name (gailview, "visible-data-changed");
2750 }
2751
2752 static void
2753 column_visibility_changed (GObject    *object,
2754                            GParamSpec *pspec,
2755                            gpointer   user_data)
2756 {
2757   if (strcmp (pspec->name, "visible") == 0)
2758     {
2759       /*
2760        * A column has been made visible or invisible
2761        *
2762        * We update our cache of cells and emit model_changed signal
2763        */ 
2764       GtkTreeView *tree_view = (GtkTreeView *)user_data;
2765       GailTreeView *gailview;
2766       GList *l;
2767       GailTreeViewCellInfo *cell_info;
2768       GtkTreeViewColumn *this_col = GTK_TREE_VIEW_COLUMN (object);
2769       GtkTreeViewColumn *tv_col;
2770
2771       gailview = GAIL_TREE_VIEW (gtk_widget_get_accessible (GTK_WIDGET (tree_view))
2772 );
2773       g_signal_emit_by_name (gailview, "model_changed");
2774
2775       for (l = gailview->cell_data; l; l = l->next)
2776         {
2777           cell_info = (GailTreeViewCellInfo *) l->data;
2778           if (cell_info->in_use) 
2779           {
2780               tv_col = cell_info->cell_col_ref;
2781               if (tv_col == this_col)
2782               {
2783                   GtkTreePath *row_path;
2784                   row_path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
2785                   if (GAIL_IS_RENDERER_CELL (cell_info->cell))
2786                   {
2787                       if (gtk_tree_view_column_get_visible (tv_col))
2788                           set_cell_visibility (tree_view, 
2789                                                cell_info->cell, 
2790                                                tv_col, row_path, FALSE);
2791                       else
2792                       {
2793                           gail_cell_remove_state (cell_info->cell, 
2794                                                   ATK_STATE_VISIBLE, TRUE);
2795                           gail_cell_remove_state (cell_info->cell, 
2796                                                   ATK_STATE_SHOWING, TRUE);
2797                       }
2798                   }
2799                   gtk_tree_path_free (row_path);
2800               }
2801           }
2802         }
2803     }
2804 }
2805
2806 /*
2807  * This is the signal handler for the "destroy" signal for a GtkTreeViewColumn
2808  *
2809  * We check whether we have stored column description or column header
2810  * and if so we get rid of it.
2811  */
2812 static void
2813 column_destroy (GtkObject *obj)
2814 {
2815   GtkTreeViewColumn *tv_col = GTK_TREE_VIEW_COLUMN (obj);
2816   AtkObject *header;
2817   gchar *desc;
2818
2819   header = g_object_get_qdata (G_OBJECT (tv_col),
2820                           quark_column_header_object);
2821   if (header)
2822     g_object_unref (header);
2823   desc = g_object_get_qdata (G_OBJECT (tv_col),
2824                            quark_column_desc_object);
2825   g_free (desc); 
2826 }
2827
2828 static void
2829 model_row_inserted (GtkTreeModel *tree_model,
2830                     GtkTreePath  *path, 
2831                     GtkTreeIter  *iter, 
2832                     gpointer     user_data)
2833 {
2834   GtkTreeView *tree_view = (GtkTreeView *)user_data;
2835   GtkTreePath *path_copy;
2836   AtkObject *atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2837   GailTreeView *gailview = GAIL_TREE_VIEW (atk_obj);
2838   gint row, n_inserted, child_row;
2839
2840   if (gailview->idle_expand_id)
2841     {
2842       g_source_remove (gailview->idle_expand_id);
2843       gailview->idle_expand_id = 0;
2844
2845       /* don't do this if the insertion precedes the idle path, since it will now be invalid */
2846       if (path && gailview->idle_expand_path &&
2847           (gtk_tree_path_compare (path, gailview->idle_expand_path) > 0))
2848           set_expand_state (tree_view, tree_model, gailview, gailview->idle_expand_path, FALSE);
2849       if (gailview->idle_expand_path) 
2850           gtk_tree_path_free (gailview->idle_expand_path);
2851     }
2852   /* Check to see if row is visible */
2853   row = get_row_from_tree_path (tree_view, path);
2854
2855  /*
2856   * A row insert is not necessarily visible.  For example,
2857   * a row can be draged & dropped into another row, which
2858   * causes an insert on the model that isn't visible in the
2859   * view.  Only generate a signal if the inserted row is
2860   * visible.
2861   */
2862   if (row != -1)
2863     {
2864       GtkTreeIter iter;
2865       gint n_cols, col;
2866
2867       gtk_tree_model_get_iter (tree_model, &iter, path);
2868
2869       /* Figure out number of visible children. */
2870       if (gtk_tree_model_iter_has_child (tree_model, &iter))
2871         {
2872          /*
2873           * By passing path into this function, we find the number of
2874           * visible children of path.
2875           */
2876           n_inserted = 0;
2877           iterate_thru_children (tree_view, tree_model,
2878                                  path, NULL, &n_inserted, 0);
2879
2880           /* Must add one to include the row that is being added */
2881           n_inserted++;
2882         }
2883       else
2884       n_inserted = 1;
2885
2886       /* Set rows below the inserted row to ATK_STATE_STALE */
2887       traverse_cells (gailview, path, TRUE, TRUE);
2888
2889       /* Generate row-inserted signal */
2890       g_signal_emit_by_name (atk_obj, "row_inserted", row, n_inserted);
2891
2892       /* Generate children-changed signals */
2893       n_cols = gail_tree_view_get_n_columns (ATK_TABLE (atk_obj));
2894       for (child_row = row; child_row < (row + n_inserted); child_row++)
2895         {
2896           for (col = 0; col < n_cols; col++)
2897             {
2898              /*
2899               * Pass NULL as the child object, i.e. 4th argument
2900               */
2901               g_signal_emit_by_name (atk_obj, "children_changed::add",
2902                                     ((row * n_cols) + col), NULL, NULL);
2903             }
2904         }
2905     }
2906   else
2907     {
2908      /*
2909       * The row has been inserted inside another row.  This can
2910       * cause a row that previously couldn't be expanded to now
2911       * be expandable.
2912       */
2913       path_copy = gtk_tree_path_copy (path);
2914       gtk_tree_path_up (path_copy);
2915       set_expand_state (tree_view, tree_model, gailview, path_copy, TRUE);
2916       gtk_tree_path_free (path_copy);
2917     }
2918 }
2919
2920 static void
2921 model_row_deleted (GtkTreeModel *tree_model,
2922                    GtkTreePath  *path, 
2923                    gpointer     user_data)
2924 {
2925   GtkTreeView *tree_view;
2926   GtkTreePath *path_copy;
2927   AtkObject *atk_obj;
2928   GailTreeView *gailview;
2929   gint row, col, n_cols;
2930
2931   tree_view = (GtkTreeView *)user_data;
2932   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2933   gailview = GAIL_TREE_VIEW (atk_obj);
2934
2935   if (gailview->idle_expand_id)
2936     {
2937       g_source_remove (gailview->idle_expand_id);
2938       gtk_tree_path_free (gailview->idle_expand_path);
2939       gailview->idle_expand_id = 0;
2940     }
2941   /* Check to see if row is visible */
2942   clean_rows (gailview);
2943
2944   /* Set rows at or below the specified row to ATK_STATE_STALE */
2945   traverse_cells (gailview, path, TRUE, TRUE);
2946
2947   /*
2948    * If deleting a row with a depth > 1, then this may affect the
2949    * expansion/contraction of its parent(s).  Make sure this is
2950    * handled.
2951    */
2952   if (gtk_tree_path_get_depth (path) > 1)
2953     {
2954       path_copy = gtk_tree_path_copy (path);
2955       gtk_tree_path_up (path_copy);
2956       set_expand_state (tree_view, tree_model, gailview, path_copy, TRUE);
2957       gtk_tree_path_free (path_copy);
2958     }
2959   row = get_row_from_tree_path (tree_view, path);
2960   /*
2961    * If the row which is deleted is not visible because it is a child of
2962    * a collapsed row then row will be -1
2963    */
2964   if (row > 0)
2965     g_signal_emit_by_name (atk_obj, "row_deleted", row, 
2966                            gailview->n_children_deleted + 1);
2967   gailview->n_children_deleted = 0;
2968
2969   /* Generate children-changed signals */
2970   n_cols = get_n_actual_columns (tree_view);
2971   for (col = 0; col < n_cols; col++)
2972   {
2973     /*
2974      * Pass NULL as the child object, 4th argument.
2975      */
2976     g_signal_emit_by_name (atk_obj, "children_changed::remove",
2977                            ((row * n_cols) + col), NULL, NULL);
2978   }
2979 }
2980
2981 /* 
2982  * This function gets called when a row is deleted or when rows are
2983  * removed from the view due to a collapse event.  Note that the
2984  * count is the number of visible *children* of the deleted row,
2985  * so it does not include the row being deleted.
2986  *
2987  * As this function is called before the rows are removed we just note the
2988  * number of rows and then deal with it when we get a notification that
2989  * rows were deleted or collapsed.
2990  */
2991 static void
2992 destroy_count_func (GtkTreeView *tree_view, 
2993                     GtkTreePath *path,
2994                     gint        count,
2995                     gpointer    user_data)
2996 {
2997   AtkObject *atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2998   GailTreeView *gailview = GAIL_TREE_VIEW (atk_obj);
2999
3000   gail_return_if_fail (gailview->n_children_deleted == 0);
3001   gailview->n_children_deleted = count;
3002 }
3003
3004 static void 
3005 model_rows_reordered (GtkTreeModel *tree_model,
3006                       GtkTreePath  *path, 
3007                       GtkTreeIter  *iter,
3008                       gint         *new_order, 
3009                       gpointer     user_data)
3010 {
3011   GtkTreeView *tree_view = (GtkTreeView *)user_data;
3012   AtkObject *atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
3013   GailTreeView *gailview = GAIL_TREE_VIEW (atk_obj);
3014
3015   if (gailview->idle_expand_id)
3016     {
3017       g_source_remove (gailview->idle_expand_id);
3018       gtk_tree_path_free (gailview->idle_expand_path);
3019       gailview->idle_expand_id = 0;
3020     }
3021   traverse_cells (gailview, NULL, TRUE, FALSE);
3022
3023   g_signal_emit_by_name (atk_obj, "row_reordered");
3024 }
3025
3026 static void
3027 adjustment_changed (GtkAdjustment *adjustment, 
3028                     GtkTreeView   *tree_view)
3029 {
3030   AtkObject *atk_obj;
3031   GailTreeView* obj;
3032
3033   /*
3034    * The scrollbars have changed
3035    */
3036   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
3037   obj = GAIL_TREE_VIEW (atk_obj);
3038
3039   traverse_cells (obj, NULL, FALSE, FALSE);
3040 }
3041
3042 static void
3043 set_cell_visibility (GtkTreeView       *tree_view,
3044                      GailCell          *cell,
3045                      GtkTreeViewColumn *tv_col,
3046                      GtkTreePath       *tree_path,
3047                      gboolean          emit_signal)
3048 {
3049   GdkRectangle cell_rect;
3050
3051   /* Get these three values in tree coords */
3052   if (GTK_WIDGET_REALIZED (GTK_WIDGET (tree_view)))
3053     gtk_tree_view_get_cell_area (tree_view, tree_path, tv_col, &cell_rect);
3054   else
3055     cell_rect.height = 0;
3056
3057   if (cell_rect.height > 0)
3058     {
3059       /*
3060        * The height will be zero for a cell for which an antecedent is not 
3061        * expanded
3062        */
3063       gail_cell_add_state (cell, ATK_STATE_VISIBLE, emit_signal);
3064       if (is_cell_showing (tree_view, &cell_rect))
3065         gail_cell_add_state (cell, ATK_STATE_SHOWING, emit_signal);
3066       else
3067         gail_cell_remove_state (cell, ATK_STATE_SHOWING, emit_signal);
3068     }
3069   else
3070     {
3071       gail_cell_remove_state (cell, ATK_STATE_VISIBLE, emit_signal);
3072       gail_cell_remove_state (cell, ATK_STATE_SHOWING, emit_signal);
3073     }
3074 }
3075
3076 static gboolean 
3077 is_cell_showing (GtkTreeView   *tree_view,
3078                  GdkRectangle  *cell_rect)
3079 {
3080   GdkRectangle rect, *visible_rect;
3081   GdkRectangle rect1, *tree_cell_rect;
3082   gint bx, by;
3083   gboolean is_showing;
3084  /*
3085   * A cell is considered "SHOWING" if any part of the cell is in the visible 
3086   * area.  Other ways we could do this is by a cell's midpoint or if the cell 
3087   * is fully in the visible range.  Since we have the cell_rect x,y,width,height
3088   * of the cell, any of these is easy to compute.
3089   *
3090   * It is assumed that cell's rectangle is in widget coordinates so we
3091   * must transform to tree cordinates.
3092   */
3093   visible_rect = &rect;
3094   tree_cell_rect = &rect1;
3095   tree_cell_rect->x = cell_rect->x;
3096   tree_cell_rect->y = cell_rect->y;
3097   tree_cell_rect->width = cell_rect->width;
3098   tree_cell_rect->height = cell_rect->height;
3099
3100   gtk_tree_view_get_visible_rect (tree_view, visible_rect);
3101   gtk_tree_view_convert_tree_to_bin_window_coords (tree_view, visible_rect->x, 
3102                                                    visible_rect->y, &bx, &by);
3103
3104   if (((tree_cell_rect->x + tree_cell_rect->width) < bx) ||
3105      ((tree_cell_rect->y + tree_cell_rect->height) < by) ||
3106      (tree_cell_rect->x > (bx + visible_rect->width)) ||
3107      (tree_cell_rect->y > (by + visible_rect->height)))
3108     is_showing =  FALSE;
3109   else
3110     is_showing = TRUE;
3111
3112   return is_showing;
3113 }
3114
3115 /* Misc Public */
3116
3117 /*
3118  * This function is called when a cell's flyweight is created in
3119  * gail_tree_view_table_ref_at with emit_change_signal set to FALSE
3120  * and in model_row_changed() on receipt of "row-changed" signal when 
3121  * emit_change_signal is set to TRUE
3122  */
3123 static gboolean
3124 update_cell_value (GailRendererCell *renderer_cell,
3125                    GailTreeView     *gailview,
3126                    gboolean         emit_change_signal)
3127 {
3128   GailTreeViewCellInfo *cell_info;
3129   GtkTreeView *tree_view;
3130   GtkTreeModel *tree_model;
3131   GtkTreePath *path;
3132   GtkTreeIter iter;
3133   GList *renderers, *cur_renderer;
3134   GParamSpec *spec;
3135   GailRendererCellClass *gail_renderer_cell_class;
3136   GtkCellRendererClass *gtk_cell_renderer_class;
3137   GailCell *cell;
3138   gchar **prop_list;
3139   AtkObject *parent;
3140   gboolean is_expander, is_expanded;
3141   
3142   gail_renderer_cell_class = GAIL_RENDERER_CELL_GET_CLASS (renderer_cell);
3143   if (renderer_cell->renderer)
3144     gtk_cell_renderer_class = GTK_CELL_RENDERER_GET_CLASS (renderer_cell->renderer);
3145   else
3146     gtk_cell_renderer_class = NULL;
3147
3148   prop_list = gail_renderer_cell_class->property_list;
3149
3150   cell = GAIL_CELL (renderer_cell);
3151   cell_info = find_cell_info (gailview, cell, NULL, TRUE);
3152   gail_return_val_if_fail (cell_info, FALSE);
3153   gail_return_val_if_fail (cell_info->cell_col_ref, FALSE);
3154   gail_return_val_if_fail (cell_info->cell_row_ref, FALSE);
3155
3156   if (emit_change_signal && cell_info->in_use)
3157     {
3158       tree_view = GTK_TREE_VIEW (GTK_ACCESSIBLE (gailview)->widget);
3159       tree_model = gtk_tree_view_get_model (tree_view);
3160       path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
3161       if (path == NULL)
3162         return FALSE;
3163
3164       gtk_tree_model_get_iter (tree_model, &iter, path);
3165       is_expander = FALSE;
3166       is_expanded = FALSE;
3167       if (gtk_tree_model_iter_has_child (tree_model, &iter))
3168         {
3169           GtkTreeViewColumn *expander_tv;
3170
3171           expander_tv = gtk_tree_view_get_expander_column (tree_view);
3172           if (expander_tv == cell_info->cell_col_ref)
3173             {
3174               is_expander = TRUE;
3175               is_expanded = gtk_tree_view_row_expanded (tree_view, path);
3176             }
3177         } 
3178       gtk_tree_path_free (path);
3179       gtk_tree_view_column_cell_set_cell_data (cell_info->cell_col_ref,
3180                                   tree_model, &iter, is_expander, is_expanded);
3181     }
3182   renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_info->cell_col_ref));
3183   gail_return_val_if_fail (renderers, FALSE);
3184
3185   /*
3186    * If the cell is in a container, it's index is used to find the renderer 
3187    * in the list
3188    */
3189
3190   /*
3191    * Otherwise, we assume that the cell is represented by the first renderer 
3192    * in the list
3193    */
3194
3195   if (cell_info->in_use) {
3196       parent = atk_object_get_parent (ATK_OBJECT (cell));
3197       if (!ATK_IS_OBJECT (cell)) g_on_error_query (NULL);
3198       if (GAIL_IS_CONTAINER_CELL (parent))
3199           cur_renderer = g_list_nth (renderers, cell->index);
3200       else
3201           cur_renderer = renderers;
3202   }
3203   else {
3204       return FALSE;
3205   }
3206   
3207   gail_return_val_if_fail (cur_renderer != NULL, FALSE);
3208
3209   if (gtk_cell_renderer_class)
3210     {
3211       while (*prop_list)
3212         {
3213           spec = g_object_class_find_property
3214                            (G_OBJECT_CLASS (gtk_cell_renderer_class), *prop_list);
3215
3216           if (spec != NULL)
3217             {
3218               GValue value = { 0, };
3219
3220               g_value_init (&value, spec->value_type);
3221               g_object_get_property (cur_renderer->data, *prop_list, &value);
3222               g_object_set_property (G_OBJECT (renderer_cell->renderer),
3223                                      *prop_list, &value);
3224               g_value_unset(&value);
3225             }
3226           else
3227             g_warning ("Invalid property: %s\n", *prop_list);
3228           prop_list++;
3229         }
3230     }
3231   g_list_free (renderers);
3232   return gail_renderer_cell_update_cache (renderer_cell, emit_change_signal);
3233 }
3234
3235 static void 
3236 set_iter_nth_row (GtkTreeView *tree_view, 
3237                   GtkTreeIter *iter, 
3238                   gint        row)
3239 {
3240   GtkTreeModel *tree_model;
3241   
3242   tree_model = gtk_tree_view_get_model (tree_view);
3243   gtk_tree_model_get_iter_first (tree_model, iter);
3244   iter = return_iter_nth_row (tree_view, tree_model, iter, 0 , row);
3245 }
3246
3247 static gint 
3248 get_row_from_tree_path (GtkTreeView *tree_view,
3249                         GtkTreePath *path)
3250 {
3251   GtkTreeModel *tree_model;
3252   GtkTreePath *root_tree;
3253   gint row;
3254
3255   tree_model = gtk_tree_view_get_model (tree_view);
3256
3257   if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
3258     row = gtk_tree_path_get_indices (path)[0];
3259   else
3260     {
3261       root_tree = gtk_tree_path_new_first ();
3262       row = 0;
3263       iterate_thru_children (tree_view, tree_model, root_tree, path, &row, 0);
3264       gtk_tree_path_free (root_tree);
3265     }
3266
3267   return row;
3268 }
3269
3270 /* Misc Private */
3271
3272 /*
3273  * Get the specified GtkTreeViewColumn in the GtkTreeView.
3274  * Only visible columns are considered.
3275  */
3276 static GtkTreeViewColumn* 
3277 get_column (GtkTreeView *tree_view, 
3278             gint        in_col)
3279 {
3280   GtkTreeViewColumn *tv_col;
3281   gint n_cols = -1;
3282   gint i = 0;
3283  
3284   if (in_col < 0)
3285     {
3286        g_warning ("Request for invalid column %d\n", in_col);
3287        return NULL;
3288     }
3289
3290   tv_col = gtk_tree_view_get_column (tree_view, i);
3291
3292   while (tv_col != NULL)
3293     {
3294       if (gtk_tree_view_column_get_visible (tv_col)) 
3295         n_cols++;
3296       if (in_col == n_cols)
3297         break;
3298       tv_col = gtk_tree_view_get_column (tree_view, ++i);
3299     }
3300
3301   if (in_col != n_cols)
3302     {
3303        g_warning ("Request for invalid column %d\n", in_col);
3304        return NULL;
3305     }
3306   return tv_col;
3307 }
3308
3309 static gint
3310 get_actual_column_number (GtkTreeView *tree_view,
3311                           gint        visible_column)
3312 {
3313   GtkTreeViewColumn *tv_col;
3314   gint actual_column = 0;
3315   gint visible_columns = -1;
3316   /*
3317    * This function calculates the column number which corresponds to the
3318    * specified visible column number
3319    */
3320   tv_col = gtk_tree_view_get_column (tree_view, actual_column);
3321
3322   while (tv_col != NULL)
3323     {
3324       if (gtk_tree_view_column_get_visible (tv_col)) 
3325         visible_columns++;
3326       if (visible_columns == visible_column)
3327         return actual_column;
3328       tv_col = gtk_tree_view_get_column (tree_view, ++actual_column);
3329     }
3330   g_warning ("get_actual_column_number failed for %d\n", visible_column);
3331   return -1;
3332 }
3333
3334 static gint
3335 get_visible_column_number (GtkTreeView *tree_view,
3336                            gint        actual_column)
3337 {
3338   GtkTreeViewColumn *tv_col;
3339   gint column = 0;
3340   gint visible_columns = -1;
3341   /*
3342    * This function calculates the visible column number which corresponds to the
3343    * specified actual column number
3344    */
3345   tv_col = gtk_tree_view_get_column (tree_view, column);
3346
3347   while (tv_col != NULL)
3348     {
3349       if (gtk_tree_view_column_get_visible (tv_col)) 
3350         {
3351           visible_columns++;
3352           if (actual_column == column)
3353             return visible_columns;
3354         }
3355       else
3356         if (actual_column == column)
3357           return -1;
3358       tv_col = gtk_tree_view_get_column (tree_view, ++column);
3359     }
3360   g_warning ("get_visible_column_number failed for %d\n", actual_column);
3361   return -1;
3362 }
3363
3364 /**
3365  * Helper recursive function that returns GtkTreeIter pointer to nth row.
3366  **/
3367 static GtkTreeIter* 
3368 return_iter_nth_row(GtkTreeView  *tree_view,
3369                     GtkTreeModel *tree_model, 
3370                     GtkTreeIter  *iter, 
3371                     gint         increment,
3372                     gint         row)
3373 {
3374   GtkTreePath *current_path = gtk_tree_model_get_path (tree_model, iter);
3375   GtkTreeIter new_iter;
3376   gboolean row_expanded;
3377
3378   if (increment == row) {
3379     gtk_tree_path_free (current_path);
3380     return iter;
3381   }
3382
3383   row_expanded = gtk_tree_view_row_expanded (tree_view, current_path);
3384   gtk_tree_path_free (current_path);
3385
3386   new_iter = *iter;
3387   if ((row_expanded && gtk_tree_model_iter_children (tree_model, iter, &new_iter)) ||
3388       (gtk_tree_model_iter_next (tree_model, iter)) ||
3389       (gtk_tree_model_iter_parent (tree_model, iter, &new_iter) &&
3390           (gtk_tree_model_iter_next (tree_model, iter))))
3391     return return_iter_nth_row (tree_view, tree_model, iter,
3392       ++increment, row);
3393
3394   return NULL;          
3395 }
3396
3397 /**
3398  * Recursively called until the row specified by orig is found.
3399  *
3400  * *count will be set to the visible row number of the child
3401  * relative to the row that was initially passed in as tree_path.
3402  *
3403  * *count will be -1 if orig is not found as a child (a row that is
3404  * not visible will not be found, e.g. if the row is inside a
3405  * collapsed row).  If NULL is passed in as orig, *count will
3406  * be a count of the visible children.
3407  *
3408  * NOTE: the value for depth must be 0 when this recursive function
3409  * is initially called, or it may not function as expected.
3410  **/
3411 static void 
3412 iterate_thru_children(GtkTreeView  *tree_view,
3413                       GtkTreeModel *tree_model,
3414                       GtkTreePath  *tree_path,
3415                       GtkTreePath  *orig,
3416                       gint         *count,
3417                       gint         depth)
3418 {
3419   GtkTreeIter iter;
3420
3421   if (!gtk_tree_model_get_iter (tree_model, &iter, tree_path))
3422     return;
3423
3424   if (tree_path && orig && !gtk_tree_path_compare (tree_path, orig)) 
3425     /* Found it! */
3426     return;
3427
3428   if (tree_path && orig && gtk_tree_path_compare (tree_path, orig) > 0)
3429     {
3430       /* Past it, so return -1 */
3431       *count = -1;
3432       return;
3433     }
3434   else if (gtk_tree_view_row_expanded (tree_view, tree_path) && 
3435     gtk_tree_model_iter_has_child (tree_model, &iter)) 
3436     {
3437       (*count)++;
3438       gtk_tree_path_append_index (tree_path, 0);
3439       iterate_thru_children (tree_view, tree_model, tree_path,
3440                              orig, count, (depth + 1));
3441       return;
3442     }
3443   else if (gtk_tree_model_iter_next (tree_model, &iter)) 
3444     {
3445       (*count)++;
3446       tree_path = gtk_tree_model_get_path (tree_model, &iter);
3447        if (tree_path)
3448          {
3449            iterate_thru_children (tree_view, tree_model, tree_path,
3450                                  orig, count, depth); 
3451            gtk_tree_path_free (tree_path);
3452          }
3453       return;
3454   }
3455   else if (gtk_tree_path_up (tree_path))
3456     {
3457       GtkTreeIter temp_iter;
3458       gboolean exit_loop = FALSE;
3459       gint new_depth = depth - 1;
3460
3461       (*count)++;
3462
3463      /*
3464       * Make sure that we back up until we find a row
3465       * where gtk_tree_path_next does not return NULL.
3466       */
3467       while (!exit_loop)
3468         {
3469           if (gtk_tree_path_get_depth (tree_path) == 0)
3470               /* depth is now zero so */
3471             return;
3472           gtk_tree_path_next (tree_path);       
3473
3474           /* Verify that the next row is a valid row! */
3475           exit_loop = gtk_tree_model_get_iter (tree_model, &temp_iter, tree_path);
3476
3477           if (!exit_loop)
3478             {
3479               /* Keep going up until we find a row that has a valid next */
3480               if (gtk_tree_path_get_depth(tree_path) > 1)
3481                 {
3482                   new_depth--;
3483                   gtk_tree_path_up (tree_path);
3484                 }
3485               else
3486                 {
3487                  /*
3488                   * If depth is 1 and gtk_tree_model_get_iter returns FALSE,
3489                   * then we are at the last row, so just return.
3490                   */ 
3491                   if (orig != NULL)
3492                     *count = -1;
3493
3494                   return;
3495                 }
3496             }
3497         }
3498
3499      /*
3500       * This guarantees that we will stop when we hit the end of the
3501       * children.
3502       */
3503       if (new_depth < 0)
3504         return;
3505
3506       iterate_thru_children (tree_view, tree_model, tree_path,
3507                             orig, count, new_depth);
3508       return;
3509     }
3510
3511  /*
3512   * If it gets here, then the path wasn't found.  Situations
3513   * that would cause this would be if the path passed in is
3514   * invalid or contained within the last row, but not visible
3515   * because the last row is not expanded.  If NULL was passed
3516   * in then a row count is desired, so only set count to -1
3517   * if orig is not NULL.
3518   */
3519   if (orig != NULL)
3520     *count = -1;
3521
3522   return;
3523 }
3524
3525 static void
3526 clean_cell_info (GailTreeView *gailview,
3527                  GList        *list) 
3528 {
3529   GailTreeViewCellInfo *cell_info;
3530   GObject *obj;
3531
3532   g_assert (GAIL_IS_TREE_VIEW (gailview));
3533
3534   cell_info = list->data;
3535
3536   if (cell_info->in_use) {
3537       obj = G_OBJECT (cell_info->cell);
3538       
3539       gail_cell_add_state (cell_info->cell, ATK_STATE_DEFUNCT, FALSE);
3540       g_object_weak_unref (obj, (GWeakNotify) cell_destroyed, cell_info);
3541       cell_info->in_use = FALSE; 
3542       if (!gailview->garbage_collection_pending) {
3543           gailview->garbage_collection_pending = TRUE;
3544           g_assert (gailview->idle_garbage_collect_id == 0);
3545           gailview->idle_garbage_collect_id = 
3546             gdk_threads_add_idle (idle_garbage_collect_cell_data, gailview);
3547       }
3548   }
3549 }
3550
3551 static void 
3552 clean_rows (GailTreeView *gailview)
3553 {
3554   GArray *array;
3555
3556   /* Clean GailTreeViewRowInfo data */
3557
3558   array = gailview->row_data;
3559   if (array != NULL)
3560     {
3561       GailTreeViewRowInfo *row_info;
3562       GtkTreePath *row_path;
3563       gint i;
3564
3565      /*
3566       * Loop backwards so that calls to free_row_info
3567       * do not affect the index numbers 
3568       */
3569       for (i = (array->len - 1); i >= 0; i  --)
3570         {
3571           row_info = g_array_index (array, GailTreeViewRowInfo*, i);
3572           row_path = gtk_tree_row_reference_get_path (row_info->row_ref);
3573
3574           /* Remove any rows that have become invalid */
3575           if (row_path == NULL)
3576             free_row_info (array, i, TRUE);
3577           else
3578             gtk_tree_path_free (row_path);
3579         }
3580     }
3581
3582   /* Clean GailTreeViewCellInfo data */
3583
3584   if (gailview->cell_data != NULL)
3585     {
3586       GailTreeViewCellInfo *cell_info;
3587       GtkTreePath *row_path;
3588       GList *cur_list;
3589       GList *temp_list;
3590
3591       temp_list = gailview->cell_data;
3592
3593       /* Must loop through them all */
3594       while (temp_list != NULL)
3595         {
3596           cur_list = temp_list;
3597           cell_info = temp_list->data;
3598           temp_list = temp_list->next;
3599           row_path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
3600
3601          /*
3602           * If the cell has become invalid because the row has been removed, 
3603           * then set the cell's state to ATK_STATE_DEFUNCT and remove the cell
3604           * from gailview->cell_data.  If row_path is NULL then the row has
3605           * been removed.
3606           */
3607           if (row_path == NULL)
3608             {
3609               clean_cell_info (gailview, cur_list);
3610             }
3611           else
3612             {
3613               gtk_tree_path_free (row_path);
3614             }
3615         }
3616     }
3617 }
3618
3619 static void 
3620 clean_cols (GailTreeView      *gailview,
3621             GtkTreeViewColumn *tv_col)
3622 {
3623   /* Clean GailTreeViewCellInfo data */
3624
3625   if (gailview->cell_data != NULL)
3626     {
3627       GailTreeViewCellInfo *cell_info;
3628       GList *cur_list, *temp_list;
3629
3630       temp_list = gailview->cell_data;
3631
3632       while (temp_list != NULL)
3633         {
3634           cur_list = temp_list;
3635           cell_info = temp_list->data;
3636           temp_list = temp_list->next;
3637
3638          /*
3639           * If the cell has become invalid because the column tv_col
3640           * has been removed, then set the cell's state to ATK_STATE_DEFUNCT
3641           * and remove the cell from gailview->cell_data. 
3642           */
3643           if (cell_info->cell_col_ref == tv_col)
3644             {
3645               clean_cell_info (gailview, cur_list);
3646             }
3647         }
3648     }
3649 }
3650
3651 static gboolean
3652 idle_garbage_collect_cell_data (gpointer data)
3653 {
3654       GailTreeView *tree_view;
3655
3656       g_assert (GAIL_IS_TREE_VIEW (data));
3657       tree_view = (GailTreeView *)data;
3658
3659       /* this is the idle handler (only one instance allowed), so
3660        * we can safely delete it.
3661        */
3662       tree_view->garbage_collection_pending = FALSE;
3663       tree_view->idle_garbage_collect_id = 0;
3664
3665       tree_view->garbage_collection_pending = garbage_collect_cell_data (data);
3666
3667       /* N.B.: if for some reason another handler has re-enterantly been queued
3668        * while this handler was being serviced, it has its own gsource, therefore this handler
3669        * should always return FALSE.
3670        */
3671       return FALSE; 
3672 }
3673
3674 static gboolean
3675 garbage_collect_cell_data (gpointer data)
3676 {
3677       GailTreeView *tree_view;
3678       GList *temp_list, *list;
3679       GailTreeViewCellInfo *cell_info;
3680
3681       g_assert (GAIL_IS_TREE_VIEW (data));
3682       tree_view = (GailTreeView *)data;
3683       list = g_list_copy (tree_view->cell_data);
3684
3685       tree_view->garbage_collection_pending = FALSE;
3686       if (tree_view->idle_garbage_collect_id != 0) 
3687       {
3688           g_source_remove (tree_view->idle_garbage_collect_id);
3689           tree_view->idle_garbage_collect_id = 0;
3690       }
3691
3692       /* Must loop through them all */
3693       temp_list = list;
3694       while (temp_list != NULL)
3695       {
3696           cell_info = temp_list->data;
3697           if (!cell_info->in_use)
3698           {
3699               /* g_object_unref (cell_info->cell); */
3700               tree_view->cell_data = g_list_remove (tree_view->cell_data, 
3701                                                     cell_info);
3702               if (cell_info->cell_row_ref)
3703                   gtk_tree_row_reference_free (cell_info->cell_row_ref);
3704               g_free (cell_info);
3705           }
3706           temp_list = temp_list->next;
3707       }
3708       g_list_free (list);
3709
3710       return tree_view->garbage_collection_pending;
3711 }
3712
3713 /**
3714  * If tree_path is passed in as NULL, then all cells are acted on.
3715  * Otherwise, just act on those cells that are on a row greater than 
3716  * the specified tree_path. If inc_row is passed in as TRUE, then rows 
3717  * greater and equal to the specified tree_path are acted on.
3718  *
3719  * if set_stale is set the ATK_STATE_STALE is set on cells which are to be
3720  * acted on. 
3721  *
3722  * The function set_cell_visibility() is called on all cells to be
3723  * acted on to update the visibility of the cell.
3724  **/
3725 static void 
3726 traverse_cells (GailTreeView *tree_view,
3727                 GtkTreePath  *tree_path,
3728                 gboolean     set_stale,
3729                 gboolean     inc_row)
3730 {
3731   if (tree_view->cell_data != NULL)
3732     {
3733       GailTreeViewCellInfo *cell_info;
3734       GtkTreeView *gtk_tree_view;
3735       GList *temp_list;
3736       GtkWidget *widget;
3737
3738       g_assert (GTK_IS_ACCESSIBLE (tree_view));
3739
3740       widget = GTK_ACCESSIBLE (tree_view)->widget;
3741       if (!widget)
3742         /* Widget is being deleted */
3743         return;
3744
3745       gtk_tree_view = GTK_TREE_VIEW (widget);
3746       temp_list = tree_view->cell_data;
3747
3748       /* Must loop through them all */
3749       while (temp_list != NULL)
3750         {
3751           GtkTreePath *row_path;
3752           gboolean act_on_cell;
3753
3754           cell_info = temp_list->data;
3755           temp_list = temp_list->next;
3756
3757           if (cell_info->in_use)
3758           {
3759               row_path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
3760               g_return_if_fail (row_path != NULL);
3761               if (tree_path == NULL)
3762                   act_on_cell = TRUE;
3763               else 
3764               {
3765                   gint comparison;
3766                   
3767                   comparison =  gtk_tree_path_compare (row_path, tree_path);
3768                   if ((comparison > 0) ||
3769                       (comparison == 0 && inc_row))
3770                       act_on_cell = TRUE;
3771                   else
3772                       act_on_cell = FALSE;
3773               }
3774               if (!cell_info->in_use) g_warning ("warning: cell info destroyed during traversal");
3775               if (act_on_cell && cell_info->in_use)
3776               {
3777                   if (set_stale)
3778                       gail_cell_add_state (cell_info->cell, ATK_STATE_STALE, TRUE);
3779                   set_cell_visibility (gtk_tree_view,
3780                                        cell_info->cell,
3781                                        cell_info->cell_col_ref,
3782                                        row_path, TRUE);
3783               }
3784               gtk_tree_path_free (row_path);
3785           }
3786         }
3787     }
3788   g_signal_emit_by_name (tree_view, "visible-data-changed");
3789 }
3790
3791 static void
3792 free_row_info (GArray   *array,
3793                gint     array_idx,
3794                gboolean shift)
3795 {
3796   GailTreeViewRowInfo* obj;
3797
3798   obj = g_array_index (array, GailTreeViewRowInfo*, array_idx);
3799
3800   g_free (obj->description);
3801   if (obj->row_ref != NULL)
3802     gtk_tree_row_reference_free (obj->row_ref);
3803   if (obj->header)
3804     g_object_unref (obj->header);
3805   g_free (obj);
3806
3807   if (shift)
3808     g_array_remove_index (array, array_idx);
3809 }
3810
3811 /*
3812  * If the tree_path passed in has children, then
3813  * ATK_STATE_EXPANDABLE is set.  If the row is expanded
3814  * ATK_STATE_EXPANDED is turned on.  If the row is 
3815  * collapsed, then ATK_STATE_EXPANDED is removed.
3816  * 
3817  * If the tree_path passed in has no children, then
3818  * ATK_STATE_EXPANDABLE and ATK_STATE_EXPANDED are removed.
3819  *
3820  * If set_on_ancestor is TRUE, then this function will also
3821  * update all cells that are ancestors of the tree_path.
3822  */
3823 static void
3824 set_expand_state (GtkTreeView  *tree_view,
3825                   GtkTreeModel *tree_model,
3826                   GailTreeView *gailview,
3827                   GtkTreePath  *tree_path,
3828                   gboolean     set_on_ancestor)
3829 {
3830   if (gailview->cell_data != NULL)
3831     {
3832       GtkTreeViewColumn *expander_tv;
3833       GailTreeViewCellInfo *cell_info;
3834       GList *temp_list;
3835       GtkTreePath *cell_path;
3836       GtkTreeIter iter;
3837       gboolean found;
3838
3839       temp_list = gailview->cell_data;
3840
3841       while (temp_list != NULL)
3842         {
3843           cell_info = temp_list->data;
3844           temp_list = temp_list->next;
3845           if (cell_info->in_use)
3846           {
3847               cell_path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
3848               found = FALSE;
3849               
3850               if (cell_path != NULL)
3851               {
3852                   GailCell *cell  = GAIL_CELL (cell_info->cell);
3853                   
3854                   expander_tv = gtk_tree_view_get_expander_column (tree_view);
3855                   
3856                   /*
3857                    * Only set state for the cell that is in the column with the
3858                    * expander toggle
3859                    */
3860                   if (expander_tv == cell_info->cell_col_ref)
3861                   {
3862                       if (tree_path && gtk_tree_path_compare (cell_path, tree_path) == 0)
3863                           found = TRUE;
3864                       else if (set_on_ancestor &&
3865                                gtk_tree_path_get_depth (cell_path) <
3866                                gtk_tree_path_get_depth (tree_path) && 
3867                                gtk_tree_path_is_ancestor (cell_path, tree_path) == 1)
3868                           /* Only set if set_on_ancestor was passed in as TRUE */
3869                           found = TRUE;
3870                   }
3871                   
3872                   /*
3873                    * Set ATK_STATE_EXPANDABLE and ATK_STATE_EXPANDED
3874                    * for ancestors and found cells.
3875                    */
3876                   if (found)
3877                   {
3878                       /*
3879                        * Must check against cell_path since cell_path
3880                        * can be equal to or an ancestor of tree_path.
3881                        */
3882                       gtk_tree_model_get_iter (tree_model, &iter, cell_path);
3883                       
3884                       /* Set or unset ATK_STATE_EXPANDABLE as appropriate */
3885                       if (gtk_tree_model_iter_has_child (tree_model, &iter)) 
3886                       {
3887                           set_cell_expandable (cell);
3888                           
3889                           if (gtk_tree_view_row_expanded (tree_view, cell_path))
3890                               gail_cell_add_state (cell, ATK_STATE_EXPANDED, TRUE);
3891                           else
3892                               gail_cell_remove_state (cell, 
3893                                                       ATK_STATE_EXPANDED, TRUE);
3894                       }
3895                       else
3896                       {
3897                           gail_cell_remove_state (cell, 
3898                                                   ATK_STATE_EXPANDED, TRUE);
3899                           if (gail_cell_remove_state (cell,
3900                                                       ATK_STATE_EXPANDABLE, TRUE))
3901                               /* The state may have been propagated to the container cell */
3902                               if (!GAIL_IS_CONTAINER_CELL (cell))
3903                                   gail_cell_remove_action_by_name (cell,
3904                                                                    "expand or contract");
3905                       }
3906                       
3907                       /*
3908                        * We assume that each cell in the cache once and
3909                        * a container cell is before its child cells so we are 
3910                        * finished if set_on_ancestor is not set to TRUE.
3911                        */
3912                       if (!set_on_ancestor)
3913                           break;
3914                   }
3915               }
3916               gtk_tree_path_free (cell_path);
3917           }
3918         }
3919     }
3920 }
3921
3922
3923 static void
3924 add_cell_actions (GailCell *cell,
3925                   gboolean editable)
3926 {
3927   if (GAIL_IS_BOOLEAN_CELL (cell))
3928     gail_cell_add_action (cell,
3929         "toggle",
3930         "toggles the cell", /* action description */
3931         NULL,
3932         toggle_cell_toggled);
3933   if (editable)
3934     gail_cell_add_action (cell,
3935         "edit",
3936         "creates a widget in which the contents of the cell can be edited", 
3937         NULL,
3938         edit_cell);
3939   gail_cell_add_action (cell,
3940         "activate",
3941         "activate the cell", 
3942         NULL,
3943         activate_cell);
3944 }
3945
3946 static void
3947 toggle_cell_expanded (GailCell *cell)
3948 {
3949   GailTreeViewCellInfo *cell_info;
3950   GtkTreeView *tree_view;
3951   GtkTreePath *path;
3952   AtkObject *parent;
3953   AtkStateSet *stateset;
3954   
3955   parent = atk_object_get_parent (ATK_OBJECT (cell));
3956   if (GAIL_IS_CONTAINER_CELL (parent))
3957     parent = atk_object_get_parent (parent);
3958
3959   cell_info = find_cell_info (GAIL_TREE_VIEW (parent), cell, NULL, TRUE);
3960   gail_return_if_fail (cell_info);
3961   gail_return_if_fail (cell_info->cell_col_ref);
3962   gail_return_if_fail (cell_info->cell_row_ref);
3963
3964   tree_view = GTK_TREE_VIEW (GTK_ACCESSIBLE (parent)->widget);
3965   path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
3966   gail_return_if_fail (path);
3967
3968   stateset = atk_object_ref_state_set (ATK_OBJECT (cell));
3969   if (atk_state_set_contains_state (stateset, ATK_STATE_EXPANDED))
3970     gtk_tree_view_collapse_row (tree_view, path);
3971   else
3972     gtk_tree_view_expand_row (tree_view, path, TRUE);
3973   g_object_unref (stateset);
3974   gtk_tree_path_free (path);
3975   return;
3976 }
3977
3978 static void
3979 toggle_cell_toggled (GailCell *cell)
3980 {
3981   GailTreeViewCellInfo *cell_info;
3982   GtkTreeView *tree_view;
3983   GtkTreePath *path;
3984   gchar *pathstring;
3985   GList *renderers, *cur_renderer;
3986   AtkObject *parent;
3987   gboolean is_container_cell = FALSE;
3988
3989   parent = atk_object_get_parent (ATK_OBJECT (cell));
3990   if (GAIL_IS_CONTAINER_CELL (parent))
3991     {
3992       is_container_cell = TRUE;
3993       parent = atk_object_get_parent (parent);
3994     }
3995
3996   cell_info = find_cell_info (GAIL_TREE_VIEW (parent), cell, NULL, TRUE);
3997   gail_return_if_fail (cell_info);
3998   gail_return_if_fail (cell_info->cell_col_ref);
3999   gail_return_if_fail (cell_info->cell_row_ref);
4000
4001   tree_view = GTK_TREE_VIEW (GTK_ACCESSIBLE (parent)->widget);
4002   path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
4003   gail_return_if_fail (path);
4004   pathstring = gtk_tree_path_to_string (path);
4005
4006   renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_info->cell_col_ref));
4007   gail_return_if_fail (renderers);
4008
4009   /* 
4010    * if the cell is in a container, it's index is used to find the 
4011    * renderer in the list
4012    */
4013
4014   if (is_container_cell)
4015     cur_renderer = g_list_nth (renderers, cell->index);
4016   else
4017   /*
4018    * Otherwise, we assume that the cell is represented by the first 
4019    * renderer in the list 
4020    */
4021     cur_renderer = renderers;
4022
4023   gail_return_if_fail (cur_renderer);
4024
4025   g_signal_emit_by_name (cur_renderer->data, "toggled", pathstring);
4026   g_list_free (renderers);
4027   g_free (pathstring);
4028   gtk_tree_path_free (path);
4029   return;
4030 }
4031
4032 static void
4033 edit_cell (GailCell *cell)
4034 {
4035   GailTreeViewCellInfo *cell_info;
4036   GtkTreeView *tree_view;
4037   GtkTreePath *path;
4038   AtkObject *parent;
4039   gboolean is_container_cell = FALSE;
4040
4041   editing = TRUE;
4042   parent = atk_object_get_parent (ATK_OBJECT (cell));
4043   if (GAIL_IS_CONTAINER_CELL (parent))
4044     {
4045       is_container_cell = TRUE;
4046       parent = atk_object_get_parent (parent);
4047     }
4048
4049   cell_info = find_cell_info (GAIL_TREE_VIEW (parent), cell, NULL, TRUE);
4050   gail_return_if_fail (cell_info);
4051   gail_return_if_fail (cell_info->cell_col_ref);
4052   gail_return_if_fail (cell_info->cell_row_ref);
4053
4054   tree_view = GTK_TREE_VIEW (GTK_ACCESSIBLE (parent)->widget);
4055   path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
4056   gail_return_if_fail (path);
4057   gtk_tree_view_set_cursor (tree_view, path, cell_info->cell_col_ref, TRUE);
4058   gtk_tree_path_free (path);
4059   return;
4060 }
4061
4062 static void
4063 activate_cell (GailCell *cell)
4064 {
4065   GailTreeViewCellInfo *cell_info;
4066   GtkTreeView *tree_view;
4067   GtkTreePath *path;
4068   AtkObject *parent;
4069   gboolean is_container_cell = FALSE;
4070
4071   editing = TRUE;
4072   parent = atk_object_get_parent (ATK_OBJECT (cell));
4073   if (GAIL_IS_CONTAINER_CELL (parent))
4074     {
4075       is_container_cell = TRUE;
4076       parent = atk_object_get_parent (parent);
4077     }
4078
4079   cell_info = find_cell_info (GAIL_TREE_VIEW (parent), cell, NULL, TRUE);
4080   gail_return_if_fail (cell_info);
4081   gail_return_if_fail (cell_info->cell_col_ref);
4082   gail_return_if_fail (cell_info->cell_row_ref);
4083
4084   tree_view = GTK_TREE_VIEW (GTK_ACCESSIBLE (parent)->widget);
4085   path = gtk_tree_row_reference_get_path (cell_info->cell_row_ref);
4086   gail_return_if_fail (path);
4087   gtk_tree_view_row_activated (tree_view, path, cell_info->cell_col_ref);
4088   gtk_tree_path_free (path);
4089   return;
4090 }
4091
4092 static void
4093 cell_destroyed (gpointer data)
4094 {
4095   GailTreeViewCellInfo *cell_info = data;
4096
4097   gail_return_if_fail (cell_info);
4098   if (cell_info->in_use) {
4099       cell_info->in_use = FALSE;
4100
4101       g_assert (GAIL_IS_TREE_VIEW (cell_info->view));
4102       if (!cell_info->view->garbage_collection_pending) {
4103           cell_info->view->garbage_collection_pending = TRUE;
4104           cell_info->view->idle_garbage_collect_id =
4105             gdk_threads_add_idle (idle_garbage_collect_cell_data, cell_info->view);
4106       }
4107   }
4108 }
4109
4110 #if 0
4111 static void
4112 cell_info_remove (GailTreeView *tree_view, 
4113                   GailCell     *cell)
4114 {
4115   GailTreeViewCellInfo *info;
4116   GList *temp_list;
4117
4118   info = find_cell_info (tree_view, cell, &temp_list, FALSE);
4119   if (info)
4120     {
4121       info->in_use = FALSE;
4122       return;
4123     }
4124   g_warning ("No cell removed in cell_info_remove\n");
4125 }
4126 #endif
4127
4128 static void
4129 cell_info_get_index (GtkTreeView            *tree_view, 
4130                      GailTreeViewCellInfo   *info,
4131                      gint                   *index)
4132 {
4133   GtkTreePath *path;
4134   gint column_number;
4135
4136   path = gtk_tree_row_reference_get_path (info->cell_row_ref);
4137   gail_return_if_fail (path);
4138
4139   column_number = get_column_number (tree_view, info->cell_col_ref, FALSE);
4140   *index = get_index (tree_view, path, column_number);
4141   gtk_tree_path_free (path);
4142 }
4143
4144 static void
4145 cell_info_new (GailTreeView      *gailview, 
4146                GtkTreeModel      *tree_model, 
4147                GtkTreePath       *path,
4148                GtkTreeViewColumn *tv_col,
4149                GailCell          *cell )
4150 {
4151   GailTreeViewCellInfo *cell_info;
4152
4153   g_assert (GAIL_IS_TREE_VIEW (gailview));
4154
4155   cell_info = g_new (GailTreeViewCellInfo, 1);
4156   cell_info->cell_row_ref = gtk_tree_row_reference_new (tree_model, path);
4157
4158   cell_info->cell_col_ref = tv_col;
4159   cell_info->cell = cell;
4160   cell_info->in_use = TRUE; /* if we've created it, assume it's in use */
4161   cell_info->view = gailview;
4162   gailview->cell_data = g_list_append (gailview->cell_data, cell_info);
4163       
4164   /* Setup weak reference notification */
4165
4166   g_object_weak_ref (G_OBJECT (cell),
4167                      (GWeakNotify) cell_destroyed,
4168                      cell_info);
4169 }
4170
4171 static GailCell*
4172 find_cell (GailTreeView *gailview, 
4173            gint         index)
4174 {
4175   GailTreeViewCellInfo *info;
4176   GtkTreeView *tree_view;
4177   GList *cell_list;
4178   GList *l;
4179   gint real_index;
4180   gboolean needs_cleaning = FALSE;
4181   GailCell *retval = NULL;
4182
4183   tree_view = GTK_TREE_VIEW (GTK_ACCESSIBLE (gailview)->widget);
4184   cell_list = gailview->cell_data;
4185
4186   for (l = cell_list; l; l = l->next)
4187     {
4188       info = (GailTreeViewCellInfo *) (l->data);
4189       if (info->in_use)
4190       {
4191           cell_info_get_index (tree_view, info, &real_index);
4192           if (index == real_index)
4193           {
4194               retval =  info->cell;
4195               break;
4196           }
4197       }
4198       else
4199       {
4200           needs_cleaning = TRUE;
4201       }
4202     }
4203   if (needs_cleaning)
4204      garbage_collect_cell_data (gailview);
4205
4206   return retval;
4207 }
4208
4209 static void
4210 refresh_cell_index (GailCell *cell)
4211 {
4212   GailTreeViewCellInfo *info;
4213   AtkObject *parent;
4214   GtkTreeView *tree_view;
4215   gint index;
4216
4217   parent = atk_object_get_parent (ATK_OBJECT (cell));
4218   gail_return_if_fail (GAIL_IS_TREE_VIEW (parent));
4219
4220   tree_view = GTK_TREE_VIEW (GTK_ACCESSIBLE (parent)->widget);
4221
4222   /* Find this cell in the GailTreeView's cache */
4223
4224   info = find_cell_info (GAIL_TREE_VIEW (parent), cell, NULL, TRUE);
4225   gail_return_if_fail (info);
4226   
4227   cell_info_get_index (tree_view, info, &index); 
4228   cell->index = index;
4229 }
4230
4231 static void
4232 get_selected_rows (GtkTreeModel *model,
4233                    GtkTreePath  *path,
4234                    GtkTreeIter  *iter,
4235                    gpointer     data)
4236 {
4237   GPtrArray *array = (GPtrArray *)data;
4238
4239   g_ptr_array_add (array, gtk_tree_path_copy (path));
4240 }
4241
4242 static void
4243 connect_model_signals (GtkTreeView  *view,
4244                        GailTreeView *gailview)
4245 {
4246   GObject *obj;
4247
4248   obj = G_OBJECT (gailview->tree_model);
4249   g_signal_connect_data (obj, "row-changed",
4250                          (GCallback) model_row_changed, view, NULL, 0);
4251   g_signal_connect_data (obj, "row-inserted",
4252                          (GCallback) model_row_inserted, view, NULL, 
4253                          G_CONNECT_AFTER);
4254   g_signal_connect_data (obj, "row-deleted",
4255                          (GCallback) model_row_deleted, view, NULL, 
4256                          G_CONNECT_AFTER);
4257   g_signal_connect_data (obj, "rows-reordered",
4258                          (GCallback) model_rows_reordered, view, NULL, 
4259                          G_CONNECT_AFTER);
4260 }
4261
4262 static void
4263 disconnect_model_signals (GailTreeView *view) 
4264 {
4265   GObject *obj;
4266   GtkWidget *widget;
4267
4268   obj = G_OBJECT (view->tree_model);
4269   widget = GTK_ACCESSIBLE (view)->widget;
4270   g_signal_handlers_disconnect_by_func (obj, (gpointer) model_row_changed, widget);
4271   g_signal_handlers_disconnect_by_func (obj, (gpointer) model_row_inserted, widget);
4272   g_signal_handlers_disconnect_by_func (obj, (gpointer) model_row_deleted, widget);
4273   g_signal_handlers_disconnect_by_func (obj, (gpointer) model_rows_reordered, widget);
4274 }
4275
4276 static void
4277 clear_cached_data (GailTreeView  *view)
4278 {
4279   GList *temp_list;
4280
4281   if (view->row_data)
4282     {
4283       GArray *array = view->row_data;
4284       gint i;
4285
4286      /*
4287       * Since the third argument to free_row_info is FALSE, we don't remove 
4288       * the element.  Therefore it is safe to loop forward.
4289       */
4290       for (i = 0; i < array->len; i++)
4291         free_row_info (array, i, FALSE);
4292
4293       g_array_free (array, TRUE);
4294
4295       view->row_data = NULL;
4296     }
4297
4298   if (view->cell_data)
4299     {
4300       /* Must loop through them all */
4301       for (temp_list = view->cell_data; temp_list; temp_list = temp_list->next)
4302         {
4303             clean_cell_info (view, temp_list);
4304         }
4305     }
4306   garbage_collect_cell_data (view);
4307   if (view->cell_data)
4308       g_list_free (view->cell_data);
4309   
4310   view->cell_data = NULL;
4311 }
4312
4313 /*
4314  * Returns the column number of the specified GtkTreeViewColumn
4315  *
4316  * If visible is set, the value returned will be the visible column number, 
4317  * i.e. suitable for use in AtkTable function. If visible is not set, the
4318  * value returned is the actual column number, which is suitable for use in 
4319  * getting an index value.
4320  */
4321 static gint
4322 get_column_number (GtkTreeView       *tree_view,
4323                    GtkTreeViewColumn *column,
4324                    gboolean          visible)
4325 {
4326   GList *temp_list, *column_list;
4327   GtkTreeViewColumn *tv_column;
4328   gint ret_val;
4329
4330   column_list = gtk_tree_view_get_columns (tree_view);
4331   ret_val = 0;
4332   for (temp_list = column_list; temp_list; temp_list = temp_list->next)
4333     {
4334       tv_column = GTK_TREE_VIEW_COLUMN (temp_list->data);
4335       if (tv_column == column)
4336         break;
4337       if (!visible || gtk_tree_view_column_get_visible (tv_column))
4338         ret_val++;
4339     }
4340   if (temp_list == NULL)
4341     {
4342       ret_val = -1;
4343     }
4344   g_list_free (column_list);
4345   return ret_val;
4346
4347
4348 static gint
4349 get_index (GtkTreeView       *tree_view,
4350            GtkTreePath       *path,
4351            gint              actual_column)
4352 {
4353   gint depth = 0;
4354   gint index = 1;
4355   gint *indices = NULL;
4356
4357
4358   if (path)
4359     {
4360       depth = gtk_tree_path_get_depth (path);
4361       indices = gtk_tree_path_get_indices (path);
4362     }
4363
4364   if (depth > 1)
4365     {
4366       GtkTreePath *copy_path;
4367       GtkTreeModel *model;
4368
4369       model = gtk_tree_view_get_model (tree_view);
4370       copy_path = gtk_tree_path_copy (path);
4371       gtk_tree_path_up (copy_path);
4372       count_rows (model, NULL, copy_path, &index, 0, depth);
4373       gtk_tree_path_free (copy_path);
4374     }
4375
4376   if (path)
4377     index += indices[depth-1];
4378   index *= get_n_actual_columns (tree_view);
4379   index +=  actual_column;
4380   return index;
4381 }
4382
4383 /*
4384  * The function count_rows counts the number of rows starting at iter and ending
4385  * at end_path. The value of level is the depth of iter and the value of depth
4386  * is the depth of end_path. Rows at depth before end_path are counted.
4387  * This functions counts rows which are not visible because an ancestor is 
4388  * collapsed.
4389  */
4390 static void 
4391 count_rows (GtkTreeModel *model,
4392             GtkTreeIter *iter,
4393             GtkTreePath *end_path,
4394             gint        *count,
4395             gint        level,
4396             gint        depth)
4397 {
4398   GtkTreeIter child_iter;
4399   
4400   if (!model) return;
4401
4402   level++;
4403
4404   *count += gtk_tree_model_iter_n_children (model, iter);
4405
4406 #if 0
4407   g_print ("count_rows : %d level: %d depth: %d\n", *count, level, depth);
4408   if (iter != NULL)
4409     g_print ("path: %s\n",
4410             gtk_tree_path_to_string (gtk_tree_model_get_path (model, iter)));
4411 #endif
4412
4413   if (level >= depth)
4414     return;
4415
4416   if (gtk_tree_model_iter_children (model, &child_iter, iter))
4417     {
4418       gboolean ret_val = TRUE;
4419
4420       while (ret_val)
4421         {
4422           if (level == depth - 1)
4423             {
4424               GtkTreePath *iter_path; 
4425               gboolean finished = FALSE;
4426
4427               iter_path = gtk_tree_model_get_path (model, &child_iter);
4428               if (end_path && gtk_tree_path_compare (iter_path, end_path) >= 0)
4429                 finished = TRUE;
4430               gtk_tree_path_free (iter_path);
4431               if (finished)
4432                 break;
4433             }
4434           if (gtk_tree_model_iter_has_child (model, &child_iter))
4435             count_rows (model, &child_iter, end_path, count, level, depth);
4436           ret_val = gtk_tree_model_iter_next (model, &child_iter);
4437         }
4438     }
4439 }
4440
4441 /*
4442  * Find the next node, which has children, at the specified depth below
4443  * the specified iter. The level is the depth of the current iter.
4444  * The position of the node is returned in path and the return value of TRUE 
4445  * means that a node was found.
4446  */
4447
4448 gboolean get_next_node_with_child_at_depth (GtkTreeModel *model,
4449                                             GtkTreeIter  *iter,
4450                                             GtkTreePath  **path,
4451                                             gint         level,
4452                                             gint         depth)
4453 {
4454   GtkTreeIter child_iter;
4455
4456   *path = NULL;
4457
4458   if (gtk_tree_model_iter_children (model, &child_iter, iter))
4459     {
4460       level++;
4461
4462       while (TRUE)
4463         {
4464           while (!gtk_tree_model_iter_has_child (model, &child_iter))
4465             {
4466               if (!gtk_tree_model_iter_next (model, &child_iter))
4467                 return FALSE;
4468             }
4469
4470           if (level == depth)
4471           /* We have found what we were looking for */
4472             {
4473               *path = gtk_tree_model_get_path (model, &child_iter);
4474               return TRUE;
4475             }
4476
4477           if (get_next_node_with_child_at_depth (model, &child_iter, path,
4478                                                  level, depth))
4479             return TRUE;
4480
4481           if (!gtk_tree_model_iter_next (model, &child_iter))
4482             return FALSE;
4483         }
4484     }
4485   return FALSE;
4486 }
4487
4488 /*
4489  * Find the next node, which has children, at the same depth as 
4490  * the specified GtkTreePath.
4491  */
4492 static gboolean 
4493 get_next_node_with_child (GtkTreeModel *model,
4494                           GtkTreePath  *path,
4495                           GtkTreePath  **return_path)
4496 {
4497   GtkTreeIter iter;
4498   gint depth;
4499
4500   gtk_tree_model_get_iter (model, &iter, path);
4501
4502   while (gtk_tree_model_iter_next (model, &iter))
4503     {
4504       if (gtk_tree_model_iter_has_child (model, &iter))
4505         {
4506           *return_path = gtk_tree_model_get_path (model, &iter);
4507           return TRUE;
4508         }
4509     }
4510   depth = gtk_tree_path_get_depth (path);
4511   while (gtk_tree_path_up (path))
4512     {
4513       if (gtk_tree_path_get_depth (path) == 0)
4514         break;
4515
4516       gtk_tree_model_get_iter (model, &iter, path);
4517       while (gtk_tree_model_iter_next (model, &iter))
4518         if (get_next_node_with_child_at_depth (model, &iter, return_path,
4519                                          gtk_tree_path_get_depth (path), depth))
4520           return TRUE;
4521     }
4522   *return_path = NULL;
4523   return FALSE;
4524 }
4525
4526 static gboolean 
4527 get_tree_path_from_row_index (GtkTreeModel *model,
4528                               gint         row_index,
4529                               GtkTreePath  **tree_path)
4530 {
4531   GtkTreeIter iter;
4532   gint count;
4533   gint depth;
4534
4535   count = gtk_tree_model_iter_n_children (model, NULL);
4536   if (count > row_index)
4537     {
4538       if (gtk_tree_model_iter_nth_child (model, &iter, NULL, row_index))
4539         {
4540           *tree_path = gtk_tree_model_get_path (model, &iter);
4541           return TRUE;
4542         }
4543       else
4544         return FALSE;
4545     }
4546   else
4547      row_index -= count;
4548
4549   depth = 0;
4550   while (TRUE)
4551     {
4552       depth++;
4553
4554       if (get_next_node_with_child_at_depth (model, NULL, tree_path, 0, depth))
4555         {
4556           GtkTreePath *next_path;
4557
4558           while (TRUE)
4559             {
4560               gtk_tree_model_get_iter (model, &iter, *tree_path);
4561               count = gtk_tree_model_iter_n_children (model, &iter);
4562               if (count > row_index)
4563                 {
4564                   gtk_tree_path_append_index (*tree_path, row_index);
4565                   return TRUE;
4566                 }
4567               else
4568                 row_index -= count;
4569
4570               if (!get_next_node_with_child (model,  *tree_path, &next_path))
4571                 break;
4572            
4573               gtk_tree_path_free (*tree_path);
4574               *tree_path = next_path;
4575             }
4576         }
4577       else
4578         {
4579           g_warning ("Index value is too large\n");
4580           gtk_tree_path_free (*tree_path);
4581            *tree_path = NULL;
4582           return FALSE;
4583         }
4584     }  
4585 }
4586
4587 /*
4588  * This function returns the number of rows, including those which are collapsed
4589  */
4590 static gint
4591 get_row_count (GtkTreeModel *model)
4592 {
4593   gint n_rows = 1;
4594
4595   count_rows (model, NULL, NULL, &n_rows, 0, G_MAXINT);
4596
4597   return n_rows;
4598 }
4599
4600 static gboolean
4601 get_path_column_from_index (GtkTreeView       *tree_view,
4602                             gint              index,
4603                             GtkTreePath       **path,
4604                             GtkTreeViewColumn **column)
4605 {
4606   GtkTreeModel *tree_model;
4607   gint n_columns;
4608
4609   tree_model = gtk_tree_view_get_model (tree_view);
4610   n_columns = get_n_actual_columns (tree_view);
4611   if (n_columns == 0)
4612     return FALSE;
4613   /* First row is the column headers */
4614   index -= n_columns;
4615   if (index < 0)
4616     return FALSE;
4617
4618   if (path)
4619     {
4620       gint row_index;
4621       gboolean retval;
4622
4623       row_index = index / n_columns;
4624       retval = get_tree_path_from_row_index (tree_model, row_index, path);
4625       gail_return_val_if_fail (retval, FALSE);
4626       if (*path == NULL)
4627         return FALSE;
4628     }    
4629
4630   if (column)
4631     {
4632       *column = gtk_tree_view_get_column (tree_view, index % n_columns);
4633       if (*column == NULL)
4634         {
4635           if (path)
4636             gtk_tree_path_free (*path);
4637           return FALSE;
4638         }
4639   }
4640   return TRUE;
4641 }
4642
4643 static void
4644 set_cell_expandable (GailCell *cell)
4645 {
4646   if (gail_cell_add_state (cell, 
4647                            ATK_STATE_EXPANDABLE,
4648                            FALSE))
4649     gail_cell_add_action (cell,
4650                           "expand or contract", /* action name */
4651                           "expands or contracts the row in the tree view "
4652                           "containing this cell", /* description */
4653                           NULL, /* Keybinding */
4654                           toggle_cell_expanded);
4655 }
4656
4657 static GailTreeViewCellInfo*
4658 find_cell_info (GailTreeView *view,
4659                 GailCell     *cell,
4660                 GList**      list,
4661                 gboolean     live_only)
4662 {
4663   GList *temp_list;
4664   GailTreeViewCellInfo *cell_info;
4665
4666   for (temp_list = view->cell_data; temp_list; temp_list = temp_list->next)
4667     {
4668       cell_info = (GailTreeViewCellInfo *) temp_list->data;
4669       if (cell_info->cell == cell && (!live_only || cell_info->in_use))
4670         {
4671           if (list)
4672             *list = temp_list;
4673           return cell_info;
4674         }
4675     }
4676   return NULL;
4677 }
4678
4679 static AtkObject *
4680 get_header_from_column (GtkTreeViewColumn *tv_col)
4681 {
4682   AtkObject *rc;
4683   GtkWidget *header_widget;
4684
4685   if (tv_col == NULL)
4686     return NULL;
4687
4688   /* If the user has set a header object, use that */
4689
4690   rc = g_object_get_qdata (G_OBJECT (tv_col), quark_column_header_object);
4691
4692   if (rc == NULL)
4693     {
4694       /* If the user has not set a header object, grab the column */
4695       /* header object defined by the GtkTreeView */
4696
4697       header_widget = tv_col->button;
4698
4699       if (header_widget)
4700         {
4701           rc = gtk_widget_get_accessible (header_widget);
4702         }
4703       else
4704         rc = NULL;
4705     }
4706   return rc;
4707 }