StephaneLenclud@433: /* StephaneLenclud@433: StephaneLenclud@433: This Source Code Form is subject to the terms of the Mozilla Public StephaneLenclud@433: License, v. 2.0. If a copy of the MPL was not distributed with this StephaneLenclud@433: file, You can obtain one at http://mozilla.org/MPL/2.0/. StephaneLenclud@433: StephaneLenclud@433: Copyright (C) 2009-2012 Michael Möller StephaneLenclud@433: StephaneLenclud@433: */ StephaneLenclud@433: StephaneLenclud@433: using System; StephaneLenclud@433: using System.Drawing; StephaneLenclud@433: using System.Drawing.Drawing2D; StephaneLenclud@433: using System.Drawing.Imaging; StephaneLenclud@433: using System.Drawing.Text; StephaneLenclud@433: using System.Runtime.InteropServices; StephaneLenclud@433: using System.Windows.Forms; StephaneLenclud@433: using OpenHardwareMonitor.Hardware; StephaneLenclud@433: using OpenHardwareMonitor.Utilities; StephaneLenclud@433: StephaneLenclud@433: namespace OpenHardwareMonitor.GUI StephaneLenclud@433: { StephaneLenclud@433: public class SensorFrontView : IDisposable StephaneLenclud@433: { StephaneLenclud@433: StephaneLenclud@433: private UnitManager unitManager; StephaneLenclud@433: StephaneLenclud@433: private ISensor sensor; StephaneLenclud@433: private Color color; StephaneLenclud@433: private Color darkColor; StephaneLenclud@433: private Font font; StephaneLenclud@433: private Font smallFont; StephaneLenclud@438: public string iFirstLine; StephaneLenclud@438: public string iSecondLine; StephaneLenclud@438: StephaneLenclud@433: StephaneLenclud@433: public SensorFrontView(SoundGraphDisplay soundGraphDisplay, ISensor sensor, StephaneLenclud@433: bool balloonTip, PersistentSettings settings, UnitManager unitManager) StephaneLenclud@433: { StephaneLenclud@433: this.unitManager = unitManager; StephaneLenclud@433: this.sensor = sensor; StephaneLenclud@433: StephaneLenclud@433: // get the default dpi to create an icon with the correct size StephaneLenclud@433: float dpiX, dpiY; StephaneLenclud@433: using (Bitmap b = new Bitmap(1, 1, PixelFormat.Format32bppArgb)) StephaneLenclud@433: { StephaneLenclud@433: dpiX = b.HorizontalResolution; StephaneLenclud@433: dpiY = b.VerticalResolution; StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: // adjust the size of the icon to current dpi (default is 16x16 at 96 dpi) StephaneLenclud@433: int width = (int)Math.Round(16 * dpiX / 96); StephaneLenclud@433: int height = (int)Math.Round(16 * dpiY / 96); StephaneLenclud@433: StephaneLenclud@433: // make sure it does never get smaller than 16x16 StephaneLenclud@433: width = width < 16 ? 16 : width; StephaneLenclud@433: height = height < 16 ? 16 : height; StephaneLenclud@433: StephaneLenclud@433: // adjust the font size to the icon size StephaneLenclud@433: FontFamily family = SystemFonts.MessageBoxFont.FontFamily; StephaneLenclud@433: float baseSize; StephaneLenclud@433: switch (family.Name) StephaneLenclud@433: { StephaneLenclud@433: case "Segoe UI": baseSize = 12; break; StephaneLenclud@433: case "Tahoma": baseSize = 11; break; StephaneLenclud@433: default: baseSize = 12; break; StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: this.font = new Font(family, StephaneLenclud@433: baseSize * width / 16.0f, GraphicsUnit.Pixel); StephaneLenclud@433: this.smallFont = new Font(family, StephaneLenclud@433: 0.75f * baseSize * width / 16.0f, GraphicsUnit.Pixel); StephaneLenclud@433: StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: public ISensor Sensor StephaneLenclud@433: { StephaneLenclud@433: get { return sensor; } StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: public Color Color StephaneLenclud@433: { StephaneLenclud@433: get { return color; } StephaneLenclud@433: set StephaneLenclud@433: { StephaneLenclud@433: this.color = value; StephaneLenclud@433: this.darkColor = Color.FromArgb(255, StephaneLenclud@433: this.color.R / 3, StephaneLenclud@433: this.color.G / 3, StephaneLenclud@433: this.color.B / 3); StephaneLenclud@433: } StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: public void Dispose() StephaneLenclud@433: { StephaneLenclud@433: font.Dispose(); StephaneLenclud@433: smallFont.Dispose(); StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@438: public string GetString() StephaneLenclud@433: { StephaneLenclud@433: if (!sensor.Value.HasValue) StephaneLenclud@433: return "-"; StephaneLenclud@433: StephaneLenclud@433: switch (sensor.SensorType) StephaneLenclud@433: { StephaneLenclud@433: case SensorType.Voltage: StephaneLenclud@433: return string.Format("{0:F1}", sensor.Value); StephaneLenclud@433: case SensorType.Clock: StephaneLenclud@433: return string.Format("{0:F1}", 1e-3f * sensor.Value); StephaneLenclud@433: case SensorType.Load: StephaneLenclud@433: return string.Format("{0:F0}", sensor.Value); StephaneLenclud@433: case SensorType.Temperature: StephaneLenclud@433: if (unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) StephaneLenclud@433: return string.Format("{0:F0}", StephaneLenclud@433: UnitManager.CelsiusToFahrenheit(sensor.Value)); StephaneLenclud@433: else StephaneLenclud@433: return string.Format("{0:F0}", sensor.Value); StephaneLenclud@433: case SensorType.Fan: StephaneLenclud@433: return string.Format("{0:F1}", 1e-3f * sensor.Value); StephaneLenclud@433: case SensorType.Flow: StephaneLenclud@433: return string.Format("{0:F1}", 1e-3f * sensor.Value); StephaneLenclud@433: case SensorType.Control: StephaneLenclud@433: return string.Format("{0:F0}", sensor.Value); StephaneLenclud@433: case SensorType.Level: StephaneLenclud@433: return string.Format("{0:F0}", sensor.Value); StephaneLenclud@433: case SensorType.Power: StephaneLenclud@433: return string.Format("{0:F0}", sensor.Value); StephaneLenclud@433: case SensorType.Data: StephaneLenclud@433: return string.Format("{0:F0}", sensor.Value); StephaneLenclud@433: case SensorType.Factor: StephaneLenclud@433: return string.Format("{0:F1}", sensor.Value); StephaneLenclud@433: } StephaneLenclud@433: return "-"; StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: StephaneLenclud@433: public void Update() StephaneLenclud@433: { StephaneLenclud@433: StephaneLenclud@433: StephaneLenclud@433: switch (sensor.SensorType) StephaneLenclud@433: { StephaneLenclud@433: case SensorType.Load: StephaneLenclud@433: case SensorType.Control: StephaneLenclud@433: case SensorType.Level: StephaneLenclud@433: //notifyIcon.Icon = CreatePercentageIcon(); StephaneLenclud@433: break; StephaneLenclud@433: default: StephaneLenclud@433: //notifyIcon.Icon = CreateTransparentIcon(); StephaneLenclud@433: break; StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: StephaneLenclud@433: string format = ""; StephaneLenclud@433: switch (sensor.SensorType) StephaneLenclud@433: { StephaneLenclud@439: case SensorType.Voltage: format = "{0:F2}V"; break; StephaneLenclud@439: case SensorType.Clock: format = "{0:F0}MHz"; break; StephaneLenclud@444: case SensorType.Load: format = "{0:F0}%"; break; StephaneLenclud@439: //iMON VFD escape sequence for Celsius StephaneLenclud@439: case SensorType.Temperature: format = "{0:F0}\x001A"; break; StephaneLenclud@444: case SensorType.Fan: format = "{0:F0}*"; break; //RPM StephaneLenclud@439: case SensorType.Flow: format = "{0:F0}L/h"; break; StephaneLenclud@444: case SensorType.Control: format = "{0:F0}%"; break; StephaneLenclud@444: case SensorType.Level: format = "{0:F0}%"; break; StephaneLenclud@439: case SensorType.Power: format = "{0:F0}W"; break; StephaneLenclud@439: case SensorType.Data: format = "{0:F0}GB"; break; StephaneLenclud@439: case SensorType.Factor: format = "{0:F3}GB"; break; StephaneLenclud@433: } StephaneLenclud@439: string formattedValue = string.Format(format, sensor.Value); StephaneLenclud@433: StephaneLenclud@433: if (sensor.SensorType == SensorType.Temperature && StephaneLenclud@433: unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) StephaneLenclud@433: { StephaneLenclud@439: //iMON VFD escape sequence for Fahrenheit StephaneLenclud@439: format = "{0:F0}\x001B"; StephaneLenclud@439: formattedValue = string.Format(format, UnitManager.CelsiusToFahrenheit(sensor.Value)); StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@439: //iFirstLine = sensor.Hardware.Name; StephaneLenclud@439: //iSecondLine = sensor.Name+ ":" + formattedValue; StephaneLenclud@439: StephaneLenclud@439: iFirstLine = sensor.Name; StephaneLenclud@438: iSecondLine = formattedValue; StephaneLenclud@433: StephaneLenclud@439: StephaneLenclud@433: } StephaneLenclud@433: } StephaneLenclud@433: }