139 lines
4.7 KiB
C#
139 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Immutable;
|
|
using System.Collections.ObjectModel;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using Dashboard.Resources;
|
|
using Dashboard.Windowing;
|
|
|
|
namespace Dashboard.Controls
|
|
{
|
|
public enum MessageBoxIcon
|
|
{
|
|
Info,
|
|
Question,
|
|
Warning,
|
|
Error,
|
|
Custom,
|
|
}
|
|
|
|
public enum MessageBoxButtons
|
|
{
|
|
AbortRetryIgnore,
|
|
CancelRetryContinue,
|
|
Ok,
|
|
OkCancel,
|
|
RetryCancel,
|
|
YesNo,
|
|
YesNoCancel,
|
|
Custom,
|
|
}
|
|
|
|
/// <summary>
|
|
/// A simple message box dialog.
|
|
/// </summary>
|
|
public class MessageBox : Form
|
|
{
|
|
private Image? _icon;
|
|
private Label _label = new Label();
|
|
private readonly List<Label> _buttons = new List<Label>();
|
|
|
|
public MessageBoxIcon Icon { get; set; }
|
|
public Image? CustomImage { get; set; }
|
|
public string? Message { get; set; }
|
|
public MessageBoxButtons Buttons { get; set; }
|
|
public ObservableCollection<string> CustomButtons { get; } = new ObservableCollection<string>();
|
|
public int Result { get; private set; }
|
|
|
|
public MessageBox(IWindow window) : base(window)
|
|
{
|
|
SetParent(this, _label);
|
|
}
|
|
|
|
public static readonly Image s_questionIcon;
|
|
public static readonly Image s_infoIcon;
|
|
public static readonly Image s_warningIcon;
|
|
public static readonly Image s_errorIcon;
|
|
|
|
private static readonly ImmutableList<string> s_abortRetryContinue = ["Abort", "Retry", "Continue"];
|
|
private static readonly ImmutableList<string> s_cancelRetryContinue = ["Cancel", "Retry", "Continue"];
|
|
private static readonly ImmutableList<string> s_ok = ["OK"];
|
|
private static readonly ImmutableList<string> s_okCancel = ["OK", "Cancel"];
|
|
private static readonly ImmutableList<string> s_retryCancel = ["Retry", "Cancel"];
|
|
private static readonly ImmutableList<string> s_yesNo = ["Yes", "No"];
|
|
private static readonly ImmutableList<string> s_yesNoCancel = ["Yes", "No", "Cancel"];
|
|
|
|
static MessageBox()
|
|
{
|
|
Assembly asm = typeof(MessageBox).Assembly;
|
|
using (Stream str = asm.GetManifestResourceStream("Dashboard.Resources.question.png")!)
|
|
s_questionIcon = Image.Load(str);
|
|
using (Stream str = asm.GetManifestResourceStream("Dashboard.Resources.info.png")!)
|
|
s_infoIcon = Image.Load(str);
|
|
using (Stream str = asm.GetManifestResourceStream("Dashboard.Resources.warning.png")!)
|
|
s_warningIcon = Image.Load(str);
|
|
using (Stream str = asm.GetManifestResourceStream("Dashboard.Resources.error.png")!)
|
|
s_errorIcon = Image.Load(str);
|
|
}
|
|
|
|
public override void OnPaint()
|
|
{
|
|
base.OnPaint();
|
|
}
|
|
|
|
public static MessageBox Create(IWindow window, string message, string title, MessageBoxIcon icon, MessageBoxButtons buttons)
|
|
{
|
|
return new MessageBox(window)
|
|
{
|
|
Message = message,
|
|
Title = title,
|
|
Icon = icon,
|
|
Buttons = buttons,
|
|
};
|
|
}
|
|
|
|
// public static MessageBox Create(IWindow window, string message, string title, Image icon, IEnumerable<string> buttons)
|
|
// {
|
|
// throw new NotImplementedException();
|
|
// }
|
|
|
|
private static string GetDefaultTitle(MessageBoxIcon icon)
|
|
{
|
|
return icon switch
|
|
{
|
|
MessageBoxIcon.Error => "Error",
|
|
MessageBoxIcon.Info => "Info",
|
|
MessageBoxIcon.Question => "Question",
|
|
MessageBoxIcon.Warning => "Warning",
|
|
_ => "Message Box",
|
|
};
|
|
}
|
|
|
|
private static Image GetDefaultIcon(MessageBoxIcon icon)
|
|
{
|
|
return icon switch
|
|
{
|
|
MessageBoxIcon.Error => s_errorIcon,
|
|
MessageBoxIcon.Question => s_questionIcon,
|
|
MessageBoxIcon.Warning => s_warningIcon,
|
|
_ => s_infoIcon,
|
|
};
|
|
}
|
|
|
|
private static ImmutableList<string> GetDefaultButtons(MessageBoxButtons buttons)
|
|
{
|
|
return buttons switch
|
|
{
|
|
MessageBoxButtons.AbortRetryIgnore => s_abortRetryContinue,
|
|
MessageBoxButtons.CancelRetryContinue => s_cancelRetryContinue,
|
|
MessageBoxButtons.OkCancel => s_okCancel,
|
|
MessageBoxButtons.RetryCancel => s_retryCancel,
|
|
MessageBoxButtons.YesNo => s_yesNo,
|
|
MessageBoxButtons.YesNoCancel => s_yesNoCancel,
|
|
_ => s_ok,
|
|
};
|
|
}
|
|
}
|
|
}
|