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