From bc1ff9a1213e4c2a0c135974cbce03bc7614d7b5 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Fri, 17 Aug 2012 14:23:38 -0700 Subject: [PATCH] More fiddling with the selection As expected, this is pretty subtle to get right. But with this change the code becomes simpler and more straight forward, I think. If the dives in a group are collapsed, we don't even try to make gtk keep track of their selection status - we explicitly do so ourselves. This avoids the artificial expand / collapse around our attempt to force gtk to allow us to select children that are hidden. But if a dive is expanded, then we trust gtk to get things right. Signed-off-by: Dirk Hohndel --- divelist.c | 52 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 21 deletions(-) diff --git a/divelist.c b/divelist.c index 0e1b40d..b1b74d4 100644 --- a/divelist.c +++ b/divelist.c @@ -169,7 +169,6 @@ static void first_leaf(GtkTreeModel *model, GtkTreeIter *iter, int *diveidx) if(!gtk_tree_view_row_expanded(GTK_TREE_VIEW(dive_list.tree_view), tpath)) gtk_tree_view_expand_row(GTK_TREE_VIEW(dive_list.tree_view), tpath, FALSE); gtk_tree_model_get(GTK_TREE_MODEL(model), iter, DIVE_INDEX, diveidx, -1); - track_select(*diveidx); } } @@ -179,31 +178,36 @@ static void select_children(GtkTreeModel *model, GtkTreeSelection * selection, GtkTreeIter *iter, gboolean was_selected) { int i, nr_children; - gboolean unexpand = FALSE; + gboolean expanded = FALSE; GtkTreeIter parent; GtkTreePath *tpath; memcpy(&parent, iter, sizeof(parent)); tpath = gtk_tree_model_get_path(model, &parent); - - /* stupid gtk doesn't allow us to select rows that are invisible; so if the - user clicks on a row that isn't expanded, we briefly expand it, select the - children, and then unexpand it again */ - if(!gtk_tree_view_row_expanded(GTK_TREE_VIEW(dive_list.tree_view), tpath)) { - unexpand = TRUE; - gtk_tree_view_expand_row(GTK_TREE_VIEW(dive_list.tree_view), tpath, FALSE); - } + expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(dive_list.tree_view), tpath); nr_children = gtk_tree_model_iter_n_children(model, &parent); for (i = 0; i < nr_children; i++) { gtk_tree_model_iter_nth_child(model, iter, &parent, i); - if (was_selected) - gtk_tree_selection_unselect_iter(selection, iter); - else - gtk_tree_selection_select_iter(selection, iter); + + /* if the parent is expanded, just (un)select the children and we'll + track their selection status in the callback + otherwise just change the selection status directly without + bothering gtk */ + if (expanded) { + if (was_selected) + gtk_tree_selection_unselect_iter(selection, iter); + else + gtk_tree_selection_select_iter(selection, iter); + } else { + int idx; + gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1); + if (was_selected) + track_unselect(idx); + else + track_select(idx); + } } - if (unexpand) - gtk_tree_view_collapse_row(GTK_TREE_VIEW(dive_list.tree_view), tpath); } /* make sure that if we expand a summary row that is selected, the children show @@ -228,11 +232,17 @@ gboolean modify_selection_cb(GtkTreeSelection *selection, GtkTreeModel *model, if (gtk_tree_model_get_iter(model, &iter, path)) { gtk_tree_model_get(model, &iter, DIVE_INDEX, &dive_idx, -1); /* turns out we need to move the selectiontracker here */ - if (was_selected) - track_unselect(dive_idx); - else - track_select(dive_idx); - if (dive_idx < 0) { + +#if DEBUG_SELECTION_TRACKING + printf("modify_selection_cb with idx %d (according to gtk was %sselected) - ", + dive_idx, was_selected ? "" : "un"); +#endif + if (dive_idx >= 0) { + if (was_selected) + track_unselect(dive_idx); + else + track_select(dive_idx); + } else { select_children(model, selection, &iter, was_selected); } } -- 2.43.0