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