Fixed Issue 219.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
41 using System.Windows.Forms;
42 using OpenHardwareMonitor.Hardware;
44 namespace OpenHardwareMonitor.GUI {
45 public class SensorGadget : Gadget {
47 private UnitManager unitManager;
49 private Image back = Utilities.EmbeddedResources.GetImage("gadget.png");
50 private Image barBack = Utilities.EmbeddedResources.GetImage("barback.png");
51 private Image barblue = Utilities.EmbeddedResources.GetImage("barblue.png");
52 private const int topBorder = 6;
53 private const int bottomBorder = 7;
54 private const int leftBorder = 6;
55 private const int rightBorder = 7;
57 private readonly float scale;
58 private float fontSize;
60 private int hardwareLineHeight;
61 private int sensorLineHeight;
62 private int rightMargin;
63 private int leftMargin;
64 private int topMargin;
65 private int bottomMargin;
66 private int progressWidth;
68 private IDictionary<IHardware, IList<ISensor>> sensors =
69 new SortedDictionary<IHardware, IList<ISensor>>(new HardwareComparer());
71 private PersistentSettings settings;
72 private UserOption hardwareNames;
73 private UserOption alwaysOnTop;
74 private UserOption lockPositionAndSize;
76 private Font largeFont;
77 private Font smallFont;
78 private Brush darkWhite;
79 private StringFormat stringFormat;
80 private StringFormat trimStringFormat;
81 private StringFormat alignRightStringFormat;
83 public SensorGadget(IComputer computer, PersistentSettings settings,
84 UnitManager unitManager)
86 this.unitManager = unitManager;
87 this.settings = settings;
88 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
89 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
91 this.darkWhite = new SolidBrush(Color.FromArgb(0xF0, 0xF0, 0xF0));
93 this.stringFormat = new StringFormat();
94 this.stringFormat.FormatFlags = StringFormatFlags.NoWrap;
96 this.trimStringFormat = new StringFormat();
97 this.trimStringFormat.Trimming = StringTrimming.EllipsisCharacter;
98 this.trimStringFormat.FormatFlags = StringFormatFlags.NoWrap;
100 this.alignRightStringFormat = new StringFormat();
101 this.alignRightStringFormat.Alignment = StringAlignment.Far;
102 this.alignRightStringFormat.FormatFlags = StringFormatFlags.NoWrap;
104 this.Location = new Point(
105 settings.GetValue("sensorGadget.Location.X", 100),
106 settings.GetValue("sensorGadget.Location.Y", 100));
107 LocationChanged += delegate(object sender, EventArgs e) {
108 settings.SetValue("sensorGadget.Location.X", Location.X);
109 settings.SetValue("sensorGadget.Location.Y", Location.Y);
112 // get the custom to default dpi ratio
113 using (Bitmap b = new Bitmap(1, 1)) {
114 scale = b.HorizontalResolution / 96.0f;
117 SetFontSize(settings.GetValue("sensorGadget.FontSize", 7.5f));
118 Resize(settings.GetValue("sensorGadget.Width", Size.Width));
120 ContextMenu contextMenu = new ContextMenu();
121 MenuItem hardwareNamesItem = new MenuItem("Hardware Names");
122 contextMenu.MenuItems.Add(hardwareNamesItem);
123 MenuItem fontSizeMenu = new MenuItem("Font Size");
124 for (int i = 0; i < 4; i++) {
128 case 0: size = 6.5f; name = "Small"; break;
129 case 1: size = 7.5f; name = "Medium"; break;
130 case 2: size = 9f; name = "Large"; break;
131 case 3: size = 11f; name = "Very Large"; break;
132 default: throw new NotImplementedException();
134 MenuItem item = new MenuItem(name);
135 item.Checked = fontSize == size;
136 item.Click += delegate(object sender, EventArgs e) {
138 settings.SetValue("sensorGadget.FontSize", size);
139 foreach (MenuItem mi in fontSizeMenu.MenuItems)
140 mi.Checked = mi == item;
142 fontSizeMenu.MenuItems.Add(item);
144 contextMenu.MenuItems.Add(fontSizeMenu);
145 contextMenu.MenuItems.Add(new MenuItem("-"));
146 MenuItem lockItem = new MenuItem("Lock Position and Size");
147 contextMenu.MenuItems.Add(lockItem);
148 contextMenu.MenuItems.Add(new MenuItem("-"));
149 MenuItem alwaysOnTopItem = new MenuItem("Always on Top");
150 contextMenu.MenuItems.Add(alwaysOnTopItem);
151 MenuItem opacityMenu = new MenuItem("Opacity");
152 contextMenu.MenuItems.Add(opacityMenu);
153 Opacity = (byte)settings.GetValue("sensorGadget.Opacity", 255);
154 for (int i = 0; i < 5; i++) {
155 MenuItem item = new MenuItem((20 * (i + 1)).ToString() + " %");
156 byte o = (byte)(51 * (i + 1));
157 item.Checked = Opacity == o;
158 item.Click += delegate(object sender, EventArgs e) {
160 settings.SetValue("sensorGadget.Opacity", Opacity);
161 foreach (MenuItem mi in opacityMenu.MenuItems)
162 mi.Checked = mi == item;
164 opacityMenu.MenuItems.Add(item);
166 this.ContextMenu = contextMenu;
168 hardwareNames = new UserOption("sensorGadget.Hardwarenames", true,
169 hardwareNamesItem, settings);
170 hardwareNames.Changed += delegate(object sender, EventArgs e) {
174 alwaysOnTop = new UserOption("sensorGadget.AlwaysOnTop", false,
175 alwaysOnTopItem, settings);
176 alwaysOnTop.Changed += delegate(object sender, EventArgs e) {
177 this.AlwaysOnTop = alwaysOnTop.Value;
179 lockPositionAndSize = new UserOption("sensorGadget.LockPositionAndSize",
180 false, lockItem, settings);
181 lockPositionAndSize.Changed += delegate(object sender, EventArgs e) {
182 this.LockPositionAndSize = lockPositionAndSize.Value;
185 HitTest += delegate(object sender, HitTestEventArgs e) {
186 if (lockPositionAndSize.Value)
189 if (e.Location.X < leftBorder) {
190 e.HitResult = HitResult.Left;
193 if (e.Location.X > Size.Width - 1 - rightBorder) {
194 e.HitResult = HitResult.Right;
199 SizeChanged += delegate(object sender, EventArgs e) {
200 settings.SetValue("sensorGadget.Width", Size.Width);
204 MouseDoubleClick += delegate(object obj, MouseEventArgs args) {
205 SendHideShowCommand();
209 public override void Dispose() {
220 stringFormat.Dispose();
223 trimStringFormat.Dispose();
224 trimStringFormat = null;
226 alignRightStringFormat.Dispose();
227 alignRightStringFormat = null;
232 private void HardwareRemoved(IHardware hardware) {
233 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
234 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
235 foreach (ISensor sensor in hardware.Sensors)
236 SensorRemoved(sensor);
237 foreach (IHardware subHardware in hardware.SubHardware)
238 HardwareRemoved(subHardware);
241 private void HardwareAdded(IHardware hardware) {
242 foreach (ISensor sensor in hardware.Sensors)
244 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
245 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
246 foreach (IHardware subHardware in hardware.SubHardware)
247 HardwareAdded(subHardware);
250 private void SensorAdded(ISensor sensor) {
251 if (settings.GetValue(new Identifier(sensor.Identifier,
252 "gadget").ToString(), false))
256 private void SensorRemoved(ISensor sensor) {
257 if (Contains(sensor))
258 Remove(sensor, false);
261 public bool Contains(ISensor sensor) {
262 foreach (IList<ISensor> list in sensors.Values)
263 if (list.Contains(sensor))
268 public void Add(ISensor sensor) {
269 if (Contains(sensor)) {
272 // get the right hardware
273 IHardware hardware = sensor.Hardware;
274 while (hardware.Parent != null)
275 hardware = hardware.Parent;
277 // get the sensor list associated with the hardware
279 if (!sensors.TryGetValue(hardware, out list)) {
280 list = new List<ISensor>();
281 sensors.Add(hardware, list);
284 // insert the sensor at the right position
286 while (i < list.Count && (list[i].SensorType < sensor.SensorType ||
287 (list[i].SensorType == sensor.SensorType &&
288 list[i].Index < sensor.Index))) i++;
289 list.Insert(i, sensor);
292 new Identifier(sensor.Identifier, "gadget").ToString(), true);
298 public void Remove(ISensor sensor) {
299 Remove(sensor, true);
302 private void Remove(ISensor sensor, bool deleteConfig) {
304 settings.Remove(new Identifier(sensor.Identifier, "gadget").ToString());
306 foreach (KeyValuePair<IHardware, IList<ISensor>> keyValue in sensors)
307 if (keyValue.Value.Contains(sensor)) {
308 keyValue.Value.Remove(sensor);
309 if (keyValue.Value.Count == 0) {
310 sensors.Remove(keyValue.Key);
317 public event EventHandler HideShowCommand;
319 public void SendHideShowCommand() {
320 if (HideShowCommand != null)
321 HideShowCommand(this, null);
324 private Font CreateFont(float size, FontStyle style) {
326 return new Font(SystemFonts.MessageBoxFont.FontFamily, size, style);
327 } catch (ArgumentException) {
328 // if the style is not supported, fall back to the original one
329 return new Font(SystemFonts.MessageBoxFont.FontFamily, size,
330 SystemFonts.MessageBoxFont.Style);
334 private void SetFontSize(float size) {
336 largeFont = CreateFont(fontSize, FontStyle.Bold);
337 smallFont = CreateFont(fontSize, FontStyle.Regular);
339 double scaledFontSize = fontSize * scale;
340 iconSize = (int)Math.Round(1.5 * scaledFontSize);
341 hardwareLineHeight = (int)Math.Round(1.66 * scaledFontSize);
342 sensorLineHeight = (int)Math.Round(1.33 * scaledFontSize);
343 leftMargin = leftBorder + (int)Math.Round(0.3 * scaledFontSize);
344 rightMargin = rightBorder + (int)Math.Round(0.3 * scaledFontSize);
345 topMargin = topBorder;
346 bottomMargin = bottomBorder + (int)Math.Round(0.3 * scaledFontSize);
347 progressWidth = (int)Math.Round(5.3 * scaledFontSize);
349 Resize((int)Math.Round(17.3 * scaledFontSize));
352 private void Resize() {
353 Resize(this.Size.Width);
356 private void Resize(int width) {
358 foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
359 if (hardwareNames.Value) {
361 y += hardwareLineHeight - sensorLineHeight;
362 y += hardwareLineHeight;
364 y += pair.Value.Count * sensorLineHeight;
366 if (sensors.Count == 0)
367 y += 4 * sensorLineHeight + hardwareLineHeight;
369 this.Size = new Size(width, y);
372 private void DrawBackground(Graphics g) {
376 int b = bottomBorder;
379 GraphicsUnit u = GraphicsUnit.Pixel;
381 g.DrawImage(back, new Rectangle(0, 0, l, t),
382 new Rectangle(0, 0, l, t), u);
383 g.DrawImage(back, new Rectangle(l, 0, w - l - r, t),
384 new Rectangle(l, 0, back.Width - l - r, t), u);
385 g.DrawImage(back, new Rectangle(w - r, 0, r, t),
386 new Rectangle(back.Width - r, 0, r, t), u);
388 g.DrawImage(back, new Rectangle(0, t, l, h - t - b),
389 new Rectangle(0, t, l, back.Height - t - b), u);
390 g.DrawImage(back, new Rectangle(l, t, w - l - r, h - t - b),
391 new Rectangle(l, t, back.Width - l - r, back.Height - t - b), u);
392 g.DrawImage(back, new Rectangle(w - r, t, r, h - t - b),
393 new Rectangle(back.Width - r, t, r, back.Height - t - b), u);
395 g.DrawImage(back, new Rectangle(0, h - b, l, b),
396 new Rectangle(0, back.Height - b, l, b), u);
397 g.DrawImage(back, new Rectangle(l, h - b, w - l - r, b),
398 new Rectangle(l, back.Height - b, back.Width - l - r, b), u);
399 g.DrawImage(back, new Rectangle(w - r, h - b, r, b),
400 new Rectangle(back.Width - r, back.Height - b, r, b), u);
403 private void DrawProgress(Graphics g, float x, float y,
404 float width, float height, float progress)
407 new RectangleF(x + width * progress, y, width * (1 - progress), height),
408 new RectangleF(barBack.Width * progress, 0,
409 (1 - progress) * barBack.Width, barBack.Height),
412 new RectangleF(x, y, width * progress, height),
413 new RectangleF(0, 0, progress * barblue.Width, barblue.Height),
417 protected override void OnPaint(PaintEventArgs e) {
418 Graphics g = e.Graphics;
421 g.Clear(Color.Transparent);
428 if (sensors.Count == 0) {
430 g.DrawString("Right-click on a sensor in the main window and select " +
431 "\"Show in Gadget\" to show the sensor here.",
432 smallFont, Brushes.White,
433 new Rectangle(x, y - 1, w - rightBorder - x, 0));
436 foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
437 if (hardwareNames.Value) {
439 y += hardwareLineHeight - sensorLineHeight;
441 g.DrawImage(HardwareTypeImage.Instance.GetImage(pair.Key.HardwareType),
442 new Rectangle(x, y + 1, iconSize, iconSize));
444 g.DrawString(pair.Key.Name, largeFont, Brushes.White,
445 new Rectangle(x, y - 1, w - rightBorder - x, 0),
447 y += hardwareLineHeight;
450 foreach (ISensor sensor in pair.Value) {
454 if ((sensor.SensorType != SensorType.Load &&
455 sensor.SensorType != SensorType.Control &&
456 sensor.SensorType != SensorType.Level) || !sensor.Value.HasValue)
460 if (sensor.Value.HasValue) {
462 switch (sensor.SensorType) {
463 case SensorType.Voltage:
466 case SensorType.Clock:
467 format = "{0:F0} MHz";
469 case SensorType.Temperature:
470 format = "{0:F1} °C";
473 format = "{0:F0} RPM";
475 case SensorType.Flow:
476 format = "{0:F0} L/h";
480 if (sensor.SensorType == SensorType.Temperature &&
481 unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) {
482 formatted = string.Format("{0:F1} °F",
483 sensor.Value * 1.8 + 32);
485 formatted = string.Format(format, sensor.Value);
491 g.DrawString(formatted, smallFont, darkWhite,
492 new RectangleF(-1, y - 1, w - rightMargin + 3, 0),
493 alignRightStringFormat);
495 remainingWidth = w - (int)Math.Floor(g.MeasureString(formatted,
496 smallFont, w, StringFormat.GenericTypographic).Width) -
499 DrawProgress(g, w - progressWidth - rightMargin,
500 y + 0.35f * sensorLineHeight, progressWidth,
501 0.6f * sensorLineHeight, 0.01f * sensor.Value.Value);
503 remainingWidth = w - progressWidth - rightMargin;
506 remainingWidth -= leftMargin + 2;
507 if (remainingWidth > 0) {
508 g.DrawString(sensor.Name, smallFont, darkWhite,
509 new RectangleF(leftMargin - 1, y - 1, remainingWidth, 0),
513 y += sensorLineHeight;
518 private class HardwareComparer : IComparer<IHardware> {
519 public int Compare(IHardware x, IHardware y) {
520 if (x == null && y == null)
527 if (x.HardwareType != y.HardwareType)
528 return x.HardwareType.CompareTo(y.HardwareType);
530 return x.Identifier.CompareTo(y.Identifier);