External/Aga.Controls/Tree/TreeViewAdv.Draw.cs
changeset 345 0c551e8818e0
child 346 f652ab1e06e2
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/Tree/TreeViewAdv.Draw.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,308 @@
     1.4 +using System;
     1.5 +using System.Drawing;
     1.6 +using System.Diagnostics;
     1.7 +using System.Drawing.Drawing2D;
     1.8 +using System.Windows.Forms;
     1.9 +using Aga.Controls.Tree.NodeControls;
    1.10 +
    1.11 +namespace Aga.Controls.Tree
    1.12 +{
    1.13 +	public partial class TreeViewAdv
    1.14 +	{
    1.15 +		public void AutoSizeColumn(TreeColumn column)
    1.16 +		{
    1.17 +			if (!Columns.Contains(column))
    1.18 +				throw new ArgumentException("column");
    1.19 +
    1.20 +			DrawContext context = new DrawContext();
    1.21 +			context.Graphics = Graphics.FromImage(new Bitmap(1, 1));
    1.22 +			context.Font = this.Font;
    1.23 +			int res = 0;
    1.24 +			for (int row = 0; row < RowCount; row++)
    1.25 +			{
    1.26 +				if (row < RowMap.Count)
    1.27 +				{
    1.28 +					int w = 0;
    1.29 +					TreeNodeAdv node = RowMap[row];
    1.30 +					foreach (NodeControl nc in NodeControls)
    1.31 +					{
    1.32 +						if (nc.ParentColumn == column)
    1.33 +							w += nc.GetActualSize(node, _measureContext).Width;
    1.34 +					}
    1.35 +					res = Math.Max(res, w);
    1.36 +				}
    1.37 +			}
    1.38 +
    1.39 +			if (res > 0)
    1.40 +				column.Width = res;
    1.41 +		}
    1.42 +
    1.43 +		private void CreatePens()
    1.44 +		{
    1.45 +			CreateLinePen();
    1.46 +			CreateMarkPen();
    1.47 +		}
    1.48 +
    1.49 +		private void CreateMarkPen()
    1.50 +		{
    1.51 +			GraphicsPath path = new GraphicsPath();
    1.52 +			path.AddLines(new Point[] { new Point(0, 0), new Point(1, 1), new Point(-1, 1), new Point(0, 0) });
    1.53 +			CustomLineCap cap = new CustomLineCap(null, path);
    1.54 +			cap.WidthScale = 1.0f;
    1.55 +
    1.56 +			_markPen = new Pen(_dragDropMarkColor, _dragDropMarkWidth);
    1.57 +			_markPen.CustomStartCap = cap;
    1.58 +			_markPen.CustomEndCap = cap;
    1.59 +		}
    1.60 +
    1.61 +		private void CreateLinePen()
    1.62 +		{
    1.63 +			_linePen = new Pen(_lineColor);
    1.64 +			_linePen.DashStyle = DashStyle.Dot;
    1.65 +		}
    1.66 +
    1.67 +        protected override void OnPaint(PaintEventArgs e)
    1.68 +        {
    1.69 +            BeginPerformanceCount();
    1.70 +			PerformanceAnalyzer.Start("OnPaint");
    1.71 +
    1.72 +            DrawContext context = new DrawContext();
    1.73 +            context.Graphics = e.Graphics;
    1.74 +            context.Font = this.Font;
    1.75 +            context.Enabled = Enabled;
    1.76 +
    1.77 +            int y = 0;
    1.78 +            int gridHeight = 0;
    1.79 +
    1.80 +            if (UseColumns)
    1.81 +            {
    1.82 +				DrawColumnHeaders(e.Graphics);
    1.83 +				y += ColumnHeaderHeight;
    1.84 +                if (Columns.Count == 0 || e.ClipRectangle.Height <= y)
    1.85 +                    return;
    1.86 +            }
    1.87 +
    1.88 +			int firstRowY = _rowLayout.GetRowBounds(FirstVisibleRow).Y;
    1.89 +            y -= firstRowY;
    1.90 +
    1.91 +            e.Graphics.ResetTransform();
    1.92 +            e.Graphics.TranslateTransform(-OffsetX, y);
    1.93 +            Rectangle displayRect = DisplayRectangle;
    1.94 +            for (int row = FirstVisibleRow; row < RowCount; row++)
    1.95 +            {
    1.96 +                Rectangle rowRect = _rowLayout.GetRowBounds(row);
    1.97 +                gridHeight += rowRect.Height;
    1.98 +                if (rowRect.Y + y > displayRect.Bottom)
    1.99 +                    break;
   1.100 +                else
   1.101 +                    DrawRow(e, ref context, row, rowRect);
   1.102 +            }
   1.103 +
   1.104 +			if ((GridLineStyle & GridLineStyle.Vertical) == GridLineStyle.Vertical && UseColumns)
   1.105 +				DrawVerticalGridLines(e.Graphics, firstRowY);
   1.106 +
   1.107 +			if (_dropPosition.Node != null && DragMode && HighlightDropPosition)
   1.108 +                DrawDropMark(e.Graphics);
   1.109 +
   1.110 +            e.Graphics.ResetTransform();
   1.111 +            DrawScrollBarsBox(e.Graphics);
   1.112 +
   1.113 +            if (DragMode && _dragBitmap != null)
   1.114 +                e.Graphics.DrawImage(_dragBitmap, PointToClient(MousePosition));
   1.115 +
   1.116 +			PerformanceAnalyzer.Finish("OnPaint");
   1.117 +			EndPerformanceCount(e);
   1.118 +        }
   1.119 +
   1.120 +		private void DrawRow(PaintEventArgs e, ref DrawContext context, int row, Rectangle rowRect)
   1.121 +		{
   1.122 +			TreeNodeAdv node = RowMap[row];
   1.123 +			context.DrawSelection = DrawSelectionMode.None;
   1.124 +			context.CurrentEditorOwner = CurrentEditorOwner;
   1.125 +			if (DragMode)
   1.126 +			{
   1.127 +				if ((_dropPosition.Node == node) && _dropPosition.Position == NodePosition.Inside && HighlightDropPosition)
   1.128 +					context.DrawSelection = DrawSelectionMode.Active;
   1.129 +			}
   1.130 +			else
   1.131 +			{
   1.132 +				if (node.IsSelected && Focused)
   1.133 +					context.DrawSelection = DrawSelectionMode.Active;
   1.134 +				else if (node.IsSelected && !Focused && !HideSelection)
   1.135 +					context.DrawSelection = DrawSelectionMode.Inactive;
   1.136 +			}
   1.137 +			context.DrawFocus = Focused && CurrentNode == node;
   1.138 +			
   1.139 +			OnRowDraw(e, node, context, row, rowRect);
   1.140 +
   1.141 +			if (FullRowSelect)
   1.142 +			{
   1.143 +				context.DrawFocus = false;
   1.144 +				if (context.DrawSelection == DrawSelectionMode.Active || context.DrawSelection == DrawSelectionMode.Inactive)
   1.145 +				{
   1.146 +					Rectangle focusRect = new Rectangle(OffsetX, rowRect.Y, ClientRectangle.Width, rowRect.Height);
   1.147 +					if (context.DrawSelection == DrawSelectionMode.Active)
   1.148 +					{
   1.149 +						e.Graphics.FillRectangle(SystemBrushes.Highlight, focusRect);
   1.150 +						context.DrawSelection = DrawSelectionMode.FullRowSelect;
   1.151 +					}
   1.152 +					else
   1.153 +					{
   1.154 +						e.Graphics.FillRectangle(SystemBrushes.InactiveBorder, focusRect);
   1.155 +						context.DrawSelection = DrawSelectionMode.None;
   1.156 +					}
   1.157 +				}
   1.158 +			}
   1.159 +
   1.160 +            if ((GridLineStyle & GridLineStyle.Horizontal) == GridLineStyle.Horizontal)
   1.161 +				e.Graphics.DrawLine(SystemPens.InactiveBorder, 0, rowRect.Bottom, e.Graphics.ClipBounds.Right, rowRect.Bottom);
   1.162 +
   1.163 +			if (ShowLines)
   1.164 +				DrawLines(e.Graphics, node, rowRect);
   1.165 +
   1.166 +			DrawNode(node, context);
   1.167 +		}
   1.168 +
   1.169 +		private void DrawVerticalGridLines(Graphics gr, int y)
   1.170 +		{
   1.171 +			int x = 0;
   1.172 +			foreach (TreeColumn c in Columns)
   1.173 +			{
   1.174 +				if (c.IsVisible)
   1.175 +				{
   1.176 +					x += c.Width;
   1.177 +					gr.DrawLine(SystemPens.InactiveBorder, x - 1, y, x - 1, gr.ClipBounds.Bottom);
   1.178 +				}
   1.179 +			}
   1.180 +		}
   1.181 +
   1.182 +		private void DrawColumnHeaders(Graphics gr)
   1.183 +		{
   1.184 +			PerformanceAnalyzer.Start("DrawColumnHeaders");
   1.185 +			ReorderColumnState reorder = Input as ReorderColumnState;
   1.186 +			int x = 0;
   1.187 +			TreeColumn.DrawBackground(gr, new Rectangle(0, 0, ClientRectangle.Width + 2, ColumnHeaderHeight - 1), false, false);
   1.188 +			gr.TranslateTransform(-OffsetX, 0);
   1.189 +			foreach (TreeColumn c in Columns)
   1.190 +			{
   1.191 +				if (c.IsVisible)
   1.192 +				{
   1.193 +					if (x >= OffsetX && x - OffsetX < this.Bounds.Width)// skip invisible columns
   1.194 +					{
   1.195 +						Rectangle rect = new Rectangle(x, 0, c.Width, ColumnHeaderHeight - 1);
   1.196 +						gr.SetClip(rect);
   1.197 +						bool pressed = ((Input is ClickColumnState || reorder != null) && ((Input as ColumnState).Column == c));
   1.198 +						c.Draw(gr, rect, Font, pressed, _hotColumn == c);
   1.199 +						gr.ResetClip();
   1.200 +
   1.201 +						if (reorder != null && reorder.DropColumn == c)
   1.202 +							TreeColumn.DrawDropMark(gr, rect);
   1.203 +					}
   1.204 +					x += c.Width;
   1.205 +				}
   1.206 +			}
   1.207 +
   1.208 +			if (reorder != null)
   1.209 +			{
   1.210 +				if (reorder.DropColumn == null)
   1.211 +					TreeColumn.DrawDropMark(gr, new Rectangle(x, 0, 0, ColumnHeaderHeight));
   1.212 +				gr.DrawImage(reorder.GhostImage, new Point(reorder.Location.X +  + reorder.DragOffset, reorder.Location.Y));
   1.213 +			}
   1.214 +			PerformanceAnalyzer.Finish("DrawColumnHeaders");
   1.215 +		}
   1.216 +
   1.217 +		public void DrawNode(TreeNodeAdv node, DrawContext context)
   1.218 +		{
   1.219 +			foreach (NodeControlInfo item in GetNodeControls(node))
   1.220 +			{
   1.221 +				if (item.Bounds.Right >= OffsetX && item.Bounds.X - OffsetX < this.Bounds.Width)// skip invisible nodes
   1.222 +				{
   1.223 +					context.Bounds = item.Bounds;
   1.224 +					context.Graphics.SetClip(context.Bounds);
   1.225 +					item.Control.Draw(node, context);
   1.226 +					context.Graphics.ResetClip();
   1.227 +				}
   1.228 +			}
   1.229 +		}
   1.230 +
   1.231 +		private void DrawScrollBarsBox(Graphics gr)
   1.232 +		{
   1.233 +			Rectangle r1 = DisplayRectangle;
   1.234 +			Rectangle r2 = ClientRectangle;
   1.235 +			gr.FillRectangle(SystemBrushes.Control,
   1.236 +				new Rectangle(r1.Right, r1.Bottom, r2.Width - r1.Width, r2.Height - r1.Height));
   1.237 +		}
   1.238 +
   1.239 +		private void DrawDropMark(Graphics gr)
   1.240 +		{
   1.241 +			if (_dropPosition.Position == NodePosition.Inside)
   1.242 +				return;
   1.243 +
   1.244 +			Rectangle rect = GetNodeBounds(_dropPosition.Node);
   1.245 +			int right = DisplayRectangle.Right - LeftMargin + OffsetX;
   1.246 +			int y = rect.Y;
   1.247 +			if (_dropPosition.Position == NodePosition.After)
   1.248 +				y = rect.Bottom;
   1.249 +			gr.DrawLine(_markPen, rect.X, y, right, y);
   1.250 +		}
   1.251 +
   1.252 +		private void DrawLines(Graphics gr, TreeNodeAdv node, Rectangle rowRect)
   1.253 +		{
   1.254 +			if (UseColumns && Columns.Count > 0)
   1.255 +				gr.SetClip(new Rectangle(0, rowRect.Y, Columns[0].Width, rowRect.Bottom));
   1.256 +
   1.257 +			TreeNodeAdv curNode = node;
   1.258 +			while (curNode != _root && curNode != null)
   1.259 +			{
   1.260 +				int level = curNode.Level;
   1.261 +				int x = (level - 1) * _indent + NodePlusMinus.ImageSize / 2 + LeftMargin;
   1.262 +				int width = NodePlusMinus.Width - NodePlusMinus.ImageSize / 2;
   1.263 +				int y = rowRect.Y;
   1.264 +				int y2 = y + rowRect.Height;
   1.265 +
   1.266 +				if (curNode == node)
   1.267 +				{
   1.268 +					int midy = y + rowRect.Height / 2;
   1.269 +					gr.DrawLine(_linePen, x, midy, x + width, midy);
   1.270 +					if (curNode.NextNode == null)
   1.271 +						y2 = y + rowRect.Height / 2;
   1.272 +				}
   1.273 +
   1.274 +				if (node.Row == 0)
   1.275 +					y = rowRect.Height / 2;
   1.276 +				if (curNode.NextNode != null || curNode == node)
   1.277 +					gr.DrawLine(_linePen, x, y, x, y2);
   1.278 +
   1.279 +				curNode = curNode.Parent;
   1.280 +			}
   1.281 +
   1.282 +			gr.ResetClip();
   1.283 +		}
   1.284 +
   1.285 +		#region Performance
   1.286 +
   1.287 +		private double _totalTime;
   1.288 +		private int _paintCount;
   1.289 +
   1.290 +		[Conditional("PERF_TEST")]
   1.291 +		private void BeginPerformanceCount()
   1.292 +		{
   1.293 +			_paintCount++;
   1.294 +			TimeCounter.Start();
   1.295 +		}
   1.296 +
   1.297 +		[Conditional("PERF_TEST")]
   1.298 +		private void EndPerformanceCount(PaintEventArgs e)
   1.299 +		{
   1.300 +			double time = TimeCounter.Finish();
   1.301 +			_totalTime += time;
   1.302 +			string debugText = string.Format("FPS {0:0.0}; Avg. FPS {1:0.0}",
   1.303 +				1 / time, 1 / (_totalTime / _paintCount));
   1.304 +			e.Graphics.FillRectangle(Brushes.White, new Rectangle(DisplayRectangle.Width - 150, DisplayRectangle.Height - 20, 150, 20));
   1.305 +			e.Graphics.DrawString(debugText, Control.DefaultFont, Brushes.Gray,
   1.306 +				new PointF(DisplayRectangle.Width - 150, DisplayRectangle.Height - 20));
   1.307 +		}
   1.308 +		#endregion
   1.309 +
   1.310 +	}
   1.311 +}