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