GUI/SensorNotifyIcon.cs
author moel.mich
Sat, 17 Jul 2010 13:25:42 +0000
changeset 155 8ffea928daad
parent 118 407f98562c3b
child 156 3e2ab626531c
permissions -rw-r--r--
Fixed Issue 91.
moel@40
     1
/*
moel@40
     2
  
moel@40
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@40
     4
moel@40
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@40
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@40
     7
  the License. You may obtain a copy of the License at
moel@40
     8
 
moel@40
     9
  http://www.mozilla.org/MPL/
moel@40
    10
moel@40
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@40
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@40
    13
  for the specific language governing rights and limitations under the License.
moel@40
    14
moel@40
    15
  The Original Code is the Open Hardware Monitor code.
moel@40
    16
moel@40
    17
  The Initial Developer of the Original Code is 
moel@40
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@40
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@40
    20
  the Initial Developer. All Rights Reserved.
moel@40
    21
moel@40
    22
  Contributor(s):
moel@40
    23
moel@40
    24
  Alternatively, the contents of this file may be used under the terms of
moel@40
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@40
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@40
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@40
    28
  of those above. If you wish to allow use of your version of this file only
moel@40
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@40
    30
  use your version of this file under the terms of the MPL, indicate your
moel@40
    31
  decision by deleting the provisions above and replace them with the notice
moel@40
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@40
    33
  the provisions above, a recipient may use your version of this file under
moel@40
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@40
    35
 
moel@40
    36
*/
moel@40
    37
moel@40
    38
using System;
moel@40
    39
using System.Collections.Generic;
moel@40
    40
using System.Drawing;
moel@40
    41
using System.Drawing.Drawing2D;
moel@40
    42
using System.Drawing.Imaging;
moel@40
    43
using System.Drawing.Text;
moel@40
    44
using System.Text;
moel@40
    45
using System.Windows.Forms;
moel@40
    46
using System.Runtime.InteropServices;
moel@40
    47
using OpenHardwareMonitor.Hardware;
moel@40
    48
using OpenHardwareMonitor.Utilities;
moel@40
    49
moel@40
    50
namespace OpenHardwareMonitor.GUI {
moel@40
    51
  public class SensorNotifyIcon : IDisposable {
moel@40
    52
moel@40
    53
    private ISensor sensor;
moel@40
    54
    private NotifyIcon notifyIcon;
moel@40
    55
    private Bitmap bitmap;
moel@40
    56
    private Graphics graphics;
moel@42
    57
    private Color color;
moel@43
    58
    private Color darkColor;
moel@43
    59
    private Brush brush;
moel@43
    60
    private Brush darkBrush;
moel@43
    61
    private Pen pen;
moel@70
    62
    private Font font;
moel@40
    63
moel@133
    64
    public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
moel@42
    65
      bool balloonTip) 
moel@42
    66
    {
moel@40
    67
      this.sensor = sensor;
moel@40
    68
      this.notifyIcon = new NotifyIcon();
moel@43
    69
moel@43
    70
      Color defaultColor = Color.Black;
moel@43
    71
      if (sensor.SensorType == SensorType.Load) {
moel@43
    72
        defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
moel@43
    73
      }
moel@109
    74
      Color = Config.Get(new Identifier(sensor.Identifier, 
moel@109
    75
        "traycolor").ToString(), defaultColor);      
moel@40
    76
      
moel@43
    77
      this.pen = new Pen(Color.FromArgb(96, Color.Black));
moel@117
    78
      this.font = SystemFonts.MessageBoxFont;
moel@43
    79
moel@40
    80
      ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
moel@133
    81
      ToolStripMenuItem hideShowItem = new ToolStripMenuItem("Hide/Show");
moel@133
    82
      hideShowItem.Click += delegate(object obj, EventArgs args) {
moel@133
    83
        sensorSystemTray.SendHideShowCommand();
moel@133
    84
      };
moel@133
    85
      contextMenuStrip.Items.Add(hideShowItem);
moel@133
    86
      contextMenuStrip.Items.Add(new ToolStripSeparator());
moel@133
    87
      ToolStripMenuItem removeItem = new ToolStripMenuItem("Remove Sensor");
moel@42
    88
      removeItem.Click += delegate(object obj, EventArgs args) {
moel@42
    89
        sensorSystemTray.Remove(this.sensor);
moel@40
    90
      };
moel@42
    91
      contextMenuStrip.Items.Add(removeItem);
moel@42
    92
      ToolStripMenuItem colorItem = new ToolStripMenuItem("Change Color...");
moel@42
    93
      colorItem.Click += delegate(object obj, EventArgs args) {
moel@42
    94
        ColorDialog dialog = new ColorDialog();
moel@43
    95
        dialog.Color = Color;
moel@42
    96
        if (dialog.ShowDialog() == DialogResult.OK) {
moel@43
    97
          Color = dialog.Color;
moel@109
    98
          Config.Set(new Identifier(sensor.Identifier,
moel@109
    99
            "traycolor").ToString(), Color);
moel@42
   100
        }
moel@42
   101
      };
moel@42
   102
      contextMenuStrip.Items.Add(colorItem);
moel@133
   103
      contextMenuStrip.Items.Add(new ToolStripSeparator());
moel@133
   104
      ToolStripMenuItem exitItem = new ToolStripMenuItem("Exit");
moel@133
   105
      exitItem.Click += delegate(object obj, EventArgs args) {
moel@133
   106
        sensorSystemTray.SendExitCommand();
moel@133
   107
      };
moel@133
   108
      contextMenuStrip.Items.Add(exitItem);
moel@40
   109
      this.notifyIcon.ContextMenuStrip = contextMenuStrip;
moel@133
   110
      this.notifyIcon.DoubleClick += delegate(object obj, EventArgs args) {
moel@133
   111
        sensorSystemTray.SendHideShowCommand();
moel@133
   112
      };
moel@40
   113
moel@40
   114
      this.bitmap = new Bitmap(16, 16, PixelFormat.Format32bppArgb);
moel@40
   115
      this.graphics = Graphics.FromImage(this.bitmap);
moel@117
   116
moel@117
   117
      if (Environment.OSVersion.Version.Major > 5) {
moel@117
   118
        this.graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
moel@117
   119
        this.graphics.SmoothingMode = SmoothingMode.HighQuality;
moel@117
   120
      }
moel@40
   121
    }
moel@40
   122
moel@40
   123
    public ISensor Sensor {
moel@40
   124
      get { return sensor; }
moel@40
   125
    }
moel@40
   126
moel@42
   127
    public Color Color {
moel@42
   128
      get { return color; }
moel@43
   129
      set { 
moel@43
   130
        this.color = value;
moel@43
   131
        this.darkColor = Color.FromArgb(255,
moel@44
   132
          this.color.R / 3,
moel@44
   133
          this.color.G / 3,
moel@44
   134
          this.color.B / 3);
moel@43
   135
        Brush brush = this.brush;
moel@43
   136
        this.brush = new SolidBrush(this.color);
moel@43
   137
        if (brush != null)
moel@43
   138
          brush.Dispose();
moel@43
   139
        Brush darkBrush = this.darkBrush;
moel@43
   140
        this.darkBrush = new SolidBrush(this.darkColor);
moel@43
   141
        if (darkBrush != null)
moel@43
   142
          darkBrush.Dispose();
moel@43
   143
      }
moel@42
   144
    }
moel@42
   145
moel@40
   146
    public void Dispose() {      
moel@40
   147
      Icon icon = notifyIcon.Icon;
moel@40
   148
      notifyIcon.Icon = null;
moel@40
   149
      if (icon != null)
moel@40
   150
        icon.Dispose();      
moel@40
   151
      notifyIcon.Dispose();
moel@40
   152
moel@43
   153
      if (brush != null)
moel@43
   154
        brush.Dispose();
moel@43
   155
      if (darkBrush != null)
moel@43
   156
        darkBrush.Dispose();
moel@43
   157
      pen.Dispose();
moel@85
   158
      graphics.Dispose();      
moel@85
   159
      bitmap.Dispose();      
moel@40
   160
    }
moel@40
   161
moel@40
   162
    private string GetString() {
moel@40
   163
      switch (sensor.SensorType) {
moel@40
   164
        case SensorType.Voltage:
moel@40
   165
          return string.Format("{0:F11}", sensor.Value);
moel@40
   166
        case SensorType.Clock:
moel@40
   167
          return string.Format("{0:F11}", 1e-3f * sensor.Value);
moel@40
   168
        case SensorType.Load: 
moel@118
   169
          return string.Format("{0:F0}", sensor.Value);
moel@40
   170
        case SensorType.Temperature: 
moel@40
   171
          return string.Format("{0:F0}", sensor.Value);
moel@40
   172
        case SensorType.Fan: 
moel@40
   173
          return string.Format("{0:F11}", 1e-3f * sensor.Value);
moel@57
   174
        case SensorType.Flow:
moel@57
   175
          return string.Format("{0:F11}", 1e-3f * sensor.Value);
moel@118
   176
        case SensorType.Control:
moel@118
   177
          return string.Format("{0:F0}", sensor.Value);
moel@40
   178
      }
moel@40
   179
      return "-";
moel@40
   180
    }
moel@40
   181
moel@40
   182
    private Icon CreateTransparentIcon() {
moel@40
   183
moel@40
   184
      graphics.Clear(Color.Black);
moel@70
   185
      TextRenderer.DrawText(graphics, GetString(), font,
moel@117
   186
        new Point(-2, 0), Color.White, Color.Black);        
moel@40
   187
moel@40
   188
      BitmapData data = bitmap.LockBits(
moel@40
   189
        new Rectangle(0, 0, bitmap.Width, bitmap.Height),
moel@40
   190
        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
moel@40
   191
moel@40
   192
      int stride = data.Stride;
moel@40
   193
      IntPtr Scan0 = data.Scan0;
moel@40
   194
moel@40
   195
      int numBytes = bitmap.Width * bitmap.Height * 4;
moel@40
   196
      byte[] bytes = new byte[numBytes];
moel@40
   197
      Marshal.Copy(Scan0, bytes, 0, numBytes);
moel@40
   198
      bitmap.UnlockBits(data);
moel@40
   199
moel@40
   200
      byte red, green, blue;
moel@40
   201
      for (int i = 0; i < bytes.Length; i += 4) {
moel@40
   202
        blue = bytes[i];
moel@40
   203
        green = bytes[i + 1];
moel@40
   204
        red = bytes[i + 2];
moel@40
   205
moel@42
   206
        bytes[i] = color.B;
moel@42
   207
        bytes[i + 1] = color.G;
moel@42
   208
        bytes[i + 2] = color.R;
moel@40
   209
        bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
moel@40
   210
      }
moel@40
   211
moel@40
   212
      return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
moel@40
   213
    }
moel@40
   214
moel@86
   215
    private Icon CreateLoadIcon() {      
moel@86
   216
      try {
moel@86
   217
        graphics.Clear(Color.Transparent);
moel@86
   218
      } catch (ArgumentException) {
moel@86
   219
        graphics.Clear(Color.Black);
moel@86
   220
      }
moel@43
   221
      graphics.FillRectangle(darkBrush, 0.5f, -0.5f, 14, 16);
moel@43
   222
      float y = 0.16f * (100 - sensor.Value.Value);
moel@43
   223
      graphics.FillRectangle(brush, 0.5f, -0.5f + y, 14, 16 - y);
moel@43
   224
      graphics.DrawRectangle(pen, 1, 0, 13, 15);
moel@43
   225
moel@43
   226
      BitmapData data = bitmap.LockBits(
moel@43
   227
        new Rectangle(0, 0, bitmap.Width, bitmap.Height),
moel@43
   228
        ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
moel@43
   229
      byte[] bytes = new byte[bitmap.Width * bitmap.Height * 4];
moel@43
   230
      Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
moel@43
   231
      bitmap.UnlockBits(data);
moel@43
   232
moel@43
   233
      return IconFactory.Create(bytes, 16, 16, PixelFormat.Format32bppArgb);
moel@43
   234
    }
moel@43
   235
moel@40
   236
    public void Update() {
moel@40
   237
      Icon icon = notifyIcon.Icon;
moel@40
   238
moel@43
   239
      if (sensor.SensorType == SensorType.Load) {
moel@43
   240
        notifyIcon.Icon = CreateLoadIcon();
moel@40
   241
      } else {
moel@40
   242
        notifyIcon.Icon = CreateTransparentIcon();
moel@40
   243
      }
moel@40
   244
      if (icon != null) 
moel@40
   245
        icon.Dispose();
moel@40
   246
moel@40
   247
      string format = "";
moel@40
   248
      switch (sensor.SensorType) {
moel@116
   249
        case SensorType.Voltage: format = "\n{0}: {1:F2} V"; break;
moel@116
   250
        case SensorType.Clock: format = "\n{0}: {1:F0} MHz"; break;
moel@116
   251
        case SensorType.Load: format = "\n{0}: {1:F1} %"; break;
moel@116
   252
        case SensorType.Temperature: format = "\n{0}: {1:F1} °C"; break;
moel@116
   253
        case SensorType.Fan: format = "\n{0}: {1:F0} RPM"; break;
moel@116
   254
        case SensorType.Flow: format = "\n{0}: {1:F0} L/h"; break;
moel@118
   255
        case SensorType.Control: format = "\n{0}: {1:F1} %"; break;
moel@40
   256
      }
moel@116
   257
      string formattedValue = string.Format(format, sensor.Name, sensor.Value);
moel@116
   258
      string hardwareName = sensor.Hardware.Name;
moel@116
   259
      hardwareName = hardwareName.Substring(0, 
moel@116
   260
        Math.Min(63 - formattedValue.Length, hardwareName.Length));
moel@116
   261
      string text = hardwareName + formattedValue;
moel@116
   262
      if (text.Length > 63)
moel@116
   263
        text = null;
moel@40
   264
moel@116
   265
      notifyIcon.Text = text;
moel@42
   266
      notifyIcon.Visible = true;         
moel@40
   267
    }
moel@40
   268
  }
moel@40
   269
}