using NStack;
using System;
using System.Collections.Generic;
using Terminal.Gui;
using static Terminal.Gui.ConfigurationManager;
namespace Terminal.Gui {
///
/// MessageBox displays a modal message to the user, with a title, a message and a series of options that the user can choose from.
///
///
/// The difference between the and
/// method is the default set of colors used for the message box.
///
///
/// The following example pops up a with the specified title and text, plus two s.
/// The value -1 is returned when the user cancels the by pressing the ESC key.
///
///
///
/// var n = MessageBox.Query ("Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
/// if (n == 0)
/// quit = true;
/// else
/// quit = false;
///
///
public static class MessageBox {
///
/// Presents a normal with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Width for the window.
/// Height for the window.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Array of buttons to add.
///
/// Use instead; it automatically sizes the MessageBox based on the contents.
///
public static int Query (int width, int height, ustring title, ustring message, params ustring [] buttons)
{
return QueryFull (false, width, height, title, message, 0, true, buttons);
}
///
/// Presents an error with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Array of buttons to add.
///
/// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
/// from the size of the message and buttons.
///
public static int Query (ustring title, ustring message, params ustring [] buttons)
{
return QueryFull (false, 0, 0, title, message, 0, true, buttons);
}
///
/// Presents an error with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Width for the window.
/// Height for the window.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Array of buttons to add.
///
/// Use instead; it automatically sizes the MessageBox based on the contents.
///
public static int ErrorQuery (int width, int height, ustring title, ustring message, params ustring [] buttons)
{
return QueryFull (true, width, height, title, message, 0, true, buttons);
}
///
/// Presents an error with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Array of buttons to add.
///
/// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
/// from the size of the title, message. and buttons.
///
public static int ErrorQuery (ustring title, ustring message, params ustring [] buttons)
{
return QueryFull (true, 0, 0, title, message, 0, true, buttons);
}
///
/// Presents a normal with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Width for the window.
/// Height for the window.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Index of the default button.
/// Array of buttons to add.
///
/// Use instead; it automatically sizes the MessageBox based on the contents.
///
public static int Query (int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
{
return QueryFull (false, width, height, title, message, defaultButton, true, buttons);
}
///
/// Presents an error with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Index of the default button.
/// Array of buttons to add.
///
/// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
/// from the size of the message and buttons.
///
public static int Query (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
{
return QueryFull (false, 0, 0, title, message, defaultButton, true, buttons);
}
///
/// Presents a normal with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Width for the window.
/// Height for the window.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Index of the default button.
/// If wrap the message or not.
/// Array of buttons to add.
///
/// Use instead; it automatically sizes the MessageBox based on the contents.
///
public static int Query (int width, int height, ustring title, ustring message, int defaultButton = 0, bool wrapMessagge = true, params ustring [] buttons)
{
return QueryFull (false, width, height, title, message, defaultButton, wrapMessagge, buttons);
}
///
/// Presents an error with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Index of the default button.
/// If wrap the message or not.
/// Array of buttons to add.
///
/// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
/// from the size of the message and buttons.
///
public static int Query (ustring title, ustring message, int defaultButton = 0, bool wrapMessagge = true, params ustring [] buttons)
{
return QueryFull (false, 0, 0, title, message, defaultButton, wrapMessagge, buttons);
}
///
/// Presents an error with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Width for the window.
/// Height for the window.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Index of the default button.
/// Array of buttons to add.
///
/// Use instead; it automatically sizes the MessageBox based on the contents.
///
public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
{
return QueryFull (true, width, height, title, message, defaultButton, true, buttons);
}
///
/// Presents an error with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Index of the default button.
/// Array of buttons to add.
///
/// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
/// from the size of the title, message. and buttons.
///
public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
{
return QueryFull (true, 0, 0, title, message, defaultButton, true, buttons);
}
///
/// Presents an error with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Width for the window.
/// Height for the window.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Index of the default button.
/// If wrap the message or not.
/// Array of buttons to add.
///
/// Use instead; it automatically sizes the MessageBox based on the contents.
///
public static int ErrorQuery (int width, int height, ustring title, ustring message, int defaultButton = 0, bool wrapMessagge = true, params ustring [] buttons)
{
return QueryFull (true, width, height, title, message, defaultButton, wrapMessagge, buttons);
}
///
/// Presents an error with the specified title and message and a list of buttons to show to the user.
///
/// The index of the selected button, or -1 if the user pressed ESC to close the dialog.
/// Title for the query.
/// Message to display, might contain multiple lines.
/// Index of the default button.
/// If wrap the message or not.
/// Array of buttons to add.
///
/// The message box will be vertically and horizontally centered in the container and the size will be automatically determined
/// from the size of the title, message. and buttons.
///
public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, bool wrapMessagge = true, params ustring [] buttons)
{
return QueryFull (true, 0, 0, title, message, defaultButton, wrapMessagge, buttons);
}
///
/// Defines the default border styling for . Can be configured via .
///
[SerializableConfigurationProperty (Scope = typeof (ThemeScope))]
public static Border DefaultBorder { get; set; } = new Border () {
BorderStyle = BorderStyle.Single,
};
static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message,
int defaultButton = 0, bool wrapMessagge = true, params ustring [] buttons)
{
int defaultWidth = 50;
if (defaultWidth > Application.Driver.Cols / 2) {
defaultWidth = (int)(Application.Driver.Cols * 0.60f);
}
int maxWidthLine = TextFormatter.MaxWidthLine (message);
if (wrapMessagge && maxWidthLine > Application.Driver.Cols) {
maxWidthLine = Application.Driver.Cols;
}
if (width == 0) {
maxWidthLine = Math.Max (maxWidthLine, defaultWidth);
} else {
maxWidthLine = width;
}
int textWidth = TextFormatter.MaxWidth (message, maxWidthLine);
int textHeight = TextFormatter.MaxLines (message, textWidth); // message.Count (ustring.Make ('\n')) + 1;
int msgboxHeight = Math.Max (1, textHeight) + 4; // textHeight + (top + top padding + buttons + bottom)
if (wrapMessagge) {
textWidth = Math.Min (textWidth, Application.Driver.Cols);
msgboxHeight = Math.Min (msgboxHeight, Application.Driver.Rows);
}
// Create button array for Dialog
int count = 0;
List