324 lines
No EOL
7.5 KiB
C#
324 lines
No EOL
7.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Linq;
|
|
using System.Linq.Expressions;
|
|
using JD.Kernel.StaticHelpers;
|
|
|
|
namespace JD.MessageService
|
|
{
|
|
/// <summary>
|
|
/// An extended message box.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Uses TaskDialogs on Vista and more recent
|
|
/// </remarks>
|
|
public class MessageDialog
|
|
{
|
|
#region Constants
|
|
|
|
internal const int CustomButtonStartId = 0x101;
|
|
|
|
#endregion
|
|
|
|
#region Variables
|
|
|
|
private readonly List<Button> m_buttons = new List<Button>();
|
|
private int m_defaultButtonId;
|
|
private int m_nextButtonId = CustomButtonStartId; // Make sure not to clash with TaskDialog common buttons
|
|
|
|
private string m_caption;
|
|
private string m_mainMessage;
|
|
private string m_content;
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
public MessageDialog()
|
|
{
|
|
Flags = MessageDialogFlags.UseCommandLinks;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Dialog title.
|
|
/// </summary>
|
|
public string Caption
|
|
{
|
|
get { return m_caption; }
|
|
set
|
|
{
|
|
if (m_caption != value)
|
|
{
|
|
m_caption = value;
|
|
OnPropertyChanged(d => d.Caption);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Main message (displayed at top of dialog).
|
|
/// </summary>
|
|
public string MainMessage
|
|
{
|
|
get { return m_mainMessage; }
|
|
set
|
|
{
|
|
if (m_mainMessage != value)
|
|
{
|
|
m_mainMessage = value;
|
|
OnPropertyChanged(d => d.MainMessage);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Content message (displayed in main body of dialog).
|
|
/// </summary>
|
|
public string Content
|
|
{
|
|
get { return m_content; }
|
|
set
|
|
{
|
|
if (m_content != value)
|
|
{
|
|
m_content = value;
|
|
OnPropertyChanged(d => d.Content);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Icon displayed besides the main message.
|
|
/// </summary>
|
|
public MessageDialogIcon MainIcon
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public IList<Button> Buttons
|
|
{
|
|
get { return m_buttons.AsReadOnly(); }
|
|
}
|
|
|
|
public MessageDialogFlags Flags
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public string CheckBoxText
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public bool IsChecked
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
public bool EscapeKeyReturnsNull
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
/// <summary>
|
|
/// If greater than 0, ProgressBarUpdateRequest event will be raised asking for current progress.
|
|
/// </summary>
|
|
public int ProgressBarMaximum
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
internal bool ShouldTick
|
|
{
|
|
get { return Tick != null; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Center the dialog on the owner window rather than the monitor.
|
|
/// </summary>
|
|
public bool PositionRelativeToParent
|
|
{
|
|
get; set;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
public Button AddButton(string text)
|
|
{
|
|
var button = new Button(this, m_nextButtonId++, text);
|
|
m_buttons.Add(button);
|
|
return button;
|
|
}
|
|
|
|
public Button AddCommonButton(MessageDialogCommonButton button)
|
|
{
|
|
Throw.InvalidArgumentIf((int)button >= CustomButtonStartId, "Invalid button", "button");
|
|
Throw.InvalidArgumentIf(m_buttons.Any(b => b.Id == (int)button), "There's already another button with that id", "button");
|
|
|
|
var theButton = new Button(this, (int)button, button.ToString());
|
|
m_buttons.Add(theButton);
|
|
return theButton;
|
|
}
|
|
|
|
public Button ShowDialog()
|
|
{
|
|
Throw.InvalidOperationIf(m_buttons.Count == 0, "Cannot ShowDialog without buttons");
|
|
|
|
if (string.IsNullOrEmpty(Caption))
|
|
{
|
|
Caption = AppDomain.CurrentDomain.FriendlyName;
|
|
}
|
|
|
|
return MessageService.Instance.ShowMessageDialog(this);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Events
|
|
|
|
public event EventHandler<TickEventArgs> Tick;
|
|
|
|
internal void OnTick(TickEventArgs e)
|
|
{
|
|
Tick?.Invoke(this, e);
|
|
}
|
|
|
|
internal event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
private void OnPropertyChanged(Expression<Func<MessageDialog, object>> func)
|
|
{
|
|
string name = MemberNameResolver.ResolveMemberName(func);
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Inner Types
|
|
|
|
public class Button
|
|
{
|
|
#region Variables
|
|
|
|
private readonly MessageDialog m_owner;
|
|
private readonly int m_id;
|
|
private readonly string m_text;
|
|
|
|
private string m_content;
|
|
private bool m_enabled = true;
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
internal Button(MessageDialog owner, int id, string text)
|
|
{
|
|
m_owner = owner;
|
|
m_id = id;
|
|
m_text = text;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
public int Id
|
|
{
|
|
get { return m_id; }
|
|
}
|
|
|
|
public string Text
|
|
{
|
|
get { return m_text; }
|
|
}
|
|
|
|
public bool IsDefault
|
|
{
|
|
get { return m_owner.m_defaultButtonId == Id; }
|
|
set
|
|
{
|
|
if (value)
|
|
{
|
|
m_owner.m_defaultButtonId = Id;
|
|
}
|
|
else if (IsDefault)
|
|
{
|
|
m_owner.m_defaultButtonId = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
public string Content
|
|
{
|
|
get { return m_content; }
|
|
set
|
|
{
|
|
Throw.InvalidOperationIf(IsCommonButton, "Cannot set a content on common buttons");
|
|
m_content = value;
|
|
}
|
|
}
|
|
|
|
public bool Enabled
|
|
{
|
|
get { return m_enabled; }
|
|
set { m_enabled = value; }
|
|
}
|
|
|
|
|
|
public bool IsCommonButton
|
|
{
|
|
get { return Id < CustomButtonStartId; }
|
|
}
|
|
|
|
public object Tag
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
|
|
public class TickEventArgs : EventArgs
|
|
{
|
|
private readonly TimeSpan m_deltaTime;
|
|
|
|
public TickEventArgs(TimeSpan deltaTime)
|
|
{
|
|
CurrentProgress = -1;
|
|
m_deltaTime += deltaTime;
|
|
}
|
|
|
|
public TimeSpan DeltaTime
|
|
{
|
|
get { return m_deltaTime; }
|
|
}
|
|
|
|
public int CurrentProgress
|
|
{
|
|
get;
|
|
set;
|
|
}
|
|
|
|
internal MessageDialog NewDialog
|
|
{
|
|
get;
|
|
private set;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |