GUI/MainForm.cs
author moel.mich
Sun, 06 Jun 2010 11:07:57 +0000
changeset 134 8b3b9b2e28e5
parent 133 9ad699538c89
child 136 fa2957aa0699
permissions -rw-r--r--
Fixed Issue 69.
     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 SystemTray systemTray;    
    61     private StartupManager startupManager = new StartupManager();
    62     private UpdateVisitor updateVisitor = new UpdateVisitor();
    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 += nodeCheckBox_IsVisibleValueNeeded;
    71       nodeCheckBox.CheckStateChanged += UpdatePlotSelection;
    72       nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
    73       nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
    74       nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
    75       nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
    76 
    77       if (Utilities.Config.Contains("mainForm.Location.X")) {
    78         int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
    79         x = x < 0 ? 0 : x;
    80         int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
    81         y = y < 0 ? 0 : y;
    82         this.Location = new Point(x, y);
    83       } else {
    84         StartPosition = FormStartPosition.CenterScreen;
    85       }
    86 
    87       Width = Utilities.Config.Get("mainForm.Width", Width);
    88       Height = Utilities.Config.Get("mainForm.Height", Height);
    89 
    90       foreach (TreeColumn column in treeView.Columns) 
    91         column.Width = Math.Max(20, Math.Min(400, 
    92           Config.Get("treeView.Columns." + column.Header + ".Width",
    93           column.Width)));
    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       systemTray = new SystemTray(computer);
   103       systemTray.HideShowCommand += hideShowClick;
   104       systemTray.ExitCommand += exitClick;
   105 
   106       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
   107       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
   108       computer.Open();
   109 
   110       timer.Enabled = true;
   111 
   112       plotColorPalette = new Color[13];
   113       plotColorPalette[0] = Color.Blue;
   114       plotColorPalette[1] = Color.OrangeRed;
   115       plotColorPalette[2] = Color.Green;
   116       plotColorPalette[3] = Color.LightSeaGreen;
   117       plotColorPalette[4] = Color.Goldenrod;
   118       plotColorPalette[5] = Color.DarkViolet;
   119       plotColorPalette[6] = Color.YellowGreen;
   120       plotColorPalette[7] = Color.SaddleBrown;
   121       plotColorPalette[8] = Color.RoyalBlue;
   122       plotColorPalette[9] = Color.DeepPink;
   123       plotColorPalette[10] = Color.MediumSeaGreen;
   124       plotColorPalette[11] = Color.Olive;
   125       plotColorPalette[12] = Color.Firebrick;
   126 
   127       hiddenMenuItem.Checked = Config.Get(hiddenMenuItem.Name, false);
   128       plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
   129 
   130       valueMenuItem.Checked = Config.Get(valueMenuItem.Name, true);
   131       minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
   132       maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
   133 
   134       startMinMenuItem.Checked = Config.Get(startMinMenuItem.Name, false); 
   135       minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
   136       startupMenuItem.Checked = startupManager.Startup;
   137       hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
   138 
   139       celciusToolStripMenuItem.Checked = 
   140         UnitManager.TemperatureUnit == TemperatureUnit.Celcius;
   141       fahrenheitToolStripMenuItem.Checked = !celciusToolStripMenuItem.Checked;
   142 
   143       // Hide the system tray and auto startup menu items on Unix
   144       int p = (int)System.Environment.OSVersion.Platform;
   145       if ((p == 4) || (p == 128)) {
   146         minTrayMenuItem.Visible = false;
   147         startupMenuItem.Visible = false;
   148       }
   149       
   150       if (startMinMenuItem.Checked) {
   151         if (!minTrayMenuItem.Checked) {
   152           WindowState = FormWindowState.Minimized;
   153           Show();
   154         }
   155       } else {
   156         Show();
   157       }
   158 
   159       // Create a handle, otherwise calling Close() does not fire FormClosed     
   160       IntPtr handle = Handle;
   161 
   162       // Make sure the settings are saved when the user logs off
   163       Microsoft.Win32.SystemEvents.SessionEnded +=
   164         delegate(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
   165           SaveConfiguration();
   166         };
   167     }
   168     
   169     private void SubHardwareAdded(IHardware hardware, Node node) {
   170       Node hardwareNode = new HardwareNode(hardware);
   171       node.Nodes.Add(hardwareNode);
   172       foreach (IHardware subHardware in hardware.SubHardware)
   173         SubHardwareAdded(subHardware, hardwareNode);  
   174     }
   175 
   176     private void HardwareAdded(IHardware hardware) {
   177       Node hardwareNode = new HardwareNode(hardware);
   178       root.Nodes.Add(hardwareNode);
   179       foreach (IHardware subHardware in hardware.SubHardware)
   180         SubHardwareAdded(subHardware, hardwareNode);     
   181     }
   182 
   183     private void HardwareRemoved(IHardware hardware) {      
   184       List<Node> nodesToRemove = new List<Node>();
   185       foreach (Node node in root.Nodes) {
   186         HardwareNode hardwareNode = node as HardwareNode;
   187         if (hardwareNode != null && hardwareNode.Hardware == hardware)
   188           nodesToRemove.Add(node);
   189       }
   190       foreach (Node node in nodesToRemove)
   191         root.Nodes.Remove(node);
   192     }
   193 
   194     private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
   195       Node node = e.Node.Tag as Node;
   196       if (node != null) {
   197         Color color;
   198         if (node.IsVisible) {
   199           SensorNode sensorNode = node as SensorNode;
   200           if (plotMenuItem.Checked && sensorNode != null &&
   201             sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
   202             e.TextColor = color;
   203         } else {
   204           e.TextColor = Color.DarkGray;
   205         }
   206       }
   207     }
   208 
   209     private void UpdatePlotSelection(object sender, 
   210       TreePathEventArgs e) 
   211     {
   212       List<ISensor> selected = new List<ISensor>();
   213       IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
   214       int colorIndex = 0;
   215       foreach (TreeNodeAdv node in treeView.AllNodes) {
   216         SensorNode sensorNode = node.Tag as SensorNode;
   217         if (sensorNode != null && 
   218           sensorNode.Sensor.SensorType == SensorType.Temperature) {
   219           if (sensorNode.Plot) {
   220             colors.Add(sensorNode.Sensor,
   221               plotColorPalette[colorIndex % plotColorPalette.Length]);
   222             selected.Add(sensorNode.Sensor);
   223           }
   224           colorIndex++;
   225         }
   226       }
   227       sensorPlotColors = colors;
   228       plotPanel.SetSensors(selected, colors);
   229     }
   230 
   231     private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
   232       NodeControlValueEventArgs e) {
   233       SensorNode node = e.Node.Tag as SensorNode;
   234       e.Value = (node != null) && 
   235         (node.Sensor.SensorType == SensorType.Temperature) && 
   236         plotMenuItem.Checked;
   237     }
   238 
   239     private void exitClick(object sender, EventArgs e) {
   240       Close();      
   241     }
   242 
   243     private void timer_Tick(object sender, EventArgs e) {
   244       computer.Accept(updateVisitor);
   245       treeView.Invalidate();
   246       plotPanel.Invalidate();
   247       systemTray.Redraw();
   248     }
   249 
   250     private void SaveConfiguration() {
   251       Config.Set(hiddenMenuItem.Name, hiddenMenuItem.Checked);
   252       Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
   253 
   254       Config.Set(valueMenuItem.Name, valueMenuItem.Checked);
   255       Config.Set(minMenuItem.Name, minMenuItem.Checked);
   256       Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
   257 
   258       Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked);
   259       Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
   260       Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
   261 
   262       if (WindowState != FormWindowState.Minimized) {
   263         Config.Set("mainForm.Location.X", Location.X);
   264         Config.Set("mainForm.Location.Y", Location.Y);
   265         Config.Set("mainForm.Width", Width);
   266         Config.Set("mainForm.Height", Height);
   267       }
   268 
   269       foreach (TreeColumn column in treeView.Columns)
   270         Config.Set("treeView.Columns." + column.Header + ".Width",
   271           column.Width);
   272 
   273       Config.Save();
   274     }
   275 
   276     private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   277       SaveConfiguration();
   278 
   279       timer.Enabled = false;
   280       systemTray.Dispose();      
   281       computer.Close();
   282     }
   283 
   284     private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
   285       new AboutBox().ShowDialog();
   286     }
   287 
   288     private void plotToolStripMenuItem_CheckedChanged(object sender, 
   289       EventArgs e) 
   290     {
   291       splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
   292       treeView.Invalidate();
   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         SensorNode node = info.Node.Tag as SensorNode;
   304         if (node != null && node.Sensor != null) {
   305 
   306           sensorContextMenuStrip.Items.Clear();
   307           if (node.Sensor.Parameters.Length > 0) {
   308             ToolStripMenuItem item = new ToolStripMenuItem("Parameters...");
   309             item.Click += delegate(object obj, EventArgs args) {
   310               ShowParameterForm(node.Sensor);
   311             };
   312             sensorContextMenuStrip.Items.Add(item);
   313           }
   314           if (node.IsVisible) {
   315             ToolStripMenuItem item = new ToolStripMenuItem("Hide");
   316             item.Click += delegate(object obj, EventArgs args) {
   317               node.IsVisible = false;
   318             };
   319             sensorContextMenuStrip.Items.Add(item);
   320           } else {
   321             ToolStripMenuItem item = new ToolStripMenuItem("Unhide");
   322             item.Click += delegate(object obj, EventArgs args) {
   323               node.IsVisible = true;
   324             };
   325             sensorContextMenuStrip.Items.Add(item);
   326           }         
   327           if (systemTray.Contains(node.Sensor)) {
   328             ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
   329             item.Click += delegate(object obj, EventArgs args) {
   330               systemTray.Remove(node.Sensor);
   331             };
   332             sensorContextMenuStrip.Items.Add(item);
   333           } else {
   334             ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
   335             item.Click += delegate(object obj, EventArgs args) {
   336               systemTray.Add(node.Sensor, true);
   337             };
   338             sensorContextMenuStrip.Items.Add(item);
   339           }
   340           sensorContextMenuStrip.Show(treeView, m.X, m.Y);
   341         }
   342       }
   343     }
   344 
   345     private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
   346       string report = computer.GetReport();
   347       if (saveFileDialog.ShowDialog() == DialogResult.OK) {
   348         using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
   349           w.Write(report);
   350         }
   351       }
   352     }
   353 
   354     private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
   355       EventArgs e) 
   356     {
   357       computer.HDDEnabled = hddMenuItem.Checked;
   358       UpdatePlotSelection(null, null);      
   359     }
   360 
   361     private void SysTrayHideShow() {
   362       Visible = !Visible;
   363       if (Visible)
   364         Activate();    
   365     }
   366 
   367     protected override void WndProc(ref Message m) {
   368       const int WM_SYSCOMMAND = 0x112;
   369       const int SC_MINIMIZE = 0xF020;
   370       if (minTrayMenuItem.Checked && 
   371         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
   372         SysTrayHideShow();
   373       } else {      
   374         base.WndProc(ref m);
   375       }
   376     }
   377 
   378     private void hideShowClick(object sender, EventArgs e) {
   379       SysTrayHideShow();
   380     }
   381 
   382     private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
   383       ToolStripMenuItem item = sender as ToolStripMenuItem;
   384       if (item == null)
   385         return;
   386 
   387       ISensor sensor = item.Owner.Tag as ISensor;
   388       if (sensor == null)
   389         return;
   390 
   391       systemTray.Remove(sensor);
   392     }
   393 
   394     private void ShowParameterForm(ISensor sensor) {
   395       ParameterForm form = new ParameterForm();
   396       form.Parameters = sensor.Parameters;
   397       form.captionLabel.Text = sensor.Name;
   398       form.ShowDialog();
   399     }
   400 
   401     private void treeView_NodeMouseDoubleClick(object sender, 
   402       TreeNodeAdvMouseEventArgs e) {
   403       SensorNode node = e.Node.Tag as SensorNode;
   404       if (node != null && node.Sensor != null && 
   405         node.Sensor.Parameters.Length > 0) {
   406         ShowParameterForm(node.Sensor);
   407       }
   408     }
   409 
   410     private void runOnWindowsStartupToolStripMenuItem_CheckedChanged(
   411       object sender, EventArgs e) 
   412     {
   413       startupManager.Startup = startupMenuItem.Checked;
   414     }
   415 
   416     private void minTrayMenuItem_CheckedChanged(object sender, EventArgs e) {
   417       systemTray.IsMainIconEnabled = minTrayMenuItem.Checked;
   418     }
   419 
   420     private void hiddenSensorsMenuItem_CheckedChanged(object sender, 
   421       EventArgs e) {
   422       treeModel.ForceVisible = hiddenMenuItem.Checked;
   423     }
   424 
   425     private void valueMenuItem_CheckedChanged(object sender, EventArgs e) {
   426       treeView.Columns[1].IsVisible = valueMenuItem.Checked;
   427     }
   428 
   429     private void minMenuItem_CheckedChanged(object sender, EventArgs e) {
   430       treeView.Columns[2].IsVisible = minMenuItem.Checked;
   431     }
   432 
   433     private void maxMenuItem_CheckedChanged(object sender, EventArgs e) {
   434       treeView.Columns[3].IsVisible = maxMenuItem.Checked;
   435     }
   436 
   437     private void celciusToolStripMenuItem_Click(object sender, EventArgs e) {
   438       celciusToolStripMenuItem.Checked = true;
   439       fahrenheitToolStripMenuItem.Checked = false;
   440       UnitManager.TemperatureUnit = TemperatureUnit.Celcius;
   441     }
   442 
   443     private void fahrenheitToolStripMenuItem_Click(object sender, EventArgs e) {
   444       celciusToolStripMenuItem.Checked = false;
   445       fahrenheitToolStripMenuItem.Checked = true;
   446       UnitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
   447     }
   448   }
   449 }