From d3cb2a4fb3391dff5c7f7cd9f962fe9176300464 Mon Sep 17 00:00:00 2001 From: adelikat Date: Tue, 17 Dec 2019 10:48:20 -0600 Subject: [PATCH] cleanup MsgBox --- .../CustomControls/MsgBox.cs | 397 ++++++------------ .../CustomControls/MsgBox.designer.cs | 6 +- 2 files changed, 129 insertions(+), 274 deletions(-) diff --git a/BizHawk.Client.EmuHawk/CustomControls/MsgBox.cs b/BizHawk.Client.EmuHawk/CustomControls/MsgBox.cs index 7dabe03020..1ac3b5346e 100644 --- a/BizHawk.Client.EmuHawk/CustomControls/MsgBox.cs +++ b/BizHawk.Client.EmuHawk/CustomControls/MsgBox.cs @@ -1,9 +1,5 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; using System.Drawing; -using System.Text; using System.Windows.Forms; // http://www.codeproject.com/Articles/154680/A-customizable-NET-WinForms-Message-Box @@ -14,60 +10,111 @@ namespace BizHawk.Client.EmuHawk.CustomControls /// partial class MsgBox : Form { + 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); + + // The min required width of the button and checkbox row. Sum of button widths + checkbox width + margins. + private int _minButtonRowWidth; + /// - /// Create a new instance of the dialog box with a message and title. + /// Create a new instance of the dialog box with a message and title and a standard windows MessageBox icon. /// /// Message text. /// Dialog Box title. - public MsgBox(string message, string title) - : this(message, title, MessageBoxIcon.None) - { - - } - - /// - /// Create a new instance of the dialog box with a message and title and a standard windows messagebox icon. - /// - /// Message text. - /// Dialog Box title. - /// Standard system messagebox icon. - public MsgBox(string message, string title, MessageBoxIcon icon) - : this(message, title, getMessageBoxIcon(icon)) - { - - } - - /// - /// Create a new instance of the dialog box with a message and title and a custom windows icon. - /// - /// Message text. - /// Dialog Box title. - /// Custom icon. - public MsgBox(string message, string title, Icon icon) + /// Standard system MessageBox icon. + public MsgBox(string message, string title, MessageBoxIcon boxIcon) { + var icon = GetMessageBoxIcon(boxIcon); InitializeComponent(); - this.ControlBox = false; // Do not set in designer (causes problems with auto scaling) - this.messageLbl.Text = message; - this.Text = title; - this.m_sysIcon = icon; + ControlBox = false; // Do not set in designer (causes problems with auto scaling) + messageLbl.Text = message; + Text = title; + _msgIcon = icon; - if (this.m_sysIcon == null) - this.messageLbl.Location = new Point(FORM_X_MARGIN, FORM_Y_MARGIN); + if (_msgIcon == null) + { + messageLbl.Location = new Point(FormXMargin, FormYMargin); + } } + /// + /// Gets the button that was pressed. + /// + public DialogBoxResult DialogBoxResult { get; private set; } + public void SetMessageToAutoSize() { - this.messageLbl.AutoSize = true; - this.messageLbl.MaximumSize = new Size(this.MaximumSize.Width - this.m_sysIcon.Width - UIHelper.ScaleX(25), this.MaximumSize.Height); + messageLbl.AutoSize = true; + messageLbl.MaximumSize = new Size(MaximumSize.Width - _msgIcon.Width - UIHelper.ScaleX(25), MaximumSize.Height); } /// - /// Get system icon for MessageBoxIcon. + /// Create up to 3 buttons with given DialogResult values. /// - /// The MessageBoxIcon value. - /// SystemIcon type Icon. - static Icon getMessageBoxIcon(MessageBoxIcon icon) + /// Array of button names. Must of length 1-3. + /// Array of DialogResult values. Must be same length as names. + public void SetButtons(string[] names, DialogResult[] results) + { + SetButtons(names, results, 1); + } + + /// + /// Create up to 3 buttons with given DialogResult values. + /// + /// Array of button names. Must of length 1-3. + /// Array of DialogResult values. Must be same length as names. + /// Default Button number. Must be 1-3. + public void SetButtons(string[] names, DialogResult[] results, int def) + { + if (names == null) + { + throw new ArgumentNullException(nameof(names), "Button Text is null"); + } + + int count = names.Length; + + if (count < 1 || count > 3) + { + throw new ArgumentException("Invalid number of buttons. Must be between 1 and 3."); + } + + //---- Set Button 1 + _minButtonRowWidth += SetButtonParams(btn1, names[0], def == 1 ? 1 : 2, results[0]); + + //---- Set Button 2 + if (count > 1) + { + _minButtonRowWidth += SetButtonParams(btn2, names[1], def == 2 ? 1 : 3, results[1]) + ButtonSpace; + } + + //---- Set Button 3 + if (count > 2) + { + _minButtonRowWidth += SetButtonParams(btn3, names[2], def == 3 ? 1 : 4, results[2]) + ButtonSpace; + } + + } + + /// + /// Paint the System Icon in the top left corner. + /// + protected override void OnPaint(PaintEventArgs e) + { + if (_msgIcon != null) + { + Graphics g = e.Graphics; + g.DrawIconUnstretched(_msgIcon, new Rectangle(FormXMargin, FormYMargin, _msgIcon.Width, _msgIcon.Height)); + } + + base.OnPaint(e); + } + + // Get system icon for MessageBoxIcon. + private static Icon GetMessageBoxIcon(MessageBoxIcon icon) { switch (icon) { @@ -84,97 +131,8 @@ namespace BizHawk.Client.EmuHawk.CustomControls } } - #region Setup API - - /// - /// Min set width. - /// - int m_minWidth; - /// - /// Min set height. - /// - int m_minHeight; - - /// - /// Sets the min size of the dialog box. If the text or button row needs more size then the dialog box will size to fit the text. - /// - /// Min width value. - /// Min height value. - public void SetMinSize(int width, int height) - { - m_minWidth = width; - m_minHeight = height; - } - - /// - /// Create up to 3 buttons with no DialogResult values. - /// - /// Array of button names. Must of length 1-3. - public void SetButtons(params string[] names) - { - DialogResult[] drs = new DialogResult[names.Length]; - for (int i = 0; i < names.Length; i++) - drs[i] = DialogResult.None; - this.SetButtons(names, drs); - } - - /// - /// Create up to 3 buttons with given DialogResult values. - /// - /// Array of button names. Must of length 1-3. - /// Array of DialogResult values. Must be same length as names. - public void SetButtons(string[] names, DialogResult[] results) - { - this.SetButtons(names, results, 1); - } - - /// - /// Create up to 3 buttons with given DialogResult values. - /// - /// Array of button names. Must of length 1-3. - /// Array of DialogResult values. Must be same length as names. - /// Default Button number. Must be 1-3. - public void SetButtons(string[] names, DialogResult[] results, int def) - { - if (names == null) - throw new ArgumentNullException(nameof(names), "Button Text is null"); - - int count = names.Length; - - if (count < 1 || count > 3) - throw new ArgumentException("Invalid number of buttons. Must be between 1 and 3."); - - //---- Set Button 1 - m_minButtonRowWidth += setButtonParams(btn1, names[0], def == 1 ? 1 : 2, results[0]); - - //---- Set Button 2 - if (count > 1) - { - m_minButtonRowWidth += setButtonParams(btn2, names[1], def == 2 ? 1 : 3, results[1]) + BUTTON_SPACE; - } - - //---- Set Button 3 - if (count > 2) - { - m_minButtonRowWidth += setButtonParams(btn3, names[2], def == 3 ? 1 : 4, results[2]) + BUTTON_SPACE; - } - - } - - /// - /// The min required width of the button and checkbox row. Sum of button widths + checkbox width + margins. - /// - int m_minButtonRowWidth; - - /// - /// Sets button text and returns the width. - /// - /// Button object. - /// Text of the button. - /// TabIndex of the button. - /// DialogResult of the button. - /// Width of the button. - static int setButtonParams(Button btn, string text, int tab, DialogResult dr) + // Sets button text and returns the width. + private static int SetButtonParams(Button btn, string text, int tab, DialogResult dr) { btn.Text = text; btn.Visible = true; @@ -183,163 +141,60 @@ namespace BizHawk.Client.EmuHawk.CustomControls return btn.Size.Width; } - /// - /// Enables the checkbox. By default the checkbox is unchecked. - /// - /// Text of the checkbox. - public void SetCheckbox(string text) - { - this.SetCheckbox(text, false); - } - - /// - /// Enables the checkbox and the default checked state. - /// - /// Text of the checkbox. - /// Default checked state of the box. - public void SetCheckbox(string text, bool chcked) - { - this.chkBx.Visible = true; - this.chkBx.Text = text; - this.chkBx.Checked = chcked; - this.m_minButtonRowWidth += this.chkBx.Size.Width + CHECKBOX_SPACE; - } - - #endregion - - #region Sizes and Locations private void DialogBox_Load(object sender, EventArgs e) { if (!btn1.Visible) - this.SetButtons(new string[] { "OK" }, new DialogResult[] { DialogResult.OK }); - - m_minButtonRowWidth += 2 * FORM_X_MARGIN; //add margin to the ends - - this.setDialogSize(); - - //this.setButtonRowLocations(); - - } - - private static readonly int FORM_Y_MARGIN = UIHelper.ScaleY(10); - private static readonly int FORM_X_MARGIN = UIHelper.ScaleX(16); - private static readonly int BUTTON_SPACE = UIHelper.ScaleX(5); - private static readonly int CHECKBOX_SPACE = UIHelper.ScaleX(15); - private static readonly int TEXT_Y_MARGIN = UIHelper.ScaleY(30); - - /// - /// Auto fits the dialog box to fit the text and the buttons. - /// - void setDialogSize() - { - int requiredWidth = this.messageLbl.Location.X + this.messageLbl.Size.Width + FORM_X_MARGIN; - requiredWidth = requiredWidth > m_minButtonRowWidth ? requiredWidth : m_minButtonRowWidth; - - int requiredHeight = this.messageLbl.Location.Y + this.messageLbl.Size.Height - this.btn2.Location.Y + this.ClientSize.Height + TEXT_Y_MARGIN; - - int minSetWidth = this.ClientSize.Width > this.m_minWidth ? this.ClientSize.Width : this.m_minWidth; - int minSetHeight = this.ClientSize.Height > this.m_minHeight ? this.ClientSize.Height : this.m_minHeight; - - Size s = new Size(); - s.Width = requiredWidth > minSetWidth ? requiredWidth : minSetWidth; - s.Height = requiredHeight > minSetHeight ? requiredHeight : minSetHeight; - this.ClientSize = s; - } - - /// - /// Sets the buttons and checkboxe location. - /// - void setButtonRowLocations() - { - int formWidth = this.ClientRectangle.Width; - - int x = formWidth - FORM_X_MARGIN; - int y = btn1.Location.Y; - - if (btn3.Visible) { - x -= btn3.Size.Width; - btn3.Location = new Point(x, y); - x -= BUTTON_SPACE; + SetButtons(new[] { "OK" }, new[] { DialogResult.OK }); } - if (btn2.Visible) - { - x -= btn2.Size.Width; - btn2.Location = new Point(x, y); - x -= BUTTON_SPACE; - } - - x -= btn1.Size.Width; - btn1.Location = new Point(x, y); - - if (this.chkBx.Visible) - this.chkBx.Location = new Point(FORM_X_MARGIN, this.chkBx.Location.Y); + _minButtonRowWidth += 2 * FormXMargin; //add margin to the ends + SetDialogSize(); } - #endregion - - #region Icon Pain - /// - /// The icon to paint. - /// - Icon m_sysIcon; - - /// - /// Paint the System Icon in the top left corner. - /// - protected override void OnPaint(PaintEventArgs e) + // Auto fits the dialog box to fit the text and the buttons. + private void SetDialogSize() { - if (m_sysIcon != null) + int requiredWidth = messageLbl.Location.X + messageLbl.Size.Width + FormXMargin; + requiredWidth = requiredWidth > _minButtonRowWidth ? requiredWidth : _minButtonRowWidth; + + int requiredHeight = messageLbl.Location.Y + messageLbl.Size.Height - btn2.Location.Y + ClientSize.Height + TextYMargin; + + int minSetWidth = ClientSize.Width; + int minSetHeight = ClientSize.Height; + + ClientSize = new Size { - Graphics g = e.Graphics; - g.DrawIconUnstretched(m_sysIcon, new Rectangle(FORM_X_MARGIN, FORM_Y_MARGIN, m_sysIcon.Width, m_sysIcon.Height)); - } - - base.OnPaint(e); - } - #endregion - - #region Result API - - /// - /// If visible checkbox was checked. - /// - public bool CheckboxChecked - { - get - { - return this.chkBx.Checked; - } + Width = requiredWidth > minSetWidth + ? requiredWidth + : minSetWidth, + Height = requiredHeight > minSetHeight + ? requiredHeight + : minSetHeight + }; } - DialogBoxResult m_result; - /// - /// Gets the button that was pressed. - /// - public DialogBoxResult DialogBoxResult - { - get - { - return m_result; - } - } - - private void btn_Click(object sender, EventArgs e) + private void ButtonClick(object sender, EventArgs e) { if (sender == btn1) - this.m_result = DialogBoxResult.Button1; + { + DialogBoxResult = DialogBoxResult.Button1; + } else if (sender == btn2) - this.m_result = DialogBoxResult.Button2; + { + DialogBoxResult = DialogBoxResult.Button2; + } else if (sender == btn3) - this.m_result = DialogBoxResult.Button3; + { + DialogBoxResult = DialogBoxResult.Button3; + } - if (((Button)sender).DialogResult == DialogResult.None) - this.Close(); + if (((Button) sender).DialogResult == DialogResult.None) + { + Close(); + } } - - #endregion } public enum DialogBoxResult diff --git a/BizHawk.Client.EmuHawk/CustomControls/MsgBox.designer.cs b/BizHawk.Client.EmuHawk/CustomControls/MsgBox.designer.cs index 4e467db7c1..7f7a9a4c4e 100644 --- a/BizHawk.Client.EmuHawk/CustomControls/MsgBox.designer.cs +++ b/BizHawk.Client.EmuHawk/CustomControls/MsgBox.designer.cs @@ -60,7 +60,7 @@ this.btn1.Text = "Button1"; this.btn1.UseVisualStyleBackColor = true; this.btn1.Visible = false; - this.btn1.Click += new System.EventHandler(this.btn_Click); + this.btn1.Click += new System.EventHandler(this.ButtonClick); // // btn2 // @@ -73,7 +73,7 @@ this.btn2.Text = "Button2"; this.btn2.UseVisualStyleBackColor = true; this.btn2.Visible = false; - this.btn2.Click += new System.EventHandler(this.btn_Click); + this.btn2.Click += new System.EventHandler(this.ButtonClick); // // messageLbl // @@ -96,7 +96,7 @@ this.btn3.Text = "Button3"; this.btn3.UseVisualStyleBackColor = true; this.btn3.Visible = false; - this.btn3.Click += new System.EventHandler(this.btn_Click); + this.btn3.Click += new System.EventHandler(this.ButtonClick); // // DialogBox //