moel@345: using System;
moel@345: using System.Collections.Generic;
moel@345: using System.Text;
moel@345: using System.Drawing;
moel@345: using System.Windows.Forms;
moel@345: 
moel@345: namespace Aga.Controls.Tree
moel@345: {
moel@345: 	internal class ReorderColumnState : ColumnState
moel@345: 	{
moel@345: 		#region Properties
moel@345: 
moel@345: 		private Point _location;
moel@345: 		public Point Location
moel@345: 		{
moel@345: 			get { return _location; }
moel@345: 		}
moel@345: 
moel@345: 		private Bitmap _ghostImage;
moel@345: 		public Bitmap GhostImage
moel@345: 		{
moel@345: 			get { return _ghostImage; }
moel@345: 		}
moel@345: 
moel@345: 		private TreeColumn _dropColumn;
moel@345: 		public TreeColumn DropColumn
moel@345: 		{
moel@345: 			get { return _dropColumn; }
moel@345: 		}
moel@345: 
moel@345: 		private int _dragOffset;
moel@345: 		public int DragOffset
moel@345: 		{
moel@345: 			get { return _dragOffset; }
moel@345: 		}
moel@345: 
moel@345: 		#endregion
moel@345: 
moel@345: 		public ReorderColumnState(TreeViewAdv tree, TreeColumn column, Point initialMouseLocation)
moel@345: 			: base(tree, column)
moel@345: 		{
moel@345: 			_location = new Point(initialMouseLocation.X + Tree.OffsetX, 0);
moel@345: 			_dragOffset = tree.GetColumnX(column) - initialMouseLocation.X;
moel@345: 			_ghostImage = column.CreateGhostImage(new Rectangle(0, 0, column.Width, tree.ColumnHeaderHeight), tree.Font);
moel@345: 		}
moel@345: 
moel@345: 		public override void KeyDown(KeyEventArgs args)
moel@345: 		{
moel@345: 			args.Handled = true;
moel@345: 			if (args.KeyCode == Keys.Escape)
moel@345: 				FinishResize();
moel@345: 		}
moel@345: 
moel@345: 		public override void MouseDown(TreeNodeAdvMouseEventArgs args)
moel@345: 		{
moel@345: 		}
moel@345: 
moel@345: 		public override void MouseUp(TreeNodeAdvMouseEventArgs args)
moel@345: 		{
moel@345: 			FinishResize();
moel@345: 		}
moel@345: 
moel@345: 		public override bool MouseMove(MouseEventArgs args)
moel@345: 		{
moel@345: 			_dropColumn = null;
moel@345: 			_location = new Point(args.X + Tree.OffsetX, 0);
moel@345: 			int x = 0;
moel@345: 			foreach (TreeColumn c in Tree.Columns)
moel@345: 			{
moel@345: 				if (c.IsVisible)
moel@345: 				{
moel@345: 					if (_location.X < x + c.Width / 2)
moel@345: 					{
moel@345: 						_dropColumn = c;
moel@345: 						break;
moel@345: 					}
moel@345: 					x += c.Width;
moel@345: 				}
moel@345: 			}
moel@345: 			Tree.UpdateHeaders();
moel@345: 			return true;
moel@345: 		}
moel@345: 
moel@345: 		private void FinishResize()
moel@345: 		{
moel@345: 			Tree.ChangeInput();
moel@345: 			if (Column == DropColumn)
moel@345: 				Tree.UpdateView();
moel@345: 			else
moel@345: 			{
moel@345: 				Tree.Columns.Remove(Column);
moel@345: 				if (DropColumn == null)
moel@345: 					Tree.Columns.Add(Column);
moel@345: 				else
moel@345: 					Tree.Columns.Insert(Tree.Columns.IndexOf(DropColumn), Column);
moel@345: 
moel@345: 				Tree.OnColumnReordered(Column);
moel@345: 			}
moel@345: 		}
moel@345: 	}
moel@345: }