|
@@ -1,18 +1,22 @@
|
|
using System;
|
|
using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Linq;
|
|
|
|
+
|
|
namespace Terminal.Gui {
|
|
namespace Terminal.Gui {
|
|
/// <summary>
|
|
/// <summary>
|
|
/// MessageBox displays a modal message to the user, with a title, a message and a series of options that the user can choose from.
|
|
/// MessageBox displays a modal message to the user, with a title, a message and a series of options that the user can choose from.
|
|
/// </summary>
|
|
/// </summary>
|
|
/// <para>
|
|
/// <para>
|
|
- /// The difference between the <see cref="Query"/> and <see cref="ErrorQuery"/> method is the default set of colors used for the message box.
|
|
|
|
|
|
+ /// The difference between the <see cref="Query(string, string, string[])"/> and <see cref="ErrorQuery(string, string, string[])"/>
|
|
|
|
+ /// method is the default set of colors used for the message box.
|
|
/// </para>
|
|
/// </para>
|
|
/// <para>
|
|
/// <para>
|
|
- /// The following example pops up a <see cref="MessageBox"/> with 50 columns, and 7 lines, with the specified title and text, plus two <see cref="Button"/>s.
|
|
|
|
|
|
+ /// The following example pops up a <see cref="MessageBox"/> with the specified title and text, plus two <see cref="Button"/>s.
|
|
/// The value -1 is returned when the user cancels the <see cref="MessageBox"/> by pressing the ESC key.
|
|
/// The value -1 is returned when the user cancels the <see cref="MessageBox"/> by pressing the ESC key.
|
|
/// </para>
|
|
/// </para>
|
|
/// <example>
|
|
/// <example>
|
|
/// <code lang="c#">
|
|
/// <code lang="c#">
|
|
- /// var n = MessageBox.Query (50, 7, "Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
|
|
|
|
|
|
+ /// var n = MessageBox.Query ("Quit Demo", "Are you sure you want to quit this demo?", "Yes", "No");
|
|
/// if (n == 0)
|
|
/// if (n == 0)
|
|
/// quit = true;
|
|
/// quit = true;
|
|
/// else
|
|
/// else
|
|
@@ -29,11 +33,30 @@ namespace Terminal.Gui {
|
|
/// <param name="title">Title for the query.</param>
|
|
/// <param name="title">Title for the query.</param>
|
|
/// <param name="message">Message to display, might contain multiple lines..</param>
|
|
/// <param name="message">Message to display, might contain multiple lines..</param>
|
|
/// <param name="buttons">Array of buttons to add.</param>
|
|
/// <param name="buttons">Array of buttons to add.</param>
|
|
|
|
+ /// <remarks>
|
|
|
|
+ /// Use <see cref="Query(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the contents.
|
|
|
|
+ /// </remarks>
|
|
public static int Query (int width, int height, string title, string message, params string [] buttons)
|
|
public static int Query (int width, int height, string title, string message, params string [] buttons)
|
|
{
|
|
{
|
|
return QueryFull (false, width, height, title, message, buttons);
|
|
return QueryFull (false, width, height, title, message, buttons);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
|
|
|
|
+ /// <param name="title">Title for the query.</param>
|
|
|
|
+ /// <param name="message">Message to display, might contain multiple lines.</param>
|
|
|
|
+ /// <param name="buttons">Array of buttons to add.</param>
|
|
|
|
+ /// <remarks>
|
|
|
|
+ /// 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.
|
|
|
|
+ /// </remarks>
|
|
|
|
+ public static int Query (string title, string message, params string [] buttons)
|
|
|
|
+ {
|
|
|
|
+ return QueryFull (false, 0, 0, title, message, buttons);
|
|
|
|
+ }
|
|
|
|
+
|
|
/// <summary>
|
|
/// <summary>
|
|
/// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
|
|
/// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
|
|
/// </summary>
|
|
/// </summary>
|
|
@@ -43,34 +66,91 @@ namespace Terminal.Gui {
|
|
/// <param name="title">Title for the query.</param>
|
|
/// <param name="title">Title for the query.</param>
|
|
/// <param name="message">Message to display, might contain multiple lines.</param>
|
|
/// <param name="message">Message to display, might contain multiple lines.</param>
|
|
/// <param name="buttons">Array of buttons to add.</param>
|
|
/// <param name="buttons">Array of buttons to add.</param>
|
|
|
|
+ /// <remarks>
|
|
|
|
+ /// Use <see cref="ErrorQuery(string, string, string[])"/> instead; it automatically sizes the MessageBox based on the contents.
|
|
|
|
+ /// </remarks>
|
|
public static int ErrorQuery (int width, int height, string title, string message, params string [] buttons)
|
|
public static int ErrorQuery (int width, int height, string title, string message, params string [] buttons)
|
|
{
|
|
{
|
|
return QueryFull (true, width, height, title, message, buttons);
|
|
return QueryFull (true, width, height, title, message, buttons);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// Presents an error <see cref="MessageBox"/> with the specified title and message and a list of buttons to show to the user.
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <returns>The index of the selected button, or -1 if the user pressed ESC to close the dialog.</returns>
|
|
|
|
+ /// <param name="title">Title for the query.</param>
|
|
|
|
+ /// <param name="message">Message to display, might contain multiple lines.</param>
|
|
|
|
+ /// <param name="buttons">Array of buttons to add.</param>
|
|
|
|
+ /// <remarks>
|
|
|
|
+ /// 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.
|
|
|
|
+ /// </remarks>
|
|
|
|
+ public static int ErrorQuery (string title, string message, params string [] buttons)
|
|
|
|
+ {
|
|
|
|
+ return QueryFull (true, 0, 0, title, message, buttons);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
static int QueryFull (bool useErrorColors, int width, int height, string title, string message, params string [] buttons)
|
|
static int QueryFull (bool useErrorColors, int width, int height, string title, string message, params string [] buttons)
|
|
{
|
|
{
|
|
|
|
+ const int defaultWidth = 30;
|
|
int textWidth = Label.MaxWidth (message, width);
|
|
int textWidth = Label.MaxWidth (message, width);
|
|
- int clicked = -1, count = 0;
|
|
|
|
-
|
|
|
|
- var d = new Dialog (title, Math.Max(width, textWidth) + 4, height);
|
|
|
|
- if (useErrorColors)
|
|
|
|
- d.ColorScheme = Colors.Error;
|
|
|
|
|
|
+ int textHeight = message.ToCharArray ().Count (c => c == '\n') + 1;
|
|
|
|
+ int msgboxHeight = Math.Max (1, textHeight) + 4; // textHeight + (top + top padding + buttons + bottom)
|
|
|
|
|
|
|
|
+ // Create button array for Dialog
|
|
|
|
+ int count = 0;
|
|
|
|
+ List<Button> buttonList = new List<Button> ();
|
|
foreach (var s in buttons) {
|
|
foreach (var s in buttons) {
|
|
- int n = count++;
|
|
|
|
var b = new Button (s);
|
|
var b = new Button (s);
|
|
- b.Clicked += delegate {
|
|
|
|
- clicked = n;
|
|
|
|
- d.Running = false;
|
|
|
|
- };
|
|
|
|
- d.AddButton (b);
|
|
|
|
|
|
+ if (count == 0) {
|
|
|
|
+ b.IsDefault = true;
|
|
|
|
+ }
|
|
|
|
+ buttonList.Add (b);
|
|
|
|
+ count++;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Create Dialog (retain backwards compat by supporting specifying height/width)
|
|
|
|
+ Dialog d;
|
|
|
|
+ if (width == 0 & height == 0) {
|
|
|
|
+ d = new Dialog (title, buttonList.ToArray ());
|
|
|
|
+ d.Height = msgboxHeight;
|
|
|
|
+ } else {
|
|
|
|
+ d = new Dialog (title, Math.Max (width, textWidth) + 4, height, buttonList.ToArray ());
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if (useErrorColors) {
|
|
|
|
+ d.ColorScheme = Colors.Error;
|
|
|
|
+ }
|
|
|
|
+
|
|
if (message != null) {
|
|
if (message != null) {
|
|
- var l = new Label (textWidth > width ? 0 : (width - 4 - textWidth) / 2, 0, message);
|
|
|
|
|
|
+ var l = new Label (textWidth > width ? 0 : (width - 4 - textWidth) / 2, 1, message);
|
|
|
|
+ //l.ColorScheme = Colors.Menu;
|
|
|
|
+ if (true) { //width == 0 & height == 0) {
|
|
|
|
+ l.LayoutStyle = LayoutStyle.Computed;
|
|
|
|
+ l.TextAlignment = TextAlignment.Centered;
|
|
|
|
+ l.X = Pos.Center ();
|
|
|
|
+ l.Y = Pos.Center ();
|
|
|
|
+ l.Width = Dim.Fill (2);
|
|
|
|
+ l.Height = Dim.Fill (2);
|
|
|
|
+ }
|
|
d.Add (l);
|
|
d.Add (l);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // Dynamically size Width
|
|
|
|
+ int msgboxWidth = Math.Max (defaultWidth, Math.Max (title.Length + 8, Math.Max (textWidth + 4, d.GetButtonsWidth ()) + 8)); // textWidth + (left + padding + padding + right)
|
|
|
|
+ d.Width = msgboxWidth;
|
|
|
|
+
|
|
|
|
+ // Setup actions
|
|
|
|
+ int clicked = -1;
|
|
|
|
+ for (int n = 0; n < buttonList.Count; n++) {
|
|
|
|
+ int buttonId = n;
|
|
|
|
+ buttonList [n].Clicked += () => {
|
|
|
|
+ clicked = buttonId;
|
|
|
|
+ Application.RequestStop ();
|
|
|
|
+ };
|
|
|
|
+ }
|
|
|
|
+
|
|
Application.Run (d);
|
|
Application.Run (d);
|
|
return clicked;
|
|
return clicked;
|
|
}
|
|
}
|