PageRenderTime 13ms CodeModel.GetById 9ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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