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