@ -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