// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.IO;
using System.Text;
namespace ICSharpCode.AvalonEdit.Utils
{
///
/// RichTextWriter implementation that writes plain text only
/// and ignores all formatted spans.
///
class PlainRichTextWriter : RichTextWriter
{
///
/// The text writer that was passed to the PlainRichTextWriter constructor.
///
protected readonly TextWriter textWriter;
string indentationString = "\t";
int indentationLevel;
char prevChar;
///
/// Creates a new PlainRichTextWriter instance that writes the text to the specified text writer.
///
public PlainRichTextWriter(TextWriter textWriter)
{
if (textWriter == null)
throw new ArgumentNullException("textWriter");
this.textWriter = textWriter;
}
///
/// Gets/Sets the string used to indent by one level.
///
public string IndentationString {
get {
return indentationString;
}
set {
indentationString = value;
}
}
///
protected override void BeginUnhandledSpan()
{
}
///
public override void EndSpan()
{
}
void WriteIndentation()
{
for (int i = 0; i < indentationLevel; i++) {
textWriter.Write(indentationString);
}
}
///
/// Writes the indentation, if necessary.
///
protected void WriteIndentationIfNecessary()
{
if (prevChar == '\n') {
WriteIndentation();
prevChar = '\0';
}
}
///
/// Is called after a write operation.
///
protected virtual void AfterWrite()
{
}
///
public override void Write(char value)
{
if (prevChar == '\n')
WriteIndentation();
textWriter.Write(value);
prevChar = value;
AfterWrite();
}
///
public override void Indent()
{
indentationLevel++;
}
///
public override void Unindent()
{
if (indentationLevel == 0)
throw new NotSupportedException();
indentationLevel--;
}
///
public override Encoding Encoding {
get { return textWriter.Encoding; }
}
///
public override IFormatProvider FormatProvider {
get { return textWriter.FormatProvider; }
}
///
public override string NewLine {
get {
return textWriter.NewLine;
}
set {
textWriter.NewLine = value;
}
}
}
}