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;
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_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar, int start, int end)
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);
98 convert_pressure(start, &pressure);
99 gtk_spin_button_set_value(cylinder->start, pressure);
100 convert_pressure(end, &pressure);
101 gtk_spin_button_set_value(cylinder->end, pressure);
104 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
107 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
108 int ml, mbar, start, end;
109 struct cylinder_widget *cylinder = data;
110 cylinder_t *cyl = current_dive->cylinder + cylinder->index;
112 /* Did the user set it to some non-standard value? */
113 if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
114 cylinder->changed = 1;
119 * We get "change" signal callbacks just because we set
120 * the description by hand. Whatever. So ignore them if
123 if (!cylinder->changed && cyl->type.description) {
125 char *desc = gtk_combo_box_get_active_text(combo_box);
127 same = !strcmp(desc, cyl->type.description);
132 cylinder->changed = 1;
134 gtk_tree_model_get(model, &iter,
141 set_cylinder_spinbuttons(cylinder, ml, mbar, start, end);
145 * The gtk_tree_model_foreach() interface is bad. It could have
146 * returned whether the callback ever returned true
148 static int found_match = 0;
150 static gboolean match_cylinder(GtkTreeModel *model,
156 struct cylinder_widget *cylinder = data;
157 GValue value = {0, };
159 gtk_tree_model_get_value(model, iter, 0, &value);
160 name = g_value_get_string(&value);
161 if (strcmp(cylinder->name, name))
163 gtk_combo_box_set_active_iter(cylinder->description, iter);
168 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
173 model = gtk_combo_box_get_model(cylinder->description);
174 cylinder->name = desc;
175 gtk_tree_model_foreach(model, match_cylinder, cylinder);
178 GtkListStore *store = GTK_LIST_STORE(model);
181 gtk_list_store_append(store, &iter);
182 gtk_list_store_set(store, &iter,
187 gtk_combo_box_set_active_iter(cylinder->description, &iter);
191 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
197 /* Don't show uninitialized cylinder widgets */
198 if (!cylinder->description)
201 desc = cyl->type.description;
204 ml = cyl->type.size.mliter;
205 mbar = cyl->type.workingpressure.mbar;
206 add_cylinder(cylinder, desc, ml, mbar);
208 set_cylinder_spinbuttons(cylinder,
209 cyl->type.size.mliter, cyl->type.workingpressure.mbar,
210 cyl->start.mbar, cyl->end.mbar);
211 o2 = cyl->gasmix.o2.permille / 10.0;
212 gtk_widget_set_sensitive(cylinder->o2, !!o2);
213 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
216 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
219 static int cyl_nothing(cylinder_t *cyl)
221 return !cyl->type.size.mliter &&
222 !cyl->type.workingpressure.mbar &&
223 !cyl->type.description &&
224 !cyl->gasmix.o2.permille &&
225 !cyl->gasmix.he.permille &&
230 static void set_one_cylinder(int index, cylinder_t *cyl, GtkListStore *model, GtkTreeIter *iter)
232 gtk_list_store_set(model, iter,
233 CYL_DESC, cyl->type.description ? : "",
234 CYL_SIZE, cyl->type.size.mliter,
235 CYL_WORKP, cyl->type.workingpressure.mbar,
236 CYL_STARTP, cyl->start.mbar,
237 CYL_ENDP, cyl->end.mbar,
238 CYL_O2, cyl->gasmix.o2.permille,
239 CYL_HE, cyl->gasmix.he.permille,
243 void show_dive_equipment(struct dive *dive)
249 model = cylinder_list.model;
250 gtk_list_store_clear(model);
253 cylinder_t *cyl = &dive->cylinder[max-1];
255 if (!cyl_nothing(cyl))
259 cylinder_list.max_index = max;
261 gtk_widget_set_sensitive(cylinder_list.edit, 0);
262 gtk_widget_set_sensitive(cylinder_list.del, 0);
263 gtk_widget_set_sensitive(cylinder_list.add, max < MAX_CYLINDERS);
265 for (i = 0; i < max; i++) {
266 cylinder_t *cyl = dive->cylinder+i;
268 gtk_list_store_append(model, &iter);
269 set_one_cylinder(i, cyl, model, &iter);
273 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
275 GtkWidget *frame, *hbox, *button;
277 frame = gtk_frame_new(name);
278 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, FALSE, 0);
280 hbox = gtk_hbox_new(FALSE, 3);
281 gtk_container_add(GTK_CONTAINER(frame), hbox);
283 button = gtk_spin_button_new_with_range(min, max, incr);
284 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
286 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
291 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc,
292 double volume, double pressure, double start, double end, int o2)
296 if (output_units.pressure == PSI) {
297 pressure /= 14.5037738;
302 if (pressure && output_units.volume == CUFT) {
303 volume *= 28.3168466; /* CUFT to liter */
304 volume /= pressure / 1.01325;
307 ml = volume * 1000 + 0.5;
308 mbar = pressure * 1000 + 0.5;
312 cyl->type.description = desc;
313 cyl->type.size.mliter = ml;
314 cyl->type.workingpressure.mbar = mbar;
315 cyl->start.mbar = start * 1000 + 0.5;
316 cyl->end.mbar = end * 1000 + 0.5;
317 cyl->gasmix.o2.permille = o2;
320 * Also, insert it into the model if it doesn't already exist
322 add_cylinder(cylinder, desc, ml, mbar);
325 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
329 double volume, pressure, start, end;
332 /* Ignore uninitialized cylinder widgets */
333 box = cylinder->description;
337 desc = gtk_combo_box_get_active_text(box);
338 volume = gtk_spin_button_get_value(cylinder->size);
339 pressure = gtk_spin_button_get_value(cylinder->pressure);
340 start = gtk_spin_button_get_value(cylinder->start);
341 end = gtk_spin_button_get_value(cylinder->end);
342 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
343 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
345 fill_cylinder_info(cylinder, cyl, desc, volume, pressure, start, end, o2);
348 void flush_dive_equipment_changes(struct dive *dive)
350 /* We do nothing: we require the "Ok" button press */
354 * We hardcode the most common standard cylinders,
355 * we should pick up any other names from the dive
358 static struct tank_info {
360 int size; /* cuft if < 1000, otherwise mliter */
361 int psi; /* If zero, size is in mliter */
363 /* Need an empty entry for the no-cylinder case */
366 /* Size-only metric cylinders */
370 /* Most common AL cylinders */
371 { "AL50", 50, 3000 },
372 { "AL63", 63, 3000 },
373 { "AL72", 72, 3000 },
374 { "AL80", 80, 3000 },
375 { "AL100", 100, 3300 },
377 /* Somewhat common LP steel cylinders */
378 { "LP85", 85, 2640 },
379 { "LP95", 95, 2640 },
380 { "LP108", 108, 2640 },
381 { "LP121", 121, 2640 },
383 /* Somewhat common HP steel cylinders */
384 { "HP65", 65, 3442 },
385 { "HP80", 80, 3442 },
386 { "HP100", 100, 3442 },
387 { "HP119", 119, 3442 },
388 { "HP130", 130, 3442 },
390 /* We'll fill in more from the dive log dynamically */
394 static void fill_tank_list(GtkListStore *store)
397 struct tank_info *info = tank_info;
400 int size = info->size;
402 int mbar = 0, ml = size;
404 /* Is it in cuft and psi? */
406 double bar = 0.0689475729 * psi;
407 double airvolume = 28316.8466 * size;
408 double atm = bar / 1.01325;
410 ml = airvolume / atm + 0.5;
411 mbar = bar*1000 + 0.5;
414 gtk_list_store_append(store, &iter);
415 gtk_list_store_set(store, &iter,
424 static void nitrox_cb(GtkToggleButton *button, gpointer data)
426 struct cylinder_widget *cylinder = data;
429 state = gtk_toggle_button_get_active(button);
430 gtk_widget_set_sensitive(cylinder->o2, state);
433 static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
435 GtkWidget *frame, *hbox;
439 * Cylinder type: description, size and
442 frame = gtk_frame_new("Cylinder");
444 hbox = gtk_hbox_new(FALSE, 3);
445 gtk_container_add(GTK_CONTAINER(frame), hbox);
447 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
448 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
450 cylinder->description = GTK_COMBO_BOX(widget);
451 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
453 hbox = gtk_hbox_new(FALSE, 3);
454 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
455 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
457 widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
458 cylinder->size = GTK_SPIN_BUTTON(widget);
460 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
461 cylinder->pressure = GTK_SPIN_BUTTON(widget);
464 * Cylinder start/end pressures
466 hbox = gtk_hbox_new(FALSE, 3);
467 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
469 widget = create_spinbutton(hbox, "Start Pressure", 0, 5000, 1);
470 cylinder->start = GTK_SPIN_BUTTON(widget);
472 widget = create_spinbutton(hbox, "End Pressure", 0, 5000, 1);
473 cylinder->end = GTK_SPIN_BUTTON(widget);
476 * Cylinder gas mix: Air, Nitrox or Trimix
478 hbox = gtk_hbox_new(FALSE, 3);
479 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
481 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
482 cylinder->o2 = widget;
483 cylinder->gasmix_button = gtk_check_button_new();
484 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
485 cylinder->gasmix_button, FALSE, FALSE, 3);
486 g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
488 gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
491 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
494 GtkWidget *dialog, *vbox;
495 struct cylinder_widget cylinder;
498 cylinder.index = index;
499 cylinder.changed = 0;
504 *cyl = dive->cylinder[index];
506 dialog = gtk_dialog_new_with_buttons("Cylinder",
507 GTK_WINDOW(main_window),
508 GTK_DIALOG_DESTROY_WITH_PARENT,
509 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
510 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
513 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
514 cylinder_widget(vbox, &cylinder, cylinder_model);
516 show_cylinder(cyl, &cylinder);
518 gtk_widget_show_all(dialog);
519 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
521 record_cylinder_changes(cyl, &cylinder);
522 dive->cylinder[index] = *cyl;
523 mark_divelist_changed(TRUE);
524 flush_divelist(dive);
527 gtk_widget_destroy(dialog);
532 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
537 path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
538 p = gtk_tree_path_get_indices(path);
540 gtk_tree_path_free(path);
544 static void edit_cb(GtkButton *button, gpointer data)
548 GtkListStore *model = cylinder_list.model;
549 GtkTreeSelection *selection;
552 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
554 /* Nothing selected? This shouldn't happen, since the button should be inactive */
555 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
558 index = get_model_index(model, &iter);
559 if (!edit_cylinder_dialog(index, &cyl))
562 set_one_cylinder(index, &cyl, model, &iter);
565 static void add_cb(GtkButton *button, gpointer data)
567 int index = cylinder_list.max_index;
569 GtkListStore *model = cylinder_list.model;
570 GtkTreeSelection *selection;
573 if (!edit_cylinder_dialog(index, &cyl))
576 gtk_list_store_append(model, &iter);
577 set_one_cylinder(index, &cyl, model, &iter);
579 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
580 gtk_tree_selection_select_iter(selection, &iter);
582 cylinder_list.max_index++;
583 gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
586 static void del_cb(GtkButton *button, gpointer data)
590 GtkListStore *model = cylinder_list.model;
591 GtkTreeSelection *selection;
595 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
597 /* Nothing selected? This shouldn't happen, since the button should be inactive */
598 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
601 index = get_model_index(model, &iter);
606 cyl = dive->cylinder + index;
607 nr = cylinder_list.max_index - index - 1;
609 gtk_list_store_remove(model, &iter);
611 cylinder_list.max_index--;
612 memmove(cyl, cyl+1, nr*sizeof(*cyl));
613 memset(cyl+nr, 0, sizeof(*cyl));
615 mark_divelist_changed(TRUE);
616 flush_divelist(dive);
618 gtk_widget_set_sensitive(cylinder_list.edit, 0);
619 gtk_widget_set_sensitive(cylinder_list.del, 0);
620 gtk_widget_set_sensitive(cylinder_list.add, 1);
623 static GtkListStore *create_tank_size_model(void)
627 model = gtk_list_store_new(3,
628 G_TYPE_STRING, /* Tank name */
629 G_TYPE_INT, /* Tank size in mliter */
630 G_TYPE_INT, /* Tank working pressure in mbar */
633 fill_tank_list(model);
637 static void size_data_func(GtkTreeViewColumn *col,
638 GtkCellRenderer *renderer,
644 double size, pressure;
647 gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
648 convert_volume_pressure(ml, mbar, &size, &pressure);
650 snprintf(buffer, sizeof(buffer), "%.1f", size);
652 strcpy(buffer, "unkn");
653 g_object_set(renderer, "text", buffer, NULL);
656 static void pressure_data_func(GtkTreeViewColumn *col,
657 GtkCellRenderer *renderer,
662 int index = (long)data;
667 gtk_tree_model_get(model, iter, index, &mbar, -1);
668 decimals = convert_pressure(mbar, &pressure);
670 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
673 g_object_set(renderer, "text", buffer, NULL);
676 static void percentage_data_func(GtkTreeViewColumn *col,
677 GtkCellRenderer *renderer,
682 int index = (long)data;
686 gtk_tree_model_get(model, iter, index, &permille, -1);
688 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
691 g_object_set(renderer, "text", buffer, NULL);
694 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
699 selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
700 gtk_widget_set_sensitive(cylinder_list.edit, selected);
701 gtk_widget_set_sensitive(cylinder_list.del, selected);
704 static void row_activated_cb(GtkTreeView *tree_view,
706 GtkTreeViewColumn *column,
712 static GtkWidget *cylinder_list_create(void)
714 GtkWidget *tree_view;
715 GtkTreeSelection *selection;
718 model = gtk_list_store_new(CYL_COLUMNS,
719 G_TYPE_STRING, /* CYL_DESC: utf8 */
720 G_TYPE_INT, /* CYL_SIZE: mliter */
721 G_TYPE_INT, /* CYL_WORKP: mbar */
722 G_TYPE_INT, /* CYL_STARTP: mbar */
723 G_TYPE_INT, /* CYL_ENDP: mbar */
724 G_TYPE_INT, /* CYL_O2: permille */
725 G_TYPE_INT /* CYL_HE: permille */
727 cylinder_list.model = model;
728 tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
729 g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), model);
731 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
732 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
733 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), model);
735 g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
736 "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
739 cylinder_list.desc = tree_view_column(tree_view, CYL_DESC, "Type", NULL, PANGO_ALIGN_LEFT, TRUE);
740 cylinder_list.size = tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, PANGO_ALIGN_RIGHT, TRUE);
741 cylinder_list.workp = tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
742 cylinder_list.startp = tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
743 cylinder_list.endp = tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
744 cylinder_list.o2 = tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
745 cylinder_list.he = tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
749 GtkWidget *equipment_widget(void)
751 GtkWidget *vbox, *hbox, *frame, *framebox;
752 GtkWidget *add, *del, *edit;
754 vbox = gtk_vbox_new(FALSE, 3);
757 * We create the cylinder size model at startup, since
758 * we're going to share it across all cylinders and all
759 * dives. So if you add a new cylinder type in one dive,
760 * it will show up when you edit the cylinder types for
763 cylinder_model = create_tank_size_model();
765 cylinder_list.tree_view = cylinder_list_create();
767 hbox = gtk_hbox_new(FALSE, 3);
768 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
770 frame = gtk_frame_new("Cylinders");
771 gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
773 framebox = gtk_vbox_new(FALSE, 3);
774 gtk_container_add(GTK_CONTAINER(frame), framebox);
776 hbox = gtk_hbox_new(FALSE, 3);
777 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
779 gtk_box_pack_start(GTK_BOX(hbox), cylinder_list.tree_view, TRUE, FALSE, 3);
781 hbox = gtk_hbox_new(TRUE, 3);
782 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
784 edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
785 add = gtk_button_new_from_stock(GTK_STOCK_ADD);
786 del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
787 gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
788 gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
789 gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
791 cylinder_list.edit = edit;
792 cylinder_list.add = add;
793 cylinder_list.del = del;
795 g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), NULL);
796 g_signal_connect(add, "clicked", G_CALLBACK(add_cb), NULL);
797 g_signal_connect(del, "clicked", G_CALLBACK(del_cb), NULL);