From: Linus Torvalds Date: Fri, 17 Aug 2012 03:39:49 +0000 (-0700) Subject: Merge branch 'tree2' of git://git.hohndel.org/subsurface X-Git-Url: http://git.tdb.fi/?p=ext%2Fsubsurface.git;a=commitdiff_plain;h=6fdeeb8c9f4dafe31aa51e785f535feb8a9df46e Merge branch 'tree2' of git://git.hohndel.org/subsurface Pull selection tracking fixes from Dirk Hohndel: "I just gave up on gtk tracking our selection. Way too much pain. The implementation below has seen some testing with the debugging code enabled and seems to work - but it needs more banging onto it, I'm sure. Ideally I'd like to leave the debug code in, ask people on the mailing list to play with it and report any inconsistencies. After that I'll be happy to remove it again." * 'tree2' of git://git.hohndel.org/subsurface: Stop relying on gtk to track which dives are selected --- 6fdeeb8c9f4dafe31aa51e785f535feb8a9df46e diff --cc divelist.c index de64bd8,19ab600..0e1b40d --- a/divelist.c +++ b/divelist.c @@@ -79,6 -77,79 +79,78 @@@ static void dump_model(GtkListStore *st #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