JD2022-TU1/main/tools/framework/JD.MessageService/CustomDialog.cs

232 lines
No EOL
7.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using JD.Collections.Extensions;
using JD.Kernel.StaticHelpers;
namespace JD.MessageService
{
/// <summary>
/// This form represents a custom message box, with a variable number
/// of buttons and a information box. Do not use this class directly. Instead, use
/// the <see cref="MessageService"/>.
/// </summary>
internal partial class CustomDialog : Form
{
private const int Spacing = 10;
private const int SpacingBetweenButton = 4;
private const int ButtonLocationY = 8;
private const int SpacingBetweenInformationBoxAndPanel = 6;
private const int ScreenSpacing = 50;
private readonly MessageDialog m_dialog;
private MessageDialog.Button m_result;
public CustomDialog(MessageDialog dialog)
{
Throw.IfNull(dialog, "dialog");
m_dialog = dialog;
InitializeComponent();
Tag = "RemoveLock";
SuspendLayout();
Icon = null;
Text = m_dialog.Caption;
m_informationBox.Text = m_dialog.MainMessage + Environment.NewLine + Environment.NewLine + m_dialog.Content;
m_informationBox.Icon = GetSystemIconType(m_dialog.MainIcon);
using (Graphics g = CreateGraphics())
{
var buttons = CreateButtons(g);
// start the client size to the minimum size of the information box
int[] positions = new int[buttons.Count];
int pos = Spacing;
for (int i = 0; i < buttons.Count; i++)
{
positions[i] = pos;
pos += buttons[i].Width + SpacingBetweenButton;
}
pos = pos - SpacingBetweenButton + Spacing; // remove space after last button + add spacing
Size clientSize = GetClientSize(g, pos);
ClientSize = clientSize;
m_buttonPanel.Width = clientSize.Width;
int start = (m_buttonPanel.Width - pos + Spacing) / 2;
for (int i = 0; i < buttons.Count; i++)
{
buttons[i].Location = new Point(start + positions[i], ButtonLocationY);
}
m_buttonPanel.Controls.AddRange(buttons.ToArray());
SetupCheckbox();
}
ResumeLayout(false);
}
private Size GetClientSize(Graphics g, int pos)
{
Size clientSize = m_informationBox.GetMiniumSize(g);
if (pos > clientSize.Width)
{
clientSize.Width = pos;
}
clientSize.Width += m_informationBox.Location.X * 2;
clientSize.Height += m_informationBox.Location.Y + SpacingBetweenInformationBoxAndPanel + m_buttonPanel.Height;
// Check that the message box will be smaller than the primary screen
Rectangle screen = Screen.PrimaryScreen.WorkingArea;
clientSize.Width = clientSize.Width > screen.Width ? screen.Width : clientSize.Width;
clientSize.Height = clientSize.Height > screen.Height ? screen.Height - ScreenSpacing : clientSize.Height;
return clientSize;
}
private void SetupCheckbox()
{
if (!string.IsNullOrEmpty(m_dialog.CheckBoxText))
{
CheckBox checkbox = new CheckBox
{
AutoSize = true,
Text = m_dialog.CheckBoxText,
Checked = m_dialog.IsChecked
};
checkbox.CheckedChanged += checkbox_CheckedChanged;
m_checkBoxPanel.Height = checkbox.Height;
Height += m_checkBoxPanel.Height;
m_checkBoxPanel.Controls.Add(checkbox);
checkbox.BringToFront();
checkbox.Location = new Point(SpacingBetweenInformationBoxAndPanel, SpacingBetweenInformationBoxAndPanel);
}
}
private IList<MessageDialog.Button> SortButtons()
{
return m_dialog.Buttons.AsSortedStable((a, b) => GetWeight(a).CompareTo(GetWeight(b)));
}
private static int GetWeight(MessageDialog.Button button)
{
// Cancel is always last
if (button.Id == (int)MessageDialogCommonButton.Cancel)
{
return 10;
}
// Common buttons are always last
if (button.IsCommonButton)
{
return 5;
}
return 0;
}
private List<Button> CreateButtons(Graphics g)
{
List<Button> buttons = new List<Button>();
foreach (var dialogButton in SortButtons())
{
var button = CreateButton(g, dialogButton);
buttons.Add(button);
if (dialogButton.IsDefault)
{
AcceptButton = button;
}
if (dialogButton.Id == (int)MessageDialogCommonButton.Cancel)
{
m_result = dialogButton;
CancelButton = button;
}
}
if (AcceptButton == null)
{
AcceptButton = buttons.First();
}
return buttons;
}
private Button CreateButton(Graphics g, MessageDialog.Button dialogButton)
{
Button newButton = new Button
{
Anchor = AnchorStyles.Right,
Enabled = dialogButton.Enabled,
FlatStyle = FlatStyle.System,
Tag = dialogButton,
Text = dialogButton.Text
};
newButton.Click += ButtonClick;
SizeF buttonSize = g.MeasureString(dialogButton.Text, newButton.Font);
newButton.Width = Math.Max(newButton.Width, ((int)Math.Ceiling(buttonSize.Width / 8.0) + 1) * 8);
return newButton;
}
#region Properties
public MessageDialog.Button Result
{
get
{
return m_result;
}
}
#endregion
#region Methods
private static SystemIconType GetSystemIconType(MessageDialogIcon icon)
{
switch (icon)
{
case MessageDialogIcon.Error:
return SystemIconType.Error;
case MessageDialogIcon.Information:
return SystemIconType.Information;
case MessageDialogIcon.Warning:
return SystemIconType.Warning;
}
return SystemIconType.Information;
}
#endregion
#region Event Handlers
private void ButtonClick(object sender, EventArgs e)
{
m_result = (MessageDialog.Button)((Control)sender).Tag;
Close();
}
void checkbox_CheckedChanged(object sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
m_dialog.IsChecked = checkBox.Checked;
}
#endregion
}
}