/AvalonEdit/ICSharpCode.AvalonEdit/Editing/CaretLayer.cs

http://github.com/icsharpcode/ILSpy · C# · 115 lines · 82 code · 15 blank · 18 comment · 9 complexity · f3f1bc4d754c626d9dc1eb09da657dba 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 System.Windows;
  20. using System.Windows.Controls;
  21. using System.Windows.Media;
  22. using System.Windows.Media.Animation;
  23. using System.Windows.Threading;
  24. using ICSharpCode.AvalonEdit.Rendering;
  25. using ICSharpCode.AvalonEdit.Utils;
  26. namespace ICSharpCode.AvalonEdit.Editing
  27. {
  28. sealed class CaretLayer : Layer
  29. {
  30. TextArea textArea;
  31. bool isVisible;
  32. Rect caretRectangle;
  33. DispatcherTimer caretBlinkTimer = new DispatcherTimer();
  34. bool blink;
  35. public CaretLayer(TextArea textArea) : base(textArea.TextView, KnownLayer.Caret)
  36. {
  37. this.textArea = textArea;
  38. this.IsHitTestVisible = false;
  39. caretBlinkTimer.Tick += new EventHandler(caretBlinkTimer_Tick);
  40. }
  41. void caretBlinkTimer_Tick(object sender, EventArgs e)
  42. {
  43. blink = !blink;
  44. InvalidateVisual();
  45. }
  46. public void Show(Rect caretRectangle)
  47. {
  48. this.caretRectangle = caretRectangle;
  49. this.isVisible = true;
  50. StartBlinkAnimation();
  51. InvalidateVisual();
  52. }
  53. public void Hide()
  54. {
  55. if (isVisible) {
  56. isVisible = false;
  57. StopBlinkAnimation();
  58. InvalidateVisual();
  59. }
  60. }
  61. void StartBlinkAnimation()
  62. {
  63. TimeSpan blinkTime = Win32.CaretBlinkTime;
  64. blink = true; // the caret should visible initially
  65. // This is important if blinking is disabled (system reports a negative blinkTime)
  66. if (blinkTime.TotalMilliseconds > 0) {
  67. caretBlinkTimer.Interval = blinkTime;
  68. caretBlinkTimer.Start();
  69. }
  70. }
  71. void StopBlinkAnimation()
  72. {
  73. caretBlinkTimer.Stop();
  74. }
  75. internal Brush CaretBrush;
  76. protected override void OnRender(DrawingContext drawingContext)
  77. {
  78. base.OnRender(drawingContext);
  79. if (isVisible && blink) {
  80. Brush caretBrush = this.CaretBrush;
  81. if (caretBrush == null)
  82. caretBrush = (Brush)textView.GetValue(TextBlock.ForegroundProperty);
  83. if (this.textArea.OverstrikeMode) {
  84. SolidColorBrush scBrush = caretBrush as SolidColorBrush;
  85. if (scBrush != null) {
  86. Color brushColor = scBrush.Color;
  87. Color newColor = Color.FromArgb(100, brushColor.R, brushColor.G, brushColor.B);
  88. caretBrush = new SolidColorBrush(newColor);
  89. caretBrush.Freeze();
  90. }
  91. }
  92. Rect r = new Rect(caretRectangle.X - textView.HorizontalOffset,
  93. caretRectangle.Y - textView.VerticalOffset,
  94. caretRectangle.Width,
  95. caretRectangle.Height);
  96. drawingContext.DrawRectangle(caretBrush, null, PixelSnapHelpers.Round(r, PixelSnapHelpers.GetPixelSize(this)));
  97. }
  98. }
  99. }
  100. }