[PATCH] Fix keyboard navigation of dive list.

Carl Worth cworth at cworth.org
Wed Jan 30 23:05:56 PST 2013


Prefer to use gtk_tree_view_set_cursor instead of
gtk_tree_selection_select_iter since the latter is unreliable (it
moves a highlight but does not move the focus rectangle).
---

It seems like there should be a way to do this without having to fetch
a treepath out, pass it back to GTK+, and then just free it again. So,
this may still not be doing everything the right way, but it at least
works.

(Linus, when I thought that set_cursor wasn't working, I had stupidly
fixed only prev_dive and not next_dive when I tested it.)

-Carl

 divelist.c |   22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/divelist.c b/divelist.c
index 882ba3b..2204e1d 100644
--- a/divelist.c
+++ b/divelist.c
@@ -1338,13 +1338,14 @@ static void fill_dive_list(void)
 
 	update_dive_list_units();
 	if (amount_selected == 0 && gtk_tree_model_get_iter_first(MODEL(dive_list), &iter)) {
-		GtkTreeSelection *selection;
+		GtkTreePath *treepath;
 
 		/* select the last dive (and make sure it's an actual dive that is selected) */
 		gtk_tree_model_get(MODEL(dive_list), &iter, DIVE_INDEX, &selected_dive, -1);
 		first_leaf(MODEL(dive_list), &iter, &selected_dive);
-		selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
-		gtk_tree_selection_select_iter(selection, &iter);
+		treepath = gtk_tree_model_get_path(MODEL(dive_list), &iter);
+		gtk_tree_view_set_cursor(GTK_TREE_VIEW(dive_list.tree_view), treepath, NULL, FALSE);
+		gtk_tree_path_free(treepath);
 	}
 }
 
@@ -2880,7 +2881,8 @@ void select_next_dive(void)
 {
 	GtkTreeIter *nextiter, *parent;
 	GtkTreeIter *iter = get_iter_from_idx(selected_dive);
-	GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
+	GtkTreeView *treeview = GTK_TREE_VIEW(dive_list.tree_view);
+	GtkTreePath *treepath;
 	int idx;
 
 	if (!iter)
@@ -2897,6 +2899,7 @@ void select_next_dive(void)
 		}
 	}
 	gtk_tree_model_get(MODEL(dive_list), nextiter, DIVE_INDEX, &idx, -1);
+	/* Is this a trip, not a dive? */
 	if (idx < 0) {
 		/* need the first child */
 		parent = gtk_tree_iter_copy(nextiter);
@@ -2904,15 +2907,16 @@ void select_next_dive(void)
 			return;
 	}
 	scroll_to_selected(nextiter);
-	gtk_tree_selection_unselect_all(selection);
-	gtk_tree_selection_select_iter(selection, nextiter);
+	treepath = gtk_tree_model_get_path(MODEL(dive_list), nextiter);
+	gtk_tree_view_set_cursor(treeview, treepath, NULL, FALSE);
+	gtk_tree_path_free(treepath);
 }
 
 void select_prev_dive(void)
 {
 	GtkTreeIter previter, *parent;
 	GtkTreeIter *iter = get_iter_from_idx(selected_dive);
-	GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
+	GtkTreeView *treeview = GTK_TREE_VIEW(dive_list.tree_view);
 	GtkTreePath *treepath;
 	int idx;
 
@@ -2934,6 +2938,7 @@ void select_prev_dive(void)
 	if (!gtk_tree_model_get_iter(MODEL(dive_list), &previter, treepath))
 		goto free_path;
 	gtk_tree_model_get(MODEL(dive_list), &previter, DIVE_INDEX, &idx, -1);
+	/* Is this a trip, not a dive? */
 	if (idx < 0) {
 		/* need the last child */
 		parent = gtk_tree_iter_copy(&previter);
@@ -2942,8 +2947,7 @@ void select_prev_dive(void)
 			goto free_path;
 	}
 	scroll_to_selected(&previter);
-	gtk_tree_selection_unselect_all(selection);
-	gtk_tree_selection_select_iter(selection, &previter);
+	gtk_tree_view_set_cursor(treeview, treepath, NULL, FALSE);
 free_path:
 	gtk_tree_path_free(treepath);
 }
-- 
1.7.10.4



More information about the subsurface mailing list