GUI/MainForm.cs
author moel.mich
Sun, 11 Jul 2010 21:26:49 +0000
changeset 153 62162db94359
parent 150 522da3b8cb1a
child 156 3e2ab626531c
permissions -rw-r--r--
Added a mainboard specific configuration for the ASRock 880GMH/USB3
     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       nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
    77 
    78       if (Utilities.Config.Contains("mainForm.Location.X")) {
    79         int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
    80         x = x < 0 ? 0 : x;
    81         int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
    82         y = y < 0 ? 0 : y;
    83         this.Location = new Point(x, y);
    84       } else {
    85         StartPosition = FormStartPosition.CenterScreen;
    86       }
    87 
    88       Width = Utilities.Config.Get("mainForm.Width", Width);
    89       Height = Utilities.Config.Get("mainForm.Height", Height);
    90 
    91       foreach (TreeColumn column in treeView.Columns) 
    92         column.Width = Math.Max(20, Math.Min(400, 
    93           Config.Get("treeView.Columns." + column.Header + ".Width",
    94           column.Width)));
    95 
    96       treeModel = new TreeModel();
    97       root = new Node(System.Environment.MachineName);
    98       root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
    99       
   100       treeModel.Nodes.Add(root);
   101       treeView.Model = treeModel;     
   102 
   103       systemTray = new SystemTray(computer);
   104       systemTray.HideShowCommand += hideShowClick;
   105       systemTray.ExitCommand += exitClick;
   106 
   107       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
   108       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
   109       computer.Open();
   110 
   111       timer.Enabled = true;
   112 
   113       plotColorPalette = new Color[13];
   114       plotColorPalette[0] = Color.Blue;
   115       plotColorPalette[1] = Color.OrangeRed;
   116       plotColorPalette[2] = Color.Green;
   117       plotColorPalette[3] = Color.LightSeaGreen;
   118       plotColorPalette[4] = Color.Goldenrod;
   119       plotColorPalette[5] = Color.DarkViolet;
   120       plotColorPalette[6] = Color.YellowGreen;
   121       plotColorPalette[7] = Color.SaddleBrown;
   122       plotColorPalette[8] = Color.RoyalBlue;
   123       plotColorPalette[9] = Color.DeepPink;
   124       plotColorPalette[10] = Color.MediumSeaGreen;
   125       plotColorPalette[11] = Color.Olive;
   126       plotColorPalette[12] = Color.Firebrick;
   127 
   128       hiddenMenuItem.Checked = Config.Get(hiddenMenuItem.Name, false);
   129       plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
   130 
   131       valueMenuItem.Checked = Config.Get(valueMenuItem.Name, true);
   132       minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
   133       maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
   134 
   135       startMinMenuItem.Checked = Config.Get(startMinMenuItem.Name, false); 
   136       minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
   137       startupMenuItem.Checked = startupManager.Startup;
   138       hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
   139 
   140       celciusToolStripMenuItem.Checked = 
   141         UnitManager.TemperatureUnit == TemperatureUnit.Celcius;
   142       fahrenheitToolStripMenuItem.Checked = !celciusToolStripMenuItem.Checked;
   143 
   144       startupMenuItem.Visible = startupManager.IsAvailable;
   145       
   146       if (startMinMenuItem.Checked) {
   147         if (!minTrayMenuItem.Checked) {
   148           WindowState = FormWindowState.Minimized;
   149           Show();
   150         }
   151       } else {
   152         Show();
   153       }
   154 
   155       // Create a handle, otherwise calling Close() does not fire FormClosed     
   156       IntPtr handle = Handle;
   157 
   158       // Make sure the settings are saved when the user logs off
   159       Microsoft.Win32.SystemEvents.SessionEnded +=
   160         delegate(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
   161           SaveConfiguration();
   162         };
   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 nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
   191       Node node = e.Node.Tag as Node;
   192       if (node != null) {
   193         Color color;
   194         if (node.IsVisible) {
   195           SensorNode sensorNode = node as SensorNode;
   196           if (plotMenuItem.Checked && sensorNode != null &&
   197             sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
   198             e.TextColor = color;
   199         } else {
   200           e.TextColor = Color.DarkGray;
   201         }
   202       }
   203     }
   204 
   205     private void UpdatePlotSelection(object sender, 
   206       TreePathEventArgs e) 
   207     {
   208       List<ISensor> selected = new List<ISensor>();
   209       IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
   210       int colorIndex = 0;
   211       foreach (TreeNodeAdv node in treeView.AllNodes) {
   212         SensorNode sensorNode = node.Tag as SensorNode;
   213         if (sensorNode != null && 
   214           sensorNode.Sensor.SensorType == SensorType.Temperature) {
   215           if (sensorNode.Plot) {
   216             colors.Add(sensorNode.Sensor,
   217               plotColorPalette[colorIndex % plotColorPalette.Length]);
   218             selected.Add(sensorNode.Sensor);
   219           }
   220           colorIndex++;
   221         }
   222       }
   223       sensorPlotColors = colors;
   224       plotPanel.SetSensors(selected, colors);
   225     }
   226 
   227     private void nodeTextBoxText_EditorShowing(object sender, CancelEventArgs e) 
   228     {
   229       e.Cancel = !(treeView.CurrentNode != null &&
   230         treeView.CurrentNode.Tag is SensorNode);
   231     }
   232 
   233     private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
   234       NodeControlValueEventArgs e) {
   235       SensorNode node = e.Node.Tag as SensorNode;
   236       e.Value = (node != null) && 
   237         (node.Sensor.SensorType == SensorType.Temperature) && 
   238         plotMenuItem.Checked;
   239     }
   240 
   241     private void exitClick(object sender, EventArgs e) {
   242       Close();      
   243     }
   244 
   245     private void timer_Tick(object sender, EventArgs e) {
   246       computer.Accept(updateVisitor);
   247       treeView.Invalidate();
   248       plotPanel.Invalidate();
   249       systemTray.Redraw();
   250     }
   251 
   252     private void SaveConfiguration() {
   253       Config.Set(hiddenMenuItem.Name, hiddenMenuItem.Checked);
   254       Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
   255 
   256       Config.Set(valueMenuItem.Name, valueMenuItem.Checked);
   257       Config.Set(minMenuItem.Name, minMenuItem.Checked);
   258       Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
   259 
   260       Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked);
   261       Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
   262       Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
   263 
   264       if (WindowState != FormWindowState.Minimized) {
   265         Config.Set("mainForm.Location.X", Location.X);
   266         Config.Set("mainForm.Location.Y", Location.Y);
   267         Config.Set("mainForm.Width", Width);
   268         Config.Set("mainForm.Height", Height);
   269       }
   270 
   271       foreach (TreeColumn column in treeView.Columns)
   272         Config.Set("treeView.Columns." + column.Header + ".Width",
   273           column.Width);
   274 
   275       Config.Save();
   276     }
   277 
   278     private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   279       SaveConfiguration();
   280 
   281       timer.Enabled = false;
   282       systemTray.Dispose();      
   283       computer.Close();
   284     }
   285 
   286     private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
   287       new AboutBox().ShowDialog();
   288     }
   289 
   290     private void plotToolStripMenuItem_CheckedChanged(object sender, 
   291       EventArgs e) 
   292     {
   293       splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
   294       treeView.Invalidate();
   295     }
   296 
   297     private void treeView_Click(object sender, EventArgs e) {
   298       
   299       MouseEventArgs m = e as MouseEventArgs;
   300       if (m == null || m.Button != MouseButtons.Right)
   301         return;
   302 
   303       NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
   304       if (info.Control != null) {
   305         SensorNode node = info.Node.Tag as SensorNode;
   306         if (node != null && node.Sensor != null) {
   307 
   308           sensorContextMenuStrip.Items.Clear();
   309           if (node.Sensor.Parameters.Length > 0) {
   310             ToolStripMenuItem item = new ToolStripMenuItem("Parameters...");
   311             item.Click += delegate(object obj, EventArgs args) {
   312               ShowParameterForm(node.Sensor);
   313             };
   314             sensorContextMenuStrip.Items.Add(item);
   315           }
   316           EditableControl control = info.Control as EditableControl;
   317           if (control != null) {
   318             ToolStripMenuItem item = new ToolStripMenuItem("Rename");
   319             item.Click += delegate(object obj, EventArgs args) {
   320               control.BeginEdit();
   321             };
   322             sensorContextMenuStrip.Items.Add(item);
   323           }          
   324           if (node.IsVisible) {
   325             ToolStripMenuItem item = new ToolStripMenuItem("Hide");
   326             item.Click += delegate(object obj, EventArgs args) {
   327               node.IsVisible = false;
   328             };
   329             sensorContextMenuStrip.Items.Add(item);
   330           } else {
   331             ToolStripMenuItem item = new ToolStripMenuItem("Unhide");
   332             item.Click += delegate(object obj, EventArgs args) {
   333               node.IsVisible = true;
   334             };
   335             sensorContextMenuStrip.Items.Add(item);
   336           }         
   337           if (systemTray.Contains(node.Sensor)) {
   338             ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
   339             item.Click += delegate(object obj, EventArgs args) {
   340               systemTray.Remove(node.Sensor);
   341             };
   342             sensorContextMenuStrip.Items.Add(item);
   343           } else {
   344             ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
   345             item.Click += delegate(object obj, EventArgs args) {
   346               systemTray.Add(node.Sensor, true);
   347             };
   348             sensorContextMenuStrip.Items.Add(item);
   349           }
   350           sensorContextMenuStrip.Show(treeView, m.X, m.Y);
   351         }
   352       }
   353     }
   354 
   355     private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
   356       string report = computer.GetReport();
   357       if (saveFileDialog.ShowDialog() == DialogResult.OK) {
   358         using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
   359           w.Write(report);
   360         }
   361       }
   362     }
   363 
   364     private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
   365       EventArgs e) 
   366     {
   367       computer.HDDEnabled = hddMenuItem.Checked;
   368       UpdatePlotSelection(null, null);      
   369     }
   370 
   371     private void SysTrayHideShow() {
   372       Visible = !Visible;
   373       if (Visible)
   374         Activate();    
   375     }
   376 
   377     protected override void WndProc(ref Message m) {
   378       const int WM_SYSCOMMAND = 0x112;
   379       const int SC_MINIMIZE = 0xF020;
   380       if (minTrayMenuItem.Checked && 
   381         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
   382         SysTrayHideShow();
   383       } else {      
   384         base.WndProc(ref m);
   385       }
   386     }
   387 
   388     private void hideShowClick(object sender, EventArgs e) {
   389       SysTrayHideShow();
   390     }
   391 
   392     private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
   393       ToolStripMenuItem item = sender as ToolStripMenuItem;
   394       if (item == null)
   395         return;
   396 
   397       ISensor sensor = item.Owner.Tag as ISensor;
   398       if (sensor == null)
   399         return;
   400 
   401       systemTray.Remove(sensor);
   402     }
   403 
   404     private void ShowParameterForm(ISensor sensor) {
   405       ParameterForm form = new ParameterForm();
   406       form.Parameters = sensor.Parameters;
   407       form.captionLabel.Text = sensor.Name;
   408       form.ShowDialog();
   409     }
   410 
   411     private void treeView_NodeMouseDoubleClick(object sender, 
   412       TreeNodeAdvMouseEventArgs e) {
   413       SensorNode node = e.Node.Tag as SensorNode;
   414       if (node != null && node.Sensor != null && 
   415         node.Sensor.Parameters.Length > 0) {
   416         ShowParameterForm(node.Sensor);
   417       }
   418     }
   419 
   420     private void runOnWindowsStartupToolStripMenuItem_CheckedChanged(
   421       object sender, EventArgs e) 
   422     {
   423       startupManager.Startup = startupMenuItem.Checked;
   424     }
   425 
   426     private void minTrayMenuItem_CheckedChanged(object sender, EventArgs e) {
   427       systemTray.IsMainIconEnabled = minTrayMenuItem.Checked;
   428     }
   429 
   430     private void hiddenSensorsMenuItem_CheckedChanged(object sender, 
   431       EventArgs e) {
   432       treeModel.ForceVisible = hiddenMenuItem.Checked;
   433     }
   434 
   435     private void valueMenuItem_CheckedChanged(object sender, EventArgs e) {
   436       treeView.Columns[1].IsVisible = valueMenuItem.Checked;
   437     }
   438 
   439     private void minMenuItem_CheckedChanged(object sender, EventArgs e) {
   440       treeView.Columns[2].IsVisible = minMenuItem.Checked;
   441     }
   442 
   443     private void maxMenuItem_CheckedChanged(object sender, EventArgs e) {
   444       treeView.Columns[3].IsVisible = maxMenuItem.Checked;
   445     }
   446 
   447     private void celciusToolStripMenuItem_Click(object sender, EventArgs e) {
   448       celciusToolStripMenuItem.Checked = true;
   449       fahrenheitToolStripMenuItem.Checked = false;
   450       UnitManager.TemperatureUnit = TemperatureUnit.Celcius;
   451     }
   452 
   453     private void fahrenheitToolStripMenuItem_Click(object sender, EventArgs e) {
   454       celciusToolStripMenuItem.Checked = false;
   455       fahrenheitToolStripMenuItem.Checked = true;
   456       UnitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
   457     }
   458 
   459     private void sumbitReportToolStripMenuItem_Click(object sender, EventArgs e) 
   460     {
   461       ReportForm form = new ReportForm();
   462       form.Report = computer.GetReport();
   463       form.ShowDialog();      
   464     }
   465 
   466     private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
   467       IVisitor visitor = new ResetMinMaxVisitor();
   468       computer.Accept(visitor);
   469     }
   470   }
   471 }