11 static int cylinder_changed;
12 static GtkComboBox *cylinder_description;
13 static GtkSpinButton *cylinder_size, *cylinder_pressure;
14 static GtkWidget *nitrox_value, *nitrox_button;
16 static void set_cylinder_spinbuttons(int ml, int mbar)
18 double volume, pressure;
21 pressure = mbar / 1000.0;
23 if (output_units.volume == CUFT) {
24 volume /= 28.3168466; /* Liters to cuft */
25 volume *= pressure / 1.01325;
27 if (output_units.pressure == PSI) {
28 pressure *= 14.5037738; /* Bar to PSI */
32 gtk_spin_button_set_value(cylinder_size, volume);
33 gtk_spin_button_set_value(cylinder_pressure, pressure);
36 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
39 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
40 GValue value1 = {0, }, value2 = {0,};
41 cylinder_t *cyl = current_dive->cylinder + 0;
43 /* Did the user set it to some non-standard value? */
44 if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
50 * We get "change" signal callbacks just because we set
51 * the description by hand. Whatever. So ignore them if
54 if (!cylinder_changed && cyl->type.description) {
56 char *desc = gtk_combo_box_get_active_text(combo_box);
58 same = !strcmp(desc, cyl->type.description);
65 gtk_tree_model_get_value(model, &iter, 1, &value1);
66 gtk_tree_model_get_value(model, &iter, 2, &value2);
68 set_cylinder_spinbuttons(g_value_get_int(&value1), g_value_get_int(&value2));
72 * The gtk_tree_model_foreach() interface is bad. It could have
73 * returned whether the callback ever returned true
75 static int found_match = 0;
77 static gboolean match_cylinder(GtkTreeModel *model,
83 const char *desc = data;
86 gtk_tree_model_get_value(model, iter, 0, &value);
87 name = g_value_get_string(&value);
88 if (strcmp(desc, name))
90 gtk_combo_box_set_active_iter(cylinder_description, iter);
95 static void add_cylinder(const char *desc, int ml, int mbar)
100 model = gtk_combo_box_get_model(cylinder_description);
101 gtk_tree_model_foreach(model, match_cylinder, (gpointer)desc);
104 GtkListStore *store = GTK_LIST_STORE(model);
107 gtk_list_store_append(store, &iter);
108 gtk_list_store_set(store, &iter,
113 gtk_combo_box_set_active_iter(cylinder_description, &iter);
117 void show_dive_equipment(struct dive *dive)
119 cylinder_t *cyl = &dive->cylinder[0];
120 const char *desc = cyl->type.description;
127 ml = cyl->type.size.mliter;
128 mbar = cyl->type.workingpressure.mbar;
129 add_cylinder(desc, ml, mbar);
131 set_cylinder_spinbuttons(cyl->type.size.mliter, cyl->type.workingpressure.mbar);
132 o2 = cyl->gasmix.o2.permille / 10.0;
133 gtk_widget_set_sensitive(nitrox_value, !!o2);
134 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(nitrox_button), !!o2);
137 gtk_spin_button_set_value(GTK_SPIN_BUTTON(nitrox_value), o2);
140 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
142 GtkWidget *frame, *hbox, *button;
144 frame = gtk_frame_new(name);
145 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
147 hbox = gtk_hbox_new(FALSE, 3);
148 gtk_container_add(GTK_CONTAINER(frame), hbox);
150 button = gtk_spin_button_new_with_range(min, max, incr);
151 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
153 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
158 static void fill_cylinder_info(cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
162 if (output_units.pressure == PSI)
163 pressure /= 14.5037738;
165 if (pressure && output_units.volume == CUFT) {
166 volume *= 28.3168466; /* CUFT to liter */
167 volume /= pressure / 1.01325;
170 ml = volume * 1000 + 0.5;
171 mbar = pressure * 1000 + 0.5;
175 cyl->type.description = desc;
176 cyl->type.size.mliter = ml;
177 cyl->type.workingpressure.mbar = mbar;
178 cyl->gasmix.o2.permille = o2;
181 * Also, insert it into the model if it doesn't already exist
183 add_cylinder(desc, ml, mbar);
186 static void record_cylinder_changes(struct dive *dive)
189 GtkComboBox *box = cylinder_description;
190 double volume, pressure;
193 desc = gtk_combo_box_get_active_text(box);
194 volume = gtk_spin_button_get_value(cylinder_size);
195 pressure = gtk_spin_button_get_value(cylinder_pressure);
196 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(nitrox_value))*10 + 0.5;
197 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(nitrox_button)))
199 fill_cylinder_info(dive->cylinder+0, desc, volume, pressure, o2);
202 void flush_dive_equipment_changes(struct dive *dive)
204 record_cylinder_changes(dive);
208 * We hardcode the most common standard cylinders,
209 * we should pick up any other names from the dive
212 static struct tank_info {
214 int size; /* cuft if < 1000, otherwise mliter */
215 int psi; /* If zero, size is in mliter */
217 /* Need an empty entry for the no-cylinder case */
220 /* Size-only metric cylinders */
224 /* Most common AL cylinders */
225 { "AL50", 50, 3000 },
226 { "AL63", 63, 3000 },
227 { "AL72", 72, 3000 },
228 { "AL80", 80, 3000 },
229 { "AL100", 100, 3300 },
231 /* Somewhat common LP steel cylinders */
232 { "LP85", 85, 2640 },
233 { "LP95", 95, 2640 },
234 { "LP108", 108, 2640 },
235 { "LP121", 121, 2640 },
237 /* Somewhat common HP steel cylinders */
238 { "HP65", 65, 3442 },
239 { "HP80", 80, 3442 },
240 { "HP100", 100, 3442 },
241 { "HP119", 119, 3442 },
242 { "HP130", 130, 3442 },
244 /* We'll fill in more from the dive log dynamically */
248 static void fill_tank_list(GtkListStore *store)
251 struct tank_info *info = tank_info;
254 int size = info->size;
256 int mbar = 0, ml = size;
258 /* Is it in cuft and psi? */
260 double bar = 0.0689475729 * psi;
261 double airvolume = 28316.8466 * size;
262 double atm = bar / 1.01325;
264 ml = airvolume / atm + 0.5;
265 mbar = bar*1000 + 0.5;
268 gtk_list_store_append(store, &iter);
269 gtk_list_store_set(store, &iter,
278 static void nitrox_cb(GtkToggleButton *button, gpointer data)
280 GtkWidget *nitrox_value = data;
283 state = gtk_toggle_button_get_active(button);
284 gtk_widget_set_sensitive(nitrox_value, state);
287 static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model)
289 GtkWidget *frame, *hbox, *hbox2;
293 hbox = gtk_hbox_new(FALSE, 3);
294 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0);
296 snprintf(buffer, sizeof(buffer), "Cylinder %d", nr+1);
297 frame = gtk_frame_new(buffer);
298 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
300 hbox2 = gtk_hbox_new(FALSE, 3);
301 gtk_container_add(GTK_CONTAINER(frame), hbox2);
303 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
304 gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, TRUE, 0);
306 cylinder_description = GTK_COMBO_BOX(widget);
307 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), NULL);
309 widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
310 cylinder_size = GTK_SPIN_BUTTON(widget);
312 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
313 cylinder_pressure = GTK_SPIN_BUTTON(widget);
315 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
316 nitrox_value = widget;
317 nitrox_button = gtk_check_button_new();
318 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(nitrox_value)),
319 nitrox_button, FALSE, FALSE, 3);
320 g_signal_connect(nitrox_button, "toggled", G_CALLBACK(nitrox_cb), nitrox_value);
322 gtk_spin_button_set_range(GTK_SPIN_BUTTON(nitrox_value), 21.0, 100.0);
325 static GtkListStore *create_tank_size_model(void)
329 model = gtk_list_store_new(3,
330 G_TYPE_STRING, /* Tank name */
331 G_TYPE_INT, /* Tank size in mliter */
332 G_TYPE_INT, /* Tank working pressure in mbar */
335 fill_tank_list(model);
339 GtkWidget *equipment_widget(void)
344 vbox = gtk_vbox_new(FALSE, 3);
346 model = create_tank_size_model();
347 cylinder_widget(vbox, 0, model);