GUI/MainForm.cs
author moel.mich
Sun, 09 Jun 2013 16:08:59 +0000
changeset 395 d1f25b504845
parent 387 87093432c843
child 400 f4e2e3e69651
permissions -rw-r--r--
Added the new OxyPlot based plot implementation.
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@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@395
   462
        if (sensorNode != null) {
moel@1
   463
          if (sensorNode.Plot) {
moel@1
   464
            colors.Add(sensorNode.Sensor,
moel@1
   465
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   466
            selected.Add(sensorNode.Sensor);
moel@1
   467
          }
moel@1
   468
          colorIndex++;
moel@1
   469
        }
moel@1
   470
      }
moel@1
   471
      sensorPlotColors = colors;
moel@1
   472
      plotPanel.SetSensors(selected, colors);
moel@1
   473
    }
moel@1
   474
paulwerelds@223
   475
    private void nodeTextBoxText_EditorShowing(object sender,
paulwerelds@223
   476
      CancelEventArgs e) 
moel@141
   477
    {
moel@141
   478
      e.Cancel = !(treeView.CurrentNode != null &&
moel@275
   479
        (treeView.CurrentNode.Tag is SensorNode || 
moel@275
   480
         treeView.CurrentNode.Tag is HardwareNode));
moel@141
   481
    }
moel@141
   482
moel@1
   483
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   484
      NodeControlValueEventArgs e) {
moel@1
   485
      SensorNode node = e.Node.Tag as SensorNode;
moel@395
   486
      e.Value = (node != null) && plotMenuItem.Checked;
moel@1
   487
    }
moel@1
   488
moel@133
   489
    private void exitClick(object sender, EventArgs e) {
paulwerelds@198
   490
      Close();
moel@1
   491
    }
moel@1
   492
moel@86
   493
    private void timer_Tick(object sender, EventArgs e) {
moel@110
   494
      computer.Accept(updateVisitor);
moel@1
   495
      treeView.Invalidate();
moel@395
   496
      plotPanel.InvalidatePlot();
paulwerelds@223
   497
      systemTray.Redraw();
moel@202
   498
      if (gadget != null)
moel@202
   499
        gadget.Redraw();
paulwerelds@223
   500
paulwerelds@223
   501
      if (wmiProvider != null)
paulwerelds@223
   502
        wmiProvider.Update();
moel@1
   503
    }
moel@1
   504
moel@128
   505
    private void SaveConfiguration() {
moel@395
   506
      plotPanel.SetCurrentSettings();
moel@128
   507
      foreach (TreeColumn column in treeView.Columns)
moel@166
   508
        settings.SetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   509
          column.Width);
moel@113
   510
moel@348
   511
      this.settings.SetValue("listenerPort", server.ListenerPort);
moel@348
   512
moel@212
   513
      string fileName = Path.ChangeExtension(
moel@212
   514
          System.Windows.Forms.Application.ExecutablePath, ".config");
moel@212
   515
      try {
moel@212
   516
        settings.Save(fileName);
moel@212
   517
      } catch (UnauthorizedAccessException) {
paulwerelds@214
   518
        MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
moel@284
   519
          "The current settings could not be saved.",
moel@284
   520
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@284
   521
      } catch (IOException) {
moel@284
   522
        MessageBox.Show("The path '" + fileName + "' is not writeable. " +
moel@284
   523
          "The current settings could not be saved.",
moel@212
   524
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@212
   525
      }
moel@128
   526
    }
moel@128
   527
paulwerelds@214
   528
    private void MainForm_Load(object sender, EventArgs e) {
paulwerelds@214
   529
      Rectangle newBounds = new Rectangle {
paulwerelds@214
   530
        X = settings.GetValue("mainForm.Location.X", Location.X),
paulwerelds@214
   531
        Y = settings.GetValue("mainForm.Location.Y", Location.Y),
paulwerelds@214
   532
        Width = settings.GetValue("mainForm.Width", 470),
paulwerelds@214
   533
        Height = settings.GetValue("mainForm.Height", 640)
paulwerelds@214
   534
      };
paulwerelds@214
   535
paulwerelds@223
   536
      Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
paulwerelds@214
   537
        int.MinValue, int.MinValue);
paulwerelds@214
   538
paulwerelds@214
   539
      foreach (Screen screen in Screen.AllScreens)
paulwerelds@223
   540
        fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds);
paulwerelds@214
   541
paulwerelds@223
   542
      Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds);
paulwerelds@214
   543
      if (intersection.Width < 20 || intersection.Height < 20 ||
paulwerelds@214
   544
        !settings.Contains("mainForm.Location.X")
paulwerelds@214
   545
      ) {
paulwerelds@214
   546
        newBounds.X = (Screen.PrimaryScreen.WorkingArea.Width / 2) -
paulwerelds@214
   547
                      (newBounds.Width/2);
paulwerelds@214
   548
paulwerelds@214
   549
        newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) -
paulwerelds@214
   550
                      (newBounds.Height / 2);
paulwerelds@214
   551
      }
paulwerelds@214
   552
paulwerelds@214
   553
      this.Bounds = newBounds;
paulwerelds@214
   554
    }
paulwerelds@214
   555
    
paulwerelds@214
   556
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@298
   557
      Visible = false;      
moel@262
   558
      systemTray.IsMainIconEnabled = false;
moel@262
   559
      timer.Enabled = false;            
moel@28
   560
      computer.Close();
moel@298
   561
      SaveConfiguration();
moel@348
   562
      if (runWebServer.Value)
moel@348
   563
          server.Quit();
moel@262
   564
      systemTray.Dispose();
moel@1
   565
    }
moel@1
   566
moel@156
   567
    private void aboutMenuItem_Click(object sender, EventArgs e) {
moel@1
   568
      new AboutBox().ShowDialog();
moel@1
   569
    }
moel@1
   570
moel@1
   571
    private void treeView_Click(object sender, EventArgs e) {
moel@275
   572
moel@1
   573
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   574
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   575
        return;
moel@1
   576
paulwerelds@223
   577
      NodeControlInfo info = treeView.GetNodeControlInfoAt(
paulwerelds@223
   578
        new Point(m.X, m.Y)
paulwerelds@223
   579
      );
moel@156
   580
      treeView.SelectedNode = info.Node;
moel@156
   581
      if (info.Node != null) {
moel@40
   582
        SensorNode node = info.Node.Tag as SensorNode;
moel@40
   583
        if (node != null && node.Sensor != null) {
moel@275
   584
          treeContextMenu.MenuItems.Clear();
moel@63
   585
          if (node.Sensor.Parameters.Length > 0) {
moel@156
   586
            MenuItem item = new MenuItem("Parameters...");
moel@63
   587
            item.Click += delegate(object obj, EventArgs args) {
moel@63
   588
              ShowParameterForm(node.Sensor);
moel@63
   589
            };
moel@275
   590
            treeContextMenu.MenuItems.Add(item);
moel@63
   591
          }
moel@156
   592
          if (nodeTextBoxText.EditEnabled) {
moel@156
   593
            MenuItem item = new MenuItem("Rename");
moel@141
   594
            item.Click += delegate(object obj, EventArgs args) {
moel@156
   595
              nodeTextBoxText.BeginEdit();
moel@141
   596
            };
moel@275
   597
            treeContextMenu.MenuItems.Add(item);
moel@176
   598
          }
moel@111
   599
          if (node.IsVisible) {
moel@156
   600
            MenuItem item = new MenuItem("Hide");
moel@111
   601
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   602
              node.IsVisible = false;
moel@111
   603
            };
moel@275
   604
            treeContextMenu.MenuItems.Add(item);
moel@111
   605
          } else {
moel@156
   606
            MenuItem item = new MenuItem("Unhide");
moel@111
   607
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   608
              node.IsVisible = true;
moel@111
   609
            };
moel@275
   610
            treeContextMenu.MenuItems.Add(item);
moel@176
   611
          }
moel@275
   612
          treeContextMenu.MenuItems.Add(new MenuItem("-"));
moel@178
   613
          {
moel@178
   614
            MenuItem item = new MenuItem("Show in Tray");
moel@178
   615
            item.Checked = systemTray.Contains(node.Sensor);
moel@178
   616
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   617
              if (item.Checked)
moel@178
   618
                systemTray.Remove(node.Sensor);
moel@178
   619
              else
moel@178
   620
                systemTray.Add(node.Sensor, true);
moel@178
   621
            };
moel@275
   622
            treeContextMenu.MenuItems.Add(item);
moel@178
   623
          }
moel@211
   624
          if (gadget != null) {
moel@178
   625
            MenuItem item = new MenuItem("Show in Gadget");
moel@178
   626
            item.Checked = gadget.Contains(node.Sensor);
moel@178
   627
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   628
              if (item.Checked) {
moel@178
   629
                gadget.Remove(node.Sensor);
moel@178
   630
              } else {
moel@178
   631
                gadget.Add(node.Sensor);
moel@178
   632
              }
moel@178
   633
            };
moel@275
   634
            treeContextMenu.MenuItems.Add(item);
moel@178
   635
          }
moel@247
   636
          if (node.Sensor.Control != null) {
moel@275
   637
            treeContextMenu.MenuItems.Add(new MenuItem("-"));
moel@247
   638
            IControl control = node.Sensor.Control;
moel@247
   639
            MenuItem controlItem = new MenuItem("Control");
moel@247
   640
            MenuItem defaultItem = new MenuItem("Default");
moel@247
   641
            defaultItem.Checked = control.ControlMode == ControlMode.Default;
moel@247
   642
            controlItem.MenuItems.Add(defaultItem);
moel@247
   643
            defaultItem.Click += delegate(object obj, EventArgs args) {
moel@247
   644
              control.SetDefault();
moel@247
   645
            };
moel@275
   646
            MenuItem manualItem = new MenuItem("Manual");
moel@247
   647
            controlItem.MenuItems.Add(manualItem);
moel@247
   648
            manualItem.Checked = control.ControlMode == ControlMode.Software;
moel@247
   649
            for (int i = 0; i <= 100; i += 5) {
moel@247
   650
              if (i <= control.MaxSoftwareValue &&
moel@275
   651
                  i >= control.MinSoftwareValue) {
moel@247
   652
                MenuItem item = new MenuItem(i + " %");
moel@378
   653
                item.RadioCheck = true;
moel@247
   654
                manualItem.MenuItems.Add(item);
moel@247
   655
                item.Checked = control.ControlMode == ControlMode.Software &&
moel@247
   656
                  Math.Round(control.SoftwareValue) == i;
moel@247
   657
                int softwareValue = i;
moel@247
   658
                item.Click += delegate(object obj, EventArgs args) {
moel@247
   659
                  control.SetSoftware(softwareValue);
moel@247
   660
                };
moel@247
   661
              }
moel@247
   662
            }
moel@275
   663
            treeContextMenu.MenuItems.Add(controlItem);
moel@247
   664
          }
moel@176
   665
moel@275
   666
          treeContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@275
   667
        }
moel@275
   668
moel@275
   669
        HardwareNode hardwareNode = info.Node.Tag as HardwareNode;
moel@275
   670
        if (hardwareNode != null && hardwareNode.Hardware != null) {
moel@275
   671
          treeContextMenu.MenuItems.Clear();
moel@275
   672
moel@275
   673
          if (nodeTextBoxText.EditEnabled) {
moel@275
   674
            MenuItem item = new MenuItem("Rename");
moel@275
   675
            item.Click += delegate(object obj, EventArgs args) {
moel@275
   676
              nodeTextBoxText.BeginEdit();
moel@275
   677
            };
moel@275
   678
            treeContextMenu.MenuItems.Add(item);
moel@275
   679
          }
moel@275
   680
moel@275
   681
          treeContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@40
   682
        }
moel@40
   683
      }
moel@1
   684
    }
moel@1
   685
moel@156
   686
    private void saveReportMenuItem_Click(object sender, EventArgs e) {
moel@83
   687
      string report = computer.GetReport();
moel@83
   688
      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
moel@83
   689
        using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
moel@83
   690
          w.Write(report);
moel@83
   691
        }
moel@83
   692
      }
moel@1
   693
    }
moel@1
   694
moel@82
   695
    private void SysTrayHideShow() {
moel@82
   696
      Visible = !Visible;
moel@82
   697
      if (Visible)
moel@82
   698
        Activate();    
moel@27
   699
    }
moel@27
   700
moel@27
   701
    protected override void WndProc(ref Message m) {
moel@27
   702
      const int WM_SYSCOMMAND = 0x112;
moel@27
   703
      const int SC_MINIMIZE = 0xF020;
paulwerelds@198
   704
      const int SC_CLOSE = 0xF060;
paulwerelds@198
   705
moel@156
   706
      if (minimizeToTray.Value && 
moel@335
   707
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt64() == SC_MINIMIZE) {
moel@82
   708
        SysTrayHideShow();
moel@335
   709
      } else if (minimizeOnClose.Value &&
moel@335
   710
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt64() == SC_CLOSE) {
paulwerelds@198
   711
        /*
paulwerelds@198
   712
         * Apparently the user wants to minimize rather than close
paulwerelds@198
   713
         * Now we still need to check if we're going to the tray or not
paulwerelds@198
   714
         * 
paulwerelds@198
   715
         * Note: the correct way to do this would be to send out SC_MINIMIZE,
paulwerelds@198
   716
         * but since the code here is so simple,
paulwerelds@198
   717
         * that would just be a waste of time.
paulwerelds@198
   718
         */
paulwerelds@198
   719
        if (minimizeToTray.Value)
paulwerelds@198
   720
          SysTrayHideShow();
paulwerelds@198
   721
        else
paulwerelds@198
   722
          WindowState = FormWindowState.Minimized;
moel@27
   723
      } else {      
moel@27
   724
        base.WndProc(ref m);
moel@27
   725
      }
moel@27
   726
    }
moel@27
   727
moel@82
   728
    private void hideShowClick(object sender, EventArgs e) {
moel@82
   729
      SysTrayHideShow();
moel@27
   730
    }
moel@27
   731
moel@63
   732
    private void ShowParameterForm(ISensor sensor) {
moel@63
   733
      ParameterForm form = new ParameterForm();
moel@63
   734
      form.Parameters = sensor.Parameters;
moel@63
   735
      form.captionLabel.Text = sensor.Name;
moel@63
   736
      form.ShowDialog();
moel@63
   737
    }
moel@63
   738
moel@63
   739
    private void treeView_NodeMouseDoubleClick(object sender, 
moel@63
   740
      TreeNodeAdvMouseEventArgs e) {
moel@63
   741
      SensorNode node = e.Node.Tag as SensorNode;
moel@63
   742
      if (node != null && node.Sensor != null && 
moel@63
   743
        node.Sensor.Parameters.Length > 0) {
moel@63
   744
        ShowParameterForm(node.Sensor);
moel@63
   745
      }
moel@63
   746
    }
moel@82
   747
moel@299
   748
    private void celsiusMenuItem_Click(object sender, EventArgs e) {
moel@299
   749
      celsiusMenuItem.Checked = true;
moel@156
   750
      fahrenheitMenuItem.Checked = false;
moel@299
   751
      unitManager.TemperatureUnit = TemperatureUnit.Celsius;
moel@122
   752
    }
moel@122
   753
moel@156
   754
    private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
moel@299
   755
      celsiusMenuItem.Checked = false;
moel@156
   756
      fahrenheitMenuItem.Checked = true;
moel@165
   757
      unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
moel@122
   758
    }
moel@150
   759
moel@156
   760
    private void sumbitReportMenuItem_Click(object sender, EventArgs e) 
moel@150
   761
    {
moel@150
   762
      ReportForm form = new ReportForm();
moel@150
   763
      form.Report = computer.GetReport();
moel@150
   764
      form.ShowDialog();      
moel@150
   765
    }
moel@151
   766
moel@151
   767
    private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
moel@159
   768
      computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
moel@159
   769
        sensor.ResetMin();
moel@159
   770
        sensor.ResetMax();
moel@159
   771
      }));
moel@151
   772
    }
moel@241
   773
moel@241
   774
    private void MainForm_MoveOrResize(object sender, EventArgs e) {
moel@241
   775
      if (WindowState != FormWindowState.Minimized) {
moel@241
   776
        settings.SetValue("mainForm.Location.X", Bounds.X);
moel@241
   777
        settings.SetValue("mainForm.Location.Y", Bounds.Y);
moel@241
   778
        settings.SetValue("mainForm.Width", Bounds.Width);
moel@241
   779
        settings.SetValue("mainForm.Height", Bounds.Height);
moel@241
   780
      }
moel@241
   781
    }
moel@262
   782
moel@262
   783
    private void resetClick(object sender, EventArgs e) {
moel@262
   784
      // disable the fallback MainIcon during reset, otherwise icon visibility
moel@262
   785
      // might be lost 
moel@262
   786
      systemTray.IsMainIconEnabled = false;
moel@262
   787
      computer.Close();
moel@262
   788
      computer.Open();
moel@262
   789
      // restore the MainIcon setting
moel@262
   790
      systemTray.IsMainIconEnabled = minimizeToTray.Value;
moel@262
   791
    }
moel@287
   792
moel@287
   793
    private void treeView_MouseMove(object sender, MouseEventArgs e) {
moel@288
   794
      selectionDragging = selectionDragging &
moel@288
   795
        (e.Button & (MouseButtons.Left | MouseButtons.Right)) > 0; 
moel@288
   796
moel@288
   797
      if (selectionDragging)
moel@288
   798
        treeView.SelectedNode = treeView.GetNodeAt(e.Location);     
moel@288
   799
    }
moel@288
   800
moel@288
   801
    private void treeView_MouseDown(object sender, MouseEventArgs e) {
moel@288
   802
      selectionDragging = true;
moel@288
   803
    }
moel@288
   804
moel@288
   805
    private void treeView_MouseUp(object sender, MouseEventArgs e) {
moel@288
   806
      selectionDragging = false;
moel@287
   807
    }
moel@348
   808
moel@348
   809
    private void serverPortMenuItem_Click(object sender, EventArgs e) {
moel@348
   810
      new PortForm(this).ShowDialog();
moel@348
   811
    }
moel@348
   812
moel@348
   813
    public HttpServer Server {
moel@348
   814
      get { return server; }
moel@348
   815
    }
moel@348
   816
moel@1
   817
  }
moel@1
   818
}