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