GUI/SystemTray.cs
author moel.mich
Sun, 01 Jan 2012 17:12:34 +0000
changeset 327 e837e1e4b282
parent 166 fa9dfbfc4145
child 344 3145aadca3d2
permissions -rw-r--r--
Added code to save and restore the plot sensor selection.
moel@133
     1
/*
moel@133
     2
  
moel@133
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@133
     4
moel@133
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@133
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@133
     7
  the License. You may obtain a copy of the License at
moel@133
     8
 
moel@133
     9
  http://www.mozilla.org/MPL/
moel@133
    10
moel@133
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@133
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@133
    13
  for the specific language governing rights and limitations under the License.
moel@133
    14
moel@133
    15
  The Original Code is the Open Hardware Monitor code.
moel@133
    16
moel@133
    17
  The Initial Developer of the Original Code is 
moel@133
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@303
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2011
moel@133
    20
  the Initial Developer. All Rights Reserved.
moel@133
    21
moel@133
    22
  Contributor(s):
moel@133
    23
moel@133
    24
  Alternatively, the contents of this file may be used under the terms of
moel@133
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@133
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@133
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@133
    28
  of those above. If you wish to allow use of your version of this file only
moel@133
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@133
    30
  use your version of this file under the terms of the MPL, indicate your
moel@133
    31
  decision by deleting the provisions above and replace them with the notice
moel@133
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@133
    33
  the provisions above, a recipient may use your version of this file under
moel@133
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@133
    35
 
moel@133
    36
*/
moel@133
    37
moel@133
    38
using System;
moel@133
    39
using System.Collections.Generic;
moel@133
    40
using System.Drawing;
moel@133
    41
using System.Text;
moel@133
    42
using System.Windows.Forms;
moel@133
    43
using OpenHardwareMonitor.Hardware;
moel@133
    44
using OpenHardwareMonitor.Utilities;
moel@133
    45
moel@133
    46
namespace OpenHardwareMonitor.GUI {
moel@133
    47
  public class SystemTray : IDisposable {
moel@133
    48
    private IComputer computer;
moel@165
    49
    private PersistentSettings settings;
moel@133
    50
    private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
moel@133
    51
    private bool mainIconEnabled = false;
moel@133
    52
    private NotifyIcon mainIcon;
moel@133
    53
moel@165
    54
    public SystemTray(IComputer computer, PersistentSettings settings) {
moel@133
    55
      this.computer = computer;
moel@165
    56
      this.settings = settings;
moel@133
    57
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@133
    58
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
moel@133
    59
moel@133
    60
      this.mainIcon = new NotifyIcon();
moel@133
    61
moel@156
    62
      ContextMenu contextMenu = new ContextMenu();
moel@156
    63
      MenuItem hideShowItem = new MenuItem("Hide/Show");
moel@133
    64
      hideShowItem.Click += delegate(object obj, EventArgs args) {
moel@133
    65
        SendHideShowCommand();
moel@133
    66
      };
moel@156
    67
      contextMenu.MenuItems.Add(hideShowItem);
moel@156
    68
      contextMenu.MenuItems.Add(new MenuItem("-"));      
moel@156
    69
      MenuItem exitItem = new MenuItem("Exit");
moel@133
    70
      exitItem.Click += delegate(object obj, EventArgs args) {
moel@133
    71
        SendExitCommand();
moel@133
    72
      };
moel@156
    73
      contextMenu.MenuItems.Add(exitItem);
moel@156
    74
      this.mainIcon.ContextMenu = contextMenu;
moel@133
    75
      this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
moel@133
    76
        SendHideShowCommand();
moel@133
    77
      };
moel@133
    78
      this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
moel@303
    79
      this.mainIcon.Text = "Open Hardware Monitor";
moel@133
    80
    }
moel@133
    81
moel@133
    82
    private void HardwareRemoved(IHardware hardware) {
moel@133
    83
      hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
moel@133
    84
      hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
moel@133
    85
      foreach (ISensor sensor in hardware.Sensors) 
moel@133
    86
        SensorRemoved(sensor);
moel@133
    87
      foreach (IHardware subHardware in hardware.SubHardware)
moel@133
    88
        HardwareRemoved(subHardware);
moel@133
    89
    }
moel@133
    90
moel@133
    91
    private void HardwareAdded(IHardware hardware) {
moel@133
    92
      foreach (ISensor sensor in hardware.Sensors)
moel@133
    93
        SensorAdded(sensor);
moel@133
    94
      hardware.SensorAdded += new SensorEventHandler(SensorAdded);
moel@133
    95
      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
moel@133
    96
      foreach (IHardware subHardware in hardware.SubHardware)
moel@133
    97
        HardwareAdded(subHardware);
moel@133
    98
    }
moel@133
    99
moel@133
   100
    private void SensorAdded(ISensor sensor) {
moel@166
   101
      if (settings.GetValue(new Identifier(sensor.Identifier, 
moel@133
   102
        "tray").ToString(), false)) 
moel@133
   103
        Add(sensor, false);   
moel@133
   104
    }
moel@133
   105
moel@133
   106
    private void SensorRemoved(ISensor sensor) {
moel@133
   107
      if (Contains(sensor)) 
moel@133
   108
        Remove(sensor, false);
moel@133
   109
    }
moel@133
   110
moel@133
   111
    public void Dispose() {
moel@133
   112
      foreach (SensorNotifyIcon icon in list)
moel@133
   113
        icon.Dispose();
moel@133
   114
      mainIcon.Dispose();
moel@133
   115
    }
moel@133
   116
moel@133
   117
    public void Redraw() {
moel@133
   118
      foreach (SensorNotifyIcon icon in list)
moel@133
   119
        icon.Update();
moel@133
   120
    }
moel@133
   121
moel@133
   122
    public bool Contains(ISensor sensor) {
moel@133
   123
      foreach (SensorNotifyIcon icon in list)
moel@133
   124
        if (icon.Sensor == sensor)
moel@133
   125
          return true;
moel@133
   126
      return false;
moel@133
   127
    }
moel@133
   128
moel@133
   129
    public void Add(ISensor sensor, bool balloonTip) {
moel@133
   130
      if (Contains(sensor)) {
moel@133
   131
        return;
moel@133
   132
      } else {        
moel@165
   133
        list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings));
moel@133
   134
        UpdateMainIconVisibilty();
moel@166
   135
        settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
moel@133
   136
      }
moel@133
   137
    }
moel@133
   138
moel@133
   139
    public void Remove(ISensor sensor) {
moel@133
   140
      Remove(sensor, true);
moel@133
   141
    }
moel@133
   142
moel@133
   143
    private void Remove(ISensor sensor, bool deleteConfig) {
moel@133
   144
      if (deleteConfig) {
moel@165
   145
        settings.Remove(
moel@133
   146
          new Identifier(sensor.Identifier, "tray").ToString());
moel@165
   147
        settings.Remove(
moel@133
   148
          new Identifier(sensor.Identifier, "traycolor").ToString());
moel@133
   149
      }
moel@133
   150
      SensorNotifyIcon instance = null;
moel@133
   151
      foreach (SensorNotifyIcon icon in list)
moel@133
   152
        if (icon.Sensor == sensor)
moel@133
   153
          instance = icon;
moel@133
   154
      if (instance != null) {
moel@133
   155
        list.Remove(instance);
moel@133
   156
        UpdateMainIconVisibilty();
moel@133
   157
        instance.Dispose();        
moel@133
   158
      }
moel@133
   159
    }
moel@133
   160
moel@133
   161
    public event EventHandler HideShowCommand;
moel@133
   162
moel@133
   163
    public void SendHideShowCommand() {
moel@133
   164
      if (HideShowCommand != null)
moel@133
   165
        HideShowCommand(this, null);
moel@133
   166
    }
moel@133
   167
moel@133
   168
    public event EventHandler ExitCommand;
moel@133
   169
moel@133
   170
    public void SendExitCommand() {
moel@133
   171
      if (ExitCommand != null)
moel@133
   172
        ExitCommand(this, null);
moel@133
   173
    }
moel@133
   174
moel@133
   175
    private void UpdateMainIconVisibilty() {
moel@133
   176
      if (mainIconEnabled) {
moel@133
   177
        mainIcon.Visible = list.Count == 0;
moel@133
   178
      } else {
moel@133
   179
        mainIcon.Visible = false;
moel@133
   180
      }
moel@133
   181
    }
moel@133
   182
moel@133
   183
    public bool IsMainIconEnabled {
moel@133
   184
      get { return mainIconEnabled; }
moel@133
   185
      set {
moel@133
   186
        if (mainIconEnabled != value) {
moel@133
   187
          mainIconEnabled = value;
moel@133
   188
          UpdateMainIconVisibilty();
moel@133
   189
        }
moel@133
   190
      }
moel@133
   191
    }
moel@133
   192
  }
moel@133
   193
}