diff --git a/TBF/TBF.csproj b/TBF/TBF.csproj index 669489932..8efa25ff4 100644 --- a/TBF/TBF.csproj +++ b/TBF/TBF.csproj @@ -2156,6 +2156,12 @@ PicturesTabPageCtrl.cs + + + + + + UserControl diff --git a/TBF/UI/Item/EditDouble.cs b/TBF/UI/Item/EditDouble.cs new file mode 100644 index 000000000..d2c40e37a --- /dev/null +++ b/TBF/UI/Item/EditDouble.cs @@ -0,0 +1,66 @@ +using System; +using System.Windows.Forms; + +namespace TBF.UI.Item +{ + public class EditDouble : IEditItem + { + string name; + double variable; + double lowerLimit; + double upperLimit; + string format; + TextBox textBox; + + public EditDouble(string name, ref double variable, double lowerLimit, double upperLimit) + { + this.name = name; + this.variable = variable; + this.lowerLimit = lowerLimit; + this.upperLimit = upperLimit; + this.format = null; + + this.textBox = new TextBox(); + } + + public EditDouble(string name, ref double variable, double lowerLimit) + : this (name, ref variable, lowerLimit, Double.MaxValue) + { + } + + public EditDouble(string name, ref double variable) + : this(name, ref variable, Double.MinValue, Double.MaxValue) + { + } + + public string Name() { return name; } + public Control EmbeddedControl() { return textBox; } + public string Print() { return string.IsNullOrEmpty(format) ? variable.ToString() : variable.ToString(format); } + public void Update(string strValue) { variable = Utils.ParseSDouble(strValue); } + + public bool Validate(string strValue, out string message) + { + double dummy; + if (!Utils.TryParseSDouble(strValue, out dummy)) + { + message = string.Format("{0} is invalid", name); + return false; + } + else if (dummy < lowerLimit) + { + message = string.Format("{0} is smaller then {1}", name, lowerLimit); + return false; + } + else if (dummy > upperLimit) + { + message = string.Format("{0} is larger then {1}", name, upperLimit); + return false; + } + else + { + message = string.Empty; + return true; + } + } + } +} diff --git a/TBF/UI/Item/EditEnum.cs b/TBF/UI/Item/EditEnum.cs new file mode 100644 index 000000000..362f484e4 --- /dev/null +++ b/TBF/UI/Item/EditEnum.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Windows.Forms; + +namespace TBF.UI.Item +{ + public class EditEnum : IEditItem where T : IComparable + { + string name; + T variable; + IEnumerable values; + string[] strValues; + ComboBox comboBox; + + public EditEnum(string name, ref T variable, IListvalues, string[] strValues) + { + if (values.Count != strValues.Length) throw new Exception("values and strValues lengths are different"); + + this.name = name; + this.variable = variable; + this.values = values; + this.strValues = strValues; + + this.comboBox = new ComboBox(); + foreach (var v in strValues) comboBox.Items.Add(v); + } + + public string Name() { return name; } + public Control EmbeddedControl() { return comboBox; } + + public string Print() + { + int ix = 0; + foreach (var v in values) + { + if (v.CompareTo(variable) == 0) return strValues[ix]; + ix++; + } + + return string.Empty; + } + + public void Update(string strValue) + { + int ix = 0; + foreach (var v in values) + { + if (strValues[ix++].Equals(strValue)) + { + variable = v; + return; + } + } + } + + public bool Validate(string strValue, out string message) + { + foreach (var sv in strValues) + { + if (sv == strValue) + { + message = string.Empty; + return true; + } + } + + message = string.Format("{0} is invalid", name); + return false; + } + } +} diff --git a/TBF/UI/Item/EditFloat.cs b/TBF/UI/Item/EditFloat.cs new file mode 100644 index 000000000..ff87b5a34 --- /dev/null +++ b/TBF/UI/Item/EditFloat.cs @@ -0,0 +1,66 @@ +using System; +using System.Windows.Forms; + +namespace TBF.UI.Item +{ + public class EditFloat + { + string name; + float variable; + float lowerLimit; + float upperLimit; + string format; + TextBox textBox; + + public EditFloat(string name, ref float variable, float lowerLimit, float upperLimit) + { + this.name = name; + this.variable = variable; + this.lowerLimit = lowerLimit; + this.upperLimit = upperLimit; + this.format = null; + + this.textBox = new TextBox(); + } + + public EditFloat(string name, ref float variable, float lowerLimit) + : this(name, ref variable, lowerLimit, Single.MaxValue) + { + } + + public EditFloat(string name, ref float variable) + : this(name, ref variable, Single.MinValue, Single.MaxValue) + { + } + + public string Name() { return name; } + public Control EmbeddedControl() { return textBox; } + public string Print() { return string.IsNullOrEmpty(format) ? variable.ToString() : variable.ToString(format); } + public void Update(string strValue) { variable = Utils.ParseSFloat(strValue); } + + public bool Validate(string strValue, out string message) + { + float dummy; + if (!Utils.TryParseSFloat(strValue, out dummy)) + { + message = string.Format("{0} is invalid", name); + return false; + } + else if (dummy < lowerLimit) + { + message = string.Format("{0} is smaller then {1}", name, lowerLimit); + return false; + } + else if (dummy > upperLimit) + { + message = string.Format("{0} is larger then {1}", name, upperLimit); + return false; + } + else + { + message = string.Empty; + return true; + } + } + } +} diff --git a/TBF/UI/Item/EditInt.cs b/TBF/UI/Item/EditInt.cs new file mode 100644 index 000000000..3e504b0d5 --- /dev/null +++ b/TBF/UI/Item/EditInt.cs @@ -0,0 +1,64 @@ +using System; +using System.Windows.Forms; + +namespace TBF.UI.Item +{ + public class EditInt : IEditItem + { + string name; + int variable; + int lowerLimit; + int upperLimit; + TextBox textBox; + + public EditInt(string name, ref int variable, int lowerLimit, int upperLimit) + { + this.name = name; + this.variable = variable; + this.lowerLimit = lowerLimit; + this.upperLimit = upperLimit; + + this.textBox = new TextBox(); + } + + public EditInt(string name, ref int variable, int lowerLimit) + : this (name, ref variable, lowerLimit, Int32.MaxValue) + { + } + + public EditInt(string name, ref int variable) + : this(name, ref variable, Int32.MinValue, Int32.MaxValue) + { + } + + public string Name() { return name; } + public Control EmbeddedControl() { return textBox; } + public string Print() { return variable.ToString(); } + public void Update(string strValue) { variable = int.Parse(strValue); } + + public bool Validate(string strValue, out string message) + { + int dummy; + if (!int.TryParse(strValue, out dummy)) + { + message = string.Format("{0} is invalid", name); + return false; + } + else if (dummy < lowerLimit) + { + message = string.Format("{0} is smaller then {1}", name, lowerLimit); + return false; + } + else if (dummy > upperLimit) + { + message = string.Format("{0} is larger then {1}", name, upperLimit); + return false; + } + else + { + message = string.Empty; + return true; + } + } + } +} diff --git a/TBF/UI/Item/EditString.cs b/TBF/UI/Item/EditString.cs new file mode 100644 index 000000000..1ca293fda --- /dev/null +++ b/TBF/UI/Item/EditString.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Windows.Forms; + +namespace TBF.UI.Item +{ + public class EditString + { + string name; + string variable; + IList alreadyUsed; + TextBox textBox; + + public EditString(string name, ref string variable, IList alreadyUsed) + { + this.name = name; + this.variable = variable; + this.alreadyUsed = alreadyUsed; + + this.textBox = new TextBox(); + } + + public EditString(string name, ref string variable) + : this (name, ref variable, new List()) + { + } + + public string Name() { return name; } + public Control EmbeddedControl() { return textBox; } + public string Print() { return variable; } + public void Update(string strValue) { variable = strValue; } + + public bool Validate(string strValue, out string message) + { + if (alreadyUsed.Contains(strValue)) + { + message = string.Format("Value '{0}' was already used", strValue); + return false; + } + else + { + message = string.Empty; + return true; + } + } + } +} diff --git a/TBF/UI/Item/IEditItem.cs b/TBF/UI/Item/IEditItem.cs new file mode 100644 index 000000000..ca641e6b4 --- /dev/null +++ b/TBF/UI/Item/IEditItem.cs @@ -0,0 +1,41 @@ +using System; +using System.Windows.Forms; + +namespace TBF.UI.Item +{ + public interface IEditItem + { + /// + /// Returns name of the parameter to be displayed as a column header, etc. + /// + /// Name of the parameter + string Name(); + + /// + /// Returns embedded control (Textbox, ComboBox, etc.). + /// + /// Embedded control + Control EmbeddedControl(); + + /// + /// Converts the embedded variable value to its string representation. + /// + /// String representation of the embedded variable + string Print(); + + /// + /// Updates the associated embedded variable from the given string representation. + /// + /// String representation of the new value + void Update(string strValue); + + /// + /// Determines whether the edited value (string representation) is valid. + /// If Validate(..) returns true, then Update(..) has to correctly update the embedded variable. + /// + /// Edited value + /// Explanation message in case Validate() returns false + /// true = string is OK, false = string is wrong (see 'message') + bool Validate(string strValue, out string message); + } +}