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