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