+ return success;
+}
+
+static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
+{
+ int *p, index;
+ GtkTreePath *path;
+
+ path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
+ p = gtk_tree_path_get_indices(path);
+ index = p ? *p : 0;
+ gtk_tree_path_free(path);
+ return index;
+}
+
+static void edit_cb(GtkButton *button, gpointer data)
+{
+ int index;
+ GtkTreeIter iter;
+ GtkListStore *model = cylinder_list.model;
+ GtkTreeSelection *selection;
+ cylinder_t cyl;
+
+ selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
+
+ /* Nothing selected? This shouldn't happen, since the button should be inactive */
+ if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
+ return;
+
+ index = get_model_index(model, &iter);
+ if (!edit_cylinder_dialog(index, &cyl))
+ return;
+
+ set_one_cylinder(index, &cyl, model, &iter);
+}
+
+static void add_cb(GtkButton *button, gpointer data)
+{
+ int index = cylinder_list.max_index;
+ GtkTreeIter iter;
+ GtkListStore *model = cylinder_list.model;
+ GtkTreeSelection *selection;
+ cylinder_t cyl;
+
+ if (!edit_cylinder_dialog(index, &cyl))
+ return;
+
+ gtk_list_store_append(model, &iter);
+ set_one_cylinder(index, &cyl, model, &iter);
+
+ selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
+ gtk_tree_selection_select_iter(selection, &iter);
+
+ cylinder_list.max_index++;
+ gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
+}
+
+static void del_cb(GtkButton *button, gpointer data)
+{
+ int index, nr;
+ GtkTreeIter iter;
+ GtkListStore *model = cylinder_list.model;
+ GtkTreeSelection *selection;
+ struct dive *dive;
+ cylinder_t *cyl;
+
+ selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
+
+ /* Nothing selected? This shouldn't happen, since the button should be inactive */
+ if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
+ return;
+
+ index = get_model_index(model, &iter);
+
+ dive = current_dive;
+ if (!dive)
+ return;
+ cyl = dive->cylinder + index;
+ nr = cylinder_list.max_index - index - 1;
+
+ gtk_list_store_remove(model, &iter);
+
+ cylinder_list.max_index--;
+ memmove(cyl, cyl+1, nr*sizeof(*cyl));
+ memset(cyl+nr, 0, sizeof(*cyl));
+
+ mark_divelist_changed(TRUE);
+ flush_divelist(dive);
+
+ gtk_widget_set_sensitive(cylinder_list.edit, 0);
+ gtk_widget_set_sensitive(cylinder_list.del, 0);
+ gtk_widget_set_sensitive(cylinder_list.add, 1);