GUI/UserOption.cs
author moel.mich
Thu, 11 Nov 2010 21:22:24 +0000
changeset 241 52007c404f32
parent 165 813d8bc3192f
child 344 3145aadca3d2
permissions -rw-r--r--
Fixed a problem, where the MainForm location and size was lost when the application is started minimized and exited without ever showing the form. This caused MainForm_Load to be never called (location and size was not loaded), but the default size and location were still saved. The new implementation only saves the location and size when one of the two is changed.
moel@156
     1
/*
moel@156
     2
  
moel@156
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@156
     4
moel@156
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@156
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@156
     7
  the License. You may obtain a copy of the License at
moel@156
     8
 
moel@156
     9
  http://www.mozilla.org/MPL/
moel@156
    10
moel@156
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@156
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@156
    13
  for the specific language governing rights and limitations under the License.
moel@156
    14
moel@156
    15
  The Original Code is the Open Hardware Monitor code.
moel@156
    16
moel@156
    17
  The Initial Developer of the Original Code is 
moel@156
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@156
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@156
    20
  the Initial Developer. All Rights Reserved.
moel@156
    21
moel@156
    22
  Contributor(s):
moel@156
    23
moel@156
    24
  Alternatively, the contents of this file may be used under the terms of
moel@156
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@156
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@156
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@156
    28
  of those above. If you wish to allow use of your version of this file only
moel@156
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@156
    30
  use your version of this file under the terms of the MPL, indicate your
moel@156
    31
  decision by deleting the provisions above and replace them with the notice
moel@156
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@156
    33
  the provisions above, a recipient may use your version of this file under
moel@156
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@156
    35
 
moel@156
    36
*/
moel@156
    37
moel@156
    38
using System;
moel@156
    39
using System.Collections.Generic;
moel@156
    40
using System.Windows.Forms;
moel@156
    41
using OpenHardwareMonitor.Utilities;
moel@156
    42
moel@156
    43
namespace OpenHardwareMonitor.GUI {
moel@156
    44
  public class UserOption {
moel@156
    45
    private string name;
moel@156
    46
    private bool value;
moel@156
    47
    private MenuItem menuItem;
moel@156
    48
    private event EventHandler changed;
moel@165
    49
    private PersistentSettings settings;
moel@156
    50
moel@156
    51
    public UserOption(string name, bool value,
moel@165
    52
      MenuItem menuItem, PersistentSettings settings) {
moel@156
    53
moel@165
    54
      this.settings = settings;
moel@156
    55
      this.name = name;
moel@156
    56
      if (name != null)
moel@166
    57
        this.value = settings.GetValue(name, value);
moel@156
    58
      else
moel@156
    59
        this.value = value;
moel@156
    60
      this.menuItem = menuItem;
moel@156
    61
      this.menuItem.Checked = this.value;
moel@156
    62
      this.menuItem.Click += new EventHandler(menuItem_Click);
moel@156
    63
    }
moel@156
    64
moel@156
    65
    private void menuItem_Click(object sender, EventArgs e) {
moel@156
    66
      this.Value = !this.Value;
moel@156
    67
    }    
moel@156
    68
moel@156
    69
    public bool Value {
moel@156
    70
      get { return value; }
moel@156
    71
      set {
moel@156
    72
        if (this.value != value) {
moel@156
    73
          this.value = value;
moel@156
    74
          if (this.name != null)
moel@166
    75
            settings.SetValue(name, value);
moel@156
    76
          this.menuItem.Checked = value;
moel@156
    77
          if (changed != null)
moel@156
    78
            changed(this, null);
moel@156
    79
        }
moel@156
    80
      }
moel@156
    81
    }
moel@156
    82
moel@156
    83
    public event EventHandler Changed {
moel@156
    84
      add {
moel@156
    85
        changed += value;
moel@156
    86
        if (changed != null)
moel@156
    87
          changed(this, null);
moel@156
    88
      }
moel@156
    89
      remove {
moel@156
    90
        changed -= value;
moel@156
    91
      }
moel@156
    92
    }
moel@156
    93
  }
moel@156
    94
}