public static int GetLineForTextPointer(this RichTextBox rtb, TextPointer tp) { TextPointer tpLine = tp.GetLineStartPosition(0); int lineNr = 1; for (TextPointer linePointer = rtb.Document.ContentStart.GetLineStartPosition(0); linePointer.CompareTo(tpLine) < 0; linePointer = linePointer.GetLineStartPosition(1)) { lineNr++; } return lineNr; }
/// <summary> /// Find the input string within the document, starting at the specified position. /// </summary> /// <param name="position">the current text position</param> /// <param name="input">input text</param> /// <returns>An <see cref="TextRange"/> representing the (next) matching string within the text container. Null if there are no matches.</returns> public TextRange FindNext(ref TextPointer position, String input) { FindText(input); foreach (var result in _searchHits) { if (position.CompareTo(result.End) < 0) { position = result.Start; double top = this.PointToScreen(position.GetLineStartPosition(0).GetCharacterRect(LogicalDirection.Forward).TopLeft).Y + this.PointFromScreen(new System.Windows.Point(0, 0)).Y; Trace.WriteLine(string.Format(" Top: {0}, CharOffset: {1}", top, position)); ScrollViewer.ScrollToVerticalOffset(ScrollViewer.VerticalOffset + top); position = result.End; return result; } } return null; }
internal static int GetColumnNumberFromSelection(TextPointer position) { if (position == null) { return 0; } int linesMoved; TextPointer lineStartPosition = position.GetLineStartPosition(0, out linesMoved); int columnNumber = 0; do { columnNumber++; position = position.GetNextInsertionPosition(LogicalDirection.Backward); } while (position != null && position.CompareTo(lineStartPosition) > 0); return columnNumber; }
internal static int GetLineNumberFromSelection(TextPointer position) { if (position == null) { return 0; } int lineNumber = 0; int linesMoved; do { position = position.GetLineStartPosition(-1, out linesMoved); lineNumber++; } while (position != null && linesMoved != 0); return lineNumber; }
private int GetLineNumber(TextPointer pointer) { int someBigNumber = int.MaxValue; int lineMoved; pointer.GetLineStartPosition(-someBigNumber, out lineMoved); return -lineMoved; }
private TextRange GetInputLine(TextPointer pointer) { var nextStart = pointer.GetLineStartPosition(1); var lineEnd = (nextStart ?? pointer.DocumentEnd).GetInsertionPosition(LogicalDirection.Backward); var lineStart = pointer.GetLineStartPosition(0); var prevEnd = lineStart.GetInsertionPosition(LogicalDirection.Backward); if (GetLineNumber(prevEnd) != 0) //are we already at the beginning of the content? lineStart = SearchBackwardsForLineContinuation(lineStart, prevEnd); return new TextRange(lineStart, lineEnd); }