Fixed Issue 69.
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 SystemTray systemTray;
61 private StartupManager startupManager = new StartupManager();
62 private UpdateVisitor updateVisitor = new UpdateVisitor();
65 InitializeComponent();
66 this.Font = SystemFonts.MessageBoxFont;
67 treeView.Font = SystemFonts.MessageBoxFont;
68 plotPanel.Font = SystemFonts.MessageBoxFont;
70 nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
71 nodeCheckBox.CheckStateChanged += UpdatePlotSelection;
72 nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
73 nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
74 nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
75 nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
77 if (Utilities.Config.Contains("mainForm.Location.X")) {
78 int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
80 int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
82 this.Location = new Point(x, y);
84 StartPosition = FormStartPosition.CenterScreen;
87 Width = Utilities.Config.Get("mainForm.Width", Width);
88 Height = Utilities.Config.Get("mainForm.Height", Height);
90 foreach (TreeColumn column in treeView.Columns)
91 column.Width = Math.Max(20, Math.Min(400,
92 Config.Get("treeView.Columns." + column.Header + ".Width",
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 systemTray = new SystemTray(computer);
103 systemTray.HideShowCommand += hideShowClick;
104 systemTray.ExitCommand += exitClick;
106 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
107 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
110 timer.Enabled = true;
112 plotColorPalette = new Color[13];
113 plotColorPalette[0] = Color.Blue;
114 plotColorPalette[1] = Color.OrangeRed;
115 plotColorPalette[2] = Color.Green;
116 plotColorPalette[3] = Color.LightSeaGreen;
117 plotColorPalette[4] = Color.Goldenrod;
118 plotColorPalette[5] = Color.DarkViolet;
119 plotColorPalette[6] = Color.YellowGreen;
120 plotColorPalette[7] = Color.SaddleBrown;
121 plotColorPalette[8] = Color.RoyalBlue;
122 plotColorPalette[9] = Color.DeepPink;
123 plotColorPalette[10] = Color.MediumSeaGreen;
124 plotColorPalette[11] = Color.Olive;
125 plotColorPalette[12] = Color.Firebrick;
127 hiddenMenuItem.Checked = Config.Get(hiddenMenuItem.Name, false);
128 plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
130 valueMenuItem.Checked = Config.Get(valueMenuItem.Name, true);
131 minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
132 maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
134 startMinMenuItem.Checked = Config.Get(startMinMenuItem.Name, false);
135 minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
136 startupMenuItem.Checked = startupManager.Startup;
137 hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
139 celciusToolStripMenuItem.Checked =
140 UnitManager.TemperatureUnit == TemperatureUnit.Celcius;
141 fahrenheitToolStripMenuItem.Checked = !celciusToolStripMenuItem.Checked;
143 // Hide the system tray and auto startup menu items on Unix
144 int p = (int)System.Environment.OSVersion.Platform;
145 if ((p == 4) || (p == 128)) {
146 minTrayMenuItem.Visible = false;
147 startupMenuItem.Visible = false;
150 if (startMinMenuItem.Checked) {
151 if (!minTrayMenuItem.Checked) {
152 WindowState = FormWindowState.Minimized;
159 // Create a handle, otherwise calling Close() does not fire FormClosed
160 IntPtr handle = Handle;
162 // Make sure the settings are saved when the user logs off
163 Microsoft.Win32.SystemEvents.SessionEnded +=
164 delegate(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
169 private void SubHardwareAdded(IHardware hardware, Node node) {
170 Node hardwareNode = new HardwareNode(hardware);
171 node.Nodes.Add(hardwareNode);
172 foreach (IHardware subHardware in hardware.SubHardware)
173 SubHardwareAdded(subHardware, hardwareNode);
176 private void HardwareAdded(IHardware hardware) {
177 Node hardwareNode = new HardwareNode(hardware);
178 root.Nodes.Add(hardwareNode);
179 foreach (IHardware subHardware in hardware.SubHardware)
180 SubHardwareAdded(subHardware, hardwareNode);
183 private void HardwareRemoved(IHardware hardware) {
184 List<Node> nodesToRemove = new List<Node>();
185 foreach (Node node in root.Nodes) {
186 HardwareNode hardwareNode = node as HardwareNode;
187 if (hardwareNode != null && hardwareNode.Hardware == hardware)
188 nodesToRemove.Add(node);
190 foreach (Node node in nodesToRemove)
191 root.Nodes.Remove(node);
194 private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
195 Node node = e.Node.Tag as Node;
198 if (node.IsVisible) {
199 SensorNode sensorNode = node as SensorNode;
200 if (plotMenuItem.Checked && sensorNode != null &&
201 sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
204 e.TextColor = Color.DarkGray;
209 private void UpdatePlotSelection(object sender,
212 List<ISensor> selected = new List<ISensor>();
213 IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
215 foreach (TreeNodeAdv node in treeView.AllNodes) {
216 SensorNode sensorNode = node.Tag as SensorNode;
217 if (sensorNode != null &&
218 sensorNode.Sensor.SensorType == SensorType.Temperature) {
219 if (sensorNode.Plot) {
220 colors.Add(sensorNode.Sensor,
221 plotColorPalette[colorIndex % plotColorPalette.Length]);
222 selected.Add(sensorNode.Sensor);
227 sensorPlotColors = colors;
228 plotPanel.SetSensors(selected, colors);
231 private void nodeCheckBox_IsVisibleValueNeeded(object sender,
232 NodeControlValueEventArgs e) {
233 SensorNode node = e.Node.Tag as SensorNode;
234 e.Value = (node != null) &&
235 (node.Sensor.SensorType == SensorType.Temperature) &&
236 plotMenuItem.Checked;
239 private void exitClick(object sender, EventArgs e) {
243 private void timer_Tick(object sender, EventArgs e) {
244 computer.Accept(updateVisitor);
245 treeView.Invalidate();
246 plotPanel.Invalidate();
250 private void SaveConfiguration() {
251 Config.Set(hiddenMenuItem.Name, hiddenMenuItem.Checked);
252 Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
254 Config.Set(valueMenuItem.Name, valueMenuItem.Checked);
255 Config.Set(minMenuItem.Name, minMenuItem.Checked);
256 Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
258 Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked);
259 Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
260 Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
262 if (WindowState != FormWindowState.Minimized) {
263 Config.Set("mainForm.Location.X", Location.X);
264 Config.Set("mainForm.Location.Y", Location.Y);
265 Config.Set("mainForm.Width", Width);
266 Config.Set("mainForm.Height", Height);
269 foreach (TreeColumn column in treeView.Columns)
270 Config.Set("treeView.Columns." + column.Header + ".Width",
276 private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
279 timer.Enabled = false;
280 systemTray.Dispose();
284 private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
285 new AboutBox().ShowDialog();
288 private void plotToolStripMenuItem_CheckedChanged(object sender,
291 splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
292 treeView.Invalidate();
295 private void treeView_Click(object sender, EventArgs e) {
297 MouseEventArgs m = e as MouseEventArgs;
298 if (m == null || m.Button != MouseButtons.Right)
301 NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
302 if (info.Control != null) {
303 SensorNode node = info.Node.Tag as SensorNode;
304 if (node != null && node.Sensor != null) {
306 sensorContextMenuStrip.Items.Clear();
307 if (node.Sensor.Parameters.Length > 0) {
308 ToolStripMenuItem item = new ToolStripMenuItem("Parameters...");
309 item.Click += delegate(object obj, EventArgs args) {
310 ShowParameterForm(node.Sensor);
312 sensorContextMenuStrip.Items.Add(item);
314 if (node.IsVisible) {
315 ToolStripMenuItem item = new ToolStripMenuItem("Hide");
316 item.Click += delegate(object obj, EventArgs args) {
317 node.IsVisible = false;
319 sensorContextMenuStrip.Items.Add(item);
321 ToolStripMenuItem item = new ToolStripMenuItem("Unhide");
322 item.Click += delegate(object obj, EventArgs args) {
323 node.IsVisible = true;
325 sensorContextMenuStrip.Items.Add(item);
327 if (systemTray.Contains(node.Sensor)) {
328 ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
329 item.Click += delegate(object obj, EventArgs args) {
330 systemTray.Remove(node.Sensor);
332 sensorContextMenuStrip.Items.Add(item);
334 ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
335 item.Click += delegate(object obj, EventArgs args) {
336 systemTray.Add(node.Sensor, true);
338 sensorContextMenuStrip.Items.Add(item);
340 sensorContextMenuStrip.Show(treeView, m.X, m.Y);
345 private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
346 string report = computer.GetReport();
347 if (saveFileDialog.ShowDialog() == DialogResult.OK) {
348 using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
354 private void hddsensorsToolStripMenuItem_CheckedChanged(object sender,
357 computer.HDDEnabled = hddMenuItem.Checked;
358 UpdatePlotSelection(null, null);
361 private void SysTrayHideShow() {
367 protected override void WndProc(ref Message m) {
368 const int WM_SYSCOMMAND = 0x112;
369 const int SC_MINIMIZE = 0xF020;
370 if (minTrayMenuItem.Checked &&
371 m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
378 private void hideShowClick(object sender, EventArgs e) {
382 private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
383 ToolStripMenuItem item = sender as ToolStripMenuItem;
387 ISensor sensor = item.Owner.Tag as ISensor;
391 systemTray.Remove(sensor);
394 private void ShowParameterForm(ISensor sensor) {
395 ParameterForm form = new ParameterForm();
396 form.Parameters = sensor.Parameters;
397 form.captionLabel.Text = sensor.Name;
401 private void treeView_NodeMouseDoubleClick(object sender,
402 TreeNodeAdvMouseEventArgs e) {
403 SensorNode node = e.Node.Tag as SensorNode;
404 if (node != null && node.Sensor != null &&
405 node.Sensor.Parameters.Length > 0) {
406 ShowParameterForm(node.Sensor);
410 private void runOnWindowsStartupToolStripMenuItem_CheckedChanged(
411 object sender, EventArgs e)
413 startupManager.Startup = startupMenuItem.Checked;
416 private void minTrayMenuItem_CheckedChanged(object sender, EventArgs e) {
417 systemTray.IsMainIconEnabled = minTrayMenuItem.Checked;
420 private void hiddenSensorsMenuItem_CheckedChanged(object sender,
422 treeModel.ForceVisible = hiddenMenuItem.Checked;
425 private void valueMenuItem_CheckedChanged(object sender, EventArgs e) {
426 treeView.Columns[1].IsVisible = valueMenuItem.Checked;
429 private void minMenuItem_CheckedChanged(object sender, EventArgs e) {
430 treeView.Columns[2].IsVisible = minMenuItem.Checked;
433 private void maxMenuItem_CheckedChanged(object sender, EventArgs e) {
434 treeView.Columns[3].IsVisible = maxMenuItem.Checked;
437 private void celciusToolStripMenuItem_Click(object sender, EventArgs e) {
438 celciusToolStripMenuItem.Checked = true;
439 fahrenheitToolStripMenuItem.Checked = false;
440 UnitManager.TemperatureUnit = TemperatureUnit.Celcius;
443 private void fahrenheitToolStripMenuItem_Click(object sender, EventArgs e) {
444 celciusToolStripMenuItem.Checked = false;
445 fahrenheitToolStripMenuItem.Checked = true;
446 UnitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;