/// ------------------------------------------------------------------------------------ /// <summary> /// Finds the nearest footnote before the given selection. /// </summary> /// <param name="helper">The selection helper.</param> /// <param name="book">The book that owns the footnote collection.</param> /// <param name="fSearchForward">True to also search forward within the current paragraph</param> /// <returns></returns> /// ------------------------------------------------------------------------------------ private IStFootnote FindFootnoteNearSelection(SelectionHelper helper, IScrBook book, bool fSearchForward) { CheckDisposed(); if (helper == null) helper = CurrentSelection; if (helper == null || book == null) return null; SelLevInfo[] levels = helper.GetLevelInfo(SelectionHelper.SelLimitType.Anchor); int iParagraph = -1; int tag = 0; int ich = helper.IchAnchor; int paraLev = helper.GetLevelForTag(StTextTags.kflidParagraphs); int contentLev = helper.GetLevelForTag(StTxtParaTags.kflidContents); Debug.Assert(paraLev != -1, "Need a paragraph for this method"); iParagraph = levels[paraLev].ihvo; int iSection = ((ITeView)Control).LocationTracker.GetSectionIndexInBook( helper, SelectionHelper.SelLimitType.Anchor); if (iSection < 0) tag = ScrBookTags.kflidTitle; else { tag = (helper.GetLevelForTag(ScrSectionTags.kflidContent) >= 0 ? ScrSectionTags.kflidContent : ScrSectionTags.kflidHeading); } // Special case: if we're in the caption of a picture, get the ich from // the first level instead of the anchor. (TE-4696) if (contentLev >= 0 && levels[contentLev].ihvo == -1) ich = levels[contentLev].ich; IScrFootnote prevFootnote = null; if (fSearchForward) // look first at our current position, if we are searching foward prevFootnote = book.FindCurrentFootnote(iSection, iParagraph, ich, (int)tag); if (prevFootnote == null) { IScrTxtPara para = m_repoScrTxtPara.GetObject(levels[paraLev].hvo); ITsString tss = para.Contents; if (ich != 0) { // look backwards in our current paragraph - skip the current run, except when // at the end of the text. prevFootnote = para.FindPrevFootnoteInContents(ref ich, ich < tss.Length); } else if (iParagraph > 0) { // look at the previous paragraph for a footnote at the end IStText text = m_repoStText.GetObject(levels[paraLev + 1].hvo); IScrTxtPara prevPara = (IScrTxtPara)text[iParagraph - 1]; ITsString prevTss = prevPara.Contents; int ichTmp = -1; prevFootnote = prevPara.FindPrevFootnoteInContents(ref ichTmp, false); if (prevFootnote != null) { if (ichTmp == prevTss.Length - 1) ich = ichTmp; else prevFootnote = null; } // ENHANCE: Look across contexts. } } if (prevFootnote == null && fSearchForward) { // look ahead in the same paragraph IScrTxtPara para = m_repoScrTxtPara.GetObject(levels[paraLev].hvo); ITsString tss = para.Contents; prevFootnote = para.FindNextFootnoteInContents(ref ich, true); } if (prevFootnote == null) { // just go back until we find one prevFootnote = book.FindPrevFootnote(ref iSection, ref iParagraph, ref ich, ref tag); } return prevFootnote; }