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