GUI/ParameterForm.cs
changeset 63 1a7c13ac7348
child 86 b4f0f206173d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GUI/ParameterForm.cs	Sat Feb 27 15:55:17 2010 +0000
     1.3 @@ -0,0 +1,122 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.ComponentModel;
     1.7 +using System.Text;
     1.8 +using System.Windows.Forms;
     1.9 +using OpenHardwareMonitor.Hardware;
    1.10 +using OpenHardwareMonitor.Utilities;
    1.11 +
    1.12 +namespace OpenHardwareMonitor.GUI {
    1.13 +  public partial class ParameterForm : Form {
    1.14 +
    1.15 +    private IReadOnlyArray<IParameter> parameters;
    1.16 +    private BindingList<ParameterRow> parameterRows;
    1.17 +
    1.18 +    public ParameterForm() {
    1.19 +      InitializeComponent();
    1.20 +    }
    1.21 +    
    1.22 +    public IReadOnlyArray<IParameter> Parameters {
    1.23 +      get {
    1.24 +        return parameters;
    1.25 +      }
    1.26 +      set {
    1.27 +        parameters = value;
    1.28 +        parameterRows = new BindingList<ParameterRow>();
    1.29 +        foreach (IParameter parameter in parameters)
    1.30 +          parameterRows.Add(new ParameterRow(parameter));
    1.31 +        bindingSource.DataSource = parameterRows;
    1.32 +      }
    1.33 +    }
    1.34 +
    1.35 +    private class ParameterRow : INotifyPropertyChanged {
    1.36 +      public IParameter parameter;
    1.37 +      private float value;
    1.38 +      public bool isDefault;
    1.39 +
    1.40 +      public event PropertyChangedEventHandler PropertyChanged;
    1.41 +
    1.42 +      private void NotifyPropertyChanged(String propertyName) {
    1.43 +        if (PropertyChanged != null) {
    1.44 +          PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    1.45 +        }
    1.46 +      }
    1.47 +
    1.48 +      public ParameterRow(IParameter parameter){
    1.49 +        this.parameter = parameter;
    1.50 +        this.value = parameter.Value;
    1.51 +        this.isDefault = parameter.IsDefault;
    1.52 +      }
    1.53 +
    1.54 +      public string Name {
    1.55 +        get { return parameter.Name; }
    1.56 +      }
    1.57 +
    1.58 +      public float Value {
    1.59 +        get { return value; }
    1.60 +        set {            
    1.61 +          this.isDefault = false;
    1.62 +          this.value = value;
    1.63 +          NotifyPropertyChanged("Default");
    1.64 +          NotifyPropertyChanged("Value");
    1.65 +        }
    1.66 +      }
    1.67 +
    1.68 +      public bool Default {
    1.69 +        get { return isDefault; }
    1.70 +        set {
    1.71 +          isDefault = value;
    1.72 +          if (value)
    1.73 +            this.value = parameter.DefaultValue;
    1.74 +          NotifyPropertyChanged("Default");
    1.75 +          NotifyPropertyChanged("Value");
    1.76 +        }
    1.77 +      }
    1.78 +    }
    1.79 +
    1.80 +    private void dataGridView_RowEnter(object sender, 
    1.81 +      DataGridViewCellEventArgs e) 
    1.82 +    {
    1.83 +      if (e.RowIndex >= 0 && e.RowIndex < parameters.Length)
    1.84 +        descriptionLabel.Text = parameters[e.RowIndex].Description;
    1.85 +      else
    1.86 +        descriptionLabel.Text = "";
    1.87 +    }
    1.88 +
    1.89 +    private void dataGridView_CellValidating(object sender, 
    1.90 +      DataGridViewCellValidatingEventArgs e) 
    1.91 +    {
    1.92 +      float value;
    1.93 +      if (e.ColumnIndex == 2 &&
    1.94 +        !float.TryParse(e.FormattedValue.ToString(), out value)) {
    1.95 +        dataGridView.Rows[e.RowIndex].Cells[0].ErrorText = 
    1.96 +          "Invalid value";
    1.97 +        e.Cancel = true;
    1.98 +      }
    1.99 +    }
   1.100 +
   1.101 +    private void dataGridView_CellEndEdit(object sender,
   1.102 +      DataGridViewCellEventArgs e) {
   1.103 +      dataGridView.Rows[e.RowIndex].Cells[0].ErrorText = "";
   1.104 +    }
   1.105 +
   1.106 +    private void okButton_Click(object sender, EventArgs e) {
   1.107 +      foreach (ParameterRow row in parameterRows) {
   1.108 +        if (row.Default) {
   1.109 +          row.parameter.IsDefault = true;
   1.110 +        } else {
   1.111 +          row.parameter.Value = row.Value;
   1.112 +        }
   1.113 +      }
   1.114 +    }
   1.115 +
   1.116 +    private void dataGridView_CurrentCellDirtyStateChanged(object sender, 
   1.117 +      EventArgs e) {
   1.118 +      if (dataGridView.CurrentCell is DataGridViewCheckBoxCell ||
   1.119 +        dataGridView.CurrentCell is DataGridViewComboBoxCell) 
   1.120 +      {
   1.121 +        dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
   1.122 +      }
   1.123 +    }
   1.124 +  }
   1.125 +}