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