GUI/SensorSystemTray.cs
author moel.mich
Fri, 12 Feb 2010 22:46:31 +0000
changeset 42 47385d4fc990
parent 40 2392f7402fb6
child 64 15181001ee61
permissions -rw-r--r--
Tray sensor display default color is black and color can be changed now. Fixed CPU load reading for AMD CPUs and added additional misc device for AMD core temperature reading.
moel@40
     1
/*
moel@40
     2
  
moel@40
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@40
     4
moel@40
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@40
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@40
     7
  the License. You may obtain a copy of the License at
moel@40
     8
 
moel@40
     9
  http://www.mozilla.org/MPL/
moel@40
    10
moel@40
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@40
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@40
    13
  for the specific language governing rights and limitations under the License.
moel@40
    14
moel@40
    15
  The Original Code is the Open Hardware Monitor code.
moel@40
    16
moel@40
    17
  The Initial Developer of the Original Code is 
moel@40
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@40
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@40
    20
  the Initial Developer. All Rights Reserved.
moel@40
    21
moel@40
    22
  Contributor(s):
moel@40
    23
moel@40
    24
  Alternatively, the contents of this file may be used under the terms of
moel@40
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@40
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@40
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@40
    28
  of those above. If you wish to allow use of your version of this file only
moel@40
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@40
    30
  use your version of this file under the terms of the MPL, indicate your
moel@40
    31
  decision by deleting the provisions above and replace them with the notice
moel@40
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@40
    33
  the provisions above, a recipient may use your version of this file under
moel@40
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@40
    35
 
moel@40
    36
*/
moel@40
    37
moel@40
    38
using System;
moel@40
    39
using System.Collections.Generic;
moel@42
    40
using System.Drawing;
moel@40
    41
using System.Text;
moel@40
    42
using System.Windows.Forms;
moel@40
    43
using OpenHardwareMonitor.Hardware;
moel@40
    44
using OpenHardwareMonitor.Utilities;
moel@40
    45
moel@40
    46
namespace OpenHardwareMonitor.GUI {
moel@40
    47
  public class SensorSystemTray : IDisposable {
moel@40
    48
    private Computer computer;
moel@40
    49
    private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
moel@40
    50
moel@40
    51
    public SensorSystemTray(Computer computer) {
moel@40
    52
      this.computer = computer;
moel@40
    53
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@40
    54
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
moel@40
    55
    }
moel@40
    56
moel@40
    57
    private void HardwareRemoved(IHardware hardware) {
moel@40
    58
      hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
moel@40
    59
      hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
moel@40
    60
      foreach (ISensor sensor in hardware.Sensors) 
moel@40
    61
        SensorRemoved(sensor);
moel@40
    62
    }
moel@40
    63
   
moel@40
    64
    private void HardwareAdded(IHardware hardware) {
moel@40
    65
      foreach (ISensor sensor in hardware.Sensors)
moel@40
    66
        SensorAdded(sensor);
moel@40
    67
      hardware.SensorAdded += new SensorEventHandler(SensorAdded);
moel@40
    68
      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
moel@40
    69
    }
moel@40
    70
moel@40
    71
    private void SensorAdded(ISensor sensor) {
moel@40
    72
      if (Config.Get(sensor.Identifier + "/tray", false)) 
moel@42
    73
        Add(sensor, false);   
moel@40
    74
    }
moel@40
    75
moel@40
    76
    private void SensorRemoved(ISensor sensor) {
moel@42
    77
      if (Contains(sensor)) 
moel@42
    78
        Remove(sensor, false);
moel@40
    79
    }
moel@40
    80
moel@40
    81
    public void Dispose() {
moel@40
    82
      foreach (SensorNotifyIcon icon in list)
moel@40
    83
        icon.Dispose();
moel@40
    84
    }
moel@40
    85
moel@40
    86
    public void Redraw() {
moel@40
    87
      foreach (SensorNotifyIcon icon in list)
moel@40
    88
        icon.Update();
moel@40
    89
    }
moel@40
    90
moel@40
    91
    public bool Contains(ISensor sensor) {
moel@40
    92
      foreach (SensorNotifyIcon icon in list)
moel@40
    93
        if (icon.Sensor == sensor)
moel@40
    94
          return true;
moel@40
    95
      return false;
moel@40
    96
    }
moel@40
    97
moel@42
    98
    public void Add(ISensor sensor, bool balloonTip) {
moel@40
    99
      if (Contains(sensor)) {
moel@40
   100
        return;
moel@42
   101
      } else {        
moel@42
   102
        list.Add(new SensorNotifyIcon(this, sensor, balloonTip));
moel@40
   103
        Config.Set(sensor.Identifier + "/tray", true);
moel@40
   104
      }
moel@40
   105
    }
moel@40
   106
moel@40
   107
    public void Remove(ISensor sensor) {
moel@42
   108
      Remove(sensor, true);
moel@42
   109
    }
moel@42
   110
moel@42
   111
    private void Remove(ISensor sensor, bool deleteConfig) {
moel@42
   112
      if (deleteConfig) {
moel@42
   113
        Config.Remove(sensor.Identifier + "/tray");
moel@42
   114
        Config.Remove(sensor.Identifier + "/traycolor");
moel@42
   115
      }
moel@40
   116
      SensorNotifyIcon instance = null;
moel@40
   117
      foreach (SensorNotifyIcon icon in list)
moel@40
   118
        if (icon.Sensor == sensor)
moel@40
   119
          instance = icon;
moel@40
   120
      if (instance != null) {
moel@40
   121
        list.Remove(instance);
moel@40
   122
        instance.Dispose();
moel@40
   123
      }
moel@40
   124
    }
moel@40
   125
moel@40
   126
  }
moel@40
   127
}