moel@295: /* moel@295: moel@344: This Source Code Form is subject to the terms of the Mozilla Public moel@344: License, v. 2.0. If a copy of the MPL was not distributed with this moel@344: file, You can obtain one at http://mozilla.org/MPL/2.0/. moel@295: moel@344: Copyright (C) 2011 Michael Möller moel@344: 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: }