GUI/MainForm.cs
author moel.mich
Sat, 19 Mar 2011 16:13:49 +0000
changeset 264 718555482989
parent 253 0044b05a3094
child 265 961c07a3bd78
permissions -rw-r--r--
Fixed Issue 158.
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@227
    47
using OpenHardwareMonitor.WMI;
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
paulwerelds@223
    77
    private WmiProvider wmiProvider;
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@253
    94
      treeView.RowHeight = treeView.Font.Height + 1;      
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@244
   133
        gadget.HideShowCommand += hideShowClick;
moel@244
   134
moel@232
   135
        wmiProvider = new WmiProvider(computer);
moel@202
   136
      }          
moel@176
   137
moel@28
   138
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@232
   139
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);        
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
moel@156
   226
      celciusMenuItem.Checked = 
moel@165
   227
        unitManager.TemperatureUnit == TemperatureUnit.Celcius;
moel@156
   228
      fahrenheitMenuItem.Checked = !celciusMenuItem.Checked;
moel@55
   229
moel@142
   230
      startupMenuItem.Visible = startupManager.IsAvailable;
moel@125
   231
      
moel@55
   232
      if (startMinMenuItem.Checked) {
moel@82
   233
        if (!minTrayMenuItem.Checked) {
moel@55
   234
          WindowState = FormWindowState.Minimized;
moel@55
   235
          Show();
moel@55
   236
        }
moel@55
   237
      } else {
moel@55
   238
        Show();
moel@55
   239
      }
moel@70
   240
moel@71
   241
      // Create a handle, otherwise calling Close() does not fire FormClosed     
moel@71
   242
      IntPtr handle = Handle;
moel@128
   243
moel@128
   244
      // Make sure the settings are saved when the user logs off
paulwerelds@223
   245
      Microsoft.Win32.SystemEvents.SessionEnded += delegate {
paulwerelds@223
   246
        SaveConfiguration();
paulwerelds@223
   247
      };  
moel@1
   248
    }
moel@128
   249
    
moel@64
   250
    private void SubHardwareAdded(IHardware hardware, Node node) {
moel@165
   251
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   252
      node.Nodes.Add(hardwareNode);
moel@64
   253
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   254
        SubHardwareAdded(subHardware, hardwareNode);  
moel@64
   255
    }
moel@64
   256
moel@28
   257
    private void HardwareAdded(IHardware hardware) {
moel@165
   258
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   259
      root.Nodes.Add(hardwareNode);
moel@64
   260
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   261
        SubHardwareAdded(subHardware, hardwareNode);     
moel@1
   262
    }
moel@1
   263
moel@28
   264
    private void HardwareRemoved(IHardware hardware) {      
moel@1
   265
      List<Node> nodesToRemove = new List<Node>();
moel@28
   266
      foreach (Node node in root.Nodes) {
moel@28
   267
        HardwareNode hardwareNode = node as HardwareNode;
moel@28
   268
        if (hardwareNode != null && hardwareNode.Hardware == hardware)
moel@28
   269
          nodesToRemove.Add(node);
moel@28
   270
      }
moel@1
   271
      foreach (Node node in nodesToRemove)
moel@1
   272
        root.Nodes.Remove(node);
moel@1
   273
    }
moel@1
   274
moel@111
   275
    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
moel@111
   276
      Node node = e.Node.Tag as Node;
moel@111
   277
      if (node != null) {
moel@1
   278
        Color color;
moel@111
   279
        if (node.IsVisible) {
moel@111
   280
          SensorNode sensorNode = node as SensorNode;
moel@111
   281
          if (plotMenuItem.Checked && sensorNode != null &&
moel@111
   282
            sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
moel@111
   283
            e.TextColor = color;
moel@111
   284
        } else {
moel@111
   285
          e.TextColor = Color.DarkGray;
moel@111
   286
        }
moel@1
   287
      }
moel@1
   288
    }
moel@1
   289
moel@1
   290
    private void UpdatePlotSelection(object sender, 
moel@1
   291
      TreePathEventArgs e) 
moel@1
   292
    {
moel@1
   293
      List<ISensor> selected = new List<ISensor>();
moel@1
   294
      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
moel@1
   295
      int colorIndex = 0;
moel@1
   296
      foreach (TreeNodeAdv node in treeView.AllNodes) {
moel@1
   297
        SensorNode sensorNode = node.Tag as SensorNode;
moel@1
   298
        if (sensorNode != null && 
moel@1
   299
          sensorNode.Sensor.SensorType == SensorType.Temperature) {
moel@1
   300
          if (sensorNode.Plot) {
moel@1
   301
            colors.Add(sensorNode.Sensor,
moel@1
   302
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   303
            selected.Add(sensorNode.Sensor);
moel@1
   304
          }
moel@1
   305
          colorIndex++;
moel@1
   306
        }
moel@1
   307
      }
moel@1
   308
      sensorPlotColors = colors;
moel@1
   309
      plotPanel.SetSensors(selected, colors);
moel@1
   310
    }
moel@1
   311
paulwerelds@223
   312
    private void nodeTextBoxText_EditorShowing(object sender,
paulwerelds@223
   313
      CancelEventArgs e) 
moel@141
   314
    {
moel@141
   315
      e.Cancel = !(treeView.CurrentNode != null &&
moel@141
   316
        treeView.CurrentNode.Tag is SensorNode);
moel@141
   317
    }
moel@141
   318
moel@1
   319
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   320
      NodeControlValueEventArgs e) {
moel@1
   321
      SensorNode node = e.Node.Tag as SensorNode;
moel@1
   322
      e.Value = (node != null) && 
moel@1
   323
        (node.Sensor.SensorType == SensorType.Temperature) && 
moel@1
   324
        plotMenuItem.Checked;
moel@1
   325
    }
moel@1
   326
moel@133
   327
    private void exitClick(object sender, EventArgs e) {
paulwerelds@198
   328
      Close();
moel@1
   329
    }
moel@1
   330
moel@86
   331
    private void timer_Tick(object sender, EventArgs e) {
moel@110
   332
      computer.Accept(updateVisitor);
moel@1
   333
      treeView.Invalidate();
moel@1
   334
      plotPanel.Invalidate();
paulwerelds@223
   335
      systemTray.Redraw();
moel@202
   336
      if (gadget != null)
moel@202
   337
        gadget.Redraw();
paulwerelds@223
   338
paulwerelds@223
   339
      if (wmiProvider != null)
paulwerelds@223
   340
        wmiProvider.Update();
moel@1
   341
    }
moel@1
   342
moel@128
   343
    private void SaveConfiguration() {
moel@128
   344
      foreach (TreeColumn column in treeView.Columns)
moel@166
   345
        settings.SetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   346
          column.Width);
moel@113
   347
moel@212
   348
      string fileName = Path.ChangeExtension(
moel@212
   349
          System.Windows.Forms.Application.ExecutablePath, ".config");
moel@212
   350
      try {
moel@212
   351
        settings.Save(fileName);
moel@212
   352
      } catch (UnauthorizedAccessException) {
paulwerelds@214
   353
        MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
moel@212
   354
          "The current seetings could not be saved.", 
moel@212
   355
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@212
   356
      }
moel@128
   357
    }
moel@128
   358
paulwerelds@214
   359
    private void MainForm_Load(object sender, EventArgs e) {
paulwerelds@214
   360
      Rectangle newBounds = new Rectangle {
paulwerelds@214
   361
        X = settings.GetValue("mainForm.Location.X", Location.X),
paulwerelds@214
   362
        Y = settings.GetValue("mainForm.Location.Y", Location.Y),
paulwerelds@214
   363
        Width = settings.GetValue("mainForm.Width", 470),
paulwerelds@214
   364
        Height = settings.GetValue("mainForm.Height", 640)
paulwerelds@214
   365
      };
paulwerelds@214
   366
paulwerelds@223
   367
      Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
paulwerelds@214
   368
        int.MinValue, int.MinValue);
paulwerelds@214
   369
paulwerelds@214
   370
      foreach (Screen screen in Screen.AllScreens)
paulwerelds@223
   371
        fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds);
paulwerelds@214
   372
paulwerelds@223
   373
      Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds);
paulwerelds@214
   374
      if (intersection.Width < 20 || intersection.Height < 20 ||
paulwerelds@214
   375
        !settings.Contains("mainForm.Location.X")
paulwerelds@214
   376
      ) {
paulwerelds@214
   377
        newBounds.X = (Screen.PrimaryScreen.WorkingArea.Width / 2) -
paulwerelds@214
   378
                      (newBounds.Width/2);
paulwerelds@214
   379
paulwerelds@214
   380
        newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) -
paulwerelds@214
   381
                      (newBounds.Height / 2);
paulwerelds@214
   382
      }
paulwerelds@214
   383
paulwerelds@214
   384
      this.Bounds = newBounds;
paulwerelds@214
   385
    }
paulwerelds@214
   386
    
paulwerelds@214
   387
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@156
   388
      Visible = false;
moel@128
   389
      SaveConfiguration();
moel@128
   390
moel@262
   391
      systemTray.IsMainIconEnabled = false;
moel@262
   392
      timer.Enabled = false;            
moel@28
   393
      computer.Close();
moel@262
   394
      systemTray.Dispose();
moel@1
   395
    }
moel@1
   396
moel@156
   397
    private void aboutMenuItem_Click(object sender, EventArgs e) {
moel@1
   398
      new AboutBox().ShowDialog();
moel@1
   399
    }
moel@1
   400
moel@1
   401
    private void treeView_Click(object sender, EventArgs e) {
moel@1
   402
      
moel@1
   403
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   404
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   405
        return;
moel@1
   406
paulwerelds@223
   407
      NodeControlInfo info = treeView.GetNodeControlInfoAt(
paulwerelds@223
   408
        new Point(m.X, m.Y)
paulwerelds@223
   409
      );
moel@156
   410
      treeView.SelectedNode = info.Node;
moel@156
   411
      if (info.Node != null) {
moel@40
   412
        SensorNode node = info.Node.Tag as SensorNode;
moel@40
   413
        if (node != null && node.Sensor != null) {
moel@156
   414
          sensorContextMenu.MenuItems.Clear();
moel@63
   415
          if (node.Sensor.Parameters.Length > 0) {
moel@156
   416
            MenuItem item = new MenuItem("Parameters...");
moel@63
   417
            item.Click += delegate(object obj, EventArgs args) {
moel@63
   418
              ShowParameterForm(node.Sensor);
moel@63
   419
            };
moel@156
   420
            sensorContextMenu.MenuItems.Add(item);
moel@63
   421
          }
moel@156
   422
          if (nodeTextBoxText.EditEnabled) {
moel@156
   423
            MenuItem item = new MenuItem("Rename");
moel@141
   424
            item.Click += delegate(object obj, EventArgs args) {
moel@156
   425
              nodeTextBoxText.BeginEdit();
moel@141
   426
            };
moel@156
   427
            sensorContextMenu.MenuItems.Add(item);
moel@176
   428
          }
moel@111
   429
          if (node.IsVisible) {
moel@156
   430
            MenuItem item = new MenuItem("Hide");
moel@111
   431
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   432
              node.IsVisible = false;
moel@111
   433
            };
moel@156
   434
            sensorContextMenu.MenuItems.Add(item);
moel@111
   435
          } else {
moel@156
   436
            MenuItem item = new MenuItem("Unhide");
moel@111
   437
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   438
              node.IsVisible = true;
moel@111
   439
            };
moel@156
   440
            sensorContextMenu.MenuItems.Add(item);
moel@176
   441
          }
moel@176
   442
          sensorContextMenu.MenuItems.Add(new MenuItem("-"));
moel@178
   443
          {
moel@178
   444
            MenuItem item = new MenuItem("Show in Tray");
moel@178
   445
            item.Checked = systemTray.Contains(node.Sensor);
moel@178
   446
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   447
              if (item.Checked)
moel@178
   448
                systemTray.Remove(node.Sensor);
moel@178
   449
              else
moel@178
   450
                systemTray.Add(node.Sensor, true);
moel@178
   451
            };
moel@178
   452
            sensorContextMenu.MenuItems.Add(item);
moel@178
   453
          }
moel@211
   454
          if (gadget != null) {
moel@178
   455
            MenuItem item = new MenuItem("Show in Gadget");
moel@178
   456
            item.Checked = gadget.Contains(node.Sensor);
moel@178
   457
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   458
              if (item.Checked) {
moel@178
   459
                gadget.Remove(node.Sensor);
moel@178
   460
              } else {
moel@178
   461
                gadget.Add(node.Sensor);
moel@178
   462
              }
moel@178
   463
            };
moel@178
   464
            sensorContextMenu.MenuItems.Add(item);
moel@178
   465
          }
moel@247
   466
          if (node.Sensor.Control != null) {
moel@248
   467
            sensorContextMenu.MenuItems.Add(new MenuItem("-"));
moel@247
   468
            IControl control = node.Sensor.Control;
moel@247
   469
            MenuItem controlItem = new MenuItem("Control");
moel@247
   470
            MenuItem defaultItem = new MenuItem("Default");
moel@247
   471
            defaultItem.Checked = control.ControlMode == ControlMode.Default;
moel@247
   472
            controlItem.MenuItems.Add(defaultItem);
moel@247
   473
            defaultItem.Click += delegate(object obj, EventArgs args) {
moel@247
   474
              control.SetDefault();
moel@247
   475
            };
moel@247
   476
            MenuItem manualItem = new MenuItem("Manual");            
moel@247
   477
            controlItem.MenuItems.Add(manualItem);
moel@247
   478
            manualItem.Checked = control.ControlMode == ControlMode.Software;
moel@247
   479
            for (int i = 0; i <= 100; i += 5) {
moel@247
   480
              if (i <= control.MaxSoftwareValue &&
moel@247
   481
                  i >= control.MinSoftwareValue) 
moel@247
   482
              {
moel@247
   483
                MenuItem item = new MenuItem(i + " %");
moel@247
   484
                manualItem.MenuItems.Add(item);
moel@247
   485
                item.Checked = control.ControlMode == ControlMode.Software &&
moel@247
   486
                  Math.Round(control.SoftwareValue) == i;
moel@247
   487
                int softwareValue = i;
moel@247
   488
                item.Click += delegate(object obj, EventArgs args) {
moel@247
   489
                  control.SetSoftware(softwareValue);
moel@247
   490
                };
moel@247
   491
              }
moel@247
   492
            }
moel@247
   493
            sensorContextMenu.MenuItems.Add(controlItem);
moel@247
   494
          }
moel@176
   495
moel@156
   496
          sensorContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@40
   497
        }
moel@40
   498
      }
moel@1
   499
    }
moel@1
   500
moel@156
   501
    private void saveReportMenuItem_Click(object sender, EventArgs e) {
moel@83
   502
      string report = computer.GetReport();
moel@83
   503
      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
moel@83
   504
        using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
moel@83
   505
          w.Write(report);
moel@83
   506
        }
moel@83
   507
      }
moel@1
   508
    }
moel@1
   509
moel@82
   510
    private void SysTrayHideShow() {
moel@82
   511
      Visible = !Visible;
moel@82
   512
      if (Visible)
moel@82
   513
        Activate();    
moel@27
   514
    }
moel@27
   515
moel@27
   516
    protected override void WndProc(ref Message m) {
moel@27
   517
      const int WM_SYSCOMMAND = 0x112;
moel@27
   518
      const int SC_MINIMIZE = 0xF020;
paulwerelds@198
   519
      const int SC_CLOSE = 0xF060;
paulwerelds@198
   520
moel@156
   521
      if (minimizeToTray.Value && 
moel@28
   522
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
moel@82
   523
        SysTrayHideShow();
paulwerelds@198
   524
      } else if(minimizeOnClose.Value && 
paulwerelds@198
   525
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE) {
paulwerelds@198
   526
        /*
paulwerelds@198
   527
         * Apparently the user wants to minimize rather than close
paulwerelds@198
   528
         * Now we still need to check if we're going to the tray or not
paulwerelds@198
   529
         * 
paulwerelds@198
   530
         * Note: the correct way to do this would be to send out SC_MINIMIZE,
paulwerelds@198
   531
         * but since the code here is so simple,
paulwerelds@198
   532
         * that would just be a waste of time.
paulwerelds@198
   533
         */
paulwerelds@198
   534
        if (minimizeToTray.Value)
paulwerelds@198
   535
          SysTrayHideShow();
paulwerelds@198
   536
        else
paulwerelds@198
   537
          WindowState = FormWindowState.Minimized;
moel@27
   538
      } else {      
moel@27
   539
        base.WndProc(ref m);
moel@27
   540
      }
moel@27
   541
    }
moel@27
   542
moel@82
   543
    private void hideShowClick(object sender, EventArgs e) {
moel@82
   544
      SysTrayHideShow();
moel@27
   545
    }
moel@27
   546
moel@63
   547
    private void ShowParameterForm(ISensor sensor) {
moel@63
   548
      ParameterForm form = new ParameterForm();
moel@63
   549
      form.Parameters = sensor.Parameters;
moel@63
   550
      form.captionLabel.Text = sensor.Name;
moel@63
   551
      form.ShowDialog();
moel@63
   552
    }
moel@63
   553
moel@63
   554
    private void treeView_NodeMouseDoubleClick(object sender, 
moel@63
   555
      TreeNodeAdvMouseEventArgs e) {
moel@63
   556
      SensorNode node = e.Node.Tag as SensorNode;
moel@63
   557
      if (node != null && node.Sensor != null && 
moel@63
   558
        node.Sensor.Parameters.Length > 0) {
moel@63
   559
        ShowParameterForm(node.Sensor);
moel@63
   560
      }
moel@63
   561
    }
moel@82
   562
moel@156
   563
    private void celciusMenuItem_Click(object sender, EventArgs e) {
moel@156
   564
      celciusMenuItem.Checked = true;
moel@156
   565
      fahrenheitMenuItem.Checked = false;
moel@165
   566
      unitManager.TemperatureUnit = TemperatureUnit.Celcius;
moel@122
   567
    }
moel@122
   568
moel@156
   569
    private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
moel@156
   570
      celciusMenuItem.Checked = false;
moel@156
   571
      fahrenheitMenuItem.Checked = true;
moel@165
   572
      unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
moel@122
   573
    }
moel@150
   574
moel@156
   575
    private void sumbitReportMenuItem_Click(object sender, EventArgs e) 
moel@150
   576
    {
moel@150
   577
      ReportForm form = new ReportForm();
moel@150
   578
      form.Report = computer.GetReport();
moel@150
   579
      form.ShowDialog();      
moel@150
   580
    }
moel@151
   581
moel@151
   582
    private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
moel@159
   583
      computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
moel@159
   584
        sensor.ResetMin();
moel@159
   585
        sensor.ResetMax();
moel@159
   586
      }));
moel@151
   587
    }
moel@241
   588
moel@241
   589
    private void MainForm_MoveOrResize(object sender, EventArgs e) {
moel@241
   590
      if (WindowState != FormWindowState.Minimized) {
moel@241
   591
        settings.SetValue("mainForm.Location.X", Bounds.X);
moel@241
   592
        settings.SetValue("mainForm.Location.Y", Bounds.Y);
moel@241
   593
        settings.SetValue("mainForm.Width", Bounds.Width);
moel@241
   594
        settings.SetValue("mainForm.Height", Bounds.Height);
moel@241
   595
      }
moel@241
   596
    }
moel@262
   597
moel@262
   598
    private void resetClick(object sender, EventArgs e) {
moel@262
   599
      // disable the fallback MainIcon during reset, otherwise icon visibility
moel@262
   600
      // might be lost 
moel@262
   601
      systemTray.IsMainIconEnabled = false;
moel@262
   602
      computer.Close();
moel@262
   603
      computer.Open();
moel@262
   604
      // restore the MainIcon setting
moel@262
   605
      systemTray.IsMainIconEnabled = minimizeToTray.Value;
moel@262
   606
    }
moel@1
   607
  }
moel@1
   608
}