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