2 /* this creates the UI for the dive list -
3 * controlled through the following interfaces:
5 * void flush_divelist(struct dive *dive)
6 * GtkWidget dive_list_create(void)
7 * void dive_list_update_dives(void)
8 * void update_dive_list_units(void)
9 * void set_divelist_font(const char *font)
10 * void mark_divelist_changed(int changed)
11 * int unsaved_changes()
22 #include "display-gtk.h"
26 GtkWidget *container_widget;
28 GtkTreeViewColumn *nr, *date, *stars, *depth, *duration, *location;
29 GtkTreeViewColumn *temperature, *cylinder, *totalweight, *suit, *nitrox, *sac, *otu;
33 static struct DiveList dive_list;
36 * The dive list has the dive data in both string format (for showing)
37 * and in "raw" format (for sorting purposes)
41 DIVE_NR, /* int: dive->nr */
42 DIVE_DATE, /* time_t: dive->when */
43 DIVE_RATING, /* int: 0-5 stars */
44 DIVE_DEPTH, /* int: dive->maxdepth in mm */
45 DIVE_DURATION, /* int: in seconds */
46 DIVE_TEMPERATURE, /* int: in mkelvin */
47 DIVE_TOTALWEIGHT, /* int: in grams */
48 DIVE_SUIT, /* "wet, 3mm" */
50 DIVE_NITROX, /* int: dummy */
51 DIVE_SAC, /* int: in ml/min */
52 DIVE_OTU, /* int: in OTUs */
53 DIVE_LOCATION, /* "2nd Cathedral, Lanai" */
57 static GList *selected_dives;
59 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
65 int nr_selected = gtk_tree_selection_count_selected_rows(selection);
68 g_list_foreach (selected_dives, (GFunc) gtk_tree_path_free, NULL);
69 g_list_free (selected_dives);
71 selected_dives = gtk_tree_selection_get_selected_rows(selection, NULL);
73 switch (nr_selected) {
74 case 0: /* keep showing the last selected dive */
77 /* just pick that dive as selected */
79 path = g_list_nth_data(selected_dives, 0);
80 if (gtk_tree_model_get_iter(model, &iter, path)) {
81 gtk_tree_model_get_value(model, &iter, DIVE_INDEX, &value);
82 selected_dive = g_value_get_int(&value);
86 default: /* multiple selections - what now? At this point I
87 * don't want to change the selected dive unless
88 * there is exactly one dive selected; not sure this
89 * is the most intuitive solution.
90 * I do however want to keep around which dives have
92 amount_selected = g_list_length(selected_dives);
93 process_selected_dives(selected_dives, model);
99 const char *star_strings[] = {
108 static void star_data_func(GtkTreeViewColumn *col,
109 GtkCellRenderer *renderer,
117 gtk_tree_model_get(model, iter, DIVE_RATING, &nr_stars, -1);
118 if (nr_stars < 0 || nr_stars > 5)
120 snprintf(buffer, sizeof(buffer), "%s", star_strings[nr_stars]);
121 g_object_set(renderer, "text", buffer, NULL);
124 static void date_data_func(GtkTreeViewColumn *col,
125 GtkCellRenderer *renderer,
135 gtk_tree_model_get(model, iter, DIVE_DATE, &val, -1);
141 snprintf(buffer, sizeof(buffer),
142 "%s, %s %d, %d %02d:%02d",
143 weekday(tm->tm_wday),
144 monthname(tm->tm_mon),
145 tm->tm_mday, tm->tm_year + 1900,
146 tm->tm_hour, tm->tm_min);
147 g_object_set(renderer, "text", buffer, NULL);
150 static void depth_data_func(GtkTreeViewColumn *col,
151 GtkCellRenderer *renderer,
156 int depth, integer, frac, len;
159 gtk_tree_model_get(model, iter, DIVE_DEPTH, &depth, -1);
161 switch (output_units.length) {
163 /* To tenths of meters */
164 depth = (depth + 49) / 100;
165 integer = depth / 10;
174 integer = mm_to_feet(depth) + 0.5;
180 len = snprintf(buffer, sizeof(buffer), "%d", integer);
182 len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
184 g_object_set(renderer, "text", buffer, NULL);
187 static void duration_data_func(GtkTreeViewColumn *col,
188 GtkCellRenderer *renderer,
196 gtk_tree_model_get(model, iter, DIVE_DURATION, &sec, -1);
197 snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
199 g_object_set(renderer, "text", buffer, NULL);
202 static void temperature_data_func(GtkTreeViewColumn *col,
203 GtkCellRenderer *renderer,
211 gtk_tree_model_get(model, iter, DIVE_TEMPERATURE, &value, -1);
216 switch (output_units.temperature) {
218 deg = mkelvin_to_C(value);
221 deg = mkelvin_to_F(value);
226 snprintf(buffer, sizeof(buffer), "%.1f", deg);
229 g_object_set(renderer, "text", buffer, NULL);
233 * Get "maximal" dive gas for a dive.
235 * - Trimix trumps nitrox (highest He wins, O2 breaks ties)
236 * - Nitrox trumps air (even if hypoxic)
237 * These are the same rules as the inter-dive sorting rules.
239 static void get_dive_gas(struct dive *dive, int *o2_p, int *he_p, int *o2low_p)
242 int maxo2 = -1, maxhe = -1, mino2 = 1000;
244 for (i = 0; i < MAX_CYLINDERS; i++) {
245 cylinder_t *cyl = dive->cylinder + i;
246 struct gasmix *mix = &cyl->gasmix;
247 int o2 = mix->o2.permille;
248 int he = mix->he.permille;
250 if (cylinder_none(cyl))
266 /* All air? Show/sort as "air"/zero */
267 if (!maxhe && maxo2 == AIR_PERMILLE && mino2 == maxo2)
274 static int total_weight(struct dive *dive)
276 int i, total_grams = 0;
279 for (i=0; i< MAX_WEIGHTSYSTEMS; i++)
280 total_grams += dive->weightsystem[i].weight.grams;
284 static void weight_data_func(GtkTreeViewColumn *col,
285 GtkCellRenderer *renderer,
295 gtk_tree_model_get(model, iter, DIVE_INDEX, &indx, -1);
296 dive = get_dive(indx);
297 value = get_weight_units(total_weight(dive), &decimals, NULL);
301 snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
303 g_object_set(renderer, "text", buffer, NULL);
306 static gint nitrox_sort_func(GtkTreeModel *model,
311 int index_a, index_b;
315 int a_o2low, b_o2low;
317 gtk_tree_model_get(model, iter_a, DIVE_INDEX, &index_a, -1);
318 gtk_tree_model_get(model, iter_b, DIVE_INDEX, &index_b, -1);
319 a = get_dive(index_a);
320 b = get_dive(index_b);
321 get_dive_gas(a, &a_o2, &a_he, &a_o2low);
322 get_dive_gas(b, &b_o2, &b_he, &b_o2low);
324 /* Sort by Helium first, O2 second */
327 return a_o2low - b_o2low;
333 #define UTF8_ELLIPSIS "\xE2\x80\xA6"
335 static void nitrox_data_func(GtkTreeViewColumn *col,
336 GtkCellRenderer *renderer,
341 int index, o2, he, o2low;
345 gtk_tree_model_get(model, iter, DIVE_INDEX, &index, -1);
346 dive = get_dive(index);
347 get_dive_gas(dive, &o2, &he, &o2low);
350 o2low = (o2low + 5) / 10;
353 snprintf(buffer, sizeof(buffer), "%d/%d", o2, he);
356 snprintf(buffer, sizeof(buffer), "%d", o2);
358 snprintf(buffer, sizeof(buffer), "%d" UTF8_ELLIPSIS "%d", o2low, o2);
360 strcpy(buffer, "air");
362 g_object_set(renderer, "text", buffer, NULL);
365 /* Render the SAC data (integer value of "ml / min") */
366 static void sac_data_func(GtkTreeViewColumn *col,
367 GtkCellRenderer *renderer,
377 gtk_tree_model_get(model, iter, DIVE_SAC, &value, -1);
380 g_object_set(renderer, "text", "", NULL);
384 sac = value / 1000.0;
385 switch (output_units.volume) {
391 sac = ml_to_cuft(sac * 1000);
394 snprintf(buffer, sizeof(buffer), fmt, sac);
396 g_object_set(renderer, "text", buffer, NULL);
399 /* Render the OTU data (integer value of "OTU") */
400 static void otu_data_func(GtkTreeViewColumn *col,
401 GtkCellRenderer *renderer,
409 gtk_tree_model_get(model, iter, DIVE_OTU, &value, -1);
412 g_object_set(renderer, "text", "", NULL);
416 snprintf(buffer, sizeof(buffer), "%d", value);
418 g_object_set(renderer, "text", buffer, NULL);
421 /* calculate OTU for a dive */
422 static int calculate_otu(struct dive *dive)
427 for (i = 1; i < dive->samples; i++) {
430 struct sample *sample = dive->sample + i;
431 struct sample *psample = sample - 1;
432 t = sample->time.seconds - psample->time.seconds;
433 int o2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille;
436 po2 = o2 / 1000.0 * (sample->depth.mm + 10000) / 10000.0;
438 otu += pow(po2 - 0.5, 0.83) * t / 30.0;
443 * Return air usage (in liters).
445 static double calculate_airuse(struct dive *dive)
450 for (i = 0; i < MAX_CYLINDERS; i++) {
451 pressure_t start, end;
452 cylinder_t *cyl = dive->cylinder + i;
453 int size = cyl->type.size.mliter;
459 start = cyl->start.mbar ? cyl->start : cyl->sample_start;
460 end = cyl->end.mbar ? cyl->end : cyl->sample_end;
461 kilo_atm = (to_ATM(start) - to_ATM(end)) / 1000.0;
463 /* Liters of air at 1 atm == milliliters at 1k atm*/
464 airuse += kilo_atm * size;
469 static int calculate_sac(struct dive *dive)
471 double airuse, pressure, sac;
474 airuse = calculate_airuse(dive);
477 if (!dive->duration.seconds)
480 /* find and eliminate long surface intervals */
481 duration = dive->duration.seconds;
482 for (i = 0; i < dive->samples; i++) {
483 if (dive->sample[i].depth.mm < 100) { /* less than 10cm */
485 while (end < dive->samples && dive->sample[end].depth.mm < 100)
487 /* we only want the actual surface time during a dive */
488 if (end < dive->samples) {
490 duration -= dive->sample[end].time.seconds -
491 dive->sample[i].time.seconds;
496 /* Mean pressure in atm: 1 atm per 10m */
497 pressure = 1 + (dive->meandepth.mm / 10000.0);
498 sac = airuse / pressure * 60 / duration;
500 /* milliliters per minute.. */
504 void update_cylinder_related_info(struct dive *dive)
507 dive->sac = calculate_sac(dive);
508 dive->otu = calculate_otu(dive);
512 static void get_string(char **str, const char *s)
528 static void get_location(struct dive *dive, char **str)
530 get_string(str, dive->location);
533 static void get_cylinder(struct dive *dive, char **str)
535 get_string(str, dive->cylinder[0].type.description);
538 static void get_suit(struct dive *dive, char **str)
540 get_string(str, dive->suit);
544 * Set up anything that could have changed due to editing
545 * of dive information
547 static void fill_one_dive(struct dive *dive,
551 char *location, *cylinder, *suit;
553 get_cylinder(dive, &cylinder);
554 get_location(dive, &location);
555 get_suit(dive, &suit);
557 gtk_list_store_set(GTK_LIST_STORE(model), iter,
558 DIVE_NR, dive->number,
559 DIVE_LOCATION, location,
560 DIVE_CYLINDER, cylinder,
561 DIVE_RATING, dive->rating,
564 DIVE_TOTALWEIGHT, total_weight(dive),
568 /* this will create a merge conflict with the memory leak patches */
572 static gboolean set_one_dive(GtkTreeModel *model,
577 GValue value = {0, };
580 /* Get the dive number */
581 gtk_tree_model_get_value(model, iter, DIVE_INDEX, &value);
582 dive = get_dive(g_value_get_int(&value));
585 if (data && dive != data)
588 fill_one_dive(dive, model, iter);
592 void flush_divelist(struct dive *dive)
594 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
596 gtk_tree_model_foreach(model, set_one_dive, dive);
599 void set_divelist_font(const char *font)
601 PangoFontDescription *font_desc = pango_font_description_from_string(font);
602 gtk_widget_modify_font(dive_list.tree_view, font_desc);
603 pango_font_description_free(font_desc);
606 void update_dive_list_units(void)
609 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
611 (void) get_depth_units(0, NULL, &unit);
612 gtk_tree_view_column_set_title(dive_list.depth, unit);
614 (void) get_temp_units(0, &unit);
615 gtk_tree_view_column_set_title(dive_list.temperature, unit);
617 (void) get_weight_units(0, NULL, &unit);
618 gtk_tree_view_column_set_title(dive_list.totalweight, unit);
620 gtk_tree_model_foreach(model, set_one_dive, NULL);
623 void update_dive_list_col_visibility(void)
625 gtk_tree_view_column_set_visible(dive_list.cylinder, visible_cols.cylinder);
626 gtk_tree_view_column_set_visible(dive_list.temperature, visible_cols.temperature);
627 gtk_tree_view_column_set_visible(dive_list.totalweight, visible_cols.totalweight);
628 gtk_tree_view_column_set_visible(dive_list.suit, visible_cols.suit);
629 gtk_tree_view_column_set_visible(dive_list.nitrox, visible_cols.nitrox);
630 gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
631 gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
635 static void fill_dive_list(void)
641 store = GTK_LIST_STORE(dive_list.model);
645 struct dive *dive = dive_table.dives[i];
647 update_cylinder_related_info(dive);
648 gtk_list_store_append(store, &iter);
649 gtk_list_store_set(store, &iter,
651 DIVE_NR, dive->number,
652 DIVE_DATE, dive->when,
653 DIVE_DEPTH, dive->maxdepth,
654 DIVE_DURATION, dive->duration.seconds,
655 DIVE_LOCATION, "location",
656 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
658 DIVE_SUIT, dive->suit,
663 update_dive_list_units();
664 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
665 GtkTreeSelection *selection;
666 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
667 gtk_tree_selection_select_iter(selection, &iter);
671 void dive_list_update_dives(void)
673 gtk_list_store_clear(GTK_LIST_STORE(dive_list.model));
678 static struct divelist_column {
685 [DIVE_NR] = { "#", NULL, NULL, ALIGN_RIGHT | UNSORTABLE },
686 [DIVE_DATE] = { "Date", date_data_func, NULL, ALIGN_LEFT },
687 [DIVE_RATING] = { UTF8_BLACKSTAR, star_data_func, NULL, ALIGN_LEFT },
688 [DIVE_DEPTH] = { "ft", depth_data_func, NULL, ALIGN_RIGHT },
689 [DIVE_DURATION] = { "min", duration_data_func, NULL, ALIGN_RIGHT },
690 [DIVE_TEMPERATURE] = { UTF8_DEGREE "F", temperature_data_func, NULL, ALIGN_RIGHT, &visible_cols.temperature },
691 [DIVE_TOTALWEIGHT] = { "lbs", weight_data_func, NULL, ALIGN_RIGHT, &visible_cols.totalweight },
692 [DIVE_SUIT] = { "Suit", NULL, NULL, ALIGN_LEFT, &visible_cols.suit },
693 [DIVE_CYLINDER] = { "Cyl", NULL, NULL, 0, &visible_cols.cylinder },
694 [DIVE_NITROX] = { "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, nitrox_sort_func, 0, &visible_cols.nitrox },
695 [DIVE_SAC] = { "SAC", sac_data_func, NULL, 0, &visible_cols.sac },
696 [DIVE_OTU] = { "OTU", otu_data_func, NULL, 0, &visible_cols.otu },
697 [DIVE_LOCATION] = { "Location", NULL, NULL, ALIGN_LEFT },
701 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, struct divelist_column *col)
703 int index = col - &dl_column[0];
704 const char *title = col->header;
705 data_func_t data_func = col->data;
706 sort_func_t sort_func = col->sort;
707 unsigned int flags = col->flags;
708 int *visible = col->visible;
709 GtkWidget *tree_view = dl->tree_view;
710 GtkListStore *model = dl->model;
711 GtkTreeViewColumn *ret;
713 if (visible && !*visible)
715 ret = tree_view_column(tree_view, index, title, data_func, flags);
717 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(model), index, sort_func, NULL, NULL);
722 * This is some crazy crap. The only way to get default focus seems
723 * to be to grab focus as the widget is being shown the first time.
725 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
727 gtk_widget_grab_focus(tree_view);
730 static void row_activated_cb(GtkTreeView *tree_view,
732 GtkTreeViewColumn *column,
738 if (!gtk_tree_model_get_iter(model, &iter, path))
740 gtk_tree_model_get(model, &iter, DIVE_INDEX, &index, -1);
741 edit_dive_info(get_dive(index));
744 void add_dive_cb(GtkWidget *menuitem, gpointer data)
749 if (add_new_dive(dive)) {
757 static void popup_divelist_menu(GtkTreeView *tree_view, GtkTreeModel *model, int button)
759 GtkWidget *menu, *menuitem;
761 menu = gtk_menu_new();
762 menuitem = gtk_menu_item_new_with_label("Add dive");
763 g_signal_connect(menuitem, "activate", G_CALLBACK(add_dive_cb), model);
764 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
765 gtk_widget_show_all(menu);
767 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
768 button, gtk_get_current_event_time());
771 static void popup_menu_cb(GtkTreeView *tree_view,
774 popup_divelist_menu(tree_view, model, 0);
777 static gboolean button_press_cb(GtkWidget *treeview, GdkEventButton *event, GtkTreeModel *model)
779 /* Right-click? Bring up the menu */
780 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
781 popup_divelist_menu(GTK_TREE_VIEW(treeview), model, 3);
787 GtkWidget *dive_list_create(void)
789 GtkTreeSelection *selection;
791 dive_list.model = gtk_list_store_new(DIVELIST_COLUMNS,
792 G_TYPE_INT, /* index */
794 G_TYPE_INT, /* Date */
795 G_TYPE_INT, /* Star rating */
796 G_TYPE_INT, /* Depth */
797 G_TYPE_INT, /* Duration */
798 G_TYPE_INT, /* Temperature */
799 G_TYPE_INT, /* Total weight */
800 G_TYPE_STRING, /* Suit */
801 G_TYPE_STRING, /* Cylinder */
802 G_TYPE_INT, /* Nitrox */
803 G_TYPE_INT, /* SAC */
804 G_TYPE_INT, /* OTU */
805 G_TYPE_STRING /* Location */
807 dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
808 set_divelist_font(divelist_font);
810 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
812 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
813 gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
815 dive_list.nr = divelist_column(&dive_list, dl_column + DIVE_NR);
816 dive_list.date = divelist_column(&dive_list, dl_column + DIVE_DATE);
817 dive_list.stars = divelist_column(&dive_list, dl_column + DIVE_RATING);
818 dive_list.depth = divelist_column(&dive_list, dl_column + DIVE_DEPTH);
819 dive_list.duration = divelist_column(&dive_list, dl_column + DIVE_DURATION);
820 dive_list.temperature = divelist_column(&dive_list, dl_column + DIVE_TEMPERATURE);
821 dive_list.totalweight = divelist_column(&dive_list, dl_column + DIVE_TOTALWEIGHT);
822 dive_list.suit = divelist_column(&dive_list, dl_column + DIVE_SUIT);
823 dive_list.cylinder = divelist_column(&dive_list, dl_column + DIVE_CYLINDER);
824 dive_list.nitrox = divelist_column(&dive_list, dl_column + DIVE_NITROX);
825 dive_list.sac = divelist_column(&dive_list, dl_column + DIVE_SAC);
826 dive_list.otu = divelist_column(&dive_list, dl_column + DIVE_OTU);
827 dive_list.location = divelist_column(&dive_list, dl_column + DIVE_LOCATION);
831 g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
832 "search-column", DIVE_LOCATION,
836 g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
837 g_signal_connect(dive_list.tree_view, "row-activated", G_CALLBACK(row_activated_cb), dive_list.model);
838 g_signal_connect(dive_list.tree_view, "button-press-event", G_CALLBACK(button_press_cb), dive_list.model);
839 g_signal_connect(dive_list.tree_view, "popup-menu", G_CALLBACK(popup_menu_cb), dive_list.model);
840 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
842 dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
843 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
844 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
845 gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
847 dive_list.changed = 0;
849 return dive_list.container_widget;
852 void mark_divelist_changed(int changed)
854 dive_list.changed = changed;
857 int unsaved_changes()
859 return dive_list.changed;