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