GUI/MainForm.cs
author moel.mich
Fri, 12 Feb 2010 08:17:51 +0000
changeset 41 d92bacc2116e
parent 34 dc276daadb2c
child 42 47385d4fc990
permissions -rw-r--r--
Changed the T-Balancer detection and added more report details.
     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       minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
   138       hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
   139 
   140       voltMenuItem.Checked = Config.Get(voltMenuItem.Name, true);
   141       clocksMenuItem.Checked = Config.Get(clocksMenuItem.Name, true);
   142       loadMenuItem.Checked = Config.Get(loadMenuItem.Name, true);
   143       tempMenuItem.Checked = Config.Get(tempMenuItem.Name, true);
   144       fansMenuItem.Checked = Config.Get(fansMenuItem.Name, true);
   145      
   146       timer.Enabled = true;   
   147     }
   148 
   149     private void HardwareAdded(IHardware hardware) {
   150       root.Nodes.Add(new HardwareNode(hardware));
   151     }
   152 
   153     private void HardwareRemoved(IHardware hardware) {      
   154       List<Node> nodesToRemove = new List<Node>();
   155       foreach (Node node in root.Nodes) {
   156         HardwareNode hardwareNode = node as HardwareNode;
   157         if (hardwareNode != null && hardwareNode.Hardware == hardware)
   158           nodesToRemove.Add(node);
   159       }
   160       foreach (Node node in nodesToRemove)
   161         root.Nodes.Remove(node);
   162     }
   163 
   164     private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
   165       SensorNode sensorNode = e.Node.Tag as SensorNode;
   166       if (sensorNode != null) 
   167         e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
   168     }
   169 
   170     private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
   171       if (!plotMenuItem.Checked)
   172         return;      
   173 
   174       SensorNode sensorNode = e.Node.Tag as SensorNode;
   175       if (sensorNode != null) {
   176         Color color;
   177         if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color)) 
   178           e.TextColor = color;        
   179       }
   180     }
   181 
   182     private void UpdatePlotSelection(object sender, 
   183       TreePathEventArgs e) 
   184     {
   185       List<ISensor> selected = new List<ISensor>();
   186       IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
   187       int colorIndex = 0;
   188       foreach (TreeNodeAdv node in treeView.AllNodes) {
   189         SensorNode sensorNode = node.Tag as SensorNode;
   190         if (sensorNode != null && 
   191           sensorNode.Sensor.SensorType == SensorType.Temperature) {
   192           if (sensorNode.Plot) {
   193             colors.Add(sensorNode.Sensor,
   194               plotColorPalette[colorIndex % plotColorPalette.Length]);
   195             selected.Add(sensorNode.Sensor);
   196           }
   197           colorIndex++;
   198         }
   199       }
   200       sensorPlotColors = colors;
   201       plotPanel.SetSensors(selected, colors);
   202     }
   203 
   204     private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
   205       NodeControlValueEventArgs e) {
   206       SensorNode node = e.Node.Tag as SensorNode;
   207       e.Value = (node != null) && 
   208         (node.Sensor.SensorType == SensorType.Temperature) && 
   209         plotMenuItem.Checked;
   210     }
   211 
   212     private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
   213       Close();
   214     }
   215 
   216     private void timer_Tick(object sender, EventArgs e) {
   217       
   218       #if !DEBUG
   219       try {
   220       #endif
   221         computer.Update();        
   222       #if !DEBUG
   223       } catch (Exception exception) {
   224         CrashReport.Save(exception);
   225         Close();
   226       }
   227       #endif
   228             
   229       treeView.Invalidate();
   230       plotPanel.Invalidate();
   231       sensorSystemTray.Redraw();
   232     }
   233 
   234     private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   235             
   236       Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
   237       Config.Set(minMenuItem.Name, minMenuItem.Checked);
   238       Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
   239       Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
   240 
   241       Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
   242       Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
   243 
   244       Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
   245       Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
   246       Config.Set(loadMenuItem.Name, loadMenuItem.Checked);
   247       Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
   248       Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
   249 
   250       if (WindowState != FormWindowState.Minimized) {
   251         Config.Set("mainForm.Location.X", Location.X);
   252         Config.Set("mainForm.Location.Y", Location.Y);
   253         Config.Set("mainForm.Width", Width);
   254         Config.Set("mainForm.Height", Height);
   255       }
   256       
   257       sensorSystemTray.Dispose();
   258       notifyIcon.Dispose();
   259 
   260       computer.Close();
   261     }
   262 
   263     private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
   264       new AboutBox().ShowDialog();
   265     }
   266 
   267     private void plotToolStripMenuItem_CheckedChanged(object sender, 
   268       EventArgs e) 
   269     {
   270       splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
   271       treeView.Invalidate();
   272     }
   273 
   274     private void valueToolStripMenuItem_CheckedChanged(object sender, 
   275       EventArgs e) 
   276     {
   277       treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
   278     }
   279 
   280     private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   281     {
   282       treeView.Columns[2].IsVisible = minMenuItem.Checked;
   283     }
   284 
   285     private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
   286     {
   287       treeView.Columns[3].IsVisible = maxMenuItem.Checked;
   288     }
   289 
   290     private void limitToolStripMenuItem_CheckedChanged(object sender, 
   291       EventArgs e) {
   292       treeView.Columns[4].IsVisible = limitMenuItem.Checked;
   293     }
   294 
   295     private void treeView_Click(object sender, EventArgs e) {
   296       
   297       MouseEventArgs m = e as MouseEventArgs;
   298       if (m == null || m.Button != MouseButtons.Right)
   299         return;
   300 
   301       NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
   302       if (info.Control == null) {
   303         columnsContextMenuStrip.Show(treeView, m.X, m.Y);
   304       } else {
   305         SensorNode node = info.Node.Tag as SensorNode;
   306         if (node != null && node.Sensor != null) {
   307 
   308           sensorContextMenuStrip.Items.Clear();
   309           if (sensorSystemTray.Contains(node.Sensor)) {
   310             ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
   311             item.Click += delegate(object obj, EventArgs args) {
   312               sensorSystemTray.Remove(node.Sensor);
   313             };
   314             sensorContextMenuStrip.Items.Add(item);
   315           } else {
   316             ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
   317             item.Click += delegate(object obj, EventArgs args) {
   318               sensorSystemTray.Add(node.Sensor);
   319             };
   320             sensorContextMenuStrip.Items.Add(item);
   321           }
   322           sensorContextMenuStrip.Show(treeView, m.X, m.Y);
   323         }
   324       }
   325     }
   326 
   327     private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
   328       computer.SaveReport(new Version(Application.ProductVersion));      
   329     }
   330 
   331     private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
   332       EventArgs e) 
   333     {
   334       computer.HDDEnabled = hddMenuItem.Checked;
   335       UpdateSensorTypeChecked(null, null);
   336       UpdatePlotSelection(null, null);      
   337     }
   338 
   339     private void UpdateSensorTypeChecked(object sender, EventArgs e) {
   340       foreach (HardwareNode node in root.Nodes) {
   341         node.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
   342         node.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
   343         node.SetVisible(SensorType.Load, loadMenuItem.Checked);
   344         node.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
   345         node.SetVisible(SensorType.Fan, fansMenuItem.Checked);
   346       }
   347     }
   348 
   349     private void ToggleSysTray() {
   350       if (Visible) {
   351         notifyIcon.Visible = true;
   352         Visible = false;        
   353       } else {
   354         Visible = true;
   355         notifyIcon.Visible = false;
   356         Activate();
   357       }
   358     }
   359 
   360     protected override void WndProc(ref Message m) {
   361       const int WM_SYSCOMMAND = 0x112;
   362       const int SC_MINIMIZE = 0xF020;
   363       if (minTrayMenuItem.Checked && 
   364         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
   365         ToggleSysTray();
   366       } else {      
   367         base.WndProc(ref m);
   368       }
   369     }
   370 
   371     private void restoreClick(object sender, EventArgs e) {
   372       ToggleSysTray();
   373     }
   374 
   375     private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
   376       ToolStripMenuItem item = sender as ToolStripMenuItem;
   377       if (item == null)
   378         return;
   379 
   380       ISensor sensor = item.Owner.Tag as ISensor;
   381       if (sensor == null)
   382         return;
   383 
   384       sensorSystemTray.Remove(sensor);
   385     }
   386 
   387   }
   388 }