Fixed Issue 9.
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;
45 using System.Windows.Forms;
46 using Aga.Controls.Tree;
47 using Aga.Controls.Tree.NodeControls;
48 using OpenHardwareMonitor.Hardware;
49 using OpenHardwareMonitor.Utilities;
51 namespace OpenHardwareMonitor.GUI {
52 public partial class MainForm : Form {
54 private Computer computer = new Computer();
56 private TreeModel treeModel;
57 private IDictionary<ISensor, Color> sensorPlotColors =
58 new Dictionary<ISensor, Color>();
59 private Color[] plotColorPalette;
60 private SensorSystemTray sensorSystemTray;
61 private NotifyIcon notifyIcon;
62 private StartupManager startupManager = new StartupManager();
65 InitializeComponent();
66 this.Font = SystemFonts.MessageBoxFont;
67 treeView.Font = SystemFonts.MessageBoxFont;
68 plotPanel.Font = SystemFonts.MessageBoxFont;
70 nodeCheckBox.IsVisibleValueNeeded +=
71 new EventHandler<NodeControlValueEventArgs>(
72 nodeCheckBox_IsVisibleValueNeeded);
73 nodeCheckBox.CheckStateChanged +=
74 new EventHandler<TreePathEventArgs>(UpdatePlotSelection);
75 nodeTextBoxText.DrawText +=
76 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
77 nodeTextBoxValue.DrawText +=
78 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
79 nodeTextBoxMin.DrawText +=
80 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
81 nodeTextBoxMax.DrawText +=
82 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
83 nodeTextBoxLimit.DrawText +=
84 new EventHandler<DrawEventArgs>(nodeTextBoxLimit_DrawText);
86 if (Utilities.Config.Contains("mainForm.Location.X")) {
87 int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
89 int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
91 this.Location = new Point(x, y);
93 StartPosition = FormStartPosition.CenterScreen;
96 Width = Utilities.Config.Get("mainForm.Width", Width);
97 Height = Utilities.Config.Get("mainForm.Height", Height);
99 treeModel = new TreeModel();
100 root = new Node(System.Environment.MachineName);
101 root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
103 treeModel.Nodes.Add(root);
104 treeView.Model = treeModel;
106 notifyIcon = new NotifyIcon();
107 notifyIcon.ContextMenuStrip = this.notifyContextMenuStrip;
108 notifyIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
109 notifyIcon.Text = "Open Hardware Monitor";
110 notifyIcon.DoubleClick += new EventHandler(this.hideShowClick);
112 sensorSystemTray = new SensorSystemTray(computer);
114 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
115 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
116 computer.Updated += new UpdateEventHandler(Updated);
119 plotColorPalette = new Color[14];
120 plotColorPalette[0] = Color.Blue;
121 plotColorPalette[1] = Color.OrangeRed;
122 plotColorPalette[2] = Color.Green;
123 plotColorPalette[3] = Color.LightSeaGreen;
124 plotColorPalette[4] = Color.Goldenrod;
125 plotColorPalette[5] = Color.DarkViolet;
126 plotColorPalette[6] = Color.YellowGreen;
127 plotColorPalette[7] = Color.SaddleBrown;
128 plotColorPalette[8] = Color.Gray;
129 plotColorPalette[9] = Color.RoyalBlue;
130 plotColorPalette[10] = Color.DeepPink;
131 plotColorPalette[11] = Color.MediumSeaGreen;
132 plotColorPalette[12] = Color.Olive;
133 plotColorPalette[13] = Color.Firebrick;
135 plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
136 minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
137 maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
138 limitMenuItem.Checked = Config.Get(limitMenuItem.Name, false);
140 startMinMenuItem.Checked = Config.Get(startMinMenuItem.Name, false);
141 minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
142 startupMenuItem.Checked = startupManager.Startup;
143 hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
145 voltMenuItem.Checked = Config.Get(voltMenuItem.Name, true);
146 clocksMenuItem.Checked = Config.Get(clocksMenuItem.Name, true);
147 loadMenuItem.Checked = Config.Get(loadMenuItem.Name, true);
148 tempMenuItem.Checked = Config.Get(tempMenuItem.Name, true);
149 fansMenuItem.Checked = Config.Get(fansMenuItem.Name, true);
150 flowsMenuItem.Checked = Config.Get(flowsMenuItem.Name, true);
152 if (startMinMenuItem.Checked) {
153 if (!minTrayMenuItem.Checked) {
154 WindowState = FormWindowState.Minimized;
161 // Create a handle, otherwise calling Close() does not fire FormClosed
162 IntPtr handle = Handle;
165 private void SubHardwareAdded(IHardware hardware, Node node) {
166 Node hardwareNode = new HardwareNode(hardware);
167 node.Nodes.Add(hardwareNode);
168 foreach (IHardware subHardware in hardware.SubHardware)
169 SubHardwareAdded(subHardware, hardwareNode);
172 private void HardwareAdded(IHardware hardware) {
173 Node hardwareNode = new HardwareNode(hardware);
174 root.Nodes.Add(hardwareNode);
175 foreach (IHardware subHardware in hardware.SubHardware)
176 SubHardwareAdded(subHardware, hardwareNode);
179 private void HardwareRemoved(IHardware hardware) {
180 List<Node> nodesToRemove = new List<Node>();
181 foreach (Node node in root.Nodes) {
182 HardwareNode hardwareNode = node as HardwareNode;
183 if (hardwareNode != null && hardwareNode.Hardware == hardware)
184 nodesToRemove.Add(node);
186 foreach (Node node in nodesToRemove)
187 root.Nodes.Remove(node);
190 private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
191 SensorNode sensorNode = e.Node.Tag as SensorNode;
192 if (sensorNode != null)
193 e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
196 private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
197 if (!plotMenuItem.Checked)
200 SensorNode sensorNode = e.Node.Tag as SensorNode;
201 if (sensorNode != null) {
203 if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
208 private void UpdatePlotSelection(object sender,
211 List<ISensor> selected = new List<ISensor>();
212 IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
214 foreach (TreeNodeAdv node in treeView.AllNodes) {
215 SensorNode sensorNode = node.Tag as SensorNode;
216 if (sensorNode != null &&
217 sensorNode.Sensor.SensorType == SensorType.Temperature) {
218 if (sensorNode.Plot) {
219 colors.Add(sensorNode.Sensor,
220 plotColorPalette[colorIndex % plotColorPalette.Length]);
221 selected.Add(sensorNode.Sensor);
226 sensorPlotColors = colors;
227 plotPanel.SetSensors(selected, colors);
230 private void nodeCheckBox_IsVisibleValueNeeded(object sender,
231 NodeControlValueEventArgs e) {
232 SensorNode node = e.Node.Tag as SensorNode;
233 e.Value = (node != null) &&
234 (node.Sensor.SensorType == SensorType.Temperature) &&
235 plotMenuItem.Checked;
238 private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
242 private void Updated() {
243 treeView.Invalidate();
244 plotPanel.Invalidate();
245 sensorSystemTray.Redraw();
248 private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
250 Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
251 Config.Set(minMenuItem.Name, minMenuItem.Checked);
252 Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
253 Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
255 Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked);
256 Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
257 Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
259 Config.Set(voltMenuItem.Name, voltMenuItem.Checked);
260 Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked);
261 Config.Set(loadMenuItem.Name, loadMenuItem.Checked);
262 Config.Set(tempMenuItem.Name, tempMenuItem.Checked);
263 Config.Set(fansMenuItem.Name, fansMenuItem.Checked);
264 Config.Set(flowsMenuItem.Name, flowsMenuItem.Checked);
266 if (WindowState != FormWindowState.Minimized) {
267 Config.Set("mainForm.Location.X", Location.X);
268 Config.Set("mainForm.Location.Y", Location.Y);
269 Config.Set("mainForm.Width", Width);
270 Config.Set("mainForm.Height", Height);
273 sensorSystemTray.Dispose();
274 notifyIcon.Dispose();
278 private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
279 new AboutBox().ShowDialog();
282 private void plotToolStripMenuItem_CheckedChanged(object sender,
285 splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
286 treeView.Invalidate();
289 private void valueToolStripMenuItem_CheckedChanged(object sender,
292 treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked;
295 private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
297 treeView.Columns[2].IsVisible = minMenuItem.Checked;
300 private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
302 treeView.Columns[3].IsVisible = maxMenuItem.Checked;
305 private void limitToolStripMenuItem_CheckedChanged(object sender,
307 treeView.Columns[4].IsVisible = limitMenuItem.Checked;
310 private void treeView_Click(object sender, EventArgs e) {
312 MouseEventArgs m = e as MouseEventArgs;
313 if (m == null || m.Button != MouseButtons.Right)
316 NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
317 if (info.Control == null) {
318 columnsContextMenuStrip.Show(treeView, m.X, m.Y);
320 SensorNode node = info.Node.Tag as SensorNode;
321 if (node != null && node.Sensor != null) {
323 sensorContextMenuStrip.Items.Clear();
324 if (node.Sensor.Parameters.Length > 0) {
325 ToolStripMenuItem item = new ToolStripMenuItem("Parameters...");
326 item.Click += delegate(object obj, EventArgs args) {
327 ShowParameterForm(node.Sensor);
329 sensorContextMenuStrip.Items.Add(item);
331 if (sensorSystemTray.Contains(node.Sensor)) {
332 ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
333 item.Click += delegate(object obj, EventArgs args) {
334 sensorSystemTray.Remove(node.Sensor);
336 sensorContextMenuStrip.Items.Add(item);
338 ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
339 item.Click += delegate(object obj, EventArgs args) {
340 sensorSystemTray.Add(node.Sensor, true);
342 sensorContextMenuStrip.Items.Add(item);
344 sensorContextMenuStrip.Show(treeView, m.X, m.Y);
349 private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
350 string report = computer.GetReport();
351 if (saveFileDialog.ShowDialog() == DialogResult.OK) {
352 using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
358 private void hddsensorsToolStripMenuItem_CheckedChanged(object sender,
361 computer.HDDEnabled = hddMenuItem.Checked;
362 UpdateSensorTypeChecked(null, null);
363 UpdatePlotSelection(null, null);
366 private void UpdateSensorTypeVisible(Node node) {
367 HardwareNode hardwareNode = node as HardwareNode;
368 if (hardwareNode == null)
371 hardwareNode.SetVisible(SensorType.Voltage, voltMenuItem.Checked);
372 hardwareNode.SetVisible(SensorType.Clock, clocksMenuItem.Checked);
373 hardwareNode.SetVisible(SensorType.Load, loadMenuItem.Checked);
374 hardwareNode.SetVisible(SensorType.Temperature, tempMenuItem.Checked);
375 hardwareNode.SetVisible(SensorType.Fan, fansMenuItem.Checked);
376 hardwareNode.SetVisible(SensorType.Flow, flowsMenuItem.Checked);
378 foreach (Node n in node.Nodes)
379 UpdateSensorTypeVisible(n);
382 private void UpdateSensorTypeChecked(object sender, EventArgs e) {
383 foreach (HardwareNode node in root.Nodes)
384 UpdateSensorTypeVisible(node);
387 private void SysTrayHideShow() {
393 protected override void WndProc(ref Message m) {
394 const int WM_SYSCOMMAND = 0x112;
395 const int SC_MINIMIZE = 0xF020;
396 if (minTrayMenuItem.Checked &&
397 m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
404 private void hideShowClick(object sender, EventArgs e) {
408 private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
409 ToolStripMenuItem item = sender as ToolStripMenuItem;
413 ISensor sensor = item.Owner.Tag as ISensor;
417 sensorSystemTray.Remove(sensor);
420 private void ShowParameterForm(ISensor sensor) {
421 ParameterForm form = new ParameterForm();
422 form.Parameters = sensor.Parameters;
423 form.captionLabel.Text = sensor.Name;
427 private void treeView_NodeMouseDoubleClick(object sender,
428 TreeNodeAdvMouseEventArgs e) {
429 SensorNode node = e.Node.Tag as SensorNode;
430 if (node != null && node.Sensor != null &&
431 node.Sensor.Parameters.Length > 0) {
432 ShowParameterForm(node.Sensor);
436 private void runOnWindowsStartupToolStripMenuItem_CheckedChanged(
437 object sender, EventArgs e)
439 startupManager.Startup = startupMenuItem.Checked;
442 private void minTrayMenuItem_CheckedChanged(object sender, EventArgs e) {
443 notifyIcon.Visible = minTrayMenuItem.Checked;