private ISegment GetNextSegment(IStTxtPara currentPara, ISegment seg, bool upward,
out AnalysisOccurrence realAnalysis)
{
ISegment nextSeg = null;
realAnalysis = null;
var currentText = currentPara.Owner as IStText;
Debug.Assert(currentText != null, "Paragraph not owned by a text.");
var lines = LineChoices.m_specs as IEnumerable<InterlinLineSpec>;
var delta = upward ? -1 : 1;
var nextSegIndex = delta + seg.IndexInOwner;
do
{
if (0 <= nextSegIndex && nextSegIndex < currentPara.SegmentsOS.Count)
{
nextSeg = currentPara.SegmentsOS[nextSegIndex];
nextSegIndex += delta; // increment for next loop in case it doesn't check out
}
else
{ // try the first (last) segment in the next (previous) paragraph
int nextParaIndex = delta + currentPara.IndexInOwner;
nextSeg = null;
IStTxtPara nextPara = null;
if (0 <= nextParaIndex && nextParaIndex < currentText.ParagraphsOS.Count)
{ // try to find this paragraph's first (last) segment
currentPara = (IStTxtPara)currentText.ParagraphsOS[nextParaIndex];
nextSegIndex = upward ? currentPara.SegmentsOS.Count - 1 : 0;
}
else
{ // no more paragraphs in this text
break;
}
}
realAnalysis = FindRealAnalysisInSegment(nextSeg, !upward);
} while (nextSeg == null || (realAnalysis == null && !HasVisibleTranslationOrNote(nextSeg, lines)));
return nextSeg;
}