GUI/MainForm.cs
author moel.mich
Thu, 19 May 2011 07:22:06 +0000
changeset 291 61c3d984fb2d
parent 288 a35a89a35532
child 295 a1c06df9928d
permissions -rw-r--r--
Fixed Issue 223.
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@275
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2011
moel@1
    20
  the Initial Developer. All Rights Reserved.
moel@1
    21
paulwerelds@223
    22
  Contributor(s): Paul Werelds
moel@1
    23
moel@1
    24
  Alternatively, the contents of this file may be used under the terms of
moel@1
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@1
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@1
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@1
    28
  of those above. If you wish to allow use of your version of this file only
moel@1
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@1
    30
  use your version of this file under the terms of the MPL, indicate your
moel@1
    31
  decision by deleting the provisions above and replace them with the notice
moel@1
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@1
    33
  the provisions above, a recipient may use your version of this file under
moel@1
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@1
    35
 
moel@1
    36
*/
moel@1
    37
moel@1
    38
using System;
moel@1
    39
using System.Collections.Generic;
moel@1
    40
using System.ComponentModel;
moel@1
    41
using System.Drawing;
moel@83
    42
using System.IO;
moel@291
    43
using System.Reflection;
moel@1
    44
using System.Windows.Forms;
moel@1
    45
using Aga.Controls.Tree;
moel@1
    46
using Aga.Controls.Tree.NodeControls;
moel@1
    47
using OpenHardwareMonitor.Hardware;
paulwerelds@227
    48
using OpenHardwareMonitor.WMI;
moel@1
    49
moel@1
    50
namespace OpenHardwareMonitor.GUI {
moel@1
    51
  public partial class MainForm : Form {
moel@1
    52
moel@165
    53
    private PersistentSettings settings;
moel@165
    54
    private UnitManager unitManager;
moel@165
    55
    private Computer computer;
moel@1
    56
    private Node root;
moel@1
    57
    private TreeModel treeModel;
moel@1
    58
    private IDictionary<ISensor, Color> sensorPlotColors = 
moel@1
    59
      new Dictionary<ISensor, Color>();
moel@1
    60
    private Color[] plotColorPalette;
moel@133
    61
    private SystemTray systemTray;    
moel@82
    62
    private StartupManager startupManager = new StartupManager();
moel@110
    63
    private UpdateVisitor updateVisitor = new UpdateVisitor();
moel@176
    64
    private SensorGadget gadget;
moel@1
    65
moel@156
    66
    private UserOption showHiddenSensors;
moel@156
    67
    private UserOption showPlot;
moel@156
    68
    private UserOption showValue;
moel@156
    69
    private UserOption showMin;
moel@156
    70
    private UserOption showMax;
moel@156
    71
    private UserOption startMinimized;
moel@156
    72
    private UserOption minimizeToTray;
paulwerelds@198
    73
    private UserOption minimizeOnClose;
moel@156
    74
    private UserOption autoStart;
moel@156
    75
    private UserOption readHddSensors;
moel@176
    76
    private UserOption showGadget;
paulwerelds@223
    77
paulwerelds@223
    78
    private WmiProvider wmiProvider;
moel@156
    79
moel@288
    80
    private bool selectionDragging = false;
moel@288
    81
moel@28
    82
    public MainForm() {      
moel@1
    83
      InitializeComponent();
moel@156
    84
moel@291
    85
      // check if the OpenHardwareMonitorLib assembly has the correct version
moel@291
    86
      if (Assembly.GetAssembly(typeof(Computer)).GetName().Version !=
moel@291
    87
        Assembly.GetExecutingAssembly().GetName().Version) {
moel@291
    88
        MessageBox.Show(
moel@291
    89
          "The version of the file OpenHardwareMonitorLib.dll is incompatible.",
moel@291
    90
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@291
    91
        Environment.Exit(0);
moel@291
    92
      }
moel@291
    93
moel@165
    94
      this.settings = new PersistentSettings();      
moel@165
    95
      this.settings.Load(Path.ChangeExtension(
paulwerelds@198
    96
        Application.ExecutablePath, ".config"));
moel@165
    97
moel@165
    98
      this.unitManager = new UnitManager(settings);
moel@165
    99
moel@156
   100
      // set the DockStyle here, to avoid conflicts with the MainMenu
moel@156
   101
      this.splitContainer.Dock = DockStyle.Fill;
moel@202
   102
            
moel@1
   103
      this.Font = SystemFonts.MessageBoxFont;
moel@1
   104
      treeView.Font = SystemFonts.MessageBoxFont;
moel@267
   105
      plotPanel.Font = SystemFonts.MessageBoxFont;           
moel@1
   106
      
moel@133
   107
      nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
moel@133
   108
      nodeCheckBox.CheckStateChanged += UpdatePlotSelection;
moel@133
   109
      nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
moel@133
   110
      nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
moel@133
   111
      nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
moel@133
   112
      nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
moel@141
   113
      nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
moel@1
   114
moel@113
   115
      foreach (TreeColumn column in treeView.Columns) 
moel@165
   116
        column.Width = Math.Max(20, Math.Min(400,
moel@166
   117
          settings.GetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   118
          column.Width)));
moel@113
   119
moel@1
   120
      treeModel = new TreeModel();
moel@1
   121
      root = new Node(System.Environment.MachineName);
moel@1
   122
      root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
moel@1
   123
      
moel@1
   124
      treeModel.Nodes.Add(root);
moel@165
   125
      treeView.Model = treeModel;
moel@40
   126
moel@165
   127
      this.computer = new Computer(settings);
moel@165
   128
moel@165
   129
      systemTray = new SystemTray(computer, settings);
moel@133
   130
      systemTray.HideShowCommand += hideShowClick;
moel@133
   131
      systemTray.ExitCommand += exitClick;
moel@1
   132
moel@202
   133
      int p = (int)Environment.OSVersion.Platform;
moel@202
   134
      if ((p == 4) || (p == 128)) { // Unix
moel@287
   135
        treeView.RowHeight = Math.Max(treeView.RowHeight, 18); 
moel@202
   136
        splitContainer.BorderStyle = BorderStyle.None;
moel@202
   137
        splitContainer.Border3DStyle = Border3DStyle.Adjust;
moel@202
   138
        splitContainer.SplitterWidth = 4;
moel@202
   139
        treeView.BorderStyle = BorderStyle.Fixed3D;
moel@202
   140
        plotPanel.BorderStyle = BorderStyle.Fixed3D;
moel@202
   141
        gadgetMenuItem.Visible = false;
moel@202
   142
        minCloseMenuItem.Visible = false;
moel@269
   143
        minTrayMenuItem.Visible = false;
moel@269
   144
        startMinMenuItem.Visible = false;
moel@202
   145
      } else { // Windows
moel@287
   146
        treeView.RowHeight = Math.Max(treeView.Font.Height + 1, 18); 
moel@267
   147
moel@202
   148
        gadget = new SensorGadget(computer, settings, unitManager);
moel@244
   149
        gadget.HideShowCommand += hideShowClick;
moel@244
   150
moel@232
   151
        wmiProvider = new WmiProvider(computer);
moel@202
   152
      }          
moel@176
   153
moel@28
   154
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@232
   155
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);        
paulwerelds@223
   156
moel@28
   157
      computer.Open();
moel@28
   158
moel@86
   159
      timer.Enabled = true;
moel@86
   160
moel@111
   161
      plotColorPalette = new Color[13];
moel@1
   162
      plotColorPalette[0] = Color.Blue;
moel@1
   163
      plotColorPalette[1] = Color.OrangeRed;
moel@1
   164
      plotColorPalette[2] = Color.Green;
moel@1
   165
      plotColorPalette[3] = Color.LightSeaGreen;
moel@1
   166
      plotColorPalette[4] = Color.Goldenrod;
moel@1
   167
      plotColorPalette[5] = Color.DarkViolet;
moel@1
   168
      plotColorPalette[6] = Color.YellowGreen;
moel@1
   169
      plotColorPalette[7] = Color.SaddleBrown;
moel@111
   170
      plotColorPalette[8] = Color.RoyalBlue;
moel@111
   171
      plotColorPalette[9] = Color.DeepPink;
moel@111
   172
      plotColorPalette[10] = Color.MediumSeaGreen;
moel@111
   173
      plotColorPalette[11] = Color.Olive;
moel@111
   174
      plotColorPalette[12] = Color.Firebrick;
moel@1
   175
paulwerelds@223
   176
      showHiddenSensors = new UserOption("hiddenMenuItem", false,
paulwerelds@223
   177
        hiddenMenuItem, settings);
moel@156
   178
      showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   179
        treeModel.ForceVisible = showHiddenSensors.Value;
moel@156
   180
      };
moel@111
   181
moel@165
   182
      showPlot = new UserOption("plotMenuItem", false, plotMenuItem, settings);
moel@156
   183
      showPlot.Changed += delegate(object sender, EventArgs e) {
moel@156
   184
        splitContainer.Panel2Collapsed = !showPlot.Value;
moel@156
   185
        treeView.Invalidate();
moel@156
   186
      };
moel@1
   187
paulwerelds@223
   188
      showValue = new UserOption("valueMenuItem", true, valueMenuItem,
paulwerelds@223
   189
        settings);
moel@156
   190
      showValue.Changed += delegate(object sender, EventArgs e) {
moel@156
   191
        treeView.Columns[1].IsVisible = showValue.Value;
moel@156
   192
      };
moel@122
   193
moel@165
   194
      showMin = new UserOption("minMenuItem", false, minMenuItem, settings);
moel@156
   195
      showMin.Changed += delegate(object sender, EventArgs e) {
moel@156
   196
        treeView.Columns[2].IsVisible = showMin.Value;
moel@156
   197
      };
moel@156
   198
moel@165
   199
      showMax = new UserOption("maxMenuItem", true, maxMenuItem, settings);
moel@156
   200
      showMax.Changed += delegate(object sender, EventArgs e) {
moel@156
   201
        treeView.Columns[3].IsVisible = showMax.Value;
moel@156
   202
      };
moel@156
   203
paulwerelds@223
   204
      startMinimized = new UserOption("startMinMenuItem", false,
paulwerelds@223
   205
        startMinMenuItem, settings);
moel@156
   206
paulwerelds@223
   207
      minimizeToTray = new UserOption("minTrayMenuItem", true,
paulwerelds@223
   208
        minTrayMenuItem, settings);
moel@156
   209
      minimizeToTray.Changed += delegate(object sender, EventArgs e) {
moel@156
   210
        systemTray.IsMainIconEnabled = minimizeToTray.Value;
moel@156
   211
      };
moel@156
   212
paulwerelds@223
   213
      minimizeOnClose = new UserOption("minCloseMenuItem", false,
paulwerelds@223
   214
        minCloseMenuItem, settings);
paulwerelds@198
   215
paulwerelds@223
   216
      autoStart = new UserOption(null, startupManager.Startup,
paulwerelds@223
   217
        startupMenuItem, settings);
moel@156
   218
      autoStart.Changed += delegate(object sender, EventArgs e) {
moel@185
   219
        try {
moel@185
   220
          startupManager.Startup = autoStart.Value;
moel@185
   221
        } catch (InvalidOperationException) {
moel@185
   222
          MessageBox.Show("Updating the auto-startup option failed.", "Error", 
moel@185
   223
            MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@185
   224
          autoStart.Value = startupManager.Startup;
moel@185
   225
        }
moel@156
   226
      };
moel@156
   227
paulwerelds@223
   228
      readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
paulwerelds@223
   229
        settings);
moel@156
   230
      readHddSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   231
        computer.HDDEnabled = readHddSensors.Value;
moel@156
   232
        UpdatePlotSelection(null, null);
moel@156
   233
      };
moel@156
   234
paulwerelds@223
   235
      showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
paulwerelds@223
   236
        settings);
moel@176
   237
      showGadget.Changed += delegate(object sender, EventArgs e) {
moel@202
   238
        if (gadget != null) 
moel@202
   239
          gadget.Visible = showGadget.Value;
moel@176
   240
      };
moel@176
   241
moel@156
   242
      celciusMenuItem.Checked = 
moel@165
   243
        unitManager.TemperatureUnit == TemperatureUnit.Celcius;
moel@156
   244
      fahrenheitMenuItem.Checked = !celciusMenuItem.Checked;
moel@55
   245
moel@142
   246
      startupMenuItem.Visible = startupManager.IsAvailable;
moel@125
   247
      
moel@55
   248
      if (startMinMenuItem.Checked) {
moel@82
   249
        if (!minTrayMenuItem.Checked) {
moel@55
   250
          WindowState = FormWindowState.Minimized;
moel@55
   251
          Show();
moel@55
   252
        }
moel@55
   253
      } else {
moel@55
   254
        Show();
moel@55
   255
      }
moel@70
   256
moel@71
   257
      // Create a handle, otherwise calling Close() does not fire FormClosed     
moel@71
   258
      IntPtr handle = Handle;
moel@128
   259
moel@128
   260
      // Make sure the settings are saved when the user logs off
paulwerelds@223
   261
      Microsoft.Win32.SystemEvents.SessionEnded += delegate {
paulwerelds@223
   262
        SaveConfiguration();
paulwerelds@223
   263
      };  
moel@1
   264
    }
moel@128
   265
    
moel@64
   266
    private void SubHardwareAdded(IHardware hardware, Node node) {
moel@165
   267
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   268
      node.Nodes.Add(hardwareNode);
moel@64
   269
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   270
        SubHardwareAdded(subHardware, hardwareNode);  
moel@64
   271
    }
moel@64
   272
moel@28
   273
    private void HardwareAdded(IHardware hardware) {
moel@165
   274
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   275
      root.Nodes.Add(hardwareNode);
moel@64
   276
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   277
        SubHardwareAdded(subHardware, hardwareNode);     
moel@1
   278
    }
moel@1
   279
moel@28
   280
    private void HardwareRemoved(IHardware hardware) {      
moel@1
   281
      List<Node> nodesToRemove = new List<Node>();
moel@28
   282
      foreach (Node node in root.Nodes) {
moel@28
   283
        HardwareNode hardwareNode = node as HardwareNode;
moel@28
   284
        if (hardwareNode != null && hardwareNode.Hardware == hardware)
moel@28
   285
          nodesToRemove.Add(node);
moel@28
   286
      }
moel@1
   287
      foreach (Node node in nodesToRemove)
moel@1
   288
        root.Nodes.Remove(node);
moel@1
   289
    }
moel@1
   290
moel@111
   291
    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
moel@111
   292
      Node node = e.Node.Tag as Node;
moel@111
   293
      if (node != null) {
moel@1
   294
        Color color;
moel@111
   295
        if (node.IsVisible) {
moel@111
   296
          SensorNode sensorNode = node as SensorNode;
moel@111
   297
          if (plotMenuItem.Checked && sensorNode != null &&
moel@111
   298
            sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
moel@111
   299
            e.TextColor = color;
moel@111
   300
        } else {
moel@111
   301
          e.TextColor = Color.DarkGray;
moel@111
   302
        }
moel@1
   303
      }
moel@1
   304
    }
moel@1
   305
moel@1
   306
    private void UpdatePlotSelection(object sender, 
moel@1
   307
      TreePathEventArgs e) 
moel@1
   308
    {
moel@1
   309
      List<ISensor> selected = new List<ISensor>();
moel@1
   310
      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
moel@1
   311
      int colorIndex = 0;
moel@1
   312
      foreach (TreeNodeAdv node in treeView.AllNodes) {
moel@1
   313
        SensorNode sensorNode = node.Tag as SensorNode;
moel@1
   314
        if (sensorNode != null && 
moel@1
   315
          sensorNode.Sensor.SensorType == SensorType.Temperature) {
moel@1
   316
          if (sensorNode.Plot) {
moel@1
   317
            colors.Add(sensorNode.Sensor,
moel@1
   318
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   319
            selected.Add(sensorNode.Sensor);
moel@1
   320
          }
moel@1
   321
          colorIndex++;
moel@1
   322
        }
moel@1
   323
      }
moel@1
   324
      sensorPlotColors = colors;
moel@1
   325
      plotPanel.SetSensors(selected, colors);
moel@1
   326
    }
moel@1
   327
paulwerelds@223
   328
    private void nodeTextBoxText_EditorShowing(object sender,
paulwerelds@223
   329
      CancelEventArgs e) 
moel@141
   330
    {
moel@141
   331
      e.Cancel = !(treeView.CurrentNode != null &&
moel@275
   332
        (treeView.CurrentNode.Tag is SensorNode || 
moel@275
   333
         treeView.CurrentNode.Tag is HardwareNode));
moel@141
   334
    }
moel@141
   335
moel@1
   336
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   337
      NodeControlValueEventArgs e) {
moel@1
   338
      SensorNode node = e.Node.Tag as SensorNode;
moel@1
   339
      e.Value = (node != null) && 
moel@1
   340
        (node.Sensor.SensorType == SensorType.Temperature) && 
moel@1
   341
        plotMenuItem.Checked;
moel@1
   342
    }
moel@1
   343
moel@133
   344
    private void exitClick(object sender, EventArgs e) {
paulwerelds@198
   345
      Close();
moel@1
   346
    }
moel@1
   347
moel@86
   348
    private void timer_Tick(object sender, EventArgs e) {
moel@110
   349
      computer.Accept(updateVisitor);
moel@1
   350
      treeView.Invalidate();
moel@1
   351
      plotPanel.Invalidate();
paulwerelds@223
   352
      systemTray.Redraw();
moel@202
   353
      if (gadget != null)
moel@202
   354
        gadget.Redraw();
paulwerelds@223
   355
paulwerelds@223
   356
      if (wmiProvider != null)
paulwerelds@223
   357
        wmiProvider.Update();
moel@1
   358
    }
moel@1
   359
moel@128
   360
    private void SaveConfiguration() {
moel@128
   361
      foreach (TreeColumn column in treeView.Columns)
moel@166
   362
        settings.SetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   363
          column.Width);
moel@113
   364
moel@212
   365
      string fileName = Path.ChangeExtension(
moel@212
   366
          System.Windows.Forms.Application.ExecutablePath, ".config");
moel@212
   367
      try {
moel@212
   368
        settings.Save(fileName);
moel@212
   369
      } catch (UnauthorizedAccessException) {
paulwerelds@214
   370
        MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
moel@284
   371
          "The current settings could not be saved.",
moel@284
   372
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@284
   373
      } catch (IOException) {
moel@284
   374
        MessageBox.Show("The path '" + fileName + "' is not writeable. " +
moel@284
   375
          "The current settings could not be saved.",
moel@212
   376
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@212
   377
      }
moel@128
   378
    }
moel@128
   379
paulwerelds@214
   380
    private void MainForm_Load(object sender, EventArgs e) {
paulwerelds@214
   381
      Rectangle newBounds = new Rectangle {
paulwerelds@214
   382
        X = settings.GetValue("mainForm.Location.X", Location.X),
paulwerelds@214
   383
        Y = settings.GetValue("mainForm.Location.Y", Location.Y),
paulwerelds@214
   384
        Width = settings.GetValue("mainForm.Width", 470),
paulwerelds@214
   385
        Height = settings.GetValue("mainForm.Height", 640)
paulwerelds@214
   386
      };
paulwerelds@214
   387
paulwerelds@223
   388
      Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
paulwerelds@214
   389
        int.MinValue, int.MinValue);
paulwerelds@214
   390
paulwerelds@214
   391
      foreach (Screen screen in Screen.AllScreens)
paulwerelds@223
   392
        fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds);
paulwerelds@214
   393
paulwerelds@223
   394
      Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds);
paulwerelds@214
   395
      if (intersection.Width < 20 || intersection.Height < 20 ||
paulwerelds@214
   396
        !settings.Contains("mainForm.Location.X")
paulwerelds@214
   397
      ) {
paulwerelds@214
   398
        newBounds.X = (Screen.PrimaryScreen.WorkingArea.Width / 2) -
paulwerelds@214
   399
                      (newBounds.Width/2);
paulwerelds@214
   400
paulwerelds@214
   401
        newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) -
paulwerelds@214
   402
                      (newBounds.Height / 2);
paulwerelds@214
   403
      }
paulwerelds@214
   404
paulwerelds@214
   405
      this.Bounds = newBounds;
paulwerelds@214
   406
    }
paulwerelds@214
   407
    
paulwerelds@214
   408
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@156
   409
      Visible = false;
moel@128
   410
      SaveConfiguration();
moel@128
   411
moel@262
   412
      systemTray.IsMainIconEnabled = false;
moel@262
   413
      timer.Enabled = false;            
moel@28
   414
      computer.Close();
moel@262
   415
      systemTray.Dispose();
moel@1
   416
    }
moel@1
   417
moel@156
   418
    private void aboutMenuItem_Click(object sender, EventArgs e) {
moel@1
   419
      new AboutBox().ShowDialog();
moel@1
   420
    }
moel@1
   421
moel@1
   422
    private void treeView_Click(object sender, EventArgs e) {
moel@275
   423
moel@1
   424
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   425
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   426
        return;
moel@1
   427
paulwerelds@223
   428
      NodeControlInfo info = treeView.GetNodeControlInfoAt(
paulwerelds@223
   429
        new Point(m.X, m.Y)
paulwerelds@223
   430
      );
moel@156
   431
      treeView.SelectedNode = info.Node;
moel@156
   432
      if (info.Node != null) {
moel@40
   433
        SensorNode node = info.Node.Tag as SensorNode;
moel@40
   434
        if (node != null && node.Sensor != null) {
moel@275
   435
          treeContextMenu.MenuItems.Clear();
moel@63
   436
          if (node.Sensor.Parameters.Length > 0) {
moel@156
   437
            MenuItem item = new MenuItem("Parameters...");
moel@63
   438
            item.Click += delegate(object obj, EventArgs args) {
moel@63
   439
              ShowParameterForm(node.Sensor);
moel@63
   440
            };
moel@275
   441
            treeContextMenu.MenuItems.Add(item);
moel@63
   442
          }
moel@156
   443
          if (nodeTextBoxText.EditEnabled) {
moel@156
   444
            MenuItem item = new MenuItem("Rename");
moel@141
   445
            item.Click += delegate(object obj, EventArgs args) {
moel@156
   446
              nodeTextBoxText.BeginEdit();
moel@141
   447
            };
moel@275
   448
            treeContextMenu.MenuItems.Add(item);
moel@176
   449
          }
moel@111
   450
          if (node.IsVisible) {
moel@156
   451
            MenuItem item = new MenuItem("Hide");
moel@111
   452
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   453
              node.IsVisible = false;
moel@111
   454
            };
moel@275
   455
            treeContextMenu.MenuItems.Add(item);
moel@111
   456
          } else {
moel@156
   457
            MenuItem item = new MenuItem("Unhide");
moel@111
   458
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   459
              node.IsVisible = true;
moel@111
   460
            };
moel@275
   461
            treeContextMenu.MenuItems.Add(item);
moel@176
   462
          }
moel@275
   463
          treeContextMenu.MenuItems.Add(new MenuItem("-"));
moel@178
   464
          {
moel@178
   465
            MenuItem item = new MenuItem("Show in Tray");
moel@178
   466
            item.Checked = systemTray.Contains(node.Sensor);
moel@178
   467
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   468
              if (item.Checked)
moel@178
   469
                systemTray.Remove(node.Sensor);
moel@178
   470
              else
moel@178
   471
                systemTray.Add(node.Sensor, true);
moel@178
   472
            };
moel@275
   473
            treeContextMenu.MenuItems.Add(item);
moel@178
   474
          }
moel@211
   475
          if (gadget != null) {
moel@178
   476
            MenuItem item = new MenuItem("Show in Gadget");
moel@178
   477
            item.Checked = gadget.Contains(node.Sensor);
moel@178
   478
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   479
              if (item.Checked) {
moel@178
   480
                gadget.Remove(node.Sensor);
moel@178
   481
              } else {
moel@178
   482
                gadget.Add(node.Sensor);
moel@178
   483
              }
moel@178
   484
            };
moel@275
   485
            treeContextMenu.MenuItems.Add(item);
moel@178
   486
          }
moel@247
   487
          if (node.Sensor.Control != null) {
moel@275
   488
            treeContextMenu.MenuItems.Add(new MenuItem("-"));
moel@247
   489
            IControl control = node.Sensor.Control;
moel@247
   490
            MenuItem controlItem = new MenuItem("Control");
moel@247
   491
            MenuItem defaultItem = new MenuItem("Default");
moel@247
   492
            defaultItem.Checked = control.ControlMode == ControlMode.Default;
moel@247
   493
            controlItem.MenuItems.Add(defaultItem);
moel@247
   494
            defaultItem.Click += delegate(object obj, EventArgs args) {
moel@247
   495
              control.SetDefault();
moel@247
   496
            };
moel@275
   497
            MenuItem manualItem = new MenuItem("Manual");
moel@247
   498
            controlItem.MenuItems.Add(manualItem);
moel@247
   499
            manualItem.Checked = control.ControlMode == ControlMode.Software;
moel@247
   500
            for (int i = 0; i <= 100; i += 5) {
moel@247
   501
              if (i <= control.MaxSoftwareValue &&
moel@275
   502
                  i >= control.MinSoftwareValue) {
moel@247
   503
                MenuItem item = new MenuItem(i + " %");
moel@247
   504
                manualItem.MenuItems.Add(item);
moel@247
   505
                item.Checked = control.ControlMode == ControlMode.Software &&
moel@247
   506
                  Math.Round(control.SoftwareValue) == i;
moel@247
   507
                int softwareValue = i;
moel@247
   508
                item.Click += delegate(object obj, EventArgs args) {
moel@247
   509
                  control.SetSoftware(softwareValue);
moel@247
   510
                };
moel@247
   511
              }
moel@247
   512
            }
moel@275
   513
            treeContextMenu.MenuItems.Add(controlItem);
moel@247
   514
          }
moel@176
   515
moel@275
   516
          treeContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@275
   517
        }
moel@275
   518
moel@275
   519
        HardwareNode hardwareNode = info.Node.Tag as HardwareNode;
moel@275
   520
        if (hardwareNode != null && hardwareNode.Hardware != null) {
moel@275
   521
          treeContextMenu.MenuItems.Clear();
moel@275
   522
moel@275
   523
          if (nodeTextBoxText.EditEnabled) {
moel@275
   524
            MenuItem item = new MenuItem("Rename");
moel@275
   525
            item.Click += delegate(object obj, EventArgs args) {
moel@275
   526
              nodeTextBoxText.BeginEdit();
moel@275
   527
            };
moel@275
   528
            treeContextMenu.MenuItems.Add(item);
moel@275
   529
          }
moel@275
   530
moel@275
   531
          treeContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@40
   532
        }
moel@40
   533
      }
moel@1
   534
    }
moel@1
   535
moel@156
   536
    private void saveReportMenuItem_Click(object sender, EventArgs e) {
moel@83
   537
      string report = computer.GetReport();
moel@83
   538
      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
moel@83
   539
        using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
moel@83
   540
          w.Write(report);
moel@83
   541
        }
moel@83
   542
      }
moel@1
   543
    }
moel@1
   544
moel@82
   545
    private void SysTrayHideShow() {
moel@82
   546
      Visible = !Visible;
moel@82
   547
      if (Visible)
moel@82
   548
        Activate();    
moel@27
   549
    }
moel@27
   550
moel@27
   551
    protected override void WndProc(ref Message m) {
moel@27
   552
      const int WM_SYSCOMMAND = 0x112;
moel@27
   553
      const int SC_MINIMIZE = 0xF020;
paulwerelds@198
   554
      const int SC_CLOSE = 0xF060;
paulwerelds@198
   555
moel@156
   556
      if (minimizeToTray.Value && 
moel@28
   557
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
moel@82
   558
        SysTrayHideShow();
paulwerelds@198
   559
      } else if(minimizeOnClose.Value && 
paulwerelds@198
   560
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE) {
paulwerelds@198
   561
        /*
paulwerelds@198
   562
         * Apparently the user wants to minimize rather than close
paulwerelds@198
   563
         * Now we still need to check if we're going to the tray or not
paulwerelds@198
   564
         * 
paulwerelds@198
   565
         * Note: the correct way to do this would be to send out SC_MINIMIZE,
paulwerelds@198
   566
         * but since the code here is so simple,
paulwerelds@198
   567
         * that would just be a waste of time.
paulwerelds@198
   568
         */
paulwerelds@198
   569
        if (minimizeToTray.Value)
paulwerelds@198
   570
          SysTrayHideShow();
paulwerelds@198
   571
        else
paulwerelds@198
   572
          WindowState = FormWindowState.Minimized;
moel@27
   573
      } else {      
moel@27
   574
        base.WndProc(ref m);
moel@27
   575
      }
moel@27
   576
    }
moel@27
   577
moel@82
   578
    private void hideShowClick(object sender, EventArgs e) {
moel@82
   579
      SysTrayHideShow();
moel@27
   580
    }
moel@27
   581
moel@63
   582
    private void ShowParameterForm(ISensor sensor) {
moel@63
   583
      ParameterForm form = new ParameterForm();
moel@63
   584
      form.Parameters = sensor.Parameters;
moel@63
   585
      form.captionLabel.Text = sensor.Name;
moel@63
   586
      form.ShowDialog();
moel@63
   587
    }
moel@63
   588
moel@63
   589
    private void treeView_NodeMouseDoubleClick(object sender, 
moel@63
   590
      TreeNodeAdvMouseEventArgs e) {
moel@63
   591
      SensorNode node = e.Node.Tag as SensorNode;
moel@63
   592
      if (node != null && node.Sensor != null && 
moel@63
   593
        node.Sensor.Parameters.Length > 0) {
moel@63
   594
        ShowParameterForm(node.Sensor);
moel@63
   595
      }
moel@63
   596
    }
moel@82
   597
moel@156
   598
    private void celciusMenuItem_Click(object sender, EventArgs e) {
moel@156
   599
      celciusMenuItem.Checked = true;
moel@156
   600
      fahrenheitMenuItem.Checked = false;
moel@165
   601
      unitManager.TemperatureUnit = TemperatureUnit.Celcius;
moel@122
   602
    }
moel@122
   603
moel@156
   604
    private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
moel@156
   605
      celciusMenuItem.Checked = false;
moel@156
   606
      fahrenheitMenuItem.Checked = true;
moel@165
   607
      unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
moel@122
   608
    }
moel@150
   609
moel@156
   610
    private void sumbitReportMenuItem_Click(object sender, EventArgs e) 
moel@150
   611
    {
moel@150
   612
      ReportForm form = new ReportForm();
moel@150
   613
      form.Report = computer.GetReport();
moel@150
   614
      form.ShowDialog();      
moel@150
   615
    }
moel@151
   616
moel@151
   617
    private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
moel@159
   618
      computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
moel@159
   619
        sensor.ResetMin();
moel@159
   620
        sensor.ResetMax();
moel@159
   621
      }));
moel@151
   622
    }
moel@241
   623
moel@241
   624
    private void MainForm_MoveOrResize(object sender, EventArgs e) {
moel@241
   625
      if (WindowState != FormWindowState.Minimized) {
moel@241
   626
        settings.SetValue("mainForm.Location.X", Bounds.X);
moel@241
   627
        settings.SetValue("mainForm.Location.Y", Bounds.Y);
moel@241
   628
        settings.SetValue("mainForm.Width", Bounds.Width);
moel@241
   629
        settings.SetValue("mainForm.Height", Bounds.Height);
moel@241
   630
      }
moel@241
   631
    }
moel@262
   632
moel@262
   633
    private void resetClick(object sender, EventArgs e) {
moel@262
   634
      // disable the fallback MainIcon during reset, otherwise icon visibility
moel@262
   635
      // might be lost 
moel@262
   636
      systemTray.IsMainIconEnabled = false;
moel@262
   637
      computer.Close();
moel@262
   638
      computer.Open();
moel@262
   639
      // restore the MainIcon setting
moel@262
   640
      systemTray.IsMainIconEnabled = minimizeToTray.Value;
moel@262
   641
    }
moel@287
   642
moel@287
   643
    private void treeView_MouseMove(object sender, MouseEventArgs e) {
moel@288
   644
      selectionDragging = selectionDragging &
moel@288
   645
        (e.Button & (MouseButtons.Left | MouseButtons.Right)) > 0; 
moel@288
   646
moel@288
   647
      if (selectionDragging)
moel@288
   648
        treeView.SelectedNode = treeView.GetNodeAt(e.Location);     
moel@288
   649
    }
moel@288
   650
moel@288
   651
    private void treeView_MouseDown(object sender, MouseEventArgs e) {
moel@288
   652
      selectionDragging = true;
moel@288
   653
    }
moel@288
   654
moel@288
   655
    private void treeView_MouseUp(object sender, MouseEventArgs e) {
moel@288
   656
      selectionDragging = false;
moel@287
   657
    }
moel@1
   658
  }
moel@1
   659
}