Fixed Issue 73.
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 auto startup menu item on Unix
144 int p = (int)System.Environment.OSVersion.Platform;
145 if ((p == 4) || (p == 128)) {
146 startupMenuItem.Visible = false;
149 if (startMinMenuItem.Checked) {
150 if (!minTrayMenuItem.Checked) {
151 WindowState = FormWindowState.Minimized;
158 // Create a handle, otherwise calling Close() does not fire FormClosed
159 IntPtr handle = Handle;
161 // Make sure the settings are saved when the user logs off
162 Microsoft.Win32.SystemEvents.SessionEnded +=
163 delegate(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
168 private void SubHardwareAdded(IHardware hardware, Node node) {
169 Node hardwareNode = new HardwareNode(hardware);
170 node.Nodes.Add(hardwareNode);
171 foreach (IHardware subHardware in hardware.SubHardware)
172 SubHardwareAdded(subHardware, hardwareNode);
175 private void HardwareAdded(IHardware hardware) {
176 Node hardwareNode = new HardwareNode(hardware);
177 root.Nodes.Add(hardwareNode);
178 foreach (IHardware subHardware in hardware.SubHardware)
179 SubHardwareAdded(subHardware, hardwareNode);
182 private void HardwareRemoved(IHardware hardware) {
183 List<Node> nodesToRemove = new List<Node>();
184 foreach (Node node in root.Nodes) {
185 HardwareNode hardwareNode = node as HardwareNode;
186 if (hardwareNode != null && hardwareNode.Hardware == hardware)
187 nodesToRemove.Add(node);
189 foreach (Node node in nodesToRemove)
190 root.Nodes.Remove(node);
193 private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
194 Node node = e.Node.Tag as Node;
197 if (node.IsVisible) {
198 SensorNode sensorNode = node as SensorNode;
199 if (plotMenuItem.Checked && sensorNode != null &&
200 sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
203 e.TextColor = Color.DarkGray;
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 exitClick(object sender, EventArgs e) {
242 private void timer_Tick(object sender, EventArgs e) {
243 computer.Accept(updateVisitor);
244 treeView.Invalidate();
245 plotPanel.Invalidate();
249 private void SaveConfiguration() {
250 Config.Set(hiddenMenuItem.Name, hiddenMenuItem.Checked);
251 Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
253 Config.Set(valueMenuItem.Name, valueMenuItem.Checked);
254 Config.Set(minMenuItem.Name, minMenuItem.Checked);
255 Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
257 Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked);
258 Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
259 Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
261 if (WindowState != FormWindowState.Minimized) {
262 Config.Set("mainForm.Location.X", Location.X);
263 Config.Set("mainForm.Location.Y", Location.Y);
264 Config.Set("mainForm.Width", Width);
265 Config.Set("mainForm.Height", Height);
268 foreach (TreeColumn column in treeView.Columns)
269 Config.Set("treeView.Columns." + column.Header + ".Width",
275 private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
278 timer.Enabled = false;
279 systemTray.Dispose();
283 private void aboutToolStripMenuItem_Click(object sender, EventArgs e) {
284 new AboutBox().ShowDialog();
287 private void plotToolStripMenuItem_CheckedChanged(object sender,
290 splitContainer.Panel2Collapsed = !plotMenuItem.Checked;
291 treeView.Invalidate();
294 private void treeView_Click(object sender, EventArgs e) {
296 MouseEventArgs m = e as MouseEventArgs;
297 if (m == null || m.Button != MouseButtons.Right)
300 NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
301 if (info.Control != null) {
302 SensorNode node = info.Node.Tag as SensorNode;
303 if (node != null && node.Sensor != null) {
305 sensorContextMenuStrip.Items.Clear();
306 if (node.Sensor.Parameters.Length > 0) {
307 ToolStripMenuItem item = new ToolStripMenuItem("Parameters...");
308 item.Click += delegate(object obj, EventArgs args) {
309 ShowParameterForm(node.Sensor);
311 sensorContextMenuStrip.Items.Add(item);
313 if (node.IsVisible) {
314 ToolStripMenuItem item = new ToolStripMenuItem("Hide");
315 item.Click += delegate(object obj, EventArgs args) {
316 node.IsVisible = false;
318 sensorContextMenuStrip.Items.Add(item);
320 ToolStripMenuItem item = new ToolStripMenuItem("Unhide");
321 item.Click += delegate(object obj, EventArgs args) {
322 node.IsVisible = true;
324 sensorContextMenuStrip.Items.Add(item);
326 if (systemTray.Contains(node.Sensor)) {
327 ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
328 item.Click += delegate(object obj, EventArgs args) {
329 systemTray.Remove(node.Sensor);
331 sensorContextMenuStrip.Items.Add(item);
333 ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
334 item.Click += delegate(object obj, EventArgs args) {
335 systemTray.Add(node.Sensor, true);
337 sensorContextMenuStrip.Items.Add(item);
339 sensorContextMenuStrip.Show(treeView, m.X, m.Y);
344 private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
345 string report = computer.GetReport();
346 if (saveFileDialog.ShowDialog() == DialogResult.OK) {
347 using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
353 private void hddsensorsToolStripMenuItem_CheckedChanged(object sender,
356 computer.HDDEnabled = hddMenuItem.Checked;
357 UpdatePlotSelection(null, null);
360 private void SysTrayHideShow() {
366 protected override void WndProc(ref Message m) {
367 const int WM_SYSCOMMAND = 0x112;
368 const int SC_MINIMIZE = 0xF020;
369 if (minTrayMenuItem.Checked &&
370 m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
377 private void hideShowClick(object sender, EventArgs e) {
381 private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
382 ToolStripMenuItem item = sender as ToolStripMenuItem;
386 ISensor sensor = item.Owner.Tag as ISensor;
390 systemTray.Remove(sensor);
393 private void ShowParameterForm(ISensor sensor) {
394 ParameterForm form = new ParameterForm();
395 form.Parameters = sensor.Parameters;
396 form.captionLabel.Text = sensor.Name;
400 private void treeView_NodeMouseDoubleClick(object sender,
401 TreeNodeAdvMouseEventArgs e) {
402 SensorNode node = e.Node.Tag as SensorNode;
403 if (node != null && node.Sensor != null &&
404 node.Sensor.Parameters.Length > 0) {
405 ShowParameterForm(node.Sensor);
409 private void runOnWindowsStartupToolStripMenuItem_CheckedChanged(
410 object sender, EventArgs e)
412 startupManager.Startup = startupMenuItem.Checked;
415 private void minTrayMenuItem_CheckedChanged(object sender, EventArgs e) {
416 systemTray.IsMainIconEnabled = minTrayMenuItem.Checked;
419 private void hiddenSensorsMenuItem_CheckedChanged(object sender,
421 treeModel.ForceVisible = hiddenMenuItem.Checked;
424 private void valueMenuItem_CheckedChanged(object sender, EventArgs e) {
425 treeView.Columns[1].IsVisible = valueMenuItem.Checked;
428 private void minMenuItem_CheckedChanged(object sender, EventArgs e) {
429 treeView.Columns[2].IsVisible = minMenuItem.Checked;
432 private void maxMenuItem_CheckedChanged(object sender, EventArgs e) {
433 treeView.Columns[3].IsVisible = maxMenuItem.Checked;
436 private void celciusToolStripMenuItem_Click(object sender, EventArgs e) {
437 celciusToolStripMenuItem.Checked = true;
438 fahrenheitToolStripMenuItem.Checked = false;
439 UnitManager.TemperatureUnit = TemperatureUnit.Celcius;
442 private void fahrenheitToolStripMenuItem_Click(object sender, EventArgs e) {
443 celciusToolStripMenuItem.Checked = false;
444 fahrenheitToolStripMenuItem.Checked = true;
445 UnitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;