context: /////////////////////////////////////////////////////////////////////////////
  //
  // (c) 2007 BinaryComponents Ltd.  All Rights Reserved.
  //
  // http://www.binarycomponents.com/
  //
  /////////////////////////////////////////////////////////////////////////////
  
  using System;
  using System.Diagnostics;
  using System.Drawing;
  using System.Windows.Forms;
  using System.Windows.Forms.VisualStyles;
  using BinaryComponents.SuperList.Helper;
  
  namespace BinaryComponents.SuperList.Sections
  {
  	public class HeaderColumnSection : Section
  	{
  		public enum DisplayMode
  		{
  			Header,
  			Customise
  		} ;
  
  		public HeaderColumnSection( ISectionHost host, DisplayMode displayMode, Column column )
  			: base( host )
  		{
  			Debug.Assert( column != null );
  			CanDrag = true;
  			_displayMode = displayMode;
  			_column = column;
  			column.DataChanged += column_DataChanged;
  		}
  
  		public override void Dispose()
  		{
  			base.Dispose();
  			if( _column != null )
  			{
  				_column.DataChanged -= column_DataChanged;
  				_column = null;
  			}
  		}
  
  		public override bool CanDropInVoid
  		{
  			get
  			{
  				return Parent.AllowColumnsToBeDroppedInVoid;
  			}
  		}
  
  		public Column Column
  		{
  			get
  			{
  				return _column;
  			}
  		}
  
  		public new HeaderColumnSectionContainer Parent
  		{
  			get
  			{
  				return (HeaderColumnSectionContainer)base.Parent;
  			}
  		}
  
  
  		public bool LeftMouseButtonPressed
  		{
  			get
  			{
  				return _leftMouseButtonPressed;
  			}
  		}
  
  		public DisplayMode Mode
  		{
  			get
  			{
  				return _displayMode;
  			}
  		}
  
  		public int ArrowWidth
  		{
  			get
  			{
  				return _arrowWidth;
  			}
  		}
  
  		public int ArrowSpaceXMargin
  		{
  			get
  			{
  				return _arrowSpaceXMargin;
  			}
  		}
  
  		public override void DroppedInVoid()
  		{
  			ListControl listControl = Host as ListControl;
  			if( listControl != null && !listControl.ShowCustomizeSection )
  			{
  				return;
  			}
  
  			Parent.Columns.Remove( Column );
  		}
  
  		public override void Paint( GraphicsSettings gs, Rectangle clipRect )
  		{
  			using( SolidBrush brush = new SolidBrush( Host.TextColor ) )
  			{
  				Rectangle rc = Rectangle;
  				int offset = 0;
  
  				if( Parent.LayoutController != null )
  				{
  					offset = Parent.LayoutController.CurrentHorizontalScrollPosition;
  					rc.X -= offset;
  				}
  
  				if( _displayMode == DisplayMode.Header )
  				{
  					if( VisualStyleRenderer.IsSupported )
  					{
  						VisualStyleRenderer renderer = GetRenderer();
  
  						renderer.DrawBackground( gs.Graphics, rc );
  					}
  					else
  					{
  						gs.Graphics.FillRectangle( SystemBrushes.Control, rc );
  
  						if( _leftMouseButtonPressed )
  						{
  							ControlPaint.DrawBorder3D( gs.Graphics, rc, Border3DStyle.Sunken );
  						}
  						else
  						{
  							ControlPaint.DrawBorder3D( gs.Graphics, rc, Border3DStyle.Raised );
  						}
  					}
  				}
  				else
  				{
  					DrawBox( gs.Graphics, rc );
  				}
  
  				DrawIcon( gs.Graphics, ref rc );
  
  				const int textMargin = 2;
  
  				rc.X += textMargin;
  				rc.Width -= textMargin;
  
  				if( Parent.LayoutController != null )
  				{
  					if( _column.ShowHeaderSortArrow )
  					{
  						rc.Width -= _arrowWidth + _arrowSpaceXMargin * 2;
  					}
  				}
  
  				rc.Y -= 2;
  
  				DrawCaption( gs, rc );
  
  				DrawSortArrow( gs, rc );
  			}
  		}
  
  		protected virtual void DrawCaption( GraphicsSettings gs, Rectangle rc )
  		{
  			TextRendererEx.DrawText( gs.Graphics,
  															_column.Caption,
  															SystemFonts.MenuFont,
  															rc,
  															Color.Black,
  															GetTextFormatFlags() );
  		}
  
  		protected virtual void DrawBox( Graphics g, Rectangle rc )
  		{
  			ControlPaint.DrawBorder3D( g, rc.Left, rc.Top, rc.Width, rc.Height - 1, Border3DStyle.RaisedInner, Border3DSide.All );
  		}
  
  		protected virtual void DrawSortArrow( GraphicsSettings gs, Rectangle rc )
  		{
  			if( !_column.ShowHeaderSortArrow )
  			{
  				return;
  			}
  
  			if( rc.Width <= 0 )
  			{
  				return;
  			}
  
  			int offset = 0;
  
  			if( Parent.LayoutController != null )
  			{
  				offset = Parent.LayoutController.CurrentHorizontalScrollPosition;
  			}
  			//
  			// Draw sort arrows
  
gt: 			const int halfArrowWidth = _arrowWidth / 2;