GUI/MainForm.cs
author moel.mich
Sat, 27 Mar 2010 12:57:09 +0000
changeset 83 3fdadd4a830f
parent 82 91edecb084a1
child 86 b4f0f206173d
permissions -rw-r--r--
Added a dialog for the report filename. Added additional checks to T-Balancer code for cases where the port gets closed. Moved the timer to the Computer class.
     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 
    64     public MainForm() {      
    65       InitializeComponent();
    66       this.Font = SystemFonts.MessageBoxFont;
    67       treeView.Font = SystemFonts.MessageBoxFont;
    68       plotPanel.Font = SystemFonts.MessageBoxFont;
    69       
    70       nodeCheckBox.IsVisibleValueNeeded += 
    71         new EventHandler<NodeControlValueEventArgs>(
    72           nodeCheckBox_IsVisibleValueNeeded);
    73       nodeCheckBox.CheckStateChanged += 
    74         new EventHandler<TreePathEventArgs>(UpdatePlotSelection);
    75       nodeTextBoxText.DrawText += 
    76         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    77       nodeTextBoxValue.DrawText +=
    78         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    79       nodeTextBoxMin.DrawText +=
    80         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    81       nodeTextBoxMax.DrawText +=
    82         new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
    83       nodeTextBoxLimit.DrawText += 
    84         new EventHandler<DrawEventArgs>(nodeTextBoxLimit_DrawText);
    85 
    86       if (Utilities.Config.Contains("mainForm.Location.X")) {
    87         int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
    88         x = x < 0 ? 0 : x;
    89         int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
    90         y = y < 0 ? 0 : y;
    91         this.Location = new Point(x, y);
    92       } else {
    93         StartPosition = FormStartPosition.CenterScreen;
    94       }
    95 
    96       Width = Utilities.Config.Get("mainForm.Width", Width);
    97       Height = Utilities.Config.Get("mainForm.Height", Height);
    98          
    99       treeModel = new TreeModel();
   100       root = new Node(System.Environment.MachineName);
   101       root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
   102       
   103       treeModel.Nodes.Add(root);
   104       treeView.Model = treeModel;
   105       
   106       notifyIcon = new NotifyIcon();
   107       notifyIcon.ContextMenuStrip = this.notifyContextMenuStrip;
   108       notifyIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
   109       notifyIcon.Text = "Open Hardware Monitor";      
   110       notifyIcon.DoubleClick += new EventHandler(this.hideShowClick);
   111 
   112       sensorSystemTray = new SensorSystemTray(computer);
   113 
   114       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
   115       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
   116       computer.Updated += new UpdateEventHandler(Updated);
   117       computer.Open();
   118 
   119       plotColorPalette = new Color[14];
   120       plotColorPalette[0] = Color.Blue;
   121       plotColorPalette[1] = Color.OrangeRed;
   122       plotColorPalette[2] = Color.Green;
   123       plotColorPalette[3] = Color.LightSeaGreen;
   124       plotColorPalette[4] = Color.Goldenrod;
   125       plotColorPalette[5] = Color.DarkViolet;
   126       plotColorPalette[6] = Color.YellowGreen;
   127       plotColorPalette[7] = Color.SaddleBrown;
   128       plotColorPalette[8] = Color.Gray;
   129       plotColorPalette[9] = Color.RoyalBlue;
   130       plotColorPalette[10] = Color.DeepPink;
   131       plotColorPalette[11] = Color.MediumSeaGreen;
   132       plotColorPalette[12] = Color.Olive;
   133       plotColorPalette[13] = Color.Firebrick;
   134 
   135       plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
   136       minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
   137       maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
   138       limitMenuItem.Checked = Config.Get(limitMenuItem.Name, false);
   139 
   140       startMinMenuItem.Checked = Config.Get(startMinMenuItem.Name, false); 
   141       minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
   142       startupMenuItem.Checked = startupManager.Startup;
   143       hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
   144 
   145       voltMenuItem.Checked = Config.Get(voltMenuItem.Name, true);
   146       clocksMenuItem.Checked = Config.Get(clocksMenuItem.Name, true);
   147       loadMenuItem.Checked = Config.Get(loadMenuItem.Name, true);
   148       tempMenuItem.Checked = Config.Get(tempMenuItem.Name, true);
   149       fansMenuItem.Checked = Config.Get(fansMenuItem.Name, true);
   150       flowsMenuItem.Checked = Config.Get(flowsMenuItem.Name, true);    
   151 
   152       if (startMinMenuItem.Checked) {
   153         if (!minTrayMenuItem.Checked) {
   154           WindowState = FormWindowState.Minimized;
   155           Show();
   156         }
   157       } else {
   158         Show();
   159       }
   160 
   161       // Create a handle, otherwise calling Close() does not fire FormClosed     
   162       IntPtr handle = Handle;
   163     }
   164 
   165     private void SubHardwareAdded(IHardware hardware, Node node) {
   166       Node hardwareNode = new HardwareNode(hardware);
   167       node.Nodes.Add(hardwareNode);
   168       foreach (IHardware subHardware in hardware.SubHardware)
   169         SubHardwareAdded(subHardware, hardwareNode);  
   170     }
   171 
   172     private void HardwareAdded(IHardware hardware) {
   173       Node hardwareNode = new HardwareNode(hardware);
   174       root.Nodes.Add(hardwareNode);
   175       foreach (IHardware subHardware in hardware.SubHardware)
   176         SubHardwareAdded(subHardware, hardwareNode);     
   177     }
   178 
   179     private void HardwareRemoved(IHardware hardware) {      
   180       List<Node> nodesToRemove = new List<Node>();
   181       foreach (Node node in root.Nodes) {
   182         HardwareNode hardwareNode = node as HardwareNode;
   183         if (hardwareNode != null && hardwareNode.Hardware == hardware)
   184           nodesToRemove.Add(node);
   185       }
   186       foreach (Node node in nodesToRemove)
   187         root.Nodes.Remove(node);
   188     }
   189 
   190     private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
   191       SensorNode sensorNode = e.Node.Tag as SensorNode;
   192       if (sensorNode != null) 
   193         e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
   194     }
   195 
   196     private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
   197       if (!plotMenuItem.Checked)
   198         return;      
   199 
   200       SensorNode sensorNode = e.Node.Tag as SensorNode;
   201       if (sensorNode != null) {
   202         Color color;
   203         if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color)) 
   204           e.TextColor = color;        
   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 Updated() {          
   243       treeView.Invalidate();
   244       plotPanel.Invalidate();
   245       sensorSystemTray.Redraw();
   246     }
   247 
   248     private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   249             
   250       Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
   251       Config.Set(minMenuItem.Name, minMenuItem.Checked);
   252       Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
   253       Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
   254 
   255       Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked);
   256       Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
   257       Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
   258 
   259       Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
   260       Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
   261       Config.Set(loadMenuItem.Name, loadMenuItem.Checked);
   262       Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
   263       Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
   264       Config.Set(flowsMenuItem.Name, flowsMenuItem.Checked);      
   265 
   266       if (WindowState != FormWindowState.Minimized) {
   267         Config.Set("mainForm.Location.X", Location.X);
   268         Config.Set("mainForm.Location.Y", Location.Y);
   269         Config.Set("mainForm.Width", Width);
   270         Config.Set("mainForm.Height", Height);
   271       }
   272            
   273       sensorSystemTray.Dispose();
   274       notifyIcon.Dispose();
   275       computer.Close();
   276     }
   277 
   278     private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
   279       new AboutBox().ShowDialog();
   280     }
   281 
   282     private void plotToolStripMenuItem_CheckedChanged(object sender, 
   283       EventArgs e) 
   284     {
   285       splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
   286       treeView.Invalidate();
   287     }
   288 
   289     private void valueToolStripMenuItem_CheckedChanged(object sender, 
   290       EventArgs e) 
   291     {
   292       treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
   293     }
   294 
   295     private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   296     {
   297       treeView.Columns[2].IsVisible = minMenuItem.Checked;
   298     }
   299 
   300     private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   301     {
   302       treeView.Columns[3].IsVisible = maxMenuItem.Checked;
   303     }
   304 
   305     private void limitToolStripMenuItem_CheckedChanged(object sender, 
   306       EventArgs e) {
   307       treeView.Columns[4].IsVisible = limitMenuItem.Checked;
   308     }
   309 
   310     private void treeView_Click(object sender, EventArgs e) {
   311       
   312       MouseEventArgs m = e as MouseEventArgs;
   313       if (m == null || m.Button != MouseButtons.Right)
   314         return;
   315 
   316       NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
   317       if (info.Control == null) {
   318         columnsContextMenuStrip.Show(treeView, m.X, m.Y);
   319       } else {
   320         SensorNode node = info.Node.Tag as SensorNode;
   321         if (node != null && node.Sensor != null) {
   322 
   323           sensorContextMenuStrip.Items.Clear();
   324           if (node.Sensor.Parameters.Length > 0) {
   325             ToolStripMenuItem item = new ToolStripMenuItem("Parameters...");
   326             item.Click += delegate(object obj, EventArgs args) {
   327               ShowParameterForm(node.Sensor);
   328             };
   329             sensorContextMenuStrip.Items.Add(item);
   330           }
   331           if (sensorSystemTray.Contains(node.Sensor)) {
   332             ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
   333             item.Click += delegate(object obj, EventArgs args) {
   334               sensorSystemTray.Remove(node.Sensor);
   335             };
   336             sensorContextMenuStrip.Items.Add(item);
   337           } else {
   338             ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
   339             item.Click += delegate(object obj, EventArgs args) {
   340               sensorSystemTray.Add(node.Sensor, true);
   341             };
   342             sensorContextMenuStrip.Items.Add(item);
   343           }
   344           sensorContextMenuStrip.Show(treeView, m.X, m.Y);
   345         }
   346       }
   347     }
   348 
   349     private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
   350       string report = computer.GetReport();
   351       if (saveFileDialog.ShowDialog() == DialogResult.OK) {
   352         using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
   353           w.Write(report);
   354         }
   355       }
   356     }
   357 
   358     private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
   359       EventArgs e) 
   360     {
   361       computer.HDDEnabled = hddMenuItem.Checked;
   362       UpdateSensorTypeChecked(null, null);
   363       UpdatePlotSelection(null, null);      
   364     }
   365 
   366     private void UpdateSensorTypeVisible(Node node) {
   367       HardwareNode hardwareNode = node as HardwareNode;
   368       if (hardwareNode == null)
   369         return;
   370 
   371       hardwareNode.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
   372       hardwareNode.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
   373       hardwareNode.SetVisible(SensorType.Load, loadMenuItem.Checked);
   374       hardwareNode.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
   375       hardwareNode.SetVisible(SensorType.Fan, fansMenuItem.Checked);
   376       hardwareNode.SetVisible(SensorType.Flow, flowsMenuItem.Checked);
   377 
   378       foreach (Node n in node.Nodes)
   379         UpdateSensorTypeVisible(n);
   380     }
   381 
   382     private void UpdateSensorTypeChecked(object sender, EventArgs e) {          
   383       foreach (HardwareNode node in root.Nodes) 
   384         UpdateSensorTypeVisible(node);
   385     }
   386 
   387     private void SysTrayHideShow() {
   388       Visible = !Visible;
   389       if (Visible)
   390         Activate();    
   391     }
   392 
   393     protected override void WndProc(ref Message m) {
   394       const int WM_SYSCOMMAND = 0x112;
   395       const int SC_MINIMIZE = 0xF020;
   396       if (minTrayMenuItem.Checked && 
   397         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
   398         SysTrayHideShow();
   399       } else {      
   400         base.WndProc(ref m);
   401       }
   402     }
   403 
   404     private void hideShowClick(object sender, EventArgs e) {
   405       SysTrayHideShow();
   406     }
   407 
   408     private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
   409       ToolStripMenuItem item = sender as ToolStripMenuItem;
   410       if (item == null)
   411         return;
   412 
   413       ISensor sensor = item.Owner.Tag as ISensor;
   414       if (sensor == null)
   415         return;
   416 
   417       sensorSystemTray.Remove(sensor);
   418     }
   419 
   420     private void ShowParameterForm(ISensor sensor) {
   421       ParameterForm form = new ParameterForm();
   422       form.Parameters = sensor.Parameters;
   423       form.captionLabel.Text = sensor.Name;
   424       form.ShowDialog();
   425     }
   426 
   427     private void treeView_NodeMouseDoubleClick(object sender, 
   428       TreeNodeAdvMouseEventArgs e) {
   429       SensorNode node = e.Node.Tag as SensorNode;
   430       if (node != null && node.Sensor != null && 
   431         node.Sensor.Parameters.Length > 0) {
   432         ShowParameterForm(node.Sensor);
   433       }
   434     }
   435 
   436     private void runOnWindowsStartupToolStripMenuItem_CheckedChanged(
   437       object sender, EventArgs e) 
   438     {
   439       startupManager.Startup = startupMenuItem.Checked;
   440     }
   441 
   442     private void minTrayMenuItem_CheckedChanged(object sender, EventArgs e) {
   443       notifyIcon.Visible = minTrayMenuItem.Checked;
   444     }
   445   }
   446 }