11 static int cylinder_changed;
12 static GtkComboBox *cylinder_description;
13 static GtkSpinButton *cylinder_size, *cylinder_pressure, *nitrox_value;
15 static void set_cylinder_spinbuttons(int ml, int mbar)
17 double volume, pressure;
20 pressure = mbar / 1000.0;
22 if (output_units.volume == CUFT) {
23 volume /= 28.3168466; /* Liters to cuft */
24 volume *= pressure / 1.01325;
26 if (output_units.pressure == PSI) {
27 pressure *= 14.5037738; /* Bar to PSI */
31 gtk_spin_button_set_value(cylinder_size, volume);
32 gtk_spin_button_set_value(cylinder_pressure, pressure);
35 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
38 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
39 GValue value1 = {0, }, value2 = {0,};
40 cylinder_t *cyl = current_dive->cylinder + 0;
42 /* Did the user set it to some non-standard value? */
43 if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
49 * We get "change" signal callbacks just because we set
50 * the description by hand. Whatever. So ignore them if
53 if (!cylinder_changed && cyl->type.description) {
55 char *desc = gtk_combo_box_get_active_text(combo_box);
57 same = !strcmp(desc, cyl->type.description);
64 gtk_tree_model_get_value(model, &iter, 1, &value1);
65 gtk_tree_model_get_value(model, &iter, 2, &value2);
67 set_cylinder_spinbuttons(g_value_get_int(&value1), g_value_get_int(&value2));
71 * The gtk_tree_model_foreach() interface is bad. It could have
72 * returned whether the callback ever returned true
74 static int found_match = 0;
76 static gboolean match_cylinder(GtkTreeModel *model,
82 const char *desc = data;
85 gtk_tree_model_get_value(model, iter, 0, &value);
86 name = g_value_get_string(&value);
87 if (strcmp(desc, name))
89 gtk_combo_box_set_active_iter(cylinder_description, iter);
94 static void add_cylinder(const char *desc, int ml, int mbar)
99 model = gtk_combo_box_get_model(cylinder_description);
100 gtk_tree_model_foreach(model, match_cylinder, (gpointer)desc);
103 GtkListStore *store = GTK_LIST_STORE(model);
106 gtk_list_store_append(store, &iter);
107 gtk_list_store_set(store, &iter,
112 gtk_combo_box_set_active_iter(cylinder_description, &iter);
116 void show_dive_equipment(struct dive *dive)
118 cylinder_t *cyl = &dive->cylinder[0];
119 const char *desc = cyl->type.description;
126 ml = cyl->type.size.mliter;
127 mbar = cyl->type.workingpressure.mbar;
128 add_cylinder(desc, ml, mbar);
130 set_cylinder_spinbuttons(cyl->type.size.mliter, cyl->type.workingpressure.mbar);
131 o2 = cyl->gasmix.o2.permille / 10.0;
134 gtk_spin_button_set_value(nitrox_value, o2);
137 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
139 GtkWidget *frame, *button;
141 frame = gtk_frame_new(name);
142 gtk_container_add(GTK_CONTAINER(vbox), frame);
144 button = gtk_spin_button_new_with_range(min, max, incr);
145 gtk_container_add(GTK_CONTAINER(frame), button);
147 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
152 static void fill_cylinder_info(cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
156 if (output_units.pressure == PSI)
157 pressure /= 14.5037738;
159 if (pressure && output_units.volume == CUFT) {
160 volume *= 28.3168466; /* CUFT to liter */
161 volume /= pressure / 1.01325;
164 ml = volume * 1000 + 0.5;
165 mbar = pressure * 1000 + 0.5;
169 cyl->type.description = desc;
170 cyl->type.size.mliter = ml;
171 cyl->type.workingpressure.mbar = mbar;
172 cyl->gasmix.o2.permille = o2;
175 * Also, insert it into the model if it doesn't already exist
177 add_cylinder(desc, ml, mbar);
180 static void record_cylinder_changes(struct dive *dive)
183 GtkComboBox *box = cylinder_description;
184 double volume, pressure;
187 desc = gtk_combo_box_get_active_text(box);
188 volume = gtk_spin_button_get_value(cylinder_size);
189 pressure = gtk_spin_button_get_value(cylinder_pressure);
190 o2 = gtk_spin_button_get_value(nitrox_value)*10 + 0.5;
191 fill_cylinder_info(dive->cylinder+0, desc, volume, pressure, o2);
194 void flush_dive_equipment_changes(struct dive *dive)
196 record_cylinder_changes(dive);
200 * We hardcode the most common standard cylinders,
201 * we should pick up any other names from the dive
204 static struct tank_info {
206 int size; /* cuft if < 1000, otherwise mliter */
207 int psi; /* If zero, size is in mliter */
209 /* Need an empty entry for the no-cylinder case */
212 /* Size-only metric cylinders */
216 /* Most common AL cylinders */
217 { "AL50", 50, 3000 },
218 { "AL63", 63, 3000 },
219 { "AL72", 72, 3000 },
220 { "AL80", 80, 3000 },
221 { "AL100", 100, 3300 },
223 /* Somewhat common LP steel cylinders */
224 { "LP85", 85, 2640 },
225 { "LP95", 95, 2640 },
226 { "LP108", 108, 2640 },
227 { "LP121", 121, 2640 },
229 /* Somewhat common HP steel cylinders */
230 { "HP65", 65, 3442 },
231 { "HP80", 80, 3442 },
232 { "HP100", 100, 3442 },
233 { "HP119", 119, 3442 },
234 { "HP130", 130, 3442 },
236 /* We'll fill in more from the dive log dynamically */
240 static void fill_tank_list(GtkListStore *store)
243 struct tank_info *info = tank_info;
246 int size = info->size;
248 int mbar = 0, ml = size;
250 /* Is it in cuft and psi? */
252 double bar = 0.0689475729 * psi;
253 double airvolume = 28316.8466 * size;
254 double atm = bar / 1.01325;
256 ml = airvolume / atm + 0.5;
257 mbar = bar*1000 + 0.5;
260 gtk_list_store_append(store, &iter);
261 gtk_list_store_set(store, &iter,
270 static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model)
272 GtkWidget *frame, *hbox;
276 snprintf(buffer, sizeof(buffer), "Cylinder %d", nr);
277 frame = gtk_frame_new(buffer);
278 gtk_box_pack_start(GTK_BOX(box), frame, FALSE, FALSE, 0);
280 hbox = gtk_hbox_new(TRUE, 3);
281 gtk_container_add(GTK_CONTAINER(frame), hbox);
283 frame = gtk_frame_new("Description");
284 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, FALSE, 0);
286 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
287 gtk_container_add(GTK_CONTAINER(frame), widget);
289 cylinder_description = GTK_COMBO_BOX(widget);
290 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), NULL);
292 widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
293 cylinder_size = GTK_SPIN_BUTTON(widget);
295 widget = create_spinbutton(hbox, "Working Pressure", 0, 5000, 1);
296 cylinder_pressure = GTK_SPIN_BUTTON(widget);
298 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
299 nitrox_value = GTK_SPIN_BUTTON(widget);
300 gtk_spin_button_set_range(nitrox_value, 21.0, 100.0);
303 static GtkListStore *create_tank_size_model(void)
307 model = gtk_list_store_new(3,
308 G_TYPE_STRING, /* Tank name */
309 G_TYPE_INT, /* Tank size in mliter */
310 G_TYPE_INT, /* Tank working pressure in mbar */
313 fill_tank_list(model);
317 GtkWidget *equipment_widget(void)
322 vbox = gtk_vbox_new(FALSE, 3);
324 model = create_tank_size_model();
325 cylinder_widget(vbox, 0, model);