Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Highlighting/HighlightedInlineBuilder.cs @ 15377

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

#2077: created branch and added first version

File size: 6.6 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.Diagnostics;
22using System.Linq;
23using System.Text;
24using System.Windows;
25using System.Windows.Documents;
26using System.Windows.Media;
27using ICSharpCode.AvalonEdit.Utils;
28
29namespace ICSharpCode.AvalonEdit.Highlighting
30{
31  /// <summary>
32  /// Takes a series of highlighting commands and stores them.
33  /// Later, it can build inline objects (for use with WPF TextBlock) from the commands.
34  /// </summary>
35  /// <remarks>
36  /// This class is not used in AvalonEdit - but it is useful for someone who wants to put a HighlightedLine
37  /// into a TextBlock.
38  /// In SharpDevelop, we use it to provide syntax highlighting inside the search results pad.
39  /// </remarks>
40  [Obsolete("Use RichText / RichTextModel instead")]
41  public sealed class HighlightedInlineBuilder
42  {
43    static HighlightingBrush MakeBrush(Brush b)
44    {
45      SolidColorBrush scb = b as SolidColorBrush;
46      if (scb != null)
47        return new SimpleHighlightingBrush(scb);
48      else
49        return null;
50    }
51   
52    readonly string text;
53    List<int> stateChangeOffsets = new List<int>();
54    List<HighlightingColor> stateChanges = new List<HighlightingColor>();
55   
56    int GetIndexForOffset(int offset)
57    {
58      if (offset < 0 || offset > text.Length)
59        throw new ArgumentOutOfRangeException("offset");
60      int index = stateChangeOffsets.BinarySearch(offset);
61      if (index < 0) {
62        index = ~index;
63        if (offset < text.Length) {
64          stateChanges.Insert(index, stateChanges[index - 1].Clone());
65          stateChangeOffsets.Insert(index, offset);
66        }
67      }
68      return index;
69    }
70   
71    /// <summary>
72    /// Creates a new HighlightedInlineBuilder instance.
73    /// </summary>
74    public HighlightedInlineBuilder(string text)
75    {
76      if (text == null)
77        throw new ArgumentNullException("text");
78      this.text = text;
79      stateChangeOffsets.Add(0);
80      stateChanges.Add(new HighlightingColor());
81    }
82   
83    /// <summary>
84    /// Creates a new HighlightedInlineBuilder instance.
85    /// </summary>
86    public HighlightedInlineBuilder(RichText text)
87    {
88      if (text == null)
89        throw new ArgumentNullException("text");
90      this.text = text.Text;
91      stateChangeOffsets.AddRange(text.stateChangeOffsets);
92      stateChanges.AddRange(text.stateChanges);
93    }
94   
95    HighlightedInlineBuilder(string text, List<int> offsets, List<HighlightingColor> states)
96    {
97      this.text = text;
98      stateChangeOffsets = offsets;
99      stateChanges = states;
100    }
101   
102    /// <summary>
103    /// Gets the text.
104    /// </summary>
105    public string Text {
106      get { return text; }
107    }
108   
109    /// <summary>
110    /// Applies the properties from the HighlightingColor to the specified text segment.
111    /// </summary>
112    public void SetHighlighting(int offset, int length, HighlightingColor color)
113    {
114      if (color == null)
115        throw new ArgumentNullException("color");
116      if (color.Foreground == null && color.Background == null && color.FontStyle == null && color.FontWeight == null) {
117        // Optimization: don't split the HighlightingState when we're not changing
118        // any property. For example, the "Punctuation" color in C# is
119        // empty by default.
120        return;
121      }
122      int startIndex = GetIndexForOffset(offset);
123      int endIndex = GetIndexForOffset(offset + length);
124      for (int i = startIndex; i < endIndex; i++) {
125        stateChanges[i].MergeWith(color);
126      }
127    }
128   
129    /// <summary>
130    /// Sets the foreground brush on the specified text segment.
131    /// </summary>
132    public void SetForeground(int offset, int length, Brush brush)
133    {
134      int startIndex = GetIndexForOffset(offset);
135      int endIndex = GetIndexForOffset(offset + length);
136      var hbrush = MakeBrush(brush);
137      for (int i = startIndex; i < endIndex; i++) {
138        stateChanges[i].Foreground = hbrush;
139      }
140    }
141   
142    /// <summary>
143    /// Sets the background brush on the specified text segment.
144    /// </summary>
145    public void SetBackground(int offset, int length, Brush brush)
146    {
147      int startIndex = GetIndexForOffset(offset);
148      int endIndex = GetIndexForOffset(offset + length);
149      var hbrush = MakeBrush(brush);
150      for (int i = startIndex; i < endIndex; i++) {
151        stateChanges[i].Background = hbrush;
152      }
153    }
154   
155    /// <summary>
156    /// Sets the font weight on the specified text segment.
157    /// </summary>
158    public void SetFontWeight(int offset, int length, FontWeight weight)
159    {
160      int startIndex = GetIndexForOffset(offset);
161      int endIndex = GetIndexForOffset(offset + length);
162      for (int i = startIndex; i < endIndex; i++) {
163        stateChanges[i].FontWeight = weight;
164      }
165    }
166   
167    /// <summary>
168    /// Sets the font style on the specified text segment.
169    /// </summary>
170    public void SetFontStyle(int offset, int length, FontStyle style)
171    {
172      int startIndex = GetIndexForOffset(offset);
173      int endIndex = GetIndexForOffset(offset + length);
174      for (int i = startIndex; i < endIndex; i++) {
175        stateChanges[i].FontStyle = style;
176      }
177    }
178   
179    /// <summary>
180    /// Creates WPF Run instances that can be used for TextBlock.Inlines.
181    /// </summary>
182    public Run[] CreateRuns()
183    {
184      return ToRichText().CreateRuns();
185    }
186   
187    /// <summary>
188    /// Creates a RichText instance.
189    /// </summary>
190    public RichText ToRichText()
191    {
192      return new RichText(text, stateChangeOffsets.ToArray(), stateChanges.Select(FreezableHelper.GetFrozenClone).ToArray());
193    }
194   
195    /// <summary>
196    /// Clones this HighlightedInlineBuilder.
197    /// </summary>
198    public HighlightedInlineBuilder Clone()
199    {
200      return new HighlightedInlineBuilder(this.text,
201                                          stateChangeOffsets.ToList(),
202                                          stateChanges.Select(sc => sc.Clone()).ToList());
203    }
204  }
205}
Note: See TracBrowser for help on using the repository browser.