External/Aga.Controls/Tree/NodeControls/NodeTextBox.cs
author StephaneLenclud
Mon, 04 Feb 2013 00:47:01 +0100
branchMiniDisplay
changeset 435 9ffcd8ed8537
permissions -rw-r--r--
Now calling display init. Not getting the expected error code though.
moel@345
     1
using System;
moel@345
     2
using System.Collections.Generic;
moel@345
     3
using System.Text;
moel@345
     4
using System.Drawing;
moel@345
     5
using System.Windows.Forms;
moel@345
     6
using System.Reflection;
moel@345
     7
using System.ComponentModel;
moel@345
     8
moel@345
     9
namespace Aga.Controls.Tree.NodeControls
moel@345
    10
{
moel@345
    11
	public class NodeTextBox : BaseTextControl
moel@345
    12
	{
moel@345
    13
		private const int MinTextBoxWidth = 30;
moel@345
    14
moel@345
    15
		public NodeTextBox()
moel@345
    16
		{
moel@345
    17
		}
moel@345
    18
moel@345
    19
		protected override Size CalculateEditorSize(EditorContext context)
moel@345
    20
		{
moel@345
    21
			if (Parent.UseColumns)
moel@345
    22
				return context.Bounds.Size;
moel@345
    23
			else
moel@345
    24
			{
moel@345
    25
				Size size = GetLabelSize(context.CurrentNode, context.DrawContext, _label);
moel@345
    26
				int width = Math.Max(size.Width + Font.Height, MinTextBoxWidth); // reserve a place for new typed character
moel@345
    27
				return new Size(width, size.Height);
moel@345
    28
			}
moel@345
    29
		}
moel@345
    30
moel@345
    31
		public override void KeyDown(KeyEventArgs args)
moel@345
    32
		{
moel@345
    33
			if (args.KeyCode == Keys.F2 && Parent.CurrentNode != null && EditEnabled)
moel@345
    34
			{
moel@345
    35
				args.Handled = true;
moel@345
    36
				BeginEdit();
moel@345
    37
			}
moel@345
    38
		}
moel@345
    39
moel@345
    40
		protected override Control CreateEditor(TreeNodeAdv node)
moel@345
    41
		{
moel@345
    42
			TextBox textBox = CreateTextBox();
moel@345
    43
			textBox.TextAlign = TextAlign;
moel@345
    44
			textBox.Text = GetLabel(node);
moel@345
    45
			textBox.BorderStyle = BorderStyle.FixedSingle;
moel@345
    46
			textBox.TextChanged += EditorTextChanged;
moel@345
    47
			textBox.KeyDown += EditorKeyDown;
moel@345
    48
			_label = textBox.Text;
moel@345
    49
			SetEditControlProperties(textBox, node);
moel@345
    50
			return textBox;
moel@345
    51
		}
moel@345
    52
moel@345
    53
		protected virtual TextBox CreateTextBox()
moel@345
    54
		{
moel@345
    55
			return new TextBox();
moel@345
    56
		}
moel@345
    57
moel@345
    58
		protected override void DisposeEditor(Control editor)
moel@345
    59
		{
moel@345
    60
			var textBox = editor as TextBox;
moel@345
    61
			textBox.TextChanged -= EditorTextChanged;
moel@345
    62
			textBox.KeyDown -= EditorKeyDown;
moel@345
    63
		}
moel@345
    64
moel@345
    65
		private void EditorKeyDown(object sender, KeyEventArgs e)
moel@345
    66
		{
moel@345
    67
			if (e.KeyCode == Keys.Escape)
moel@345
    68
				EndEdit(false);
moel@345
    69
			else if (e.KeyCode == Keys.Enter)
moel@345
    70
				EndEdit(true);
moel@345
    71
		}
moel@345
    72
moel@345
    73
		private string _label;
moel@345
    74
		private void EditorTextChanged(object sender, EventArgs e)
moel@345
    75
		{
moel@345
    76
			var textBox = sender as TextBox;
moel@345
    77
			_label = textBox.Text;
moel@345
    78
			Parent.UpdateEditorBounds();
moel@345
    79
		}
moel@345
    80
moel@345
    81
		protected override void DoApplyChanges(TreeNodeAdv node, Control editor)
moel@345
    82
		{
moel@345
    83
			var label = (editor as TextBox).Text;
moel@345
    84
			string oldLabel = GetLabel(node);
moel@345
    85
			if (oldLabel != label)
moel@345
    86
			{
moel@345
    87
				SetLabel(node, label);
moel@345
    88
				OnLabelChanged(node.Tag, oldLabel, label);
moel@345
    89
			}
moel@345
    90
		}
moel@345
    91
moel@345
    92
		public override void Cut(Control control)
moel@345
    93
		{
moel@345
    94
			(control as TextBox).Cut();
moel@345
    95
		}
moel@345
    96
moel@345
    97
		public override void Copy(Control control)
moel@345
    98
		{
moel@345
    99
			(control as TextBox).Copy();
moel@345
   100
		}
moel@345
   101
moel@345
   102
		public override void Paste(Control control)
moel@345
   103
		{
moel@345
   104
			(control as TextBox).Paste();
moel@345
   105
		}
moel@345
   106
moel@345
   107
		public override void Delete(Control control)
moel@345
   108
		{
moel@345
   109
			var textBox = control as TextBox;
moel@345
   110
			int len = Math.Max(textBox.SelectionLength, 1);
moel@345
   111
			if (textBox.SelectionStart < textBox.Text.Length)
moel@345
   112
			{
moel@345
   113
				int start = textBox.SelectionStart;
moel@345
   114
				textBox.Text = textBox.Text.Remove(textBox.SelectionStart, len);
moel@345
   115
				textBox.SelectionStart = start;
moel@345
   116
			}
moel@345
   117
		}
moel@345
   118
moel@345
   119
		public event EventHandler<LabelEventArgs> LabelChanged;
moel@345
   120
		protected void OnLabelChanged(object subject, string oldLabel, string newLabel)
moel@345
   121
		{
moel@345
   122
			if (LabelChanged != null)
moel@345
   123
				LabelChanged(this, new LabelEventArgs(subject, oldLabel, newLabel));
moel@345
   124
		}
moel@345
   125
	}
moel@345
   126
}