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