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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
|
// 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.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.TextFormatting;
using Tango.Scripting.Editors.Editing;
using Tango.Scripting.Editors.Rendering;
using Tango.Scripting.Editors.Utils;
namespace Tango.Scripting.Editors.Folding
{
/// <summary>
/// A margin that shows markers for foldings and allows to expand/collapse the foldings.
/// </summary>
public class FoldingMargin : AbstractMargin
{
/// <summary>
/// Gets/Sets the folding manager from which the foldings should be shown.
/// </summary>
public FoldingManager FoldingManager { get; set; }
internal const double SizeFactor = Constants.PixelPerPoint;
#region Brushes
/// <summary>
/// FoldingMarkerBrush dependency property.
/// </summary>
public static readonly DependencyProperty FoldingMarkerBrushProperty =
DependencyProperty.RegisterAttached("FoldingMarkerBrush", typeof(Brush), typeof(FoldingMargin),
new FrameworkPropertyMetadata(Brushes.Gainsboro));
/// <summary>
/// Gets/sets the Brush used for displaying the lines of folding markers.
/// </summary>
public Brush FoldingMarkerBrush {
get { return (Brush)GetValue(FoldingMarkerBrushProperty); }
set { SetValue(FoldingMarkerBrushProperty, value); }
}
/// <summary>
/// FoldingMarkerBackgroundBrush dependency property.
/// </summary>
public static readonly DependencyProperty FoldingMarkerBackgroundBrushProperty =
DependencyProperty.RegisterAttached("FoldingMarkerBackgroundBrush", typeof(Brush), typeof(FoldingMargin),
new FrameworkPropertyMetadata(Brushes.Transparent));
/// <summary>
/// Gets/sets the Brush used for displaying the background of folding markers.
/// </summary>
public Brush FoldingMarkerBackgroundBrush {
get { return (Brush)GetValue(FoldingMarkerBackgroundBrushProperty); }
set { SetValue(FoldingMarkerBackgroundBrushProperty, value); }
}
/// <summary>
/// SelectedFoldingMarkerBrush dependency property.
/// </summary>
public static readonly DependencyProperty SelectedFoldingMarkerBrushProperty =
DependencyProperty.RegisterAttached("SelectedFoldingMarkerBrush",
typeof(Brush), typeof(FoldingMargin),
new FrameworkPropertyMetadata(Brushes.Gainsboro));
/// <summary>
/// Gets/sets the Brush used for displaying the lines of selected folding markers.
/// </summary>
public Brush SelectedFoldingMarkerBrush {
get { return (Brush)GetValue(SelectedFoldingMarkerBrushProperty); }
set { SetValue(SelectedFoldingMarkerBrushProperty, value); }
}
/// <summary>
/// SelectedFoldingMarkerBackgroundBrush dependency property.
/// </summary>
public static readonly DependencyProperty SelectedFoldingMarkerBackgroundBrushProperty =
DependencyProperty.RegisterAttached("SelectedFoldingMarkerBackgroundBrush",
typeof(Brush), typeof(FoldingMargin),
new FrameworkPropertyMetadata(Brushes.Transparent));
/// <summary>
/// Gets/sets the Brush used for displaying the background of selected folding markers.
/// </summary>
public Brush SelectedFoldingMarkerBackgroundBrush {
get { return (Brush)GetValue(SelectedFoldingMarkerBackgroundBrushProperty); }
set { SetValue(SelectedFoldingMarkerBackgroundBrushProperty, value); }
}
static void OnUpdateBrushes(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
FoldingMargin m = null;
if (d is FoldingMargin)
m = (FoldingMargin)d;
else if (d is TextEditor)
m = ((TextEditor)d).TextArea.LeftMargins.FirstOrDefault(c => c is FoldingMargin) as FoldingMargin;
if (m == null) return;
if (e.Property.Name == FoldingMarkerBrushProperty.Name)
m.foldingControlPen = MakeFrozenPen((Brush)e.NewValue);
if (e.Property.Name == SelectedFoldingMarkerBrushProperty.Name)
m.selectedFoldingControlPen = MakeFrozenPen((Brush)e.NewValue);
}
#endregion
/// <inheritdoc/>
protected override Size MeasureOverride(Size availableSize)
{
foreach (FoldingMarginMarker m in markers) {
m.Measure(availableSize);
}
double width = SizeFactor * (double)GetValue(TextBlock.FontSizeProperty);
return new Size(PixelSnapHelpers.RoundToOdd(width, PixelSnapHelpers.GetPixelSize(this).Width), 0);
}
/// <inheritdoc/>
protected override Size ArrangeOverride(Size finalSize)
{
Size pixelSize = PixelSnapHelpers.GetPixelSize(this);
foreach (FoldingMarginMarker m in markers) {
int visualColumn = m.VisualLine.GetVisualColumn(m.FoldingSection.StartOffset - m.VisualLine.FirstDocumentLine.Offset);
TextLine textLine = m.VisualLine.GetTextLine(visualColumn);
double yPos = m.VisualLine.GetTextLineVisualYPosition(textLine, VisualYPosition.TextMiddle) - TextView.VerticalOffset;
yPos -= m.DesiredSize.Height / 2;
double xPos = (finalSize.Width - m.DesiredSize.Width) / 2;
m.Arrange(new Rect(PixelSnapHelpers.Round(new Point(xPos, yPos), pixelSize), m.DesiredSize));
}
return base.ArrangeOverride(finalSize);
}
/// <inheritdoc/>
protected override void OnTextViewChanged(TextView oldTextView, TextView newTextView)
{
if (oldTextView != null) {
oldTextView.VisualLinesChanged -= TextViewVisualLinesChanged;
}
base.OnTextViewChanged(oldTextView, newTextView);
if (newTextView != null) {
newTextView.VisualLinesChanged += TextViewVisualLinesChanged;
}
TextViewVisualLinesChanged(null, null);
}
List<FoldingMarginMarker> markers = new List<FoldingMarginMarker>();
void TextViewVisualLinesChanged(object sender, EventArgs e)
{
foreach (FoldingMarginMarker m in markers) {
RemoveVisualChild(m);
}
markers.Clear();
InvalidateVisual();
if (TextView != null && FoldingManager != null && TextView.VisualLinesValid) {
foreach (VisualLine line in TextView.VisualLines) {
FoldingSection fs = FoldingManager.GetNextFolding(line.FirstDocumentLine.Offset);
if (fs == null)
continue;
if (fs.StartOffset <= line.LastDocumentLine.Offset + line.LastDocumentLine.Length) {
FoldingMarginMarker m = new FoldingMarginMarker {
IsExpanded = !fs.IsFolded,
VisualLine = line,
FoldingSection = fs
};
markers.Add(m);
AddVisualChild(m);
m.IsMouseDirectlyOverChanged += delegate { InvalidateVisual(); };
InvalidateMeasure();
continue;
}
}
}
}
/// <inheritdoc/>
protected override int VisualChildrenCount {
get { return markers.Count; }
}
/// <inheritdoc/>
protected override Visual GetVisualChild(int index)
{
return markers[index];
}
Pen foldingControlPen = MakeFrozenPen((Brush)FoldingMarkerBrushProperty.DefaultMetadata.DefaultValue);
Pen selectedFoldingControlPen = MakeFrozenPen((Brush)SelectedFoldingMarkerBrushProperty.DefaultMetadata.DefaultValue);
static Pen MakeFrozenPen(Brush brush)
{
Pen pen = new Pen(brush, 1);
pen.Freeze();
return pen;
}
/// <inheritdoc/>
protected override void OnRender(DrawingContext drawingContext)
{
if (TextView == null || !TextView.VisualLinesValid)
return;
if (TextView.VisualLines.Count == 0 || FoldingManager == null)
return;
var allTextLines = TextView.VisualLines.SelectMany(vl => vl.TextLines).ToList();
Pen[] colors = new Pen[allTextLines.Count + 1];
Pen[] endMarker = new Pen[allTextLines.Count];
CalculateFoldLinesForFoldingsActiveAtStart(allTextLines, colors, endMarker);
CalculateFoldLinesForMarkers(allTextLines, colors, endMarker);
DrawFoldLines(drawingContext, colors, endMarker);
base.OnRender(drawingContext);
}
/// <summary>
/// Calculates fold lines for all folding sections that start in front of the current view
/// and run into the current view.
/// </summary>
void CalculateFoldLinesForFoldingsActiveAtStart(List<TextLine> allTextLines, Pen[] colors, Pen[] endMarker)
{
int viewStartOffset = TextView.VisualLines[0].FirstDocumentLine.Offset;
int viewEndOffset = TextView.VisualLines.Last().LastDocumentLine.EndOffset;
var foldings = FoldingManager.GetFoldingsContaining(viewStartOffset);
int maxEndOffset = 0;
foreach (FoldingSection fs in foldings) {
int end = fs.EndOffset;
if (end <= viewEndOffset && !fs.IsFolded) {
int textLineNr = GetTextLineIndexFromOffset(allTextLines, end);
if (textLineNr >= 0) {
endMarker[textLineNr] = foldingControlPen;
}
}
if (end > maxEndOffset && fs.StartOffset < viewStartOffset) {
maxEndOffset = end;
}
}
if (maxEndOffset > 0) {
if (maxEndOffset > viewEndOffset) {
for (int i = 0; i < colors.Length; i++) {
colors[i] = foldingControlPen;
}
} else {
int maxTextLine = GetTextLineIndexFromOffset(allTextLines, maxEndOffset);
for (int i = 0; i <= maxTextLine; i++) {
colors[i] = foldingControlPen;
}
}
}
}
/// <summary>
/// Calculates fold lines for all folding sections that start inside the current view
/// </summary>
void CalculateFoldLinesForMarkers(List<TextLine> allTextLines, Pen[] colors, Pen[] endMarker)
{
foreach (FoldingMarginMarker marker in markers) {
int end = marker.FoldingSection.EndOffset;
int endTextLineNr = GetTextLineIndexFromOffset(allTextLines, end);
if (!marker.FoldingSection.IsFolded && endTextLineNr >= 0) {
if (marker.IsMouseDirectlyOver)
endMarker[endTextLineNr] = selectedFoldingControlPen;
else if (endMarker[endTextLineNr] == null)
endMarker[endTextLineNr] = foldingControlPen;
}
int startTextLineNr = GetTextLineIndexFromOffset(allTextLines, marker.FoldingSection.StartOffset);
if (startTextLineNr >= 0) {
for (int i = startTextLineNr + 1; i < colors.Length && i - 1 != endTextLineNr; i++) {
if (marker.IsMouseDirectlyOver)
colors[i] = selectedFoldingControlPen;
else if (colors[i] == null)
colors[i] = foldingControlPen;
}
}
}
}
/// <summary>
/// Draws the lines for the folding sections (vertical line with 'color', horizontal lines with 'endMarker')
/// Each entry in the input arrays corresponds to one TextLine.
/// </summary>
void DrawFoldLines(DrawingContext drawingContext, Pen[] colors, Pen[] endMarker)
{
// Because we are using PenLineCap.Flat (the default), for vertical lines,
// Y coordinates must be on pixel boundaries, whereas the X coordinate must be in the
// middle of a pixel. (and the other way round for horizontal lines)
Size pixelSize = PixelSnapHelpers.GetPixelSize(this);
double markerXPos = PixelSnapHelpers.PixelAlign(RenderSize.Width / 2, pixelSize.Width);
double startY = 0;
Pen currentPen = colors[0];
Pen p = new Pen(FoldingMarkerBrush, 1);
int tlNumber = 0;
foreach (VisualLine vl in TextView.VisualLines) {
foreach (TextLine tl in vl.TextLines) {
if (endMarker[tlNumber] != null) {
double visualPos = GetVisualPos(vl, tl, pixelSize.Height);
drawingContext.DrawLine(p, new Point(markerXPos - pixelSize.Width / 2, visualPos), new Point(RenderSize.Width - 4, visualPos));
}
if (colors[tlNumber + 1] != currentPen) {
double visualPos = GetVisualPos(vl, tl, pixelSize.Height);
if (currentPen != null) {
drawingContext.DrawLine(p, new Point(markerXPos, startY + pixelSize.Height), new Point(markerXPos, visualPos - pixelSize.Height / 2));
}
currentPen = colors[tlNumber + 1];
startY = visualPos;
}
tlNumber++;
}
}
if (currentPen != null) {
drawingContext.DrawLine(currentPen, new Point(markerXPos, startY + pixelSize.Height / 2), new Point(markerXPos, RenderSize.Height));
}
}
double GetVisualPos(VisualLine vl, TextLine tl, double pixelHeight)
{
double pos = vl.GetTextLineVisualYPosition(tl, VisualYPosition.TextMiddle) - TextView.VerticalOffset;
return PixelSnapHelpers.PixelAlign(pos, pixelHeight);
}
int GetTextLineIndexFromOffset(List<TextLine> textLines, int offset)
{
int lineNumber = TextView.Document.GetLineByOffset(offset).LineNumber;
VisualLine vl = TextView.GetVisualLine(lineNumber);
if (vl != null) {
int relOffset = offset - vl.FirstDocumentLine.Offset;
TextLine line = vl.GetTextLine(vl.GetVisualColumn(relOffset));
return textLines.IndexOf(line);
}
return -1;
}
static FoldingMargin()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(FoldingMargin), new FrameworkPropertyMetadata(typeof(FoldingMargin)));
}
}
}
|