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 static GtkListStore *cylinder_model;
39 GtkWidget *edit, *add, *del;
40 GtkTreeViewColumn *desc, *size, *workp, *startp, *endp, *o2, *he;
43 struct cylinder_widget {
47 GtkComboBox *description;
48 GtkSpinButton *size, *pressure, *start, *end;
49 GtkWidget *o2, *gasmix_button;
52 static int convert_pressure(int mbar, double *p)
57 pressure = mbar / 1000.0;
59 if (output_units.pressure == PSI) {
60 pressure *= 14.5037738; /* Bar to PSI */
68 static int convert_volume_pressure(int ml, int mbar, double *v, double *p)
71 double volume, pressure;
74 pressure = mbar / 1000.0;
76 if (output_units.volume == CUFT) {
77 volume /= 28.3168466; /* Liters to cuft */
78 volume *= pressure / 1.01325;
80 if (output_units.pressure == PSI) {
81 pressure *= 14.5037738; /* Bar to PSI */
90 static void set_cylinder_type_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
92 double volume, pressure;
94 convert_volume_pressure(ml, mbar, &volume, &pressure);
95 gtk_spin_button_set_value(cylinder->size, volume);
96 gtk_spin_button_set_value(cylinder->pressure, pressure);
99 static void set_cylinder_pressure_spinbuttons(struct cylinder_widget *cylinder, int start, int end)
103 convert_pressure(start, &pressure);
104 gtk_spin_button_set_value(cylinder->start, pressure);
105 convert_pressure(end, &pressure);
106 gtk_spin_button_set_value(cylinder->end, pressure);
109 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
112 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
114 struct cylinder_widget *cylinder = data;
115 cylinder_t *cyl = current_dive->cylinder + cylinder->index;
117 /* Did the user set it to some non-standard value? */
118 if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
119 cylinder->changed = 1;
124 * We get "change" signal callbacks just because we set
125 * the description by hand. Whatever. So ignore them if
128 if (!cylinder->changed && cyl->type.description) {
130 char *desc = gtk_combo_box_get_active_text(combo_box);
132 same = !strcmp(desc, cyl->type.description);
137 cylinder->changed = 1;
139 gtk_tree_model_get(model, &iter,
144 set_cylinder_type_spinbuttons(cylinder, ml, mbar);
148 * The gtk_tree_model_foreach() interface is bad. It could have
149 * returned whether the callback ever returned true
151 static GtkTreeIter *found_match = NULL;
152 static GtkTreeIter match_iter;
154 static gboolean match_cylinder(GtkTreeModel *model,
160 const char *desc = data;
161 GValue value = {0, };
163 gtk_tree_model_get_value(model, iter, 0, &value);
164 name = g_value_get_string(&value);
165 if (strcmp(desc, name))
168 found_match = &match_iter;
172 static GtkTreeIter *add_cylinder_type(const char *desc, int ml, int mbar, GtkTreeIter *iter)
176 /* Don't even bother adding stuff without a size */
181 model = GTK_TREE_MODEL(cylinder_model);
182 gtk_tree_model_foreach(model, match_cylinder, (void *)desc);
185 GtkListStore *store = GTK_LIST_STORE(model);
187 gtk_list_store_append(store, iter);
188 gtk_list_store_set(store, iter,
199 * When adding a dive, we'll add all the pre-existing cylinder
200 * information from that dive to our cylinder model.
202 void add_cylinder_description(cylinder_type_t *type)
206 unsigned int size, workp;
208 desc = type->description;
211 size = type->size.mliter;
212 workp = type->workingpressure.mbar;
213 add_cylinder_type(desc, size, workp, &iter);
216 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
218 GtkTreeIter iter, *match;
220 cylinder->name = desc;
221 match = add_cylinder_type(desc, ml, mbar, &iter);
223 gtk_combo_box_set_active_iter(cylinder->description, match);
226 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
232 /* Don't show uninitialized cylinder widgets */
233 if (!cylinder->description)
236 desc = cyl->type.description;
239 ml = cyl->type.size.mliter;
240 mbar = cyl->type.workingpressure.mbar;
241 add_cylinder(cylinder, desc, ml, mbar);
243 set_cylinder_type_spinbuttons(cylinder,
244 cyl->type.size.mliter, cyl->type.workingpressure.mbar);
245 set_cylinder_pressure_spinbuttons(cylinder,
246 cyl->start.mbar, cyl->end.mbar);
247 o2 = cyl->gasmix.o2.permille / 10.0;
248 gtk_widget_set_sensitive(cylinder->o2, !!o2);
249 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
252 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
255 static int cyl_nothing(cylinder_t *cyl)
257 return !cyl->type.size.mliter &&
258 !cyl->type.workingpressure.mbar &&
259 !cyl->type.description &&
260 !cyl->gasmix.o2.permille &&
261 !cyl->gasmix.he.permille &&
266 static void set_one_cylinder(int index, cylinder_t *cyl, GtkListStore *model, GtkTreeIter *iter)
268 gtk_list_store_set(model, iter,
269 CYL_DESC, cyl->type.description ? : "",
270 CYL_SIZE, cyl->type.size.mliter,
271 CYL_WORKP, cyl->type.workingpressure.mbar,
272 CYL_STARTP, cyl->start.mbar,
273 CYL_ENDP, cyl->end.mbar,
274 CYL_O2, cyl->gasmix.o2.permille,
275 CYL_HE, cyl->gasmix.he.permille,
279 void show_dive_equipment(struct dive *dive)
285 model = cylinder_list.model;
286 gtk_list_store_clear(model);
289 cylinder_t *cyl = &dive->cylinder[max-1];
291 if (!cyl_nothing(cyl))
295 cylinder_list.max_index = max;
297 gtk_widget_set_sensitive(cylinder_list.edit, 0);
298 gtk_widget_set_sensitive(cylinder_list.del, 0);
299 gtk_widget_set_sensitive(cylinder_list.add, max < MAX_CYLINDERS);
301 for (i = 0; i < max; i++) {
302 cylinder_t *cyl = dive->cylinder+i;
304 gtk_list_store_append(model, &iter);
305 set_one_cylinder(i, cyl, model, &iter);
309 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
311 GtkWidget *frame, *hbox, *button;
313 frame = gtk_frame_new(name);
314 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, FALSE, 0);
316 hbox = gtk_hbox_new(FALSE, 3);
317 gtk_container_add(GTK_CONTAINER(frame), hbox);
319 button = gtk_spin_button_new_with_range(min, max, incr);
320 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
322 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
327 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc,
328 double volume, double pressure, double start, double end, int o2)
332 if (output_units.pressure == PSI) {
333 pressure /= 14.5037738;
338 if (pressure && output_units.volume == CUFT) {
339 volume *= 28.3168466; /* CUFT to liter */
340 volume /= pressure / 1.01325;
343 ml = volume * 1000 + 0.5;
344 mbar = pressure * 1000 + 0.5;
348 cyl->type.description = desc;
349 cyl->type.size.mliter = ml;
350 cyl->type.workingpressure.mbar = mbar;
351 cyl->start.mbar = start * 1000 + 0.5;
352 cyl->end.mbar = end * 1000 + 0.5;
353 cyl->gasmix.o2.permille = o2;
356 * Also, insert it into the model if it doesn't already exist
358 add_cylinder(cylinder, desc, ml, mbar);
361 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
365 double volume, pressure, start, end;
368 /* Ignore uninitialized cylinder widgets */
369 box = cylinder->description;
373 desc = gtk_combo_box_get_active_text(box);
374 volume = gtk_spin_button_get_value(cylinder->size);
375 pressure = gtk_spin_button_get_value(cylinder->pressure);
376 start = gtk_spin_button_get_value(cylinder->start);
377 end = gtk_spin_button_get_value(cylinder->end);
378 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
379 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
381 fill_cylinder_info(cylinder, cyl, desc, volume, pressure, start, end, o2);
384 void flush_dive_equipment_changes(struct dive *dive)
386 /* We do nothing: we require the "Ok" button press */
390 * We hardcode the most common standard cylinders,
391 * we should pick up any other names from the dive
394 static struct tank_info {
396 int size; /* cuft if < 1000, otherwise mliter */
397 int psi; /* If zero, size is in mliter */
399 /* Need an empty entry for the no-cylinder case */
402 /* Size-only metric cylinders */
406 /* Most common AL cylinders */
407 { "AL50", 50, 3000 },
408 { "AL63", 63, 3000 },
409 { "AL72", 72, 3000 },
410 { "AL80", 80, 3000 },
411 { "AL100", 100, 3300 },
413 /* Somewhat common LP steel cylinders */
414 { "LP85", 85, 2640 },
415 { "LP95", 95, 2640 },
416 { "LP108", 108, 2640 },
417 { "LP121", 121, 2640 },
419 /* Somewhat common HP steel cylinders */
420 { "HP65", 65, 3442 },
421 { "HP80", 80, 3442 },
422 { "HP100", 100, 3442 },
423 { "HP119", 119, 3442 },
424 { "HP130", 130, 3442 },
426 /* We'll fill in more from the dive log dynamically */
430 static void fill_tank_list(GtkListStore *store)
433 struct tank_info *info = tank_info;
436 int size = info->size;
438 int mbar = 0, ml = size;
440 /* Is it in cuft and psi? */
442 double bar = 0.0689475729 * psi;
443 double airvolume = 28316.8466 * size;
444 double atm = bar / 1.01325;
446 ml = airvolume / atm + 0.5;
447 mbar = bar*1000 + 0.5;
450 gtk_list_store_append(store, &iter);
451 gtk_list_store_set(store, &iter,
460 static void nitrox_cb(GtkToggleButton *button, gpointer data)
462 struct cylinder_widget *cylinder = data;
465 state = gtk_toggle_button_get_active(button);
466 gtk_widget_set_sensitive(cylinder->o2, state);
469 static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
471 GtkWidget *frame, *hbox;
475 * Cylinder type: description, size and
478 frame = gtk_frame_new("Cylinder");
480 hbox = gtk_hbox_new(FALSE, 3);
481 gtk_container_add(GTK_CONTAINER(frame), hbox);
483 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
484 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
486 cylinder->description = GTK_COMBO_BOX(widget);
487 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
489 hbox = gtk_hbox_new(FALSE, 3);
490 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
491 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
493 widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
494 cylinder->size = GTK_SPIN_BUTTON(widget);
496 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
497 cylinder->pressure = GTK_SPIN_BUTTON(widget);
500 * Cylinder start/end pressures
502 hbox = gtk_hbox_new(FALSE, 3);
503 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
505 widget = create_spinbutton(hbox, "Start Pressure", 0, 5000, 1);
506 cylinder->start = GTK_SPIN_BUTTON(widget);
508 widget = create_spinbutton(hbox, "End Pressure", 0, 5000, 1);
509 cylinder->end = GTK_SPIN_BUTTON(widget);
512 * Cylinder gas mix: Air, Nitrox or Trimix
514 hbox = gtk_hbox_new(FALSE, 3);
515 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
517 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
518 cylinder->o2 = widget;
519 cylinder->gasmix_button = gtk_check_button_new();
520 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
521 cylinder->gasmix_button, FALSE, FALSE, 3);
522 g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
524 gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
527 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
530 GtkWidget *dialog, *vbox;
531 struct cylinder_widget cylinder;
534 cylinder.index = index;
535 cylinder.changed = 0;
540 *cyl = dive->cylinder[index];
542 dialog = gtk_dialog_new_with_buttons("Cylinder",
543 GTK_WINDOW(main_window),
544 GTK_DIALOG_DESTROY_WITH_PARENT,
545 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
546 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
549 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
550 cylinder_widget(vbox, &cylinder, cylinder_model);
552 show_cylinder(cyl, &cylinder);
554 gtk_widget_show_all(dialog);
555 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
557 record_cylinder_changes(cyl, &cylinder);
558 dive->cylinder[index] = *cyl;
559 mark_divelist_changed(TRUE);
560 flush_divelist(dive);
563 gtk_widget_destroy(dialog);
568 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
573 path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
574 p = gtk_tree_path_get_indices(path);
576 gtk_tree_path_free(path);
580 static void edit_cb(GtkButton *button, gpointer data)
584 GtkListStore *model = cylinder_list.model;
585 GtkTreeSelection *selection;
588 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
590 /* Nothing selected? This shouldn't happen, since the button should be inactive */
591 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
594 index = get_model_index(model, &iter);
595 if (!edit_cylinder_dialog(index, &cyl))
598 set_one_cylinder(index, &cyl, model, &iter);
602 static void add_cb(GtkButton *button, gpointer data)
604 int index = cylinder_list.max_index;
606 GtkListStore *model = cylinder_list.model;
607 GtkTreeSelection *selection;
610 if (!edit_cylinder_dialog(index, &cyl))
613 gtk_list_store_append(model, &iter);
614 set_one_cylinder(index, &cyl, model, &iter);
616 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
617 gtk_tree_selection_select_iter(selection, &iter);
619 cylinder_list.max_index++;
620 gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
623 static void del_cb(GtkButton *button, gpointer data)
627 GtkListStore *model = cylinder_list.model;
628 GtkTreeSelection *selection;
632 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
634 /* Nothing selected? This shouldn't happen, since the button should be inactive */
635 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
638 index = get_model_index(model, &iter);
643 cyl = dive->cylinder + index;
644 nr = cylinder_list.max_index - index - 1;
646 gtk_list_store_remove(model, &iter);
648 cylinder_list.max_index--;
649 memmove(cyl, cyl+1, nr*sizeof(*cyl));
650 memset(cyl+nr, 0, sizeof(*cyl));
652 mark_divelist_changed(TRUE);
653 flush_divelist(dive);
655 gtk_widget_set_sensitive(cylinder_list.edit, 0);
656 gtk_widget_set_sensitive(cylinder_list.del, 0);
657 gtk_widget_set_sensitive(cylinder_list.add, 1);
660 static GtkListStore *create_tank_size_model(void)
664 model = gtk_list_store_new(3,
665 G_TYPE_STRING, /* Tank name */
666 G_TYPE_INT, /* Tank size in mliter */
667 G_TYPE_INT, /* Tank working pressure in mbar */
670 fill_tank_list(model);
674 static void size_data_func(GtkTreeViewColumn *col,
675 GtkCellRenderer *renderer,
681 double size, pressure;
684 gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
685 convert_volume_pressure(ml, mbar, &size, &pressure);
687 snprintf(buffer, sizeof(buffer), "%.1f", size);
689 strcpy(buffer, "unkn");
690 g_object_set(renderer, "text", buffer, NULL);
693 static void pressure_data_func(GtkTreeViewColumn *col,
694 GtkCellRenderer *renderer,
699 int index = (long)data;
704 gtk_tree_model_get(model, iter, index, &mbar, -1);
705 decimals = convert_pressure(mbar, &pressure);
707 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
710 g_object_set(renderer, "text", buffer, NULL);
713 static void percentage_data_func(GtkTreeViewColumn *col,
714 GtkCellRenderer *renderer,
719 int index = (long)data;
723 gtk_tree_model_get(model, iter, index, &permille, -1);
725 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
728 g_object_set(renderer, "text", buffer, NULL);
731 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
736 selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
737 gtk_widget_set_sensitive(cylinder_list.edit, selected);
738 gtk_widget_set_sensitive(cylinder_list.del, selected);
741 static void row_activated_cb(GtkTreeView *tree_view,
743 GtkTreeViewColumn *column,
749 static GtkWidget *cylinder_list_create(void)
751 GtkWidget *tree_view;
752 GtkTreeSelection *selection;
755 model = gtk_list_store_new(CYL_COLUMNS,
756 G_TYPE_STRING, /* CYL_DESC: utf8 */
757 G_TYPE_INT, /* CYL_SIZE: mliter */
758 G_TYPE_INT, /* CYL_WORKP: mbar */
759 G_TYPE_INT, /* CYL_STARTP: mbar */
760 G_TYPE_INT, /* CYL_ENDP: mbar */
761 G_TYPE_INT, /* CYL_O2: permille */
762 G_TYPE_INT /* CYL_HE: permille */
764 cylinder_list.model = model;
765 tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
766 g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), model);
768 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
769 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
770 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), model);
772 g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
773 "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
776 cylinder_list.desc = tree_view_column(tree_view, CYL_DESC, "Type", NULL, PANGO_ALIGN_LEFT, TRUE);
777 cylinder_list.size = tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, PANGO_ALIGN_RIGHT, TRUE);
778 cylinder_list.workp = tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
779 cylinder_list.startp = tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
780 cylinder_list.endp = tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
781 cylinder_list.o2 = tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
782 cylinder_list.he = tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
786 GtkWidget *equipment_widget(void)
788 GtkWidget *vbox, *hbox, *frame, *framebox;
789 GtkWidget *add, *del, *edit;
791 vbox = gtk_vbox_new(FALSE, 3);
794 * We create the cylinder size model at startup, since
795 * we're going to share it across all cylinders and all
796 * dives. So if you add a new cylinder type in one dive,
797 * it will show up when you edit the cylinder types for
800 cylinder_model = create_tank_size_model();
802 cylinder_list.tree_view = cylinder_list_create();
804 hbox = gtk_hbox_new(FALSE, 3);
805 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
807 frame = gtk_frame_new("Cylinders");
808 gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
810 framebox = gtk_vbox_new(FALSE, 3);
811 gtk_container_add(GTK_CONTAINER(frame), framebox);
813 hbox = gtk_hbox_new(FALSE, 3);
814 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
816 gtk_box_pack_start(GTK_BOX(hbox), cylinder_list.tree_view, TRUE, FALSE, 3);
818 hbox = gtk_hbox_new(TRUE, 3);
819 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
821 edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
822 add = gtk_button_new_from_stock(GTK_STOCK_ADD);
823 del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
824 gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
825 gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
826 gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
828 cylinder_list.edit = edit;
829 cylinder_list.add = add;
830 cylinder_list.del = del;
832 g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), NULL);
833 g_signal_connect(add, "clicked", G_CALLBACK(add_cb), NULL);
834 g_signal_connect(del, "clicked", G_CALLBACK(del_cb), NULL);