// -----------------------------------------------------------------------
//
// TODO: Update copyright text.
//
// -----------------------------------------------------------------------
namespace Plane.Windows.Messages
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
///
/// Displays a message box.
///
public static class Alert
{
private static Dispatcher _Dispatcher;
private static Dispatcher Dispatcher
{
get { return _Dispatcher ?? (_Dispatcher = Application.Current.Dispatcher); }
}
private static MessageBoxResult RunOnUIThread(Func func)
{
if (Dispatcher.CheckAccess())
{
try
{
return func();
}
catch (Exception ex)
{
return 0;
}
}
else
{
return (MessageBoxResult)Dispatcher.Invoke(func);
}
}
///
/// Displays a message box that has a message and returns a result.
///
/// A System.String that specifies the text to display.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult Show(string messageBoxText)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText);
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message and a title bar caption; and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult Show(string messageBoxText, string caption)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption);
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box in front of the specified window. The message box displays a message and returns a result.
///
/// A System.Windows.Window that represents the owner window of the message box.
/// A System.String that specifies the text to display.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult Show(Window owner, string messageBoxText)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText);
msg.Owner = owner;
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box in front of the specified window. The message box displays a message and title bar caption; and it returns a result.
///
/// A System.Windows.Window that represents the owner window of the message box.
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult Show(Window owner, string messageBoxText, string caption)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption);
msg.Owner = owner;
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message, title bar caption, and button; and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.Windows.MessageBoxButton value that specifies which button or buttons to display.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption, button);
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message, title bar caption, button, and icon; and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.Windows.MessageBoxButton value that specifies which button or buttons to display.
/// A System.Windows.MessageBoxImage value that specifies the icon to display.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult Show(string messageBoxText, string caption, MessageBoxButton button, MessageBoxImage icon)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption, button, icon);
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message, title bar caption, and OK button with a custom System.String value for the button's text; and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.String that specifies the text to display within the OK button.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult ShowOK(string messageBoxText, string caption, string okButtonText)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.OK);
msg.OkButtonText = okButtonText;
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message, title bar caption, OK button with a custom System.String value for the button's text, and icon; and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.String that specifies the text to display within the OK button.
/// A System.Windows.MessageBoxImage value that specifies the icon to display.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult ShowOK(string messageBoxText, string caption, string okButtonText, MessageBoxImage icon)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.OK, icon);
msg.OkButtonText = okButtonText;
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message, caption, and OK/Cancel buttons with custom System.String values for the buttons' text;
/// and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.String that specifies the text to display within the OK button.
/// A System.String that specifies the text to display within the Cancel button.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult ShowOKCancel(string messageBoxText, string caption, string okButtonText, string cancelButtonText)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.OKCancel);
msg.OkButtonText = okButtonText;
msg.CancelButtonText = cancelButtonText;
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message, caption, OK/Cancel buttons with custom System.String values for the buttons' text, and icon;
/// and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.String that specifies the text to display within the OK button.
/// A System.String that specifies the text to display within the Cancel button.
/// A System.Windows.MessageBoxImage value that specifies the icon to display.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult ShowOKCancel(string messageBoxText, string caption, string okButtonText, string cancelButtonText, MessageBoxImage icon)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.OKCancel, icon);
msg.OkButtonText = okButtonText;
msg.CancelButtonText = cancelButtonText;
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message, caption, and Yes/No buttons with custom System.String values for the buttons' text;
/// and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.String that specifies the text to display within the Yes button.
/// A System.String that specifies the text to display within the No button.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult ShowYesNo(string messageBoxText, string caption, string yesButtonText, string noButtonText)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.YesNo);
msg.YesButtonText = yesButtonText;
msg.NoButtonText = noButtonText;
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message, caption, Yes/No buttons with custom System.String values for the buttons' text, and icon;
/// and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.String that specifies the text to display within the Yes button.
/// A System.String that specifies the text to display within the No button.
/// A System.Windows.MessageBoxImage value that specifies the icon to display.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult ShowYesNo(string messageBoxText, string caption, string yesButtonText, string noButtonText, MessageBoxImage icon)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.YesNo, icon);
msg.YesButtonText = yesButtonText;
msg.NoButtonText = noButtonText;
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message, caption, and Yes/No/Cancel buttons with custom System.String values for the buttons' text;
/// and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.String that specifies the text to display within the Yes button.
/// A System.String that specifies the text to display within the No button.
/// A System.String that specifies the text to display within the Cancel button.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult ShowYesNoCancel(string messageBoxText, string caption, string yesButtonText, string noButtonText, string cancelButtonText)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.YesNoCancel);
msg.YesButtonText = yesButtonText;
msg.NoButtonText = noButtonText;
msg.CancelButtonText = cancelButtonText;
msg.ShowDialog();
return msg.Result;
});
}
///
/// Displays a message box that has a message, caption, Yes/No/Cancel buttons with custom System.String values for the buttons' text, and icon;
/// and that returns a result.
///
/// A System.String that specifies the text to display.
/// A System.String that specifies the title bar caption to display.
/// A System.String that specifies the text to display within the Yes button.
/// A System.String that specifies the text to display within the No button.
/// A System.String that specifies the text to display within the Cancel button.
/// A System.Windows.MessageBoxImage value that specifies the icon to display.
/// A System.Windows.MessageBoxResult value that specifies which message box button is clicked by the user.
public static MessageBoxResult ShowYesNoCancel(string messageBoxText, string caption, string yesButtonText, string noButtonText, string cancelButtonText, MessageBoxImage icon)
{
return RunOnUIThread(() =>
{
AlertWindow msg = new AlertWindow(messageBoxText, caption, MessageBoxButton.YesNoCancel, icon);
msg.YesButtonText = yesButtonText;
msg.NoButtonText = noButtonText;
msg.CancelButtonText = cancelButtonText;
msg.ShowDialog();
return msg.Result;
});
}
public static MessageBoxResult ShowProgressAutoClose(string loadingText, Func completedChecker, Func cancelledChecker)
{
return ShowProgressCore(loadingText, msgWindow => msgWindow.Close(), completedChecker, cancelledChecker);
}
public static MessageBoxResult ShowProgress(string loadingText, string completedText, Func completedChecker, Func cancelledChecker)
{
return ShowProgressCore(loadingText, msgWindow => msgWindow.CompleteProgress(completedText), completedChecker, cancelledChecker);
}
private static MessageBoxResult ShowProgressCore(string loadingText, Action completedHandler, Func completedChecker, Func cancelledChecker)
{
return RunOnUIThread(() =>
{
var msgWindow = AlertWindow.CreateProgressWindow(loadingText);
Task.Factory.StartNew(() =>
{
while (true)
{
if (completedChecker())
{
Application.Current.Dispatcher.Invoke(new Action(() => completedHandler(msgWindow)));
break;
}
else if (cancelledChecker != null && cancelledChecker())
{
Application.Current.Dispatcher.Invoke(new Action(() => msgWindow.Close()));
break;
};
Thread.Sleep(500);
}
});
if (!completedChecker())
{
msgWindow.ShowDialog();
}
return msgWindow.Result;
});
}
}
}