GUI/MainForm.cs
author moel.mich
Sun, 14 Feb 2010 20:16:30 +0000
changeset 44 c150de283ca0
parent 40 2392f7402fb6
child 55 898935080fd6
permissions -rw-r--r--
Added core and bus clock support for Intel CPUs (Core 2).
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@40
    59
    private SensorSystemTray sensorSystemTray;
moel@40
    60
    private NotifyIcon notifyIcon;
moel@1
    61
moel@28
    62
    public MainForm() {      
moel@1
    63
      InitializeComponent();
moel@1
    64
      this.Font = SystemFonts.MessageBoxFont;
moel@1
    65
      treeView.Font = SystemFonts.MessageBoxFont;
moel@1
    66
      plotPanel.Font = SystemFonts.MessageBoxFont;      
moel@1
    67
      
moel@1
    68
      nodeCheckBox.IsVisibleValueNeeded += 
moel@1
    69
        new EventHandler<NodeControlValueEventArgs>(
moel@1
    70
          nodeCheckBox_IsVisibleValueNeeded);
moel@1
    71
      nodeCheckBox.CheckStateChanged += 
moel@1
    72
        new EventHandler<TreePathEventArgs>(UpdatePlotSelection);
moel@1
    73
      nodeTextBoxText.DrawText += 
moel@1
    74
        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
moel@1
    75
      nodeTextBoxValue.DrawText +=
moel@1
    76
        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
moel@1
    77
      nodeTextBoxMin.DrawText +=
moel@1
    78
        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
moel@1
    79
      nodeTextBoxMax.DrawText +=
moel@1
    80
        new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
moel@1
    81
      nodeTextBoxLimit.DrawText += 
moel@1
    82
        new EventHandler<DrawEventArgs>(nodeTextBoxLimit_DrawText);
moel@1
    83
moel@1
    84
      if (Utilities.Config.Contains("mainForm.Location.X")) {
moel@1
    85
        int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
moel@1
    86
        x = x < 0 ? 0 : x;
moel@1
    87
        int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
moel@1
    88
        y = y < 0 ? 0 : y;
moel@1
    89
        this.Location = new Point(x, y);
moel@1
    90
      } else {
moel@1
    91
        StartPosition = FormStartPosition.CenterScreen;
moel@1
    92
      }
moel@1
    93
moel@1
    94
      Width = Utilities.Config.Get("mainForm.Width", Width);
moel@1
    95
      Height = Utilities.Config.Get("mainForm.Height", Height);
moel@1
    96
         
moel@1
    97
      treeModel = new TreeModel();
moel@1
    98
      root = new Node(System.Environment.MachineName);
moel@1
    99
      root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
moel@1
   100
      
moel@1
   101
      treeModel.Nodes.Add(root);
moel@1
   102
      treeView.Model = treeModel;
moel@40
   103
      
moel@40
   104
      notifyIcon = new NotifyIcon();
moel@40
   105
      notifyIcon.ContextMenuStrip = this.notifyContextMenuStrip;
moel@40
   106
      notifyIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
moel@40
   107
      notifyIcon.Text = "Open Hardware Monitor";      
moel@40
   108
      notifyIcon.DoubleClick += new EventHandler(this.restoreClick);
moel@40
   109
moel@40
   110
      sensorSystemTray = new SensorSystemTray(computer);
moel@1
   111
moel@28
   112
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@28
   113
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
moel@28
   114
      computer.Open();
moel@28
   115
moel@1
   116
      plotColorPalette = new Color[14];
moel@1
   117
      plotColorPalette[0] = Color.Blue;
moel@1
   118
      plotColorPalette[1] = Color.OrangeRed;
moel@1
   119
      plotColorPalette[2] = Color.Green;
moel@1
   120
      plotColorPalette[3] = Color.LightSeaGreen;
moel@1
   121
      plotColorPalette[4] = Color.Goldenrod;
moel@1
   122
      plotColorPalette[5] = Color.DarkViolet;
moel@1
   123
      plotColorPalette[6] = Color.YellowGreen;
moel@1
   124
      plotColorPalette[7] = Color.SaddleBrown;
moel@1
   125
      plotColorPalette[8] = Color.Gray;
moel@1
   126
      plotColorPalette[9] = Color.RoyalBlue;
moel@1
   127
      plotColorPalette[10] = Color.DeepPink;
moel@1
   128
      plotColorPalette[11] = Color.MediumSeaGreen;
moel@1
   129
      plotColorPalette[12] = Color.Olive;
moel@1
   130
      plotColorPalette[13] = Color.Firebrick;
moel@1
   131
moel@28
   132
      plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
moel@28
   133
      minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
moel@28
   134
      maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
moel@28
   135
      limitMenuItem.Checked = Config.Get(limitMenuItem.Name, false);
moel@1
   136
moel@28
   137
      minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
moel@28
   138
      hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
moel@1
   139
moel@28
   140
      voltMenuItem.Checked = Config.Get(voltMenuItem.Name, true);
moel@28
   141
      clocksMenuItem.Checked = Config.Get(clocksMenuItem.Name, true);
moel@28
   142
      loadMenuItem.Checked = Config.Get(loadMenuItem.Name, true);
moel@28
   143
      tempMenuItem.Checked = Config.Get(tempMenuItem.Name, true);
moel@28
   144
      fansMenuItem.Checked = Config.Get(fansMenuItem.Name, true);
moel@28
   145
     
moel@28
   146
      timer.Enabled = true;   
moel@1
   147
    }
moel@1
   148
moel@28
   149
    private void HardwareAdded(IHardware hardware) {
moel@28
   150
      root.Nodes.Add(new HardwareNode(hardware));
moel@1
   151
    }
moel@1
   152
moel@28
   153
    private void HardwareRemoved(IHardware hardware) {      
moel@1
   154
      List<Node> nodesToRemove = new List<Node>();
moel@28
   155
      foreach (Node node in root.Nodes) {
moel@28
   156
        HardwareNode hardwareNode = node as HardwareNode;
moel@28
   157
        if (hardwareNode != null && hardwareNode.Hardware == hardware)
moel@28
   158
          nodesToRemove.Add(node);
moel@28
   159
      }
moel@1
   160
      foreach (Node node in nodesToRemove)
moel@1
   161
        root.Nodes.Remove(node);
moel@1
   162
    }
moel@1
   163
moel@1
   164
    private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
moel@1
   165
      SensorNode sensorNode = e.Node.Tag as SensorNode;
moel@1
   166
      if (sensorNode != null) 
moel@1
   167
        e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
moel@1
   168
    }
moel@1
   169
moel@1
   170
    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
moel@1
   171
      if (!plotMenuItem.Checked)
moel@1
   172
        return;      
moel@1
   173
moel@1
   174
      SensorNode sensorNode = e.Node.Tag as SensorNode;
moel@1
   175
      if (sensorNode != null) {
moel@1
   176
        Color color;
moel@1
   177
        if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color)) 
moel@1
   178
          e.TextColor = color;        
moel@1
   179
      }
moel@1
   180
    }
moel@1
   181
moel@1
   182
    private void UpdatePlotSelection(object sender, 
moel@1
   183
      TreePathEventArgs e) 
moel@1
   184
    {
moel@1
   185
      List<ISensor> selected = new List<ISensor>();
moel@1
   186
      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
moel@1
   187
      int colorIndex = 0;
moel@1
   188
      foreach (TreeNodeAdv node in treeView.AllNodes) {
moel@1
   189
        SensorNode sensorNode = node.Tag as SensorNode;
moel@1
   190
        if (sensorNode != null && 
moel@1
   191
          sensorNode.Sensor.SensorType == SensorType.Temperature) {
moel@1
   192
          if (sensorNode.Plot) {
moel@1
   193
            colors.Add(sensorNode.Sensor,
moel@1
   194
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   195
            selected.Add(sensorNode.Sensor);
moel@1
   196
          }
moel@1
   197
          colorIndex++;
moel@1
   198
        }
moel@1
   199
      }
moel@1
   200
      sensorPlotColors = colors;
moel@1
   201
      plotPanel.SetSensors(selected, colors);
moel@1
   202
    }
moel@1
   203
moel@1
   204
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   205
      NodeControlValueEventArgs e) {
moel@1
   206
      SensorNode node = e.Node.Tag as SensorNode;
moel@1
   207
      e.Value = (node != null) && 
moel@1
   208
        (node.Sensor.SensorType == SensorType.Temperature) && 
moel@1
   209
        plotMenuItem.Checked;
moel@1
   210
    }
moel@1
   211
moel@1
   212
    private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
moel@1
   213
      Close();
moel@1
   214
    }
moel@1
   215
moel@1
   216
    private void timer_Tick(object sender, EventArgs e) {
moel@1
   217
      
moel@1
   218
      #if !DEBUG
moel@1
   219
      try {
moel@1
   220
      #endif
moel@28
   221
        computer.Update();        
moel@1
   222
      #if !DEBUG
moel@1
   223
      } catch (Exception exception) {
moel@28
   224
        CrashReport.Save(exception);
moel@1
   225
        Close();
moel@1
   226
      }
moel@1
   227
      #endif
moel@1
   228
            
moel@1
   229
      treeView.Invalidate();
moel@1
   230
      plotPanel.Invalidate();
moel@40
   231
      sensorSystemTray.Redraw();
moel@1
   232
    }
moel@1
   233
moel@1
   234
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@28
   235
            
moel@28
   236
      Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
moel@28
   237
      Config.Set(minMenuItem.Name, minMenuItem.Checked);
moel@28
   238
      Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
moel@28
   239
      Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
moel@1
   240
moel@28
   241
      Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
moel@28
   242
      Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
moel@28
   243
moel@28
   244
      Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
moel@28
   245
      Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
moel@28
   246
      Config.Set(loadMenuItem.Name, loadMenuItem.Checked);
moel@28
   247
      Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
moel@28
   248
      Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
moel@1
   249
moel@14
   250
      if (WindowState != FormWindowState.Minimized) {
moel@28
   251
        Config.Set("mainForm.Location.X", Location.X);
moel@28
   252
        Config.Set("mainForm.Location.Y", Location.Y);
moel@28
   253
        Config.Set("mainForm.Width", Width);
moel@28
   254
        Config.Set("mainForm.Height", Height);
moel@14
   255
      }
moel@42
   256
           
moel@40
   257
      sensorSystemTray.Dispose();
moel@40
   258
      notifyIcon.Dispose();
moel@28
   259
      computer.Close();
moel@1
   260
    }
moel@1
   261
moel@1
   262
    private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
moel@1
   263
      new AboutBox().ShowDialog();
moel@1
   264
    }
moel@1
   265
moel@1
   266
    private void plotToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   267
      EventArgs e) 
moel@1
   268
    {
moel@1
   269
      splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
moel@1
   270
      treeView.Invalidate();
moel@1
   271
    }
moel@1
   272
moel@1
   273
    private void valueToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   274
      EventArgs e) 
moel@1
   275
    {
moel@1
   276
      treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
moel@1
   277
    }
moel@1
   278
moel@1
   279
    private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
moel@1
   280
    {
moel@1
   281
      treeView.Columns[2].IsVisible = minMenuItem.Checked;
moel@1
   282
    }
moel@1
   283
moel@1
   284
    private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 
moel@1
   285
    {
moel@1
   286
      treeView.Columns[3].IsVisible = maxMenuItem.Checked;
moel@1
   287
    }
moel@1
   288
moel@1
   289
    private void limitToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   290
      EventArgs e) {
moel@1
   291
      treeView.Columns[4].IsVisible = limitMenuItem.Checked;
moel@1
   292
    }
moel@1
   293
moel@1
   294
    private void treeView_Click(object sender, EventArgs e) {
moel@1
   295
      
moel@1
   296
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   297
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   298
        return;
moel@1
   299
moel@1
   300
      NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
moel@28
   301
      if (info.Control == null) {
moel@1
   302
        columnsContextMenuStrip.Show(treeView, m.X, m.Y);
moel@40
   303
      } else {
moel@40
   304
        SensorNode node = info.Node.Tag as SensorNode;
moel@40
   305
        if (node != null && node.Sensor != null) {
moel@40
   306
moel@40
   307
          sensorContextMenuStrip.Items.Clear();
moel@40
   308
          if (sensorSystemTray.Contains(node.Sensor)) {
moel@40
   309
            ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
moel@40
   310
            item.Click += delegate(object obj, EventArgs args) {
moel@40
   311
              sensorSystemTray.Remove(node.Sensor);
moel@40
   312
            };
moel@40
   313
            sensorContextMenuStrip.Items.Add(item);
moel@40
   314
          } else {
moel@40
   315
            ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
moel@40
   316
            item.Click += delegate(object obj, EventArgs args) {
moel@42
   317
              sensorSystemTray.Add(node.Sensor, true);
moel@40
   318
            };
moel@40
   319
            sensorContextMenuStrip.Items.Add(item);
moel@40
   320
          }
moel@40
   321
          sensorContextMenuStrip.Show(treeView, m.X, m.Y);
moel@40
   322
        }
moel@40
   323
      }
moel@1
   324
    }
moel@1
   325
moel@1
   326
    private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
moel@28
   327
      computer.SaveReport(new Version(Application.ProductVersion));      
moel@1
   328
    }
moel@1
   329
moel@1
   330
    private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, 
moel@1
   331
      EventArgs e) 
moel@1
   332
    {
moel@28
   333
      computer.HDDEnabled = hddMenuItem.Checked;
moel@28
   334
      UpdateSensorTypeChecked(null, null);
moel@28
   335
      UpdatePlotSelection(null, null);      
moel@1
   336
    }
moel@1
   337
moel@1
   338
    private void UpdateSensorTypeChecked(object sender, EventArgs e) {
moel@1
   339
      foreach (HardwareNode node in root.Nodes) {
moel@1
   340
        node.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
moel@1
   341
        node.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
moel@24
   342
        node.SetVisible(SensorType.Load, loadMenuItem.Checked);
moel@1
   343
        node.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
moel@1
   344
        node.SetVisible(SensorType.Fan, fansMenuItem.Checked);
moel@1
   345
      }
moel@27
   346
    }
moel@27
   347
moel@27
   348
    private void ToggleSysTray() {
moel@27
   349
      if (Visible) {
moel@27
   350
        notifyIcon.Visible = true;
moel@27
   351
        Visible = false;        
moel@27
   352
      } else {
moel@27
   353
        Visible = true;
moel@27
   354
        notifyIcon.Visible = false;
moel@34
   355
        Activate();
moel@27
   356
      }
moel@27
   357
    }
moel@27
   358
moel@27
   359
    protected override void WndProc(ref Message m) {
moel@27
   360
      const int WM_SYSCOMMAND = 0x112;
moel@27
   361
      const int SC_MINIMIZE = 0xF020;
moel@28
   362
      if (minTrayMenuItem.Checked && 
moel@28
   363
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
moel@27
   364
        ToggleSysTray();
moel@27
   365
      } else {      
moel@27
   366
        base.WndProc(ref m);
moel@27
   367
      }
moel@27
   368
    }
moel@27
   369
moel@28
   370
    private void restoreClick(object sender, EventArgs e) {
moel@28
   371
      ToggleSysTray();
moel@27
   372
    }
moel@27
   373
moel@40
   374
    private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
moel@40
   375
      ToolStripMenuItem item = sender as ToolStripMenuItem;
moel@40
   376
      if (item == null)
moel@40
   377
        return;
moel@40
   378
moel@40
   379
      ISensor sensor = item.Owner.Tag as ISensor;
moel@40
   380
      if (sensor == null)
moel@40
   381
        return;
moel@40
   382
moel@40
   383
      sensorSystemTray.Remove(sensor);
moel@40
   384
    }
moel@40
   385
moel@1
   386
  }
moel@1
   387
}