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 GtkListStore *cylinder_model;
40 GtkWidget *edit, *add, *del;
41 GtkTreeViewColumn *desc, *size, *workp, *startp, *endp, *o2, *he;
44 struct cylinder_widget {
48 GtkComboBox *description;
49 GtkSpinButton *size, *pressure;
50 GtkWidget *o2, *gasmix_button;
53 static struct cylinder_widget gtk_cylinder[MAX_CYLINDERS];
55 static int convert_pressure(int mbar, double *p)
60 pressure = mbar / 1000.0;
62 if (output_units.pressure == PSI) {
63 pressure *= 14.5037738; /* Bar to PSI */
71 static int convert_volume_pressure(int ml, int mbar, double *v, double *p)
74 double volume, pressure;
77 pressure = mbar / 1000.0;
79 if (output_units.volume == CUFT) {
80 volume /= 28.3168466; /* Liters to cuft */
81 volume *= pressure / 1.01325;
83 if (output_units.pressure == PSI) {
84 pressure *= 14.5037738; /* Bar to PSI */
93 static void set_cylinder_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
95 double volume, pressure;
97 convert_volume_pressure(ml, mbar, &volume, &pressure);
98 gtk_spin_button_set_value(cylinder->size, volume);
99 gtk_spin_button_set_value(cylinder->pressure, pressure);
102 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
105 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
106 GValue value1 = {0, }, value2 = {0,};
107 struct cylinder_widget *cylinder = data;
108 cylinder_t *cyl = current_dive->cylinder + cylinder->index;
110 /* Did the user set it to some non-standard value? */
111 if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
112 cylinder->changed = 1;
117 * We get "change" signal callbacks just because we set
118 * the description by hand. Whatever. So ignore them if
121 if (!cylinder->changed && cyl->type.description) {
123 char *desc = gtk_combo_box_get_active_text(combo_box);
125 same = !strcmp(desc, cyl->type.description);
130 cylinder->changed = 1;
132 gtk_tree_model_get_value(model, &iter, 1, &value1);
133 gtk_tree_model_get_value(model, &iter, 2, &value2);
135 set_cylinder_spinbuttons(cylinder, g_value_get_int(&value1), g_value_get_int(&value2));
139 * The gtk_tree_model_foreach() interface is bad. It could have
140 * returned whether the callback ever returned true
142 static int found_match = 0;
144 static gboolean match_cylinder(GtkTreeModel *model,
150 struct cylinder_widget *cylinder = data;
151 GValue value = {0, };
153 gtk_tree_model_get_value(model, iter, 0, &value);
154 name = g_value_get_string(&value);
155 if (strcmp(cylinder->name, name))
157 gtk_combo_box_set_active_iter(cylinder->description, iter);
162 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
167 model = gtk_combo_box_get_model(cylinder->description);
168 cylinder->name = desc;
169 gtk_tree_model_foreach(model, match_cylinder, cylinder);
172 GtkListStore *store = GTK_LIST_STORE(model);
175 gtk_list_store_append(store, &iter);
176 gtk_list_store_set(store, &iter,
181 gtk_combo_box_set_active_iter(cylinder->description, &iter);
185 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
191 /* Don't show uninitialized cylinder widgets */
192 if (!cylinder->description)
195 desc = cyl->type.description;
198 ml = cyl->type.size.mliter;
199 mbar = cyl->type.workingpressure.mbar;
200 add_cylinder(cylinder, desc, ml, mbar);
202 set_cylinder_spinbuttons(cylinder, cyl->type.size.mliter, cyl->type.workingpressure.mbar);
203 o2 = cyl->gasmix.o2.permille / 10.0;
204 gtk_widget_set_sensitive(cylinder->o2, !!o2);
205 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
208 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
211 static int cyl_nothing(cylinder_t *cyl)
213 return !cyl->type.size.mliter &&
214 !cyl->type.workingpressure.mbar &&
215 !cyl->type.description &&
216 !cyl->gasmix.o2.permille &&
217 !cyl->gasmix.he.permille &&
222 void show_dive_equipment(struct dive *dive)
228 model = cylinder_list.model;
229 gtk_list_store_clear(model);
232 cylinder_t *cyl = &dive->cylinder[max-1];
234 if (!cyl_nothing(cyl))
238 cylinder_list.max_index = max;
240 gtk_widget_set_sensitive(cylinder_list.edit, 0);
241 gtk_widget_set_sensitive(cylinder_list.del, 0);
242 gtk_widget_set_sensitive(cylinder_list.add, max < MAX_CYLINDERS);
244 for (i = 0; i < max; i++) {
245 cylinder_t *cyl = dive->cylinder+i;
247 gtk_list_store_append(model, &iter);
248 gtk_list_store_set(model, &iter,
250 CYL_DESC, cyl->type.description ? : "",
251 CYL_SIZE, cyl->type.size.mliter,
252 CYL_WORKP, cyl->type.workingpressure.mbar,
253 CYL_STARTP, cyl->start.mbar,
254 CYL_ENDP, cyl->end.mbar,
255 CYL_O2, cyl->gasmix.o2.permille,
256 CYL_HE, cyl->gasmix.he.permille,
261 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
263 GtkWidget *frame, *hbox, *button;
265 frame = gtk_frame_new(name);
266 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
268 hbox = gtk_hbox_new(FALSE, 3);
269 gtk_container_add(GTK_CONTAINER(frame), hbox);
271 button = gtk_spin_button_new_with_range(min, max, incr);
272 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
274 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
279 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
283 if (output_units.pressure == PSI)
284 pressure /= 14.5037738;
286 if (pressure && output_units.volume == CUFT) {
287 volume *= 28.3168466; /* CUFT to liter */
288 volume /= pressure / 1.01325;
291 ml = volume * 1000 + 0.5;
292 mbar = pressure * 1000 + 0.5;
296 cyl->type.description = desc;
297 cyl->type.size.mliter = ml;
298 cyl->type.workingpressure.mbar = mbar;
299 cyl->gasmix.o2.permille = o2;
302 * Also, insert it into the model if it doesn't already exist
304 add_cylinder(cylinder, desc, ml, mbar);
307 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
311 double volume, pressure;
314 /* Ignore uninitialized cylinder widgets */
315 box = cylinder->description;
319 desc = gtk_combo_box_get_active_text(box);
320 volume = gtk_spin_button_get_value(cylinder->size);
321 pressure = gtk_spin_button_get_value(cylinder->pressure);
322 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
323 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
325 fill_cylinder_info(cylinder, cyl, desc, volume, pressure, o2);
328 void flush_dive_equipment_changes(struct dive *dive)
330 /* We do nothing: we require the "Ok" button press */
333 static void apply_cb(GtkButton *button, gpointer data)
336 struct dive *dive = current_dive;
341 for (i = 0; i < MAX_CYLINDERS; i++)
342 record_cylinder_changes(dive->cylinder+i, gtk_cylinder+i);
343 mark_divelist_changed(TRUE);
344 flush_divelist(dive);
347 static void cancel_cb(GtkButton *button, gpointer data)
349 struct dive *dive = current_dive;
354 show_dive_equipment(current_dive);
358 * We hardcode the most common standard cylinders,
359 * we should pick up any other names from the dive
362 static struct tank_info {
364 int size; /* cuft if < 1000, otherwise mliter */
365 int psi; /* If zero, size is in mliter */
367 /* Need an empty entry for the no-cylinder case */
370 /* Size-only metric cylinders */
374 /* Most common AL cylinders */
375 { "AL50", 50, 3000 },
376 { "AL63", 63, 3000 },
377 { "AL72", 72, 3000 },
378 { "AL80", 80, 3000 },
379 { "AL100", 100, 3300 },
381 /* Somewhat common LP steel cylinders */
382 { "LP85", 85, 2640 },
383 { "LP95", 95, 2640 },
384 { "LP108", 108, 2640 },
385 { "LP121", 121, 2640 },
387 /* Somewhat common HP steel cylinders */
388 { "HP65", 65, 3442 },
389 { "HP80", 80, 3442 },
390 { "HP100", 100, 3442 },
391 { "HP119", 119, 3442 },
392 { "HP130", 130, 3442 },
394 /* We'll fill in more from the dive log dynamically */
398 static void fill_tank_list(GtkListStore *store)
401 struct tank_info *info = tank_info;
404 int size = info->size;
406 int mbar = 0, ml = size;
408 /* Is it in cuft and psi? */
410 double bar = 0.0689475729 * psi;
411 double airvolume = 28316.8466 * size;
412 double atm = bar / 1.01325;
414 ml = airvolume / atm + 0.5;
415 mbar = bar*1000 + 0.5;
418 gtk_list_store_append(store, &iter);
419 gtk_list_store_set(store, &iter,
428 static void nitrox_cb(GtkToggleButton *button, gpointer data)
430 struct cylinder_widget *cylinder = data;
433 state = gtk_toggle_button_get_active(button);
434 gtk_widget_set_sensitive(cylinder->o2, state);
437 static void cylinder_widget(int nr, GtkListStore *model)
439 struct cylinder_widget *cylinder;
440 GtkWidget *frame, *hbox, *hbox2;
444 cylinder = gtk_cylinder + nr;
445 cylinder->index = nr;
447 hbox = gtk_hbox_new(FALSE, 3);
448 cylinder->hbox = hbox;
450 snprintf(buffer, sizeof(buffer), "Cylinder %d", nr+1);
451 frame = gtk_frame_new(buffer);
452 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
454 hbox2 = gtk_hbox_new(FALSE, 3);
455 gtk_container_add(GTK_CONTAINER(frame), hbox2);
457 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
458 gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, TRUE, 0);
460 cylinder->description = GTK_COMBO_BOX(widget);
461 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
463 widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
464 cylinder->size = GTK_SPIN_BUTTON(widget);
466 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
467 cylinder->pressure = GTK_SPIN_BUTTON(widget);
469 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
470 cylinder->o2 = widget;
471 cylinder->gasmix_button = gtk_check_button_new();
472 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
473 cylinder->gasmix_button, FALSE, FALSE, 3);
474 g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
476 gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
479 static void edit_dive_dialog(int index, GtkListStore *model, GtkTreeIter *iter)
483 static void edit_cb(GtkButton *button, gpointer data)
487 GtkListStore *model = cylinder_list.model;
488 GtkTreeSelection *selection;
490 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
492 /* Nothing selected? This shouldn't happen, since the button should be inactive */
493 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
496 gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, CYL_INDEX, &index, -1);
497 edit_dive_dialog(index, model, &iter);
500 static void add_cb(GtkButton *button, gpointer data)
502 int index = cylinder_list.max_index;
504 GtkListStore *model = cylinder_list.model;
506 gtk_list_store_append(model, &iter);
507 edit_dive_dialog(index, model, &iter);
508 cylinder_list.max_index++;
509 gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
512 static void del_cb(GtkButton *button, gpointer data)
516 static GtkListStore *create_tank_size_model(void)
520 model = gtk_list_store_new(3,
521 G_TYPE_STRING, /* Tank name */
522 G_TYPE_INT, /* Tank size in mliter */
523 G_TYPE_INT, /* Tank working pressure in mbar */
526 fill_tank_list(model);
530 static void size_data_func(GtkTreeViewColumn *col,
531 GtkCellRenderer *renderer,
537 double size, pressure;
540 gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
541 convert_volume_pressure(ml, mbar, &size, &pressure);
543 snprintf(buffer, sizeof(buffer), "%.1f", size);
545 strcpy(buffer, "unkn");
546 g_object_set(renderer, "text", buffer, NULL);
549 static void pressure_data_func(GtkTreeViewColumn *col,
550 GtkCellRenderer *renderer,
555 int index = (long)data;
560 gtk_tree_model_get(model, iter, index, &mbar, -1);
561 decimals = convert_pressure(mbar, &pressure);
563 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
566 g_object_set(renderer, "text", buffer, NULL);
569 static void percentage_data_func(GtkTreeViewColumn *col,
570 GtkCellRenderer *renderer,
575 int index = (long)data;
579 gtk_tree_model_get(model, iter, index, &permille, -1);
581 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
584 g_object_set(renderer, "text", buffer, NULL);
587 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
592 selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
593 gtk_widget_set_sensitive(cylinder_list.edit, selected);
594 gtk_widget_set_sensitive(cylinder_list.del, selected);
597 static GtkWidget *cylinder_list_create(void)
599 GtkWidget *tree_view;
600 GtkTreeSelection *selection;
603 model = gtk_list_store_new(CYL_COLUMNS,
604 G_TYPE_INT, /* CYL_INDEX */
605 G_TYPE_STRING, /* CYL_DESC: utf8 */
606 G_TYPE_INT, /* CYL_SIZE: mliter */
607 G_TYPE_INT, /* CYL_WORKP: mbar */
608 G_TYPE_INT, /* CYL_STARTP: mbar */
609 G_TYPE_INT, /* CYL_ENDP: mbar */
610 G_TYPE_INT, /* CYL_O2: permille */
611 G_TYPE_INT /* CYL_HE: permille */
613 cylinder_list.model = model;
614 tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
616 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
617 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
618 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), model);
620 cylinder_list.desc = tree_view_column(tree_view, CYL_DESC, "Type", NULL, PANGO_ALIGN_LEFT, TRUE);
621 cylinder_list.size = tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, PANGO_ALIGN_RIGHT, TRUE);
622 cylinder_list.workp = tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
623 cylinder_list.startp = tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
624 cylinder_list.endp = tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
625 cylinder_list.o2 = tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
626 cylinder_list.he = tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
630 GtkWidget *equipment_widget(void)
632 GtkWidget *vbox, *hbox, *frame, *framebox;
633 GtkWidget *add, *del, *edit;
635 vbox = gtk_vbox_new(FALSE, 3);
638 * We create the cylinder size model at startup, since
639 * we're going to share it across all cylinders and all
640 * dives. So if you add a new cylinder type in one dive,
641 * it will show up when you edit the cylinder types for
644 cylinder_model = create_tank_size_model();
646 cylinder_list.tree_view = cylinder_list_create();
648 hbox = gtk_hbox_new(FALSE, 3);
649 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
651 frame = gtk_frame_new("Cylinders");
652 gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
654 framebox = gtk_vbox_new(FALSE, 3);
655 gtk_container_add(GTK_CONTAINER(frame), framebox);
657 hbox = gtk_hbox_new(FALSE, 3);
658 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
660 gtk_box_pack_start(GTK_BOX(hbox), cylinder_list.tree_view, TRUE, FALSE, 3);
662 hbox = gtk_hbox_new(TRUE, 3);
663 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
665 edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
666 add = gtk_button_new_from_stock(GTK_STOCK_ADD);
667 del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
668 gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
669 gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
670 gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
672 cylinder_list.edit = edit;
673 cylinder_list.add = add;
674 cylinder_list.del = del;
676 g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), NULL);
677 g_signal_connect(add, "clicked", G_CALLBACK(add_cb), NULL);
678 g_signal_connect(del, "clicked", G_CALLBACK(del_cb), NULL);