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