GUI/MainForm.cs
author moel.mich
Mon, 01 Mar 2010 22:12:29 +0000
changeset 67 bb5696abab23
parent 63 1a7c13ac7348
child 70 dc69dc4d7512
permissions -rw-r--r--
Added support for Winbond W83687THF and improved report output for LPC detection.
     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     private SensorSystemTray sensorSystemTray;
    60     private NotifyIcon notifyIcon;
    61 
    62     public MainForm() {      
    63       InitializeComponent();
    64       this.Font = SystemFonts.MessageBoxFont;
    65       treeView.Font = SystemFonts.MessageBoxFont;
    66       plotPanel.Font = SystemFonts.MessageBoxFont;
    67       
    68       nodeCheckBox.IsVisibleValueNeeded += 
    69         new EventHandler<NodeControlValueEventArgs>(
    70           nodeCheckBox_IsVisibleValueNeeded);
    71       nodeCheckBox.CheckStateChanged += 
    72         new EventHandler<TreePathEventArgs>(UpdatePlotSelection);
    73       nodeTextBoxText.DrawText += 
    74         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    75       nodeTextBoxValue.DrawText +=
    76         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    77       nodeTextBoxMin.DrawText +=
    78         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    79       nodeTextBoxMax.DrawText +=
    80         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    81       nodeTextBoxLimit.DrawText += 
    82         new EventHandler<DrawEventArgs>(nodeTextBoxLimit_DrawText);
    83 
    84       if (Utilities.Config.Contains("mainForm.Location.X")) {
    85         int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
    86         x = x < 0 ? 0 : x;
    87         int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
    88         y = y < 0 ? 0 : y;
    89         this.Location = new Point(x, y);
    90       } else {
    91         StartPosition = FormStartPosition.CenterScreen;
    92       }
    93 
    94       Width = Utilities.Config.Get("mainForm.Width", Width);
    95       Height = Utilities.Config.Get("mainForm.Height", Height);
    96          
    97       treeModel = new TreeModel();
    98       root = new Node(System.Environment.MachineName);
    99       root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
   100       
   101       treeModel.Nodes.Add(root);
   102       treeView.Model = treeModel;
   103       
   104       notifyIcon = new NotifyIcon();
   105       notifyIcon.ContextMenuStrip = this.notifyContextMenuStrip;
   106       notifyIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
   107       notifyIcon.Text = "Open Hardware Monitor";      
   108       notifyIcon.DoubleClick += new EventHandler(this.restoreClick);
   109 
   110       sensorSystemTray = new SensorSystemTray(computer);
   111 
   112       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
   113       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
   114       computer.Open();
   115 
   116       plotColorPalette = new Color[14];
   117       plotColorPalette[0] = Color.Blue;
   118       plotColorPalette[1] = Color.OrangeRed;
   119       plotColorPalette[2] = Color.Green;
   120       plotColorPalette[3] = Color.LightSeaGreen;
   121       plotColorPalette[4] = Color.Goldenrod;
   122       plotColorPalette[5] = Color.DarkViolet;
   123       plotColorPalette[6] = Color.YellowGreen;
   124       plotColorPalette[7] = Color.SaddleBrown;
   125       plotColorPalette[8] = Color.Gray;
   126       plotColorPalette[9] = Color.RoyalBlue;
   127       plotColorPalette[10] = Color.DeepPink;
   128       plotColorPalette[11] = Color.MediumSeaGreen;
   129       plotColorPalette[12] = Color.Olive;
   130       plotColorPalette[13] = Color.Firebrick;
   131 
   132       plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
   133       minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
   134       maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
   135       limitMenuItem.Checked = Config.Get(limitMenuItem.Name, false);
   136 
   137       startMinMenuItem.Checked = Config.Get(startMinMenuItem.Name, false); 
   138       minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
   139       hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
   140 
   141       voltMenuItem.Checked = Config.Get(voltMenuItem.Name, true);
   142       clocksMenuItem.Checked = Config.Get(clocksMenuItem.Name, true);
   143       loadMenuItem.Checked = Config.Get(loadMenuItem.Name, true);
   144       tempMenuItem.Checked = Config.Get(tempMenuItem.Name, true);
   145       fansMenuItem.Checked = Config.Get(fansMenuItem.Name, true);
   146       flowsMenuItem.Checked = Config.Get(flowsMenuItem.Name, true);
   147      
   148       timer.Enabled = true;
   149 
   150       if (startMinMenuItem.Checked) {
   151         if (minTrayMenuItem.Checked) {
   152           notifyIcon.Visible = true;
   153         } else {
   154           WindowState = FormWindowState.Minimized;
   155           Show();
   156         }
   157       } else {
   158         Show();
   159       }
   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       if (!plotMenuItem.Checked)
   195         return;      
   196 
   197       SensorNode sensorNode = e.Node.Tag as SensorNode;
   198       if (sensorNode != null) {
   199         Color color;
   200         if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color)) 
   201           e.TextColor = color;        
   202       }
   203     }
   204 
   205     private void UpdatePlotSelection(object sender, 
   206       TreePathEventArgs e) 
   207     {
   208       List<ISensor> selected = new List<ISensor>();
   209       IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
   210       int colorIndex = 0;
   211       foreach (TreeNodeAdv node in treeView.AllNodes) {
   212         SensorNode sensorNode = node.Tag as SensorNode;
   213         if (sensorNode != null && 
   214           sensorNode.Sensor.SensorType == SensorType.Temperature) {
   215           if (sensorNode.Plot) {
   216             colors.Add(sensorNode.Sensor,
   217               plotColorPalette[colorIndex % plotColorPalette.Length]);
   218             selected.Add(sensorNode.Sensor);
   219           }
   220           colorIndex++;
   221         }
   222       }
   223       sensorPlotColors = colors;
   224       plotPanel.SetSensors(selected, colors);
   225     }
   226 
   227     private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
   228       NodeControlValueEventArgs e) {
   229       SensorNode node = e.Node.Tag as SensorNode;
   230       e.Value = (node != null) && 
   231         (node.Sensor.SensorType == SensorType.Temperature) && 
   232         plotMenuItem.Checked;
   233     }
   234 
   235     private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
   236       Close();
   237     }
   238 
   239     private void timer_Tick(object sender, EventArgs e) {
   240       
   241       #if !DEBUG
   242       try {
   243       #endif
   244         computer.Update();        
   245       #if !DEBUG
   246       } catch (Exception exception) {
   247         CrashReport.Save(exception);
   248         Close();
   249       }
   250       #endif
   251             
   252       treeView.Invalidate();
   253       plotPanel.Invalidate();
   254       sensorSystemTray.Redraw();
   255     }
   256 
   257     private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   258             
   259       Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
   260       Config.Set(minMenuItem.Name, minMenuItem.Checked);
   261       Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
   262       Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
   263 
   264       Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked);
   265       Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
   266       Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
   267 
   268       Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
   269       Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
   270       Config.Set(loadMenuItem.Name, loadMenuItem.Checked);
   271       Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
   272       Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
   273       Config.Set(flowsMenuItem.Name, flowsMenuItem.Checked);
   274 
   275       if (WindowState != FormWindowState.Minimized) {
   276         Config.Set("mainForm.Location.X", Location.X);
   277         Config.Set("mainForm.Location.Y", Location.Y);
   278         Config.Set("mainForm.Width", Width);
   279         Config.Set("mainForm.Height", Height);
   280       }
   281            
   282       sensorSystemTray.Dispose();
   283       notifyIcon.Dispose();
   284       computer.Close();
   285     }
   286 
   287     private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
   288       new AboutBox().ShowDialog();
   289     }
   290 
   291     private void plotToolStripMenuItem_CheckedChanged(object sender, 
   292       EventArgs e) 
   293     {
   294       splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
   295       treeView.Invalidate();
   296     }
   297 
   298     private void valueToolStripMenuItem_CheckedChanged(object sender, 
   299       EventArgs e) 
   300     {
   301       treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
   302     }
   303 
   304     private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   305     {
   306       treeView.Columns[2].IsVisible = minMenuItem.Checked;
   307     }
   308 
   309     private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   310     {
   311       treeView.Columns[3].IsVisible = maxMenuItem.Checked;
   312     }
   313 
   314     private void limitToolStripMenuItem_CheckedChanged(object sender, 
   315       EventArgs e) {
   316       treeView.Columns[4].IsVisible = limitMenuItem.Checked;
   317     }
   318 
   319     private void treeView_Click(object sender, EventArgs e) {
   320       
   321       MouseEventArgs m = e as MouseEventArgs;
   322       if (m == null || m.Button != MouseButtons.Right)
   323         return;
   324 
   325       NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
   326       if (info.Control == null) {
   327         columnsContextMenuStrip.Show(treeView, m.X, m.Y);
   328       } else {
   329         SensorNode node = info.Node.Tag as SensorNode;
   330         if (node != null && node.Sensor != null) {
   331 
   332           sensorContextMenuStrip.Items.Clear();
   333           if (node.Sensor.Parameters.Length > 0) {
   334             ToolStripMenuItem item = new ToolStripMenuItem("Parameters...");
   335             item.Click += delegate(object obj, EventArgs args) {
   336               ShowParameterForm(node.Sensor);
   337             };
   338             sensorContextMenuStrip.Items.Add(item);
   339           }
   340           if (sensorSystemTray.Contains(node.Sensor)) {
   341             ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
   342             item.Click += delegate(object obj, EventArgs args) {
   343               sensorSystemTray.Remove(node.Sensor);
   344             };
   345             sensorContextMenuStrip.Items.Add(item);
   346           } else {
   347             ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
   348             item.Click += delegate(object obj, EventArgs args) {
   349               sensorSystemTray.Add(node.Sensor, true);
   350             };
   351             sensorContextMenuStrip.Items.Add(item);
   352           }
   353           sensorContextMenuStrip.Show(treeView, m.X, m.Y);
   354         }
   355       }
   356     }
   357 
   358     private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
   359       computer.SaveReport(new Version(Application.ProductVersion));      
   360     }
   361 
   362     private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
   363       EventArgs e) 
   364     {
   365       computer.HDDEnabled = hddMenuItem.Checked;
   366       UpdateSensorTypeChecked(null, null);
   367       UpdatePlotSelection(null, null);      
   368     }
   369 
   370     private void UpdateSensorTypeVisible(Node node) {
   371       HardwareNode hardwareNode = node as HardwareNode;
   372       if (hardwareNode == null)
   373         return;
   374 
   375       hardwareNode.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
   376       hardwareNode.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
   377       hardwareNode.SetVisible(SensorType.Load, loadMenuItem.Checked);
   378       hardwareNode.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
   379       hardwareNode.SetVisible(SensorType.Fan, fansMenuItem.Checked);
   380       hardwareNode.SetVisible(SensorType.Flow, flowsMenuItem.Checked);
   381 
   382       foreach (Node n in node.Nodes)
   383         UpdateSensorTypeVisible(n);
   384     }
   385 
   386     private void UpdateSensorTypeChecked(object sender, EventArgs e) {          
   387       foreach (HardwareNode node in root.Nodes) 
   388         UpdateSensorTypeVisible(node);
   389     }
   390 
   391     private void ToggleSysTray() {
   392       if (notifyIcon.Visible) {
   393         Visible = true;
   394         notifyIcon.Visible = false;
   395         Activate(); 
   396       } else {
   397         notifyIcon.Visible = true;
   398         Visible = false;           
   399       }
   400     }
   401 
   402     protected override void WndProc(ref Message m) {
   403       const int WM_SYSCOMMAND = 0x112;
   404       const int SC_MINIMIZE = 0xF020;
   405       if (minTrayMenuItem.Checked && 
   406         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
   407         ToggleSysTray();
   408       } else {      
   409         base.WndProc(ref m);
   410       }
   411     }
   412 
   413     private void restoreClick(object sender, EventArgs e) {
   414       ToggleSysTray();
   415     }
   416 
   417     private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
   418       ToolStripMenuItem item = sender as ToolStripMenuItem;
   419       if (item == null)
   420         return;
   421 
   422       ISensor sensor = item.Owner.Tag as ISensor;
   423       if (sensor == null)
   424         return;
   425 
   426       sensorSystemTray.Remove(sensor);
   427     }
   428 
   429     private void ShowParameterForm(ISensor sensor) {
   430       ParameterForm form = new ParameterForm();
   431       form.Parameters = sensor.Parameters;
   432       form.captionLabel.Text = sensor.Name;
   433       form.ShowDialog();
   434     }
   435 
   436     private void treeView_NodeMouseDoubleClick(object sender, 
   437       TreeNodeAdvMouseEventArgs e) {
   438       SensorNode node = e.Node.Tag as SensorNode;
   439       if (node != null && node.Sensor != null && 
   440         node.Sensor.Parameters.Length > 0) {
   441         ShowParameterForm(node.Sensor);
   442       }
   443     }
   444   }
   445 }