GUI/MainForm.cs
author moel.mich
Mon, 23 May 2011 18:14:25 +0000
changeset 296 414783d1cda9
parent 291 61c3d984fb2d
child 297 21504f47aedf
permissions -rw-r--r--
Added support for the Fintek F71889AD super I/O chip. Extended the identification list of mainboard manufacturers.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@275
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2011
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
paulwerelds@223
    22
  Contributor(s): Paul Werelds
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.ComponentModel;
moel@1
    41
using System.Drawing;
moel@83
    42
using System.IO;
moel@291
    43
using System.Reflection;
moel@1
    44
using System.Windows.Forms;
moel@1
    45
using Aga.Controls.Tree;
moel@1
    46
using Aga.Controls.Tree.NodeControls;
moel@1
    47
using OpenHardwareMonitor.Hardware;
paulwerelds@227
    48
using OpenHardwareMonitor.WMI;
moel@1
    49
moel@1
    50
namespace OpenHardwareMonitor.GUI {
moel@1
    51
  public partial class MainForm : Form {
moel@1
    52
moel@165
    53
    private PersistentSettings settings;
moel@165
    54
    private UnitManager unitManager;
moel@165
    55
    private Computer computer;
moel@1
    56
    private Node root;
moel@1
    57
    private TreeModel treeModel;
moel@1
    58
    private IDictionary<ISensor, Color> sensorPlotColors = 
moel@1
    59
      new Dictionary<ISensor, Color>();
moel@1
    60
    private Color[] plotColorPalette;
moel@133
    61
    private SystemTray systemTray;    
moel@82
    62
    private StartupManager startupManager = new StartupManager();
moel@110
    63
    private UpdateVisitor updateVisitor = new UpdateVisitor();
moel@176
    64
    private SensorGadget gadget;
moel@295
    65
    private Form plotForm;
moel@1
    66
moel@156
    67
    private UserOption showHiddenSensors;
moel@156
    68
    private UserOption showPlot;
moel@156
    69
    private UserOption showValue;
moel@156
    70
    private UserOption showMin;
moel@156
    71
    private UserOption showMax;
moel@156
    72
    private UserOption startMinimized;
moel@156
    73
    private UserOption minimizeToTray;
paulwerelds@198
    74
    private UserOption minimizeOnClose;
moel@156
    75
    private UserOption autoStart;
moel@156
    76
    private UserOption readHddSensors;
moel@176
    77
    private UserOption showGadget;
moel@295
    78
    private UserRadioGroup plotLocation;
paulwerelds@223
    79
paulwerelds@223
    80
    private WmiProvider wmiProvider;
moel@156
    81
moel@288
    82
    private bool selectionDragging = false;
moel@288
    83
moel@28
    84
    public MainForm() {      
moel@1
    85
      InitializeComponent();
moel@156
    86
moel@291
    87
      // check if the OpenHardwareMonitorLib assembly has the correct version
moel@291
    88
      if (Assembly.GetAssembly(typeof(Computer)).GetName().Version !=
moel@291
    89
        Assembly.GetExecutingAssembly().GetName().Version) {
moel@291
    90
        MessageBox.Show(
moel@291
    91
          "The version of the file OpenHardwareMonitorLib.dll is incompatible.",
moel@291
    92
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@291
    93
        Environment.Exit(0);
moel@291
    94
      }
moel@291
    95
moel@165
    96
      this.settings = new PersistentSettings();      
moel@165
    97
      this.settings.Load(Path.ChangeExtension(
paulwerelds@198
    98
        Application.ExecutablePath, ".config"));
moel@165
    99
moel@165
   100
      this.unitManager = new UnitManager(settings);
moel@165
   101
moel@156
   102
      // set the DockStyle here, to avoid conflicts with the MainMenu
moel@156
   103
      this.splitContainer.Dock = DockStyle.Fill;
moel@202
   104
            
moel@1
   105
      this.Font = SystemFonts.MessageBoxFont;
moel@1
   106
      treeView.Font = SystemFonts.MessageBoxFont;
moel@267
   107
      plotPanel.Font = SystemFonts.MessageBoxFont;           
moel@1
   108
      
moel@133
   109
      nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
moel@133
   110
      nodeCheckBox.CheckStateChanged += UpdatePlotSelection;
moel@133
   111
      nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
moel@133
   112
      nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
moel@133
   113
      nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
moel@133
   114
      nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
moel@141
   115
      nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
moel@1
   116
moel@113
   117
      foreach (TreeColumn column in treeView.Columns) 
moel@165
   118
        column.Width = Math.Max(20, Math.Min(400,
moel@166
   119
          settings.GetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   120
          column.Width)));
moel@113
   121
moel@1
   122
      treeModel = new TreeModel();
moel@1
   123
      root = new Node(System.Environment.MachineName);
moel@1
   124
      root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
moel@1
   125
      
moel@1
   126
      treeModel.Nodes.Add(root);
moel@165
   127
      treeView.Model = treeModel;
moel@40
   128
moel@165
   129
      this.computer = new Computer(settings);
moel@165
   130
moel@165
   131
      systemTray = new SystemTray(computer, settings);
moel@133
   132
      systemTray.HideShowCommand += hideShowClick;
moel@133
   133
      systemTray.ExitCommand += exitClick;
moel@1
   134
moel@202
   135
      int p = (int)Environment.OSVersion.Platform;
moel@202
   136
      if ((p == 4) || (p == 128)) { // Unix
moel@287
   137
        treeView.RowHeight = Math.Max(treeView.RowHeight, 18); 
moel@202
   138
        splitContainer.BorderStyle = BorderStyle.None;
moel@202
   139
        splitContainer.Border3DStyle = Border3DStyle.Adjust;
moel@202
   140
        splitContainer.SplitterWidth = 4;
moel@202
   141
        treeView.BorderStyle = BorderStyle.Fixed3D;
moel@202
   142
        plotPanel.BorderStyle = BorderStyle.Fixed3D;
moel@202
   143
        gadgetMenuItem.Visible = false;
moel@202
   144
        minCloseMenuItem.Visible = false;
moel@269
   145
        minTrayMenuItem.Visible = false;
moel@269
   146
        startMinMenuItem.Visible = false;
moel@202
   147
      } else { // Windows
moel@287
   148
        treeView.RowHeight = Math.Max(treeView.Font.Height + 1, 18); 
moel@267
   149
moel@202
   150
        gadget = new SensorGadget(computer, settings, unitManager);
moel@244
   151
        gadget.HideShowCommand += hideShowClick;
moel@244
   152
moel@232
   153
        wmiProvider = new WmiProvider(computer);
moel@295
   154
      }    
moel@295
   155
      
moel@28
   156
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@232
   157
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);        
paulwerelds@223
   158
moel@28
   159
      computer.Open();
moel@28
   160
moel@86
   161
      timer.Enabled = true;
moel@86
   162
moel@111
   163
      plotColorPalette = new Color[13];
moel@1
   164
      plotColorPalette[0] = Color.Blue;
moel@1
   165
      plotColorPalette[1] = Color.OrangeRed;
moel@1
   166
      plotColorPalette[2] = Color.Green;
moel@1
   167
      plotColorPalette[3] = Color.LightSeaGreen;
moel@1
   168
      plotColorPalette[4] = Color.Goldenrod;
moel@1
   169
      plotColorPalette[5] = Color.DarkViolet;
moel@1
   170
      plotColorPalette[6] = Color.YellowGreen;
moel@1
   171
      plotColorPalette[7] = Color.SaddleBrown;
moel@111
   172
      plotColorPalette[8] = Color.RoyalBlue;
moel@111
   173
      plotColorPalette[9] = Color.DeepPink;
moel@111
   174
      plotColorPalette[10] = Color.MediumSeaGreen;
moel@111
   175
      plotColorPalette[11] = Color.Olive;
moel@111
   176
      plotColorPalette[12] = Color.Firebrick;
moel@1
   177
paulwerelds@223
   178
      showHiddenSensors = new UserOption("hiddenMenuItem", false,
paulwerelds@223
   179
        hiddenMenuItem, settings);
moel@156
   180
      showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   181
        treeModel.ForceVisible = showHiddenSensors.Value;
moel@156
   182
      };
moel@111
   183
paulwerelds@223
   184
      showValue = new UserOption("valueMenuItem", true, valueMenuItem,
paulwerelds@223
   185
        settings);
moel@156
   186
      showValue.Changed += delegate(object sender, EventArgs e) {
moel@156
   187
        treeView.Columns[1].IsVisible = showValue.Value;
moel@156
   188
      };
moel@122
   189
moel@165
   190
      showMin = new UserOption("minMenuItem", false, minMenuItem, settings);
moel@156
   191
      showMin.Changed += delegate(object sender, EventArgs e) {
moel@156
   192
        treeView.Columns[2].IsVisible = showMin.Value;
moel@156
   193
      };
moel@156
   194
moel@165
   195
      showMax = new UserOption("maxMenuItem", true, maxMenuItem, settings);
moel@156
   196
      showMax.Changed += delegate(object sender, EventArgs e) {
moel@156
   197
        treeView.Columns[3].IsVisible = showMax.Value;
moel@156
   198
      };
moel@156
   199
paulwerelds@223
   200
      startMinimized = new UserOption("startMinMenuItem", false,
paulwerelds@223
   201
        startMinMenuItem, settings);
moel@156
   202
paulwerelds@223
   203
      minimizeToTray = new UserOption("minTrayMenuItem", true,
paulwerelds@223
   204
        minTrayMenuItem, settings);
moel@156
   205
      minimizeToTray.Changed += delegate(object sender, EventArgs e) {
moel@156
   206
        systemTray.IsMainIconEnabled = minimizeToTray.Value;
moel@156
   207
      };
moel@156
   208
paulwerelds@223
   209
      minimizeOnClose = new UserOption("minCloseMenuItem", false,
paulwerelds@223
   210
        minCloseMenuItem, settings);
paulwerelds@198
   211
paulwerelds@223
   212
      autoStart = new UserOption(null, startupManager.Startup,
paulwerelds@223
   213
        startupMenuItem, settings);
moel@156
   214
      autoStart.Changed += delegate(object sender, EventArgs e) {
moel@185
   215
        try {
moel@185
   216
          startupManager.Startup = autoStart.Value;
moel@185
   217
        } catch (InvalidOperationException) {
moel@185
   218
          MessageBox.Show("Updating the auto-startup option failed.", "Error", 
moel@185
   219
            MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@185
   220
          autoStart.Value = startupManager.Startup;
moel@185
   221
        }
moel@156
   222
      };
moel@156
   223
paulwerelds@223
   224
      readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
paulwerelds@223
   225
        settings);
moel@156
   226
      readHddSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   227
        computer.HDDEnabled = readHddSensors.Value;
moel@156
   228
        UpdatePlotSelection(null, null);
moel@156
   229
      };
moel@156
   230
paulwerelds@223
   231
      showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
paulwerelds@223
   232
        settings);
moel@176
   233
      showGadget.Changed += delegate(object sender, EventArgs e) {
moel@202
   234
        if (gadget != null) 
moel@202
   235
          gadget.Visible = showGadget.Value;
moel@176
   236
      };
moel@176
   237
moel@156
   238
      celciusMenuItem.Checked = 
moel@165
   239
        unitManager.TemperatureUnit == TemperatureUnit.Celcius;
moel@156
   240
      fahrenheitMenuItem.Checked = !celciusMenuItem.Checked;
moel@55
   241
moel@295
   242
      InitializePlotForm();
moel@295
   243
moel@142
   244
      startupMenuItem.Visible = startupManager.IsAvailable;
moel@125
   245
      
moel@55
   246
      if (startMinMenuItem.Checked) {
moel@82
   247
        if (!minTrayMenuItem.Checked) {
moel@55
   248
          WindowState = FormWindowState.Minimized;
moel@55
   249
          Show();
moel@55
   250
        }
moel@55
   251
      } else {
moel@55
   252
        Show();
moel@55
   253
      }
moel@70
   254
moel@71
   255
      // Create a handle, otherwise calling Close() does not fire FormClosed     
moel@71
   256
      IntPtr handle = Handle;
moel@128
   257
moel@128
   258
      // Make sure the settings are saved when the user logs off
paulwerelds@223
   259
      Microsoft.Win32.SystemEvents.SessionEnded += delegate {
paulwerelds@223
   260
        SaveConfiguration();
paulwerelds@223
   261
      };  
moel@1
   262
    }
moel@295
   263
moel@295
   264
    private void InitializePlotForm() {
moel@295
   265
      plotForm = new Form();
moel@295
   266
      plotForm.FormBorderStyle = FormBorderStyle.SizableToolWindow;
moel@295
   267
      plotForm.ShowInTaskbar = false;
moel@295
   268
      plotForm.StartPosition = FormStartPosition.Manual;
moel@295
   269
      this.AddOwnedForm(plotForm);
moel@295
   270
      plotForm.Bounds = new Rectangle {
moel@295
   271
        X = settings.GetValue("plotForm.Location.X", -100000),
moel@295
   272
        Y = settings.GetValue("plotForm.Location.Y", 100),
moel@295
   273
        Width = settings.GetValue("plotForm.Width", 600),
moel@295
   274
        Height = settings.GetValue("plotForm.Height", 400)
moel@295
   275
      };
moel@295
   276
moel@295
   277
      showPlot = new UserOption("plotMenuItem", false, plotMenuItem, settings);
moel@295
   278
      plotLocation = new UserRadioGroup("plotLocation", 0,
moel@295
   279
        new[] { plotWindowMenuItem, plotBottomMenuItem, plotRightMenuItem },
moel@295
   280
        settings);
moel@295
   281
moel@295
   282
      showPlot.Changed += delegate(object sender, EventArgs e) {
moel@295
   283
        if (plotLocation.Value == 0) {
moel@295
   284
          if (showPlot.Value && this.Visible)
moel@295
   285
            plotForm.Show();
moel@295
   286
          else
moel@295
   287
            plotForm.Hide();
moel@295
   288
        } else {
moel@295
   289
          splitContainer.Panel2Collapsed = !showPlot.Value;
moel@295
   290
        }
moel@295
   291
        treeView.Invalidate();
moel@295
   292
      };
moel@295
   293
      plotLocation.Changed += delegate(object sender, EventArgs e) {
moel@295
   294
        switch (plotLocation.Value) {
moel@295
   295
          case 0:
moel@295
   296
            splitContainer.Panel2.Controls.Clear();
moel@295
   297
            splitContainer.Panel2Collapsed = true;
moel@295
   298
            plotForm.Controls.Add(plotPanel);
moel@295
   299
            if (showPlot.Value && this.Visible)
moel@295
   300
              plotForm.Show();
moel@295
   301
            break;
moel@295
   302
          case 1:
moel@295
   303
            plotForm.Controls.Clear();
moel@295
   304
            plotForm.Hide();
moel@295
   305
            splitContainer.Orientation = Orientation.Horizontal;
moel@295
   306
            splitContainer.Panel2.Controls.Add(plotPanel);
moel@295
   307
            splitContainer.Panel2Collapsed = !showPlot.Value;
moel@295
   308
            break;
moel@295
   309
          case 2:
moel@295
   310
            plotForm.Controls.Clear();
moel@295
   311
            plotForm.Hide();
moel@295
   312
            splitContainer.Orientation = Orientation.Vertical;
moel@295
   313
            splitContainer.Panel2.Controls.Add(plotPanel);
moel@295
   314
            splitContainer.Panel2Collapsed = !showPlot.Value;
moel@295
   315
            break;
moel@295
   316
        }
moel@295
   317
      };
moel@295
   318
      plotForm.Closing += delegate(object sender, CancelEventArgs e) {
moel@295
   319
        if (plotLocation.Value == 0) {
moel@295
   320
          showPlot.Value = false;
moel@295
   321
        }
moel@295
   322
        e.Cancel = true;
moel@295
   323
      };
moel@295
   324
      EventHandler moveOrResizePlotForm = delegate(object sender, EventArgs e) {
moel@295
   325
        if (plotForm.WindowState != FormWindowState.Minimized) {
moel@295
   326
          settings.SetValue("plotForm.Location.X", plotForm.Bounds.X);
moel@295
   327
          settings.SetValue("plotForm.Location.Y", plotForm.Bounds.Y);
moel@295
   328
          settings.SetValue("plotForm.Width", plotForm.Bounds.Width);
moel@295
   329
          settings.SetValue("plotForm.Height", plotForm.Bounds.Height);
moel@295
   330
        }
moel@295
   331
      };
moel@295
   332
      plotForm.Move += moveOrResizePlotForm;
moel@295
   333
      plotForm.Resize += moveOrResizePlotForm;
moel@295
   334
moel@295
   335
      plotForm.VisibleChanged += delegate(object sender, EventArgs e) {
moel@295
   336
        Rectangle bounds = new Rectangle(plotForm.Location, plotForm.Size);
moel@295
   337
        Screen screen = Screen.FromRectangle(bounds);
moel@295
   338
        Rectangle intersection =
moel@295
   339
          Rectangle.Intersect(screen.WorkingArea, bounds);
moel@295
   340
        if (intersection.Width < Math.Min(16, bounds.Width) ||
moel@295
   341
            intersection.Height < Math.Min(16, bounds.Height)) {
moel@295
   342
          plotForm.Location = new Point(
moel@295
   343
            screen.WorkingArea.Width / 2 - bounds.Width / 2,
moel@295
   344
            screen.WorkingArea.Height / 2 - bounds.Height / 2);
moel@295
   345
        }
moel@295
   346
      };
moel@295
   347
moel@295
   348
      this.VisibleChanged += delegate(object sender, EventArgs e) {
moel@295
   349
        if (this.Visible && showPlot.Value && plotLocation.Value == 0)
moel@295
   350
          plotForm.Show();
moel@295
   351
        else
moel@295
   352
          plotForm.Hide();
moel@295
   353
      };
moel@295
   354
    }
moel@128
   355
    
moel@64
   356
    private void SubHardwareAdded(IHardware hardware, Node node) {
moel@165
   357
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   358
      node.Nodes.Add(hardwareNode);
moel@64
   359
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   360
        SubHardwareAdded(subHardware, hardwareNode);  
moel@64
   361
    }
moel@64
   362
moel@28
   363
    private void HardwareAdded(IHardware hardware) {
moel@165
   364
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   365
      root.Nodes.Add(hardwareNode);
moel@64
   366
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   367
        SubHardwareAdded(subHardware, hardwareNode);     
moel@1
   368
    }
moel@1
   369
moel@28
   370
    private void HardwareRemoved(IHardware hardware) {      
moel@1
   371
      List<Node> nodesToRemove = new List<Node>();
moel@28
   372
      foreach (Node node in root.Nodes) {
moel@28
   373
        HardwareNode hardwareNode = node as HardwareNode;
moel@28
   374
        if (hardwareNode != null && hardwareNode.Hardware == hardware)
moel@28
   375
          nodesToRemove.Add(node);
moel@28
   376
      }
moel@1
   377
      foreach (Node node in nodesToRemove)
moel@1
   378
        root.Nodes.Remove(node);
moel@1
   379
    }
moel@1
   380
moel@111
   381
    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
moel@111
   382
      Node node = e.Node.Tag as Node;
moel@111
   383
      if (node != null) {
moel@1
   384
        Color color;
moel@111
   385
        if (node.IsVisible) {
moel@111
   386
          SensorNode sensorNode = node as SensorNode;
moel@111
   387
          if (plotMenuItem.Checked && sensorNode != null &&
moel@111
   388
            sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
moel@111
   389
            e.TextColor = color;
moel@111
   390
        } else {
moel@111
   391
          e.TextColor = Color.DarkGray;
moel@111
   392
        }
moel@1
   393
      }
moel@1
   394
    }
moel@1
   395
moel@1
   396
    private void UpdatePlotSelection(object sender, 
moel@1
   397
      TreePathEventArgs e) 
moel@1
   398
    {
moel@1
   399
      List<ISensor> selected = new List<ISensor>();
moel@1
   400
      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
moel@1
   401
      int colorIndex = 0;
moel@1
   402
      foreach (TreeNodeAdv node in treeView.AllNodes) {
moel@1
   403
        SensorNode sensorNode = node.Tag as SensorNode;
moel@1
   404
        if (sensorNode != null && 
moel@1
   405
          sensorNode.Sensor.SensorType == SensorType.Temperature) {
moel@1
   406
          if (sensorNode.Plot) {
moel@1
   407
            colors.Add(sensorNode.Sensor,
moel@1
   408
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   409
            selected.Add(sensorNode.Sensor);
moel@1
   410
          }
moel@1
   411
          colorIndex++;
moel@1
   412
        }
moel@1
   413
      }
moel@1
   414
      sensorPlotColors = colors;
moel@1
   415
      plotPanel.SetSensors(selected, colors);
moel@1
   416
    }
moel@1
   417
paulwerelds@223
   418
    private void nodeTextBoxText_EditorShowing(object sender,
paulwerelds@223
   419
      CancelEventArgs e) 
moel@141
   420
    {
moel@141
   421
      e.Cancel = !(treeView.CurrentNode != null &&
moel@275
   422
        (treeView.CurrentNode.Tag is SensorNode || 
moel@275
   423
         treeView.CurrentNode.Tag is HardwareNode));
moel@141
   424
    }
moel@141
   425
moel@1
   426
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   427
      NodeControlValueEventArgs e) {
moel@1
   428
      SensorNode node = e.Node.Tag as SensorNode;
moel@1
   429
      e.Value = (node != null) && 
moel@1
   430
        (node.Sensor.SensorType == SensorType.Temperature) && 
moel@1
   431
        plotMenuItem.Checked;
moel@1
   432
    }
moel@1
   433
moel@133
   434
    private void exitClick(object sender, EventArgs e) {
paulwerelds@198
   435
      Close();
moel@1
   436
    }
moel@1
   437
moel@86
   438
    private void timer_Tick(object sender, EventArgs e) {
moel@110
   439
      computer.Accept(updateVisitor);
moel@1
   440
      treeView.Invalidate();
moel@1
   441
      plotPanel.Invalidate();
paulwerelds@223
   442
      systemTray.Redraw();
moel@202
   443
      if (gadget != null)
moel@202
   444
        gadget.Redraw();
paulwerelds@223
   445
paulwerelds@223
   446
      if (wmiProvider != null)
paulwerelds@223
   447
        wmiProvider.Update();
moel@1
   448
    }
moel@1
   449
moel@128
   450
    private void SaveConfiguration() {
moel@128
   451
      foreach (TreeColumn column in treeView.Columns)
moel@166
   452
        settings.SetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   453
          column.Width);
moel@113
   454
moel@212
   455
      string fileName = Path.ChangeExtension(
moel@212
   456
          System.Windows.Forms.Application.ExecutablePath, ".config");
moel@212
   457
      try {
moel@212
   458
        settings.Save(fileName);
moel@212
   459
      } catch (UnauthorizedAccessException) {
paulwerelds@214
   460
        MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
moel@284
   461
          "The current settings could not be saved.",
moel@284
   462
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@284
   463
      } catch (IOException) {
moel@284
   464
        MessageBox.Show("The path '" + fileName + "' is not writeable. " +
moel@284
   465
          "The current settings could not be saved.",
moel@212
   466
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@212
   467
      }
moel@128
   468
    }
moel@128
   469
paulwerelds@214
   470
    private void MainForm_Load(object sender, EventArgs e) {
paulwerelds@214
   471
      Rectangle newBounds = new Rectangle {
paulwerelds@214
   472
        X = settings.GetValue("mainForm.Location.X", Location.X),
paulwerelds@214
   473
        Y = settings.GetValue("mainForm.Location.Y", Location.Y),
paulwerelds@214
   474
        Width = settings.GetValue("mainForm.Width", 470),
paulwerelds@214
   475
        Height = settings.GetValue("mainForm.Height", 640)
paulwerelds@214
   476
      };
paulwerelds@214
   477
paulwerelds@223
   478
      Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
paulwerelds@214
   479
        int.MinValue, int.MinValue);
paulwerelds@214
   480
paulwerelds@214
   481
      foreach (Screen screen in Screen.AllScreens)
paulwerelds@223
   482
        fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds);
paulwerelds@214
   483
paulwerelds@223
   484
      Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds);
paulwerelds@214
   485
      if (intersection.Width < 20 || intersection.Height < 20 ||
paulwerelds@214
   486
        !settings.Contains("mainForm.Location.X")
paulwerelds@214
   487
      ) {
paulwerelds@214
   488
        newBounds.X = (Screen.PrimaryScreen.WorkingArea.Width / 2) -
paulwerelds@214
   489
                      (newBounds.Width/2);
paulwerelds@214
   490
paulwerelds@214
   491
        newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) -
paulwerelds@214
   492
                      (newBounds.Height / 2);
paulwerelds@214
   493
      }
paulwerelds@214
   494
paulwerelds@214
   495
      this.Bounds = newBounds;
paulwerelds@214
   496
    }
paulwerelds@214
   497
    
paulwerelds@214
   498
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@156
   499
      Visible = false;
moel@128
   500
      SaveConfiguration();
moel@128
   501
moel@262
   502
      systemTray.IsMainIconEnabled = false;
moel@262
   503
      timer.Enabled = false;            
moel@28
   504
      computer.Close();
moel@262
   505
      systemTray.Dispose();
moel@1
   506
    }
moel@1
   507
moel@156
   508
    private void aboutMenuItem_Click(object sender, EventArgs e) {
moel@1
   509
      new AboutBox().ShowDialog();
moel@1
   510
    }
moel@1
   511
moel@1
   512
    private void treeView_Click(object sender, EventArgs e) {
moel@275
   513
moel@1
   514
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   515
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   516
        return;
moel@1
   517
paulwerelds@223
   518
      NodeControlInfo info = treeView.GetNodeControlInfoAt(
paulwerelds@223
   519
        new Point(m.X, m.Y)
paulwerelds@223
   520
      );
moel@156
   521
      treeView.SelectedNode = info.Node;
moel@156
   522
      if (info.Node != null) {
moel@40
   523
        SensorNode node = info.Node.Tag as SensorNode;
moel@40
   524
        if (node != null && node.Sensor != null) {
moel@275
   525
          treeContextMenu.MenuItems.Clear();
moel@63
   526
          if (node.Sensor.Parameters.Length > 0) {
moel@156
   527
            MenuItem item = new MenuItem("Parameters...");
moel@63
   528
            item.Click += delegate(object obj, EventArgs args) {
moel@63
   529
              ShowParameterForm(node.Sensor);
moel@63
   530
            };
moel@275
   531
            treeContextMenu.MenuItems.Add(item);
moel@63
   532
          }
moel@156
   533
          if (nodeTextBoxText.EditEnabled) {
moel@156
   534
            MenuItem item = new MenuItem("Rename");
moel@141
   535
            item.Click += delegate(object obj, EventArgs args) {
moel@156
   536
              nodeTextBoxText.BeginEdit();
moel@141
   537
            };
moel@275
   538
            treeContextMenu.MenuItems.Add(item);
moel@176
   539
          }
moel@111
   540
          if (node.IsVisible) {
moel@156
   541
            MenuItem item = new MenuItem("Hide");
moel@111
   542
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   543
              node.IsVisible = false;
moel@111
   544
            };
moel@275
   545
            treeContextMenu.MenuItems.Add(item);
moel@111
   546
          } else {
moel@156
   547
            MenuItem item = new MenuItem("Unhide");
moel@111
   548
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   549
              node.IsVisible = true;
moel@111
   550
            };
moel@275
   551
            treeContextMenu.MenuItems.Add(item);
moel@176
   552
          }
moel@275
   553
          treeContextMenu.MenuItems.Add(new MenuItem("-"));
moel@178
   554
          {
moel@178
   555
            MenuItem item = new MenuItem("Show in Tray");
moel@178
   556
            item.Checked = systemTray.Contains(node.Sensor);
moel@178
   557
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   558
              if (item.Checked)
moel@178
   559
                systemTray.Remove(node.Sensor);
moel@178
   560
              else
moel@178
   561
                systemTray.Add(node.Sensor, true);
moel@178
   562
            };
moel@275
   563
            treeContextMenu.MenuItems.Add(item);
moel@178
   564
          }
moel@211
   565
          if (gadget != null) {
moel@178
   566
            MenuItem item = new MenuItem("Show in Gadget");
moel@178
   567
            item.Checked = gadget.Contains(node.Sensor);
moel@178
   568
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   569
              if (item.Checked) {
moel@178
   570
                gadget.Remove(node.Sensor);
moel@178
   571
              } else {
moel@178
   572
                gadget.Add(node.Sensor);
moel@178
   573
              }
moel@178
   574
            };
moel@275
   575
            treeContextMenu.MenuItems.Add(item);
moel@178
   576
          }
moel@247
   577
          if (node.Sensor.Control != null) {
moel@275
   578
            treeContextMenu.MenuItems.Add(new MenuItem("-"));
moel@247
   579
            IControl control = node.Sensor.Control;
moel@247
   580
            MenuItem controlItem = new MenuItem("Control");
moel@247
   581
            MenuItem defaultItem = new MenuItem("Default");
moel@247
   582
            defaultItem.Checked = control.ControlMode == ControlMode.Default;
moel@247
   583
            controlItem.MenuItems.Add(defaultItem);
moel@247
   584
            defaultItem.Click += delegate(object obj, EventArgs args) {
moel@247
   585
              control.SetDefault();
moel@247
   586
            };
moel@275
   587
            MenuItem manualItem = new MenuItem("Manual");
moel@247
   588
            controlItem.MenuItems.Add(manualItem);
moel@247
   589
            manualItem.Checked = control.ControlMode == ControlMode.Software;
moel@247
   590
            for (int i = 0; i <= 100; i += 5) {
moel@247
   591
              if (i <= control.MaxSoftwareValue &&
moel@275
   592
                  i >= control.MinSoftwareValue) {
moel@247
   593
                MenuItem item = new MenuItem(i + " %");
moel@247
   594
                manualItem.MenuItems.Add(item);
moel@247
   595
                item.Checked = control.ControlMode == ControlMode.Software &&
moel@247
   596
                  Math.Round(control.SoftwareValue) == i;
moel@247
   597
                int softwareValue = i;
moel@247
   598
                item.Click += delegate(object obj, EventArgs args) {
moel@247
   599
                  control.SetSoftware(softwareValue);
moel@247
   600
                };
moel@247
   601
              }
moel@247
   602
            }
moel@275
   603
            treeContextMenu.MenuItems.Add(controlItem);
moel@247
   604
          }
moel@176
   605
moel@275
   606
          treeContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@275
   607
        }
moel@275
   608
moel@275
   609
        HardwareNode hardwareNode = info.Node.Tag as HardwareNode;
moel@275
   610
        if (hardwareNode != null && hardwareNode.Hardware != null) {
moel@275
   611
          treeContextMenu.MenuItems.Clear();
moel@275
   612
moel@275
   613
          if (nodeTextBoxText.EditEnabled) {
moel@275
   614
            MenuItem item = new MenuItem("Rename");
moel@275
   615
            item.Click += delegate(object obj, EventArgs args) {
moel@275
   616
              nodeTextBoxText.BeginEdit();
moel@275
   617
            };
moel@275
   618
            treeContextMenu.MenuItems.Add(item);
moel@275
   619
          }
moel@275
   620
moel@275
   621
          treeContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@40
   622
        }
moel@40
   623
      }
moel@1
   624
    }
moel@1
   625
moel@156
   626
    private void saveReportMenuItem_Click(object sender, EventArgs e) {
moel@83
   627
      string report = computer.GetReport();
moel@83
   628
      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
moel@83
   629
        using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
moel@83
   630
          w.Write(report);
moel@83
   631
        }
moel@83
   632
      }
moel@1
   633
    }
moel@1
   634
moel@82
   635
    private void SysTrayHideShow() {
moel@82
   636
      Visible = !Visible;
moel@82
   637
      if (Visible)
moel@82
   638
        Activate();    
moel@27
   639
    }
moel@27
   640
moel@27
   641
    protected override void WndProc(ref Message m) {
moel@27
   642
      const int WM_SYSCOMMAND = 0x112;
moel@27
   643
      const int SC_MINIMIZE = 0xF020;
paulwerelds@198
   644
      const int SC_CLOSE = 0xF060;
paulwerelds@198
   645
moel@156
   646
      if (minimizeToTray.Value && 
moel@28
   647
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
moel@82
   648
        SysTrayHideShow();
paulwerelds@198
   649
      } else if(minimizeOnClose.Value && 
paulwerelds@198
   650
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE) {
paulwerelds@198
   651
        /*
paulwerelds@198
   652
         * Apparently the user wants to minimize rather than close
paulwerelds@198
   653
         * Now we still need to check if we're going to the tray or not
paulwerelds@198
   654
         * 
paulwerelds@198
   655
         * Note: the correct way to do this would be to send out SC_MINIMIZE,
paulwerelds@198
   656
         * but since the code here is so simple,
paulwerelds@198
   657
         * that would just be a waste of time.
paulwerelds@198
   658
         */
paulwerelds@198
   659
        if (minimizeToTray.Value)
paulwerelds@198
   660
          SysTrayHideShow();
paulwerelds@198
   661
        else
paulwerelds@198
   662
          WindowState = FormWindowState.Minimized;
moel@27
   663
      } else {      
moel@27
   664
        base.WndProc(ref m);
moel@27
   665
      }
moel@27
   666
    }
moel@27
   667
moel@82
   668
    private void hideShowClick(object sender, EventArgs e) {
moel@82
   669
      SysTrayHideShow();
moel@27
   670
    }
moel@27
   671
moel@63
   672
    private void ShowParameterForm(ISensor sensor) {
moel@63
   673
      ParameterForm form = new ParameterForm();
moel@63
   674
      form.Parameters = sensor.Parameters;
moel@63
   675
      form.captionLabel.Text = sensor.Name;
moel@63
   676
      form.ShowDialog();
moel@63
   677
    }
moel@63
   678
moel@63
   679
    private void treeView_NodeMouseDoubleClick(object sender, 
moel@63
   680
      TreeNodeAdvMouseEventArgs e) {
moel@63
   681
      SensorNode node = e.Node.Tag as SensorNode;
moel@63
   682
      if (node != null && node.Sensor != null && 
moel@63
   683
        node.Sensor.Parameters.Length > 0) {
moel@63
   684
        ShowParameterForm(node.Sensor);
moel@63
   685
      }
moel@63
   686
    }
moel@82
   687
moel@156
   688
    private void celciusMenuItem_Click(object sender, EventArgs e) {
moel@156
   689
      celciusMenuItem.Checked = true;
moel@156
   690
      fahrenheitMenuItem.Checked = false;
moel@165
   691
      unitManager.TemperatureUnit = TemperatureUnit.Celcius;
moel@122
   692
    }
moel@122
   693
moel@156
   694
    private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
moel@156
   695
      celciusMenuItem.Checked = false;
moel@156
   696
      fahrenheitMenuItem.Checked = true;
moel@165
   697
      unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
moel@122
   698
    }
moel@150
   699
moel@156
   700
    private void sumbitReportMenuItem_Click(object sender, EventArgs e) 
moel@150
   701
    {
moel@150
   702
      ReportForm form = new ReportForm();
moel@150
   703
      form.Report = computer.GetReport();
moel@150
   704
      form.ShowDialog();      
moel@150
   705
    }
moel@151
   706
moel@151
   707
    private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
moel@159
   708
      computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
moel@159
   709
        sensor.ResetMin();
moel@159
   710
        sensor.ResetMax();
moel@159
   711
      }));
moel@151
   712
    }
moel@241
   713
moel@241
   714
    private void MainForm_MoveOrResize(object sender, EventArgs e) {
moel@241
   715
      if (WindowState != FormWindowState.Minimized) {
moel@241
   716
        settings.SetValue("mainForm.Location.X", Bounds.X);
moel@241
   717
        settings.SetValue("mainForm.Location.Y", Bounds.Y);
moel@241
   718
        settings.SetValue("mainForm.Width", Bounds.Width);
moel@241
   719
        settings.SetValue("mainForm.Height", Bounds.Height);
moel@241
   720
      }
moel@241
   721
    }
moel@262
   722
moel@262
   723
    private void resetClick(object sender, EventArgs e) {
moel@262
   724
      // disable the fallback MainIcon during reset, otherwise icon visibility
moel@262
   725
      // might be lost 
moel@262
   726
      systemTray.IsMainIconEnabled = false;
moel@262
   727
      computer.Close();
moel@262
   728
      computer.Open();
moel@262
   729
      // restore the MainIcon setting
moel@262
   730
      systemTray.IsMainIconEnabled = minimizeToTray.Value;
moel@262
   731
    }
moel@287
   732
moel@287
   733
    private void treeView_MouseMove(object sender, MouseEventArgs e) {
moel@288
   734
      selectionDragging = selectionDragging &
moel@288
   735
        (e.Button & (MouseButtons.Left | MouseButtons.Right)) > 0; 
moel@288
   736
moel@288
   737
      if (selectionDragging)
moel@288
   738
        treeView.SelectedNode = treeView.GetNodeAt(e.Location);     
moel@288
   739
    }
moel@288
   740
moel@288
   741
    private void treeView_MouseDown(object sender, MouseEventArgs e) {
moel@288
   742
      selectionDragging = true;
moel@288
   743
    }
moel@288
   744
moel@288
   745
    private void treeView_MouseUp(object sender, MouseEventArgs e) {
moel@288
   746
      selectionDragging = false;
moel@287
   747
    }
moel@1
   748
  }
moel@1
   749
}