moel@86: /*
moel@86:   
moel@86:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@86: 
moel@86:   The contents of this file are subject to the Mozilla Public License Version
moel@86:   1.1 (the "License"); you may not use this file except in compliance with
moel@86:   the License. You may obtain a copy of the License at
moel@86:  
moel@86:   http://www.mozilla.org/MPL/
moel@86: 
moel@86:   Software distributed under the License is distributed on an "AS IS" basis,
moel@86:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@86:   for the specific language governing rights and limitations under the License.
moel@86: 
moel@86:   The Original Code is the Open Hardware Monitor code.
moel@86: 
moel@86:   The Initial Developer of the Original Code is 
moel@86:   Michael Möller <m.moeller@gmx.ch>.
moel@86:   Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@86:   the Initial Developer. All Rights Reserved.
moel@86: 
moel@86:   Contributor(s):
moel@86: 
moel@86:   Alternatively, the contents of this file may be used under the terms of
moel@86:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@86:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@86:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@86:   of those above. If you wish to allow use of your version of this file only
moel@86:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@86:   use your version of this file under the terms of the MPL, indicate your
moel@86:   decision by deleting the provisions above and replace them with the notice
moel@86:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@86:   the provisions above, a recipient may use your version of this file under
moel@86:   the terms of any one of the MPL, the GPL or the LGPL.
moel@86:  
moel@86: */
moel@86: 
moel@86: 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@165: using OpenHardwareMonitor.Collections;
moel@63: 
moel@63: namespace OpenHardwareMonitor.GUI {
moel@63:   public partial class ParameterForm : Form {
moel@63: 
moel@63:     private IReadOnlyArray<IParameter> parameters;
moel@63:     private BindingList<ParameterRow> parameterRows;
moel@63: 
moel@63:     public ParameterForm() {
moel@63:       InitializeComponent();
moel@63:     }
moel@63:     
moel@63:     public IReadOnlyArray<IParameter> Parameters {
moel@63:       get {
moel@63:         return parameters;
moel@63:       }
moel@63:       set {
moel@63:         parameters = value;
moel@63:         parameterRows = new BindingList<ParameterRow>();
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: }