Fixed Issue 57.
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();
63 private UpdateVisitor updateVisitor = new UpdateVisitor();
66 InitializeComponent();
67 this.Font = SystemFonts.MessageBoxFont;
68 treeView.Font = SystemFonts.MessageBoxFont;
69 plotPanel.Font = SystemFonts.MessageBoxFont;
71 nodeCheckBox.IsVisibleValueNeeded +=
72 new EventHandler<NodeControlValueEventArgs>(
73 nodeCheckBox_IsVisibleValueNeeded);
74 nodeCheckBox.CheckStateChanged +=
75 new EventHandler<TreePathEventArgs>(UpdatePlotSelection);
76 nodeTextBoxText.DrawText +=
77 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
78 nodeTextBoxValue.DrawText +=
79 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
80 nodeTextBoxMin.DrawText +=
81 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
82 nodeTextBoxMax.DrawText +=
83 new EventHandler<DrawEventArgs>(nodeTextBoxText_DrawText);
84 nodeTextBoxLimit.DrawText +=
85 new EventHandler<DrawEventArgs>(nodeTextBoxLimit_DrawText);
87 if (Utilities.Config.Contains("mainForm.Location.X")) {
88 int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
90 int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
92 this.Location = new Point(x, y);
94 StartPosition = FormStartPosition.CenterScreen;
97 Width = Utilities.Config.Get("mainForm.Width", Width);
98 Height = Utilities.Config.Get("mainForm.Height", Height);
100 foreach (TreeColumn column in treeView.Columns)
101 column.Width = Math.Max(20, Math.Min(400,
102 Config.Get("treeView.Columns." + column.Header + ".Width",
105 treeModel = new TreeModel();
106 root = new Node(System.Environment.MachineName);
107 root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
109 treeModel.Nodes.Add(root);
110 treeView.Model = treeModel;
112 notifyIcon = new NotifyIcon();
113 notifyIcon.ContextMenuStrip = this.notifyContextMenuStrip;
114 notifyIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
115 notifyIcon.Text = "Open Hardware Monitor";
116 notifyIcon.DoubleClick += new EventHandler(this.hideShowClick);
118 sensorSystemTray = new SensorSystemTray(computer);
120 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
121 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
124 timer.Enabled = true;
126 plotColorPalette = new Color[13];
127 plotColorPalette[0] = Color.Blue;
128 plotColorPalette[1] = Color.OrangeRed;
129 plotColorPalette[2] = Color.Green;
130 plotColorPalette[3] = Color.LightSeaGreen;
131 plotColorPalette[4] = Color.Goldenrod;
132 plotColorPalette[5] = Color.DarkViolet;
133 plotColorPalette[6] = Color.YellowGreen;
134 plotColorPalette[7] = Color.SaddleBrown;
135 plotColorPalette[8] = Color.RoyalBlue;
136 plotColorPalette[9] = Color.DeepPink;
137 plotColorPalette[10] = Color.MediumSeaGreen;
138 plotColorPalette[11] = Color.Olive;
139 plotColorPalette[12] = Color.Firebrick;
141 hiddenMenuItem.Checked = Config.Get(hiddenMenuItem.Name, false);
142 plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
144 valueMenuItem.Checked = Config.Get(valueMenuItem.Name, true);
145 minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
146 maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
147 limitMenuItem.Checked = Config.Get(limitMenuItem.Name, false);
149 startMinMenuItem.Checked = Config.Get(startMinMenuItem.Name, false);
150 minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
151 startupMenuItem.Checked = startupManager.Startup;
152 hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
154 if (startMinMenuItem.Checked) {
155 if (!minTrayMenuItem.Checked) {
156 WindowState = FormWindowState.Minimized;
163 // Create a handle, otherwise calling Close() does not fire FormClosed
164 IntPtr handle = Handle;
167 private void SubHardwareAdded(IHardware hardware, Node node) {
168 Node hardwareNode = new HardwareNode(hardware);
169 node.Nodes.Add(hardwareNode);
170 foreach (IHardware subHardware in hardware.SubHardware)
171 SubHardwareAdded(subHardware, hardwareNode);
174 private void HardwareAdded(IHardware hardware) {
175 Node hardwareNode = new HardwareNode(hardware);
176 root.Nodes.Add(hardwareNode);
177 foreach (IHardware subHardware in hardware.SubHardware)
178 SubHardwareAdded(subHardware, hardwareNode);
181 private void HardwareRemoved(IHardware hardware) {
182 List<Node> nodesToRemove = new List<Node>();
183 foreach (Node node in root.Nodes) {
184 HardwareNode hardwareNode = node as HardwareNode;
185 if (hardwareNode != null && hardwareNode.Hardware == hardware)
186 nodesToRemove.Add(node);
188 foreach (Node node in nodesToRemove)
189 root.Nodes.Remove(node);
192 private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) {
193 SensorNode sensorNode = e.Node.Tag as SensorNode;
194 if (sensorNode != null)
195 e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit);
198 private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
199 Node node = e.Node.Tag as Node;
202 if (node.IsVisible) {
203 SensorNode sensorNode = node as SensorNode;
204 if (plotMenuItem.Checked && sensorNode != null &&
205 sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
208 e.TextColor = Color.DarkGray;
213 private void UpdatePlotSelection(object sender,
216 List<ISensor> selected = new List<ISensor>();
217 IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
219 foreach (TreeNodeAdv node in treeView.AllNodes) {
220 SensorNode sensorNode = node.Tag as SensorNode;
221 if (sensorNode != null &&
222 sensorNode.Sensor.SensorType == SensorType.Temperature) {
223 if (sensorNode.Plot) {
224 colors.Add(sensorNode.Sensor,
225 plotColorPalette[colorIndex % plotColorPalette.Length]);
226 selected.Add(sensorNode.Sensor);
231 sensorPlotColors = colors;
232 plotPanel.SetSensors(selected, colors);
235 private void nodeCheckBox_IsVisibleValueNeeded(object sender,
236 NodeControlValueEventArgs e) {
237 SensorNode node = e.Node.Tag as SensorNode;
238 e.Value = (node != null) &&
239 (node.Sensor.SensorType == SensorType.Temperature) &&
240 plotMenuItem.Checked;
243 private void exitToolStripMenuItem_Click(object sender, EventArgs e) {
247 private void timer_Tick(object sender, EventArgs e) {
248 computer.Accept(updateVisitor);
249 treeView.Invalidate();
250 plotPanel.Invalidate();
251 sensorSystemTray.Redraw();
254 private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
256 Config.Set(hiddenMenuItem.Name, hiddenMenuItem.Checked);
257 Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
259 Config.Set(valueMenuItem.Name, valueMenuItem.Checked);
260 Config.Set(minMenuItem.Name, minMenuItem.Checked);
261 Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
262 Config.Set(limitMenuItem.Name, limitMenuItem.Checked);
264 Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked);
265 Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
266 Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
268 if (WindowState != FormWindowState.Minimized) {
269 Config.Set("mainForm.Location.X", Location.X);
270 Config.Set("mainForm.Location.Y", Location.Y);
271 Config.Set("mainForm.Width", Width);
272 Config.Set("mainForm.Height", Height);
275 foreach (TreeColumn column in treeView.Columns)
276 Config.Set("treeView.Columns." + column.Header + ".Width",
279 timer.Enabled = false;
281 sensorSystemTray.Dispose();
282 notifyIcon.Dispose();
286 private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
287 new AboutBox().ShowDialog();
290 private void plotToolStripMenuItem_CheckedChanged(object sender,
293 splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
294 treeView.Invalidate();
297 private void treeView_Click(object sender, EventArgs e) {
299 MouseEventArgs m = e as MouseEventArgs;
300 if (m == null || m.Button != MouseButtons.Right)
303 NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
304 if (info.Control != null) {
305 SensorNode node = info.Node.Tag as SensorNode;
306 if (node != null && node.Sensor != null) {
308 sensorContextMenuStrip.Items.Clear();
309 if (node.Sensor.Parameters.Length > 0) {
310 ToolStripMenuItem item = new ToolStripMenuItem("Parameters...");
311 item.Click += delegate(object obj, EventArgs args) {
312 ShowParameterForm(node.Sensor);
314 sensorContextMenuStrip.Items.Add(item);
316 if (node.IsVisible) {
317 ToolStripMenuItem item = new ToolStripMenuItem("Hide");
318 item.Click += delegate(object obj, EventArgs args) {
319 node.IsVisible = false;
321 sensorContextMenuStrip.Items.Add(item);
323 ToolStripMenuItem item = new ToolStripMenuItem("Unhide");
324 item.Click += delegate(object obj, EventArgs args) {
325 node.IsVisible = true;
327 sensorContextMenuStrip.Items.Add(item);
329 if (sensorSystemTray.Contains(node.Sensor)) {
330 ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
331 item.Click += delegate(object obj, EventArgs args) {
332 sensorSystemTray.Remove(node.Sensor);
334 sensorContextMenuStrip.Items.Add(item);
336 ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
337 item.Click += delegate(object obj, EventArgs args) {
338 sensorSystemTray.Add(node.Sensor, true);
340 sensorContextMenuStrip.Items.Add(item);
342 sensorContextMenuStrip.Show(treeView, m.X, m.Y);
347 private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
348 string report = computer.GetReport();
349 if (saveFileDialog.ShowDialog() == DialogResult.OK) {
350 using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
356 private void hddsensorsToolStripMenuItem_CheckedChanged(object sender,
359 computer.HDDEnabled = hddMenuItem.Checked;
360 UpdatePlotSelection(null, null);
363 private void SysTrayHideShow() {
369 protected override void WndProc(ref Message m) {
370 const int WM_SYSCOMMAND = 0x112;
371 const int SC_MINIMIZE = 0xF020;
372 if (minTrayMenuItem.Checked &&
373 m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
380 private void hideShowClick(object sender, EventArgs e) {
384 private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
385 ToolStripMenuItem item = sender as ToolStripMenuItem;
389 ISensor sensor = item.Owner.Tag as ISensor;
393 sensorSystemTray.Remove(sensor);
396 private void ShowParameterForm(ISensor sensor) {
397 ParameterForm form = new ParameterForm();
398 form.Parameters = sensor.Parameters;
399 form.captionLabel.Text = sensor.Name;
403 private void treeView_NodeMouseDoubleClick(object sender,
404 TreeNodeAdvMouseEventArgs e) {
405 SensorNode node = e.Node.Tag as SensorNode;
406 if (node != null && node.Sensor != null &&
407 node.Sensor.Parameters.Length > 0) {
408 ShowParameterForm(node.Sensor);
412 private void runOnWindowsStartupToolStripMenuItem_CheckedChanged(
413 object sender, EventArgs e)
415 startupManager.Startup = startupMenuItem.Checked;
418 private void minTrayMenuItem_CheckedChanged(object sender, EventArgs e) {
419 notifyIcon.Visible = minTrayMenuItem.Checked;
422 private void hiddenSensorsMenuItem_CheckedChanged(object sender,
424 treeModel.ForceVisible = hiddenMenuItem.Checked;
427 private void valueMenuItem_CheckedChanged(object sender, EventArgs e) {
428 treeView.Columns[1].IsVisible = valueMenuItem.Checked;
431 private void minMenuItem_CheckedChanged(object sender, EventArgs e) {
432 treeView.Columns[2].IsVisible = minMenuItem.Checked;
435 private void maxMenuItem_CheckedChanged(object sender, EventArgs e) {
436 treeView.Columns[3].IsVisible = maxMenuItem.Checked;
439 private void limitMenuItem_CheckedChanged(object sender, EventArgs e) {
440 treeView.Columns[4].IsVisible = limitMenuItem.Checked;