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