Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Rendering/VisualLineText.cs @ 12048

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

#2077: created branch and added first version

File size: 5.7 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.Documents;
22using System.Windows.Media.TextFormatting;
23
24using ICSharpCode.AvalonEdit.Document;
25using ICSharpCode.AvalonEdit.Utils;
26
27namespace ICSharpCode.AvalonEdit.Rendering
28{
29  /// <summary>
30  /// VisualLineElement that represents a piece of text.
31  /// </summary>
32  public class VisualLineText : VisualLineElement
33  {
34    VisualLine parentVisualLine;
35   
36    /// <summary>
37    /// Gets the parent visual line.
38    /// </summary>
39    public VisualLine ParentVisualLine {
40      get { return parentVisualLine; }
41    }
42   
43    /// <summary>
44    /// Creates a visual line text element with the specified length.
45    /// It uses the <see cref="ITextRunConstructionContext.VisualLine"/> and its
46    /// <see cref="VisualLineElement.RelativeTextOffset"/> to find the actual text string.
47    /// </summary>
48    public VisualLineText(VisualLine parentVisualLine, int length) : base(length, length)
49    {
50      if (parentVisualLine == null)
51        throw new ArgumentNullException("parentVisualLine");
52      this.parentVisualLine = parentVisualLine;
53    }
54   
55    /// <summary>
56    /// Override this method to control the type of new VisualLineText instances when
57    /// the visual line is split due to syntax highlighting.
58    /// </summary>
59    protected virtual VisualLineText CreateInstance(int length)
60    {
61      return new VisualLineText(parentVisualLine, length);
62    }
63   
64    /// <inheritdoc/>
65    public override TextRun CreateTextRun(int startVisualColumn, ITextRunConstructionContext context)
66    {
67      if (context == null)
68        throw new ArgumentNullException("context");
69     
70      int relativeOffset = startVisualColumn - VisualColumn;
71      StringSegment text = context.GetText(context.VisualLine.FirstDocumentLine.Offset + RelativeTextOffset + relativeOffset, DocumentLength - relativeOffset);
72      return new TextCharacters(text.Text, text.Offset, text.Count, this.TextRunProperties);
73    }
74   
75    /// <inheritdoc/>
76    public override bool IsWhitespace(int visualColumn)
77    {
78      int offset = visualColumn - this.VisualColumn + parentVisualLine.FirstDocumentLine.Offset + this.RelativeTextOffset;
79      return char.IsWhiteSpace(parentVisualLine.Document.GetCharAt(offset));
80    }
81   
82    /// <inheritdoc/>
83    public override TextSpan<CultureSpecificCharacterBufferRange> GetPrecedingText(int visualColumnLimit, ITextRunConstructionContext context)
84    {
85      if (context == null)
86        throw new ArgumentNullException("context");
87     
88      int relativeOffset = visualColumnLimit - VisualColumn;
89      StringSegment text = context.GetText(context.VisualLine.FirstDocumentLine.Offset + RelativeTextOffset, relativeOffset);
90      CharacterBufferRange range = new CharacterBufferRange(text.Text, text.Offset, text.Count);
91      return new TextSpan<CultureSpecificCharacterBufferRange>(range.Length, new CultureSpecificCharacterBufferRange(this.TextRunProperties.CultureInfo, range));
92    }
93   
94    /// <inheritdoc/>
95    public override bool CanSplit {
96      get { return true; }
97    }
98   
99    /// <inheritdoc/>
100    public override void Split(int splitVisualColumn, IList<VisualLineElement> elements, int elementIndex)
101    {
102      if (splitVisualColumn <= VisualColumn || splitVisualColumn >= VisualColumn + VisualLength)
103        throw new ArgumentOutOfRangeException("splitVisualColumn", splitVisualColumn, "Value must be between " + (VisualColumn + 1) + " and " + (VisualColumn + VisualLength - 1));
104      if (elements == null)
105        throw new ArgumentNullException("elements");
106      if (elements[elementIndex] != this)
107        throw new ArgumentException("Invalid elementIndex - couldn't find this element at the index");
108      int relativeSplitPos = splitVisualColumn - VisualColumn;
109      VisualLineText splitPart = CreateInstance(DocumentLength - relativeSplitPos);
110      SplitHelper(this, splitPart, splitVisualColumn, relativeSplitPos + RelativeTextOffset);
111      elements.Insert(elementIndex + 1, splitPart);
112    }
113   
114    /// <inheritdoc/>
115    public override int GetRelativeOffset(int visualColumn)
116    {
117      return this.RelativeTextOffset + visualColumn - this.VisualColumn;
118    }
119   
120    /// <inheritdoc/>
121    public override int GetVisualColumn(int relativeTextOffset)
122    {
123      return this.VisualColumn + relativeTextOffset - this.RelativeTextOffset;
124    }
125   
126    /// <inheritdoc/>
127    public override int GetNextCaretPosition(int visualColumn, LogicalDirection direction, CaretPositioningMode mode)
128    {
129      int textOffset = parentVisualLine.StartOffset + this.RelativeTextOffset;
130      int pos = TextUtilities.GetNextCaretPosition(parentVisualLine.Document, textOffset + visualColumn - this.VisualColumn, direction, mode);
131      if (pos < textOffset || pos > textOffset + this.DocumentLength)
132        return -1;
133      else
134        return this.VisualColumn + pos - textOffset;
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.