GUI/MainForm.cs
author moel.mich
Sun, 31 Jan 2010 22:00:31 +0000
changeset 15 0e812fe67bac
parent 2 105939e4eb7e
child 24 09ab31bee6bd
permissions -rw-r--r--
Release version 0.1.6. Fixed F71882 temperature reading. Added error handling for ATI GPUs (ADL). Fixed sensor events.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.ComponentModel;
moel@1
    41
using System.Configuration;
moel@1
    42
using System.Drawing;
moel@1
    43
using System.Text;
moel@1
    44
using System.Windows.Forms;
moel@1
    45
using Aga.Controls.Tree;
moel@1
    46
using Aga.Controls.Tree.NodeControls;
moel@1
    47
using OpenHardwareMonitor.Hardware;
moel@1
    48
moel@1
    49
namespace OpenHardwareMonitor.GUI {
moel@1
    50
  public partial class MainForm : Form {
moel@1
    51
moel@1
    52
    private Node root;
moel@1
    53
    private List<IGroup> groupList = new List<IGroup>();
moel@1
    54
    private TreeModel treeModel;
moel@1
    55
    private IDictionary<ISensor, Color> sensorPlotColors = 
moel@1
    56
      new Dictionary<ISensor, Color>();
moel@1
    57
    private Color[] plotColorPalette;
moel@1
    58
moel@1
    59
    public MainForm() {
moel@1
    60
      
moel@1
    61
      InitializeComponent();
moel@1
    62
      this.Font = SystemFonts.MessageBoxFont;
moel@1
    63
      treeView.Font = SystemFonts.MessageBoxFont;
moel@1
    64
      plotPanel.Font = SystemFonts.MessageBoxFont;      
moel@1
    65
      
moel@1
    66
      nodeCheckBox.IsVisibleValueNeeded += 
moel@1
    67
        new EventHandler<NodeControlValueEventArgs>(
moel@1
    68
          nodeCheckBox_IsVisibleValueNeeded);
moel@1
    69
      nodeCheckBox.CheckStateChanged += 
moel@1
    70
        new EventHandler<TreePathEventArgs>(UpdatePlotSelection);
moel@1
    71
      nodeTextBoxText.DrawText += 
moel@1
    72
        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
moel@1
    73
      nodeTextBoxValue.DrawText +=
moel@1
    74
        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
moel@1
    75
      nodeTextBoxMin.DrawText +=
moel@1
    76
        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
moel@1
    77
      nodeTextBoxMax.DrawText +=
moel@1
    78
        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
moel@1
    79
      nodeTextBoxLimit.DrawText += 
moel@1
    80
        new EventHandler<DrawEventArgs>(nodeTextBoxLimit_DrawText);
moel@1
    81
moel@1
    82
      if (Utilities.Config.Contains("mainForm.Location.X")) {
moel@1
    83
        int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
moel@1
    84
        x = x < 0 ? 0 : x;
moel@1
    85
        int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
moel@1
    86
        y = y < 0 ? 0 : y;
moel@1
    87
        this.Location = new Point(x, y);
moel@1
    88
      } else {
moel@1
    89
        StartPosition = FormStartPosition.CenterScreen;
moel@1
    90
      }
moel@1
    91
moel@1
    92
      Width = Utilities.Config.Get("mainForm.Width", Width);
moel@1
    93
      Height = Utilities.Config.Get("mainForm.Height", Height);
moel@1
    94
         
moel@1
    95
      treeModel = new TreeModel();
moel@1
    96
      root = new Node(System.Environment.MachineName);
moel@1
    97
      root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
moel@1
    98
      
moel@1
    99
      treeModel.Nodes.Add(root);
moel@1
   100
      treeView.Model = treeModel;
moel@1
   101
moel@1
   102
      AddGroup(new Hardware.SMBIOS.SMBIOSGroup());
moel@1
   103
      AddGroup(new Hardware.LPC.LPCGroup());
moel@1
   104
      AddGroup(new Hardware.CPU.CPUGroup());
moel@1
   105
      AddGroup(new Hardware.ATI.ATIGroup());
moel@1
   106
      AddGroup(new Hardware.Nvidia.NvidiaGroup());
moel@1
   107
      AddGroup(new Hardware.TBalancer.TBalancerGroup());
moel@1
   108
      
moel@1
   109
      plotColorPalette = new Color[14];
moel@1
   110
      plotColorPalette[0] = Color.Blue;
moel@1
   111
      plotColorPalette[1] = Color.OrangeRed;
moel@1
   112
      plotColorPalette[2] = Color.Green;
moel@1
   113
      plotColorPalette[3] = Color.LightSeaGreen;
moel@1
   114
      plotColorPalette[4] = Color.Goldenrod;
moel@1
   115
      plotColorPalette[5] = Color.DarkViolet;
moel@1
   116
      plotColorPalette[6] = Color.YellowGreen;
moel@1
   117
      plotColorPalette[7] = Color.SaddleBrown;
moel@1
   118
      plotColorPalette[8] = Color.Gray;
moel@1
   119
      plotColorPalette[9] = Color.RoyalBlue;
moel@1
   120
      plotColorPalette[10] = Color.DeepPink;
moel@1
   121
      plotColorPalette[11] = Color.MediumSeaGreen;
moel@1
   122
      plotColorPalette[12] = Color.Olive;
moel@1
   123
      plotColorPalette[13] = Color.Firebrick;
moel@1
   124
moel@1
   125
      plotMenuItem.Checked = Utilities.Config.Get(plotMenuItem.Name, false);
moel@1
   126
      minMenuItem.Checked = Utilities.Config.Get(minMenuItem.Name, false);
moel@1
   127
      maxMenuItem.Checked = Utilities.Config.Get(maxMenuItem.Name, true);
moel@1
   128
      limitMenuItem.Checked = Utilities.Config.Get(limitMenuItem.Name, false);
moel@2
   129
      hddMenuItem.Checked = Utilities.Config.Get(hddMenuItem.Name, true);
moel@1
   130
moel@1
   131
      voltMenuItem.Checked = Utilities.Config.Get(voltMenuItem.Name, true);
moel@1
   132
      clocksMenuItem.Checked = Utilities.Config.Get(clocksMenuItem.Name, true);
moel@1
   133
      tempMenuItem.Checked = Utilities.Config.Get(tempMenuItem.Name, true);
moel@1
   134
      fansMenuItem.Checked = Utilities.Config.Get(fansMenuItem.Name, true);
moel@1
   135
moel@1
   136
      timer.Enabled = true;
moel@1
   137
    }
moel@1
   138
moel@1
   139
    private void AddGroup(IGroup group) {
moel@1
   140
      groupList.Add(group);
moel@1
   141
      foreach (IHardware hardware in group.Hardware)
moel@1
   142
        root.Nodes.Add(new HardwareNode(hardware));
moel@1
   143
    }
moel@1
   144
moel@1
   145
    private void RemoveGroup(IGroup group) {
moel@1
   146
      List<Node> nodesToRemove = new List<Node>();
moel@1
   147
      foreach (IHardware hardware in group.Hardware)
moel@1
   148
        foreach (Node node in root.Nodes) {
moel@1
   149
          HardwareNode hardwareNode = node as HardwareNode;
moel@1
   150
          if (hardwareNode != null && hardwareNode.Hardware == hardware)
moel@1
   151
            nodesToRemove.Add(node);
moel@1
   152
        }
moel@1
   153
      foreach (Node node in nodesToRemove)
moel@1
   154
        root.Nodes.Remove(node);
moel@1
   155
      groupList.Remove(group);
moel@1
   156
    }
moel@1
   157
moel@1
   158
    private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
moel@1
   159
      SensorNode sensorNode = e.Node.Tag as SensorNode;
moel@1
   160
      if (sensorNode != null) 
moel@1
   161
        e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
moel@1
   162
    }
moel@1
   163
moel@1
   164
    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
moel@1
   165
      if (!plotMenuItem.Checked)
moel@1
   166
        return;      
moel@1
   167
moel@1
   168
      SensorNode sensorNode = e.Node.Tag as SensorNode;
moel@1
   169
      if (sensorNode != null) {
moel@1
   170
        Color color;
moel@1
   171
        if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color)) 
moel@1
   172
          e.TextColor = color;        
moel@1
   173
      }
moel@1
   174
    }
moel@1
   175
moel@1
   176
    private void UpdatePlotSelection(object sender, 
moel@1
   177
      TreePathEventArgs e) 
moel@1
   178
    {
moel@1
   179
      List<ISensor> selected = new List<ISensor>();
moel@1
   180
      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
moel@1
   181
      int colorIndex = 0;
moel@1
   182
      foreach (TreeNodeAdv node in treeView.AllNodes) {
moel@1
   183
        SensorNode sensorNode = node.Tag as SensorNode;
moel@1
   184
        if (sensorNode != null && 
moel@1
   185
          sensorNode.Sensor.SensorType == SensorType.Temperature) {
moel@1
   186
          if (sensorNode.Plot) {
moel@1
   187
            colors.Add(sensorNode.Sensor,
moel@1
   188
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   189
            selected.Add(sensorNode.Sensor);
moel@1
   190
          }
moel@1
   191
          colorIndex++;
moel@1
   192
        }
moel@1
   193
      }
moel@1
   194
      sensorPlotColors = colors;
moel@1
   195
      plotPanel.SetSensors(selected, colors);
moel@1
   196
    }
moel@1
   197
moel@1
   198
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   199
      NodeControlValueEventArgs e) {
moel@1
   200
      SensorNode node = e.Node.Tag as SensorNode;
moel@1
   201
      e.Value = (node != null) && 
moel@1
   202
        (node.Sensor.SensorType == SensorType.Temperature) && 
moel@1
   203
        plotMenuItem.Checked;
moel@1
   204
    }
moel@1
   205
moel@1
   206
    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
moel@1
   207
      Close();
moel@1
   208
    }
moel@1
   209
moel@1
   210
    private void timer_Tick(object sender, EventArgs e) {
moel@1
   211
      
moel@1
   212
      #if !DEBUG
moel@1
   213
      try {
moel@1
   214
      #endif
moel@1
   215
        foreach (IGroup group in groupList)
moel@1
   216
          foreach (IHardware hardware in group.Hardware)
moel@1
   217
            hardware.Update();
moel@1
   218
      #if !DEBUG
moel@1
   219
      } catch (Exception exception) {
moel@1
   220
        Utilities.CrashReport.Save(exception);
moel@1
   221
        Close();
moel@1
   222
      }
moel@1
   223
      #endif
moel@1
   224
            
moel@1
   225
      treeView.Invalidate();
moel@1
   226
      plotPanel.Invalidate();
moel@1
   227
    }
moel@1
   228
moel@1
   229
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@1
   230
      Utilities.Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
moel@1
   231
      Utilities.Config.Set(minMenuItem.Name, minMenuItem.Checked);
moel@1
   232
      Utilities.Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
moel@1
   233
      Utilities.Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
moel@1
   234
      Utilities.Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
moel@1
   235
moel@1
   236
      Utilities.Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
moel@1
   237
      Utilities.Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
moel@1
   238
      Utilities.Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
moel@1
   239
      Utilities.Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
moel@1
   240
moel@14
   241
      if (WindowState != FormWindowState.Minimized) {
moel@14
   242
        Utilities.Config.Set("mainForm.Location.X", Location.X);
moel@14
   243
        Utilities.Config.Set("mainForm.Location.Y", Location.Y);
moel@14
   244
        Utilities.Config.Set("mainForm.Width", Width);
moel@14
   245
        Utilities.Config.Set("mainForm.Height", Height);
moel@14
   246
      }
moel@1
   247
moel@1
   248
      foreach (IGroup group in groupList)
moel@1
   249
        group.Close();
moel@1
   250
    }
moel@1
   251
moel@1
   252
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
moel@1
   253
      new AboutBox().ShowDialog();
moel@1
   254
    }
moel@1
   255
moel@1
   256
    private void plotToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   257
      EventArgs e) 
moel@1
   258
    {
moel@1
   259
      splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
moel@1
   260
      treeView.Invalidate();
moel@1
   261
    }
moel@1
   262
moel@1
   263
    private void valueToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   264
      EventArgs e) 
moel@1
   265
    {
moel@1
   266
      treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
moel@1
   267
    }
moel@1
   268
moel@1
   269
    private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
moel@1
   270
    {
moel@1
   271
      treeView.Columns[2].IsVisible = minMenuItem.Checked;
moel@1
   272
    }
moel@1
   273
moel@1
   274
    private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
moel@1
   275
    {
moel@1
   276
      treeView.Columns[3].IsVisible = maxMenuItem.Checked;
moel@1
   277
    }
moel@1
   278
moel@1
   279
    private void limitToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   280
      EventArgs e) {
moel@1
   281
      treeView.Columns[4].IsVisible = limitMenuItem.Checked;
moel@1
   282
    }
moel@1
   283
moel@1
   284
    private void treeView_Click(object sender, EventArgs e) {
moel@1
   285
      
moel@1
   286
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   287
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   288
        return;
moel@1
   289
moel@1
   290
      NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
moel@1
   291
      if (info.Control == null)
moel@1
   292
        columnsContextMenuStrip.Show(treeView, m.X, m.Y);
moel@1
   293
    }
moel@1
   294
moel@1
   295
    private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
moel@1
   296
      ReportWriter.Save(groupList, new Version(Application.ProductVersion));
moel@1
   297
    }
moel@1
   298
moel@1
   299
    private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   300
      EventArgs e) 
moel@1
   301
    {
moel@1
   302
      if (hddMenuItem.Checked) {
moel@1
   303
        AddGroup(new Hardware.HDD.HDDGroup());
moel@1
   304
        UpdateSensorTypeChecked(null, null);
moel@1
   305
      } else {
moel@1
   306
        List<IGroup> groupsToRemove = new List<IGroup>();
moel@1
   307
        foreach (IGroup group in groupList) 
moel@1
   308
          if (group is Hardware.HDD.HDDGroup)
moel@1
   309
            groupsToRemove.Add(group);
moel@1
   310
        foreach (IGroup group in groupsToRemove) {
moel@1
   311
          group.Close();
moel@1
   312
          RemoveGroup(group);
moel@1
   313
        }
moel@1
   314
        UpdatePlotSelection(null, null);        
moel@1
   315
      }
moel@1
   316
    }
moel@1
   317
moel@1
   318
    private void UpdateSensorTypeChecked(object sender, EventArgs e) {
moel@1
   319
      foreach (HardwareNode node in root.Nodes) {
moel@1
   320
        node.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
moel@1
   321
        node.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
moel@1
   322
        node.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
moel@1
   323
        node.SetVisible(SensorType.Fan, fansMenuItem.Checked);
moel@1
   324
      }
moel@1
   325
    }  
moel@1
   326
  }
moel@1
   327
}