External/Aga.Controls/Tree/Input/NormalInputState.cs
changeset 345 0c551e8818e0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/Tree/Input/NormalInputState.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,202 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Text;
     1.7 +using System.Windows.Forms;
     1.8 +
     1.9 +namespace Aga.Controls.Tree
    1.10 +{
    1.11 +	internal class NormalInputState : InputState
    1.12 +	{
    1.13 +		private bool _mouseDownFlag = false;
    1.14 +
    1.15 +		public NormalInputState(TreeViewAdv tree) : base(tree)
    1.16 +		{
    1.17 +		}
    1.18 +
    1.19 +		public override void KeyDown(KeyEventArgs args)
    1.20 +		{
    1.21 +			if (Tree.CurrentNode == null && Tree.Root.Nodes.Count > 0)
    1.22 +				Tree.CurrentNode = Tree.Root.Nodes[0];
    1.23 +
    1.24 +			if (Tree.CurrentNode != null)
    1.25 +			{
    1.26 +				switch (args.KeyCode)
    1.27 +				{
    1.28 +					case Keys.Right:
    1.29 +						if (!Tree.CurrentNode.IsExpanded)
    1.30 +							Tree.CurrentNode.IsExpanded = true;
    1.31 +						else if (Tree.CurrentNode.Nodes.Count > 0)
    1.32 +							Tree.SelectedNode = Tree.CurrentNode.Nodes[0];
    1.33 +						args.Handled = true;
    1.34 +						break;
    1.35 +					case Keys.Left:
    1.36 +						if (Tree.CurrentNode.IsExpanded)
    1.37 +							Tree.CurrentNode.IsExpanded = false;
    1.38 +						else if (Tree.CurrentNode.Parent != Tree.Root)
    1.39 +							Tree.SelectedNode = Tree.CurrentNode.Parent;
    1.40 +						args.Handled = true;
    1.41 +						break;
    1.42 +					case Keys.Down:
    1.43 +						NavigateForward(1);
    1.44 +						args.Handled = true;
    1.45 +						break;
    1.46 +					case Keys.Up:
    1.47 +						NavigateBackward(1);
    1.48 +						args.Handled = true;
    1.49 +						break;
    1.50 +					case Keys.PageDown:
    1.51 +						NavigateForward(Math.Max(1, Tree.CurrentPageSize - 1));
    1.52 +						args.Handled = true;
    1.53 +						break;
    1.54 +					case Keys.PageUp:
    1.55 +						NavigateBackward(Math.Max(1, Tree.CurrentPageSize - 1));
    1.56 +						args.Handled = true;
    1.57 +						break;
    1.58 +					case Keys.Home:
    1.59 +						if (Tree.RowMap.Count > 0)
    1.60 +							FocusRow(Tree.RowMap[0]);
    1.61 +						args.Handled = true;
    1.62 +						break;
    1.63 +					case Keys.End:
    1.64 +						if (Tree.RowMap.Count > 0)
    1.65 +							FocusRow(Tree.RowMap[Tree.RowMap.Count-1]);
    1.66 +						args.Handled = true;
    1.67 +						break;
    1.68 +					case Keys.Subtract:
    1.69 +						Tree.CurrentNode.Collapse();
    1.70 +						args.Handled = true;
    1.71 +						args.SuppressKeyPress = true;
    1.72 +						break;
    1.73 +					case Keys.Add:
    1.74 +						Tree.CurrentNode.Expand();
    1.75 +						args.Handled = true;
    1.76 +						args.SuppressKeyPress = true;
    1.77 +						break;
    1.78 +					case Keys.Multiply:
    1.79 +						Tree.CurrentNode.ExpandAll();
    1.80 +						args.Handled = true;
    1.81 +						args.SuppressKeyPress = true;
    1.82 +						break;
    1.83 +					case Keys.A:
    1.84 +						if (args.Modifiers == Keys.Control)
    1.85 +							Tree.SelectAllNodes();
    1.86 +						break;
    1.87 +				}
    1.88 +			}
    1.89 +		}
    1.90 +
    1.91 +		public override void MouseDown(TreeNodeAdvMouseEventArgs args)
    1.92 +		{
    1.93 +			if (args.Node != null)
    1.94 +			{
    1.95 +				Tree.ItemDragMode = true;
    1.96 +				Tree.ItemDragStart = args.Location;
    1.97 +
    1.98 +				if (args.Button == MouseButtons.Left || args.Button == MouseButtons.Right)
    1.99 +				{
   1.100 +					Tree.BeginUpdate();
   1.101 +					try
   1.102 +					{
   1.103 +						Tree.CurrentNode = args.Node;
   1.104 +						if (args.Node.IsSelected)
   1.105 +							_mouseDownFlag = true;
   1.106 +						else
   1.107 +						{
   1.108 +							_mouseDownFlag = false;
   1.109 +							DoMouseOperation(args);
   1.110 +						}
   1.111 +					}
   1.112 +					finally
   1.113 +					{
   1.114 +						Tree.EndUpdate();
   1.115 +					}
   1.116 +				}
   1.117 +
   1.118 +			}
   1.119 +			else
   1.120 +			{
   1.121 +				Tree.ItemDragMode = false;
   1.122 +				MouseDownAtEmptySpace(args);
   1.123 +			}
   1.124 +		}
   1.125 +
   1.126 +		public override void MouseUp(TreeNodeAdvMouseEventArgs args)
   1.127 +		{
   1.128 +			Tree.ItemDragMode = false;
   1.129 +			if (_mouseDownFlag && args.Node != null)
   1.130 +			{
   1.131 +				if (args.Button == MouseButtons.Left)
   1.132 +					DoMouseOperation(args);
   1.133 +				else if (args.Button == MouseButtons.Right)
   1.134 +					Tree.CurrentNode = args.Node;
   1.135 +			}
   1.136 +			_mouseDownFlag = false;
   1.137 +		}
   1.138 +
   1.139 +
   1.140 +		private void NavigateBackward(int n)
   1.141 +		{
   1.142 +			int row = Math.Max(Tree.CurrentNode.Row - n, 0);
   1.143 +			if (row != Tree.CurrentNode.Row)
   1.144 +				FocusRow(Tree.RowMap[row]);
   1.145 +		}
   1.146 +
   1.147 +		private void NavigateForward(int n)
   1.148 +		{
   1.149 +			int row = Math.Min(Tree.CurrentNode.Row + n, Tree.RowCount - 1);
   1.150 +			if (row != Tree.CurrentNode.Row)
   1.151 +				FocusRow(Tree.RowMap[row]);
   1.152 +		}
   1.153 +
   1.154 +		protected virtual void MouseDownAtEmptySpace(TreeNodeAdvMouseEventArgs args)
   1.155 +		{
   1.156 +			Tree.ClearSelection();
   1.157 +		}
   1.158 +
   1.159 +		protected virtual void FocusRow(TreeNodeAdv node)
   1.160 +		{
   1.161 +			Tree.SuspendSelectionEvent = true;
   1.162 +			try
   1.163 +			{
   1.164 +				Tree.ClearSelectionInternal();
   1.165 +				Tree.CurrentNode = node;
   1.166 +				Tree.SelectionStart = node;
   1.167 +				node.IsSelected = true;
   1.168 +				Tree.ScrollTo(node);
   1.169 +			}
   1.170 +			finally
   1.171 +			{
   1.172 +				Tree.SuspendSelectionEvent = false;
   1.173 +			}
   1.174 +		}
   1.175 +
   1.176 +		protected bool CanSelect(TreeNodeAdv node)
   1.177 +		{
   1.178 +			if (Tree.SelectionMode == TreeSelectionMode.MultiSameParent)
   1.179 +			{
   1.180 +				return (Tree.SelectionStart == null || node.Parent == Tree.SelectionStart.Parent);
   1.181 +			}
   1.182 +			else
   1.183 +				return true;
   1.184 +		}
   1.185 +
   1.186 +		protected virtual void DoMouseOperation(TreeNodeAdvMouseEventArgs args)
   1.187 +		{
   1.188 +			if (Tree.SelectedNodes.Count == 1 && args.Node != null && args.Node.IsSelected)
   1.189 +				return;
   1.190 +
   1.191 +			Tree.SuspendSelectionEvent = true;
   1.192 +			try
   1.193 +			{
   1.194 +				Tree.ClearSelectionInternal();
   1.195 +				if (args.Node != null)
   1.196 +					args.Node.IsSelected = true;
   1.197 +				Tree.SelectionStart = args.Node;
   1.198 +			}
   1.199 +			finally
   1.200 +			{
   1.201 +				Tree.SuspendSelectionEvent = false;
   1.202 +			}
   1.203 +		}
   1.204 +	}
   1.205 +}