1 /* GAIL - The GNOME Accessibility Implementation Library
2 * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
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.
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.
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.
23 #ifdef GDK_WINDOWING_X11
24 #include <gdk/x11/gdkx.h>
27 #include "gtktreeprivate.h"
28 #include "gtkwidgetprivate.h"
30 #include "gtktreeviewaccessible.h"
31 #include "gtkrenderercellaccessible.h"
32 #include "gtkbooleancellaccessible.h"
33 #include "gtkimagecellaccessible.h"
34 #include "gtkcontainercellaccessible.h"
35 #include "gtktextcellaccessible.h"
36 #include "gtkcellaccessibleparent.h"
38 typedef struct _GtkTreeViewAccessibleCellInfo GtkTreeViewAccessibleCellInfo;
39 struct _GtkTreeViewAccessibleCellInfo
41 GtkCellAccessible *cell;
44 GtkTreeViewColumn *cell_col_ref;
45 GtkTreeViewAccessible *view;
50 static gboolean row_expanded_cb (GtkTreeView *tree_view,
53 static gboolean row_collapsed_cb (GtkTreeView *tree_view,
56 static void size_allocate_cb (GtkWidget *widget,
57 GtkAllocation *allocation);
58 static void selection_changed_cb (GtkTreeSelection *selection,
61 static void cursor_changed (GtkTreeView *tree_view,
62 GtkTreeViewAccessible *accessible);
63 static gboolean focus_in (GtkWidget *widget);
64 static gboolean focus_out (GtkWidget *widget);
66 static void destroy_count_func (GtkTreeView *tree_view,
73 static void set_iter_nth_row (GtkTreeView *tree_view,
76 static gint get_row_from_tree_path (GtkTreeView *tree_view,
78 static void iterate_thru_children (GtkTreeView *tree_view,
79 GtkTreeModel *tree_model,
80 GtkTreePath *tree_path,
84 static int cell_info_get_index (GtkTreeView *tree_view,
85 GtkTreeViewAccessibleCellInfo *info);
86 static void traverse_cells (GtkTreeViewAccessible *tree_view,
87 GtkTreePath *tree_path,
89 static gboolean update_cell_value (GtkRendererCellAccessible *renderer_cell,
90 GtkTreeViewAccessible *accessible,
91 gboolean emit_change_signal);
92 static void set_cell_visibility (GtkTreeView *tree_view,
93 GtkCellAccessible *cell,
94 GtkTreeViewColumn *tv_col,
95 GtkTreePath *tree_path,
96 gboolean emit_signal);
97 static gboolean is_cell_showing (GtkTreeView *tree_view,
98 GdkRectangle *cell_rect);
99 static void set_expand_state (GtkTreeView *tree_view,
100 GtkTreeModel *tree_model,
101 GtkTreeViewAccessible *accessible,
102 GtkTreePath *tree_path,
103 gboolean set_on_ancestor);
104 static void set_cell_expandable (GtkCellAccessible *cell);
105 static void add_cell_actions (GtkCellAccessible *cell,
108 static void toggle_cell_toggled (GtkCellAccessible *cell);
109 static void edit_cell (GtkCellAccessible *cell);
110 static void activate_cell (GtkCellAccessible *cell);
111 static void cell_destroyed (gpointer data);
112 static void cell_info_new (GtkTreeViewAccessible *accessible,
113 GtkTreeModel *tree_model,
116 GtkTreeViewColumn *tv_col,
117 GtkCellAccessible *cell);
118 static GtkCellAccessible *find_cell (GtkTreeViewAccessible *accessible,
120 static void connect_model_signals (GtkTreeView *view,
121 GtkTreeViewAccessible *accessible);
122 static void disconnect_model_signals (GtkTreeViewAccessible *accessible);
123 static gint get_column_number (GtkTreeView *tree_view,
124 GtkTreeViewColumn *column);
125 static gint get_focus_index (GtkTreeView *tree_view);
126 static gint get_index (GtkTreeView *tree_view,
129 static void count_rows (GtkTreeModel *model,
131 GtkTreePath *end_path,
136 static gboolean get_rbtree_column_from_index (GtkTreeView *tree_view,
140 GtkTreeViewColumn **column);
142 static GtkTreeViewAccessibleCellInfo* find_cell_info (GtkTreeViewAccessible *view,
143 GtkCellAccessible *cell);
144 static AtkObject * get_header_from_column (GtkTreeViewColumn *tv_col);
147 static void atk_table_interface_init (AtkTableIface *iface);
148 static void atk_selection_interface_init (AtkSelectionIface *iface);
149 static void atk_component_interface_init (AtkComponentIface *iface);
150 static void gtk_cell_accessible_parent_interface_init (GtkCellAccessibleParentIface *iface);
152 G_DEFINE_TYPE_WITH_CODE (GtkTreeViewAccessible, _gtk_tree_view_accessible, GTK_TYPE_CONTAINER_ACCESSIBLE,
153 G_IMPLEMENT_INTERFACE (ATK_TYPE_TABLE, atk_table_interface_init)
154 G_IMPLEMENT_INTERFACE (ATK_TYPE_SELECTION, atk_selection_interface_init)
155 G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, atk_component_interface_init)
156 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_ACCESSIBLE_PARENT, gtk_cell_accessible_parent_interface_init))
160 adjustment_changed (GtkAdjustment *adjustment,
163 GtkTreeViewAccessible *accessible;
165 accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
166 traverse_cells (accessible, NULL, FALSE);
170 hadjustment_set_cb (GObject *widget,
174 GtkTreeViewAccessible *accessible = data;
177 g_object_get (widget, "hadjustment", &adj, NULL);
178 accessible->old_hadj = adj;
179 g_object_add_weak_pointer (G_OBJECT (accessible->old_hadj), (gpointer *)&accessible->old_hadj);
180 g_signal_connect (adj, "value-changed", G_CALLBACK (adjustment_changed), widget);
184 vadjustment_set_cb (GObject *widget,
188 GtkTreeViewAccessible *accessible = data;
191 g_object_get (widget, "vadjustment", &adj, NULL);
192 accessible->old_vadj = adj;
193 g_object_add_weak_pointer (G_OBJECT (accessible->old_vadj), (gpointer *)&accessible->old_vadj);
194 g_signal_connect (adj, "value-changed",
195 G_CALLBACK (adjustment_changed), widget);
199 gtk_tree_view_accessible_get_data_quark (void)
201 static GQuark quark = 0;
203 if (G_UNLIKELY (quark == 0))
204 quark = g_quark_from_static_string ("gtk-tree-view-accessible-data");
210 cell_info_free (GtkTreeViewAccessibleCellInfo *cell_info)
214 g_object_steal_qdata (G_OBJECT (cell_info->cell),
215 gtk_tree_view_accessible_get_data_quark ());
216 _gtk_cell_accessible_add_state (cell_info->cell, ATK_STATE_DEFUNCT, FALSE);
223 cell_info_get_path (GtkTreeViewAccessibleCellInfo *cell_info)
225 return _gtk_tree_view_find_path (NULL,
231 cell_info_hash (gconstpointer info)
233 const GtkTreeViewAccessibleCellInfo *cell_info = info;
236 node = GPOINTER_TO_UINT (cell_info->node);
237 col = GPOINTER_TO_UINT (cell_info->cell_col_ref);
239 return ((node << sizeof (guint) / 2) | (node >> sizeof (guint) / 2)) ^ col;
243 cell_info_equal (gconstpointer a, gconstpointer b)
245 const GtkTreeViewAccessibleCellInfo *cell_info_a = a;
246 const GtkTreeViewAccessibleCellInfo *cell_info_b = b;
248 return cell_info_a->node == cell_info_b->node &&
249 cell_info_a->cell_col_ref == cell_info_b->cell_col_ref;
253 gtk_tree_view_accessible_initialize (AtkObject *obj,
256 GtkTreeViewAccessible *accessible;
257 GtkTreeView *tree_view;
258 GtkTreeModel *tree_model;
260 GtkTreeSelection *selection;
262 ATK_OBJECT_CLASS (_gtk_tree_view_accessible_parent_class)->initialize (obj, data);
264 accessible = GTK_TREE_VIEW_ACCESSIBLE (obj);
265 accessible->focus_cell = NULL;
266 accessible->old_hadj = NULL;
267 accessible->old_vadj = NULL;
268 accessible->idle_expand_id = 0;
269 accessible->idle_expand_path = NULL;
270 accessible->n_children_deleted = 0;
272 accessible->cell_infos = g_hash_table_new_full (cell_info_hash,
273 cell_info_equal, NULL, (GDestroyNotify) cell_info_free);
275 widget = GTK_WIDGET (data);
276 tree_view = GTK_TREE_VIEW (widget);
277 tree_model = gtk_tree_view_get_model (tree_view);
278 selection = gtk_tree_view_get_selection (tree_view);
280 g_signal_connect_after (widget, "row-collapsed",
281 G_CALLBACK (row_collapsed_cb), NULL);
282 g_signal_connect (widget, "row-expanded",
283 G_CALLBACK (row_expanded_cb), NULL);
284 g_signal_connect (widget, "size-allocate",
285 G_CALLBACK (size_allocate_cb), NULL);
286 g_signal_connect (selection, "changed",
287 G_CALLBACK (selection_changed_cb), obj);
289 g_signal_connect (tree_view, "cursor-changed",
290 G_CALLBACK (cursor_changed), accessible);
291 g_signal_connect (tree_view, "focus-in-event",
292 G_CALLBACK (focus_in), NULL);
293 g_signal_connect (tree_view, "focus-out-event",
294 G_CALLBACK (focus_out), NULL);
296 accessible->tree_model = tree_model;
299 g_object_add_weak_pointer (G_OBJECT (accessible->tree_model), (gpointer *)&accessible->tree_model);
300 connect_model_signals (tree_view, accessible);
302 if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
303 obj->role = ATK_ROLE_TABLE;
305 obj->role = ATK_ROLE_TREE_TABLE;
308 hadjustment_set_cb (G_OBJECT (widget), NULL, accessible);
309 vadjustment_set_cb (G_OBJECT (widget), NULL, accessible);
310 g_signal_connect (widget, "notify::hadjustment",
311 G_CALLBACK (hadjustment_set_cb), accessible);
312 g_signal_connect (widget, "notify::vadjustment",
313 G_CALLBACK (vadjustment_set_cb), accessible);
315 gtk_tree_view_set_destroy_count_func (tree_view,
321 gtk_tree_view_accessible_finalize (GObject *object)
323 GtkTreeViewAccessible *accessible = GTK_TREE_VIEW_ACCESSIBLE (object);
325 /* remove any idle handlers still pending */
326 if (accessible->idle_expand_id)
327 g_source_remove (accessible->idle_expand_id);
329 if (accessible->tree_model)
330 disconnect_model_signals (accessible);
332 if (accessible->cell_infos)
333 g_hash_table_destroy (accessible->cell_infos);
335 G_OBJECT_CLASS (_gtk_tree_view_accessible_parent_class)->finalize (object);
339 gtk_tree_view_accessible_notify_gtk (GObject *obj,
343 GtkTreeView *tree_view;
344 GtkTreeViewAccessible *accessible;
347 widget = GTK_WIDGET (obj);
348 accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
349 tree_view = GTK_TREE_VIEW (widget);
351 if (g_strcmp0 (pspec->name, "model") == 0)
353 GtkTreeModel *tree_model;
356 tree_model = gtk_tree_view_get_model (tree_view);
357 if (accessible->tree_model)
358 disconnect_model_signals (accessible);
359 g_hash_table_remove_all (accessible->cell_infos);
360 accessible->tree_model = tree_model;
364 g_object_add_weak_pointer (G_OBJECT (accessible->tree_model), (gpointer *)&accessible->tree_model);
365 connect_model_signals (tree_view, accessible);
367 if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
368 role = ATK_ROLE_TABLE;
370 role = ATK_ROLE_TREE_TABLE;
374 role = ATK_ROLE_UNKNOWN;
376 atk_object_set_role (ATK_OBJECT (accessible), role);
377 g_object_freeze_notify (G_OBJECT (accessible));
378 g_signal_emit_by_name (accessible, "model-changed");
379 g_signal_emit_by_name (accessible, "visible-data-changed");
380 g_object_thaw_notify (G_OBJECT (accessible));
382 else if (g_strcmp0 (pspec->name, "hadjustment") == 0)
384 g_object_get (tree_view, "hadjustment", &adj, NULL);
385 g_signal_handlers_disconnect_by_func (accessible->old_hadj,
386 (gpointer) adjustment_changed,
388 accessible->old_hadj = adj;
389 g_object_add_weak_pointer (G_OBJECT (accessible->old_hadj), (gpointer *)&accessible->old_hadj);
390 g_signal_connect (adj, "value-changed", G_CALLBACK (adjustment_changed), tree_view);
392 else if (g_strcmp0 (pspec->name, "vadjustment") == 0)
394 g_object_get (tree_view, "vadjustment", &adj, NULL);
395 g_signal_handlers_disconnect_by_func (accessible->old_vadj,
396 (gpointer) adjustment_changed,
398 accessible->old_vadj = adj;
399 g_object_add_weak_pointer (G_OBJECT (accessible->old_hadj), (gpointer *)&accessible->old_vadj);
400 g_signal_connect (adj, "value-changed", G_CALLBACK (adjustment_changed), tree_view);
403 GTK_WIDGET_ACCESSIBLE_CLASS (_gtk_tree_view_accessible_parent_class)->notify_gtk (obj, pspec);
407 gtk_tree_view_accessible_destroyed (GtkWidget *widget,
408 GtkAccessible *gtk_accessible)
411 GtkTreeViewAccessible *accessible;
413 if (!GTK_IS_TREE_VIEW (widget))
416 accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_accessible);
417 adj = accessible->old_hadj;
419 g_signal_handlers_disconnect_by_func (adj,
420 (gpointer) adjustment_changed,
422 adj = accessible->old_vadj;
424 g_signal_handlers_disconnect_by_func (adj,
425 (gpointer) adjustment_changed,
427 if (accessible->tree_model)
429 disconnect_model_signals (accessible);
430 accessible->tree_model = NULL;
432 if (accessible->focus_cell)
434 g_object_unref (accessible->focus_cell);
435 accessible->focus_cell = NULL;
437 if (accessible->idle_expand_id)
439 g_source_remove (accessible->idle_expand_id);
440 accessible->idle_expand_id = 0;
445 gtk_tree_view_accessible_connect_widget_destroyed (GtkAccessible *accessible)
449 widget = gtk_accessible_get_widget (accessible);
451 g_signal_connect_after (widget, "destroy",
452 G_CALLBACK (gtk_tree_view_accessible_destroyed), accessible);
454 GTK_ACCESSIBLE_CLASS (_gtk_tree_view_accessible_parent_class)->connect_widget_destroyed (accessible);
458 get_n_rows (GtkTreeView *tree_view)
462 tree = _gtk_tree_view_get_rbtree (tree_view);
467 return tree->root->total_count;
471 get_n_columns (GtkTreeView *tree_view)
473 guint i, visible_columns;
477 for (i = 0; i < gtk_tree_view_get_n_columns (tree_view); i++)
479 GtkTreeViewColumn *column = gtk_tree_view_get_column (tree_view, i);
481 if (gtk_tree_view_column_get_visible (column))
485 return visible_columns;
488 gtk_tree_view_accessible_get_n_children (AtkObject *obj)
491 GtkTreeView *tree_view;
493 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
497 tree_view = GTK_TREE_VIEW (widget);
498 return (get_n_rows (tree_view) + 1) * get_n_columns (tree_view);
501 static GtkTreeViewColumn *
502 get_visible_column (GtkTreeView *tree_view,
507 for (i = 0; i < gtk_tree_view_get_n_columns (tree_view); i++)
509 GtkTreeViewColumn *column = gtk_tree_view_get_column (tree_view, i);
511 if (!gtk_tree_view_column_get_visible (column))
520 g_return_val_if_reached (NULL);
524 gtk_tree_view_accessible_ref_child (AtkObject *obj,
528 GtkTreeViewAccessible *accessible;
529 GtkCellAccessible *cell;
530 GtkTreeView *tree_view;
531 GtkTreeModel *tree_model;
532 GtkCellRenderer *renderer;
534 GtkTreeViewColumn *tv_col;
535 GtkTreeSelection *selection;
541 GtkTreeViewColumn *expander_tv;
542 GList *renderer_list;
544 GtkContainerCellAccessible *container = NULL;
545 GtkRendererCellAccessible *renderer_cell;
546 gboolean is_expander, is_expanded, retval;
547 gboolean editable = FALSE;
550 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
554 if (i >= gtk_tree_view_accessible_get_n_children (obj))
557 accessible = GTK_TREE_VIEW_ACCESSIBLE (obj);
558 tree_view = GTK_TREE_VIEW (widget);
559 if (i < get_n_columns (tree_view))
561 tv_col = get_visible_column (tree_view, i);
562 child = get_header_from_column (tv_col);
564 g_object_ref (child);
568 /* Check whether the child is cached */
569 cell = find_cell (accessible, i);
573 return ATK_OBJECT (cell);
576 if (accessible->focus_cell == NULL)
577 focus_index = get_focus_index (tree_view);
581 /* Find the RBTree and GtkTreeViewColumn for the index */
582 if (!get_rbtree_column_from_index (tree_view, i, &tree, &node, &tv_col))
585 path = _gtk_tree_view_find_path (tree_view, tree, node);
586 tree_model = gtk_tree_view_get_model (tree_view);
587 retval = gtk_tree_model_get_iter (tree_model, &iter, path);
591 expander_tv = gtk_tree_view_get_expander_column (tree_view);
594 if (GTK_RBNODE_FLAG_SET (node, GTK_RBNODE_IS_PARENT))
596 if (expander_tv == tv_col)
599 is_expanded = node->children != NULL;
602 gtk_tree_view_column_cell_set_cell_data (tv_col, tree_model, &iter,
603 is_expander, is_expanded);
605 renderer_list = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
607 /* If there are more than one renderer in the list,
610 if (renderer_list && renderer_list->next)
612 GtkCellAccessible *container_cell;
614 container = _gtk_container_cell_accessible_new ();
616 container_cell = GTK_CELL_ACCESSIBLE (container);
617 _gtk_cell_accessible_initialise (container_cell, widget, ATK_OBJECT (accessible));
619 /* The GtkTreeViewAccessibleCellInfo structure for the container will
620 * be before the ones for the cells so that the first one we find for
621 * a position will be for the container
623 cell_info_new (accessible, tree_model, tree, node, tv_col, container_cell);
624 parent = ATK_OBJECT (container);
627 parent = ATK_OBJECT (accessible);
631 /* Now we make a fake cell_renderer if there is no cell
634 if (renderer_list == NULL)
636 GtkCellRenderer *fake_renderer;
638 fake_renderer = g_object_new (GTK_TYPE_CELL_RENDERER_TEXT, NULL);
639 child = _gtk_text_cell_accessible_new ();
640 cell = GTK_CELL_ACCESSIBLE (child);
641 renderer_cell = GTK_RENDERER_CELL_ACCESSIBLE (child);
642 renderer_cell->renderer = fake_renderer;
644 /* Create the GtkTreeViewAccessibleCellInfo structure for this cell */
645 cell_info_new (accessible, tree_model, tree, node, tv_col, cell);
647 _gtk_cell_accessible_initialise (cell, widget, parent);
649 /* Set state if it is expandable */
652 set_cell_expandable (cell);
654 _gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDED, FALSE);
659 for (l = renderer_list; l; l = l->next)
661 renderer = GTK_CELL_RENDERER (l->data);
663 if (GTK_IS_CELL_RENDERER_TEXT (renderer))
665 g_object_get (G_OBJECT (renderer), "editable", &editable, NULL);
666 child = _gtk_text_cell_accessible_new ();
668 else if (GTK_IS_CELL_RENDERER_TOGGLE (renderer))
669 child = _gtk_boolean_cell_accessible_new ();
670 else if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
671 child = _gtk_image_cell_accessible_new ();
673 child = _gtk_renderer_cell_accessible_new ();
675 cell = GTK_CELL_ACCESSIBLE (child);
676 renderer_cell = GTK_RENDERER_CELL_ACCESSIBLE (child);
678 /* Create the GtkTreeViewAccessibleCellInfo for this cell */
679 if (parent == ATK_OBJECT (accessible))
680 cell_info_new (accessible, tree_model, tree, node, tv_col, cell);
682 _gtk_cell_accessible_initialise (cell, widget, parent);
685 _gtk_container_cell_accessible_add_child (container, cell);
687 update_cell_value (renderer_cell, accessible, FALSE);
689 /* Add the actions appropriate for this cell */
690 add_cell_actions (cell, editable);
692 /* Set state if it is expandable */
695 set_cell_expandable (cell);
697 _gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDED, FALSE);
700 /* If the column is visible, sets the cell's state */
701 if (gtk_tree_view_column_get_visible (tv_col))
702 set_cell_visibility (tree_view, cell, tv_col, path, FALSE);
704 /* If the row is selected, all cells on the row are selected */
705 selection = gtk_tree_view_get_selection (tree_view);
707 if (gtk_tree_selection_path_is_selected (selection, path))
708 _gtk_cell_accessible_add_state (cell, ATK_STATE_SELECTED, FALSE);
710 _gtk_cell_accessible_add_state (cell, ATK_STATE_FOCUSABLE, FALSE);
711 if (focus_index == i)
713 accessible->focus_cell = g_object_ref (cell);
714 _gtk_cell_accessible_add_state (cell, ATK_STATE_FOCUSED, FALSE);
715 g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
718 g_list_free (renderer_list);
720 child = ATK_OBJECT (container);
723 if (expander_tv == tv_col)
725 AtkRelationSet *relation_set;
726 AtkObject *accessible_array[1];
727 AtkRelation* relation;
728 AtkObject *parent_node;
730 relation_set = atk_object_ref_relation_set (ATK_OBJECT (child));
732 gtk_tree_path_up (path);
733 if (gtk_tree_path_get_depth (path) == 0)
739 parent_index = get_index (tree_view, path, i % get_n_columns (tree_view));
740 parent_node = atk_object_ref_accessible_child (obj, parent_index);
742 accessible_array[0] = parent_node;
743 relation = atk_relation_new (accessible_array, 1,
744 ATK_RELATION_NODE_CHILD_OF);
745 atk_relation_set_add (relation_set, relation);
746 atk_object_add_relationship (parent_node, ATK_RELATION_NODE_PARENT_OF, child);
747 g_object_unref (relation);
748 g_object_unref (relation_set);
750 gtk_tree_path_free (path);
752 /* We do not increase the reference count here; when g_object_unref()
753 * is called for the cell then cell_destroyed() is called and this
754 * removes the cell from the cache.
760 gtk_tree_view_accessible_ref_state_set (AtkObject *obj)
762 AtkStateSet *state_set;
765 state_set = ATK_OBJECT_CLASS (_gtk_tree_view_accessible_parent_class)->ref_state_set (obj);
766 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
769 atk_state_set_add_state (state_set, ATK_STATE_MANAGES_DESCENDANTS);
775 _gtk_tree_view_accessible_class_init (GtkTreeViewAccessibleClass *klass)
777 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
778 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
779 GtkAccessibleClass *accessible_class = (GtkAccessibleClass*)klass;
780 GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
781 GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
783 class->get_n_children = gtk_tree_view_accessible_get_n_children;
784 class->ref_child = gtk_tree_view_accessible_ref_child;
785 class->ref_state_set = gtk_tree_view_accessible_ref_state_set;
786 class->initialize = gtk_tree_view_accessible_initialize;
788 widget_class->notify_gtk = gtk_tree_view_accessible_notify_gtk;
790 accessible_class->connect_widget_destroyed = gtk_tree_view_accessible_connect_widget_destroyed;
792 /* The children of a GtkTreeView are the buttons at the top of the columns
793 * we do not represent these as children so we do not want to report
794 * children added or deleted when these changed.
796 container_class->add_gtk = NULL;
797 container_class->remove_gtk = NULL;
799 gobject_class->finalize = gtk_tree_view_accessible_finalize;
803 _gtk_tree_view_accessible_init (GtkTreeViewAccessible *view)
808 get_focus_index (GtkTreeView *tree_view)
810 GtkTreePath *focus_path;
811 GtkTreeViewColumn *focus_column;
814 gtk_tree_view_get_cursor (tree_view, &focus_path, &focus_column);
815 if (focus_path && focus_column)
816 index = get_index (tree_view, focus_path,
817 get_column_number (tree_view, focus_column));
822 gtk_tree_path_free (focus_path);
827 /* This function returns a reference to the accessible object
828 * for the cell in the treeview which has focus, if any
831 gtk_tree_view_accessible_ref_focus_cell (GtkTreeView *tree_view)
833 AtkObject *focus_cell = NULL;
837 focus_index = get_focus_index (tree_view);
838 if (focus_index >= 0)
840 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
841 focus_cell = atk_object_ref_accessible_child (atk_obj, focus_index);
850 gtk_tree_view_accessible_ref_accessible_at_point (AtkComponent *component,
853 AtkCoordType coord_type)
856 GtkTreeView *tree_view;
858 GtkTreeViewColumn *tv_column;
863 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (component));
867 tree_view = GTK_TREE_VIEW (widget);
869 atk_component_get_extents (component, &x_pos, &y_pos, NULL, NULL, coord_type);
870 gtk_tree_view_convert_widget_to_bin_window_coords (tree_view, x, y, &bx, &by);
871 ret_val = gtk_tree_view_get_path_at_pos (tree_view,
872 bx - x_pos, by - y_pos,
873 &path, &tv_column, NULL, NULL);
878 column = get_column_number (tree_view, tv_column);
879 index = get_index (tree_view, path, column);
880 gtk_tree_path_free (path);
882 return gtk_tree_view_accessible_ref_child (ATK_OBJECT (component), index);
889 atk_component_interface_init (AtkComponentIface *iface)
891 iface->ref_accessible_at_point = gtk_tree_view_accessible_ref_accessible_at_point;
897 gtk_tree_view_accessible_get_index_at (AtkTable *table,
904 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
908 n_cols = atk_table_get_n_columns (table);
909 n_rows = atk_table_get_n_rows (table);
911 if (row >= n_rows || column >= n_cols)
914 return (row + 1) * n_cols + column;
918 gtk_tree_view_accessible_get_column_at_index (AtkTable *table,
924 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
928 if (index >= gtk_tree_view_accessible_get_n_children (ATK_OBJECT (table)))
931 n_columns = get_n_columns (GTK_TREE_VIEW (widget));
933 /* checked by the n_children() check above */
934 g_assert (n_columns > 0);
936 return index % n_columns;
940 gtk_tree_view_accessible_get_row_at_index (AtkTable *table,
944 GtkTreeView *tree_view;
946 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
950 tree_view = GTK_TREE_VIEW (widget);
952 index /= get_n_columns (tree_view);
954 if (index >= get_n_rows (tree_view))
961 gtk_tree_view_accessible_table_ref_at (AtkTable *table,
967 index = gtk_tree_view_accessible_get_index_at (table, row, column);
971 return gtk_tree_view_accessible_ref_child (ATK_OBJECT (table), index);
975 gtk_tree_view_accessible_get_n_rows (AtkTable *table)
979 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
983 return get_n_rows (GTK_TREE_VIEW (widget));
987 gtk_tree_view_accessible_get_n_columns (AtkTable *table)
991 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
995 return get_n_columns (GTK_TREE_VIEW (widget));
999 gtk_tree_view_accessible_is_row_selected (AtkTable *table,
1003 GtkTreeView *tree_view;
1004 GtkTreeSelection *selection;
1007 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1014 tree_view = GTK_TREE_VIEW (widget);
1015 selection = gtk_tree_view_get_selection (tree_view);
1017 set_iter_nth_row (tree_view, &iter, row);
1018 return gtk_tree_selection_iter_is_selected (selection, &iter);
1022 gtk_tree_view_accessible_is_selected (AtkTable *table,
1026 return gtk_tree_view_accessible_is_row_selected (table, row);
1030 get_selected_rows (GtkTreeModel *model,
1035 GPtrArray *array = (GPtrArray *)data;
1037 g_ptr_array_add (array, gtk_tree_path_copy (path));
1041 gtk_tree_view_accessible_get_selected_rows (AtkTable *table,
1042 gint **rows_selected)
1045 GtkTreeView *tree_view;
1046 GtkTreeModel *tree_model;
1048 GtkTreeSelection *selection;
1049 GtkTreePath *tree_path;
1052 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1056 tree_view = GTK_TREE_VIEW (widget);
1057 selection = gtk_tree_view_get_selection (tree_view);
1059 switch (gtk_tree_selection_get_mode (selection))
1061 case GTK_SELECTION_SINGLE:
1062 case GTK_SELECTION_BROWSE:
1063 if (gtk_tree_selection_get_selected (selection, &tree_model, &iter))
1069 *rows_selected = g_new (gint, 1);
1070 tree_path = gtk_tree_model_get_path (tree_model, &iter);
1071 row = get_row_from_tree_path (tree_view, tree_path);
1072 gtk_tree_path_free (tree_path);
1074 /* shouldn't ever happen */
1075 g_return_val_if_fail (row != -1, 0);
1077 *rows_selected[0] = row;
1082 case GTK_SELECTION_MULTIPLE:
1084 GPtrArray *array = g_ptr_array_new();
1086 gtk_tree_selection_selected_foreach (selection, get_selected_rows, array);
1087 ret_val = array->len;
1089 if (rows_selected && ret_val)
1093 *rows_selected = g_new (gint, ret_val);
1094 for (i = 0; i < ret_val; i++)
1098 tree_path = (GtkTreePath *) g_ptr_array_index (array, i);
1099 row = get_row_from_tree_path (tree_view, tree_path);
1100 gtk_tree_path_free (tree_path);
1101 (*rows_selected)[i] = row;
1104 g_ptr_array_free (array, FALSE);
1107 case GTK_SELECTION_NONE:
1114 gtk_tree_view_accessible_add_row_selection (AtkTable *table,
1118 GtkTreeView *tree_view;
1119 GtkTreeModel *tree_model;
1120 GtkTreeSelection *selection;
1121 GtkTreePath *tree_path;
1122 GtkTreeIter iter_to_row;
1124 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1128 if (!gtk_tree_view_accessible_is_row_selected (table, row))
1130 tree_view = GTK_TREE_VIEW (widget);
1131 tree_model = gtk_tree_view_get_model (tree_view);
1132 selection = gtk_tree_view_get_selection (tree_view);
1134 if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
1136 tree_path = gtk_tree_path_new ();
1137 gtk_tree_path_append_index (tree_path, row);
1138 gtk_tree_selection_select_path (selection,tree_path);
1139 gtk_tree_path_free (tree_path);
1143 set_iter_nth_row (tree_view, &iter_to_row, row);
1144 gtk_tree_selection_select_iter (selection, &iter_to_row);
1148 return gtk_tree_view_accessible_is_row_selected (table, row);
1152 gtk_tree_view_accessible_remove_row_selection (AtkTable *table,
1156 GtkTreeView *tree_view;
1157 GtkTreeSelection *selection;
1159 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1163 tree_view = GTK_TREE_VIEW (widget);
1164 selection = gtk_tree_view_get_selection (tree_view);
1166 if (gtk_tree_view_accessible_is_row_selected (table, row))
1168 gtk_tree_selection_unselect_all (selection);
1176 gtk_tree_view_accessible_get_column_header (AtkTable *table,
1180 GtkTreeView *tree_view;
1181 GtkTreeViewColumn *tv_col;
1183 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1187 tree_view = GTK_TREE_VIEW (widget);
1188 if (in_col < 0 || in_col >= get_n_columns (tree_view))
1191 tv_col = get_visible_column (tree_view, in_col);
1192 return get_header_from_column (tv_col);
1195 static const gchar *
1196 gtk_tree_view_accessible_get_column_description (AtkTable *table,
1200 GtkTreeView *tree_view;
1201 GtkTreeViewColumn *tv_col;
1203 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1207 tree_view = GTK_TREE_VIEW (widget);
1208 if (in_col < 0 || in_col >= get_n_columns (tree_view))
1211 tv_col = get_visible_column (tree_view, in_col);
1212 return gtk_tree_view_column_get_title (tv_col);
1216 atk_table_interface_init (AtkTableIface *iface)
1218 iface->ref_at = gtk_tree_view_accessible_table_ref_at;
1219 iface->get_n_rows = gtk_tree_view_accessible_get_n_rows;
1220 iface->get_n_columns = gtk_tree_view_accessible_get_n_columns;
1221 iface->get_index_at = gtk_tree_view_accessible_get_index_at;
1222 iface->get_column_at_index = gtk_tree_view_accessible_get_column_at_index;
1223 iface->get_row_at_index = gtk_tree_view_accessible_get_row_at_index;
1224 iface->is_row_selected = gtk_tree_view_accessible_is_row_selected;
1225 iface->is_selected = gtk_tree_view_accessible_is_selected;
1226 iface->get_selected_rows = gtk_tree_view_accessible_get_selected_rows;
1227 iface->add_row_selection = gtk_tree_view_accessible_add_row_selection;
1228 iface->remove_row_selection = gtk_tree_view_accessible_remove_row_selection;
1229 iface->get_column_extent_at = NULL;
1230 iface->get_row_extent_at = NULL;
1231 iface->get_column_header = gtk_tree_view_accessible_get_column_header;
1232 iface->get_column_description = gtk_tree_view_accessible_get_column_description;
1235 /* atkselection.h */
1238 gtk_tree_view_accessible_add_selection (AtkSelection *selection,
1245 table = ATK_TABLE (selection);
1246 n_columns = gtk_tree_view_accessible_get_n_columns (table);
1250 row = gtk_tree_view_accessible_get_row_at_index (table, i);
1251 return gtk_tree_view_accessible_add_row_selection (table, row);
1255 gtk_tree_view_accessible_clear_selection (AtkSelection *selection)
1258 GtkTreeView *tree_view;
1259 GtkTreeSelection *tree_selection;
1261 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
1265 tree_view = GTK_TREE_VIEW (widget);
1266 tree_selection = gtk_tree_view_get_selection (tree_view);
1268 gtk_tree_selection_unselect_all (tree_selection);
1273 gtk_tree_view_accessible_ref_selection (AtkSelection *selection,
1282 table = ATK_TABLE (selection);
1283 n_columns = gtk_tree_view_accessible_get_n_columns (table);
1284 n_selected = gtk_tree_view_accessible_get_selected_rows (table, &selected);
1285 if (i >= n_columns * n_selected)
1288 row = selected[i / n_columns];
1291 return gtk_tree_view_accessible_table_ref_at (table, row, i % n_columns);
1295 gtk_tree_view_accessible_get_selection_count (AtkSelection *selection)
1300 table = ATK_TABLE (selection);
1301 n_selected = gtk_tree_view_accessible_get_selected_rows (table, NULL);
1303 n_selected *= gtk_tree_view_accessible_get_n_columns (table);
1308 gtk_tree_view_accessible_is_child_selected (AtkSelection *selection,
1314 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
1318 row = atk_table_get_row_at_index (ATK_TABLE (selection), i);
1320 return gtk_tree_view_accessible_is_row_selected (ATK_TABLE (selection), row);
1323 static void atk_selection_interface_init (AtkSelectionIface *iface)
1325 iface->add_selection = gtk_tree_view_accessible_add_selection;
1326 iface->clear_selection = gtk_tree_view_accessible_clear_selection;
1327 iface->ref_selection = gtk_tree_view_accessible_ref_selection;
1328 iface->get_selection_count = gtk_tree_view_accessible_get_selection_count;
1329 iface->is_child_selected = gtk_tree_view_accessible_is_child_selected;
1332 #define EXTRA_EXPANDER_PADDING 4
1335 gtk_tree_view_accessible_get_cell_area (GtkCellAccessibleParent *parent,
1336 GtkCellAccessible *cell,
1337 GdkRectangle *cell_rect)
1340 GtkTreeView *tree_view;
1341 GtkTreeViewColumn *tv_col;
1343 AtkObject *parent_cell;
1344 GtkTreeViewAccessibleCellInfo *cell_info;
1345 GtkCellAccessible *top_cell;
1347 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1351 tree_view = GTK_TREE_VIEW (widget);
1352 parent_cell = atk_object_get_parent (ATK_OBJECT (cell));
1353 if (parent_cell != ATK_OBJECT (parent))
1354 top_cell = GTK_CELL_ACCESSIBLE (parent_cell);
1357 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), top_cell);
1360 path = cell_info_get_path (cell_info);
1361 tv_col = cell_info->cell_col_ref;
1364 GtkTreeViewColumn *expander_column;
1365 gint focus_line_width;
1367 gtk_tree_view_get_cell_area (tree_view, path, tv_col, cell_rect);
1368 expander_column = gtk_tree_view_get_expander_column (tree_view);
1369 if (expander_column == tv_col)
1372 gtk_widget_style_get (widget,
1373 "expander-size", &expander_size,
1375 cell_rect->x += expander_size + EXTRA_EXPANDER_PADDING;
1376 cell_rect->width -= expander_size + EXTRA_EXPANDER_PADDING;
1378 gtk_widget_style_get (widget,
1379 "focus-line-width", &focus_line_width,
1382 cell_rect->x += focus_line_width;
1383 cell_rect->width -= 2 * focus_line_width;
1385 gtk_tree_path_free (path);
1387 /* A column has more than one renderer so we find the position
1390 if (top_cell != cell)
1397 GtkCellRenderer *renderer;
1399 cell_index = atk_object_get_index_in_parent (ATK_OBJECT (cell));
1400 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
1401 renderer = g_list_nth_data (renderers, cell_index);
1403 found = gtk_tree_view_column_cell_get_position (tv_col, renderer, &cell_start, &cell_width);
1406 cell_rect->x += cell_start;
1407 cell_rect->width = cell_width;
1409 g_list_free (renderers);
1416 gtk_tree_view_accessible_get_cell_extents (GtkCellAccessibleParent *parent,
1417 GtkCellAccessible *cell,
1422 AtkCoordType coord_type)
1425 GtkTreeView *tree_view;
1426 GdkWindow *bin_window;
1427 GdkRectangle cell_rect;
1430 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1434 tree_view = GTK_TREE_VIEW (widget);
1435 gtk_tree_view_accessible_get_cell_area (parent, cell, &cell_rect);
1436 bin_window = gtk_tree_view_get_bin_window (tree_view);
1437 gdk_window_get_origin (bin_window, &w_x, &w_y);
1439 if (coord_type == ATK_XY_WINDOW)
1442 gint x_toplevel, y_toplevel;
1444 window = gdk_window_get_toplevel (bin_window);
1445 gdk_window_get_origin (window, &x_toplevel, &y_toplevel);
1451 *width = cell_rect.width;
1452 *height = cell_rect.height;
1453 if (is_cell_showing (tree_view, &cell_rect))
1455 *x = cell_rect.x + w_x;
1456 *y = cell_rect.y + w_y;
1466 gtk_tree_view_accessible_grab_cell_focus (GtkCellAccessibleParent *parent,
1467 GtkCellAccessible *cell)
1470 GtkTreeView *tree_view;
1471 GtkTreeViewColumn *tv_col;
1473 AtkObject *parent_cell;
1474 AtkObject *cell_object;
1475 GtkTreeViewAccessibleCellInfo *cell_info;
1476 GtkCellRenderer *renderer = NULL;
1477 GtkWidget *toplevel;
1480 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1484 tree_view = GTK_TREE_VIEW (widget);
1486 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1489 cell_object = ATK_OBJECT (cell);
1490 parent_cell = atk_object_get_parent (cell_object);
1491 tv_col = cell_info->cell_col_ref;
1492 if (parent_cell != ATK_OBJECT (parent))
1494 /* GtkCellAccessible is in a GtkContainerCellAccessible.
1495 * The GtkTreeViewColumn has multiple renderers;
1496 * find the corresponding one.
1500 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
1501 index = atk_object_get_index_in_parent (cell_object);
1502 renderer = g_list_nth_data (renderers, index);
1503 g_list_free (renderers);
1505 path = cell_info_get_path (cell_info);
1509 gtk_tree_view_set_cursor_on_cell (tree_view, path, tv_col, renderer, FALSE);
1511 gtk_tree_view_set_cursor (tree_view, path, tv_col, FALSE);
1513 gtk_tree_path_free (path);
1514 gtk_widget_grab_focus (widget);
1515 toplevel = gtk_widget_get_toplevel (widget);
1516 if (gtk_widget_is_toplevel (toplevel))
1518 #ifdef GDK_WINDOWING_X11
1519 gtk_window_present_with_time (GTK_WINDOW (toplevel),
1520 gdk_x11_get_server_time (gtk_widget_get_window (widget)));
1522 gtk_window_present (GTK_WINDOW (toplevel));
1533 gtk_tree_view_accessible_get_child_index (GtkCellAccessibleParent *parent,
1534 GtkCellAccessible *cell)
1536 GtkTreeViewAccessibleCellInfo *cell_info;
1537 GtkTreeView *tree_view;
1539 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1543 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
1545 return cell_info_get_index (tree_view, cell_info);
1549 gtk_cell_accessible_parent_interface_init (GtkCellAccessibleParentIface *iface)
1551 iface->get_cell_extents = gtk_tree_view_accessible_get_cell_extents;
1552 iface->get_cell_area = gtk_tree_view_accessible_get_cell_area;
1553 iface->grab_focus = gtk_tree_view_accessible_grab_cell_focus;
1554 iface->get_child_index = gtk_tree_view_accessible_get_child_index;
1557 /* signal handling */
1560 idle_expand_row (gpointer data)
1562 GtkTreeViewAccessible *accessible = data;
1564 GtkTreeView *tree_view;
1566 GtkTreeModel *tree_model;
1567 gint n_inserted, row;
1569 accessible->idle_expand_id = 0;
1571 path = accessible->idle_expand_path;
1572 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
1574 tree_model = gtk_tree_view_get_model (tree_view);
1578 if (!path || !gtk_tree_model_get_iter (tree_model, &iter, path))
1581 /* Update visibility of cells below expansion row */
1582 traverse_cells (accessible, path, FALSE);
1584 /* Figure out number of visible children, the following test
1587 if (gtk_tree_model_iter_has_child (tree_model, &iter))
1589 GtkTreePath *path_copy;
1591 /* By passing path into this function, we find the number of
1592 * visible children of path.
1594 path_copy = gtk_tree_path_copy (path);
1595 gtk_tree_path_append_index (path_copy, 0);
1598 iterate_thru_children (tree_view, tree_model,
1599 path_copy, NULL, &n_inserted, 0);
1600 gtk_tree_path_free (path_copy);
1604 /* We can get here if the row expanded callback deleted the row */
1608 /* Set expand state */
1609 set_expand_state (tree_view, tree_model, accessible, path, TRUE);
1611 row = get_row_from_tree_path (tree_view, path);
1613 /* shouldn't ever happen */
1615 g_assert_not_reached ();
1617 /* Must add 1 because the "added rows" are below the row being expanded */
1620 g_signal_emit_by_name (accessible, "row-inserted", row, n_inserted);
1622 accessible->idle_expand_path = NULL;
1624 gtk_tree_path_free (path);
1630 row_expanded_cb (GtkTreeView *tree_view,
1635 GtkTreeViewAccessible *accessible;
1637 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1638 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1641 * The visible rectangle has not been updated when this signal is emitted
1642 * so we process the signal when the GTK processing is completed
1644 /* this seems wrong since it overwrites any other pending expand handlers... */
1645 accessible->idle_expand_path = gtk_tree_path_copy (path);
1646 if (accessible->idle_expand_id)
1647 g_source_remove (accessible->idle_expand_id);
1648 accessible->idle_expand_id = gdk_threads_add_idle (idle_expand_row, accessible);
1654 row_collapsed_cb (GtkTreeView *tree_view,
1658 GtkTreeModel *tree_model;
1660 GtkTreeViewAccessible *accessible;
1663 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1664 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1665 tree_model = gtk_tree_view_get_model (tree_view);
1667 /* Update visibility of cells below collapsed row */
1668 traverse_cells (accessible, path, FALSE);
1670 /* Set collapse state */
1671 set_expand_state (tree_view, tree_model, accessible, path, FALSE);
1672 if (accessible->n_children_deleted == 0)
1674 row = get_row_from_tree_path (tree_view, path);
1677 g_signal_emit_by_name (atk_obj, "row-deleted", row,
1678 accessible->n_children_deleted);
1679 accessible->n_children_deleted = 0;
1684 size_allocate_cb (GtkWidget *widget,
1685 GtkAllocation *allocation)
1688 GtkTreeViewAccessible *accessible;
1690 atk_obj = gtk_widget_get_accessible (widget);
1691 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1693 /* If the size allocation changes, the visibility of cells
1694 * may change so update the cells visibility.
1696 traverse_cells (accessible, NULL, FALSE);
1700 selection_changed_cb (GtkTreeSelection *selection,
1703 GtkTreeViewAccessible *accessible;
1704 GtkTreeView *tree_view;
1706 GtkTreeViewAccessibleCellInfo *info;
1707 GtkTreeSelection *tree_selection;
1709 GHashTableIter iter;
1711 accessible = GTK_TREE_VIEW_ACCESSIBLE (data);
1712 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
1716 tree_view = GTK_TREE_VIEW (widget);
1717 tree_selection = gtk_tree_view_get_selection (tree_view);
1719 /* FIXME: clean rows iterates through all cells too */
1720 g_hash_table_iter_init (&iter, accessible->cell_infos);
1721 while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&info))
1723 _gtk_cell_accessible_remove_state (info->cell, ATK_STATE_SELECTED, TRUE);
1725 path = cell_info_get_path (info);
1726 if (path && gtk_tree_selection_path_is_selected (tree_selection, path))
1727 _gtk_cell_accessible_add_state (info->cell, ATK_STATE_SELECTED, TRUE);
1728 gtk_tree_path_free (path);
1730 if (gtk_widget_get_realized (widget))
1731 g_signal_emit_by_name (accessible, "selection-changed");
1735 cursor_changed (GtkTreeView *tree_view,
1736 GtkTreeViewAccessible *accessible)
1740 cell = gtk_tree_view_accessible_ref_focus_cell (tree_view);
1743 if (cell != accessible->focus_cell)
1745 if (accessible->focus_cell)
1747 _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_ACTIVE, FALSE);
1748 _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_FOCUSED, FALSE);
1749 g_object_unref (accessible->focus_cell);
1750 accessible->focus_cell = cell;
1753 if (gtk_widget_has_focus (GTK_WIDGET (tree_view)))
1755 _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_ACTIVE, FALSE);
1756 _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_FOCUSED, FALSE);
1759 g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
1762 g_object_unref (cell);
1767 focus_in (GtkWidget *widget)
1769 GtkTreeView *tree_view;
1770 GtkTreeViewAccessible *accessible;
1771 AtkStateSet *state_set;
1774 tree_view = GTK_TREE_VIEW (widget);
1775 accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
1777 if (accessible->focus_cell == NULL)
1779 cell = gtk_tree_view_accessible_ref_focus_cell (tree_view);
1782 state_set = atk_object_ref_state_set (cell);
1785 if (!atk_state_set_contains_state (state_set, ATK_STATE_FOCUSED))
1787 _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_ACTIVE, FALSE);
1788 accessible->focus_cell = cell;
1789 _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_FOCUSED, FALSE);
1790 g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
1792 g_object_unref (state_set);
1800 focus_out (GtkWidget *widget)
1802 GtkTreeViewAccessible *accessible;
1804 accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
1805 if (accessible->focus_cell)
1807 _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_ACTIVE, FALSE);
1808 _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_FOCUSED, FALSE);
1809 g_object_unref (accessible->focus_cell);
1810 accessible->focus_cell = NULL;
1816 model_row_changed (GtkTreeModel *tree_model,
1821 GtkTreeView *tree_view = GTK_TREE_VIEW (user_data);
1822 GtkTreeViewAccessible *accessible;
1823 GtkTreePath *cell_path;
1824 GtkTreeViewAccessibleCellInfo *cell_info;
1825 GHashTableIter hash_iter;
1827 accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (tree_view)));
1829 /* Loop through our cached cells */
1830 /* Must loop through them all */
1831 g_hash_table_iter_init (&hash_iter, accessible->cell_infos);
1832 while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer *)&cell_info))
1834 cell_path = cell_info_get_path (cell_info);
1836 if (cell_path != NULL)
1838 if (path && gtk_tree_path_compare (cell_path, path) == 0)
1840 if (GTK_IS_RENDERER_CELL_ACCESSIBLE (cell_info->cell))
1841 update_cell_value (GTK_RENDERER_CELL_ACCESSIBLE (cell_info->cell),
1844 gtk_tree_path_free (cell_path);
1847 g_signal_emit_by_name (accessible, "visible-data-changed");
1851 model_row_inserted (GtkTreeModel *tree_model,
1856 GtkTreeView *tree_view = (GtkTreeView *)user_data;
1858 GtkTreeViewAccessible *accessible;
1859 GtkTreePath *path_copy;
1860 gint row, n_inserted, child_row;
1862 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1863 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1865 if (accessible->idle_expand_id)
1867 g_source_remove (accessible->idle_expand_id);
1868 accessible->idle_expand_id = 0;
1870 /* don't do this if the insertion precedes the idle path,
1871 * since it will now be invalid
1873 if (path && accessible->idle_expand_path &&
1874 (gtk_tree_path_compare (path, accessible->idle_expand_path) > 0))
1875 set_expand_state (tree_view, tree_model, accessible, accessible->idle_expand_path, FALSE);
1876 if (accessible->idle_expand_path)
1877 gtk_tree_path_free (accessible->idle_expand_path);
1879 /* Check to see if row is visible */
1880 row = get_row_from_tree_path (tree_view, path);
1882 /* A row insert is not necessarily visible. For example,
1883 * a row can be draged & dropped into another row, which
1884 * causes an insert on the model that isn't visible in the
1885 * view. Only generate a signal if the inserted row is
1890 GtkTreeIter tmp_iter;
1893 gtk_tree_model_get_iter (tree_model, &tmp_iter, path);
1895 /* Figure out number of visible children. */
1896 if (gtk_tree_model_iter_has_child (tree_model, &tmp_iter))
1900 * By passing path into this function, we find the number of
1901 * visible children of path.
1904 /* iterate_thru_children modifies path, we don't want that, so give
1906 path2 = gtk_tree_path_copy (path);
1907 iterate_thru_children (tree_view, tree_model,
1908 path2, NULL, &n_inserted, 0);
1909 gtk_tree_path_free (path2);
1911 /* Must add one to include the row that is being added */
1917 traverse_cells (accessible, path, TRUE);
1919 /* Generate row-inserted signal */
1920 g_signal_emit_by_name (atk_obj, "row-inserted", row, n_inserted);
1922 /* Generate children-changed signals */
1923 n_cols = get_n_columns (tree_view);
1924 for (child_row = row; child_row < (row + n_inserted); child_row++)
1926 for (col = 0; col < n_cols; col++)
1928 /* Pass NULL as the child object, i.e. 4th argument */
1929 g_signal_emit_by_name (atk_obj, "children-changed::add",
1930 ((row * n_cols) + col), NULL, NULL);
1936 /* The row has been inserted inside another row. This can
1937 * cause a row that previously couldn't be expanded to now
1940 path_copy = gtk_tree_path_copy (path);
1941 gtk_tree_path_up (path_copy);
1942 set_expand_state (tree_view, tree_model, accessible, path_copy, TRUE);
1943 gtk_tree_path_free (path_copy);
1948 model_row_deleted (GtkTreeModel *tree_model,
1952 GtkTreeView *tree_view = (GtkTreeView *)user_data;
1953 GtkTreePath *path_copy;
1955 GtkTreeViewAccessible *accessible;
1958 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1959 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1961 if (accessible->idle_expand_id)
1963 g_source_remove (accessible->idle_expand_id);
1964 gtk_tree_path_free (accessible->idle_expand_path);
1965 accessible->idle_expand_id = 0;
1968 traverse_cells (accessible, path, TRUE);
1970 /* If deleting a row with a depth > 1, then this may affect the
1971 * expansion/contraction of its parent(s). Make sure this is
1974 if (gtk_tree_path_get_depth (path) > 1)
1976 path_copy = gtk_tree_path_copy (path);
1977 gtk_tree_path_up (path_copy);
1978 set_expand_state (tree_view, tree_model, accessible, path_copy, TRUE);
1979 gtk_tree_path_free (path_copy);
1981 row = get_row_from_tree_path (tree_view, path);
1983 /* If the row which is deleted is not visible because it is a child of
1984 * a collapsed row then row will be -1
1987 g_signal_emit_by_name (atk_obj, "row-deleted", row,
1988 accessible->n_children_deleted + 1);
1989 accessible->n_children_deleted = 0;
1991 /* Generate children-changed signals */
1992 for (col = 0; col < get_n_columns (tree_view); col++)
1994 /* Pass NULL as the child object, 4th argument */
1995 g_signal_emit_by_name (atk_obj, "children-changed::remove",
1996 ((row * get_n_columns (tree_view)) + col), NULL, NULL);
2000 /* This function gets called when a row is deleted or when rows are
2001 * removed from the view due to a collapse event. Note that the
2002 * count is the number of visible *children* of the deleted row,
2003 * so it does not include the row being deleted.
2005 * As this function is called before the rows are removed we just note
2006 * the number of rows and then deal with it when we get a notification
2007 * that rows were deleted or collapsed.
2010 destroy_count_func (GtkTreeView *tree_view,
2016 GtkTreeViewAccessible *accessible;
2018 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2019 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
2021 if (accessible->n_children_deleted != 0)
2024 accessible->n_children_deleted = count;
2028 model_rows_reordered (GtkTreeModel *tree_model,
2034 GtkTreeView *tree_view = (GtkTreeView *)user_data;
2036 GtkTreeViewAccessible *accessible;
2038 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2039 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
2041 if (accessible->idle_expand_id)
2043 g_source_remove (accessible->idle_expand_id);
2044 gtk_tree_path_free (accessible->idle_expand_path);
2045 accessible->idle_expand_id = 0;
2047 traverse_cells (accessible, NULL, FALSE);
2049 g_signal_emit_by_name (atk_obj, "row-reordered");
2053 set_cell_visibility (GtkTreeView *tree_view,
2054 GtkCellAccessible *cell,
2055 GtkTreeViewColumn *tv_col,
2056 GtkTreePath *tree_path,
2057 gboolean emit_signal)
2059 GdkRectangle cell_rect;
2061 /* Get these three values in tree coords */
2062 if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
2063 gtk_tree_view_get_cell_area (tree_view, tree_path, tv_col, &cell_rect);
2065 cell_rect.height = 0;
2067 if (cell_rect.height > 0)
2069 /* The height will be zero for a cell for which an antecedent
2072 _gtk_cell_accessible_add_state (cell, ATK_STATE_VISIBLE, emit_signal);
2073 if (is_cell_showing (tree_view, &cell_rect))
2074 _gtk_cell_accessible_add_state (cell, ATK_STATE_SHOWING, emit_signal);
2076 _gtk_cell_accessible_remove_state (cell, ATK_STATE_SHOWING, emit_signal);
2080 _gtk_cell_accessible_remove_state (cell, ATK_STATE_VISIBLE, emit_signal);
2081 _gtk_cell_accessible_remove_state (cell, ATK_STATE_SHOWING, emit_signal);
2086 is_cell_showing (GtkTreeView *tree_view,
2087 GdkRectangle *cell_rect)
2089 GdkRectangle rect, *visible_rect;
2090 GdkRectangle rect1, *tree_cell_rect;
2092 gboolean is_showing;
2094 /* A cell is considered "SHOWING" if any part of the cell is
2095 * in the visible area. Other ways we could do this is by a
2096 * cell's midpoint or if the cell is fully in the visible range.
2097 * Since we have the cell_rect x, y, width, height of the cell,
2098 * any of these is easy to compute.
2100 * It is assumed that cell's rectangle is in widget coordinates
2101 * so we must transform to tree cordinates.
2103 visible_rect = ▭
2104 tree_cell_rect = &rect1;
2105 tree_cell_rect->x = cell_rect->x;
2106 tree_cell_rect->y = cell_rect->y;
2107 tree_cell_rect->width = cell_rect->width;
2108 tree_cell_rect->height = cell_rect->height;
2110 gtk_tree_view_get_visible_rect (tree_view, visible_rect);
2111 gtk_tree_view_convert_tree_to_bin_window_coords (tree_view, visible_rect->x,
2112 visible_rect->y, &bx, &by);
2114 if (((tree_cell_rect->x + tree_cell_rect->width) < bx) ||
2115 ((tree_cell_rect->y + tree_cell_rect->height) < by) ||
2116 (tree_cell_rect->x > (bx + visible_rect->width)) ||
2117 (tree_cell_rect->y > (by + visible_rect->height)))
2127 /* This function is called when a cell's flyweight is created in
2128 * gtk_tree_view_accessible_table_ref_at with emit_change_signal
2129 * set to FALSE and in model_row_changed() on receipt of "row-changed"
2130 * signal when emit_change_signal is set to TRUE
2133 update_cell_value (GtkRendererCellAccessible *renderer_cell,
2134 GtkTreeViewAccessible *accessible,
2135 gboolean emit_change_signal)
2137 GtkTreeViewAccessibleCellInfo *cell_info;
2138 GtkTreeView *tree_view;
2139 GtkTreeModel *tree_model;
2142 GList *renderers, *cur_renderer;
2144 GtkRendererCellAccessibleClass *renderer_cell_class;
2145 GtkCellRendererClass *gtk_cell_renderer_class;
2146 GtkCellAccessible *cell;
2149 gboolean is_expander, is_expanded;
2151 renderer_cell_class = GTK_RENDERER_CELL_ACCESSIBLE_GET_CLASS (renderer_cell);
2152 if (renderer_cell->renderer)
2153 gtk_cell_renderer_class = GTK_CELL_RENDERER_GET_CLASS (renderer_cell->renderer);
2155 gtk_cell_renderer_class = NULL;
2157 prop_list = renderer_cell_class->property_list;
2159 cell = GTK_CELL_ACCESSIBLE (renderer_cell);
2160 cell_info = find_cell_info (accessible, cell);
2164 if (emit_change_signal)
2166 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
2167 tree_model = gtk_tree_view_get_model (tree_view);
2168 path = cell_info_get_path (cell_info);
2172 gtk_tree_model_get_iter (tree_model, &iter, path);
2173 is_expander = FALSE;
2174 is_expanded = FALSE;
2175 if (gtk_tree_model_iter_has_child (tree_model, &iter))
2177 GtkTreeViewColumn *expander_tv;
2179 expander_tv = gtk_tree_view_get_expander_column (tree_view);
2180 if (expander_tv == cell_info->cell_col_ref)
2183 is_expanded = gtk_tree_view_row_expanded (tree_view, path);
2186 gtk_tree_path_free (path);
2187 gtk_tree_view_column_cell_set_cell_data (cell_info->cell_col_ref,
2189 is_expander, is_expanded);
2191 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_info->cell_col_ref));
2195 /* If the cell is in a container, its index is used to find the renderer
2196 * in the list. Otherwise, we assume that the cell is represented
2197 * by the first renderer in the list
2199 parent = atk_object_get_parent (ATK_OBJECT (cell));
2201 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2202 cur_renderer = g_list_nth (renderers, atk_object_get_index_in_parent (ATK_OBJECT (cell)));
2204 cur_renderer = renderers;
2206 if (cur_renderer == NULL)
2209 if (gtk_cell_renderer_class)
2213 spec = g_object_class_find_property
2214 (G_OBJECT_CLASS (gtk_cell_renderer_class), *prop_list);
2218 GValue value = G_VALUE_INIT;
2220 g_value_init (&value, spec->value_type);
2221 g_object_get_property (cur_renderer->data, *prop_list, &value);
2222 g_object_set_property (G_OBJECT (renderer_cell->renderer),
2223 *prop_list, &value);
2224 g_value_unset (&value);
2227 g_warning ("Invalid property: %s\n", *prop_list);
2231 g_list_free (renderers);
2233 return _gtk_renderer_cell_accessible_update_cache (renderer_cell, emit_change_signal);
2237 get_row_from_tree_path (GtkTreeView *tree_view,
2240 GtkTreeModel *tree_model;
2241 GtkTreePath *root_tree;
2244 tree_model = gtk_tree_view_get_model (tree_view);
2246 if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
2247 row = gtk_tree_path_get_indices (path)[0];
2250 root_tree = gtk_tree_path_new_first ();
2252 iterate_thru_children (tree_view, tree_model, root_tree, path, &row, 0);
2253 gtk_tree_path_free (root_tree);
2261 /* Helper recursive function that returns an iter to nth row
2263 static GtkTreeIter *
2264 return_iter_nth_row (GtkTreeView *tree_view,
2265 GtkTreeModel *tree_model,
2270 GtkTreePath *current_path;
2271 GtkTreeIter new_iter;
2272 gboolean row_expanded;
2274 current_path = gtk_tree_model_get_path (tree_model, iter);
2275 if (increment == row)
2277 gtk_tree_path_free (current_path);
2281 row_expanded = gtk_tree_view_row_expanded (tree_view, current_path);
2282 gtk_tree_path_free (current_path);
2285 if ((row_expanded && gtk_tree_model_iter_children (tree_model, iter, &new_iter)) ||
2286 (gtk_tree_model_iter_next (tree_model, iter)) ||
2287 (gtk_tree_model_iter_parent (tree_model, iter, &new_iter) &&
2288 (gtk_tree_model_iter_next (tree_model, iter))))
2289 return return_iter_nth_row (tree_view, tree_model, iter,
2296 set_iter_nth_row (GtkTreeView *tree_view,
2300 GtkTreeModel *tree_model;
2302 tree_model = gtk_tree_view_get_model (tree_view);
2303 gtk_tree_model_get_iter_first (tree_model, iter);
2304 iter = return_iter_nth_row (tree_view, tree_model, iter, 0, row);
2307 /* Recursively called until the row specified by orig is found.
2309 * *count will be set to the visible row number of the child
2310 * relative to the row that was initially passed in as tree_path.
2311 * tree_path could be modified by this function.
2313 * *count will be -1 if orig is not found as a child (a row that is
2314 * not visible will not be found, e.g. if the row is inside a
2315 * collapsed row). If NULL is passed in as orig, *count will
2316 * be a count of the visible children.
2318 * NOTE: the value for depth must be 0 when this recursive function
2319 * is initially called, or it may not function as expected.
2322 iterate_thru_children (GtkTreeView *tree_view,
2323 GtkTreeModel *tree_model,
2324 GtkTreePath *tree_path,
2331 if (!gtk_tree_model_get_iter (tree_model, &iter, tree_path))
2334 if (tree_path && orig && !gtk_tree_path_compare (tree_path, orig))
2338 if (tree_path && orig && gtk_tree_path_compare (tree_path, orig) > 0)
2340 /* Past it, so return -1 */
2344 else if (gtk_tree_view_row_expanded (tree_view, tree_path) &&
2345 gtk_tree_model_iter_has_child (tree_model, &iter))
2348 gtk_tree_path_append_index (tree_path, 0);
2349 iterate_thru_children (tree_view, tree_model, tree_path,
2350 orig, count, (depth + 1));
2353 else if (gtk_tree_model_iter_next (tree_model, &iter))
2356 tree_path = gtk_tree_model_get_path (tree_model, &iter);
2359 iterate_thru_children (tree_view, tree_model, tree_path,
2360 orig, count, depth);
2361 gtk_tree_path_free (tree_path);
2365 else if (gtk_tree_path_up (tree_path))
2367 GtkTreeIter temp_iter;
2368 gboolean exit_loop = FALSE;
2369 gint new_depth = depth - 1;
2373 /* Make sure that we back up until we find a row
2374 * where gtk_tree_path_next does not return NULL.
2378 if (gtk_tree_path_get_depth (tree_path) == 0)
2379 /* depth is now zero so */
2381 gtk_tree_path_next (tree_path);
2383 /* Verify that the next row is a valid row! */
2384 exit_loop = gtk_tree_model_get_iter (tree_model, &temp_iter, tree_path);
2388 /* Keep going up until we find a row that has a valid next */
2389 if (gtk_tree_path_get_depth(tree_path) > 1)
2392 gtk_tree_path_up (tree_path);
2396 /* If depth is 1 and gtk_tree_model_get_iter returns FALSE,
2397 * then we are at the last row, so just return.
2407 /* This guarantees that we will stop when we hit the end of the
2413 iterate_thru_children (tree_view, tree_model, tree_path,
2414 orig, count, new_depth);
2418 /* If it gets here, then the path wasn't found. Situations
2419 * that would cause this would be if the path passed in is
2420 * invalid or contained within the last row, but not visible
2421 * because the last row is not expanded. If NULL was passed
2422 * in then a row count is desired, so only set count to -1
2423 * if orig is not NULL.
2431 /* If tree_path is passed in as NULL, then all cells are acted on.
2432 * Otherwise, just act on those cells that are on a row greater than
2433 * the specified tree_path. If inc_row is passed in as TRUE, then rows
2434 * greater and equal to the specified tree_path are acted on.
2436 * The function set_cell_visibility() is called on all cells to be
2437 * acted on to update the visibility of the cell.
2440 traverse_cells (GtkTreeViewAccessible *accessible,
2441 GtkTreePath *tree_path,
2444 GtkTreeViewAccessibleCellInfo *cell_info;
2446 GHashTableIter iter;
2448 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
2452 /* Must loop through them all */
2453 g_hash_table_iter_init (&iter, accessible->cell_infos);
2454 while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&cell_info))
2456 GtkTreePath *row_path;
2457 gboolean act_on_cell;
2459 row_path = cell_info_get_path (cell_info);
2460 g_return_if_fail (row_path != NULL);
2461 if (tree_path == NULL)
2467 comparison = gtk_tree_path_compare (row_path, tree_path);
2468 if ((comparison > 0) ||
2469 (comparison == 0 && inc_row))
2472 act_on_cell = FALSE;
2477 set_cell_visibility (GTK_TREE_VIEW (widget),
2479 cell_info->cell_col_ref,
2482 gtk_tree_path_free (row_path);
2485 g_signal_emit_by_name (accessible, "visible-data-changed");
2488 /* If the tree_path passed in has children, then
2489 * ATK_STATE_EXPANDABLE is set. If the row is expanded
2490 * ATK_STATE_EXPANDED is turned on. If the row is
2491 * collapsed, then ATK_STATE_EXPANDED is removed.
2493 * If the tree_path passed in has no children, then
2494 * ATK_STATE_EXPANDABLE and ATK_STATE_EXPANDED are removed.
2496 * If set_on_ancestor is TRUE, then this function will also
2497 * update all cells that are ancestors of the tree_path.
2500 set_expand_state (GtkTreeView *tree_view,
2501 GtkTreeModel *tree_model,
2502 GtkTreeViewAccessible *accessible,
2503 GtkTreePath *tree_path,
2504 gboolean set_on_ancestor)
2506 GtkTreeViewColumn *expander_tv;
2507 GtkTreeViewAccessibleCellInfo *cell_info;
2508 GtkTreePath *cell_path;
2511 GHashTableIter hash_iter;
2513 g_hash_table_iter_init (&hash_iter, accessible->cell_infos);
2514 while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer *) &cell_info))
2516 cell_path = cell_info_get_path (cell_info);
2519 if (cell_path != NULL)
2521 GtkCellAccessible *cell = GTK_CELL_ACCESSIBLE (cell_info->cell);
2523 expander_tv = gtk_tree_view_get_expander_column (tree_view);
2525 /* Only set state for the cell that is in the column with the
2528 if (expander_tv == cell_info->cell_col_ref)
2530 if (tree_path && gtk_tree_path_compare (cell_path, tree_path) == 0)
2532 else if (set_on_ancestor &&
2533 gtk_tree_path_get_depth (cell_path) <
2534 gtk_tree_path_get_depth (tree_path) &&
2535 gtk_tree_path_is_ancestor (cell_path, tree_path) == 1)
2536 /* Only set if set_on_ancestor was passed in as TRUE */
2540 /* Set ATK_STATE_EXPANDABLE and ATK_STATE_EXPANDED
2541 * for ancestors and found cells.
2545 /* Must check against cell_path since cell_path
2546 * can be equal to or an ancestor of tree_path.
2548 gtk_tree_model_get_iter (tree_model, &iter, cell_path);
2550 /* Set or unset ATK_STATE_EXPANDABLE as appropriate */
2551 if (gtk_tree_model_iter_has_child (tree_model, &iter))
2553 set_cell_expandable (cell);
2555 if (gtk_tree_view_row_expanded (tree_view, cell_path))
2556 _gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDED, TRUE);
2558 _gtk_cell_accessible_remove_state (cell, ATK_STATE_EXPANDED, TRUE);
2562 _gtk_cell_accessible_remove_state (cell, ATK_STATE_EXPANDED, TRUE);
2563 if (_gtk_cell_accessible_remove_state (cell, ATK_STATE_EXPANDABLE, TRUE))
2564 /* The state may have been propagated to the container cell */
2565 if (!GTK_IS_CONTAINER_CELL_ACCESSIBLE (cell))
2566 _gtk_cell_accessible_remove_action_by_name (cell,
2567 "expand or contract");
2570 /* We assume that each cell in the cache once and
2571 * a container cell is before its child cells so we are
2572 * finished if set_on_ancestor is not set to TRUE.
2574 if (!set_on_ancestor)
2578 gtk_tree_path_free (cell_path);
2583 add_cell_actions (GtkCellAccessible *cell,
2586 if (GTK_IS_BOOLEAN_CELL_ACCESSIBLE (cell))
2587 _gtk_cell_accessible_add_action (cell,
2588 "toggle", "toggles the cell",
2589 NULL, toggle_cell_toggled);
2591 _gtk_cell_accessible_add_action (cell,
2592 "edit", "creates a widget in which the contents of the cell can be edited",
2594 _gtk_cell_accessible_add_action (cell,
2595 "activate", "activate the cell",
2596 NULL, activate_cell);
2600 toggle_cell_expanded (GtkCellAccessible *cell)
2602 GtkTreeViewAccessibleCellInfo *cell_info;
2603 GtkTreeView *tree_view;
2606 AtkStateSet *stateset;
2608 parent = atk_object_get_parent (ATK_OBJECT (cell));
2609 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2610 parent = atk_object_get_parent (parent);
2612 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
2616 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
2617 path = cell_info_get_path (cell_info);
2621 stateset = atk_object_ref_state_set (ATK_OBJECT (cell));
2622 if (atk_state_set_contains_state (stateset, ATK_STATE_EXPANDED))
2623 gtk_tree_view_collapse_row (tree_view, path);
2625 gtk_tree_view_expand_row (tree_view, path, TRUE);
2626 g_object_unref (stateset);
2627 gtk_tree_path_free (path);
2631 toggle_cell_toggled (GtkCellAccessible *cell)
2633 GtkTreeViewAccessibleCellInfo *cell_info;
2636 GList *renderers, *cur_renderer;
2638 gboolean is_container_cell = FALSE;
2640 parent = atk_object_get_parent (ATK_OBJECT (cell));
2641 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2643 is_container_cell = TRUE;
2644 parent = atk_object_get_parent (parent);
2647 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
2651 path = cell_info_get_path (cell_info);
2655 /* If the cell is in a container, its index is used to find the
2656 * renderer in the list. Otherwise, we assume that the cell is
2657 * represented by the first renderer in the list
2659 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_info->cell_col_ref));
2660 if (is_container_cell)
2661 cur_renderer = g_list_nth (renderers, atk_object_get_index_in_parent (ATK_OBJECT (cell)));
2663 cur_renderer = renderers;
2667 pathstring = gtk_tree_path_to_string (path);
2668 g_signal_emit_by_name (cur_renderer->data, "toggled", pathstring);
2669 g_free (pathstring);
2672 g_list_free (renderers);
2673 gtk_tree_path_free (path);
2677 edit_cell (GtkCellAccessible *cell)
2679 GtkTreeViewAccessibleCellInfo *cell_info;
2680 GtkTreeView *tree_view;
2684 parent = atk_object_get_parent (ATK_OBJECT (cell));
2685 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2686 parent = atk_object_get_parent (parent);
2688 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
2692 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
2693 path = cell_info_get_path (cell_info);
2696 gtk_tree_view_set_cursor (tree_view, path, cell_info->cell_col_ref, TRUE);
2697 gtk_tree_path_free (path);
2701 activate_cell (GtkCellAccessible *cell)
2703 GtkTreeViewAccessibleCellInfo *cell_info;
2704 GtkTreeView *tree_view;
2708 parent = atk_object_get_parent (ATK_OBJECT (cell));
2709 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2710 parent = atk_object_get_parent (parent);
2712 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
2716 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
2717 path = cell_info_get_path (cell_info);
2720 gtk_tree_view_row_activated (tree_view, path, cell_info->cell_col_ref);
2721 gtk_tree_path_free (path);
2725 cell_destroyed (gpointer data)
2727 GtkTreeViewAccessibleCellInfo *cell_info = data;
2729 cell_info->cell = NULL;
2731 g_hash_table_remove (cell_info->view->cell_infos, cell_info);
2735 cell_info_get_index (GtkTreeView *tree_view,
2736 GtkTreeViewAccessibleCellInfo *info)
2740 index = _gtk_rbtree_node_get_index (info->tree, info->node) + 1;
2741 index *= get_n_columns (tree_view);
2742 index += get_column_number (tree_view, info->cell_col_ref);
2748 cell_info_new (GtkTreeViewAccessible *accessible,
2749 GtkTreeModel *tree_model,
2752 GtkTreeViewColumn *tv_col,
2753 GtkCellAccessible *cell)
2755 GtkTreeViewAccessibleCellInfo *cell_info;
2757 cell_info = g_new (GtkTreeViewAccessibleCellInfo, 1);
2759 cell_info->tree = tree;
2760 cell_info->node = node;
2761 cell_info->cell_col_ref = tv_col;
2762 cell_info->cell = cell;
2763 cell_info->view = accessible;
2765 g_object_set_qdata_full (G_OBJECT (cell),
2766 gtk_tree_view_accessible_get_data_quark (),
2770 g_hash_table_replace (accessible->cell_infos, cell_info, cell_info);
2773 static GtkCellAccessible *
2774 find_cell (GtkTreeViewAccessible *accessible,
2777 GtkTreeViewAccessibleCellInfo lookup, *cell_info;
2778 GtkTreeView *tree_view;
2780 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
2782 if (!_gtk_rbtree_find_index (_gtk_tree_view_get_rbtree (tree_view),
2783 index / get_n_columns (tree_view) - 1,
2787 g_assert_not_reached ();
2789 lookup.cell_col_ref = get_visible_column (tree_view, index % get_n_columns (tree_view));
2791 cell_info = g_hash_table_lookup (accessible->cell_infos, &lookup);
2792 if (cell_info == NULL)
2795 return cell_info->cell;
2799 connect_model_signals (GtkTreeView *view,
2800 GtkTreeViewAccessible *accessible)
2804 obj = G_OBJECT (accessible->tree_model);
2805 g_signal_connect_data (obj, "row-changed",
2806 G_CALLBACK (model_row_changed), view, NULL, 0);
2807 g_signal_connect_data (obj, "row-inserted",
2808 G_CALLBACK (model_row_inserted), view, NULL,
2810 g_signal_connect_data (obj, "row-deleted",
2811 G_CALLBACK (model_row_deleted), view, NULL,
2813 g_signal_connect_data (obj, "rows-reordered",
2814 G_CALLBACK (model_rows_reordered), view, NULL,
2819 disconnect_model_signals (GtkTreeViewAccessible *accessible)
2824 obj = G_OBJECT (accessible->tree_model);
2825 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
2826 g_signal_handlers_disconnect_by_func (obj, model_row_changed, widget);
2827 g_signal_handlers_disconnect_by_func (obj, model_row_inserted, widget);
2828 g_signal_handlers_disconnect_by_func (obj, model_row_deleted, widget);
2829 g_signal_handlers_disconnect_by_func (obj, model_rows_reordered, widget);
2832 /* Returns the column number of the specified GtkTreeViewColumn
2833 * The column must be visible.
2836 get_column_number (GtkTreeView *treeview,
2837 GtkTreeViewColumn *column)
2839 GtkTreeViewColumn *cur;
2844 for (i = 0; i < gtk_tree_view_get_n_columns (treeview); i++)
2846 cur = gtk_tree_view_get_column (treeview, i);
2848 if (!gtk_tree_view_column_get_visible (cur))
2857 g_return_val_if_fail (i < gtk_tree_view_get_n_columns (treeview), 0);
2863 get_index (GtkTreeView *tree_view,
2869 gint *indices = NULL;
2873 depth = gtk_tree_path_get_depth (path);
2874 indices = gtk_tree_path_get_indices (path);
2879 GtkTreePath *copy_path;
2880 GtkTreeModel *model;
2882 model = gtk_tree_view_get_model (tree_view);
2883 copy_path = gtk_tree_path_copy (path);
2884 gtk_tree_path_up (copy_path);
2885 count_rows (model, NULL, copy_path, &index, 0, depth);
2886 gtk_tree_path_free (copy_path);
2890 index += indices[depth - 1];
2891 index *= get_n_columns (tree_view);
2892 index += actual_column;
2896 /* The function count_rows counts the number of rows starting at iter
2897 * and ending at end_path. The value of level is the depth of iter and
2898 * the value of depth is the depth of end_path. Rows at depth before
2899 * end_path are counted. This functions counts rows which are not visible
2900 * because an ancestor is collapsed.
2903 count_rows (GtkTreeModel *model,
2905 GtkTreePath *end_path,
2910 GtkTreeIter child_iter;
2916 *count += gtk_tree_model_iter_n_children (model, iter);
2918 if (gtk_tree_model_get_flags (model) & GTK_TREE_MODEL_LIST_ONLY)
2924 if (gtk_tree_model_iter_children (model, &child_iter, iter))
2926 gboolean ret_val = TRUE;
2930 if (level == depth - 1)
2932 GtkTreePath *iter_path;
2933 gboolean finished = FALSE;
2935 iter_path = gtk_tree_model_get_path (model, &child_iter);
2936 if (end_path && gtk_tree_path_compare (iter_path, end_path) >= 0)
2938 gtk_tree_path_free (iter_path);
2942 if (gtk_tree_model_iter_has_child (model, &child_iter))
2943 count_rows (model, &child_iter, end_path, count, level, depth);
2944 ret_val = gtk_tree_model_iter_next (model, &child_iter);
2950 get_rbtree_column_from_index (GtkTreeView *tree_view,
2954 GtkTreeViewColumn **column)
2956 guint n_columns = get_n_columns (tree_view);
2960 /* First row is the column headers */
2967 g_return_val_if_fail (node != NULL, FALSE);
2969 if (!_gtk_rbtree_find_index (_gtk_tree_view_get_rbtree (tree_view),
2978 *column = get_visible_column (tree_view, index % n_columns);
2979 if (*column == NULL)
2986 set_cell_expandable (GtkCellAccessible *cell)
2988 if (_gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDABLE, FALSE))
2989 _gtk_cell_accessible_add_action (cell,
2990 "expand or contract",
2991 "expands or contracts the row in the tree view containing this cell",
2992 NULL, toggle_cell_expanded);
2995 static GtkTreeViewAccessibleCellInfo *
2996 find_cell_info (GtkTreeViewAccessible *accessible,
2997 GtkCellAccessible *cell)
3001 parent = atk_object_get_parent (ATK_OBJECT (cell));
3002 while (parent != ATK_OBJECT (accessible))
3004 cell = GTK_CELL_ACCESSIBLE (parent);
3005 parent = atk_object_get_parent (ATK_OBJECT (cell));
3008 return g_object_get_qdata (G_OBJECT (cell),
3009 gtk_tree_view_accessible_get_data_quark ());
3013 get_header_from_column (GtkTreeViewColumn *tv_col)
3016 GtkWidget *header_widget;
3021 header_widget = gtk_tree_view_column_get_button (tv_col);
3024 rc = gtk_widget_get_accessible (header_widget);
3032 * _gtk_rbtree_get_ancestor_node:
3033 * @ancestor: the ancestor tree
3034 * @child_tree: the potential child's tree
3035 * @child_node: the potential child's node
3037 * Finds the node that is the ancestor of @child_tree and @child_node
3038 * and belongs to @ancestor. If @ancestor is not an ancestor tree
3039 * of @child_node, %NULL is returned.
3041 * Returns: the ancestor node or %NULL if @ancestor is not an ancestor.
3044 _gtk_rbtree_get_ancestor_node (GtkRBTree *ancestor,
3045 GtkRBTree *child_tree,
3046 GtkRBNode *child_node)
3048 while (child_tree != NULL)
3050 if (child_tree == ancestor)
3053 child_node = child_tree->parent_node;
3054 child_tree = child_tree->parent_tree;
3061 _gtk_tree_view_accessible_remove (GtkTreeView *treeview,
3065 GtkTreeViewAccessibleCellInfo *cell_info;
3066 GHashTableIter iter;
3067 GtkTreeViewAccessible *accessible;
3069 accessible = GTK_TREE_VIEW_ACCESSIBLE (_gtk_widget_peek_accessible (GTK_WIDGET (treeview)));
3070 if (accessible == NULL)
3073 /* if this shows up in profiles, special-case node->children == NULL */
3075 g_hash_table_iter_init (&iter, accessible->cell_infos);
3076 while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&cell_info))
3078 GtkRBNode *child_node = _gtk_rbtree_get_ancestor_node (tree,
3082 if (child_node == NULL)
3085 if (node == NULL || node == child_node)
3086 g_hash_table_iter_remove (&iter);
3090 /* NB: id is not checked, only columns < id are.
3091 * This is important so the function works for notification of removal of a column */
3093 to_visible_column_id (GtkTreeView *treeview,
3101 for (i = 0; i < id; i++)
3103 GtkTreeViewColumn *column = gtk_tree_view_get_column (treeview, i);
3105 if (!gtk_tree_view_column_get_visible (column))
3109 return id - invisible;
3113 _gtk_tree_view_accessible_do_add_column (GtkTreeViewAccessible *accessible,
3114 GtkTreeView *treeview,
3115 GtkTreeViewColumn *column,
3118 guint row, n_rows, n_cols;
3120 /* Generate column-inserted signal */
3121 g_signal_emit_by_name (accessible, "column-inserted", id, 1);
3123 n_rows = get_n_rows (treeview);
3124 n_cols = get_n_columns (treeview);
3126 /* Generate children-changed signals */
3127 for (row = 0; row <= n_rows; row++)
3129 /* Pass NULL as the child object, i.e. 4th argument */
3130 g_signal_emit_by_name (accessible, "children-changed::add",
3131 (row * n_cols) + id, NULL, NULL);
3134 traverse_cells (accessible, NULL, FALSE);
3138 _gtk_tree_view_accessible_add_column (GtkTreeView *treeview,
3139 GtkTreeViewColumn *column,
3144 if (!gtk_tree_view_column_get_visible (column))
3147 obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
3151 _gtk_tree_view_accessible_do_add_column (GTK_TREE_VIEW_ACCESSIBLE (obj),
3154 to_visible_column_id (treeview, id));
3158 _gtk_tree_view_accessible_do_remove_column (GtkTreeViewAccessible *accessible,
3159 GtkTreeView *treeview,
3160 GtkTreeViewColumn *column,
3163 GtkTreeViewAccessibleCellInfo *cell_info;
3164 GHashTableIter iter;
3166 guint row, n_rows, n_cols;
3168 /* Clean column from cache */
3169 g_hash_table_iter_init (&iter, accessible->cell_infos);
3170 while (g_hash_table_iter_next (&iter, NULL, &value))
3173 if (cell_info->cell_col_ref == column)
3174 g_hash_table_iter_remove (&iter);
3177 /* Generate column-deleted signal */
3178 g_signal_emit_by_name (accessible, "column-deleted", id, 1);
3180 n_rows = get_n_rows (treeview);
3181 n_cols = get_n_columns (treeview);
3183 /* Generate children-changed signals */
3184 for (row = 0; row <= n_rows; row++)
3186 /* Pass NULL as the child object, 4th argument */
3187 g_signal_emit_by_name (accessible, "children-changed::remove",
3188 (row * n_cols) + id, NULL, NULL);
3191 traverse_cells (accessible, NULL, FALSE);
3195 _gtk_tree_view_accessible_remove_column (GtkTreeView *treeview,
3196 GtkTreeViewColumn *column,
3201 if (!gtk_tree_view_column_get_visible (column))
3204 obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
3208 _gtk_tree_view_accessible_do_remove_column (GTK_TREE_VIEW_ACCESSIBLE (obj),
3211 to_visible_column_id (treeview, id));
3215 _gtk_tree_view_accessible_reorder_column (GtkTreeView *treeview,
3216 GtkTreeViewColumn *column)
3220 obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
3224 g_signal_emit_by_name (obj, "column-reordered");
3226 traverse_cells (GTK_TREE_VIEW_ACCESSIBLE (obj), NULL, FALSE);
3230 _gtk_tree_view_accessible_toggle_visibility (GtkTreeView *treeview,
3231 GtkTreeViewColumn *column)
3236 obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
3240 id = get_column_number (treeview, column);
3242 if (gtk_tree_view_column_get_visible (column))
3243 _gtk_tree_view_accessible_do_add_column (GTK_TREE_VIEW_ACCESSIBLE (obj),
3248 _gtk_tree_view_accessible_do_remove_column (GTK_TREE_VIEW_ACCESSIBLE (obj),