11 struct cylinder_widget {
14 GtkComboBox *description;
15 GtkSpinButton *size, *pressure;
16 GtkWidget *o2, *gasmix_button;
19 static struct cylinder_widget gtk_cylinder[MAX_CYLINDERS];
21 static void set_cylinder_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
23 double volume, pressure;
26 pressure = mbar / 1000.0;
28 if (output_units.volume == CUFT) {
29 volume /= 28.3168466; /* Liters to cuft */
30 volume *= pressure / 1.01325;
32 if (output_units.pressure == PSI) {
33 pressure *= 14.5037738; /* Bar to PSI */
37 gtk_spin_button_set_value(cylinder->size, volume);
38 gtk_spin_button_set_value(cylinder->pressure, pressure);
41 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
44 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
45 GValue value1 = {0, }, value2 = {0,};
46 struct cylinder_widget *cylinder = data;
47 cylinder_t *cyl = current_dive->cylinder + cylinder->index;
49 /* Did the user set it to some non-standard value? */
50 if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
51 cylinder->changed = 1;
56 * We get "change" signal callbacks just because we set
57 * the description by hand. Whatever. So ignore them if
60 if (!cylinder->changed && cyl->type.description) {
62 char *desc = gtk_combo_box_get_active_text(combo_box);
64 same = !strcmp(desc, cyl->type.description);
69 cylinder->changed = 1;
71 gtk_tree_model_get_value(model, &iter, 1, &value1);
72 gtk_tree_model_get_value(model, &iter, 2, &value2);
74 set_cylinder_spinbuttons(cylinder, g_value_get_int(&value1), g_value_get_int(&value2));
78 * The gtk_tree_model_foreach() interface is bad. It could have
79 * returned whether the callback ever returned true
81 static int found_match = 0;
83 static gboolean match_cylinder(GtkTreeModel *model,
89 struct cylinder_widget *cylinder = data;
92 gtk_tree_model_get_value(model, iter, 0, &value);
93 name = g_value_get_string(&value);
94 if (strcmp(cylinder->name, name))
96 gtk_combo_box_set_active_iter(cylinder->description, iter);
101 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
106 model = gtk_combo_box_get_model(cylinder->description);
107 cylinder->name = desc;
108 gtk_tree_model_foreach(model, match_cylinder, cylinder);
111 GtkListStore *store = GTK_LIST_STORE(model);
114 gtk_list_store_append(store, &iter);
115 gtk_list_store_set(store, &iter,
120 gtk_combo_box_set_active_iter(cylinder->description, &iter);
124 void show_dive_equipment(struct dive *dive)
126 cylinder_t *cyl = &dive->cylinder[0];
127 const char *desc = cyl->type.description;
128 struct cylinder_widget *cylinder = >k_cylinder[0];
135 ml = cyl->type.size.mliter;
136 mbar = cyl->type.workingpressure.mbar;
137 add_cylinder(cylinder, desc, ml, mbar);
139 set_cylinder_spinbuttons(cylinder, cyl->type.size.mliter, cyl->type.workingpressure.mbar);
140 o2 = cyl->gasmix.o2.permille / 10.0;
141 gtk_widget_set_sensitive(cylinder->o2, !!o2);
142 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
145 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
148 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
150 GtkWidget *frame, *hbox, *button;
152 frame = gtk_frame_new(name);
153 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
155 hbox = gtk_hbox_new(FALSE, 3);
156 gtk_container_add(GTK_CONTAINER(frame), hbox);
158 button = gtk_spin_button_new_with_range(min, max, incr);
159 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
161 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
166 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
170 if (output_units.pressure == PSI)
171 pressure /= 14.5037738;
173 if (pressure && output_units.volume == CUFT) {
174 volume *= 28.3168466; /* CUFT to liter */
175 volume /= pressure / 1.01325;
178 ml = volume * 1000 + 0.5;
179 mbar = pressure * 1000 + 0.5;
183 cyl->type.description = desc;
184 cyl->type.size.mliter = ml;
185 cyl->type.workingpressure.mbar = mbar;
186 cyl->gasmix.o2.permille = o2;
189 * Also, insert it into the model if it doesn't already exist
191 add_cylinder(cylinder, desc, ml, mbar);
194 static void record_cylinder_changes(struct dive *dive)
197 struct cylinder_widget *cylinder = >k_cylinder[0];
198 GtkComboBox *box = cylinder->description;
199 double volume, pressure;
202 desc = gtk_combo_box_get_active_text(box);
203 volume = gtk_spin_button_get_value(cylinder->size);
204 pressure = gtk_spin_button_get_value(cylinder->pressure);
205 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
206 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
208 fill_cylinder_info(cylinder, dive->cylinder+0, desc, volume, pressure, o2);
211 void flush_dive_equipment_changes(struct dive *dive)
213 record_cylinder_changes(dive);
217 * We hardcode the most common standard cylinders,
218 * we should pick up any other names from the dive
221 static struct tank_info {
223 int size; /* cuft if < 1000, otherwise mliter */
224 int psi; /* If zero, size is in mliter */
226 /* Need an empty entry for the no-cylinder case */
229 /* Size-only metric cylinders */
233 /* Most common AL cylinders */
234 { "AL50", 50, 3000 },
235 { "AL63", 63, 3000 },
236 { "AL72", 72, 3000 },
237 { "AL80", 80, 3000 },
238 { "AL100", 100, 3300 },
240 /* Somewhat common LP steel cylinders */
241 { "LP85", 85, 2640 },
242 { "LP95", 95, 2640 },
243 { "LP108", 108, 2640 },
244 { "LP121", 121, 2640 },
246 /* Somewhat common HP steel cylinders */
247 { "HP65", 65, 3442 },
248 { "HP80", 80, 3442 },
249 { "HP100", 100, 3442 },
250 { "HP119", 119, 3442 },
251 { "HP130", 130, 3442 },
253 /* We'll fill in more from the dive log dynamically */
257 static void fill_tank_list(GtkListStore *store)
260 struct tank_info *info = tank_info;
263 int size = info->size;
265 int mbar = 0, ml = size;
267 /* Is it in cuft and psi? */
269 double bar = 0.0689475729 * psi;
270 double airvolume = 28316.8466 * size;
271 double atm = bar / 1.01325;
273 ml = airvolume / atm + 0.5;
274 mbar = bar*1000 + 0.5;
277 gtk_list_store_append(store, &iter);
278 gtk_list_store_set(store, &iter,
287 static void nitrox_cb(GtkToggleButton *button, gpointer data)
289 struct cylinder_widget *cylinder = data;
292 state = gtk_toggle_button_get_active(button);
293 gtk_widget_set_sensitive(cylinder->o2, state);
296 static void cylinder_widget(GtkWidget *box, int nr, GtkListStore *model)
298 struct cylinder_widget *cylinder;
299 GtkWidget *frame, *hbox, *hbox2;
303 cylinder = gtk_cylinder + nr;
304 cylinder->index = nr;
306 hbox = gtk_hbox_new(FALSE, 3);
307 gtk_box_pack_start(GTK_BOX(box), hbox, FALSE, FALSE, 0);
309 snprintf(buffer, sizeof(buffer), "Cylinder %d", nr+1);
310 frame = gtk_frame_new(buffer);
311 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
313 hbox2 = gtk_hbox_new(FALSE, 3);
314 gtk_container_add(GTK_CONTAINER(frame), hbox2);
316 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
317 gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, TRUE, 0);
319 cylinder->description = GTK_COMBO_BOX(widget);
320 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
322 widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
323 cylinder->size = GTK_SPIN_BUTTON(widget);
325 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
326 cylinder->pressure = GTK_SPIN_BUTTON(widget);
328 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
329 cylinder->o2 = widget;
330 cylinder->gasmix_button = gtk_check_button_new();
331 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
332 cylinder->gasmix_button, FALSE, FALSE, 3);
333 g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
335 gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
338 static GtkListStore *create_tank_size_model(void)
342 model = gtk_list_store_new(3,
343 G_TYPE_STRING, /* Tank name */
344 G_TYPE_INT, /* Tank size in mliter */
345 G_TYPE_INT, /* Tank working pressure in mbar */
348 fill_tank_list(model);
352 GtkWidget *equipment_widget(void)
357 vbox = gtk_vbox_new(FALSE, 3);
359 model = create_tank_size_model();
360 cylinder_widget(vbox, 0, model);