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 static GList *selected_dives;
82 static int st_size = 0;
84 gboolean is_in_st(int idx, int *atpos)
88 for (i = 0; i < amount_selected; i++)
89 if (selectiontracker[i] == idx) {
97 #if DEBUG_SELECTION_TRACKING
98 void dump_selection(void)
102 printf("currently selected are ");
103 for (i = 0; i < amount_selected; i++)
104 printf("%d ", selectiontracker[i]);
109 void track_select(int idx)
114 #if DEBUG_SELECTION_TRACKING
115 printf("add %d to selection of %d entries\n", idx, amount_selected);
117 if (is_in_st(idx, NULL))
119 if (amount_selected >= st_size) {
120 selectiontracker = realloc(selectiontracker, dive_table.nr * sizeof(int));
121 st_size = dive_table.nr;
123 selectiontracker[amount_selected] = idx;
125 if (amount_selected == 1)
127 #if DEBUG_SELECTION_TRACKING
128 printf("increased amount_selected to %d\n", amount_selected);
133 void track_unselect(int idx)
138 #if DEBUG_SELECTION_TRACKING
139 printf("remove %d from selection of %d entries\n", idx, amount_selected);
143 if (! is_in_st(idx, &atpos))
145 memmove(selectiontracker + atpos,
146 selectiontracker + atpos + 1,
147 (amount_selected - atpos - 1) * sizeof(int));
149 #if DEBUG_SELECTION_TRACKING
150 printf("removed %d at pos %d and decreased amount_selected to %d\n", idx, atpos, amount_selected);
155 void clear_tracker(void)
160 /* when subsurface starts we want to have the last dive selected. So we simply
161 walk to the first leaf (and skip the summary entries - which have negative
163 static void first_leaf(GtkTreeModel *model, GtkTreeIter *iter, int *diveidx)
168 while (*diveidx < 0) {
169 memcpy(&parent, iter, sizeof(parent));
170 tpath = gtk_tree_model_get_path(model, &parent);
171 if (!gtk_tree_model_iter_children(model, iter, &parent))
172 /* we should never have a parent without child */
174 if(!gtk_tree_view_row_expanded(GTK_TREE_VIEW(dive_list.tree_view), tpath))
175 gtk_tree_view_expand_row(GTK_TREE_VIEW(dive_list.tree_view), tpath, FALSE);
176 gtk_tree_model_get(GTK_TREE_MODEL(model), iter, DIVE_INDEX, diveidx, -1);
180 /* if we click on a summary dive, we actually want to select / unselect
181 all the dives "below" it */
182 static void select_children(GtkTreeModel *model, GtkTreeSelection * selection,
183 GtkTreeIter *iter, gboolean was_selected)
186 gboolean expanded = FALSE;
190 memcpy(&parent, iter, sizeof(parent));
192 tpath = gtk_tree_model_get_path(model, &parent);
193 expanded = gtk_tree_view_row_expanded(GTK_TREE_VIEW(dive_list.tree_view), tpath);
194 nr_children = gtk_tree_model_iter_n_children(model, &parent);
195 for (i = 0; i < nr_children; i++) {
196 gtk_tree_model_iter_nth_child(model, iter, &parent, i);
198 /* if the parent is expanded, just (un)select the children and we'll
199 track their selection status in the callback
200 otherwise just change the selection status directly without
204 gtk_tree_selection_unselect_iter(selection, iter);
206 gtk_tree_selection_select_iter(selection, iter);
209 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
218 /* make sure that if we expand a summary row that is selected, the children show
219 up as selected, too */
220 void row_expanded_cb(GtkTreeView *tree_view, GtkTreeIter *iter, GtkTreePath *path, gpointer data)
222 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
224 if (gtk_tree_selection_path_is_selected(selection, path))
225 select_children(GTK_TREE_MODEL(dive_list.model), selection, iter, FALSE);
228 /* this is called _before_ the selection is changed, for every single entry;
229 * we simply have it call down the tree to make sure that summary items toggle
231 gboolean modify_selection_cb(GtkTreeSelection *selection, GtkTreeModel *model,
232 GtkTreePath *path, gboolean was_selected, gpointer userdata)
237 /* if gtk thinks nothing is selected we should clear out our
238 tracker as well - otherwise hidden selected rows can stay
239 "stuck". The down side is that we now have a different bug:
240 If you select a dive, collapse the dive trip and ctrl-click
241 another dive trip, the initial dive is no longer selected.
242 Just don't do that, ok? */
243 if (gtk_tree_selection_count_selected_rows(selection) == 0)
246 if (gtk_tree_model_get_iter(model, &iter, path)) {
247 gtk_tree_model_get(model, &iter, DIVE_INDEX, &dive_idx, -1);
248 /* turns out we need to move the selectiontracker here */
250 #if DEBUG_SELECTION_TRACKING
251 printf("modify_selection_cb with idx %d (according to gtk was %sselected)\n",
252 dive_idx, was_selected ? "" : "un");
256 track_unselect(dive_idx);
258 track_select(dive_idx);
260 select_children(model, selection, &iter, was_selected);
263 /* allow this selection to proceed */
267 /* this is called when gtk thinks that the selection has changed */
268 static void selection_cb(GtkTreeSelection *selection, gpointer userdata)
270 process_selected_dives(selected_dives, selectiontracker, GTK_TREE_MODEL(dive_list.model));
274 const char *star_strings[] = {
283 static void star_data_func(GtkTreeViewColumn *col,
284 GtkCellRenderer *renderer,
292 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_RATING, &nr_stars, -1);
296 if (nr_stars < 0 || nr_stars > 5)
298 snprintf(buffer, sizeof(buffer), "%s", star_strings[nr_stars]);
300 g_object_set(renderer, "text", buffer, NULL);
303 static void date_data_func(GtkTreeViewColumn *col,
304 GtkCellRenderer *renderer,
314 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DATE, &val, -1);
322 snprintf(buffer, sizeof(buffer),
323 "Trip %s, %s %d, %d",
324 weekday(tm->tm_wday),
325 monthname(tm->tm_mon),
326 tm->tm_mday, tm->tm_year + 1900);
329 snprintf(buffer, sizeof(buffer),
330 "%s, %s %d, %d %02d:%02d",
331 weekday(tm->tm_wday),
332 monthname(tm->tm_mon),
333 tm->tm_mday, tm->tm_year + 1900,
334 tm->tm_hour, tm->tm_min);
336 g_object_set(renderer, "text", buffer, NULL);
339 static void depth_data_func(GtkTreeViewColumn *col,
340 GtkCellRenderer *renderer,
345 int depth, integer, frac, len, idx;
348 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DEPTH, &depth, -1);
353 switch (output_units.length) {
355 /* To tenths of meters */
356 depth = (depth + 49) / 100;
357 integer = depth / 10;
366 integer = mm_to_feet(depth) + 0.5;
372 len = snprintf(buffer, sizeof(buffer), "%d", integer);
374 len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
376 g_object_set(renderer, "text", buffer, NULL);
379 static void duration_data_func(GtkTreeViewColumn *col,
380 GtkCellRenderer *renderer,
389 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DURATION, &sec, -1);
393 snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
395 g_object_set(renderer, "text", buffer, NULL);
398 static void temperature_data_func(GtkTreeViewColumn *col,
399 GtkCellRenderer *renderer,
407 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_TEMPERATURE, &value, -1);
410 if (idx >= 0 && value) {
412 switch (output_units.temperature) {
414 deg = mkelvin_to_C(value);
417 deg = mkelvin_to_F(value);
422 snprintf(buffer, sizeof(buffer), "%.1f", deg);
425 g_object_set(renderer, "text", buffer, NULL);
428 static void nr_data_func(GtkTreeViewColumn *col,
429 GtkCellRenderer *renderer,
437 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_NR, &nr, -1);
441 snprintf(buffer, sizeof(buffer), "%d", nr);
442 g_object_set(renderer, "text", buffer, NULL);
446 * Get "maximal" dive gas for a dive.
448 * - Trimix trumps nitrox (highest He wins, O2 breaks ties)
449 * - Nitrox trumps air (even if hypoxic)
450 * These are the same rules as the inter-dive sorting rules.
452 static void get_dive_gas(struct dive *dive, int *o2_p, int *he_p, int *o2low_p)
455 int maxo2 = -1, maxhe = -1, mino2 = 1000;
457 for (i = 0; i < MAX_CYLINDERS; i++) {
458 cylinder_t *cyl = dive->cylinder + i;
459 struct gasmix *mix = &cyl->gasmix;
460 int o2 = mix->o2.permille;
461 int he = mix->he.permille;
463 if (cylinder_none(cyl))
479 /* All air? Show/sort as "air"/zero */
480 if (!maxhe && maxo2 == AIR_PERMILLE && mino2 == maxo2)
487 static int total_weight(struct dive *dive)
489 int i, total_grams = 0;
492 for (i=0; i< MAX_WEIGHTSYSTEMS; i++)
493 total_grams += dive->weightsystem[i].weight.grams;
497 static void weight_data_func(GtkTreeViewColumn *col,
498 GtkCellRenderer *renderer,
508 gtk_tree_model_get(model, iter, DIVE_INDEX, &indx, -1);
509 dive = get_dive(indx);
510 value = get_weight_units(total_weight(dive), &decimals, NULL);
514 snprintf(buffer, sizeof(buffer), "%.*f", decimals, value);
516 g_object_set(renderer, "text", buffer, NULL);
519 static gint nitrox_sort_func(GtkTreeModel *model,
524 int index_a, index_b;
528 int a_o2low, b_o2low;
530 gtk_tree_model_get(model, iter_a, DIVE_INDEX, &index_a, -1);
531 gtk_tree_model_get(model, iter_b, DIVE_INDEX, &index_b, -1);
532 a = get_dive(index_a);
533 b = get_dive(index_b);
534 get_dive_gas(a, &a_o2, &a_he, &a_o2low);
535 get_dive_gas(b, &b_o2, &b_he, &b_o2low);
537 /* Sort by Helium first, O2 second */
540 return a_o2low - b_o2low;
546 #define UTF8_ELLIPSIS "\xE2\x80\xA6"
548 static void nitrox_data_func(GtkTreeViewColumn *col,
549 GtkCellRenderer *renderer,
554 int idx, o2, he, o2low;
558 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
563 dive = get_dive(idx);
564 get_dive_gas(dive, &o2, &he, &o2low);
567 o2low = (o2low + 5) / 10;
570 snprintf(buffer, sizeof(buffer), "%d/%d", o2, he);
573 snprintf(buffer, sizeof(buffer), "%d", o2);
575 snprintf(buffer, sizeof(buffer), "%d" UTF8_ELLIPSIS "%d", o2low, o2);
577 strcpy(buffer, "air");
579 g_object_set(renderer, "text", buffer, NULL);
582 /* Render the SAC data (integer value of "ml / min") */
583 static void sac_data_func(GtkTreeViewColumn *col,
584 GtkCellRenderer *renderer,
594 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_SAC, &value, -1);
596 if (idx < 0 || !value) {
601 sac = value / 1000.0;
602 switch (output_units.volume) {
608 sac = ml_to_cuft(sac * 1000);
611 snprintf(buffer, sizeof(buffer), fmt, sac);
613 g_object_set(renderer, "text", buffer, NULL);
616 /* Render the OTU data (integer value of "OTU") */
617 static void otu_data_func(GtkTreeViewColumn *col,
618 GtkCellRenderer *renderer,
626 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_OTU, &value, -1);
628 if (idx < 0 || !value)
631 snprintf(buffer, sizeof(buffer), "%d", value);
633 g_object_set(renderer, "text", buffer, NULL);
636 /* calculate OTU for a dive */
637 static int calculate_otu(struct dive *dive)
642 for (i = 1; i < dive->samples; i++) {
645 struct sample *sample = dive->sample + i;
646 struct sample *psample = sample - 1;
647 t = sample->time.seconds - psample->time.seconds;
648 int o2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille;
651 po2 = o2 / 1000.0 * (sample->depth.mm + 10000) / 10000.0;
653 otu += pow(po2 - 0.5, 0.83) * t / 30.0;
658 * Return air usage (in liters).
660 static double calculate_airuse(struct dive *dive)
665 for (i = 0; i < MAX_CYLINDERS; i++) {
666 pressure_t start, end;
667 cylinder_t *cyl = dive->cylinder + i;
668 int size = cyl->type.size.mliter;
674 start = cyl->start.mbar ? cyl->start : cyl->sample_start;
675 end = cyl->end.mbar ? cyl->end : cyl->sample_end;
676 kilo_atm = (to_ATM(start) - to_ATM(end)) / 1000.0;
678 /* Liters of air at 1 atm == milliliters at 1k atm*/
679 airuse += kilo_atm * size;
684 static int calculate_sac(struct dive *dive)
686 double airuse, pressure, sac;
689 airuse = calculate_airuse(dive);
692 if (!dive->duration.seconds)
695 /* find and eliminate long surface intervals */
696 duration = dive->duration.seconds;
697 for (i = 0; i < dive->samples; i++) {
698 if (dive->sample[i].depth.mm < 100) { /* less than 10cm */
700 while (end < dive->samples && dive->sample[end].depth.mm < 100)
702 /* we only want the actual surface time during a dive */
703 if (end < dive->samples) {
705 duration -= dive->sample[end].time.seconds -
706 dive->sample[i].time.seconds;
711 /* Mean pressure in atm: 1 atm per 10m */
712 pressure = 1 + (dive->meandepth.mm / 10000.0);
713 sac = airuse / pressure * 60 / duration;
715 /* milliliters per minute.. */
719 void update_cylinder_related_info(struct dive *dive)
722 dive->sac = calculate_sac(dive);
723 dive->otu = calculate_otu(dive);
727 static void get_string(char **str, const char *s)
743 static void get_location(struct dive *dive, char **str)
745 get_string(str, dive->location);
748 static void get_cylinder(struct dive *dive, char **str)
750 get_string(str, dive->cylinder[0].type.description);
753 static void get_suit(struct dive *dive, char **str)
755 get_string(str, dive->suit);
759 * Set up anything that could have changed due to editing
760 * of dive information; we need to do this for both models,
761 * so we simply call set_one_dive again with the non-current model
763 /* forward declaration for recursion */
764 static gboolean set_one_dive(GtkTreeModel *model,
769 static void fill_one_dive(struct dive *dive,
773 char *location, *cylinder, *suit;
774 GtkTreeStore *othermodel;
776 get_cylinder(dive, &cylinder);
777 get_location(dive, &location);
778 get_suit(dive, &suit);
780 gtk_tree_store_set(GTK_TREE_STORE(model), iter,
781 DIVE_NR, dive->number,
782 DIVE_LOCATION, location,
783 DIVE_CYLINDER, cylinder,
784 DIVE_RATING, dive->rating,
787 DIVE_TOTALWEIGHT, total_weight(dive),
795 if (model == GTK_TREE_MODEL(dive_list.treemodel))
796 othermodel = dive_list.listmodel;
798 othermodel = dive_list.treemodel;
799 if (othermodel != dive_list.model)
801 gtk_tree_model_foreach(GTK_TREE_MODEL(othermodel), set_one_dive, dive);
804 static gboolean set_one_dive(GtkTreeModel *model,
812 /* Get the dive number */
813 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
816 dive = get_dive(idx);
819 if (data && dive != data)
822 fill_one_dive(dive, model, iter);
826 void flush_divelist(struct dive *dive)
828 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
830 gtk_tree_model_foreach(model, set_one_dive, dive);
833 void set_divelist_font(const char *font)
835 PangoFontDescription *font_desc = pango_font_description_from_string(font);
836 gtk_widget_modify_font(dive_list.tree_view, font_desc);
837 pango_font_description_free(font_desc);
840 void update_dive_list_units(void)
843 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
845 (void) get_depth_units(0, NULL, &unit);
846 gtk_tree_view_column_set_title(dive_list.depth, unit);
848 (void) get_temp_units(0, &unit);
849 gtk_tree_view_column_set_title(dive_list.temperature, unit);
851 (void) get_weight_units(0, NULL, &unit);
852 gtk_tree_view_column_set_title(dive_list.totalweight, unit);
854 gtk_tree_model_foreach(model, set_one_dive, NULL);
857 void update_dive_list_col_visibility(void)
859 gtk_tree_view_column_set_visible(dive_list.cylinder, visible_cols.cylinder);
860 gtk_tree_view_column_set_visible(dive_list.temperature, visible_cols.temperature);
861 gtk_tree_view_column_set_visible(dive_list.totalweight, visible_cols.totalweight);
862 gtk_tree_view_column_set_visible(dive_list.suit, visible_cols.suit);
863 gtk_tree_view_column_set_visible(dive_list.nitrox, visible_cols.nitrox);
864 gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
865 gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
869 /* random heuristic - not diving in three days implies new dive trip */
870 #define TRIP_THRESHOLD 3600*24*3
871 static int new_group(struct dive *dive, struct dive **last_dive, time_t *tm_date)
876 struct dive *ldive = *last_dive;
877 if (abs(dive->when - ldive->when) < TRIP_THRESHOLD) {
884 struct tm *tm1 = gmtime(&dive->when);
888 *tm_date = mktime(tm1);
893 static void fill_dive_list(void)
896 GtkTreeIter iter, parent_iter;
897 GtkTreeStore *liststore, *treestore;
898 struct dive *last_dive = NULL;
899 struct dive *first_trip_dive = NULL;
900 struct dive *last_trip_dive = NULL;
903 treestore = GTK_TREE_STORE(dive_list.treemodel);
904 liststore = GTK_TREE_STORE(dive_list.listmodel);
908 struct dive *dive = dive_table.dives[i];
910 if (new_group(dive, &last_dive, &dive_date))
912 /* make sure we display the first date of the trip in previous summary */
913 if (first_trip_dive && last_trip_dive && last_trip_dive->when < first_trip_dive->when)
914 gtk_tree_store_set(treestore, &parent_iter,
915 DIVE_DATE, last_trip_dive->when,
916 DIVE_LOCATION, last_trip_dive->location,
918 first_trip_dive = dive;
920 gtk_tree_store_append(treestore, &parent_iter, NULL);
921 gtk_tree_store_set(treestore, &parent_iter,
922 DIVE_INDEX, -NEW_TRIP,
924 DIVE_DATE, dive_date,
925 DIVE_LOCATION, dive->location,
930 last_trip_dive = dive;
931 update_cylinder_related_info(dive);
932 gtk_tree_store_append(treestore, &iter, &parent_iter);
933 gtk_tree_store_set(treestore, &iter,
935 DIVE_NR, dive->number,
936 DIVE_DATE, dive->when,
937 DIVE_DEPTH, dive->maxdepth,
938 DIVE_DURATION, dive->duration.seconds,
939 DIVE_LOCATION, dive->location,
940 DIVE_RATING, dive->rating,
941 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
944 gtk_tree_store_append(liststore, &iter, NULL);
945 gtk_tree_store_set(liststore, &iter,
947 DIVE_NR, dive->number,
948 DIVE_DATE, dive->when,
949 DIVE_DEPTH, dive->maxdepth,
950 DIVE_DURATION, dive->duration.seconds,
951 DIVE_LOCATION, dive->location,
952 DIVE_RATING, dive->rating,
953 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
955 DIVE_SUIT, dive->suit,
960 update_dive_list_units();
961 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
962 GtkTreeSelection *selection;
964 /* select the last dive (and make sure it's an actual dive that is selected) */
965 gtk_tree_model_get(GTK_TREE_MODEL(dive_list.model), &iter, DIVE_INDEX, &selected_dive, -1);
966 first_leaf(GTK_TREE_MODEL(dive_list.model), &iter, &selected_dive);
967 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
968 gtk_tree_selection_select_iter(selection, &iter);
972 void dive_list_update_dives(void)
974 gtk_tree_store_clear(GTK_TREE_STORE(dive_list.treemodel));
975 gtk_tree_store_clear(GTK_TREE_STORE(dive_list.listmodel));
980 static struct divelist_column {
987 [DIVE_NR] = { "#", nr_data_func, NULL, ALIGN_RIGHT | UNSORTABLE },
988 [DIVE_DATE] = { "Date", date_data_func, NULL, ALIGN_LEFT },
989 [DIVE_RATING] = { UTF8_BLACKSTAR, star_data_func, NULL, ALIGN_LEFT },
990 [DIVE_DEPTH] = { "ft", depth_data_func, NULL, ALIGN_RIGHT },
991 [DIVE_DURATION] = { "min", duration_data_func, NULL, ALIGN_RIGHT },
992 [DIVE_TEMPERATURE] = { UTF8_DEGREE "F", temperature_data_func, NULL, ALIGN_RIGHT, &visible_cols.temperature },
993 [DIVE_TOTALWEIGHT] = { "lbs", weight_data_func, NULL, ALIGN_RIGHT, &visible_cols.totalweight },
994 [DIVE_SUIT] = { "Suit", NULL, NULL, ALIGN_LEFT, &visible_cols.suit },
995 [DIVE_CYLINDER] = { "Cyl", NULL, NULL, 0, &visible_cols.cylinder },
996 [DIVE_NITROX] = { "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, nitrox_sort_func, 0, &visible_cols.nitrox },
997 [DIVE_SAC] = { "SAC", sac_data_func, NULL, 0, &visible_cols.sac },
998 [DIVE_OTU] = { "OTU", otu_data_func, NULL, 0, &visible_cols.otu },
999 [DIVE_LOCATION] = { "Location", NULL, NULL, ALIGN_LEFT },
1003 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, struct divelist_column *col)
1005 int index = col - &dl_column[0];
1006 const char *title = col->header;
1007 data_func_t data_func = col->data;
1008 sort_func_t sort_func = col->sort;
1009 unsigned int flags = col->flags;
1010 int *visible = col->visible;
1011 GtkWidget *tree_view = dl->tree_view;
1012 GtkTreeStore *treemodel = dl->treemodel;
1013 GtkTreeStore *listmodel = dl->listmodel;
1014 GtkTreeViewColumn *ret;
1016 if (visible && !*visible)
1018 ret = tree_view_column(tree_view, index, title, data_func, flags);
1020 /* the sort functions are needed in the corresponding models */
1021 if (index == DIVE_DATE)
1022 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(treemodel), index, sort_func, NULL, NULL);
1024 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(listmodel), index, sort_func, NULL, NULL);
1030 * This is some crazy crap. The only way to get default focus seems
1031 * to be to grab focus as the widget is being shown the first time.
1033 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
1035 gtk_widget_grab_focus(tree_view);
1038 static void row_activated_cb(GtkTreeView *tree_view,
1040 GtkTreeViewColumn *column,
1046 if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(dive_list.model), &iter, path))
1048 gtk_tree_model_get(GTK_TREE_MODEL(dive_list.model), &iter, DIVE_INDEX, &index, -1);
1049 /* a negative index is special for the "group by date" entries */
1051 edit_dive_info(get_dive(index));
1054 void add_dive_cb(GtkWidget *menuitem, gpointer data)
1058 dive = alloc_dive();
1059 if (add_new_dive(dive)) {
1067 void edit_dive_cb(GtkWidget *menuitem, gpointer data)
1069 edit_multi_dive_info(amount_selected, selectiontracker);
1072 static void popup_divelist_menu(GtkTreeView *tree_view, GtkTreeModel *model, int button)
1074 GtkWidget *menu, *menuitem, *image;
1075 char editlabel[] = "Edit dives";
1077 menu = gtk_menu_new();
1078 menuitem = gtk_image_menu_item_new_with_label("Add dive");
1079 image = gtk_image_new_from_stock(GTK_STOCK_ADD, GTK_ICON_SIZE_MENU);
1080 gtk_image_menu_item_set_image(GTK_IMAGE_MENU_ITEM(menuitem), image);
1081 g_signal_connect(menuitem, "activate", G_CALLBACK(add_dive_cb), NULL);
1082 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1083 if (amount_selected) {
1084 if (amount_selected == 1)
1085 editlabel[strlen(editlabel) - 1] = '\0';
1086 menuitem = gtk_menu_item_new_with_label(editlabel);
1087 g_signal_connect(menuitem, "activate", G_CALLBACK(edit_dive_cb), model);
1088 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
1090 gtk_widget_show_all(menu);
1092 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
1093 button, gtk_get_current_event_time());
1096 static void popup_menu_cb(GtkTreeView *tree_view, gpointer userdata)
1098 popup_divelist_menu(tree_view, GTK_TREE_MODEL(dive_list.model), 0);
1101 static gboolean button_press_cb(GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
1103 /* Right-click? Bring up the menu */
1104 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
1105 popup_divelist_menu(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(dive_list.model), 3);
1111 /* we need to have a temporary copy of the selected dives while
1112 switching model as the selection_cb function keeps getting called
1113 when gtk_tree_selection_select_path is called. We also need to
1114 keep copies of the sort order so we can restore that as well after
1115 switching models. */
1116 static int *oldselection;
1117 static int old_nr_selected;
1118 static gboolean second_call = FALSE;
1119 static GtkSortType sortorder[] = { [0 ... DIVELIST_COLUMNS - 1] = GTK_SORT_DESCENDING, };
1120 static int lastcol = DIVE_DATE;
1122 /* Check if this dive was selected previously and select it again in the new model;
1123 * This is used after we switch models to maintain consistent selections.
1124 * We always return FALSE to iterate through all dives */
1125 static gboolean select_selected(GtkTreeModel *model, GtkTreePath *path,
1126 GtkTreeIter *iter, gpointer data)
1129 GtkTreeSelection *selection = GTK_TREE_SELECTION(data);
1131 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
1132 for (i = 0; i < old_nr_selected; i++)
1133 if (oldselection[i] == idx) {
1134 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(dive_list.tree_view), path);
1135 gtk_tree_selection_select_path(selection, path);
1143 static void update_column_and_order(int colid)
1145 /* Careful: the index into treecolumns is off by one as we don't have a
1146 tree_view column for DIVE_INDEX */
1147 GtkTreeViewColumn **treecolumns = &dive_list.nr;
1149 /* this will trigger a second call into sort_column_change_cb,
1150 so make sure we don't start an infinite recursion... */
1152 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(dive_list.model), colid, sortorder[colid]);
1153 gtk_tree_view_column_set_sort_order(treecolumns[colid - 1], sortorder[colid]);
1154 second_call = FALSE;
1157 /* If the sort column is date (default), show the tree model.
1158 For every other sort column only show the list model.
1159 If the model changed, inform the new model of the chosen sort column and make
1160 sure the same dives are still selected.
1162 The challenge with this function is that once we change the model
1163 we also need to change the sort column again (as it was changed in
1164 the other model) and that causes this function to be called
1165 recursively - so we need to catch that.
1167 static void sort_column_change_cb(GtkTreeSortable *treeview, gpointer data)
1171 GtkTreeStore *currentmodel = dive_list.model;
1176 gtk_tree_sortable_get_sort_column_id(treeview, &colid, &order);
1177 if(colid == lastcol) {
1178 /* we just changed sort order */
1179 sortorder[colid] = order;
1184 if(colid == DIVE_DATE)
1185 dive_list.model = dive_list.treemodel;
1187 dive_list.model = dive_list.listmodel;
1188 if (dive_list.model != currentmodel) {
1189 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
1191 /* remember what is currently selected, switch models and reselect the selected rows */
1192 old_nr_selected = amount_selected;
1193 oldselection = malloc(old_nr_selected * sizeof(int));
1194 if (amount_selected)
1195 memcpy(oldselection, selectiontracker, amount_selected * sizeof(int));
1196 gtk_tree_view_set_model(GTK_TREE_VIEW(dive_list.tree_view), GTK_TREE_MODEL(dive_list.model));
1198 update_column_and_order(colid);
1200 if (old_nr_selected) {
1201 /* we need to select all the dives that were selected */
1202 /* this is fundamentally an n^2 algorithm as implemented - YUCK */
1203 gtk_tree_model_foreach(GTK_TREE_MODEL(dive_list.model), select_selected, selection);
1206 if (order != sortorder[colid]) {
1207 update_column_and_order(colid);
1212 GtkWidget *dive_list_create(void)
1214 GtkTreeSelection *selection;
1216 dive_list.listmodel = gtk_tree_store_new(DIVELIST_COLUMNS,
1217 G_TYPE_INT, /* index */
1218 G_TYPE_INT, /* nr */
1219 G_TYPE_INT, /* Date */
1220 G_TYPE_INT, /* Star rating */
1221 G_TYPE_INT, /* Depth */
1222 G_TYPE_INT, /* Duration */
1223 G_TYPE_INT, /* Temperature */
1224 G_TYPE_INT, /* Total weight */
1225 G_TYPE_STRING, /* Suit */
1226 G_TYPE_STRING, /* Cylinder */
1227 G_TYPE_INT, /* Nitrox */
1228 G_TYPE_INT, /* SAC */
1229 G_TYPE_INT, /* OTU */
1230 G_TYPE_STRING /* Location */
1232 dive_list.treemodel = gtk_tree_store_new(DIVELIST_COLUMNS,
1233 G_TYPE_INT, /* index */
1234 G_TYPE_INT, /* nr */
1235 G_TYPE_INT, /* Date */
1236 G_TYPE_INT, /* Star rating */
1237 G_TYPE_INT, /* Depth */
1238 G_TYPE_INT, /* Duration */
1239 G_TYPE_INT, /* Temperature */
1240 G_TYPE_INT, /* Total weight */
1241 G_TYPE_STRING, /* Suit */
1242 G_TYPE_STRING, /* Cylinder */
1243 G_TYPE_INT, /* Nitrox */
1244 G_TYPE_INT, /* SAC */
1245 G_TYPE_INT, /* OTU */
1246 G_TYPE_STRING /* Location */
1248 dive_list.model = dive_list.treemodel;
1249 dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
1250 set_divelist_font(divelist_font);
1252 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
1254 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
1255 gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
1257 dive_list.nr = divelist_column(&dive_list, dl_column + DIVE_NR);
1258 dive_list.date = divelist_column(&dive_list, dl_column + DIVE_DATE);
1259 dive_list.stars = divelist_column(&dive_list, dl_column + DIVE_RATING);
1260 dive_list.depth = divelist_column(&dive_list, dl_column + DIVE_DEPTH);
1261 dive_list.duration = divelist_column(&dive_list, dl_column + DIVE_DURATION);
1262 dive_list.temperature = divelist_column(&dive_list, dl_column + DIVE_TEMPERATURE);
1263 dive_list.totalweight = divelist_column(&dive_list, dl_column + DIVE_TOTALWEIGHT);
1264 dive_list.suit = divelist_column(&dive_list, dl_column + DIVE_SUIT);
1265 dive_list.cylinder = divelist_column(&dive_list, dl_column + DIVE_CYLINDER);
1266 dive_list.nitrox = divelist_column(&dive_list, dl_column + DIVE_NITROX);
1267 dive_list.sac = divelist_column(&dive_list, dl_column + DIVE_SAC);
1268 dive_list.otu = divelist_column(&dive_list, dl_column + DIVE_OTU);
1269 dive_list.location = divelist_column(&dive_list, dl_column + DIVE_LOCATION);
1273 g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
1274 "search-column", DIVE_LOCATION,
1278 g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
1279 g_signal_connect(dive_list.tree_view, "row-activated", G_CALLBACK(row_activated_cb), NULL);
1280 g_signal_connect(dive_list.tree_view, "row-expanded", G_CALLBACK(row_expanded_cb), NULL);
1281 g_signal_connect(dive_list.tree_view, "button-press-event", G_CALLBACK(button_press_cb), NULL);
1282 g_signal_connect(dive_list.tree_view, "popup-menu", G_CALLBACK(popup_menu_cb), NULL);
1283 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), NULL);
1284 g_signal_connect(dive_list.listmodel, "sort-column-changed", G_CALLBACK(sort_column_change_cb), NULL);
1285 g_signal_connect(dive_list.treemodel, "sort-column-changed", G_CALLBACK(sort_column_change_cb), NULL);
1287 gtk_tree_selection_set_select_function(selection, modify_selection_cb, NULL, NULL);
1289 dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
1290 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
1291 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1292 gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
1294 dive_list.changed = 0;
1296 return dive_list.container_widget;
1299 void mark_divelist_changed(int changed)
1301 dive_list.changed = changed;
1304 int unsaved_changes()
1306 return dive_list.changed;