External/Aga.Controls/Tree/AutoRowHeightLayout.cs
changeset 345 0c551e8818e0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/Tree/AutoRowHeightLayout.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,151 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Text;
     1.7 +using System.Drawing;
     1.8 +using Aga.Controls.Tree.NodeControls;
     1.9 +
    1.10 +namespace Aga.Controls.Tree
    1.11 +{
    1.12 +	public class AutoRowHeightLayout: IRowLayout
    1.13 +	{
    1.14 +		private DrawContext _measureContext;
    1.15 +		private TreeViewAdv _treeView;
    1.16 +		private List<Rectangle> _rowCache;
    1.17 +
    1.18 +		public AutoRowHeightLayout(TreeViewAdv treeView, int rowHeight)
    1.19 +		{
    1.20 +			_rowCache = new List<Rectangle>();
    1.21 +			_treeView = treeView;
    1.22 +			PreferredRowHeight = rowHeight;
    1.23 +			_measureContext = new DrawContext();
    1.24 +			_measureContext.Graphics = Graphics.FromImage(new Bitmap(1, 1));
    1.25 +		}
    1.26 +
    1.27 +		private int _rowHeight;
    1.28 +		public int PreferredRowHeight
    1.29 +		{
    1.30 +			get { return _rowHeight; }
    1.31 +			set { _rowHeight = value; }
    1.32 +		}
    1.33 +
    1.34 +
    1.35 +		public int PageRowCount
    1.36 +		{
    1.37 +			get 
    1.38 +			{
    1.39 +				if (_treeView.RowCount == 0)
    1.40 +					return 0;
    1.41 +				else
    1.42 +				{
    1.43 +					int pageHeight = _treeView.DisplayRectangle.Height - _treeView.ColumnHeaderHeight;
    1.44 +					int y = 0;
    1.45 +					for (int i = _treeView.RowCount - 1; i >= 0; i--)
    1.46 +					{
    1.47 +						y += GetRowHeight(i);
    1.48 +						if (y > pageHeight)
    1.49 +							return Math.Max(0, _treeView.RowCount - 1 - i);
    1.50 +					}
    1.51 +					return _treeView.RowCount;
    1.52 +				}
    1.53 +			}
    1.54 +		}
    1.55 +
    1.56 +		public int CurrentPageSize
    1.57 +		{
    1.58 +			get
    1.59 +			{
    1.60 +				if (_treeView.RowCount == 0)
    1.61 +					return 0;
    1.62 +				else
    1.63 +				{
    1.64 +					int pageHeight = _treeView.DisplayRectangle.Height - _treeView.ColumnHeaderHeight;
    1.65 +					int y = 0;
    1.66 +					for (int i = _treeView.FirstVisibleRow; i < _treeView.RowCount; i++)
    1.67 +					{
    1.68 +						y += GetRowHeight(i);
    1.69 +						if (y > pageHeight)
    1.70 +							return Math.Max(0, i - _treeView.FirstVisibleRow);
    1.71 +					}
    1.72 +					return Math.Max(0, _treeView.RowCount - _treeView.FirstVisibleRow);
    1.73 +				}
    1.74 +			}
    1.75 +		}
    1.76 +
    1.77 +		public Rectangle GetRowBounds(int rowNo)
    1.78 +		{
    1.79 +			if (rowNo >= _rowCache.Count)
    1.80 +			{
    1.81 +				int count = _rowCache.Count;
    1.82 +				int y = count > 0 ? _rowCache[count - 1].Bottom : 0;
    1.83 +				for (int i = count; i <= rowNo; i++)
    1.84 +				{
    1.85 +					int height = GetRowHeight(i);
    1.86 +					_rowCache.Add(new Rectangle(0, y, 0, height));
    1.87 +					y += height;
    1.88 +				}
    1.89 +				if (rowNo < _rowCache.Count - 1)
    1.90 +					return Rectangle.Empty;
    1.91 +			}
    1.92 +			if (rowNo >= 0 && rowNo < _rowCache.Count)
    1.93 +				return _rowCache[rowNo];
    1.94 +			else
    1.95 +				return Rectangle.Empty;
    1.96 +		}
    1.97 +
    1.98 +		private int GetRowHeight(int rowNo)
    1.99 +		{
   1.100 +			if (rowNo < _treeView.RowMap.Count)
   1.101 +			{
   1.102 +				TreeNodeAdv node = _treeView.RowMap[rowNo];
   1.103 +				if (node.Height == null)
   1.104 +				{
   1.105 +					int res = 0;
   1.106 +					_measureContext.Font = _treeView.Font;
   1.107 +					foreach (NodeControl nc in _treeView.NodeControls)
   1.108 +					{
   1.109 +						int h = nc.GetActualSize(node, _measureContext).Height;
   1.110 +						if (h > res)
   1.111 +							res = h;
   1.112 +					}
   1.113 +					node.Height = res;
   1.114 +				}
   1.115 +				return node.Height.Value;
   1.116 +			}
   1.117 +			else
   1.118 +				return 0;
   1.119 +		}
   1.120 +
   1.121 +		public int GetRowAt(Point point)
   1.122 +		{
   1.123 +			int py = point.Y - _treeView.ColumnHeaderHeight;
   1.124 +			int y = 0;
   1.125 +			for (int i = _treeView.FirstVisibleRow; i < _treeView.RowCount; i++)
   1.126 +			{
   1.127 +				int h = GetRowHeight(i);
   1.128 +				if (py >= y && py < y + h)
   1.129 +					return i;
   1.130 +				else
   1.131 +					y += h;
   1.132 +			}
   1.133 +			return -1;
   1.134 +		}
   1.135 +
   1.136 +		public int GetFirstRow(int lastPageRow)
   1.137 +		{
   1.138 +			int pageHeight = _treeView.DisplayRectangle.Height - _treeView.ColumnHeaderHeight;
   1.139 +			int y = 0;
   1.140 +			for (int i = lastPageRow; i >= 0; i--)
   1.141 +			{
   1.142 +				y += GetRowHeight(i);
   1.143 +				if (y > pageHeight)
   1.144 +					return Math.Max(0, i + 1);
   1.145 +			}
   1.146 +			return 0;
   1.147 +		}
   1.148 +
   1.149 +		public void ClearCache()
   1.150 +		{
   1.151 +			_rowCache.Clear();
   1.152 +		}
   1.153 +	}
   1.154 +}