Replaced the more expensive calls to DateTime.Now with DateTime.UtcNow.
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-2011
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 VisibleChanged += delegate(object sender, EventArgs e) {
205 Rectangle bounds = new Rectangle(Location, Size);
206 Screen screen = Screen.FromRectangle(bounds);
207 Rectangle intersection =
208 Rectangle.Intersect(screen.WorkingArea, bounds);
209 if (intersection.Width < Math.Min(16, bounds.Width) ||
210 intersection.Height < Math.Min(16, bounds.Height))
212 Location = new Point(
213 screen.WorkingArea.Width / 2 - bounds.Width / 2,
214 screen.WorkingArea.Height / 2 - bounds.Height / 2);
218 MouseDoubleClick += delegate(object obj, MouseEventArgs args) {
219 SendHideShowCommand();
223 public override void Dispose() {
234 stringFormat.Dispose();
237 trimStringFormat.Dispose();
238 trimStringFormat = null;
240 alignRightStringFormat.Dispose();
241 alignRightStringFormat = null;
246 private void HardwareRemoved(IHardware hardware) {
247 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
248 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
249 foreach (ISensor sensor in hardware.Sensors)
250 SensorRemoved(sensor);
251 foreach (IHardware subHardware in hardware.SubHardware)
252 HardwareRemoved(subHardware);
255 private void HardwareAdded(IHardware hardware) {
256 foreach (ISensor sensor in hardware.Sensors)
258 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
259 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
260 foreach (IHardware subHardware in hardware.SubHardware)
261 HardwareAdded(subHardware);
264 private void SensorAdded(ISensor sensor) {
265 if (settings.GetValue(new Identifier(sensor.Identifier,
266 "gadget").ToString(), false))
270 private void SensorRemoved(ISensor sensor) {
271 if (Contains(sensor))
272 Remove(sensor, false);
275 public bool Contains(ISensor sensor) {
276 foreach (IList<ISensor> list in sensors.Values)
277 if (list.Contains(sensor))
282 public void Add(ISensor sensor) {
283 if (Contains(sensor)) {
286 // get the right hardware
287 IHardware hardware = sensor.Hardware;
288 while (hardware.Parent != null)
289 hardware = hardware.Parent;
291 // get the sensor list associated with the hardware
293 if (!sensors.TryGetValue(hardware, out list)) {
294 list = new List<ISensor>();
295 sensors.Add(hardware, list);
298 // insert the sensor at the right position
300 while (i < list.Count && (list[i].SensorType < sensor.SensorType ||
301 (list[i].SensorType == sensor.SensorType &&
302 list[i].Index < sensor.Index))) i++;
303 list.Insert(i, sensor);
306 new Identifier(sensor.Identifier, "gadget").ToString(), true);
312 public void Remove(ISensor sensor) {
313 Remove(sensor, true);
316 private void Remove(ISensor sensor, bool deleteConfig) {
318 settings.Remove(new Identifier(sensor.Identifier, "gadget").ToString());
320 foreach (KeyValuePair<IHardware, IList<ISensor>> keyValue in sensors)
321 if (keyValue.Value.Contains(sensor)) {
322 keyValue.Value.Remove(sensor);
323 if (keyValue.Value.Count == 0) {
324 sensors.Remove(keyValue.Key);
331 public event EventHandler HideShowCommand;
333 public void SendHideShowCommand() {
334 if (HideShowCommand != null)
335 HideShowCommand(this, null);
338 private Font CreateFont(float size, FontStyle style) {
340 return new Font(SystemFonts.MessageBoxFont.FontFamily, size, style);
341 } catch (ArgumentException) {
342 // if the style is not supported, fall back to the original one
343 return new Font(SystemFonts.MessageBoxFont.FontFamily, size,
344 SystemFonts.MessageBoxFont.Style);
348 private void SetFontSize(float size) {
350 largeFont = CreateFont(fontSize, FontStyle.Bold);
351 smallFont = CreateFont(fontSize, FontStyle.Regular);
353 double scaledFontSize = fontSize * scale;
354 iconSize = (int)Math.Round(1.5 * scaledFontSize);
355 hardwareLineHeight = (int)Math.Round(1.66 * scaledFontSize);
356 sensorLineHeight = (int)Math.Round(1.33 * scaledFontSize);
357 leftMargin = leftBorder + (int)Math.Round(0.3 * scaledFontSize);
358 rightMargin = rightBorder + (int)Math.Round(0.3 * scaledFontSize);
359 topMargin = topBorder;
360 bottomMargin = bottomBorder + (int)Math.Round(0.3 * scaledFontSize);
361 progressWidth = (int)Math.Round(5.3 * scaledFontSize);
363 Resize((int)Math.Round(17.3 * scaledFontSize));
366 private void Resize() {
367 Resize(this.Size.Width);
370 private void Resize(int width) {
372 foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
373 if (hardwareNames.Value) {
375 y += hardwareLineHeight - sensorLineHeight;
376 y += hardwareLineHeight;
378 y += pair.Value.Count * sensorLineHeight;
380 if (sensors.Count == 0)
381 y += 4 * sensorLineHeight + hardwareLineHeight;
383 this.Size = new Size(width, y);
386 private void DrawBackground(Graphics g) {
390 int b = bottomBorder;
393 GraphicsUnit u = GraphicsUnit.Pixel;
395 g.DrawImage(back, new Rectangle(0, 0, l, t),
396 new Rectangle(0, 0, l, t), u);
397 g.DrawImage(back, new Rectangle(l, 0, w - l - r, t),
398 new Rectangle(l, 0, back.Width - l - r, t), u);
399 g.DrawImage(back, new Rectangle(w - r, 0, r, t),
400 new Rectangle(back.Width - r, 0, r, t), u);
402 g.DrawImage(back, new Rectangle(0, t, l, h - t - b),
403 new Rectangle(0, t, l, back.Height - t - b), u);
404 g.DrawImage(back, new Rectangle(l, t, w - l - r, h - t - b),
405 new Rectangle(l, t, back.Width - l - r, back.Height - t - b), u);
406 g.DrawImage(back, new Rectangle(w - r, t, r, h - t - b),
407 new Rectangle(back.Width - r, t, r, back.Height - t - b), u);
409 g.DrawImage(back, new Rectangle(0, h - b, l, b),
410 new Rectangle(0, back.Height - b, l, b), u);
411 g.DrawImage(back, new Rectangle(l, h - b, w - l - r, b),
412 new Rectangle(l, back.Height - b, back.Width - l - r, b), u);
413 g.DrawImage(back, new Rectangle(w - r, h - b, r, b),
414 new Rectangle(back.Width - r, back.Height - b, r, b), u);
417 private void DrawProgress(Graphics g, float x, float y,
418 float width, float height, float progress)
421 new RectangleF(x + width * progress, y, width * (1 - progress), height),
422 new RectangleF(barBack.Width * progress, 0,
423 (1 - progress) * barBack.Width, barBack.Height),
426 new RectangleF(x, y, width * progress, height),
427 new RectangleF(0, 0, progress * barblue.Width, barblue.Height),
431 protected override void OnPaint(PaintEventArgs e) {
432 Graphics g = e.Graphics;
435 g.Clear(Color.Transparent);
442 if (sensors.Count == 0) {
444 g.DrawString("Right-click on a sensor in the main window and select " +
445 "\"Show in Gadget\" to show the sensor here.",
446 smallFont, Brushes.White,
447 new Rectangle(x, y - 1, w - rightBorder - x, 0));
450 foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
451 if (hardwareNames.Value) {
453 y += hardwareLineHeight - sensorLineHeight;
455 g.DrawImage(HardwareTypeImage.Instance.GetImage(pair.Key.HardwareType),
456 new Rectangle(x, y + 1, iconSize, iconSize));
458 g.DrawString(pair.Key.Name, largeFont, Brushes.White,
459 new Rectangle(x, y - 1, w - rightBorder - x, 0),
461 y += hardwareLineHeight;
464 foreach (ISensor sensor in pair.Value) {
468 if ((sensor.SensorType != SensorType.Load &&
469 sensor.SensorType != SensorType.Control &&
470 sensor.SensorType != SensorType.Level) || !sensor.Value.HasValue)
474 if (sensor.Value.HasValue) {
476 switch (sensor.SensorType) {
477 case SensorType.Voltage:
480 case SensorType.Clock:
481 format = "{0:F0} MHz";
483 case SensorType.Temperature:
484 format = "{0:F1} °C";
487 format = "{0:F0} RPM";
489 case SensorType.Flow:
490 format = "{0:F0} L/h";
494 if (sensor.SensorType == SensorType.Temperature &&
495 unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) {
496 formatted = string.Format("{0:F1} °F",
497 sensor.Value * 1.8 + 32);
499 formatted = string.Format(format, sensor.Value);
505 g.DrawString(formatted, smallFont, darkWhite,
506 new RectangleF(-1, y - 1, w - rightMargin + 3, 0),
507 alignRightStringFormat);
509 remainingWidth = w - (int)Math.Floor(g.MeasureString(formatted,
510 smallFont, w, StringFormat.GenericTypographic).Width) -
513 DrawProgress(g, w - progressWidth - rightMargin,
514 y + 0.35f * sensorLineHeight, progressWidth,
515 0.6f * sensorLineHeight, 0.01f * sensor.Value.Value);
517 remainingWidth = w - progressWidth - rightMargin;
520 remainingWidth -= leftMargin + 2;
521 if (remainingWidth > 0) {
522 g.DrawString(sensor.Name, smallFont, darkWhite,
523 new RectangleF(leftMargin - 1, y - 1, remainingWidth, 0),
527 y += sensorLineHeight;
532 private class HardwareComparer : IComparer<IHardware> {
533 public int Compare(IHardware x, IHardware y) {
534 if (x == null && y == null)
541 if (x.HardwareType != y.HardwareType)
542 return x.HardwareType.CompareTo(y.HardwareType);
544 return x.Identifier.CompareTo(y.Identifier);