Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Editing/AbstractMargin.cs @ 17246

Last change on this file since 17246 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 4.2 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Diagnostics;
21using System.Windows;
22
23using ICSharpCode.AvalonEdit.Document;
24using ICSharpCode.AvalonEdit.Rendering;
25
26namespace ICSharpCode.AvalonEdit.Editing
27{
28  /// <summary>
29  /// Base class for margins.
30  /// Margins don't have to derive from this class, it just helps maintaining a reference to the TextView
31  /// and the TextDocument.
32  /// AbstractMargin derives from FrameworkElement, so if you don't want to handle visual children and rendering
33  /// on your own, choose another base class for your margin!
34  /// </summary>
35  public abstract class AbstractMargin : FrameworkElement, ITextViewConnect
36  {
37    /// <summary>
38    /// TextView property.
39    /// </summary>
40    public static readonly DependencyProperty TextViewProperty =
41      DependencyProperty.Register("TextView", typeof(TextView), typeof(AbstractMargin),
42                                  new FrameworkPropertyMetadata(OnTextViewChanged));
43   
44    /// <summary>
45    /// Gets/sets the text view for which line numbers are displayed.
46    /// </summary>
47    /// <remarks>Adding a margin to <see cref="TextArea.LeftMargins"/> will automatically set this property to the text area's TextView.</remarks>
48    public TextView TextView {
49      get { return (TextView)GetValue(TextViewProperty); }
50      set { SetValue(TextViewProperty, value); }
51    }
52   
53    static void OnTextViewChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
54    {
55      AbstractMargin margin = (AbstractMargin)dp;
56      margin.wasAutoAddedToTextView = false;
57      margin.OnTextViewChanged((TextView)e.OldValue, (TextView)e.NewValue);
58    }
59   
60    // automatically set/unset TextView property using ITextViewConnect
61    bool wasAutoAddedToTextView;
62   
63    void ITextViewConnect.AddToTextView(TextView textView)
64    {
65      if (this.TextView == null) {
66        this.TextView = textView;
67        wasAutoAddedToTextView = true;
68      } else if (this.TextView != textView) {
69        throw new InvalidOperationException("This margin belongs to a different TextView.");
70      }
71    }
72   
73    void ITextViewConnect.RemoveFromTextView(TextView textView)
74    {
75      if (wasAutoAddedToTextView && this.TextView == textView) {
76        this.TextView = null;
77        Debug.Assert(!wasAutoAddedToTextView); // setting this.TextView should have unset this flag
78      }
79    }
80   
81    TextDocument document;
82   
83    /// <summary>
84    /// Gets the document associated with the margin.
85    /// </summary>
86    public TextDocument Document {
87      get { return document; }
88    }
89   
90    /// <summary>
91    /// Called when the <see cref="TextView"/> is changing.
92    /// </summary>
93    protected virtual void OnTextViewChanged(TextView oldTextView, TextView newTextView)
94    {
95      if (oldTextView != null) {
96        oldTextView.DocumentChanged -= TextViewDocumentChanged;
97      }
98      if (newTextView != null) {
99        newTextView.DocumentChanged += TextViewDocumentChanged;
100      }
101      TextViewDocumentChanged(null, null);
102    }
103   
104    void TextViewDocumentChanged(object sender, EventArgs e)
105    {
106      OnDocumentChanged(document, TextView != null ? TextView.Document : null);
107    }
108   
109    /// <summary>
110    /// Called when the <see cref="Document"/> is changing.
111    /// </summary>
112    protected virtual void OnDocumentChanged(TextDocument oldDocument, TextDocument newDocument)
113    {
114      document = newDocument;
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.