1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
5
using System.Diagnostics;
8
using ICSharpCode.AvalonEdit.Document;
9
using ICSharpCode.AvalonEdit.Rendering;
11
namespace ICSharpCode.AvalonEdit.Editing
14
/// Base class for margins.
15
/// Margins don't have to derive from this class, it just helps maintaining a reference to the TextView
16
/// and the TextDocument.
17
/// AbstractMargin derives from FrameworkElement, so if you don't want to handle visual children and rendering
18
/// on your own, choose another base class for your margin!
20
public abstract class AbstractMargin : FrameworkElement, ITextViewConnect
23
/// TextView property.
25
public static readonly DependencyProperty TextViewProperty =
26
DependencyProperty.Register("TextView", typeof(TextView), typeof(AbstractMargin),
27
new FrameworkPropertyMetadata(OnTextViewChanged));
30
/// Gets/sets the text view for which line numbers are displayed.
32
/// <remarks>Adding a margin to <see cref="TextArea.LeftMargins"/> will automatically set this property to the text area's TextView.</remarks>
33
public TextView TextView {
34
get { return (TextView)GetValue(TextViewProperty); }
35
set { SetValue(TextViewProperty, value); }
38
static void OnTextViewChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
40
AbstractMargin margin = (AbstractMargin)dp;
41
margin.wasAutoAddedToTextView = false;
42
margin.OnTextViewChanged((TextView)e.OldValue, (TextView)e.NewValue);
45
// automatically set/unset TextView property using ITextViewConnect
46
bool wasAutoAddedToTextView;
48
void ITextViewConnect.AddToTextView(TextView textView)
50
if (this.TextView == null) {
51
this.TextView = textView;
52
wasAutoAddedToTextView = true;
53
} else if (this.TextView != textView) {
54
throw new InvalidOperationException("This margin belongs to a different TextView.");
58
void ITextViewConnect.RemoveFromTextView(TextView textView)
60
if (wasAutoAddedToTextView && this.TextView == textView) {
62
Debug.Assert(!wasAutoAddedToTextView); // setting this.TextView should have unset this flag
66
TextDocument document;
69
/// Gets the document associated with the margin.
71
public TextDocument Document {
72
get { return document; }
76
/// Called when the <see cref="TextView"/> is changing.
78
protected virtual void OnTextViewChanged(TextView oldTextView, TextView newTextView)
80
if (oldTextView != null) {
81
oldTextView.DocumentChanged -= TextViewDocumentChanged;
83
if (newTextView != null) {
84
newTextView.DocumentChanged += TextViewDocumentChanged;
86
TextViewDocumentChanged(null, null);
89
void TextViewDocumentChanged(object sender, EventArgs e)
91
OnDocumentChanged(document, TextView != null ? TextView.Document : null);
95
/// Called when the <see cref="Document"/> is changing.
97
protected virtual void OnDocumentChanged(TextDocument oldDocument, TextDocument newDocument)
99
document = newDocument;