GUI/UserRadioGroup.cs
author moel.mich
Mon, 02 Jul 2012 21:14:40 +0000
changeset 357 fb8dc26f65a4
parent 298 96263190189a
permissions -rw-r--r--
Added mainboard specific configurations for the following Gigabyte mainboards: EX58-UD3R, G41M-Combo, G41MT-S2, G41MT-S2P, GA-MA770T-UD3P, GA-MA785GM-US2H, GA-MA78LM-S2H, GA-MA790X-UD3P, H55-USB3, H55N-USB3, H61M-DS2 REV 1.2, H61M-USB3-B3 REV 2.0, H67A-USB3-B3, P55A-UD3, P67A-UD3-B3, P67A-UD3R-B3, Z68A-D3H-B3, Z68AP-D3, Z68X-UD3H-B3.
moel@295
     1
/*
moel@295
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@295
     6
 
moel@344
     7
  Copyright (C) 2011 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@295
     9
*/
moel@295
    10
moel@295
    11
using System;
moel@295
    12
using System.Collections.Generic;
moel@295
    13
using System.Windows.Forms;
moel@295
    14
using OpenHardwareMonitor.Utilities;
moel@295
    15
moel@295
    16
namespace OpenHardwareMonitor.GUI {
moel@295
    17
  public class UserRadioGroup {
moel@295
    18
    private string name;
moel@295
    19
    private int value;
moel@295
    20
    private MenuItem[] menuItems;
moel@295
    21
    private event EventHandler changed;
moel@295
    22
    private PersistentSettings settings;
moel@295
    23
moel@295
    24
    public UserRadioGroup(string name, int value,
moel@295
    25
      MenuItem[] menuItems, PersistentSettings settings) {
moel@295
    26
      this.settings = settings;
moel@295
    27
      this.name = name;
moel@295
    28
      if (name != null)
moel@295
    29
        this.value = settings.GetValue(name, value);
moel@295
    30
      else
moel@295
    31
        this.value = value;
moel@295
    32
      this.menuItems = menuItems;
moel@295
    33
      this.value = Math.Max(Math.Min(this.value, menuItems.Length - 1), 0);
moel@295
    34
moel@295
    35
      for (int i = 0; i < this.menuItems.Length; i++) {
moel@295
    36
        this.menuItems[i].Checked = i == this.value;
moel@295
    37
        int index = i;
moel@295
    38
        this.menuItems[i].Click += delegate(object sender, EventArgs e) {
moel@295
    39
          this.Value = index;
moel@295
    40
        };
moel@295
    41
      }      
moel@295
    42
    }
moel@295
    43
moel@295
    44
    public int Value {
moel@295
    45
      get { return value; }
moel@295
    46
      set {
moel@295
    47
        if (this.value != value) {
moel@295
    48
          this.value = value;
moel@295
    49
          if (this.name != null)
moel@295
    50
            settings.SetValue(name, value);
moel@295
    51
          for (int i = 0; i < this.menuItems.Length; i++) 
moel@295
    52
            this.menuItems[i].Checked = i == value;
moel@295
    53
          if (changed != null)
moel@295
    54
            changed(this, null);
moel@295
    55
        }
moel@295
    56
      }
moel@295
    57
    }
moel@295
    58
moel@295
    59
    public event EventHandler Changed {
moel@295
    60
      add {
moel@295
    61
        changed += value;
moel@295
    62
        if (changed != null)
moel@295
    63
          changed(this, null);
moel@295
    64
      }
moel@295
    65
      remove {
moel@295
    66
        changed -= value;
moel@295
    67
      }
moel@295
    68
    }
moel@295
    69
  }
moel@295
    70
}