2 /* creates the UI for the equipment page -
3 * controlled through the following interfaces:
5 * void show_dive_equipment(struct dive *dive)
6 * void flush_dive_equipment_changes(struct dive *dive)
9 * GtkWidget *equipment_widget(void)
19 #include "display-gtk.h"
22 struct cylinder_widget {
26 GtkComboBox *description;
27 GtkSpinButton *size, *pressure;
28 GtkWidget *o2, *gasmix_button;
31 static struct cylinder_widget gtk_cylinder[MAX_CYLINDERS];
33 static void set_cylinder_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
35 double volume, pressure;
38 pressure = mbar / 1000.0;
40 if (output_units.volume == CUFT) {
41 volume /= 28.3168466; /* Liters to cuft */
42 volume *= pressure / 1.01325;
44 if (output_units.pressure == PSI) {
45 pressure *= 14.5037738; /* Bar to PSI */
49 gtk_spin_button_set_value(cylinder->size, volume);
50 gtk_spin_button_set_value(cylinder->pressure, pressure);
53 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
56 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
57 GValue value1 = {0, }, value2 = {0,};
58 struct cylinder_widget *cylinder = data;
59 cylinder_t *cyl = current_dive->cylinder + cylinder->index;
61 /* Did the user set it to some non-standard value? */
62 if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
63 cylinder->changed = 1;
68 * We get "change" signal callbacks just because we set
69 * the description by hand. Whatever. So ignore them if
72 if (!cylinder->changed && cyl->type.description) {
74 char *desc = gtk_combo_box_get_active_text(combo_box);
76 same = !strcmp(desc, cyl->type.description);
81 cylinder->changed = 1;
83 gtk_tree_model_get_value(model, &iter, 1, &value1);
84 gtk_tree_model_get_value(model, &iter, 2, &value2);
86 set_cylinder_spinbuttons(cylinder, g_value_get_int(&value1), g_value_get_int(&value2));
90 * The gtk_tree_model_foreach() interface is bad. It could have
91 * returned whether the callback ever returned true
93 static int found_match = 0;
95 static gboolean match_cylinder(GtkTreeModel *model,
101 struct cylinder_widget *cylinder = data;
102 GValue value = {0, };
104 gtk_tree_model_get_value(model, iter, 0, &value);
105 name = g_value_get_string(&value);
106 if (strcmp(cylinder->name, name))
108 gtk_combo_box_set_active_iter(cylinder->description, iter);
113 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
118 model = gtk_combo_box_get_model(cylinder->description);
119 cylinder->name = desc;
120 gtk_tree_model_foreach(model, match_cylinder, cylinder);
123 GtkListStore *store = GTK_LIST_STORE(model);
126 gtk_list_store_append(store, &iter);
127 gtk_list_store_set(store, &iter,
132 gtk_combo_box_set_active_iter(cylinder->description, &iter);
136 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
142 /* Don't show uninitialized cylinder widgets */
143 if (!cylinder->description)
146 desc = cyl->type.description;
149 ml = cyl->type.size.mliter;
150 mbar = cyl->type.workingpressure.mbar;
151 add_cylinder(cylinder, desc, ml, mbar);
153 set_cylinder_spinbuttons(cylinder, cyl->type.size.mliter, cyl->type.workingpressure.mbar);
154 o2 = cyl->gasmix.o2.permille / 10.0;
155 gtk_widget_set_sensitive(cylinder->o2, !!o2);
156 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
159 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
162 void show_dive_equipment(struct dive *dive)
166 for (i = 0; i < MAX_CYLINDERS; i++)
167 show_cylinder(dive->cylinder + i, gtk_cylinder+i);
170 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
172 GtkWidget *frame, *hbox, *button;
174 frame = gtk_frame_new(name);
175 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
177 hbox = gtk_hbox_new(FALSE, 3);
178 gtk_container_add(GTK_CONTAINER(frame), hbox);
180 button = gtk_spin_button_new_with_range(min, max, incr);
181 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
183 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
188 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
192 if (output_units.pressure == PSI)
193 pressure /= 14.5037738;
195 if (pressure && output_units.volume == CUFT) {
196 volume *= 28.3168466; /* CUFT to liter */
197 volume /= pressure / 1.01325;
200 ml = volume * 1000 + 0.5;
201 mbar = pressure * 1000 + 0.5;
205 cyl->type.description = desc;
206 cyl->type.size.mliter = ml;
207 cyl->type.workingpressure.mbar = mbar;
208 cyl->gasmix.o2.permille = o2;
211 * Also, insert it into the model if it doesn't already exist
213 add_cylinder(cylinder, desc, ml, mbar);
216 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
220 double volume, pressure;
223 /* Ignore uninitialized cylinder widgets */
224 box = cylinder->description;
228 desc = gtk_combo_box_get_active_text(box);
229 volume = gtk_spin_button_get_value(cylinder->size);
230 pressure = gtk_spin_button_get_value(cylinder->pressure);
231 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
232 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
234 fill_cylinder_info(cylinder, cyl, desc, volume, pressure, o2);
237 void flush_dive_equipment_changes(struct dive *dive)
239 /* We do nothing: we require the "Ok" button press */
242 static void apply_cb(GtkButton *button, gpointer data)
245 struct dive *dive = current_dive;
250 for (i = 0; i < MAX_CYLINDERS; i++)
251 record_cylinder_changes(dive->cylinder+i, gtk_cylinder+i);
252 flush_divelist(dive);
255 static void cancel_cb(GtkButton *button, gpointer data)
257 struct dive *dive = current_dive;
262 show_dive_equipment(current_dive);
266 * We hardcode the most common standard cylinders,
267 * we should pick up any other names from the dive
270 static struct tank_info {
272 int size; /* cuft if < 1000, otherwise mliter */
273 int psi; /* If zero, size is in mliter */
275 /* Need an empty entry for the no-cylinder case */
278 /* Size-only metric cylinders */
282 /* Most common AL cylinders */
283 { "AL50", 50, 3000 },
284 { "AL63", 63, 3000 },
285 { "AL72", 72, 3000 },
286 { "AL80", 80, 3000 },
287 { "AL100", 100, 3300 },
289 /* Somewhat common LP steel cylinders */
290 { "LP85", 85, 2640 },
291 { "LP95", 95, 2640 },
292 { "LP108", 108, 2640 },
293 { "LP121", 121, 2640 },
295 /* Somewhat common HP steel cylinders */
296 { "HP65", 65, 3442 },
297 { "HP80", 80, 3442 },
298 { "HP100", 100, 3442 },
299 { "HP119", 119, 3442 },
300 { "HP130", 130, 3442 },
302 /* We'll fill in more from the dive log dynamically */
306 static void fill_tank_list(GtkListStore *store)
309 struct tank_info *info = tank_info;
312 int size = info->size;
314 int mbar = 0, ml = size;
316 /* Is it in cuft and psi? */
318 double bar = 0.0689475729 * psi;
319 double airvolume = 28316.8466 * size;
320 double atm = bar / 1.01325;
322 ml = airvolume / atm + 0.5;
323 mbar = bar*1000 + 0.5;
326 gtk_list_store_append(store, &iter);
327 gtk_list_store_set(store, &iter,
336 static void nitrox_cb(GtkToggleButton *button, gpointer data)
338 struct cylinder_widget *cylinder = data;
341 state = gtk_toggle_button_get_active(button);
342 gtk_widget_set_sensitive(cylinder->o2, state);
345 static void cylinder_widget(int nr, GtkListStore *model)
347 struct cylinder_widget *cylinder;
348 GtkWidget *frame, *hbox, *hbox2;
352 cylinder = gtk_cylinder + nr;
353 cylinder->index = nr;
355 hbox = gtk_hbox_new(FALSE, 3);
356 cylinder->hbox = hbox;
358 snprintf(buffer, sizeof(buffer), "Cylinder %d", nr+1);
359 frame = gtk_frame_new(buffer);
360 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
362 hbox2 = gtk_hbox_new(FALSE, 3);
363 gtk_container_add(GTK_CONTAINER(frame), hbox2);
365 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
366 gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, TRUE, 0);
368 cylinder->description = GTK_COMBO_BOX(widget);
369 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
371 widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
372 cylinder->size = GTK_SPIN_BUTTON(widget);
374 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
375 cylinder->pressure = GTK_SPIN_BUTTON(widget);
377 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
378 cylinder->o2 = widget;
379 cylinder->gasmix_button = gtk_check_button_new();
380 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
381 cylinder->gasmix_button, FALSE, FALSE, 3);
382 g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
384 gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
387 static GtkListStore *create_tank_size_model(void)
391 model = gtk_list_store_new(3,
392 G_TYPE_STRING, /* Tank name */
393 G_TYPE_INT, /* Tank size in mliter */
394 G_TYPE_INT, /* Tank working pressure in mbar */
397 fill_tank_list(model);
401 GtkWidget *equipment_widget(void)
404 GtkWidget *vbox, *hbox;
405 GtkWidget *apply, *cancel;
408 vbox = gtk_vbox_new(FALSE, 3);
410 model = create_tank_size_model();
412 /* Create all MAX_CYLINDER gtk widgets */
413 for (i = 0; i < MAX_CYLINDERS; i++)
414 cylinder_widget(i, model);
416 /* But only connect two of them right now to the frame vbox */
417 for (i = 0; i < 2; i++) {
418 struct cylinder_widget *cylinder = gtk_cylinder+i;
419 gtk_box_pack_start(GTK_BOX(vbox), cylinder->hbox, FALSE, TRUE, 0);
422 hbox = gtk_hbox_new(TRUE, 3);
423 gtk_box_pack_end(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
425 apply = gtk_button_new_from_stock(GTK_STOCK_APPLY);
426 cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
427 gtk_box_pack_start(GTK_BOX(hbox), apply, FALSE, FALSE, 0);
428 gtk_box_pack_start(GTK_BOX(hbox), cancel, FALSE, FALSE, 0);
430 g_signal_connect(apply, "clicked", G_CALLBACK(apply_cb), NULL);
431 g_signal_connect(cancel, "clicked", G_CALLBACK(cancel_cb), NULL);