public bool AddChapterToInterestingTexts(IStText newText)
{
int oldCount = m_scriptureTexts.Count;
int targetPosition = TextPosition(newText);
if (targetPosition < 0)
{
var excludedCoreTextIdList = ExcludedCoreTextIdList();
if (newText.Owner is IText && excludedCoreTextIdList.Contains(newText.Guid))
{
CoreTexts.Add(newText);
if (m_interestingTests != null)
m_interestingTests.Add(newText);
excludedCoreTextIdList.Remove(newText.Guid);
UpdateExcludedCoreTexts(excludedCoreTextIdList);
RaiseInterestingTextsChanged(CoreTexts.Count - 1, 1, 0);
return true; // added sucessfully
}
return false; // not a text in current Scripture.
}
int index;
for (index = 0; index < m_scriptureTexts.Count; index++)
{
if (TextPosition(m_scriptureTexts[index]) > targetPosition)
{
break;
}
}
m_scriptureTexts.Insert(index, newText);
// Also insert the other text in the same section
var sec = newText.Owner as IScrSection;
if (sec != null) // not a book title
{
if (newText == sec.ContentOA && sec.HeadingOA != null)
{
if (index == 0 || m_scriptureTexts[index - 1] != sec.HeadingOA)
m_scriptureTexts.Insert(index, sec.HeadingOA);
else
index--; // move index to point at heading
}
else if (sec.ContentOA != null)
{
if (index >= m_scriptureTexts.Count - 1 || m_scriptureTexts[index + 1] != sec.ContentOA)
m_scriptureTexts.Insert(index + 1, sec.ContentOA);
}
// At this point the heading and contents of the section for the inserted text
// are at index. We look for adjacent sections in the same chapter and if necessary
// add them too.
int indexAfter = index + 1;
if (sec.ContentOA != null && sec.HeadingOA != null)
indexAfter++;
// It would be nicer to use ScrReference, but not worth adding a whole project reference.
int chapMax = sec.VerseRefMax / 1000;
int chapMin = sec.VerseRefMin / 1000;
var book = (IScrBook)sec.Owner;
int csec = book.SectionsOS.Count;
int isecCur = sec.IndexInOwner;
for (int isec = isecCur + 1; isec < csec; isec++)
{
IScrSection secNext = book.SectionsOS[isec];
if (secNext.VerseRefMin / 1000 != chapMax)
break; // different chapter.
indexAfter = AddAfter(indexAfter, secNext.HeadingOA);
indexAfter = AddAfter(indexAfter, secNext.ContentOA);
}
for (int isec = isecCur - 1; isec >= 0; isec--)
{
IScrSection secPrev = book.SectionsOS[isec];
if (secPrev.VerseRefMax / 1000 != chapMin)
break;
index = AddBefore(index, secPrev.ContentOA);
index = AddBefore(index, secPrev.HeadingOA);
}
}
// We could get fancy and try to figure the exact range that changed, but this is close enough.
RaiseInterestingTextsChanged(CoreTexts.Count, m_scriptureTexts.Count, oldCount);
return true;
}