GUI/SensorFrontView.cs
branchMiniDisplay
changeset 433 090259cfd699
child 438 f590956d3234
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GUI/SensorFrontView.cs	Sun Feb 03 18:01:50 2013 +0100
     1.3 @@ -0,0 +1,283 @@
     1.4 +/*
     1.5 + 
     1.6 +  This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +  License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 +  file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 + 
    1.10 +  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
    1.11 +	
    1.12 +*/
    1.13 +
    1.14 +using System;
    1.15 +using System.Drawing;
    1.16 +using System.Drawing.Drawing2D;
    1.17 +using System.Drawing.Imaging;
    1.18 +using System.Drawing.Text;
    1.19 +using System.Runtime.InteropServices;
    1.20 +using System.Windows.Forms;
    1.21 +using OpenHardwareMonitor.Hardware;
    1.22 +using OpenHardwareMonitor.Utilities;
    1.23 +
    1.24 +namespace OpenHardwareMonitor.GUI
    1.25 +{
    1.26 +    public class SensorFrontView : IDisposable
    1.27 +    {
    1.28 +
    1.29 +        private UnitManager unitManager;
    1.30 +
    1.31 +        private ISensor sensor;
    1.32 +        private Bitmap bitmap;
    1.33 +        private Graphics graphics;
    1.34 +        private Color color;
    1.35 +        private Color darkColor;
    1.36 +        private Brush brush;
    1.37 +        private Brush darkBrush;
    1.38 +        private Pen pen;
    1.39 +        private Font font;
    1.40 +        private Font smallFont;
    1.41 +
    1.42 +        public SensorFrontView(SoundGraphDisplay soundGraphDisplay, ISensor sensor,
    1.43 +          bool balloonTip, PersistentSettings settings, UnitManager unitManager)
    1.44 +        {
    1.45 +            this.unitManager = unitManager;
    1.46 +            this.sensor = sensor;
    1.47 +       
    1.48 +            // get the default dpi to create an icon with the correct size
    1.49 +            float dpiX, dpiY;
    1.50 +            using (Bitmap b = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
    1.51 +            {
    1.52 +                dpiX = b.HorizontalResolution;
    1.53 +                dpiY = b.VerticalResolution;
    1.54 +            }
    1.55 +
    1.56 +            // adjust the size of the icon to current dpi (default is 16x16 at 96 dpi) 
    1.57 +            int width = (int)Math.Round(16 * dpiX / 96);
    1.58 +            int height = (int)Math.Round(16 * dpiY / 96);
    1.59 +
    1.60 +            // make sure it does never get smaller than 16x16
    1.61 +            width = width < 16 ? 16 : width;
    1.62 +            height = height < 16 ? 16 : height;
    1.63 +
    1.64 +            // adjust the font size to the icon size
    1.65 +            FontFamily family = SystemFonts.MessageBoxFont.FontFamily;
    1.66 +            float baseSize;
    1.67 +            switch (family.Name)
    1.68 +            {
    1.69 +                case "Segoe UI": baseSize = 12; break;
    1.70 +                case "Tahoma": baseSize = 11; break;
    1.71 +                default: baseSize = 12; break;
    1.72 +            }
    1.73 +
    1.74 +            this.font = new Font(family,
    1.75 +              baseSize * width / 16.0f, GraphicsUnit.Pixel);
    1.76 +            this.smallFont = new Font(family,
    1.77 +              0.75f * baseSize * width / 16.0f, GraphicsUnit.Pixel);
    1.78 +
    1.79 +            this.bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
    1.80 +            this.graphics = Graphics.FromImage(this.bitmap);
    1.81 +
    1.82 +            if (Environment.OSVersion.Version.Major > 5)
    1.83 +            {
    1.84 +                this.graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
    1.85 +                this.graphics.SmoothingMode = SmoothingMode.HighQuality;
    1.86 +            }
    1.87 +        }
    1.88 +
    1.89 +        public ISensor Sensor
    1.90 +        {
    1.91 +            get { return sensor; }
    1.92 +        }
    1.93 +
    1.94 +        public Color Color
    1.95 +        {
    1.96 +            get { return color; }
    1.97 +            set
    1.98 +            {
    1.99 +                this.color = value;
   1.100 +                this.darkColor = Color.FromArgb(255,
   1.101 +                  this.color.R / 3,
   1.102 +                  this.color.G / 3,
   1.103 +                  this.color.B / 3);
   1.104 +                Brush brush = this.brush;
   1.105 +                this.brush = new SolidBrush(this.color);
   1.106 +                if (brush != null)
   1.107 +                    brush.Dispose();
   1.108 +                Brush darkBrush = this.darkBrush;
   1.109 +                this.darkBrush = new SolidBrush(this.darkColor);
   1.110 +                if (darkBrush != null)
   1.111 +                    darkBrush.Dispose();
   1.112 +            }
   1.113 +        }
   1.114 +
   1.115 +        public void Dispose()
   1.116 +        {
   1.117 +
   1.118 +            if (brush != null)
   1.119 +                brush.Dispose();
   1.120 +            if (darkBrush != null)
   1.121 +                darkBrush.Dispose();
   1.122 +            pen.Dispose();
   1.123 +            graphics.Dispose();
   1.124 +            bitmap.Dispose();
   1.125 +            font.Dispose();
   1.126 +            smallFont.Dispose();
   1.127 +        }
   1.128 +
   1.129 +        private string GetString()
   1.130 +        {
   1.131 +            if (!sensor.Value.HasValue)
   1.132 +                return "-";
   1.133 +
   1.134 +            switch (sensor.SensorType)
   1.135 +            {
   1.136 +                case SensorType.Voltage:
   1.137 +                    return string.Format("{0:F1}", sensor.Value);
   1.138 +                case SensorType.Clock:
   1.139 +                    return string.Format("{0:F1}", 1e-3f * sensor.Value);
   1.140 +                case SensorType.Load:
   1.141 +                    return string.Format("{0:F0}", sensor.Value);
   1.142 +                case SensorType.Temperature:
   1.143 +                    if (unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
   1.144 +                        return string.Format("{0:F0}",
   1.145 +                          UnitManager.CelsiusToFahrenheit(sensor.Value));
   1.146 +                    else
   1.147 +                        return string.Format("{0:F0}", sensor.Value);
   1.148 +                case SensorType.Fan:
   1.149 +                    return string.Format("{0:F1}", 1e-3f * sensor.Value);
   1.150 +                case SensorType.Flow:
   1.151 +                    return string.Format("{0:F1}", 1e-3f * sensor.Value);
   1.152 +                case SensorType.Control:
   1.153 +                    return string.Format("{0:F0}", sensor.Value);
   1.154 +                case SensorType.Level:
   1.155 +                    return string.Format("{0:F0}", sensor.Value);
   1.156 +                case SensorType.Power:
   1.157 +                    return string.Format("{0:F0}", sensor.Value);
   1.158 +                case SensorType.Data:
   1.159 +                    return string.Format("{0:F0}", sensor.Value);
   1.160 +                case SensorType.Factor:
   1.161 +                    return string.Format("{0:F1}", sensor.Value);
   1.162 +            }
   1.163 +            return "-";
   1.164 +        }
   1.165 +
   1.166 +        private Icon CreateTransparentIcon()
   1.167 +        {
   1.168 +            string text = GetString();
   1.169 +            int count = 0;
   1.170 +            for (int i = 0; i < text.Length; i++)
   1.171 +                if ((text[i] >= '0' && text[i] <= '9') || text[i] == '-')
   1.172 +                    count++;
   1.173 +            bool small = count > 2;
   1.174 +
   1.175 +            graphics.Clear(Color.Black);
   1.176 +            TextRenderer.DrawText(graphics, text, small ? smallFont : font,
   1.177 +              new Point(-2, small ? 1 : 0), Color.White, Color.Black);
   1.178 +
   1.179 +            BitmapData data = bitmap.LockBits(
   1.180 +              new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   1.181 +              ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   1.182 +
   1.183 +            IntPtr Scan0 = data.Scan0;
   1.184 +
   1.185 +            int numBytes = bitmap.Width * bitmap.Height * 4;
   1.186 +            byte[] bytes = new byte[numBytes];
   1.187 +            Marshal.Copy(Scan0, bytes, 0, numBytes);
   1.188 +            bitmap.UnlockBits(data);
   1.189 +
   1.190 +            byte red, green, blue;
   1.191 +            for (int i = 0; i < bytes.Length; i += 4)
   1.192 +            {
   1.193 +                blue = bytes[i];
   1.194 +                green = bytes[i + 1];
   1.195 +                red = bytes[i + 2];
   1.196 +
   1.197 +                bytes[i] = color.B;
   1.198 +                bytes[i + 1] = color.G;
   1.199 +                bytes[i + 2] = color.R;
   1.200 +                bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
   1.201 +            }
   1.202 +
   1.203 +            return IconFactory.Create(bytes, bitmap.Width, bitmap.Height,
   1.204 +              PixelFormat.Format32bppArgb);
   1.205 +        }
   1.206 +
   1.207 +        private Icon CreatePercentageIcon()
   1.208 +        {
   1.209 +            try
   1.210 +            {
   1.211 +                graphics.Clear(Color.Transparent);
   1.212 +            }
   1.213 +            catch (ArgumentException)
   1.214 +            {
   1.215 +                graphics.Clear(Color.Black);
   1.216 +            }
   1.217 +            graphics.FillRectangle(darkBrush, 0.5f, -0.5f, bitmap.Width - 2, bitmap.Height);
   1.218 +            float value = sensor.Value.GetValueOrDefault();
   1.219 +            float y = 0.16f * (100 - value);
   1.220 +            graphics.FillRectangle(brush, 0.5f, -0.5f + y, bitmap.Width - 2, bitmap.Height - y);
   1.221 +            graphics.DrawRectangle(pen, 1, 0, bitmap.Width - 3, bitmap.Height - 1);
   1.222 +
   1.223 +            BitmapData data = bitmap.LockBits(
   1.224 +              new Rectangle(0, 0, bitmap.Width, bitmap.Height),
   1.225 +              ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
   1.226 +            byte[] bytes = new byte[bitmap.Width * bitmap.Height * 4];
   1.227 +            Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
   1.228 +            bitmap.UnlockBits(data);
   1.229 +
   1.230 +            return IconFactory.Create(bytes, bitmap.Width, bitmap.Height,
   1.231 +              PixelFormat.Format32bppArgb);
   1.232 +        }
   1.233 +
   1.234 +        public void Update()
   1.235 +        {
   1.236 + 
   1.237 +
   1.238 +            switch (sensor.SensorType)
   1.239 +            {
   1.240 +                case SensorType.Load:
   1.241 +                case SensorType.Control:
   1.242 +                case SensorType.Level:
   1.243 +                    //notifyIcon.Icon = CreatePercentageIcon();
   1.244 +                    break;
   1.245 +                default:
   1.246 +                    //notifyIcon.Icon = CreateTransparentIcon();
   1.247 +                    break;
   1.248 +            }
   1.249 +
   1.250 +
   1.251 +            string format = "";
   1.252 +            switch (sensor.SensorType)
   1.253 +            {
   1.254 +                case SensorType.Voltage: format = "\n{0}: {1:F2} V"; break;
   1.255 +                case SensorType.Clock: format = "\n{0}: {1:F0} MHz"; break;
   1.256 +                case SensorType.Load: format = "\n{0}: {1:F1} %"; break;
   1.257 +                case SensorType.Temperature: format = "\n{0}: {1:F1} °C"; break;
   1.258 +                case SensorType.Fan: format = "\n{0}: {1:F0} RPM"; break;
   1.259 +                case SensorType.Flow: format = "\n{0}: {1:F0} L/h"; break;
   1.260 +                case SensorType.Control: format = "\n{0}: {1:F1} %"; break;
   1.261 +                case SensorType.Level: format = "\n{0}: {1:F1} %"; break;
   1.262 +                case SensorType.Power: format = "\n{0}: {1:F0} W"; break;
   1.263 +                case SensorType.Data: format = "\n{0}: {1:F0} GB"; break;
   1.264 +                case SensorType.Factor: format = "\n{0}: {1:F3} GB"; break;
   1.265 +            }
   1.266 +            string formattedValue = string.Format(format, sensor.Name, sensor.Value);
   1.267 +
   1.268 +            if (sensor.SensorType == SensorType.Temperature &&
   1.269 +              unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
   1.270 +            {
   1.271 +                format = "\n{0}: {1:F1} °F";
   1.272 +                formattedValue = string.Format(format, sensor.Name,
   1.273 +                  UnitManager.CelsiusToFahrenheit(sensor.Value));
   1.274 +            }
   1.275 +
   1.276 +            string hardwareName = sensor.Hardware.Name;
   1.277 +            hardwareName = hardwareName.Substring(0,
   1.278 +              Math.Min(63 - formattedValue.Length, hardwareName.Length));
   1.279 +            string text = hardwareName + formattedValue;
   1.280 +            if (text.Length > 63)
   1.281 +                text = null;
   1.282 +
   1.283 +
   1.284 +        }
   1.285 +    }
   1.286 +}