External/Aga.Controls/Tree/TreeColumn.cs
changeset 345 0c551e8818e0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/Tree/TreeColumn.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,371 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Text;
     1.7 +using System.ComponentModel;
     1.8 +using System.Windows.Forms;
     1.9 +using System.Drawing;
    1.10 +using System.Windows.Forms.VisualStyles;
    1.11 +using System.Drawing.Imaging;
    1.12 +
    1.13 +namespace Aga.Controls.Tree
    1.14 +{
    1.15 +	[TypeConverter(typeof(TreeColumn.TreeColumnConverter)), DesignTimeVisible(false), ToolboxItem(false)]
    1.16 +	public class TreeColumn : Component
    1.17 +	{
    1.18 +		private class TreeColumnConverter : ComponentConverter
    1.19 +		{
    1.20 +			public TreeColumnConverter()
    1.21 +				: base(typeof(TreeColumn))
    1.22 +			{
    1.23 +			}
    1.24 +
    1.25 +			public override bool GetPropertiesSupported(ITypeDescriptorContext context)
    1.26 +			{
    1.27 +				return false;
    1.28 +			}
    1.29 +		}
    1.30 +
    1.31 +		private const int HeaderLeftMargin = 5;
    1.32 +        private const int HeaderRightMargin = 5;   
    1.33 +		private const int SortOrderMarkMargin = 8;
    1.34 +
    1.35 +        private TextFormatFlags _headerFlags;
    1.36 +        private TextFormatFlags _baseHeaderFlags = TextFormatFlags.NoPadding | 
    1.37 +                                                   TextFormatFlags.EndEllipsis |
    1.38 +                                                   TextFormatFlags.VerticalCenter |
    1.39 +												TextFormatFlags.PreserveGraphicsTranslateTransform;
    1.40 +
    1.41 +		#region Properties
    1.42 +
    1.43 +        private TreeColumnCollection _owner;
    1.44 +		internal TreeColumnCollection Owner
    1.45 +		{
    1.46 +			get { return _owner; }
    1.47 +			set { _owner = value; }
    1.48 +		}
    1.49 +
    1.50 +		[Browsable(false)]
    1.51 +		public int Index
    1.52 +		{
    1.53 +			get 
    1.54 +			{
    1.55 +				if (Owner != null)
    1.56 +					return Owner.IndexOf(this);
    1.57 +				else
    1.58 +					return -1;
    1.59 +			}
    1.60 +		}
    1.61 +
    1.62 +		private string _header;
    1.63 +		[Localizable(true)]
    1.64 +		public string Header
    1.65 +		{
    1.66 +			get { return _header; }
    1.67 +			set 
    1.68 +			{ 
    1.69 +				_header = value;
    1.70 +				OnHeaderChanged();
    1.71 +			}
    1.72 +		}
    1.73 +
    1.74 +		private string _tooltipText;
    1.75 +		[Localizable(true)]
    1.76 +		public string TooltipText
    1.77 +		{
    1.78 +			get { return _tooltipText; }
    1.79 +			set { _tooltipText = value; }
    1.80 +		}
    1.81 +
    1.82 +		private int _width;
    1.83 +		[DefaultValue(50), Localizable(true)]
    1.84 +		public int Width
    1.85 +		{
    1.86 +			get
    1.87 +            {
    1.88 +                return _width;
    1.89 +            }
    1.90 +			set 
    1.91 +			{
    1.92 +				if (_width != value)
    1.93 +				{
    1.94 +                    _width = Math.Max(MinColumnWidth, value);
    1.95 +                    if (_maxColumnWidth > 0)
    1.96 +                    {
    1.97 +                        _width = Math.Min(_width, MaxColumnWidth);
    1.98 +                    }
    1.99 +					OnWidthChanged();
   1.100 +				}
   1.101 +			}
   1.102 +		}
   1.103 +
   1.104 +        private int _minColumnWidth;
   1.105 +        [DefaultValue(0)]
   1.106 +        public int MinColumnWidth
   1.107 +        {
   1.108 +            get { return _minColumnWidth; }
   1.109 +            set
   1.110 +            {
   1.111 +				if (value < 0)
   1.112 +					throw new ArgumentOutOfRangeException("value");
   1.113 +
   1.114 +				_minColumnWidth = value;
   1.115 +                Width = Math.Max(value, Width);
   1.116 +            }
   1.117 +        }
   1.118 +
   1.119 +        private int _maxColumnWidth;
   1.120 +        [DefaultValue(0)]
   1.121 +        public int MaxColumnWidth
   1.122 +        {
   1.123 +            get { return _maxColumnWidth; }
   1.124 +            set
   1.125 +            {
   1.126 +				if (value < 0)
   1.127 +					throw new ArgumentOutOfRangeException("value");
   1.128 +
   1.129 +				_maxColumnWidth = value;
   1.130 +				if (value > 0)
   1.131 +					Width = Math.Min(value, _width);
   1.132 +            }
   1.133 +        }
   1.134 +
   1.135 +		private bool _visible = true;
   1.136 +		[DefaultValue(true)]
   1.137 +		public bool IsVisible
   1.138 +		{
   1.139 +			get { return _visible; }
   1.140 +			set 
   1.141 +			{ 
   1.142 +				_visible = value;
   1.143 +				OnIsVisibleChanged();
   1.144 +			}
   1.145 +		}
   1.146 +
   1.147 +		private HorizontalAlignment _textAlign = HorizontalAlignment.Left;
   1.148 +		[DefaultValue(HorizontalAlignment.Left)]
   1.149 +		public HorizontalAlignment TextAlign
   1.150 +		{
   1.151 +			get { return _textAlign; }
   1.152 +			set 
   1.153 +			{
   1.154 +				if (value != _textAlign)
   1.155 +				{
   1.156 +					_textAlign = value;
   1.157 +                    _headerFlags = _baseHeaderFlags | TextHelper.TranslateAligmentToFlag(value);
   1.158 +					OnHeaderChanged();
   1.159 +				}
   1.160 +			}
   1.161 +		}
   1.162 +
   1.163 +        private bool _sortable = false;
   1.164 +        [DefaultValue(false)]
   1.165 +        public bool Sortable
   1.166 +        {
   1.167 +            get { return _sortable; }
   1.168 +            set { _sortable = value; }
   1.169 +        }
   1.170 +
   1.171 +		private SortOrder _sort_order = SortOrder.None;
   1.172 +		public SortOrder SortOrder
   1.173 +		{
   1.174 +			get { return _sort_order; }
   1.175 +			set
   1.176 +			{
   1.177 +				if (value == _sort_order)
   1.178 +					return;
   1.179 +				_sort_order = value;
   1.180 +				OnSortOrderChanged();
   1.181 +			}
   1.182 +		}
   1.183 +
   1.184 +		public Size SortMarkSize
   1.185 +		{
   1.186 +			get
   1.187 +			{
   1.188 +				if (Application.RenderWithVisualStyles)
   1.189 +					return new Size(9, 5);
   1.190 +				else
   1.191 +					return new Size(7, 4);
   1.192 +			}
   1.193 +		}
   1.194 +		#endregion
   1.195 +
   1.196 +		public TreeColumn(): 
   1.197 +			this(string.Empty, 50)
   1.198 +		{
   1.199 +		}
   1.200 +
   1.201 +        public TreeColumn(string header, int width)
   1.202 +		{
   1.203 +			_header = header;
   1.204 +			_width = width;
   1.205 +            _headerFlags = _baseHeaderFlags | TextFormatFlags.Left;
   1.206 +		}
   1.207 +
   1.208 +		public override string ToString()
   1.209 +		{
   1.210 +			if (string.IsNullOrEmpty(Header))
   1.211 +				return GetType().Name;
   1.212 +			else
   1.213 +				return Header;
   1.214 +		}
   1.215 +
   1.216 +		protected override void Dispose(bool disposing)
   1.217 +		{
   1.218 +			base.Dispose(disposing);
   1.219 +		}
   1.220 +
   1.221 +		#region Draw
   1.222 +
   1.223 +		private static VisualStyleRenderer _normalRenderer;
   1.224 +		private static VisualStyleRenderer _pressedRenderer;
   1.225 +		private static VisualStyleRenderer _hotRenderer;
   1.226 +
   1.227 +		private static void CreateRenderers()
   1.228 +		{
   1.229 +			if (Application.RenderWithVisualStyles && _normalRenderer == null)
   1.230 +			{
   1.231 +				_normalRenderer = new VisualStyleRenderer(VisualStyleElement.Header.Item.Normal);
   1.232 +				_pressedRenderer = new VisualStyleRenderer(VisualStyleElement.Header.Item.Pressed);
   1.233 +				_hotRenderer = new VisualStyleRenderer(VisualStyleElement.Header.Item.Hot);
   1.234 +			}
   1.235 +		}
   1.236 +
   1.237 +		internal Bitmap CreateGhostImage(Rectangle bounds, Font font)
   1.238 +		{
   1.239 +			Bitmap b = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
   1.240 +			Graphics gr = Graphics.FromImage(b);
   1.241 +			gr.FillRectangle(SystemBrushes.ControlDark, bounds);
   1.242 +			DrawContent(gr, bounds, font);
   1.243 +			BitmapHelper.SetAlphaChanelValue(b, 150);
   1.244 +			return b;
   1.245 +		}
   1.246 +
   1.247 +		internal void Draw(Graphics gr, Rectangle bounds, Font font, bool pressed, bool hot)
   1.248 +		{
   1.249 +			DrawBackground(gr, bounds, pressed, hot);
   1.250 +			DrawContent(gr, bounds, font);
   1.251 +		}
   1.252 +
   1.253 +        private void DrawContent(Graphics gr, Rectangle bounds, Font font)
   1.254 +        {
   1.255 +            Rectangle innerBounds = new Rectangle(bounds.X + HeaderLeftMargin, bounds.Y,
   1.256 +                                   bounds.Width - HeaderLeftMargin - HeaderRightMargin,
   1.257 +                                   bounds.Height);
   1.258 +
   1.259 +            if (SortOrder != SortOrder.None)
   1.260 +				innerBounds.Width -= (SortMarkSize.Width + SortOrderMarkMargin);
   1.261 +
   1.262 +            Size maxTextSize = TextRenderer.MeasureText(gr, Header, font, innerBounds.Size, TextFormatFlags.NoPadding);
   1.263 +			Size textSize = TextRenderer.MeasureText(gr, Header, font, innerBounds.Size, _baseHeaderFlags);
   1.264 +
   1.265 +            if (SortOrder != SortOrder.None)
   1.266 +            {
   1.267 +				int tw = Math.Min(textSize.Width, innerBounds.Size.Width);
   1.268 +
   1.269 +                int x = 0;
   1.270 +                if (TextAlign == HorizontalAlignment.Left)
   1.271 +					x = innerBounds.X + tw + SortOrderMarkMargin;
   1.272 +                else if (TextAlign == HorizontalAlignment.Right)
   1.273 +					x = innerBounds.Right + SortOrderMarkMargin;
   1.274 +                else
   1.275 +					x = innerBounds.X + tw + (innerBounds.Width - tw) / 2 + SortOrderMarkMargin;
   1.276 +                DrawSortMark(gr, bounds, x);
   1.277 +			}
   1.278 +
   1.279 +			if (textSize.Width < maxTextSize.Width)
   1.280 +				TextRenderer.DrawText(gr, Header, font, innerBounds, SystemColors.ControlText, _baseHeaderFlags | TextFormatFlags.Left);
   1.281 +            else
   1.282 +				TextRenderer.DrawText(gr, Header, font, innerBounds, SystemColors.ControlText, _headerFlags);
   1.283 +        }
   1.284 +
   1.285 +		private void DrawSortMark(Graphics gr, Rectangle bounds, int x)
   1.286 +		{
   1.287 +			int y = bounds.Y + bounds.Height / 2 - 2;
   1.288 +			x = Math.Max(x, bounds.X + SortOrderMarkMargin);
   1.289 +
   1.290 +            int w2 = SortMarkSize.Width / 2;
   1.291 +            if (SortOrder == SortOrder.Ascending)
   1.292 +            {
   1.293 +                Point[] points = new Point[] { new Point(x, y), new Point(x + SortMarkSize.Width, y), new Point(x + w2, y + SortMarkSize.Height) };
   1.294 +                gr.FillPolygon(SystemBrushes.ControlDark, points);
   1.295 +            }
   1.296 +            else if (SortOrder == SortOrder.Descending)
   1.297 +            {
   1.298 +                Point[] points = new Point[] { new Point(x - 1, y + SortMarkSize.Height), new Point(x + SortMarkSize.Width, y + SortMarkSize.Height), new Point(x + w2, y - 1) };
   1.299 +                gr.FillPolygon(SystemBrushes.ControlDark, points);
   1.300 +            }
   1.301 +		}
   1.302 +
   1.303 +		internal static void DrawDropMark(Graphics gr, Rectangle rect)
   1.304 +		{
   1.305 +			gr.FillRectangle(SystemBrushes.HotTrack, rect.X-1, rect.Y, 2, rect.Height);
   1.306 +		}
   1.307 +
   1.308 +		internal static void DrawBackground(Graphics gr, Rectangle bounds, bool pressed, bool hot)
   1.309 +		{
   1.310 +			if (Application.RenderWithVisualStyles)
   1.311 +			{
   1.312 +				CreateRenderers();
   1.313 +				if (pressed)
   1.314 +					_pressedRenderer.DrawBackground(gr, bounds);
   1.315 +				else if (hot)
   1.316 +					_hotRenderer.DrawBackground(gr, bounds);
   1.317 +				else
   1.318 +					_normalRenderer.DrawBackground(gr, bounds);
   1.319 +			}
   1.320 +			else
   1.321 +			{
   1.322 +				gr.FillRectangle(SystemBrushes.Control, bounds);
   1.323 +				Pen p1 = SystemPens.ControlLightLight;
   1.324 +				Pen p2 = SystemPens.ControlDark;
   1.325 +				Pen p3 = SystemPens.ControlDarkDark;
   1.326 +				if (pressed)
   1.327 +					gr.DrawRectangle(p2, bounds.X, bounds.Y, bounds.Width, bounds.Height);
   1.328 +				else
   1.329 +				{
   1.330 +					gr.DrawLine(p1, bounds.X, bounds.Y, bounds.Right, bounds.Y);
   1.331 +					gr.DrawLine(p3, bounds.X, bounds.Bottom, bounds.Right, bounds.Bottom);
   1.332 +					gr.DrawLine(p3, bounds.Right - 1, bounds.Y, bounds.Right - 1, bounds.Bottom - 1);
   1.333 +					gr.DrawLine(p1, bounds.Left, bounds.Y + 1, bounds.Left, bounds.Bottom - 2);
   1.334 +					gr.DrawLine(p2, bounds.Right - 2, bounds.Y + 1, bounds.Right - 2, bounds.Bottom - 2);
   1.335 +					gr.DrawLine(p2, bounds.X, bounds.Bottom - 1, bounds.Right - 2, bounds.Bottom - 1);
   1.336 +				}
   1.337 +			}
   1.338 +		}
   1.339 +
   1.340 +		#endregion
   1.341 +
   1.342 +		#region Events
   1.343 +
   1.344 +		public event EventHandler HeaderChanged;
   1.345 +		private void OnHeaderChanged()
   1.346 +		{
   1.347 +			if (HeaderChanged != null)
   1.348 +				HeaderChanged(this, EventArgs.Empty);
   1.349 +		}
   1.350 +
   1.351 +		public event EventHandler SortOrderChanged;
   1.352 +		private void OnSortOrderChanged()
   1.353 +		{
   1.354 +			if (SortOrderChanged != null)
   1.355 +				SortOrderChanged(this, EventArgs.Empty);
   1.356 +		}
   1.357 +
   1.358 +		public event EventHandler IsVisibleChanged;
   1.359 +		private void OnIsVisibleChanged()
   1.360 +		{
   1.361 +			if (IsVisibleChanged != null)
   1.362 +				IsVisibleChanged(this, EventArgs.Empty);
   1.363 +		}
   1.364 +
   1.365 +		public event EventHandler WidthChanged;
   1.366 +		private void OnWidthChanged()
   1.367 +		{
   1.368 +			if (WidthChanged != null)
   1.369 +				WidthChanged(this, EventArgs.Empty);
   1.370 +		}
   1.371 +
   1.372 +		#endregion
   1.373 +	}
   1.374 +}