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 <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2012-08-17 14:23:38 -07:00
parent aab94d07cc
commit bc1ff9a121

View file

@ -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);
}
}