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