Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Rendering/ColorizingTransformer.cs @ 11700

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

#2077: created branch and added first version

File size: 4.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.Collections.Generic;
21
22namespace ICSharpCode.AvalonEdit.Rendering
23{
24  /// <summary>
25  /// Base class for <see cref="IVisualLineTransformer"/> that helps
26  /// splitting visual elements so that colors (and other text properties) can be easily assigned
27  /// to individual words/characters.
28  /// </summary>
29  public abstract class ColorizingTransformer : IVisualLineTransformer, ITextViewConnect
30  {
31    /// <summary>
32    /// Gets the list of elements currently being transformed.
33    /// </summary>
34    protected IList<VisualLineElement> CurrentElements { get; private set; }
35   
36    /// <summary>
37    /// <see cref="IVisualLineTransformer.Transform"/> implementation.
38    /// Sets <see cref="CurrentElements"/> and calls <see cref="Colorize"/>.
39    /// </summary>
40    public void Transform(ITextRunConstructionContext context, IList<VisualLineElement> elements)
41    {
42      if (elements == null)
43        throw new ArgumentNullException("elements");
44      if (this.CurrentElements != null)
45        throw new InvalidOperationException("Recursive Transform() call");
46      this.CurrentElements = elements;
47      try {
48        Colorize(context);
49      } finally {
50        this.CurrentElements = null;
51      }
52    }
53   
54    /// <summary>
55    /// Performs the colorization.
56    /// </summary>
57    protected abstract void Colorize(ITextRunConstructionContext context);
58   
59    /// <summary>
60    /// Changes visual element properties.
61    /// This method accesses <see cref="CurrentElements"/>, so it must be called only during
62    /// a <see cref="Transform"/> call.
63    /// This method splits <see cref="VisualLineElement"/>s as necessary to ensure that the region
64    /// can be colored by setting the <see cref="VisualLineElement.TextRunProperties"/> of whole elements,
65    /// and then calls the <paramref name="action"/> on all elements in the region.
66    /// </summary>
67    /// <param name="visualStartColumn">Start visual column of the region to change</param>
68    /// <param name="visualEndColumn">End visual column of the region to change</param>
69    /// <param name="action">Action that changes an individual <see cref="VisualLineElement"/>.</param>
70    protected void ChangeVisualElements(int visualStartColumn, int visualEndColumn, Action<VisualLineElement> action)
71    {
72      if (action == null)
73        throw new ArgumentNullException("action");
74      for (int i = 0; i < CurrentElements.Count; i++) {
75        VisualLineElement e = CurrentElements[i];
76        if (e.VisualColumn > visualEndColumn)
77          break;
78        if (e.VisualColumn < visualStartColumn &&
79            e.VisualColumn + e.VisualLength > visualStartColumn)
80        {
81          if (e.CanSplit) {
82            e.Split(visualStartColumn, CurrentElements, i--);
83            continue;
84          }
85        }
86        if (e.VisualColumn >= visualStartColumn && e.VisualColumn < visualEndColumn) {
87          if (e.VisualColumn + e.VisualLength > visualEndColumn) {
88            if (e.CanSplit) {
89              e.Split(visualEndColumn, CurrentElements, i--);
90              continue;
91            }
92          } else {
93            action(e);
94          }
95        }
96      }
97    }
98   
99    /// <summary>
100    /// Called when added to a text view.
101    /// </summary>
102    protected virtual void OnAddToTextView(TextView textView)
103    {
104    }
105   
106    /// <summary>
107    /// Called when removed from a text view.
108    /// </summary>
109    protected virtual void OnRemoveFromTextView(TextView textView)
110    {
111    }
112   
113    void ITextViewConnect.AddToTextView(TextView textView)
114    {
115      OnAddToTextView(textView);
116    }
117   
118    void ITextViewConnect.RemoveFromTextView(TextView textView)
119    {
120      OnRemoveFromTextView(textView);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.