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, *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 */
48 DIVE_NITROX, /* int: dummy */
49 DIVE_SAC, /* int: in ml/min */
50 DIVE_OTU, /* int: in OTUs */
51 DIVE_LOCATION, /* "2nd Cathedral, Lanai" */
55 /* magic numbers that indicate (as negative values) model entries that
56 * are summary entries for a divetrip */
60 static gboolean dump_model_entry(GtkTreeModel *model, GtkTreePath *path,
61 GtkTreeIter *iter, gpointer data)
64 int idx, nr, rating, depth;
66 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_NR, &nr, DIVE_RATING, &rating, DIVE_DEPTH, &depth, DIVE_LOCATION, &location, -1);
67 printf("entry #%d : nr %d rating %d depth %d location %s \n", idx, nr, rating, depth, location);
73 static void dump_model(GtkListStore *store)
75 gtk_tree_model_foreach(GTK_TREE_MODEL(store), dump_model_entry, NULL);
79 static GList *selected_dives;
80 static int *selectiontracker;
82 /* when subsurface starts we want to have the last dive selected. So we simply
83 walk to the first leaf (and skip the summary entries - which have negative
85 static void first_leaf(GtkTreeModel *model, GtkTreeIter *iter, int *diveidx)
90 while (*diveidx < 0) {
91 memcpy(&parent, iter, sizeof(parent));
92 tpath = gtk_tree_model_get_path(model, &parent);
93 if (!gtk_tree_model_iter_children(model, iter, &parent))
94 /* we should never have a parent without child */
96 if(!gtk_tree_view_row_expanded(GTK_TREE_VIEW(dive_list.tree_view), tpath))
97 gtk_tree_view_expand_row(GTK_TREE_VIEW(dive_list.tree_view), tpath, FALSE);
98 gtk_tree_model_get(GTK_TREE_MODEL(model), iter, DIVE_INDEX, diveidx, -1);
102 /* if we click on a summary dive, we actually want to select / unselect
103 all the dives "below" it */
104 static void select_children(GtkTreeModel *model, GtkTreeSelection * selection,
105 GtkTreeIter *iter, gboolean was_selected)
111 memcpy(&parent, iter, sizeof(parent));
113 tpath = gtk_tree_model_get_path(model, &parent);
114 if(!gtk_tree_view_row_expanded(GTK_TREE_VIEW(dive_list.tree_view), tpath))
115 gtk_tree_view_expand_row(GTK_TREE_VIEW(dive_list.tree_view), tpath, FALSE);
117 nr_children = gtk_tree_model_iter_n_children(model, &parent);
118 for (i = 0; i < nr_children; i++) {
119 gtk_tree_model_iter_nth_child(model, iter, &parent, i);
121 gtk_tree_selection_unselect_iter(selection, iter);
123 gtk_tree_selection_select_iter(selection, iter);
127 /* this is called _before_ the selection is changed, for every single entry;
128 * we simply have it call down the tree to make sure that summary items toggle
130 gboolean modify_selection_cb(GtkTreeSelection *selection, GtkTreeModel *model,
131 GtkTreePath *path, gboolean was_selected, gpointer userdata)
136 if (gtk_tree_model_get_iter(model, &iter, path)) {
137 gtk_tree_model_get(model, &iter, DIVE_INDEX, &dive_idx, -1);
139 select_children(model, selection, &iter, was_selected);
142 /* allow this selection to proceed */
146 /* this is called when gtk thinks that the selection has changed */
147 static void selection_cb(GtkTreeSelection *selection, gpointer userdata)
152 int nr_selected = gtk_tree_selection_count_selected_rows(selection);
154 if (selected_dives) {
155 g_list_foreach (selected_dives, (GFunc) gtk_tree_path_free, NULL);
156 g_list_free (selected_dives);
158 selected_dives = gtk_tree_selection_get_selected_rows(selection, NULL);
159 selectiontracker = realloc(selectiontracker, nr_selected * sizeof(int));
161 switch (nr_selected) {
162 case 0: /* there is no clear way to figure out which dive to show */
167 /* just pick that dive as selected */
169 path = g_list_nth_data(selected_dives, 0);
170 if (gtk_tree_model_get_iter(GTK_TREE_MODEL(dive_list.model), &iter, path)) {
171 gtk_tree_model_get(GTK_TREE_MODEL(dive_list.model), &iter, DIVE_INDEX, &selected_dive, -1);
172 /* due to the way this callback gets invoked it is possible that
173 in the process of unselecting a summary dive we get here with
174 just one summary dive selected - ignore that case */
175 if (selected_dive < 0) {
179 selectiontracker[0] = selected_dive;
183 default: /* multiple selections - what now?
184 * We don't change the selected dive unless there is exactly one dive selected; not sure this
185 * is the most intuitive solution.
186 * The dives that have been selected are processed */
187 amount_selected = g_list_length(selected_dives);
188 process_selected_dives(selected_dives, selectiontracker, GTK_TREE_MODEL(dive_list.model));
194 const char *star_strings[] = {
203 static void star_data_func(GtkTreeViewColumn *col,
204 GtkCellRenderer *renderer,
212 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_RATING, &nr_stars, -1);
216 if (nr_stars < 0 || nr_stars > 5)
218 snprintf(buffer, sizeof(buffer), "%s", star_strings[nr_stars]);
220 g_object_set(renderer, "text", buffer, NULL);
223 static void date_data_func(GtkTreeViewColumn *col,
224 GtkCellRenderer *renderer,
234 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DATE, &val, -1);
242 snprintf(buffer, sizeof(buffer),
243 "Trip %s, %s %d, %d",
244 weekday(tm->tm_wday),
245 monthname(tm->tm_mon),
246 tm->tm_mday, tm->tm_year + 1900);
249 snprintf(buffer, sizeof(buffer),
250 "%s, %s %d, %d %02d:%02d",
251 weekday(tm->tm_wday),
252 monthname(tm->tm_mon),
253 tm->tm_mday, tm->tm_year + 1900,
254 tm->tm_hour, tm->tm_min);
256 g_object_set(renderer, "text", buffer, NULL);
259 static void depth_data_func(GtkTreeViewColumn *col,
260 GtkCellRenderer *renderer,
265 int depth, integer, frac, len, idx;
268 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DEPTH, &depth, -1);
273 switch (output_units.length) {
275 /* To tenths of meters */
276 depth = (depth + 49) / 100;
277 integer = depth / 10;
286 integer = mm_to_feet(depth) + 0.5;
292 len = snprintf(buffer, sizeof(buffer), "%d", integer);
294 len += snprintf(buffer+len, sizeof(buffer)-len, ".%d", frac);
296 g_object_set(renderer, "text", buffer, NULL);
299 static void duration_data_func(GtkTreeViewColumn *col,
300 GtkCellRenderer *renderer,
309 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_DURATION, &sec, -1);
313 snprintf(buffer, sizeof(buffer), "%d:%02d", sec / 60, sec % 60);
315 g_object_set(renderer, "text", buffer, NULL);
318 static void temperature_data_func(GtkTreeViewColumn *col,
319 GtkCellRenderer *renderer,
327 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_TEMPERATURE, &value, -1);
330 if (idx >= 0 && value) {
332 switch (output_units.temperature) {
334 deg = mkelvin_to_C(value);
337 deg = mkelvin_to_F(value);
342 snprintf(buffer, sizeof(buffer), "%.1f", deg);
345 g_object_set(renderer, "text", buffer, NULL);
348 static void nr_data_func(GtkTreeViewColumn *col,
349 GtkCellRenderer *renderer,
357 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_NR, &nr, -1);
361 snprintf(buffer, sizeof(buffer), "%d", nr);
362 g_object_set(renderer, "text", buffer, NULL);
366 * Get "maximal" dive gas for a dive.
368 * - Trimix trumps nitrox (highest He wins, O2 breaks ties)
369 * - Nitrox trumps air (even if hypoxic)
370 * These are the same rules as the inter-dive sorting rules.
372 static void get_dive_gas(struct dive *dive, int *o2_p, int *he_p, int *o2low_p)
375 int maxo2 = -1, maxhe = -1, mino2 = 1000;
377 for (i = 0; i < MAX_CYLINDERS; i++) {
378 cylinder_t *cyl = dive->cylinder + i;
379 struct gasmix *mix = &cyl->gasmix;
380 int o2 = mix->o2.permille;
381 int he = mix->he.permille;
383 if (cylinder_none(cyl))
399 /* All air? Show/sort as "air"/zero */
400 if (!maxhe && maxo2 == AIR_PERMILLE && mino2 == maxo2)
407 static gint nitrox_sort_func(GtkTreeModel *model,
412 int index_a, index_b;
416 int a_o2low, b_o2low;
418 gtk_tree_model_get(model, iter_a, DIVE_INDEX, &index_a, -1);
419 gtk_tree_model_get(model, iter_b, DIVE_INDEX, &index_b, -1);
420 a = get_dive(index_a);
421 b = get_dive(index_b);
422 get_dive_gas(a, &a_o2, &a_he, &a_o2low);
423 get_dive_gas(b, &b_o2, &b_he, &b_o2low);
425 /* Sort by Helium first, O2 second */
428 return a_o2low - b_o2low;
434 #define UTF8_ELLIPSIS "\xE2\x80\xA6"
436 static void nitrox_data_func(GtkTreeViewColumn *col,
437 GtkCellRenderer *renderer,
442 int idx, o2, he, o2low;
446 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
451 dive = get_dive(idx);
452 get_dive_gas(dive, &o2, &he, &o2low);
455 o2low = (o2low + 5) / 10;
458 snprintf(buffer, sizeof(buffer), "%d/%d", o2, he);
461 snprintf(buffer, sizeof(buffer), "%d", o2);
463 snprintf(buffer, sizeof(buffer), "%d" UTF8_ELLIPSIS "%d", o2low, o2);
465 strcpy(buffer, "air");
467 g_object_set(renderer, "text", buffer, NULL);
470 /* Render the SAC data (integer value of "ml / min") */
471 static void sac_data_func(GtkTreeViewColumn *col,
472 GtkCellRenderer *renderer,
482 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_SAC, &value, -1);
484 if (idx < 0 || !value) {
489 sac = value / 1000.0;
490 switch (output_units.volume) {
496 sac = ml_to_cuft(sac * 1000);
499 snprintf(buffer, sizeof(buffer), fmt, sac);
501 g_object_set(renderer, "text", buffer, NULL);
504 /* Render the OTU data (integer value of "OTU") */
505 static void otu_data_func(GtkTreeViewColumn *col,
506 GtkCellRenderer *renderer,
514 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, DIVE_OTU, &value, -1);
516 if (idx < 0 || !value)
519 snprintf(buffer, sizeof(buffer), "%d", value);
521 g_object_set(renderer, "text", buffer, NULL);
524 /* calculate OTU for a dive */
525 static int calculate_otu(struct dive *dive)
530 for (i = 1; i < dive->samples; i++) {
533 struct sample *sample = dive->sample + i;
534 struct sample *psample = sample - 1;
535 t = sample->time.seconds - psample->time.seconds;
536 int o2 = dive->cylinder[sample->cylinderindex].gasmix.o2.permille;
539 po2 = o2 / 1000.0 * (sample->depth.mm + 10000) / 10000.0;
541 otu += pow(po2 - 0.5, 0.83) * t / 30.0;
546 * Return air usage (in liters).
548 static double calculate_airuse(struct dive *dive)
553 for (i = 0; i < MAX_CYLINDERS; i++) {
554 pressure_t start, end;
555 cylinder_t *cyl = dive->cylinder + i;
556 int size = cyl->type.size.mliter;
562 start = cyl->start.mbar ? cyl->start : cyl->sample_start;
563 end = cyl->end.mbar ? cyl->end : cyl->sample_end;
564 kilo_atm = (to_ATM(start) - to_ATM(end)) / 1000.0;
566 /* Liters of air at 1 atm == milliliters at 1k atm*/
567 airuse += kilo_atm * size;
572 static int calculate_sac(struct dive *dive)
574 double airuse, pressure, sac;
577 airuse = calculate_airuse(dive);
580 if (!dive->duration.seconds)
583 /* find and eliminate long surface intervals */
584 duration = dive->duration.seconds;
585 for (i = 0; i < dive->samples; i++) {
586 if (dive->sample[i].depth.mm < 100) { /* less than 10cm */
588 while (end < dive->samples && dive->sample[end].depth.mm < 100)
590 /* we only want the actual surface time during a dive */
591 if (end < dive->samples) {
593 duration -= dive->sample[end].time.seconds -
594 dive->sample[i].time.seconds;
599 /* Mean pressure in atm: 1 atm per 10m */
600 pressure = 1 + (dive->meandepth.mm / 10000.0);
601 sac = airuse / pressure * 60 / duration;
603 /* milliliters per minute.. */
607 void update_cylinder_related_info(struct dive *dive)
610 dive->sac = calculate_sac(dive);
611 dive->otu = calculate_otu(dive);
615 static void get_string(char **str, const char *s)
631 static void get_location(struct dive *dive, char **str)
633 get_string(str, dive->location);
636 static void get_cylinder(struct dive *dive, char **str)
638 get_string(str, dive->cylinder[0].type.description);
642 * Set up anything that could have changed due to editing
643 * of dive information; we need to do this for both models,
644 * so we simply call set_one_dive again with the non-current model
646 /* forward declaration for recursion */
647 static gboolean set_one_dive(GtkTreeModel *model,
652 static void fill_one_dive(struct dive *dive,
656 char *location, *cylinder;
657 GtkTreeStore *othermodel;
659 get_cylinder(dive, &cylinder);
660 get_location(dive, &location);
662 gtk_tree_store_set(GTK_TREE_STORE(model), iter,
663 DIVE_NR, dive->number,
664 DIVE_LOCATION, location,
665 DIVE_CYLINDER, cylinder,
666 DIVE_RATING, dive->rating,
670 if (model == GTK_TREE_MODEL(dive_list.treemodel))
671 othermodel = dive_list.listmodel;
673 othermodel = dive_list.treemodel;
674 if (othermodel != dive_list.model)
676 gtk_tree_model_foreach(GTK_TREE_MODEL(othermodel), set_one_dive, dive);
679 static gboolean set_one_dive(GtkTreeModel *model,
687 /* Get the dive number */
688 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
691 dive = get_dive(idx);
694 if (data && dive != data)
697 fill_one_dive(dive, model, iter);
701 void flush_divelist(struct dive *dive)
703 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
705 gtk_tree_model_foreach(model, set_one_dive, dive);
708 void set_divelist_font(const char *font)
710 PangoFontDescription *font_desc = pango_font_description_from_string(font);
711 gtk_widget_modify_font(dive_list.tree_view, font_desc);
712 pango_font_description_free(font_desc);
715 void update_dive_list_units(void)
718 GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model);
720 (void) get_depth_units(0, NULL, &unit);
721 gtk_tree_view_column_set_title(dive_list.depth, unit);
723 (void) get_temp_units(0, &unit);
724 gtk_tree_view_column_set_title(dive_list.temperature, unit);
726 gtk_tree_model_foreach(model, set_one_dive, NULL);
729 void update_dive_list_col_visibility(void)
731 gtk_tree_view_column_set_visible(dive_list.cylinder, visible_cols.cylinder);
732 gtk_tree_view_column_set_visible(dive_list.temperature, visible_cols.temperature);
733 gtk_tree_view_column_set_visible(dive_list.nitrox, visible_cols.nitrox);
734 gtk_tree_view_column_set_visible(dive_list.sac, visible_cols.sac);
735 gtk_tree_view_column_set_visible(dive_list.otu, visible_cols.otu);
739 /* random heuristic - not diving in three days implies new dive trip */
740 #define TRIP_THRESHOLD 3600*24*3
741 static int new_group(struct dive *dive, struct dive **last_dive, time_t *tm_date)
746 struct dive *ldive = *last_dive;
747 if (abs(dive->when - ldive->when) < TRIP_THRESHOLD) {
754 struct tm *tm1 = gmtime(&dive->when);
758 *tm_date = mktime(tm1);
763 static void fill_dive_list(void)
766 GtkTreeIter iter, parent_iter;
767 GtkTreeStore *liststore, *treestore;
768 struct dive *last_dive = NULL;
769 struct dive *first_trip_dive = NULL;
770 struct dive *last_trip_dive = NULL;
773 treestore = GTK_TREE_STORE(dive_list.treemodel);
774 liststore = GTK_TREE_STORE(dive_list.listmodel);
778 struct dive *dive = dive_table.dives[i];
780 if (new_group(dive, &last_dive, &dive_date))
782 /* make sure we display the first date of the trip in previous summary */
783 if (first_trip_dive && last_trip_dive && last_trip_dive->when < first_trip_dive->when)
784 gtk_tree_store_set(treestore, &parent_iter,
785 DIVE_DATE, last_trip_dive->when,
786 DIVE_LOCATION, last_trip_dive->location,
788 first_trip_dive = dive;
790 gtk_tree_store_append(treestore, &parent_iter, NULL);
791 gtk_tree_store_set(treestore, &parent_iter,
792 DIVE_INDEX, -NEW_TRIP,
794 DIVE_DATE, dive_date,
795 DIVE_LOCATION, dive->location,
800 last_trip_dive = dive;
801 update_cylinder_related_info(dive);
802 gtk_tree_store_append(treestore, &iter, &parent_iter);
803 gtk_tree_store_set(treestore, &iter,
805 DIVE_NR, dive->number,
806 DIVE_DATE, dive->when,
807 DIVE_DEPTH, dive->maxdepth,
808 DIVE_DURATION, dive->duration.seconds,
809 DIVE_LOCATION, dive->location,
810 DIVE_RATING, dive->rating,
811 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
814 gtk_tree_store_append(liststore, &iter, NULL);
815 gtk_tree_store_set(liststore, &iter,
817 DIVE_NR, dive->number,
818 DIVE_DATE, dive->when,
819 DIVE_DEPTH, dive->maxdepth,
820 DIVE_DURATION, dive->duration.seconds,
821 DIVE_LOCATION, dive->location,
822 DIVE_RATING, dive->rating,
823 DIVE_TEMPERATURE, dive->watertemp.mkelvin,
828 update_dive_list_units();
829 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(dive_list.model), &iter)) {
830 GtkTreeSelection *selection;
832 /* select the last dive (and make sure it's an actual dive that is selected) */
833 gtk_tree_model_get(GTK_TREE_MODEL(dive_list.model), &iter, DIVE_INDEX, &selected_dive, -1);
834 first_leaf(GTK_TREE_MODEL(dive_list.model), &iter, &selected_dive);
835 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
836 gtk_tree_selection_select_iter(selection, &iter);
837 selectiontracker = realloc(selectiontracker, sizeof(int));
838 *selectiontracker = selected_dive;
842 void dive_list_update_dives(void)
844 gtk_tree_store_clear(GTK_TREE_STORE(dive_list.treemodel));
845 gtk_tree_store_clear(GTK_TREE_STORE(dive_list.listmodel));
850 static struct divelist_column {
857 [DIVE_NR] = { "#", nr_data_func, NULL, ALIGN_RIGHT | UNSORTABLE },
858 [DIVE_DATE] = { "Date", date_data_func, NULL, ALIGN_LEFT },
859 [DIVE_RATING] = { UTF8_BLACKSTAR, star_data_func, NULL, ALIGN_LEFT },
860 [DIVE_DEPTH] = { "ft", depth_data_func, NULL, ALIGN_RIGHT },
861 [DIVE_DURATION] = { "min", duration_data_func, NULL, ALIGN_RIGHT },
862 [DIVE_TEMPERATURE] = { UTF8_DEGREE "F", temperature_data_func, NULL, ALIGN_RIGHT, &visible_cols.temperature },
863 [DIVE_CYLINDER] = { "Cyl", NULL, NULL, 0, &visible_cols.cylinder },
864 [DIVE_NITROX] = { "O" UTF8_SUBSCRIPT_2 "%", nitrox_data_func, nitrox_sort_func, 0, &visible_cols.nitrox },
865 [DIVE_SAC] = { "SAC", sac_data_func, NULL, 0, &visible_cols.sac },
866 [DIVE_OTU] = { "OTU", otu_data_func, NULL, 0, &visible_cols.otu },
867 [DIVE_LOCATION] = { "Location", NULL, NULL, ALIGN_LEFT },
871 static GtkTreeViewColumn *divelist_column(struct DiveList *dl, struct divelist_column *col)
873 int index = col - &dl_column[0];
874 const char *title = col->header;
875 data_func_t data_func = col->data;
876 sort_func_t sort_func = col->sort;
877 unsigned int flags = col->flags;
878 int *visible = col->visible;
879 GtkWidget *tree_view = dl->tree_view;
880 GtkTreeStore *treemodel = dl->treemodel;
881 GtkTreeStore *listmodel = dl->listmodel;
882 GtkTreeViewColumn *ret;
884 if (visible && !*visible)
886 ret = tree_view_column(tree_view, index, title, data_func, flags);
888 /* the sort functions are needed in the corresponding models */
889 if (index == DIVE_DATE)
890 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(treemodel), index, sort_func, NULL, NULL);
892 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(listmodel), index, sort_func, NULL, NULL);
898 * This is some crazy crap. The only way to get default focus seems
899 * to be to grab focus as the widget is being shown the first time.
901 static void realize_cb(GtkWidget *tree_view, gpointer userdata)
903 gtk_widget_grab_focus(tree_view);
906 static void row_activated_cb(GtkTreeView *tree_view,
908 GtkTreeViewColumn *column,
914 if (!gtk_tree_model_get_iter(GTK_TREE_MODEL(dive_list.model), &iter, path))
916 gtk_tree_model_get(GTK_TREE_MODEL(dive_list.model), &iter, DIVE_INDEX, &index, -1);
917 /* a negative index is special for the "group by date" entries */
919 edit_dive_info(get_dive(index));
922 void add_dive_cb(GtkWidget *menuitem, gpointer data)
927 if (add_new_dive(dive)) {
935 void edit_dive_cb(GtkWidget *menuitem, gpointer data)
937 edit_multi_dive_info(amount_selected, selectiontracker);
940 static void popup_divelist_menu(GtkTreeView *tree_view, GtkTreeModel *model, int button)
942 GtkWidget *menu, *menuitem;
943 char editlabel[] = "Edit dives";
945 menu = gtk_menu_new();
946 menuitem = gtk_menu_item_new_with_label("Add dive");
947 g_signal_connect(menuitem, "activate", G_CALLBACK(add_dive_cb), NULL);
948 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
949 if (amount_selected) {
950 if (amount_selected == 1)
951 editlabel[strlen(editlabel) - 1] = '\0';
952 menuitem = gtk_menu_item_new_with_label(editlabel);
953 g_signal_connect(menuitem, "activate", G_CALLBACK(edit_dive_cb), model);
954 gtk_menu_shell_append(GTK_MENU_SHELL(menu), menuitem);
956 gtk_widget_show_all(menu);
958 gtk_menu_popup(GTK_MENU(menu), NULL, NULL, NULL, NULL,
959 button, gtk_get_current_event_time());
962 static void popup_menu_cb(GtkTreeView *tree_view, gpointer userdata)
964 popup_divelist_menu(tree_view, GTK_TREE_MODEL(dive_list.model), 0);
967 static gboolean button_press_cb(GtkWidget *treeview, GdkEventButton *event, gpointer userdata)
969 /* Right-click? Bring up the menu */
970 if (event->type == GDK_BUTTON_PRESS && event->button == 3) {
971 popup_divelist_menu(GTK_TREE_VIEW(treeview), GTK_TREE_MODEL(dive_list.model), 3);
977 /* we need to have a temporary copy of the selected dives while
978 switching model as the selection_cb function keeps getting called
979 when gtk_tree_selection_select_path is called. We also need to
980 keep copies of the sort order so we can restore that as well after
982 static int *oldselection;
983 static int old_nr_selected;
984 static gboolean second_call = FALSE;
985 static GtkSortType sortorder[] = { [0 ... DIVELIST_COLUMNS - 1] = GTK_SORT_DESCENDING, };
986 static int lastcol = DIVE_DATE;
988 /* Check if this dive was selected previously and select it again in the new model;
989 * This is used after we switch models to maintain consistent selections.
990 * We always return FALSE to iterate through all dives */
991 static gboolean select_selected(GtkTreeModel *model, GtkTreePath *path,
992 GtkTreeIter *iter, gpointer data)
995 GtkTreeSelection *selection = GTK_TREE_SELECTION(data);
997 gtk_tree_model_get(model, iter, DIVE_INDEX, &idx, -1);
998 for (i = 0; i < old_nr_selected; i++)
999 if (oldselection[i] == idx) {
1000 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(dive_list.tree_view), path);
1001 gtk_tree_selection_select_path(selection, path);
1009 static void update_column_and_order(int colid)
1011 /* Careful: the index into treecolumns is off by one as we don't have a
1012 tree_view column for DIVE_INDEX */
1013 GtkTreeViewColumn **treecolumns = &dive_list.nr;
1015 /* this will trigger a second call into sort_column_change_cb,
1016 so make sure we don't start an infinite recursion... */
1018 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(dive_list.model), colid, sortorder[colid]);
1019 gtk_tree_view_column_set_sort_order(treecolumns[colid - 1], sortorder[colid]);
1020 second_call = FALSE;
1023 /* If the sort column is date (default), show the tree model.
1024 For every other sort column only show the list model.
1025 If the model changed, inform the new model of the chosen sort column and make
1026 sure the same dives are still selected.
1028 The challenge with this function is that once we change the model
1029 we also need to change the sort column again (as it was changed in
1030 the other model) and that causes this function to be called
1031 recursively - so we need to catch that.
1033 static void sort_column_change_cb(GtkTreeSortable *treeview, gpointer data)
1037 GtkTreeStore *currentmodel = dive_list.model;
1042 gtk_tree_sortable_get_sort_column_id(treeview, &colid, &order);
1043 if(colid == lastcol) {
1044 /* we just changed sort order */
1045 sortorder[colid] = order;
1050 if(colid == DIVE_DATE)
1051 dive_list.model = dive_list.treemodel;
1053 dive_list.model = dive_list.listmodel;
1054 if (dive_list.model != currentmodel) {
1055 GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
1057 /* remember what is currently selected, switch models and reselect the selected rows */
1058 old_nr_selected = amount_selected;
1059 oldselection = malloc(old_nr_selected * sizeof(int));
1060 if (amount_selected)
1061 memcpy(oldselection, selectiontracker, amount_selected * sizeof(int));
1062 gtk_tree_view_set_model(GTK_TREE_VIEW(dive_list.tree_view), GTK_TREE_MODEL(dive_list.model));
1064 update_column_and_order(colid);
1066 if (old_nr_selected) {
1067 /* we need to select all the dives that were selected */
1068 /* this is fundamentally an n^2 algorithm as implemented - YUCK */
1069 gtk_tree_model_foreach(GTK_TREE_MODEL(dive_list.model), select_selected, selection);
1072 if (order != sortorder[colid]) {
1073 update_column_and_order(colid);
1078 GtkWidget *dive_list_create(void)
1080 GtkTreeSelection *selection;
1082 dive_list.listmodel = gtk_tree_store_new(DIVELIST_COLUMNS,
1083 G_TYPE_INT, /* index */
1084 G_TYPE_INT, /* nr */
1085 G_TYPE_INT, /* Date */
1086 G_TYPE_INT, /* Star rating */
1087 G_TYPE_INT, /* Depth */
1088 G_TYPE_INT, /* Duration */
1089 G_TYPE_INT, /* Temperature */
1090 G_TYPE_STRING, /* Cylinder */
1091 G_TYPE_INT, /* Nitrox */
1092 G_TYPE_INT, /* SAC */
1093 G_TYPE_INT, /* OTU */
1094 G_TYPE_STRING /* Location */
1096 dive_list.treemodel = gtk_tree_store_new(DIVELIST_COLUMNS,
1097 G_TYPE_INT, /* index */
1098 G_TYPE_INT, /* nr */
1099 G_TYPE_INT, /* Date */
1100 G_TYPE_INT, /* Star rating */
1101 G_TYPE_INT, /* Depth */
1102 G_TYPE_INT, /* Duration */
1103 G_TYPE_INT, /* Temperature */
1104 G_TYPE_STRING, /* Cylinder */
1105 G_TYPE_INT, /* Nitrox */
1106 G_TYPE_INT, /* SAC */
1107 G_TYPE_INT, /* OTU */
1108 G_TYPE_STRING /* Location */
1110 dive_list.model = dive_list.treemodel;
1111 dive_list.tree_view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(dive_list.model));
1112 set_divelist_font(divelist_font);
1114 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(dive_list.tree_view));
1116 gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_MULTIPLE);
1117 gtk_widget_set_size_request(dive_list.tree_view, 200, 200);
1119 dive_list.nr = divelist_column(&dive_list, dl_column + DIVE_NR);
1120 dive_list.date = divelist_column(&dive_list, dl_column + DIVE_DATE);
1121 dive_list.stars = divelist_column(&dive_list, dl_column + DIVE_RATING);
1122 dive_list.depth = divelist_column(&dive_list, dl_column + DIVE_DEPTH);
1123 dive_list.duration = divelist_column(&dive_list, dl_column + DIVE_DURATION);
1124 dive_list.temperature = divelist_column(&dive_list, dl_column + DIVE_TEMPERATURE);
1125 dive_list.cylinder = divelist_column(&dive_list, dl_column + DIVE_CYLINDER);
1126 dive_list.nitrox = divelist_column(&dive_list, dl_column + DIVE_NITROX);
1127 dive_list.sac = divelist_column(&dive_list, dl_column + DIVE_SAC);
1128 dive_list.otu = divelist_column(&dive_list, dl_column + DIVE_OTU);
1129 dive_list.location = divelist_column(&dive_list, dl_column + DIVE_LOCATION);
1133 g_object_set(G_OBJECT(dive_list.tree_view), "headers-visible", TRUE,
1134 "search-column", DIVE_LOCATION,
1138 g_signal_connect_after(dive_list.tree_view, "realize", G_CALLBACK(realize_cb), NULL);
1139 g_signal_connect(dive_list.tree_view, "row-activated", G_CALLBACK(row_activated_cb), NULL);
1140 g_signal_connect(dive_list.tree_view, "button-press-event", G_CALLBACK(button_press_cb), NULL);
1141 g_signal_connect(dive_list.tree_view, "popup-menu", G_CALLBACK(popup_menu_cb), NULL);
1142 g_signal_connect(selection, "changed", G_CALLBACK(selection_cb), NULL);
1143 g_signal_connect(dive_list.listmodel, "sort-column-changed", G_CALLBACK(sort_column_change_cb), NULL);
1144 g_signal_connect(dive_list.treemodel, "sort-column-changed", G_CALLBACK(sort_column_change_cb), NULL);
1146 gtk_tree_selection_set_select_function(selection, modify_selection_cb, NULL, NULL);
1148 dive_list.container_widget = gtk_scrolled_window_new(NULL, NULL);
1149 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dive_list.container_widget),
1150 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1151 gtk_container_add(GTK_CONTAINER(dive_list.container_widget), dive_list.tree_view);
1153 dive_list.changed = 0;
1155 return dive_list.container_widget;
1158 void mark_divelist_changed(int changed)
1160 dive_list.changed = changed;
1163 int unsaved_changes()
1165 return dive_list.changed;