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