#endif
static GList *selected_dives;
-static int *selectiontracker;
+ static int st_size = 0;
+
+ gboolean is_in_st(int idx, int *atpos)
+ {
+ int i;
+
+ for (i = 0; i < amount_selected; i++)
+ if (selectiontracker[i] == idx) {
+ if (atpos)
+ *atpos = i;
+ return TRUE;
+ }
+ return FALSE;
+ }
+
+ #if DEBUG_SELECTION_TRACKING
+ void dump_selection(void)
+ {
+ int i;
+
+ printf("currently selected are ");
+ for (i = 0; i < amount_selected; i++)
+ printf("%d ", selectiontracker[i]);
+ printf("\n");
+ }
+ #endif
+
+ void track_select(int idx)
+ {
+ if (idx < 0)
+ return;
+
+ #if DEBUG_SELECTION_TRACKING
+ printf("add %d to selection of %d entries\n", idx, amount_selected);
+ #endif
+ if (is_in_st(idx, NULL))
+ return;
+ if (amount_selected >= st_size) {
+ selectiontracker = realloc(selectiontracker, dive_table.nr * sizeof(int));
+ st_size = dive_table.nr;
+ }
+ selectiontracker[amount_selected] = idx;
+ amount_selected++;
+ if (amount_selected == 1)
+ selected_dive = idx;
+ #if DEBUG_SELECTION_TRACKING
+ printf("increased amount_selected to %d\n", amount_selected);
+ dump_selection();
+ #endif
+ }
+
+ void track_unselect(int idx)
+ {
+ if (idx < 0)
+ return;
+
+ #if DEBUG_SELECTION_TRACKING
+ printf("remove %d from selection of %d entries\n", idx, amount_selected);
+ #endif
+ int atpos;
+
+ if (! is_in_st(idx, &atpos))
+ return;
+ memmove(selectiontracker + atpos,
+ selectiontracker + atpos + 1,
+ (amount_selected - atpos - 1) * sizeof(int));
+ amount_selected--;
+ #if DEBUG_SELECTION_TRACKING
+ printf("removed %d at pos %d and decreased amount_selected to %d\n", idx, atpos, amount_selected);
+ dump_selection();
+ #endif
+ }
/* when subsurface starts we want to have the last dive selected. So we simply
walk to the first leaf (and skip the summary entries - which have negative