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