private static bool PreviousUnit(int start, ITextRange subrange, TomUnit unit) { if (subrange.Start <= start) { return false; } else { // collapse the range to the end and then extend it another unit. subrange.Collapse(TomStartEnd.tomStart); subrange.MoveStart(unit, -1); // truncate if necessary to ensure it fits inside the range if (subrange.Start < start) { subrange.Start = start; } return true; } }
private ITextRangeProvider FindAttributeBackwards(AutomationTextAttribute attribute, object val, TomUnit unit) { // this works just like FindAttributeForwards except we work our way backward through the range. // we accumulate the resulting subrange with these two endpoints: const int NoMatchYet = -1; int start = NoMatchYet; // a character position that is extended each time we find another matching subrange. int end = NoMatchYet; // set to a character position when we find the beginning of the match. // examine each subrange of uniform formatting until we reach the end of our range. // if we complete a match within the range we will break out of the middle of the loop. int limit = _range.Start; // cache the limit of our search range. ITextRange subrange = LastUnit(_range); while (PreviousUnit(limit, subrange, unit)) { // if this subrange of values has a matching attribute then add it to // our resulting subrange. object subrangeVal = GetAttributeValueForRange(subrange, attribute); if (AttributeValuesAreEqual(val, subrangeVal)) { // set the start pointer if this is the first matching subrange. if (end == NoMatchYet) { end = subrange.End; } // update the start of the matching subrange to include the current one. start = subrange.Start; } else { // no match. // if we have found a matching subrange then we're done. if (end != NoMatchYet) { break; } } } // if we have a matching subrange then return it, otherwise return null. if (end != NoMatchYet) { subrange.SetRange(start, end); return new WindowsRichEditRange(subrange, _pattern); } else { return null; } }
private static bool NextUnit(int end, ITextRange subrange, TomUnit unit) { if (subrange.End >= end) { return false; } else { // collapse the range to the end and then extend it another unit. subrange.Collapse(TomStartEnd.tomEnd); subrange.MoveEnd(unit, 1); // truncate if necessary to ensure it fits inside the range if (subrange.End > end) { subrange.End = end; } return true; } }