2 /* creates the UI for the equipment page -
3 * controlled through the following interfaces:
5 * void show_dive_equipment(struct dive *dive)
8 * GtkWidget *equipment_widget(void)
18 #include "display-gtk.h"
21 static GtkListStore *cylinder_model, *weightsystem_model;
40 struct equipment_list {
43 GtkWidget *edit, *add, *del;
46 static struct equipment_list cylinder_list, weightsystem_list;
49 struct cylinder_widget {
53 GtkComboBox *description;
54 GtkSpinButton *size, *pressure;
55 GtkWidget *start, *end, *pressure_button;
56 GtkWidget *o2, *he, *gasmix_button;
63 GtkComboBox *description;
64 GtkSpinButton *weight;
67 /* we want bar - so let's not use our unit functions */
68 static int convert_pressure(int mbar, double *p)
73 if (output_units.pressure == PSI) {
74 pressure = mbar_to_PSI(mbar);
77 pressure = mbar / 1000.0;
84 static void convert_volume_pressure(int ml, int mbar, double *v, double *p)
86 double volume, pressure;
90 if (output_units.volume == CUFT) {
91 volume = ml_to_cuft(ml);
92 volume *= bar_to_atm(mbar / 1000.0);
95 if (output_units.pressure == PSI) {
96 pressure = mbar_to_PSI(mbar);
98 pressure = mbar / 1000.0;
104 static int convert_weight(int grams, double *m)
106 int decimals = 1; /* not sure - do people do less than whole lbs/kg ? */
109 if (output_units.weight == LBS)
110 weight = grams_to_lbs(grams);
112 weight = grams / 1000.0;
117 static void set_cylinder_type_spinbuttons(struct cylinder_widget *cylinder, int ml, int mbar)
119 double volume, pressure;
121 convert_volume_pressure(ml, mbar, &volume, &pressure);
122 gtk_spin_button_set_value(cylinder->size, volume);
123 gtk_spin_button_set_value(cylinder->pressure, pressure);
126 static void set_cylinder_pressure_spinbuttons(struct cylinder_widget *cylinder, cylinder_t *cyl)
129 unsigned int start, end;
132 start = cyl->start.mbar;
136 start = cyl->sample_start.mbar;
137 end = cyl->sample_end.mbar;
139 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button), set);
140 gtk_widget_set_sensitive(cylinder->start, set);
141 gtk_widget_set_sensitive(cylinder->end, set);
143 convert_pressure(start, &pressure);
144 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->start), pressure);
145 convert_pressure(end, &pressure);
146 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->end), pressure);
149 static void set_weight_weight_spinbutton(struct ws_widget *ws_widget, int grams)
153 convert_weight(grams, &weight);
154 gtk_spin_button_set_value(ws_widget->weight, weight);
158 * The gtk_tree_model_foreach() interface is bad. It could have
159 * returned whether the callback ever returned true
161 static GtkTreeIter *found_match = NULL;
162 static GtkTreeIter match_iter;
164 static gboolean match_desc(GtkTreeModel *model, GtkTreePath *path,
165 GtkTreeIter *iter, gpointer data)
169 const char *desc = data;
171 gtk_tree_model_get(model, iter, 0, &name, -1);
172 match = !strcmp(desc, name);
176 found_match = &match_iter;
181 static int get_active_item(GtkComboBox *combo_box, GtkTreeIter *iter, GtkListStore *model)
185 if (gtk_combo_box_get_active_iter(combo_box, iter))
188 desc = gtk_combo_box_get_active_text(combo_box);
191 gtk_tree_model_foreach(GTK_TREE_MODEL(model), match_desc, (void *)desc);
197 *iter = *found_match;
198 gtk_combo_box_set_active_iter(combo_box, iter);
202 static void cylinder_cb(GtkComboBox *combo_box, gpointer data)
205 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
207 struct cylinder_widget *cylinder = data;
208 cylinder_t *cyl = current_dive->cylinder + cylinder->index;
210 /* Did the user set it to some non-standard value? */
211 if (!get_active_item(combo_box, &iter, cylinder_model)) {
212 cylinder->changed = 1;
217 * We get "change" signal callbacks just because we set
218 * the description by hand. Whatever. So ignore them if
221 if (!cylinder->changed && cyl->type.description) {
223 char *desc = gtk_combo_box_get_active_text(combo_box);
225 same = !strcmp(desc, cyl->type.description);
230 cylinder->changed = 1;
232 gtk_tree_model_get(model, &iter,
237 set_cylinder_type_spinbuttons(cylinder, ml, mbar);
240 static void weight_cb(GtkComboBox *combo_box, gpointer data)
243 GtkTreeModel *model = gtk_combo_box_get_model(combo_box);
245 struct ws_widget *ws_widget = data;
246 weightsystem_t *ws = current_dive->weightsystem + ws_widget->index;
248 /* Did the user set it to some non-standard value? */
249 if (!get_active_item(combo_box, &iter, weightsystem_model)) {
250 ws_widget->changed = 1;
255 * We get "change" signal callbacks just because we set
256 * the description by hand. Whatever. So ignore them if
259 if (!ws_widget->changed && ws->description) {
261 char *desc = gtk_combo_box_get_active_text(combo_box);
263 same = !strcmp(desc, ws->description);
268 ws_widget->changed = 1;
270 gtk_tree_model_get(model, &iter,
274 set_weight_weight_spinbutton(ws_widget, weight);
277 static GtkTreeIter *add_cylinder_type(const char *desc, int ml, int mbar, GtkTreeIter *iter)
281 /* Don't even bother adding stuff without a size */
286 model = GTK_TREE_MODEL(cylinder_model);
287 gtk_tree_model_foreach(model, match_desc, (void *)desc);
290 GtkListStore *store = GTK_LIST_STORE(model);
292 gtk_list_store_append(store, iter);
293 gtk_list_store_set(store, iter,
303 static GtkTreeIter *add_weightsystem_type(const char *desc, int weight, GtkTreeIter *iter)
308 model = GTK_TREE_MODEL(weightsystem_model);
309 gtk_tree_model_foreach(model, match_desc, (void *)desc);
312 GtkListStore *store = GTK_LIST_STORE(model);
314 gtk_list_store_append(store, iter);
315 gtk_list_store_set(store, iter,
325 * When adding a dive, we'll add all the pre-existing cylinder
326 * information from that dive to our cylinder model.
328 void add_cylinder_description(cylinder_type_t *type)
332 unsigned int size, workp;
334 desc = type->description;
337 size = type->size.mliter;
338 workp = type->workingpressure.mbar;
339 add_cylinder_type(desc, size, workp, &iter);
342 static void add_cylinder(struct cylinder_widget *cylinder, const char *desc, int ml, int mbar)
344 GtkTreeIter iter, *match;
346 cylinder->name = desc;
347 match = add_cylinder_type(desc, ml, mbar, &iter);
349 gtk_combo_box_set_active_iter(cylinder->description, match);
352 void add_weightsystem_description(weightsystem_t *weightsystem)
358 desc = weightsystem->description;
361 weight = weightsystem->weight.grams;
362 add_weightsystem_type(desc, weight, &iter);
365 static void add_weightsystem(struct ws_widget *ws_widget, const char *desc, int weight)
367 GtkTreeIter iter, *match;
369 ws_widget->name = desc;
370 match = add_weightsystem_type(desc, weight, &iter);
372 gtk_combo_box_set_active_iter(ws_widget->description, match);
375 static void show_cylinder(cylinder_t *cyl, struct cylinder_widget *cylinder)
382 /* Don't show uninitialized cylinder widgets */
383 if (!cylinder->description)
386 desc = cyl->type.description;
389 ml = cyl->type.size.mliter;
390 mbar = cyl->type.workingpressure.mbar;
391 add_cylinder(cylinder, desc, ml, mbar);
393 set_cylinder_type_spinbuttons(cylinder,
394 cyl->type.size.mliter, cyl->type.workingpressure.mbar);
395 set_cylinder_pressure_spinbuttons(cylinder, cyl);
397 gasmix = cyl->gasmix.o2.permille || cyl->gasmix.he.permille;
398 gtk_widget_set_sensitive(cylinder->o2, gasmix);
399 gtk_widget_set_sensitive(cylinder->he, gasmix);
400 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button), gasmix);
402 o2 = cyl->gasmix.o2.permille / 10.0;
403 he = cyl->gasmix.he.permille / 10.0;
405 o2 = AIR_PERMILLE / 10.0;
406 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->o2), o2);
407 gtk_spin_button_set_value(GTK_SPIN_BUTTON(cylinder->he), he);
410 static void show_weightsystem(weightsystem_t *ws, struct ws_widget *weightsystem_widget)
415 /* Don't show uninitialized widgets */
416 if (!weightsystem_widget->description)
419 desc = ws->description;
422 grams = ws->weight.grams;
423 add_weightsystem(weightsystem_widget, desc, grams);
425 set_weight_weight_spinbutton(weightsystem_widget, ws->weight.grams);
428 int cylinder_none(void *_data)
430 cylinder_t *cyl = _data;
431 return !cyl->type.size.mliter &&
432 !cyl->type.workingpressure.mbar &&
433 !cyl->type.description &&
434 !cyl->gasmix.o2.permille &&
435 !cyl->gasmix.he.permille &&
436 !cyl->sample_start.mbar &&
437 !cyl->sample_end.mbar &&
442 int weightsystem_none(void *_data)
444 weightsystem_t *ws = _data;
445 return !ws->weight.grams && !ws->description;
448 static void set_one_cylinder(void *_data, GtkListStore *model, GtkTreeIter *iter)
450 cylinder_t *cyl = _data;
451 unsigned int start, end;
453 start = cyl->start.mbar ? : cyl->sample_start.mbar;
454 end = cyl->end.mbar ? : cyl->sample_end.mbar;
455 gtk_list_store_set(model, iter,
456 CYL_DESC, cyl->type.description ? : "",
457 CYL_SIZE, cyl->type.size.mliter,
458 CYL_WORKP, cyl->type.workingpressure.mbar,
461 CYL_O2, cyl->gasmix.o2.permille,
462 CYL_HE, cyl->gasmix.he.permille,
466 static void set_one_weightsystem(void *_data, GtkListStore *model, GtkTreeIter *iter)
468 weightsystem_t *ws = _data;
470 gtk_list_store_set(model, iter,
471 WS_DESC, ws->description ? : "unspecified",
472 WS_WEIGHT, ws->weight.grams,
476 static void *cyl_ptr(struct dive *dive, int idx)
478 if (idx < 0 || idx >= MAX_CYLINDERS)
480 return &dive->cylinder[idx];
483 static void *ws_ptr(struct dive *dive, int idx)
485 if (idx < 0 || idx >= MAX_WEIGHTSYSTEMS)
487 return &dive->weightsystem[idx];
490 static void show_equipment(struct dive *dive, int max,
491 struct equipment_list *equipment_list,
492 void*(*ptr_function)(struct dive*, int),
493 int(*none_function)(void *),
494 void(*set_one_function)(void*, GtkListStore*, GtkTreeIter *))
499 GtkListStore *model = equipment_list->model;
501 gtk_list_store_clear(model);
504 data = ptr_function(dive, used-1);
505 if (!none_function(data))
509 equipment_list->max_index = used;
511 gtk_widget_set_sensitive(equipment_list->edit, 0);
512 gtk_widget_set_sensitive(equipment_list->del, 0);
513 gtk_widget_set_sensitive(equipment_list->add, used < max);
515 for (i = 0; i < used; i++) {
516 data = ptr_function(dive, i);
517 gtk_list_store_append(model, &iter);
518 set_one_function(data, model, &iter);
522 void show_dive_equipment(struct dive *dive)
524 show_equipment(dive, MAX_CYLINDERS, &cylinder_list,
525 &cyl_ptr, &cylinder_none, &set_one_cylinder);
526 show_equipment(dive, MAX_WEIGHTSYSTEMS, &weightsystem_list,
527 &ws_ptr, &weightsystem_none, &set_one_weightsystem);
530 static GtkWidget *create_spinbutton(GtkWidget *vbox, const char *name, double min, double max, double incr)
532 GtkWidget *frame, *hbox, *button;
534 frame = gtk_frame_new(name);
535 gtk_box_pack_start(GTK_BOX(vbox), frame, TRUE, FALSE, 0);
537 hbox = gtk_hbox_new(FALSE, 3);
538 gtk_container_add(GTK_CONTAINER(frame), hbox);
540 button = gtk_spin_button_new_with_range(min, max, incr);
541 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
543 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
548 static void fill_cylinder_info(struct cylinder_widget *cylinder, cylinder_t *cyl, const char *desc,
549 double volume, double pressure, double start, double end, int o2, int he)
553 if (output_units.pressure == PSI) {
554 pressure = psi_to_bar(pressure);
555 start = psi_to_bar(start);
556 end = psi_to_bar(end);
559 if (pressure && output_units.volume == CUFT) {
560 volume = cuft_to_l(volume);
561 volume /= bar_to_atm(pressure);
564 ml = volume * 1000 + 0.5;
565 mbar = pressure * 1000 + 0.5;
567 /* Ignore obviously crazy He values */
571 /* We have a rule that normal air is all zeroes */
572 if (!he && o2 > 208 && o2 < 211)
575 cyl->type.description = desc;
576 cyl->type.size.mliter = ml;
577 cyl->type.workingpressure.mbar = mbar;
578 cyl->start.mbar = start * 1000 + 0.5;
579 cyl->end.mbar = end * 1000 + 0.5;
580 cyl->gasmix.o2.permille = o2;
581 cyl->gasmix.he.permille = he;
584 * Also, insert it into the model if it doesn't already exist
586 add_cylinder(cylinder, desc, ml, mbar);
589 static void record_cylinder_changes(cylinder_t *cyl, struct cylinder_widget *cylinder)
593 double volume, pressure, start, end;
596 /* Ignore uninitialized cylinder widgets */
597 box = cylinder->description;
601 desc = gtk_combo_box_get_active_text(box);
602 volume = gtk_spin_button_get_value(cylinder->size);
603 pressure = gtk_spin_button_get_value(cylinder->pressure);
604 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->pressure_button))) {
605 start = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->start));
606 end = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->end));
610 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cylinder->gasmix_button))) {
611 o2 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->o2))*10 + 0.5;
612 he = gtk_spin_button_get_value(GTK_SPIN_BUTTON(cylinder->he))*10 + 0.5;
617 fill_cylinder_info(cylinder, cyl, desc, volume, pressure, start, end, o2, he);
620 static void record_weightsystem_changes(weightsystem_t *ws, struct ws_widget *weightsystem_widget)
627 /* Ignore uninitialized cylinder widgets */
628 box = weightsystem_widget->description;
632 desc = gtk_combo_box_get_active_text(box);
633 value = gtk_spin_button_get_value(weightsystem_widget->weight);
635 if (output_units.weight == LBS)
636 grams = lbs_to_grams(value);
638 grams = value * 1000;
639 ws->weight.grams = grams;
640 ws->description = desc;
644 * We hardcode the most common standard cylinders,
645 * we should pick up any other names from the dive
648 static struct tank_info {
650 int cuft, ml, psi, bar;
652 /* Need an empty entry for the no-cylinder case */
655 /* Size-only metric cylinders */
656 { "10.0 l", .ml = 10000 },
657 { "11.1 l", .ml = 11100 },
659 /* Most common AL cylinders */
660 { "AL50", .cuft = 50, .psi = 3000 },
661 { "AL63", .cuft = 63, .psi = 3000 },
662 { "AL72", .cuft = 72, .psi = 3000 },
663 { "AL80", .cuft = 80, .psi = 3000 },
664 { "AL100", .cuft = 100, .psi = 3300 },
666 /* Somewhat common LP steel cylinders */
667 { "LP85", .cuft = 85, .psi = 2640 },
668 { "LP95", .cuft = 95, .psi = 2640 },
669 { "LP108", .cuft = 108, .psi = 2640 },
670 { "LP121", .cuft = 121, .psi = 2640 },
672 /* Somewhat common HP steel cylinders */
673 { "HP65", .cuft = 65, .psi = 3442 },
674 { "HP80", .cuft = 80, .psi = 3442 },
675 { "HP100", .cuft = 100, .psi = 3442 },
676 { "HP119", .cuft = 119, .psi = 3442 },
677 { "HP130", .cuft = 130, .psi = 3442 },
679 /* Common European steel cylinders */
680 { "10L 300 bar", .ml = 10000, .bar = 300 },
681 { "12L 200 bar", .ml = 12000, .bar = 200 },
682 { "12L 232 bar", .ml = 12000, .bar = 232 },
683 { "12L 300 bar", .ml = 12000, .bar = 300 },
684 { "15L 200 bar", .ml = 15000, .bar = 200 },
685 { "15L 232 bar", .ml = 15000, .bar = 232 },
686 { "D7 300 bar", .ml = 14000, .bar = 300 },
687 { "D8.5 232 bar", .ml = 17000, .bar = 232 },
688 { "D12 232 bar", .ml = 24000, .bar = 232 },
690 /* We'll fill in more from the dive log dynamically */
694 static void fill_tank_list(GtkListStore *store)
697 struct tank_info *info = tank_info;
699 for (info = tank_info ; info->name; info++) {
701 int cuft = info->cuft;
704 double bar = info->bar;
713 /* Is it in cuft and psi? */
715 bar = psi_to_bar(psi);
718 double airvolume = cuft_to_l(cuft) * 1000.0;
719 double atm = bar_to_atm(bar);
721 ml = airvolume / atm + 0.5;
724 mbar = bar * 1000 + 0.5;
726 gtk_list_store_append(store, &iter);
727 gtk_list_store_set(store, &iter,
735 fprintf(stderr, "Bad tank info for '%s'\n", info->name);
740 * We hardcode the most common weight system types
741 * This is a bit odd as the weight system types don't usually encode weight
743 static struct ws_info {
747 /* Need an empty entry for the no weight system case */
756 static void fill_ws_list(GtkListStore *store)
759 struct ws_info *info = ws_info;
762 gtk_list_store_append(store, &iter);
763 gtk_list_store_set(store, &iter,
771 static void gasmix_cb(GtkToggleButton *button, gpointer data)
773 struct cylinder_widget *cylinder = data;
776 state = gtk_toggle_button_get_active(button);
777 gtk_widget_set_sensitive(cylinder->o2, state);
778 gtk_widget_set_sensitive(cylinder->he, state);
781 static void pressure_cb(GtkToggleButton *button, gpointer data)
783 struct cylinder_widget *cylinder = data;
786 state = gtk_toggle_button_get_active(button);
787 gtk_widget_set_sensitive(cylinder->start, state);
788 gtk_widget_set_sensitive(cylinder->end, state);
791 static gboolean completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct cylinder_widget *cylinder)
794 unsigned int ml, mbar;
796 gtk_tree_model_get(model, iter, CYL_DESC, &desc, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
797 add_cylinder(cylinder, desc, ml, mbar);
801 static void cylinder_activate_cb(GtkComboBox *combo_box, gpointer data)
803 struct cylinder_widget *cylinder = data;
804 cylinder_cb(cylinder->description, data);
807 /* Return a frame containing a hbox inside a hbox */
808 static GtkWidget *frame_box(const char *title, GtkWidget *vbox)
810 GtkWidget *hbox, *frame;
812 hbox = gtk_hbox_new(FALSE, 10);
813 gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE, FALSE, 0);
815 frame = gtk_frame_new(title);
816 gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 0);
818 hbox = gtk_hbox_new(FALSE, 10);
819 gtk_container_add(GTK_CONTAINER(frame), hbox);
824 static GtkWidget *labeled_spinbutton(GtkWidget *box, const char *name, double min, double max, double incr)
826 GtkWidget *hbox, *label, *button;
828 hbox = gtk_hbox_new(FALSE, 0);
829 gtk_box_pack_start(GTK_BOX(box), hbox, TRUE, FALSE, 0);
831 label = gtk_label_new(name);
832 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, FALSE, 0);
834 button = gtk_spin_button_new_with_range(min, max, incr);
835 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, FALSE, 0);
837 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
842 static void cylinder_widget(GtkWidget *vbox, struct cylinder_widget *cylinder, GtkListStore *model)
844 GtkWidget *frame, *hbox;
846 GtkEntryCompletion *completion;
850 * Cylinder type: description, size and
853 frame = gtk_frame_new("Cylinder");
855 hbox = gtk_hbox_new(FALSE, 3);
856 gtk_container_add(GTK_CONTAINER(frame), hbox);
858 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
859 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
861 cylinder->description = GTK_COMBO_BOX(widget);
862 g_signal_connect(widget, "changed", G_CALLBACK(cylinder_cb), cylinder);
864 entry = GTK_ENTRY(GTK_BIN(widget)->child);
865 g_signal_connect(entry, "activate", G_CALLBACK(cylinder_activate_cb), cylinder);
867 completion = gtk_entry_completion_new();
868 gtk_entry_completion_set_text_column(completion, 0);
869 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
870 g_signal_connect(completion, "match-selected", G_CALLBACK(completion_cb), cylinder);
871 gtk_entry_set_completion(entry, completion);
873 hbox = gtk_hbox_new(FALSE, 3);
874 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
875 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
877 widget = create_spinbutton(hbox, "Size", 0, 300, 0.1);
878 cylinder->size = GTK_SPIN_BUTTON(widget);
880 widget = create_spinbutton(hbox, "Pressure", 0, 5000, 1);
881 cylinder->pressure = GTK_SPIN_BUTTON(widget);
884 * Cylinder start/end pressures
886 hbox = frame_box("Pressure", vbox);
888 widget = labeled_spinbutton(hbox, "Start", 0, 5000, 1);
889 cylinder->start = widget;
891 widget = labeled_spinbutton(hbox, "End", 0, 5000, 1);
892 cylinder->end = widget;
894 cylinder->pressure_button = gtk_check_button_new();
895 gtk_box_pack_start(GTK_BOX(hbox), cylinder->pressure_button, FALSE, FALSE, 3);
896 g_signal_connect(cylinder->pressure_button, "toggled", G_CALLBACK(pressure_cb), cylinder);
899 * Cylinder gas mix: Air, Nitrox or Trimix
901 hbox = frame_box("Gasmix", vbox);
903 widget = labeled_spinbutton(hbox, "O"UTF8_SUBSCRIPT_2 "%", 1, 100, 0.1);
904 cylinder->o2 = widget;
905 widget = labeled_spinbutton(hbox, "He%", 0, 100, 0.1);
906 cylinder->he = widget;
907 cylinder->gasmix_button = gtk_check_button_new();
908 gtk_box_pack_start(GTK_BOX(hbox), cylinder->gasmix_button, FALSE, FALSE, 3);
909 g_signal_connect(cylinder->gasmix_button, "toggled", G_CALLBACK(gasmix_cb), cylinder);
912 static gboolean weight_completion_cb(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, struct ws_widget *ws_widget)
917 gtk_tree_model_get(model, iter, WS_DESC, &desc, WS_WEIGHT, &weight, -1);
918 add_weightsystem(ws_widget, desc, weight);
922 static void weight_activate_cb(GtkComboBox *combo_box, gpointer data)
924 struct ws_widget *ws_widget = data;
925 weight_cb(ws_widget->description, data);
928 static void ws_widget(GtkWidget *vbox, struct ws_widget *ws_widget, GtkListStore *model)
930 GtkWidget *frame, *hbox;
931 GtkEntryCompletion *completion;
936 * weight_system: description and weight
938 frame = gtk_frame_new("Weight");
940 hbox = gtk_hbox_new(FALSE, 3);
941 gtk_container_add(GTK_CONTAINER(frame), hbox);
943 widget = gtk_combo_box_entry_new_with_model(GTK_TREE_MODEL(model), 0);
944 gtk_box_pack_start(GTK_BOX(hbox), widget, FALSE, TRUE, 0);
946 ws_widget->description = GTK_COMBO_BOX(widget);
947 g_signal_connect(widget, "changed", G_CALLBACK(weight_cb), ws_widget);
949 entry = GTK_ENTRY(GTK_BIN(widget)->child);
950 g_signal_connect(entry, "activate", G_CALLBACK(weight_activate_cb), ws_widget);
952 completion = gtk_entry_completion_new();
953 gtk_entry_completion_set_text_column(completion, 0);
954 gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model));
955 g_signal_connect(completion, "match-selected", G_CALLBACK(weight_completion_cb), ws_widget);
956 gtk_entry_set_completion(entry, completion);
958 hbox = gtk_hbox_new(FALSE, 3);
959 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 0);
960 gtk_box_pack_start(GTK_BOX(hbox), frame, FALSE, TRUE, 0);
962 if ( output_units.weight == KG)
963 widget = create_spinbutton(hbox, "kg", 0, 50, 0.5);
965 widget = create_spinbutton(hbox, "lbs", 0, 110, 1);
966 ws_widget->weight = GTK_SPIN_BUTTON(widget);
969 static int edit_cylinder_dialog(int index, cylinder_t *cyl)
972 GtkWidget *dialog, *vbox;
973 struct cylinder_widget cylinder;
976 cylinder.index = index;
977 cylinder.changed = 0;
982 *cyl = dive->cylinder[index];
984 dialog = gtk_dialog_new_with_buttons("Cylinder",
985 GTK_WINDOW(main_window),
986 GTK_DIALOG_DESTROY_WITH_PARENT,
987 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
988 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
991 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
992 cylinder_widget(vbox, &cylinder, cylinder_model);
994 show_cylinder(cyl, &cylinder);
996 gtk_widget_show_all(dialog);
997 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
999 record_cylinder_changes(cyl, &cylinder);
1000 dive->cylinder[index] = *cyl;
1001 mark_divelist_changed(TRUE);
1002 update_cylinder_related_info(dive);
1003 flush_divelist(dive);
1006 gtk_widget_destroy(dialog);
1011 static int edit_weightsystem_dialog(int index, weightsystem_t *ws)
1014 GtkWidget *dialog, *vbox;
1015 struct ws_widget weightsystem_widget;
1018 weightsystem_widget.index = index;
1019 weightsystem_widget.changed = 0;
1021 dive = current_dive;
1024 *ws = dive->weightsystem[index];
1026 dialog = gtk_dialog_new_with_buttons("Weight System",
1027 GTK_WINDOW(main_window),
1028 GTK_DIALOG_DESTROY_WITH_PARENT,
1029 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1030 GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
1033 vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
1034 ws_widget(vbox, &weightsystem_widget, weightsystem_model);
1036 show_weightsystem(ws, &weightsystem_widget);
1038 gtk_widget_show_all(dialog);
1039 success = gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT;
1041 record_weightsystem_changes(ws, &weightsystem_widget);
1042 dive->weightsystem[index] = *ws;
1043 mark_divelist_changed(TRUE);
1044 flush_divelist(dive);
1047 gtk_widget_destroy(dialog);
1052 static int get_model_index(GtkListStore *model, GtkTreeIter *iter)
1057 path = gtk_tree_model_get_path(GTK_TREE_MODEL(model), iter);
1058 p = gtk_tree_path_get_indices(path);
1060 gtk_tree_path_free(path);
1064 static void edit_cb(GtkButton *button, GtkTreeView *tree_view)
1068 GtkListStore *model = cylinder_list.model;
1069 GtkTreeSelection *selection;
1072 selection = gtk_tree_view_get_selection(tree_view);
1074 /* Nothing selected? This shouldn't happen, since the button should be inactive */
1075 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1078 index = get_model_index(model, &iter);
1079 if (!edit_cylinder_dialog(index, &cyl))
1082 set_one_cylinder(&cyl, model, &iter);
1086 static void add_cb(GtkButton *button, GtkTreeView *tree_view)
1088 int index = cylinder_list.max_index;
1090 GtkListStore *model = cylinder_list.model;
1091 GtkTreeSelection *selection;
1094 if (!edit_cylinder_dialog(index, &cyl))
1097 gtk_list_store_append(model, &iter);
1098 set_one_cylinder(&cyl, model, &iter);
1100 selection = gtk_tree_view_get_selection(tree_view);
1101 gtk_tree_selection_select_iter(selection, &iter);
1103 cylinder_list.max_index++;
1104 gtk_widget_set_sensitive(cylinder_list.add, cylinder_list.max_index < MAX_CYLINDERS);
1107 static void del_cb(GtkButton *button, GtkTreeView *tree_view)
1111 GtkListStore *model = cylinder_list.model;
1112 GtkTreeSelection *selection;
1116 selection = gtk_tree_view_get_selection(tree_view);
1118 /* Nothing selected? This shouldn't happen, since the button should be inactive */
1119 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1122 index = get_model_index(model, &iter);
1124 dive = current_dive;
1127 cyl = dive->cylinder + index;
1128 nr = cylinder_list.max_index - index - 1;
1130 gtk_list_store_remove(model, &iter);
1132 cylinder_list.max_index--;
1133 memmove(cyl, cyl+1, nr*sizeof(*cyl));
1134 memset(cyl+nr, 0, sizeof(*cyl));
1136 mark_divelist_changed(TRUE);
1137 flush_divelist(dive);
1139 gtk_widget_set_sensitive(cylinder_list.edit, 0);
1140 gtk_widget_set_sensitive(cylinder_list.del, 0);
1141 gtk_widget_set_sensitive(cylinder_list.add, 1);
1144 static void ws_edit_cb(GtkButton *button, GtkTreeView *tree_view)
1148 GtkListStore *model = weightsystem_list.model;
1149 GtkTreeSelection *selection;
1152 selection = gtk_tree_view_get_selection(tree_view);
1154 /* Nothing selected? This shouldn't happen, since the button should be inactive */
1155 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1158 index = get_model_index(model, &iter);
1159 if (!edit_weightsystem_dialog(index, &ws))
1162 set_one_weightsystem(&ws, model, &iter);
1166 static void ws_add_cb(GtkButton *button, GtkTreeView *tree_view)
1168 int index = weightsystem_list.max_index;
1170 GtkListStore *model = weightsystem_list.model;
1171 GtkTreeSelection *selection;
1174 if (!edit_weightsystem_dialog(index, &ws))
1177 gtk_list_store_append(model, &iter);
1178 set_one_weightsystem(&ws, model, &iter);
1180 selection = gtk_tree_view_get_selection(tree_view);
1181 gtk_tree_selection_select_iter(selection, &iter);
1183 weightsystem_list.max_index++;
1184 gtk_widget_set_sensitive(weightsystem_list.add, weightsystem_list.max_index < MAX_WEIGHTSYSTEMS);
1187 static void ws_del_cb(GtkButton *button, GtkTreeView *tree_view)
1191 GtkListStore *model = weightsystem_list.model;
1192 GtkTreeSelection *selection;
1196 selection = gtk_tree_view_get_selection(tree_view);
1198 /* Nothing selected? This shouldn't happen, since the button should be inactive */
1199 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
1202 index = get_model_index(model, &iter);
1204 dive = current_dive;
1207 ws = dive->weightsystem + index;
1208 nr = weightsystem_list.max_index - index - 1;
1210 gtk_list_store_remove(model, &iter);
1212 weightsystem_list.max_index--;
1213 memmove(ws, ws+1, nr*sizeof(*ws));
1214 memset(ws+nr, 0, sizeof(*ws));
1216 mark_divelist_changed(TRUE);
1217 flush_divelist(dive);
1219 gtk_widget_set_sensitive(weightsystem_list.edit, 0);
1220 gtk_widget_set_sensitive(weightsystem_list.del, 0);
1221 gtk_widget_set_sensitive(weightsystem_list.add, 1);
1224 static GtkListStore *create_tank_size_model(void)
1226 GtkListStore *model;
1228 model = gtk_list_store_new(3,
1229 G_TYPE_STRING, /* Tank name */
1230 G_TYPE_INT, /* Tank size in mliter */
1231 G_TYPE_INT, /* Tank working pressure in mbar */
1234 fill_tank_list(model);
1238 static GtkListStore *create_weightsystem_model(void)
1240 GtkListStore *model;
1242 model = gtk_list_store_new(2,
1243 G_TYPE_STRING, /* Weightsystem description */
1244 G_TYPE_INT, /* Weight in grams */
1247 fill_ws_list(model);
1251 static void size_data_func(GtkTreeViewColumn *col,
1252 GtkCellRenderer *renderer,
1253 GtkTreeModel *model,
1258 double size, pressure;
1261 gtk_tree_model_get(model, iter, CYL_SIZE, &ml, CYL_WORKP, &mbar, -1);
1262 convert_volume_pressure(ml, mbar, &size, &pressure);
1264 snprintf(buffer, sizeof(buffer), "%.1f", size);
1266 strcpy(buffer, "unkn");
1267 g_object_set(renderer, "text", buffer, NULL);
1270 static void weight_data_func(GtkTreeViewColumn *col,
1271 GtkCellRenderer *renderer,
1272 GtkTreeModel *model,
1276 int idx = (long)data;
1277 int grams, decimals;
1281 gtk_tree_model_get(model, iter, idx, &grams, -1);
1282 decimals = convert_weight(grams, &value);
1284 snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
1286 strcpy(buffer, "unkn");
1287 g_object_set(renderer, "text", buffer, NULL);
1290 static void pressure_data_func(GtkTreeViewColumn *col,
1291 GtkCellRenderer *renderer,
1292 GtkTreeModel *model,
1296 int index = (long)data;
1301 gtk_tree_model_get(model, iter, index, &mbar, -1);
1302 decimals = convert_pressure(mbar, &pressure);
1304 snprintf(buffer, sizeof(buffer), "%.*f", decimals, pressure);
1307 g_object_set(renderer, "text", buffer, NULL);
1310 static void percentage_data_func(GtkTreeViewColumn *col,
1311 GtkCellRenderer *renderer,
1312 GtkTreeModel *model,
1316 int index = (long)data;
1320 gtk_tree_model_get(model, iter, index, &permille, -1);
1322 snprintf(buffer, sizeof(buffer), "%.1f%%", permille / 10.0);
1325 g_object_set(renderer, "text", buffer, NULL);
1328 static void selection_cb(GtkTreeSelection *selection, struct equipment_list *list)
1333 selected = gtk_tree_selection_get_selected(selection, NULL, &iter);
1334 gtk_widget_set_sensitive(list->edit, selected);
1335 gtk_widget_set_sensitive(list->del, selected);
1338 static void row_activated_cb(GtkTreeView *tree_view,
1340 GtkTreeViewColumn *column,
1341 GtkTreeModel *model)
1343 edit_cb(NULL, tree_view);
1346 static void ws_row_activated_cb(GtkTreeView *tree_view,
1348 GtkTreeViewColumn *column,
1349 GtkTreeModel *model)
1351 ws_edit_cb(NULL, tree_view);
1354 GtkWidget *cylinder_list_widget(void)
1356 GtkListStore *model = cylinder_list.model;
1357 GtkWidget *tree_view;
1358 GtkTreeSelection *selection;
1360 tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
1361 gtk_widget_set_can_focus(tree_view, FALSE);
1363 g_signal_connect(tree_view, "row-activated", G_CALLBACK(row_activated_cb), model);
1365 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
1366 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
1367 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), &cylinder_list);
1369 g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
1370 "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
1373 tree_view_column(tree_view, CYL_DESC, "Type", NULL, ALIGN_LEFT | UNSORTABLE);
1374 tree_view_column(tree_view, CYL_SIZE, "Size", size_data_func, ALIGN_RIGHT | UNSORTABLE);
1375 tree_view_column(tree_view, CYL_WORKP, "MaxPress", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1376 tree_view_column(tree_view, CYL_STARTP, "Start", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1377 tree_view_column(tree_view, CYL_ENDP, "End", pressure_data_func, ALIGN_RIGHT | UNSORTABLE);
1378 tree_view_column(tree_view, CYL_O2, "O" UTF8_SUBSCRIPT_2 "%", percentage_data_func, ALIGN_RIGHT | UNSORTABLE);
1379 tree_view_column(tree_view, CYL_HE, "He%", percentage_data_func, ALIGN_RIGHT | UNSORTABLE);
1383 GtkWidget *weightsystem_list_widget(void)
1385 GtkListStore *model = weightsystem_list.model;
1386 GtkWidget *tree_view;
1387 GtkTreeSelection *selection;
1389 tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(model));
1390 gtk_widget_set_can_focus(tree_view, FALSE);
1391 g_signal_connect(tree_view, "row-activated", G_CALLBACK(ws_row_activated_cb), model);
1393 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
1394 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_BROWSE);
1395 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), &weightsystem_list);
1397 g_object_set(G_OBJECT(tree_view), "headers-visible", TRUE,
1398 "enable-grid-lines", GTK_TREE_VIEW_GRID_LINES_BOTH,
1401 tree_view_column(tree_view, WS_DESC, "Type", NULL, ALIGN_LEFT | UNSORTABLE);
1402 tree_view_column(tree_view, WS_WEIGHT, "weight",
1403 weight_data_func, ALIGN_RIGHT | UNSORTABLE);
1408 static GtkWidget *cylinder_list_create(void)
1410 GtkListStore *model;
1412 model = gtk_list_store_new(CYL_COLUMNS,
1413 G_TYPE_STRING, /* CYL_DESC: utf8 */
1414 G_TYPE_INT, /* CYL_SIZE: mliter */
1415 G_TYPE_INT, /* CYL_WORKP: mbar */
1416 G_TYPE_INT, /* CYL_STARTP: mbar */
1417 G_TYPE_INT, /* CYL_ENDP: mbar */
1418 G_TYPE_INT, /* CYL_O2: permille */
1419 G_TYPE_INT /* CYL_HE: permille */
1421 cylinder_list.model = model;
1422 return cylinder_list_widget();
1425 static GtkWidget *weightsystem_list_create(void)
1427 GtkListStore *model;
1429 model = gtk_list_store_new(WS_COLUMNS,
1430 G_TYPE_STRING, /* WS_DESC: utf8 */
1431 G_TYPE_INT /* WS_WEIGHT: grams */
1433 weightsystem_list.model = model;
1434 return weightsystem_list_widget();
1437 GtkWidget *equipment_widget(void)
1439 GtkWidget *vbox, *hbox, *frame, *framebox, *tree_view;
1440 GtkWidget *add, *del, *edit;
1442 vbox = gtk_vbox_new(FALSE, 3);
1445 * We create the cylinder size model at startup, since
1446 * we're going to share it across all cylinders and all
1447 * dives. So if you add a new cylinder type in one dive,
1448 * it will show up when you edit the cylinder types for
1451 cylinder_model = create_tank_size_model();
1452 tree_view = cylinder_list_create();
1454 hbox = gtk_hbox_new(FALSE, 3);
1455 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1457 frame = gtk_frame_new("Cylinders");
1458 gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
1460 framebox = gtk_vbox_new(FALSE, 3);
1461 gtk_container_add(GTK_CONTAINER(frame), framebox);
1463 hbox = gtk_hbox_new(FALSE, 3);
1464 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1466 gtk_box_pack_start(GTK_BOX(hbox), tree_view, TRUE, FALSE, 3);
1468 hbox = gtk_hbox_new(TRUE, 3);
1469 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1471 edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1472 add = gtk_button_new_from_stock(GTK_STOCK_ADD);
1473 del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1474 gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
1475 gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
1476 gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
1478 cylinder_list.edit = edit;
1479 cylinder_list.add = add;
1480 cylinder_list.del = del;
1482 g_signal_connect(edit, "clicked", G_CALLBACK(edit_cb), tree_view);
1483 g_signal_connect(add, "clicked", G_CALLBACK(add_cb), tree_view);
1484 g_signal_connect(del, "clicked", G_CALLBACK(del_cb), tree_view);
1486 hbox = gtk_hbox_new(FALSE, 3);
1487 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 3);
1489 weightsystem_model = create_weightsystem_model();
1490 tree_view = weightsystem_list_create();
1492 frame = gtk_frame_new("Weight");
1493 gtk_box_pack_start(GTK_BOX(hbox), frame, TRUE, FALSE, 3);
1495 framebox = gtk_vbox_new(FALSE, 3);
1496 gtk_container_add(GTK_CONTAINER(frame), framebox);
1498 hbox = gtk_hbox_new(FALSE, 3);
1499 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1501 gtk_box_pack_start(GTK_BOX(hbox), tree_view, TRUE, FALSE, 3);
1503 hbox = gtk_hbox_new(TRUE, 3);
1504 gtk_box_pack_start(GTK_BOX(framebox), hbox, TRUE, FALSE, 3);
1506 edit = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1507 add = gtk_button_new_from_stock(GTK_STOCK_ADD);
1508 del = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1509 gtk_box_pack_start(GTK_BOX(hbox), edit, FALSE, FALSE, 0);
1510 gtk_box_pack_start(GTK_BOX(hbox), add, FALSE, FALSE, 0);
1511 gtk_box_pack_start(GTK_BOX(hbox), del, FALSE, FALSE, 0);
1513 weightsystem_list.edit = edit;
1514 weightsystem_list.add = add;
1515 weightsystem_list.del = del;
1517 g_signal_connect(edit, "clicked", G_CALLBACK(ws_edit_cb), tree_view);
1518 g_signal_connect(add, "clicked", G_CALLBACK(ws_add_cb), tree_view);
1519 g_signal_connect(del, "clicked", G_CALLBACK(ws_del_cb), tree_view);