External/Aga.Controls/Tree/Input/NormalInputState.cs
author sl
Thu, 01 Jan 2015 23:35:49 +0100
changeset 405 5715aefd2bcc
permissions -rw-r--r--
SharpDisplay: Migrating to new robust client scheme.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Windows.Forms;
     5 
     6 namespace Aga.Controls.Tree
     7 {
     8 	internal class NormalInputState : InputState
     9 	{
    10 		private bool _mouseDownFlag = false;
    11 
    12 		public NormalInputState(TreeViewAdv tree) : base(tree)
    13 		{
    14 		}
    15 
    16 		public override void KeyDown(KeyEventArgs args)
    17 		{
    18 			if (Tree.CurrentNode == null && Tree.Root.Nodes.Count > 0)
    19 				Tree.CurrentNode = Tree.Root.Nodes[0];
    20 
    21 			if (Tree.CurrentNode != null)
    22 			{
    23 				switch (args.KeyCode)
    24 				{
    25 					case Keys.Right:
    26 						if (!Tree.CurrentNode.IsExpanded)
    27 							Tree.CurrentNode.IsExpanded = true;
    28 						else if (Tree.CurrentNode.Nodes.Count > 0)
    29 							Tree.SelectedNode = Tree.CurrentNode.Nodes[0];
    30 						args.Handled = true;
    31 						break;
    32 					case Keys.Left:
    33 						if (Tree.CurrentNode.IsExpanded)
    34 							Tree.CurrentNode.IsExpanded = false;
    35 						else if (Tree.CurrentNode.Parent != Tree.Root)
    36 							Tree.SelectedNode = Tree.CurrentNode.Parent;
    37 						args.Handled = true;
    38 						break;
    39 					case Keys.Down:
    40 						NavigateForward(1);
    41 						args.Handled = true;
    42 						break;
    43 					case Keys.Up:
    44 						NavigateBackward(1);
    45 						args.Handled = true;
    46 						break;
    47 					case Keys.PageDown:
    48 						NavigateForward(Math.Max(1, Tree.CurrentPageSize - 1));
    49 						args.Handled = true;
    50 						break;
    51 					case Keys.PageUp:
    52 						NavigateBackward(Math.Max(1, Tree.CurrentPageSize - 1));
    53 						args.Handled = true;
    54 						break;
    55 					case Keys.Home:
    56 						if (Tree.RowMap.Count > 0)
    57 							FocusRow(Tree.RowMap[0]);
    58 						args.Handled = true;
    59 						break;
    60 					case Keys.End:
    61 						if (Tree.RowMap.Count > 0)
    62 							FocusRow(Tree.RowMap[Tree.RowMap.Count-1]);
    63 						args.Handled = true;
    64 						break;
    65 					case Keys.Subtract:
    66 						Tree.CurrentNode.Collapse();
    67 						args.Handled = true;
    68 						args.SuppressKeyPress = true;
    69 						break;
    70 					case Keys.Add:
    71 						Tree.CurrentNode.Expand();
    72 						args.Handled = true;
    73 						args.SuppressKeyPress = true;
    74 						break;
    75 					case Keys.Multiply:
    76 						Tree.CurrentNode.ExpandAll();
    77 						args.Handled = true;
    78 						args.SuppressKeyPress = true;
    79 						break;
    80 					case Keys.A:
    81 						if (args.Modifiers == Keys.Control)
    82 							Tree.SelectAllNodes();
    83 						break;
    84 				}
    85 			}
    86 		}
    87 
    88 		public override void MouseDown(TreeNodeAdvMouseEventArgs args)
    89 		{
    90 			if (args.Node != null)
    91 			{
    92 				Tree.ItemDragMode = true;
    93 				Tree.ItemDragStart = args.Location;
    94 
    95 				if (args.Button == MouseButtons.Left || args.Button == MouseButtons.Right)
    96 				{
    97 					Tree.BeginUpdate();
    98 					try
    99 					{
   100 						Tree.CurrentNode = args.Node;
   101 						if (args.Node.IsSelected)
   102 							_mouseDownFlag = true;
   103 						else
   104 						{
   105 							_mouseDownFlag = false;
   106 							DoMouseOperation(args);
   107 						}
   108 					}
   109 					finally
   110 					{
   111 						Tree.EndUpdate();
   112 					}
   113 				}
   114 
   115 			}
   116 			else
   117 			{
   118 				Tree.ItemDragMode = false;
   119 				MouseDownAtEmptySpace(args);
   120 			}
   121 		}
   122 
   123 		public override void MouseUp(TreeNodeAdvMouseEventArgs args)
   124 		{
   125 			Tree.ItemDragMode = false;
   126 			if (_mouseDownFlag && args.Node != null)
   127 			{
   128 				if (args.Button == MouseButtons.Left)
   129 					DoMouseOperation(args);
   130 				else if (args.Button == MouseButtons.Right)
   131 					Tree.CurrentNode = args.Node;
   132 			}
   133 			_mouseDownFlag = false;
   134 		}
   135 
   136 
   137 		private void NavigateBackward(int n)
   138 		{
   139 			int row = Math.Max(Tree.CurrentNode.Row - n, 0);
   140 			if (row != Tree.CurrentNode.Row)
   141 				FocusRow(Tree.RowMap[row]);
   142 		}
   143 
   144 		private void NavigateForward(int n)
   145 		{
   146 			int row = Math.Min(Tree.CurrentNode.Row + n, Tree.RowCount - 1);
   147 			if (row != Tree.CurrentNode.Row)
   148 				FocusRow(Tree.RowMap[row]);
   149 		}
   150 
   151 		protected virtual void MouseDownAtEmptySpace(TreeNodeAdvMouseEventArgs args)
   152 		{
   153 			Tree.ClearSelection();
   154 		}
   155 
   156 		protected virtual void FocusRow(TreeNodeAdv node)
   157 		{
   158 			Tree.SuspendSelectionEvent = true;
   159 			try
   160 			{
   161 				Tree.ClearSelectionInternal();
   162 				Tree.CurrentNode = node;
   163 				Tree.SelectionStart = node;
   164 				node.IsSelected = true;
   165 				Tree.ScrollTo(node);
   166 			}
   167 			finally
   168 			{
   169 				Tree.SuspendSelectionEvent = false;
   170 			}
   171 		}
   172 
   173 		protected bool CanSelect(TreeNodeAdv node)
   174 		{
   175 			if (Tree.SelectionMode == TreeSelectionMode.MultiSameParent)
   176 			{
   177 				return (Tree.SelectionStart == null || node.Parent == Tree.SelectionStart.Parent);
   178 			}
   179 			else
   180 				return true;
   181 		}
   182 
   183 		protected virtual void DoMouseOperation(TreeNodeAdvMouseEventArgs args)
   184 		{
   185 			if (Tree.SelectedNodes.Count == 1 && args.Node != null && args.Node.IsSelected)
   186 				return;
   187 
   188 			Tree.SuspendSelectionEvent = true;
   189 			try
   190 			{
   191 				Tree.ClearSelectionInternal();
   192 				if (args.Node != null)
   193 					args.Node.IsSelected = true;
   194 				Tree.SelectionStart = args.Node;
   195 			}
   196 			finally
   197 			{
   198 				Tree.SuspendSelectionEvent = false;
   199 			}
   200 		}
   201 	}
   202 }