/AvalonEdit/ICSharpCode.AvalonEdit/Editing/SelectionColorizer.cs

http://github.com/icsharpcode/ILSpy · C# · 74 lines · 48 code · 8 blank · 18 comment · 8 complexity · 7ba57bc4744381bddd77b6e8aafd460c MD5 · raw file

  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. using System;
  19. using ICSharpCode.AvalonEdit.Rendering;
  20. #if NREFACTORY
  21. using ICSharpCode.NRefactory.Editor;
  22. #endif
  23. namespace ICSharpCode.AvalonEdit.Editing
  24. {
  25. sealed class SelectionColorizer : ColorizingTransformer
  26. {
  27. TextArea textArea;
  28. public SelectionColorizer(TextArea textArea)
  29. {
  30. if (textArea == null)
  31. throw new ArgumentNullException("textArea");
  32. this.textArea = textArea;
  33. }
  34. protected override void Colorize(ITextRunConstructionContext context)
  35. {
  36. // if SelectionForeground is null, keep the existing foreground color
  37. if (textArea.SelectionForeground == null)
  38. return;
  39. int lineStartOffset = context.VisualLine.FirstDocumentLine.Offset;
  40. int lineEndOffset = context.VisualLine.LastDocumentLine.Offset + context.VisualLine.LastDocumentLine.TotalLength;
  41. foreach (SelectionSegment segment in textArea.Selection.Segments) {
  42. int segmentStart = segment.StartOffset;
  43. int segmentEnd = segment.EndOffset;
  44. if (segmentEnd <= lineStartOffset)
  45. continue;
  46. if (segmentStart >= lineEndOffset)
  47. continue;
  48. int startColumn;
  49. if (segmentStart < lineStartOffset)
  50. startColumn = 0;
  51. else
  52. startColumn = context.VisualLine.ValidateVisualColumn(segment.StartOffset, segment.StartVisualColumn, textArea.Selection.EnableVirtualSpace);
  53. int endColumn;
  54. if (segmentEnd > lineEndOffset)
  55. endColumn = textArea.Selection.EnableVirtualSpace ? int.MaxValue : context.VisualLine.VisualLengthWithEndOfLineMarker;
  56. else
  57. endColumn = context.VisualLine.ValidateVisualColumn(segment.EndOffset, segment.EndVisualColumn, textArea.Selection.EnableVirtualSpace);
  58. ChangeVisualElements(
  59. startColumn, endColumn,
  60. element => {
  61. element.TextRunProperties.SetForegroundBrush(textArea.SelectionForeground);
  62. });
  63. }
  64. }
  65. }
  66. }