private void GridReordered(string s)
{
var newSeq = new List<IPage>();
var keys = s.Split(new [] {','}, StringSplitOptions.RemoveEmptyEntries);
foreach (var key in keys)
{
IPage page;
if (_pageMap.TryGetValue(key, out page))
newSeq.Add(page);
}
Debug.Assert(newSeq.Count == _pages.Count);
// Now, which one moved?
int firstDiff = 0;
while (firstDiff < _pages.Count && _pages[firstDiff] == newSeq[firstDiff])
firstDiff++;
int limDiff = _pages.Count;
while (limDiff > firstDiff && _pages[limDiff-1] == newSeq[limDiff-1])
limDiff--;
if (firstDiff == limDiff)
return; // spurious notification somehow? Nothing changed.
// We have the subsequence that altered.
// Is the change legal?
for (int i = firstDiff; i < limDiff; i++)
{
if (!_pages[i].CanRelocate)
{
var msg = LocalizationManager.GetString("EditTab.PageList.CantMoveXMatter",
"That change is not allowed. Front matter and back matter pages must remain where they are.");
//previously had a caption that didn't add value, just more translation work
if (_pages[i].Book.LockedDown)
{
msg = LocalizationManager.GetString("PageList.CantMoveWhenTranslating",
"Pages can not be re-ordered when you are translating a book.");
msg = msg + System.Environment.NewLine+ EditingView.GetInstructionsForUnlockingBook();
}
MessageBox.Show(msg);
UpdateItems(_pages); // reset to old state
return;
}
}
// There are two possibilities: the user dragged the item that used to be at the start to the end,
// or the item that used to be the end to the start.
IPage movedPage;
int newPageIndex;
if (_pages[firstDiff] == newSeq[limDiff - 1])
{
// Move forward
movedPage = _pages[firstDiff];
newPageIndex = limDiff - 1;
}
else
{
Debug.Assert(_pages[limDiff - 1] == newSeq[firstDiff]); // moved last page forwards
movedPage = _pages[limDiff - 1];
newPageIndex = firstDiff;
}
var relocatePageInfo = new RelocatePageInfo(movedPage, newPageIndex);
RelocatePageEvent.Raise(relocatePageInfo);
if (relocatePageInfo.Cancel)
UpdateItems(_pages);
else
{
_pages = newSeq;
UpdatePageNumbers();
// This is only needed if left and right pages are styled differently.
// Unfortunately gecko does not re-apply the styles when things are re-ordered!
UpdateItems(_pages);
}
}