GUI/SensorGadget.cs
author moel.mich
Sun, 21 Nov 2010 12:28:31 +0000
changeset 244 99f16e21cdc8
parent 243 07a9329cd87c
child 252 e62afa69214f
permissions -rw-r--r--
Fixed Issue 137.
moel@202
     1
/*
moel@176
     2
  
moel@176
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@176
     4
moel@176
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@176
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@176
     7
  the License. You may obtain a copy of the License at
moel@176
     8
 
moel@176
     9
  http://www.mozilla.org/MPL/
moel@176
    10
moel@176
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@176
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@176
    13
  for the specific language governing rights and limitations under the License.
moel@176
    14
moel@176
    15
  The Original Code is the Open Hardware Monitor code.
moel@176
    16
moel@176
    17
  The Initial Developer of the Original Code is 
moel@176
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@176
    19
  Portions created by the Initial Developer are Copyright (C) 2010
moel@176
    20
  the Initial Developer. All Rights Reserved.
moel@176
    21
moel@176
    22
  Contributor(s):
moel@176
    23
moel@176
    24
  Alternatively, the contents of this file may be used under the terms of
moel@176
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@176
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@176
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@176
    28
  of those above. If you wish to allow use of your version of this file only
moel@176
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@176
    30
  use your version of this file under the terms of the MPL, indicate your
moel@176
    31
  decision by deleting the provisions above and replace them with the notice
moel@176
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@176
    33
  the provisions above, a recipient may use your version of this file under
moel@176
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@176
    35
 
moel@176
    36
*/
moel@176
    37
moel@176
    38
using System;
moel@176
    39
using System.Collections.Generic;
moel@176
    40
using System.Drawing;
moel@176
    41
using System.Windows.Forms;
moel@176
    42
using OpenHardwareMonitor.Hardware;
moel@176
    43
moel@176
    44
namespace OpenHardwareMonitor.GUI {
moel@176
    45
  public class SensorGadget : Gadget {
moel@176
    46
moel@176
    47
    private UnitManager unitManager;
moel@176
    48
moel@176
    49
    private Image back = Utilities.EmbeddedResources.GetImage("gadget.png");
moel@176
    50
    private Image barBack = Utilities.EmbeddedResources.GetImage("barback.png");
moel@176
    51
    private Image barblue = Utilities.EmbeddedResources.GetImage("barblue.png");
moel@183
    52
    private const int topBorder = 6;
moel@183
    53
    private const int bottomBorder = 7;
moel@176
    54
    private const int leftBorder = 6;
moel@183
    55
    private const int rightBorder = 7;
moel@183
    56
moel@183
    57
    private float fontSize;
moel@183
    58
    private int iconSize;
moel@183
    59
    private int hardwareLineHeight;
moel@183
    60
    private int sensorLineHeight;
moel@183
    61
    private int rightMargin;
moel@183
    62
    private int leftMargin;
moel@183
    63
    private int topMargin;
moel@183
    64
    private int bottomMargin;
moel@183
    65
    private int progressWidth;
moel@176
    66
moel@176
    67
    private IDictionary<IHardware, IList<ISensor>> sensors =
moel@176
    68
      new SortedDictionary<IHardware, IList<ISensor>>(new HardwareComparer());
moel@176
    69
moel@176
    70
    private PersistentSettings settings;
moel@181
    71
    private UserOption hardwareNames;
moel@176
    72
    private UserOption alwaysOnTop;
moel@183
    73
    private UserOption lockPositionAndSize;
moel@176
    74
moel@176
    75
    private Font largeFont;
moel@176
    76
    private Font smallFont;
moel@181
    77
    private Brush darkWhite;
moel@183
    78
    private StringFormat stringFormat;
moel@181
    79
    private StringFormat trimStringFormat;
moel@181
    80
    private StringFormat alignRightStringFormat;
moel@176
    81
moel@176
    82
    public SensorGadget(IComputer computer, PersistentSettings settings, 
moel@176
    83
      UnitManager unitManager) 
moel@176
    84
    {
moel@176
    85
      this.unitManager = unitManager;
moel@176
    86
      this.settings = settings;
moel@176
    87
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@183
    88
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);      
moel@176
    89
moel@181
    90
      this.darkWhite = new SolidBrush(Color.FromArgb(0xF0, 0xF0, 0xF0));
moel@181
    91
moel@183
    92
      this.stringFormat = new StringFormat();
moel@183
    93
      this.stringFormat.FormatFlags = StringFormatFlags.NoWrap;
moel@183
    94
moel@181
    95
      this.trimStringFormat = new StringFormat();
moel@181
    96
      this.trimStringFormat.Trimming = StringTrimming.EllipsisCharacter;
moel@183
    97
      this.trimStringFormat.FormatFlags = StringFormatFlags.NoWrap;
moel@181
    98
moel@181
    99
      this.alignRightStringFormat = new StringFormat();
moel@181
   100
      this.alignRightStringFormat.Alignment = StringAlignment.Far;
moel@183
   101
      this.alignRightStringFormat.FormatFlags = StringFormatFlags.NoWrap;
moel@176
   102
moel@176
   103
      this.Location = new Point(
moel@176
   104
        settings.GetValue("sensorGadget.Location.X", 100),
moel@176
   105
        settings.GetValue("sensorGadget.Location.Y", 100)); 
moel@176
   106
      LocationChanged += delegate(object sender, EventArgs e) {
moel@176
   107
        settings.SetValue("sensorGadget.Location.X", Location.X);
moel@176
   108
        settings.SetValue("sensorGadget.Location.Y", Location.Y);
moel@176
   109
      };
moel@183
   110
moel@183
   111
      SetFontSize(settings.GetValue("sensorGadget.FontSize", 7.5f));
moel@183
   112
      Resize(settings.GetValue("sensorGadget.Width", Size.Width));
moel@176
   113
      
moel@176
   114
      ContextMenu contextMenu = new ContextMenu();
moel@181
   115
      MenuItem hardwareNamesItem = new MenuItem("Hardware Names");
moel@181
   116
      contextMenu.MenuItems.Add(hardwareNamesItem);
moel@183
   117
      MenuItem fontSizeMenu = new MenuItem("Font Size");
moel@183
   118
      for (int i = 0; i < 4; i++) {
moel@183
   119
        float size;
moel@183
   120
        string name;
moel@183
   121
        switch (i) {
moel@183
   122
          case 0: size = 6.5f; name = "Small"; break;
moel@183
   123
          case 1: size = 7.5f; name = "Medium"; break;
moel@183
   124
          case 2: size = 9f; name = "Large"; break;
moel@183
   125
          case 3: size = 11f; name = "Very Large"; break;
moel@183
   126
          default: throw new NotImplementedException();
moel@183
   127
        }
moel@183
   128
        MenuItem item = new MenuItem(name);
moel@183
   129
        item.Checked = fontSize == size;
moel@183
   130
        item.Click += delegate(object sender, EventArgs e) {
moel@183
   131
          SetFontSize(size);
moel@183
   132
          settings.SetValue("sensorGadget.FontSize", size);
moel@183
   133
          foreach (MenuItem mi in fontSizeMenu.MenuItems)
moel@183
   134
            mi.Checked = mi == item;
moel@183
   135
        };
moel@183
   136
        fontSizeMenu.MenuItems.Add(item);
moel@183
   137
      }
moel@183
   138
      contextMenu.MenuItems.Add(fontSizeMenu);
moel@181
   139
      contextMenu.MenuItems.Add(new MenuItem("-"));
moel@183
   140
      MenuItem lockItem = new MenuItem("Lock Position and Size");
moel@176
   141
      contextMenu.MenuItems.Add(lockItem);
moel@176
   142
      contextMenu.MenuItems.Add(new MenuItem("-"));
moel@176
   143
      MenuItem alwaysOnTopItem = new MenuItem("Always on Top");
moel@176
   144
      contextMenu.MenuItems.Add(alwaysOnTopItem);
moel@176
   145
      MenuItem opacityMenu = new MenuItem("Opacity");
moel@176
   146
      contextMenu.MenuItems.Add(opacityMenu);
moel@176
   147
      Opacity = (byte)settings.GetValue("sensorGadget.Opacity", 255);      
moel@176
   148
      for (int i = 0; i < 5; i++) {
moel@176
   149
        MenuItem item = new MenuItem((20 * (i + 1)).ToString() + " %");
moel@176
   150
        byte o = (byte)(51 * (i + 1));
moel@176
   151
        item.Checked = Opacity == o;
moel@176
   152
        item.Click += delegate(object sender, EventArgs e) {
moel@178
   153
          Opacity = o;
moel@176
   154
          settings.SetValue("sensorGadget.Opacity", Opacity);
moel@176
   155
          foreach (MenuItem mi in opacityMenu.MenuItems)
moel@178
   156
            mi.Checked = mi == item;          
moel@176
   157
        };
moel@176
   158
        opacityMenu.MenuItems.Add(item);
moel@176
   159
      }
moel@176
   160
      this.ContextMenu = contextMenu;
moel@176
   161
moel@181
   162
      hardwareNames = new UserOption("sensorGadget.Hardwarenames", true,
moel@181
   163
        hardwareNamesItem, settings);
moel@181
   164
      hardwareNames.Changed += delegate(object sender, EventArgs e) {
moel@181
   165
        Resize();
moel@181
   166
      };
moel@181
   167
moel@176
   168
      alwaysOnTop = new UserOption("sensorGadget.AlwaysOnTop", false, 
moel@176
   169
        alwaysOnTopItem, settings);
moel@176
   170
      alwaysOnTop.Changed += delegate(object sender, EventArgs e) {
moel@176
   171
        this.AlwaysOnTop = alwaysOnTop.Value;
moel@176
   172
      };
moel@183
   173
      lockPositionAndSize = new UserOption("sensorGadget.LockPositionAndSize", 
moel@183
   174
        false, lockItem, settings);
moel@183
   175
      lockPositionAndSize.Changed += delegate(object sender, EventArgs e) {
moel@183
   176
        this.LockPositionAndSize = lockPositionAndSize.Value;
moel@176
   177
      };
moel@176
   178
moel@183
   179
      HitTest += delegate(object sender, HitTestEventArgs e) {
moel@183
   180
        if (lockPositionAndSize.Value)
moel@183
   181
          return;
moel@183
   182
moel@183
   183
        if (e.Location.X < leftBorder) {
moel@183
   184
          e.HitResult = HitResult.Left;
moel@183
   185
          return;
moel@183
   186
        }
moel@183
   187
        if (e.Location.X > Size.Width - 1 - rightBorder) {
moel@183
   188
          e.HitResult = HitResult.Right;
moel@183
   189
          return;
moel@183
   190
        }
moel@183
   191
      };
moel@183
   192
moel@183
   193
      SizeChanged += delegate(object sender, EventArgs e) {
moel@183
   194
        settings.SetValue("sensorGadget.Width", Size.Width);
moel@183
   195
        Redraw();
moel@183
   196
      };
moel@244
   197
moel@244
   198
      MouseDoubleClick += delegate(object obj, MouseEventArgs args) {
moel@244
   199
        SendHideShowCommand();
moel@244
   200
      };
moel@176
   201
    }
moel@176
   202
moel@181
   203
    public override void Dispose() {
moel@181
   204
moel@181
   205
      largeFont.Dispose();
moel@181
   206
      largeFont = null;
moel@181
   207
moel@181
   208
      smallFont.Dispose();
moel@181
   209
      smallFont = null;
moel@181
   210
moel@181
   211
      darkWhite.Dispose();
moel@181
   212
      darkWhite = null;
moel@181
   213
moel@183
   214
      stringFormat.Dispose();
moel@183
   215
      stringFormat = null;
moel@183
   216
moel@181
   217
      trimStringFormat.Dispose();
moel@181
   218
      trimStringFormat = null;
moel@181
   219
moel@181
   220
      alignRightStringFormat.Dispose();
moel@181
   221
      alignRightStringFormat = null;      
moel@181
   222
moel@181
   223
      base.Dispose();
moel@181
   224
    }
moel@181
   225
moel@176
   226
    private void HardwareRemoved(IHardware hardware) {
moel@176
   227
      hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
moel@176
   228
      hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
moel@176
   229
      foreach (ISensor sensor in hardware.Sensors)
moel@176
   230
        SensorRemoved(sensor);
moel@176
   231
      foreach (IHardware subHardware in hardware.SubHardware)
moel@176
   232
        HardwareRemoved(subHardware);
moel@176
   233
    }
moel@176
   234
moel@176
   235
    private void HardwareAdded(IHardware hardware) {
moel@176
   236
      foreach (ISensor sensor in hardware.Sensors)
moel@176
   237
        SensorAdded(sensor);
moel@176
   238
      hardware.SensorAdded += new SensorEventHandler(SensorAdded);
moel@176
   239
      hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
moel@176
   240
      foreach (IHardware subHardware in hardware.SubHardware)
moel@176
   241
        HardwareAdded(subHardware);
moel@176
   242
    }
moel@176
   243
moel@176
   244
    private void SensorAdded(ISensor sensor) {
moel@176
   245
      if (settings.GetValue(new Identifier(sensor.Identifier,
moel@176
   246
        "gadget").ToString(), false)) 
moel@176
   247
        Add(sensor);
moel@176
   248
    }
moel@176
   249
moel@176
   250
    private void SensorRemoved(ISensor sensor) {
moel@176
   251
      if (Contains(sensor))
moel@176
   252
        Remove(sensor, false);
moel@176
   253
    }
moel@176
   254
moel@176
   255
    public bool Contains(ISensor sensor) {
moel@176
   256
      foreach (IList<ISensor> list in sensors.Values)
moel@176
   257
        if (list.Contains(sensor))
moel@176
   258
          return true;
moel@176
   259
      return false;
moel@176
   260
    }
moel@176
   261
moel@176
   262
    public void Add(ISensor sensor) {
moel@176
   263
      if (Contains(sensor)) {
moel@176
   264
        return;
moel@176
   265
      } else {
moel@176
   266
        // get the right hardware
moel@176
   267
        IHardware hardware = sensor.Hardware;
moel@176
   268
        while (hardware.Parent != null)
moel@176
   269
          hardware = hardware.Parent;
moel@176
   270
moel@176
   271
        // get the sensor list associated with the hardware
moel@176
   272
        IList<ISensor> list;
moel@176
   273
        if (!sensors.TryGetValue(hardware, out list)) {
moel@176
   274
          list = new List<ISensor>();
moel@176
   275
          sensors.Add(hardware, list);
moel@176
   276
        }
moel@176
   277
moel@176
   278
        // insert the sensor at the right position
moel@176
   279
        int i = 0;
moel@176
   280
        while (i < list.Count && (list[i].SensorType < sensor.SensorType || 
moel@176
   281
          (list[i].SensorType == sensor.SensorType && 
moel@176
   282
           list[i].Index < sensor.Index))) i++;
moel@176
   283
        list.Insert(i, sensor);
moel@176
   284
moel@176
   285
        settings.SetValue(
moel@176
   286
          new Identifier(sensor.Identifier, "gadget").ToString(), true);
moel@176
   287
        
moel@176
   288
        Resize();
moel@176
   289
      }
moel@176
   290
    }
moel@176
   291
moel@176
   292
    public void Remove(ISensor sensor) {
moel@176
   293
      Remove(sensor, true);
moel@176
   294
    }
moel@176
   295
moel@176
   296
    private void Remove(ISensor sensor, bool deleteConfig) {
moel@176
   297
      if (deleteConfig) 
moel@176
   298
        settings.Remove(new Identifier(sensor.Identifier, "gadget").ToString());
moel@176
   299
moel@176
   300
      foreach (KeyValuePair<IHardware, IList<ISensor>> keyValue in sensors)
moel@176
   301
        if (keyValue.Value.Contains(sensor)) {
moel@176
   302
          keyValue.Value.Remove(sensor);          
moel@176
   303
          if (keyValue.Value.Count == 0) {
moel@176
   304
            sensors.Remove(keyValue.Key);
moel@176
   305
            break;
moel@176
   306
          }
moel@176
   307
        }
moel@176
   308
      Resize();
moel@183
   309
    }
moel@183
   310
moel@244
   311
    public event EventHandler HideShowCommand;
moel@244
   312
moel@244
   313
    public void SendHideShowCommand() {
moel@244
   314
      if (HideShowCommand != null)
moel@244
   315
        HideShowCommand(this, null);
moel@244
   316
    }
moel@244
   317
moel@183
   318
    private Font CreateFont(float size, FontStyle style) {
moel@215
   319
      try {
moel@215
   320
        return new Font(SystemFonts.MessageBoxFont.FontFamily, size, style);
moel@215
   321
      } catch (ArgumentException) {
moel@215
   322
        // if the style is not supported, fall back to the original one
moel@215
   323
        return new Font(SystemFonts.MessageBoxFont.FontFamily, size, 
moel@215
   324
          SystemFonts.MessageBoxFont.Style);
moel@215
   325
      }
moel@183
   326
    }
moel@183
   327
moel@183
   328
    private void SetFontSize(float size) {
moel@183
   329
      fontSize = size;
moel@183
   330
      largeFont = CreateFont(fontSize, FontStyle.Bold);
moel@183
   331
      smallFont = CreateFont(fontSize, FontStyle.Regular);
moel@183
   332
      iconSize = (int)Math.Round(1.5 * fontSize);
moel@183
   333
      hardwareLineHeight = (int)Math.Round(1.66 * fontSize);
moel@183
   334
      sensorLineHeight = (int)Math.Round(1.33 * fontSize);      
moel@183
   335
      leftMargin = leftBorder + (int)Math.Round(0.3 * fontSize);
moel@183
   336
      rightMargin = rightBorder + (int)Math.Round(0.3 * fontSize);
moel@183
   337
      topMargin = topBorder;
moel@183
   338
      bottomMargin = bottomBorder + (int)Math.Round(0.3 * fontSize);
moel@183
   339
      progressWidth = (int)Math.Round(5.3 * fontSize);
moel@183
   340
      Resize((int)Math.Round(17.3 * fontSize));
moel@176
   341
    }
moel@176
   342
moel@176
   343
    private void Resize() {
moel@183
   344
      Resize(this.Size.Width);
moel@183
   345
    }
moel@183
   346
moel@183
   347
    private void Resize(int width) {
moel@183
   348
      int y = topMargin;      
moel@176
   349
      foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
moel@181
   350
        if (hardwareNames.Value) {
moel@183
   351
          if (y > topMargin)
moel@183
   352
            y += hardwareLineHeight - sensorLineHeight;
moel@181
   353
          y += hardwareLineHeight;
moel@181
   354
        }
moel@176
   355
        y += pair.Value.Count * sensorLineHeight;
moel@176
   356
      }
moel@183
   357
      y += bottomMargin;
moel@186
   358
      y = Math.Max(y, topBorder + hardwareLineHeight + bottomBorder);
moel@183
   359
      this.Size = new Size(width, y);
moel@176
   360
    }
moel@176
   361
moel@176
   362
    private void DrawBackground(Graphics g) {
moel@176
   363
      int w = Size.Width;
moel@176
   364
      int h = Size.Height;
moel@176
   365
      int t = topBorder;
moel@176
   366
      int b = bottomBorder;
moel@176
   367
      int l = leftBorder;
moel@176
   368
      int r = rightBorder;
moel@176
   369
      GraphicsUnit u = GraphicsUnit.Pixel;
moel@176
   370
moel@176
   371
      g.DrawImage(back, new Rectangle(0, 0, l, t),
moel@176
   372
        new Rectangle(0, 0, l, t), u);
moel@176
   373
      g.DrawImage(back, new Rectangle(l, 0, w - l - r, t),
moel@176
   374
        new Rectangle(l, 0, back.Width - l - r, t), u);
moel@176
   375
      g.DrawImage(back, new Rectangle(w - r, 0, r, t),
moel@176
   376
        new Rectangle(back.Width - r, 0, r, t), u);
moel@176
   377
moel@176
   378
      g.DrawImage(back, new Rectangle(0, t, l, h - t - b),
moel@176
   379
        new Rectangle(0, t, l, back.Height - t - b), u);
moel@176
   380
      g.DrawImage(back, new Rectangle(l, t, w - l - r, h - t - b),
moel@176
   381
        new Rectangle(l, t, back.Width - l - r, back.Height - t - b), u);
moel@176
   382
      g.DrawImage(back, new Rectangle(w - r, t, r, h - t - b),
moel@176
   383
        new Rectangle(back.Width - r, t, r, back.Height - t - b), u);
moel@176
   384
moel@176
   385
      g.DrawImage(back, new Rectangle(0, h - b, l, b),
moel@176
   386
        new Rectangle(0, back.Height - b, l, b), u);
moel@176
   387
      g.DrawImage(back, new Rectangle(l, h - b, w - l - r, b),
moel@176
   388
        new Rectangle(l, back.Height - b, back.Width - l - r, b), u);
moel@176
   389
      g.DrawImage(back, new Rectangle(w - r, h - b, r, b),
moel@176
   390
        new Rectangle(back.Width - r, back.Height - b, r, b), u);
moel@176
   391
    }
moel@176
   392
moel@243
   393
    private void DrawProgress(Graphics g, float x, float y, 
moel@243
   394
      float width, float height, float progress) 
moel@176
   395
    {
moel@176
   396
      g.DrawImage(barBack, 
moel@176
   397
        new RectangleF(x + width * progress, y, width * (1 - progress), height), 
moel@176
   398
        new RectangleF(barBack.Width * progress, 0, 
moel@176
   399
          (1 - progress) * barBack.Width, barBack.Height), 
moel@176
   400
        GraphicsUnit.Pixel);
moel@176
   401
      g.DrawImage(barblue,
moel@176
   402
        new RectangleF(x, y, width * progress, height),
moel@176
   403
        new RectangleF(0, 0, progress * barblue.Width, barblue.Height),
moel@176
   404
        GraphicsUnit.Pixel);
moel@176
   405
    }
moel@176
   406
moel@176
   407
    protected override void OnPaint(PaintEventArgs e) {
moel@176
   408
      Graphics g = e.Graphics;
moel@176
   409
      int w = Size.Width;
moel@176
   410
moel@176
   411
      g.Clear(Color.Transparent);
moel@183
   412
      
moel@176
   413
      DrawBackground(g);
moel@176
   414
moel@176
   415
      int x;
moel@183
   416
      int y = topMargin;
moel@186
   417
moel@186
   418
      if (sensors.Count == 0) {
moel@186
   419
        x = leftBorder + 1;
moel@186
   420
        g.DrawString("Add a sensor ...", smallFont, Brushes.White,
moel@186
   421
          new Rectangle(x, y - 1, w - rightBorder - x, 0));
moel@186
   422
      }
moel@186
   423
moel@176
   424
      foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
moel@181
   425
        if (hardwareNames.Value) {
moel@183
   426
          if (y > topMargin)
moel@183
   427
            y += hardwareLineHeight - sensorLineHeight;
moel@181
   428
          x = leftBorder + 1;
moel@181
   429
          g.DrawImage(HardwareTypeImage.Instance.GetImage(pair.Key.HardwareType),
moel@181
   430
            new Rectangle(x, y + 1, iconSize, iconSize));
moel@181
   431
          x += iconSize + 1;
moel@181
   432
          g.DrawString(pair.Key.Name, largeFont, Brushes.White,
moel@183
   433
            new Rectangle(x, y - 1, w - rightBorder - x, 0), 
moel@183
   434
            stringFormat);
moel@181
   435
          y += hardwareLineHeight;
moel@181
   436
        }
moel@176
   437
moel@176
   438
        foreach (ISensor sensor in pair.Value) {
moel@183
   439
          int remainingWidth;
moel@176
   440
moel@187
   441
moel@187
   442
          if ((sensor.SensorType != SensorType.Load &&
moel@217
   443
               sensor.SensorType != SensorType.Control &&
moel@217
   444
               sensor.SensorType != SensorType.Level) || !sensor.Value.HasValue) 
moel@176
   445
          {
moel@187
   446
            string formatted;
moel@187
   447
moel@187
   448
            if (sensor.Value.HasValue) {
moel@187
   449
              string format = "";
moel@187
   450
              switch (sensor.SensorType) {
moel@187
   451
                case SensorType.Voltage:
moel@187
   452
                  format = "{0:F2} V";
moel@187
   453
                  break;
moel@187
   454
                case SensorType.Clock:
moel@187
   455
                  format = "{0:F0} MHz";
moel@187
   456
                  break;
moel@187
   457
                case SensorType.Temperature:
moel@187
   458
                  format = "{0:F1} °C";
moel@187
   459
                  break;
moel@187
   460
                case SensorType.Fan:
moel@187
   461
                  format = "{0:F0} RPM";
moel@187
   462
                  break;
moel@187
   463
                case SensorType.Flow:
moel@187
   464
                  format = "{0:F0} L/h";
moel@187
   465
                  break;
moel@187
   466
              }
moel@187
   467
moel@187
   468
              if (sensor.SensorType == SensorType.Temperature &&
moel@187
   469
                unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) {
moel@187
   470
                formatted = string.Format("{0:F1} °F",
moel@187
   471
                  sensor.Value * 1.8 + 32);
moel@187
   472
              } else {
moel@187
   473
                formatted = string.Format(format, sensor.Value);
moel@187
   474
              }
moel@187
   475
            } else {
moel@187
   476
              formatted = "-";
moel@176
   477
            }
moel@176
   478
moel@187
   479
            g.DrawString(formatted, smallFont, darkWhite,
moel@187
   480
              new RectangleF(-1, y - 1, w - rightMargin + 3, 0),
moel@181
   481
              alignRightStringFormat);
moel@181
   482
moel@187
   483
            remainingWidth = w - (int)Math.Floor(g.MeasureString(formatted,
moel@187
   484
              smallFont, w, StringFormat.GenericTypographic).Width) -
moel@181
   485
              rightMargin;
moel@187
   486
          } else {
moel@187
   487
            DrawProgress(g, w - progressWidth - rightMargin,
moel@243
   488
              y + 0.35f * sensorLineHeight, progressWidth,
moel@243
   489
              0.6f * sensorLineHeight, 0.01f * sensor.Value.Value);
moel@183
   490
moel@183
   491
            remainingWidth = w - progressWidth - rightMargin;
moel@176
   492
          }
moel@187
   493
           
moel@183
   494
          remainingWidth -= leftMargin + 2;
moel@183
   495
          if (remainingWidth > 0) {
moel@183
   496
            g.DrawString(sensor.Name, smallFont, darkWhite,
moel@183
   497
              new RectangleF(leftMargin - 1, y - 1, remainingWidth, 0), 
moel@183
   498
              trimStringFormat);
moel@183
   499
          }
moel@181
   500
moel@176
   501
          y += sensorLineHeight;
moel@176
   502
        }
moel@176
   503
      }
moel@176
   504
    }
moel@176
   505
moel@176
   506
    private class HardwareComparer : IComparer<IHardware> {
moel@176
   507
      public int Compare(IHardware x, IHardware y) {
moel@176
   508
        if (x == null && y == null)
moel@176
   509
          return 0;
moel@176
   510
        if (x == null)
moel@176
   511
          return -1;
moel@176
   512
        if (y == null)
moel@176
   513
          return 1;
moel@176
   514
moel@176
   515
        if (x.HardwareType != y.HardwareType)
moel@176
   516
          return x.HardwareType.CompareTo(y.HardwareType);
moel@176
   517
moel@184
   518
        return x.Identifier.CompareTo(y.Identifier);
moel@176
   519
      }
moel@176
   520
    }
moel@176
   521
  }
moel@176
   522
}
moel@176
   523