1.1 --- a/GUI/SensorGadget.cs Wed Sep 08 19:29:58 2010 +0000
1.2 +++ b/GUI/SensorGadget.cs Mon Sep 13 22:34:08 2010 +0000
1.3 @@ -49,13 +49,20 @@
1.4 private Image back = Utilities.EmbeddedResources.GetImage("gadget.png");
1.5 private Image barBack = Utilities.EmbeddedResources.GetImage("barback.png");
1.6 private Image barblue = Utilities.EmbeddedResources.GetImage("barblue.png");
1.7 - private const int topBorder = 4;
1.8 - private const int bottomBorder = 6;
1.9 + private const int topBorder = 6;
1.10 + private const int bottomBorder = 7;
1.11 private const int leftBorder = 6;
1.12 - private const int rightBorder = 6;
1.13 - private const int iconSize = 11;
1.14 - private const int hardwareLineHeight = 12;
1.15 - private const int sensorLineHeight = 10;
1.16 + private const int rightBorder = 7;
1.17 +
1.18 + private float fontSize;
1.19 + private int iconSize;
1.20 + private int hardwareLineHeight;
1.21 + private int sensorLineHeight;
1.22 + private int rightMargin;
1.23 + private int leftMargin;
1.24 + private int topMargin;
1.25 + private int bottomMargin;
1.26 + private int progressWidth;
1.27
1.28 private IDictionary<IHardware, IList<ISensor>> sensors =
1.29 new SortedDictionary<IHardware, IList<ISensor>>(new HardwareComparer());
1.30 @@ -63,11 +70,12 @@
1.31 private PersistentSettings settings;
1.32 private UserOption hardwareNames;
1.33 private UserOption alwaysOnTop;
1.34 - private UserOption lockPosition;
1.35 + private UserOption lockPositionAndSize;
1.36
1.37 private Font largeFont;
1.38 private Font smallFont;
1.39 private Brush darkWhite;
1.40 + private StringFormat stringFormat;
1.41 private StringFormat trimStringFormat;
1.42 private StringFormat alignRightStringFormat;
1.43
1.44 @@ -77,18 +85,20 @@
1.45 this.unitManager = unitManager;
1.46 this.settings = settings;
1.47 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
1.48 - computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
1.49 + computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
1.50
1.51 - this.largeFont = new Font(SystemFonts.MessageBoxFont.FontFamily, 7.5f,
1.52 - FontStyle.Bold);
1.53 - this.smallFont = new Font(SystemFonts.MessageBoxFont.FontFamily, 7.5f);
1.54 this.darkWhite = new SolidBrush(Color.FromArgb(0xF0, 0xF0, 0xF0));
1.55
1.56 + this.stringFormat = new StringFormat();
1.57 + this.stringFormat.FormatFlags = StringFormatFlags.NoWrap;
1.58 +
1.59 this.trimStringFormat = new StringFormat();
1.60 this.trimStringFormat.Trimming = StringTrimming.EllipsisCharacter;
1.61 + this.trimStringFormat.FormatFlags = StringFormatFlags.NoWrap;
1.62
1.63 this.alignRightStringFormat = new StringFormat();
1.64 this.alignRightStringFormat.Alignment = StringAlignment.Far;
1.65 + this.alignRightStringFormat.FormatFlags = StringFormatFlags.NoWrap;
1.66
1.67 this.Location = new Point(
1.68 settings.GetValue("sensorGadget.Location.X", 100),
1.69 @@ -97,12 +107,37 @@
1.70 settings.SetValue("sensorGadget.Location.X", Location.X);
1.71 settings.SetValue("sensorGadget.Location.Y", Location.Y);
1.72 };
1.73 +
1.74 + SetFontSize(settings.GetValue("sensorGadget.FontSize", 7.5f));
1.75 + Resize(settings.GetValue("sensorGadget.Width", Size.Width));
1.76
1.77 ContextMenu contextMenu = new ContextMenu();
1.78 MenuItem hardwareNamesItem = new MenuItem("Hardware Names");
1.79 contextMenu.MenuItems.Add(hardwareNamesItem);
1.80 + MenuItem fontSizeMenu = new MenuItem("Font Size");
1.81 + for (int i = 0; i < 4; i++) {
1.82 + float size;
1.83 + string name;
1.84 + switch (i) {
1.85 + case 0: size = 6.5f; name = "Small"; break;
1.86 + case 1: size = 7.5f; name = "Medium"; break;
1.87 + case 2: size = 9f; name = "Large"; break;
1.88 + case 3: size = 11f; name = "Very Large"; break;
1.89 + default: throw new NotImplementedException();
1.90 + }
1.91 + MenuItem item = new MenuItem(name);
1.92 + item.Checked = fontSize == size;
1.93 + item.Click += delegate(object sender, EventArgs e) {
1.94 + SetFontSize(size);
1.95 + settings.SetValue("sensorGadget.FontSize", size);
1.96 + foreach (MenuItem mi in fontSizeMenu.MenuItems)
1.97 + mi.Checked = mi == item;
1.98 + };
1.99 + fontSizeMenu.MenuItems.Add(item);
1.100 + }
1.101 + contextMenu.MenuItems.Add(fontSizeMenu);
1.102 contextMenu.MenuItems.Add(new MenuItem("-"));
1.103 - MenuItem lockItem = new MenuItem("Lock Position");
1.104 + MenuItem lockItem = new MenuItem("Lock Position and Size");
1.105 contextMenu.MenuItems.Add(lockItem);
1.106 contextMenu.MenuItems.Add(new MenuItem("-"));
1.107 MenuItem alwaysOnTopItem = new MenuItem("Always on Top");
1.108 @@ -128,7 +163,6 @@
1.109 hardwareNamesItem, settings);
1.110 hardwareNames.Changed += delegate(object sender, EventArgs e) {
1.111 Resize();
1.112 - Redraw();
1.113 };
1.114
1.115 alwaysOnTop = new UserOption("sensorGadget.AlwaysOnTop", false,
1.116 @@ -136,13 +170,30 @@
1.117 alwaysOnTop.Changed += delegate(object sender, EventArgs e) {
1.118 this.AlwaysOnTop = alwaysOnTop.Value;
1.119 };
1.120 - lockPosition = new UserOption("sensorGadget.LockPosition", false,
1.121 - lockItem, settings);
1.122 - lockPosition.Changed += delegate(object sender, EventArgs e) {
1.123 - this.LockPosition = lockPosition.Value;
1.124 + lockPositionAndSize = new UserOption("sensorGadget.LockPositionAndSize",
1.125 + false, lockItem, settings);
1.126 + lockPositionAndSize.Changed += delegate(object sender, EventArgs e) {
1.127 + this.LockPositionAndSize = lockPositionAndSize.Value;
1.128 };
1.129
1.130 - Resize();
1.131 + HitTest += delegate(object sender, HitTestEventArgs e) {
1.132 + if (lockPositionAndSize.Value)
1.133 + return;
1.134 +
1.135 + if (e.Location.X < leftBorder) {
1.136 + e.HitResult = HitResult.Left;
1.137 + return;
1.138 + }
1.139 + if (e.Location.X > Size.Width - 1 - rightBorder) {
1.140 + e.HitResult = HitResult.Right;
1.141 + return;
1.142 + }
1.143 + };
1.144 +
1.145 + SizeChanged += delegate(object sender, EventArgs e) {
1.146 + settings.SetValue("sensorGadget.Width", Size.Width);
1.147 + Redraw();
1.148 + };
1.149 }
1.150
1.151 public override void Dispose() {
1.152 @@ -156,6 +207,9 @@
1.153 darkWhite.Dispose();
1.154 darkWhite = null;
1.155
1.156 + stringFormat.Dispose();
1.157 + stringFormat = null;
1.158 +
1.159 trimStringFormat.Dispose();
1.160 trimStringFormat = null;
1.161
1.162 @@ -228,7 +282,6 @@
1.163 new Identifier(sensor.Identifier, "gadget").ToString(), true);
1.164
1.165 Resize();
1.166 - Redraw();
1.167 }
1.168 }
1.169
1.170 @@ -249,22 +302,45 @@
1.171 }
1.172 }
1.173 Resize();
1.174 - Redraw();
1.175 + }
1.176 +
1.177 + private Font CreateFont(float size, FontStyle style) {
1.178 + return new Font(SystemFonts.MessageBoxFont.FontFamily, size,
1.179 + style);
1.180 + }
1.181 +
1.182 + private void SetFontSize(float size) {
1.183 + fontSize = size;
1.184 + largeFont = CreateFont(fontSize, FontStyle.Bold);
1.185 + smallFont = CreateFont(fontSize, FontStyle.Regular);
1.186 + iconSize = (int)Math.Round(1.5 * fontSize);
1.187 + hardwareLineHeight = (int)Math.Round(1.66 * fontSize);
1.188 + sensorLineHeight = (int)Math.Round(1.33 * fontSize);
1.189 + leftMargin = leftBorder + (int)Math.Round(0.3 * fontSize);
1.190 + rightMargin = rightBorder + (int)Math.Round(0.3 * fontSize);
1.191 + topMargin = topBorder;
1.192 + bottomMargin = bottomBorder + (int)Math.Round(0.3 * fontSize);
1.193 + progressWidth = (int)Math.Round(5.3 * fontSize);
1.194 + Resize((int)Math.Round(17.3 * fontSize));
1.195 }
1.196
1.197 private void Resize() {
1.198 - int y = topBorder + 1;
1.199 + Resize(this.Size.Width);
1.200 + }
1.201 +
1.202 + private void Resize(int width) {
1.203 + int y = topMargin;
1.204 foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
1.205 if (hardwareNames.Value) {
1.206 - if (y > topBorder + 1)
1.207 - y += 2;
1.208 + if (y > topMargin)
1.209 + y += hardwareLineHeight - sensorLineHeight;
1.210 y += hardwareLineHeight;
1.211 }
1.212 y += pair.Value.Count * sensorLineHeight;
1.213 }
1.214 - y += bottomBorder + 3;
1.215 + y += bottomMargin;
1.216 y = Math.Max(y, topBorder + bottomBorder + 10);
1.217 - this.Size = new Size(130, y);
1.218 + this.Size = new Size(width, y);
1.219 }
1.220
1.221 private void DrawBackground(Graphics g) {
1.222 @@ -318,26 +394,27 @@
1.223 int h = Size.Height;
1.224
1.225 g.Clear(Color.Transparent);
1.226 -
1.227 +
1.228 DrawBackground(g);
1.229
1.230 int x;
1.231 - int y = topBorder + 1;
1.232 + int y = topMargin;
1.233 foreach (KeyValuePair<IHardware, IList<ISensor>> pair in sensors) {
1.234 if (hardwareNames.Value) {
1.235 - if (y > topBorder + 1)
1.236 - y += 2;
1.237 + if (y > topMargin)
1.238 + y += hardwareLineHeight - sensorLineHeight;
1.239 x = leftBorder + 1;
1.240 g.DrawImage(HardwareTypeImage.Instance.GetImage(pair.Key.HardwareType),
1.241 new Rectangle(x, y + 1, iconSize, iconSize));
1.242 x += iconSize + 1;
1.243 g.DrawString(pair.Key.Name, largeFont, Brushes.White,
1.244 - new Rectangle(x, y - 1, w - rightBorder - x, 15));
1.245 + new Rectangle(x, y - 1, w - rightBorder - x, 0),
1.246 + stringFormat);
1.247 y += hardwareLineHeight;
1.248 }
1.249
1.250 foreach (ISensor sensor in pair.Value) {
1.251 - int restWidth;
1.252 + int remainingWidth;
1.253
1.254 if (sensor.SensorType != SensorType.Load &&
1.255 sensor.SensorType != SensorType.Control)
1.256 @@ -370,24 +447,27 @@
1.257 formattedValue = string.Format(format, sensor.Value);
1.258 }
1.259
1.260 - int rightMargin = 8;
1.261 +
1.262 g.DrawString(formattedValue, smallFont, darkWhite,
1.263 - new RectangleF(-1, y - 1, w - rightMargin + 2, 15),
1.264 + new RectangleF(-1, y - 1, w - rightMargin + 3, 0),
1.265 alignRightStringFormat);
1.266
1.267 - restWidth = w - (int)Math.Floor(g.MeasureString(formattedValue,
1.268 + remainingWidth = w - (int)Math.Floor(g.MeasureString(formattedValue,
1.269 smallFont, w, StringFormat.GenericTypographic).Width) -
1.270 rightMargin;
1.271 - } else {
1.272 - restWidth = 80;
1.273 - DrawProgress(g, restWidth, y + 4, w - restWidth - 9, 6,
1.274 - 0.01f * sensor.Value.Value);
1.275 + } else {
1.276 + DrawProgress(g, w - progressWidth - rightMargin,
1.277 + y + 4, progressWidth, 6, 0.01f * sensor.Value.Value);
1.278 +
1.279 + remainingWidth = w - progressWidth - rightMargin;
1.280 }
1.281
1.282 - int leftMargin = 8;
1.283 - g.DrawString(sensor.Name, smallFont, darkWhite,
1.284 - new RectangleF(leftMargin - 1, y - 1, restWidth - leftMargin + 2,
1.285 - 15), trimStringFormat);
1.286 + remainingWidth -= leftMargin + 2;
1.287 + if (remainingWidth > 0) {
1.288 + g.DrawString(sensor.Name, smallFont, darkWhite,
1.289 + new RectangleF(leftMargin - 1, y - 1, remainingWidth, 0),
1.290 + trimStringFormat);
1.291 + }
1.292
1.293 y += sensorLineHeight;
1.294 }