using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using RainmeterStudio.Resources;
namespace RainmeterStudio.UI.Dialogs
{
///
/// Interaction logic for InputDialog.xaml
///
public partial class InputDialog : Window
{
private Func _validateFunction;
///
/// A validate function that always returns true
///
public static readonly Func AlwaysValid = (str => true);
#region Properties
///
/// Gets or sets the prompt text
///
public string Prompt
{
get
{
return textPrompt.Text;
}
set
{
textPrompt.Text = value;
}
}
///
/// Gets or sets the text on the 'ok' button
///
public string OKCaption
{
get
{
return (string)buttonOK.Content;
}
set
{
buttonOK.Content = value;
}
}
///
/// Gets or sets the text on the 'cancel' button
///
public string CancelCaption
{
get
{
return (string)buttonCancel.Content;
}
set
{
buttonCancel.Content = value;
}
}
///
/// Gets or sets the text inputted by the user
///
public string InputText
{
get
{
return textInput.Text;
}
set
{
textInput.Text = value;
Validate();
}
}
///
/// Gets or sets a function used to validate the input
///
public Func ValidateFunction
{
get
{
return _validateFunction;
}
set
{
_validateFunction = value;
Validate();
}
}
#endregion
///
/// Initializes the input dialog
///
/// Message displayed to user
public InputDialog(string prompt)
:this(prompt, String.Empty, String.Empty, AlwaysValid)
{
}
///
/// Initializes the input dialog
///
/// Message displayed to user
/// Title of dialog
public InputDialog(string prompt, string caption)
: this(prompt, caption, String.Empty, AlwaysValid)
{
}
///
/// Initializes the input dialog
///
/// Message displayed to user
/// Title of dialog
/// Caption of the 'OK' button
/// Caption of the 'Cancel' button
public InputDialog(string prompt, string caption, string okCaption, string cancelCaption)
: this(prompt, caption, String.Empty, AlwaysValid, okCaption, cancelCaption)
{
}
///
/// Initializes the input dialog
///
/// Message displayed to user
/// Title of dialog
/// Callback function which validates the inputted text
public InputDialog(string prompt, string caption, Func validateFunction)
: this(prompt, caption, String.Empty, validateFunction)
{
}
///
/// Initializes the input dialog
///
/// Message displayed to user
/// Title of dialog
/// Callback function which validates the inputted text
/// Caption of the 'OK' button
/// Caption of the 'Cancel' button
public InputDialog(string prompt, string caption, Func validateFunction, string okCaption, string cancelCaption)
: this(prompt, caption, String.Empty, validateFunction, okCaption, cancelCaption)
{
}
///
/// Initializes the input dialog
///
/// Message displayed to user
/// Title of dialog
/// Initial value of the input dialog
public InputDialog(string prompt, string caption, string initialValue)
: this(prompt, caption, initialValue, AlwaysValid)
{
}
///
/// Initializes the input dialog
///
/// Message displayed to user
/// Title of dialog
/// Initial value of the input dialog
/// Caption of the 'OK' button
/// Caption of the 'Cancel' button
public InputDialog(string prompt, string caption, string initialValue, string okCaption, string cancelCaption)
: this(prompt, caption, initialValue, AlwaysValid, okCaption, cancelCaption)
{
}
///
/// Initializes the input dialog
///
/// Message displayed to user
/// Title of dialog
/// Initial value of the input dialog
/// Callback function which validates the inputted text
public InputDialog(string prompt, string caption, string initialValue, Func validateFunction)
: this(prompt, caption, initialValue, validateFunction, Strings.Dialog_OK, Strings.Dialog_Cancel)
{
}
///
/// Initializes the input dialog
///
/// Message displayed to user
/// Title of dialog
/// Initial value of the input dialog
/// Callback function which validates the inputted text
/// Caption of the 'OK' button
/// Caption of the 'Cancel' button
public InputDialog(string prompt, string caption, string initialValue, Func validateFunction, string okCaption, string cancelCaption)
{
InitializeComponent();
Prompt = prompt;
Title = caption;
InputText = initialValue;
ValidateFunction = validateFunction;
OKCaption = okCaption;
CancelCaption = cancelCaption;
}
private void buttonOK_Click(object sender, RoutedEventArgs e)
{
DialogResult = true;
Close();
}
private void buttonCancel_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
Close();
}
private void textInput_TextChanged(object sender, TextChangedEventArgs e)
{
Validate();
}
private void Validate()
{
bool res = true;
if (ValidateFunction != null)
res = ValidateFunction(textInput.Text);
buttonOK.IsEnabled = res;
}
#region Static show
///
/// Shows a dialog prompting the user for input
///
/// Message displayed to user
/// Input text, or null if canceled
public static string Show(string prompt)
{
return ShowCommon(new InputDialog(prompt));
}
///
/// Shows a dialog prompting the user for input
///
/// Message displayed to user
/// Title of dialog
/// Input text, or null if canceled
public static string Show(string prompt, string caption)
{
return ShowCommon(new InputDialog(prompt, caption));
}
///
/// Shows a dialog prompting the user for input
///
/// Message displayed to user
/// Title of dialog
/// Caption of the 'OK' button
/// Caption of the 'Cancel' button
/// Input text, or null if canceled
public static string Show(string prompt, string caption, string okCaption, string cancelCaption)
{
return ShowCommon(new InputDialog(prompt, caption, okCaption, cancelCaption));
}
///
/// Shows a dialog prompting the user for input
///
/// Message displayed to user
/// Title of dialog
/// Callback function which validates the inputted text
/// Input text, or null if canceled
public static string Show(string prompt, string caption, Func validateFunction)
{
return ShowCommon(new InputDialog(prompt, caption, validateFunction));
}
///
/// Shows a dialog prompting the user for input
///
/// Message displayed to user
/// Title of dialog
/// Callback function which validates the inputted text
/// Caption of the 'OK' button
/// Caption of the 'Cancel' button
/// Input text, or null if canceled
public static string Show(string prompt, string caption, Func validateFunction, string okCaption, string cancelCaption)
{
return ShowCommon(new InputDialog(prompt, caption, validateFunction, okCaption, cancelCaption));
}
///
/// Shows a dialog prompting the user for input
///
/// Message displayed to user
/// Title of dialog
/// Initial value of the input dialog
/// Input text, or null if canceled
public static string Show(string prompt, string caption, string initialValue)
{
return ShowCommon(new InputDialog(prompt, caption, initialValue));
}
///
/// Shows a dialog prompting the user for input
///
/// Message displayed to user
/// Title of dialog
/// Initial value of the input dialog
/// Caption of the 'OK' button
/// Caption of the 'Cancel' button
/// Input text, or null if canceled
public static string Show(string prompt, string caption, string initialValue, string okCaption, string cancelCaption)
{
return ShowCommon(new InputDialog(prompt, caption, initialValue, okCaption, cancelCaption));
}
///
/// Shows a dialog prompting the user for input
///
/// Message displayed to user
/// Title of dialog
/// Initial value of the input dialog
/// Callback function which validates the inputted text
/// Input text, or null if canceled
public static string Show(string prompt, string caption, string initialValue, Func validateFunction)
{
return ShowCommon(new InputDialog(prompt, caption, initialValue, validateFunction));
}
///
/// Shows a dialog prompting the user for input
///
/// Message displayed to user
/// Title of dialog
/// Initial value of the input dialog
/// Callback function which validates the inputted text
/// Caption of the 'OK' button
/// Caption of the 'Cancel' button
/// Input text, or null if canceled
public static string Show(string prompt, string caption, string initialValue, Func validateFunction, string okCaption, string cancelCaption)
{
return ShowCommon(new InputDialog(prompt, caption, initialValue, validateFunction, okCaption, cancelCaption));
}
private static string ShowCommon(InputDialog dialog)
{
bool? res = dialog.ShowDialog();
if (res.HasValue && res.Value)
{
return dialog.InputText;
}
return null;
}
#endregion
}
}