Refactoring: New class Computer manages all the hardware and creates events.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
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
9 http://www.mozilla.org/MPL/
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.
15 The Original Code is the Open Hardware Monitor code.
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.
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.
39 using System.Collections.Generic;
40 using System.ComponentModel;
41 using System.Configuration;
44 using System.Windows.Forms;
45 using Aga.Controls.Tree;
46 using Aga.Controls.Tree.NodeControls;
47 using OpenHardwareMonitor.Hardware;
48 using OpenHardwareMonitor.Utilities;
50 namespace OpenHardwareMonitor.GUI {
51 public partial class MainForm : Form {
53 private Computer computer = new Computer();
55 private TreeModel treeModel;
56 private IDictionary<ISensor, Color> sensorPlotColors =
57 new Dictionary<ISensor, Color>();
58 private Color[] plotColorPalette;
61 InitializeComponent();
62 this.Font = SystemFonts.MessageBoxFont;
63 treeView.Font = SystemFonts.MessageBoxFont;
64 plotPanel.Font = SystemFonts.MessageBoxFont;
66 nodeCheckBox.IsVisibleValueNeeded +=
67 new EventHandler<NodeControlValueEventArgs>(
68 nodeCheckBox_IsVisibleValueNeeded);
69 nodeCheckBox.CheckStateChanged +=
70 new EventHandler<TreePathEventArgs>(UpdatePlotSelection);
71 nodeTextBoxText.DrawText +=
72 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
73 nodeTextBoxValue.DrawText +=
74 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
75 nodeTextBoxMin.DrawText +=
76 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
77 nodeTextBoxMax.DrawText +=
78 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
79 nodeTextBoxLimit.DrawText +=
80 new EventHandler<DrawEventArgs>(nodeTextBoxLimit_DrawText);
82 if (Utilities.Config.Contains("mainForm.Location.X")) {
83 int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
85 int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
87 this.Location = new Point(x, y);
89 StartPosition = FormStartPosition.CenterScreen;
92 Width = Utilities.Config.Get("mainForm.Width", Width);
93 Height = Utilities.Config.Get("mainForm.Height", Height);
95 treeModel = new TreeModel();
96 root = new Node(System.Environment.MachineName);
97 root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
99 treeModel.Nodes.Add(root);
100 treeView.Model = treeModel;
102 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
103 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
106 plotColorPalette = new Color[14];
107 plotColorPalette[0] = Color.Blue;
108 plotColorPalette[1] = Color.OrangeRed;
109 plotColorPalette[2] = Color.Green;
110 plotColorPalette[3] = Color.LightSeaGreen;
111 plotColorPalette[4] = Color.Goldenrod;
112 plotColorPalette[5] = Color.DarkViolet;
113 plotColorPalette[6] = Color.YellowGreen;
114 plotColorPalette[7] = Color.SaddleBrown;
115 plotColorPalette[8] = Color.Gray;
116 plotColorPalette[9] = Color.RoyalBlue;
117 plotColorPalette[10] = Color.DeepPink;
118 plotColorPalette[11] = Color.MediumSeaGreen;
119 plotColorPalette[12] = Color.Olive;
120 plotColorPalette[13] = Color.Firebrick;
122 plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
123 minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
124 maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
125 limitMenuItem.Checked = Config.Get(limitMenuItem.Name, false);
127 minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
128 hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
130 voltMenuItem.Checked = Config.Get(voltMenuItem.Name, true);
131 clocksMenuItem.Checked = Config.Get(clocksMenuItem.Name, true);
132 loadMenuItem.Checked = Config.Get(loadMenuItem.Name, true);
133 tempMenuItem.Checked = Config.Get(tempMenuItem.Name, true);
134 fansMenuItem.Checked = Config.Get(fansMenuItem.Name, true);
136 timer.Enabled = true;
139 private void HardwareAdded(IHardware hardware) {
140 root.Nodes.Add(new HardwareNode(hardware));
143 private void HardwareRemoved(IHardware hardware) {
144 List<Node> nodesToRemove = new List<Node>();
145 foreach (Node node in root.Nodes) {
146 HardwareNode hardwareNode = node as HardwareNode;
147 if (hardwareNode != null && hardwareNode.Hardware == hardware)
148 nodesToRemove.Add(node);
150 foreach (Node node in nodesToRemove)
151 root.Nodes.Remove(node);
154 private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
155 SensorNode sensorNode = e.Node.Tag as SensorNode;
156 if (sensorNode != null)
157 e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
160 private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
161 if (!plotMenuItem.Checked)
164 SensorNode sensorNode = e.Node.Tag as SensorNode;
165 if (sensorNode != null) {
167 if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
172 private void UpdatePlotSelection(object sender,
175 List<ISensor> selected = new List<ISensor>();
176 IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
178 foreach (TreeNodeAdv node in treeView.AllNodes) {
179 SensorNode sensorNode = node.Tag as SensorNode;
180 if (sensorNode != null &&
181 sensorNode.Sensor.SensorType == SensorType.Temperature) {
182 if (sensorNode.Plot) {
183 colors.Add(sensorNode.Sensor,
184 plotColorPalette[colorIndex % plotColorPalette.Length]);
185 selected.Add(sensorNode.Sensor);
190 sensorPlotColors = colors;
191 plotPanel.SetSensors(selected, colors);
194 private void nodeCheckBox_IsVisibleValueNeeded(object sender,
195 NodeControlValueEventArgs e) {
196 SensorNode node = e.Node.Tag as SensorNode;
197 e.Value = (node != null) &&
198 (node.Sensor.SensorType == SensorType.Temperature) &&
199 plotMenuItem.Checked;
202 private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
206 private void timer_Tick(object sender, EventArgs e) {
213 } catch (Exception exception) {
214 CrashReport.Save(exception);
219 treeView.Invalidate();
220 plotPanel.Invalidate();
223 private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
225 Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
226 Config.Set(minMenuItem.Name, minMenuItem.Checked);
227 Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
228 Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
230 Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
231 Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
233 Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
234 Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
235 Config.Set(loadMenuItem.Name, loadMenuItem.Checked);
236 Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
237 Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
239 if (WindowState != FormWindowState.Minimized) {
240 Config.Set("mainForm.Location.X", Location.X);
241 Config.Set("mainForm.Location.Y", Location.Y);
242 Config.Set("mainForm.Width", Width);
243 Config.Set("mainForm.Height", Height);
249 private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
250 new AboutBox().ShowDialog();
253 private void plotToolStripMenuItem_CheckedChanged(object sender,
256 splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
257 treeView.Invalidate();
260 private void valueToolStripMenuItem_CheckedChanged(object sender,
263 treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
266 private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
268 treeView.Columns[2].IsVisible = minMenuItem.Checked;
271 private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
273 treeView.Columns[3].IsVisible = maxMenuItem.Checked;
276 private void limitToolStripMenuItem_CheckedChanged(object sender,
278 treeView.Columns[4].IsVisible = limitMenuItem.Checked;
281 private void treeView_Click(object sender, EventArgs e) {
283 MouseEventArgs m = e as MouseEventArgs;
284 if (m == null || m.Button != MouseButtons.Right)
287 NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
288 if (info.Control == null) {
289 columnsContextMenuStrip.Show(treeView, m.X, m.Y);
293 private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
294 computer.SaveReport(new Version(Application.ProductVersion));
297 private void hddsensorsToolStripMenuItem_CheckedChanged(object sender,
300 computer.HDDEnabled = hddMenuItem.Checked;
301 UpdateSensorTypeChecked(null, null);
302 UpdatePlotSelection(null, null);
305 private void UpdateSensorTypeChecked(object sender, EventArgs e) {
306 foreach (HardwareNode node in root.Nodes) {
307 node.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
308 node.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
309 node.SetVisible(SensorType.Load, loadMenuItem.Checked);
310 node.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
311 node.SetVisible(SensorType.Fan, fansMenuItem.Checked);
315 private void ToggleSysTray() {
317 notifyIcon.Visible = true;
321 notifyIcon.Visible = false;
326 protected override void WndProc(ref Message m) {
327 const int WM_SYSCOMMAND = 0x112;
328 const int SC_MINIMIZE = 0xF020;
329 if (minTrayMenuItem.Checked &&
330 m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
337 private void restoreClick(object sender, EventArgs e) {