GUI/SystemTray.cs
author moel.mich
Sat, 19 Mar 2011 22:55:05 +0000
changeset 265 961c07a3bd78
parent 165 813d8bc3192f
child 303 f6db7258890e
permissions -rw-r--r--
Fixed Issue 156.
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@133
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
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@133
    79
    }
moel@133
    80
moel@133
    81
    private void HardwareRemoved(IHardware hardware) {
moel@133
    82
      hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
moel@133
    83
      hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
moel@133
    84
      foreach (ISensor sensor in hardware.Sensors) 
moel@133
    85
        SensorRemoved(sensor);
moel@133
    86
      foreach (IHardware subHardware in hardware.SubHardware)
moel@133
    87
        HardwareRemoved(subHardware);
moel@133
    88
    }
moel@133
    89
moel@133
    90
    private void HardwareAdded(IHardware hardware) {
moel@133
    91
      foreach (ISensor sensor in hardware.Sensors)
moel@133
    92
        SensorAdded(sensor);
moel@133
    93
      hardware.SensorAdded += new SensorEventHandler(SensorAdded);
moel@133
    94
      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
moel@133
    95
      foreach (IHardware subHardware in hardware.SubHardware)
moel@133
    96
        HardwareAdded(subHardware);
moel@133
    97
    }
moel@133
    98
moel@133
    99
    private void SensorAdded(ISensor sensor) {
moel@166
   100
      if (settings.GetValue(new Identifier(sensor.Identifier, 
moel@133
   101
        "tray").ToString(), false)) 
moel@133
   102
        Add(sensor, false);   
moel@133
   103
    }
moel@133
   104
moel@133
   105
    private void SensorRemoved(ISensor sensor) {
moel@133
   106
      if (Contains(sensor)) 
moel@133
   107
        Remove(sensor, false);
moel@133
   108
    }
moel@133
   109
moel@133
   110
    public void Dispose() {
moel@133
   111
      foreach (SensorNotifyIcon icon in list)
moel@133
   112
        icon.Dispose();
moel@133
   113
      mainIcon.Dispose();
moel@133
   114
    }
moel@133
   115
moel@133
   116
    public void Redraw() {
moel@133
   117
      foreach (SensorNotifyIcon icon in list)
moel@133
   118
        icon.Update();
moel@133
   119
    }
moel@133
   120
moel@133
   121
    public bool Contains(ISensor sensor) {
moel@133
   122
      foreach (SensorNotifyIcon icon in list)
moel@133
   123
        if (icon.Sensor == sensor)
moel@133
   124
          return true;
moel@133
   125
      return false;
moel@133
   126
    }
moel@133
   127
moel@133
   128
    public void Add(ISensor sensor, bool balloonTip) {
moel@133
   129
      if (Contains(sensor)) {
moel@133
   130
        return;
moel@133
   131
      } else {        
moel@165
   132
        list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings));
moel@133
   133
        UpdateMainIconVisibilty();
moel@166
   134
        settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
moel@133
   135
      }
moel@133
   136
    }
moel@133
   137
moel@133
   138
    public void Remove(ISensor sensor) {
moel@133
   139
      Remove(sensor, true);
moel@133
   140
    }
moel@133
   141
moel@133
   142
    private void Remove(ISensor sensor, bool deleteConfig) {
moel@133
   143
      if (deleteConfig) {
moel@165
   144
        settings.Remove(
moel@133
   145
          new Identifier(sensor.Identifier, "tray").ToString());
moel@165
   146
        settings.Remove(
moel@133
   147
          new Identifier(sensor.Identifier, "traycolor").ToString());
moel@133
   148
      }
moel@133
   149
      SensorNotifyIcon instance = null;
moel@133
   150
      foreach (SensorNotifyIcon icon in list)
moel@133
   151
        if (icon.Sensor == sensor)
moel@133
   152
          instance = icon;
moel@133
   153
      if (instance != null) {
moel@133
   154
        list.Remove(instance);
moel@133
   155
        UpdateMainIconVisibilty();
moel@133
   156
        instance.Dispose();        
moel@133
   157
      }
moel@133
   158
    }
moel@133
   159
moel@133
   160
    public event EventHandler HideShowCommand;
moel@133
   161
moel@133
   162
    public void SendHideShowCommand() {
moel@133
   163
      if (HideShowCommand != null)
moel@133
   164
        HideShowCommand(this, null);
moel@133
   165
    }
moel@133
   166
moel@133
   167
    public event EventHandler ExitCommand;
moel@133
   168
moel@133
   169
    public void SendExitCommand() {
moel@133
   170
      if (ExitCommand != null)
moel@133
   171
        ExitCommand(this, null);
moel@133
   172
    }
moel@133
   173
moel@133
   174
    private void UpdateMainIconVisibilty() {
moel@133
   175
      if (mainIconEnabled) {
moel@133
   176
        mainIcon.Visible = list.Count == 0;
moel@133
   177
      } else {
moel@133
   178
        mainIcon.Visible = false;
moel@133
   179
      }
moel@133
   180
    }
moel@133
   181
moel@133
   182
    public bool IsMainIconEnabled {
moel@133
   183
      get { return mainIconEnabled; }
moel@133
   184
      set {
moel@133
   185
        if (mainIconEnabled != value) {
moel@133
   186
          mainIconEnabled = value;
moel@133
   187
          UpdateMainIconVisibilty();
moel@133
   188
        }
moel@133
   189
      }
moel@133
   190
    }
moel@133
   191
  }
moel@133
   192
}