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;
76 nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
78 if (Utilities.Config.Contains("mainForm.Location.X")) {
79 int x = Utilities.Config.Get("mainForm.Location.X", Location.X);
81 int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y);
83 this.Location = new Point(x, y);
85 StartPosition = FormStartPosition.CenterScreen;
88 Width = Utilities.Config.Get("mainForm.Width", Width);
89 Height = Utilities.Config.Get("mainForm.Height", Height);
91 foreach (TreeColumn column in treeView.Columns)
92 column.Width = Math.Max(20, Math.Min(400,
93 Config.Get("treeView.Columns." + column.Header + ".Width",
96 treeModel = new TreeModel();
97 root = new Node(System.Environment.MachineName);
98 root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
100 treeModel.Nodes.Add(root);
101 treeView.Model = treeModel;
103 systemTray = new SystemTray(computer);
104 systemTray.HideShowCommand += hideShowClick;
105 systemTray.ExitCommand += exitClick;
107 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
108 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
111 timer.Enabled = true;
113 plotColorPalette = new Color[13];
114 plotColorPalette[0] = Color.Blue;
115 plotColorPalette[1] = Color.OrangeRed;
116 plotColorPalette[2] = Color.Green;
117 plotColorPalette[3] = Color.LightSeaGreen;
118 plotColorPalette[4] = Color.Goldenrod;
119 plotColorPalette[5] = Color.DarkViolet;
120 plotColorPalette[6] = Color.YellowGreen;
121 plotColorPalette[7] = Color.SaddleBrown;
122 plotColorPalette[8] = Color.RoyalBlue;
123 plotColorPalette[9] = Color.DeepPink;
124 plotColorPalette[10] = Color.MediumSeaGreen;
125 plotColorPalette[11] = Color.Olive;
126 plotColorPalette[12] = Color.Firebrick;
128 hiddenMenuItem.Checked = Config.Get(hiddenMenuItem.Name, false);
129 plotMenuItem.Checked = Config.Get(plotMenuItem.Name, false);
131 valueMenuItem.Checked = Config.Get(valueMenuItem.Name, true);
132 minMenuItem.Checked = Config.Get(minMenuItem.Name, false);
133 maxMenuItem.Checked = Config.Get(maxMenuItem.Name, true);
135 startMinMenuItem.Checked = Config.Get(startMinMenuItem.Name, false);
136 minTrayMenuItem.Checked = Config.Get(minTrayMenuItem.Name, true);
137 startupMenuItem.Checked = startupManager.Startup;
138 hddMenuItem.Checked = Config.Get(hddMenuItem.Name, true);
140 celciusToolStripMenuItem.Checked =
141 UnitManager.TemperatureUnit == TemperatureUnit.Celcius;
142 fahrenheitToolStripMenuItem.Checked = !celciusToolStripMenuItem.Checked;
144 startupMenuItem.Visible = startupManager.IsAvailable;
146 if (startMinMenuItem.Checked) {
147 if (!minTrayMenuItem.Checked) {
148 WindowState = FormWindowState.Minimized;
155 // Create a handle, otherwise calling Close() does not fire FormClosed
156 IntPtr handle = Handle;
158 // Make sure the settings are saved when the user logs off
159 Microsoft.Win32.SystemEvents.SessionEnded +=
160 delegate(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
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 nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {
191 Node node = e.Node.Tag as Node;
194 if (node.IsVisible) {
195 SensorNode sensorNode = node as SensorNode;
196 if (plotMenuItem.Checked && sensorNode != null &&
197 sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
200 e.TextColor = Color.DarkGray;
205 private void UpdatePlotSelection(object sender,
208 List<ISensor> selected = new List<ISensor>();
209 IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
211 foreach (TreeNodeAdv node in treeView.AllNodes) {
212 SensorNode sensorNode = node.Tag as SensorNode;
213 if (sensorNode != null &&
214 sensorNode.Sensor.SensorType == SensorType.Temperature) {
215 if (sensorNode.Plot) {
216 colors.Add(sensorNode.Sensor,
217 plotColorPalette[colorIndex % plotColorPalette.Length]);
218 selected.Add(sensorNode.Sensor);
223 sensorPlotColors = colors;
224 plotPanel.SetSensors(selected, colors);
227 private void nodeTextBoxText_EditorShowing(object sender, CancelEventArgs e)
229 e.Cancel = !(treeView.CurrentNode != null &&
230 treeView.CurrentNode.Tag is SensorNode);
233 private void nodeCheckBox_IsVisibleValueNeeded(object sender,
234 NodeControlValueEventArgs e) {
235 SensorNode node = e.Node.Tag as SensorNode;
236 e.Value = (node != null) &&
237 (node.Sensor.SensorType == SensorType.Temperature) &&
238 plotMenuItem.Checked;
241 private void exitClick(object sender, EventArgs e) {
245 private void timer_Tick(object sender, EventArgs e) {
246 computer.Accept(updateVisitor);
247 treeView.Invalidate();
248 plotPanel.Invalidate();
252 private void SaveConfiguration() {
253 Config.Set(hiddenMenuItem.Name, hiddenMenuItem.Checked);
254 Config.Set(plotMenuItem.Name, plotMenuItem.Checked);
256 Config.Set(valueMenuItem.Name, valueMenuItem.Checked);
257 Config.Set(minMenuItem.Name, minMenuItem.Checked);
258 Config.Set(maxMenuItem.Name, maxMenuItem.Checked);
260 Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked);
261 Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked);
262 Config.Set(hddMenuItem.Name, hddMenuItem.Checked);
264 if (WindowState != FormWindowState.Minimized) {
265 Config.Set("mainForm.Location.X", Location.X);
266 Config.Set("mainForm.Location.Y", Location.Y);
267 Config.Set("mainForm.Width", Width);
268 Config.Set("mainForm.Height", Height);
271 foreach (TreeColumn column in treeView.Columns)
272 Config.Set("treeView.Columns." + column.Header + ".Width",
278 private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
281 timer.Enabled = false;
282 systemTray.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 EditableControl control = info.Control as EditableControl;
317 if (control != null) {
318 ToolStripMenuItem item = new ToolStripMenuItem("Rename");
319 item.Click += delegate(object obj, EventArgs args) {
322 sensorContextMenuStrip.Items.Add(item);
324 if (node.IsVisible) {
325 ToolStripMenuItem item = new ToolStripMenuItem("Hide");
326 item.Click += delegate(object obj, EventArgs args) {
327 node.IsVisible = false;
329 sensorContextMenuStrip.Items.Add(item);
331 ToolStripMenuItem item = new ToolStripMenuItem("Unhide");
332 item.Click += delegate(object obj, EventArgs args) {
333 node.IsVisible = true;
335 sensorContextMenuStrip.Items.Add(item);
337 if (systemTray.Contains(node.Sensor)) {
338 ToolStripMenuItem item = new ToolStripMenuItem("Remove From Tray");
339 item.Click += delegate(object obj, EventArgs args) {
340 systemTray.Remove(node.Sensor);
342 sensorContextMenuStrip.Items.Add(item);
344 ToolStripMenuItem item = new ToolStripMenuItem("Add To Tray");
345 item.Click += delegate(object obj, EventArgs args) {
346 systemTray.Add(node.Sensor, true);
348 sensorContextMenuStrip.Items.Add(item);
350 sensorContextMenuStrip.Show(treeView, m.X, m.Y);
355 private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) {
356 string report = computer.GetReport();
357 if (saveFileDialog.ShowDialog() == DialogResult.OK) {
358 using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
364 private void hddsensorsToolStripMenuItem_CheckedChanged(object sender,
367 computer.HDDEnabled = hddMenuItem.Checked;
368 UpdatePlotSelection(null, null);
371 private void SysTrayHideShow() {
377 protected override void WndProc(ref Message m) {
378 const int WM_SYSCOMMAND = 0x112;
379 const int SC_MINIMIZE = 0xF020;
380 if (minTrayMenuItem.Checked &&
381 m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
388 private void hideShowClick(object sender, EventArgs e) {
392 private void removeToolStripMenuItem_Click(object sender, EventArgs e) {
393 ToolStripMenuItem item = sender as ToolStripMenuItem;
397 ISensor sensor = item.Owner.Tag as ISensor;
401 systemTray.Remove(sensor);
404 private void ShowParameterForm(ISensor sensor) {
405 ParameterForm form = new ParameterForm();
406 form.Parameters = sensor.Parameters;
407 form.captionLabel.Text = sensor.Name;
411 private void treeView_NodeMouseDoubleClick(object sender,
412 TreeNodeAdvMouseEventArgs e) {
413 SensorNode node = e.Node.Tag as SensorNode;
414 if (node != null && node.Sensor != null &&
415 node.Sensor.Parameters.Length > 0) {
416 ShowParameterForm(node.Sensor);
420 private void runOnWindowsStartupToolStripMenuItem_CheckedChanged(
421 object sender, EventArgs e)
423 startupManager.Startup = startupMenuItem.Checked;
426 private void minTrayMenuItem_CheckedChanged(object sender, EventArgs e) {
427 systemTray.IsMainIconEnabled = minTrayMenuItem.Checked;
430 private void hiddenSensorsMenuItem_CheckedChanged(object sender,
432 treeModel.ForceVisible = hiddenMenuItem.Checked;
435 private void valueMenuItem_CheckedChanged(object sender, EventArgs e) {
436 treeView.Columns[1].IsVisible = valueMenuItem.Checked;
439 private void minMenuItem_CheckedChanged(object sender, EventArgs e) {
440 treeView.Columns[2].IsVisible = minMenuItem.Checked;
443 private void maxMenuItem_CheckedChanged(object sender, EventArgs e) {
444 treeView.Columns[3].IsVisible = maxMenuItem.Checked;
447 private void celciusToolStripMenuItem_Click(object sender, EventArgs e) {
448 celciusToolStripMenuItem.Checked = true;
449 fahrenheitToolStripMenuItem.Checked = false;
450 UnitManager.TemperatureUnit = TemperatureUnit.Celcius;
453 private void fahrenheitToolStripMenuItem_Click(object sender, EventArgs e) {
454 celciusToolStripMenuItem.Checked = false;
455 fahrenheitToolStripMenuItem.Checked = true;
456 UnitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
459 private void sumbitReportToolStripMenuItem_Click(object sender, EventArgs e)
461 ReportForm form = new ReportForm();
462 form.Report = computer.GetReport();
466 private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
467 IVisitor visitor = new ResetMinMaxVisitor();
468 computer.Accept(visitor);