GUI/SystemTray.cs
author moel.mich
Sat, 17 Jul 2010 13:25:42 +0000
changeset 155 8ffea928daad
child 156 3e2ab626531c
permissions -rw-r--r--
Fixed Issue 91.
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@133
    49
    private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
moel@133
    50
    private bool mainIconEnabled = false;
moel@133
    51
    private NotifyIcon mainIcon;
moel@133
    52
moel@133
    53
    public SystemTray(IComputer computer) {
moel@133
    54
      this.computer = computer;
moel@133
    55
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@133
    56
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
moel@133
    57
moel@133
    58
      this.mainIcon = new NotifyIcon();
moel@133
    59
moel@133
    60
      ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
moel@133
    61
      ToolStripMenuItem hideShowItem = new ToolStripMenuItem("Hide/Show");
moel@133
    62
      hideShowItem.Click += delegate(object obj, EventArgs args) {
moel@133
    63
        SendHideShowCommand();
moel@133
    64
      };
moel@133
    65
      contextMenuStrip.Items.Add(hideShowItem);
moel@133
    66
      contextMenuStrip.Items.Add(new ToolStripSeparator());      
moel@133
    67
      ToolStripMenuItem exitItem = new ToolStripMenuItem("Exit");
moel@133
    68
      exitItem.Click += delegate(object obj, EventArgs args) {
moel@133
    69
        SendExitCommand();
moel@133
    70
      };
moel@133
    71
      contextMenuStrip.Items.Add(exitItem);
moel@133
    72
      this.mainIcon.ContextMenuStrip = contextMenuStrip;
moel@133
    73
      this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
moel@133
    74
        SendHideShowCommand();
moel@133
    75
      };
moel@133
    76
      this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
moel@133
    77
    }
moel@133
    78
moel@133
    79
    private void HardwareRemoved(IHardware hardware) {
moel@133
    80
      hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
moel@133
    81
      hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
moel@133
    82
      foreach (ISensor sensor in hardware.Sensors) 
moel@133
    83
        SensorRemoved(sensor);
moel@133
    84
      foreach (IHardware subHardware in hardware.SubHardware)
moel@133
    85
        HardwareRemoved(subHardware);
moel@133
    86
    }
moel@133
    87
moel@133
    88
    private void HardwareAdded(IHardware hardware) {
moel@133
    89
      foreach (ISensor sensor in hardware.Sensors)
moel@133
    90
        SensorAdded(sensor);
moel@133
    91
      hardware.SensorAdded += new SensorEventHandler(SensorAdded);
moel@133
    92
      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
moel@133
    93
      foreach (IHardware subHardware in hardware.SubHardware)
moel@133
    94
        HardwareAdded(subHardware);
moel@133
    95
    }
moel@133
    96
moel@133
    97
    private void SensorAdded(ISensor sensor) {
moel@133
    98
      if (Config.Get(new Identifier(sensor.Identifier, 
moel@133
    99
        "tray").ToString(), false)) 
moel@133
   100
        Add(sensor, false);   
moel@133
   101
    }
moel@133
   102
moel@133
   103
    private void SensorRemoved(ISensor sensor) {
moel@133
   104
      if (Contains(sensor)) 
moel@133
   105
        Remove(sensor, false);
moel@133
   106
    }
moel@133
   107
moel@133
   108
    public void Dispose() {
moel@133
   109
      foreach (SensorNotifyIcon icon in list)
moel@133
   110
        icon.Dispose();
moel@133
   111
      mainIcon.Dispose();
moel@133
   112
    }
moel@133
   113
moel@133
   114
    public void Redraw() {
moel@133
   115
      foreach (SensorNotifyIcon icon in list)
moel@133
   116
        icon.Update();
moel@133
   117
    }
moel@133
   118
moel@133
   119
    public bool Contains(ISensor sensor) {
moel@133
   120
      foreach (SensorNotifyIcon icon in list)
moel@133
   121
        if (icon.Sensor == sensor)
moel@133
   122
          return true;
moel@133
   123
      return false;
moel@133
   124
    }
moel@133
   125
moel@133
   126
    public void Add(ISensor sensor, bool balloonTip) {
moel@133
   127
      if (Contains(sensor)) {
moel@133
   128
        return;
moel@133
   129
      } else {        
moel@133
   130
        list.Add(new SensorNotifyIcon(this, sensor, balloonTip));
moel@133
   131
        UpdateMainIconVisibilty();
moel@133
   132
        Config.Set(new Identifier(sensor.Identifier, "tray").ToString(), true);
moel@133
   133
      }
moel@133
   134
    }
moel@133
   135
moel@133
   136
    public void Remove(ISensor sensor) {
moel@133
   137
      Remove(sensor, true);
moel@133
   138
    }
moel@133
   139
moel@133
   140
    private void Remove(ISensor sensor, bool deleteConfig) {
moel@133
   141
      if (deleteConfig) {
moel@133
   142
        Config.Remove(
moel@133
   143
          new Identifier(sensor.Identifier, "tray").ToString());
moel@133
   144
        Config.Remove(
moel@133
   145
          new Identifier(sensor.Identifier, "traycolor").ToString());
moel@133
   146
      }
moel@133
   147
      SensorNotifyIcon instance = null;
moel@133
   148
      foreach (SensorNotifyIcon icon in list)
moel@133
   149
        if (icon.Sensor == sensor)
moel@133
   150
          instance = icon;
moel@133
   151
      if (instance != null) {
moel@133
   152
        list.Remove(instance);
moel@133
   153
        UpdateMainIconVisibilty();
moel@133
   154
        instance.Dispose();        
moel@133
   155
      }
moel@133
   156
    }
moel@133
   157
moel@133
   158
    public event EventHandler HideShowCommand;
moel@133
   159
moel@133
   160
    public void SendHideShowCommand() {
moel@133
   161
      if (HideShowCommand != null)
moel@133
   162
        HideShowCommand(this, null);
moel@133
   163
    }
moel@133
   164
moel@133
   165
    public event EventHandler ExitCommand;
moel@133
   166
moel@133
   167
    public void SendExitCommand() {
moel@133
   168
      if (ExitCommand != null)
moel@133
   169
        ExitCommand(this, null);
moel@133
   170
    }
moel@133
   171
moel@133
   172
    private void UpdateMainIconVisibilty() {
moel@133
   173
      if (mainIconEnabled) {
moel@133
   174
        mainIcon.Visible = list.Count == 0;
moel@133
   175
      } else {
moel@133
   176
        mainIcon.Visible = false;
moel@133
   177
      }
moel@133
   178
    }
moel@133
   179
moel@133
   180
    public bool IsMainIconEnabled {
moel@133
   181
      get { return mainIconEnabled; }
moel@133
   182
      set {
moel@133
   183
        if (mainIconEnabled != value) {
moel@133
   184
          mainIconEnabled = value;
moel@133
   185
          UpdateMainIconVisibilty();
moel@133
   186
        }
moel@133
   187
      }
moel@133
   188
    }
moel@133
   189
  }
moel@133
   190
}