Browse Source

Implement INotifyPropertyChanged in TextDocument.

4.0
Daniel Grunwald 15 years ago
parent
commit
941739469e
  1. 25
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs
  2. 2
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocumentWeakEventManager.cs
  3. 7
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/LineNumberMargin.cs

25
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs

@ -4,6 +4,7 @@ @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Globalization;
@ -21,7 +22,7 @@ namespace ICSharpCode.AvalonEdit.Document @@ -21,7 +22,7 @@ namespace ICSharpCode.AvalonEdit.Document
/// <inheritdoc cref="VerifyAccess"/>
/// <para>However, there is a single method that is thread-safe: <see cref="CreateSnapshot()"/> (and its overloads).</para>
/// </remarks>
public sealed class TextDocument : ITextSource
public sealed class TextDocument : ITextSource, INotifyPropertyChanged
{
#region Thread ownership
readonly object lockObject = new object();
@ -207,8 +208,16 @@ namespace ICSharpCode.AvalonEdit.Document @@ -207,8 +208,16 @@ namespace ICSharpCode.AvalonEdit.Document
/// Is raised when the TextLength property changes.
/// </summary>
/// <remarks><inheritdoc cref="Changing"/></remarks>
[Obsolete("This event will be removed in a future version; use the PropertyChanged event instead")]
public event EventHandler TextLengthChanged;
/// <summary>
/// Is raised when one of the properties <see cref="Text"/>, <see cref="TextLength"/>, <see cref="LineCount"/>,
/// <see cref="UndoStack"/> changes.
/// </summary>
/// <remarks><inheritdoc cref="Changing"/></remarks>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Is raised before the document changes.
/// </summary>
@ -231,8 +240,7 @@ namespace ICSharpCode.AvalonEdit.Document @@ -231,8 +240,7 @@ namespace ICSharpCode.AvalonEdit.Document
/// <item><description><b><see cref="EndUpdate">EndUpdate()</see></b></description>
/// <list type="bullet">
/// <item><description><see cref="TextChanged"/> event is raised</description></item>
/// <item><description><see cref="TextLengthChanged"/> event is raised</description></item>
/// <item><description><see cref="LineCountChanged"/> event is raised</description></item>
/// <item><description><see cref="PropertyChanged"/> event is raised (for the Text, TextLength, LineCount properties, in that order)</description></item>
/// <item><description>End of change group (on undo stack)</description></item>
/// <item><description><see cref="UpdateFinished"/> event is raised</description></item>
/// </list></item>
@ -418,21 +426,30 @@ namespace ICSharpCode.AvalonEdit.Document @@ -418,21 +426,30 @@ namespace ICSharpCode.AvalonEdit.Document
fireTextChanged = false;
if (TextChanged != null)
TextChanged(this, EventArgs.Empty);
OnPropertyChanged("Text");
int textLength = rope.Length;
if (textLength != oldTextLength) {
oldTextLength = textLength;
if (TextLengthChanged != null)
TextLengthChanged(this, EventArgs.Empty);
OnPropertyChanged("TextLength");
}
int lineCount = lineTree.LineCount;
if (lineCount != oldLineCount) {
oldLineCount = lineCount;
if (LineCountChanged != null)
LineCountChanged(this, EventArgs.Empty);
OnPropertyChanged("LineCount");
}
}
}
void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
#region Insert / Remove / Replace
@ -743,6 +760,7 @@ namespace ICSharpCode.AvalonEdit.Document @@ -743,6 +760,7 @@ namespace ICSharpCode.AvalonEdit.Document
undoStack.ClearAll(); // first clear old undo stack, so that it can't be used to perform unexpected changes on this document
// ClearAll() will also throw an exception when it's not safe to replace the undo stack (e.g. update is currently in progress)
undoStack = value;
OnPropertyChanged("UndoStack");
}
}
}
@ -775,6 +793,7 @@ namespace ICSharpCode.AvalonEdit.Document @@ -775,6 +793,7 @@ namespace ICSharpCode.AvalonEdit.Document
/// <summary>
/// Is raised when the LineCount property changes.
/// </summary>
[Obsolete("This event will be removed in a future version; use the PropertyChanged event instead")]
public event EventHandler LineCountChanged;
#endregion

2
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocumentWeakEventManager.cs

@ -91,6 +91,7 @@ namespace ICSharpCode.AvalonEdit.Document @@ -91,6 +91,7 @@ namespace ICSharpCode.AvalonEdit.Document
/// Weak event manager for the <see cref="TextDocument.LineCountChanged"/> event.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
[Obsolete("The TextDocument.LineCountChanged event will be removed in a future version. Use PropertyChangedEventManager instead.")]
public sealed class LineCountChanged : WeakEventManagerBase<LineCountChanged, TextDocument>
{
/// <inheritdoc/>
@ -110,6 +111,7 @@ namespace ICSharpCode.AvalonEdit.Document @@ -110,6 +111,7 @@ namespace ICSharpCode.AvalonEdit.Document
/// Weak event manager for the <see cref="TextDocument.TextLengthChanged"/> event.
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
[Obsolete("The TextDocument.TextLengthChanged event will be removed in a future version. Use PropertyChangedEventManager instead.")]
public sealed class TextLengthChanged : WeakEventManagerBase<TextLengthChanged, TextDocument>
{
/// <inheritdoc/>

7
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/LineNumberMargin.cs

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
@ -89,11 +90,11 @@ namespace ICSharpCode.AvalonEdit.Editing @@ -89,11 +90,11 @@ namespace ICSharpCode.AvalonEdit.Editing
protected override void OnDocumentChanged(TextDocument oldDocument, TextDocument newDocument)
{
if (oldDocument != null) {
TextDocumentWeakEventManager.LineCountChanged.RemoveListener(oldDocument, this);
PropertyChangedEventManager.RemoveListener(oldDocument, this, "LineCount");
}
base.OnDocumentChanged(oldDocument, newDocument);
if (newDocument != null) {
TextDocumentWeakEventManager.LineCountChanged.AddListener(newDocument, this);
PropertyChangedEventManager.AddListener(oldDocument, this, "LineCount");
}
OnDocumentLineCountChanged();
}
@ -101,7 +102,7 @@ namespace ICSharpCode.AvalonEdit.Editing @@ -101,7 +102,7 @@ namespace ICSharpCode.AvalonEdit.Editing
/// <inheritdoc cref="IWeakEventListener.ReceiveWeakEvent"/>
protected virtual bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e)
{
if (managerType == typeof(TextDocumentWeakEventManager.LineCountChanged)) {
if (managerType == typeof(PropertyChangedEventManager)) {
OnDocumentLineCountChanged();
return true;
}

Loading…
Cancel
Save