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