BizHawk/BizHawk.Client.EmuHawk/CustomControls/MsgBox.cs

201 lines
5.7 KiB
C#
Raw Normal View History

using System;
using System.Drawing;
using System.Windows.Forms;
2020-01-12 20:58:02 +00:00
using BizHawk.Common;
// http://www.codeproject.com/Articles/154680/A-customizable-NET-WinForms-Message-Box
namespace BizHawk.Client.EmuHawk.CustomControls
{
/// <summary>
/// A customizable Dialog box with 3 buttons, custom icon, and checkbox.
/// </summary>
partial class MsgBox : Form
{
2019-12-17 16:48:20 +00:00
private readonly Icon _msgIcon;
private static readonly int FormYMargin = UIHelper.ScaleY(10);
private static readonly int FormXMargin = UIHelper.ScaleX(16);
private static readonly int ButtonSpace = UIHelper.ScaleX(5);
private static readonly int TextYMargin = UIHelper.ScaleY(30);
2019-12-17 16:48:20 +00:00
// The min required width of the button and checkbox row. Sum of button widths + checkbox width + margins.
private int _minButtonRowWidth;
/// <summary>
2019-12-17 16:48:20 +00:00
/// Create a new instance of the dialog box with a message and title and a standard windows MessageBox icon.
/// </summary>
/// <param name="message">Message text.</param>
/// <param name="title">Dialog Box title.</param>
2019-12-17 16:48:20 +00:00
/// <param name="boxIcon">Standard system MessageBox icon.</param>
public MsgBox(string message, string title, MessageBoxIcon boxIcon)
{
2019-12-17 16:48:20 +00:00
var icon = GetMessageBoxIcon(boxIcon);
InitializeComponent();
2019-12-17 16:48:20 +00:00
ControlBox = false; // Do not set in designer (causes problems with auto scaling)
messageLbl.Text = message;
Text = title;
_msgIcon = icon;
2019-12-17 16:48:20 +00:00
if (_msgIcon == null)
{
2019-12-17 16:48:20 +00:00
messageLbl.Location = new Point(FormXMargin, FormYMargin);
}
}
/// <summary>
2019-12-17 16:48:20 +00:00
/// Gets the button that was pressed.
/// </summary>
2019-12-17 16:48:20 +00:00
public DialogBoxResult DialogBoxResult { get; private set; }
2019-12-17 16:48:20 +00:00
public void SetMessageToAutoSize()
{
2019-12-17 16:48:20 +00:00
messageLbl.AutoSize = true;
messageLbl.MaximumSize = new Size(MaximumSize.Width - _msgIcon.Width - UIHelper.ScaleX(25), MaximumSize.Height);
}
/// <summary>
/// Create up to 3 buttons with given DialogResult values.
/// </summary>
/// <param name="names">Array of button names. Must of length 1-3.</param>
/// <param name="results">Array of DialogResult values. Must be same length as names.</param>
/// <param name="def">Default Button number. Must be 1-3.</param>
/// <exception cref="ArgumentException">length of <paramref name="names"/> is not in range <c>1..3</c></exception>
/// <exception cref="ArgumentNullException"><paramref name="names"/> is null</exception>
public void SetButtons(string[] names, DialogResult[] results, int def = 1)
{
if (names == null)
2019-12-17 16:48:20 +00:00
{
2017-04-10 12:36:42 +00:00
throw new ArgumentNullException(nameof(names), "Button Text is null");
2019-12-17 16:48:20 +00:00
}
int count = names.Length;
2020-01-12 20:58:02 +00:00
if (!1.RangeTo(3).Contains(count))
2019-12-17 16:48:20 +00:00
{
throw new ArgumentException("Invalid number of buttons. Must be between 1 and 3.");
2019-12-17 16:48:20 +00:00
}
//---- Set Button 1
2019-12-17 16:48:20 +00:00
_minButtonRowWidth += SetButtonParams(btn1, names[0], def == 1 ? 1 : 2, results[0]);
//---- Set Button 2
if (count > 1)
{
2019-12-17 16:48:20 +00:00
_minButtonRowWidth += SetButtonParams(btn2, names[1], def == 2 ? 1 : 3, results[1]) + ButtonSpace;
}
//---- Set Button 3
if (count > 2)
{
2019-12-17 16:48:20 +00:00
_minButtonRowWidth += SetButtonParams(btn3, names[2], def == 3 ? 1 : 4, results[2]) + ButtonSpace;
}
}
/// <summary>
2019-12-17 16:48:20 +00:00
/// Paint the System Icon in the top left corner.
/// </summary>
2019-12-17 16:48:20 +00:00
protected override void OnPaint(PaintEventArgs e)
{
2019-12-17 16:48:20 +00:00
if (_msgIcon != null)
{
Graphics g = e.Graphics;
g.DrawIconUnstretched(_msgIcon, new Rectangle(FormXMargin, FormYMargin, _msgIcon.Width, _msgIcon.Height));
}
base.OnPaint(e);
}
2019-12-17 16:48:20 +00:00
// Get system icon for MessageBoxIcon.
private static Icon GetMessageBoxIcon(MessageBoxIcon icon)
{
2019-12-17 16:48:20 +00:00
switch (icon)
{
case MessageBoxIcon.Asterisk:
return SystemIcons.Asterisk;
case MessageBoxIcon.Error:
return SystemIcons.Error;
case MessageBoxIcon.Exclamation:
return SystemIcons.Exclamation;
case MessageBoxIcon.Question:
return SystemIcons.Question;
default:
return null;
}
}
2019-12-17 16:48:20 +00:00
// Sets button text and returns the width.
private static int SetButtonParams(Button btn, string text, int tab, DialogResult dr)
{
2019-12-17 16:48:20 +00:00
btn.Text = text;
btn.Visible = true;
btn.DialogResult = dr;
btn.TabIndex = tab;
return btn.Size.Width;
}
private void DialogBox_Load(object sender, EventArgs e)
{
if (!btn1.Visible)
2019-12-17 16:48:20 +00:00
{
SetButtons(new[] { "OK" }, new[] { DialogResult.OK });
}
2019-12-17 16:48:20 +00:00
_minButtonRowWidth += 2 * FormXMargin; //add margin to the ends
2019-12-17 16:48:20 +00:00
SetDialogSize();
}
2019-12-17 16:48:20 +00:00
// Auto fits the dialog box to fit the text and the buttons.
private void SetDialogSize()
{
2019-12-17 16:48:20 +00:00
int requiredWidth = messageLbl.Location.X + messageLbl.Size.Width + FormXMargin;
requiredWidth = requiredWidth > _minButtonRowWidth ? requiredWidth : _minButtonRowWidth;
2019-12-17 16:48:20 +00:00
int requiredHeight = messageLbl.Location.Y + messageLbl.Size.Height - btn2.Location.Y + ClientSize.Height + TextYMargin;
2019-12-17 16:48:20 +00:00
int minSetWidth = ClientSize.Width;
int minSetHeight = ClientSize.Height;
2019-12-17 16:48:20 +00:00
ClientSize = new Size
{
Width = requiredWidth > minSetWidth
? requiredWidth
: minSetWidth,
Height = requiredHeight > minSetHeight
? requiredHeight
: minSetHeight
};
}
2019-12-17 16:48:20 +00:00
private void ButtonClick(object sender, EventArgs e)
{
2019-12-17 16:48:20 +00:00
if (sender == btn1)
{
2019-12-17 16:48:20 +00:00
DialogBoxResult = DialogBoxResult.Button1;
}
2019-12-17 16:48:20 +00:00
else if (sender == btn2)
{
2019-12-17 16:48:20 +00:00
DialogBoxResult = DialogBoxResult.Button2;
}
2019-12-17 16:48:20 +00:00
else if (sender == btn3)
{
2019-12-17 16:48:20 +00:00
DialogBoxResult = DialogBoxResult.Button3;
}
2019-12-17 16:48:20 +00:00
if (((Button) sender).DialogResult == DialogResult.None)
{
2019-12-17 16:48:20 +00:00
Close();
}
}
}
public enum DialogBoxResult
{
Button1,
Button2,
Button3
}
}