Skip to content

Commit

Permalink
Fixes gui-cs#1499. Allowing border settings for the MessageBox. (gui-…
Browse files Browse the repository at this point in the history
  • Loading branch information
BDisp committed Nov 12, 2021
1 parent 1c5a4b0 commit c6580fe
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 17 deletions.
98 changes: 89 additions & 9 deletions Terminal.Gui/Windows/MessageBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static class MessageBox {
/// </remarks>
public static int Query (int width, int height, ustring title, ustring message, params ustring [] buttons)
{
return QueryFull (false, width, height, title, message, 0, buttons);
return QueryFull (false, width, height, title, message, 0, null, buttons);
}

/// <summary>
Expand All @@ -55,7 +55,7 @@ public static int Query (int width, int height, ustring title, ustring message,
/// </remarks>
public static int Query (ustring title, ustring message, params ustring [] buttons)
{
return QueryFull (false, 0, 0, title, message, 0, buttons);
return QueryFull (false, 0, 0, title, message, 0, null, buttons);
}

/// <summary>
Expand All @@ -72,7 +72,7 @@ public static int Query (ustring title, ustring message, params ustring [] butto
/// </remarks>
public static int ErrorQuery (int width, int height, ustring title, ustring message, params ustring [] buttons)
{
return QueryFull (true, width, height, title, message, 0, buttons);
return QueryFull (true, width, height, title, message, 0, null, buttons);
}

/// <summary>
Expand All @@ -88,7 +88,7 @@ public static int ErrorQuery (int width, int height, ustring title, ustring mess
/// </remarks>
public static int ErrorQuery (ustring title, ustring message, params ustring [] buttons)
{
return QueryFull (true, 0, 0, title, message, 0, buttons);
return QueryFull (true, 0, 0, title, message, 0, null, buttons);
}

/// <summary>
Expand All @@ -106,7 +106,7 @@ public static int ErrorQuery (ustring title, ustring message, params ustring []
/// </remarks>
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, buttons);
return QueryFull (false, width, height, title, message, defaultButton, null, buttons);
}

/// <summary>
Expand All @@ -123,9 +123,47 @@ public static int Query (int width, int height, ustring title, ustring message,
/// </remarks>
public static int Query (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
{
return QueryFull (false, 0, 0, title, message, defaultButton, buttons);
return QueryFull (false, 0, 0, title, message, defaultButton, null, buttons);
}

/// <summary>
/// Presents a normal <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="width">Width for the window.</param>
/// <param name="height">Height for the window.</param>
/// <param name="title">Title for the query.</param>
/// <param name="message">Message to display, might contain multiple lines.</param>
/// <param name="defaultButton">Index of the default button.</param>
/// <param name="border">The border settings.</param>
/// <param name="buttons">Array of buttons to add.</param>
/// <remarks>
/// Use <see cref="Query(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
/// </remarks>
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);
}

/// <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="defaultButton">Index of the default button.</param>
/// <param name="border">The border settings.</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 (ustring title, ustring message, int defaultButton = 0, Border border = null, params ustring [] buttons)
{
return QueryFull (false, 0, 0, title, message, defaultButton, border, 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>
Expand All @@ -141,7 +179,7 @@ public static int Query (ustring title, ustring message, int defaultButton = 0,
/// </remarks>
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, buttons);
return QueryFull (true, width, height, title, message, defaultButton, null, buttons);
}

/// <summary>
Expand All @@ -158,10 +196,48 @@ public static int ErrorQuery (int width, int height, ustring title, ustring mess
/// </remarks>
public static int ErrorQuery (ustring title, ustring message, int defaultButton = 0, params ustring [] buttons)
{
return QueryFull (true, 0, 0, title, message, defaultButton, buttons);
return QueryFull (true, 0, 0, title, message, defaultButton, null, buttons);
}

static int QueryFull (bool useErrorColors, int width, int height, ustring title, ustring message, int defaultButton = 0, params ustring [] 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="width">Width for the window.</param>
/// <param name="height">Height for the window.</param>
/// <param name="title">Title for the query.</param>
/// <param name="message">Message to display, might contain multiple lines.</param>
/// <param name="defaultButton">Index of the default button.</param>
/// <param name="border">The border settings.</param>
/// <param name="buttons">Array of buttons to add.</param>
/// <remarks>
/// Use <see cref="ErrorQuery(ustring, ustring, ustring[])"/> instead; it automatically sizes the MessageBox based on the contents.
/// </remarks>
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);
}

/// <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="defaultButton">Index of the default button.</param>
/// <param name="border">The border settings.</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 (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);
Expand Down Expand Up @@ -192,6 +268,10 @@ static int QueryFull (bool useErrorColors, int width, int height, ustring title,
d = new Dialog (title, Math.Max (width, textWidth) + 4, height, buttonList.ToArray ());
}

if (border != null) {
d.Border = border;
}

if (useErrorColors) {
d.ColorScheme = Colors.Error;
}
Expand Down
29 changes: 21 additions & 8 deletions UICatalog/Scenarios/MessageBoxes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class MessageBoxes : Scenario {
public override void Setup ()
{
var frame = new FrameView ("MessageBox Options") {
X = Pos.Center(),
X = Pos.Center (),
Y = 1,
Width = Dim.Percent(75),
Width = Dim.Percent (75),
Height = 10
};
Win.Add (frame);
Expand Down Expand Up @@ -71,7 +71,7 @@ public override void Setup ()
var titleEdit = new TextField ("Title") {
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
Width = Dim.Fill(),
Width = Dim.Fill (),
Height = 1
};
frame.Add (titleEdit);
Expand Down Expand Up @@ -134,16 +134,29 @@ public override void Setup ()
TextAlignment = Terminal.Gui.TextAlignment.Right,
};
frame.Add (label);
var styleRadioGroup = new RadioGroup (new ustring [] { "_Query", "_Error" } ) {
var styleRadioGroup = new RadioGroup (new ustring [] { "_Query", "_Error" }) {
X = Pos.Right (label) + 1,
Y = Pos.Top (label),
};
frame.Add (styleRadioGroup);

var border = new Border () {
Effect3D = true,
BorderStyle = BorderStyle.Single
};
var ckbEffect3D = new CheckBox ("Effect3D", true) {
X = Pos.Right (label) + 1,
Y = Pos.Top (label) + 2
};
ckbEffect3D.Toggled += (e) => {
border.Effect3D = !e;
};
frame.Add (ckbEffect3D);

void Top_Loaded ()
{
frame.Height = Dim.Height (widthEdit) + Dim.Height (heightEdit) + Dim.Height (titleEdit) + Dim.Height (messageEdit)
+ Dim.Height (numButtonsEdit) + Dim.Height(defaultButtonEdit) + Dim.Height (styleRadioGroup) + 2;
+ Dim.Height (numButtonsEdit) + Dim.Height (defaultButtonEdit) + Dim.Height (styleRadioGroup) + 2 + Dim.Height (ckbEffect3D);
Top.Loaded -= Top_Loaded;
}
Top.Loaded += Top_Loaded;
Expand All @@ -167,7 +180,7 @@ void Top_Loaded ()
//var btnText = new [] { "_Zero", "_One", "T_wo", "_Three", "_Four", "Fi_ve", "Si_x", "_Seven", "_Eight", "_Nine" };

var showMessageBoxButton = new Button ("Show MessageBox") {
X = Pos.Center(),
X = Pos.Center (),
Y = Pos.Bottom (frame) + 2,
IsDefault = true,
};
Expand All @@ -184,9 +197,9 @@ void Top_Loaded ()
btns.Add (NumberToWords.Convert (i));
}
if (styleRadioGroup.SelectedItem == 0) {
buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, btns.ToArray ())}";
buttonPressedLabel.Text = $"{MessageBox.Query (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, border, btns.ToArray ())}";
} else {
buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, btns.ToArray ())}";
buttonPressedLabel.Text = $"{MessageBox.ErrorQuery (width, height, titleEdit.Text.ToString (), messageEdit.Text.ToString (), defaultButton, border, btns.ToArray ())}";
}
} catch (FormatException) {
buttonPressedLabel.Text = "Invalid Options";
Expand Down

0 comments on commit c6580fe

Please sign in to comment.