GUI/SystemTray.cs
changeset 133 9ad699538c89
child 156 3e2ab626531c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GUI/SystemTray.cs	Sat Jun 05 18:59:54 2010 +0000
     1.3 @@ -0,0 +1,190 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.Drawing;
    1.44 +using System.Text;
    1.45 +using System.Windows.Forms;
    1.46 +using OpenHardwareMonitor.Hardware;
    1.47 +using OpenHardwareMonitor.Utilities;
    1.48 +
    1.49 +namespace OpenHardwareMonitor.GUI {
    1.50 +  public class SystemTray : IDisposable {
    1.51 +    private IComputer computer;
    1.52 +    private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
    1.53 +    private bool mainIconEnabled = false;
    1.54 +    private NotifyIcon mainIcon;
    1.55 +
    1.56 +    public SystemTray(IComputer computer) {
    1.57 +      this.computer = computer;
    1.58 +      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    1.59 +      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    1.60 +
    1.61 +      this.mainIcon = new NotifyIcon();
    1.62 +
    1.63 +      ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
    1.64 +      ToolStripMenuItem hideShowItem = new ToolStripMenuItem("Hide/Show");
    1.65 +      hideShowItem.Click += delegate(object obj, EventArgs args) {
    1.66 +        SendHideShowCommand();
    1.67 +      };
    1.68 +      contextMenuStrip.Items.Add(hideShowItem);
    1.69 +      contextMenuStrip.Items.Add(new ToolStripSeparator());      
    1.70 +      ToolStripMenuItem exitItem = new ToolStripMenuItem("Exit");
    1.71 +      exitItem.Click += delegate(object obj, EventArgs args) {
    1.72 +        SendExitCommand();
    1.73 +      };
    1.74 +      contextMenuStrip.Items.Add(exitItem);
    1.75 +      this.mainIcon.ContextMenuStrip = contextMenuStrip;
    1.76 +      this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
    1.77 +        SendHideShowCommand();
    1.78 +      };
    1.79 +      this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
    1.80 +    }
    1.81 +
    1.82 +    private void HardwareRemoved(IHardware hardware) {
    1.83 +      hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
    1.84 +      hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
    1.85 +      foreach (ISensor sensor in hardware.Sensors) 
    1.86 +        SensorRemoved(sensor);
    1.87 +      foreach (IHardware subHardware in hardware.SubHardware)
    1.88 +        HardwareRemoved(subHardware);
    1.89 +    }
    1.90 +
    1.91 +    private void HardwareAdded(IHardware hardware) {
    1.92 +      foreach (ISensor sensor in hardware.Sensors)
    1.93 +        SensorAdded(sensor);
    1.94 +      hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    1.95 +      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    1.96 +      foreach (IHardware subHardware in hardware.SubHardware)
    1.97 +        HardwareAdded(subHardware);
    1.98 +    }
    1.99 +
   1.100 +    private void SensorAdded(ISensor sensor) {
   1.101 +      if (Config.Get(new Identifier(sensor.Identifier, 
   1.102 +        "tray").ToString(), false)) 
   1.103 +        Add(sensor, false);   
   1.104 +    }
   1.105 +
   1.106 +    private void SensorRemoved(ISensor sensor) {
   1.107 +      if (Contains(sensor)) 
   1.108 +        Remove(sensor, false);
   1.109 +    }
   1.110 +
   1.111 +    public void Dispose() {
   1.112 +      foreach (SensorNotifyIcon icon in list)
   1.113 +        icon.Dispose();
   1.114 +      mainIcon.Dispose();
   1.115 +    }
   1.116 +
   1.117 +    public void Redraw() {
   1.118 +      foreach (SensorNotifyIcon icon in list)
   1.119 +        icon.Update();
   1.120 +    }
   1.121 +
   1.122 +    public bool Contains(ISensor sensor) {
   1.123 +      foreach (SensorNotifyIcon icon in list)
   1.124 +        if (icon.Sensor == sensor)
   1.125 +          return true;
   1.126 +      return false;
   1.127 +    }
   1.128 +
   1.129 +    public void Add(ISensor sensor, bool balloonTip) {
   1.130 +      if (Contains(sensor)) {
   1.131 +        return;
   1.132 +      } else {        
   1.133 +        list.Add(new SensorNotifyIcon(this, sensor, balloonTip));
   1.134 +        UpdateMainIconVisibilty();
   1.135 +        Config.Set(new Identifier(sensor.Identifier, "tray").ToString(), true);
   1.136 +      }
   1.137 +    }
   1.138 +
   1.139 +    public void Remove(ISensor sensor) {
   1.140 +      Remove(sensor, true);
   1.141 +    }
   1.142 +
   1.143 +    private void Remove(ISensor sensor, bool deleteConfig) {
   1.144 +      if (deleteConfig) {
   1.145 +        Config.Remove(
   1.146 +          new Identifier(sensor.Identifier, "tray").ToString());
   1.147 +        Config.Remove(
   1.148 +          new Identifier(sensor.Identifier, "traycolor").ToString());
   1.149 +      }
   1.150 +      SensorNotifyIcon instance = null;
   1.151 +      foreach (SensorNotifyIcon icon in list)
   1.152 +        if (icon.Sensor == sensor)
   1.153 +          instance = icon;
   1.154 +      if (instance != null) {
   1.155 +        list.Remove(instance);
   1.156 +        UpdateMainIconVisibilty();
   1.157 +        instance.Dispose();        
   1.158 +      }
   1.159 +    }
   1.160 +
   1.161 +    public event EventHandler HideShowCommand;
   1.162 +
   1.163 +    public void SendHideShowCommand() {
   1.164 +      if (HideShowCommand != null)
   1.165 +        HideShowCommand(this, null);
   1.166 +    }
   1.167 +
   1.168 +    public event EventHandler ExitCommand;
   1.169 +
   1.170 +    public void SendExitCommand() {
   1.171 +      if (ExitCommand != null)
   1.172 +        ExitCommand(this, null);
   1.173 +    }
   1.174 +
   1.175 +    private void UpdateMainIconVisibilty() {
   1.176 +      if (mainIconEnabled) {
   1.177 +        mainIcon.Visible = list.Count == 0;
   1.178 +      } else {
   1.179 +        mainIcon.Visible = false;
   1.180 +      }
   1.181 +    }
   1.182 +
   1.183 +    public bool IsMainIconEnabled {
   1.184 +      get { return mainIconEnabled; }
   1.185 +      set {
   1.186 +        if (mainIconEnabled != value) {
   1.187 +          mainIconEnabled = value;
   1.188 +          UpdateMainIconVisibilty();
   1.189 +        }
   1.190 +      }
   1.191 +    }
   1.192 +  }
   1.193 +}