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