moel@1: /* moel@1: moel@1: Version: MPL 1.1/GPL 2.0/LGPL 2.1 moel@1: moel@1: The contents of this file are subject to the Mozilla Public License Version moel@1: 1.1 (the "License"); you may not use this file except in compliance with moel@1: the License. You may obtain a copy of the License at moel@1: moel@1: http://www.mozilla.org/MPL/ moel@1: moel@1: Software distributed under the License is distributed on an "AS IS" basis, moel@1: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License moel@1: for the specific language governing rights and limitations under the License. moel@1: moel@1: The Original Code is the Open Hardware Monitor code. moel@1: moel@1: The Initial Developer of the Original Code is moel@1: Michael Möller . moel@1: Portions created by the Initial Developer are Copyright (C) 2009-2010 moel@1: the Initial Developer. All Rights Reserved. moel@1: moel@1: Contributor(s): moel@1: moel@1: Alternatively, the contents of this file may be used under the terms of moel@1: either the GNU General Public License Version 2 or later (the "GPL"), or moel@1: the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), moel@1: in which case the provisions of the GPL or the LGPL are applicable instead moel@1: of those above. If you wish to allow use of your version of this file only moel@1: under the terms of either the GPL or the LGPL, and not to allow others to moel@1: use your version of this file under the terms of the MPL, indicate your moel@1: decision by deleting the provisions above and replace them with the notice moel@1: and other provisions required by the GPL or the LGPL. If you do not delete moel@1: the provisions above, a recipient may use your version of this file under moel@1: the terms of any one of the MPL, the GPL or the LGPL. moel@1: moel@1: */ moel@1: moel@1: using System; moel@1: using System.Collections.Generic; moel@1: using System.ComponentModel; moel@1: using System.Configuration; moel@1: using System.Drawing; moel@1: using System.Text; moel@1: using System.Windows.Forms; moel@1: using Aga.Controls.Tree; moel@1: using Aga.Controls.Tree.NodeControls; moel@1: using OpenHardwareMonitor.Hardware; moel@1: moel@1: namespace OpenHardwareMonitor.GUI { moel@1: public partial class MainForm : Form { moel@1: moel@1: private Node root; moel@1: private List groupList = new List(); moel@1: private TreeModel treeModel; moel@1: private IDictionary sensorPlotColors = moel@1: new Dictionary(); moel@1: private Color[] plotColorPalette; moel@1: moel@1: public MainForm() { moel@1: moel@1: InitializeComponent(); moel@1: this.Font = SystemFonts.MessageBoxFont; moel@1: treeView.Font = SystemFonts.MessageBoxFont; moel@1: plotPanel.Font = SystemFonts.MessageBoxFont; moel@1: moel@1: nodeCheckBox.IsVisibleValueNeeded += moel@1: new EventHandler( moel@1: nodeCheckBox_IsVisibleValueNeeded); moel@1: nodeCheckBox.CheckStateChanged += moel@1: new EventHandler(UpdatePlotSelection); moel@1: nodeTextBoxText.DrawText += moel@1: new EventHandler(nodeTextBoxText_DrawText); moel@1: nodeTextBoxValue.DrawText += moel@1: new EventHandler(nodeTextBoxText_DrawText); moel@1: nodeTextBoxMin.DrawText += moel@1: new EventHandler(nodeTextBoxText_DrawText); moel@1: nodeTextBoxMax.DrawText += moel@1: new EventHandler(nodeTextBoxText_DrawText); moel@1: nodeTextBoxLimit.DrawText += moel@1: new EventHandler(nodeTextBoxLimit_DrawText); moel@1: moel@1: if (Utilities.Config.Contains("mainForm.Location.X")) { moel@1: int x = Utilities.Config.Get("mainForm.Location.X", Location.X); moel@1: x = x < 0 ? 0 : x; moel@1: int y = Utilities.Config.Get("mainForm.Location.Y", Location.Y); moel@1: y = y < 0 ? 0 : y; moel@1: this.Location = new Point(x, y); moel@1: } else { moel@1: StartPosition = FormStartPosition.CenterScreen; moel@1: } moel@1: moel@1: Width = Utilities.Config.Get("mainForm.Width", Width); moel@1: Height = Utilities.Config.Get("mainForm.Height", Height); moel@1: moel@1: treeModel = new TreeModel(); moel@1: root = new Node(System.Environment.MachineName); moel@1: root.Image = Utilities.EmbeddedResources.GetImage("computer.png"); moel@1: moel@1: treeModel.Nodes.Add(root); moel@1: treeView.Model = treeModel; moel@1: moel@1: AddGroup(new Hardware.SMBIOS.SMBIOSGroup()); moel@1: AddGroup(new Hardware.LPC.LPCGroup()); moel@1: AddGroup(new Hardware.CPU.CPUGroup()); moel@1: AddGroup(new Hardware.ATI.ATIGroup()); moel@1: AddGroup(new Hardware.Nvidia.NvidiaGroup()); moel@1: AddGroup(new Hardware.TBalancer.TBalancerGroup()); moel@1: moel@1: plotColorPalette = new Color[14]; moel@1: plotColorPalette[0] = Color.Blue; moel@1: plotColorPalette[1] = Color.OrangeRed; moel@1: plotColorPalette[2] = Color.Green; moel@1: plotColorPalette[3] = Color.LightSeaGreen; moel@1: plotColorPalette[4] = Color.Goldenrod; moel@1: plotColorPalette[5] = Color.DarkViolet; moel@1: plotColorPalette[6] = Color.YellowGreen; moel@1: plotColorPalette[7] = Color.SaddleBrown; moel@1: plotColorPalette[8] = Color.Gray; moel@1: plotColorPalette[9] = Color.RoyalBlue; moel@1: plotColorPalette[10] = Color.DeepPink; moel@1: plotColorPalette[11] = Color.MediumSeaGreen; moel@1: plotColorPalette[12] = Color.Olive; moel@1: plotColorPalette[13] = Color.Firebrick; moel@1: moel@1: plotMenuItem.Checked = Utilities.Config.Get(plotMenuItem.Name, false); moel@1: minMenuItem.Checked = Utilities.Config.Get(minMenuItem.Name, false); moel@1: maxMenuItem.Checked = Utilities.Config.Get(maxMenuItem.Name, true); moel@1: limitMenuItem.Checked = Utilities.Config.Get(limitMenuItem.Name, false); moel@2: hddMenuItem.Checked = Utilities.Config.Get(hddMenuItem.Name, true); moel@1: moel@1: voltMenuItem.Checked = Utilities.Config.Get(voltMenuItem.Name, true); moel@1: clocksMenuItem.Checked = Utilities.Config.Get(clocksMenuItem.Name, true); moel@1: tempMenuItem.Checked = Utilities.Config.Get(tempMenuItem.Name, true); moel@1: fansMenuItem.Checked = Utilities.Config.Get(fansMenuItem.Name, true); moel@1: moel@1: timer.Enabled = true; moel@1: } moel@1: moel@1: private void AddGroup(IGroup group) { moel@1: groupList.Add(group); moel@1: foreach (IHardware hardware in group.Hardware) moel@1: root.Nodes.Add(new HardwareNode(hardware)); moel@1: } moel@1: moel@1: private void RemoveGroup(IGroup group) { moel@1: List nodesToRemove = new List(); moel@1: foreach (IHardware hardware in group.Hardware) moel@1: foreach (Node node in root.Nodes) { moel@1: HardwareNode hardwareNode = node as HardwareNode; moel@1: if (hardwareNode != null && hardwareNode.Hardware == hardware) moel@1: nodesToRemove.Add(node); moel@1: } moel@1: foreach (Node node in nodesToRemove) moel@1: root.Nodes.Remove(node); moel@1: groupList.Remove(group); moel@1: } moel@1: moel@1: private void nodeTextBoxLimit_DrawText(object sender, DrawEventArgs e) { moel@1: SensorNode sensorNode = e.Node.Tag as SensorNode; moel@1: if (sensorNode != null) moel@1: e.Text = sensorNode.ValueToString(sensorNode.Sensor.Limit); moel@1: } moel@1: moel@1: private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) { moel@1: if (!plotMenuItem.Checked) moel@1: return; moel@1: moel@1: SensorNode sensorNode = e.Node.Tag as SensorNode; moel@1: if (sensorNode != null) { moel@1: Color color; moel@1: if (sensorPlotColors.TryGetValue(sensorNode.Sensor, out color)) moel@1: e.TextColor = color; moel@1: } moel@1: } moel@1: moel@1: private void UpdatePlotSelection(object sender, moel@1: TreePathEventArgs e) moel@1: { moel@1: List selected = new List(); moel@1: IDictionary colors = new Dictionary(); moel@1: int colorIndex = 0; moel@1: foreach (TreeNodeAdv node in treeView.AllNodes) { moel@1: SensorNode sensorNode = node.Tag as SensorNode; moel@1: if (sensorNode != null && moel@1: sensorNode.Sensor.SensorType == SensorType.Temperature) { moel@1: if (sensorNode.Plot) { moel@1: colors.Add(sensorNode.Sensor, moel@1: plotColorPalette[colorIndex % plotColorPalette.Length]); moel@1: selected.Add(sensorNode.Sensor); moel@1: } moel@1: colorIndex++; moel@1: } moel@1: } moel@1: sensorPlotColors = colors; moel@1: plotPanel.SetSensors(selected, colors); moel@1: } moel@1: moel@1: private void nodeCheckBox_IsVisibleValueNeeded(object sender, moel@1: NodeControlValueEventArgs e) { moel@1: SensorNode node = e.Node.Tag as SensorNode; moel@1: e.Value = (node != null) && moel@1: (node.Sensor.SensorType == SensorType.Temperature) && moel@1: plotMenuItem.Checked; moel@1: } moel@1: moel@1: private void exitToolStripMenuItem_Click(object sender, EventArgs e) { moel@1: Close(); moel@1: } moel@1: moel@1: private void timer_Tick(object sender, EventArgs e) { moel@1: moel@1: #if !DEBUG moel@1: try { moel@1: #endif moel@1: foreach (IGroup group in groupList) moel@1: foreach (IHardware hardware in group.Hardware) moel@1: hardware.Update(); moel@1: #if !DEBUG moel@1: } catch (Exception exception) { moel@1: Utilities.CrashReport.Save(exception); moel@1: Close(); moel@1: } moel@1: #endif moel@1: moel@1: treeView.Invalidate(); moel@1: plotPanel.Invalidate(); moel@1: } moel@1: moel@1: private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { moel@1: Utilities.Config.Set(plotMenuItem.Name, plotMenuItem.Checked); moel@1: Utilities.Config.Set(minMenuItem.Name, minMenuItem.Checked); moel@1: Utilities.Config.Set(maxMenuItem.Name, maxMenuItem.Checked); moel@1: Utilities.Config.Set(limitMenuItem.Name, limitMenuItem.Checked); moel@1: Utilities.Config.Set(hddMenuItem.Name, hddMenuItem.Checked); moel@1: moel@1: Utilities.Config.Set(voltMenuItem.Name, voltMenuItem.Checked); moel@1: Utilities.Config.Set(clocksMenuItem.Name, clocksMenuItem.Checked); moel@1: Utilities.Config.Set(tempMenuItem.Name, tempMenuItem.Checked); moel@1: Utilities.Config.Set(fansMenuItem.Name, fansMenuItem.Checked); moel@1: moel@1: Utilities.Config.Set("mainForm.Location.X", Location.X); moel@1: Utilities.Config.Set("mainForm.Location.Y", Location.Y); moel@1: Utilities.Config.Set("mainForm.Width", Width); moel@1: Utilities.Config.Set("mainForm.Height", Height); moel@1: moel@1: foreach (IGroup group in groupList) moel@1: group.Close(); moel@1: } moel@1: moel@1: private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { moel@1: new AboutBox().ShowDialog(); moel@1: } moel@1: moel@1: private void plotToolStripMenuItem_CheckedChanged(object sender, moel@1: EventArgs e) moel@1: { moel@1: splitContainer.Panel2Collapsed = !plotMenuItem.Checked; moel@1: treeView.Invalidate(); moel@1: } moel@1: moel@1: private void valueToolStripMenuItem_CheckedChanged(object sender, moel@1: EventArgs e) moel@1: { moel@1: treeView.Columns[1].IsVisible = valueToolStripMenuItem.Checked; moel@1: } moel@1: moel@1: private void minToolStripMenuItem_CheckedChanged(object sender, EventArgs e) moel@1: { moel@1: treeView.Columns[2].IsVisible = minMenuItem.Checked; moel@1: } moel@1: moel@1: private void maxToolStripMenuItem_CheckedChanged(object sender, EventArgs e) moel@1: { moel@1: treeView.Columns[3].IsVisible = maxMenuItem.Checked; moel@1: } moel@1: moel@1: private void limitToolStripMenuItem_CheckedChanged(object sender, moel@1: EventArgs e) { moel@1: treeView.Columns[4].IsVisible = limitMenuItem.Checked; moel@1: } moel@1: moel@1: private void treeView_Click(object sender, EventArgs e) { moel@1: moel@1: MouseEventArgs m = e as MouseEventArgs; moel@1: if (m == null || m.Button != MouseButtons.Right) moel@1: return; moel@1: moel@1: NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y)); moel@1: if (info.Control == null) moel@1: columnsContextMenuStrip.Show(treeView, m.X, m.Y); moel@1: } moel@1: moel@1: private void saveReportToolStripMenuItem_Click(object sender, EventArgs e) { moel@1: ReportWriter.Save(groupList, new Version(Application.ProductVersion)); moel@1: } moel@1: moel@1: private void hddsensorsToolStripMenuItem_CheckedChanged(object sender, moel@1: EventArgs e) moel@1: { moel@1: if (hddMenuItem.Checked) { moel@1: AddGroup(new Hardware.HDD.HDDGroup()); moel@1: UpdateSensorTypeChecked(null, null); moel@1: } else { moel@1: List groupsToRemove = new List(); moel@1: foreach (IGroup group in groupList) moel@1: if (group is Hardware.HDD.HDDGroup) moel@1: groupsToRemove.Add(group); moel@1: foreach (IGroup group in groupsToRemove) { moel@1: group.Close(); moel@1: RemoveGroup(group); moel@1: } moel@1: UpdatePlotSelection(null, null); moel@1: } moel@1: } moel@1: moel@1: private void UpdateSensorTypeChecked(object sender, EventArgs e) { moel@1: foreach (HardwareNode node in root.Nodes) { moel@1: node.SetVisible(SensorType.Voltage, voltMenuItem.Checked); moel@1: node.SetVisible(SensorType.Clock, clocksMenuItem.Checked); moel@1: node.SetVisible(SensorType.Temperature, tempMenuItem.Checked); moel@1: node.SetVisible(SensorType.Fan, fansMenuItem.Checked); moel@1: } moel@1: } moel@1: } moel@1: }