GUI/MainForm.cs
author moel.mich
Tue, 21 Sep 2010 20:32:36 +0000
changeset 195 0ee888c485d5
parent 178 67b9b4d8c5fc
child 198 d5de6fa31bc6
permissions -rw-r--r--
Refactored some of the hardware monitoring code and fixed a few code inspection warnings.
moel@1
     1
/*
moel@1
     2
  
moel@1
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@1
     4
moel@1
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@1
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@1
     7
  the License. You may obtain a copy of the License at
moel@1
     8
 
moel@1
     9
  http://www.mozilla.org/MPL/
moel@1
    10
moel@1
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@1
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@1
    13
  for the specific language governing rights and limitations under the License.
moel@1
    14
moel@1
    15
  The Original Code is the Open Hardware Monitor code.
moel@1
    16
moel@1
    17
  The Initial Developer of the Original Code is 
moel@1
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@1
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
moel@1
    22
  Contributor(s):
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.ComponentModel;
moel@1
    41
using System.Configuration;
moel@1
    42
using System.Drawing;
moel@83
    43
using System.IO;
moel@1
    44
using System.Text;
moel@1
    45
using System.Windows.Forms;
moel@1
    46
using Aga.Controls.Tree;
moel@1
    47
using Aga.Controls.Tree.NodeControls;
moel@1
    48
using OpenHardwareMonitor.Hardware;
moel@28
    49
using OpenHardwareMonitor.Utilities;
moel@1
    50
moel@1
    51
namespace OpenHardwareMonitor.GUI {
moel@1
    52
  public partial class MainForm : Form {
moel@1
    53
moel@165
    54
    private PersistentSettings settings;
moel@165
    55
    private UnitManager unitManager;
moel@165
    56
    private Computer computer;
moel@1
    57
    private Node root;
moel@1
    58
    private TreeModel treeModel;
moel@1
    59
    private IDictionary<ISensor, Color> sensorPlotColors = 
moel@1
    60
      new Dictionary<ISensor, Color>();
moel@1
    61
    private Color[] plotColorPalette;
moel@133
    62
    private SystemTray systemTray;    
moel@82
    63
    private StartupManager startupManager = new StartupManager();
moel@110
    64
    private UpdateVisitor updateVisitor = new UpdateVisitor();
moel@176
    65
    private SensorGadget gadget;
moel@1
    66
moel@156
    67
    private UserOption showHiddenSensors;
moel@156
    68
    private UserOption showPlot;
moel@156
    69
    private UserOption showValue;
moel@156
    70
    private UserOption showMin;
moel@156
    71
    private UserOption showMax;
moel@156
    72
    private UserOption startMinimized;
moel@156
    73
    private UserOption minimizeToTray;
moel@156
    74
    private UserOption autoStart;
moel@156
    75
    private UserOption readHddSensors;
moel@176
    76
    private UserOption showGadget;
moel@156
    77
moel@28
    78
    public MainForm() {      
moel@1
    79
      InitializeComponent();
moel@156
    80
moel@165
    81
      this.settings = new PersistentSettings();      
moel@165
    82
      this.settings.Load(Path.ChangeExtension(
moel@165
    83
        System.Windows.Forms.Application.ExecutablePath, ".config"));
moel@165
    84
moel@165
    85
      this.unitManager = new UnitManager(settings);
moel@165
    86
moel@156
    87
      // set the DockStyle here, to avoid conflicts with the MainMenu
moel@156
    88
      this.splitContainer.Dock = DockStyle.Fill;
moel@156
    89
      
moel@159
    90
      int p = (int)System.Environment.OSVersion.Platform;
moel@159
    91
      if ((p == 4) || (p == 128)) {
moel@159
    92
        splitContainer.BorderStyle = BorderStyle.None;
moel@159
    93
        splitContainer.Border3DStyle = Border3DStyle.Adjust;
moel@159
    94
        splitContainer.SplitterWidth = 4;
moel@159
    95
        treeView.BorderStyle = BorderStyle.Fixed3D;
moel@159
    96
        plotPanel.BorderStyle = BorderStyle.Fixed3D;
moel@159
    97
      }
moel@159
    98
      
moel@1
    99
      this.Font = SystemFonts.MessageBoxFont;
moel@1
   100
      treeView.Font = SystemFonts.MessageBoxFont;
moel@63
   101
      plotPanel.Font = SystemFonts.MessageBoxFont;
moel@1
   102
      
moel@133
   103
      nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
moel@133
   104
      nodeCheckBox.CheckStateChanged += UpdatePlotSelection;
moel@133
   105
      nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
moel@133
   106
      nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
moel@133
   107
      nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
moel@133
   108
      nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
moel@141
   109
      nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
moel@1
   110
moel@165
   111
      if (settings.Contains("mainForm.Location.X")) {
moel@166
   112
        int x = settings.GetValue("mainForm.Location.X", Location.X);
moel@1
   113
        x = x < 0 ? 0 : x;
moel@166
   114
        int y = settings.GetValue("mainForm.Location.Y", Location.Y);
moel@1
   115
        y = y < 0 ? 0 : y;
moel@1
   116
        this.Location = new Point(x, y);
moel@1
   117
      } else {
moel@1
   118
        StartPosition = FormStartPosition.CenterScreen;
moel@1
   119
      }
moel@1
   120
moel@156
   121
      ClientSize = new Size(
moel@166
   122
        settings.GetValue("mainForm.Width", 470),
moel@166
   123
        settings.GetValue("mainForm.Height", 640));
moel@113
   124
moel@113
   125
      foreach (TreeColumn column in treeView.Columns) 
moel@165
   126
        column.Width = Math.Max(20, Math.Min(400,
moel@166
   127
          settings.GetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   128
          column.Width)));
moel@113
   129
moel@1
   130
      treeModel = new TreeModel();
moel@1
   131
      root = new Node(System.Environment.MachineName);
moel@1
   132
      root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
moel@1
   133
      
moel@1
   134
      treeModel.Nodes.Add(root);
moel@165
   135
      treeView.Model = treeModel;
moel@40
   136
moel@165
   137
      this.computer = new Computer(settings);
moel@165
   138
moel@165
   139
      systemTray = new SystemTray(computer, settings);
moel@133
   140
      systemTray.HideShowCommand += hideShowClick;
moel@133
   141
      systemTray.ExitCommand += exitClick;
moel@1
   142
moel@176
   143
      gadget = new SensorGadget(computer, settings, unitManager);
moel@176
   144
moel@28
   145
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@28
   146
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
moel@28
   147
      computer.Open();
moel@28
   148
moel@86
   149
      timer.Enabled = true;
moel@86
   150
moel@111
   151
      plotColorPalette = new Color[13];
moel@1
   152
      plotColorPalette[0] = Color.Blue;
moel@1
   153
      plotColorPalette[1] = Color.OrangeRed;
moel@1
   154
      plotColorPalette[2] = Color.Green;
moel@1
   155
      plotColorPalette[3] = Color.LightSeaGreen;
moel@1
   156
      plotColorPalette[4] = Color.Goldenrod;
moel@1
   157
      plotColorPalette[5] = Color.DarkViolet;
moel@1
   158
      plotColorPalette[6] = Color.YellowGreen;
moel@1
   159
      plotColorPalette[7] = Color.SaddleBrown;
moel@111
   160
      plotColorPalette[8] = Color.RoyalBlue;
moel@111
   161
      plotColorPalette[9] = Color.DeepPink;
moel@111
   162
      plotColorPalette[10] = Color.MediumSeaGreen;
moel@111
   163
      plotColorPalette[11] = Color.Olive;
moel@111
   164
      plotColorPalette[12] = Color.Firebrick;
moel@1
   165
moel@165
   166
      showHiddenSensors = new UserOption("hiddenMenuItem", false, hiddenMenuItem, settings);
moel@156
   167
      showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   168
        treeModel.ForceVisible = showHiddenSensors.Value;
moel@156
   169
      };
moel@111
   170
moel@165
   171
      showPlot = new UserOption("plotMenuItem", false, plotMenuItem, settings);
moel@156
   172
      showPlot.Changed += delegate(object sender, EventArgs e) {
moel@156
   173
        splitContainer.Panel2Collapsed = !showPlot.Value;
moel@156
   174
        treeView.Invalidate();
moel@156
   175
      };
moel@1
   176
moel@165
   177
      showValue = new UserOption("valueMenuItem", true, valueMenuItem, 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
moel@165
   192
      startMinimized = new UserOption("startMinMenuItem", false, startMinMenuItem, settings);
moel@156
   193
moel@165
   194
      minimizeToTray = new UserOption("minTrayMenuItem", true, 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
moel@165
   199
      autoStart = new UserOption(null, startupManager.Startup, startupMenuItem, settings);
moel@156
   200
      autoStart.Changed += delegate(object sender, EventArgs e) {
moel@185
   201
        try {
moel@185
   202
          startupManager.Startup = autoStart.Value;
moel@185
   203
        } catch (InvalidOperationException) {
moel@185
   204
          MessageBox.Show("Updating the auto-startup option failed.", "Error", 
moel@185
   205
            MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@185
   206
          autoStart.Value = startupManager.Startup;
moel@185
   207
        }
moel@156
   208
      };
moel@156
   209
moel@165
   210
      readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem, settings);
moel@156
   211
      readHddSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   212
        computer.HDDEnabled = readHddSensors.Value;
moel@156
   213
        UpdatePlotSelection(null, null);
moel@156
   214
      };
moel@156
   215
moel@176
   216
      showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem, settings);
moel@176
   217
      showGadget.Changed += delegate(object sender, EventArgs e) {
moel@176
   218
        gadget.Visible = showGadget.Value;
moel@176
   219
      };
moel@176
   220
moel@156
   221
      celciusMenuItem.Checked = 
moel@165
   222
        unitManager.TemperatureUnit == TemperatureUnit.Celcius;
moel@156
   223
      fahrenheitMenuItem.Checked = !celciusMenuItem.Checked;
moel@55
   224
moel@142
   225
      startupMenuItem.Visible = startupManager.IsAvailable;
moel@125
   226
      
moel@55
   227
      if (startMinMenuItem.Checked) {
moel@82
   228
        if (!minTrayMenuItem.Checked) {
moel@55
   229
          WindowState = FormWindowState.Minimized;
moel@55
   230
          Show();
moel@55
   231
        }
moel@55
   232
      } else {
moel@55
   233
        Show();
moel@55
   234
      }
moel@70
   235
moel@71
   236
      // Create a handle, otherwise calling Close() does not fire FormClosed     
moel@71
   237
      IntPtr handle = Handle;
moel@128
   238
moel@128
   239
      // Make sure the settings are saved when the user logs off
moel@128
   240
      Microsoft.Win32.SystemEvents.SessionEnded +=
moel@128
   241
        delegate(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
moel@128
   242
          SaveConfiguration();
moel@176
   243
        };  
moel@1
   244
    }
moel@128
   245
    
moel@64
   246
    private void SubHardwareAdded(IHardware hardware, Node node) {
moel@165
   247
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   248
      node.Nodes.Add(hardwareNode);
moel@64
   249
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   250
        SubHardwareAdded(subHardware, hardwareNode);  
moel@64
   251
    }
moel@64
   252
moel@28
   253
    private void HardwareAdded(IHardware hardware) {
moel@165
   254
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   255
      root.Nodes.Add(hardwareNode);
moel@64
   256
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   257
        SubHardwareAdded(subHardware, hardwareNode);     
moel@1
   258
    }
moel@1
   259
moel@28
   260
    private void HardwareRemoved(IHardware hardware) {      
moel@1
   261
      List<Node> nodesToRemove = new List<Node>();
moel@28
   262
      foreach (Node node in root.Nodes) {
moel@28
   263
        HardwareNode hardwareNode = node as HardwareNode;
moel@28
   264
        if (hardwareNode != null && hardwareNode.Hardware == hardware)
moel@28
   265
          nodesToRemove.Add(node);
moel@28
   266
      }
moel@1
   267
      foreach (Node node in nodesToRemove)
moel@1
   268
        root.Nodes.Remove(node);
moel@1
   269
    }
moel@1
   270
moel@111
   271
    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
moel@111
   272
      Node node = e.Node.Tag as Node;
moel@111
   273
      if (node != null) {
moel@1
   274
        Color color;
moel@111
   275
        if (node.IsVisible) {
moel@111
   276
          SensorNode sensorNode = node as SensorNode;
moel@111
   277
          if (plotMenuItem.Checked && sensorNode != null &&
moel@111
   278
            sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
moel@111
   279
            e.TextColor = color;
moel@111
   280
        } else {
moel@111
   281
          e.TextColor = Color.DarkGray;
moel@111
   282
        }
moel@1
   283
      }
moel@1
   284
    }
moel@1
   285
moel@1
   286
    private void UpdatePlotSelection(object sender, 
moel@1
   287
      TreePathEventArgs e) 
moel@1
   288
    {
moel@1
   289
      List<ISensor> selected = new List<ISensor>();
moel@1
   290
      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
moel@1
   291
      int colorIndex = 0;
moel@1
   292
      foreach (TreeNodeAdv node in treeView.AllNodes) {
moel@1
   293
        SensorNode sensorNode = node.Tag as SensorNode;
moel@1
   294
        if (sensorNode != null && 
moel@1
   295
          sensorNode.Sensor.SensorType == SensorType.Temperature) {
moel@1
   296
          if (sensorNode.Plot) {
moel@1
   297
            colors.Add(sensorNode.Sensor,
moel@1
   298
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   299
            selected.Add(sensorNode.Sensor);
moel@1
   300
          }
moel@1
   301
          colorIndex++;
moel@1
   302
        }
moel@1
   303
      }
moel@1
   304
      sensorPlotColors = colors;
moel@1
   305
      plotPanel.SetSensors(selected, colors);
moel@1
   306
    }
moel@1
   307
moel@141
   308
    private void nodeTextBoxText_EditorShowing(object sender, CancelEventArgs e) 
moel@141
   309
    {
moel@141
   310
      e.Cancel = !(treeView.CurrentNode != null &&
moel@141
   311
        treeView.CurrentNode.Tag is SensorNode);
moel@141
   312
    }
moel@141
   313
moel@1
   314
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   315
      NodeControlValueEventArgs e) {
moel@1
   316
      SensorNode node = e.Node.Tag as SensorNode;
moel@1
   317
      e.Value = (node != null) && 
moel@1
   318
        (node.Sensor.SensorType == SensorType.Temperature) && 
moel@1
   319
        plotMenuItem.Checked;
moel@1
   320
    }
moel@1
   321
moel@133
   322
    private void exitClick(object sender, EventArgs e) {
moel@70
   323
      Close();      
moel@1
   324
    }
moel@1
   325
moel@86
   326
    private void timer_Tick(object sender, EventArgs e) {
moel@110
   327
      computer.Accept(updateVisitor);
moel@1
   328
      treeView.Invalidate();
moel@1
   329
      plotPanel.Invalidate();
moel@133
   330
      systemTray.Redraw();
moel@176
   331
      gadget.Redraw();
moel@1
   332
    }
moel@1
   333
moel@128
   334
    private void SaveConfiguration() {
moel@14
   335
      if (WindowState != FormWindowState.Minimized) {
moel@166
   336
        settings.SetValue("mainForm.Location.X", Location.X);
moel@166
   337
        settings.SetValue("mainForm.Location.Y", Location.Y);
moel@166
   338
        settings.SetValue("mainForm.Width", ClientSize.Width);
moel@166
   339
        settings.SetValue("mainForm.Height", ClientSize.Height);
moel@14
   340
      }
moel@86
   341
moel@128
   342
      foreach (TreeColumn column in treeView.Columns)
moel@166
   343
        settings.SetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   344
          column.Width);
moel@113
   345
moel@165
   346
      settings.Save(Path.ChangeExtension(
moel@165
   347
        System.Windows.Forms.Application.ExecutablePath, ".config"));
moel@128
   348
    }
moel@128
   349
moel@128
   350
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@156
   351
      Visible = false;
moel@128
   352
      SaveConfiguration();
moel@128
   353
moel@86
   354
      timer.Enabled = false;
moel@133
   355
      systemTray.Dispose();      
moel@28
   356
      computer.Close();
moel@1
   357
    }
moel@1
   358
moel@156
   359
    private void aboutMenuItem_Click(object sender, EventArgs e) {
moel@1
   360
      new AboutBox().ShowDialog();
moel@1
   361
    }
moel@1
   362
moel@1
   363
    private void treeView_Click(object sender, EventArgs e) {
moel@1
   364
      
moel@1
   365
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   366
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   367
        return;
moel@1
   368
moel@1
   369
      NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
moel@156
   370
      treeView.SelectedNode = info.Node;
moel@156
   371
      if (info.Node != null) {
moel@40
   372
        SensorNode node = info.Node.Tag as SensorNode;
moel@40
   373
        if (node != null && node.Sensor != null) {
moel@156
   374
          sensorContextMenu.MenuItems.Clear();
moel@63
   375
          if (node.Sensor.Parameters.Length > 0) {
moel@156
   376
            MenuItem item = new MenuItem("Parameters...");
moel@63
   377
            item.Click += delegate(object obj, EventArgs args) {
moel@63
   378
              ShowParameterForm(node.Sensor);
moel@63
   379
            };
moel@156
   380
            sensorContextMenu.MenuItems.Add(item);
moel@63
   381
          }
moel@156
   382
          if (nodeTextBoxText.EditEnabled) {
moel@156
   383
            MenuItem item = new MenuItem("Rename");
moel@141
   384
            item.Click += delegate(object obj, EventArgs args) {
moel@156
   385
              nodeTextBoxText.BeginEdit();
moel@141
   386
            };
moel@156
   387
            sensorContextMenu.MenuItems.Add(item);
moel@176
   388
          }
moel@111
   389
          if (node.IsVisible) {
moel@156
   390
            MenuItem item = new MenuItem("Hide");
moel@111
   391
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   392
              node.IsVisible = false;
moel@111
   393
            };
moel@156
   394
            sensorContextMenu.MenuItems.Add(item);
moel@111
   395
          } else {
moel@156
   396
            MenuItem item = new MenuItem("Unhide");
moel@111
   397
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   398
              node.IsVisible = true;
moel@111
   399
            };
moel@156
   400
            sensorContextMenu.MenuItems.Add(item);
moel@176
   401
          }
moel@176
   402
          sensorContextMenu.MenuItems.Add(new MenuItem("-"));
moel@178
   403
          {
moel@178
   404
            MenuItem item = new MenuItem("Show in Tray");
moel@178
   405
            item.Checked = systemTray.Contains(node.Sensor);
moel@178
   406
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   407
              if (item.Checked)
moel@178
   408
                systemTray.Remove(node.Sensor);
moel@178
   409
              else
moel@178
   410
                systemTray.Add(node.Sensor, true);
moel@178
   411
            };
moel@178
   412
            sensorContextMenu.MenuItems.Add(item);
moel@178
   413
          }
moel@178
   414
          {
moel@178
   415
            MenuItem item = new MenuItem("Show in Gadget");
moel@178
   416
            item.Checked = gadget.Contains(node.Sensor);
moel@178
   417
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   418
              if (item.Checked) {
moel@178
   419
                gadget.Remove(node.Sensor);
moel@178
   420
              } else {
moel@178
   421
                gadget.Add(node.Sensor);
moel@178
   422
              }
moel@178
   423
            };
moel@178
   424
            sensorContextMenu.MenuItems.Add(item);
moel@178
   425
          }
moel@176
   426
moel@156
   427
          sensorContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@40
   428
        }
moel@40
   429
      }
moel@1
   430
    }
moel@1
   431
moel@156
   432
    private void saveReportMenuItem_Click(object sender, EventArgs e) {
moel@83
   433
      string report = computer.GetReport();
moel@83
   434
      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
moel@83
   435
        using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
moel@83
   436
          w.Write(report);
moel@83
   437
        }
moel@83
   438
      }
moel@1
   439
    }
moel@1
   440
moel@82
   441
    private void SysTrayHideShow() {
moel@82
   442
      Visible = !Visible;
moel@82
   443
      if (Visible)
moel@82
   444
        Activate();    
moel@27
   445
    }
moel@27
   446
moel@27
   447
    protected override void WndProc(ref Message m) {
moel@27
   448
      const int WM_SYSCOMMAND = 0x112;
moel@27
   449
      const int SC_MINIMIZE = 0xF020;
moel@156
   450
      if (minimizeToTray.Value && 
moel@28
   451
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
moel@82
   452
        SysTrayHideShow();
moel@27
   453
      } else {      
moel@27
   454
        base.WndProc(ref m);
moel@27
   455
      }
moel@27
   456
    }
moel@27
   457
moel@82
   458
    private void hideShowClick(object sender, EventArgs e) {
moel@82
   459
      SysTrayHideShow();
moel@27
   460
    }
moel@27
   461
moel@156
   462
    private void removeMenuItem_Click(object sender, EventArgs e) {
moel@156
   463
      MenuItem item = sender as MenuItem;
moel@40
   464
      if (item == null)
moel@40
   465
        return;
moel@40
   466
moel@156
   467
      ISensor sensor = item.Parent.Tag as ISensor;
moel@40
   468
      if (sensor == null)
moel@40
   469
        return;
moel@40
   470
moel@133
   471
      systemTray.Remove(sensor);
moel@40
   472
    }
moel@63
   473
moel@63
   474
    private void ShowParameterForm(ISensor sensor) {
moel@63
   475
      ParameterForm form = new ParameterForm();
moel@63
   476
      form.Parameters = sensor.Parameters;
moel@63
   477
      form.captionLabel.Text = sensor.Name;
moel@63
   478
      form.ShowDialog();
moel@63
   479
    }
moel@63
   480
moel@63
   481
    private void treeView_NodeMouseDoubleClick(object sender, 
moel@63
   482
      TreeNodeAdvMouseEventArgs e) {
moel@63
   483
      SensorNode node = e.Node.Tag as SensorNode;
moel@63
   484
      if (node != null && node.Sensor != null && 
moel@63
   485
        node.Sensor.Parameters.Length > 0) {
moel@63
   486
        ShowParameterForm(node.Sensor);
moel@63
   487
      }
moel@63
   488
    }
moel@82
   489
moel@156
   490
    private void celciusMenuItem_Click(object sender, EventArgs e) {
moel@156
   491
      celciusMenuItem.Checked = true;
moel@156
   492
      fahrenheitMenuItem.Checked = false;
moel@165
   493
      unitManager.TemperatureUnit = TemperatureUnit.Celcius;
moel@122
   494
    }
moel@122
   495
moel@156
   496
    private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
moel@156
   497
      celciusMenuItem.Checked = false;
moel@156
   498
      fahrenheitMenuItem.Checked = true;
moel@165
   499
      unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
moel@122
   500
    }
moel@150
   501
moel@156
   502
    private void sumbitReportMenuItem_Click(object sender, EventArgs e) 
moel@150
   503
    {
moel@150
   504
      ReportForm form = new ReportForm();
moel@150
   505
      form.Report = computer.GetReport();
moel@150
   506
      form.ShowDialog();      
moel@150
   507
    }
moel@151
   508
moel@151
   509
    private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
moel@159
   510
      computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
moel@159
   511
        sensor.ResetMin();
moel@159
   512
        sensor.ResetMax();
moel@159
   513
      }));
moel@151
   514
    }
moel@1
   515
  }
moel@1
   516
}