GUI/MainForm.cs
author paulwerelds
Sat, 16 Oct 2010 13:29:06 +0000
changeset 223 39f73ac8c2f4
parent 214 9ddbb54998ac
child 225 04dbd92d1116
permissions -rw-r--r--
Added a WMI provider, documentation to follow.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
paulwerelds@223
    22
  Contributor(s): Paul Werelds
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.ComponentModel;
moel@1
    41
using System.Drawing;
moel@83
    42
using System.IO;
moel@1
    43
using System.Windows.Forms;
moel@1
    44
using Aga.Controls.Tree;
moel@1
    45
using Aga.Controls.Tree.NodeControls;
moel@1
    46
using OpenHardwareMonitor.Hardware;
paulwerelds@223
    47
using OpenHardwareMonitor.WMIProvider;
moel@1
    48
moel@1
    49
namespace OpenHardwareMonitor.GUI {
moel@1
    50
  public partial class MainForm : Form {
moel@1
    51
moel@165
    52
    private PersistentSettings settings;
moel@165
    53
    private UnitManager unitManager;
moel@165
    54
    private Computer computer;
moel@1
    55
    private Node root;
moel@1
    56
    private TreeModel treeModel;
moel@1
    57
    private IDictionary<ISensor, Color> sensorPlotColors = 
moel@1
    58
      new Dictionary<ISensor, Color>();
moel@1
    59
    private Color[] plotColorPalette;
moel@133
    60
    private SystemTray systemTray;    
moel@82
    61
    private StartupManager startupManager = new StartupManager();
moel@110
    62
    private UpdateVisitor updateVisitor = new UpdateVisitor();
moel@176
    63
    private SensorGadget gadget;
moel@1
    64
moel@156
    65
    private UserOption showHiddenSensors;
moel@156
    66
    private UserOption showPlot;
moel@156
    67
    private UserOption showValue;
moel@156
    68
    private UserOption showMin;
moel@156
    69
    private UserOption showMax;
moel@156
    70
    private UserOption startMinimized;
moel@156
    71
    private UserOption minimizeToTray;
paulwerelds@198
    72
    private UserOption minimizeOnClose;
moel@156
    73
    private UserOption autoStart;
moel@156
    74
    private UserOption readHddSensors;
moel@176
    75
    private UserOption showGadget;
paulwerelds@223
    76
    private UserOption enableWmiProvider;
paulwerelds@223
    77
paulwerelds@223
    78
    private WmiProvider wmiProvider;
moel@156
    79
moel@28
    80
    public MainForm() {      
moel@1
    81
      InitializeComponent();
moel@156
    82
moel@165
    83
      this.settings = new PersistentSettings();      
moel@165
    84
      this.settings.Load(Path.ChangeExtension(
paulwerelds@198
    85
        Application.ExecutablePath, ".config"));
moel@165
    86
moel@165
    87
      this.unitManager = new UnitManager(settings);
moel@165
    88
moel@156
    89
      // set the DockStyle here, to avoid conflicts with the MainMenu
moel@156
    90
      this.splitContainer.Dock = DockStyle.Fill;
moel@202
    91
            
moel@1
    92
      this.Font = SystemFonts.MessageBoxFont;
moel@1
    93
      treeView.Font = SystemFonts.MessageBoxFont;
moel@63
    94
      plotPanel.Font = SystemFonts.MessageBoxFont;
moel@1
    95
      
moel@133
    96
      nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
moel@133
    97
      nodeCheckBox.CheckStateChanged += UpdatePlotSelection;
moel@133
    98
      nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
moel@133
    99
      nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
moel@133
   100
      nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
moel@133
   101
      nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
moel@141
   102
      nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
moel@1
   103
moel@113
   104
      foreach (TreeColumn column in treeView.Columns) 
moel@165
   105
        column.Width = Math.Max(20, Math.Min(400,
moel@166
   106
          settings.GetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   107
          column.Width)));
moel@113
   108
moel@1
   109
      treeModel = new TreeModel();
moel@1
   110
      root = new Node(System.Environment.MachineName);
moel@1
   111
      root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
moel@1
   112
      
moel@1
   113
      treeModel.Nodes.Add(root);
moel@165
   114
      treeView.Model = treeModel;
moel@40
   115
moel@165
   116
      this.computer = new Computer(settings);
moel@165
   117
moel@165
   118
      systemTray = new SystemTray(computer, settings);
moel@133
   119
      systemTray.HideShowCommand += hideShowClick;
moel@133
   120
      systemTray.ExitCommand += exitClick;
moel@1
   121
moel@202
   122
      int p = (int)Environment.OSVersion.Platform;
moel@202
   123
      if ((p == 4) || (p == 128)) { // Unix
moel@202
   124
        splitContainer.BorderStyle = BorderStyle.None;
moel@202
   125
        splitContainer.Border3DStyle = Border3DStyle.Adjust;
moel@202
   126
        splitContainer.SplitterWidth = 4;
moel@202
   127
        treeView.BorderStyle = BorderStyle.Fixed3D;
moel@202
   128
        plotPanel.BorderStyle = BorderStyle.Fixed3D;
moel@202
   129
        gadgetMenuItem.Visible = false;
moel@202
   130
        minCloseMenuItem.Visible = false;
moel@202
   131
      } else { // Windows
moel@202
   132
        gadget = new SensorGadget(computer, settings, unitManager);
moel@202
   133
      }          
moel@176
   134
moel@28
   135
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@28
   136
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
paulwerelds@223
   137
paulwerelds@223
   138
      if (settings.GetValue("enableWmiProvider", false))
paulwerelds@223
   139
        wmiProvider = new WmiProvider(computer);
paulwerelds@223
   140
moel@28
   141
      computer.Open();
moel@28
   142
moel@86
   143
      timer.Enabled = true;
moel@86
   144
moel@111
   145
      plotColorPalette = new Color[13];
moel@1
   146
      plotColorPalette[0] = Color.Blue;
moel@1
   147
      plotColorPalette[1] = Color.OrangeRed;
moel@1
   148
      plotColorPalette[2] = Color.Green;
moel@1
   149
      plotColorPalette[3] = Color.LightSeaGreen;
moel@1
   150
      plotColorPalette[4] = Color.Goldenrod;
moel@1
   151
      plotColorPalette[5] = Color.DarkViolet;
moel@1
   152
      plotColorPalette[6] = Color.YellowGreen;
moel@1
   153
      plotColorPalette[7] = Color.SaddleBrown;
moel@111
   154
      plotColorPalette[8] = Color.RoyalBlue;
moel@111
   155
      plotColorPalette[9] = Color.DeepPink;
moel@111
   156
      plotColorPalette[10] = Color.MediumSeaGreen;
moel@111
   157
      plotColorPalette[11] = Color.Olive;
moel@111
   158
      plotColorPalette[12] = Color.Firebrick;
moel@1
   159
paulwerelds@223
   160
      showHiddenSensors = new UserOption("hiddenMenuItem", false,
paulwerelds@223
   161
        hiddenMenuItem, settings);
moel@156
   162
      showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   163
        treeModel.ForceVisible = showHiddenSensors.Value;
moel@156
   164
      };
moel@111
   165
moel@165
   166
      showPlot = new UserOption("plotMenuItem", false, plotMenuItem, settings);
moel@156
   167
      showPlot.Changed += delegate(object sender, EventArgs e) {
moel@156
   168
        splitContainer.Panel2Collapsed = !showPlot.Value;
moel@156
   169
        treeView.Invalidate();
moel@156
   170
      };
moel@1
   171
paulwerelds@223
   172
      showValue = new UserOption("valueMenuItem", true, valueMenuItem,
paulwerelds@223
   173
        settings);
moel@156
   174
      showValue.Changed += delegate(object sender, EventArgs e) {
moel@156
   175
        treeView.Columns[1].IsVisible = showValue.Value;
moel@156
   176
      };
moel@122
   177
moel@165
   178
      showMin = new UserOption("minMenuItem", false, minMenuItem, settings);
moel@156
   179
      showMin.Changed += delegate(object sender, EventArgs e) {
moel@156
   180
        treeView.Columns[2].IsVisible = showMin.Value;
moel@156
   181
      };
moel@156
   182
moel@165
   183
      showMax = new UserOption("maxMenuItem", true, maxMenuItem, settings);
moel@156
   184
      showMax.Changed += delegate(object sender, EventArgs e) {
moel@156
   185
        treeView.Columns[3].IsVisible = showMax.Value;
moel@156
   186
      };
moel@156
   187
paulwerelds@223
   188
      startMinimized = new UserOption("startMinMenuItem", false,
paulwerelds@223
   189
        startMinMenuItem, settings);
moel@156
   190
paulwerelds@223
   191
      minimizeToTray = new UserOption("minTrayMenuItem", true,
paulwerelds@223
   192
        minTrayMenuItem, settings);
moel@156
   193
      minimizeToTray.Changed += delegate(object sender, EventArgs e) {
moel@156
   194
        systemTray.IsMainIconEnabled = minimizeToTray.Value;
moel@156
   195
      };
moel@156
   196
paulwerelds@223
   197
      minimizeOnClose = new UserOption("minCloseMenuItem", false,
paulwerelds@223
   198
        minCloseMenuItem, settings);
paulwerelds@198
   199
paulwerelds@223
   200
      autoStart = new UserOption(null, startupManager.Startup,
paulwerelds@223
   201
        startupMenuItem, settings);
moel@156
   202
      autoStart.Changed += delegate(object sender, EventArgs e) {
moel@185
   203
        try {
moel@185
   204
          startupManager.Startup = autoStart.Value;
moel@185
   205
        } catch (InvalidOperationException) {
moel@185
   206
          MessageBox.Show("Updating the auto-startup option failed.", "Error", 
moel@185
   207
            MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@185
   208
          autoStart.Value = startupManager.Startup;
moel@185
   209
        }
moel@156
   210
      };
moel@156
   211
paulwerelds@223
   212
      readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
paulwerelds@223
   213
        settings);
moel@156
   214
      readHddSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   215
        computer.HDDEnabled = readHddSensors.Value;
moel@156
   216
        UpdatePlotSelection(null, null);
moel@156
   217
      };
moel@156
   218
paulwerelds@223
   219
      showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
paulwerelds@223
   220
        settings);
moel@176
   221
      showGadget.Changed += delegate(object sender, EventArgs e) {
moel@202
   222
        if (gadget != null) 
moel@202
   223
          gadget.Visible = showGadget.Value;
moel@176
   224
      };
moel@176
   225
paulwerelds@223
   226
      enableWmiProvider = new UserOption("enableWmiProvider", false,
paulwerelds@223
   227
        wmiMenuItem, settings);
paulwerelds@223
   228
      enableWmiProvider.Changed += delegate {
paulwerelds@223
   229
        if (enableWmiProvider.Value && wmiProvider == null)
paulwerelds@223
   230
          wmiProvider = new WmiProvider(computer);
paulwerelds@223
   231
        else if (!enableWmiProvider.Value && wmiProvider != null) {
paulwerelds@223
   232
          wmiProvider.Dispose();
paulwerelds@223
   233
          wmiProvider = null;
paulwerelds@223
   234
        }
paulwerelds@223
   235
      };
paulwerelds@223
   236
moel@156
   237
      celciusMenuItem.Checked = 
moel@165
   238
        unitManager.TemperatureUnit == TemperatureUnit.Celcius;
moel@156
   239
      fahrenheitMenuItem.Checked = !celciusMenuItem.Checked;
moel@55
   240
moel@142
   241
      startupMenuItem.Visible = startupManager.IsAvailable;
moel@125
   242
      
moel@55
   243
      if (startMinMenuItem.Checked) {
moel@82
   244
        if (!minTrayMenuItem.Checked) {
moel@55
   245
          WindowState = FormWindowState.Minimized;
moel@55
   246
          Show();
moel@55
   247
        }
moel@55
   248
      } else {
moel@55
   249
        Show();
moel@55
   250
      }
moel@70
   251
moel@71
   252
      // Create a handle, otherwise calling Close() does not fire FormClosed     
moel@71
   253
      IntPtr handle = Handle;
moel@128
   254
moel@128
   255
      // Make sure the settings are saved when the user logs off
paulwerelds@223
   256
      Microsoft.Win32.SystemEvents.SessionEnded += delegate {
paulwerelds@223
   257
        SaveConfiguration();
paulwerelds@223
   258
      };  
moel@1
   259
    }
moel@128
   260
    
moel@64
   261
    private void SubHardwareAdded(IHardware hardware, Node node) {
moel@165
   262
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   263
      node.Nodes.Add(hardwareNode);
moel@64
   264
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   265
        SubHardwareAdded(subHardware, hardwareNode);  
moel@64
   266
    }
moel@64
   267
moel@28
   268
    private void HardwareAdded(IHardware hardware) {
moel@165
   269
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   270
      root.Nodes.Add(hardwareNode);
moel@64
   271
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   272
        SubHardwareAdded(subHardware, hardwareNode);     
moel@1
   273
    }
moel@1
   274
moel@28
   275
    private void HardwareRemoved(IHardware hardware) {      
moel@1
   276
      List<Node> nodesToRemove = new List<Node>();
moel@28
   277
      foreach (Node node in root.Nodes) {
moel@28
   278
        HardwareNode hardwareNode = node as HardwareNode;
moel@28
   279
        if (hardwareNode != null && hardwareNode.Hardware == hardware)
moel@28
   280
          nodesToRemove.Add(node);
moel@28
   281
      }
moel@1
   282
      foreach (Node node in nodesToRemove)
moel@1
   283
        root.Nodes.Remove(node);
moel@1
   284
    }
moel@1
   285
moel@111
   286
    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
moel@111
   287
      Node node = e.Node.Tag as Node;
moel@111
   288
      if (node != null) {
moel@1
   289
        Color color;
moel@111
   290
        if (node.IsVisible) {
moel@111
   291
          SensorNode sensorNode = node as SensorNode;
moel@111
   292
          if (plotMenuItem.Checked && sensorNode != null &&
moel@111
   293
            sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
moel@111
   294
            e.TextColor = color;
moel@111
   295
        } else {
moel@111
   296
          e.TextColor = Color.DarkGray;
moel@111
   297
        }
moel@1
   298
      }
moel@1
   299
    }
moel@1
   300
moel@1
   301
    private void UpdatePlotSelection(object sender, 
moel@1
   302
      TreePathEventArgs e) 
moel@1
   303
    {
moel@1
   304
      List<ISensor> selected = new List<ISensor>();
moel@1
   305
      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
moel@1
   306
      int colorIndex = 0;
moel@1
   307
      foreach (TreeNodeAdv node in treeView.AllNodes) {
moel@1
   308
        SensorNode sensorNode = node.Tag as SensorNode;
moel@1
   309
        if (sensorNode != null && 
moel@1
   310
          sensorNode.Sensor.SensorType == SensorType.Temperature) {
moel@1
   311
          if (sensorNode.Plot) {
moel@1
   312
            colors.Add(sensorNode.Sensor,
moel@1
   313
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   314
            selected.Add(sensorNode.Sensor);
moel@1
   315
          }
moel@1
   316
          colorIndex++;
moel@1
   317
        }
moel@1
   318
      }
moel@1
   319
      sensorPlotColors = colors;
moel@1
   320
      plotPanel.SetSensors(selected, colors);
moel@1
   321
    }
moel@1
   322
paulwerelds@223
   323
    private void nodeTextBoxText_EditorShowing(object sender,
paulwerelds@223
   324
      CancelEventArgs e) 
moel@141
   325
    {
moel@141
   326
      e.Cancel = !(treeView.CurrentNode != null &&
moel@141
   327
        treeView.CurrentNode.Tag is SensorNode);
moel@141
   328
    }
moel@141
   329
moel@1
   330
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   331
      NodeControlValueEventArgs e) {
moel@1
   332
      SensorNode node = e.Node.Tag as SensorNode;
moel@1
   333
      e.Value = (node != null) && 
moel@1
   334
        (node.Sensor.SensorType == SensorType.Temperature) && 
moel@1
   335
        plotMenuItem.Checked;
moel@1
   336
    }
moel@1
   337
moel@133
   338
    private void exitClick(object sender, EventArgs e) {
paulwerelds@198
   339
      Close();
moel@1
   340
    }
moel@1
   341
moel@86
   342
    private void timer_Tick(object sender, EventArgs e) {
moel@110
   343
      computer.Accept(updateVisitor);
moel@1
   344
      treeView.Invalidate();
moel@1
   345
      plotPanel.Invalidate();
paulwerelds@223
   346
      systemTray.Redraw();
moel@202
   347
      if (gadget != null)
moel@202
   348
        gadget.Redraw();
paulwerelds@223
   349
paulwerelds@223
   350
      if (wmiProvider != null)
paulwerelds@223
   351
        wmiProvider.Update();
moel@1
   352
    }
moel@1
   353
moel@128
   354
    private void SaveConfiguration() {
moel@14
   355
      if (WindowState != FormWindowState.Minimized) {
paulwerelds@214
   356
        settings.SetValue("mainForm.Location.X", Bounds.X);
paulwerelds@214
   357
        settings.SetValue("mainForm.Location.Y", Bounds.Y);
paulwerelds@214
   358
        settings.SetValue("mainForm.Width", Bounds.Width);
paulwerelds@214
   359
        settings.SetValue("mainForm.Height", Bounds.Height);
moel@14
   360
      }
moel@86
   361
moel@128
   362
      foreach (TreeColumn column in treeView.Columns)
moel@166
   363
        settings.SetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   364
          column.Width);
moel@113
   365
moel@212
   366
      string fileName = Path.ChangeExtension(
moel@212
   367
          System.Windows.Forms.Application.ExecutablePath, ".config");
moel@212
   368
      try {
moel@212
   369
        settings.Save(fileName);
moel@212
   370
      } catch (UnauthorizedAccessException) {
paulwerelds@214
   371
        MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
moel@212
   372
          "The current seetings could not be saved.", 
moel@212
   373
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@212
   374
      }
moel@128
   375
    }
moel@128
   376
paulwerelds@214
   377
    private void MainForm_Load(object sender, EventArgs e) {
paulwerelds@214
   378
      Rectangle newBounds = new Rectangle {
paulwerelds@214
   379
        X = settings.GetValue("mainForm.Location.X", Location.X),
paulwerelds@214
   380
        Y = settings.GetValue("mainForm.Location.Y", Location.Y),
paulwerelds@214
   381
        Width = settings.GetValue("mainForm.Width", 470),
paulwerelds@214
   382
        Height = settings.GetValue("mainForm.Height", 640)
paulwerelds@214
   383
      };
paulwerelds@214
   384
paulwerelds@223
   385
      Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
paulwerelds@214
   386
        int.MinValue, int.MinValue);
paulwerelds@214
   387
paulwerelds@214
   388
      foreach (Screen screen in Screen.AllScreens)
paulwerelds@223
   389
        fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds);
paulwerelds@214
   390
paulwerelds@223
   391
      Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds);
paulwerelds@214
   392
      if (intersection.Width < 20 || intersection.Height < 20 ||
paulwerelds@214
   393
        !settings.Contains("mainForm.Location.X")
paulwerelds@214
   394
      ) {
paulwerelds@214
   395
        newBounds.X = (Screen.PrimaryScreen.WorkingArea.Width / 2) -
paulwerelds@214
   396
                      (newBounds.Width/2);
paulwerelds@214
   397
paulwerelds@214
   398
        newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) -
paulwerelds@214
   399
                      (newBounds.Height / 2);
paulwerelds@214
   400
      }
paulwerelds@214
   401
paulwerelds@214
   402
      this.Bounds = newBounds;
paulwerelds@214
   403
    }
paulwerelds@214
   404
    
paulwerelds@214
   405
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@156
   406
      Visible = false;
moel@128
   407
      SaveConfiguration();
moel@128
   408
moel@86
   409
      timer.Enabled = false;
moel@133
   410
      systemTray.Dispose();      
moel@28
   411
      computer.Close();
moel@1
   412
    }
moel@1
   413
moel@156
   414
    private void aboutMenuItem_Click(object sender, EventArgs e) {
moel@1
   415
      new AboutBox().ShowDialog();
moel@1
   416
    }
moel@1
   417
moel@1
   418
    private void treeView_Click(object sender, EventArgs e) {
moel@1
   419
      
moel@1
   420
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   421
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   422
        return;
moel@1
   423
paulwerelds@223
   424
      NodeControlInfo info = treeView.GetNodeControlInfoAt(
paulwerelds@223
   425
        new Point(m.X, m.Y)
paulwerelds@223
   426
      );
moel@156
   427
      treeView.SelectedNode = info.Node;
moel@156
   428
      if (info.Node != null) {
moel@40
   429
        SensorNode node = info.Node.Tag as SensorNode;
moel@40
   430
        if (node != null && node.Sensor != null) {
moel@156
   431
          sensorContextMenu.MenuItems.Clear();
moel@63
   432
          if (node.Sensor.Parameters.Length > 0) {
moel@156
   433
            MenuItem item = new MenuItem("Parameters...");
moel@63
   434
            item.Click += delegate(object obj, EventArgs args) {
moel@63
   435
              ShowParameterForm(node.Sensor);
moel@63
   436
            };
moel@156
   437
            sensorContextMenu.MenuItems.Add(item);
moel@63
   438
          }
moel@156
   439
          if (nodeTextBoxText.EditEnabled) {
moel@156
   440
            MenuItem item = new MenuItem("Rename");
moel@141
   441
            item.Click += delegate(object obj, EventArgs args) {
moel@156
   442
              nodeTextBoxText.BeginEdit();
moel@141
   443
            };
moel@156
   444
            sensorContextMenu.MenuItems.Add(item);
moel@176
   445
          }
moel@111
   446
          if (node.IsVisible) {
moel@156
   447
            MenuItem item = new MenuItem("Hide");
moel@111
   448
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   449
              node.IsVisible = false;
moel@111
   450
            };
moel@156
   451
            sensorContextMenu.MenuItems.Add(item);
moel@111
   452
          } else {
moel@156
   453
            MenuItem item = new MenuItem("Unhide");
moel@111
   454
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   455
              node.IsVisible = true;
moel@111
   456
            };
moel@156
   457
            sensorContextMenu.MenuItems.Add(item);
moel@176
   458
          }
moel@176
   459
          sensorContextMenu.MenuItems.Add(new MenuItem("-"));
moel@178
   460
          {
moel@178
   461
            MenuItem item = new MenuItem("Show in Tray");
moel@178
   462
            item.Checked = systemTray.Contains(node.Sensor);
moel@178
   463
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   464
              if (item.Checked)
moel@178
   465
                systemTray.Remove(node.Sensor);
moel@178
   466
              else
moel@178
   467
                systemTray.Add(node.Sensor, true);
moel@178
   468
            };
moel@178
   469
            sensorContextMenu.MenuItems.Add(item);
moel@178
   470
          }
moel@211
   471
          if (gadget != null) {
moel@178
   472
            MenuItem item = new MenuItem("Show in Gadget");
moel@178
   473
            item.Checked = gadget.Contains(node.Sensor);
moel@178
   474
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   475
              if (item.Checked) {
moel@178
   476
                gadget.Remove(node.Sensor);
moel@178
   477
              } else {
moel@178
   478
                gadget.Add(node.Sensor);
moel@178
   479
              }
moel@178
   480
            };
moel@178
   481
            sensorContextMenu.MenuItems.Add(item);
moel@178
   482
          }
moel@176
   483
moel@156
   484
          sensorContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@40
   485
        }
moel@40
   486
      }
moel@1
   487
    }
moel@1
   488
moel@156
   489
    private void saveReportMenuItem_Click(object sender, EventArgs e) {
moel@83
   490
      string report = computer.GetReport();
moel@83
   491
      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
moel@83
   492
        using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
moel@83
   493
          w.Write(report);
moel@83
   494
        }
moel@83
   495
      }
moel@1
   496
    }
moel@1
   497
moel@82
   498
    private void SysTrayHideShow() {
moel@82
   499
      Visible = !Visible;
moel@82
   500
      if (Visible)
moel@82
   501
        Activate();    
moel@27
   502
    }
moel@27
   503
moel@27
   504
    protected override void WndProc(ref Message m) {
moel@27
   505
      const int WM_SYSCOMMAND = 0x112;
moel@27
   506
      const int SC_MINIMIZE = 0xF020;
paulwerelds@198
   507
      const int SC_CLOSE = 0xF060;
paulwerelds@198
   508
moel@156
   509
      if (minimizeToTray.Value && 
moel@28
   510
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
moel@82
   511
        SysTrayHideShow();
paulwerelds@198
   512
      } else if(minimizeOnClose.Value && 
paulwerelds@198
   513
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE) {
paulwerelds@198
   514
        /*
paulwerelds@198
   515
         * Apparently the user wants to minimize rather than close
paulwerelds@198
   516
         * Now we still need to check if we're going to the tray or not
paulwerelds@198
   517
         * 
paulwerelds@198
   518
         * Note: the correct way to do this would be to send out SC_MINIMIZE,
paulwerelds@198
   519
         * but since the code here is so simple,
paulwerelds@198
   520
         * that would just be a waste of time.
paulwerelds@198
   521
         */
paulwerelds@198
   522
        if (minimizeToTray.Value)
paulwerelds@198
   523
          SysTrayHideShow();
paulwerelds@198
   524
        else
paulwerelds@198
   525
          WindowState = FormWindowState.Minimized;
moel@27
   526
      } else {      
moel@27
   527
        base.WndProc(ref m);
moel@27
   528
      }
moel@27
   529
    }
moel@27
   530
moel@82
   531
    private void hideShowClick(object sender, EventArgs e) {
moel@82
   532
      SysTrayHideShow();
moel@27
   533
    }
moel@27
   534
moel@63
   535
    private void ShowParameterForm(ISensor sensor) {
moel@63
   536
      ParameterForm form = new ParameterForm();
moel@63
   537
      form.Parameters = sensor.Parameters;
moel@63
   538
      form.captionLabel.Text = sensor.Name;
moel@63
   539
      form.ShowDialog();
moel@63
   540
    }
moel@63
   541
moel@63
   542
    private void treeView_NodeMouseDoubleClick(object sender, 
moel@63
   543
      TreeNodeAdvMouseEventArgs e) {
moel@63
   544
      SensorNode node = e.Node.Tag as SensorNode;
moel@63
   545
      if (node != null && node.Sensor != null && 
moel@63
   546
        node.Sensor.Parameters.Length > 0) {
moel@63
   547
        ShowParameterForm(node.Sensor);
moel@63
   548
      }
moel@63
   549
    }
moel@82
   550
moel@156
   551
    private void celciusMenuItem_Click(object sender, EventArgs e) {
moel@156
   552
      celciusMenuItem.Checked = true;
moel@156
   553
      fahrenheitMenuItem.Checked = false;
moel@165
   554
      unitManager.TemperatureUnit = TemperatureUnit.Celcius;
moel@122
   555
    }
moel@122
   556
moel@156
   557
    private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
moel@156
   558
      celciusMenuItem.Checked = false;
moel@156
   559
      fahrenheitMenuItem.Checked = true;
moel@165
   560
      unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
moel@122
   561
    }
moel@150
   562
moel@156
   563
    private void sumbitReportMenuItem_Click(object sender, EventArgs e) 
moel@150
   564
    {
moel@150
   565
      ReportForm form = new ReportForm();
moel@150
   566
      form.Report = computer.GetReport();
moel@150
   567
      form.ShowDialog();      
moel@150
   568
    }
moel@151
   569
moel@151
   570
    private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
moel@159
   571
      computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
moel@159
   572
        sensor.ResetMin();
moel@159
   573
        sensor.ResetMax();
moel@159
   574
      }));
moel@151
   575
    }
moel@1
   576
  }
moel@1
   577
}