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