+static GtkListStore *cylinder_model;
+
+enum {
+ CYL_DESC,
+ CYL_SIZE,
+ CYL_WORKP,
+ CYL_STARTP,
+ CYL_ENDP,
+ CYL_O2,
+ CYL_HE,
+ CYL_COLUMNS
+};
+
+static struct {
+ int max_index;
+ GtkListStore *model;
+ GtkWidget *edit, *add, *del;
+} cylinder_list;
+
+struct cylinder_widget {
+ int index, changed;
+ const char *name;
+ GtkWidget *hbox;
+ GtkComboBox *description;
+ GtkSpinButton *size, *pressure;
+ GtkWidget *start, *end, *pressure_button;
+ GtkWidget *o2, *gasmix_button;
+};
+
+/* we want bar - so let's not use our unit functions */
+static int convert_pressure(int mbar, double *p)
+{
+ int decimals = 1;
+ double pressure;
+
+ if (output_units.pressure == PSI) {
+ pressure = mbar_to_PSI(mbar);
+ decimals = 0;
+ } else {
+ pressure = mbar / 1000.0;
+ }
+
+ *p = pressure;
+ return decimals;
+}
+
+static int convert_volume_pressure(int ml, int mbar, double *v, double *p)
+{
+ int decimals = 1;
+ double volume, pressure;
+
+ volume = ml / 1000.0;
+ if (mbar) {
+ if (output_units.volume == CUFT) {
+ volume = ml_to_cuft(ml);
+ volume *= bar_to_atm(mbar / 1000.0);
+ }
+
+ if (output_units.pressure == PSI) {
+ pressure = mbar_to_PSI(mbar);
+ decimals = 0;
+ } else
+ pressure = mbar / 1000.0;
+ }
+ *v = volume;
+ *p = pressure;
+ return decimals;
+}
+
+static void set_cylinder_type_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
+{
+ double volume, pressure;
+
+ convert_volume_pressure(ml, mbar, &volume, &pressure);
+ gtk_spin_button_set_value(cylinder->size, volume);
+ gtk_spin_button_set_value(cylinder->pressure, pressure);
+}
+
+static void set_cylinder_pressure_spinbuttons(struct cylinder_widget *cylinder, cylinder_t *cyl)
+{
+ int set;
+ unsigned int start, end;
+ double pressure;
+
+ start = cyl->start.mbar;
+ end = cyl->end.mbar;
+ set = start || end;
+ if (!set) {
+ start = cyl->sample_start.mbar;
+ end = cyl->sample_end.mbar;
+ }
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button), set);
+ gtk_widget_set_sensitive(cylinder->start, set);
+ gtk_widget_set_sensitive(cylinder->end, set);
+
+ convert_pressure(start, &pressure);
+ gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->start), pressure);
+ convert_pressure(end, &pressure);
+ gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->end), pressure);
+}
+
+/*
+ * The gtk_tree_model_foreach() interface is bad. It could have
+ * returned whether the callback ever returned true
+ */
+static GtkTreeIter *found_match = NULL;
+static GtkTreeIter match_iter;
+
+static gboolean match_cylinder(GtkTreeModel *model,
+ GtkTreePath *path,
+ GtkTreeIter *iter,
+ gpointer data)
+{
+ int match;
+ gchar *name;
+ const char *desc = data;
+
+ gtk_tree_model_get(model, iter, 0, &name, -1);
+ match = !strcmp(desc, name);
+ g_free(name);
+ if (match) {
+ match_iter = *iter;
+ found_match = &match_iter;
+ }
+ return match;
+}
+
+static int get_active_cylinder(GtkComboBox *combo_box, GtkTreeIter *iter)
+{
+ char *desc;
+
+ if (gtk_combo_box_get_active_iter(combo_box, iter))
+ return TRUE;
+
+ desc = gtk_combo_box_get_active_text(combo_box);
+
+ found_match = NULL;
+ gtk_tree_model_foreach(GTK_TREE_MODEL(cylinder_model), match_cylinder, (void *)desc);
+
+ g_free(desc);
+ if (!found_match)
+ return FALSE;
+
+ *iter = *found_match;
+ gtk_combo_box_set_active_iter(combo_box, iter);
+ return TRUE;
+}
+
+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;
+ double o2;
+
+ /* 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);
+ o2 = cyl->gasmix.o2.permille / 10.0;
+ gtk_widget_set_sensitive(cylinder->o2, !!o2);
+ gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
+ if (!o2)
+ o2 = 21.0;
+ gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
+}
+
+static int cyl_nothing(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);
+}
+