//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
// NOTE: Window is functionally identical to FrameView with the following exceptions.
// - Window is a Toplevel
// - FrameView Does not support padding (but should)
// - FrameView Does not support mouse dragging
// - FrameView Does not support IEnumerable
// Any udpates done here should probably be done in FrameView as well; TODO: Merge these classes
using System.Collections;
using NStack;
namespace Terminal.Gui {
///
/// A that draws a border around its with a at the top.
///
///
/// The 'client area' of a is a rectangle deflated by one or more rows/columns from . A this time there is no
/// API to determine this rectangle.
///
public class Window : Toplevel {
View contentView;
ustring title;
int padding;
///
/// The title to be displayed for this window.
///
/// The title
public ustring Title {
get => title;
set {
title = value;
SetNeedsDisplay ();
}
}
///
/// 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 with an optional title using positioning.
///
/// Superview-relative rectangle specifying the location and size
/// Title
///
/// This constructor initializes a Window with a of . Use constructors
/// that do not take Rect parameters to initialize a Window with .
///
public Window (Rect frame, ustring title = null) : this (frame, title, padding: 0)
{
}
///
/// Initializes a new instance of the class with an optional title using positioning.
///
/// Title.
///
/// This constructor initializes a View with a of .
/// Use , , , and properties to dynamically control the size and location of the view.
///
public Window (ustring title = null) : this (title, padding: 0)
{
}
///
/// Initializes a new instance of the class using positioning.
///
public Window () : this (title: null) { }
///
/// Initializes a new instance of the using positioning with the specified frame for its location, with the specified frame padding,
/// and an optional title.
///
/// Superview-relative rectangle specifying the location and size
/// Number of characters to use for padding of the drawn frame.
/// Title
///
/// This constructor initializes a Window with a of . Use constructors
/// that do not take Rect parameters to initialize a Window with of
///
public Window (Rect frame, ustring title = null, int padding = 0) : base (frame)
{
Initialize (title, frame, padding);
}
///
/// Initializes a new instance of the using positioning,
/// and an optional title.
///
/// Number of characters to use for padding of the drawn frame.
/// Title.
///
/// This constructor initializes a View with a of .
/// Use , , , and properties to dynamically control the size and location of the view.
///
public Window (ustring title = null, int padding = 0) : base ()
{
Initialize (title, Rect.Empty, padding);
}
void Initialize (ustring title, Rect frame, int padding = 0)
{
ColorScheme = Colors.Base;
Title = title;
int wb;
if (frame == Rect.Empty) {
wb = 1 + padding;
contentView = new ContentView () {
X = wb,
Y = wb,
Width = Dim.Fill (wb),
Height = Dim.Fill (wb)
};
} else {
wb = 2 * (1 + padding);
var cFrame = new Rect (1 + padding, 1 + padding, frame.Width - wb, frame.Height - wb);
contentView = new ContentView (cFrame);
}
this.padding = padding;
base.Add (contentView);
}
/////
///// Enumerates the various s in the embedded .
/////
///// The enumerator.
//public new IEnumerator GetEnumerator ()
//{
// return contentView.GetEnumerator ();
//}
///
public override void Add (View view)
{
contentView.Add (view);
if (view.CanFocus) {
CanFocus = true;
}
AddMenuStatusBar (view);
}
///
public override void Remove (View view)
{
if (view == null) {
return;
}
SetNeedsDisplay ();
var touched = view.Frame;
contentView.Remove (view);
if (contentView.InternalSubviews.Count < 1) {
CanFocus = false;
}
RemoveMenuStatusBar (view);
}
///
public override void RemoveAll ()
{
contentView.RemoveAll ();
}
///
public override void Redraw (Rect bounds)
{
//var padding = 0;
var scrRect = ViewToScreen (new Rect (0, 0, Frame.Width, Frame.Height));
// BUGBUG: Why do we draw the frame twice? This call is here to clear the content area, I think. Why not just clear that area?
if (!NeedDisplay.IsEmpty) {
Driver.SetAttribute (ColorScheme.Normal);
Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: true);
}
var savedClip = ClipToBounds ();
// Redraw our contentView
// TODO: smartly constrict contentView.Bounds to just be what intersects with the 'bounds' we were passed
contentView.Redraw (contentView.Bounds);
Driver.Clip = savedClip;
ClearLayoutNeeded ();
ClearNeedsDisplay ();
Driver.SetAttribute (ColorScheme.Normal);
Driver.DrawWindowFrame (scrRect, padding + 1, padding + 1, padding + 1, padding + 1, border: true, fill: false);
if (HasFocus)
Driver.SetAttribute (ColorScheme.HotNormal);
Driver.DrawWindowTitle (scrRect, Title, padding, padding, padding, padding);
Driver.SetAttribute (ColorScheme.Normal);
// Checks if there are any SuperView view which intersect with this window.
if (SuperView != null) {
SuperView.SetNeedsLayout ();
SuperView.SetNeedsDisplay ();
}
}
//
// FIXED:It does not look like the event is raised on clicked-drag
// need to figure that out.
//
internal static Point? dragPosition;
Point start;
///
public override bool MouseEvent (MouseEvent mouseEvent)
{
// FIXED:The code is currently disabled, because the
// Driver.UncookMouse does not seem to have an effect if there is
// a pending mouse event activated.
int nx, ny;
if (!dragPosition.HasValue && (mouseEvent.Flags == MouseFlags.Button1Pressed
|| mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition))) {
// Only start grabbing if the user clicks on the title bar.
if (mouseEvent.Y == 0) {
start = new Point (mouseEvent.X, mouseEvent.Y);
dragPosition = new Point ();
nx = mouseEvent.X - mouseEvent.OfX;
ny = mouseEvent.Y - mouseEvent.OfY;
dragPosition = new Point (nx, ny);
Application.GrabMouse (this);
}
//System.Diagnostics.Debug.WriteLine ($"Starting at {dragPosition}");
return true;
} else if (mouseEvent.Flags == (MouseFlags.Button1Pressed | MouseFlags.ReportMousePosition) ||
mouseEvent.Flags == MouseFlags.Button3Pressed) {
if (dragPosition.HasValue) {
if (SuperView == null) {
Application.Top.SetNeedsDisplay (Frame);
// Redraw the entire app window using just our Frame. Since we are
// Application.Top, and our Frame always == our Bounds (Location is always (0,0))
// our Frame is actually view-relative (which is what Redraw takes).
Application.Top.Redraw (Frame);
} else {
SuperView.SetNeedsDisplay (Frame);
}
EnsureVisibleBounds (this, mouseEvent.X + (SuperView == null ? mouseEvent.OfX - start.X : Frame.X - start.X),
mouseEvent.Y + (SuperView == null ? mouseEvent.OfY : Frame.Y), out nx, out ny);
dragPosition = new Point (nx, ny);
LayoutSubviews ();
Frame = new Rect (nx, ny, Frame.Width, Frame.Height);
if (X == null || X is Pos.PosAbsolute) {
X = nx;
}
if (Y == null || Y is Pos.PosAbsolute) {
Y = ny;
}
//System.Diagnostics.Debug.WriteLine ($"nx:{nx},ny:{ny}");
// FIXED: optimize, only SetNeedsDisplay on the before/after regions.
SetNeedsDisplay ();
return true;
}
}
if (mouseEvent.Flags == MouseFlags.Button1Released && dragPosition.HasValue) {
Application.UngrabMouse ();
Driver.UncookMouse ();
dragPosition = null;
}
//System.Diagnostics.Debug.WriteLine (mouseEvent.ToString ());
return 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;
}
}
}
}