/SharpTreeView/LinesRenderer.cs

http://github.com/icsharpcode/ILSpy · C# · 75 lines · 47 code · 9 blank · 19 comment · 10 complexity · 8cbe09aff8b833ecc2d74be04fabea68 MD5 · raw file

  1. // Copyright (c) 2020 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.Windows;
  19. using System.Windows.Media;
  20. using System.Diagnostics;
  21. namespace ICSharpCode.TreeView
  22. {
  23. class LinesRenderer : FrameworkElement
  24. {
  25. static LinesRenderer()
  26. {
  27. pen = new Pen(Brushes.LightGray, 1);
  28. pen.Freeze();
  29. }
  30. static Pen pen;
  31. SharpTreeNodeView NodeView
  32. {
  33. get { return TemplatedParent as SharpTreeNodeView; }
  34. }
  35. protected override void OnRender(DrawingContext dc)
  36. {
  37. if (NodeView.Node == null) {
  38. // This seems to happen sometimes with DataContext==DisconnectedItem,
  39. // though I'm not sure why WPF would call OnRender() on a disconnected node
  40. Debug.WriteLine($"LinesRenderer.OnRender() called with DataContext={NodeView.DataContext}");
  41. return;
  42. }
  43. var indent = NodeView.CalculateIndent();
  44. var p = new Point(indent + 4.5, 0);
  45. if (!NodeView.Node.IsRoot || NodeView.ParentTreeView.ShowRootExpander) {
  46. dc.DrawLine(pen, new Point(p.X, ActualHeight / 2), new Point(p.X + 10, ActualHeight / 2));
  47. }
  48. if (NodeView.Node.IsRoot) return;
  49. if (NodeView.Node.IsLast) {
  50. dc.DrawLine(pen, p, new Point(p.X, ActualHeight / 2));
  51. }
  52. else {
  53. dc.DrawLine(pen, p, new Point(p.X, ActualHeight));
  54. }
  55. var current = NodeView.Node;
  56. while (true) {
  57. p.X -= 19;
  58. current = current.Parent;
  59. if (p.X < 0) break;
  60. if (!current.IsLast) {
  61. dc.DrawLine(pen, p, new Point(p.X, ActualHeight));
  62. }
  63. }
  64. }
  65. }
  66. }