moel@345: using System; moel@345: using System.Collections.Generic; moel@345: using System.Text; moel@345: using System.Drawing; moel@345: using Aga.Controls.Tree.NodeControls; moel@345: moel@345: namespace Aga.Controls.Tree moel@345: { moel@345: public class AutoRowHeightLayout: IRowLayout moel@345: { moel@345: private DrawContext _measureContext; moel@345: private TreeViewAdv _treeView; moel@345: private List _rowCache; moel@345: moel@345: public AutoRowHeightLayout(TreeViewAdv treeView, int rowHeight) moel@345: { moel@345: _rowCache = new List(); moel@345: _treeView = treeView; moel@345: PreferredRowHeight = rowHeight; moel@345: _measureContext = new DrawContext(); moel@345: _measureContext.Graphics = Graphics.FromImage(new Bitmap(1, 1)); moel@345: } moel@345: moel@345: private int _rowHeight; moel@345: public int PreferredRowHeight moel@345: { moel@345: get { return _rowHeight; } moel@345: set { _rowHeight = value; } moel@345: } moel@345: moel@345: moel@345: public int PageRowCount moel@345: { moel@345: get moel@345: { moel@345: if (_treeView.RowCount == 0) moel@345: return 0; moel@345: else moel@345: { moel@345: int pageHeight = _treeView.DisplayRectangle.Height - _treeView.ColumnHeaderHeight; moel@345: int y = 0; moel@345: for (int i = _treeView.RowCount - 1; i >= 0; i--) moel@345: { moel@345: y += GetRowHeight(i); moel@345: if (y > pageHeight) moel@345: return Math.Max(0, _treeView.RowCount - 1 - i); moel@345: } moel@345: return _treeView.RowCount; moel@345: } moel@345: } moel@345: } moel@345: moel@345: public int CurrentPageSize moel@345: { moel@345: get moel@345: { moel@345: if (_treeView.RowCount == 0) moel@345: return 0; moel@345: else moel@345: { moel@345: int pageHeight = _treeView.DisplayRectangle.Height - _treeView.ColumnHeaderHeight; moel@345: int y = 0; moel@345: for (int i = _treeView.FirstVisibleRow; i < _treeView.RowCount; i++) moel@345: { moel@345: y += GetRowHeight(i); moel@345: if (y > pageHeight) moel@345: return Math.Max(0, i - _treeView.FirstVisibleRow); moel@345: } moel@345: return Math.Max(0, _treeView.RowCount - _treeView.FirstVisibleRow); moel@345: } moel@345: } moel@345: } moel@345: moel@345: public Rectangle GetRowBounds(int rowNo) moel@345: { moel@345: if (rowNo >= _rowCache.Count) moel@345: { moel@345: int count = _rowCache.Count; moel@345: int y = count > 0 ? _rowCache[count - 1].Bottom : 0; moel@345: for (int i = count; i <= rowNo; i++) moel@345: { moel@345: int height = GetRowHeight(i); moel@345: _rowCache.Add(new Rectangle(0, y, 0, height)); moel@345: y += height; moel@345: } moel@345: if (rowNo < _rowCache.Count - 1) moel@345: return Rectangle.Empty; moel@345: } moel@345: if (rowNo >= 0 && rowNo < _rowCache.Count) moel@345: return _rowCache[rowNo]; moel@345: else moel@345: return Rectangle.Empty; moel@345: } moel@345: moel@345: private int GetRowHeight(int rowNo) moel@345: { moel@345: if (rowNo < _treeView.RowMap.Count) moel@345: { moel@345: TreeNodeAdv node = _treeView.RowMap[rowNo]; moel@345: if (node.Height == null) moel@345: { moel@345: int res = 0; moel@345: _measureContext.Font = _treeView.Font; moel@345: foreach (NodeControl nc in _treeView.NodeControls) moel@345: { moel@345: int h = nc.GetActualSize(node, _measureContext).Height; moel@345: if (h > res) moel@345: res = h; moel@345: } moel@345: node.Height = res; moel@345: } moel@345: return node.Height.Value; moel@345: } moel@345: else moel@345: return 0; moel@345: } moel@345: moel@345: public int GetRowAt(Point point) moel@345: { moel@345: int py = point.Y - _treeView.ColumnHeaderHeight; moel@345: int y = 0; moel@345: for (int i = _treeView.FirstVisibleRow; i < _treeView.RowCount; i++) moel@345: { moel@345: int h = GetRowHeight(i); moel@345: if (py >= y && py < y + h) moel@345: return i; moel@345: else moel@345: y += h; moel@345: } moel@345: return -1; moel@345: } moel@345: moel@345: public int GetFirstRow(int lastPageRow) moel@345: { moel@345: int pageHeight = _treeView.DisplayRectangle.Height - _treeView.ColumnHeaderHeight; moel@345: int y = 0; moel@345: for (int i = lastPageRow; i >= 0; i--) moel@345: { moel@345: y += GetRowHeight(i); moel@345: if (y > pageHeight) moel@345: return Math.Max(0, i + 1); moel@345: } moel@345: return 0; moel@345: } moel@345: moel@345: public void ClearCache() moel@345: { moel@345: _rowCache.Clear(); moel@345: } moel@345: } moel@345: }