Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Utils/PlainRichTextWriter.cs @ 13397

Last change on this file since 13397 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 3.5 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.IO;
21using System.Text;
22
23namespace ICSharpCode.AvalonEdit.Utils
24{
25  /// <summary>
26  /// RichTextWriter implementation that writes plain text only
27  /// and ignores all formatted spans.
28  /// </summary>
29  class PlainRichTextWriter : RichTextWriter
30  {
31    /// <summary>
32    /// The text writer that was passed to the PlainRichTextWriter constructor.
33    /// </summary>
34    protected readonly TextWriter textWriter;
35    string indentationString = "\t";
36    int indentationLevel;
37    char prevChar;
38   
39    /// <summary>
40    /// Creates a new PlainRichTextWriter instance that writes the text to the specified text writer.
41    /// </summary>
42    public PlainRichTextWriter(TextWriter textWriter)
43    {
44      if (textWriter == null)
45        throw new ArgumentNullException("textWriter");
46      this.textWriter = textWriter;
47    }
48   
49    /// <summary>
50    /// Gets/Sets the string used to indent by one level.
51    /// </summary>
52    public string IndentationString {
53      get {
54        return indentationString;
55      }
56      set {
57        indentationString = value;
58      }
59    }
60   
61    /// <inheritdoc/>
62    protected override void BeginUnhandledSpan()
63    {
64    }
65   
66    /// <inheritdoc/>
67    public override void EndSpan()
68    {
69    }
70   
71    void WriteIndentation()
72    {
73      for (int i = 0; i < indentationLevel; i++) {
74        textWriter.Write(indentationString);
75      }
76    }
77   
78    /// <summary>
79    /// Writes the indentation, if necessary.
80    /// </summary>
81    protected void WriteIndentationIfNecessary()
82    {
83      if (prevChar == '\n') {
84        WriteIndentation();
85        prevChar = '\0';
86      }
87    }
88   
89    /// <summary>
90    /// Is called after a write operation.
91    /// </summary>
92    protected virtual void AfterWrite()
93    {
94    }
95   
96    /// <inheritdoc/>
97    public override void Write(char value)
98    {
99      if (prevChar == '\n')
100        WriteIndentation();
101      textWriter.Write(value);
102      prevChar = value;
103      AfterWrite();
104    }
105   
106    /// <inheritdoc/>
107    public override void Indent()
108    {
109      indentationLevel++;
110    }
111   
112    /// <inheritdoc/>
113    public override void Unindent()
114    {
115      if (indentationLevel == 0)
116        throw new NotSupportedException();
117      indentationLevel--;
118    }
119   
120    /// <inheritdoc/>
121    public override Encoding Encoding {
122      get { return textWriter.Encoding; }
123    }
124   
125    /// <inheritdoc/>
126    public override IFormatProvider FormatProvider {
127      get { return textWriter.FormatProvider; }
128    }
129   
130    /// <inheritdoc/>
131    public override string NewLine {
132      get {
133        return textWriter.NewLine;
134      }
135      set {
136        textWriter.NewLine = value;
137      }
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.