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