GUI/MainForm.cs
author moel.mich
Sun, 21 Jul 2013 14:17:11 +0000
changeset 413 362c5e77197d
parent 395 d1f25b504845
child 420 46c056e76130
permissions -rw-r--r--
Added experimental support for the Nuvoton NCT6791D super I/O chip.
     1 /*
     2  
     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/.
     6  
     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>
    10 
    11 */
    12 
    13 using System;
    14 using System.Collections.Generic;
    15 using System.Collections.ObjectModel;
    16 using System.ComponentModel;
    17 using System.Drawing;
    18 using System.IO;
    19 using System.Reflection;
    20 using System.Windows.Forms;
    21 using Aga.Controls.Tree;
    22 using Aga.Controls.Tree.NodeControls;
    23 using OpenHardwareMonitor.Hardware;
    24 using OpenHardwareMonitor.WMI;
    25 using OpenHardwareMonitor.Utilities;
    26 
    27 namespace OpenHardwareMonitor.GUI {
    28   public partial class MainForm : Form {
    29 
    30     private PersistentSettings settings;
    31     private UnitManager unitManager;
    32     private Computer computer;
    33     private Node root;
    34     private TreeModel treeModel;
    35     private IDictionary<ISensor, Color> sensorPlotColors = 
    36       new Dictionary<ISensor, Color>();
    37     private Color[] plotColorPalette;
    38     private SystemTray systemTray;    
    39     private StartupManager startupManager = new StartupManager();
    40     private UpdateVisitor updateVisitor = new UpdateVisitor();
    41     private SensorGadget gadget;
    42     private Form plotForm;
    43     private PlotPanel plotPanel;
    44 
    45     private UserOption showHiddenSensors;
    46     private UserOption showPlot;
    47     private UserOption showValue;
    48     private UserOption showMin;
    49     private UserOption showMax;
    50     private UserOption startMinimized;
    51     private UserOption minimizeToTray;
    52     private UserOption minimizeOnClose;
    53     private UserOption autoStart;
    54 
    55     private UserOption readMainboardSensors;
    56     private UserOption readCpuSensors;
    57     private UserOption readRamSensors;
    58     private UserOption readGpuSensors;
    59     private UserOption readFanControllersSensors;
    60     private UserOption readHddSensors;
    61 
    62     private UserOption showGadget;
    63     private UserRadioGroup plotLocation;
    64     private WmiProvider wmiProvider;
    65 
    66     private UserOption runWebServer;
    67     private HttpServer server;
    68 
    69     private bool selectionDragging = false;
    70 
    71     public MainForm() {      
    72       InitializeComponent();
    73 
    74       // check if the OpenHardwareMonitorLib assembly has the correct version
    75       if (Assembly.GetAssembly(typeof(Computer)).GetName().Version !=
    76         Assembly.GetExecutingAssembly().GetName().Version) {
    77         MessageBox.Show(
    78           "The version of the file OpenHardwareMonitorLib.dll is incompatible.",
    79           "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    80         Environment.Exit(0);
    81       }
    82 
    83       this.settings = new PersistentSettings();      
    84       this.settings.Load(Path.ChangeExtension(
    85         Application.ExecutablePath, ".config"));
    86 
    87       this.unitManager = new UnitManager(settings);
    88 
    89       // make sure the buffers used for double buffering are not disposed 
    90       // after each draw call
    91       BufferedGraphicsManager.Current.MaximumBuffer =
    92         Screen.PrimaryScreen.Bounds.Size;  
    93 
    94       // set the DockStyle here, to avoid conflicts with the MainMenu
    95       this.splitContainer.Dock = DockStyle.Fill;
    96             
    97       this.Font = SystemFonts.MessageBoxFont;
    98       treeView.Font = SystemFonts.MessageBoxFont;
    99 
   100       plotPanel = new PlotPanel(settings, unitManager);
   101       plotPanel.Font = SystemFonts.MessageBoxFont;
   102       plotPanel.Dock = DockStyle.Fill;
   103       
   104       nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
   105       nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
   106       nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
   107       nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
   108       nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
   109       nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
   110 
   111       foreach (TreeColumn column in treeView.Columns) 
   112         column.Width = Math.Max(20, Math.Min(400,
   113           settings.GetValue("treeView.Columns." + column.Header + ".Width",
   114           column.Width)));
   115 
   116       treeModel = new TreeModel();
   117       root = new Node(System.Environment.MachineName);
   118       root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
   119       
   120       treeModel.Nodes.Add(root);
   121       treeView.Model = treeModel;
   122 
   123       this.computer = new Computer(settings);
   124 
   125       systemTray = new SystemTray(computer, settings, unitManager);
   126       systemTray.HideShowCommand += hideShowClick;
   127       systemTray.ExitCommand += exitClick;
   128 
   129       int p = (int)Environment.OSVersion.Platform;
   130       if ((p == 4) || (p == 128)) { // Unix
   131         treeView.RowHeight = Math.Max(treeView.RowHeight, 18); 
   132         splitContainer.BorderStyle = BorderStyle.None;
   133         splitContainer.Border3DStyle = Border3DStyle.Adjust;
   134         splitContainer.SplitterWidth = 4;
   135         treeView.BorderStyle = BorderStyle.Fixed3D;
   136         plotPanel.BorderStyle = BorderStyle.Fixed3D;
   137         gadgetMenuItem.Visible = false;
   138         minCloseMenuItem.Visible = false;
   139         minTrayMenuItem.Visible = false;
   140         startMinMenuItem.Visible = false;
   141       } else { // Windows
   142         treeView.RowHeight = Math.Max(treeView.Font.Height + 1, 18); 
   143 
   144         gadget = new SensorGadget(computer, settings, unitManager);
   145         gadget.HideShowCommand += hideShowClick;
   146 
   147         wmiProvider = new WmiProvider(computer);
   148       }
   149 
   150       plotColorPalette = new Color[13];
   151       plotColorPalette[0] = Color.Blue;
   152       plotColorPalette[1] = Color.OrangeRed;
   153       plotColorPalette[2] = Color.Green;
   154       plotColorPalette[3] = Color.LightSeaGreen;
   155       plotColorPalette[4] = Color.Goldenrod;
   156       plotColorPalette[5] = Color.DarkViolet;
   157       plotColorPalette[6] = Color.YellowGreen;
   158       plotColorPalette[7] = Color.SaddleBrown;
   159       plotColorPalette[8] = Color.RoyalBlue;
   160       plotColorPalette[9] = Color.DeepPink;
   161       plotColorPalette[10] = Color.MediumSeaGreen;
   162       plotColorPalette[11] = Color.Olive;
   163       plotColorPalette[12] = Color.Firebrick;
   164       
   165       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
   166       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);        
   167 
   168       computer.Open();
   169 
   170       timer.Enabled = true;
   171 
   172       showHiddenSensors = new UserOption("hiddenMenuItem", false,
   173         hiddenMenuItem, settings);
   174       showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
   175         treeModel.ForceVisible = showHiddenSensors.Value;
   176       };
   177 
   178       showValue = new UserOption("valueMenuItem", true, valueMenuItem,
   179         settings);
   180       showValue.Changed += delegate(object sender, EventArgs e) {
   181         treeView.Columns[1].IsVisible = showValue.Value;
   182       };
   183 
   184       showMin = new UserOption("minMenuItem", false, minMenuItem, settings);
   185       showMin.Changed += delegate(object sender, EventArgs e) {
   186         treeView.Columns[2].IsVisible = showMin.Value;
   187       };
   188 
   189       showMax = new UserOption("maxMenuItem", true, maxMenuItem, settings);
   190       showMax.Changed += delegate(object sender, EventArgs e) {
   191         treeView.Columns[3].IsVisible = showMax.Value;
   192       };
   193 
   194       startMinimized = new UserOption("startMinMenuItem", false,
   195         startMinMenuItem, settings);
   196 
   197       minimizeToTray = new UserOption("minTrayMenuItem", true,
   198         minTrayMenuItem, settings);
   199       minimizeToTray.Changed += delegate(object sender, EventArgs e) {
   200         systemTray.IsMainIconEnabled = minimizeToTray.Value;
   201       };
   202 
   203       minimizeOnClose = new UserOption("minCloseMenuItem", false,
   204         minCloseMenuItem, settings);
   205 
   206       autoStart = new UserOption(null, startupManager.Startup,
   207         startupMenuItem, settings);
   208       autoStart.Changed += delegate(object sender, EventArgs e) {
   209         try {
   210           startupManager.Startup = autoStart.Value;
   211         } catch (InvalidOperationException) {
   212           MessageBox.Show("Updating the auto-startup option failed.", "Error", 
   213             MessageBoxButtons.OK, MessageBoxIcon.Error);
   214           autoStart.Value = startupManager.Startup;
   215         }
   216       };
   217 
   218       readMainboardSensors = new UserOption("mainboardMenuItem", true, 
   219         mainboardMenuItem, settings);
   220       readMainboardSensors.Changed += delegate(object sender, EventArgs e) {
   221         computer.MainboardEnabled = readMainboardSensors.Value;
   222       };
   223 
   224       readCpuSensors = new UserOption("cpuMenuItem", true,
   225         cpuMenuItem, settings);
   226       readCpuSensors.Changed += delegate(object sender, EventArgs e) {
   227         computer.CPUEnabled = readCpuSensors.Value;
   228       };
   229 
   230       readRamSensors = new UserOption("ramMenuItem", true,
   231         ramMenuItem, settings);
   232       readRamSensors.Changed += delegate(object sender, EventArgs e) {
   233         computer.RAMEnabled = readRamSensors.Value;
   234       };
   235 
   236       readGpuSensors = new UserOption("gpuMenuItem", true,
   237         gpuMenuItem, settings);
   238       readGpuSensors.Changed += delegate(object sender, EventArgs e) {
   239         computer.GPUEnabled = readGpuSensors.Value;
   240       };
   241 
   242       readFanControllersSensors = new UserOption("fanControllerMenuItem", true,
   243         fanControllerMenuItem, settings);
   244       readFanControllersSensors.Changed += delegate(object sender, EventArgs e) {
   245         computer.FanControllerEnabled = readFanControllersSensors.Value;
   246       };
   247 
   248       readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
   249         settings);
   250       readHddSensors.Changed += delegate(object sender, EventArgs e) {
   251         computer.HDDEnabled = readHddSensors.Value;
   252       };
   253 
   254       showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
   255         settings);
   256       showGadget.Changed += delegate(object sender, EventArgs e) {
   257         if (gadget != null) 
   258           gadget.Visible = showGadget.Value;
   259       };
   260 
   261       celsiusMenuItem.Checked = 
   262         unitManager.TemperatureUnit == TemperatureUnit.Celsius;
   263       fahrenheitMenuItem.Checked = !celsiusMenuItem.Checked;
   264 
   265       server = new HttpServer(root, this.settings.GetValue("listenerPort", 8085));
   266       if (server.PlatformNotSupported) {
   267         webMenuItemSeparator.Visible = false;
   268         webMenuItem.Visible = false;
   269       }
   270 
   271       runWebServer = new UserOption("runWebServerMenuItem", false,
   272         runWebServerMenuItem, settings);
   273       runWebServer.Changed += delegate(object sender, EventArgs e) {
   274         if (runWebServer.Value)
   275           server.StartHTTPListener();
   276         else
   277           server.StopHTTPListener();
   278       };
   279 
   280       InitializePlotForm();
   281 
   282       startupMenuItem.Visible = startupManager.IsAvailable;
   283       
   284       if (startMinMenuItem.Checked) {
   285         if (!minTrayMenuItem.Checked) {
   286           WindowState = FormWindowState.Minimized;
   287           Show();
   288         }
   289       } else {
   290         Show();
   291       }
   292 
   293       // Create a handle, otherwise calling Close() does not fire FormClosed     
   294       IntPtr handle = Handle;
   295 
   296       // Make sure the settings are saved when the user logs off
   297       Microsoft.Win32.SystemEvents.SessionEnded += delegate {
   298         computer.Close();
   299         SaveConfiguration();
   300         if (runWebServer.Value) 
   301           server.Quit();
   302       };
   303     }
   304 
   305     private void InitializePlotForm() {
   306       plotForm = new Form();
   307       plotForm.FormBorderStyle = FormBorderStyle.SizableToolWindow;
   308       plotForm.ShowInTaskbar = false;
   309       plotForm.StartPosition = FormStartPosition.Manual;
   310       this.AddOwnedForm(plotForm);
   311       plotForm.Bounds = new Rectangle {
   312         X = settings.GetValue("plotForm.Location.X", -100000),
   313         Y = settings.GetValue("plotForm.Location.Y", 100),
   314         Width = settings.GetValue("plotForm.Width", 600),
   315         Height = settings.GetValue("plotForm.Height", 400)
   316       };
   317 
   318       showPlot = new UserOption("plotMenuItem", false, plotMenuItem, settings);
   319       plotLocation = new UserRadioGroup("plotLocation", 0,
   320         new[] { plotWindowMenuItem, plotBottomMenuItem, plotRightMenuItem },
   321         settings);
   322 
   323       showPlot.Changed += delegate(object sender, EventArgs e) {
   324         if (plotLocation.Value == 0) {
   325           if (showPlot.Value && this.Visible)
   326             plotForm.Show();
   327           else
   328             plotForm.Hide();
   329         } else {
   330           splitContainer.Panel2Collapsed = !showPlot.Value;
   331         }
   332         treeView.Invalidate();
   333       };
   334       plotLocation.Changed += delegate(object sender, EventArgs e) {
   335         switch (plotLocation.Value) {
   336           case 0:
   337             splitContainer.Panel2.Controls.Clear();
   338             splitContainer.Panel2Collapsed = true;
   339             plotForm.Controls.Add(plotPanel);
   340             if (showPlot.Value && this.Visible)
   341               plotForm.Show();
   342             break;
   343           case 1:
   344             plotForm.Controls.Clear();
   345             plotForm.Hide();
   346             splitContainer.Orientation = Orientation.Horizontal;
   347             splitContainer.Panel2.Controls.Add(plotPanel);
   348             splitContainer.Panel2Collapsed = !showPlot.Value;
   349             break;
   350           case 2:
   351             plotForm.Controls.Clear();
   352             plotForm.Hide();
   353             splitContainer.Orientation = Orientation.Vertical;
   354             splitContainer.Panel2.Controls.Add(plotPanel);
   355             splitContainer.Panel2Collapsed = !showPlot.Value;
   356             break;
   357         }
   358       };
   359 
   360       plotForm.FormClosing += delegate(object sender, FormClosingEventArgs e) {
   361         if (e.CloseReason == CloseReason.UserClosing) {
   362           // just switch off the plotting when the user closes the form
   363           if (plotLocation.Value == 0) {
   364             showPlot.Value = false;
   365           }
   366           e.Cancel = true;
   367         }
   368       };
   369 
   370       EventHandler moveOrResizePlotForm = delegate(object sender, EventArgs e) {
   371         if (plotForm.WindowState != FormWindowState.Minimized) {
   372           settings.SetValue("plotForm.Location.X", plotForm.Bounds.X);
   373           settings.SetValue("plotForm.Location.Y", plotForm.Bounds.Y);
   374           settings.SetValue("plotForm.Width", plotForm.Bounds.Width);
   375           settings.SetValue("plotForm.Height", plotForm.Bounds.Height);
   376         }
   377       };
   378       plotForm.Move += moveOrResizePlotForm;
   379       plotForm.Resize += moveOrResizePlotForm;
   380 
   381       plotForm.VisibleChanged += delegate(object sender, EventArgs e) {
   382         Rectangle bounds = new Rectangle(plotForm.Location, plotForm.Size);
   383         Screen screen = Screen.FromRectangle(bounds);
   384         Rectangle intersection =
   385           Rectangle.Intersect(screen.WorkingArea, bounds);
   386         if (intersection.Width < Math.Min(16, bounds.Width) ||
   387             intersection.Height < Math.Min(16, bounds.Height)) {
   388           plotForm.Location = new Point(
   389             screen.WorkingArea.Width / 2 - bounds.Width / 2,
   390             screen.WorkingArea.Height / 2 - bounds.Height / 2);
   391         }
   392       };
   393 
   394       this.VisibleChanged += delegate(object sender, EventArgs e) {
   395         if (this.Visible && showPlot.Value && plotLocation.Value == 0)
   396           plotForm.Show();
   397         else
   398           plotForm.Hide();
   399       };
   400     }
   401 
   402     private void InsertSorted(Collection<Node> nodes, HardwareNode node) {
   403       int i = 0;
   404       while (i < nodes.Count && nodes[i] is HardwareNode &&
   405         ((HardwareNode)nodes[i]).Hardware.HardwareType < 
   406           node.Hardware.HardwareType)
   407         i++;
   408       nodes.Insert(i, node);
   409     }
   410     
   411     private void SubHardwareAdded(IHardware hardware, Node node) {
   412       HardwareNode hardwareNode = 
   413         new HardwareNode(hardware, settings, unitManager);
   414       hardwareNode.PlotSelectionChanged += PlotSelectionChanged;
   415 
   416       InsertSorted(node.Nodes, hardwareNode);
   417 
   418       foreach (IHardware subHardware in hardware.SubHardware)
   419         SubHardwareAdded(subHardware, hardwareNode);  
   420     }
   421 
   422     private void HardwareAdded(IHardware hardware) {      
   423       SubHardwareAdded(hardware, root);
   424       PlotSelectionChanged(this, null);
   425     }
   426 
   427     private void HardwareRemoved(IHardware hardware) {
   428       List<HardwareNode> nodesToRemove = new List<HardwareNode>();
   429       foreach (Node node in root.Nodes) {
   430         HardwareNode hardwareNode = node as HardwareNode;
   431         if (hardwareNode != null && hardwareNode.Hardware == hardware)
   432           nodesToRemove.Add(hardwareNode);
   433       }
   434       foreach (HardwareNode hardwareNode in nodesToRemove) {
   435         root.Nodes.Remove(hardwareNode);
   436         hardwareNode.PlotSelectionChanged -= PlotSelectionChanged;
   437       }
   438       PlotSelectionChanged(this, null);
   439     }
   440 
   441     private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
   442       Node node = e.Node.Tag as Node;
   443       if (node != null) {
   444         Color color;
   445         if (node.IsVisible) {
   446           SensorNode sensorNode = node as SensorNode;
   447           if (plotMenuItem.Checked && sensorNode != null &&
   448             sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
   449             e.TextColor = color;
   450         } else {
   451           e.TextColor = Color.DarkGray;
   452         }
   453       }
   454     }
   455 
   456     private void PlotSelectionChanged(object sender, EventArgs e) {
   457       List<ISensor> selected = new List<ISensor>();
   458       IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
   459       int colorIndex = 0;
   460       foreach (TreeNodeAdv node in treeView.AllNodes) {
   461         SensorNode sensorNode = node.Tag as SensorNode;
   462         if (sensorNode != null) {
   463           if (sensorNode.Plot) {
   464             colors.Add(sensorNode.Sensor,
   465               plotColorPalette[colorIndex % plotColorPalette.Length]);
   466             selected.Add(sensorNode.Sensor);
   467           }
   468           colorIndex++;
   469         }
   470       }
   471       sensorPlotColors = colors;
   472       plotPanel.SetSensors(selected, colors);
   473     }
   474 
   475     private void nodeTextBoxText_EditorShowing(object sender,
   476       CancelEventArgs e) 
   477     {
   478       e.Cancel = !(treeView.CurrentNode != null &&
   479         (treeView.CurrentNode.Tag is SensorNode || 
   480          treeView.CurrentNode.Tag is HardwareNode));
   481     }
   482 
   483     private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
   484       NodeControlValueEventArgs e) {
   485       SensorNode node = e.Node.Tag as SensorNode;
   486       e.Value = (node != null) && plotMenuItem.Checked;
   487     }
   488 
   489     private void exitClick(object sender, EventArgs e) {
   490       Close();
   491     }
   492 
   493     private void timer_Tick(object sender, EventArgs e) {
   494       computer.Accept(updateVisitor);
   495       treeView.Invalidate();
   496       plotPanel.InvalidatePlot();
   497       systemTray.Redraw();
   498       if (gadget != null)
   499         gadget.Redraw();
   500 
   501       if (wmiProvider != null)
   502         wmiProvider.Update();
   503     }
   504 
   505     private void SaveConfiguration() {
   506       plotPanel.SetCurrentSettings();
   507       foreach (TreeColumn column in treeView.Columns)
   508         settings.SetValue("treeView.Columns." + column.Header + ".Width",
   509           column.Width);
   510 
   511       this.settings.SetValue("listenerPort", server.ListenerPort);
   512 
   513       string fileName = Path.ChangeExtension(
   514           System.Windows.Forms.Application.ExecutablePath, ".config");
   515       try {
   516         settings.Save(fileName);
   517       } catch (UnauthorizedAccessException) {
   518         MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
   519           "The current settings could not be saved.",
   520           "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   521       } catch (IOException) {
   522         MessageBox.Show("The path '" + fileName + "' is not writeable. " +
   523           "The current settings could not be saved.",
   524           "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   525       }
   526     }
   527 
   528     private void MainForm_Load(object sender, EventArgs e) {
   529       Rectangle newBounds = new Rectangle {
   530         X = settings.GetValue("mainForm.Location.X", Location.X),
   531         Y = settings.GetValue("mainForm.Location.Y", Location.Y),
   532         Width = settings.GetValue("mainForm.Width", 470),
   533         Height = settings.GetValue("mainForm.Height", 640)
   534       };
   535 
   536       Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
   537         int.MinValue, int.MinValue);
   538 
   539       foreach (Screen screen in Screen.AllScreens)
   540         fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds);
   541 
   542       Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds);
   543       if (intersection.Width < 20 || intersection.Height < 20 ||
   544         !settings.Contains("mainForm.Location.X")
   545       ) {
   546         newBounds.X = (Screen.PrimaryScreen.WorkingArea.Width / 2) -
   547                       (newBounds.Width/2);
   548 
   549         newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) -
   550                       (newBounds.Height / 2);
   551       }
   552 
   553       this.Bounds = newBounds;
   554     }
   555     
   556     private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   557       Visible = false;      
   558       systemTray.IsMainIconEnabled = false;
   559       timer.Enabled = false;            
   560       computer.Close();
   561       SaveConfiguration();
   562       if (runWebServer.Value)
   563           server.Quit();
   564       systemTray.Dispose();
   565     }
   566 
   567     private void aboutMenuItem_Click(object sender, EventArgs e) {
   568       new AboutBox().ShowDialog();
   569     }
   570 
   571     private void treeView_Click(object sender, EventArgs e) {
   572 
   573       MouseEventArgs m = e as MouseEventArgs;
   574       if (m == null || m.Button != MouseButtons.Right)
   575         return;
   576 
   577       NodeControlInfo info = treeView.GetNodeControlInfoAt(
   578         new Point(m.X, m.Y)
   579       );
   580       treeView.SelectedNode = info.Node;
   581       if (info.Node != null) {
   582         SensorNode node = info.Node.Tag as SensorNode;
   583         if (node != null && node.Sensor != null) {
   584           treeContextMenu.MenuItems.Clear();
   585           if (node.Sensor.Parameters.Length > 0) {
   586             MenuItem item = new MenuItem("Parameters...");
   587             item.Click += delegate(object obj, EventArgs args) {
   588               ShowParameterForm(node.Sensor);
   589             };
   590             treeContextMenu.MenuItems.Add(item);
   591           }
   592           if (nodeTextBoxText.EditEnabled) {
   593             MenuItem item = new MenuItem("Rename");
   594             item.Click += delegate(object obj, EventArgs args) {
   595               nodeTextBoxText.BeginEdit();
   596             };
   597             treeContextMenu.MenuItems.Add(item);
   598           }
   599           if (node.IsVisible) {
   600             MenuItem item = new MenuItem("Hide");
   601             item.Click += delegate(object obj, EventArgs args) {
   602               node.IsVisible = false;
   603             };
   604             treeContextMenu.MenuItems.Add(item);
   605           } else {
   606             MenuItem item = new MenuItem("Unhide");
   607             item.Click += delegate(object obj, EventArgs args) {
   608               node.IsVisible = true;
   609             };
   610             treeContextMenu.MenuItems.Add(item);
   611           }
   612           treeContextMenu.MenuItems.Add(new MenuItem("-"));
   613           {
   614             MenuItem item = new MenuItem("Show in Tray");
   615             item.Checked = systemTray.Contains(node.Sensor);
   616             item.Click += delegate(object obj, EventArgs args) {
   617               if (item.Checked)
   618                 systemTray.Remove(node.Sensor);
   619               else
   620                 systemTray.Add(node.Sensor, true);
   621             };
   622             treeContextMenu.MenuItems.Add(item);
   623           }
   624           if (gadget != null) {
   625             MenuItem item = new MenuItem("Show in Gadget");
   626             item.Checked = gadget.Contains(node.Sensor);
   627             item.Click += delegate(object obj, EventArgs args) {
   628               if (item.Checked) {
   629                 gadget.Remove(node.Sensor);
   630               } else {
   631                 gadget.Add(node.Sensor);
   632               }
   633             };
   634             treeContextMenu.MenuItems.Add(item);
   635           }
   636           if (node.Sensor.Control != null) {
   637             treeContextMenu.MenuItems.Add(new MenuItem("-"));
   638             IControl control = node.Sensor.Control;
   639             MenuItem controlItem = new MenuItem("Control");
   640             MenuItem defaultItem = new MenuItem("Default");
   641             defaultItem.Checked = control.ControlMode == ControlMode.Default;
   642             controlItem.MenuItems.Add(defaultItem);
   643             defaultItem.Click += delegate(object obj, EventArgs args) {
   644               control.SetDefault();
   645             };
   646             MenuItem manualItem = new MenuItem("Manual");
   647             controlItem.MenuItems.Add(manualItem);
   648             manualItem.Checked = control.ControlMode == ControlMode.Software;
   649             for (int i = 0; i <= 100; i += 5) {
   650               if (i <= control.MaxSoftwareValue &&
   651                   i >= control.MinSoftwareValue) {
   652                 MenuItem item = new MenuItem(i + " %");
   653                 item.RadioCheck = true;
   654                 manualItem.MenuItems.Add(item);
   655                 item.Checked = control.ControlMode == ControlMode.Software &&
   656                   Math.Round(control.SoftwareValue) == i;
   657                 int softwareValue = i;
   658                 item.Click += delegate(object obj, EventArgs args) {
   659                   control.SetSoftware(softwareValue);
   660                 };
   661               }
   662             }
   663             treeContextMenu.MenuItems.Add(controlItem);
   664           }
   665 
   666           treeContextMenu.Show(treeView, new Point(m.X, m.Y));
   667         }
   668 
   669         HardwareNode hardwareNode = info.Node.Tag as HardwareNode;
   670         if (hardwareNode != null && hardwareNode.Hardware != null) {
   671           treeContextMenu.MenuItems.Clear();
   672 
   673           if (nodeTextBoxText.EditEnabled) {
   674             MenuItem item = new MenuItem("Rename");
   675             item.Click += delegate(object obj, EventArgs args) {
   676               nodeTextBoxText.BeginEdit();
   677             };
   678             treeContextMenu.MenuItems.Add(item);
   679           }
   680 
   681           treeContextMenu.Show(treeView, new Point(m.X, m.Y));
   682         }
   683       }
   684     }
   685 
   686     private void saveReportMenuItem_Click(object sender, EventArgs e) {
   687       string report = computer.GetReport();
   688       if (saveFileDialog.ShowDialog() == DialogResult.OK) {
   689         using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
   690           w.Write(report);
   691         }
   692       }
   693     }
   694 
   695     private void SysTrayHideShow() {
   696       Visible = !Visible;
   697       if (Visible)
   698         Activate();    
   699     }
   700 
   701     protected override void WndProc(ref Message m) {
   702       const int WM_SYSCOMMAND = 0x112;
   703       const int SC_MINIMIZE = 0xF020;
   704       const int SC_CLOSE = 0xF060;
   705 
   706       if (minimizeToTray.Value && 
   707         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt64() == SC_MINIMIZE) {
   708         SysTrayHideShow();
   709       } else if (minimizeOnClose.Value &&
   710         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt64() == SC_CLOSE) {
   711         /*
   712          * Apparently the user wants to minimize rather than close
   713          * Now we still need to check if we're going to the tray or not
   714          * 
   715          * Note: the correct way to do this would be to send out SC_MINIMIZE,
   716          * but since the code here is so simple,
   717          * that would just be a waste of time.
   718          */
   719         if (minimizeToTray.Value)
   720           SysTrayHideShow();
   721         else
   722           WindowState = FormWindowState.Minimized;
   723       } else {      
   724         base.WndProc(ref m);
   725       }
   726     }
   727 
   728     private void hideShowClick(object sender, EventArgs e) {
   729       SysTrayHideShow();
   730     }
   731 
   732     private void ShowParameterForm(ISensor sensor) {
   733       ParameterForm form = new ParameterForm();
   734       form.Parameters = sensor.Parameters;
   735       form.captionLabel.Text = sensor.Name;
   736       form.ShowDialog();
   737     }
   738 
   739     private void treeView_NodeMouseDoubleClick(object sender, 
   740       TreeNodeAdvMouseEventArgs e) {
   741       SensorNode node = e.Node.Tag as SensorNode;
   742       if (node != null && node.Sensor != null && 
   743         node.Sensor.Parameters.Length > 0) {
   744         ShowParameterForm(node.Sensor);
   745       }
   746     }
   747 
   748     private void celsiusMenuItem_Click(object sender, EventArgs e) {
   749       celsiusMenuItem.Checked = true;
   750       fahrenheitMenuItem.Checked = false;
   751       unitManager.TemperatureUnit = TemperatureUnit.Celsius;
   752     }
   753 
   754     private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
   755       celsiusMenuItem.Checked = false;
   756       fahrenheitMenuItem.Checked = true;
   757       unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
   758     }
   759 
   760     private void sumbitReportMenuItem_Click(object sender, EventArgs e) 
   761     {
   762       ReportForm form = new ReportForm();
   763       form.Report = computer.GetReport();
   764       form.ShowDialog();      
   765     }
   766 
   767     private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
   768       computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
   769         sensor.ResetMin();
   770         sensor.ResetMax();
   771       }));
   772     }
   773 
   774     private void MainForm_MoveOrResize(object sender, EventArgs e) {
   775       if (WindowState != FormWindowState.Minimized) {
   776         settings.SetValue("mainForm.Location.X", Bounds.X);
   777         settings.SetValue("mainForm.Location.Y", Bounds.Y);
   778         settings.SetValue("mainForm.Width", Bounds.Width);
   779         settings.SetValue("mainForm.Height", Bounds.Height);
   780       }
   781     }
   782 
   783     private void resetClick(object sender, EventArgs e) {
   784       // disable the fallback MainIcon during reset, otherwise icon visibility
   785       // might be lost 
   786       systemTray.IsMainIconEnabled = false;
   787       computer.Close();
   788       computer.Open();
   789       // restore the MainIcon setting
   790       systemTray.IsMainIconEnabled = minimizeToTray.Value;
   791     }
   792 
   793     private void treeView_MouseMove(object sender, MouseEventArgs e) {
   794       selectionDragging = selectionDragging &
   795         (e.Button & (MouseButtons.Left | MouseButtons.Right)) > 0; 
   796 
   797       if (selectionDragging)
   798         treeView.SelectedNode = treeView.GetNodeAt(e.Location);     
   799     }
   800 
   801     private void treeView_MouseDown(object sender, MouseEventArgs e) {
   802       selectionDragging = true;
   803     }
   804 
   805     private void treeView_MouseUp(object sender, MouseEventArgs e) {
   806       selectionDragging = false;
   807     }
   808 
   809     private void serverPortMenuItem_Click(object sender, EventArgs e) {
   810       new PortForm(this).ShowDialog();
   811     }
   812 
   813     public HttpServer Server {
   814       get { return server; }
   815     }
   816 
   817   }
   818 }