moel@295: /*
moel@295:   
moel@295:   Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@295: 
moel@295:   The contents of this file are subject to the Mozilla Public License Version
moel@295:   1.1 (the "License"); you may not use this file except in compliance with
moel@295:   the License. You may obtain a copy of the License at
moel@295:  
moel@295:   http://www.mozilla.org/MPL/
moel@295: 
moel@295:   Software distributed under the License is distributed on an "AS IS" basis,
moel@295:   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@295:   for the specific language governing rights and limitations under the License.
moel@295: 
moel@295:   The Original Code is the Open Hardware Monitor code.
moel@295: 
moel@295:   The Initial Developer of the Original Code is 
moel@295:   Michael Möller <m.moeller@gmx.ch>.
moel@298:   Portions created by the Initial Developer are Copyright (C) 2011
moel@295:   the Initial Developer. All Rights Reserved.
moel@295: 
moel@295:   Contributor(s):
moel@295: 
moel@295:   Alternatively, the contents of this file may be used under the terms of
moel@295:   either the GNU General Public License Version 2 or later (the "GPL"), or
moel@295:   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@295:   in which case the provisions of the GPL or the LGPL are applicable instead
moel@295:   of those above. If you wish to allow use of your version of this file only
moel@295:   under the terms of either the GPL or the LGPL, and not to allow others to
moel@295:   use your version of this file under the terms of the MPL, indicate your
moel@295:   decision by deleting the provisions above and replace them with the notice
moel@295:   and other provisions required by the GPL or the LGPL. If you do not delete
moel@295:   the provisions above, a recipient may use your version of this file under
moel@295:   the terms of any one of the MPL, the GPL or the LGPL.
moel@295:  
moel@295: */
moel@295: 
moel@295: using System;
moel@295: using System.Collections.Generic;
moel@295: using System.Windows.Forms;
moel@295: using OpenHardwareMonitor.Utilities;
moel@295: 
moel@295: namespace OpenHardwareMonitor.GUI {
moel@295:   public class UserRadioGroup {
moel@295:     private string name;
moel@295:     private int value;
moel@295:     private MenuItem[] menuItems;
moel@295:     private event EventHandler changed;
moel@295:     private PersistentSettings settings;
moel@295: 
moel@295:     public UserRadioGroup(string name, int value,
moel@295:       MenuItem[] menuItems, PersistentSettings settings) {
moel@295:       this.settings = settings;
moel@295:       this.name = name;
moel@295:       if (name != null)
moel@295:         this.value = settings.GetValue(name, value);
moel@295:       else
moel@295:         this.value = value;
moel@295:       this.menuItems = menuItems;
moel@295:       this.value = Math.Max(Math.Min(this.value, menuItems.Length - 1), 0);
moel@295: 
moel@295:       for (int i = 0; i < this.menuItems.Length; i++) {
moel@295:         this.menuItems[i].Checked = i == this.value;
moel@295:         int index = i;
moel@295:         this.menuItems[i].Click += delegate(object sender, EventArgs e) {
moel@295:           this.Value = index;
moel@295:         };
moel@295:       }      
moel@295:     }
moel@295: 
moel@295:     public int Value {
moel@295:       get { return value; }
moel@295:       set {
moel@295:         if (this.value != value) {
moel@295:           this.value = value;
moel@295:           if (this.name != null)
moel@295:             settings.SetValue(name, value);
moel@295:           for (int i = 0; i < this.menuItems.Length; i++) 
moel@295:             this.menuItems[i].Checked = i == value;
moel@295:           if (changed != null)
moel@295:             changed(this, null);
moel@295:         }
moel@295:       }
moel@295:     }
moel@295: 
moel@295:     public event EventHandler Changed {
moel@295:       add {
moel@295:         changed += value;
moel@295:         if (changed != null)
moel@295:           changed(this, null);
moel@295:       }
moel@295:       remove {
moel@295:         changed -= value;
moel@295:       }
moel@295:     }
moel@295:   }
moel@295: }