Migration to Visual Studio 2015.
SharpLibDisplay: Now closing client upon server request.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2013 Michael Möller <mmoeller@openhardwaremonitor.org>
8 Copyright (C) 2010 Paul Werelds <paul@werelds.net>
9 Copyright (C) 2012 Prince Samuel <prince.samuel@gmail.com>
14 using System.Collections.Generic;
15 using System.Collections.ObjectModel;
16 using System.ComponentModel;
19 using System.Reflection;
20 using System.Windows.Forms;
22 using Aga.Controls.Tree;
23 using Aga.Controls.Tree.NodeControls;
24 using OpenHardwareMonitor.Hardware;
25 using OpenHardwareMonitor.WMI;
26 using OpenHardwareMonitor.Utilities;
29 namespace OpenHardwareMonitor.GUI {
30 public partial class MainForm : Form {
32 private PersistentSettings settings;
33 private UnitManager unitManager;
34 private Computer computer;
36 private TreeModel treeModel;
37 private IDictionary<ISensor, Color> sensorPlotColors =
38 new Dictionary<ISensor, Color>();
39 private Color[] plotColorPalette;
40 private SystemTray systemTray;
41 private SoundGraphDisplay soundGraphDisplay;
42 private SharpDisplay sharpDisplay;
43 private bool displayTick;
44 private StartupManager startupManager = new StartupManager();
45 private UpdateVisitor updateVisitor = new UpdateVisitor();
46 private SensorGadget gadget;
47 private Form plotForm;
48 private PlotPanel plotPanel;
50 private UserOption showHiddenSensors;
51 private UserOption showPlot;
52 private UserOption showValue;
53 private UserOption showMin;
54 private UserOption showMax;
55 private UserOption startMinimized;
56 private UserOption minimizeToTray;
57 private UserOption minimizeOnClose;
58 private UserOption autoStart;
59 private UserOption frontViewPacked; //Tells whether FrontView should cycle or pack sensors
60 private UserOption frontViewDisplayTime; //Tells whether or not FrontView should display current time
61 private UserOption sharpDisplayPacked; //Tells whether SharpDisplay should cycle or pack sensors
62 private UserOption sharpDisplayShowTime; //Tells whether or not SharpDisplay should display current time
64 private UserOption readMainboardSensors;
65 private UserOption readCpuSensors;
66 private UserOption readRamSensors;
67 private UserOption readGpuSensors;
68 private UserOption readFanControllersSensors;
69 private UserOption readHddSensors;
71 private UserOption showGadget;
72 private UserRadioGroup plotLocation;
73 private WmiProvider wmiProvider;
75 private UserOption runWebServer;
76 private HttpServer server;
78 private UserOption logSensors;
79 private UserRadioGroup loggingInterval;
80 private Logger logger;
82 private bool selectionDragging = false;
85 InitializeComponent();
87 // check if the OpenHardwareMonitorLib assembly has the correct version
88 if (Assembly.GetAssembly(typeof(Computer)).GetName().Version !=
89 Assembly.GetExecutingAssembly().GetName().Version) {
91 "The version of the file OpenHardwareMonitorLib.dll is incompatible.",
92 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
96 this.settings = new PersistentSettings();
97 this.settings.Load(Path.ChangeExtension(
98 Application.ExecutablePath, ".config"));
100 this.unitManager = new UnitManager(settings);
102 // make sure the buffers used for double buffering are not disposed
103 // after each draw call
104 BufferedGraphicsManager.Current.MaximumBuffer =
105 Screen.PrimaryScreen.Bounds.Size;
107 // set the DockStyle here, to avoid conflicts with the MainMenu
108 this.splitContainer.Dock = DockStyle.Fill;
110 this.Font = SystemFonts.MessageBoxFont;
111 treeView.Font = SystemFonts.MessageBoxFont;
113 plotPanel = new PlotPanel(settings, unitManager);
114 plotPanel.Font = SystemFonts.MessageBoxFont;
115 plotPanel.Dock = DockStyle.Fill;
117 nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
118 nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
119 nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
120 nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
121 nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
122 nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
124 foreach (TreeColumn column in treeView.Columns)
125 column.Width = Math.Max(20, Math.Min(400,
126 settings.GetValue("treeView.Columns." + column.Header + ".Width",
129 treeModel = new TreeModel();
130 root = new Node(System.Environment.MachineName);
131 root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
133 treeModel.Nodes.Add(root);
134 treeView.Model = treeModel;
136 this.computer = new Computer(settings);
139 systemTray = new SystemTray(computer, settings, unitManager);
140 systemTray.HideShowCommand += hideShowClick;
141 systemTray.ExitCommand += exitClick;
144 soundGraphDisplay = new SoundGraphDisplay(computer, settings, unitManager);
145 //Sharp Display Manager
146 sharpDisplay = new SharpDisplay(computer, settings, unitManager);
149 int p = (int)Environment.OSVersion.Platform;
150 if ((p == 4) || (p == 128)) { // Unix
151 treeView.RowHeight = Math.Max(treeView.RowHeight, 18);
152 splitContainer.BorderStyle = BorderStyle.None;
153 splitContainer.Border3DStyle = Border3DStyle.Adjust;
154 splitContainer.SplitterWidth = 4;
155 treeView.BorderStyle = BorderStyle.Fixed3D;
156 plotPanel.BorderStyle = BorderStyle.Fixed3D;
157 gadgetMenuItem.Visible = false;
158 minCloseMenuItem.Visible = false;
159 minTrayMenuItem.Visible = false;
160 startMinMenuItem.Visible = false;
162 treeView.RowHeight = Math.Max(treeView.Font.Height + 1, 18);
164 gadget = new SensorGadget(computer, settings, unitManager);
165 gadget.HideShowCommand += hideShowClick;
167 wmiProvider = new WmiProvider(computer);
170 logger = new Logger(computer);
172 plotColorPalette = new Color[13];
173 plotColorPalette[0] = Color.Blue;
174 plotColorPalette[1] = Color.OrangeRed;
175 plotColorPalette[2] = Color.Green;
176 plotColorPalette[3] = Color.LightSeaGreen;
177 plotColorPalette[4] = Color.Goldenrod;
178 plotColorPalette[5] = Color.DarkViolet;
179 plotColorPalette[6] = Color.YellowGreen;
180 plotColorPalette[7] = Color.SaddleBrown;
181 plotColorPalette[8] = Color.RoyalBlue;
182 plotColorPalette[9] = Color.DeepPink;
183 plotColorPalette[10] = Color.MediumSeaGreen;
184 plotColorPalette[11] = Color.Olive;
185 plotColorPalette[12] = Color.Firebrick;
187 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
188 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
192 timer.Enabled = true;
194 showHiddenSensors = new UserOption("hiddenMenuItem", false,
195 hiddenMenuItem, settings);
196 showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
197 treeModel.ForceVisible = showHiddenSensors.Value;
200 showValue = new UserOption("valueMenuItem", true, valueMenuItem,
202 showValue.Changed += delegate(object sender, EventArgs e) {
203 treeView.Columns[1].IsVisible = showValue.Value;
206 showMin = new UserOption("minMenuItem", false, minMenuItem, settings);
207 showMin.Changed += delegate(object sender, EventArgs e) {
208 treeView.Columns[2].IsVisible = showMin.Value;
211 showMax = new UserOption("maxMenuItem", true, maxMenuItem, settings);
212 showMax.Changed += delegate(object sender, EventArgs e) {
213 treeView.Columns[3].IsVisible = showMax.Value;
216 startMinimized = new UserOption("startMinMenuItem", false,
217 startMinMenuItem, settings);
219 minimizeToTray = new UserOption("minTrayMenuItem", true,
220 minTrayMenuItem, settings);
221 minimizeToTray.Changed += delegate(object sender, EventArgs e) {
222 systemTray.IsMainIconEnabled = minimizeToTray.Value;
225 minimizeOnClose = new UserOption("minCloseMenuItem", false,
226 minCloseMenuItem, settings);
228 autoStart = new UserOption(null, startupManager.Startup,
229 startupMenuItem, settings);
230 autoStart.Changed += delegate(object sender, EventArgs e) {
232 startupManager.Startup = autoStart.Value;
233 } catch (InvalidOperationException) {
234 MessageBox.Show("Updating the auto-startup option failed.", "Error",
235 MessageBoxButtons.OK, MessageBoxIcon.Error);
236 autoStart.Value = startupManager.Startup;
240 frontViewPacked = new UserOption("frontViewPackedMenuItem", false, frontViewPackedMenuItem, settings);
241 frontViewDisplayTime = new UserOption("frontViewDisplayTimeMenuItem", false, frontViewDisplayTimeMenuItem, settings);
243 sharpDisplayPacked = new UserOption("sharpDisplayPackedMenuItem", false, sharpDisplayPackedMenuItem, settings);
244 sharpDisplayShowTime = new UserOption("sharpDisplayShowTimeMenuItem", false, sharpDisplayShowTimeMenuItem, settings);
247 readMainboardSensors = new UserOption("mainboardMenuItem", true,
248 mainboardMenuItem, settings);
249 readMainboardSensors.Changed += delegate(object sender, EventArgs e) {
250 computer.MainboardEnabled = readMainboardSensors.Value;
253 readCpuSensors = new UserOption("cpuMenuItem", true,
254 cpuMenuItem, settings);
255 readCpuSensors.Changed += delegate(object sender, EventArgs e) {
256 computer.CPUEnabled = readCpuSensors.Value;
259 readRamSensors = new UserOption("ramMenuItem", true,
260 ramMenuItem, settings);
261 readRamSensors.Changed += delegate(object sender, EventArgs e) {
262 computer.RAMEnabled = readRamSensors.Value;
265 readGpuSensors = new UserOption("gpuMenuItem", true,
266 gpuMenuItem, settings);
267 readGpuSensors.Changed += delegate(object sender, EventArgs e) {
268 computer.GPUEnabled = readGpuSensors.Value;
271 readFanControllersSensors = new UserOption("fanControllerMenuItem", true,
272 fanControllerMenuItem, settings);
273 readFanControllersSensors.Changed += delegate(object sender, EventArgs e) {
274 computer.FanControllerEnabled = readFanControllersSensors.Value;
277 readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
279 readHddSensors.Changed += delegate(object sender, EventArgs e) {
280 computer.HDDEnabled = readHddSensors.Value;
283 showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
285 showGadget.Changed += delegate(object sender, EventArgs e) {
287 gadget.Visible = showGadget.Value;
290 celsiusMenuItem.Checked =
291 unitManager.TemperatureUnit == TemperatureUnit.Celsius;
292 fahrenheitMenuItem.Checked = !celsiusMenuItem.Checked;
294 server = new HttpServer(root, this.settings.GetValue("listenerPort", 8085));
295 if (server.PlatformNotSupported) {
296 webMenuItemSeparator.Visible = false;
297 webMenuItem.Visible = false;
300 runWebServer = new UserOption("runWebServerMenuItem", false,
301 runWebServerMenuItem, settings);
302 runWebServer.Changed += delegate(object sender, EventArgs e) {
303 if (runWebServer.Value)
304 server.StartHTTPListener();
306 server.StopHTTPListener();
309 logSensors = new UserOption("logSensorsMenuItem", false, logSensorsMenuItem,
312 loggingInterval = new UserRadioGroup("loggingInterval", 0,
313 new[] { log1sMenuItem, log2sMenuItem, log5sMenuItem, log10sMenuItem,
314 log30sMenuItem, log1minMenuItem, log2minMenuItem, log5minMenuItem,
315 log10minMenuItem, log30minMenuItem, log1hMenuItem, log2hMenuItem,
318 loggingInterval.Changed += (sender, e) => {
319 switch (loggingInterval.Value) {
320 case 0: logger.LoggingInterval = new TimeSpan(0, 0, 1); break;
321 case 1: logger.LoggingInterval = new TimeSpan(0, 0, 2); break;
322 case 2: logger.LoggingInterval = new TimeSpan(0, 0, 5); break;
323 case 3: logger.LoggingInterval = new TimeSpan(0, 0, 10); break;
324 case 4: logger.LoggingInterval = new TimeSpan(0, 0, 30); break;
325 case 5: logger.LoggingInterval = new TimeSpan(0, 1, 0); break;
326 case 6: logger.LoggingInterval = new TimeSpan(0, 2, 0); break;
327 case 7: logger.LoggingInterval = new TimeSpan(0, 5, 0); break;
328 case 8: logger.LoggingInterval = new TimeSpan(0, 10, 0); break;
329 case 9: logger.LoggingInterval = new TimeSpan(0, 30, 0); break;
330 case 10: logger.LoggingInterval = new TimeSpan(1, 0, 0); break;
331 case 11: logger.LoggingInterval = new TimeSpan(2, 0, 0); break;
332 case 12: logger.LoggingInterval = new TimeSpan(6, 0, 0); break;
336 InitializePlotForm();
338 startupMenuItem.Visible = startupManager.IsAvailable;
340 if (startMinMenuItem.Checked) {
341 if (!minTrayMenuItem.Checked) {
342 WindowState = FormWindowState.Minimized;
349 // Create a handle, otherwise calling Close() does not fire FormClosed
350 IntPtr handle = Handle;
352 // Make sure the settings are saved when the user logs off
353 Microsoft.Win32.SystemEvents.SessionEnded += delegate {
356 if (runWebServer.Value)
361 private void InitializePlotForm() {
362 plotForm = new Form();
363 plotForm.FormBorderStyle = FormBorderStyle.SizableToolWindow;
364 plotForm.ShowInTaskbar = false;
365 plotForm.StartPosition = FormStartPosition.Manual;
366 this.AddOwnedForm(plotForm);
367 plotForm.Bounds = new Rectangle {
368 X = settings.GetValue("plotForm.Location.X", -100000),
369 Y = settings.GetValue("plotForm.Location.Y", 100),
370 Width = settings.GetValue("plotForm.Width", 600),
371 Height = settings.GetValue("plotForm.Height", 400)
374 showPlot = new UserOption("plotMenuItem", false, plotMenuItem, settings);
375 plotLocation = new UserRadioGroup("plotLocation", 0,
376 new[] { plotWindowMenuItem, plotBottomMenuItem, plotRightMenuItem },
379 showPlot.Changed += delegate(object sender, EventArgs e) {
380 if (plotLocation.Value == 0) {
381 if (showPlot.Value && this.Visible)
386 splitContainer.Panel2Collapsed = !showPlot.Value;
388 treeView.Invalidate();
390 plotLocation.Changed += delegate(object sender, EventArgs e) {
391 switch (plotLocation.Value) {
393 splitContainer.Panel2.Controls.Clear();
394 splitContainer.Panel2Collapsed = true;
395 plotForm.Controls.Add(plotPanel);
396 if (showPlot.Value && this.Visible)
400 plotForm.Controls.Clear();
402 splitContainer.Orientation = Orientation.Horizontal;
403 splitContainer.Panel2.Controls.Add(plotPanel);
404 splitContainer.Panel2Collapsed = !showPlot.Value;
407 plotForm.Controls.Clear();
409 splitContainer.Orientation = Orientation.Vertical;
410 splitContainer.Panel2.Controls.Add(plotPanel);
411 splitContainer.Panel2Collapsed = !showPlot.Value;
416 plotForm.FormClosing += delegate(object sender, FormClosingEventArgs e) {
417 if (e.CloseReason == CloseReason.UserClosing) {
418 // just switch off the plotting when the user closes the form
419 if (plotLocation.Value == 0) {
420 showPlot.Value = false;
426 EventHandler moveOrResizePlotForm = delegate(object sender, EventArgs e) {
427 if (plotForm.WindowState != FormWindowState.Minimized) {
428 settings.SetValue("plotForm.Location.X", plotForm.Bounds.X);
429 settings.SetValue("plotForm.Location.Y", plotForm.Bounds.Y);
430 settings.SetValue("plotForm.Width", plotForm.Bounds.Width);
431 settings.SetValue("plotForm.Height", plotForm.Bounds.Height);
434 plotForm.Move += moveOrResizePlotForm;
435 plotForm.Resize += moveOrResizePlotForm;
437 plotForm.VisibleChanged += delegate(object sender, EventArgs e) {
438 Rectangle bounds = new Rectangle(plotForm.Location, plotForm.Size);
439 Screen screen = Screen.FromRectangle(bounds);
440 Rectangle intersection =
441 Rectangle.Intersect(screen.WorkingArea, bounds);
442 if (intersection.Width < Math.Min(16, bounds.Width) ||
443 intersection.Height < Math.Min(16, bounds.Height)) {
444 plotForm.Location = new Point(
445 screen.WorkingArea.Width / 2 - bounds.Width / 2,
446 screen.WorkingArea.Height / 2 - bounds.Height / 2);
450 this.VisibleChanged += delegate(object sender, EventArgs e) {
451 if (this.Visible && showPlot.Value && plotLocation.Value == 0)
458 private void InsertSorted(Collection<Node> nodes, HardwareNode node) {
460 while (i < nodes.Count && nodes[i] is HardwareNode &&
461 ((HardwareNode)nodes[i]).Hardware.HardwareType <
462 node.Hardware.HardwareType)
464 nodes.Insert(i, node);
467 private void SubHardwareAdded(IHardware hardware, Node node) {
468 HardwareNode hardwareNode =
469 new HardwareNode(hardware, settings, unitManager);
470 hardwareNode.PlotSelectionChanged += PlotSelectionChanged;
472 InsertSorted(node.Nodes, hardwareNode);
474 foreach (IHardware subHardware in hardware.SubHardware)
475 SubHardwareAdded(subHardware, hardwareNode);
478 private void HardwareAdded(IHardware hardware) {
479 SubHardwareAdded(hardware, root);
480 PlotSelectionChanged(this, null);
483 private void HardwareRemoved(IHardware hardware) {
484 List<HardwareNode> nodesToRemove = new List<HardwareNode>();
485 foreach (Node node in root.Nodes) {
486 HardwareNode hardwareNode = node as HardwareNode;
487 if (hardwareNode != null && hardwareNode.Hardware == hardware)
488 nodesToRemove.Add(hardwareNode);
490 foreach (HardwareNode hardwareNode in nodesToRemove) {
491 root.Nodes.Remove(hardwareNode);
492 hardwareNode.PlotSelectionChanged -= PlotSelectionChanged;
494 PlotSelectionChanged(this, null);
497 private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
498 Node node = e.Node.Tag as Node;
501 if (node.IsVisible) {
502 SensorNode sensorNode = node as SensorNode;
503 if (plotMenuItem.Checked && sensorNode != null &&
504 sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
507 //If displayed in FrontView draw background in green
508 if (sensorNode != null && settings.GetValue(new Identifier(sensorNode.Sensor.Identifier, "FrontView").ToString(), false))
510 SolidBrush greenBrush = new SolidBrush(Color.FromName("mediumspringgreen"));
511 e.BackgroundBrush = greenBrush;
514 //If displayed in SharpDisplay draw background in blue
515 if (sensorNode != null && settings.GetValue(new Identifier(sensorNode.Sensor.Identifier, "SharpDisplay").ToString(), false))
517 SolidBrush aquaBrush = new SolidBrush(Color.FromName("aqua"));
518 e.BackgroundBrush = aquaBrush;
523 e.TextColor = Color.DarkGray;
528 private void PlotSelectionChanged(object sender, EventArgs e) {
529 List<ISensor> selected = new List<ISensor>();
530 IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
532 foreach (TreeNodeAdv node in treeView.AllNodes) {
533 SensorNode sensorNode = node.Tag as SensorNode;
534 if (sensorNode != null) {
535 if (sensorNode.Plot) {
536 colors.Add(sensorNode.Sensor,
537 plotColorPalette[colorIndex % plotColorPalette.Length]);
538 selected.Add(sensorNode.Sensor);
543 sensorPlotColors = colors;
544 plotPanel.SetSensors(selected, colors);
547 private void nodeTextBoxText_EditorShowing(object sender,
550 e.Cancel = !(treeView.CurrentNode != null &&
551 (treeView.CurrentNode.Tag is SensorNode ||
552 treeView.CurrentNode.Tag is HardwareNode));
555 private void nodeCheckBox_IsVisibleValueNeeded(object sender,
556 NodeControlValueEventArgs e) {
557 SensorNode node = e.Node.Tag as SensorNode;
558 e.Value = (node != null) && plotMenuItem.Checked;
561 private void exitClick(object sender, EventArgs e) {
565 private int delayCount = 0;
566 private void timer_Tick(object sender, EventArgs e) {
567 computer.Accept(updateVisitor);
568 treeView.Invalidate();
569 plotPanel.InvalidatePlot();
574 if (wmiProvider != null)
575 wmiProvider.Update();
577 if (soundGraphDisplay != null)
579 soundGraphDisplay.Redraw(frontViewPacked.Value,frontViewDisplayTime.Value);
581 displayTick=!displayTick;
584 soundGraphDisplay.SetText(" ---", "");
588 soundGraphDisplay.SetText(" -+-", "");
593 if (sharpDisplay != null)
595 sharpDisplay.Redraw(sharpDisplayPacked.Value, sharpDisplayShowTime.Value);
599 if (logSensors != null && logSensors.Value && delayCount >= 4)
606 private void SaveConfiguration() {
607 plotPanel.SetCurrentSettings();
608 foreach (TreeColumn column in treeView.Columns)
609 settings.SetValue("treeView.Columns." + column.Header + ".Width",
612 this.settings.SetValue("listenerPort", server.ListenerPort);
614 string fileName = Path.ChangeExtension(
615 System.Windows.Forms.Application.ExecutablePath, ".config");
617 settings.Save(fileName);
618 } catch (UnauthorizedAccessException) {
619 MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
620 "The current settings could not be saved.",
621 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
622 } catch (IOException) {
623 MessageBox.Show("The path '" + fileName + "' is not writeable. " +
624 "The current settings could not be saved.",
625 "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
629 private void MainForm_Load(object sender, EventArgs e) {
630 Rectangle newBounds = new Rectangle {
631 X = settings.GetValue("mainForm.Location.X", Location.X),
632 Y = settings.GetValue("mainForm.Location.Y", Location.Y),
633 Width = settings.GetValue("mainForm.Width", 470),
634 Height = settings.GetValue("mainForm.Height", 640)
637 Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
638 int.MinValue, int.MinValue);
640 foreach (Screen screen in Screen.AllScreens)
641 fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds);
643 Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds);
644 if (intersection.Width < 20 || intersection.Height < 20 ||
645 !settings.Contains("mainForm.Location.X")
647 newBounds.X = (Screen.PrimaryScreen.WorkingArea.Width / 2) -
650 newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) -
651 (newBounds.Height / 2);
654 this.Bounds = newBounds;
657 private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
659 systemTray.IsMainIconEnabled = false;
660 timer.Enabled = false;
663 if (runWebServer.Value)
665 systemTray.Dispose();
666 soundGraphDisplay.Dispose();
667 sharpDisplay.Dispose();
670 private void aboutMenuItem_Click(object sender, EventArgs e) {
671 new AboutBox().ShowDialog();
674 private void treeView_Click(object sender, EventArgs e) {
676 MouseEventArgs m = e as MouseEventArgs;
677 if (m == null || m.Button != MouseButtons.Right)
680 NodeControlInfo info = treeView.GetNodeControlInfoAt(
683 treeView.SelectedNode = info.Node;
684 if (info.Node != null) {
685 SensorNode node = info.Node.Tag as SensorNode;
686 if (node != null && node.Sensor != null) {
687 treeContextMenu.MenuItems.Clear();
688 if (node.Sensor.Parameters.Length > 0) {
689 MenuItem item = new MenuItem("Parameters...");
690 item.Click += delegate(object obj, EventArgs args) {
691 ShowParameterForm(node.Sensor);
693 treeContextMenu.MenuItems.Add(item);
695 if (nodeTextBoxText.EditEnabled) {
696 MenuItem item = new MenuItem("Rename");
697 item.Click += delegate(object obj, EventArgs args) {
698 nodeTextBoxText.BeginEdit();
700 treeContextMenu.MenuItems.Add(item);
702 if (node.IsVisible) {
703 MenuItem item = new MenuItem("Hide");
704 item.Click += delegate(object obj, EventArgs args) {
705 node.IsVisible = false;
707 treeContextMenu.MenuItems.Add(item);
709 MenuItem item = new MenuItem("Unhide");
710 item.Click += delegate(object obj, EventArgs args) {
711 node.IsVisible = true;
713 treeContextMenu.MenuItems.Add(item);
715 treeContextMenu.MenuItems.Add(new MenuItem("-"));
717 MenuItem item = new MenuItem("Show in Tray");
718 item.Checked = systemTray.Contains(node.Sensor);
719 item.Click += delegate(object obj, EventArgs args) {
721 systemTray.Remove(node.Sensor);
723 systemTray.Add(node.Sensor, true);
725 treeContextMenu.MenuItems.Add(item);
728 MenuItem item = new MenuItem("Show in iMON FrontView");
729 item.Checked = soundGraphDisplay.Contains(node.Sensor);
730 item.Click += delegate(object obj, EventArgs args)
733 soundGraphDisplay.Remove(node.Sensor);
735 soundGraphDisplay.Add(node.Sensor, true);
737 treeContextMenu.MenuItems.Add(item);
740 MenuItem item = new MenuItem("Show in SharpDisplay");
741 item.Checked = sharpDisplay.Contains(node.Sensor);
742 item.Click += delegate(object obj, EventArgs args)
745 sharpDisplay.Remove(node.Sensor);
747 sharpDisplay.Add(node.Sensor, true);
749 treeContextMenu.MenuItems.Add(item);
751 if (gadget != null) {
752 MenuItem item = new MenuItem("Show in Gadget");
753 item.Checked = gadget.Contains(node.Sensor);
754 item.Click += delegate(object obj, EventArgs args) {
756 gadget.Remove(node.Sensor);
758 gadget.Add(node.Sensor);
761 treeContextMenu.MenuItems.Add(item);
763 if (node.Sensor.Control != null) {
764 treeContextMenu.MenuItems.Add(new MenuItem("-"));
765 IControl control = node.Sensor.Control;
766 MenuItem controlItem = new MenuItem("Control");
767 MenuItem defaultItem = new MenuItem("Default");
768 defaultItem.Checked = control.ControlMode == ControlMode.Default;
769 controlItem.MenuItems.Add(defaultItem);
770 defaultItem.Click += delegate(object obj, EventArgs args) {
771 control.SetDefault();
773 MenuItem manualItem = new MenuItem("Manual");
774 controlItem.MenuItems.Add(manualItem);
775 manualItem.Checked = control.ControlMode == ControlMode.Software;
776 for (int i = 0; i <= 100; i += 5) {
777 if (i <= control.MaxSoftwareValue &&
778 i >= control.MinSoftwareValue) {
779 MenuItem item = new MenuItem(i + " %");
780 item.RadioCheck = true;
781 manualItem.MenuItems.Add(item);
782 item.Checked = control.ControlMode == ControlMode.Software &&
783 Math.Round(control.SoftwareValue) == i;
784 int softwareValue = i;
785 item.Click += delegate(object obj, EventArgs args) {
786 control.SetSoftware(softwareValue);
790 treeContextMenu.MenuItems.Add(controlItem);
793 treeContextMenu.Show(treeView, new Point(m.X, m.Y));
796 HardwareNode hardwareNode = info.Node.Tag as HardwareNode;
797 if (hardwareNode != null && hardwareNode.Hardware != null) {
798 treeContextMenu.MenuItems.Clear();
800 if (nodeTextBoxText.EditEnabled) {
801 MenuItem item = new MenuItem("Rename");
802 item.Click += delegate(object obj, EventArgs args) {
803 nodeTextBoxText.BeginEdit();
805 treeContextMenu.MenuItems.Add(item);
808 treeContextMenu.Show(treeView, new Point(m.X, m.Y));
813 private void saveReportMenuItem_Click(object sender, EventArgs e) {
814 string report = computer.GetReport();
815 if (saveFileDialog.ShowDialog() == DialogResult.OK) {
816 using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
822 private void SysTrayHideShow() {
828 protected override void WndProc(ref Message m) {
829 const int WM_SYSCOMMAND = 0x112;
830 const int SC_MINIMIZE = 0xF020;
831 const int SC_CLOSE = 0xF060;
834 if (minimizeToTray.Value &&
835 m.Msg == WM_SYSCOMMAND && m.WParam.ToInt64() == SC_MINIMIZE)
839 else if (minimizeOnClose.Value &&
840 m.Msg == WM_SYSCOMMAND && m.WParam.ToInt64() == SC_CLOSE)
843 * Apparently the user wants to minimize rather than close
844 * Now we still need to check if we're going to the tray or not
846 * Note: the correct way to do this would be to send out SC_MINIMIZE,
847 * but since the code here is so simple,
848 * that would just be a waste of time.
850 if (minimizeToTray.Value)
853 WindowState = FormWindowState.Minimized;
861 private void hideShowClick(object sender, EventArgs e) {
865 private void ShowParameterForm(ISensor sensor) {
866 ParameterForm form = new ParameterForm();
867 form.Parameters = sensor.Parameters;
868 form.captionLabel.Text = sensor.Name;
872 private void treeView_NodeMouseDoubleClick(object sender,
873 TreeNodeAdvMouseEventArgs e) {
874 SensorNode node = e.Node.Tag as SensorNode;
875 if (node != null && node.Sensor != null &&
876 node.Sensor.Parameters.Length > 0) {
877 ShowParameterForm(node.Sensor);
881 private void celsiusMenuItem_Click(object sender, EventArgs e) {
882 celsiusMenuItem.Checked = true;
883 fahrenheitMenuItem.Checked = false;
884 unitManager.TemperatureUnit = TemperatureUnit.Celsius;
887 private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
888 celsiusMenuItem.Checked = false;
889 fahrenheitMenuItem.Checked = true;
890 unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
893 private void sumbitReportMenuItem_Click(object sender, EventArgs e)
895 ReportForm form = new ReportForm();
896 form.Report = computer.GetReport();
900 private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
901 computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
907 private void MainForm_MoveOrResize(object sender, EventArgs e) {
908 if (WindowState != FormWindowState.Minimized) {
909 settings.SetValue("mainForm.Location.X", Bounds.X);
910 settings.SetValue("mainForm.Location.Y", Bounds.Y);
911 settings.SetValue("mainForm.Width", Bounds.Width);
912 settings.SetValue("mainForm.Height", Bounds.Height);
916 private void resetClick(object sender, EventArgs e) {
917 // disable the fallback MainIcon during reset, otherwise icon visibility
919 systemTray.IsMainIconEnabled = false;
922 // restore the MainIcon setting
923 systemTray.IsMainIconEnabled = minimizeToTray.Value;
926 private void treeView_MouseMove(object sender, MouseEventArgs e) {
927 selectionDragging = selectionDragging &
928 (e.Button & (MouseButtons.Left | MouseButtons.Right)) > 0;
930 if (selectionDragging)
931 treeView.SelectedNode = treeView.GetNodeAt(e.Location);
934 private void treeView_MouseDown(object sender, MouseEventArgs e) {
935 selectionDragging = true;
938 private void treeView_MouseUp(object sender, MouseEventArgs e) {
939 selectionDragging = false;
942 private void serverPortMenuItem_Click(object sender, EventArgs e) {
943 new PortForm(this).ShowDialog();
946 public HttpServer Server {
947 get { return server; }