+ amount_selected = 0;
+}
+
+/* when subsurface starts we want to have the last dive selected. So we simply
+ walk to the first leaf (and skip the summary entries - which have negative
+ DIVE_INDEX) */
+static void first_leaf(GtkTreeModel *model, GtkTreeIter *iter, int *diveidx)
+{
+ GtkTreeIter parent;
+ GtkTreePath *tpath;
+
+ while (*diveidx < 0) {
+ memcpy(&parent, iter, sizeof(parent));
+ tpath = gtk_tree_model_get_path(model, &parent);
+ if (!gtk_tree_model_iter_children(model, iter, &parent))
+ /* we should never have a parent without child */
+ return;
+ 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);
+ }
+}
+
+/* if we click on a summary dive, we actually want to select / unselect
+ all the dives "below" it */
+static void select_children(GtkTreeModel *model, GtkTreeSelection * selection,
+ GtkTreeIter *iter, gboolean was_selected)
+{
+ int i, nr_children;
+ gboolean expanded = FALSE;
+ GtkTreeIter parent;
+ GtkTreePath *tpath;
+
+ memcpy(&parent, iter, sizeof(parent));
+
+ tpath = gtk_tree_model_get_path(model, &parent);
+ 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 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);
+ }
+ }
+}
+
+/* make sure that if we expand a summary row that is selected, the children show
+ up as selected, too */
+void row_expanded_cb(GtkTreeView *tree_view, GtkTreeIter *iter, GtkTreePath *path, gpointer data)
+{
+ GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
+
+ if (gtk_tree_selection_path_is_selected(selection, path))
+ select_children(GTK_TREE_MODEL(dive_list.model), selection, iter, FALSE);
+}
+
+/* this is called _before_ the selection is changed, for every single entry;
+ * we simply have it call down the tree to make sure that summary items toggle
+ * their children */
+gboolean modify_selection_cb(GtkTreeSelection *selection, GtkTreeModel *model,
+ GtkTreePath *path, gboolean was_selected, gpointer userdata)
+{
+ GtkTreeIter iter;
+ int dive_idx;
+
+ /* if gtk thinks nothing is selected we should clear out our
+ tracker as well - otherwise hidden selected rows can stay
+ "stuck". The down side is that we now have a different bug:
+ If you select a dive, collapse the dive trip and ctrl-click
+ another dive trip, the initial dive is no longer selected.
+ Just don't do that, ok? */
+ if (gtk_tree_selection_count_selected_rows(selection) == 0)
+ clear_tracker();
+
+ 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 DEBUG_SELECTION_TRACKING
+ printf("modify_selection_cb with idx %d (according to gtk was %sselected)\n",
+ 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);
+ }
+ }
+ /* allow this selection to proceed */
+ return TRUE;
+}
+
+/* this is called when gtk thinks that the selection has changed */
+static void selection_cb(GtkTreeSelection *selection, gpointer userdata)
+{
+ process_selected_dives(selected_dives, selectiontracker, GTK_TREE_MODEL(dive_list.model));
+ repaint_dive();
+}
+
+const char *star_strings[] = {
+ ZERO_STARS,
+ ONE_STARS,
+ TWO_STARS,
+ THREE_STARS,
+ FOUR_STARS,
+ FIVE_STARS
+};
+
+static void star_data_func(GtkTreeViewColumn *col,
+ GtkCellRenderer *renderer,
+ GtkTreeModel *model,
+ GtkTreeIter *iter,
+ gpointer data)
+{
+ int nr_stars, idx;
+ char buffer[40];
+
+ gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_RATING, &nr_stars, -1);
+ if (idx < 0) {
+ *buffer = '\0';
+ } else {
+ if (nr_stars < 0 || nr_stars > 5)
+ nr_stars = 0;
+ snprintf(buffer, sizeof(buffer), "%s", star_strings[nr_stars]);
+ }
+ g_object_set(renderer, "text", buffer, NULL);