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

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

@ -91,6 +91,7 @@ namespace ICSharpCode.AvalonEdit.Document
/// Weak event manager for the <see cref="TextDocument.LineCountChanged"/> event. /// Weak event manager for the <see cref="TextDocument.LineCountChanged"/> event.
/// </summary> /// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] [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> public sealed class LineCountChanged : WeakEventManagerBase<LineCountChanged, TextDocument>
{ {
/// <inheritdoc/> /// <inheritdoc/>
@ -110,6 +111,7 @@ namespace ICSharpCode.AvalonEdit.Document
/// Weak event manager for the <see cref="TextDocument.TextLengthChanged"/> event. /// Weak event manager for the <see cref="TextDocument.TextLengthChanged"/> event.
/// </summary> /// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] [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> public sealed class TextLengthChanged : WeakEventManagerBase<TextLengthChanged, TextDocument>
{ {
/// <inheritdoc/> /// <inheritdoc/>

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

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

Loading…
Cancel
Save