External/Aga.Controls/Tree/AutoRowHeightLayout.cs
author moel.mich
Sun, 27 May 2012 15:16:19 +0000
changeset 345 0c551e8818e0
permissions -rw-r--r--
Added the source code of Aga.Controls (TreeViewAdv for .Net) version 1.7.0.0.
moel@345
     1
using System;
moel@345
     2
using System.Collections.Generic;
moel@345
     3
using System.Text;
moel@345
     4
using System.Drawing;
moel@345
     5
using Aga.Controls.Tree.NodeControls;
moel@345
     6
moel@345
     7
namespace Aga.Controls.Tree
moel@345
     8
{
moel@345
     9
	public class AutoRowHeightLayout: IRowLayout
moel@345
    10
	{
moel@345
    11
		private DrawContext _measureContext;
moel@345
    12
		private TreeViewAdv _treeView;
moel@345
    13
		private List<Rectangle> _rowCache;
moel@345
    14
moel@345
    15
		public AutoRowHeightLayout(TreeViewAdv treeView, int rowHeight)
moel@345
    16
		{
moel@345
    17
			_rowCache = new List<Rectangle>();
moel@345
    18
			_treeView = treeView;
moel@345
    19
			PreferredRowHeight = rowHeight;
moel@345
    20
			_measureContext = new DrawContext();
moel@345
    21
			_measureContext.Graphics = Graphics.FromImage(new Bitmap(1, 1));
moel@345
    22
		}
moel@345
    23
moel@345
    24
		private int _rowHeight;
moel@345
    25
		public int PreferredRowHeight
moel@345
    26
		{
moel@345
    27
			get { return _rowHeight; }
moel@345
    28
			set { _rowHeight = value; }
moel@345
    29
		}
moel@345
    30
moel@345
    31
moel@345
    32
		public int PageRowCount
moel@345
    33
		{
moel@345
    34
			get 
moel@345
    35
			{
moel@345
    36
				if (_treeView.RowCount == 0)
moel@345
    37
					return 0;
moel@345
    38
				else
moel@345
    39
				{
moel@345
    40
					int pageHeight = _treeView.DisplayRectangle.Height - _treeView.ColumnHeaderHeight;
moel@345
    41
					int y = 0;
moel@345
    42
					for (int i = _treeView.RowCount - 1; i >= 0; i--)
moel@345
    43
					{
moel@345
    44
						y += GetRowHeight(i);
moel@345
    45
						if (y > pageHeight)
moel@345
    46
							return Math.Max(0, _treeView.RowCount - 1 - i);
moel@345
    47
					}
moel@345
    48
					return _treeView.RowCount;
moel@345
    49
				}
moel@345
    50
			}
moel@345
    51
		}
moel@345
    52
moel@345
    53
		public int CurrentPageSize
moel@345
    54
		{
moel@345
    55
			get
moel@345
    56
			{
moel@345
    57
				if (_treeView.RowCount == 0)
moel@345
    58
					return 0;
moel@345
    59
				else
moel@345
    60
				{
moel@345
    61
					int pageHeight = _treeView.DisplayRectangle.Height - _treeView.ColumnHeaderHeight;
moel@345
    62
					int y = 0;
moel@345
    63
					for (int i = _treeView.FirstVisibleRow; i < _treeView.RowCount; i++)
moel@345
    64
					{
moel@345
    65
						y += GetRowHeight(i);
moel@345
    66
						if (y > pageHeight)
moel@345
    67
							return Math.Max(0, i - _treeView.FirstVisibleRow);
moel@345
    68
					}
moel@345
    69
					return Math.Max(0, _treeView.RowCount - _treeView.FirstVisibleRow);
moel@345
    70
				}
moel@345
    71
			}
moel@345
    72
		}
moel@345
    73
moel@345
    74
		public Rectangle GetRowBounds(int rowNo)
moel@345
    75
		{
moel@345
    76
			if (rowNo >= _rowCache.Count)
moel@345
    77
			{
moel@345
    78
				int count = _rowCache.Count;
moel@345
    79
				int y = count > 0 ? _rowCache[count - 1].Bottom : 0;
moel@345
    80
				for (int i = count; i <= rowNo; i++)
moel@345
    81
				{
moel@345
    82
					int height = GetRowHeight(i);
moel@345
    83
					_rowCache.Add(new Rectangle(0, y, 0, height));
moel@345
    84
					y += height;
moel@345
    85
				}
moel@345
    86
				if (rowNo < _rowCache.Count - 1)
moel@345
    87
					return Rectangle.Empty;
moel@345
    88
			}
moel@345
    89
			if (rowNo >= 0 && rowNo < _rowCache.Count)
moel@345
    90
				return _rowCache[rowNo];
moel@345
    91
			else
moel@345
    92
				return Rectangle.Empty;
moel@345
    93
		}
moel@345
    94
moel@345
    95
		private int GetRowHeight(int rowNo)
moel@345
    96
		{
moel@345
    97
			if (rowNo < _treeView.RowMap.Count)
moel@345
    98
			{
moel@345
    99
				TreeNodeAdv node = _treeView.RowMap[rowNo];
moel@345
   100
				if (node.Height == null)
moel@345
   101
				{
moel@345
   102
					int res = 0;
moel@345
   103
					_measureContext.Font = _treeView.Font;
moel@345
   104
					foreach (NodeControl nc in _treeView.NodeControls)
moel@345
   105
					{
moel@345
   106
						int h = nc.GetActualSize(node, _measureContext).Height;
moel@345
   107
						if (h > res)
moel@345
   108
							res = h;
moel@345
   109
					}
moel@345
   110
					node.Height = res;
moel@345
   111
				}
moel@345
   112
				return node.Height.Value;
moel@345
   113
			}
moel@345
   114
			else
moel@345
   115
				return 0;
moel@345
   116
		}
moel@345
   117
moel@345
   118
		public int GetRowAt(Point point)
moel@345
   119
		{
moel@345
   120
			int py = point.Y - _treeView.ColumnHeaderHeight;
moel@345
   121
			int y = 0;
moel@345
   122
			for (int i = _treeView.FirstVisibleRow; i < _treeView.RowCount; i++)
moel@345
   123
			{
moel@345
   124
				int h = GetRowHeight(i);
moel@345
   125
				if (py >= y && py < y + h)
moel@345
   126
					return i;
moel@345
   127
				else
moel@345
   128
					y += h;
moel@345
   129
			}
moel@345
   130
			return -1;
moel@345
   131
		}
moel@345
   132
moel@345
   133
		public int GetFirstRow(int lastPageRow)
moel@345
   134
		{
moel@345
   135
			int pageHeight = _treeView.DisplayRectangle.Height - _treeView.ColumnHeaderHeight;
moel@345
   136
			int y = 0;
moel@345
   137
			for (int i = lastPageRow; i >= 0; i--)
moel@345
   138
			{
moel@345
   139
				y += GetRowHeight(i);
moel@345
   140
				if (y > pageHeight)
moel@345
   141
					return Math.Max(0, i + 1);
moel@345
   142
			}
moel@345
   143
			return 0;
moel@345
   144
		}
moel@345
   145
moel@345
   146
		public void ClearCache()
moel@345
   147
		{
moel@345
   148
			_rowCache.Clear();
moel@345
   149
		}
moel@345
   150
	}
moel@345
   151
}