Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistentDataStructures/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Highlighting/RichTextModelWriter.cs @ 16724

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

#2077: created branch and added first version

File size: 4.1 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.Collections.Generic;
21using System.Windows;
22using System.Windows.Media;
23using ICSharpCode.NRefactory.Editor;
24using ICSharpCode.AvalonEdit.Document;
25using ICSharpCode.AvalonEdit.Utils;
26
27namespace ICSharpCode.AvalonEdit.Highlighting
28{
29  /// <summary>
30  /// A RichTextWriter that writes into a document and RichTextModel.
31  /// </summary>
32  class RichTextModelWriter : PlainRichTextWriter
33  {
34    readonly RichTextModel richTextModel;
35    readonly DocumentTextWriter documentTextWriter;
36    readonly Stack<HighlightingColor> colorStack = new Stack<HighlightingColor>();
37    HighlightingColor currentColor;
38    int currentColorBegin = -1;
39   
40    /// <summary>
41    /// Creates a new RichTextModelWriter that inserts into document, starting at insertionOffset.
42    /// </summary>
43    public RichTextModelWriter(RichTextModel richTextModel, IDocument document, int insertionOffset)
44      : base(new DocumentTextWriter(document, insertionOffset))
45    {
46      if (richTextModel == null)
47        throw new ArgumentNullException("richTextModel");
48      this.richTextModel = richTextModel;
49      this.documentTextWriter = (DocumentTextWriter)base.textWriter;
50      currentColor = richTextModel.GetHighlightingAt(Math.Max(0, insertionOffset - 1));
51    }
52   
53    /// <summary>
54    /// Gets/Sets the current insertion offset.
55    /// </summary>
56    public int InsertionOffset {
57      get { return documentTextWriter.InsertionOffset; }
58      set { documentTextWriter.InsertionOffset = value; }
59    }
60   
61   
62    /// <inheritdoc/>
63    protected override void BeginUnhandledSpan()
64    {
65      colorStack.Push(currentColor);
66    }
67   
68    void BeginColorSpan()
69    {
70      WriteIndentationIfNecessary();
71      colorStack.Push(currentColor);
72      currentColor = currentColor.Clone();
73      currentColorBegin = documentTextWriter.InsertionOffset;
74    }
75   
76    /// <inheritdoc/>
77    public override void EndSpan()
78    {
79      currentColor = colorStack.Pop();
80      currentColorBegin = documentTextWriter.InsertionOffset;
81    }
82   
83    /// <inheritdoc/>
84    protected override void AfterWrite()
85    {
86      base.AfterWrite();
87      richTextModel.SetHighlighting(currentColorBegin, documentTextWriter.InsertionOffset - currentColorBegin, currentColor);
88    }
89   
90    /// <inheritdoc/>
91    public override void BeginSpan(Color foregroundColor)
92    {
93      BeginColorSpan();
94      currentColor.Foreground = new SimpleHighlightingBrush(foregroundColor);
95      currentColor.Freeze();
96    }
97   
98    /// <inheritdoc/>
99    public override void BeginSpan(FontFamily fontFamily)
100    {
101      BeginUnhandledSpan(); // TODO
102    }
103   
104    /// <inheritdoc/>
105    public override void BeginSpan(FontStyle fontStyle)
106    {
107      BeginColorSpan();
108      currentColor.FontStyle = fontStyle;
109      currentColor.Freeze();
110    }
111   
112    /// <inheritdoc/>
113    public override void BeginSpan(FontWeight fontWeight)
114    {
115      BeginColorSpan();
116      currentColor.FontWeight = fontWeight;
117      currentColor.Freeze();
118    }
119   
120    /// <inheritdoc/>
121    public override void BeginSpan(HighlightingColor highlightingColor)
122    {
123      BeginColorSpan();
124      currentColor.MergeWith(highlightingColor);
125      currentColor.Freeze();
126    }
127  }
128}
Note: See TracBrowser for help on using the repository browser.