GUI/MainForm.cs
author moel.mich
Sun, 07 Feb 2010 20:11:30 +0000
changeset 32 a7f5e400f0da
parent 27 beed5a9e1b78
child 34 dc276daadb2c
permissions -rw-r--r--
Release version 0.1.14. Added more report details for T-Balancer bigNG.
     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 using OpenHardwareMonitor.Utilities;
    49 
    50 namespace OpenHardwareMonitor.GUI {
    51   public partial class MainForm : Form {
    52 
    53     private Computer computer = new Computer();
    54     private Node root;
    55     private TreeModel treeModel;
    56     private IDictionary<ISensor, Color> sensorPlotColors = 
    57       new Dictionary<ISensor, Color>();
    58     private Color[] plotColorPalette;
    59 
    60     public MainForm() {      
    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       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
   103       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
   104       computer.Open();
   105 
   106       plotColorPalette = new Color[14];
   107       plotColorPalette[0] = Color.Blue;
   108       plotColorPalette[1] = Color.OrangeRed;
   109       plotColorPalette[2] = Color.Green;
   110       plotColorPalette[3] = Color.LightSeaGreen;
   111       plotColorPalette[4] = Color.Goldenrod;
   112       plotColorPalette[5] = Color.DarkViolet;
   113       plotColorPalette[6] = Color.YellowGreen;
   114       plotColorPalette[7] = Color.SaddleBrown;
   115       plotColorPalette[8] = Color.Gray;
   116       plotColorPalette[9] = Color.RoyalBlue;
   117       plotColorPalette[10] = Color.DeepPink;
   118       plotColorPalette[11] = Color.MediumSeaGreen;
   119       plotColorPalette[12] = Color.Olive;
   120       plotColorPalette[13] = Color.Firebrick;
   121 
   122       plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
   123       minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
   124       maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
   125       limitMenuItem.Checked = Config.Get(limitMenuItem.Name, false);
   126 
   127       minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
   128       hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
   129 
   130       voltMenuItem.Checked = Config.Get(voltMenuItem.Name, true);
   131       clocksMenuItem.Checked = Config.Get(clocksMenuItem.Name, true);
   132       loadMenuItem.Checked = Config.Get(loadMenuItem.Name, true);
   133       tempMenuItem.Checked = Config.Get(tempMenuItem.Name, true);
   134       fansMenuItem.Checked = Config.Get(fansMenuItem.Name, true);
   135      
   136       timer.Enabled = true;   
   137     }
   138 
   139     private void HardwareAdded(IHardware hardware) {
   140       root.Nodes.Add(new HardwareNode(hardware));
   141     }
   142 
   143     private void HardwareRemoved(IHardware hardware) {      
   144       List<Node> nodesToRemove = new List<Node>();
   145       foreach (Node node in root.Nodes) {
   146         HardwareNode hardwareNode = node as HardwareNode;
   147         if (hardwareNode != null && hardwareNode.Hardware == hardware)
   148           nodesToRemove.Add(node);
   149       }
   150       foreach (Node node in nodesToRemove)
   151         root.Nodes.Remove(node);
   152     }
   153 
   154     private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
   155       SensorNode sensorNode = e.Node.Tag as SensorNode;
   156       if (sensorNode != null) 
   157         e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
   158     }
   159 
   160     private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
   161       if (!plotMenuItem.Checked)
   162         return;      
   163 
   164       SensorNode sensorNode = e.Node.Tag as SensorNode;
   165       if (sensorNode != null) {
   166         Color color;
   167         if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color)) 
   168           e.TextColor = color;        
   169       }
   170     }
   171 
   172     private void UpdatePlotSelection(object sender, 
   173       TreePathEventArgs e) 
   174     {
   175       List<ISensor> selected = new List<ISensor>();
   176       IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
   177       int colorIndex = 0;
   178       foreach (TreeNodeAdv node in treeView.AllNodes) {
   179         SensorNode sensorNode = node.Tag as SensorNode;
   180         if (sensorNode != null && 
   181           sensorNode.Sensor.SensorType == SensorType.Temperature) {
   182           if (sensorNode.Plot) {
   183             colors.Add(sensorNode.Sensor,
   184               plotColorPalette[colorIndex % plotColorPalette.Length]);
   185             selected.Add(sensorNode.Sensor);
   186           }
   187           colorIndex++;
   188         }
   189       }
   190       sensorPlotColors = colors;
   191       plotPanel.SetSensors(selected, colors);
   192     }
   193 
   194     private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
   195       NodeControlValueEventArgs e) {
   196       SensorNode node = e.Node.Tag as SensorNode;
   197       e.Value = (node != null) && 
   198         (node.Sensor.SensorType == SensorType.Temperature) && 
   199         plotMenuItem.Checked;
   200     }
   201 
   202     private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
   203       Close();
   204     }
   205 
   206     private void timer_Tick(object sender, EventArgs e) {
   207       
   208       #if !DEBUG
   209       try {
   210       #endif
   211         computer.Update();        
   212       #if !DEBUG
   213       } catch (Exception exception) {
   214         CrashReport.Save(exception);
   215         Close();
   216       }
   217       #endif
   218             
   219       treeView.Invalidate();
   220       plotPanel.Invalidate();
   221     }
   222 
   223     private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   224             
   225       Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
   226       Config.Set(minMenuItem.Name, minMenuItem.Checked);
   227       Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
   228       Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
   229 
   230       Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
   231       Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
   232 
   233       Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
   234       Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
   235       Config.Set(loadMenuItem.Name, loadMenuItem.Checked);
   236       Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
   237       Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
   238 
   239       if (WindowState != FormWindowState.Minimized) {
   240         Config.Set("mainForm.Location.X", Location.X);
   241         Config.Set("mainForm.Location.Y", Location.Y);
   242         Config.Set("mainForm.Width", Width);
   243         Config.Set("mainForm.Height", Height);
   244       }
   245 
   246       computer.Close();
   247     }
   248 
   249     private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
   250       new AboutBox().ShowDialog();
   251     }
   252 
   253     private void plotToolStripMenuItem_CheckedChanged(object sender, 
   254       EventArgs e) 
   255     {
   256       splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
   257       treeView.Invalidate();
   258     }
   259 
   260     private void valueToolStripMenuItem_CheckedChanged(object sender, 
   261       EventArgs e) 
   262     {
   263       treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
   264     }
   265 
   266     private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   267     {
   268       treeView.Columns[2].IsVisible = minMenuItem.Checked;
   269     }
   270 
   271     private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   272     {
   273       treeView.Columns[3].IsVisible = maxMenuItem.Checked;
   274     }
   275 
   276     private void limitToolStripMenuItem_CheckedChanged(object sender, 
   277       EventArgs e) {
   278       treeView.Columns[4].IsVisible = limitMenuItem.Checked;
   279     }
   280 
   281     private void treeView_Click(object sender, EventArgs e) {
   282       
   283       MouseEventArgs m = e as MouseEventArgs;
   284       if (m == null || m.Button != MouseButtons.Right)
   285         return;
   286 
   287       NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
   288       if (info.Control == null) {
   289         columnsContextMenuStrip.Show(treeView, m.X, m.Y);
   290       } 
   291     }
   292 
   293     private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
   294       computer.SaveReport(new Version(Application.ProductVersion));      
   295     }
   296 
   297     private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
   298       EventArgs e) 
   299     {
   300       computer.HDDEnabled = hddMenuItem.Checked;
   301       UpdateSensorTypeChecked(null, null);
   302       UpdatePlotSelection(null, null);      
   303     }
   304 
   305     private void UpdateSensorTypeChecked(object sender, EventArgs e) {
   306       foreach (HardwareNode node in root.Nodes) {
   307         node.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
   308         node.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
   309         node.SetVisible(SensorType.Load, loadMenuItem.Checked);
   310         node.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
   311         node.SetVisible(SensorType.Fan, fansMenuItem.Checked);
   312       }
   313     }
   314 
   315     private void ToggleSysTray() {
   316       if (Visible) {
   317         notifyIcon.Visible = true;
   318         Visible = false;        
   319       } else {
   320         Visible = true;
   321         notifyIcon.Visible = false;
   322         BringToFront();
   323       }
   324     }
   325 
   326     protected override void WndProc(ref Message m) {
   327       const int WM_SYSCOMMAND = 0x112;
   328       const int SC_MINIMIZE = 0xF020;
   329       if (minTrayMenuItem.Checked && 
   330         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
   331         ToggleSysTray();
   332       } else {      
   333         base.WndProc(ref m);
   334       }
   335     }
   336 
   337     private void restoreClick(object sender, EventArgs e) {
   338       ToggleSysTray();
   339     }
   340 
   341   }
   342 }