// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Reactive;
using System.Reactive.Linq;
using Avalonia.Data;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.Metadata;
namespace Avalonia.Controls
{
///
/// A control that displays a block of text.
///
public class TextBlock : Control
{
///
/// Defines the property.
///
public static readonly StyledProperty BackgroundProperty =
Border.BackgroundProperty.AddOwner();
// TODO: Define these attached properties elswhere (e.g. on a Text class) and AddOwner
// them into TextBlock.
///
/// Defines the property.
///
public static readonly AttachedProperty FontFamilyProperty =
AvaloniaProperty.RegisterAttached(
nameof(FontFamily),
defaultValue: new FontFamily("Courier New"),
inherits: true);
///
/// Defines the property.
///
public static readonly AttachedProperty FontSizeProperty =
AvaloniaProperty.RegisterAttached(
nameof(FontSize),
defaultValue: 12,
inherits: true);
///
/// Defines the property.
///
public static readonly AttachedProperty FontStyleProperty =
AvaloniaProperty.RegisterAttached(
nameof(FontStyle),
inherits: true);
///
/// Defines the property.
///
public static readonly AttachedProperty FontWeightProperty =
AvaloniaProperty.RegisterAttached(
nameof(FontWeight),
inherits: true,
defaultValue: FontWeight.Normal);
///
/// Defines the property.
///
public static readonly AttachedProperty ForegroundProperty =
AvaloniaProperty.RegisterAttached(
nameof(Foreground),
new SolidColorBrush(0xff000000),
inherits: true);
///
/// Defines the property.
///
public static readonly DirectProperty TextProperty =
AvaloniaProperty.RegisterDirect(
nameof(Text),
o => o.Text,
(o, v) => o.Text = v);
///
/// Defines the property.
///
public static readonly StyledProperty TextAlignmentProperty =
AvaloniaProperty.Register(nameof(TextAlignment));
///
/// Defines the property.
///
public static readonly StyledProperty TextWrappingProperty =
AvaloniaProperty.Register(nameof(TextWrapping));
private string _text;
private FormattedText _formattedText;
private Size _constraint;
///
/// Initializes static members of the class.
///
static TextBlock()
{
ClipToBoundsProperty.OverrideDefaultValue(true);
AffectsRender(ForegroundProperty);
AffectsRender(FontWeightProperty);
AffectsRender(FontSizeProperty);
AffectsRender(FontStyleProperty);
}
///
/// Initializes a new instance of the class.
///
public TextBlock()
{
Observable.Merge(
this.GetObservable(TextProperty).Select(_ => Unit.Default),
this.GetObservable(TextAlignmentProperty).Select(_ => Unit.Default),
this.GetObservable(FontSizeProperty).Select(_ => Unit.Default),
this.GetObservable(FontStyleProperty).Select(_ => Unit.Default),
this.GetObservable(FontWeightProperty).Select(_ => Unit.Default))
.Subscribe(_ =>
{
InvalidateFormattedText();
InvalidateMeasure();
});
}
///
/// Gets or sets a brush used to paint the control's background.
///
public IBrush Background
{
get { return GetValue(BackgroundProperty); }
set { SetValue(BackgroundProperty, value); }
}
///
/// Gets or sets the text.
///
[Content]
public string Text
{
get { return _text; }
set { SetAndRaise(TextProperty, ref _text, value); }
}
///
/// Gets or sets the font family.
///
public FontFamily FontFamily
{
get { return GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}
///
/// Gets or sets the font size.
///
public double FontSize
{
get { return GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
///
/// Gets or sets the font style.
///
public FontStyle FontStyle
{
get { return GetValue(FontStyleProperty); }
set { SetValue(FontStyleProperty, value); }
}
///
/// Gets or sets the font weight.
///
public FontWeight FontWeight
{
get { return GetValue(FontWeightProperty); }
set { SetValue(FontWeightProperty, value); }
}
///
/// Gets or sets a brush used to paint the text.
///
public IBrush Foreground
{
get { return GetValue(ForegroundProperty); }
set { SetValue(ForegroundProperty, value); }
}
///
/// Gets the used to render the text.
///
public FormattedText FormattedText
{
get
{
if (_formattedText == null)
{
_formattedText = CreateFormattedText(_constraint);
}
return _formattedText;
}
}
///
/// Gets or sets the control's text wrapping mode.
///
public TextWrapping TextWrapping
{
get { return GetValue(TextWrappingProperty); }
set { SetValue(TextWrappingProperty, value); }
}
///
/// Gets or sets the text alignment.
///
public TextAlignment TextAlignment
{
get { return GetValue(TextAlignmentProperty); }
set { SetValue(TextAlignmentProperty, value); }
}
///
/// Gets the value of the attached on a control.
///
/// The control.
/// The font family.
public static FontFamily GetFontFamily(Control control)
{
return control.GetValue(FontFamilyProperty);
}
///
/// Gets the value of the attached on a control.
///
/// The control.
/// The font family.
public static double GetFontSize(Control control)
{
return control.GetValue(FontSizeProperty);
}
///
/// Gets the value of the attached on a control.
///
/// The control.
/// The font family.
public static FontStyle GetFontStyle(Control control)
{
return control.GetValue(FontStyleProperty);
}
///
/// Gets the value of the attached on a control.
///
/// The control.
/// The font family.
public static FontWeight GetFontWeight(Control control)
{
return control.GetValue(FontWeightProperty);
}
///
/// Gets the value of the attached on a control.
///
/// The control.
/// The foreground.
public static IBrush GetForeground(Control control)
{
return control.GetValue(ForegroundProperty);
}
///
/// Sets the value of the attached on a control.
///
/// The control.
/// The property value to set.
/// The font family.
public static void SetFontFamily(Control control, string value)
{
control.SetValue(FontFamilyProperty, value);
}
///
/// Sets the value of the attached on a control.
///
/// The control.
/// The property value to set.
/// The font family.
public static void SetFontSize(Control control, double value)
{
control.SetValue(FontSizeProperty, value);
}
///
/// Sets the value of the attached on a control.
///
/// The control.
/// The property value to set.
/// The font family.
public static void SetFontStyle(Control control, FontStyle value)
{
control.SetValue(FontStyleProperty, value);
}
///
/// Sets the value of the attached on a control.
///
/// The control.
/// The property value to set.
/// The font family.
public static void SetFontWeight(Control control, FontWeight value)
{
control.SetValue(FontWeightProperty, value);
}
///
/// Sets the value of the attached on a control.
///
/// The control.
/// The property value to set.
/// The font family.
public static void SetForeground(Control control, IBrush value)
{
control.SetValue(ForegroundProperty, value);
}
///
/// Renders the to a drawing context.
///
/// The drawing context.
public override void Render(DrawingContext context)
{
var background = Background;
if (background != null)
{
context.FillRectangle(background, new Rect(Bounds.Size));
}
FormattedText.Constraint = Bounds.Size;
context.DrawText(Foreground, new Point(), FormattedText);
}
///
/// Creates the used to render the text.
///
/// The constraint of the text.
/// A object.
protected virtual FormattedText CreateFormattedText(Size constraint)
{
return new FormattedText
{
Constraint = constraint,
Typeface = new Typeface(FontFamily, FontSize, FontStyle, FontWeight),
Text = Text ?? string.Empty,
TextAlignment = TextAlignment,
Wrapping = TextWrapping,
};
}
///
/// Invalidates .
///
protected void InvalidateFormattedText()
{
if (_formattedText != null)
{
_constraint = _formattedText.Constraint;
_formattedText = null;
}
}
///
/// Measures the control.
///
/// The available size for the control.
/// The desired size.
protected override Size MeasureOverride(Size availableSize)
{
if (!string.IsNullOrEmpty(Text))
{
if (TextWrapping == TextWrapping.Wrap)
{
FormattedText.Constraint = new Size(availableSize.Width, double.PositiveInfinity);
}
else
{
FormattedText.Constraint = Size.Infinity;
}
return FormattedText.Measure();
}
return new Size();
}
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnAttachedToLogicalTree(e);
InvalidateFormattedText();
InvalidateMeasure();
}
}
}