Fixed some dpi scaling bugs that occurred when changing "Control Panel\Appearance and Personalization\Display" to 125% text size or 150% text size.
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;
367 y = Math.Max(y, topBorder + hardwareLineHeight + bottomBorder);
368 this.Size = new Size(width, y);
371 private void DrawBackground(Graphics g) {
375 int b = bottomBorder;
378 GraphicsUnit u = GraphicsUnit.Pixel;
380 g.DrawImage(back, new Rectangle(0, 0, l, t),
381 new Rectangle(0, 0, l, t), u);
382 g.DrawImage(back, new Rectangle(l, 0, w - l - r, t),
383 new Rectangle(l, 0, back.Width - l - r, t), u);
384 g.DrawImage(back, new Rectangle(w - r, 0, r, t),
385 new Rectangle(back.Width - r, 0, r, t), u);
387 g.DrawImage(back, new Rectangle(0, t, l, h - t - b),
388 new Rectangle(0, t, l, back.Height - t - b), u);
389 g.DrawImage(back, new Rectangle(l, t, w - l - r, h - t - b),
390 new Rectangle(l, t, back.Width - l - r, back.Height - t - b), u);
391 g.DrawImage(back, new Rectangle(w - r, t, r, h - t - b),
392 new Rectangle(back.Width - r, t, r, back.Height - t - b), u);
394 g.DrawImage(back, new Rectangle(0, h - b, l, b),
395 new Rectangle(0, back.Height - b, l, b), u);
396 g.DrawImage(back, new Rectangle(l, h - b, w - l - r, b),
397 new Rectangle(l, back.Height - b, back.Width - l - r, b), u);
398 g.DrawImage(back, new Rectangle(w - r, h - b, r, b),
399 new Rectangle(back.Width - r, back.Height - b, r, b), u);
402 private void DrawProgress(Graphics g, float x, float y,
403 float width, float height, float progress)
406 new RectangleF(x + width * progress, y, width * (1 - progress), height),
407 new RectangleF(barBack.Width * progress, 0,
408 (1 - progress) * barBack.Width, barBack.Height),
411 new RectangleF(x, y, width * progress, height),
412 new RectangleF(0, 0, progress * barblue.Width, barblue.Height),
416 protected override void OnPaint(PaintEventArgs e) {
417 Graphics g = e.Graphics;
420 g.Clear(Color.Transparent);
427 if (sensors.Count == 0) {
429 g.DrawString("Add a sensor ...", smallFont, Brushes.White,
430 new Rectangle(x, y - 1, w - rightBorder - x, 0));
433 foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
434 if (hardwareNames.Value) {
436 y += hardwareLineHeight - sensorLineHeight;
438 g.DrawImage(HardwareTypeImage.Instance.GetImage(pair.Key.HardwareType),
439 new Rectangle(x, y + 1, iconSize, iconSize));
441 g.DrawString(pair.Key.Name, largeFont, Brushes.White,
442 new Rectangle(x, y - 1, w - rightBorder - x, 0),
444 y += hardwareLineHeight;
447 foreach (ISensor sensor in pair.Value) {
451 if ((sensor.SensorType != SensorType.Load &&
452 sensor.SensorType != SensorType.Control &&
453 sensor.SensorType != SensorType.Level) || !sensor.Value.HasValue)
457 if (sensor.Value.HasValue) {
459 switch (sensor.SensorType) {
460 case SensorType.Voltage:
463 case SensorType.Clock:
464 format = "{0:F0} MHz";
466 case SensorType.Temperature:
467 format = "{0:F1} °C";
470 format = "{0:F0} RPM";
472 case SensorType.Flow:
473 format = "{0:F0} L/h";
477 if (sensor.SensorType == SensorType.Temperature &&
478 unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) {
479 formatted = string.Format("{0:F1} °F",
480 sensor.Value * 1.8 + 32);
482 formatted = string.Format(format, sensor.Value);
488 g.DrawString(formatted, smallFont, darkWhite,
489 new RectangleF(-1, y - 1, w - rightMargin + 3, 0),
490 alignRightStringFormat);
492 remainingWidth = w - (int)Math.Floor(g.MeasureString(formatted,
493 smallFont, w, StringFormat.GenericTypographic).Width) -
496 DrawProgress(g, w - progressWidth - rightMargin,
497 y + 0.35f * sensorLineHeight, progressWidth,
498 0.6f * sensorLineHeight, 0.01f * sensor.Value.Value);
500 remainingWidth = w - progressWidth - rightMargin;
503 remainingWidth -= leftMargin + 2;
504 if (remainingWidth > 0) {
505 g.DrawString(sensor.Name, smallFont, darkWhite,
506 new RectangleF(leftMargin - 1, y - 1, remainingWidth, 0),
510 y += sensorLineHeight;
515 private class HardwareComparer : IComparer<IHardware> {
516 public int Compare(IHardware x, IHardware y) {
517 if (x == null && y == null)
524 if (x.HardwareType != y.HardwareType)
525 return x.HardwareType.CompareTo(y.HardwareType);
527 return x.Identifier.CompareTo(y.Identifier);