//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
// NOTE: FrameView is functionally identical to Window with the following exceptions.
// - Is not a Toplevel
// - Does not support mouse dragging
// - Does not support padding (but should)
// - Does not support IEnumerable
// Any udpates done here should probably be done in Window as well; TODO: Merge these classes
using System.Linq;
using NStack;
namespace Terminal.Gui {
///
/// The FrameView is a container frame that draws a frame around the contents. It is similar to
/// a GroupBox in Windows.
///
public class FrameView : View {
View contentView;
ustring title;
///
/// The title to be displayed for this .
///
/// The title.
public ustring Title {
get => title;
set {
title = value;
if (Border != null) {
Border.Title = title;
}
SetNeedsDisplay ();
}
}
///
public override Border Border {
get => base.Border;
set {
if (base.Border != null && base.Border.Child != null && value.Child == null) {
value.Child = base.Border.Child;
}
base.Border = value;
if (value == null) {
return;
}
Rect frame;
if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
frame = Rect.Empty;
} else {
frame = Frame;
}
AdjustContentView (frame);
Border.BorderChanged += Border_BorderChanged;
}
}
void Border_BorderChanged (Border border)
{
Rect frame;
if (contentView != null && (contentView.Width is Dim || contentView.Height is Dim)) {
frame = Rect.Empty;
} else {
frame = Frame;
}
AdjustContentView (frame);
}
///
/// ContentView is an internal implementation detail of Window. It is used to host Views added with .
/// Its ONLY reason for being is to provide a simple way for Window to expose to those SubViews that the Window's Bounds
/// are actually deflated due to the border.
///
class ContentView : View {
public ContentView (Rect frame) : base (frame) { }
public ContentView () : base () { }
}
///
/// Initializes a new instance of the class using layout.
///
/// Frame.
/// Title.
/// Views.
/// The .
public FrameView (Rect frame, ustring title = null, View [] views = null, Border border = null) : base (frame)
{
//var cFrame = new Rect (1, 1, Math.Max (frame.Width - 2, 0), Math.Max (frame.Height - 2, 0));
Initialize (frame, title, views, border);
}
///
/// Initializes a new instance of the class using layout.
///
/// Title.
/// The .
public FrameView (ustring title, Border border = null)
{
Initialize (Rect.Empty, title, null, border);
}
///
/// Initializes a new instance of the class using layout.
///
public FrameView () : this (title: string.Empty) { }
void Initialize (Rect frame, ustring title, View [] views = null, Border border = null)
{
if (title == null) title = ustring.Empty;
this.Title = title;
if (border == null) {
Border = new Border () {
BorderStyle = BorderStyle.Single,
Title = title
};
} else {
Border = border;
if (ustring.IsNullOrEmpty (border.Title)) {
border.Title = title;
}
}
AdjustContentView (frame, views);
}
void AdjustContentView (Rect frame, View [] views = null)
{
var borderLength = Border.DrawMarginFrame ? 1 : 0;
var sumPadding = Border.GetSumThickness ();
var wp = new Point ();
var wb = new Size ();
if (frame == Rect.Empty) {
wp.X = borderLength + sumPadding.Left;
wp.Y = borderLength + sumPadding.Top;
wb.Width = borderLength + sumPadding.Right;
wb.Height = borderLength + sumPadding.Bottom;
if (contentView == null) {
contentView = new ContentView () {
X = wp.X,
Y = wp.Y,
Width = Dim.Fill (wb.Width),
Height = Dim.Fill (wb.Height)
};
} else {
contentView.X = wp.X;
contentView.Y = wp.Y;
contentView.Width = Dim.Fill (wb.Width);
contentView.Height = Dim.Fill (wb.Height);
}
} else {
wb.Width = (2 * borderLength) + sumPadding.Right + sumPadding.Left;
wb.Height = (2 * borderLength) + sumPadding.Bottom + sumPadding.Top;
var cFrame = new Rect (borderLength + sumPadding.Left, borderLength + sumPadding.Top, frame.Width - wb.Width, frame.Height - wb.Height);
if (contentView == null) {
contentView = new ContentView (cFrame);
} else {
contentView.Frame = cFrame;
}
}
if (views != null) {
foreach (var view in views) {
contentView.Add (view);
}
}
if (Subviews?.Count == 0) {
base.Add (contentView);
contentView.Text = base.Text;
}
Border.Child = contentView;
}
void DrawFrame ()
{
DrawFrame (new Rect (0, 0, Frame.Width, Frame.Height), 0, fill: true);
}
///
/// Add the specified to this container.
///
/// to add to this container
public override void Add (View view)
{
contentView.Add (view);
if (view.CanFocus)
CanFocus = true;
}
///
/// Removes a from this container.
///
///
///
public override void Remove (View view)
{
if (view == null)
return;
SetNeedsDisplay ();
var touched = view.Frame;
if (view == contentView) {
base.Remove (view);
} else {
contentView.Remove (view);
}
if (contentView.InternalSubviews.Count < 1)
this.CanFocus = false;
}
///
/// Removes all s from this container.
///
///
///
public override void RemoveAll ()
{
contentView.RemoveAll ();
}
///
public override void Redraw (Rect bounds)
{
if (!NeedDisplay.IsEmpty) {
Driver.SetAttribute (GetNormalColor ());
Clear ();
}
var savedClip = contentView.ClipToBounds ();
contentView.Redraw (!NeedDisplay.IsEmpty ? contentView.Bounds : bounds);
Driver.Clip = savedClip;
ClearLayoutNeeded ();
ClearNeedsDisplay ();
Driver.SetAttribute (GetNormalColor ());
Border.DrawContent (this, false);
}
///
/// The text displayed by the .
///
public override ustring Text {
get => contentView?.Text;
set {
base.Text = value;
if (contentView != null) {
contentView.Text = value;
}
}
}
///
/// Controls the text-alignment property of the label, changing it will redisplay the .
///
/// The text alignment.
public override TextAlignment TextAlignment {
get => contentView.TextAlignment;
set {
base.TextAlignment = contentView.TextAlignment = value;
}
}
///
public override bool OnEnter (View view)
{
if (Subviews.Count == 0 || !Subviews.Any (subview => subview.CanFocus)) {
Application.Driver?.SetCursorVisibility (CursorVisibility.Invisible);
}
return base.OnEnter (view);
}
///
public override void OnCanFocusChanged ()
{
if (contentView != null) {
contentView.CanFocus = CanFocus;
}
base.OnCanFocusChanged ();
}
}
}