11 struct cylinder_widget {
13 GtkComboBox *description;
14 GtkSpinButton *size, *pressure;
15 GtkWidget *o2, *gasmix_button;
18 static struct cylinder_widget cylinder;
20 static void set_cylinder_spinbuttons(int ml, int mbar)
22 double volume, pressure;
25 pressure = mbar / 1000.0;
27 if (output_units.volume == CUFT) {
28 volume /= 28.3168466; /* Liters to cuft */
29 volume *= pressure / 1.01325;
31 if (output_units.pressure == PSI) {
32 pressure *= 14.5037738; /* Bar to PSI */
36 gtk_spin_button_set_value(cylinder.size, volume);
37 gtk_spin_button_set_value(cylinder.pressure, pressure);
40 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
43 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
44 GValue value1 = {0, }, value2 = {0,};
45 cylinder_t *cyl = current_dive->cylinder + 0;
47 /* Did the user set it to some non-standard value? */
48 if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
54 * We get "change" signal callbacks just because we set
55 * the description by hand. Whatever. So ignore them if
58 if (!cylinder.changed && cyl->type.description) {
60 char *desc = gtk_combo_box_get_active_text(combo_box);
62 same = !strcmp(desc, cyl->type.description);
69 gtk_tree_model_get_value(model, &iter, 1, &value1);
70 gtk_tree_model_get_value(model, &iter, 2, &value2);
72 set_cylinder_spinbuttons(g_value_get_int(&value1), g_value_get_int(&value2));
76 * The gtk_tree_model_foreach() interface is bad. It could have
77 * returned whether the callback ever returned true
79 static int found_match = 0;
81 static gboolean match_cylinder(GtkTreeModel *model,
87 const char *desc = data;
90 gtk_tree_model_get_value(model, iter, 0, &value);
91 name = g_value_get_string(&value);
92 if (strcmp(desc, name))
94 gtk_combo_box_set_active_iter(cylinder.description, iter);
99 static void add_cylinder(const char *desc, int ml, int mbar)
104 model = gtk_combo_box_get_model(cylinder.description);
105 gtk_tree_model_foreach(model, match_cylinder, (gpointer)desc);
108 GtkListStore *store = GTK_LIST_STORE(model);
111 gtk_list_store_append(store, &iter);
112 gtk_list_store_set(store, &iter,
117 gtk_combo_box_set_active_iter(cylinder.description, &iter);
121 void show_dive_equipment(struct dive *dive)
123 cylinder_t *cyl = &dive->cylinder[0];
124 const char *desc = cyl->type.description;
131 ml = cyl->type.size.mliter;
132 mbar = cyl->type.workingpressure.mbar;
133 add_cylinder(desc, ml, mbar);
135 set_cylinder_spinbuttons(cyl->type.size.mliter, cyl->type.workingpressure.mbar);
136 o2 = cyl->gasmix.o2.permille / 10.0;
137 gtk_widget_set_sensitive(cylinder.o2, !!o2);
138 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder.gasmix_button), !!o2);
141 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder.o2), o2);
144 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
146 GtkWidget *frame, *hbox, *button;
148 frame = gtk_frame_new(name);
149 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
151 hbox = gtk_hbox_new(FALSE, 3);
152 gtk_container_add(GTK_CONTAINER(frame), hbox);
154 button = gtk_spin_button_new_with_range(min, max, incr);
155 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
157 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
162 static void fill_cylinder_info(cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
166 if (output_units.pressure == PSI)
167 pressure /= 14.5037738;
169 if (pressure && output_units.volume == CUFT) {
170 volume *= 28.3168466; /* CUFT to liter */
171 volume /= pressure / 1.01325;
174 ml = volume * 1000 + 0.5;
175 mbar = pressure * 1000 + 0.5;
179 cyl->type.description = desc;
180 cyl->type.size.mliter = ml;
181 cyl->type.workingpressure.mbar = mbar;
182 cyl->gasmix.o2.permille = o2;
185 * Also, insert it into the model if it doesn't already exist
187 add_cylinder(desc, ml, mbar);
190 static void record_cylinder_changes(struct dive *dive)
193 GtkComboBox *box = cylinder.description;
194 double volume, pressure;
197 desc = gtk_combo_box_get_active_text(box);
198 volume = gtk_spin_button_get_value(cylinder.size);
199 pressure = gtk_spin_button_get_value(cylinder.pressure);
200 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder.o2))*10 + 0.5;
201 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder.gasmix_button)))
203 fill_cylinder_info(dive->cylinder+0, desc, volume, pressure, o2);
206 void flush_dive_equipment_changes(struct dive *dive)
208 record_cylinder_changes(dive);
212 * We hardcode the most common standard cylinders,
213 * we should pick up any other names from the dive
216 static struct tank_info {
218 int size; /* cuft if < 1000, otherwise mliter */
219 int psi; /* If zero, size is in mliter */
221 /* Need an empty entry for the no-cylinder case */
224 /* Size-only metric cylinders */
228 /* Most common AL cylinders */
229 { "AL50", 50, 3000 },
230 { "AL63", 63, 3000 },
231 { "AL72", 72, 3000 },
232 { "AL80", 80, 3000 },
233 { "AL100", 100, 3300 },
235 /* Somewhat common LP steel cylinders */
236 { "LP85", 85, 2640 },
237 { "LP95", 95, 2640 },
238 { "LP108", 108, 2640 },
239 { "LP121", 121, 2640 },
241 /* Somewhat common HP steel cylinders */
242 { "HP65", 65, 3442 },
243 { "HP80", 80, 3442 },
244 { "HP100", 100, 3442 },
245 { "HP119", 119, 3442 },
246 { "HP130", 130, 3442 },
248 /* We'll fill in more from the dive log dynamically */
252 static void fill_tank_list(GtkListStore *store)
255 struct tank_info *info = tank_info;
258 int size = info->size;
260 int mbar = 0, ml = size;
262 /* Is it in cuft and psi? */
264 double bar = 0.0689475729 * psi;
265 double airvolume = 28316.8466 * size;
266 double atm = bar / 1.01325;
268 ml = airvolume / atm + 0.5;
269 mbar = bar*1000 + 0.5;
272 gtk_list_store_append(store, &iter);
273 gtk_list_store_set(store, &iter,
282 static void nitrox_cb(GtkToggleButton *button, gpointer data)
284 GtkWidget *o2 = data;
287 state = gtk_toggle_button_get_active(button);
288 gtk_widget_set_sensitive(o2, state);
291 static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model)
293 GtkWidget *frame, *hbox, *hbox2;
297 hbox = gtk_hbox_new(FALSE, 3);
298 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0);
300 snprintf(buffer, sizeof(buffer), "Cylinder %d", nr+1);
301 frame = gtk_frame_new(buffer);
302 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
304 hbox2 = gtk_hbox_new(FALSE, 3);
305 gtk_container_add(GTK_CONTAINER(frame), hbox2);
307 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
308 gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, TRUE, 0);
310 cylinder.description = GTK_COMBO_BOX(widget);
311 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), NULL);
313 widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
314 cylinder.size = GTK_SPIN_BUTTON(widget);
316 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
317 cylinder.pressure = GTK_SPIN_BUTTON(widget);
319 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
320 cylinder.o2 = widget;
321 cylinder.gasmix_button = gtk_check_button_new();
322 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder.o2)),
323 cylinder.gasmix_button, FALSE, FALSE, 3);
324 g_signal_connect(cylinder.gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder.o2);
326 gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder.o2), 21.0, 100.0);
329 static GtkListStore *create_tank_size_model(void)
333 model = gtk_list_store_new(3,
334 G_TYPE_STRING, /* Tank name */
335 G_TYPE_INT, /* Tank size in mliter */
336 G_TYPE_INT, /* Tank working pressure in mbar */
339 fill_tank_list(model);
343 GtkWidget *equipment_widget(void)
348 vbox = gtk_vbox_new(FALSE, 3);
350 model = create_tank_size_model();
351 cylinder_widget(vbox, 0, model);