moel@345: using System; moel@345: using System.Collections.Generic; moel@345: using System.Text; moel@345: using System.Drawing; moel@345: using System.Windows.Forms; moel@345: using System.Reflection; moel@345: using System.ComponentModel; moel@345: moel@345: namespace Aga.Controls.Tree.NodeControls moel@345: { moel@345: public class NodeTextBox : BaseTextControl moel@345: { moel@345: private const int MinTextBoxWidth = 30; moel@345: moel@345: public NodeTextBox() moel@345: { moel@345: } moel@345: moel@345: protected override Size CalculateEditorSize(EditorContext context) moel@345: { moel@345: if (Parent.UseColumns) moel@345: return context.Bounds.Size; moel@345: else moel@345: { moel@345: Size size = GetLabelSize(context.CurrentNode, context.DrawContext, _label); moel@345: int width = Math.Max(size.Width + Font.Height, MinTextBoxWidth); // reserve a place for new typed character moel@345: return new Size(width, size.Height); moel@345: } moel@345: } moel@345: moel@345: public override void KeyDown(KeyEventArgs args) moel@345: { moel@345: if (args.KeyCode == Keys.F2 && Parent.CurrentNode != null && EditEnabled) moel@345: { moel@345: args.Handled = true; moel@345: BeginEdit(); moel@345: } moel@345: } moel@345: moel@345: protected override Control CreateEditor(TreeNodeAdv node) moel@345: { moel@345: TextBox textBox = CreateTextBox(); moel@345: textBox.TextAlign = TextAlign; moel@345: textBox.Text = GetLabel(node); moel@345: textBox.BorderStyle = BorderStyle.FixedSingle; moel@345: textBox.TextChanged += EditorTextChanged; moel@345: textBox.KeyDown += EditorKeyDown; moel@345: _label = textBox.Text; moel@345: SetEditControlProperties(textBox, node); moel@345: return textBox; moel@345: } moel@345: moel@345: protected virtual TextBox CreateTextBox() moel@345: { moel@345: return new TextBox(); moel@345: } moel@345: moel@345: protected override void DisposeEditor(Control editor) moel@345: { moel@345: var textBox = editor as TextBox; moel@345: textBox.TextChanged -= EditorTextChanged; moel@345: textBox.KeyDown -= EditorKeyDown; moel@345: } moel@345: moel@345: private void EditorKeyDown(object sender, KeyEventArgs e) moel@345: { moel@345: if (e.KeyCode == Keys.Escape) moel@345: EndEdit(false); moel@345: else if (e.KeyCode == Keys.Enter) moel@345: EndEdit(true); moel@345: } moel@345: moel@345: private string _label; moel@345: private void EditorTextChanged(object sender, EventArgs e) moel@345: { moel@345: var textBox = sender as TextBox; moel@345: _label = textBox.Text; moel@345: Parent.UpdateEditorBounds(); moel@345: } moel@345: moel@345: protected override void DoApplyChanges(TreeNodeAdv node, Control editor) moel@345: { moel@345: var label = (editor as TextBox).Text; moel@345: string oldLabel = GetLabel(node); moel@345: if (oldLabel != label) moel@345: { moel@345: SetLabel(node, label); moel@345: OnLabelChanged(node.Tag, oldLabel, label); moel@345: } moel@345: } moel@345: moel@345: public override void Cut(Control control) moel@345: { moel@345: (control as TextBox).Cut(); moel@345: } moel@345: moel@345: public override void Copy(Control control) moel@345: { moel@345: (control as TextBox).Copy(); moel@345: } moel@345: moel@345: public override void Paste(Control control) moel@345: { moel@345: (control as TextBox).Paste(); moel@345: } moel@345: moel@345: public override void Delete(Control control) moel@345: { moel@345: var textBox = control as TextBox; moel@345: int len = Math.Max(textBox.SelectionLength, 1); moel@345: if (textBox.SelectionStart < textBox.Text.Length) moel@345: { moel@345: int start = textBox.SelectionStart; moel@345: textBox.Text = textBox.Text.Remove(textBox.SelectionStart, len); moel@345: textBox.SelectionStart = start; moel@345: } moel@345: } moel@345: moel@345: public event EventHandler LabelChanged; moel@345: protected void OnLabelChanged(object subject, string oldLabel, string newLabel) moel@345: { moel@345: if (LabelChanged != null) moel@345: LabelChanged(this, new LabelEventArgs(subject, oldLabel, newLabel)); moel@345: } moel@345: } moel@345: }