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