GUI/UserRadioGroup.cs
author moel.mich
Sun, 18 Aug 2013 21:44:08 +0000
changeset 421 055a9ec117d2
parent 298 96263190189a
permissions -rw-r--r--
Fixed a few stability issues in the logging implementation. Added support for logging sensors once the reappear.
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
}