GUI/SystemTray.cs
author StephaneLenclud
Mon, 04 Feb 2013 00:47:01 +0100
branchMiniDisplay
changeset 435 9ffcd8ed8537
parent 362 1dfe9dac1651
permissions -rw-r--r--
Now calling display init. Not getting the expected error code though.
moel@133
     1
/*
moel@133
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@133
     6
 
moel@362
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@133
     9
*/
moel@133
    10
moel@133
    11
using System;
moel@133
    12
using System.Collections.Generic;
moel@133
    13
using System.Drawing;
moel@133
    14
using System.Text;
moel@133
    15
using System.Windows.Forms;
moel@133
    16
using OpenHardwareMonitor.Hardware;
moel@133
    17
using OpenHardwareMonitor.Utilities;
moel@133
    18
moel@133
    19
namespace OpenHardwareMonitor.GUI {
moel@133
    20
  public class SystemTray : IDisposable {
moel@133
    21
    private IComputer computer;
moel@165
    22
    private PersistentSettings settings;
moel@362
    23
    private UnitManager unitManager;
moel@133
    24
    private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
moel@133
    25
    private bool mainIconEnabled = false;
moel@363
    26
    private NotifyIconAdv mainIcon;
moel@133
    27
moel@362
    28
    public SystemTray(IComputer computer, PersistentSettings settings,
moel@362
    29
      UnitManager unitManager) 
moel@362
    30
    {
moel@133
    31
      this.computer = computer;
moel@165
    32
      this.settings = settings;
moel@362
    33
      this.unitManager = unitManager;
moel@133
    34
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@133
    35
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
moel@133
    36
moel@363
    37
      this.mainIcon = new NotifyIconAdv();
moel@133
    38
moel@156
    39
      ContextMenu contextMenu = new ContextMenu();
moel@156
    40
      MenuItem hideShowItem = new MenuItem("Hide/Show");
moel@133
    41
      hideShowItem.Click += delegate(object obj, EventArgs args) {
moel@133
    42
        SendHideShowCommand();
moel@133
    43
      };
moel@156
    44
      contextMenu.MenuItems.Add(hideShowItem);
moel@156
    45
      contextMenu.MenuItems.Add(new MenuItem("-"));      
moel@156
    46
      MenuItem exitItem = new MenuItem("Exit");
moel@133
    47
      exitItem.Click += delegate(object obj, EventArgs args) {
moel@133
    48
        SendExitCommand();
moel@133
    49
      };
moel@156
    50
      contextMenu.MenuItems.Add(exitItem);
moel@156
    51
      this.mainIcon.ContextMenu = contextMenu;
moel@133
    52
      this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
moel@133
    53
        SendHideShowCommand();
moel@133
    54
      };
moel@133
    55
      this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
moel@303
    56
      this.mainIcon.Text = "Open Hardware Monitor";
moel@133
    57
    }
moel@133
    58
moel@133
    59
    private void HardwareRemoved(IHardware hardware) {
moel@133
    60
      hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
moel@133
    61
      hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
moel@133
    62
      foreach (ISensor sensor in hardware.Sensors) 
moel@133
    63
        SensorRemoved(sensor);
moel@133
    64
      foreach (IHardware subHardware in hardware.SubHardware)
moel@133
    65
        HardwareRemoved(subHardware);
moel@133
    66
    }
moel@133
    67
moel@133
    68
    private void HardwareAdded(IHardware hardware) {
moel@133
    69
      foreach (ISensor sensor in hardware.Sensors)
moel@133
    70
        SensorAdded(sensor);
moel@133
    71
      hardware.SensorAdded += new SensorEventHandler(SensorAdded);
moel@133
    72
      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
moel@133
    73
      foreach (IHardware subHardware in hardware.SubHardware)
moel@133
    74
        HardwareAdded(subHardware);
moel@133
    75
    }
moel@133
    76
moel@133
    77
    private void SensorAdded(ISensor sensor) {
moel@166
    78
      if (settings.GetValue(new Identifier(sensor.Identifier, 
moel@133
    79
        "tray").ToString(), false)) 
moel@133
    80
        Add(sensor, false);   
moel@133
    81
    }
moel@133
    82
moel@133
    83
    private void SensorRemoved(ISensor sensor) {
moel@133
    84
      if (Contains(sensor)) 
moel@133
    85
        Remove(sensor, false);
moel@133
    86
    }
moel@133
    87
moel@133
    88
    public void Dispose() {
moel@133
    89
      foreach (SensorNotifyIcon icon in list)
moel@133
    90
        icon.Dispose();
moel@133
    91
      mainIcon.Dispose();
moel@133
    92
    }
moel@133
    93
moel@133
    94
    public void Redraw() {
moel@133
    95
      foreach (SensorNotifyIcon icon in list)
moel@133
    96
        icon.Update();
moel@133
    97
    }
moel@133
    98
moel@133
    99
    public bool Contains(ISensor sensor) {
moel@133
   100
      foreach (SensorNotifyIcon icon in list)
moel@133
   101
        if (icon.Sensor == sensor)
moel@133
   102
          return true;
moel@133
   103
      return false;
moel@133
   104
    }
moel@133
   105
moel@133
   106
    public void Add(ISensor sensor, bool balloonTip) {
moel@133
   107
      if (Contains(sensor)) {
moel@133
   108
        return;
moel@133
   109
      } else {        
moel@362
   110
        list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings, unitManager));
moel@133
   111
        UpdateMainIconVisibilty();
moel@166
   112
        settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
moel@133
   113
      }
moel@133
   114
    }
moel@133
   115
moel@133
   116
    public void Remove(ISensor sensor) {
moel@133
   117
      Remove(sensor, true);
moel@133
   118
    }
moel@133
   119
moel@133
   120
    private void Remove(ISensor sensor, bool deleteConfig) {
moel@133
   121
      if (deleteConfig) {
moel@165
   122
        settings.Remove(
moel@133
   123
          new Identifier(sensor.Identifier, "tray").ToString());
moel@165
   124
        settings.Remove(
moel@133
   125
          new Identifier(sensor.Identifier, "traycolor").ToString());
moel@133
   126
      }
moel@133
   127
      SensorNotifyIcon instance = null;
moel@133
   128
      foreach (SensorNotifyIcon icon in list)
moel@133
   129
        if (icon.Sensor == sensor)
moel@133
   130
          instance = icon;
moel@133
   131
      if (instance != null) {
moel@133
   132
        list.Remove(instance);
moel@133
   133
        UpdateMainIconVisibilty();
moel@133
   134
        instance.Dispose();        
moel@133
   135
      }
moel@133
   136
    }
moel@133
   137
moel@133
   138
    public event EventHandler HideShowCommand;
moel@133
   139
moel@133
   140
    public void SendHideShowCommand() {
moel@133
   141
      if (HideShowCommand != null)
moel@133
   142
        HideShowCommand(this, null);
moel@133
   143
    }
moel@133
   144
moel@133
   145
    public event EventHandler ExitCommand;
moel@133
   146
moel@133
   147
    public void SendExitCommand() {
moel@133
   148
      if (ExitCommand != null)
moel@133
   149
        ExitCommand(this, null);
moel@133
   150
    }
moel@133
   151
moel@133
   152
    private void UpdateMainIconVisibilty() {
moel@133
   153
      if (mainIconEnabled) {
moel@133
   154
        mainIcon.Visible = list.Count == 0;
moel@133
   155
      } else {
moel@133
   156
        mainIcon.Visible = false;
moel@133
   157
      }
moel@133
   158
    }
moel@133
   159
moel@133
   160
    public bool IsMainIconEnabled {
moel@133
   161
      get { return mainIconEnabled; }
moel@133
   162
      set {
moel@133
   163
        if (mainIconEnabled != value) {
moel@133
   164
          mainIconEnabled = value;
moel@133
   165
          UpdateMainIconVisibilty();
moel@133
   166
        }
moel@133
   167
      }
moel@133
   168
    }
moel@133
   169
  }
moel@133
   170
}