Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Project/Src/Gui/TextAreaControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -508,9 +508,14 @@ public void ScrollTo(int line)
if (TextArea.TextView.VisibleLineCount == 1)
VScrollBar.Value = Math.Max(0, Math.Min(VScrollBar.Maximum, (line - scrollMarginHeight - 1) * TextArea.TextView.FontHeight));
else
{
int possibleHeightLoss = HScrollBar.Visible ? 0 : SystemInformation.HorizontalScrollBarHeight;
int fontHeight = TextArea.TextView.FontHeight;
int completelyVisibleLinesCount = (TextArea.TextView.DrawingPosition.Height - possibleHeightLoss) / fontHeight;
VScrollBar.Value = Math.Min(
VScrollBar.Maximum,
(line - TextArea.TextView.VisibleLineCount + scrollMarginHeight - 1) * TextArea.TextView.FontHeight);
(line - completelyVisibleLinesCount + scrollMarginHeight - 1) * fontHeight);
}
VScrollBarValueChanged(this, EventArgs.Empty);
}
}
Expand Down
33 changes: 22 additions & 11 deletions Project/Src/Gui/TextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,7 @@ public TextView(TextArea textArea) : base(textArea)
public int LineHeightRemainder => textArea.VirtualTop.Y%FontHeight;

/// <summary>Gets the first visible <b>logical</b> line.</summary>
public int FirstVisibleLine
{
get => textArea.Document.GetFirstLogicalLine(textArea.VirtualTop.Y/FontHeight);
set
{
// Clamp the value in order to avoid scrolling the text out of view
int clampedValue = Math.Max(0, Math.Min(value, textArea.Document.TotalNumberOfLines - textArea.TextView.VisibleLineCount + 1));
if (FirstVisibleLine != clampedValue)
textArea.VirtualTop = new Point(textArea.VirtualTop.X, textArea.Document.GetVisibleLine(clampedValue) * FontHeight);
}
}
public int FirstVisibleLine => textArea.Document.GetFirstLogicalLine(textArea.VirtualTop.Y/FontHeight);

public int VisibleLineDrawingRemainder => textArea.VirtualTop.Y%FontHeight;

Expand Down Expand Up @@ -1135,5 +1125,26 @@ private void DrawVerticalRuler(Graphics g, Rectangle lineRectangle)
}

#endregion

/// <summary>
/// Sets the first visible <b>logical</b> line.
/// </summary>
/// <param name="lineIndex">
/// The logical line which should be the top line.
/// The value is clamped in order to avoid scrolling the text out of view.
/// </param>
/// <param name="possibleHeightLoss">
/// A horizontal scrollbar might be needed and would reduce the number of visible lines.
/// Pass the height scrollbar height if it should be considered when clamping <paramref name="lineIndex"/>.
/// </param>
public void SetFirstVisibleLine(int lineIndex, int possibleHeightLoss = 0)
{
int completelyVisibleLineCount = (DrawingPosition.Height - possibleHeightLoss) / FontHeight;
int clampedValue = Math.Max(0, Math.Min(lineIndex, textArea.Document.TotalNumberOfLines - completelyVisibleLineCount));
if (FirstVisibleLine != clampedValue)
{
textArea.VirtualTop = new Point(textArea.VirtualTop.X, textArea.Document.GetVisibleLine(clampedValue) * FontHeight);
}
}
}
}