context: using System;
  using System.ComponentModel;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.Windows.Forms;
  using System.Windows.Forms.VisualStyles;
  
  namespace Aga.Controls.Tree
  {
  	[TypeConverter(typeof(TreeColumn.TreeColumnConverter)), DesignTimeVisible(false), ToolboxItem(false)]
  	public class TreeColumn : Component
  	{
  		private class TreeColumnConverter : ComponentConverter
  		{
  			public TreeColumnConverter()
  				: base(typeof(TreeColumn))
  			{
  			}
  
  			public override bool GetPropertiesSupported(ITypeDescriptorContext context)
  			{
  				return false;
  			}
  		}
  
  		private const int HeaderLeftMargin = 5;
          private const int HeaderRightMargin = 5;   
  		private const int SortOrderMarkMargin = 8;
  
          private TextFormatFlags _headerFlags;
          private TextFormatFlags _baseHeaderFlags = TextFormatFlags.NoPadding | 
                                                     TextFormatFlags.EndEllipsis |
                                                     TextFormatFlags.VerticalCenter |
  												TextFormatFlags.PreserveGraphicsTranslateTransform;
  
  		#region Properties
  
          private TreeColumnCollection _owner;
  		internal TreeColumnCollection Owner
  		{
  			get { return _owner; }
  			set { _owner = value; }
  		}
  
  		[Browsable(false)]
  		public int Index
  		{
  			get 
  			{
  				if (Owner != null)
  					return Owner.IndexOf(this);
  				else
  					return -1;
  			}
  		}
  
  		private object _tag;
  		public object Tag
  		{
  			get { return _tag; }
  			set { _tag = value; }
  		}
  
  		private string _header;
  		[Localizable(true)]
  		public string Header
  		{
  			get { return _header; }
  			set 
  			{ 
  				_header = value;
  				OnHeaderChanged();
  			}
  		}
  
  		private string _tooltipText;
  		[Localizable(true)]
  		public string TooltipText
  		{
  			get { return _tooltipText; }
  			set { _tooltipText = value; }
  		}
  
  		private int _width;
  		[DefaultValue(50), Localizable(true)]
  		public int Width
  		{
  			get
              {
                  return _width;
              }
  			set 
  			{
  				if (_width != value)
  				{
                      _width = Math.Max(MinColumnWidth, value);
                      if (_maxColumnWidth > 0)
                      {
                          _width = Math.Min(_width, MaxColumnWidth);
                      }
  					OnWidthChanged();
  				}
  			}
  		}
  
          private int _minColumnWidth;
          [DefaultValue(0)]
          public int MinColumnWidth
          {
              get { return _minColumnWidth; }
              set
              {
  				if (value < 0)
  					throw new ArgumentOutOfRangeException("value");
  
  				_minColumnWidth = value;
                  Width = Math.Max(value, Width);
              }
          }
  
          private int _maxColumnWidth;
          [DefaultValue(0)]
          public int MaxColumnWidth
          {
              get { return _maxColumnWidth; }
              set
              {
  				if (value < 0)
  					throw new ArgumentOutOfRangeException("value");
  
  				_maxColumnWidth = value;
  				if (value > 0)
  					Width = Math.Min(value, _width);
              }
          }
  
  		private bool _visible = true;
  		[DefaultValue(true)]
  		public bool IsVisible
  		{
  			get { return _visible; }
  			set 
  			{ 
  				_visible = value;
  				OnIsVisibleChanged();
  			}
  		}
  
  		private HorizontalAlignment _textAlign = HorizontalAlignment.Left;
  		[DefaultValue(HorizontalAlignment.Left)]
  		public HorizontalAlignment TextAlign
  		{
  			get { return _textAlign; }
  			set 
  			{
  				if (value != _textAlign)
  				{
  					_textAlign = value;
                      _headerFlags = _baseHeaderFlags | TextHelper.TranslateAligmentToFlag(value);
  					OnHeaderChanged();
  				}
  			}
  		}
  
          private bool _sortable = false;
          [DefaultValue(false)]
          public bool Sortable
          {
              get { return _sortable; }
              set { _sortable = value; }
          }
  
  		private SortOrder _sort_order = SortOrder.None;
  		public SortOrder SortOrder
  		{
  			get { return _sort_order; }
  			set
  			{
  				if (value == _sort_order)
  					return;
  				_sort_order = value;
  				OnSortOrderChanged();
  			}
  		}
  
  		public Size SortMarkSize
  		{
  			get
  			{
  				if (Application.RenderWithVisualStyles)
  					return new Size(9, 5);
  				else
  					return new Size(7, 4);
  			}
  		}
  		#endregion
  
  		public TreeColumn(): 
  			this(string.Empty, 50)
  		{
  		}
  
          public TreeColumn(string header, int width)
  		{
  			_header = header;
  			_width = width;
              _headerFlags = _baseHeaderFlags | TextFormatFlags.Left;
  		}
  
  		public override string ToString()
  		{
  			if (string.IsNullOrEmpty(Header))
  				return GetType().Name;
  			else
  				return Header;
  		}
  
  		protected override void Dispose(bool disposing)
  		{
  			base.Dispose(disposing);
  		}
  
  		#region Draw
  
  		private static VisualStyleRenderer _normalRenderer;
  		private static VisualStyleRenderer _pressedRenderer;
  		private static VisualStyleRenderer _hotRenderer;
  
  		private static void CreateRenderers()
  		{
  			if (Application.RenderWithVisualStyles && _normalRenderer == null)
  			{
  				_normalRenderer = new VisualStyleRenderer(VisualStyleElement.Header.Item.Normal);
  				_pressedRenderer = new VisualStyleRenderer(VisualStyleElement.Header.Item.Pressed);
  				_hotRenderer = new VisualStyleRenderer(VisualStyleElement.Header.Item.Hot);
  			}
  		}
  
  		internal Bitmap CreateGhostImage(Rectangle bounds, Font font)
  		{
  			Bitmap b = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb);
  
gt: 			Graphics gr = Graphics.FromImage(b);