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