Added a desktop gadget implementation.
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 = 4;
53 private const int bottomBorder = 6;
54 private const int leftBorder = 6;
55 private const int rightBorder = 6;
56 private const int iconSize = 11;
57 private const int hardwareLineHeight = 13;
58 private const int sensorLineHeight = 11;
60 private IDictionary<IHardware, IList<ISensor>> sensors =
61 new SortedDictionary<IHardware, IList<ISensor>>(new HardwareComparer());
63 private PersistentSettings settings;
64 private UserOption alwaysOnTop;
65 private UserOption lockPosition;
67 private Font largeFont;
68 private Font smallFont;
69 private Brush darkWhite = new SolidBrush(Color.FromArgb(0xF0, 0xF0, 0xF0));
71 public SensorGadget(IComputer computer, PersistentSettings settings,
72 UnitManager unitManager)
74 this.unitManager = unitManager;
75 this.settings = settings;
76 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
77 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
79 this.largeFont = new Font(SystemFonts.MessageBoxFont.FontFamily, 7.5f,
81 this.smallFont = new Font(SystemFonts.MessageBoxFont.FontFamily, 6.5f);
83 this.Location = new Point(
84 settings.GetValue("sensorGadget.Location.X", 100),
85 settings.GetValue("sensorGadget.Location.Y", 100));
86 LocationChanged += delegate(object sender, EventArgs e) {
87 settings.SetValue("sensorGadget.Location.X", Location.X);
88 settings.SetValue("sensorGadget.Location.Y", Location.Y);
91 ContextMenu contextMenu = new ContextMenu();
92 MenuItem lockItem = new MenuItem("Lock Position");
93 contextMenu.MenuItems.Add(lockItem);
94 contextMenu.MenuItems.Add(new MenuItem("-"));
95 MenuItem alwaysOnTopItem = new MenuItem("Always on Top");
96 contextMenu.MenuItems.Add(alwaysOnTopItem);
97 MenuItem opacityMenu = new MenuItem("Opacity");
98 contextMenu.MenuItems.Add(opacityMenu);
99 Opacity = (byte)settings.GetValue("sensorGadget.Opacity", 255);
100 for (int i = 0; i < 5; i++) {
101 MenuItem item = new MenuItem((20 * (i + 1)).ToString() + " %");
102 byte o = (byte)(51 * (i + 1));
104 item.Checked = Opacity == o;
105 item.Click += delegate(object sender, EventArgs e) {
106 Opacity = (byte)item.Tag;
107 settings.SetValue("sensorGadget.Opacity", Opacity);
108 foreach (MenuItem mi in opacityMenu.MenuItems)
109 mi.Checked = (byte)mi.Tag == Opacity;
111 opacityMenu.MenuItems.Add(item);
113 this.ContextMenu = contextMenu;
115 alwaysOnTop = new UserOption("sensorGadget.AlwaysOnTop", false,
116 alwaysOnTopItem, settings);
117 alwaysOnTop.Changed += delegate(object sender, EventArgs e) {
118 this.AlwaysOnTop = alwaysOnTop.Value;
120 lockPosition = new UserOption("sensorGadget.LockPosition", false,
122 lockPosition.Changed += delegate(object sender, EventArgs e) {
123 this.LockPosition = lockPosition.Value;
129 private void HardwareRemoved(IHardware hardware) {
130 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
131 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
132 foreach (ISensor sensor in hardware.Sensors)
133 SensorRemoved(sensor);
134 foreach (IHardware subHardware in hardware.SubHardware)
135 HardwareRemoved(subHardware);
138 private void HardwareAdded(IHardware hardware) {
139 foreach (ISensor sensor in hardware.Sensors)
141 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
142 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
143 foreach (IHardware subHardware in hardware.SubHardware)
144 HardwareAdded(subHardware);
147 private void SensorAdded(ISensor sensor) {
148 if (settings.GetValue(new Identifier(sensor.Identifier,
149 "gadget").ToString(), false))
153 private void SensorRemoved(ISensor sensor) {
154 if (Contains(sensor))
155 Remove(sensor, false);
158 public bool Contains(ISensor sensor) {
159 foreach (IList<ISensor> list in sensors.Values)
160 if (list.Contains(sensor))
165 public void Add(ISensor sensor) {
166 if (Contains(sensor)) {
169 // get the right hardware
170 IHardware hardware = sensor.Hardware;
171 while (hardware.Parent != null)
172 hardware = hardware.Parent;
174 // get the sensor list associated with the hardware
176 if (!sensors.TryGetValue(hardware, out list)) {
177 list = new List<ISensor>();
178 sensors.Add(hardware, list);
181 // insert the sensor at the right position
183 while (i < list.Count && (list[i].SensorType < sensor.SensorType ||
184 (list[i].SensorType == sensor.SensorType &&
185 list[i].Index < sensor.Index))) i++;
186 list.Insert(i, sensor);
189 new Identifier(sensor.Identifier, "gadget").ToString(), true);
196 public void Remove(ISensor sensor) {
197 Remove(sensor, true);
200 private void Remove(ISensor sensor, bool deleteConfig) {
202 settings.Remove(new Identifier(sensor.Identifier, "gadget").ToString());
204 foreach (KeyValuePair<IHardware, IList<ISensor>> keyValue in sensors)
205 if (keyValue.Value.Contains(sensor)) {
206 keyValue.Value.Remove(sensor);
207 if (keyValue.Value.Count == 0) {
208 sensors.Remove(keyValue.Key);
216 private void Resize() {
217 int y = topBorder + 1;
218 foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
219 y += hardwareLineHeight;
220 y += pair.Value.Count * sensorLineHeight;
222 y += bottomBorder + 2;
223 y = Math.Max(y, topBorder + bottomBorder + 10);
224 this.Size = new Size(130, y);
227 private void DrawBackground(Graphics g) {
231 int b = bottomBorder;
234 GraphicsUnit u = GraphicsUnit.Pixel;
236 g.DrawImage(back, new Rectangle(0, 0, l, t),
237 new Rectangle(0, 0, l, t), u);
238 g.DrawImage(back, new Rectangle(l, 0, w - l - r, t),
239 new Rectangle(l, 0, back.Width - l - r, t), u);
240 g.DrawImage(back, new Rectangle(w - r, 0, r, t),
241 new Rectangle(back.Width - r, 0, r, t), u);
243 g.DrawImage(back, new Rectangle(0, t, l, h - t - b),
244 new Rectangle(0, t, l, back.Height - t - b), u);
245 g.DrawImage(back, new Rectangle(l, t, w - l - r, h - t - b),
246 new Rectangle(l, t, back.Width - l - r, back.Height - t - b), u);
247 g.DrawImage(back, new Rectangle(w - r, t, r, h - t - b),
248 new Rectangle(back.Width - r, t, r, back.Height - t - b), u);
250 g.DrawImage(back, new Rectangle(0, h - b, l, b),
251 new Rectangle(0, back.Height - b, l, b), u);
252 g.DrawImage(back, new Rectangle(l, h - b, w - l - r, b),
253 new Rectangle(l, back.Height - b, back.Width - l - r, b), u);
254 g.DrawImage(back, new Rectangle(w - r, h - b, r, b),
255 new Rectangle(back.Width - r, back.Height - b, r, b), u);
258 private void DrawProgress(Graphics g, int x, int y, int width, int height,
262 new RectangleF(x + width * progress, y, width * (1 - progress), height),
263 new RectangleF(barBack.Width * progress, 0,
264 (1 - progress) * barBack.Width, barBack.Height),
267 new RectangleF(x, y, width * progress, height),
268 new RectangleF(0, 0, progress * barblue.Width, barblue.Height),
272 protected override void OnPaint(PaintEventArgs e) {
273 Graphics g = e.Graphics;
277 g.Clear(Color.Transparent);
281 StringFormat stringFormat = new StringFormat();
282 stringFormat.Alignment = StringAlignment.Far;
285 int y = topBorder + 1;
286 foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
288 g.DrawImage(HardwareTypeImage.Instance.GetImage(pair.Key.HardwareType),
289 new Rectangle(x, y + 2, iconSize, iconSize));
291 g.DrawString(pair.Key.Name, largeFont, Brushes.White,
292 new Rectangle(x, y, w - rightBorder - x, 15));
293 y += hardwareLineHeight;
295 foreach (ISensor sensor in pair.Value) {
297 g.DrawString(sensor.Name + ":", smallFont, darkWhite,
298 new Rectangle(9, y, 64, 15));
300 if (sensor.SensorType != SensorType.Load &&
301 sensor.SensorType != SensorType.Control)
304 switch (sensor.SensorType) {
305 case SensorType.Voltage:
308 case SensorType.Clock:
309 format = "{0:F0} MHz";
311 case SensorType.Temperature:
312 format = "{0:F1} °C";
315 format = "{0:F0} RPM";
317 case SensorType.Flow:
318 format = "{0:F0} L/h";
322 string formattedValue;
323 if (sensor.SensorType == SensorType.Temperature &&
324 unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit) {
325 formattedValue = string.Format("{0:F1} °F",
326 sensor.Value * 1.8 + 32);
328 formattedValue = string.Format(format, sensor.Value);
332 g.DrawString(formattedValue, smallFont, darkWhite,
333 new RectangleF(x, y, w - x - 9, 15), stringFormat);
336 DrawProgress(g, x, y + 4, w - x - 9, 6, 0.01f * sensor.Value.Value);
339 y += sensorLineHeight;
344 private class HardwareComparer : IComparer<IHardware> {
345 public int Compare(IHardware x, IHardware y) {
346 if (x == null && y == null)
353 if (x.HardwareType != y.HardwareType)
354 return x.HardwareType.CompareTo(y.HardwareType);
356 return x.Name.CompareTo(y.Name);