using System.Text;
using System;
namespace Terminal.Gui {
public partial class View {
string _text;
///
/// The text displayed by the .
///
///
///
/// The text will be drawn before any subviews are drawn.
///
///
/// The text will be drawn starting at the view origin (0, 0) and will be formatted according
/// to and .
///
///
/// The text will word-wrap to additional lines if it does not fit horizontally. If 's height
/// is 1, the text will be clipped.
///
///
/// Set the to enable hotkey support. To disable hotkey support set to
/// (Rune)0xffff.
///
///
public virtual string Text {
get => _text;
set {
_text = value;
SetHotKey ();
UpdateTextFormatterText ();
//TextFormatter.Format ();
OnResizeNeeded ();
#if DEBUG
if (_text != null && string.IsNullOrEmpty (Id)) {
Id = _text;
}
#endif
}
}
///
/// Gets or sets the used to format .
///
public TextFormatter TextFormatter { get; set; }
void TextFormatter_HotKeyChanged (object sender, KeyChangedEventArgs e)
{
HotKeyChanged?.Invoke (this, e);
}
///
/// Can be overridden if the has
/// different format than the default.
///
protected virtual void UpdateTextFormatterText ()
{
if (TextFormatter != null) {
TextFormatter.Text = _text;
}
}
///
/// Gets or sets whether trailing spaces at the end of word-wrapped lines are preserved
/// or not when is enabled.
/// If trailing spaces at the end of wrapped lines will be removed when
/// is formatted for display. The default is .
///
public virtual bool PreserveTrailingSpaces {
get => TextFormatter.PreserveTrailingSpaces;
set {
if (TextFormatter.PreserveTrailingSpaces != value) {
TextFormatter.PreserveTrailingSpaces = value;
TextFormatter.NeedsFormat = true;
}
}
}
///
/// Gets or sets how the View's is aligned horizontally when drawn. Changing this property will redisplay the .
///
/// The text alignment.
public virtual TextAlignment TextAlignment {
get => TextFormatter.Alignment;
set {
TextFormatter.Alignment = value;
UpdateTextFormatterText ();
OnResizeNeeded ();
}
}
///
/// Gets or sets how the View's is aligned vertically when drawn. Changing this property will redisplay the .
///
/// The text alignment.
public virtual VerticalTextAlignment VerticalTextAlignment {
get => TextFormatter.VerticalAlignment;
set {
TextFormatter.VerticalAlignment = value;
SetNeedsDisplay ();
}
}
///
/// Gets or sets the direction of the View's . Changing this property will redisplay the .
///
/// The text alignment.
public virtual TextDirection TextDirection {
get => TextFormatter.Direction;
set {
UpdateTextDirection (value);
TextFormatter.Direction = value;
}
}
private void UpdateTextDirection (TextDirection newDirection)
{
var directionChanged = TextFormatter.IsHorizontalDirection (TextFormatter.Direction)
!= TextFormatter.IsHorizontalDirection (newDirection);
TextFormatter.Direction = newDirection;
var isValidOldAutoSize = AutoSize && IsValidAutoSize (out var _);
UpdateTextFormatterText ();
if ((!ForceValidatePosDim && directionChanged && AutoSize)
|| (ForceValidatePosDim && directionChanged && AutoSize && isValidOldAutoSize)) {
OnResizeNeeded ();
} else if (directionChanged && IsAdded) {
SetWidthHeight (Bounds.Size);
SetMinWidthHeight ();
} else {
SetMinWidthHeight ();
}
TextFormatter.Size = GetSizeNeededForTextAndHotKey ();
SetNeedsDisplay ();
}
///
/// Gets the width or height of the characters
/// in the property.
///
///
/// Only the first hotkey specifier found in is supported.
///
/// If (the default) the width required for the hotkey specifier is returned. Otherwise the height is returned.
/// The number of characters required for the . If the text direction specified
/// by does not match the parameter, 0 is returned.
public int GetHotKeySpecifierLength (bool isWidth = true)
{
if (isWidth) {
return TextFormatter.IsHorizontalDirection (TextDirection) &&
TextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
? Math.Max (HotKeySpecifier.GetColumns (), 0) : 0;
} else {
return TextFormatter.IsVerticalDirection (TextDirection) &&
TextFormatter.Text?.Contains ((char)HotKeySpecifier.Value) == true
? Math.Max (HotKeySpecifier.GetColumns(), 0) : 0;
}
}
///
/// Gets the dimensions required for ignoring a .
///
///
public Size GetSizeNeededForTextWithoutHotKey ()
{
return new Size (TextFormatter.Size.Width - GetHotKeySpecifierLength (),
TextFormatter.Size.Height - GetHotKeySpecifierLength (false));
}
///
/// Gets the dimensions required for accounting for a .
///
///
public Size GetSizeNeededForTextAndHotKey ()
{
if (string.IsNullOrEmpty (TextFormatter.Text)) {
if (!IsInitialized) return Size.Empty;
return Bounds.Size;
}
// BUGBUG: This IGNORES what Text is set to, using on only the current View size. This doesn't seem to make sense.
// BUGBUG: This uses Frame; in v2 it should be Bounds
return new Size (_frame.Size.Width + GetHotKeySpecifierLength (),
_frame.Size.Height + GetHotKeySpecifierLength (false));
}
}
}