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