// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using System.Linq; using Perspex.Controls.Primitives; using Perspex.Input; using Perspex.Styling; public class TextBox : TemplatedControl { public static readonly PerspexProperty AcceptsReturnProperty = PerspexProperty.Register("AcceptsReturn"); public static readonly PerspexProperty AcceptsTabProperty = PerspexProperty.Register("AcceptsTab"); public static readonly PerspexProperty TextProperty = TextBlock.TextProperty.AddOwner(); private int caretIndex; private TextBoxView textBoxView; static TextBox() { FocusableProperty.OverrideDefaultValue(typeof(TextBox), true); } public TextBox() { this.GotFocus += (s, e) => this.textBoxView.GotFocus(); this.LostFocus += (s, e) => this.textBoxView.LostFocus(); this.KeyDown += this.OnKeyDown; this.PointerPressed += this.OnPointerPressed; } public bool AcceptsReturn { get { return this.GetValue(AcceptsReturnProperty); } set { this.SetValue(AcceptsReturnProperty, value); } } public bool AcceptsTab { get { return this.GetValue(AcceptsTabProperty); } set { this.SetValue(AcceptsTabProperty, value); } } public int CaretIndex { get { return this.caretIndex; } set { var text = this.Text ?? string.Empty; value = Math.Min(Math.Max(value, 0), text.Length); if (this.caretIndex != value) { this.caretIndex = value; this.textBoxView.CaretMoved(); } } } public string Text { get { return this.GetValue(TextProperty); } set { this.SetValue(TextProperty, value); } } protected override void OnTemplateApplied() { Decorator textContainer = this.GetVisualDescendents() .OfType() .FirstOrDefault(x => x.Id == "textContainer"); if (textContainer == null) { throw new Exception( "TextBox template doesn't contain a textContainer " + "or textContainer is not a Decorator."); } textContainer.Content = this.textBoxView = new TextBoxView(this); } private void OnKeyDown(object sender, KeyEventArgs e) { string text = this.Text ?? string.Empty; switch (e.Key) { case Key.Left: --this.CaretIndex; break; case Key.Right: ++this.CaretIndex; break; case Key.Back: if (this.caretIndex > 0) { this.Text = text.Substring(0, this.caretIndex - 1) + text.Substring(this.caretIndex); --this.CaretIndex; } break; case Key.Delete: if (this.caretIndex < text.Length) { this.Text = text.Substring(0, this.caretIndex) + text.Substring(this.caretIndex + 1); } break; case Key.Enter: if (this.AcceptsReturn) { goto default; } break; case Key.Tab: if (this.AcceptsTab) { goto default; } break; default: if (!string.IsNullOrEmpty(e.Text)) { this.Text = text.Substring(0, this.caretIndex) + e.Text + text.Substring(this.caretIndex); ++this.CaretIndex; } break; } e.Handled = true; } private void OnPointerPressed(object sender, PointerEventArgs e) { var point = e.GetPosition(this.textBoxView); this.CaretIndex = this.textBoxView.GetCaretIndex(point); } } }