GUI/MainForm.cs
author moel.mich
Tue, 16 Nov 2010 21:48:26 +0000
changeset 243 07a9329cd87c
parent 232 475b7160e3bc
child 244 99f16e21cdc8
permissions -rw-r--r--
Improved the moving and resizing of the gadget on multi-monitor systems. Fixed one problem where the underlying gadget window size would be reduced unrequested. Fine tuned the progress bar size in the gadget.
     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): Paul Werelds
    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.Drawing;
    42 using System.IO;
    43 using System.Windows.Forms;
    44 using Aga.Controls.Tree;
    45 using Aga.Controls.Tree.NodeControls;
    46 using OpenHardwareMonitor.Hardware;
    47 using OpenHardwareMonitor.WMI;
    48 
    49 namespace OpenHardwareMonitor.GUI {
    50   public partial class MainForm : Form {
    51 
    52     private PersistentSettings settings;
    53     private UnitManager unitManager;
    54     private Computer 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     private SensorGadget gadget;
    64 
    65     private UserOption showHiddenSensors;
    66     private UserOption showPlot;
    67     private UserOption showValue;
    68     private UserOption showMin;
    69     private UserOption showMax;
    70     private UserOption startMinimized;
    71     private UserOption minimizeToTray;
    72     private UserOption minimizeOnClose;
    73     private UserOption autoStart;
    74     private UserOption readHddSensors;
    75     private UserOption showGadget;
    76 
    77     private WmiProvider wmiProvider;
    78 
    79     public MainForm() {      
    80       InitializeComponent();
    81 
    82       this.settings = new PersistentSettings();      
    83       this.settings.Load(Path.ChangeExtension(
    84         Application.ExecutablePath, ".config"));
    85 
    86       this.unitManager = new UnitManager(settings);
    87 
    88       // set the DockStyle here, to avoid conflicts with the MainMenu
    89       this.splitContainer.Dock = DockStyle.Fill;
    90             
    91       this.Font = SystemFonts.MessageBoxFont;
    92       treeView.Font = SystemFonts.MessageBoxFont;
    93       plotPanel.Font = SystemFonts.MessageBoxFont;
    94       
    95       nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
    96       nodeCheckBox.CheckStateChanged += UpdatePlotSelection;
    97       nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
    98       nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
    99       nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
   100       nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
   101       nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
   102 
   103       foreach (TreeColumn column in treeView.Columns) 
   104         column.Width = Math.Max(20, Math.Min(400,
   105           settings.GetValue("treeView.Columns." + column.Header + ".Width",
   106           column.Width)));
   107 
   108       treeModel = new TreeModel();
   109       root = new Node(System.Environment.MachineName);
   110       root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
   111       
   112       treeModel.Nodes.Add(root);
   113       treeView.Model = treeModel;
   114 
   115       this.computer = new Computer(settings);
   116 
   117       systemTray = new SystemTray(computer, settings);
   118       systemTray.HideShowCommand += hideShowClick;
   119       systemTray.ExitCommand += exitClick;
   120 
   121       int p = (int)Environment.OSVersion.Platform;
   122       if ((p == 4) || (p == 128)) { // Unix
   123         splitContainer.BorderStyle = BorderStyle.None;
   124         splitContainer.Border3DStyle = Border3DStyle.Adjust;
   125         splitContainer.SplitterWidth = 4;
   126         treeView.BorderStyle = BorderStyle.Fixed3D;
   127         plotPanel.BorderStyle = BorderStyle.Fixed3D;
   128         gadgetMenuItem.Visible = false;
   129         minCloseMenuItem.Visible = false;
   130       } else { // Windows
   131         gadget = new SensorGadget(computer, settings, unitManager);
   132         wmiProvider = new WmiProvider(computer);
   133       }          
   134 
   135       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
   136       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);        
   137 
   138       computer.Open();
   139 
   140       timer.Enabled = true;
   141 
   142       plotColorPalette = new Color[13];
   143       plotColorPalette[0] = Color.Blue;
   144       plotColorPalette[1] = Color.OrangeRed;
   145       plotColorPalette[2] = Color.Green;
   146       plotColorPalette[3] = Color.LightSeaGreen;
   147       plotColorPalette[4] = Color.Goldenrod;
   148       plotColorPalette[5] = Color.DarkViolet;
   149       plotColorPalette[6] = Color.YellowGreen;
   150       plotColorPalette[7] = Color.SaddleBrown;
   151       plotColorPalette[8] = Color.RoyalBlue;
   152       plotColorPalette[9] = Color.DeepPink;
   153       plotColorPalette[10] = Color.MediumSeaGreen;
   154       plotColorPalette[11] = Color.Olive;
   155       plotColorPalette[12] = Color.Firebrick;
   156 
   157       showHiddenSensors = new UserOption("hiddenMenuItem", false,
   158         hiddenMenuItem, settings);
   159       showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
   160         treeModel.ForceVisible = showHiddenSensors.Value;
   161       };
   162 
   163       showPlot = new UserOption("plotMenuItem", false, plotMenuItem, settings);
   164       showPlot.Changed += delegate(object sender, EventArgs e) {
   165         splitContainer.Panel2Collapsed = !showPlot.Value;
   166         treeView.Invalidate();
   167       };
   168 
   169       showValue = new UserOption("valueMenuItem", true, valueMenuItem,
   170         settings);
   171       showValue.Changed += delegate(object sender, EventArgs e) {
   172         treeView.Columns[1].IsVisible = showValue.Value;
   173       };
   174 
   175       showMin = new UserOption("minMenuItem", false, minMenuItem, settings);
   176       showMin.Changed += delegate(object sender, EventArgs e) {
   177         treeView.Columns[2].IsVisible = showMin.Value;
   178       };
   179 
   180       showMax = new UserOption("maxMenuItem", true, maxMenuItem, settings);
   181       showMax.Changed += delegate(object sender, EventArgs e) {
   182         treeView.Columns[3].IsVisible = showMax.Value;
   183       };
   184 
   185       startMinimized = new UserOption("startMinMenuItem", false,
   186         startMinMenuItem, settings);
   187 
   188       minimizeToTray = new UserOption("minTrayMenuItem", true,
   189         minTrayMenuItem, settings);
   190       minimizeToTray.Changed += delegate(object sender, EventArgs e) {
   191         systemTray.IsMainIconEnabled = minimizeToTray.Value;
   192       };
   193 
   194       minimizeOnClose = new UserOption("minCloseMenuItem", false,
   195         minCloseMenuItem, settings);
   196 
   197       autoStart = new UserOption(null, startupManager.Startup,
   198         startupMenuItem, settings);
   199       autoStart.Changed += delegate(object sender, EventArgs e) {
   200         try {
   201           startupManager.Startup = autoStart.Value;
   202         } catch (InvalidOperationException) {
   203           MessageBox.Show("Updating the auto-startup option failed.", "Error", 
   204             MessageBoxButtons.OK, MessageBoxIcon.Error);
   205           autoStart.Value = startupManager.Startup;
   206         }
   207       };
   208 
   209       readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
   210         settings);
   211       readHddSensors.Changed += delegate(object sender, EventArgs e) {
   212         computer.HDDEnabled = readHddSensors.Value;
   213         UpdatePlotSelection(null, null);
   214       };
   215 
   216       showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
   217         settings);
   218       showGadget.Changed += delegate(object sender, EventArgs e) {
   219         if (gadget != null) 
   220           gadget.Visible = showGadget.Value;
   221       };
   222 
   223       celciusMenuItem.Checked = 
   224         unitManager.TemperatureUnit == TemperatureUnit.Celcius;
   225       fahrenheitMenuItem.Checked = !celciusMenuItem.Checked;
   226 
   227       startupMenuItem.Visible = startupManager.IsAvailable;
   228       
   229       if (startMinMenuItem.Checked) {
   230         if (!minTrayMenuItem.Checked) {
   231           WindowState = FormWindowState.Minimized;
   232           Show();
   233         }
   234       } else {
   235         Show();
   236       }
   237 
   238       // Create a handle, otherwise calling Close() does not fire FormClosed     
   239       IntPtr handle = Handle;
   240 
   241       // Make sure the settings are saved when the user logs off
   242       Microsoft.Win32.SystemEvents.SessionEnded += delegate {
   243         SaveConfiguration();
   244       };  
   245     }
   246     
   247     private void SubHardwareAdded(IHardware hardware, Node node) {
   248       Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
   249       node.Nodes.Add(hardwareNode);
   250       foreach (IHardware subHardware in hardware.SubHardware)
   251         SubHardwareAdded(subHardware, hardwareNode);  
   252     }
   253 
   254     private void HardwareAdded(IHardware hardware) {
   255       Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
   256       root.Nodes.Add(hardwareNode);
   257       foreach (IHardware subHardware in hardware.SubHardware)
   258         SubHardwareAdded(subHardware, hardwareNode);     
   259     }
   260 
   261     private void HardwareRemoved(IHardware hardware) {      
   262       List<Node> nodesToRemove = new List<Node>();
   263       foreach (Node node in root.Nodes) {
   264         HardwareNode hardwareNode = node as HardwareNode;
   265         if (hardwareNode != null && hardwareNode.Hardware == hardware)
   266           nodesToRemove.Add(node);
   267       }
   268       foreach (Node node in nodesToRemove)
   269         root.Nodes.Remove(node);
   270     }
   271 
   272     private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
   273       Node node = e.Node.Tag as Node;
   274       if (node != null) {
   275         Color color;
   276         if (node.IsVisible) {
   277           SensorNode sensorNode = node as SensorNode;
   278           if (plotMenuItem.Checked && sensorNode != null &&
   279             sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
   280             e.TextColor = color;
   281         } else {
   282           e.TextColor = Color.DarkGray;
   283         }
   284       }
   285     }
   286 
   287     private void UpdatePlotSelection(object sender, 
   288       TreePathEventArgs e) 
   289     {
   290       List<ISensor> selected = new List<ISensor>();
   291       IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
   292       int colorIndex = 0;
   293       foreach (TreeNodeAdv node in treeView.AllNodes) {
   294         SensorNode sensorNode = node.Tag as SensorNode;
   295         if (sensorNode != null && 
   296           sensorNode.Sensor.SensorType == SensorType.Temperature) {
   297           if (sensorNode.Plot) {
   298             colors.Add(sensorNode.Sensor,
   299               plotColorPalette[colorIndex % plotColorPalette.Length]);
   300             selected.Add(sensorNode.Sensor);
   301           }
   302           colorIndex++;
   303         }
   304       }
   305       sensorPlotColors = colors;
   306       plotPanel.SetSensors(selected, colors);
   307     }
   308 
   309     private void nodeTextBoxText_EditorShowing(object sender,
   310       CancelEventArgs e) 
   311     {
   312       e.Cancel = !(treeView.CurrentNode != null &&
   313         treeView.CurrentNode.Tag is SensorNode);
   314     }
   315 
   316     private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
   317       NodeControlValueEventArgs e) {
   318       SensorNode node = e.Node.Tag as SensorNode;
   319       e.Value = (node != null) && 
   320         (node.Sensor.SensorType == SensorType.Temperature) && 
   321         plotMenuItem.Checked;
   322     }
   323 
   324     private void exitClick(object sender, EventArgs e) {
   325       Close();
   326     }
   327 
   328     private void timer_Tick(object sender, EventArgs e) {
   329       computer.Accept(updateVisitor);
   330       treeView.Invalidate();
   331       plotPanel.Invalidate();
   332       systemTray.Redraw();
   333       if (gadget != null)
   334         gadget.Redraw();
   335 
   336       if (wmiProvider != null)
   337         wmiProvider.Update();
   338     }
   339 
   340     private void SaveConfiguration() {
   341       foreach (TreeColumn column in treeView.Columns)
   342         settings.SetValue("treeView.Columns." + column.Header + ".Width",
   343           column.Width);
   344 
   345       string fileName = Path.ChangeExtension(
   346           System.Windows.Forms.Application.ExecutablePath, ".config");
   347       try {
   348         settings.Save(fileName);
   349       } catch (UnauthorizedAccessException) {
   350         MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
   351           "The current seetings could not be saved.", 
   352           "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
   353       }
   354     }
   355 
   356     private void MainForm_Load(object sender, EventArgs e) {
   357       Rectangle newBounds = new Rectangle {
   358         X = settings.GetValue("mainForm.Location.X", Location.X),
   359         Y = settings.GetValue("mainForm.Location.Y", Location.Y),
   360         Width = settings.GetValue("mainForm.Width", 470),
   361         Height = settings.GetValue("mainForm.Height", 640)
   362       };
   363 
   364       Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
   365         int.MinValue, int.MinValue);
   366 
   367       foreach (Screen screen in Screen.AllScreens)
   368         fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds);
   369 
   370       Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds);
   371       if (intersection.Width < 20 || intersection.Height < 20 ||
   372         !settings.Contains("mainForm.Location.X")
   373       ) {
   374         newBounds.X = (Screen.PrimaryScreen.WorkingArea.Width / 2) -
   375                       (newBounds.Width/2);
   376 
   377         newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) -
   378                       (newBounds.Height / 2);
   379       }
   380 
   381       this.Bounds = newBounds;
   382     }
   383     
   384     private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
   385       Visible = false;
   386       SaveConfiguration();
   387 
   388       timer.Enabled = false;
   389       systemTray.Dispose();      
   390       computer.Close();
   391     }
   392 
   393     private void aboutMenuItem_Click(object sender, EventArgs e) {
   394       new AboutBox().ShowDialog();
   395     }
   396 
   397     private void treeView_Click(object sender, EventArgs e) {
   398       
   399       MouseEventArgs m = e as MouseEventArgs;
   400       if (m == null || m.Button != MouseButtons.Right)
   401         return;
   402 
   403       NodeControlInfo info = treeView.GetNodeControlInfoAt(
   404         new Point(m.X, m.Y)
   405       );
   406       treeView.SelectedNode = info.Node;
   407       if (info.Node != null) {
   408         SensorNode node = info.Node.Tag as SensorNode;
   409         if (node != null && node.Sensor != null) {
   410           sensorContextMenu.MenuItems.Clear();
   411           if (node.Sensor.Parameters.Length > 0) {
   412             MenuItem item = new MenuItem("Parameters...");
   413             item.Click += delegate(object obj, EventArgs args) {
   414               ShowParameterForm(node.Sensor);
   415             };
   416             sensorContextMenu.MenuItems.Add(item);
   417           }
   418           if (nodeTextBoxText.EditEnabled) {
   419             MenuItem item = new MenuItem("Rename");
   420             item.Click += delegate(object obj, EventArgs args) {
   421               nodeTextBoxText.BeginEdit();
   422             };
   423             sensorContextMenu.MenuItems.Add(item);
   424           }
   425           if (node.IsVisible) {
   426             MenuItem item = new MenuItem("Hide");
   427             item.Click += delegate(object obj, EventArgs args) {
   428               node.IsVisible = false;
   429             };
   430             sensorContextMenu.MenuItems.Add(item);
   431           } else {
   432             MenuItem item = new MenuItem("Unhide");
   433             item.Click += delegate(object obj, EventArgs args) {
   434               node.IsVisible = true;
   435             };
   436             sensorContextMenu.MenuItems.Add(item);
   437           }
   438           sensorContextMenu.MenuItems.Add(new MenuItem("-"));
   439           {
   440             MenuItem item = new MenuItem("Show in Tray");
   441             item.Checked = systemTray.Contains(node.Sensor);
   442             item.Click += delegate(object obj, EventArgs args) {
   443               if (item.Checked)
   444                 systemTray.Remove(node.Sensor);
   445               else
   446                 systemTray.Add(node.Sensor, true);
   447             };
   448             sensorContextMenu.MenuItems.Add(item);
   449           }
   450           if (gadget != null) {
   451             MenuItem item = new MenuItem("Show in Gadget");
   452             item.Checked = gadget.Contains(node.Sensor);
   453             item.Click += delegate(object obj, EventArgs args) {
   454               if (item.Checked) {
   455                 gadget.Remove(node.Sensor);
   456               } else {
   457                 gadget.Add(node.Sensor);
   458               }
   459             };
   460             sensorContextMenu.MenuItems.Add(item);
   461           }
   462 
   463           sensorContextMenu.Show(treeView, new Point(m.X, m.Y));
   464         }
   465       }
   466     }
   467 
   468     private void saveReportMenuItem_Click(object sender, EventArgs e) {
   469       string report = computer.GetReport();
   470       if (saveFileDialog.ShowDialog() == DialogResult.OK) {
   471         using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
   472           w.Write(report);
   473         }
   474       }
   475     }
   476 
   477     private void SysTrayHideShow() {
   478       Visible = !Visible;
   479       if (Visible)
   480         Activate();    
   481     }
   482 
   483     protected override void WndProc(ref Message m) {
   484       const int WM_SYSCOMMAND = 0x112;
   485       const int SC_MINIMIZE = 0xF020;
   486       const int SC_CLOSE = 0xF060;
   487 
   488       if (minimizeToTray.Value && 
   489         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
   490         SysTrayHideShow();
   491       } else if(minimizeOnClose.Value && 
   492         m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE) {
   493         /*
   494          * Apparently the user wants to minimize rather than close
   495          * Now we still need to check if we're going to the tray or not
   496          * 
   497          * Note: the correct way to do this would be to send out SC_MINIMIZE,
   498          * but since the code here is so simple,
   499          * that would just be a waste of time.
   500          */
   501         if (minimizeToTray.Value)
   502           SysTrayHideShow();
   503         else
   504           WindowState = FormWindowState.Minimized;
   505       } else {      
   506         base.WndProc(ref m);
   507       }
   508     }
   509 
   510     private void hideShowClick(object sender, EventArgs e) {
   511       SysTrayHideShow();
   512     }
   513 
   514     private void ShowParameterForm(ISensor sensor) {
   515       ParameterForm form = new ParameterForm();
   516       form.Parameters = sensor.Parameters;
   517       form.captionLabel.Text = sensor.Name;
   518       form.ShowDialog();
   519     }
   520 
   521     private void treeView_NodeMouseDoubleClick(object sender, 
   522       TreeNodeAdvMouseEventArgs e) {
   523       SensorNode node = e.Node.Tag as SensorNode;
   524       if (node != null && node.Sensor != null && 
   525         node.Sensor.Parameters.Length > 0) {
   526         ShowParameterForm(node.Sensor);
   527       }
   528     }
   529 
   530     private void celciusMenuItem_Click(object sender, EventArgs e) {
   531       celciusMenuItem.Checked = true;
   532       fahrenheitMenuItem.Checked = false;
   533       unitManager.TemperatureUnit = TemperatureUnit.Celcius;
   534     }
   535 
   536     private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
   537       celciusMenuItem.Checked = false;
   538       fahrenheitMenuItem.Checked = true;
   539       unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
   540     }
   541 
   542     private void sumbitReportMenuItem_Click(object sender, EventArgs e) 
   543     {
   544       ReportForm form = new ReportForm();
   545       form.Report = computer.GetReport();
   546       form.ShowDialog();      
   547     }
   548 
   549     private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
   550       computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
   551         sensor.ResetMin();
   552         sensor.ResetMax();
   553       }));
   554     }
   555 
   556     private void MainForm_MoveOrResize(object sender, EventArgs e) {
   557       if (WindowState != FormWindowState.Minimized) {
   558         settings.SetValue("mainForm.Location.X", Bounds.X);
   559         settings.SetValue("mainForm.Location.Y", Bounds.Y);
   560         settings.SetValue("mainForm.Width", Bounds.Width);
   561         settings.SetValue("mainForm.Height", Bounds.Height);
   562       }
   563     }
   564   }
   565 }