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