GUI/MainForm.cs
author moel.mich
Wed, 27 Jan 2010 18:12:51 +0000
changeset 2 105939e4eb7e
parent 1 361e324a0ed4
child 14 51c2f209da6d
permissions -rw-r--r--
Fixed a NullReferenceException in the HDD SMART code.
     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.Text;
    44 using System.Windows.Forms;
    45 using Aga.Controls.Tree;
    46 using Aga.Controls.Tree.NodeControls;
    47 using OpenHardwareMonitor.Hardware;
    48 
    49 namespace OpenHardwareMonitor.GUI {
    50   public partial class MainForm : Form {
    51 
    52     private Node root;
    53     private List<IGroup> groupList = new List<IGroup>();
    54     private TreeModel treeModel;
    55     private IDictionary<ISensor, Color> sensorPlotColors = 
    56       new Dictionary<ISensor, Color>();
    57     private Color[] plotColorPalette;
    58 
    59     public MainForm() {
    60       
    61       InitializeComponent();
    62       this.Font = SystemFonts.MessageBoxFont;
    63       treeView.Font = SystemFonts.MessageBoxFont;
    64       plotPanel.Font = SystemFonts.MessageBoxFont;      
    65       
    66       nodeCheckBox.IsVisibleValueNeeded += 
    67         new EventHandler<NodeControlValueEventArgs>(
    68           nodeCheckBox_IsVisibleValueNeeded);
    69       nodeCheckBox.CheckStateChanged += 
    70         new EventHandler<TreePathEventArgs>(UpdatePlotSelection);
    71       nodeTextBoxText.DrawText += 
    72         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    73       nodeTextBoxValue.DrawText +=
    74         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    75       nodeTextBoxMin.DrawText +=
    76         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    77       nodeTextBoxMax.DrawText +=
    78         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    79       nodeTextBoxLimit.DrawText += 
    80         new EventHandler<DrawEventArgs>(nodeTextBoxLimit_DrawText);
    81 
    82       if (Utilities.Config.Contains("mainForm.Location.X")) {
    83         int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
    84         x = x < 0 ? 0 : x;
    85         int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
    86         y = y < 0 ? 0 : y;
    87         this.Location = new Point(x, y);
    88       } else {
    89         StartPosition = FormStartPosition.CenterScreen;
    90       }
    91 
    92       Width = Utilities.Config.Get("mainForm.Width", Width);
    93       Height = Utilities.Config.Get("mainForm.Height", Height);
    94          
    95       treeModel = new TreeModel();
    96       root = new Node(System.Environment.MachineName);
    97       root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
    98       
    99       treeModel.Nodes.Add(root);
   100       treeView.Model = treeModel;
   101 
   102       AddGroup(new Hardware.SMBIOS.SMBIOSGroup());
   103       AddGroup(new Hardware.LPC.LPCGroup());
   104       AddGroup(new Hardware.CPU.CPUGroup());
   105       AddGroup(new Hardware.ATI.ATIGroup());
   106       AddGroup(new Hardware.Nvidia.NvidiaGroup());
   107       AddGroup(new Hardware.TBalancer.TBalancerGroup());
   108       
   109       plotColorPalette = new Color[14];
   110       plotColorPalette[0] = Color.Blue;
   111       plotColorPalette[1] = Color.OrangeRed;
   112       plotColorPalette[2] = Color.Green;
   113       plotColorPalette[3] = Color.LightSeaGreen;
   114       plotColorPalette[4] = Color.Goldenrod;
   115       plotColorPalette[5] = Color.DarkViolet;
   116       plotColorPalette[6] = Color.YellowGreen;
   117       plotColorPalette[7] = Color.SaddleBrown;
   118       plotColorPalette[8] = Color.Gray;
   119       plotColorPalette[9] = Color.RoyalBlue;
   120       plotColorPalette[10] = Color.DeepPink;
   121       plotColorPalette[11] = Color.MediumSeaGreen;
   122       plotColorPalette[12] = Color.Olive;
   123       plotColorPalette[13] = Color.Firebrick;
   124 
   125       plotMenuItem.Checked = Utilities.Config.Get(plotMenuItem.Name, false);
   126       minMenuItem.Checked = Utilities.Config.Get(minMenuItem.Name, false);
   127       maxMenuItem.Checked = Utilities.Config.Get(maxMenuItem.Name, true);
   128       limitMenuItem.Checked = Utilities.Config.Get(limitMenuItem.Name, false);
   129       hddMenuItem.Checked = Utilities.Config.Get(hddMenuItem.Name, true);
   130 
   131       voltMenuItem.Checked = Utilities.Config.Get(voltMenuItem.Name, true);
   132       clocksMenuItem.Checked = Utilities.Config.Get(clocksMenuItem.Name, true);
   133       tempMenuItem.Checked = Utilities.Config.Get(tempMenuItem.Name, true);
   134       fansMenuItem.Checked = Utilities.Config.Get(fansMenuItem.Name, true);
   135 
   136       timer.Enabled = true;
   137     }
   138 
   139     private void AddGroup(IGroup group) {
   140       groupList.Add(group);
   141       foreach (IHardware hardware in group.Hardware)
   142         root.Nodes.Add(new HardwareNode(hardware));
   143     }
   144 
   145     private void RemoveGroup(IGroup group) {
   146       List<Node> nodesToRemove = new List<Node>();
   147       foreach (IHardware hardware in group.Hardware)
   148         foreach (Node node in root.Nodes) {
   149           HardwareNode hardwareNode = node as HardwareNode;
   150           if (hardwareNode != null && hardwareNode.Hardware == hardware)
   151             nodesToRemove.Add(node);
   152         }
   153       foreach (Node node in nodesToRemove)
   154         root.Nodes.Remove(node);
   155       groupList.Remove(group);
   156     }
   157 
   158     private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
   159       SensorNode sensorNode = e.Node.Tag as SensorNode;
   160       if (sensorNode != null) 
   161         e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
   162     }
   163 
   164     private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
   165       if (!plotMenuItem.Checked)
   166         return;      
   167 
   168       SensorNode sensorNode = e.Node.Tag as SensorNode;
   169       if (sensorNode != null) {
   170         Color color;
   171         if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color)) 
   172           e.TextColor = color;        
   173       }
   174     }
   175 
   176     private void UpdatePlotSelection(object sender, 
   177       TreePathEventArgs e) 
   178     {
   179       List<ISensor> selected = new List<ISensor>();
   180       IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
   181       int colorIndex = 0;
   182       foreach (TreeNodeAdv node in treeView.AllNodes) {
   183         SensorNode sensorNode = node.Tag as SensorNode;
   184         if (sensorNode != null && 
   185           sensorNode.Sensor.SensorType == SensorType.Temperature) {
   186           if (sensorNode.Plot) {
   187             colors.Add(sensorNode.Sensor,
   188               plotColorPalette[colorIndex % plotColorPalette.Length]);
   189             selected.Add(sensorNode.Sensor);
   190           }
   191           colorIndex++;
   192         }
   193       }
   194       sensorPlotColors = colors;
   195       plotPanel.SetSensors(selected, colors);
   196     }
   197 
   198     private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
   199       NodeControlValueEventArgs e) {
   200       SensorNode node = e.Node.Tag as SensorNode;
   201       e.Value = (node != null) && 
   202         (node.Sensor.SensorType == SensorType.Temperature) && 
   203         plotMenuItem.Checked;
   204     }
   205 
   206     private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
   207       Close();
   208     }
   209 
   210     private void timer_Tick(object sender, EventArgs e) {
   211       
   212       #if !DEBUG
   213       try {
   214       #endif
   215         foreach (IGroup group in groupList)
   216           foreach (IHardware hardware in group.Hardware)
   217             hardware.Update();
   218       #if !DEBUG
   219       } catch (Exception exception) {
   220         Utilities.CrashReport.Save(exception);
   221         Close();
   222       }
   223       #endif
   224             
   225       treeView.Invalidate();
   226       plotPanel.Invalidate();
   227     }
   228 
   229     private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   230       Utilities.Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
   231       Utilities.Config.Set(minMenuItem.Name, minMenuItem.Checked);
   232       Utilities.Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
   233       Utilities.Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
   234       Utilities.Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
   235 
   236       Utilities.Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
   237       Utilities.Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
   238       Utilities.Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
   239       Utilities.Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
   240 
   241       Utilities.Config.Set("mainForm.Location.X", Location.X);
   242       Utilities.Config.Set("mainForm.Location.Y", Location.Y);
   243       Utilities.Config.Set("mainForm.Width", Width);
   244       Utilities.Config.Set("mainForm.Height", Height);
   245 
   246       foreach (IGroup group in groupList)
   247         group.Close();
   248     }
   249 
   250     private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
   251       new AboutBox().ShowDialog();
   252     }
   253 
   254     private void plotToolStripMenuItem_CheckedChanged(object sender, 
   255       EventArgs e) 
   256     {
   257       splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
   258       treeView.Invalidate();
   259     }
   260 
   261     private void valueToolStripMenuItem_CheckedChanged(object sender, 
   262       EventArgs e) 
   263     {
   264       treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
   265     }
   266 
   267     private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   268     {
   269       treeView.Columns[2].IsVisible = minMenuItem.Checked;
   270     }
   271 
   272     private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   273     {
   274       treeView.Columns[3].IsVisible = maxMenuItem.Checked;
   275     }
   276 
   277     private void limitToolStripMenuItem_CheckedChanged(object sender, 
   278       EventArgs e) {
   279       treeView.Columns[4].IsVisible = limitMenuItem.Checked;
   280     }
   281 
   282     private void treeView_Click(object sender, EventArgs e) {
   283       
   284       MouseEventArgs m = e as MouseEventArgs;
   285       if (m == null || m.Button != MouseButtons.Right)
   286         return;
   287 
   288       NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
   289       if (info.Control == null)
   290         columnsContextMenuStrip.Show(treeView, m.X, m.Y);
   291     }
   292 
   293     private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
   294       ReportWriter.Save(groupList, new Version(Application.ProductVersion));
   295     }
   296 
   297     private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
   298       EventArgs e) 
   299     {
   300       if (hddMenuItem.Checked) {
   301         AddGroup(new Hardware.HDD.HDDGroup());
   302         UpdateSensorTypeChecked(null, null);
   303       } else {
   304         List<IGroup> groupsToRemove = new List<IGroup>();
   305         foreach (IGroup group in groupList) 
   306           if (group is Hardware.HDD.HDDGroup)
   307             groupsToRemove.Add(group);
   308         foreach (IGroup group in groupsToRemove) {
   309           group.Close();
   310           RemoveGroup(group);
   311         }
   312         UpdatePlotSelection(null, null);        
   313       }
   314     }
   315 
   316     private void UpdateSensorTypeChecked(object sender, EventArgs e) {
   317       foreach (HardwareNode node in root.Nodes) {
   318         node.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
   319         node.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
   320         node.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
   321         node.SetVisible(SensorType.Fan, fansMenuItem.Checked);
   322       }
   323     }  
   324   }
   325 }