moel@63: using System; moel@63: using System.Collections.Generic; moel@63: using System.ComponentModel; moel@63: using System.Text; moel@63: using System.Windows.Forms; moel@63: using OpenHardwareMonitor.Hardware; moel@63: using OpenHardwareMonitor.Utilities; moel@63: moel@63: namespace OpenHardwareMonitor.GUI { moel@63: public partial class ParameterForm : Form { moel@63: moel@63: private IReadOnlyArray parameters; moel@63: private BindingList parameterRows; moel@63: moel@63: public ParameterForm() { moel@63: InitializeComponent(); moel@63: } moel@63: moel@63: public IReadOnlyArray Parameters { moel@63: get { moel@63: return parameters; moel@63: } moel@63: set { moel@63: parameters = value; moel@63: parameterRows = new BindingList(); moel@63: foreach (IParameter parameter in parameters) moel@63: parameterRows.Add(new ParameterRow(parameter)); moel@63: bindingSource.DataSource = parameterRows; moel@63: } moel@63: } moel@63: moel@63: private class ParameterRow : INotifyPropertyChanged { moel@63: public IParameter parameter; moel@63: private float value; moel@63: public bool isDefault; moel@63: moel@63: public event PropertyChangedEventHandler PropertyChanged; moel@63: moel@63: private void NotifyPropertyChanged(String propertyName) { moel@63: if (PropertyChanged != null) { moel@63: PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); moel@63: } moel@63: } moel@63: moel@63: public ParameterRow(IParameter parameter){ moel@63: this.parameter = parameter; moel@63: this.value = parameter.Value; moel@63: this.isDefault = parameter.IsDefault; moel@63: } moel@63: moel@63: public string Name { moel@63: get { return parameter.Name; } moel@63: } moel@63: moel@63: public float Value { moel@63: get { return value; } moel@63: set { moel@63: this.isDefault = false; moel@63: this.value = value; moel@63: NotifyPropertyChanged("Default"); moel@63: NotifyPropertyChanged("Value"); moel@63: } moel@63: } moel@63: moel@63: public bool Default { moel@63: get { return isDefault; } moel@63: set { moel@63: isDefault = value; moel@63: if (value) moel@63: this.value = parameter.DefaultValue; moel@63: NotifyPropertyChanged("Default"); moel@63: NotifyPropertyChanged("Value"); moel@63: } moel@63: } moel@63: } moel@63: moel@63: private void dataGridView_RowEnter(object sender, moel@63: DataGridViewCellEventArgs e) moel@63: { moel@63: if (e.RowIndex >= 0 && e.RowIndex < parameters.Length) moel@63: descriptionLabel.Text = parameters[e.RowIndex].Description; moel@63: else moel@63: descriptionLabel.Text = ""; moel@63: } moel@63: moel@63: private void dataGridView_CellValidating(object sender, moel@63: DataGridViewCellValidatingEventArgs e) moel@63: { moel@63: float value; moel@63: if (e.ColumnIndex == 2 && moel@63: !float.TryParse(e.FormattedValue.ToString(), out value)) { moel@63: dataGridView.Rows[e.RowIndex].Cells[0].ErrorText = moel@63: "Invalid value"; moel@63: e.Cancel = true; moel@63: } moel@63: } moel@63: moel@63: private void dataGridView_CellEndEdit(object sender, moel@63: DataGridViewCellEventArgs e) { moel@63: dataGridView.Rows[e.RowIndex].Cells[0].ErrorText = ""; moel@63: } moel@63: moel@63: private void okButton_Click(object sender, EventArgs e) { moel@63: foreach (ParameterRow row in parameterRows) { moel@63: if (row.Default) { moel@63: row.parameter.IsDefault = true; moel@63: } else { moel@63: row.parameter.Value = row.Value; moel@63: } moel@63: } moel@63: } moel@63: moel@63: private void dataGridView_CurrentCellDirtyStateChanged(object sender, moel@63: EventArgs e) { moel@63: if (dataGridView.CurrentCell is DataGridViewCheckBoxCell || moel@63: dataGridView.CurrentCell is DataGridViewComboBoxCell) moel@63: { moel@63: dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); moel@63: } moel@63: } moel@63: } moel@63: }