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