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;
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)
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 cylinder_cb(GtkComboBox *combo_box, gpointer data)
102 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
103 GValue value1 = {0, }, value2 = {0,};
104 struct cylinder_widget *cylinder = data;
105 cylinder_t *cyl = current_dive->cylinder + cylinder->index;
107 /* Did the user set it to some non-standard value? */
108 if (!gtk_combo_box_get_active_iter(combo_box, &iter)) {
109 cylinder->changed = 1;
114 * We get "change" signal callbacks just because we set
115 * the description by hand. Whatever. So ignore them if
118 if (!cylinder->changed && cyl->type.description) {
120 char *desc = gtk_combo_box_get_active_text(combo_box);
122 same = !strcmp(desc, cyl->type.description);
127 cylinder->changed = 1;
129 gtk_tree_model_get_value(model, &iter, 1, &value1);
130 gtk_tree_model_get_value(model, &iter, 2, &value2);
132 set_cylinder_spinbuttons(cylinder, g_value_get_int(&value1), g_value_get_int(&value2));
136 * The gtk_tree_model_foreach() interface is bad. It could have
137 * returned whether the callback ever returned true
139 static int found_match = 0;
141 static gboolean match_cylinder(GtkTreeModel *model,
147 struct cylinder_widget *cylinder = data;
148 GValue value = {0, };
150 gtk_tree_model_get_value(model, iter, 0, &value);
151 name = g_value_get_string(&value);
152 if (strcmp(cylinder->name, name))
154 gtk_combo_box_set_active_iter(cylinder->description, iter);
159 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
164 model = gtk_combo_box_get_model(cylinder->description);
165 cylinder->name = desc;
166 gtk_tree_model_foreach(model, match_cylinder, cylinder);
169 GtkListStore *store = GTK_LIST_STORE(model);
172 gtk_list_store_append(store, &iter);
173 gtk_list_store_set(store, &iter,
178 gtk_combo_box_set_active_iter(cylinder->description, &iter);
182 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
188 /* Don't show uninitialized cylinder widgets */
189 if (!cylinder->description)
192 desc = cyl->type.description;
195 ml = cyl->type.size.mliter;
196 mbar = cyl->type.workingpressure.mbar;
197 add_cylinder(cylinder, desc, ml, mbar);
199 set_cylinder_spinbuttons(cylinder, cyl->type.size.mliter, cyl->type.workingpressure.mbar);
200 o2 = cyl->gasmix.o2.permille / 10.0;
201 gtk_widget_set_sensitive(cylinder->o2, !!o2);
202 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
205 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
208 static int cyl_nothing(cylinder_t *cyl)
210 return !cyl->type.size.mliter &&
211 !cyl->type.workingpressure.mbar &&
212 !cyl->type.description &&
213 !cyl->gasmix.o2.permille &&
214 !cyl->gasmix.he.permille &&
219 static void set_one_cylinder(int index, cylinder_t *cyl, GtkListStore *model, GtkTreeIter *iter)
221 gtk_list_store_set(model, iter,
222 CYL_DESC, cyl->type.description ? : "",
223 CYL_SIZE, cyl->type.size.mliter,
224 CYL_WORKP, cyl->type.workingpressure.mbar,
225 CYL_STARTP, cyl->start.mbar,
226 CYL_ENDP, cyl->end.mbar,
227 CYL_O2, cyl->gasmix.o2.permille,
228 CYL_HE, cyl->gasmix.he.permille,
232 void show_dive_equipment(struct dive *dive)
238 model = cylinder_list.model;
239 gtk_list_store_clear(model);
242 cylinder_t *cyl = &dive->cylinder[max-1];
244 if (!cyl_nothing(cyl))
248 cylinder_list.max_index = max;
250 gtk_widget_set_sensitive(cylinder_list.edit, 0);
251 gtk_widget_set_sensitive(cylinder_list.del, 0);
252 gtk_widget_set_sensitive(cylinder_list.add, max < MAX_CYLINDERS);
254 for (i = 0; i < max; i++) {
255 cylinder_t *cyl = dive->cylinder+i;
257 gtk_list_store_append(model, &iter);
258 set_one_cylinder(i, cyl, model, &iter);
262 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
264 GtkWidget *frame, *hbox, *button;
266 frame = gtk_frame_new(name);
267 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, TRUE, 0);
269 hbox = gtk_hbox_new(FALSE, 3);
270 gtk_container_add(GTK_CONTAINER(frame), hbox);
272 button = gtk_spin_button_new_with_range(min, max, incr);
273 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, TRUE, 0);
275 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
280 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc, double volume, double pressure, int o2)
284 if (output_units.pressure == PSI)
285 pressure /= 14.5037738;
287 if (pressure && output_units.volume == CUFT) {
288 volume *= 28.3168466; /* CUFT to liter */
289 volume /= pressure / 1.01325;
292 ml = volume * 1000 + 0.5;
293 mbar = pressure * 1000 + 0.5;
297 cyl->type.description = desc;
298 cyl->type.size.mliter = ml;
299 cyl->type.workingpressure.mbar = mbar;
300 cyl->gasmix.o2.permille = o2;
303 * Also, insert it into the model if it doesn't already exist
305 add_cylinder(cylinder, desc, ml, mbar);
308 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
312 double volume, pressure;
315 /* Ignore uninitialized cylinder widgets */
316 box = cylinder->description;
320 desc = gtk_combo_box_get_active_text(box);
321 volume = gtk_spin_button_get_value(cylinder->size);
322 pressure = gtk_spin_button_get_value(cylinder->pressure);
323 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
324 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
326 fill_cylinder_info(cylinder, cyl, desc, volume, pressure, o2);
329 void flush_dive_equipment_changes(struct dive *dive)
331 /* We do nothing: we require the "Ok" button press */
335 * We hardcode the most common standard cylinders,
336 * we should pick up any other names from the dive
339 static struct tank_info {
341 int size; /* cuft if < 1000, otherwise mliter */
342 int psi; /* If zero, size is in mliter */
344 /* Need an empty entry for the no-cylinder case */
347 /* Size-only metric cylinders */
351 /* Most common AL cylinders */
352 { "AL50", 50, 3000 },
353 { "AL63", 63, 3000 },
354 { "AL72", 72, 3000 },
355 { "AL80", 80, 3000 },
356 { "AL100", 100, 3300 },
358 /* Somewhat common LP steel cylinders */
359 { "LP85", 85, 2640 },
360 { "LP95", 95, 2640 },
361 { "LP108", 108, 2640 },
362 { "LP121", 121, 2640 },
364 /* Somewhat common HP steel cylinders */
365 { "HP65", 65, 3442 },
366 { "HP80", 80, 3442 },
367 { "HP100", 100, 3442 },
368 { "HP119", 119, 3442 },
369 { "HP130", 130, 3442 },
371 /* We'll fill in more from the dive log dynamically */
375 static void fill_tank_list(GtkListStore *store)
378 struct tank_info *info = tank_info;
381 int size = info->size;
383 int mbar = 0, ml = size;
385 /* Is it in cuft and psi? */
387 double bar = 0.0689475729 * psi;
388 double airvolume = 28316.8466 * size;
389 double atm = bar / 1.01325;
391 ml = airvolume / atm + 0.5;
392 mbar = bar*1000 + 0.5;
395 gtk_list_store_append(store, &iter);
396 gtk_list_store_set(store, &iter,
405 static void nitrox_cb(GtkToggleButton *button, gpointer data)
407 struct cylinder_widget *cylinder = data;
410 state = gtk_toggle_button_get_active(button);
411 gtk_widget_set_sensitive(cylinder->o2, state);
414 static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
416 GtkWidget *frame, *hbox;
419 frame = gtk_frame_new("Cylinder");
421 hbox = gtk_hbox_new(FALSE, 3);
422 gtk_container_add(GTK_CONTAINER(frame), hbox);
424 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
425 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
427 cylinder->description = GTK_COMBO_BOX(widget);
428 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
430 hbox = gtk_hbox_new(FALSE, 3);
431 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
432 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
434 widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
435 cylinder->size = GTK_SPIN_BUTTON(widget);
437 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
438 cylinder->pressure = GTK_SPIN_BUTTON(widget);
440 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
441 cylinder->o2 = widget;
442 cylinder->gasmix_button = gtk_check_button_new();
443 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
444 cylinder->gasmix_button, FALSE, FALSE, 3);
445 g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
447 gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
450 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
453 GtkWidget *dialog, *vbox;
454 struct cylinder_widget cylinder;
457 cylinder.index = index;
458 cylinder.changed = 0;
463 *cyl = dive->cylinder[index];
465 dialog = gtk_dialog_new_with_buttons("Cylinder",
466 GTK_WINDOW(main_window),
467 GTK_DIALOG_DESTROY_WITH_PARENT,
468 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
469 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
472 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
473 cylinder_widget(vbox, &cylinder, cylinder_model);
475 show_cylinder(cyl, &cylinder);
477 gtk_widget_show_all(dialog);
478 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
480 record_cylinder_changes(cyl, &cylinder);
481 dive->cylinder[index] = *cyl;
482 mark_divelist_changed(TRUE);
483 flush_divelist(dive);
486 gtk_widget_destroy(dialog);
491 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
496 path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
497 p = gtk_tree_path_get_indices(path);
499 gtk_tree_path_free(path);
503 static void edit_cb(GtkButton *button, gpointer data)
507 GtkListStore *model = cylinder_list.model;
508 GtkTreeSelection *selection;
511 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
513 /* Nothing selected? This shouldn't happen, since the button should be inactive */
514 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
517 index = get_model_index(model, &iter);
518 if (!edit_cylinder_dialog(index, &cyl))
521 set_one_cylinder(index, &cyl, model, &iter);
524 static void add_cb(GtkButton *button, gpointer data)
526 int index = cylinder_list.max_index;
528 GtkListStore *model = cylinder_list.model;
529 GtkTreeSelection *selection;
532 if (!edit_cylinder_dialog(index, &cyl))
535 gtk_list_store_append(model, &iter);
536 set_one_cylinder(index, &cyl, model, &iter);
538 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
539 gtk_tree_selection_select_iter(selection, &iter);
541 cylinder_list.max_index++;
542 gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
545 static void del_cb(GtkButton *button, gpointer data)
549 GtkListStore *model = cylinder_list.model;
550 GtkTreeSelection *selection;
554 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
556 /* Nothing selected? This shouldn't happen, since the button should be inactive */
557 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
560 index = get_model_index(model, &iter);
565 cyl = dive->cylinder + index;
566 nr = cylinder_list.max_index - index - 1;
568 gtk_list_store_remove(model, &iter);
570 cylinder_list.max_index--;
571 memmove(cyl, cyl+1, nr*sizeof(*cyl));
572 memset(cyl+nr, 0, sizeof(*cyl));
574 mark_divelist_changed(TRUE);
575 flush_divelist(dive);
577 gtk_widget_set_sensitive(cylinder_list.edit, 0);
578 gtk_widget_set_sensitive(cylinder_list.del, 0);
579 gtk_widget_set_sensitive(cylinder_list.add, 1);
582 static GtkListStore *create_tank_size_model(void)
586 model = gtk_list_store_new(3,
587 G_TYPE_STRING, /* Tank name */
588 G_TYPE_INT, /* Tank size in mliter */
589 G_TYPE_INT, /* Tank working pressure in mbar */
592 fill_tank_list(model);
596 static void size_data_func(GtkTreeViewColumn *col,
597 GtkCellRenderer *renderer,
603 double size, pressure;
606 gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
607 convert_volume_pressure(ml, mbar, &size, &pressure);
609 snprintf(buffer, sizeof(buffer), "%.1f", size);
611 strcpy(buffer, "unkn");
612 g_object_set(renderer, "text", buffer, NULL);
615 static void pressure_data_func(GtkTreeViewColumn *col,
616 GtkCellRenderer *renderer,
621 int index = (long)data;
626 gtk_tree_model_get(model, iter, index, &mbar, -1);
627 decimals = convert_pressure(mbar, &pressure);
629 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
632 g_object_set(renderer, "text", buffer, NULL);
635 static void percentage_data_func(GtkTreeViewColumn *col,
636 GtkCellRenderer *renderer,
641 int index = (long)data;
645 gtk_tree_model_get(model, iter, index, &permille, -1);
647 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
650 g_object_set(renderer, "text", buffer, NULL);
653 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
658 selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
659 gtk_widget_set_sensitive(cylinder_list.edit, selected);
660 gtk_widget_set_sensitive(cylinder_list.del, selected);
663 static void row_activated_cb(GtkTreeView *tree_view,
665 GtkTreeViewColumn *column,
671 static GtkWidget *cylinder_list_create(void)
673 GtkWidget *tree_view;
674 GtkTreeSelection *selection;
677 model = gtk_list_store_new(CYL_COLUMNS,
678 G_TYPE_STRING, /* CYL_DESC: utf8 */
679 G_TYPE_INT, /* CYL_SIZE: mliter */
680 G_TYPE_INT, /* CYL_WORKP: mbar */
681 G_TYPE_INT, /* CYL_STARTP: mbar */
682 G_TYPE_INT, /* CYL_ENDP: mbar */
683 G_TYPE_INT, /* CYL_O2: permille */
684 G_TYPE_INT /* CYL_HE: permille */
686 cylinder_list.model = model;
687 tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
688 g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), model);
690 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
691 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
692 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), model);
694 g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
695 "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
698 cylinder_list.desc = tree_view_column(tree_view, CYL_DESC, "Type", NULL, PANGO_ALIGN_LEFT, TRUE);
699 cylinder_list.size = tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, PANGO_ALIGN_RIGHT, TRUE);
700 cylinder_list.workp = tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
701 cylinder_list.startp = tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
702 cylinder_list.endp = tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
703 cylinder_list.o2 = tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
704 cylinder_list.he = tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
708 GtkWidget *equipment_widget(void)
710 GtkWidget *vbox, *hbox, *frame, *framebox;
711 GtkWidget *add, *del, *edit;
713 vbox = gtk_vbox_new(FALSE, 3);
716 * We create the cylinder size model at startup, since
717 * we're going to share it across all cylinders and all
718 * dives. So if you add a new cylinder type in one dive,
719 * it will show up when you edit the cylinder types for
722 cylinder_model = create_tank_size_model();
724 cylinder_list.tree_view = cylinder_list_create();
726 hbox = gtk_hbox_new(FALSE, 3);
727 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
729 frame = gtk_frame_new("Cylinders");
730 gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
732 framebox = gtk_vbox_new(FALSE, 3);
733 gtk_container_add(GTK_CONTAINER(frame), framebox);
735 hbox = gtk_hbox_new(FALSE, 3);
736 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
738 gtk_box_pack_start(GTK_BOX(hbox), cylinder_list.tree_view, TRUE, FALSE, 3);
740 hbox = gtk_hbox_new(TRUE, 3);
741 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
743 edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
744 add = gtk_button_new_from_stock(GTK_STOCK_ADD);
745 del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
746 gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
747 gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
748 gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
750 cylinder_list.edit = edit;
751 cylinder_list.add = add;
752 cylinder_list.del = del;
754 g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), NULL);
755 g_signal_connect(add, "clicked", G_CALLBACK(add_cb), NULL);
756 g_signal_connect(del, "clicked", G_CALLBACK(del_cb), NULL);