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