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_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 int found_match = 0;
153 static gboolean match_cylinder(GtkTreeModel *model,
159 struct cylinder_widget *cylinder = data;
160 GValue value = {0, };
162 gtk_tree_model_get_value(model, iter, 0, &value);
163 name = g_value_get_string(&value);
164 if (strcmp(cylinder->name, name))
166 gtk_combo_box_set_active_iter(cylinder->description, iter);
171 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
176 model = gtk_combo_box_get_model(cylinder->description);
177 cylinder->name = desc;
178 gtk_tree_model_foreach(model, match_cylinder, cylinder);
181 GtkListStore *store = GTK_LIST_STORE(model);
184 gtk_list_store_append(store, &iter);
185 gtk_list_store_set(store, &iter,
190 gtk_combo_box_set_active_iter(cylinder->description, &iter);
194 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
200 /* Don't show uninitialized cylinder widgets */
201 if (!cylinder->description)
204 desc = cyl->type.description;
207 ml = cyl->type.size.mliter;
208 mbar = cyl->type.workingpressure.mbar;
209 add_cylinder(cylinder, desc, ml, mbar);
211 set_cylinder_type_spinbuttons(cylinder,
212 cyl->type.size.mliter, cyl->type.workingpressure.mbar);
213 set_cylinder_pressure_spinbuttons(cylinder,
214 cyl->start.mbar, cyl->end.mbar);
215 o2 = cyl->gasmix.o2.permille / 10.0;
216 gtk_widget_set_sensitive(cylinder->o2, !!o2);
217 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), !!o2);
220 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
223 static int cyl_nothing(cylinder_t *cyl)
225 return !cyl->type.size.mliter &&
226 !cyl->type.workingpressure.mbar &&
227 !cyl->type.description &&
228 !cyl->gasmix.o2.permille &&
229 !cyl->gasmix.he.permille &&
234 static void set_one_cylinder(int index, cylinder_t *cyl, GtkListStore *model, GtkTreeIter *iter)
236 gtk_list_store_set(model, iter,
237 CYL_DESC, cyl->type.description ? : "",
238 CYL_SIZE, cyl->type.size.mliter,
239 CYL_WORKP, cyl->type.workingpressure.mbar,
240 CYL_STARTP, cyl->start.mbar,
241 CYL_ENDP, cyl->end.mbar,
242 CYL_O2, cyl->gasmix.o2.permille,
243 CYL_HE, cyl->gasmix.he.permille,
247 void show_dive_equipment(struct dive *dive)
253 model = cylinder_list.model;
254 gtk_list_store_clear(model);
257 cylinder_t *cyl = &dive->cylinder[max-1];
259 if (!cyl_nothing(cyl))
263 cylinder_list.max_index = max;
265 gtk_widget_set_sensitive(cylinder_list.edit, 0);
266 gtk_widget_set_sensitive(cylinder_list.del, 0);
267 gtk_widget_set_sensitive(cylinder_list.add, max < MAX_CYLINDERS);
269 for (i = 0; i < max; i++) {
270 cylinder_t *cyl = dive->cylinder+i;
272 gtk_list_store_append(model, &iter);
273 set_one_cylinder(i, cyl, model, &iter);
277 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
279 GtkWidget *frame, *hbox, *button;
281 frame = gtk_frame_new(name);
282 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, FALSE, 0);
284 hbox = gtk_hbox_new(FALSE, 3);
285 gtk_container_add(GTK_CONTAINER(frame), hbox);
287 button = gtk_spin_button_new_with_range(min, max, incr);
288 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
290 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
295 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc,
296 double volume, double pressure, double start, double end, int o2)
300 if (output_units.pressure == PSI) {
301 pressure /= 14.5037738;
306 if (pressure && output_units.volume == CUFT) {
307 volume *= 28.3168466; /* CUFT to liter */
308 volume /= pressure / 1.01325;
311 ml = volume * 1000 + 0.5;
312 mbar = pressure * 1000 + 0.5;
316 cyl->type.description = desc;
317 cyl->type.size.mliter = ml;
318 cyl->type.workingpressure.mbar = mbar;
319 cyl->start.mbar = start * 1000 + 0.5;
320 cyl->end.mbar = end * 1000 + 0.5;
321 cyl->gasmix.o2.permille = o2;
324 * Also, insert it into the model if it doesn't already exist
326 add_cylinder(cylinder, desc, ml, mbar);
329 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
333 double volume, pressure, start, end;
336 /* Ignore uninitialized cylinder widgets */
337 box = cylinder->description;
341 desc = gtk_combo_box_get_active_text(box);
342 volume = gtk_spin_button_get_value(cylinder->size);
343 pressure = gtk_spin_button_get_value(cylinder->pressure);
344 start = gtk_spin_button_get_value(cylinder->start);
345 end = gtk_spin_button_get_value(cylinder->end);
346 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
347 if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button)))
349 fill_cylinder_info(cylinder, cyl, desc, volume, pressure, start, end, o2);
352 void flush_dive_equipment_changes(struct dive *dive)
354 /* We do nothing: we require the "Ok" button press */
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(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
439 GtkWidget *frame, *hbox;
443 * Cylinder type: description, size and
446 frame = gtk_frame_new("Cylinder");
448 hbox = gtk_hbox_new(FALSE, 3);
449 gtk_container_add(GTK_CONTAINER(frame), hbox);
451 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
452 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
454 cylinder->description = GTK_COMBO_BOX(widget);
455 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
457 hbox = gtk_hbox_new(FALSE, 3);
458 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
459 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
461 widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
462 cylinder->size = GTK_SPIN_BUTTON(widget);
464 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
465 cylinder->pressure = GTK_SPIN_BUTTON(widget);
468 * Cylinder start/end pressures
470 hbox = gtk_hbox_new(FALSE, 3);
471 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
473 widget = create_spinbutton(hbox, "Start Pressure", 0, 5000, 1);
474 cylinder->start = GTK_SPIN_BUTTON(widget);
476 widget = create_spinbutton(hbox, "End Pressure", 0, 5000, 1);
477 cylinder->end = GTK_SPIN_BUTTON(widget);
480 * Cylinder gas mix: Air, Nitrox or Trimix
482 hbox = gtk_hbox_new(FALSE, 3);
483 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
485 widget = create_spinbutton(hbox, "Nitrox", 21, 100, 0.1);
486 cylinder->o2 = widget;
487 cylinder->gasmix_button = gtk_check_button_new();
488 gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(cylinder->o2)),
489 cylinder->gasmix_button, FALSE, FALSE, 3);
490 g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(nitrox_cb), cylinder);
492 gtk_spin_button_set_range(GTK_SPIN_BUTTON(cylinder->o2), 21.0, 100.0);
495 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
498 GtkWidget *dialog, *vbox;
499 struct cylinder_widget cylinder;
502 cylinder.index = index;
503 cylinder.changed = 0;
508 *cyl = dive->cylinder[index];
510 dialog = gtk_dialog_new_with_buttons("Cylinder",
511 GTK_WINDOW(main_window),
512 GTK_DIALOG_DESTROY_WITH_PARENT,
513 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
514 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
517 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
518 cylinder_widget(vbox, &cylinder, cylinder_model);
520 show_cylinder(cyl, &cylinder);
522 gtk_widget_show_all(dialog);
523 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
525 record_cylinder_changes(cyl, &cylinder);
526 dive->cylinder[index] = *cyl;
527 mark_divelist_changed(TRUE);
528 flush_divelist(dive);
531 gtk_widget_destroy(dialog);
536 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
541 path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
542 p = gtk_tree_path_get_indices(path);
544 gtk_tree_path_free(path);
548 static void edit_cb(GtkButton *button, gpointer data)
552 GtkListStore *model = cylinder_list.model;
553 GtkTreeSelection *selection;
556 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
558 /* Nothing selected? This shouldn't happen, since the button should be inactive */
559 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
562 index = get_model_index(model, &iter);
563 if (!edit_cylinder_dialog(index, &cyl))
566 set_one_cylinder(index, &cyl, model, &iter);
570 static void add_cb(GtkButton *button, gpointer data)
572 int index = cylinder_list.max_index;
574 GtkListStore *model = cylinder_list.model;
575 GtkTreeSelection *selection;
578 if (!edit_cylinder_dialog(index, &cyl))
581 gtk_list_store_append(model, &iter);
582 set_one_cylinder(index, &cyl, model, &iter);
584 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
585 gtk_tree_selection_select_iter(selection, &iter);
587 cylinder_list.max_index++;
588 gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
591 static void del_cb(GtkButton *button, gpointer data)
595 GtkListStore *model = cylinder_list.model;
596 GtkTreeSelection *selection;
600 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cylinder_list.tree_view));
602 /* Nothing selected? This shouldn't happen, since the button should be inactive */
603 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
606 index = get_model_index(model, &iter);
611 cyl = dive->cylinder + index;
612 nr = cylinder_list.max_index - index - 1;
614 gtk_list_store_remove(model, &iter);
616 cylinder_list.max_index--;
617 memmove(cyl, cyl+1, nr*sizeof(*cyl));
618 memset(cyl+nr, 0, sizeof(*cyl));
620 mark_divelist_changed(TRUE);
621 flush_divelist(dive);
623 gtk_widget_set_sensitive(cylinder_list.edit, 0);
624 gtk_widget_set_sensitive(cylinder_list.del, 0);
625 gtk_widget_set_sensitive(cylinder_list.add, 1);
628 static GtkListStore *create_tank_size_model(void)
632 model = gtk_list_store_new(3,
633 G_TYPE_STRING, /* Tank name */
634 G_TYPE_INT, /* Tank size in mliter */
635 G_TYPE_INT, /* Tank working pressure in mbar */
638 fill_tank_list(model);
642 static void size_data_func(GtkTreeViewColumn *col,
643 GtkCellRenderer *renderer,
649 double size, pressure;
652 gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
653 convert_volume_pressure(ml, mbar, &size, &pressure);
655 snprintf(buffer, sizeof(buffer), "%.1f", size);
657 strcpy(buffer, "unkn");
658 g_object_set(renderer, "text", buffer, NULL);
661 static void pressure_data_func(GtkTreeViewColumn *col,
662 GtkCellRenderer *renderer,
667 int index = (long)data;
672 gtk_tree_model_get(model, iter, index, &mbar, -1);
673 decimals = convert_pressure(mbar, &pressure);
675 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
678 g_object_set(renderer, "text", buffer, NULL);
681 static void percentage_data_func(GtkTreeViewColumn *col,
682 GtkCellRenderer *renderer,
687 int index = (long)data;
691 gtk_tree_model_get(model, iter, index, &permille, -1);
693 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
696 g_object_set(renderer, "text", buffer, NULL);
699 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
704 selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
705 gtk_widget_set_sensitive(cylinder_list.edit, selected);
706 gtk_widget_set_sensitive(cylinder_list.del, selected);
709 static void row_activated_cb(GtkTreeView *tree_view,
711 GtkTreeViewColumn *column,
717 static GtkWidget *cylinder_list_create(void)
719 GtkWidget *tree_view;
720 GtkTreeSelection *selection;
723 model = gtk_list_store_new(CYL_COLUMNS,
724 G_TYPE_STRING, /* CYL_DESC: utf8 */
725 G_TYPE_INT, /* CYL_SIZE: mliter */
726 G_TYPE_INT, /* CYL_WORKP: mbar */
727 G_TYPE_INT, /* CYL_STARTP: mbar */
728 G_TYPE_INT, /* CYL_ENDP: mbar */
729 G_TYPE_INT, /* CYL_O2: permille */
730 G_TYPE_INT /* CYL_HE: permille */
732 cylinder_list.model = model;
733 tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
734 g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), model);
736 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
737 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
738 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), model);
740 g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
741 "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
744 cylinder_list.desc = tree_view_column(tree_view, CYL_DESC, "Type", NULL, PANGO_ALIGN_LEFT, TRUE);
745 cylinder_list.size = tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, PANGO_ALIGN_RIGHT, TRUE);
746 cylinder_list.workp = tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
747 cylinder_list.startp = tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
748 cylinder_list.endp = tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, PANGO_ALIGN_RIGHT, TRUE);
749 cylinder_list.o2 = tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
750 cylinder_list.he = tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, PANGO_ALIGN_RIGHT, TRUE);
754 GtkWidget *equipment_widget(void)
756 GtkWidget *vbox, *hbox, *frame, *framebox;
757 GtkWidget *add, *del, *edit;
759 vbox = gtk_vbox_new(FALSE, 3);
762 * We create the cylinder size model at startup, since
763 * we're going to share it across all cylinders and all
764 * dives. So if you add a new cylinder type in one dive,
765 * it will show up when you edit the cylinder types for
768 cylinder_model = create_tank_size_model();
770 cylinder_list.tree_view = cylinder_list_create();
772 hbox = gtk_hbox_new(FALSE, 3);
773 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
775 frame = gtk_frame_new("Cylinders");
776 gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
778 framebox = gtk_vbox_new(FALSE, 3);
779 gtk_container_add(GTK_CONTAINER(frame), framebox);
781 hbox = gtk_hbox_new(FALSE, 3);
782 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
784 gtk_box_pack_start(GTK_BOX(hbox), cylinder_list.tree_view, TRUE, FALSE, 3);
786 hbox = gtk_hbox_new(TRUE, 3);
787 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
789 edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
790 add = gtk_button_new_from_stock(GTK_STOCK_ADD);
791 del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
792 gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
793 gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
794 gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
796 cylinder_list.edit = edit;
797 cylinder_list.add = add;
798 cylinder_list.del = del;
800 g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), NULL);
801 g_signal_connect(add, "clicked", G_CALLBACK(add_cb), NULL);
802 g_signal_connect(del, "clicked", G_CALLBACK(del_cb), NULL);