// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using System.Reactive.Linq; using Perspex.Controls.Primitives; public class TextBox : TemplatedControl { public static readonly PerspexProperty AcceptsReturnProperty = PerspexProperty.Register("AcceptsReturn"); public static readonly PerspexProperty AcceptsTabProperty = PerspexProperty.Register("AcceptsTab"); public static readonly PerspexProperty CaretIndexProperty = PerspexProperty.Register("CaretIndex", coerce: CoerceCaretIndex); public static readonly PerspexProperty SelectionStartProperty = PerspexProperty.Register("SelectionStart", coerce: CoerceCaretIndex); public static readonly PerspexProperty SelectionEndProperty = PerspexProperty.Register("SelectionEnd", coerce: CoerceCaretIndex); public static readonly PerspexProperty TextProperty = TextBlock.TextProperty.AddOwner(); public static readonly PerspexProperty TextWrappingProperty = TextBlock.TextWrappingProperty.AddOwner(); public TextBox() { var canScrollHorizontally = this.GetObservable(AcceptsReturnProperty) .Select(x => !x); this.Bind( ScrollViewer.CanScrollHorizontallyProperty, canScrollHorizontally, BindingPriority.Style); var horizontalScrollBarVisibility = this.GetObservable(AcceptsReturnProperty) .Select(x => x ? ScrollBarVisibility.Auto : ScrollBarVisibility.Hidden); this.Bind( ScrollViewer.HorizontalScrollBarVisibilityProperty, horizontalScrollBarVisibility, BindingPriority.Style); } 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.GetValue(CaretIndexProperty); } set { this.SetValue(CaretIndexProperty, value); } } public int SelectionStart { get { return this.GetValue(SelectionStartProperty); } set { this.SetValue(SelectionStartProperty, value); } } public int SelectionEnd { get { return this.GetValue(SelectionEndProperty); } set { this.SetValue(SelectionEndProperty, value); } } public string Text { get { return this.GetValue(TextProperty); } set { this.SetValue(TextProperty, value); } } public TextWrapping TextWrapping { get { return this.GetValue(TextWrappingProperty); } set { this.SetValue(TextWrappingProperty, value); } } private static int CoerceCaretIndex(PerspexObject o, int value) { var text = o.GetValue(TextProperty); var length = (text != null) ? text.Length : 0; return Math.Max(0, Math.Min(length, value)); } } }