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 { /// /// 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 . /// 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 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