GUI/MainForm.cs
author moel.mich
Fri, 14 May 2010 22:30:06 +0000
changeset 111 2b8a8cf92c3a
parent 110 411b72b73d8f
child 113 62a2848ec0a6
permissions -rw-r--r--
Added a user interface to configure certain sensors as hidden. This fixed Issue 53.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     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
     8  
     9   http://www.mozilla.org/MPL/
    10 
    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.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    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) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    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.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.ComponentModel;
    41 using System.Configuration;
    42 using System.Drawing;
    43 using System.IO;
    44 using System.Text;
    45 using System.Windows.Forms;
    46 using Aga.Controls.Tree;
    47 using Aga.Controls.Tree.NodeControls;
    48 using OpenHardwareMonitor.Hardware;
    49 using OpenHardwareMonitor.Utilities;
    50 
    51 namespace OpenHardwareMonitor.GUI {
    52   public partial class MainForm : Form {
    53 
    54     private Computer computer = new Computer();
    55     private Node root;
    56     private TreeModel treeModel;
    57     private IDictionary<ISensor, Color> sensorPlotColors = 
    58       new Dictionary<ISensor, Color>();
    59     private Color[] plotColorPalette;
    60     private SensorSystemTray sensorSystemTray;
    61     private NotifyIcon notifyIcon;
    62     private StartupManager startupManager = new StartupManager();
    63     private UpdateVisitor updateVisitor = new UpdateVisitor();
    64 
    65     public MainForm() {      
    66       InitializeComponent();
    67       this.Font = SystemFonts.MessageBoxFont;
    68       treeView.Font = SystemFonts.MessageBoxFont;
    69       plotPanel.Font = SystemFonts.MessageBoxFont;
    70       
    71       nodeCheckBox.IsVisibleValueNeeded += 
    72         new EventHandler<NodeControlValueEventArgs>(
    73           nodeCheckBox_IsVisibleValueNeeded);
    74       nodeCheckBox.CheckStateChanged += 
    75         new EventHandler<TreePathEventArgs>(UpdatePlotSelection);
    76       nodeTextBoxText.DrawText += 
    77         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    78       nodeTextBoxValue.DrawText +=
    79         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    80       nodeTextBoxMin.DrawText +=
    81         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    82       nodeTextBoxMax.DrawText +=
    83         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    84       nodeTextBoxLimit.DrawText += 
    85         new EventHandler<DrawEventArgs>(nodeTextBoxLimit_DrawText);
    86 
    87       if (Utilities.Config.Contains("mainForm.Location.X")) {
    88         int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
    89         x = x < 0 ? 0 : x;
    90         int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
    91         y = y < 0 ? 0 : y;
    92         this.Location = new Point(x, y);
    93       } else {
    94         StartPosition = FormStartPosition.CenterScreen;
    95       }
    96 
    97       Width = Utilities.Config.Get("mainForm.Width", Width);
    98       Height = Utilities.Config.Get("mainForm.Height", Height);
    99          
   100       treeModel = new TreeModel();
   101       root = new Node(System.Environment.MachineName);
   102       root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
   103       
   104       treeModel.Nodes.Add(root);
   105       treeView.Model = treeModel;
   106       
   107       notifyIcon = new NotifyIcon();
   108       notifyIcon.ContextMenuStrip = this.notifyContextMenuStrip;
   109       notifyIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
   110       notifyIcon.Text = "Open Hardware Monitor";      
   111       notifyIcon.DoubleClick += new EventHandler(this.hideShowClick);
   112 
   113       sensorSystemTray = new SensorSystemTray(computer);
   114 
   115       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
   116       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
   117       computer.Open();
   118 
   119       timer.Enabled = true;
   120 
   121       plotColorPalette = new Color[13];
   122       plotColorPalette[0] = Color.Blue;
   123       plotColorPalette[1] = Color.OrangeRed;
   124       plotColorPalette[2] = Color.Green;
   125       plotColorPalette[3] = Color.LightSeaGreen;
   126       plotColorPalette[4] = Color.Goldenrod;
   127       plotColorPalette[5] = Color.DarkViolet;
   128       plotColorPalette[6] = Color.YellowGreen;
   129       plotColorPalette[7] = Color.SaddleBrown;
   130       plotColorPalette[8] = Color.RoyalBlue;
   131       plotColorPalette[9] = Color.DeepPink;
   132       plotColorPalette[10] = Color.MediumSeaGreen;
   133       plotColorPalette[11] = Color.Olive;
   134       plotColorPalette[12] = Color.Firebrick;
   135 
   136       hiddenMenuItem.Checked = Config.Get(hiddenMenuItem.Name, false);
   137       plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
   138 
   139       valueMenuItem.Checked = Config.Get(valueMenuItem.Name, true);
   140       minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
   141       maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
   142       limitMenuItem.Checked = Config.Get(limitMenuItem.Name, false);
   143 
   144       startMinMenuItem.Checked = Config.Get(startMinMenuItem.Name, false); 
   145       minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
   146       startupMenuItem.Checked = startupManager.Startup;
   147       hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);   
   148 
   149       if (startMinMenuItem.Checked) {
   150         if (!minTrayMenuItem.Checked) {
   151           WindowState = FormWindowState.Minimized;
   152           Show();
   153         }
   154       } else {
   155         Show();
   156       }
   157 
   158       // Create a handle, otherwise calling Close() does not fire FormClosed     
   159       IntPtr handle = Handle;
   160     }
   161 
   162     private void SubHardwareAdded(IHardware hardware, Node node) {
   163       Node hardwareNode = new HardwareNode(hardware);
   164       node.Nodes.Add(hardwareNode);
   165       foreach (IHardware subHardware in hardware.SubHardware)
   166         SubHardwareAdded(subHardware, hardwareNode);  
   167     }
   168 
   169     private void HardwareAdded(IHardware hardware) {
   170       Node hardwareNode = new HardwareNode(hardware);
   171       root.Nodes.Add(hardwareNode);
   172       foreach (IHardware subHardware in hardware.SubHardware)
   173         SubHardwareAdded(subHardware, hardwareNode);     
   174     }
   175 
   176     private void HardwareRemoved(IHardware hardware) {      
   177       List<Node> nodesToRemove = new List<Node>();
   178       foreach (Node node in root.Nodes) {
   179         HardwareNode hardwareNode = node as HardwareNode;
   180         if (hardwareNode != null && hardwareNode.Hardware == hardware)
   181           nodesToRemove.Add(node);
   182       }
   183       foreach (Node node in nodesToRemove)
   184         root.Nodes.Remove(node);
   185     }
   186 
   187     private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
   188       SensorNode sensorNode = e.Node.Tag as SensorNode;
   189       if (sensorNode != null) 
   190         e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
   191     }
   192 
   193     private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
   194       Node node = e.Node.Tag as Node;
   195       if (node != null) {
   196         Color color;
   197         if (node.IsVisible) {
   198           SensorNode sensorNode = node as SensorNode;
   199           if (plotMenuItem.Checked && sensorNode != null &&
   200             sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
   201             e.TextColor = color;
   202         } else {
   203           e.TextColor = Color.DarkGray;
   204         }
   205       }
   206     }
   207 
   208     private void UpdatePlotSelection(object sender, 
   209       TreePathEventArgs e) 
   210     {
   211       List<ISensor> selected = new List<ISensor>();
   212       IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
   213       int colorIndex = 0;
   214       foreach (TreeNodeAdv node in treeView.AllNodes) {
   215         SensorNode sensorNode = node.Tag as SensorNode;
   216         if (sensorNode != null && 
   217           sensorNode.Sensor.SensorType == SensorType.Temperature) {
   218           if (sensorNode.Plot) {
   219             colors.Add(sensorNode.Sensor,
   220               plotColorPalette[colorIndex % plotColorPalette.Length]);
   221             selected.Add(sensorNode.Sensor);
   222           }
   223           colorIndex++;
   224         }
   225       }
   226       sensorPlotColors = colors;
   227       plotPanel.SetSensors(selected, colors);
   228     }
   229 
   230     private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
   231       NodeControlValueEventArgs e) {
   232       SensorNode node = e.Node.Tag as SensorNode;
   233       e.Value = (node != null) && 
   234         (node.Sensor.SensorType == SensorType.Temperature) && 
   235         plotMenuItem.Checked;
   236     }
   237 
   238     private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
   239       Close();      
   240     }
   241 
   242     private void timer_Tick(object sender, EventArgs e) {
   243       computer.Accept(updateVisitor);
   244       treeView.Invalidate();
   245       plotPanel.Invalidate();
   246       sensorSystemTray.Redraw();
   247     }
   248 
   249     private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   250       
   251       Config.Set(hiddenMenuItem.Name, hiddenMenuItem.Checked);
   252       Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
   253 
   254       Config.Set(valueMenuItem.Name, valueMenuItem.Checked);
   255       Config.Set(minMenuItem.Name, minMenuItem.Checked);
   256       Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
   257       Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
   258 
   259       Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked);
   260       Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
   261       Config.Set(hddMenuItem.Name, hddMenuItem.Checked);   
   262 
   263       if (WindowState != FormWindowState.Minimized) {
   264         Config.Set("mainForm.Location.X", Location.X);
   265         Config.Set("mainForm.Location.Y", Location.Y);
   266         Config.Set("mainForm.Width", Width);
   267         Config.Set("mainForm.Height", Height);
   268       }
   269 
   270       timer.Enabled = false;
   271 
   272       sensorSystemTray.Dispose();
   273       notifyIcon.Dispose();
   274       computer.Close();
   275     }
   276 
   277     private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
   278       new AboutBox().ShowDialog();
   279     }
   280 
   281     private void plotToolStripMenuItem_CheckedChanged(object sender, 
   282       EventArgs e) 
   283     {
   284       splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
   285       treeView.Invalidate();
   286     }
   287 
   288     private void treeView_Click(object sender, EventArgs e) {
   289       
   290       MouseEventArgs m = e as MouseEventArgs;
   291       if (m == null || m.Button != MouseButtons.Right)
   292         return;
   293 
   294       NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
   295       if (info.Control != null) {
   296         SensorNode node = info.Node.Tag as SensorNode;
   297         if (node != null && node.Sensor != null) {
   298 
   299           sensorContextMenuStrip.Items.Clear();
   300           if (node.Sensor.Parameters.Length > 0) {
   301             ToolStripMenuItem item = new ToolStripMenuItem("Parameters...");
   302             item.Click += delegate(object obj, EventArgs args) {
   303               ShowParameterForm(node.Sensor);
   304             };
   305             sensorContextMenuStrip.Items.Add(item);
   306           }
   307           if (node.IsVisible) {
   308             ToolStripMenuItem item = new ToolStripMenuItem("Hide");
   309             item.Click += delegate(object obj, EventArgs args) {
   310               node.IsVisible = false;
   311             };
   312             sensorContextMenuStrip.Items.Add(item);
   313           } else {
   314             ToolStripMenuItem item = new ToolStripMenuItem("Unhide");
   315             item.Click += delegate(object obj, EventArgs args) {
   316               node.IsVisible = true;
   317             };
   318             sensorContextMenuStrip.Items.Add(item);
   319           }         
   320           if (sensorSystemTray.Contains(node.Sensor)) {
   321             ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
   322             item.Click += delegate(object obj, EventArgs args) {
   323               sensorSystemTray.Remove(node.Sensor);
   324             };
   325             sensorContextMenuStrip.Items.Add(item);
   326           } else {
   327             ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
   328             item.Click += delegate(object obj, EventArgs args) {
   329               sensorSystemTray.Add(node.Sensor, true);
   330             };
   331             sensorContextMenuStrip.Items.Add(item);
   332           }
   333           sensorContextMenuStrip.Show(treeView, m.X, m.Y);
   334         }
   335       }
   336     }
   337 
   338     private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
   339       string report = computer.GetReport();
   340       if (saveFileDialog.ShowDialog() == DialogResult.OK) {
   341         using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
   342           w.Write(report);
   343         }
   344       }
   345     }
   346 
   347     private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
   348       EventArgs e) 
   349     {
   350       computer.HDDEnabled = hddMenuItem.Checked;
   351       UpdatePlotSelection(null, null);      
   352     }
   353 
   354     private void SysTrayHideShow() {
   355       Visible = !Visible;
   356       if (Visible)
   357         Activate();    
   358     }
   359 
   360     protected override void WndProc(ref Message m) {
   361       const int WM_SYSCOMMAND = 0x112;
   362       const int SC_MINIMIZE = 0xF020;
   363       if (minTrayMenuItem.Checked && 
   364         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
   365         SysTrayHideShow();
   366       } else {      
   367         base.WndProc(ref m);
   368       }
   369     }
   370 
   371     private void hideShowClick(object sender, EventArgs e) {
   372       SysTrayHideShow();
   373     }
   374 
   375     private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
   376       ToolStripMenuItem item = sender as ToolStripMenuItem;
   377       if (item == null)
   378         return;
   379 
   380       ISensor sensor = item.Owner.Tag as ISensor;
   381       if (sensor == null)
   382         return;
   383 
   384       sensorSystemTray.Remove(sensor);
   385     }
   386 
   387     private void ShowParameterForm(ISensor sensor) {
   388       ParameterForm form = new ParameterForm();
   389       form.Parameters = sensor.Parameters;
   390       form.captionLabel.Text = sensor.Name;
   391       form.ShowDialog();
   392     }
   393 
   394     private void treeView_NodeMouseDoubleClick(object sender, 
   395       TreeNodeAdvMouseEventArgs e) {
   396       SensorNode node = e.Node.Tag as SensorNode;
   397       if (node != null && node.Sensor != null && 
   398         node.Sensor.Parameters.Length > 0) {
   399         ShowParameterForm(node.Sensor);
   400       }
   401     }
   402 
   403     private void runOnWindowsStartupToolStripMenuItem_CheckedChanged(
   404       object sender, EventArgs e) 
   405     {
   406       startupManager.Startup = startupMenuItem.Checked;
   407     }
   408 
   409     private void minTrayMenuItem_CheckedChanged(object sender, EventArgs e) {
   410       notifyIcon.Visible = minTrayMenuItem.Checked;
   411     }
   412 
   413     private void hiddenSensorsMenuItem_CheckedChanged(object sender, 
   414       EventArgs e) {
   415       treeModel.ForceVisible = hiddenMenuItem.Checked;
   416     }
   417 
   418     private void valueMenuItem_CheckedChanged(object sender, EventArgs e) {
   419       treeView.Columns[1].IsVisible = valueMenuItem.Checked;
   420     }
   421 
   422     private void minMenuItem_CheckedChanged(object sender, EventArgs e) {
   423       treeView.Columns[2].IsVisible = minMenuItem.Checked;
   424     }
   425 
   426     private void maxMenuItem_CheckedChanged(object sender, EventArgs e) {
   427       treeView.Columns[3].IsVisible = maxMenuItem.Checked;
   428     }
   429 
   430     private void limitMenuItem_CheckedChanged(object sender, EventArgs e) {
   431       treeView.Columns[4].IsVisible = limitMenuItem.Checked;
   432     }
   433   }
   434 }