GUI/MainForm.cs
author moel.mich
Tue, 07 Sep 2010 18:32:37 +0000
changeset 178 67b9b4d8c5fc
parent 176 c16fd81b520a
child 185 edb59f3745e8
permissions -rw-r--r--
Fixed a few closure variables. The local variables used in the closure can not be shared between closures if their values should be different.
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@176
   201
        startupManager.Startup = autoStart.Value; 
moel@156
   202
      };
moel@156
   203
moel@165
   204
      readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem, settings);
moel@156
   205
      readHddSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   206
        computer.HDDEnabled = readHddSensors.Value;
moel@156
   207
        UpdatePlotSelection(null, null);
moel@156
   208
      };
moel@156
   209
moel@176
   210
      showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem, settings);
moel@176
   211
      showGadget.Changed += delegate(object sender, EventArgs e) {
moel@176
   212
        gadget.Visible = showGadget.Value;
moel@176
   213
      };
moel@176
   214
moel@156
   215
      celciusMenuItem.Checked = 
moel@165
   216
        unitManager.TemperatureUnit == TemperatureUnit.Celcius;
moel@156
   217
      fahrenheitMenuItem.Checked = !celciusMenuItem.Checked;
moel@55
   218
moel@142
   219
      startupMenuItem.Visible = startupManager.IsAvailable;
moel@125
   220
      
moel@55
   221
      if (startMinMenuItem.Checked) {
moel@82
   222
        if (!minTrayMenuItem.Checked) {
moel@55
   223
          WindowState = FormWindowState.Minimized;
moel@55
   224
          Show();
moel@55
   225
        }
moel@55
   226
      } else {
moel@55
   227
        Show();
moel@55
   228
      }
moel@70
   229
moel@71
   230
      // Create a handle, otherwise calling Close() does not fire FormClosed     
moel@71
   231
      IntPtr handle = Handle;
moel@128
   232
moel@128
   233
      // Make sure the settings are saved when the user logs off
moel@128
   234
      Microsoft.Win32.SystemEvents.SessionEnded +=
moel@128
   235
        delegate(object sender, Microsoft.Win32.SessionEndedEventArgs e) {
moel@128
   236
          SaveConfiguration();
moel@176
   237
        };  
moel@1
   238
    }
moel@128
   239
    
moel@64
   240
    private void SubHardwareAdded(IHardware hardware, Node node) {
moel@165
   241
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   242
      node.Nodes.Add(hardwareNode);
moel@64
   243
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   244
        SubHardwareAdded(subHardware, hardwareNode);  
moel@64
   245
    }
moel@64
   246
moel@28
   247
    private void HardwareAdded(IHardware hardware) {
moel@165
   248
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   249
      root.Nodes.Add(hardwareNode);
moel@64
   250
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   251
        SubHardwareAdded(subHardware, hardwareNode);     
moel@1
   252
    }
moel@1
   253
moel@28
   254
    private void HardwareRemoved(IHardware hardware) {      
moel@1
   255
      List<Node> nodesToRemove = new List<Node>();
moel@28
   256
      foreach (Node node in root.Nodes) {
moel@28
   257
        HardwareNode hardwareNode = node as HardwareNode;
moel@28
   258
        if (hardwareNode != null && hardwareNode.Hardware == hardware)
moel@28
   259
          nodesToRemove.Add(node);
moel@28
   260
      }
moel@1
   261
      foreach (Node node in nodesToRemove)
moel@1
   262
        root.Nodes.Remove(node);
moel@1
   263
    }
moel@1
   264
moel@111
   265
    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
moel@111
   266
      Node node = e.Node.Tag as Node;
moel@111
   267
      if (node != null) {
moel@1
   268
        Color color;
moel@111
   269
        if (node.IsVisible) {
moel@111
   270
          SensorNode sensorNode = node as SensorNode;
moel@111
   271
          if (plotMenuItem.Checked && sensorNode != null &&
moel@111
   272
            sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
moel@111
   273
            e.TextColor = color;
moel@111
   274
        } else {
moel@111
   275
          e.TextColor = Color.DarkGray;
moel@111
   276
        }
moel@1
   277
      }
moel@1
   278
    }
moel@1
   279
moel@1
   280
    private void UpdatePlotSelection(object sender, 
moel@1
   281
      TreePathEventArgs e) 
moel@1
   282
    {
moel@1
   283
      List<ISensor> selected = new List<ISensor>();
moel@1
   284
      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
moel@1
   285
      int colorIndex = 0;
moel@1
   286
      foreach (TreeNodeAdv node in treeView.AllNodes) {
moel@1
   287
        SensorNode sensorNode = node.Tag as SensorNode;
moel@1
   288
        if (sensorNode != null && 
moel@1
   289
          sensorNode.Sensor.SensorType == SensorType.Temperature) {
moel@1
   290
          if (sensorNode.Plot) {
moel@1
   291
            colors.Add(sensorNode.Sensor,
moel@1
   292
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   293
            selected.Add(sensorNode.Sensor);
moel@1
   294
          }
moel@1
   295
          colorIndex++;
moel@1
   296
        }
moel@1
   297
      }
moel@1
   298
      sensorPlotColors = colors;
moel@1
   299
      plotPanel.SetSensors(selected, colors);
moel@1
   300
    }
moel@1
   301
moel@141
   302
    private void nodeTextBoxText_EditorShowing(object sender, CancelEventArgs e) 
moel@141
   303
    {
moel@141
   304
      e.Cancel = !(treeView.CurrentNode != null &&
moel@141
   305
        treeView.CurrentNode.Tag is SensorNode);
moel@141
   306
    }
moel@141
   307
moel@1
   308
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   309
      NodeControlValueEventArgs e) {
moel@1
   310
      SensorNode node = e.Node.Tag as SensorNode;
moel@1
   311
      e.Value = (node != null) && 
moel@1
   312
        (node.Sensor.SensorType == SensorType.Temperature) && 
moel@1
   313
        plotMenuItem.Checked;
moel@1
   314
    }
moel@1
   315
moel@133
   316
    private void exitClick(object sender, EventArgs e) {
moel@70
   317
      Close();      
moel@1
   318
    }
moel@1
   319
moel@86
   320
    private void timer_Tick(object sender, EventArgs e) {
moel@110
   321
      computer.Accept(updateVisitor);
moel@1
   322
      treeView.Invalidate();
moel@1
   323
      plotPanel.Invalidate();
moel@133
   324
      systemTray.Redraw();
moel@176
   325
      gadget.Redraw();
moel@1
   326
    }
moel@1
   327
moel@128
   328
    private void SaveConfiguration() {
moel@14
   329
      if (WindowState != FormWindowState.Minimized) {
moel@166
   330
        settings.SetValue("mainForm.Location.X", Location.X);
moel@166
   331
        settings.SetValue("mainForm.Location.Y", Location.Y);
moel@166
   332
        settings.SetValue("mainForm.Width", ClientSize.Width);
moel@166
   333
        settings.SetValue("mainForm.Height", ClientSize.Height);
moel@14
   334
      }
moel@86
   335
moel@128
   336
      foreach (TreeColumn column in treeView.Columns)
moel@166
   337
        settings.SetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   338
          column.Width);
moel@113
   339
moel@165
   340
      settings.Save(Path.ChangeExtension(
moel@165
   341
        System.Windows.Forms.Application.ExecutablePath, ".config"));
moel@128
   342
    }
moel@128
   343
moel@128
   344
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@156
   345
      Visible = false;
moel@128
   346
      SaveConfiguration();
moel@128
   347
moel@86
   348
      timer.Enabled = false;
moel@133
   349
      systemTray.Dispose();      
moel@28
   350
      computer.Close();
moel@1
   351
    }
moel@1
   352
moel@156
   353
    private void aboutMenuItem_Click(object sender, EventArgs e) {
moel@1
   354
      new AboutBox().ShowDialog();
moel@1
   355
    }
moel@1
   356
moel@1
   357
    private void treeView_Click(object sender, EventArgs e) {
moel@1
   358
      
moel@1
   359
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   360
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   361
        return;
moel@1
   362
moel@1
   363
      NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y));
moel@156
   364
      treeView.SelectedNode = info.Node;
moel@156
   365
      if (info.Node != null) {
moel@40
   366
        SensorNode node = info.Node.Tag as SensorNode;
moel@40
   367
        if (node != null && node.Sensor != null) {
moel@156
   368
          sensorContextMenu.MenuItems.Clear();
moel@63
   369
          if (node.Sensor.Parameters.Length > 0) {
moel@156
   370
            MenuItem item = new MenuItem("Parameters...");
moel@63
   371
            item.Click += delegate(object obj, EventArgs args) {
moel@63
   372
              ShowParameterForm(node.Sensor);
moel@63
   373
            };
moel@156
   374
            sensorContextMenu.MenuItems.Add(item);
moel@63
   375
          }
moel@156
   376
          if (nodeTextBoxText.EditEnabled) {
moel@156
   377
            MenuItem item = new MenuItem("Rename");
moel@141
   378
            item.Click += delegate(object obj, EventArgs args) {
moel@156
   379
              nodeTextBoxText.BeginEdit();
moel@141
   380
            };
moel@156
   381
            sensorContextMenu.MenuItems.Add(item);
moel@176
   382
          }
moel@111
   383
          if (node.IsVisible) {
moel@156
   384
            MenuItem item = new MenuItem("Hide");
moel@111
   385
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   386
              node.IsVisible = false;
moel@111
   387
            };
moel@156
   388
            sensorContextMenu.MenuItems.Add(item);
moel@111
   389
          } else {
moel@156
   390
            MenuItem item = new MenuItem("Unhide");
moel@111
   391
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   392
              node.IsVisible = true;
moel@111
   393
            };
moel@156
   394
            sensorContextMenu.MenuItems.Add(item);
moel@176
   395
          }
moel@176
   396
          sensorContextMenu.MenuItems.Add(new MenuItem("-"));
moel@178
   397
          {
moel@178
   398
            MenuItem item = new MenuItem("Show in Tray");
moel@178
   399
            item.Checked = systemTray.Contains(node.Sensor);
moel@178
   400
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   401
              if (item.Checked)
moel@178
   402
                systemTray.Remove(node.Sensor);
moel@178
   403
              else
moel@178
   404
                systemTray.Add(node.Sensor, true);
moel@178
   405
            };
moel@178
   406
            sensorContextMenu.MenuItems.Add(item);
moel@178
   407
          }
moel@178
   408
          {
moel@178
   409
            MenuItem item = new MenuItem("Show in Gadget");
moel@178
   410
            item.Checked = gadget.Contains(node.Sensor);
moel@178
   411
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   412
              if (item.Checked) {
moel@178
   413
                gadget.Remove(node.Sensor);
moel@178
   414
              } else {
moel@178
   415
                gadget.Add(node.Sensor);
moel@178
   416
              }
moel@178
   417
            };
moel@178
   418
            sensorContextMenu.MenuItems.Add(item);
moel@178
   419
          }
moel@176
   420
moel@156
   421
          sensorContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@40
   422
        }
moel@40
   423
      }
moel@1
   424
    }
moel@1
   425
moel@156
   426
    private void saveReportMenuItem_Click(object sender, EventArgs e) {
moel@83
   427
      string report = computer.GetReport();
moel@83
   428
      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
moel@83
   429
        using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
moel@83
   430
          w.Write(report);
moel@83
   431
        }
moel@83
   432
      }
moel@1
   433
    }
moel@1
   434
moel@82
   435
    private void SysTrayHideShow() {
moel@82
   436
      Visible = !Visible;
moel@82
   437
      if (Visible)
moel@82
   438
        Activate();    
moel@27
   439
    }
moel@27
   440
moel@27
   441
    protected override void WndProc(ref Message m) {
moel@27
   442
      const int WM_SYSCOMMAND = 0x112;
moel@27
   443
      const int SC_MINIMIZE = 0xF020;
moel@156
   444
      if (minimizeToTray.Value && 
moel@28
   445
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
moel@82
   446
        SysTrayHideShow();
moel@27
   447
      } else {      
moel@27
   448
        base.WndProc(ref m);
moel@27
   449
      }
moel@27
   450
    }
moel@27
   451
moel@82
   452
    private void hideShowClick(object sender, EventArgs e) {
moel@82
   453
      SysTrayHideShow();
moel@27
   454
    }
moel@27
   455
moel@156
   456
    private void removeMenuItem_Click(object sender, EventArgs e) {
moel@156
   457
      MenuItem item = sender as MenuItem;
moel@40
   458
      if (item == null)
moel@40
   459
        return;
moel@40
   460
moel@156
   461
      ISensor sensor = item.Parent.Tag as ISensor;
moel@40
   462
      if (sensor == null)
moel@40
   463
        return;
moel@40
   464
moel@133
   465
      systemTray.Remove(sensor);
moel@40
   466
    }
moel@63
   467
moel@63
   468
    private void ShowParameterForm(ISensor sensor) {
moel@63
   469
      ParameterForm form = new ParameterForm();
moel@63
   470
      form.Parameters = sensor.Parameters;
moel@63
   471
      form.captionLabel.Text = sensor.Name;
moel@63
   472
      form.ShowDialog();
moel@63
   473
    }
moel@63
   474
moel@63
   475
    private void treeView_NodeMouseDoubleClick(object sender, 
moel@63
   476
      TreeNodeAdvMouseEventArgs e) {
moel@63
   477
      SensorNode node = e.Node.Tag as SensorNode;
moel@63
   478
      if (node != null && node.Sensor != null && 
moel@63
   479
        node.Sensor.Parameters.Length > 0) {
moel@63
   480
        ShowParameterForm(node.Sensor);
moel@63
   481
      }
moel@63
   482
    }
moel@82
   483
moel@156
   484
    private void celciusMenuItem_Click(object sender, EventArgs e) {
moel@156
   485
      celciusMenuItem.Checked = true;
moel@156
   486
      fahrenheitMenuItem.Checked = false;
moel@165
   487
      unitManager.TemperatureUnit = TemperatureUnit.Celcius;
moel@122
   488
    }
moel@122
   489
moel@156
   490
    private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
moel@156
   491
      celciusMenuItem.Checked = false;
moel@156
   492
      fahrenheitMenuItem.Checked = true;
moel@165
   493
      unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
moel@122
   494
    }
moel@150
   495
moel@156
   496
    private void sumbitReportMenuItem_Click(object sender, EventArgs e) 
moel@150
   497
    {
moel@150
   498
      ReportForm form = new ReportForm();
moel@150
   499
      form.Report = computer.GetReport();
moel@150
   500
      form.ShowDialog();      
moel@150
   501
    }
moel@151
   502
moel@151
   503
    private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
moel@159
   504
      computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
moel@159
   505
        sensor.ResetMin();
moel@159
   506
        sensor.ResetMax();
moel@159
   507
      }));
moel@151
   508
    }
moel@1
   509
  }
moel@1
   510
}