GUI/UserRadioGroup.cs
author moel.mich
Sun, 24 Jul 2011 22:12:01 +0000
changeset 314 d19c6b4d625e
parent 295 a1c06df9928d
child 344 3145aadca3d2
permissions -rw-r--r--
Replaced the more expensive calls to DateTime.Now with DateTime.UtcNow.
moel@295
     1
/*
moel@295
     2
  
moel@295
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@295
     4
moel@295
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@295
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@295
     7
  the License. You may obtain a copy of the License at
moel@295
     8
 
moel@295
     9
  http://www.mozilla.org/MPL/
moel@295
    10
moel@295
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@295
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@295
    13
  for the specific language governing rights and limitations under the License.
moel@295
    14
moel@295
    15
  The Original Code is the Open Hardware Monitor code.
moel@295
    16
moel@295
    17
  The Initial Developer of the Original Code is 
moel@295
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@298
    19
  Portions created by the Initial Developer are Copyright (C) 2011
moel@295
    20
  the Initial Developer. All Rights Reserved.
moel@295
    21
moel@295
    22
  Contributor(s):
moel@295
    23
moel@295
    24
  Alternatively, the contents of this file may be used under the terms of
moel@295
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@295
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@295
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@295
    28
  of those above. If you wish to allow use of your version of this file only
moel@295
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@295
    30
  use your version of this file under the terms of the MPL, indicate your
moel@295
    31
  decision by deleting the provisions above and replace them with the notice
moel@295
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@295
    33
  the provisions above, a recipient may use your version of this file under
moel@295
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@295
    35
 
moel@295
    36
*/
moel@295
    37
moel@295
    38
using System;
moel@295
    39
using System.Collections.Generic;
moel@295
    40
using System.Windows.Forms;
moel@295
    41
using OpenHardwareMonitor.Utilities;
moel@295
    42
moel@295
    43
namespace OpenHardwareMonitor.GUI {
moel@295
    44
  public class UserRadioGroup {
moel@295
    45
    private string name;
moel@295
    46
    private int value;
moel@295
    47
    private MenuItem[] menuItems;
moel@295
    48
    private event EventHandler changed;
moel@295
    49
    private PersistentSettings settings;
moel@295
    50
moel@295
    51
    public UserRadioGroup(string name, int value,
moel@295
    52
      MenuItem[] menuItems, PersistentSettings settings) {
moel@295
    53
      this.settings = settings;
moel@295
    54
      this.name = name;
moel@295
    55
      if (name != null)
moel@295
    56
        this.value = settings.GetValue(name, value);
moel@295
    57
      else
moel@295
    58
        this.value = value;
moel@295
    59
      this.menuItems = menuItems;
moel@295
    60
      this.value = Math.Max(Math.Min(this.value, menuItems.Length - 1), 0);
moel@295
    61
moel@295
    62
      for (int i = 0; i < this.menuItems.Length; i++) {
moel@295
    63
        this.menuItems[i].Checked = i == this.value;
moel@295
    64
        int index = i;
moel@295
    65
        this.menuItems[i].Click += delegate(object sender, EventArgs e) {
moel@295
    66
          this.Value = index;
moel@295
    67
        };
moel@295
    68
      }      
moel@295
    69
    }
moel@295
    70
moel@295
    71
    public int Value {
moel@295
    72
      get { return value; }
moel@295
    73
      set {
moel@295
    74
        if (this.value != value) {
moel@295
    75
          this.value = value;
moel@295
    76
          if (this.name != null)
moel@295
    77
            settings.SetValue(name, value);
moel@295
    78
          for (int i = 0; i < this.menuItems.Length; i++) 
moel@295
    79
            this.menuItems[i].Checked = i == value;
moel@295
    80
          if (changed != null)
moel@295
    81
            changed(this, null);
moel@295
    82
        }
moel@295
    83
      }
moel@295
    84
    }
moel@295
    85
moel@295
    86
    public event EventHandler Changed {
moel@295
    87
      add {
moel@295
    88
        changed += value;
moel@295
    89
        if (changed != null)
moel@295
    90
          changed(this, null);
moel@295
    91
      }
moel@295
    92
      remove {
moel@295
    93
        changed -= value;
moel@295
    94
      }
moel@295
    95
    }
moel@295
    96
  }
moel@295
    97
}