+static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
+{
+ GtkTreeIter iter;
+ GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
+ int ml, mbar;
+ struct cylinder_widget *cylinder = data;
+ cylinder_t *cyl = current_dive->cylinder + cylinder->index;
+
+ /* Did the user set it to some non-standard value? */
+ if (!get_active_cylinder(combo_box, &iter)) {
+ cylinder->changed = 1;
+ return;
+ }
+
+ /*
+ * We get "change" signal callbacks just because we set
+ * the description by hand. Whatever. So ignore them if
+ * they are no-ops.
+ */
+ if (!cylinder->changed && cyl->type.description) {
+ int same;
+ char *desc = gtk_combo_box_get_active_text(combo_box);
+
+ same = !strcmp(desc, cyl->type.description);
+ g_free(desc);
+ if (same)
+ return;
+ }
+ cylinder->changed = 1;
+
+ gtk_tree_model_get(model, &iter,
+ CYL_SIZE, &ml,
+ CYL_WORKP, &mbar,
+ -1);
+
+ set_cylinder_type_spinbuttons(cylinder, ml, mbar);
+}
+
+static GtkTreeIter *add_cylinder_type(const char *desc, int ml, int mbar, GtkTreeIter *iter)
+{
+ GtkTreeModel *model;
+
+ /* Don't even bother adding stuff without a size */
+ if (!ml)
+ return NULL;
+
+ found_match = NULL;
+ model = GTK_TREE_MODEL(cylinder_model);
+ gtk_tree_model_foreach(model, match_cylinder, (void *)desc);
+
+ if (!found_match) {
+ GtkListStore *store = GTK_LIST_STORE(model);
+
+ gtk_list_store_append(store, iter);
+ gtk_list_store_set(store, iter,
+ 0, desc,
+ 1, ml,
+ 2, mbar,
+ -1);
+ return iter;
+ }
+ return found_match;
+}
+
+/*
+ * When adding a dive, we'll add all the pre-existing cylinder
+ * information from that dive to our cylinder model.
+ */
+void add_cylinder_description(cylinder_type_t *type)
+{
+ GtkTreeIter iter;
+ const char *desc;
+ unsigned int size, workp;
+
+ desc = type->description;
+ if (!desc)
+ return;
+ size = type->size.mliter;
+ workp = type->workingpressure.mbar;
+ add_cylinder_type(desc, size, workp, &iter);
+}
+
+static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
+{
+ GtkTreeIter iter, *match;
+
+ cylinder->name = desc;
+ match = add_cylinder_type(desc, ml, mbar, &iter);
+ if (match)
+ gtk_combo_box_set_active_iter(cylinder->description, match);
+}
+
+static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
+{
+ const char *desc;
+ int ml, mbar;
+ int gasmix;
+ double o2, he;
+
+ /* Don't show uninitialized cylinder widgets */
+ if (!cylinder->description)
+ return;
+
+ desc = cyl->type.description;
+ if (!desc)
+ desc = "";
+ ml = cyl->type.size.mliter;
+ mbar = cyl->type.workingpressure.mbar;
+ add_cylinder(cylinder, desc, ml, mbar);
+
+ set_cylinder_type_spinbuttons(cylinder,
+ cyl->type.size.mliter, cyl->type.workingpressure.mbar);
+ set_cylinder_pressure_spinbuttons(cylinder, cyl);
+
+ gasmix = cyl->gasmix.o2.permille || cyl->gasmix.he.permille;
+ gtk_widget_set_sensitive(cylinder->o2, gasmix);
+ gtk_widget_set_sensitive(cylinder->he, gasmix);
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), gasmix);
+
+ o2 = cyl->gasmix.o2.permille / 10.0;
+ he = cyl->gasmix.he.permille / 10.0;
+ if (!o2)
+ o2 = 21.0;
+ gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
+ gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->he), he);
+}
+
+int cylinder_none(cylinder_t *cyl)
+{
+ return !cyl->type.size.mliter &&
+ !cyl->type.workingpressure.mbar &&
+ !cyl->type.description &&
+ !cyl->gasmix.o2.permille &&
+ !cyl->gasmix.he.permille &&
+ !cyl->sample_start.mbar &&
+ !cyl->sample_end.mbar &&
+ !cyl->start.mbar &&
+ !cyl->end.mbar;
+}
+
+static void set_one_cylinder(int index, cylinder_t *cyl, GtkListStore *model, GtkTreeIter *iter)
+{
+ unsigned int start, end;
+
+ start = cyl->start.mbar ? : cyl->sample_start.mbar;
+ end = cyl->end.mbar ? : cyl->sample_end.mbar;
+ gtk_list_store_set(model, iter,
+ CYL_DESC, cyl->type.description ? : "",
+ CYL_SIZE, cyl->type.size.mliter,
+ CYL_WORKP, cyl->type.workingpressure.mbar,
+ CYL_STARTP, start,
+ CYL_ENDP, end,
+ CYL_O2, cyl->gasmix.o2.permille,
+ CYL_HE, cyl->gasmix.he.permille,
+ -1);
+}
+