+static int cylinder_changed;
+static GtkComboBox *cylinder_description;
+static GtkSpinButton *cylinder_size, *cylinder_pressure, *nitrox_value;
+
+static void set_cylinder_spinbuttons(int ml, int mbar)
+{
+ double volume, pressure;
+
+ volume = ml / 1000.0;
+ pressure = mbar / 1000.0;
+ if (mbar) {
+ if (output_units.volume == CUFT) {
+ volume /= 28.3168466; /* Liters to cuft */
+ volume *= pressure / 1.01325;
+ }
+ if (output_units.pressure == PSI) {
+ pressure *= 14.5037738; /* Bar to PSI */
+ }
+ }
+
+ gtk_spin_button_set_value(cylinder_size, volume);
+ gtk_spin_button_set_value(cylinder_pressure, pressure);
+}
+
+static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
+{
+ GtkTreeIter iter;
+ GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
+ GValue value1 = {0, }, value2 = {0,};
+ cylinder_t *cyl = current_dive->cylinder + 0;
+
+ /* Did the user set it to some non-standard value? */
+ if (!gtk_combo_box_get_active_iter(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_value(model, &iter, 1, &value1);
+ gtk_tree_model_get_value(model, &iter, 2, &value2);
+
+ set_cylinder_spinbuttons(g_value_get_int(&value1), g_value_get_int(&value2));
+}
+
+/*
+ * The gtk_tree_model_foreach() interface is bad. It could have
+ * returned whether the callback ever returned true
+ */
+static int found_match = 0;
+
+static gboolean match_cylinder(GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gpointer data)
+{
+ const char *name;
+ const char *desc = data;
+ GValue value = {0, };
+
+ gtk_tree_model_get_value(model, iter, 0, &value);
+ name = g_value_get_string(&value);
+ if (strcmp(desc, name))
+ return FALSE;
+ gtk_combo_box_set_active_iter(cylinder_description, iter);
+ found_match = 1;
+ return TRUE;
+}
+
+static void add_cylinder(const char *desc, int ml, int mbar)
+{
+ GtkTreeModel *model;
+
+ found_match = 0;
+ model = gtk_combo_box_get_model(cylinder_description);
+ gtk_tree_model_foreach(model, match_cylinder, (gpointer)desc);
+
+ if (!found_match) {
+ GtkListStore *store = GTK_LIST_STORE(model);
+ GtkTreeIter iter;
+
+ gtk_list_store_append(store, &iter);
+ gtk_list_store_set(store, &iter,
+ 0, desc,
+ 1, ml,
+ 2, mbar,
+ -1);
+ gtk_combo_box_set_active_iter(cylinder_description, &iter);
+ }
+}
+