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