GUI/MainForm.cs
changeset 1 361e324a0ed4
child 2 105939e4eb7e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GUI/MainForm.cs	Tue Jan 26 22:37:48 2010 +0000
     1.3 @@ -0,0 +1,325 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.ComponentModel;
    1.44 +using System.Configuration;
    1.45 +using System.Drawing;
    1.46 +using System.Text;
    1.47 +using System.Windows.Forms;
    1.48 +using Aga.Controls.Tree;
    1.49 +using Aga.Controls.Tree.NodeControls;
    1.50 +using OpenHardwareMonitor.Hardware;
    1.51 +
    1.52 +namespace OpenHardwareMonitor.GUI {
    1.53 +  public partial class MainForm : Form {
    1.54 +
    1.55 +    private Node root;
    1.56 +    private List<IGroup> groupList = new List<IGroup>();
    1.57 +    private TreeModel treeModel;
    1.58 +    private IDictionary<ISensor, Color> sensorPlotColors = 
    1.59 +      new Dictionary<ISensor, Color>();
    1.60 +    private Color[] plotColorPalette;
    1.61 +
    1.62 +    public MainForm() {
    1.63 +      
    1.64 +      InitializeComponent();
    1.65 +      this.Font = SystemFonts.MessageBoxFont;
    1.66 +      treeView.Font = SystemFonts.MessageBoxFont;
    1.67 +      plotPanel.Font = SystemFonts.MessageBoxFont;      
    1.68 +      
    1.69 +      nodeCheckBox.IsVisibleValueNeeded += 
    1.70 +        new EventHandler<NodeControlValueEventArgs>(
    1.71 +          nodeCheckBox_IsVisibleValueNeeded);
    1.72 +      nodeCheckBox.CheckStateChanged += 
    1.73 +        new EventHandler<TreePathEventArgs>(UpdatePlotSelection);
    1.74 +      nodeTextBoxText.DrawText += 
    1.75 +        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    1.76 +      nodeTextBoxValue.DrawText +=
    1.77 +        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    1.78 +      nodeTextBoxMin.DrawText +=
    1.79 +        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    1.80 +      nodeTextBoxMax.DrawText +=
    1.81 +        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    1.82 +      nodeTextBoxLimit.DrawText += 
    1.83 +        new EventHandler<DrawEventArgs>(nodeTextBoxLimit_DrawText);
    1.84 +
    1.85 +      if (Utilities.Config.Contains("mainForm.Location.X")) {
    1.86 +        int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
    1.87 +        x = x < 0 ? 0 : x;
    1.88 +        int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
    1.89 +        y = y < 0 ? 0 : y;
    1.90 +        this.Location = new Point(x, y);
    1.91 +      } else {
    1.92 +        StartPosition = FormStartPosition.CenterScreen;
    1.93 +      }
    1.94 +
    1.95 +      Width = Utilities.Config.Get("mainForm.Width", Width);
    1.96 +      Height = Utilities.Config.Get("mainForm.Height", Height);
    1.97 +         
    1.98 +      treeModel = new TreeModel();
    1.99 +      root = new Node(System.Environment.MachineName);
   1.100 +      root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
   1.101 +      
   1.102 +      treeModel.Nodes.Add(root);
   1.103 +      treeView.Model = treeModel;
   1.104 +
   1.105 +      AddGroup(new Hardware.SMBIOS.SMBIOSGroup());
   1.106 +      AddGroup(new Hardware.LPC.LPCGroup());
   1.107 +      AddGroup(new Hardware.CPU.CPUGroup());
   1.108 +      AddGroup(new Hardware.ATI.ATIGroup());
   1.109 +      AddGroup(new Hardware.Nvidia.NvidiaGroup());
   1.110 +      AddGroup(new Hardware.TBalancer.TBalancerGroup());
   1.111 +      
   1.112 +      plotColorPalette = new Color[14];
   1.113 +      plotColorPalette[0] = Color.Blue;
   1.114 +      plotColorPalette[1] = Color.OrangeRed;
   1.115 +      plotColorPalette[2] = Color.Green;
   1.116 +      plotColorPalette[3] = Color.LightSeaGreen;
   1.117 +      plotColorPalette[4] = Color.Goldenrod;
   1.118 +      plotColorPalette[5] = Color.DarkViolet;
   1.119 +      plotColorPalette[6] = Color.YellowGreen;
   1.120 +      plotColorPalette[7] = Color.SaddleBrown;
   1.121 +      plotColorPalette[8] = Color.Gray;
   1.122 +      plotColorPalette[9] = Color.RoyalBlue;
   1.123 +      plotColorPalette[10] = Color.DeepPink;
   1.124 +      plotColorPalette[11] = Color.MediumSeaGreen;
   1.125 +      plotColorPalette[12] = Color.Olive;
   1.126 +      plotColorPalette[13] = Color.Firebrick;
   1.127 +
   1.128 +      plotMenuItem.Checked = Utilities.Config.Get(plotMenuItem.Name, false);
   1.129 +      minMenuItem.Checked = Utilities.Config.Get(minMenuItem.Name, false);
   1.130 +      maxMenuItem.Checked = Utilities.Config.Get(maxMenuItem.Name, true);
   1.131 +      limitMenuItem.Checked = Utilities.Config.Get(limitMenuItem.Name, false);
   1.132 +      hddMenuItem.Checked = Utilities.Config.Get(hddMenuItem.Name, false);
   1.133 +
   1.134 +      voltMenuItem.Checked = Utilities.Config.Get(voltMenuItem.Name, true);
   1.135 +      clocksMenuItem.Checked = Utilities.Config.Get(clocksMenuItem.Name, true);
   1.136 +      tempMenuItem.Checked = Utilities.Config.Get(tempMenuItem.Name, true);
   1.137 +      fansMenuItem.Checked = Utilities.Config.Get(fansMenuItem.Name, true);
   1.138 +
   1.139 +      timer.Enabled = true;
   1.140 +    }
   1.141 +
   1.142 +    private void AddGroup(IGroup group) {
   1.143 +      groupList.Add(group);
   1.144 +      foreach (IHardware hardware in group.Hardware)
   1.145 +        root.Nodes.Add(new HardwareNode(hardware));
   1.146 +    }
   1.147 +
   1.148 +    private void RemoveGroup(IGroup group) {
   1.149 +      List<Node> nodesToRemove = new List<Node>();
   1.150 +      foreach (IHardware hardware in group.Hardware)
   1.151 +        foreach (Node node in root.Nodes) {
   1.152 +          HardwareNode hardwareNode = node as HardwareNode;
   1.153 +          if (hardwareNode != null && hardwareNode.Hardware == hardware)
   1.154 +            nodesToRemove.Add(node);
   1.155 +        }
   1.156 +      foreach (Node node in nodesToRemove)
   1.157 +        root.Nodes.Remove(node);
   1.158 +      groupList.Remove(group);
   1.159 +    }
   1.160 +
   1.161 +    private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
   1.162 +      SensorNode sensorNode = e.Node.Tag as SensorNode;
   1.163 +      if (sensorNode != null) 
   1.164 +        e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
   1.165 +    }
   1.166 +
   1.167 +    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
   1.168 +      if (!plotMenuItem.Checked)
   1.169 +        return;      
   1.170 +
   1.171 +      SensorNode sensorNode = e.Node.Tag as SensorNode;
   1.172 +      if (sensorNode != null) {
   1.173 +        Color color;
   1.174 +        if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color)) 
   1.175 +          e.TextColor = color;        
   1.176 +      }
   1.177 +    }
   1.178 +
   1.179 +    private void UpdatePlotSelection(object sender, 
   1.180 +      TreePathEventArgs e) 
   1.181 +    {
   1.182 +      List<ISensor> selected = new List<ISensor>();
   1.183 +      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
   1.184 +      int colorIndex = 0;
   1.185 +      foreach (TreeNodeAdv node in treeView.AllNodes) {
   1.186 +        SensorNode sensorNode = node.Tag as SensorNode;
   1.187 +        if (sensorNode != null && 
   1.188 +          sensorNode.Sensor.SensorType == SensorType.Temperature) {
   1.189 +          if (sensorNode.Plot) {
   1.190 +            colors.Add(sensorNode.Sensor,
   1.191 +              plotColorPalette[colorIndex % plotColorPalette.Length]);
   1.192 +            selected.Add(sensorNode.Sensor);
   1.193 +          }
   1.194 +          colorIndex++;
   1.195 +        }
   1.196 +      }
   1.197 +      sensorPlotColors = colors;
   1.198 +      plotPanel.SetSensors(selected, colors);
   1.199 +    }
   1.200 +
   1.201 +    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
   1.202 +      NodeControlValueEventArgs e) {
   1.203 +      SensorNode node = e.Node.Tag as SensorNode;
   1.204 +      e.Value = (node != null) && 
   1.205 +        (node.Sensor.SensorType == SensorType.Temperature) && 
   1.206 +        plotMenuItem.Checked;
   1.207 +    }
   1.208 +
   1.209 +    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
   1.210 +      Close();
   1.211 +    }
   1.212 +
   1.213 +    private void timer_Tick(object sender, EventArgs e) {
   1.214 +      
   1.215 +      #if !DEBUG
   1.216 +      try {
   1.217 +      #endif
   1.218 +        foreach (IGroup group in groupList)
   1.219 +          foreach (IHardware hardware in group.Hardware)
   1.220 +            hardware.Update();
   1.221 +      #if !DEBUG
   1.222 +      } catch (Exception exception) {
   1.223 +        Utilities.CrashReport.Save(exception);
   1.224 +        Close();
   1.225 +      }
   1.226 +      #endif
   1.227 +            
   1.228 +      treeView.Invalidate();
   1.229 +      plotPanel.Invalidate();
   1.230 +    }
   1.231 +
   1.232 +    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   1.233 +      Utilities.Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
   1.234 +      Utilities.Config.Set(minMenuItem.Name, minMenuItem.Checked);
   1.235 +      Utilities.Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
   1.236 +      Utilities.Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
   1.237 +      Utilities.Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
   1.238 +
   1.239 +      Utilities.Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
   1.240 +      Utilities.Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
   1.241 +      Utilities.Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
   1.242 +      Utilities.Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
   1.243 +
   1.244 +      Utilities.Config.Set("mainForm.Location.X", Location.X);
   1.245 +      Utilities.Config.Set("mainForm.Location.Y", Location.Y);
   1.246 +      Utilities.Config.Set("mainForm.Width", Width);
   1.247 +      Utilities.Config.Set("mainForm.Height", Height);
   1.248 +
   1.249 +      foreach (IGroup group in groupList)
   1.250 +        group.Close();
   1.251 +    }
   1.252 +
   1.253 +    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
   1.254 +      new AboutBox().ShowDialog();
   1.255 +    }
   1.256 +
   1.257 +    private void plotToolStripMenuItem_CheckedChanged(object sender, 
   1.258 +      EventArgs e) 
   1.259 +    {
   1.260 +      splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
   1.261 +      treeView.Invalidate();
   1.262 +    }
   1.263 +
   1.264 +    private void valueToolStripMenuItem_CheckedChanged(object sender, 
   1.265 +      EventArgs e) 
   1.266 +    {
   1.267 +      treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
   1.268 +    }
   1.269 +
   1.270 +    private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   1.271 +    {
   1.272 +      treeView.Columns[2].IsVisible = minMenuItem.Checked;
   1.273 +    }
   1.274 +
   1.275 +    private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   1.276 +    {
   1.277 +      treeView.Columns[3].IsVisible = maxMenuItem.Checked;
   1.278 +    }
   1.279 +
   1.280 +    private void limitToolStripMenuItem_CheckedChanged(object sender, 
   1.281 +      EventArgs e) {
   1.282 +      treeView.Columns[4].IsVisible = limitMenuItem.Checked;
   1.283 +    }
   1.284 +
   1.285 +    private void treeView_Click(object sender, EventArgs e) {
   1.286 +      
   1.287 +      MouseEventArgs m = e as MouseEventArgs;
   1.288 +      if (m == null || m.Button != MouseButtons.Right)
   1.289 +        return;
   1.290 +
   1.291 +      NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
   1.292 +      if (info.Control == null)
   1.293 +        columnsContextMenuStrip.Show(treeView, m.X, m.Y);
   1.294 +    }
   1.295 +
   1.296 +    private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
   1.297 +      ReportWriter.Save(groupList, new Version(Application.ProductVersion));
   1.298 +    }
   1.299 +
   1.300 +    private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
   1.301 +      EventArgs e) 
   1.302 +    {
   1.303 +      if (hddMenuItem.Checked) {
   1.304 +        AddGroup(new Hardware.HDD.HDDGroup());
   1.305 +        UpdateSensorTypeChecked(null, null);
   1.306 +      } else {
   1.307 +        List<IGroup> groupsToRemove = new List<IGroup>();
   1.308 +        foreach (IGroup group in groupList) 
   1.309 +          if (group is Hardware.HDD.HDDGroup)
   1.310 +            groupsToRemove.Add(group);
   1.311 +        foreach (IGroup group in groupsToRemove) {
   1.312 +          group.Close();
   1.313 +          RemoveGroup(group);
   1.314 +        }
   1.315 +        UpdatePlotSelection(null, null);        
   1.316 +      }
   1.317 +    }
   1.318 +
   1.319 +    private void UpdateSensorTypeChecked(object sender, EventArgs e) {
   1.320 +      foreach (HardwareNode node in root.Nodes) {
   1.321 +        node.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
   1.322 +        node.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
   1.323 +        node.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
   1.324 +        node.SetVisible(SensorType.Fan, fansMenuItem.Checked);
   1.325 +      }
   1.326 +    }  
   1.327 +  }
   1.328 +}