/// <summary> /// Scrolls to the specified line/column. /// This method requires that the TextEditor was already assigned a size (WPF layout must have run prior). /// </summary> /// <param name="line">Line to scroll to.</param> /// <param name="column">Column to scroll to (important if wrapping is 'on', and for the horizontal scroll position).</param> /// <param name="yPositionMode">The mode how to reference the Y position of the line.</param> /// <param name="referencedVerticalViewPortOffset">Offset from the top of the viewport to where the referenced line/column should be positioned.</param> /// <param name="minimumScrollFraction">The minimum vertical and/or horizontal scroll offset, expressed as fraction of the height or width of the viewport window, respectively.</param> public void ScrollTo(int line, int column, VisualYPosition yPositionMode, double referencedVerticalViewPortOffset, double minimumScrollFraction) { TextView textView = textArea.TextView; TextDocument document = textView.Document; if (scrollViewer != null && document != null) { if (line < 1) { line = 1; } if (line > document.LineCount) { line = document.LineCount; } IScrollInfo scrollInfo = textView; if (!scrollInfo.CanHorizontallyScroll) { // Word wrap is enabled. Ensure that we have up-to-date info about line height so that we scroll // to the correct position. // This avoids that the user has to repeat the ScrollTo() call several times when there are very long lines. VisualLine vl = textView.GetOrConstructVisualLine(document.GetLineByNumber(line)); double remainingHeight = referencedVerticalViewPortOffset; while (remainingHeight > 0) { DocumentLine prevLine = vl.FirstDocumentLine.PreviousLine; if (prevLine == null) { break; } vl = textView.GetOrConstructVisualLine(prevLine); remainingHeight -= vl.Height; } } Point p = textArea.TextView.GetVisualPosition(new TextViewPosition(line, Math.Max(1, column)), yPositionMode); double verticalPos = p.Y - referencedVerticalViewPortOffset; if (Math.Abs(verticalPos - scrollViewer.VerticalOffset) > minimumScrollFraction * scrollViewer.ViewportHeight) { scrollViewer.ScrollToVerticalOffset(Math.Max(0, verticalPos)); } if (column > 0) { if (p.X > scrollViewer.ViewportWidth - Caret.MinimumDistanceToViewBorder * 2) { double horizontalPos = Math.Max(0, p.X - scrollViewer.ViewportWidth / 2); if (Math.Abs(horizontalPos - scrollViewer.HorizontalOffset) > minimumScrollFraction * scrollViewer.ViewportWidth) { scrollViewer.ScrollToHorizontalOffset(horizontalPos); } } else { scrollViewer.ScrollToHorizontalOffset(0); } } } }
public Point GetPositionFromOffset(TextEditor editor, VisualYPosition position, int offset) { var startLocation = editor.TextArea.TextView.Document.GetLocation(offset); var point = editor.TextArea.TextView.GetVisualPosition(new TextViewPosition(startLocation), position); return new Point(Math.Round(point.X), Math.Round(point.Y)); }
/// <summary> /// Gets the visual position from the specified visualColumn. /// </summary> /// <returns>Position in device-independent pixels /// relative to the top left of the document.</returns> public Point GetVisualPosition(int visualColumn, VisualYPosition yPositionMode) { TextLine textLine = GetTextLine(visualColumn); double xPos = textLine.GetDistanceFromCharacterHit(new CharacterHit(visualColumn, 0)); double yPos = GetTextLineVisualYPosition(textLine, yPositionMode); return(new Point(xPos, yPos)); }
/// <summary> /// Gets the visual position from the specified visualColumn. /// </summary> /// <returns>Position in device-independent pixels /// relative to the top left of the document.</returns> public Point GetVisualPosition(int visualColumn, VisualYPosition yPositionMode) { TextLine textLine = GetTextLine(visualColumn); double xPos = GetTextLineVisualXPosition(textLine, visualColumn); double yPos = GetTextLineVisualYPosition(textLine, yPositionMode); return(new Point(xPos, yPos)); }
internal Point GetVisualPosition(int visualColumn, bool isAtEndOfLine, VisualYPosition yPositionMode) { TextLine textLine = GetTextLine(visualColumn, isAtEndOfLine); double xPos = GetTextLineVisualXPosition(textLine, visualColumn); double yPos = GetTextLineVisualYPosition(textLine, yPositionMode); return(new Point(xPos, yPos)); }
/// <summary>The visual position of this line at the given column.</summary> public Point VisualPosition(int column, VisualYPosition mode) { // Clamp the column to within the range of the line var col = Math.Max(0, Math.Min(column, FirstLine.LengthWithLineEnd)); var text_line = TextLine(col) ?? throw new NotSupportedException("Need to handle columns off the end"); var xpos = text_line.GetDistanceFromCharacterHit(new CharacterHit(col, 0)) + Math.Max(0, column - col) * TextView.WideSpaceWidth; var ypos = YPos + YOffset(TextView, text_line, mode); return(new Point(xpos, ypos)); }
/// <summary> /// Gets the visual top from the specified text line. /// </summary> /// <returns>Distance in device-independent pixels /// from the top of the document to the top of the specified text line.</returns> public double GetTextLineVisualYPosition(TextLine textLine, VisualYPosition yPositionMode) { if (textLine == null) { throw new ArgumentNullException("textLine"); } double pos = VisualTop; foreach (TextLine tl in TextLines) { if (tl == textLine) { switch (yPositionMode) { case VisualYPosition.LineTop: return(pos); case VisualYPosition.LineMiddle: return(pos + (tl.Height + LineHeight) / 2); /* Az Add Start */ case VisualYPosition.LineBottom: return(pos + (tl.Height + LineHeight)); /* Az Add Start */ case VisualYPosition.TextTop: return(pos + tl.Baseline - textView.DefaultBaseline); case VisualYPosition.TextBottom: return(pos + tl.Baseline - textView.DefaultBaseline + textView.DefaultLineHeight); case VisualYPosition.TextMiddle: return(pos + tl.Baseline - textView.DefaultBaseline + textView.DefaultLineHeight / 2); case VisualYPosition.Baseline: return(pos + tl.Baseline); default: throw new ArgumentException("Invalid yPositionMode:" + yPositionMode); } } else { /* Az Add Start */ pos += tl.Height + LineHeight; /* Az Add End */ } } throw new ArgumentException("textLine is not a line in this VisualLine"); }
/// <summary> /// Gets the visual top from the specified text line. /// </summary> /// <returns>Distance in device-independent pixels /// from the top of the document to the top of the specified text line.</returns> public double GetTextLineVisualYPosition(TextLine textLine, VisualYPosition yPositionMode) { if (textLine == null) { throw new ArgumentNullException("textLine"); } double pos = VisualTop; foreach (TextLine tl in TextLines) { var lineHeight = Math.Ceiling(tl.Height) + textView.Options.LinePadding.Top + textView.Options.LinePadding.Bottom; if (tl == textLine) { var baseline = tl.Baseline; switch (yPositionMode) { case VisualYPosition.LineTop: return(pos); case VisualYPosition.LineMiddle: return(pos + lineHeight / 2); case VisualYPosition.LineBottom: return(pos + lineHeight); case VisualYPosition.TextTop: return(pos + baseline - textView.DefaultBaseline); case VisualYPosition.TextBottom: return(pos + baseline - textView.DefaultBaseline + textView.DefaultLineHeight); case VisualYPosition.TextMiddle: return(pos + baseline - textView.DefaultBaseline + textView.DefaultLineHeight / 2); case VisualYPosition.Baseline: return(pos + baseline); default: throw new ArgumentException("Invalid yPositionMode:" + yPositionMode); } } else { pos += lineHeight; } } throw new ArgumentException("textLine is not a line in this VisualLine"); }
/// <summary> /// Gets the visual top from the specified text line. /// </summary> /// <returns>Distance in device-independent pixels /// from the top of the document to the top of the specified text line.</returns> public double GetTextLineVisualYPosition(TextLine textLine, VisualYPosition yPositionMode) { if (textLine == null) { throw new ArgumentNullException(nameof(textLine)); } var pos = VisualTop; foreach (var tl in TextLines) { if (tl == textLine) { switch (yPositionMode) { case VisualYPosition.LineTop: return(pos); case VisualYPosition.LineMiddle: return(pos + tl.Height / 2); case VisualYPosition.LineBottom: return(pos + tl.Height); case VisualYPosition.TextTop: return(pos + tl.Baseline - _textView.DefaultBaseline); case VisualYPosition.TextBottom: return(pos + tl.Baseline - _textView.DefaultBaseline + _textView.DefaultLineHeight); case VisualYPosition.TextMiddle: return(pos + tl.Baseline - _textView.DefaultBaseline + _textView.DefaultLineHeight / 2); case VisualYPosition.Baseline: return(pos + tl.Baseline); default: throw new ArgumentException("Invalid yPositionMode:" + yPositionMode); } } pos += tl.Height; } throw new ArgumentException("textLine is not a line in this VisualLine"); }
/// <summary> /// Gets the visual top from the specified text line. /// </summary> /// <returns>Distance in device-independent pixels /// from the top of the document to the top of the specified text line.</returns> public double GetTextLineVisualYPosition(TextLine textLine, VisualYPosition yPositionMode) { if (textLine == null) { throw new ArgumentNullException("textLine"); } double pos = VisualTop; foreach (TextLine tl in TextLines) { if (tl == textLine) { switch (yPositionMode) { case VisualYPosition.LineTop: return(pos); case VisualYPosition.LineMiddle: return(pos + tl.Height / 2); case VisualYPosition.LineBottom: return(pos + tl.Height); case VisualYPosition.TextTop: return(pos + tl.Height - textView.FontSize); default: throw new ArgumentException("Invalid yPositionMode:" + yPositionMode); } } else { pos += tl.Height; } } throw new ArgumentException("textLine is not a line in this VisualLine"); }
/// <summary> /// Gets the visual position from the specified visualColumn. /// </summary> /// <returns>Position in device-independent pixels /// relative to the top left of the document.</returns> public Point GetVisualPosition(int visualColumn, VisualYPosition yPositionMode) { TextLine textLine = GetTextLine(visualColumn); double xPos = GetTextLineVisualXPosition(textLine, visualColumn); double yPos = GetTextLineVisualYPosition(textLine, yPositionMode); return new Point(xPos, yPos); }
/// <summary> /// Gets the visual top from the specified text line. /// </summary> /// <returns>Distance in device-independent pixels /// from the top of the document to the top of the specified text line.</returns> public double GetTextLineVisualYPosition(TextLine textLine, VisualYPosition yPositionMode) { if (textLine == null) throw new ArgumentNullException("textLine"); double pos = VisualTop; foreach (TextLine tl in TextLines) { if (tl == textLine) { switch (yPositionMode) { case VisualYPosition.LineTop: return pos; case VisualYPosition.LineMiddle: return pos + tl.Height / 2; case VisualYPosition.LineBottom: return pos + tl.Height; case VisualYPosition.TextTop: return pos + tl.Baseline - textView.DefaultBaseline; case VisualYPosition.TextBottom: return pos + tl.Baseline - textView.DefaultBaseline + textView.DefaultLineHeight; case VisualYPosition.TextMiddle: return pos + tl.Baseline - textView.DefaultBaseline + textView.DefaultLineHeight / 2; case VisualYPosition.Baseline: return pos + tl.Baseline; default: throw new ArgumentException("Invalid yPositionMode:" + yPositionMode); } } else { pos += tl.Height; } } throw new ArgumentException("textLine is not a line in this VisualLine"); }
/// <summary> /// Gets the visual position from the specified visualColumn. /// </summary> /// <returns>Position in device-independent pixels /// relative to the top left of the document.</returns> public Point GetVisualPosition(int visualColumn, VisualYPosition yPositionMode) { TextLine textLine = GetTextLine(visualColumn); double xPos = textLine.GetDistanceFromCharacterHit(new CharacterHit(visualColumn, 0)); double yPos = GetTextLineVisualYPosition(textLine, yPositionMode); return new Point(xPos, yPos); }
internal Point GetVisualPosition(int visualColumn, bool isAtEndOfLine, VisualYPosition yPositionMode) { TextLine textLine = GetTextLine(visualColumn, isAtEndOfLine); double xPos = GetTextLineVisualXPosition(textLine, visualColumn); double yPos = GetTextLineVisualYPosition(textLine, yPositionMode); return new Point(xPos, yPos); }
public static void ScrollToAvalonEdit(this ScrollViewer scrollViewer, TextArea textArea, int line, int column, VisualYPosition yPositionMode, double referencedVerticalViewPortOffset, double minimumScrollFraction) { TextView textView = textArea.TextView; TextDocument document = textView.Document; if (scrollViewer == null || document == null) { return; } if (line < 1) { line = 1; } if (line > document.LineCount) { line = document.LineCount; } if (!((System.Windows.Controls.Primitives.IScrollInfo)textView).CanHorizontallyScroll) { VisualLine orConstructVisualLine = textView.GetOrConstructVisualLine(document.GetLineByNumber(line)); for (double num = referencedVerticalViewPortOffset; num > 0.0; num -= orConstructVisualLine.Height) { DocumentLine previousLine = orConstructVisualLine.FirstDocumentLine.PreviousLine; if (previousLine == null) { break; } orConstructVisualLine = textView.GetOrConstructVisualLine(previousLine); } } Point visualPosition = textArea.TextView.GetVisualPosition(new TextViewPosition(line, Math.Max(1, column)), yPositionMode); double num2 = visualPosition.Y - referencedVerticalViewPortOffset; if (Math.Abs(num2 - scrollViewer.VerticalOffset) > minimumScrollFraction * scrollViewer.ViewportHeight) { scrollViewer.AnimateScroll(Math.Max(0.0, num2)); } if (column <= 0) { return; } if (visualPosition.X > scrollViewer.ViewportWidth - 60.0) { double num3 = Math.Max(0.0, visualPosition.X - scrollViewer.ViewportWidth / 2.0); if (Math.Abs(num3 - scrollViewer.HorizontalOffset) > minimumScrollFraction * scrollViewer.ViewportWidth) { scrollViewer.ScrollToHorizontalOffset(num3); } } else { scrollViewer.AnimateScroll(0.0); } }