GUI/SensorFrontView.cs
author StephaneLenclud
Sat, 13 Apr 2013 00:43:25 +0200
branchMiniDisplay
changeset 438 f590956d3234
parent 433 090259cfd699
child 439 06369ace500d
permissions -rw-r--r--
Sensors can now be displayed in FrontView.
StephaneLenclud@433
     1
/*
StephaneLenclud@433
     2
 
StephaneLenclud@433
     3
  This Source Code Form is subject to the terms of the Mozilla Public
StephaneLenclud@433
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
StephaneLenclud@433
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
StephaneLenclud@433
     6
 
StephaneLenclud@433
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
StephaneLenclud@433
     8
	
StephaneLenclud@433
     9
*/
StephaneLenclud@433
    10
StephaneLenclud@433
    11
using System;
StephaneLenclud@433
    12
using System.Drawing;
StephaneLenclud@433
    13
using System.Drawing.Drawing2D;
StephaneLenclud@433
    14
using System.Drawing.Imaging;
StephaneLenclud@433
    15
using System.Drawing.Text;
StephaneLenclud@433
    16
using System.Runtime.InteropServices;
StephaneLenclud@433
    17
using System.Windows.Forms;
StephaneLenclud@433
    18
using OpenHardwareMonitor.Hardware;
StephaneLenclud@433
    19
using OpenHardwareMonitor.Utilities;
StephaneLenclud@433
    20
StephaneLenclud@433
    21
namespace OpenHardwareMonitor.GUI
StephaneLenclud@433
    22
{
StephaneLenclud@433
    23
    public class SensorFrontView : IDisposable
StephaneLenclud@433
    24
    {
StephaneLenclud@433
    25
StephaneLenclud@433
    26
        private UnitManager unitManager;
StephaneLenclud@433
    27
StephaneLenclud@433
    28
        private ISensor sensor;
StephaneLenclud@433
    29
        private Color color;
StephaneLenclud@433
    30
        private Color darkColor;
StephaneLenclud@433
    31
        private Font font;
StephaneLenclud@433
    32
        private Font smallFont;
StephaneLenclud@438
    33
        public string iFirstLine;
StephaneLenclud@438
    34
        public string iSecondLine;
StephaneLenclud@438
    35
StephaneLenclud@433
    36
StephaneLenclud@433
    37
        public SensorFrontView(SoundGraphDisplay soundGraphDisplay, ISensor sensor,
StephaneLenclud@433
    38
          bool balloonTip, PersistentSettings settings, UnitManager unitManager)
StephaneLenclud@433
    39
        {
StephaneLenclud@433
    40
            this.unitManager = unitManager;
StephaneLenclud@433
    41
            this.sensor = sensor;
StephaneLenclud@433
    42
       
StephaneLenclud@433
    43
            // get the default dpi to create an icon with the correct size
StephaneLenclud@433
    44
            float dpiX, dpiY;
StephaneLenclud@433
    45
            using (Bitmap b = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
StephaneLenclud@433
    46
            {
StephaneLenclud@433
    47
                dpiX = b.HorizontalResolution;
StephaneLenclud@433
    48
                dpiY = b.VerticalResolution;
StephaneLenclud@433
    49
            }
StephaneLenclud@433
    50
StephaneLenclud@433
    51
            // adjust the size of the icon to current dpi (default is 16x16 at 96 dpi) 
StephaneLenclud@433
    52
            int width = (int)Math.Round(16 * dpiX / 96);
StephaneLenclud@433
    53
            int height = (int)Math.Round(16 * dpiY / 96);
StephaneLenclud@433
    54
StephaneLenclud@433
    55
            // make sure it does never get smaller than 16x16
StephaneLenclud@433
    56
            width = width < 16 ? 16 : width;
StephaneLenclud@433
    57
            height = height < 16 ? 16 : height;
StephaneLenclud@433
    58
StephaneLenclud@433
    59
            // adjust the font size to the icon size
StephaneLenclud@433
    60
            FontFamily family = SystemFonts.MessageBoxFont.FontFamily;
StephaneLenclud@433
    61
            float baseSize;
StephaneLenclud@433
    62
            switch (family.Name)
StephaneLenclud@433
    63
            {
StephaneLenclud@433
    64
                case "Segoe UI": baseSize = 12; break;
StephaneLenclud@433
    65
                case "Tahoma": baseSize = 11; break;
StephaneLenclud@433
    66
                default: baseSize = 12; break;
StephaneLenclud@433
    67
            }
StephaneLenclud@433
    68
StephaneLenclud@433
    69
            this.font = new Font(family,
StephaneLenclud@433
    70
              baseSize * width / 16.0f, GraphicsUnit.Pixel);
StephaneLenclud@433
    71
            this.smallFont = new Font(family,
StephaneLenclud@433
    72
              0.75f * baseSize * width / 16.0f, GraphicsUnit.Pixel);
StephaneLenclud@433
    73
StephaneLenclud@433
    74
        }
StephaneLenclud@433
    75
StephaneLenclud@433
    76
        public ISensor Sensor
StephaneLenclud@433
    77
        {
StephaneLenclud@433
    78
            get { return sensor; }
StephaneLenclud@433
    79
        }
StephaneLenclud@433
    80
StephaneLenclud@433
    81
        public Color Color
StephaneLenclud@433
    82
        {
StephaneLenclud@433
    83
            get { return color; }
StephaneLenclud@433
    84
            set
StephaneLenclud@433
    85
            {
StephaneLenclud@433
    86
                this.color = value;
StephaneLenclud@433
    87
                this.darkColor = Color.FromArgb(255,
StephaneLenclud@433
    88
                  this.color.R / 3,
StephaneLenclud@433
    89
                  this.color.G / 3,
StephaneLenclud@433
    90
                  this.color.B / 3);
StephaneLenclud@433
    91
            }
StephaneLenclud@433
    92
        }
StephaneLenclud@433
    93
StephaneLenclud@433
    94
        public void Dispose()
StephaneLenclud@433
    95
        {
StephaneLenclud@433
    96
            font.Dispose();
StephaneLenclud@433
    97
            smallFont.Dispose();
StephaneLenclud@433
    98
        }
StephaneLenclud@433
    99
StephaneLenclud@438
   100
        public string GetString()
StephaneLenclud@433
   101
        {
StephaneLenclud@433
   102
            if (!sensor.Value.HasValue)
StephaneLenclud@433
   103
                return "-";
StephaneLenclud@433
   104
StephaneLenclud@433
   105
            switch (sensor.SensorType)
StephaneLenclud@433
   106
            {
StephaneLenclud@433
   107
                case SensorType.Voltage:
StephaneLenclud@433
   108
                    return string.Format("{0:F1}", sensor.Value);
StephaneLenclud@433
   109
                case SensorType.Clock:
StephaneLenclud@433
   110
                    return string.Format("{0:F1}", 1e-3f * sensor.Value);
StephaneLenclud@433
   111
                case SensorType.Load:
StephaneLenclud@433
   112
                    return string.Format("{0:F0}", sensor.Value);
StephaneLenclud@433
   113
                case SensorType.Temperature:
StephaneLenclud@433
   114
                    if (unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
StephaneLenclud@433
   115
                        return string.Format("{0:F0}",
StephaneLenclud@433
   116
                          UnitManager.CelsiusToFahrenheit(sensor.Value));
StephaneLenclud@433
   117
                    else
StephaneLenclud@433
   118
                        return string.Format("{0:F0}", sensor.Value);
StephaneLenclud@433
   119
                case SensorType.Fan:
StephaneLenclud@433
   120
                    return string.Format("{0:F1}", 1e-3f * sensor.Value);
StephaneLenclud@433
   121
                case SensorType.Flow:
StephaneLenclud@433
   122
                    return string.Format("{0:F1}", 1e-3f * sensor.Value);
StephaneLenclud@433
   123
                case SensorType.Control:
StephaneLenclud@433
   124
                    return string.Format("{0:F0}", sensor.Value);
StephaneLenclud@433
   125
                case SensorType.Level:
StephaneLenclud@433
   126
                    return string.Format("{0:F0}", sensor.Value);
StephaneLenclud@433
   127
                case SensorType.Power:
StephaneLenclud@433
   128
                    return string.Format("{0:F0}", sensor.Value);
StephaneLenclud@433
   129
                case SensorType.Data:
StephaneLenclud@433
   130
                    return string.Format("{0:F0}", sensor.Value);
StephaneLenclud@433
   131
                case SensorType.Factor:
StephaneLenclud@433
   132
                    return string.Format("{0:F1}", sensor.Value);
StephaneLenclud@433
   133
            }
StephaneLenclud@433
   134
            return "-";
StephaneLenclud@433
   135
        }
StephaneLenclud@433
   136
StephaneLenclud@433
   137
StephaneLenclud@433
   138
        public void Update()
StephaneLenclud@433
   139
        {
StephaneLenclud@433
   140
 
StephaneLenclud@433
   141
StephaneLenclud@433
   142
            switch (sensor.SensorType)
StephaneLenclud@433
   143
            {
StephaneLenclud@433
   144
                case SensorType.Load:
StephaneLenclud@433
   145
                case SensorType.Control:
StephaneLenclud@433
   146
                case SensorType.Level:
StephaneLenclud@433
   147
                    //notifyIcon.Icon = CreatePercentageIcon();
StephaneLenclud@433
   148
                    break;
StephaneLenclud@433
   149
                default:
StephaneLenclud@433
   150
                    //notifyIcon.Icon = CreateTransparentIcon();
StephaneLenclud@433
   151
                    break;
StephaneLenclud@433
   152
            }
StephaneLenclud@433
   153
StephaneLenclud@433
   154
StephaneLenclud@433
   155
            string format = "";
StephaneLenclud@433
   156
            switch (sensor.SensorType)
StephaneLenclud@433
   157
            {
StephaneLenclud@438
   158
                case SensorType.Voltage: format = "{0}: {1:F2} V"; break;
StephaneLenclud@438
   159
                case SensorType.Clock: format = "{0}: {1:F0} MHz"; break;
StephaneLenclud@438
   160
                case SensorType.Load: format = "{0}: {1:F1} %"; break;
StephaneLenclud@438
   161
                case SensorType.Temperature: format = "{0}: {1:F1} °C"; break;
StephaneLenclud@438
   162
                case SensorType.Fan: format = "{0}: {1:F0} RPM"; break;
StephaneLenclud@438
   163
                case SensorType.Flow: format = "{0}: {1:F0} L/h"; break;
StephaneLenclud@438
   164
                case SensorType.Control: format = "{0}: {1:F1} %"; break;
StephaneLenclud@438
   165
                case SensorType.Level: format = "{0}: {1:F1} %"; break;
StephaneLenclud@438
   166
                case SensorType.Power: format = "{0}: {1:F0} W"; break;
StephaneLenclud@438
   167
                case SensorType.Data: format = "{0}: {1:F0} GB"; break;
StephaneLenclud@438
   168
                case SensorType.Factor: format = "{0}: {1:F3} GB"; break;
StephaneLenclud@433
   169
            }
StephaneLenclud@433
   170
            string formattedValue = string.Format(format, sensor.Name, sensor.Value);
StephaneLenclud@433
   171
StephaneLenclud@433
   172
            if (sensor.SensorType == SensorType.Temperature &&
StephaneLenclud@433
   173
              unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
StephaneLenclud@433
   174
            {
StephaneLenclud@438
   175
                format = "{0}: {1:F1} °F";
StephaneLenclud@433
   176
                formattedValue = string.Format(format, sensor.Name,
StephaneLenclud@433
   177
                  UnitManager.CelsiusToFahrenheit(sensor.Value));
StephaneLenclud@433
   178
            }
StephaneLenclud@433
   179
StephaneLenclud@438
   180
            iFirstLine = sensor.Hardware.Name;
StephaneLenclud@438
   181
            iSecondLine = formattedValue;
StephaneLenclud@433
   182
StephaneLenclud@433
   183
        }
StephaneLenclud@433
   184
    }
StephaneLenclud@433
   185
}