GUI/MainForm.cs
author moel.mich
Tue, 09 Feb 2010 19:42:33 +0000
changeset 38 0e09d845eb00
parent 28 9b205b2ab056
child 40 2392f7402fb6
permissions -rw-r--r--
Added support for NVIDIA fan rpm. Changed NVIDIA GPU enumeration.
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@28
    48
using OpenHardwareMonitor.Utilities;
moel@1
    49
moel@1
    50
namespace OpenHardwareMonitor.GUI {
moel@1
    51
  public partial class MainForm : Form {
moel@1
    52
moel@28
    53
    private Computer computer = new Computer();
moel@1
    54
    private Node root;
moel@1
    55
    private TreeModel treeModel;
moel@1
    56
    private IDictionary<ISensor, Color> sensorPlotColors = 
moel@1
    57
      new Dictionary<ISensor, Color>();
moel@1
    58
    private Color[] plotColorPalette;
moel@1
    59
moel@28
    60
    public MainForm() {      
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@28
   102
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@28
   103
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
moel@28
   104
      computer.Open();
moel@28
   105
moel@1
   106
      plotColorPalette = new Color[14];
moel@1
   107
      plotColorPalette[0] = Color.Blue;
moel@1
   108
      plotColorPalette[1] = Color.OrangeRed;
moel@1
   109
      plotColorPalette[2] = Color.Green;
moel@1
   110
      plotColorPalette[3] = Color.LightSeaGreen;
moel@1
   111
      plotColorPalette[4] = Color.Goldenrod;
moel@1
   112
      plotColorPalette[5] = Color.DarkViolet;
moel@1
   113
      plotColorPalette[6] = Color.YellowGreen;
moel@1
   114
      plotColorPalette[7] = Color.SaddleBrown;
moel@1
   115
      plotColorPalette[8] = Color.Gray;
moel@1
   116
      plotColorPalette[9] = Color.RoyalBlue;
moel@1
   117
      plotColorPalette[10] = Color.DeepPink;
moel@1
   118
      plotColorPalette[11] = Color.MediumSeaGreen;
moel@1
   119
      plotColorPalette[12] = Color.Olive;
moel@1
   120
      plotColorPalette[13] = Color.Firebrick;
moel@1
   121
moel@28
   122
      plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
moel@28
   123
      minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
moel@28
   124
      maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
moel@28
   125
      limitMenuItem.Checked = Config.Get(limitMenuItem.Name, false);
moel@1
   126
moel@28
   127
      minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
moel@28
   128
      hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
moel@1
   129
moel@28
   130
      voltMenuItem.Checked = Config.Get(voltMenuItem.Name, true);
moel@28
   131
      clocksMenuItem.Checked = Config.Get(clocksMenuItem.Name, true);
moel@28
   132
      loadMenuItem.Checked = Config.Get(loadMenuItem.Name, true);
moel@28
   133
      tempMenuItem.Checked = Config.Get(tempMenuItem.Name, true);
moel@28
   134
      fansMenuItem.Checked = Config.Get(fansMenuItem.Name, true);
moel@28
   135
     
moel@28
   136
      timer.Enabled = true;   
moel@1
   137
    }
moel@1
   138
moel@28
   139
    private void HardwareAdded(IHardware hardware) {
moel@28
   140
      root.Nodes.Add(new HardwareNode(hardware));
moel@1
   141
    }
moel@1
   142
moel@28
   143
    private void HardwareRemoved(IHardware hardware) {      
moel@1
   144
      List<Node> nodesToRemove = new List<Node>();
moel@28
   145
      foreach (Node node in root.Nodes) {
moel@28
   146
        HardwareNode hardwareNode = node as HardwareNode;
moel@28
   147
        if (hardwareNode != null && hardwareNode.Hardware == hardware)
moel@28
   148
          nodesToRemove.Add(node);
moel@28
   149
      }
moel@1
   150
      foreach (Node node in nodesToRemove)
moel@1
   151
        root.Nodes.Remove(node);
moel@1
   152
    }
moel@1
   153
moel@1
   154
    private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
moel@1
   155
      SensorNode sensorNode = e.Node.Tag as SensorNode;
moel@1
   156
      if (sensorNode != null) 
moel@1
   157
        e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
moel@1
   158
    }
moel@1
   159
moel@1
   160
    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
moel@1
   161
      if (!plotMenuItem.Checked)
moel@1
   162
        return;      
moel@1
   163
moel@1
   164
      SensorNode sensorNode = e.Node.Tag as SensorNode;
moel@1
   165
      if (sensorNode != null) {
moel@1
   166
        Color color;
moel@1
   167
        if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color)) 
moel@1
   168
          e.TextColor = color;        
moel@1
   169
      }
moel@1
   170
    }
moel@1
   171
moel@1
   172
    private void UpdatePlotSelection(object sender, 
moel@1
   173
      TreePathEventArgs e) 
moel@1
   174
    {
moel@1
   175
      List<ISensor> selected = new List<ISensor>();
moel@1
   176
      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
moel@1
   177
      int colorIndex = 0;
moel@1
   178
      foreach (TreeNodeAdv node in treeView.AllNodes) {
moel@1
   179
        SensorNode sensorNode = node.Tag as SensorNode;
moel@1
   180
        if (sensorNode != null && 
moel@1
   181
          sensorNode.Sensor.SensorType == SensorType.Temperature) {
moel@1
   182
          if (sensorNode.Plot) {
moel@1
   183
            colors.Add(sensorNode.Sensor,
moel@1
   184
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   185
            selected.Add(sensorNode.Sensor);
moel@1
   186
          }
moel@1
   187
          colorIndex++;
moel@1
   188
        }
moel@1
   189
      }
moel@1
   190
      sensorPlotColors = colors;
moel@1
   191
      plotPanel.SetSensors(selected, colors);
moel@1
   192
    }
moel@1
   193
moel@1
   194
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   195
      NodeControlValueEventArgs e) {
moel@1
   196
      SensorNode node = e.Node.Tag as SensorNode;
moel@1
   197
      e.Value = (node != null) && 
moel@1
   198
        (node.Sensor.SensorType == SensorType.Temperature) && 
moel@1
   199
        plotMenuItem.Checked;
moel@1
   200
    }
moel@1
   201
moel@1
   202
    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
moel@1
   203
      Close();
moel@1
   204
    }
moel@1
   205
moel@1
   206
    private void timer_Tick(object sender, EventArgs e) {
moel@1
   207
      
moel@1
   208
      #if !DEBUG
moel@1
   209
      try {
moel@1
   210
      #endif
moel@28
   211
        computer.Update();        
moel@1
   212
      #if !DEBUG
moel@1
   213
      } catch (Exception exception) {
moel@28
   214
        CrashReport.Save(exception);
moel@1
   215
        Close();
moel@1
   216
      }
moel@1
   217
      #endif
moel@1
   218
            
moel@1
   219
      treeView.Invalidate();
moel@1
   220
      plotPanel.Invalidate();
moel@1
   221
    }
moel@1
   222
moel@1
   223
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@28
   224
            
moel@28
   225
      Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
moel@28
   226
      Config.Set(minMenuItem.Name, minMenuItem.Checked);
moel@28
   227
      Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
moel@28
   228
      Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
moel@1
   229
moel@28
   230
      Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
moel@28
   231
      Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
moel@28
   232
moel@28
   233
      Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
moel@28
   234
      Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
moel@28
   235
      Config.Set(loadMenuItem.Name, loadMenuItem.Checked);
moel@28
   236
      Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
moel@28
   237
      Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
moel@1
   238
moel@14
   239
      if (WindowState != FormWindowState.Minimized) {
moel@28
   240
        Config.Set("mainForm.Location.X", Location.X);
moel@28
   241
        Config.Set("mainForm.Location.Y", Location.Y);
moel@28
   242
        Config.Set("mainForm.Width", Width);
moel@28
   243
        Config.Set("mainForm.Height", Height);
moel@14
   244
      }
moel@1
   245
moel@28
   246
      computer.Close();
moel@1
   247
    }
moel@1
   248
moel@1
   249
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
moel@1
   250
      new AboutBox().ShowDialog();
moel@1
   251
    }
moel@1
   252
moel@1
   253
    private void plotToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   254
      EventArgs e) 
moel@1
   255
    {
moel@1
   256
      splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
moel@1
   257
      treeView.Invalidate();
moel@1
   258
    }
moel@1
   259
moel@1
   260
    private void valueToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   261
      EventArgs e) 
moel@1
   262
    {
moel@1
   263
      treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
moel@1
   264
    }
moel@1
   265
moel@1
   266
    private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
moel@1
   267
    {
moel@1
   268
      treeView.Columns[2].IsVisible = minMenuItem.Checked;
moel@1
   269
    }
moel@1
   270
moel@1
   271
    private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
moel@1
   272
    {
moel@1
   273
      treeView.Columns[3].IsVisible = maxMenuItem.Checked;
moel@1
   274
    }
moel@1
   275
moel@1
   276
    private void limitToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   277
      EventArgs e) {
moel@1
   278
      treeView.Columns[4].IsVisible = limitMenuItem.Checked;
moel@1
   279
    }
moel@1
   280
moel@1
   281
    private void treeView_Click(object sender, EventArgs e) {
moel@1
   282
      
moel@1
   283
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   284
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   285
        return;
moel@1
   286
moel@1
   287
      NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
moel@28
   288
      if (info.Control == null) {
moel@1
   289
        columnsContextMenuStrip.Show(treeView, m.X, m.Y);
moel@28
   290
      } 
moel@1
   291
    }
moel@1
   292
moel@1
   293
    private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
moel@28
   294
      computer.SaveReport(new Version(Application.ProductVersion));      
moel@1
   295
    }
moel@1
   296
moel@1
   297
    private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   298
      EventArgs e) 
moel@1
   299
    {
moel@28
   300
      computer.HDDEnabled = hddMenuItem.Checked;
moel@28
   301
      UpdateSensorTypeChecked(null, null);
moel@28
   302
      UpdatePlotSelection(null, null);      
moel@1
   303
    }
moel@1
   304
moel@1
   305
    private void UpdateSensorTypeChecked(object sender, EventArgs e) {
moel@1
   306
      foreach (HardwareNode node in root.Nodes) {
moel@1
   307
        node.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
moel@1
   308
        node.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
moel@24
   309
        node.SetVisible(SensorType.Load, loadMenuItem.Checked);
moel@1
   310
        node.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
moel@1
   311
        node.SetVisible(SensorType.Fan, fansMenuItem.Checked);
moel@1
   312
      }
moel@27
   313
    }
moel@27
   314
moel@27
   315
    private void ToggleSysTray() {
moel@27
   316
      if (Visible) {
moel@27
   317
        notifyIcon.Visible = true;
moel@27
   318
        Visible = false;        
moel@27
   319
      } else {
moel@27
   320
        Visible = true;
moel@27
   321
        notifyIcon.Visible = false;
moel@34
   322
        Activate();
moel@27
   323
      }
moel@27
   324
    }
moel@27
   325
moel@27
   326
    protected override void WndProc(ref Message m) {
moel@27
   327
      const int WM_SYSCOMMAND = 0x112;
moel@27
   328
      const int SC_MINIMIZE = 0xF020;
moel@28
   329
      if (minTrayMenuItem.Checked && 
moel@28
   330
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
moel@27
   331
        ToggleSysTray();
moel@27
   332
      } else {      
moel@27
   333
        base.WndProc(ref m);
moel@27
   334
      }
moel@27
   335
    }
moel@27
   336
moel@28
   337
    private void restoreClick(object sender, EventArgs e) {
moel@28
   338
      ToggleSysTray();
moel@27
   339
    }
moel@27
   340
moel@1
   341
  }
moel@1
   342
}