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 is not exactly one renderer in the list,
610 if (renderer_list == NULL || 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 for (l = renderer_list; l; l = l->next)
633 renderer = GTK_CELL_RENDERER (l->data);
635 if (GTK_IS_CELL_RENDERER_TEXT (renderer))
637 g_object_get (G_OBJECT (renderer), "editable", &editable, NULL);
638 child = _gtk_text_cell_accessible_new ();
640 else if (GTK_IS_CELL_RENDERER_TOGGLE (renderer))
641 child = _gtk_boolean_cell_accessible_new ();
642 else if (GTK_IS_CELL_RENDERER_PIXBUF (renderer))
643 child = _gtk_image_cell_accessible_new ();
645 child = _gtk_renderer_cell_accessible_new ();
647 cell = GTK_CELL_ACCESSIBLE (child);
648 renderer_cell = GTK_RENDERER_CELL_ACCESSIBLE (child);
650 /* Create the GtkTreeViewAccessibleCellInfo for this cell */
651 if (parent == ATK_OBJECT (accessible))
652 cell_info_new (accessible, tree_model, tree, node, tv_col, cell);
654 _gtk_cell_accessible_initialise (cell, widget, parent);
657 _gtk_container_cell_accessible_add_child (container, cell);
659 update_cell_value (renderer_cell, accessible, FALSE);
661 /* Add the actions appropriate for this cell */
662 add_cell_actions (cell, editable);
664 /* Set state if it is expandable */
667 set_cell_expandable (cell);
669 _gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDED, FALSE);
672 /* If the column is visible, sets the cell's state */
673 if (gtk_tree_view_column_get_visible (tv_col))
674 set_cell_visibility (tree_view, cell, tv_col, path, FALSE);
676 /* If the row is selected, all cells on the row are selected */
677 selection = gtk_tree_view_get_selection (tree_view);
679 if (gtk_tree_selection_path_is_selected (selection, path))
680 _gtk_cell_accessible_add_state (cell, ATK_STATE_SELECTED, FALSE);
682 _gtk_cell_accessible_add_state (cell, ATK_STATE_FOCUSABLE, FALSE);
683 if (focus_index == i)
685 accessible->focus_cell = g_object_ref (cell);
686 _gtk_cell_accessible_add_state (cell, ATK_STATE_FOCUSED, FALSE);
687 g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
690 g_list_free (renderer_list);
692 child = ATK_OBJECT (container);
694 if (expander_tv == tv_col)
696 AtkRelationSet *relation_set;
697 AtkObject *accessible_array[1];
698 AtkRelation* relation;
699 AtkObject *parent_node;
701 relation_set = atk_object_ref_relation_set (ATK_OBJECT (child));
703 gtk_tree_path_up (path);
704 if (gtk_tree_path_get_depth (path) == 0)
710 parent_index = get_index (tree_view, path, i % get_n_columns (tree_view));
711 parent_node = atk_object_ref_accessible_child (obj, parent_index);
713 accessible_array[0] = parent_node;
714 relation = atk_relation_new (accessible_array, 1,
715 ATK_RELATION_NODE_CHILD_OF);
716 atk_relation_set_add (relation_set, relation);
717 atk_object_add_relationship (parent_node, ATK_RELATION_NODE_PARENT_OF, child);
718 g_object_unref (relation);
719 g_object_unref (relation_set);
721 gtk_tree_path_free (path);
723 /* We do not increase the reference count here; when g_object_unref()
724 * is called for the cell then cell_destroyed() is called and this
725 * removes the cell from the cache.
731 gtk_tree_view_accessible_ref_state_set (AtkObject *obj)
733 AtkStateSet *state_set;
736 state_set = ATK_OBJECT_CLASS (_gtk_tree_view_accessible_parent_class)->ref_state_set (obj);
737 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (obj));
740 atk_state_set_add_state (state_set, ATK_STATE_MANAGES_DESCENDANTS);
746 _gtk_tree_view_accessible_class_init (GtkTreeViewAccessibleClass *klass)
748 AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
749 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
750 GtkAccessibleClass *accessible_class = (GtkAccessibleClass*)klass;
751 GtkWidgetAccessibleClass *widget_class = (GtkWidgetAccessibleClass*)klass;
752 GtkContainerAccessibleClass *container_class = (GtkContainerAccessibleClass*)klass;
754 class->get_n_children = gtk_tree_view_accessible_get_n_children;
755 class->ref_child = gtk_tree_view_accessible_ref_child;
756 class->ref_state_set = gtk_tree_view_accessible_ref_state_set;
757 class->initialize = gtk_tree_view_accessible_initialize;
759 widget_class->notify_gtk = gtk_tree_view_accessible_notify_gtk;
761 accessible_class->connect_widget_destroyed = gtk_tree_view_accessible_connect_widget_destroyed;
763 /* The children of a GtkTreeView are the buttons at the top of the columns
764 * we do not represent these as children so we do not want to report
765 * children added or deleted when these changed.
767 container_class->add_gtk = NULL;
768 container_class->remove_gtk = NULL;
770 gobject_class->finalize = gtk_tree_view_accessible_finalize;
774 _gtk_tree_view_accessible_init (GtkTreeViewAccessible *view)
779 get_focus_index (GtkTreeView *tree_view)
781 GtkTreePath *focus_path;
782 GtkTreeViewColumn *focus_column;
785 gtk_tree_view_get_cursor (tree_view, &focus_path, &focus_column);
786 if (focus_path && focus_column)
787 index = get_index (tree_view, focus_path,
788 get_column_number (tree_view, focus_column));
793 gtk_tree_path_free (focus_path);
798 /* This function returns a reference to the accessible object
799 * for the cell in the treeview which has focus, if any
802 gtk_tree_view_accessible_ref_focus_cell (GtkTreeView *tree_view)
804 AtkObject *focus_cell = NULL;
808 focus_index = get_focus_index (tree_view);
809 if (focus_index >= 0)
811 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
812 focus_cell = atk_object_ref_accessible_child (atk_obj, focus_index);
821 gtk_tree_view_accessible_ref_accessible_at_point (AtkComponent *component,
824 AtkCoordType coord_type)
827 GtkTreeView *tree_view;
829 GtkTreeViewColumn *tv_column;
834 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (component));
838 tree_view = GTK_TREE_VIEW (widget);
840 atk_component_get_extents (component, &x_pos, &y_pos, NULL, NULL, coord_type);
841 gtk_tree_view_convert_widget_to_bin_window_coords (tree_view, x, y, &bx, &by);
842 ret_val = gtk_tree_view_get_path_at_pos (tree_view,
843 bx - x_pos, by - y_pos,
844 &path, &tv_column, NULL, NULL);
849 column = get_column_number (tree_view, tv_column);
850 index = get_index (tree_view, path, column);
851 gtk_tree_path_free (path);
853 return gtk_tree_view_accessible_ref_child (ATK_OBJECT (component), index);
860 atk_component_interface_init (AtkComponentIface *iface)
862 iface->ref_accessible_at_point = gtk_tree_view_accessible_ref_accessible_at_point;
868 gtk_tree_view_accessible_get_index_at (AtkTable *table,
875 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
879 n_cols = atk_table_get_n_columns (table);
880 n_rows = atk_table_get_n_rows (table);
882 if (row >= n_rows || column >= n_cols)
885 return (row + 1) * n_cols + column;
889 gtk_tree_view_accessible_get_column_at_index (AtkTable *table,
895 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
899 if (index >= gtk_tree_view_accessible_get_n_children (ATK_OBJECT (table)))
902 n_columns = get_n_columns (GTK_TREE_VIEW (widget));
904 /* checked by the n_children() check above */
905 g_assert (n_columns > 0);
907 return index % n_columns;
911 gtk_tree_view_accessible_get_row_at_index (AtkTable *table,
915 GtkTreeView *tree_view;
917 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
921 tree_view = GTK_TREE_VIEW (widget);
923 index /= get_n_columns (tree_view);
925 if (index >= get_n_rows (tree_view))
932 gtk_tree_view_accessible_table_ref_at (AtkTable *table,
938 index = gtk_tree_view_accessible_get_index_at (table, row, column);
942 return gtk_tree_view_accessible_ref_child (ATK_OBJECT (table), index);
946 gtk_tree_view_accessible_get_n_rows (AtkTable *table)
950 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
954 return get_n_rows (GTK_TREE_VIEW (widget));
958 gtk_tree_view_accessible_get_n_columns (AtkTable *table)
962 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
966 return get_n_columns (GTK_TREE_VIEW (widget));
970 gtk_tree_view_accessible_is_row_selected (AtkTable *table,
974 GtkTreeView *tree_view;
975 GtkTreeSelection *selection;
978 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
985 tree_view = GTK_TREE_VIEW (widget);
986 selection = gtk_tree_view_get_selection (tree_view);
988 set_iter_nth_row (tree_view, &iter, row);
989 return gtk_tree_selection_iter_is_selected (selection, &iter);
993 gtk_tree_view_accessible_is_selected (AtkTable *table,
997 return gtk_tree_view_accessible_is_row_selected (table, row);
1001 get_selected_rows (GtkTreeModel *model,
1006 GPtrArray *array = (GPtrArray *)data;
1008 g_ptr_array_add (array, gtk_tree_path_copy (path));
1012 gtk_tree_view_accessible_get_selected_rows (AtkTable *table,
1013 gint **rows_selected)
1016 GtkTreeView *tree_view;
1017 GtkTreeModel *tree_model;
1019 GtkTreeSelection *selection;
1020 GtkTreePath *tree_path;
1023 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1027 tree_view = GTK_TREE_VIEW (widget);
1028 selection = gtk_tree_view_get_selection (tree_view);
1030 switch (gtk_tree_selection_get_mode (selection))
1032 case GTK_SELECTION_SINGLE:
1033 case GTK_SELECTION_BROWSE:
1034 if (gtk_tree_selection_get_selected (selection, &tree_model, &iter))
1040 *rows_selected = g_new (gint, 1);
1041 tree_path = gtk_tree_model_get_path (tree_model, &iter);
1042 row = get_row_from_tree_path (tree_view, tree_path);
1043 gtk_tree_path_free (tree_path);
1045 /* shouldn't ever happen */
1046 g_return_val_if_fail (row != -1, 0);
1048 *rows_selected[0] = row;
1053 case GTK_SELECTION_MULTIPLE:
1055 GPtrArray *array = g_ptr_array_new();
1057 gtk_tree_selection_selected_foreach (selection, get_selected_rows, array);
1058 ret_val = array->len;
1060 if (rows_selected && ret_val)
1064 *rows_selected = g_new (gint, ret_val);
1065 for (i = 0; i < ret_val; i++)
1069 tree_path = (GtkTreePath *) g_ptr_array_index (array, i);
1070 row = get_row_from_tree_path (tree_view, tree_path);
1071 gtk_tree_path_free (tree_path);
1072 (*rows_selected)[i] = row;
1075 g_ptr_array_free (array, FALSE);
1078 case GTK_SELECTION_NONE:
1085 gtk_tree_view_accessible_add_row_selection (AtkTable *table,
1089 GtkTreeView *tree_view;
1090 GtkTreeModel *tree_model;
1091 GtkTreeSelection *selection;
1092 GtkTreePath *tree_path;
1093 GtkTreeIter iter_to_row;
1095 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1099 if (!gtk_tree_view_accessible_is_row_selected (table, row))
1101 tree_view = GTK_TREE_VIEW (widget);
1102 tree_model = gtk_tree_view_get_model (tree_view);
1103 selection = gtk_tree_view_get_selection (tree_view);
1105 if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
1107 tree_path = gtk_tree_path_new ();
1108 gtk_tree_path_append_index (tree_path, row);
1109 gtk_tree_selection_select_path (selection,tree_path);
1110 gtk_tree_path_free (tree_path);
1114 set_iter_nth_row (tree_view, &iter_to_row, row);
1115 gtk_tree_selection_select_iter (selection, &iter_to_row);
1119 return gtk_tree_view_accessible_is_row_selected (table, row);
1123 gtk_tree_view_accessible_remove_row_selection (AtkTable *table,
1127 GtkTreeView *tree_view;
1128 GtkTreeSelection *selection;
1130 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1134 tree_view = GTK_TREE_VIEW (widget);
1135 selection = gtk_tree_view_get_selection (tree_view);
1137 if (gtk_tree_view_accessible_is_row_selected (table, row))
1139 gtk_tree_selection_unselect_all (selection);
1147 gtk_tree_view_accessible_get_column_header (AtkTable *table,
1151 GtkTreeView *tree_view;
1152 GtkTreeViewColumn *tv_col;
1154 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1158 tree_view = GTK_TREE_VIEW (widget);
1159 if (in_col < 0 || in_col >= get_n_columns (tree_view))
1162 tv_col = get_visible_column (tree_view, in_col);
1163 return get_header_from_column (tv_col);
1166 static const gchar *
1167 gtk_tree_view_accessible_get_column_description (AtkTable *table,
1171 GtkTreeView *tree_view;
1172 GtkTreeViewColumn *tv_col;
1174 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (table));
1178 tree_view = GTK_TREE_VIEW (widget);
1179 if (in_col < 0 || in_col >= get_n_columns (tree_view))
1182 tv_col = get_visible_column (tree_view, in_col);
1183 return gtk_tree_view_column_get_title (tv_col);
1187 atk_table_interface_init (AtkTableIface *iface)
1189 iface->ref_at = gtk_tree_view_accessible_table_ref_at;
1190 iface->get_n_rows = gtk_tree_view_accessible_get_n_rows;
1191 iface->get_n_columns = gtk_tree_view_accessible_get_n_columns;
1192 iface->get_index_at = gtk_tree_view_accessible_get_index_at;
1193 iface->get_column_at_index = gtk_tree_view_accessible_get_column_at_index;
1194 iface->get_row_at_index = gtk_tree_view_accessible_get_row_at_index;
1195 iface->is_row_selected = gtk_tree_view_accessible_is_row_selected;
1196 iface->is_selected = gtk_tree_view_accessible_is_selected;
1197 iface->get_selected_rows = gtk_tree_view_accessible_get_selected_rows;
1198 iface->add_row_selection = gtk_tree_view_accessible_add_row_selection;
1199 iface->remove_row_selection = gtk_tree_view_accessible_remove_row_selection;
1200 iface->get_column_extent_at = NULL;
1201 iface->get_row_extent_at = NULL;
1202 iface->get_column_header = gtk_tree_view_accessible_get_column_header;
1203 iface->get_column_description = gtk_tree_view_accessible_get_column_description;
1206 /* atkselection.h */
1209 gtk_tree_view_accessible_add_selection (AtkSelection *selection,
1216 table = ATK_TABLE (selection);
1217 n_columns = gtk_tree_view_accessible_get_n_columns (table);
1221 row = gtk_tree_view_accessible_get_row_at_index (table, i);
1222 return gtk_tree_view_accessible_add_row_selection (table, row);
1226 gtk_tree_view_accessible_clear_selection (AtkSelection *selection)
1229 GtkTreeView *tree_view;
1230 GtkTreeSelection *tree_selection;
1232 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
1236 tree_view = GTK_TREE_VIEW (widget);
1237 tree_selection = gtk_tree_view_get_selection (tree_view);
1239 gtk_tree_selection_unselect_all (tree_selection);
1244 gtk_tree_view_accessible_ref_selection (AtkSelection *selection,
1253 table = ATK_TABLE (selection);
1254 n_columns = gtk_tree_view_accessible_get_n_columns (table);
1255 n_selected = gtk_tree_view_accessible_get_selected_rows (table, &selected);
1256 if (i >= n_columns * n_selected)
1259 row = selected[i / n_columns];
1262 return gtk_tree_view_accessible_table_ref_at (table, row, i % n_columns);
1266 gtk_tree_view_accessible_get_selection_count (AtkSelection *selection)
1271 table = ATK_TABLE (selection);
1272 n_selected = gtk_tree_view_accessible_get_selected_rows (table, NULL);
1274 n_selected *= gtk_tree_view_accessible_get_n_columns (table);
1279 gtk_tree_view_accessible_is_child_selected (AtkSelection *selection,
1285 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (selection));
1289 row = atk_table_get_row_at_index (ATK_TABLE (selection), i);
1291 return gtk_tree_view_accessible_is_row_selected (ATK_TABLE (selection), row);
1294 static void atk_selection_interface_init (AtkSelectionIface *iface)
1296 iface->add_selection = gtk_tree_view_accessible_add_selection;
1297 iface->clear_selection = gtk_tree_view_accessible_clear_selection;
1298 iface->ref_selection = gtk_tree_view_accessible_ref_selection;
1299 iface->get_selection_count = gtk_tree_view_accessible_get_selection_count;
1300 iface->is_child_selected = gtk_tree_view_accessible_is_child_selected;
1303 #define EXTRA_EXPANDER_PADDING 4
1306 gtk_tree_view_accessible_get_cell_area (GtkCellAccessibleParent *parent,
1307 GtkCellAccessible *cell,
1308 GdkRectangle *cell_rect)
1311 GtkTreeView *tree_view;
1312 GtkTreeViewColumn *tv_col;
1314 AtkObject *parent_cell;
1315 GtkTreeViewAccessibleCellInfo *cell_info;
1316 GtkCellAccessible *top_cell;
1318 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1322 tree_view = GTK_TREE_VIEW (widget);
1323 parent_cell = atk_object_get_parent (ATK_OBJECT (cell));
1324 if (parent_cell != ATK_OBJECT (parent))
1325 top_cell = GTK_CELL_ACCESSIBLE (parent_cell);
1328 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), top_cell);
1331 path = cell_info_get_path (cell_info);
1332 tv_col = cell_info->cell_col_ref;
1335 GtkTreeViewColumn *expander_column;
1336 gint focus_line_width;
1338 gtk_tree_view_get_cell_area (tree_view, path, tv_col, cell_rect);
1339 expander_column = gtk_tree_view_get_expander_column (tree_view);
1340 if (expander_column == tv_col)
1343 gtk_widget_style_get (widget,
1344 "expander-size", &expander_size,
1346 cell_rect->x += expander_size + EXTRA_EXPANDER_PADDING;
1347 cell_rect->width -= expander_size + EXTRA_EXPANDER_PADDING;
1349 gtk_widget_style_get (widget,
1350 "focus-line-width", &focus_line_width,
1353 cell_rect->x += focus_line_width;
1354 cell_rect->width -= 2 * focus_line_width;
1356 gtk_tree_path_free (path);
1358 /* A column has more than one renderer so we find the position
1361 if (top_cell != cell)
1368 GtkCellRenderer *renderer;
1370 cell_index = atk_object_get_index_in_parent (ATK_OBJECT (cell));
1371 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
1372 renderer = g_list_nth_data (renderers, cell_index);
1374 found = gtk_tree_view_column_cell_get_position (tv_col, renderer, &cell_start, &cell_width);
1377 cell_rect->x += cell_start;
1378 cell_rect->width = cell_width;
1380 g_list_free (renderers);
1387 gtk_tree_view_accessible_get_cell_extents (GtkCellAccessibleParent *parent,
1388 GtkCellAccessible *cell,
1393 AtkCoordType coord_type)
1396 GtkTreeView *tree_view;
1397 GdkWindow *bin_window;
1398 GdkRectangle cell_rect;
1401 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1405 tree_view = GTK_TREE_VIEW (widget);
1406 gtk_tree_view_accessible_get_cell_area (parent, cell, &cell_rect);
1407 bin_window = gtk_tree_view_get_bin_window (tree_view);
1408 gdk_window_get_origin (bin_window, &w_x, &w_y);
1410 if (coord_type == ATK_XY_WINDOW)
1413 gint x_toplevel, y_toplevel;
1415 window = gdk_window_get_toplevel (bin_window);
1416 gdk_window_get_origin (window, &x_toplevel, &y_toplevel);
1422 *width = cell_rect.width;
1423 *height = cell_rect.height;
1424 if (is_cell_showing (tree_view, &cell_rect))
1426 *x = cell_rect.x + w_x;
1427 *y = cell_rect.y + w_y;
1437 gtk_tree_view_accessible_grab_cell_focus (GtkCellAccessibleParent *parent,
1438 GtkCellAccessible *cell)
1441 GtkTreeView *tree_view;
1442 GtkTreeViewColumn *tv_col;
1444 AtkObject *parent_cell;
1445 AtkObject *cell_object;
1446 GtkTreeViewAccessibleCellInfo *cell_info;
1447 GtkCellRenderer *renderer = NULL;
1448 GtkWidget *toplevel;
1451 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (parent));
1455 tree_view = GTK_TREE_VIEW (widget);
1457 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1460 cell_object = ATK_OBJECT (cell);
1461 parent_cell = atk_object_get_parent (cell_object);
1462 tv_col = cell_info->cell_col_ref;
1463 if (parent_cell != ATK_OBJECT (parent))
1465 /* GtkCellAccessible is in a GtkContainerCellAccessible.
1466 * The GtkTreeViewColumn has multiple renderers;
1467 * find the corresponding one.
1471 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (tv_col));
1472 index = atk_object_get_index_in_parent (cell_object);
1473 renderer = g_list_nth_data (renderers, index);
1474 g_list_free (renderers);
1476 path = cell_info_get_path (cell_info);
1480 gtk_tree_view_set_cursor_on_cell (tree_view, path, tv_col, renderer, FALSE);
1482 gtk_tree_view_set_cursor (tree_view, path, tv_col, FALSE);
1484 gtk_tree_path_free (path);
1485 gtk_widget_grab_focus (widget);
1486 toplevel = gtk_widget_get_toplevel (widget);
1487 if (gtk_widget_is_toplevel (toplevel))
1489 #ifdef GDK_WINDOWING_X11
1490 gtk_window_present_with_time (GTK_WINDOW (toplevel),
1491 gdk_x11_get_server_time (gtk_widget_get_window (widget)));
1493 gtk_window_present (GTK_WINDOW (toplevel));
1504 gtk_tree_view_accessible_get_child_index (GtkCellAccessibleParent *parent,
1505 GtkCellAccessible *cell)
1507 GtkTreeViewAccessibleCellInfo *cell_info;
1508 GtkTreeView *tree_view;
1510 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1514 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
1516 return cell_info_get_index (tree_view, cell_info);
1520 gtk_tree_view_accessible_set_cell_data (GtkCellAccessibleParent *parent,
1521 GtkCellAccessible *cell)
1523 GtkTreeViewAccessibleCellInfo *cell_info;
1524 GtkTreeView *treeview;
1525 gboolean is_expander, is_expanded;
1526 GtkTreeModel *model;
1530 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
1534 treeview = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
1535 model = gtk_tree_view_get_model (treeview);
1537 if (GTK_RBNODE_FLAG_SET (cell_info->node, GTK_RBNODE_IS_PARENT) &&
1538 cell_info->cell_col_ref == gtk_tree_view_get_expander_column (treeview))
1541 is_expanded = cell_info->node->children != NULL;
1545 is_expander = FALSE;
1546 is_expanded = FALSE;
1549 path = cell_info_get_path (cell_info);
1551 !gtk_tree_model_get_iter (model, &iter, path))
1553 /* We only track valid cells, this should never happen */
1554 g_return_if_reached ();
1557 gtk_tree_view_column_cell_set_cell_data (cell_info->cell_col_ref,
1565 gtk_cell_accessible_parent_interface_init (GtkCellAccessibleParentIface *iface)
1567 iface->get_cell_extents = gtk_tree_view_accessible_get_cell_extents;
1568 iface->get_cell_area = gtk_tree_view_accessible_get_cell_area;
1569 iface->grab_focus = gtk_tree_view_accessible_grab_cell_focus;
1570 iface->get_child_index = gtk_tree_view_accessible_get_child_index;
1571 iface->set_cell_data = gtk_tree_view_accessible_set_cell_data;
1574 /* signal handling */
1577 idle_expand_row (gpointer data)
1579 GtkTreeViewAccessible *accessible = data;
1581 GtkTreeView *tree_view;
1583 GtkTreeModel *tree_model;
1584 gint n_inserted, row;
1586 accessible->idle_expand_id = 0;
1588 path = accessible->idle_expand_path;
1589 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
1591 tree_model = gtk_tree_view_get_model (tree_view);
1595 if (!path || !gtk_tree_model_get_iter (tree_model, &iter, path))
1598 /* Update visibility of cells below expansion row */
1599 traverse_cells (accessible, path, FALSE);
1601 /* Figure out number of visible children, the following test
1604 if (gtk_tree_model_iter_has_child (tree_model, &iter))
1606 GtkTreePath *path_copy;
1608 /* By passing path into this function, we find the number of
1609 * visible children of path.
1611 path_copy = gtk_tree_path_copy (path);
1612 gtk_tree_path_append_index (path_copy, 0);
1615 iterate_thru_children (tree_view, tree_model,
1616 path_copy, NULL, &n_inserted, 0);
1617 gtk_tree_path_free (path_copy);
1621 /* We can get here if the row expanded callback deleted the row */
1625 /* Set expand state */
1626 set_expand_state (tree_view, tree_model, accessible, path, TRUE);
1628 row = get_row_from_tree_path (tree_view, path);
1630 /* shouldn't ever happen */
1632 g_assert_not_reached ();
1634 /* Must add 1 because the "added rows" are below the row being expanded */
1637 g_signal_emit_by_name (accessible, "row-inserted", row, n_inserted);
1639 accessible->idle_expand_path = NULL;
1641 gtk_tree_path_free (path);
1647 row_expanded_cb (GtkTreeView *tree_view,
1652 GtkTreeViewAccessible *accessible;
1654 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1655 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1658 * The visible rectangle has not been updated when this signal is emitted
1659 * so we process the signal when the GTK processing is completed
1661 /* this seems wrong since it overwrites any other pending expand handlers... */
1662 accessible->idle_expand_path = gtk_tree_path_copy (path);
1663 if (accessible->idle_expand_id)
1664 g_source_remove (accessible->idle_expand_id);
1665 accessible->idle_expand_id = gdk_threads_add_idle (idle_expand_row, accessible);
1671 row_collapsed_cb (GtkTreeView *tree_view,
1675 GtkTreeModel *tree_model;
1677 GtkTreeViewAccessible *accessible;
1680 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1681 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1682 tree_model = gtk_tree_view_get_model (tree_view);
1684 /* Update visibility of cells below collapsed row */
1685 traverse_cells (accessible, path, FALSE);
1687 /* Set collapse state */
1688 set_expand_state (tree_view, tree_model, accessible, path, FALSE);
1689 if (accessible->n_children_deleted == 0)
1691 row = get_row_from_tree_path (tree_view, path);
1694 g_signal_emit_by_name (atk_obj, "row-deleted", row,
1695 accessible->n_children_deleted);
1696 accessible->n_children_deleted = 0;
1701 size_allocate_cb (GtkWidget *widget,
1702 GtkAllocation *allocation)
1705 GtkTreeViewAccessible *accessible;
1707 atk_obj = gtk_widget_get_accessible (widget);
1708 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1710 /* If the size allocation changes, the visibility of cells
1711 * may change so update the cells visibility.
1713 traverse_cells (accessible, NULL, FALSE);
1717 selection_changed_cb (GtkTreeSelection *selection,
1720 GtkTreeViewAccessible *accessible;
1721 GtkTreeView *tree_view;
1723 GtkTreeViewAccessibleCellInfo *info;
1724 GtkTreeSelection *tree_selection;
1726 GHashTableIter iter;
1728 accessible = GTK_TREE_VIEW_ACCESSIBLE (data);
1729 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
1733 tree_view = GTK_TREE_VIEW (widget);
1734 tree_selection = gtk_tree_view_get_selection (tree_view);
1736 /* FIXME: clean rows iterates through all cells too */
1737 g_hash_table_iter_init (&iter, accessible->cell_infos);
1738 while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&info))
1740 _gtk_cell_accessible_remove_state (info->cell, ATK_STATE_SELECTED, TRUE);
1742 path = cell_info_get_path (info);
1743 if (path && gtk_tree_selection_path_is_selected (tree_selection, path))
1744 _gtk_cell_accessible_add_state (info->cell, ATK_STATE_SELECTED, TRUE);
1745 gtk_tree_path_free (path);
1747 if (gtk_widget_get_realized (widget))
1748 g_signal_emit_by_name (accessible, "selection-changed");
1752 cursor_changed (GtkTreeView *tree_view,
1753 GtkTreeViewAccessible *accessible)
1757 cell = gtk_tree_view_accessible_ref_focus_cell (tree_view);
1760 if (cell != accessible->focus_cell)
1762 if (accessible->focus_cell)
1764 _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_ACTIVE, FALSE);
1765 _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_FOCUSED, FALSE);
1766 g_object_unref (accessible->focus_cell);
1767 accessible->focus_cell = cell;
1770 if (gtk_widget_has_focus (GTK_WIDGET (tree_view)))
1772 _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_ACTIVE, FALSE);
1773 _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_FOCUSED, FALSE);
1776 g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
1779 g_object_unref (cell);
1784 focus_in (GtkWidget *widget)
1786 GtkTreeView *tree_view;
1787 GtkTreeViewAccessible *accessible;
1788 AtkStateSet *state_set;
1791 tree_view = GTK_TREE_VIEW (widget);
1792 accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
1794 if (accessible->focus_cell == NULL)
1796 cell = gtk_tree_view_accessible_ref_focus_cell (tree_view);
1799 state_set = atk_object_ref_state_set (cell);
1802 if (!atk_state_set_contains_state (state_set, ATK_STATE_FOCUSED))
1804 _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_ACTIVE, FALSE);
1805 accessible->focus_cell = cell;
1806 _gtk_cell_accessible_add_state (GTK_CELL_ACCESSIBLE (cell), ATK_STATE_FOCUSED, FALSE);
1807 g_signal_emit_by_name (accessible, "active-descendant-changed", cell);
1809 g_object_unref (state_set);
1817 focus_out (GtkWidget *widget)
1819 GtkTreeViewAccessible *accessible;
1821 accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (widget));
1822 if (accessible->focus_cell)
1824 _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_ACTIVE, FALSE);
1825 _gtk_cell_accessible_remove_state (GTK_CELL_ACCESSIBLE (accessible->focus_cell), ATK_STATE_FOCUSED, FALSE);
1826 g_object_unref (accessible->focus_cell);
1827 accessible->focus_cell = NULL;
1833 model_row_changed (GtkTreeModel *tree_model,
1838 GtkTreeView *tree_view = GTK_TREE_VIEW (user_data);
1839 GtkTreeViewAccessible *accessible;
1840 GtkTreePath *cell_path;
1841 GtkTreeViewAccessibleCellInfo *cell_info;
1842 GHashTableIter hash_iter;
1844 accessible = GTK_TREE_VIEW_ACCESSIBLE (gtk_widget_get_accessible (GTK_WIDGET (tree_view)));
1846 /* Loop through our cached cells */
1847 /* Must loop through them all */
1848 g_hash_table_iter_init (&hash_iter, accessible->cell_infos);
1849 while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer *)&cell_info))
1851 cell_path = cell_info_get_path (cell_info);
1853 if (cell_path != NULL)
1855 if (path && gtk_tree_path_compare (cell_path, path) == 0)
1857 if (GTK_IS_RENDERER_CELL_ACCESSIBLE (cell_info->cell))
1858 update_cell_value (GTK_RENDERER_CELL_ACCESSIBLE (cell_info->cell),
1861 gtk_tree_path_free (cell_path);
1864 g_signal_emit_by_name (accessible, "visible-data-changed");
1868 model_row_inserted (GtkTreeModel *tree_model,
1873 GtkTreeView *tree_view = (GtkTreeView *)user_data;
1875 GtkTreeViewAccessible *accessible;
1876 GtkTreePath *path_copy;
1877 gint row, n_inserted, child_row;
1879 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1880 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1882 if (accessible->idle_expand_id)
1884 g_source_remove (accessible->idle_expand_id);
1885 accessible->idle_expand_id = 0;
1887 /* don't do this if the insertion precedes the idle path,
1888 * since it will now be invalid
1890 if (path && accessible->idle_expand_path &&
1891 (gtk_tree_path_compare (path, accessible->idle_expand_path) > 0))
1892 set_expand_state (tree_view, tree_model, accessible, accessible->idle_expand_path, FALSE);
1893 if (accessible->idle_expand_path)
1894 gtk_tree_path_free (accessible->idle_expand_path);
1896 /* Check to see if row is visible */
1897 row = get_row_from_tree_path (tree_view, path);
1899 /* A row insert is not necessarily visible. For example,
1900 * a row can be draged & dropped into another row, which
1901 * causes an insert on the model that isn't visible in the
1902 * view. Only generate a signal if the inserted row is
1907 GtkTreeIter tmp_iter;
1910 gtk_tree_model_get_iter (tree_model, &tmp_iter, path);
1912 /* Figure out number of visible children. */
1913 if (gtk_tree_model_iter_has_child (tree_model, &tmp_iter))
1917 * By passing path into this function, we find the number of
1918 * visible children of path.
1921 /* iterate_thru_children modifies path, we don't want that, so give
1923 path2 = gtk_tree_path_copy (path);
1924 iterate_thru_children (tree_view, tree_model,
1925 path2, NULL, &n_inserted, 0);
1926 gtk_tree_path_free (path2);
1928 /* Must add one to include the row that is being added */
1934 traverse_cells (accessible, path, TRUE);
1936 /* Generate row-inserted signal */
1937 g_signal_emit_by_name (atk_obj, "row-inserted", row, n_inserted);
1939 /* Generate children-changed signals */
1940 n_cols = get_n_columns (tree_view);
1941 for (child_row = row; child_row < (row + n_inserted); child_row++)
1943 for (col = 0; col < n_cols; col++)
1945 /* Pass NULL as the child object, i.e. 4th argument */
1946 g_signal_emit_by_name (atk_obj, "children-changed::add",
1947 ((row * n_cols) + col), NULL, NULL);
1953 /* The row has been inserted inside another row. This can
1954 * cause a row that previously couldn't be expanded to now
1957 path_copy = gtk_tree_path_copy (path);
1958 gtk_tree_path_up (path_copy);
1959 set_expand_state (tree_view, tree_model, accessible, path_copy, TRUE);
1960 gtk_tree_path_free (path_copy);
1965 model_row_deleted (GtkTreeModel *tree_model,
1969 GtkTreeView *tree_view = (GtkTreeView *)user_data;
1970 GtkTreePath *path_copy;
1972 GtkTreeViewAccessible *accessible;
1975 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
1976 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
1978 if (accessible->idle_expand_id)
1980 g_source_remove (accessible->idle_expand_id);
1981 gtk_tree_path_free (accessible->idle_expand_path);
1982 accessible->idle_expand_id = 0;
1985 traverse_cells (accessible, path, TRUE);
1987 /* If deleting a row with a depth > 1, then this may affect the
1988 * expansion/contraction of its parent(s). Make sure this is
1991 if (gtk_tree_path_get_depth (path) > 1)
1993 path_copy = gtk_tree_path_copy (path);
1994 gtk_tree_path_up (path_copy);
1995 set_expand_state (tree_view, tree_model, accessible, path_copy, TRUE);
1996 gtk_tree_path_free (path_copy);
1998 row = get_row_from_tree_path (tree_view, path);
2000 /* If the row which is deleted is not visible because it is a child of
2001 * a collapsed row then row will be -1
2004 g_signal_emit_by_name (atk_obj, "row-deleted", row,
2005 accessible->n_children_deleted + 1);
2006 accessible->n_children_deleted = 0;
2008 /* Generate children-changed signals */
2009 for (col = 0; col < get_n_columns (tree_view); col++)
2011 /* Pass NULL as the child object, 4th argument */
2012 g_signal_emit_by_name (atk_obj, "children-changed::remove",
2013 ((row * get_n_columns (tree_view)) + col), NULL, NULL);
2017 /* This function gets called when a row is deleted or when rows are
2018 * removed from the view due to a collapse event. Note that the
2019 * count is the number of visible *children* of the deleted row,
2020 * so it does not include the row being deleted.
2022 * As this function is called before the rows are removed we just note
2023 * the number of rows and then deal with it when we get a notification
2024 * that rows were deleted or collapsed.
2027 destroy_count_func (GtkTreeView *tree_view,
2033 GtkTreeViewAccessible *accessible;
2035 atk_obj = gtk_widget_get_accessible (GTK_WIDGET (tree_view));
2036 accessible = GTK_TREE_VIEW_ACCESSIBLE (atk_obj);
2038 if (accessible->n_children_deleted != 0)
2041 accessible->n_children_deleted = count;
2045 _gtk_tree_view_accessible_reorder (GtkTreeView *treeview)
2047 GtkTreeViewAccessible *accessible;
2049 accessible = GTK_TREE_VIEW_ACCESSIBLE (_gtk_widget_peek_accessible (GTK_WIDGET (treeview)));
2050 if (accessible == NULL)
2053 if (accessible->idle_expand_id)
2055 g_source_remove (accessible->idle_expand_id);
2056 gtk_tree_path_free (accessible->idle_expand_path);
2057 accessible->idle_expand_id = 0;
2059 traverse_cells (accessible, NULL, FALSE);
2061 g_signal_emit_by_name (accessible, "row-reordered");
2065 set_cell_visibility (GtkTreeView *tree_view,
2066 GtkCellAccessible *cell,
2067 GtkTreeViewColumn *tv_col,
2068 GtkTreePath *tree_path,
2069 gboolean emit_signal)
2071 GdkRectangle cell_rect;
2073 /* Get these three values in tree coords */
2074 if (gtk_widget_get_realized (GTK_WIDGET (tree_view)))
2075 gtk_tree_view_get_cell_area (tree_view, tree_path, tv_col, &cell_rect);
2077 cell_rect.height = 0;
2079 if (cell_rect.height > 0)
2081 /* The height will be zero for a cell for which an antecedent
2084 _gtk_cell_accessible_add_state (cell, ATK_STATE_VISIBLE, emit_signal);
2085 if (is_cell_showing (tree_view, &cell_rect))
2086 _gtk_cell_accessible_add_state (cell, ATK_STATE_SHOWING, emit_signal);
2088 _gtk_cell_accessible_remove_state (cell, ATK_STATE_SHOWING, emit_signal);
2092 _gtk_cell_accessible_remove_state (cell, ATK_STATE_VISIBLE, emit_signal);
2093 _gtk_cell_accessible_remove_state (cell, ATK_STATE_SHOWING, emit_signal);
2098 is_cell_showing (GtkTreeView *tree_view,
2099 GdkRectangle *cell_rect)
2101 GdkRectangle rect, *visible_rect;
2102 GdkRectangle rect1, *tree_cell_rect;
2104 gboolean is_showing;
2106 /* A cell is considered "SHOWING" if any part of the cell is
2107 * in the visible area. Other ways we could do this is by a
2108 * cell's midpoint or if the cell is fully in the visible range.
2109 * Since we have the cell_rect x, y, width, height of the cell,
2110 * any of these is easy to compute.
2112 * It is assumed that cell's rectangle is in widget coordinates
2113 * so we must transform to tree cordinates.
2115 visible_rect = ▭
2116 tree_cell_rect = &rect1;
2117 tree_cell_rect->x = cell_rect->x;
2118 tree_cell_rect->y = cell_rect->y;
2119 tree_cell_rect->width = cell_rect->width;
2120 tree_cell_rect->height = cell_rect->height;
2122 gtk_tree_view_get_visible_rect (tree_view, visible_rect);
2123 gtk_tree_view_convert_tree_to_bin_window_coords (tree_view, visible_rect->x,
2124 visible_rect->y, &bx, &by);
2126 if (((tree_cell_rect->x + tree_cell_rect->width) < bx) ||
2127 ((tree_cell_rect->y + tree_cell_rect->height) < by) ||
2128 (tree_cell_rect->x > (bx + visible_rect->width)) ||
2129 (tree_cell_rect->y > (by + visible_rect->height)))
2139 /* This function is called when a cell's flyweight is created in
2140 * gtk_tree_view_accessible_table_ref_at with emit_change_signal
2141 * set to FALSE and in model_row_changed() on receipt of "row-changed"
2142 * signal when emit_change_signal is set to TRUE
2145 update_cell_value (GtkRendererCellAccessible *renderer_cell,
2146 GtkTreeViewAccessible *accessible,
2147 gboolean emit_change_signal)
2149 GtkTreeViewAccessibleCellInfo *cell_info;
2150 GtkTreeView *tree_view;
2151 GtkTreeModel *tree_model;
2154 GList *renderers, *cur_renderer;
2156 GtkRendererCellAccessibleClass *renderer_cell_class;
2157 GtkCellRendererClass *gtk_cell_renderer_class;
2158 GtkCellAccessible *cell;
2161 gboolean is_expander, is_expanded;
2163 renderer_cell_class = GTK_RENDERER_CELL_ACCESSIBLE_GET_CLASS (renderer_cell);
2164 if (renderer_cell->renderer)
2165 gtk_cell_renderer_class = GTK_CELL_RENDERER_GET_CLASS (renderer_cell->renderer);
2167 gtk_cell_renderer_class = NULL;
2169 prop_list = renderer_cell_class->property_list;
2171 cell = GTK_CELL_ACCESSIBLE (renderer_cell);
2172 cell_info = find_cell_info (accessible, cell);
2176 if (emit_change_signal)
2178 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
2179 tree_model = gtk_tree_view_get_model (tree_view);
2180 path = cell_info_get_path (cell_info);
2184 gtk_tree_model_get_iter (tree_model, &iter, path);
2185 is_expander = FALSE;
2186 is_expanded = FALSE;
2187 if (gtk_tree_model_iter_has_child (tree_model, &iter))
2189 GtkTreeViewColumn *expander_tv;
2191 expander_tv = gtk_tree_view_get_expander_column (tree_view);
2192 if (expander_tv == cell_info->cell_col_ref)
2195 is_expanded = gtk_tree_view_row_expanded (tree_view, path);
2198 gtk_tree_path_free (path);
2199 gtk_tree_view_column_cell_set_cell_data (cell_info->cell_col_ref,
2201 is_expander, is_expanded);
2203 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_info->cell_col_ref));
2207 /* If the cell is in a container, its index is used to find the renderer
2208 * in the list. Otherwise, we assume that the cell is represented
2209 * by the first renderer in the list
2211 parent = atk_object_get_parent (ATK_OBJECT (cell));
2213 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2214 cur_renderer = g_list_nth (renderers, atk_object_get_index_in_parent (ATK_OBJECT (cell)));
2216 cur_renderer = renderers;
2218 if (cur_renderer == NULL)
2221 if (gtk_cell_renderer_class)
2225 spec = g_object_class_find_property
2226 (G_OBJECT_CLASS (gtk_cell_renderer_class), *prop_list);
2230 GValue value = G_VALUE_INIT;
2232 g_value_init (&value, spec->value_type);
2233 g_object_get_property (cur_renderer->data, *prop_list, &value);
2234 g_object_set_property (G_OBJECT (renderer_cell->renderer),
2235 *prop_list, &value);
2236 g_value_unset (&value);
2239 g_warning ("Invalid property: %s\n", *prop_list);
2243 g_list_free (renderers);
2245 return _gtk_renderer_cell_accessible_update_cache (renderer_cell, emit_change_signal);
2249 get_row_from_tree_path (GtkTreeView *tree_view,
2252 GtkTreeModel *tree_model;
2253 GtkTreePath *root_tree;
2256 tree_model = gtk_tree_view_get_model (tree_view);
2258 if (gtk_tree_model_get_flags (tree_model) & GTK_TREE_MODEL_LIST_ONLY)
2259 row = gtk_tree_path_get_indices (path)[0];
2262 root_tree = gtk_tree_path_new_first ();
2264 iterate_thru_children (tree_view, tree_model, root_tree, path, &row, 0);
2265 gtk_tree_path_free (root_tree);
2273 /* Helper recursive function that returns an iter to nth row
2275 static GtkTreeIter *
2276 return_iter_nth_row (GtkTreeView *tree_view,
2277 GtkTreeModel *tree_model,
2282 GtkTreePath *current_path;
2283 GtkTreeIter new_iter;
2284 gboolean row_expanded;
2286 current_path = gtk_tree_model_get_path (tree_model, iter);
2287 if (increment == row)
2289 gtk_tree_path_free (current_path);
2293 row_expanded = gtk_tree_view_row_expanded (tree_view, current_path);
2294 gtk_tree_path_free (current_path);
2297 if ((row_expanded && gtk_tree_model_iter_children (tree_model, iter, &new_iter)) ||
2298 (gtk_tree_model_iter_next (tree_model, iter)) ||
2299 (gtk_tree_model_iter_parent (tree_model, iter, &new_iter) &&
2300 (gtk_tree_model_iter_next (tree_model, iter))))
2301 return return_iter_nth_row (tree_view, tree_model, iter,
2308 set_iter_nth_row (GtkTreeView *tree_view,
2312 GtkTreeModel *tree_model;
2314 tree_model = gtk_tree_view_get_model (tree_view);
2315 gtk_tree_model_get_iter_first (tree_model, iter);
2316 iter = return_iter_nth_row (tree_view, tree_model, iter, 0, row);
2319 /* Recursively called until the row specified by orig is found.
2321 * *count will be set to the visible row number of the child
2322 * relative to the row that was initially passed in as tree_path.
2323 * tree_path could be modified by this function.
2325 * *count will be -1 if orig is not found as a child (a row that is
2326 * not visible will not be found, e.g. if the row is inside a
2327 * collapsed row). If NULL is passed in as orig, *count will
2328 * be a count of the visible children.
2330 * NOTE: the value for depth must be 0 when this recursive function
2331 * is initially called, or it may not function as expected.
2334 iterate_thru_children (GtkTreeView *tree_view,
2335 GtkTreeModel *tree_model,
2336 GtkTreePath *tree_path,
2343 if (!gtk_tree_model_get_iter (tree_model, &iter, tree_path))
2346 if (tree_path && orig && !gtk_tree_path_compare (tree_path, orig))
2350 if (tree_path && orig && gtk_tree_path_compare (tree_path, orig) > 0)
2352 /* Past it, so return -1 */
2356 else if (gtk_tree_view_row_expanded (tree_view, tree_path) &&
2357 gtk_tree_model_iter_has_child (tree_model, &iter))
2360 gtk_tree_path_append_index (tree_path, 0);
2361 iterate_thru_children (tree_view, tree_model, tree_path,
2362 orig, count, (depth + 1));
2365 else if (gtk_tree_model_iter_next (tree_model, &iter))
2368 tree_path = gtk_tree_model_get_path (tree_model, &iter);
2371 iterate_thru_children (tree_view, tree_model, tree_path,
2372 orig, count, depth);
2373 gtk_tree_path_free (tree_path);
2377 else if (gtk_tree_path_up (tree_path))
2379 GtkTreeIter temp_iter;
2380 gboolean exit_loop = FALSE;
2381 gint new_depth = depth - 1;
2385 /* Make sure that we back up until we find a row
2386 * where gtk_tree_path_next does not return NULL.
2390 if (gtk_tree_path_get_depth (tree_path) == 0)
2391 /* depth is now zero so */
2393 gtk_tree_path_next (tree_path);
2395 /* Verify that the next row is a valid row! */
2396 exit_loop = gtk_tree_model_get_iter (tree_model, &temp_iter, tree_path);
2400 /* Keep going up until we find a row that has a valid next */
2401 if (gtk_tree_path_get_depth(tree_path) > 1)
2404 gtk_tree_path_up (tree_path);
2408 /* If depth is 1 and gtk_tree_model_get_iter returns FALSE,
2409 * then we are at the last row, so just return.
2419 /* This guarantees that we will stop when we hit the end of the
2425 iterate_thru_children (tree_view, tree_model, tree_path,
2426 orig, count, new_depth);
2430 /* If it gets here, then the path wasn't found. Situations
2431 * that would cause this would be if the path passed in is
2432 * invalid or contained within the last row, but not visible
2433 * because the last row is not expanded. If NULL was passed
2434 * in then a row count is desired, so only set count to -1
2435 * if orig is not NULL.
2443 /* If tree_path is passed in as NULL, then all cells are acted on.
2444 * Otherwise, just act on those cells that are on a row greater than
2445 * the specified tree_path. If inc_row is passed in as TRUE, then rows
2446 * greater and equal to the specified tree_path are acted on.
2448 * The function set_cell_visibility() is called on all cells to be
2449 * acted on to update the visibility of the cell.
2452 traverse_cells (GtkTreeViewAccessible *accessible,
2453 GtkTreePath *tree_path,
2456 GtkTreeViewAccessibleCellInfo *cell_info;
2458 GHashTableIter iter;
2460 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
2464 /* Must loop through them all */
2465 g_hash_table_iter_init (&iter, accessible->cell_infos);
2466 while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&cell_info))
2468 GtkTreePath *row_path;
2469 gboolean act_on_cell;
2471 row_path = cell_info_get_path (cell_info);
2472 g_return_if_fail (row_path != NULL);
2473 if (tree_path == NULL)
2479 comparison = gtk_tree_path_compare (row_path, tree_path);
2480 if ((comparison > 0) ||
2481 (comparison == 0 && inc_row))
2484 act_on_cell = FALSE;
2489 set_cell_visibility (GTK_TREE_VIEW (widget),
2491 cell_info->cell_col_ref,
2494 gtk_tree_path_free (row_path);
2497 g_signal_emit_by_name (accessible, "visible-data-changed");
2500 /* If the tree_path passed in has children, then
2501 * ATK_STATE_EXPANDABLE is set. If the row is expanded
2502 * ATK_STATE_EXPANDED is turned on. If the row is
2503 * collapsed, then ATK_STATE_EXPANDED is removed.
2505 * If the tree_path passed in has no children, then
2506 * ATK_STATE_EXPANDABLE and ATK_STATE_EXPANDED are removed.
2508 * If set_on_ancestor is TRUE, then this function will also
2509 * update all cells that are ancestors of the tree_path.
2512 set_expand_state (GtkTreeView *tree_view,
2513 GtkTreeModel *tree_model,
2514 GtkTreeViewAccessible *accessible,
2515 GtkTreePath *tree_path,
2516 gboolean set_on_ancestor)
2518 GtkTreeViewColumn *expander_tv;
2519 GtkTreeViewAccessibleCellInfo *cell_info;
2520 GtkTreePath *cell_path;
2523 GHashTableIter hash_iter;
2525 g_hash_table_iter_init (&hash_iter, accessible->cell_infos);
2526 while (g_hash_table_iter_next (&hash_iter, NULL, (gpointer *) &cell_info))
2528 cell_path = cell_info_get_path (cell_info);
2531 if (cell_path != NULL)
2533 GtkCellAccessible *cell = GTK_CELL_ACCESSIBLE (cell_info->cell);
2535 expander_tv = gtk_tree_view_get_expander_column (tree_view);
2537 /* Only set state for the cell that is in the column with the
2540 if (expander_tv == cell_info->cell_col_ref)
2542 if (tree_path && gtk_tree_path_compare (cell_path, tree_path) == 0)
2544 else if (set_on_ancestor &&
2545 gtk_tree_path_get_depth (cell_path) <
2546 gtk_tree_path_get_depth (tree_path) &&
2547 gtk_tree_path_is_ancestor (cell_path, tree_path) == 1)
2548 /* Only set if set_on_ancestor was passed in as TRUE */
2552 /* Set ATK_STATE_EXPANDABLE and ATK_STATE_EXPANDED
2553 * for ancestors and found cells.
2557 /* Must check against cell_path since cell_path
2558 * can be equal to or an ancestor of tree_path.
2560 gtk_tree_model_get_iter (tree_model, &iter, cell_path);
2562 /* Set or unset ATK_STATE_EXPANDABLE as appropriate */
2563 if (gtk_tree_model_iter_has_child (tree_model, &iter))
2565 set_cell_expandable (cell);
2567 if (gtk_tree_view_row_expanded (tree_view, cell_path))
2568 _gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDED, TRUE);
2570 _gtk_cell_accessible_remove_state (cell, ATK_STATE_EXPANDED, TRUE);
2574 _gtk_cell_accessible_remove_state (cell, ATK_STATE_EXPANDED, TRUE);
2575 if (_gtk_cell_accessible_remove_state (cell, ATK_STATE_EXPANDABLE, TRUE))
2576 /* The state may have been propagated to the container cell */
2577 if (!GTK_IS_CONTAINER_CELL_ACCESSIBLE (cell))
2578 _gtk_cell_accessible_remove_action_by_name (cell,
2579 "expand or contract");
2582 /* We assume that each cell in the cache once and
2583 * a container cell is before its child cells so we are
2584 * finished if set_on_ancestor is not set to TRUE.
2586 if (!set_on_ancestor)
2590 gtk_tree_path_free (cell_path);
2595 add_cell_actions (GtkCellAccessible *cell,
2598 if (GTK_IS_BOOLEAN_CELL_ACCESSIBLE (cell))
2599 _gtk_cell_accessible_add_action (cell,
2600 "toggle", "toggles the cell",
2601 NULL, toggle_cell_toggled);
2603 _gtk_cell_accessible_add_action (cell,
2604 "edit", "creates a widget in which the contents of the cell can be edited",
2606 _gtk_cell_accessible_add_action (cell,
2607 "activate", "activate the cell",
2608 NULL, activate_cell);
2612 toggle_cell_expanded (GtkCellAccessible *cell)
2614 GtkTreeViewAccessibleCellInfo *cell_info;
2615 GtkTreeView *tree_view;
2618 AtkStateSet *stateset;
2620 parent = atk_object_get_parent (ATK_OBJECT (cell));
2621 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2622 parent = atk_object_get_parent (parent);
2624 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
2628 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
2629 path = cell_info_get_path (cell_info);
2633 stateset = atk_object_ref_state_set (ATK_OBJECT (cell));
2634 if (atk_state_set_contains_state (stateset, ATK_STATE_EXPANDED))
2635 gtk_tree_view_collapse_row (tree_view, path);
2637 gtk_tree_view_expand_row (tree_view, path, TRUE);
2638 g_object_unref (stateset);
2639 gtk_tree_path_free (path);
2643 toggle_cell_toggled (GtkCellAccessible *cell)
2645 GtkTreeViewAccessibleCellInfo *cell_info;
2648 GList *renderers, *cur_renderer;
2650 gboolean is_container_cell = FALSE;
2652 parent = atk_object_get_parent (ATK_OBJECT (cell));
2653 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2655 is_container_cell = TRUE;
2656 parent = atk_object_get_parent (parent);
2659 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
2663 path = cell_info_get_path (cell_info);
2667 /* If the cell is in a container, its index is used to find the
2668 * renderer in the list. Otherwise, we assume that the cell is
2669 * represented by the first renderer in the list
2671 renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (cell_info->cell_col_ref));
2672 if (is_container_cell)
2673 cur_renderer = g_list_nth (renderers, atk_object_get_index_in_parent (ATK_OBJECT (cell)));
2675 cur_renderer = renderers;
2679 pathstring = gtk_tree_path_to_string (path);
2680 g_signal_emit_by_name (cur_renderer->data, "toggled", pathstring);
2681 g_free (pathstring);
2684 g_list_free (renderers);
2685 gtk_tree_path_free (path);
2689 edit_cell (GtkCellAccessible *cell)
2691 GtkTreeViewAccessibleCellInfo *cell_info;
2692 GtkTreeView *tree_view;
2696 parent = atk_object_get_parent (ATK_OBJECT (cell));
2697 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2698 parent = atk_object_get_parent (parent);
2700 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
2704 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
2705 path = cell_info_get_path (cell_info);
2708 gtk_tree_view_set_cursor (tree_view, path, cell_info->cell_col_ref, TRUE);
2709 gtk_tree_path_free (path);
2713 activate_cell (GtkCellAccessible *cell)
2715 GtkTreeViewAccessibleCellInfo *cell_info;
2716 GtkTreeView *tree_view;
2720 parent = atk_object_get_parent (ATK_OBJECT (cell));
2721 if (GTK_IS_CONTAINER_CELL_ACCESSIBLE (parent))
2722 parent = atk_object_get_parent (parent);
2724 cell_info = find_cell_info (GTK_TREE_VIEW_ACCESSIBLE (parent), cell);
2728 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (parent)));
2729 path = cell_info_get_path (cell_info);
2732 gtk_tree_view_row_activated (tree_view, path, cell_info->cell_col_ref);
2733 gtk_tree_path_free (path);
2737 cell_destroyed (gpointer data)
2739 GtkTreeViewAccessibleCellInfo *cell_info = data;
2741 cell_info->cell = NULL;
2743 g_hash_table_remove (cell_info->view->cell_infos, cell_info);
2747 cell_info_get_index (GtkTreeView *tree_view,
2748 GtkTreeViewAccessibleCellInfo *info)
2752 index = _gtk_rbtree_node_get_index (info->tree, info->node) + 1;
2753 index *= get_n_columns (tree_view);
2754 index += get_column_number (tree_view, info->cell_col_ref);
2760 cell_info_new (GtkTreeViewAccessible *accessible,
2761 GtkTreeModel *tree_model,
2764 GtkTreeViewColumn *tv_col,
2765 GtkCellAccessible *cell)
2767 GtkTreeViewAccessibleCellInfo *cell_info;
2769 cell_info = g_new (GtkTreeViewAccessibleCellInfo, 1);
2771 cell_info->tree = tree;
2772 cell_info->node = node;
2773 cell_info->cell_col_ref = tv_col;
2774 cell_info->cell = cell;
2775 cell_info->view = accessible;
2777 g_object_set_qdata_full (G_OBJECT (cell),
2778 gtk_tree_view_accessible_get_data_quark (),
2782 g_hash_table_replace (accessible->cell_infos, cell_info, cell_info);
2785 static GtkCellAccessible *
2786 find_cell (GtkTreeViewAccessible *accessible,
2789 GtkTreeViewAccessibleCellInfo lookup, *cell_info;
2790 GtkTreeView *tree_view;
2792 tree_view = GTK_TREE_VIEW (gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible)));
2794 if (!_gtk_rbtree_find_index (_gtk_tree_view_get_rbtree (tree_view),
2795 index / get_n_columns (tree_view) - 1,
2799 g_assert_not_reached ();
2801 lookup.cell_col_ref = get_visible_column (tree_view, index % get_n_columns (tree_view));
2803 cell_info = g_hash_table_lookup (accessible->cell_infos, &lookup);
2804 if (cell_info == NULL)
2807 return cell_info->cell;
2811 connect_model_signals (GtkTreeView *view,
2812 GtkTreeViewAccessible *accessible)
2816 obj = G_OBJECT (accessible->tree_model);
2817 g_signal_connect_data (obj, "row-changed",
2818 G_CALLBACK (model_row_changed), view, NULL, 0);
2819 g_signal_connect_data (obj, "row-inserted",
2820 G_CALLBACK (model_row_inserted), view, NULL,
2822 g_signal_connect_data (obj, "row-deleted",
2823 G_CALLBACK (model_row_deleted), view, NULL,
2828 disconnect_model_signals (GtkTreeViewAccessible *accessible)
2833 obj = G_OBJECT (accessible->tree_model);
2834 widget = gtk_accessible_get_widget (GTK_ACCESSIBLE (accessible));
2835 g_signal_handlers_disconnect_by_func (obj, model_row_changed, widget);
2836 g_signal_handlers_disconnect_by_func (obj, model_row_inserted, widget);
2837 g_signal_handlers_disconnect_by_func (obj, model_row_deleted, widget);
2840 /* Returns the column number of the specified GtkTreeViewColumn
2841 * The column must be visible.
2844 get_column_number (GtkTreeView *treeview,
2845 GtkTreeViewColumn *column)
2847 GtkTreeViewColumn *cur;
2852 for (i = 0; i < gtk_tree_view_get_n_columns (treeview); i++)
2854 cur = gtk_tree_view_get_column (treeview, i);
2856 if (!gtk_tree_view_column_get_visible (cur))
2865 g_return_val_if_fail (i < gtk_tree_view_get_n_columns (treeview), 0);
2871 get_index (GtkTreeView *tree_view,
2877 gint *indices = NULL;
2881 depth = gtk_tree_path_get_depth (path);
2882 indices = gtk_tree_path_get_indices (path);
2887 GtkTreePath *copy_path;
2888 GtkTreeModel *model;
2890 model = gtk_tree_view_get_model (tree_view);
2891 copy_path = gtk_tree_path_copy (path);
2892 gtk_tree_path_up (copy_path);
2893 count_rows (model, NULL, copy_path, &index, 0, depth);
2894 gtk_tree_path_free (copy_path);
2898 index += indices[depth - 1];
2899 index *= get_n_columns (tree_view);
2900 index += actual_column;
2904 /* The function count_rows counts the number of rows starting at iter
2905 * and ending at end_path. The value of level is the depth of iter and
2906 * the value of depth is the depth of end_path. Rows at depth before
2907 * end_path are counted. This functions counts rows which are not visible
2908 * because an ancestor is collapsed.
2911 count_rows (GtkTreeModel *model,
2913 GtkTreePath *end_path,
2918 GtkTreeIter child_iter;
2924 *count += gtk_tree_model_iter_n_children (model, iter);
2926 if (gtk_tree_model_get_flags (model) & GTK_TREE_MODEL_LIST_ONLY)
2932 if (gtk_tree_model_iter_children (model, &child_iter, iter))
2934 gboolean ret_val = TRUE;
2938 if (level == depth - 1)
2940 GtkTreePath *iter_path;
2941 gboolean finished = FALSE;
2943 iter_path = gtk_tree_model_get_path (model, &child_iter);
2944 if (end_path && gtk_tree_path_compare (iter_path, end_path) >= 0)
2946 gtk_tree_path_free (iter_path);
2950 if (gtk_tree_model_iter_has_child (model, &child_iter))
2951 count_rows (model, &child_iter, end_path, count, level, depth);
2952 ret_val = gtk_tree_model_iter_next (model, &child_iter);
2958 get_rbtree_column_from_index (GtkTreeView *tree_view,
2962 GtkTreeViewColumn **column)
2964 guint n_columns = get_n_columns (tree_view);
2968 /* First row is the column headers */
2975 g_return_val_if_fail (node != NULL, FALSE);
2977 if (!_gtk_rbtree_find_index (_gtk_tree_view_get_rbtree (tree_view),
2986 *column = get_visible_column (tree_view, index % n_columns);
2987 if (*column == NULL)
2994 set_cell_expandable (GtkCellAccessible *cell)
2996 if (_gtk_cell_accessible_add_state (cell, ATK_STATE_EXPANDABLE, FALSE))
2997 _gtk_cell_accessible_add_action (cell,
2998 "expand or contract",
2999 "expands or contracts the row in the tree view containing this cell",
3000 NULL, toggle_cell_expanded);
3003 static GtkTreeViewAccessibleCellInfo *
3004 find_cell_info (GtkTreeViewAccessible *accessible,
3005 GtkCellAccessible *cell)
3009 parent = atk_object_get_parent (ATK_OBJECT (cell));
3010 while (parent != ATK_OBJECT (accessible))
3012 cell = GTK_CELL_ACCESSIBLE (parent);
3013 parent = atk_object_get_parent (ATK_OBJECT (cell));
3016 return g_object_get_qdata (G_OBJECT (cell),
3017 gtk_tree_view_accessible_get_data_quark ());
3021 get_header_from_column (GtkTreeViewColumn *tv_col)
3024 GtkWidget *header_widget;
3029 header_widget = gtk_tree_view_column_get_button (tv_col);
3032 rc = gtk_widget_get_accessible (header_widget);
3040 * _gtk_rbtree_get_ancestor_node:
3041 * @ancestor: the ancestor tree
3042 * @child_tree: the potential child's tree
3043 * @child_node: the potential child's node
3045 * Finds the node that is the ancestor of @child_tree and @child_node
3046 * and belongs to @ancestor. If @ancestor is not an ancestor tree
3047 * of @child_node, %NULL is returned.
3049 * Returns: the ancestor node or %NULL if @ancestor is not an ancestor.
3052 _gtk_rbtree_get_ancestor_node (GtkRBTree *ancestor,
3053 GtkRBTree *child_tree,
3054 GtkRBNode *child_node)
3056 while (child_tree != NULL)
3058 if (child_tree == ancestor)
3061 child_node = child_tree->parent_node;
3062 child_tree = child_tree->parent_tree;
3069 _gtk_tree_view_accessible_remove (GtkTreeView *treeview,
3073 GtkTreeViewAccessibleCellInfo *cell_info;
3074 GHashTableIter iter;
3075 GtkTreeViewAccessible *accessible;
3077 accessible = GTK_TREE_VIEW_ACCESSIBLE (_gtk_widget_peek_accessible (GTK_WIDGET (treeview)));
3078 if (accessible == NULL)
3081 /* if this shows up in profiles, special-case node->children == NULL */
3083 g_hash_table_iter_init (&iter, accessible->cell_infos);
3084 while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&cell_info))
3086 GtkRBNode *child_node = _gtk_rbtree_get_ancestor_node (tree,
3090 if (child_node == NULL)
3093 if (node == NULL || node == child_node)
3094 g_hash_table_iter_remove (&iter);
3098 /* NB: id is not checked, only columns < id are.
3099 * This is important so the function works for notification of removal of a column */
3101 to_visible_column_id (GtkTreeView *treeview,
3109 for (i = 0; i < id; i++)
3111 GtkTreeViewColumn *column = gtk_tree_view_get_column (treeview, i);
3113 if (!gtk_tree_view_column_get_visible (column))
3117 return id - invisible;
3121 _gtk_tree_view_accessible_do_add_column (GtkTreeViewAccessible *accessible,
3122 GtkTreeView *treeview,
3123 GtkTreeViewColumn *column,
3126 guint row, n_rows, n_cols;
3128 /* Generate column-inserted signal */
3129 g_signal_emit_by_name (accessible, "column-inserted", id, 1);
3131 n_rows = get_n_rows (treeview);
3132 n_cols = get_n_columns (treeview);
3134 /* Generate children-changed signals */
3135 for (row = 0; row <= n_rows; row++)
3137 /* Pass NULL as the child object, i.e. 4th argument */
3138 g_signal_emit_by_name (accessible, "children-changed::add",
3139 (row * n_cols) + id, NULL, NULL);
3142 traverse_cells (accessible, NULL, FALSE);
3146 _gtk_tree_view_accessible_add_column (GtkTreeView *treeview,
3147 GtkTreeViewColumn *column,
3152 if (!gtk_tree_view_column_get_visible (column))
3155 obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
3159 _gtk_tree_view_accessible_do_add_column (GTK_TREE_VIEW_ACCESSIBLE (obj),
3162 to_visible_column_id (treeview, id));
3166 _gtk_tree_view_accessible_do_remove_column (GtkTreeViewAccessible *accessible,
3167 GtkTreeView *treeview,
3168 GtkTreeViewColumn *column,
3171 GtkTreeViewAccessibleCellInfo *cell_info;
3172 GHashTableIter iter;
3174 guint row, n_rows, n_cols;
3176 /* Clean column from cache */
3177 g_hash_table_iter_init (&iter, accessible->cell_infos);
3178 while (g_hash_table_iter_next (&iter, NULL, &value))
3181 if (cell_info->cell_col_ref == column)
3182 g_hash_table_iter_remove (&iter);
3185 /* Generate column-deleted signal */
3186 g_signal_emit_by_name (accessible, "column-deleted", id, 1);
3188 n_rows = get_n_rows (treeview);
3189 n_cols = get_n_columns (treeview);
3191 /* Generate children-changed signals */
3192 for (row = 0; row <= n_rows; row++)
3194 /* Pass NULL as the child object, 4th argument */
3195 g_signal_emit_by_name (accessible, "children-changed::remove",
3196 (row * n_cols) + id, NULL, NULL);
3199 traverse_cells (accessible, NULL, FALSE);
3203 _gtk_tree_view_accessible_remove_column (GtkTreeView *treeview,
3204 GtkTreeViewColumn *column,
3209 if (!gtk_tree_view_column_get_visible (column))
3212 obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
3216 _gtk_tree_view_accessible_do_remove_column (GTK_TREE_VIEW_ACCESSIBLE (obj),
3219 to_visible_column_id (treeview, id));
3223 _gtk_tree_view_accessible_reorder_column (GtkTreeView *treeview,
3224 GtkTreeViewColumn *column)
3228 obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
3232 g_signal_emit_by_name (obj, "column-reordered");
3234 traverse_cells (GTK_TREE_VIEW_ACCESSIBLE (obj), NULL, FALSE);
3238 _gtk_tree_view_accessible_toggle_visibility (GtkTreeView *treeview,
3239 GtkTreeViewColumn *column)
3244 obj = _gtk_widget_peek_accessible (GTK_WIDGET (treeview));
3248 id = get_column_number (treeview, column);
3250 if (gtk_tree_view_column_get_visible (column))
3251 _gtk_tree_view_accessible_do_add_column (GTK_TREE_VIEW_ACCESSIBLE (obj),
3256 _gtk_tree_view_accessible_do_remove_column (GTK_TREE_VIEW_ACCESSIBLE (obj),