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;
27 GtkTreeStore *model, *listmodel, *treemodel;
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 /* magic numbers that indicate (as negative values) model entries that
58 * are summary entries for a divetrip */
62 static gboolean dump_model_entry(GtkTreeModel *model, GtkTreePath *path,
63 GtkTreeIter *iter, gpointer data)
66 int idx, nr, rating, depth;
68 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_NR, &nr, DIVE_RATING, &rating, DIVE_DEPTH, &depth, DIVE_LOCATION, &location, -1);
69 printf("entry #%d : nr %d rating %d depth %d location %s \n", idx, nr, rating, depth, location);
75 static void dump_model(GtkListStore *store)
77 gtk_tree_model_foreach(GTK_TREE_MODEL(store), dump_model_entry, NULL);
81 #if DEBUG_SELECTION_TRACKING
82 void dump_selection(void)
87 printf("currently selected are %d dives:", amount_selected);
88 for (i = 0; (dive = get_dive(i)) != NULL; i++) {
96 /* when subsurface starts we want to have the last dive selected. So we simply
97 walk to the first leaf (and skip the summary entries - which have negative
99 static void first_leaf(GtkTreeModel *model, GtkTreeIter *iter, int *diveidx)
104 while (*diveidx < 0) {
105 memcpy(&parent, iter, sizeof(parent));
106 tpath = gtk_tree_model_get_path(model, &parent);
107 if (!gtk_tree_model_iter_children(model, iter, &parent))
108 /* we should never have a parent without child */
110 if(!gtk_tree_view_row_expanded(GTK_TREE_VIEW(dive_list.tree_view), tpath))
111 gtk_tree_view_expand_row(GTK_TREE_VIEW(dive_list.tree_view), tpath, FALSE);
112 gtk_tree_model_get(GTK_TREE_MODEL(model), iter, DIVE_INDEX, diveidx, -1);
116 /* make sure that if we expand a summary row that is selected, the children show
117 up as selected, too */
118 void row_expanded_cb(GtkTreeView *tree_view, GtkTreeIter *iter, GtkTreePath *path, gpointer data)
121 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
122 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
124 if (!gtk_tree_model_iter_children(model, &child, iter))
131 gtk_tree_model_get(model, &child, DIVE_INDEX, &idx, -1);
132 dive = get_dive(idx);
135 gtk_tree_selection_select_iter(selection, &child);
137 gtk_tree_selection_unselect_iter(selection, &child);
138 } while (gtk_tree_model_iter_next(model, &child));
141 static int selected_children(GtkTreeModel *model, GtkTreeIter *iter)
145 if (!gtk_tree_model_iter_children(model, &child, iter))
152 gtk_tree_model_get(model, &child, DIVE_INDEX, &idx, -1);
153 dive = get_dive(idx);
157 } while (gtk_tree_model_iter_next(model, &child));
161 /* Make sure that if we collapse a summary row with any selected children, the row
162 shows up as selected too */
163 void row_collapsed_cb(GtkTreeView *tree_view, GtkTreeIter *iter, GtkTreePath *path, gpointer data)
165 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
166 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
168 if (selected_children(model, iter))
169 gtk_tree_selection_select_iter(selection, iter);
172 static GList *selection_changed = NULL;
175 * This is called _before_ the selection is changed, for every single entry;
177 * We simply create a list of all changed entries, and make sure that the
178 * group entries go at the end of the list.
180 gboolean modify_selection_cb(GtkTreeSelection *selection, GtkTreeModel *model,
181 GtkTreePath *path, gboolean was_selected, gpointer userdata)
183 GtkTreeIter iter, *p;
185 if (!gtk_tree_model_get_iter(model, &iter, path))
188 /* Add the group entries to the end */
189 p = gtk_tree_iter_copy(&iter);
190 if (gtk_tree_model_iter_has_child(model, p))
191 selection_changed = g_list_append(selection_changed, p);
193 selection_changed = g_list_prepend(selection_changed, p);
197 static void select_dive(struct dive *dive, int selected)
199 if (dive->selected != selected) {
200 amount_selected += selected ? 1 : -1;
201 dive->selected = selected;
206 * This gets called when a dive group has changed selection.
208 static void select_dive_group(GtkTreeModel *model, GtkTreeSelection *selection, GtkTreeIter *iter, int selected)
213 if (selected == selected_children(model, iter))
216 if (!gtk_tree_model_iter_children(model, &child, iter))
223 gtk_tree_model_get(model, &child, DIVE_INDEX, &idx, -1);
224 if (first && selected)
227 dive = get_dive(idx);
228 if (dive->selected == selected)
231 select_dive(dive, selected);
233 gtk_tree_selection_select_iter(selection, &child);
235 gtk_tree_selection_unselect_iter(selection, &child);
236 } while (gtk_tree_model_iter_next(model, &child));
240 * This gets called _after_ the selections have changed, for each entry that
241 * may have changed. Check if the gtk selection state matches our internal
242 * selection state to verify.
244 * The group entries are at the end, this guarantees that we have handled
245 * all the dives before we handle groups.
247 static void check_selection_cb(GtkTreeIter *iter, GtkTreeSelection *selection)
249 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
251 int idx, gtk_selected;
253 gtk_tree_model_get(model, iter,
256 dive = get_dive(idx);
257 gtk_selected = gtk_tree_selection_iter_is_selected(selection, iter);
259 select_dive_group(model, selection, iter, gtk_selected);
261 select_dive(dive, gtk_selected);
265 gtk_tree_iter_free(iter);
268 /* this is called when gtk thinks that the selection has changed */
269 static void selection_cb(GtkTreeSelection *selection, GtkTreeModel *model)
271 GList *changed = selection_changed;
273 selection_changed = NULL;
274 g_list_foreach(changed, (GFunc) check_selection_cb, selection);
275 g_list_free(changed);
276 #if DEBUG_SELECTION_TRACKING
280 process_selected_dives();
284 const char *star_strings[] = {
293 static void star_data_func(GtkTreeViewColumn *col,
294 GtkCellRenderer *renderer,
302 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_RATING, &nr_stars, -1);
306 if (nr_stars < 0 || nr_stars > 5)
308 snprintf(buffer, sizeof(buffer), "%s", star_strings[nr_stars]);
310 g_object_set(renderer, "text", buffer, NULL);
313 static void date_data_func(GtkTreeViewColumn *col,
314 GtkCellRenderer *renderer,
324 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DATE, &val, DIVE_NR, &nr, -1);
332 snprintf(buffer, sizeof(buffer),
333 "Trip %s, %s %d, %d (%d dive%s)",
334 weekday(tm->tm_wday),
335 monthname(tm->tm_mon),
336 tm->tm_mday, tm->tm_year + 1900,
337 nr, nr > 1 ? "s" : "");
340 snprintf(buffer, sizeof(buffer),
341 "%s, %s %d, %d %02d:%02d",
342 weekday(tm->tm_wday),
343 monthname(tm->tm_mon),
344 tm->tm_mday, tm->tm_year + 1900,
345 tm->tm_hour, tm->tm_min);
347 g_object_set(renderer, "text", buffer, NULL);
350 static void depth_data_func(GtkTreeViewColumn *col,
351 GtkCellRenderer *renderer,
356 int depth, integer, frac, len, idx;
359 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DEPTH, &depth, -1);
364 switch (output_units.length) {
366 /* To tenths of meters */
367 depth = (depth + 49) / 100;
368 integer = depth / 10;
377 integer = mm_to_feet(depth) + 0.5;
383 len = snprintf(buffer, sizeof(buffer), "%d", integer);
385 len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
387 g_object_set(renderer, "text", buffer, NULL);
390 static void duration_data_func(GtkTreeViewColumn *col,
391 GtkCellRenderer *renderer,
400 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DURATION, &sec, -1);
404 snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
406 g_object_set(renderer, "text", buffer, NULL);
409 static void temperature_data_func(GtkTreeViewColumn *col,
410 GtkCellRenderer *renderer,
418 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_TEMPERATURE, &value, -1);
421 if (idx >= 0 && value) {
423 switch (output_units.temperature) {
425 deg = mkelvin_to_C(value);
428 deg = mkelvin_to_F(value);
433 snprintf(buffer, sizeof(buffer), "%.1f", deg);
436 g_object_set(renderer, "text", buffer, NULL);
439 static void nr_data_func(GtkTreeViewColumn *col,
440 GtkCellRenderer *renderer,
448 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_NR, &nr, -1);
452 snprintf(buffer, sizeof(buffer), "%d", nr);
453 g_object_set(renderer, "text", buffer, NULL);
457 * Get "maximal" dive gas for a dive.
459 * - Trimix trumps nitrox (highest He wins, O2 breaks ties)
460 * - Nitrox trumps air (even if hypoxic)
461 * These are the same rules as the inter-dive sorting rules.
463 static void get_dive_gas(struct dive *dive, int *o2_p, int *he_p, int *o2low_p)
466 int maxo2 = -1, maxhe = -1, mino2 = 1000;
468 for (i = 0; i < MAX_CYLINDERS; i++) {
469 cylinder_t *cyl = dive->cylinder + i;
470 struct gasmix *mix = &cyl->gasmix;
471 int o2 = mix->o2.permille;
472 int he = mix->he.permille;
474 if (cylinder_none(cyl))
490 /* All air? Show/sort as "air"/zero */
491 if (!maxhe && maxo2 == AIR_PERMILLE && mino2 == maxo2)
498 static int total_weight(struct dive *dive)
500 int i, total_grams = 0;
503 for (i=0; i< MAX_WEIGHTSYSTEMS; i++)
504 total_grams += dive->weightsystem[i].weight.grams;
508 static void weight_data_func(GtkTreeViewColumn *col,
509 GtkCellRenderer *renderer,
519 gtk_tree_model_get(model, iter, DIVE_INDEX, &indx, -1);
520 dive = get_dive(indx);
521 value = get_weight_units(total_weight(dive), &decimals, NULL);
525 snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
527 g_object_set(renderer, "text", buffer, NULL);
530 static gint nitrox_sort_func(GtkTreeModel *model,
535 int index_a, index_b;
539 int a_o2low, b_o2low;
541 gtk_tree_model_get(model, iter_a, DIVE_INDEX, &index_a, -1);
542 gtk_tree_model_get(model, iter_b, DIVE_INDEX, &index_b, -1);
543 a = get_dive(index_a);
544 b = get_dive(index_b);
545 get_dive_gas(a, &a_o2, &a_he, &a_o2low);
546 get_dive_gas(b, &b_o2, &b_he, &b_o2low);
548 /* Sort by Helium first, O2 second */
551 return a_o2low - b_o2low;
557 #define UTF8_ELLIPSIS "\xE2\x80\xA6"
559 static void nitrox_data_func(GtkTreeViewColumn *col,
560 GtkCellRenderer *renderer,
565 int idx, o2, he, o2low;
569 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
574 dive = get_dive(idx);
575 get_dive_gas(dive, &o2, &he, &o2low);
578 o2low = (o2low + 5) / 10;
581 snprintf(buffer, sizeof(buffer), "%d/%d", o2, he);
584 snprintf(buffer, sizeof(buffer), "%d", o2);
586 snprintf(buffer, sizeof(buffer), "%d" UTF8_ELLIPSIS "%d", o2low, o2);
588 strcpy(buffer, "air");
590 g_object_set(renderer, "text", buffer, NULL);
593 /* Render the SAC data (integer value of "ml / min") */
594 static void sac_data_func(GtkTreeViewColumn *col,
595 GtkCellRenderer *renderer,
605 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_SAC, &value, -1);
607 if (idx < 0 || !value) {
612 sac = value / 1000.0;
613 switch (output_units.volume) {
619 sac = ml_to_cuft(sac * 1000);
622 snprintf(buffer, sizeof(buffer), fmt, sac);
624 g_object_set(renderer, "text", buffer, NULL);
627 /* Render the OTU data (integer value of "OTU") */
628 static void otu_data_func(GtkTreeViewColumn *col,
629 GtkCellRenderer *renderer,
637 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_OTU, &value, -1);
639 if (idx < 0 || !value)
642 snprintf(buffer, sizeof(buffer), "%d", value);
644 g_object_set(renderer, "text", buffer, NULL);
647 /* calculate OTU for a dive */
648 static int calculate_otu(struct dive *dive)
653 for (i = 1; i < dive->samples; i++) {
656 struct sample *sample = dive->sample + i;
657 struct sample *psample = sample - 1;
658 t = sample->time.seconds - psample->time.seconds;
659 int o2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille;
662 po2 = o2 / 1000.0 * (sample->depth.mm + 10000) / 10000.0;
664 otu += pow(po2 - 0.5, 0.83) * t / 30.0;
669 * Return air usage (in liters).
671 static double calculate_airuse(struct dive *dive)
676 for (i = 0; i < MAX_CYLINDERS; i++) {
677 pressure_t start, end;
678 cylinder_t *cyl = dive->cylinder + i;
679 int size = cyl->type.size.mliter;
685 start = cyl->start.mbar ? cyl->start : cyl->sample_start;
686 end = cyl->end.mbar ? cyl->end : cyl->sample_end;
687 kilo_atm = (to_ATM(start) - to_ATM(end)) / 1000.0;
689 /* Liters of air at 1 atm == milliliters at 1k atm*/
690 airuse += kilo_atm * size;
695 static int calculate_sac(struct dive *dive)
697 double airuse, pressure, sac;
700 airuse = calculate_airuse(dive);
703 if (!dive->duration.seconds)
706 /* find and eliminate long surface intervals */
707 duration = dive->duration.seconds;
708 for (i = 0; i < dive->samples; i++) {
709 if (dive->sample[i].depth.mm < 100) { /* less than 10cm */
711 while (end < dive->samples && dive->sample[end].depth.mm < 100)
713 /* we only want the actual surface time during a dive */
714 if (end < dive->samples) {
716 duration -= dive->sample[end].time.seconds -
717 dive->sample[i].time.seconds;
722 /* Mean pressure in atm: 1 atm per 10m */
723 pressure = 1 + (dive->meandepth.mm / 10000.0);
724 sac = airuse / pressure * 60 / duration;
726 /* milliliters per minute.. */
730 void update_cylinder_related_info(struct dive *dive)
733 dive->sac = calculate_sac(dive);
734 dive->otu = calculate_otu(dive);
738 static void get_string(char **str, const char *s)
754 static void get_location(struct dive *dive, char **str)
756 get_string(str, dive->location);
759 static void get_cylinder(struct dive *dive, char **str)
761 get_string(str, dive->cylinder[0].type.description);
764 static void get_suit(struct dive *dive, char **str)
766 get_string(str, dive->suit);
770 * Set up anything that could have changed due to editing
771 * of dive information; we need to do this for both models,
772 * so we simply call set_one_dive again with the non-current model
774 /* forward declaration for recursion */
775 static gboolean set_one_dive(GtkTreeModel *model,
780 static void fill_one_dive(struct dive *dive,
784 char *location, *cylinder, *suit;
785 GtkTreeStore *othermodel;
787 get_cylinder(dive, &cylinder);
788 get_location(dive, &location);
789 get_suit(dive, &suit);
791 gtk_tree_store_set(GTK_TREE_STORE(model), iter,
792 DIVE_NR, dive->number,
793 DIVE_LOCATION, location,
794 DIVE_CYLINDER, cylinder,
795 DIVE_RATING, dive->rating,
798 DIVE_TOTALWEIGHT, total_weight(dive),
806 if (model == GTK_TREE_MODEL(dive_list.treemodel))
807 othermodel = dive_list.listmodel;
809 othermodel = dive_list.treemodel;
810 if (othermodel != dive_list.model)
812 gtk_tree_model_foreach(GTK_TREE_MODEL(othermodel), set_one_dive, dive);
815 static gboolean set_one_dive(GtkTreeModel *model,
823 /* Get the dive number */
824 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
827 dive = get_dive(idx);
830 if (data && dive != data)
833 fill_one_dive(dive, model, iter);
837 void flush_divelist(struct dive *dive)
839 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
841 gtk_tree_model_foreach(model, set_one_dive, dive);
844 void set_divelist_font(const char *font)
846 PangoFontDescription *font_desc = pango_font_description_from_string(font);
847 gtk_widget_modify_font(dive_list.tree_view, font_desc);
848 pango_font_description_free(font_desc);
851 void update_dive_list_units(void)
854 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
856 (void) get_depth_units(0, NULL, &unit);
857 gtk_tree_view_column_set_title(dive_list.depth, unit);
859 (void) get_temp_units(0, &unit);
860 gtk_tree_view_column_set_title(dive_list.temperature, unit);
862 (void) get_weight_units(0, NULL, &unit);
863 gtk_tree_view_column_set_title(dive_list.totalweight, unit);
865 gtk_tree_model_foreach(model, set_one_dive, NULL);
868 void update_dive_list_col_visibility(void)
870 gtk_tree_view_column_set_visible(dive_list.cylinder, visible_cols.cylinder);
871 gtk_tree_view_column_set_visible(dive_list.temperature, visible_cols.temperature);
872 gtk_tree_view_column_set_visible(dive_list.totalweight, visible_cols.totalweight);
873 gtk_tree_view_column_set_visible(dive_list.suit, visible_cols.suit);
874 gtk_tree_view_column_set_visible(dive_list.nitrox, visible_cols.nitrox);
875 gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
876 gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
880 /* random heuristic - not diving in three days implies new dive trip */
881 #define TRIP_THRESHOLD 3600*24*3
882 static int new_group(struct dive *dive, struct dive **last_dive, time_t *tm_date)
887 struct dive *ldive = *last_dive;
888 if (abs(dive->when - ldive->when) < TRIP_THRESHOLD) {
895 struct tm *tm1 = gmtime(&dive->when);
899 *tm_date = mktime(tm1);
904 static void fill_dive_list(void)
907 GtkTreeIter iter, parent_iter;
908 GtkTreeStore *liststore, *treestore;
909 struct dive *last_dive = NULL;
910 struct dive *last_trip_dive = NULL;
911 const char *last_location = NULL;
914 treestore = GTK_TREE_STORE(dive_list.treemodel);
915 liststore = GTK_TREE_STORE(dive_list.listmodel);
919 struct dive *dive = dive_table.dives[i];
921 if (new_group(dive, &last_dive, &dive_date))
923 /* make sure we display the first date of the trip in previous summary */
925 gtk_tree_store_set(treestore, &parent_iter,
927 DIVE_DATE, last_trip_dive->when,
928 DIVE_LOCATION, last_location,
931 gtk_tree_store_append(treestore, &parent_iter, NULL);
932 gtk_tree_store_set(treestore, &parent_iter,
933 DIVE_INDEX, -NEW_TRIP,
940 /* This might be NULL */
941 last_location = dive->location;
944 last_trip_dive = dive;
946 last_location = dive->location;
947 update_cylinder_related_info(dive);
948 gtk_tree_store_append(treestore, &iter, &parent_iter);
949 gtk_tree_store_set(treestore, &iter,
951 DIVE_NR, dive->number,
952 DIVE_DATE, dive->when,
953 DIVE_DEPTH, dive->maxdepth,
954 DIVE_DURATION, dive->duration.seconds,
955 DIVE_LOCATION, dive->location,
956 DIVE_RATING, dive->rating,
957 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
960 gtk_tree_store_append(liststore, &iter, NULL);
961 gtk_tree_store_set(liststore, &iter,
963 DIVE_NR, dive->number,
964 DIVE_DATE, dive->when,
965 DIVE_DEPTH, dive->maxdepth,
966 DIVE_DURATION, dive->duration.seconds,
967 DIVE_LOCATION, dive->location,
968 DIVE_RATING, dive->rating,
969 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
971 DIVE_SUIT, dive->suit,
976 /* make sure we display the first date of the trip in previous summary */
978 gtk_tree_store_set(treestore, &parent_iter,
980 DIVE_DATE, last_trip_dive->when,
981 DIVE_LOCATION, last_location,
984 update_dive_list_units();
985 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
986 GtkTreeSelection *selection;
988 /* select the last dive (and make sure it's an actual dive that is selected) */
989 gtk_tree_model_get(GTK_TREE_MODEL(dive_list.model), &iter, DIVE_INDEX, &selected_dive, -1);
990 first_leaf(GTK_TREE_MODEL(dive_list.model), &iter, &selected_dive);
991 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
992 gtk_tree_selection_select_iter(selection, &iter);
996 void dive_list_update_dives(void)
998 gtk_tree_store_clear(GTK_TREE_STORE(dive_list.treemodel));
999 gtk_tree_store_clear(GTK_TREE_STORE(dive_list.listmodel));
1004 static struct divelist_column {
1011 [DIVE_NR] = { "#", nr_data_func, NULL, ALIGN_RIGHT | UNSORTABLE },
1012 [DIVE_DATE] = { "Date", date_data_func, NULL, ALIGN_LEFT },
1013 [DIVE_RATING] = { UTF8_BLACKSTAR, star_data_func, NULL, ALIGN_LEFT },
1014 [DIVE_DEPTH] = { "ft", depth_data_func, NULL, ALIGN_RIGHT },
1015 [DIVE_DURATION] = { "min", duration_data_func, NULL, ALIGN_RIGHT },
1016 [DIVE_TEMPERATURE] = { UTF8_DEGREE "F", temperature_data_func, NULL, ALIGN_RIGHT, &visible_cols.temperature },
1017 [DIVE_TOTALWEIGHT] = { "lbs", weight_data_func, NULL, ALIGN_RIGHT, &visible_cols.totalweight },
1018 [DIVE_SUIT] = { "Suit", NULL, NULL, ALIGN_LEFT, &visible_cols.suit },
1019 [DIVE_CYLINDER] = { "Cyl", NULL, NULL, 0, &visible_cols.cylinder },
1020 [DIVE_NITROX] = { "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, nitrox_sort_func, 0, &visible_cols.nitrox },
1021 [DIVE_SAC] = { "SAC", sac_data_func, NULL, 0, &visible_cols.sac },
1022 [DIVE_OTU] = { "OTU", otu_data_func, NULL, 0, &visible_cols.otu },
1023 [DIVE_LOCATION] = { "Location", NULL, NULL, ALIGN_LEFT },
1027 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, struct divelist_column *col)
1029 int index = col - &dl_column[0];
1030 const char *title = col->header;
1031 data_func_t data_func = col->data;
1032 sort_func_t sort_func = col->sort;
1033 unsigned int flags = col->flags;
1034 int *visible = col->visible;
1035 GtkWidget *tree_view = dl->tree_view;
1036 GtkTreeStore *treemodel = dl->treemodel;
1037 GtkTreeStore *listmodel = dl->listmodel;
1038 GtkTreeViewColumn *ret;
1040 if (visible && !*visible)
1042 ret = tree_view_column(tree_view, index, title, data_func, flags);
1044 /* the sort functions are needed in the corresponding models */
1045 if (index == DIVE_DATE)
1046 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(treemodel), index, sort_func, NULL, NULL);
1048 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(listmodel), index, sort_func, NULL, NULL);
1054 * This is some crazy crap. The only way to get default focus seems
1055 * to be to grab focus as the widget is being shown the first time.
1057 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
1059 gtk_widget_grab_focus(tree_view);
1063 * Double-clicking on a group entry will expand a collapsed group
1066 static void collapse_expand(GtkTreeView *tree_view, GtkTreePath *path)
1068 if (!gtk_tree_view_row_expanded(tree_view, path))
1069 gtk_tree_view_expand_row(tree_view, path, FALSE);
1071 gtk_tree_view_collapse_row(tree_view, path);
1075 /* Double-click on a dive list */
1076 static void row_activated_cb(GtkTreeView *tree_view,
1078 GtkTreeViewColumn *column,
1084 if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(dive_list.model), &iter, path))
1087 gtk_tree_model_get(GTK_TREE_MODEL(dive_list.model), &iter, DIVE_INDEX, &index, -1);
1088 /* a negative index is special for the "group by date" entries */
1090 collapse_expand(tree_view, path);
1093 edit_dive_info(get_dive(index));
1096 void add_dive_cb(GtkWidget *menuitem, gpointer data)
1100 dive = alloc_dive();
1101 if (add_new_dive(dive)) {
1109 void edit_dive_cb(GtkWidget *menuitem, gpointer data)
1111 edit_multi_dive_info(NULL);
1114 static void expand_all_cb(GtkWidget *menuitem, GtkTreeView *tree_view)
1116 gtk_tree_view_expand_all(tree_view);
1119 static void collapse_all_cb(GtkWidget *menuitem, GtkTreeView *tree_view)
1121 gtk_tree_view_collapse_all(tree_view);
1124 static void popup_divelist_menu(GtkTreeView *tree_view, GtkTreeModel *model, int button)
1126 GtkWidget *menu, *menuitem, *image;
1127 char editlabel[] = "Edit dives";
1129 menu = gtk_menu_new();
1130 menuitem = gtk_image_menu_item_new_with_label("Add dive");
1131 image = gtk_image_new_from_stock(GTK_STOCK_ADD, GTK_ICON_SIZE_MENU);
1132 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuitem), image);
1133 g_signal_connect(menuitem, "activate", G_CALLBACK(add_dive_cb), NULL);
1134 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1135 if (amount_selected) {
1136 if (amount_selected == 1)
1137 editlabel[strlen(editlabel) - 1] = '\0';
1138 menuitem = gtk_menu_item_new_with_label(editlabel);
1139 g_signal_connect(menuitem, "activate", G_CALLBACK(edit_dive_cb), model);
1140 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1142 menuitem = gtk_menu_item_new_with_label("Expand all");
1143 g_signal_connect(menuitem, "activate", G_CALLBACK(expand_all_cb), tree_view);
1144 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1145 menuitem = gtk_menu_item_new_with_label("Collapse all");
1146 g_signal_connect(menuitem, "activate", G_CALLBACK(collapse_all_cb), tree_view);
1147 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1148 gtk_widget_show_all(menu);
1150 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
1151 button, gtk_get_current_event_time());
1154 static void popup_menu_cb(GtkTreeView *tree_view, gpointer userdata)
1156 popup_divelist_menu(tree_view, GTK_TREE_MODEL(dive_list.model), 0);
1159 static gboolean button_press_cb(GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
1161 /* Right-click? Bring up the menu */
1162 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
1163 popup_divelist_menu(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(dive_list.model), 3);
1169 /* we need to have a temporary copy of the selected dives while
1170 switching model as the selection_cb function keeps getting called
1171 when gtk_tree_selection_select_path is called. We also need to
1172 keep copies of the sort order so we can restore that as well after
1173 switching models. */
1174 static gboolean second_call = FALSE;
1175 static GtkSortType sortorder[] = { [0 ... DIVELIST_COLUMNS - 1] = GTK_SORT_DESCENDING, };
1176 static int lastcol = DIVE_DATE;
1178 /* Check if this dive was selected previously and select it again in the new model;
1179 * This is used after we switch models to maintain consistent selections.
1180 * We always return FALSE to iterate through all dives */
1181 static gboolean set_selected(GtkTreeModel *model, GtkTreePath *path,
1182 GtkTreeIter *iter, gpointer data)
1184 GtkTreeSelection *selection = GTK_TREE_SELECTION(data);
1188 gtk_tree_model_get(model, iter,
1193 if (gtk_tree_model_iter_children(model, &child, iter))
1194 gtk_tree_model_get(model, &child, DIVE_INDEX, &idx, -1);
1196 dive = get_dive(idx);
1197 selected = dive && dive->selected;
1199 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(dive_list.tree_view), path);
1200 gtk_tree_selection_select_path(selection, path);
1206 static void update_column_and_order(int colid)
1208 /* Careful: the index into treecolumns is off by one as we don't have a
1209 tree_view column for DIVE_INDEX */
1210 GtkTreeViewColumn **treecolumns = &dive_list.nr;
1212 /* this will trigger a second call into sort_column_change_cb,
1213 so make sure we don't start an infinite recursion... */
1215 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(dive_list.model), colid, sortorder[colid]);
1216 gtk_tree_view_column_set_sort_order(treecolumns[colid - 1], sortorder[colid]);
1217 second_call = FALSE;
1220 /* If the sort column is date (default), show the tree model.
1221 For every other sort column only show the list model.
1222 If the model changed, inform the new model of the chosen sort column and make
1223 sure the same dives are still selected.
1225 The challenge with this function is that once we change the model
1226 we also need to change the sort column again (as it was changed in
1227 the other model) and that causes this function to be called
1228 recursively - so we need to catch that.
1230 static void sort_column_change_cb(GtkTreeSortable *treeview, gpointer data)
1234 GtkTreeStore *currentmodel = dive_list.model;
1239 gtk_tree_sortable_get_sort_column_id(treeview, &colid, &order);
1240 if(colid == lastcol) {
1241 /* we just changed sort order */
1242 sortorder[colid] = order;
1247 if(colid == DIVE_DATE)
1248 dive_list.model = dive_list.treemodel;
1250 dive_list.model = dive_list.listmodel;
1251 if (dive_list.model != currentmodel) {
1252 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
1254 gtk_tree_view_set_model(GTK_TREE_VIEW(dive_list.tree_view), GTK_TREE_MODEL(dive_list.model));
1255 update_column_and_order(colid);
1256 gtk_tree_model_foreach(GTK_TREE_MODEL(dive_list.model), set_selected, selection);
1258 if (order != sortorder[colid]) {
1259 update_column_and_order(colid);
1264 GtkWidget *dive_list_create(void)
1266 GtkTreeSelection *selection;
1268 dive_list.listmodel = gtk_tree_store_new(DIVELIST_COLUMNS,
1269 G_TYPE_INT, /* index */
1270 G_TYPE_INT, /* nr */
1271 G_TYPE_INT, /* Date */
1272 G_TYPE_INT, /* Star rating */
1273 G_TYPE_INT, /* Depth */
1274 G_TYPE_INT, /* Duration */
1275 G_TYPE_INT, /* Temperature */
1276 G_TYPE_INT, /* Total weight */
1277 G_TYPE_STRING, /* Suit */
1278 G_TYPE_STRING, /* Cylinder */
1279 G_TYPE_INT, /* Nitrox */
1280 G_TYPE_INT, /* SAC */
1281 G_TYPE_INT, /* OTU */
1282 G_TYPE_STRING /* Location */
1284 dive_list.treemodel = gtk_tree_store_new(DIVELIST_COLUMNS,
1285 G_TYPE_INT, /* index */
1286 G_TYPE_INT, /* nr */
1287 G_TYPE_INT, /* Date */
1288 G_TYPE_INT, /* Star rating */
1289 G_TYPE_INT, /* Depth */
1290 G_TYPE_INT, /* Duration */
1291 G_TYPE_INT, /* Temperature */
1292 G_TYPE_INT, /* Total weight */
1293 G_TYPE_STRING, /* Suit */
1294 G_TYPE_STRING, /* Cylinder */
1295 G_TYPE_INT, /* Nitrox */
1296 G_TYPE_INT, /* SAC */
1297 G_TYPE_INT, /* OTU */
1298 G_TYPE_STRING /* Location */
1300 dive_list.model = dive_list.treemodel;
1301 dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
1302 set_divelist_font(divelist_font);
1304 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
1306 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
1307 gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
1309 dive_list.nr = divelist_column(&dive_list, dl_column + DIVE_NR);
1310 dive_list.date = divelist_column(&dive_list, dl_column + DIVE_DATE);
1311 dive_list.stars = divelist_column(&dive_list, dl_column + DIVE_RATING);
1312 dive_list.depth = divelist_column(&dive_list, dl_column + DIVE_DEPTH);
1313 dive_list.duration = divelist_column(&dive_list, dl_column + DIVE_DURATION);
1314 dive_list.temperature = divelist_column(&dive_list, dl_column + DIVE_TEMPERATURE);
1315 dive_list.totalweight = divelist_column(&dive_list, dl_column + DIVE_TOTALWEIGHT);
1316 dive_list.suit = divelist_column(&dive_list, dl_column + DIVE_SUIT);
1317 dive_list.cylinder = divelist_column(&dive_list, dl_column + DIVE_CYLINDER);
1318 dive_list.nitrox = divelist_column(&dive_list, dl_column + DIVE_NITROX);
1319 dive_list.sac = divelist_column(&dive_list, dl_column + DIVE_SAC);
1320 dive_list.otu = divelist_column(&dive_list, dl_column + DIVE_OTU);
1321 dive_list.location = divelist_column(&dive_list, dl_column + DIVE_LOCATION);
1325 g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
1326 "search-column", DIVE_LOCATION,
1330 g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
1331 g_signal_connect(dive_list.tree_view, "row-activated", G_CALLBACK(row_activated_cb), NULL);
1332 g_signal_connect(dive_list.tree_view, "row-expanded", G_CALLBACK(row_expanded_cb), NULL);
1333 g_signal_connect(dive_list.tree_view, "row-collapsed", G_CALLBACK(row_collapsed_cb), NULL);
1334 g_signal_connect(dive_list.tree_view, "button-press-event", G_CALLBACK(button_press_cb), NULL);
1335 g_signal_connect(dive_list.tree_view, "popup-menu", G_CALLBACK(popup_menu_cb), NULL);
1336 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), dive_list.model);
1337 g_signal_connect(dive_list.listmodel, "sort-column-changed", G_CALLBACK(sort_column_change_cb), NULL);
1338 g_signal_connect(dive_list.treemodel, "sort-column-changed", G_CALLBACK(sort_column_change_cb), NULL);
1340 gtk_tree_selection_set_select_function(selection, modify_selection_cb, NULL, NULL);
1342 dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
1343 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
1344 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1345 gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
1347 dive_list.changed = 0;
1349 return dive_list.container_widget;
1352 void mark_divelist_changed(int changed)
1354 dive_list.changed = changed;
1357 int unsaved_changes()
1359 return dive_list.changed;