Browse Source

AvalonEdit: when creating a new TextEditor instance, create a new TextDocument. Fixed bugs when setting Document to null.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@3930 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 17 years ago
parent
commit
407f3328e8
  1. 8
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Gui/TextArea.cs
  2. 23
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Gui/TextEditor.cs
  3. 47
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Gui/TextView.cs
  4. 5
      src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Utils/ThrowUtil.cs

8
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Gui/TextArea.cs

@ -632,6 +632,8 @@ namespace ICSharpCode.AvalonEdit @@ -632,6 +632,8 @@ namespace ICSharpCode.AvalonEdit
internal void RemoveSelectedText()
{
if (this.Document == null)
throw ThrowUtil.NoDocumentAssigned();
selection.RemoveSelectedText(this);
#if DEBUG
if (!selection.IsEmpty) {
@ -646,11 +648,13 @@ namespace ICSharpCode.AvalonEdit @@ -646,11 +648,13 @@ namespace ICSharpCode.AvalonEdit
{
if (newText == null)
throw new ArgumentNullException("newText");
using (Document.RunUpdate()) {
if (this.Document == null)
throw ThrowUtil.NoDocumentAssigned();
using (this.Document.RunUpdate()) {
RemoveSelectedText();
if (newText.Length > 0) {
if (ReadOnlySectionProvider.CanInsert(Caret.Offset)) {
Document.Insert(Caret.Offset, newText);
this.Document.Insert(Caret.Offset, newText);
}
}
}

23
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Gui/TextEditor.cs

@ -43,7 +43,9 @@ namespace ICSharpCode.AvalonEdit @@ -43,7 +43,9 @@ namespace ICSharpCode.AvalonEdit
/// <summary>
/// Creates a new TextEditor instance.
/// </summary>
public TextEditor() : this(new TextArea()) {}
public TextEditor() : this(new TextArea())
{
}
// Forward focus to TextArea.
/// <inheritdoc/>
@ -65,6 +67,7 @@ namespace ICSharpCode.AvalonEdit @@ -65,6 +67,7 @@ namespace ICSharpCode.AvalonEdit
throw new ArgumentNullException("textArea");
this.textArea = textArea;
this.Options = textArea.Options;
this.Document = new TextDocument();
textArea.SetBinding(TextArea.DocumentProperty, new Binding("Document") { Source = this });
textArea.SetBinding(TextArea.OptionsProperty, new Binding("Options") { Source = this });
}
@ -184,7 +187,7 @@ namespace ICSharpCode.AvalonEdit @@ -184,7 +187,7 @@ namespace ICSharpCode.AvalonEdit
set {
if (value == null)
value = string.Empty;
TextDocument document = GetOrCreateDocument();
TextDocument document = GetDocument();
document.Text = value;
// after replacing the full text, the caret is positioned at the end of the document
// - reset it to the beginning.
@ -193,13 +196,11 @@ namespace ICSharpCode.AvalonEdit @@ -193,13 +196,11 @@ namespace ICSharpCode.AvalonEdit
}
}
TextDocument GetOrCreateDocument()
TextDocument GetDocument()
{
TextDocument document = this.Document;
if (document == null) {
document = new TextDocument();
this.Document = document;
}
if (document == null)
throw ThrowUtil.NoDocumentAssigned();
return document;
}
@ -300,7 +301,7 @@ namespace ICSharpCode.AvalonEdit @@ -300,7 +301,7 @@ namespace ICSharpCode.AvalonEdit
/// </summary>
public void AppendText(string textData)
{
TextDocument document = GetOrCreateDocument();
var document = GetDocument();
document.Insert(document.TextLength, textData);
}
@ -309,7 +310,7 @@ namespace ICSharpCode.AvalonEdit @@ -309,7 +310,7 @@ namespace ICSharpCode.AvalonEdit
/// </summary>
public void BeginChange()
{
GetOrCreateDocument().BeginUpdate();
GetDocument().BeginUpdate();
}
/// <summary>
@ -334,7 +335,7 @@ namespace ICSharpCode.AvalonEdit @@ -334,7 +335,7 @@ namespace ICSharpCode.AvalonEdit
/// </summary>
public IDisposable DeclareChangeBlock()
{
return GetOrCreateDocument().RunUpdate();
return GetDocument().RunUpdate();
}
/// <summary>
@ -342,7 +343,7 @@ namespace ICSharpCode.AvalonEdit @@ -342,7 +343,7 @@ namespace ICSharpCode.AvalonEdit
/// </summary>
public void EndChange()
{
GetOrCreateDocument().EndUpdate();
GetDocument().EndUpdate();
}
/// <summary>

47
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Gui/TextView.cs

@ -562,28 +562,34 @@ namespace ICSharpCode.AvalonEdit.Gui @@ -562,28 +562,34 @@ namespace ICSharpCode.AvalonEdit.Gui
InvalidateVisual(); // = InvalidateArrange+InvalidateRender
textLayer.InvalidateVisual();
if (document == null)
return Size.Empty;
double maxWidth;
inMeasure = true;
try {
maxWidth = CreateAndMeasureVisualLines(availableSize);
} finally {
inMeasure = false;
if (document == null) {
// no document -> create empty list of lines
allVisualLines = new List<VisualLine>();
visibleVisualLines = allVisualLines.AsReadOnly();
maxWidth = 0;
} else {
inMeasure = true;
try {
maxWidth = CreateAndMeasureVisualLines(availableSize);
} finally {
inMeasure = false;
}
}
textLayer.RemoveInlineObjectsNow();
double heightTreeHeight = this.DocumentHeight;
SetScrollData(availableSize,
new Size(maxWidth, heightTree.TotalHeight),
new Size(maxWidth, heightTreeHeight),
scrollOffset);
if (VisualLinesChanged != null)
VisualLinesChanged(this, EventArgs.Empty);
return new Size(
canHorizontallyScroll ? Math.Min(availableSize.Width, maxWidth) : maxWidth,
canVerticallyScroll ? Math.Min(availableSize.Height, heightTree.TotalHeight) : heightTree.TotalHeight
canVerticallyScroll ? Math.Min(availableSize.Height, heightTreeHeight) : heightTreeHeight
);
}
@ -734,6 +740,10 @@ namespace ICSharpCode.AvalonEdit.Gui @@ -734,6 +740,10 @@ namespace ICSharpCode.AvalonEdit.Gui
/// </summary>
protected override Size ArrangeOverride(Size finalSize)
{
foreach (UIElement layer in layers) {
layer.Arrange(new Rect(new Point(0, 0), finalSize));
}
if (document == null || allVisualLines.Count == 0)
return finalSize;
@ -752,10 +762,6 @@ namespace ICSharpCode.AvalonEdit.Gui @@ -752,10 +762,6 @@ namespace ICSharpCode.AvalonEdit.Gui
// double maxWidth = 0;
foreach (UIElement adorner in layers) {
adorner.Arrange(new Rect(new Point(0, 0), finalSize));
}
if (visibleVisualLines != null) {
Point pos = new Point(-scrollOffset.X, -clippedPixelsOnTop);
foreach (VisualLine visualLine in visibleVisualLines) {
@ -1222,7 +1228,7 @@ namespace ICSharpCode.AvalonEdit.Gui @@ -1222,7 +1228,7 @@ namespace ICSharpCode.AvalonEdit.Gui
{
VerifyAccess();
if (this.Document == null)
throw new InvalidOperationException("There is no document assigned to the TextView");
throw ThrowUtil.NoDocumentAssigned();
DocumentLine documentLine = this.Document.GetLineByNumber(position.Line);
VisualLine visualLine = GetOrConstructVisualLine(documentLine);
int visualColumn = position.VisualColumn;
@ -1243,7 +1249,7 @@ namespace ICSharpCode.AvalonEdit.Gui @@ -1243,7 +1249,7 @@ namespace ICSharpCode.AvalonEdit.Gui
{
VerifyAccess();
if (this.Document == null)
throw new InvalidOperationException("There is no document assigned to the TextView");
throw ThrowUtil.NoDocumentAssigned();
VisualLine line = GetVisualLineFromVisualTop(visualPosition.Y);
if (line == null)
return null;
@ -1425,6 +1431,8 @@ namespace ICSharpCode.AvalonEdit.Gui @@ -1425,6 +1431,8 @@ namespace ICSharpCode.AvalonEdit.Gui
public CollapsedLineSection CollapseLines(DocumentLine start, DocumentLine end)
{
VerifyAccess();
if (heightTree == null)
throw ThrowUtil.NoDocumentAssigned();
return heightTree.CollapseText(start, end);
}
@ -1432,7 +1440,10 @@ namespace ICSharpCode.AvalonEdit.Gui @@ -1432,7 +1440,10 @@ namespace ICSharpCode.AvalonEdit.Gui
/// Gets the height of the document.
/// </summary>
public double DocumentHeight {
get { return heightTree.TotalHeight; }
get {
// return 0 if there is no document = no heightTree
return heightTree != null ? heightTree.TotalHeight : 0;
}
}
/// <summary>
@ -1442,7 +1453,7 @@ namespace ICSharpCode.AvalonEdit.Gui @@ -1442,7 +1453,7 @@ namespace ICSharpCode.AvalonEdit.Gui
{
VerifyAccess();
if (heightTree == null)
throw new InvalidOperationException();
throw ThrowUtil.NoDocumentAssigned();
return heightTree.GetLineByVisualPosition(visualTop);
}
}

5
src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Utils/ThrowUtil.cs

@ -31,5 +31,10 @@ namespace ICSharpCode.AvalonEdit.Utils @@ -31,5 +31,10 @@ namespace ICSharpCode.AvalonEdit.Utils
throw new ArgumentNullException(parameterName);
return val;
}
public static InvalidOperationException NoDocumentAssigned()
{
throw new InvalidOperationException("Document is null");
}
}
}

Loading…
Cancel
Save