1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
|
public class Couple
{
public int Start { get; set; }
public int End { get; set; }
}
///<summary>
///TextAdornment1 places red boxes behind all the "A"s in the editor window
///</summary>
public class TextAdornment1
{
int iBrush = 0;
IAdornmentLayer _layer;
IWpfTextView _view;
Brush _brush;
List<Pen> _pens = new List<Pen>();
List<Couple> _tableau = new List<Couple>();
int _lastCouple;
public TextAdornment1(IWpfTextView view)
{
_view = view;
_layer = view.GetAdornmentLayer("TextAdornment1");
//Listen to any event that changes the layout (text changes, scrolling, etc)
_view.LayoutChanged += OnLayoutChanged;
Brush brush = new SolidColorBrush(Colors.Red);
brush.Freeze();
Brush penBrush;
Pen pen;
penBrush = new SolidColorBrush(Colors.Red);
penBrush.Freeze();
pen = new Pen(penBrush, 0.5);
pen.Freeze();
_pens.Add(pen);
penBrush = new SolidColorBrush(Colors.Orange);
penBrush.Freeze();
pen = new Pen(penBrush, 0.5);
pen.Freeze();
_pens.Add(pen);
penBrush = new SolidColorBrush(Colors.Green);
penBrush.Freeze();
pen = new Pen(penBrush, 0.5);
pen.Freeze();
_pens.Add(pen);
penBrush = new SolidColorBrush(Colors.Aqua);
penBrush.Freeze();
pen = new Pen(penBrush, 0.5);
pen.Freeze();
_pens.Add(pen);
penBrush = new SolidColorBrush(Colors.Blue);
penBrush.Freeze();
pen = new Pen(penBrush, 0.5);
pen.Freeze();
_pens.Add(pen);
penBrush = new SolidColorBrush(Colors.Purple);
penBrush.Freeze();
pen = new Pen(penBrush, 0.5);
pen.Freeze();
_pens.Add(pen);
}
/// <summary>
/// On layout change add the adornment to any reformatted lines
/// </summary>
private void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
foreach (ITextViewLine line in e.NewOrReformattedLines)
{
SelectBraces(line);
}
Colorize();
}
/// <summary>
/// Within the given line add the scarlet box behind the a
/// </summary>
private void CreateVisuals(ITextViewLine line)
{
//grab a reference to the lines in the current TextView
IWpfTextViewLineCollection textViewLines = _view.TextViewLines;
int start = line.Start;
int end = line.End;
//Loop through each character, and place a box around any a
for (int i = start; (i < end); ++i)
{
if ((_view.TextSnapshot[i] == '{') || (_view.TextSnapshot[i] == '}'))
{
SnapshotSpan span = new SnapshotSpan(_view.TextSnapshot, Span.FromBounds(i, i + 1));
Geometry g = textViewLines.GetMarkerGeometry(span);
if (g != null)
{
Pen currentPen;
currentPen = _pens[iBrush];
iBrush++;
if (iBrush > 5) iBrush = 0;
GeometryDrawing drawing = new GeometryDrawing(_brush, currentPen, g);
drawing.Freeze();
DrawingImage drawingImage = new DrawingImage(drawing);
drawingImage.Freeze();
Image image = new Image();
image.Source = drawingImage;
//Align the image with the top of the bounds of the text geometry
Canvas.SetLeft(image, g.Bounds.Left);
Canvas.SetTop(image, g.Bounds.Top);
_layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, image, null);
}
}
}
}
void SelectBraces(ITextViewLine line)
{
int start = line.Start;
int end = line.End;
for (int i = start; (i < end); ++i)
{
if ((_view.TextSnapshot[i] == '{') || (_view.TextSnapshot[i] == '}'))
{
if (_view.TextSnapshot[i] == '{')
{
Couple couple = new Couple();
couple.Start = i;
couple.End = i;
_tableau.Add(couple);
}
else
{
Couple couple = _tableau[_tableau.Count - 1];
couple.End = i;
}
}
}
}
void Colorize()
{
IWpfTextViewLineCollection textViewLines = _view.TextViewLines;
Pen currentPen;
for (int i = 0; i < _tableau.Count; i++)
{
currentPen = _pens[iBrush];
iBrush++;
if (iBrush > 5) iBrush = 0;
Couple couple = _tableau[i];
SnapshotSpan spanStart = new SnapshotSpan(_view.TextSnapshot, Span.FromBounds(couple.Start, couple.Start + 1));
Geometry gStart = textViewLines.GetMarkerGeometry(spanStart);
if (gStart != null)
{
GeometryDrawing drawing = new GeometryDrawing(_brush, currentPen, gStart);
drawing.Freeze();
DrawingImage drawingImage = new DrawingImage(drawing);
drawingImage.Freeze();
Image image = new Image();
image.Source = drawingImage;
//Align the image with the top of the bounds of the text geometry
Canvas.SetLeft(image, gStart.Bounds.Left);
Canvas.SetTop(image, gStart.Bounds.Top);
_layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, spanStart, null, image, null);
}
SnapshotSpan spanEnd = new SnapshotSpan(_view.TextSnapshot, Span.FromBounds(couple.End, couple.End + 1));
Geometry gEnd = textViewLines.GetMarkerGeometry(spanEnd);
if (gEnd != null)
{
GeometryDrawing drawing = new GeometryDrawing(_brush, currentPen, gEnd);
drawing.Freeze();
DrawingImage drawingImage = new DrawingImage(drawing);
drawingImage.Freeze();
Image image = new Image();
image.Source = drawingImage;
//Align the image with the top of the bounds of the text geometry
Canvas.SetLeft(image, gEnd.Bounds.Left);
Canvas.SetTop(image, gEnd.Bounds.Top);
_layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, spanEnd, null, image, null);
}
}
}
} |
Partager