using NStack;
using System;
using System.Collections.Generic;
using System.Linq;
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, null, 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, null, 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, null, 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, null, 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, null, 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, null, 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.
/// The border settings.
/// 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, Border border = null, params ustring [] buttons)
{
return QueryFull (false, width, height, title, message, defaultButton, border, 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.
/// The border settings.
/// 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, Border border = null, params ustring [] buttons)
{
return QueryFull (false, 0, 0, title, message, defaultButton, border, 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, null, 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, null, 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.
/// The border settings.
/// 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, Border border = null, params ustring [] buttons)
{
return QueryFull (true, width, height, title, message, defaultButton, border, 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.
/// The border settings.
/// 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, Border border = null, params ustring [] buttons)
{
return QueryFull (true, 0, 0, title, message, defaultButton, border, buttons);
}
static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message,
int defaultButton = 0, Border border = null, params ustring [] buttons)
{
const int defaultWidth = 50;
int textWidth = TextFormatter.MaxWidth (message, width == 0 ? defaultWidth : width);
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)
// Create button array for Dialog
int count = 0;
List