~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/AbstractMargin.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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)
 
3
 
 
4
using System;
 
5
using System.Diagnostics;
 
6
using System.Windows;
 
7
 
 
8
using ICSharpCode.AvalonEdit.Document;
 
9
using ICSharpCode.AvalonEdit.Rendering;
 
10
 
 
11
namespace ICSharpCode.AvalonEdit.Editing
 
12
{
 
13
        /// <summary>
 
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!
 
19
        /// </summary>
 
20
        public abstract class AbstractMargin : FrameworkElement, ITextViewConnect
 
21
        {
 
22
                /// <summary>
 
23
                /// TextView property.
 
24
                /// </summary>
 
25
                public static readonly DependencyProperty TextViewProperty =
 
26
                        DependencyProperty.Register("TextView", typeof(TextView), typeof(AbstractMargin),
 
27
                                                    new FrameworkPropertyMetadata(OnTextViewChanged));
 
28
                
 
29
                /// <summary>
 
30
                /// Gets/sets the text view for which line numbers are displayed.
 
31
                /// </summary>
 
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); }
 
36
                }
 
37
                
 
38
                static void OnTextViewChanged(DependencyObject dp, DependencyPropertyChangedEventArgs e)
 
39
                {
 
40
                        AbstractMargin margin = (AbstractMargin)dp;
 
41
                        margin.wasAutoAddedToTextView = false;
 
42
                        margin.OnTextViewChanged((TextView)e.OldValue, (TextView)e.NewValue);
 
43
                }
 
44
                
 
45
                // automatically set/unset TextView property using ITextViewConnect
 
46
                bool wasAutoAddedToTextView;
 
47
                
 
48
                void ITextViewConnect.AddToTextView(TextView textView)
 
49
                {
 
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.");
 
55
                        }
 
56
                }
 
57
                
 
58
                void ITextViewConnect.RemoveFromTextView(TextView textView)
 
59
                {
 
60
                        if (wasAutoAddedToTextView && this.TextView == textView) {
 
61
                                this.TextView = null;
 
62
                                Debug.Assert(!wasAutoAddedToTextView); // setting this.TextView should have unset this flag
 
63
                        }
 
64
                }
 
65
                
 
66
                TextDocument document;
 
67
                
 
68
                /// <summary>
 
69
                /// Gets the document associated with the margin.
 
70
                /// </summary>
 
71
                public TextDocument Document {
 
72
                        get { return document; }
 
73
                }
 
74
                
 
75
                /// <summary>
 
76
                /// Called when the <see cref="TextView"/> is changing.
 
77
                /// </summary>
 
78
                protected virtual void OnTextViewChanged(TextView oldTextView, TextView newTextView)
 
79
                {
 
80
                        if (oldTextView != null) {
 
81
                                oldTextView.DocumentChanged -= TextViewDocumentChanged;
 
82
                        }
 
83
                        if (newTextView != null) {
 
84
                                newTextView.DocumentChanged += TextViewDocumentChanged;
 
85
                        }
 
86
                        TextViewDocumentChanged(null, null);
 
87
                }
 
88
                
 
89
                void TextViewDocumentChanged(object sender, EventArgs e)
 
90
                {
 
91
                        OnDocumentChanged(document, TextView != null ? TextView.Document : null);
 
92
                }
 
93
                
 
94
                /// <summary>
 
95
                /// Called when the <see cref="Document"/> is changing.
 
96
                /// </summary>
 
97
                protected virtual void OnDocumentChanged(TextDocument oldDocument, TextDocument newDocument)
 
98
                {
 
99
                        document = newDocument;
 
100
                }
 
101
        }
 
102
}