//
// Dialog.cs: Dialog box
//
// Authors:
// Miguel de Icaza (miguel@gnome.org)
//
using System;
using System.Collections.Generic;
using System.Linq;
using NStack;
namespace Terminal.Gui {
///
/// The is a that by default is centered and contains one
/// or more s. It defaults to the color scheme and has a 1 cell padding around the edges.
///
///
/// To run the modally, create the , and pass it to .
/// This will execute the dialog until it terminates via the [ESC] or [CTRL-Q] key, or when one of the views
/// or buttons added to the dialog calls .
///
public class Dialog : Window {
List buttons = new List ();
const int padding = 0;
///
/// Initializes a new instance of the class using positioning
/// and an optional set of s to display
///
/// Title for the dialog.
/// Width for the dialog.
/// Height for the dialog.
/// Optional buttons to lay out at the bottom of the dialog.
///
/// if width and height are both 0, the Dialog will be vertically and horizontally centered in the
/// container and the size will be 85% of the container.
/// After initialzation use X , Y , Width , and Height to override this with a location or size.
///
///
/// Use the constructor that does not take a width and height instead.
///
public Dialog (ustring title, int width, int height, params Button [] buttons) : base (title, padding: padding)
{
X = Pos.Center ();
Y = Pos.Center ();
if (width == 0 & height == 0) {
Width = Dim.Percent (85);
Height = Dim.Percent (85);
} else {
Width = width;
Height = height;
}
ColorScheme = Colors.Dialog;
Modal = true;
if (buttons != null) {
foreach (var b in buttons) {
this.buttons.Add (b);
Add (b);
}
}
LayoutStarted += (args) => {
LayoutStartedHandler ();
};
}
///
/// Initializes a new instance of the class using .
///
///
///
/// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container.
/// After initialzation use X , Y , Width , and Height to override this with a location or size.
///
///
/// Use to add buttons to the dialog.
///
///
public Dialog () : this (title: string.Empty, width: 0, height: 0, buttons: null) { }
///
/// Initializes a new instance of the class using positioning
/// and with an optional set of s to display
///
/// Title for the dialog.
/// Optional buttons to lay out at the bottom of the dialog.
///
/// Te Dialog will be vertically and horizontally centered in the container and the size will be 85% of the container.
/// After initialzation use X , Y , Width , and Height to override this with a location or size.
///
public Dialog (ustring title, params Button [] buttons) : this (title: title, width: 0, height: 0, buttons: buttons) { }
///
/// Adds a to the , its layout will be controlled by the
///
/// Button to add.
public void AddButton (Button button)
{
if (button == null)
return;
buttons.Add (button);
Add (button);
LayoutSubviews ();
}
internal int GetButtonsWidth ()
{
if (buttons.Count == 0) {
return 0;
}
return buttons.Select (b => b.Bounds.Width).Sum () + buttons.Count() - 1;
}
void LayoutStartedHandler ()
{
int buttonsWidth = GetButtonsWidth ();
int shiftLeft = Math.Max ((Bounds.Width - buttonsWidth) / 2 - 2, 0);
for (int i = buttons.Count - 1; i >= 0; i--) {
Button button = buttons [i];
shiftLeft += button.Frame.Width + 1;
button.X = Pos.AnchorEnd (shiftLeft);
button.Y = Pos.AnchorEnd (1);
}
}
///
public override bool ProcessKey (KeyEvent kb)
{
switch (kb.Key) {
case Key.Esc:
Running = false;
return true;
}
return base.ProcessKey (kb);
}
}
}