Move HighlightCurrentLine from AvalonEdit.Addin to ICSharpCode.AvalonEdit

This commit is contained in:
Patryk Mikos 2014-01-10 20:43:19 +01:00
Родитель eb70e1002d
Коммит 19a7887bcf
5 изменённых файлов: 177 добавлений и 0 удалений

Просмотреть файл

@ -69,6 +69,7 @@ namespace ICSharpCode.AvalonEdit.Editing
caret = new Caret(this);
caret.PositionChanged += (sender, e) => RequestSelectionValidation();
caret.PositionChanged += CaretPositionChanged;
ime = new ImeSupport(this);
leftMargins.CollectionChanged += leftMargins_CollectionChanged;
@ -559,6 +560,14 @@ namespace ICSharpCode.AvalonEdit.Editing
get { return caret; }
}
void CaretPositionChanged(object sender, EventArgs e)
{
if (textView == null)
return;
this.textView.HighlightedLine = this.Caret.Line;
}
ObservableCollection<UIElement> leftMargins = new ObservableCollection<UIElement>();
/// <summary>

Просмотреть файл

@ -242,6 +242,7 @@
<DependentUpon>IVisualLineTransformer.cs</DependentUpon>
</Compile>
<Compile Include="Rendering\ColumnRulerRenderer.cs" />
<Compile Include="Rendering\CurrentLineHighlightRenderer.cs" />
<Compile Include="Rendering\DefaultTextRunTypographyProperties.cs" />
<Compile Include="Rendering\DocumentColorizingTransformer.cs">
<DependentUpon>IVisualLineTransformer.cs</DependentUpon>

Просмотреть файл

@ -0,0 +1,105 @@
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Media;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Rendering;
namespace ICSharpCode.AvalonEdit.Rendering
{
sealed class CurrentLineHighlightRenderer : IBackgroundRenderer
{
#region Fields
int line;
TextView textView;
public static readonly Color DefaultBackground = Color.FromArgb(22, 20, 220, 224);
public static readonly Color DefaultBorder = Color.FromArgb(52, 0, 255, 110);
#endregion
#region Properties
public int Line {
get { return this.Line; }
set {
this.line = value;
this.textView.InvalidateLayer(this.Layer);
}
}
public bool Enabled {
get; set;
}
public KnownLayer Layer
{
get { return KnownLayer.Selection; }
}
public Brush BackgroundBrush {
get; set;
}
public Pen BorderPen {
get; set;
}
#endregion
public CurrentLineHighlightRenderer(TextView textView)
{
if (textView == null)
throw new ArgumentNullException("textView");
this.BorderPen = new Pen(new SolidColorBrush(DefaultBorder), 1);
this.BorderPen.Freeze();
this.BackgroundBrush = new SolidColorBrush(DefaultBackground);
this.BackgroundBrush.Freeze();
this.textView = textView;
this.textView.BackgroundRenderers.Add(this);
this.line = 0;
}
public void Draw(TextView textView, DrawingContext drawingContext)
{
if(!Enabled)
return;
BackgroundGeometryBuilder builder = new BackgroundGeometryBuilder();
builder.CornerRadius = 1;
builder.AlignToMiddleOfPixels = true;
var visualLine = this.textView.GetVisualLine(line);
if(visualLine == null) return;
var textViewPos = visualLine.GetTextViewPosition(0);
if(textViewPos == null) return;
var position = this.textView.GetVisualPosition(textViewPos, VisualYPosition.LineTop);
if(position == null) return;
var lineWidth = this.textView.ActualWidth;
var lineHeigth = visualLine.Height;
var linePosX = position.X;
var linePosY = position.Y - this.textView.ScrollOffset.Y;
builder.AddRectangle(textView, new Rect(linePosX, linePosY, lineWidth, lineHeigth));
Geometry geometry = builder.CreateGeometry();
if (geometry != null) {
drawingContext.DrawGeometry(this.BackgroundBrush, this.BorderPen, geometry);
}
}
}
}

Просмотреть файл

@ -42,6 +42,7 @@ namespace ICSharpCode.AvalonEdit.Rendering
}
ColumnRulerRenderer columnRulerRenderer;
CurrentLineHighlightRenderer currentLineHighlighRenderer;
/// <summary>
/// Creates a new TextView instance.
@ -54,6 +55,7 @@ namespace ICSharpCode.AvalonEdit.Rendering
lineTransformers = new ObserveAddRemoveCollection<IVisualLineTransformer>(LineTransformer_Added, LineTransformer_Removed);
backgroundRenderers = new ObserveAddRemoveCollection<IBackgroundRenderer>(BackgroundRenderer_Added, BackgroundRenderer_Removed);
columnRulerRenderer = new ColumnRulerRenderer(this);
currentLineHighlighRenderer = new CurrentLineHighlightRenderer(this);
this.Options = new TextEditorOptions();
Debug.Assert(singleCharacterElementGenerator != null); // assert that the option change created the builtin element generators
@ -209,6 +211,8 @@ namespace ICSharpCode.AvalonEdit.Rendering
else
columnRulerRenderer.SetRuler(-1, ColumnRulerPen);
currentLineHighlighRenderer.Enabled = Options.HighlightCurrentLine;
UpdateBuiltinElementGeneratorsFromOptions();
Redraw();
}
@ -1994,6 +1998,12 @@ namespace ICSharpCode.AvalonEdit.Rendering
if (e.Property == ColumnRulerPenProperty) {
columnRulerRenderer.SetRuler(this.Options.ColumnRulerPosition, this.ColumnRulerPen);
}
if (e.Property == CurrentLineBorderProperty) {
currentLineHighlighRenderer.BorderPen = this.CurrentLineBorder;
}
if (e.Property == CurrentLineBackgroundProperty) {
currentLineHighlighRenderer.BackgroundBrush = this.CurrentLineBackground;
}
}
/// <summary>
@ -2019,5 +2029,41 @@ namespace ICSharpCode.AvalonEdit.Rendering
get { return (Pen)GetValue(ColumnRulerPenProperty); }
set { SetValue(ColumnRulerPenProperty, value); }
}
/// <summary>
/// The <see cref="CurrentLineBackground"/> property.
/// </summary>
public static readonly DependencyProperty CurrentLineBackgroundProperty =
DependencyProperty.Register("CurrentLineBackground", typeof(Brush), typeof(TextView));
/// <summary>
/// Gets/Sets the background brush used by current line highlighter.
/// </summary>
public Brush CurrentLineBackground {
get { return (Brush)GetValue(CurrentLineBackgroundProperty); }
set { SetValue(CurrentLineBackgroundProperty, value); }
}
/// <summary>
/// The <see cref="CurrentLineBorder"/> property.
/// </summary>
public static readonly DependencyProperty CurrentLineBorderProperty =
DependencyProperty.Register("CurrentLineBorder", typeof(Pen), typeof(TextView));
/// <summary>
/// Gets/Sets the background brush used for the current line.
/// </summary>
public Pen CurrentLineBorder {
get { return (Pen)GetValue(CurrentLineBorderProperty); }
set { SetValue(CurrentLineBorderProperty, value); }
}
/// <summary>
/// Gets/Sets highlighted line number.
/// </summary>
public int HighlightedLine {
get { return this.currentLineHighlighRenderer.Line; }
set { this.currentLineHighlighRenderer.Line = value; }
}
}
}

Просмотреть файл

@ -430,5 +430,21 @@ namespace ICSharpCode.AvalonEdit
}
}
}
bool highlightCurrentLine = false;
/// <summary>
/// Gets/Sets whether current line should be shown.
/// </summary>
[DefaultValue(false)]
public virtual bool HighlightCurrentLine {
get { return highlightCurrentLine; }
set {
if (highlightCurrentLine != value) {
highlightCurrentLine = value;
OnPropertyChanged("HighlightCurrentLine");
}
}
}
}
}