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