From 71e46c3084f32984c7faa956e8de34c70cab411b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 9 Dec 2014 02:57:52 +0100 Subject: [PATCH] Added TextWrapping setting. TextBox doesn't handle quite a few things yet, scrolling and lines for example. --- Perspex.Controls/Perspex.Controls.csproj | 1 + Perspex.Controls/TextBlock.cs | 19 ++++++++++++++++++- Perspex.Controls/TextBox.cs | 9 +++++++++ Perspex.Controls/TextBoxView.cs | 1 + Perspex.Controls/TextWrapping.cs | 14 ++++++++++++++ Perspex.SceneGraph/Size.cs | 5 +++++ TestApplication/Program.cs | 9 ++++++++- 7 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 Perspex.Controls/TextWrapping.cs diff --git a/Perspex.Controls/Perspex.Controls.csproj b/Perspex.Controls/Perspex.Controls.csproj index 7d903d4afb..dd5f2d6402 100644 --- a/Perspex.Controls/Perspex.Controls.csproj +++ b/Perspex.Controls/Perspex.Controls.csproj @@ -73,6 +73,7 @@ + diff --git a/Perspex.Controls/TextBlock.cs b/Perspex.Controls/TextBlock.cs index 47fd736a1a..e1911ba152 100644 --- a/Perspex.Controls/TextBlock.cs +++ b/Perspex.Controls/TextBlock.cs @@ -30,6 +30,9 @@ namespace Perspex.Controls public static readonly PerspexProperty TextProperty = PerspexProperty.Register("Text"); + public static readonly PerspexProperty TextWrappingProperty = + PerspexProperty.Register("TextWrapping"); + private FormattedText formattedText; public TextBlock() @@ -74,6 +77,12 @@ namespace Perspex.Controls set { this.SetValue(FontStyleProperty, value); } } + public TextWrapping TextWrapping + { + get { return this.GetValue(TextWrappingProperty); } + set { this.SetValue(TextWrappingProperty, value); } + } + protected FormattedText FormattedText { get @@ -108,7 +117,15 @@ namespace Perspex.Controls { if (!string.IsNullOrEmpty(this.Text)) { - this.FormattedText.Constraint = availableSize; + if (this.TextWrapping == TextWrapping.Wrap) + { + this.FormattedText.Constraint = new Size(availableSize.Width, double.PositiveInfinity); + } + else + { + this.FormattedText.Constraint = Size.Infinity; + } + return this.FormattedText.Measure(); } diff --git a/Perspex.Controls/TextBox.cs b/Perspex.Controls/TextBox.cs index 74f27c128a..1f1c7795e2 100644 --- a/Perspex.Controls/TextBox.cs +++ b/Perspex.Controls/TextBox.cs @@ -32,6 +32,9 @@ namespace Perspex.Controls public static readonly PerspexProperty TextProperty = TextBlock.TextProperty.AddOwner(); + public static readonly PerspexProperty TextWrappingProperty = + TextBlock.TextWrappingProperty.AddOwner(); + private TextBoxView textBoxView; static TextBox() @@ -83,6 +86,12 @@ namespace Perspex.Controls set { this.SetValue(TextProperty, value); } } + public TextWrapping TextWrapping + { + get { return this.GetValue(TextWrappingProperty); } + set { this.SetValue(TextWrappingProperty, value); } + } + protected override void OnTemplateApplied() { Decorator textContainer = this.GetVisualDescendents() diff --git a/Perspex.Controls/TextBoxView.cs b/Perspex.Controls/TextBoxView.cs index bd554c797f..5536c04e89 100644 --- a/Perspex.Controls/TextBoxView.cs +++ b/Perspex.Controls/TextBoxView.cs @@ -26,6 +26,7 @@ namespace Perspex.Controls this.caretTimer.Tick += this.CaretTimerTick; this.parent = parent; this[!TextProperty] = parent[!TextProperty]; + this[!TextWrappingProperty] = parent[!TextWrappingProperty]; Observable.Merge( this.parent.GetObservable(TextBox.SelectionStartProperty), diff --git a/Perspex.Controls/TextWrapping.cs b/Perspex.Controls/TextWrapping.cs new file mode 100644 index 0000000000..d514872991 --- /dev/null +++ b/Perspex.Controls/TextWrapping.cs @@ -0,0 +1,14 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2014 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls +{ + public enum TextWrapping + { + NoWrap, + Wrap + } +} diff --git a/Perspex.SceneGraph/Size.cs b/Perspex.SceneGraph/Size.cs index 958d34dcb7..e25b82619e 100644 --- a/Perspex.SceneGraph/Size.cs +++ b/Perspex.SceneGraph/Size.cs @@ -14,6 +14,11 @@ namespace Perspex /// public struct Size { + /// + /// A size representing infinity. + /// + public static readonly Size Infinity = new Size(double.PositiveInfinity, double.PositiveInfinity); + /// /// The width. /// diff --git a/TestApplication/Program.cs b/TestApplication/Program.cs index 0cf8b695d6..eeb19ddfb7 100644 --- a/TestApplication/Program.cs +++ b/TestApplication/Program.cs @@ -220,6 +220,7 @@ namespace TestApplication new TextBlock { Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin venenatis dui quis libero suscipit tincidunt.", + TextWrapping = TextWrapping.Wrap, }, new TextBlock { @@ -228,7 +229,13 @@ namespace TestApplication }, new TextBox { - Text = "Some example text", + Text = "A non-wrapping text box. Lorem ipsum dolor sit amet.", + TextWrapping = TextWrapping.NoWrap, + }, + new TextBox + { + Text = "A wrapping text box. Lorem ipsum dolor sit amet.", + TextWrapping = TextWrapping.Wrap, }, } },