//
// Toplevel.cs: Toplevel views can be modally executed
//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace Terminal.Gui {
///
/// Toplevel views can be modally executed.
///
///
///
/// Toplevels can be modally executing views, started by calling .
/// They return control to the caller when has
/// been called (which sets the property to false).
///
///
/// A Toplevel is created when an application initializes Terminal.Gui by calling .
/// The application Toplevel can be accessed via . Additional Toplevels can be created
/// and run (e.g. s. To run a Toplevel, create the and
/// call .
///
///
/// Toplevels can also opt-in to more sophisticated initialization
/// by implementing . When they do
/// so, the and
/// methods will be called
/// before running the view.
/// If first-run-only initialization is preferred, the
/// can be implemented too, in which case the
/// methods will only be called if
/// is . This allows proper inheritance hierarchies
/// to override base class layout code optimally by doing so only on first run,
/// instead of on every run.
///
///
public class Toplevel : View {
///
/// Gets or sets whether the for this is running or not.
///
///
/// Setting this property directly is discouraged. Use instead.
///
public bool Running { get; set; }
///
/// Fired once the Toplevel's has begin loaded.
/// A Loaded event handler is a good place to finalize initialization before calling `.
///
public event Action Loaded;
///
/// Fired once the Toplevel's has started it's first iteration.
/// Subscribe to this event to perform tasks when the has been laid out and focus has been set.
/// changes. A Ready event handler is a good place to finalize initialization after calling `(topLevel)`.
///
public event Action Ready;
///
/// Fired once the Toplevel's has begin unloaded.
/// A Unloaded event handler is a good place to disposing after calling `.
///
public event Action Unloaded;
///
/// Invoked once the Toplevel's becomes the .
///
public event Action Activate;
///
/// Invoked once the Toplevel's ceases to be the .
///
public event Action Deactivate;
///
/// Invoked once the child Toplevel's is closed from the
///
public event Action ChildClosed;
///
/// Invoked once the last child Toplevel's is closed from the
///
public event Action AllChildClosed;
///
/// Invoked once the Toplevel's is being closing from the
///
public event Action Closing;
///
/// Invoked once the Toplevel's is closed from the
///
public event Action Closed;
///
/// Invoked once the child Toplevel's has begin loaded.
///
public event Action ChildLoaded;
///
/// Invoked once the child Toplevel's has begin unloaded.
///
public event Action ChildUnloaded;
internal virtual void OnChildUnloaded (Toplevel top)
{
ChildUnloaded?.Invoke (top);
}
internal virtual void OnChildLoaded (Toplevel top)
{
ChildLoaded?.Invoke (top);
}
internal virtual void OnClosed (Toplevel top)
{
Closed?.Invoke (top);
}
internal virtual bool OnClosing (ToplevelClosingEventArgs ev)
{
Closing?.Invoke (ev);
return ev.Cancel;
}
internal virtual void OnAllChildClosed ()
{
AllChildClosed?.Invoke ();
}
internal virtual void OnChildClosed (Toplevel top)
{
if (IsMdiContainer) {
SetChildNeedsDisplay ();
}
ChildClosed?.Invoke (top);
}
internal virtual void OnDeactivate (Toplevel activated)
{
Deactivate?.Invoke (activated);
}
internal virtual void OnActivate (Toplevel deactivated)
{
Activate?.Invoke (deactivated);
}
///
/// Called from before the is redraws for the first time.
///
internal virtual void OnLoaded ()
{
Loaded?.Invoke ();
}
///
/// Called from after the has entered it's first iteration of the loop.
///
internal virtual void OnReady ()
{
Ready?.Invoke ();
}
///
/// Called from before the is disposed.
///
internal virtual void OnUnloaded ()
{
Unloaded?.Invoke ();
}
///
/// Initializes a new instance of the class with the specified absolute layout.
///
/// A superview-relative rectangle specifying the location and size for the new Toplevel
public Toplevel (Rect frame) : base (frame)
{
Initialize ();
}
///
/// Initializes a new instance of the class with layout, defaulting to full screen.
///
public Toplevel () : base ()
{
Initialize ();
Width = Dim.Fill ();
Height = Dim.Fill ();
}
void Initialize ()
{
ColorScheme = Colors.TopLevel;
}
///
/// Convenience factory method that creates a new Toplevel with the current terminal dimensions.
///
/// The create.
public static Toplevel Create ()
{
return new Toplevel (new Rect (0, 0, Driver.Cols, Driver.Rows));
}
///
/// Gets or sets a value indicating whether this can focus.
///
/// true if can focus; otherwise, false.
public override bool CanFocus {
get => true;
}
///
/// Determines whether the is modal or not.
/// Causes to propagate keys upwards
/// by default unless set to .
///
public bool Modal { get; set; }
///
/// Gets or sets the menu for this Toplevel
///
public virtual MenuBar MenuBar { get; set; }
///
/// Gets or sets the status bar for this Toplevel
///
public virtual StatusBar StatusBar { get; set; }
///
/// Gets or sets if this Toplevel is a Mdi container.
///
public bool IsMdiContainer { get; set; }
///
/// Gets or sets if this Toplevel is a Mdi child.
///
public bool IsMdiChild {
get {
return Application.MdiTop != null && Application.MdiTop != this && !Modal;
}
}
///
public override bool OnKeyDown (KeyEvent keyEvent)
{
if (base.OnKeyDown (keyEvent)) {
return true;
}
switch (keyEvent.Key) {
case Key.AltMask:
case Key.AltMask | Key.Space:
case Key.CtrlMask | Key.Space:
if (MenuBar != null && MenuBar.OnKeyDown (keyEvent)) {
return true;
}
break;
}
return false;
}
///
public override bool OnKeyUp (KeyEvent keyEvent)
{
if (base.OnKeyUp (keyEvent)) {
return true;
}
switch (keyEvent.Key) {
case Key.AltMask:
case Key.AltMask | Key.Space:
case Key.CtrlMask | Key.Space:
if (MenuBar != null && MenuBar.OnKeyUp (keyEvent)) {
return true;
}
break;
}
return false;
}
///
public override bool ProcessKey (KeyEvent keyEvent)
{
if (base.ProcessKey (keyEvent))
return true;
switch (ShortcutHelper.GetModifiersKey (keyEvent)) {
case Key k when k == Application.QuitKey:
// FIXED: stop current execution of this container
if (Application.MdiTop != null) {
Application.MdiTop.RequestStop ();
} else {
Application.RequestStop ();
}
break;
case Key.Z | Key.CtrlMask:
Driver.Suspend ();
return true;
#if false
case Key.F5:
Application.DebugDrawBounds = !Application.DebugDrawBounds;
SetNeedsDisplay ();
return true;
#endif
case Key.Tab:
case Key.CursorRight:
case Key.CursorDown:
case Key.I | Key.CtrlMask: // Unix
var old = GetDeepestFocusedSubview (Focused);
if (!FocusNext ())
FocusNext ();
if (old != Focused && old != Focused?.Focused) {
old?.SetNeedsDisplay ();
Focused?.SetNeedsDisplay ();
} else {
FocusNearestView (SuperView?.TabIndexes, Direction.Forward);
}
return true;
case Key.BackTab | Key.ShiftMask:
case Key.CursorLeft:
case Key.CursorUp:
old = GetDeepestFocusedSubview (Focused);
if (!FocusPrev ())
FocusPrev ();
if (old != Focused && old != Focused?.Focused) {
old?.SetNeedsDisplay ();
Focused?.SetNeedsDisplay ();
} else {
FocusNearestView (SuperView?.TabIndexes?.Reverse (), Direction.Backward);
}
return true;
case Key.Tab | Key.CtrlMask:
case Key key when key == Application.AlternateForwardKey: // Needed on Unix
if (Application.MdiTop == null) {
var top = Modal ? this : Application.Top;
top.FocusNext ();
if (top.Focused == null) {
top.FocusNext ();
}
top.SetNeedsDisplay ();
} else {
MoveNext ();
}
return true;
case Key.Tab | Key.ShiftMask | Key.CtrlMask:
case Key key when key == Application.AlternateBackwardKey: // Needed on Unix
if (Application.MdiTop == null) {
var top = Modal ? this : Application.Top;
top.FocusPrev ();
if (top.Focused == null) {
top.FocusPrev ();
}
top.SetNeedsDisplay ();
} else {
MovePrevious ();
}
return true;
case Key.L | Key.CtrlMask:
Application.Refresh ();
return true;
}
return false;
}
///
public override bool ProcessColdKey (KeyEvent keyEvent)
{
if (base.ProcessColdKey (keyEvent)) {
return true;
}
if (ShortcutHelper.FindAndOpenByShortcut (keyEvent, this)) {
return true;
}
return false;
}
View GetDeepestFocusedSubview (View view)
{
if (view == null) {
return null;
}
foreach (var v in view.Subviews) {
if (v.HasFocus) {
return GetDeepestFocusedSubview (v);
}
}
return view;
}
void FocusNearestView (IEnumerable views, Direction direction)
{
if (views == null) {
return;
}
bool found = false;
bool focusProcessed = false;
int idx = 0;
foreach (var v in views) {
if (v == this) {
found = true;
}
if (found && v != this) {
if (direction == Direction.Forward) {
SuperView?.FocusNext ();
} else {
SuperView?.FocusPrev ();
}
focusProcessed = true;
if (SuperView.Focused != null && SuperView.Focused != this) {
return;
}
} else if (found && !focusProcessed && idx == views.Count () - 1) {
views.ToList () [0].SetFocus ();
}
idx++;
}
}
///
public override void Add (View view)
{
AddMenuStatusBar (view);
base.Add (view);
}
internal void AddMenuStatusBar (View view)
{
if (view is MenuBar) {
MenuBar = view as MenuBar;
}
if (view is StatusBar) {
StatusBar = view as StatusBar;
}
}
///
public override void Remove (View view)
{
if (this is Toplevel toplevel && toplevel.MenuBar != null) {
RemoveMenuStatusBar (view);
}
base.Remove (view);
}
///
public override void RemoveAll ()
{
if (this == Application.Top) {
MenuBar?.Dispose ();
MenuBar = null;
StatusBar?.Dispose ();
StatusBar = null;
}
base.RemoveAll ();
}
internal void RemoveMenuStatusBar (View view)
{
if (view is MenuBar) {
MenuBar?.Dispose ();
MenuBar = null;
}
if (view is StatusBar) {
StatusBar?.Dispose ();
StatusBar = null;
}
}
internal View EnsureVisibleBounds (Toplevel top, int x, int y,
out int nx, out int ny, out View mb, out View sb)
{
int l;
View superView;
if (top?.SuperView == null || top == Application.Top || top?.SuperView == Application.Top) {
l = Driver.Cols;
superView = Application.Top;
} else {
l = top.SuperView.Frame.Width;
superView = top.SuperView;
}
nx = Math.Max (x, 0);
nx = nx + top.Frame.Width > l ? Math.Max (l - top.Frame.Width, 0) : nx;
SetWidth (top.Frame.Width, out int rWidth);
if (rWidth < 0 && nx >= top.Frame.X) {
nx = Math.Max (top.Frame.Right - 2, 0);
}
//System.Diagnostics.Debug.WriteLine ($"nx:{nx}, rWidth:{rWidth}");
bool m, s;
if (top?.SuperView == null || top == Application.Top || top?.SuperView == Application.Top) {
m = Application.Top.MenuBar?.Visible == true;
mb = Application.Top.MenuBar;
} else {
var t = top.SuperView;
while (!(t is Toplevel)) {
t = t.SuperView;
}
m = ((Toplevel)t).MenuBar?.Visible == true;
mb = ((Toplevel)t).MenuBar;
}
if (top?.SuperView == null || top == Application.Top || top?.SuperView == Application.Top) {
l = m ? 1 : 0;
} else {
l = 0;
}
ny = Math.Max (y, l);
if (top?.SuperView == null || top == Application.Top || top?.SuperView == Application.Top) {
s = Application.Top.StatusBar?.Visible == true;
sb = Application.Top.StatusBar;
} else {
var t = top.SuperView;
while (!(t is Toplevel)) {
t = t.SuperView;
}
s = ((Toplevel)t).StatusBar?.Visible == true;
sb = ((Toplevel)t).StatusBar;
}
if (top?.SuperView == null || top == Application.Top || top?.SuperView == Application.Top) {
l = s ? Driver.Rows - 1 : Driver.Rows;
} else {
l = s ? top.SuperView.Frame.Height - 1 : top.SuperView.Frame.Height;
}
ny = Math.Min (ny, l);
ny = ny + top.Frame.Height >= l ? Math.Max (l - top.Frame.Height, m ? 1 : 0) : ny;
SetHeight (top.Frame.Height, out int rHeight);
if (rHeight < 0 && ny >= top.Frame.Y) {
ny = Math.Max (top.Frame.Bottom - 2, 0);
}
//System.Diagnostics.Debug.WriteLine ($"ny:{ny}, rHeight:{rHeight}");
return superView;
}
internal void PositionToplevels ()
{
PositionToplevel (this);
foreach (var top in Subviews) {
if (top is Toplevel) {
PositionToplevel ((Toplevel)top);
}
}
}
///
/// Virtual method which allow to be overridden to implement specific positions for inherited .
///
/// The toplevel.
public virtual void PositionToplevel (Toplevel top)
{
var superView = EnsureVisibleBounds (top, top.Frame.X, top.Frame.Y,
out int nx, out int ny, out _, out View sb);
bool layoutSubviews = false;
if ((top?.SuperView != null || (top != Application.Top && top.Modal)
|| (top?.SuperView == null && top.IsMdiChild))
&& (nx > top.Frame.X || ny > top.Frame.Y) && top.LayoutStyle == LayoutStyle.Computed) {
if ((top.X == null || top.X is Pos.PosAbsolute) && top.Bounds.X != nx) {
top.X = nx;
layoutSubviews = true;
}
if ((top.Y == null || top.Y is Pos.PosAbsolute) && top.Bounds.Y != ny) {
top.Y = ny;
layoutSubviews = true;
}
}
if (sb != null && ny + top.Frame.Height != superView.Frame.Height - (sb.Visible ? 1 : 0)
&& top.Height is Dim.DimFill) {
top.Height = Dim.Fill (sb.Visible ? 1 : 0);
layoutSubviews = true;
}
if (layoutSubviews) {
superView.LayoutSubviews ();
}
}
///
public override void Redraw (Rect bounds)
{
if (!Visible) {
return;
}
if (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded) {
Driver.SetAttribute (GetNormalColor ());
// This is the Application.Top. Clear just the region we're being asked to redraw
// (the bounds passed to us).
Clear ();
Driver.SetAttribute (Enabled ? Colors.Base.Normal : Colors.Base.Disabled);
LayoutSubviews ();
PositionToplevels ();
if (this == Application.MdiTop) {
foreach (var top in Application.MdiChildes.AsEnumerable ().Reverse ()) {
if (top.Frame.IntersectsWith (bounds)) {
if (top != this && !top.IsCurrentTop && !OutsideTopFrame (top) && top.Visible) {
top.SetNeedsLayout ();
top.SetNeedsDisplay (top.Bounds);
top.Redraw (top.Bounds);
}
}
}
}
foreach (var view in Subviews) {
if (view.Frame.IntersectsWith (bounds) && !OutsideTopFrame (this)) {
view.SetNeedsLayout ();
view.SetNeedsDisplay (view.Bounds);
//view.Redraw (view.Bounds);
}
}
ClearLayoutNeeded ();
ClearNeedsDisplay ();
}
base.Redraw (Bounds);
}
bool OutsideTopFrame (Toplevel top)
{
if (top.Frame.X > Driver.Cols || top.Frame.Y > Driver.Rows) {
return true;
}
return false;
}
//
// 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)) {
// 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);
SuperView?.BringSubviewToFront (this);
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) {
// 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).
// We need to pass all the view bounds because since the windows was
// moved around, we don't know exactly what was the affected region.
Application.Top.SetNeedsDisplay ();
} else {
SuperView.SetNeedsDisplay ();
}
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, out _, out _);
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;
}
///
/// Invoked by as part of the after
/// the views have been laid out, and before the views are drawn for the first time.
///
public virtual void WillPresent ()
{
FocusFirst ();
}
///
/// Move to the next Mdi child from the .
///
public virtual void MoveNext ()
{
Application.MoveNext ();
}
///
/// Move to the previous Mdi child from the .
///
public virtual void MovePrevious ()
{
Application.MovePrevious ();
}
///
/// Stops running this .
///
public virtual void RequestStop ()
{
if (IsMdiContainer && Running
&& (Application.Current == this
|| Application.Current?.Modal == false
|| Application.Current?.Modal == true && Application.Current?.Running == false)) {
foreach (var child in Application.MdiChildes) {
var ev = new ToplevelClosingEventArgs (this);
if (child.OnClosing (ev)) {
return;
}
child.Running = false;
Application.RequestStop (child);
}
Running = false;
Application.RequestStop (this);
} else if (IsMdiContainer && Running && Application.Current?.Modal == true && Application.Current?.Running == true) {
var ev = new ToplevelClosingEventArgs (Application.Current);
if (OnClosing (ev)) {
return;
}
Application.RequestStop (Application.Current);
} else if (!IsMdiContainer && Running && (!Modal || (Modal && Application.Current != this))) {
var ev = new ToplevelClosingEventArgs (this);
if (OnClosing (ev)) {
return;
}
Running = false;
Application.RequestStop (this);
} else {
Application.RequestStop (Application.Current);
}
}
///
/// Stops running the .
///
/// The toplevel to request stop.
public virtual void RequestStop (Toplevel top)
{
top.RequestStop ();
}
///
public override void PositionCursor ()
{
if (!IsMdiContainer) {
base.PositionCursor ();
return;
}
if (Focused == null) {
foreach (var top in Application.MdiChildes) {
if (top != this && top.Visible) {
top.SetFocus ();
return;
}
}
}
base.PositionCursor ();
}
///
/// Gets the current visible toplevel Mdi child that match the arguments pattern.
///
/// The type.
/// The strings to exclude.
/// The matched view.
public View GetTopMdiChild (Type type = null, string [] exclude = null)
{
if (Application.MdiTop == null) {
return null;
}
foreach (var top in Application.MdiChildes) {
if (type != null && top.GetType () == type
&& exclude?.Contains (top.Data.ToString ()) == false) {
return top;
} else if ((type != null && top.GetType () != type)
|| (exclude?.Contains (top.Data.ToString ()) == true)) {
continue;
}
return top;
}
return null;
}
///
/// Shows the Mdi child indicated by the setting as .
///
/// The toplevel.
/// if the toplevel can be showed. otherwise.
public virtual bool ShowChild (Toplevel top = null)
{
if (Application.MdiTop != null) {
return Application.ShowChild (top == null ? this : top);
}
return false;
}
}
///
/// Implements the to comparing two used by .
///
public class ToplevelEqualityComparer : IEqualityComparer {
/// Determines whether the specified objects are equal.
/// The first object of type to compare.
/// The second object of type to compare.
///
/// if the specified objects are equal; otherwise, .
public bool Equals (Toplevel x, Toplevel y)
{
if (y == null && x == null)
return true;
else if (x == null || y == null)
return false;
else if (x.Id == y.Id)
return true;
else
return false;
}
/// Returns a hash code for the specified object.
/// The for which a hash code is to be returned.
/// A hash code for the specified object.
/// The type of is a reference type and is .
public int GetHashCode (Toplevel obj)
{
if (obj == null)
throw new ArgumentNullException ();
int hCode = 0;
if (int.TryParse (obj.Id.ToString (), out int result)) {
hCode = result;
}
return hCode.GetHashCode ();
}
}
///
/// Implements the to sort the from the if needed.
///
public sealed class ToplevelComparer : IComparer {
/// Compares two objects and returns a value indicating whether one is less than, equal to, or greater than the other.
/// The first object to compare.
/// The second object to compare.
/// A signed integer that indicates the relative values of and , as shown in the following table.Value Meaning Less than zero
/// is less than .Zero
/// equals .Greater than zero
/// is greater than .
public int Compare (Toplevel x, Toplevel y)
{
if (ReferenceEquals (x, y))
return 0;
else if (x == null)
return -1;
else if (y == null)
return 1;
else
return string.Compare (x.Id.ToString (), y.Id.ToString ());
}
}
///
/// implementation for the event.
///
public class ToplevelClosingEventArgs : EventArgs {
///
/// The toplevel requesting stop.
///
public View RequestingTop { get; }
///
/// Provides an event cancellation option.
///
public bool Cancel { get; set; }
///
/// Initializes the event arguments with the requesting toplevel.
///
/// The .
public ToplevelClosingEventArgs (Toplevel requestingTop)
{
RequestingTop = requestingTop;
}
}
}