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 mark_divelist_changed(TRUE);
253 flush_divelist(dive);
256 static void cancel_cb(GtkButton *button, gpointer data)
258 struct dive *dive = current_dive;
263 show_dive_equipment(current_dive);
267 * We hardcode the most common standard cylinders,
268 * we should pick up any other names from the dive
271 static struct tank_info {
273 int size; /* cuft if < 1000, otherwise mliter */
274 int psi; /* If zero, size is in mliter */
276 /* Need an empty entry for the no-cylinder case */
279 /* Size-only metric cylinders */
283 /* Most common AL cylinders */
284 { "AL50", 50, 3000 },
285 { "AL63", 63, 3000 },
286 { "AL72", 72, 3000 },
287 { "AL80", 80, 3000 },
288 { "AL100", 100, 3300 },
290 /* Somewhat common LP steel cylinders */
291 { "LP85", 85, 2640 },
292 { "LP95", 95, 2640 },
293 { "LP108", 108, 2640 },
294 { "LP121", 121, 2640 },
296 /* Somewhat common HP steel cylinders */
297 { "HP65", 65, 3442 },
298 { "HP80", 80, 3442 },
299 { "HP100", 100, 3442 },
300 { "HP119", 119, 3442 },
301 { "HP130", 130, 3442 },
303 /* We'll fill in more from the dive log dynamically */
307 static void fill_tank_list(GtkListStore *store)
310 struct tank_info *info = tank_info;
313 int size = info->size;
315 int mbar = 0, ml = size;
317 /* Is it in cuft and psi? */
319 double bar = 0.0689475729 * psi;
320 double airvolume = 28316.8466 * size;
321 double atm = bar / 1.01325;
323 ml = airvolume / atm + 0.5;
324 mbar = bar*1000 + 0.5;
327 gtk_list_store_append(store, &iter);
328 gtk_list_store_set(store, &iter,
337 static void nitrox_cb(GtkToggleButton *button, gpointer data)
339 struct cylinder_widget *cylinder = data;
342 state = gtk_toggle_button_get_active(button);
343 gtk_widget_set_sensitive(cylinder->o2, state);
346 static void cylinder_widget(int nr, GtkListStore *model)
348 struct cylinder_widget *cylinder;
349 GtkWidget *frame, *hbox, *hbox2;
353 cylinder = gtk_cylinder + nr;
354 cylinder->index = nr;
356 hbox = gtk_hbox_new(FALSE, 3);
357 cylinder->hbox = hbox;
359 snprintf(buffer, sizeof(buffer), "Cylinder %d", nr+1);
360 frame = gtk_frame_new(buffer);
361 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
363 hbox2 = gtk_hbox_new(FALSE, 3);
364 gtk_container_add(GTK_CONTAINER(frame), hbox2);
366 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
367 gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, TRUE, 0);
369 cylinder->description = GTK_COMBO_BOX(widget);
370 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
372 widget = create_spinbutton(hbox, "Size", 0, 200, 0.1);
373 cylinder->size = GTK_SPIN_BUTTON(widget);
375 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
376 cylinder->pressure = GTK_SPIN_BUTTON(widget);
378 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
379 cylinder->o2 = widget;
380 cylinder->gasmix_button = gtk_check_button_new();
381 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
382 cylinder->gasmix_button, FALSE, FALSE, 3);
383 g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
385 gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
388 static GtkListStore *create_tank_size_model(void)
392 model = gtk_list_store_new(3,
393 G_TYPE_STRING, /* Tank name */
394 G_TYPE_INT, /* Tank size in mliter */
395 G_TYPE_INT, /* Tank working pressure in mbar */
398 fill_tank_list(model);
402 GtkWidget *equipment_widget(void)
405 GtkWidget *vbox, *hbox;
406 GtkWidget *apply, *cancel;
409 vbox = gtk_vbox_new(FALSE, 3);
411 model = create_tank_size_model();
413 /* Create all MAX_CYLINDER gtk widgets */
414 for (i = 0; i < MAX_CYLINDERS; i++)
415 cylinder_widget(i, model);
417 /* But only connect two of them right now to the frame vbox */
418 for (i = 0; i < 2; i++) {
419 struct cylinder_widget *cylinder = gtk_cylinder+i;
420 gtk_box_pack_start(GTK_BOX(vbox), cylinder->hbox, FALSE, TRUE, 0);
423 hbox = gtk_hbox_new(TRUE, 3);
424 gtk_box_pack_end(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
426 apply = gtk_button_new_from_stock(GTK_STOCK_APPLY);
427 cancel = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
428 gtk_box_pack_start(GTK_BOX(hbox), apply, FALSE, FALSE, 0);
429 gtk_box_pack_start(GTK_BOX(hbox), cancel, FALSE, FALSE, 0);
431 g_signal_connect(apply, "clicked", G_CALLBACK(apply_cb), NULL);
432 g_signal_connect(cancel, "clicked", G_CALLBACK(cancel_cb), NULL);