GUI/MainForm.cs
author moel.mich
Wed, 22 Jun 2011 22:36:17 +0000
changeset 302 44c0e7f76e9e
parent 298 96263190189a
child 304 16a86362c2ca
permissions -rw-r--r--
Removed the bitmap.GetHbitmap call for every redraw of the gadget. This should reduce dynamic memory allocation and freeing and removes some of the page faults increasing.
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@295
    65
    private Form plotForm;
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@295
    78
    private UserRadioGroup plotLocation;
paulwerelds@223
    79
paulwerelds@223
    80
    private WmiProvider wmiProvider;
moel@156
    81
moel@288
    82
    private bool selectionDragging = false;
moel@288
    83
moel@28
    84
    public MainForm() {      
moel@1
    85
      InitializeComponent();
moel@156
    86
moel@291
    87
      // check if the OpenHardwareMonitorLib assembly has the correct version
moel@291
    88
      if (Assembly.GetAssembly(typeof(Computer)).GetName().Version !=
moel@291
    89
        Assembly.GetExecutingAssembly().GetName().Version) {
moel@291
    90
        MessageBox.Show(
moel@291
    91
          "The version of the file OpenHardwareMonitorLib.dll is incompatible.",
moel@291
    92
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@291
    93
        Environment.Exit(0);
moel@291
    94
      }
moel@291
    95
moel@165
    96
      this.settings = new PersistentSettings();      
moel@165
    97
      this.settings.Load(Path.ChangeExtension(
paulwerelds@198
    98
        Application.ExecutablePath, ".config"));
moel@165
    99
moel@165
   100
      this.unitManager = new UnitManager(settings);
moel@165
   101
moel@156
   102
      // set the DockStyle here, to avoid conflicts with the MainMenu
moel@156
   103
      this.splitContainer.Dock = DockStyle.Fill;
moel@202
   104
            
moel@1
   105
      this.Font = SystemFonts.MessageBoxFont;
moel@1
   106
      treeView.Font = SystemFonts.MessageBoxFont;
moel@267
   107
      plotPanel.Font = SystemFonts.MessageBoxFont;           
moel@1
   108
      
moel@133
   109
      nodeCheckBox.IsVisibleValueNeeded += nodeCheckBox_IsVisibleValueNeeded;
moel@133
   110
      nodeCheckBox.CheckStateChanged += UpdatePlotSelection;
moel@133
   111
      nodeTextBoxText.DrawText += nodeTextBoxText_DrawText;
moel@133
   112
      nodeTextBoxValue.DrawText += nodeTextBoxText_DrawText;
moel@133
   113
      nodeTextBoxMin.DrawText += nodeTextBoxText_DrawText;
moel@133
   114
      nodeTextBoxMax.DrawText += nodeTextBoxText_DrawText;
moel@141
   115
      nodeTextBoxText.EditorShowing += nodeTextBoxText_EditorShowing;
moel@1
   116
moel@113
   117
      foreach (TreeColumn column in treeView.Columns) 
moel@165
   118
        column.Width = Math.Max(20, Math.Min(400,
moel@166
   119
          settings.GetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   120
          column.Width)));
moel@113
   121
moel@1
   122
      treeModel = new TreeModel();
moel@1
   123
      root = new Node(System.Environment.MachineName);
moel@1
   124
      root.Image = Utilities.EmbeddedResources.GetImage("computer.png");
moel@1
   125
      
moel@1
   126
      treeModel.Nodes.Add(root);
moel@165
   127
      treeView.Model = treeModel;
moel@40
   128
moel@165
   129
      this.computer = new Computer(settings);
moel@165
   130
moel@165
   131
      systemTray = new SystemTray(computer, settings);
moel@133
   132
      systemTray.HideShowCommand += hideShowClick;
moel@133
   133
      systemTray.ExitCommand += exitClick;
moel@1
   134
moel@202
   135
      int p = (int)Environment.OSVersion.Platform;
moel@202
   136
      if ((p == 4) || (p == 128)) { // Unix
moel@287
   137
        treeView.RowHeight = Math.Max(treeView.RowHeight, 18); 
moel@202
   138
        splitContainer.BorderStyle = BorderStyle.None;
moel@202
   139
        splitContainer.Border3DStyle = Border3DStyle.Adjust;
moel@202
   140
        splitContainer.SplitterWidth = 4;
moel@202
   141
        treeView.BorderStyle = BorderStyle.Fixed3D;
moel@202
   142
        plotPanel.BorderStyle = BorderStyle.Fixed3D;
moel@202
   143
        gadgetMenuItem.Visible = false;
moel@202
   144
        minCloseMenuItem.Visible = false;
moel@269
   145
        minTrayMenuItem.Visible = false;
moel@269
   146
        startMinMenuItem.Visible = false;
moel@202
   147
      } else { // Windows
moel@287
   148
        treeView.RowHeight = Math.Max(treeView.Font.Height + 1, 18); 
moel@267
   149
moel@202
   150
        gadget = new SensorGadget(computer, settings, unitManager);
moel@244
   151
        gadget.HideShowCommand += hideShowClick;
moel@244
   152
moel@232
   153
        wmiProvider = new WmiProvider(computer);
moel@295
   154
      }    
moel@295
   155
      
moel@28
   156
      computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
moel@232
   157
      computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);        
paulwerelds@223
   158
moel@28
   159
      computer.Open();
moel@28
   160
moel@86
   161
      timer.Enabled = true;
moel@86
   162
moel@111
   163
      plotColorPalette = new Color[13];
moel@1
   164
      plotColorPalette[0] = Color.Blue;
moel@1
   165
      plotColorPalette[1] = Color.OrangeRed;
moel@1
   166
      plotColorPalette[2] = Color.Green;
moel@1
   167
      plotColorPalette[3] = Color.LightSeaGreen;
moel@1
   168
      plotColorPalette[4] = Color.Goldenrod;
moel@1
   169
      plotColorPalette[5] = Color.DarkViolet;
moel@1
   170
      plotColorPalette[6] = Color.YellowGreen;
moel@1
   171
      plotColorPalette[7] = Color.SaddleBrown;
moel@111
   172
      plotColorPalette[8] = Color.RoyalBlue;
moel@111
   173
      plotColorPalette[9] = Color.DeepPink;
moel@111
   174
      plotColorPalette[10] = Color.MediumSeaGreen;
moel@111
   175
      plotColorPalette[11] = Color.Olive;
moel@111
   176
      plotColorPalette[12] = Color.Firebrick;
moel@1
   177
paulwerelds@223
   178
      showHiddenSensors = new UserOption("hiddenMenuItem", false,
paulwerelds@223
   179
        hiddenMenuItem, settings);
moel@156
   180
      showHiddenSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   181
        treeModel.ForceVisible = showHiddenSensors.Value;
moel@156
   182
      };
moel@111
   183
paulwerelds@223
   184
      showValue = new UserOption("valueMenuItem", true, valueMenuItem,
paulwerelds@223
   185
        settings);
moel@156
   186
      showValue.Changed += delegate(object sender, EventArgs e) {
moel@156
   187
        treeView.Columns[1].IsVisible = showValue.Value;
moel@156
   188
      };
moel@122
   189
moel@165
   190
      showMin = new UserOption("minMenuItem", false, minMenuItem, settings);
moel@156
   191
      showMin.Changed += delegate(object sender, EventArgs e) {
moel@156
   192
        treeView.Columns[2].IsVisible = showMin.Value;
moel@156
   193
      };
moel@156
   194
moel@165
   195
      showMax = new UserOption("maxMenuItem", true, maxMenuItem, settings);
moel@156
   196
      showMax.Changed += delegate(object sender, EventArgs e) {
moel@156
   197
        treeView.Columns[3].IsVisible = showMax.Value;
moel@156
   198
      };
moel@156
   199
paulwerelds@223
   200
      startMinimized = new UserOption("startMinMenuItem", false,
paulwerelds@223
   201
        startMinMenuItem, settings);
moel@156
   202
paulwerelds@223
   203
      minimizeToTray = new UserOption("minTrayMenuItem", true,
paulwerelds@223
   204
        minTrayMenuItem, settings);
moel@156
   205
      minimizeToTray.Changed += delegate(object sender, EventArgs e) {
moel@156
   206
        systemTray.IsMainIconEnabled = minimizeToTray.Value;
moel@156
   207
      };
moel@156
   208
paulwerelds@223
   209
      minimizeOnClose = new UserOption("minCloseMenuItem", false,
paulwerelds@223
   210
        minCloseMenuItem, settings);
paulwerelds@198
   211
paulwerelds@223
   212
      autoStart = new UserOption(null, startupManager.Startup,
paulwerelds@223
   213
        startupMenuItem, settings);
moel@156
   214
      autoStart.Changed += delegate(object sender, EventArgs e) {
moel@185
   215
        try {
moel@185
   216
          startupManager.Startup = autoStart.Value;
moel@185
   217
        } catch (InvalidOperationException) {
moel@185
   218
          MessageBox.Show("Updating the auto-startup option failed.", "Error", 
moel@185
   219
            MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@185
   220
          autoStart.Value = startupManager.Startup;
moel@185
   221
        }
moel@156
   222
      };
moel@156
   223
paulwerelds@223
   224
      readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem,
paulwerelds@223
   225
        settings);
moel@156
   226
      readHddSensors.Changed += delegate(object sender, EventArgs e) {
moel@156
   227
        computer.HDDEnabled = readHddSensors.Value;
moel@156
   228
        UpdatePlotSelection(null, null);
moel@156
   229
      };
moel@156
   230
paulwerelds@223
   231
      showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem,
paulwerelds@223
   232
        settings);
moel@176
   233
      showGadget.Changed += delegate(object sender, EventArgs e) {
moel@202
   234
        if (gadget != null) 
moel@202
   235
          gadget.Visible = showGadget.Value;
moel@176
   236
      };
moel@176
   237
moel@299
   238
      celsiusMenuItem.Checked = 
moel@299
   239
        unitManager.TemperatureUnit == TemperatureUnit.Celsius;
moel@299
   240
      fahrenheitMenuItem.Checked = !celsiusMenuItem.Checked;
moel@55
   241
moel@295
   242
      InitializePlotForm();
moel@295
   243
moel@142
   244
      startupMenuItem.Visible = startupManager.IsAvailable;
moel@125
   245
      
moel@55
   246
      if (startMinMenuItem.Checked) {
moel@82
   247
        if (!minTrayMenuItem.Checked) {
moel@55
   248
          WindowState = FormWindowState.Minimized;
moel@55
   249
          Show();
moel@55
   250
        }
moel@55
   251
      } else {
moel@55
   252
        Show();
moel@55
   253
      }
moel@70
   254
moel@71
   255
      // Create a handle, otherwise calling Close() does not fire FormClosed     
moel@71
   256
      IntPtr handle = Handle;
moel@128
   257
moel@128
   258
      // Make sure the settings are saved when the user logs off
paulwerelds@223
   259
      Microsoft.Win32.SystemEvents.SessionEnded += delegate {
paulwerelds@223
   260
        SaveConfiguration();
paulwerelds@223
   261
      };  
moel@1
   262
    }
moel@295
   263
moel@295
   264
    private void InitializePlotForm() {
moel@295
   265
      plotForm = new Form();
moel@295
   266
      plotForm.FormBorderStyle = FormBorderStyle.SizableToolWindow;
moel@295
   267
      plotForm.ShowInTaskbar = false;
moel@295
   268
      plotForm.StartPosition = FormStartPosition.Manual;
moel@295
   269
      this.AddOwnedForm(plotForm);
moel@295
   270
      plotForm.Bounds = new Rectangle {
moel@295
   271
        X = settings.GetValue("plotForm.Location.X", -100000),
moel@295
   272
        Y = settings.GetValue("plotForm.Location.Y", 100),
moel@295
   273
        Width = settings.GetValue("plotForm.Width", 600),
moel@295
   274
        Height = settings.GetValue("plotForm.Height", 400)
moel@295
   275
      };
moel@295
   276
moel@295
   277
      showPlot = new UserOption("plotMenuItem", false, plotMenuItem, settings);
moel@295
   278
      plotLocation = new UserRadioGroup("plotLocation", 0,
moel@295
   279
        new[] { plotWindowMenuItem, plotBottomMenuItem, plotRightMenuItem },
moel@295
   280
        settings);
moel@295
   281
moel@295
   282
      showPlot.Changed += delegate(object sender, EventArgs e) {
moel@295
   283
        if (plotLocation.Value == 0) {
moel@295
   284
          if (showPlot.Value && this.Visible)
moel@295
   285
            plotForm.Show();
moel@295
   286
          else
moel@295
   287
            plotForm.Hide();
moel@295
   288
        } else {
moel@295
   289
          splitContainer.Panel2Collapsed = !showPlot.Value;
moel@295
   290
        }
moel@295
   291
        treeView.Invalidate();
moel@295
   292
      };
moel@295
   293
      plotLocation.Changed += delegate(object sender, EventArgs e) {
moel@295
   294
        switch (plotLocation.Value) {
moel@295
   295
          case 0:
moel@295
   296
            splitContainer.Panel2.Controls.Clear();
moel@295
   297
            splitContainer.Panel2Collapsed = true;
moel@295
   298
            plotForm.Controls.Add(plotPanel);
moel@295
   299
            if (showPlot.Value && this.Visible)
moel@295
   300
              plotForm.Show();
moel@295
   301
            break;
moel@295
   302
          case 1:
moel@295
   303
            plotForm.Controls.Clear();
moel@295
   304
            plotForm.Hide();
moel@295
   305
            splitContainer.Orientation = Orientation.Horizontal;
moel@295
   306
            splitContainer.Panel2.Controls.Add(plotPanel);
moel@295
   307
            splitContainer.Panel2Collapsed = !showPlot.Value;
moel@295
   308
            break;
moel@295
   309
          case 2:
moel@295
   310
            plotForm.Controls.Clear();
moel@295
   311
            plotForm.Hide();
moel@295
   312
            splitContainer.Orientation = Orientation.Vertical;
moel@295
   313
            splitContainer.Panel2.Controls.Add(plotPanel);
moel@295
   314
            splitContainer.Panel2Collapsed = !showPlot.Value;
moel@295
   315
            break;
moel@295
   316
        }
moel@295
   317
      };
moel@297
   318
moel@297
   319
      plotForm.FormClosing += delegate(object sender, FormClosingEventArgs e) {
moel@297
   320
        if (e.CloseReason == CloseReason.UserClosing) {
moel@297
   321
          // just switch off the plotting when the user closes the form
moel@297
   322
          if (plotLocation.Value == 0) {
moel@297
   323
            showPlot.Value = false;
moel@297
   324
          }
moel@297
   325
          e.Cancel = true;
moel@295
   326
        }
moel@295
   327
      };
moel@297
   328
moel@295
   329
      EventHandler moveOrResizePlotForm = delegate(object sender, EventArgs e) {
moel@295
   330
        if (plotForm.WindowState != FormWindowState.Minimized) {
moel@295
   331
          settings.SetValue("plotForm.Location.X", plotForm.Bounds.X);
moel@295
   332
          settings.SetValue("plotForm.Location.Y", plotForm.Bounds.Y);
moel@295
   333
          settings.SetValue("plotForm.Width", plotForm.Bounds.Width);
moel@295
   334
          settings.SetValue("plotForm.Height", plotForm.Bounds.Height);
moel@295
   335
        }
moel@295
   336
      };
moel@295
   337
      plotForm.Move += moveOrResizePlotForm;
moel@295
   338
      plotForm.Resize += moveOrResizePlotForm;
moel@295
   339
moel@295
   340
      plotForm.VisibleChanged += delegate(object sender, EventArgs e) {
moel@295
   341
        Rectangle bounds = new Rectangle(plotForm.Location, plotForm.Size);
moel@295
   342
        Screen screen = Screen.FromRectangle(bounds);
moel@295
   343
        Rectangle intersection =
moel@295
   344
          Rectangle.Intersect(screen.WorkingArea, bounds);
moel@295
   345
        if (intersection.Width < Math.Min(16, bounds.Width) ||
moel@295
   346
            intersection.Height < Math.Min(16, bounds.Height)) {
moel@295
   347
          plotForm.Location = new Point(
moel@295
   348
            screen.WorkingArea.Width / 2 - bounds.Width / 2,
moel@295
   349
            screen.WorkingArea.Height / 2 - bounds.Height / 2);
moel@295
   350
        }
moel@295
   351
      };
moel@295
   352
moel@295
   353
      this.VisibleChanged += delegate(object sender, EventArgs e) {
moel@295
   354
        if (this.Visible && showPlot.Value && plotLocation.Value == 0)
moel@295
   355
          plotForm.Show();
moel@295
   356
        else
moel@295
   357
          plotForm.Hide();
moel@295
   358
      };
moel@295
   359
    }
moel@128
   360
    
moel@64
   361
    private void SubHardwareAdded(IHardware hardware, Node node) {
moel@165
   362
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   363
      node.Nodes.Add(hardwareNode);
moel@64
   364
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   365
        SubHardwareAdded(subHardware, hardwareNode);  
moel@64
   366
    }
moel@64
   367
moel@28
   368
    private void HardwareAdded(IHardware hardware) {
moel@165
   369
      Node hardwareNode = new HardwareNode(hardware, settings, unitManager);
moel@64
   370
      root.Nodes.Add(hardwareNode);
moel@64
   371
      foreach (IHardware subHardware in hardware.SubHardware)
moel@64
   372
        SubHardwareAdded(subHardware, hardwareNode);     
moel@1
   373
    }
moel@1
   374
moel@28
   375
    private void HardwareRemoved(IHardware hardware) {      
moel@1
   376
      List<Node> nodesToRemove = new List<Node>();
moel@28
   377
      foreach (Node node in root.Nodes) {
moel@28
   378
        HardwareNode hardwareNode = node as HardwareNode;
moel@28
   379
        if (hardwareNode != null && hardwareNode.Hardware == hardware)
moel@28
   380
          nodesToRemove.Add(node);
moel@28
   381
      }
moel@1
   382
      foreach (Node node in nodesToRemove)
moel@1
   383
        root.Nodes.Remove(node);
moel@1
   384
    }
moel@1
   385
moel@111
   386
    private void nodeTextBoxText_DrawText(object sender, DrawEventArgs e) {       
moel@111
   387
      Node node = e.Node.Tag as Node;
moel@111
   388
      if (node != null) {
moel@1
   389
        Color color;
moel@111
   390
        if (node.IsVisible) {
moel@111
   391
          SensorNode sensorNode = node as SensorNode;
moel@111
   392
          if (plotMenuItem.Checked && sensorNode != null &&
moel@111
   393
            sensorPlotColors.TryGetValue(sensorNode.Sensor, out color))
moel@111
   394
            e.TextColor = color;
moel@111
   395
        } else {
moel@111
   396
          e.TextColor = Color.DarkGray;
moel@111
   397
        }
moel@1
   398
      }
moel@1
   399
    }
moel@1
   400
moel@1
   401
    private void UpdatePlotSelection(object sender, 
moel@1
   402
      TreePathEventArgs e) 
moel@1
   403
    {
moel@1
   404
      List<ISensor> selected = new List<ISensor>();
moel@1
   405
      IDictionary<ISensor, Color> colors = new Dictionary<ISensor, Color>();
moel@1
   406
      int colorIndex = 0;
moel@1
   407
      foreach (TreeNodeAdv node in treeView.AllNodes) {
moel@1
   408
        SensorNode sensorNode = node.Tag as SensorNode;
moel@1
   409
        if (sensorNode != null && 
moel@1
   410
          sensorNode.Sensor.SensorType == SensorType.Temperature) {
moel@1
   411
          if (sensorNode.Plot) {
moel@1
   412
            colors.Add(sensorNode.Sensor,
moel@1
   413
              plotColorPalette[colorIndex % plotColorPalette.Length]);
moel@1
   414
            selected.Add(sensorNode.Sensor);
moel@1
   415
          }
moel@1
   416
          colorIndex++;
moel@1
   417
        }
moel@1
   418
      }
moel@1
   419
      sensorPlotColors = colors;
moel@1
   420
      plotPanel.SetSensors(selected, colors);
moel@1
   421
    }
moel@1
   422
paulwerelds@223
   423
    private void nodeTextBoxText_EditorShowing(object sender,
paulwerelds@223
   424
      CancelEventArgs e) 
moel@141
   425
    {
moel@141
   426
      e.Cancel = !(treeView.CurrentNode != null &&
moel@275
   427
        (treeView.CurrentNode.Tag is SensorNode || 
moel@275
   428
         treeView.CurrentNode.Tag is HardwareNode));
moel@141
   429
    }
moel@141
   430
moel@1
   431
    private void nodeCheckBox_IsVisibleValueNeeded(object sender, 
moel@1
   432
      NodeControlValueEventArgs e) {
moel@1
   433
      SensorNode node = e.Node.Tag as SensorNode;
moel@1
   434
      e.Value = (node != null) && 
moel@1
   435
        (node.Sensor.SensorType == SensorType.Temperature) && 
moel@1
   436
        plotMenuItem.Checked;
moel@1
   437
    }
moel@1
   438
moel@133
   439
    private void exitClick(object sender, EventArgs e) {
paulwerelds@198
   440
      Close();
moel@1
   441
    }
moel@1
   442
moel@86
   443
    private void timer_Tick(object sender, EventArgs e) {
moel@110
   444
      computer.Accept(updateVisitor);
moel@1
   445
      treeView.Invalidate();
moel@1
   446
      plotPanel.Invalidate();
paulwerelds@223
   447
      systemTray.Redraw();
moel@202
   448
      if (gadget != null)
moel@202
   449
        gadget.Redraw();
paulwerelds@223
   450
paulwerelds@223
   451
      if (wmiProvider != null)
paulwerelds@223
   452
        wmiProvider.Update();
moel@1
   453
    }
moel@1
   454
moel@128
   455
    private void SaveConfiguration() {
moel@128
   456
      foreach (TreeColumn column in treeView.Columns)
moel@166
   457
        settings.SetValue("treeView.Columns." + column.Header + ".Width",
moel@113
   458
          column.Width);
moel@113
   459
moel@212
   460
      string fileName = Path.ChangeExtension(
moel@212
   461
          System.Windows.Forms.Application.ExecutablePath, ".config");
moel@212
   462
      try {
moel@212
   463
        settings.Save(fileName);
moel@212
   464
      } catch (UnauthorizedAccessException) {
paulwerelds@214
   465
        MessageBox.Show("Access to the path '" + fileName + "' is denied. " +
moel@284
   466
          "The current settings could not be saved.",
moel@284
   467
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@284
   468
      } catch (IOException) {
moel@284
   469
        MessageBox.Show("The path '" + fileName + "' is not writeable. " +
moel@284
   470
          "The current settings could not be saved.",
moel@212
   471
          "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
moel@212
   472
      }
moel@128
   473
    }
moel@128
   474
paulwerelds@214
   475
    private void MainForm_Load(object sender, EventArgs e) {
paulwerelds@214
   476
      Rectangle newBounds = new Rectangle {
paulwerelds@214
   477
        X = settings.GetValue("mainForm.Location.X", Location.X),
paulwerelds@214
   478
        Y = settings.GetValue("mainForm.Location.Y", Location.Y),
paulwerelds@214
   479
        Width = settings.GetValue("mainForm.Width", 470),
paulwerelds@214
   480
        Height = settings.GetValue("mainForm.Height", 640)
paulwerelds@214
   481
      };
paulwerelds@214
   482
paulwerelds@223
   483
      Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue,
paulwerelds@214
   484
        int.MinValue, int.MinValue);
paulwerelds@214
   485
paulwerelds@214
   486
      foreach (Screen screen in Screen.AllScreens)
paulwerelds@223
   487
        fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds);
paulwerelds@214
   488
paulwerelds@223
   489
      Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds);
paulwerelds@214
   490
      if (intersection.Width < 20 || intersection.Height < 20 ||
paulwerelds@214
   491
        !settings.Contains("mainForm.Location.X")
paulwerelds@214
   492
      ) {
paulwerelds@214
   493
        newBounds.X = (Screen.PrimaryScreen.WorkingArea.Width / 2) -
paulwerelds@214
   494
                      (newBounds.Width/2);
paulwerelds@214
   495
paulwerelds@214
   496
        newBounds.Y = (Screen.PrimaryScreen.WorkingArea.Height / 2) -
paulwerelds@214
   497
                      (newBounds.Height / 2);
paulwerelds@214
   498
      }
paulwerelds@214
   499
paulwerelds@214
   500
      this.Bounds = newBounds;
paulwerelds@214
   501
    }
paulwerelds@214
   502
    
paulwerelds@214
   503
    private void MainForm_FormClosed(object sender, FormClosedEventArgs e) {
moel@298
   504
      Visible = false;      
moel@262
   505
      systemTray.IsMainIconEnabled = false;
moel@262
   506
      timer.Enabled = false;            
moel@28
   507
      computer.Close();
moel@298
   508
      SaveConfiguration();
moel@262
   509
      systemTray.Dispose();
moel@1
   510
    }
moel@1
   511
moel@156
   512
    private void aboutMenuItem_Click(object sender, EventArgs e) {
moel@1
   513
      new AboutBox().ShowDialog();
moel@1
   514
    }
moel@1
   515
moel@1
   516
    private void treeView_Click(object sender, EventArgs e) {
moel@275
   517
moel@1
   518
      MouseEventArgs m = e as MouseEventArgs;
moel@1
   519
      if (m == null || m.Button != MouseButtons.Right)
moel@1
   520
        return;
moel@1
   521
paulwerelds@223
   522
      NodeControlInfo info = treeView.GetNodeControlInfoAt(
paulwerelds@223
   523
        new Point(m.X, m.Y)
paulwerelds@223
   524
      );
moel@156
   525
      treeView.SelectedNode = info.Node;
moel@156
   526
      if (info.Node != null) {
moel@40
   527
        SensorNode node = info.Node.Tag as SensorNode;
moel@40
   528
        if (node != null && node.Sensor != null) {
moel@275
   529
          treeContextMenu.MenuItems.Clear();
moel@63
   530
          if (node.Sensor.Parameters.Length > 0) {
moel@156
   531
            MenuItem item = new MenuItem("Parameters...");
moel@63
   532
            item.Click += delegate(object obj, EventArgs args) {
moel@63
   533
              ShowParameterForm(node.Sensor);
moel@63
   534
            };
moel@275
   535
            treeContextMenu.MenuItems.Add(item);
moel@63
   536
          }
moel@156
   537
          if (nodeTextBoxText.EditEnabled) {
moel@156
   538
            MenuItem item = new MenuItem("Rename");
moel@141
   539
            item.Click += delegate(object obj, EventArgs args) {
moel@156
   540
              nodeTextBoxText.BeginEdit();
moel@141
   541
            };
moel@275
   542
            treeContextMenu.MenuItems.Add(item);
moel@176
   543
          }
moel@111
   544
          if (node.IsVisible) {
moel@156
   545
            MenuItem item = new MenuItem("Hide");
moel@111
   546
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   547
              node.IsVisible = false;
moel@111
   548
            };
moel@275
   549
            treeContextMenu.MenuItems.Add(item);
moel@111
   550
          } else {
moel@156
   551
            MenuItem item = new MenuItem("Unhide");
moel@111
   552
            item.Click += delegate(object obj, EventArgs args) {
moel@111
   553
              node.IsVisible = true;
moel@111
   554
            };
moel@275
   555
            treeContextMenu.MenuItems.Add(item);
moel@176
   556
          }
moel@275
   557
          treeContextMenu.MenuItems.Add(new MenuItem("-"));
moel@178
   558
          {
moel@178
   559
            MenuItem item = new MenuItem("Show in Tray");
moel@178
   560
            item.Checked = systemTray.Contains(node.Sensor);
moel@178
   561
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   562
              if (item.Checked)
moel@178
   563
                systemTray.Remove(node.Sensor);
moel@178
   564
              else
moel@178
   565
                systemTray.Add(node.Sensor, true);
moel@178
   566
            };
moel@275
   567
            treeContextMenu.MenuItems.Add(item);
moel@178
   568
          }
moel@211
   569
          if (gadget != null) {
moel@178
   570
            MenuItem item = new MenuItem("Show in Gadget");
moel@178
   571
            item.Checked = gadget.Contains(node.Sensor);
moel@178
   572
            item.Click += delegate(object obj, EventArgs args) {
moel@178
   573
              if (item.Checked) {
moel@178
   574
                gadget.Remove(node.Sensor);
moel@178
   575
              } else {
moel@178
   576
                gadget.Add(node.Sensor);
moel@178
   577
              }
moel@178
   578
            };
moel@275
   579
            treeContextMenu.MenuItems.Add(item);
moel@178
   580
          }
moel@247
   581
          if (node.Sensor.Control != null) {
moel@275
   582
            treeContextMenu.MenuItems.Add(new MenuItem("-"));
moel@247
   583
            IControl control = node.Sensor.Control;
moel@247
   584
            MenuItem controlItem = new MenuItem("Control");
moel@247
   585
            MenuItem defaultItem = new MenuItem("Default");
moel@247
   586
            defaultItem.Checked = control.ControlMode == ControlMode.Default;
moel@247
   587
            controlItem.MenuItems.Add(defaultItem);
moel@247
   588
            defaultItem.Click += delegate(object obj, EventArgs args) {
moel@247
   589
              control.SetDefault();
moel@247
   590
            };
moel@275
   591
            MenuItem manualItem = new MenuItem("Manual");
moel@247
   592
            controlItem.MenuItems.Add(manualItem);
moel@247
   593
            manualItem.Checked = control.ControlMode == ControlMode.Software;
moel@247
   594
            for (int i = 0; i <= 100; i += 5) {
moel@247
   595
              if (i <= control.MaxSoftwareValue &&
moel@275
   596
                  i >= control.MinSoftwareValue) {
moel@247
   597
                MenuItem item = new MenuItem(i + " %");
moel@247
   598
                manualItem.MenuItems.Add(item);
moel@247
   599
                item.Checked = control.ControlMode == ControlMode.Software &&
moel@247
   600
                  Math.Round(control.SoftwareValue) == i;
moel@247
   601
                int softwareValue = i;
moel@247
   602
                item.Click += delegate(object obj, EventArgs args) {
moel@247
   603
                  control.SetSoftware(softwareValue);
moel@247
   604
                };
moel@247
   605
              }
moel@247
   606
            }
moel@275
   607
            treeContextMenu.MenuItems.Add(controlItem);
moel@247
   608
          }
moel@176
   609
moel@275
   610
          treeContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@275
   611
        }
moel@275
   612
moel@275
   613
        HardwareNode hardwareNode = info.Node.Tag as HardwareNode;
moel@275
   614
        if (hardwareNode != null && hardwareNode.Hardware != null) {
moel@275
   615
          treeContextMenu.MenuItems.Clear();
moel@275
   616
moel@275
   617
          if (nodeTextBoxText.EditEnabled) {
moel@275
   618
            MenuItem item = new MenuItem("Rename");
moel@275
   619
            item.Click += delegate(object obj, EventArgs args) {
moel@275
   620
              nodeTextBoxText.BeginEdit();
moel@275
   621
            };
moel@275
   622
            treeContextMenu.MenuItems.Add(item);
moel@275
   623
          }
moel@275
   624
moel@275
   625
          treeContextMenu.Show(treeView, new Point(m.X, m.Y));
moel@40
   626
        }
moel@40
   627
      }
moel@1
   628
    }
moel@1
   629
moel@156
   630
    private void saveReportMenuItem_Click(object sender, EventArgs e) {
moel@83
   631
      string report = computer.GetReport();
moel@83
   632
      if (saveFileDialog.ShowDialog() == DialogResult.OK) {
moel@83
   633
        using (TextWriter w = new StreamWriter(saveFileDialog.FileName)) {
moel@83
   634
          w.Write(report);
moel@83
   635
        }
moel@83
   636
      }
moel@1
   637
    }
moel@1
   638
moel@82
   639
    private void SysTrayHideShow() {
moel@82
   640
      Visible = !Visible;
moel@82
   641
      if (Visible)
moel@82
   642
        Activate();    
moel@27
   643
    }
moel@27
   644
moel@27
   645
    protected override void WndProc(ref Message m) {
moel@27
   646
      const int WM_SYSCOMMAND = 0x112;
moel@27
   647
      const int SC_MINIMIZE = 0xF020;
paulwerelds@198
   648
      const int SC_CLOSE = 0xF060;
paulwerelds@198
   649
moel@156
   650
      if (minimizeToTray.Value && 
moel@28
   651
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_MINIMIZE) {
moel@82
   652
        SysTrayHideShow();
paulwerelds@198
   653
      } else if(minimizeOnClose.Value && 
paulwerelds@198
   654
        m.Msg == WM_SYSCOMMAND && m.WParam.ToInt32() == SC_CLOSE) {
paulwerelds@198
   655
        /*
paulwerelds@198
   656
         * Apparently the user wants to minimize rather than close
paulwerelds@198
   657
         * Now we still need to check if we're going to the tray or not
paulwerelds@198
   658
         * 
paulwerelds@198
   659
         * Note: the correct way to do this would be to send out SC_MINIMIZE,
paulwerelds@198
   660
         * but since the code here is so simple,
paulwerelds@198
   661
         * that would just be a waste of time.
paulwerelds@198
   662
         */
paulwerelds@198
   663
        if (minimizeToTray.Value)
paulwerelds@198
   664
          SysTrayHideShow();
paulwerelds@198
   665
        else
paulwerelds@198
   666
          WindowState = FormWindowState.Minimized;
moel@27
   667
      } else {      
moel@27
   668
        base.WndProc(ref m);
moel@27
   669
      }
moel@27
   670
    }
moel@27
   671
moel@82
   672
    private void hideShowClick(object sender, EventArgs e) {
moel@82
   673
      SysTrayHideShow();
moel@27
   674
    }
moel@27
   675
moel@63
   676
    private void ShowParameterForm(ISensor sensor) {
moel@63
   677
      ParameterForm form = new ParameterForm();
moel@63
   678
      form.Parameters = sensor.Parameters;
moel@63
   679
      form.captionLabel.Text = sensor.Name;
moel@63
   680
      form.ShowDialog();
moel@63
   681
    }
moel@63
   682
moel@63
   683
    private void treeView_NodeMouseDoubleClick(object sender, 
moel@63
   684
      TreeNodeAdvMouseEventArgs e) {
moel@63
   685
      SensorNode node = e.Node.Tag as SensorNode;
moel@63
   686
      if (node != null && node.Sensor != null && 
moel@63
   687
        node.Sensor.Parameters.Length > 0) {
moel@63
   688
        ShowParameterForm(node.Sensor);
moel@63
   689
      }
moel@63
   690
    }
moel@82
   691
moel@299
   692
    private void celsiusMenuItem_Click(object sender, EventArgs e) {
moel@299
   693
      celsiusMenuItem.Checked = true;
moel@156
   694
      fahrenheitMenuItem.Checked = false;
moel@299
   695
      unitManager.TemperatureUnit = TemperatureUnit.Celsius;
moel@122
   696
    }
moel@122
   697
moel@156
   698
    private void fahrenheitMenuItem_Click(object sender, EventArgs e) {
moel@299
   699
      celsiusMenuItem.Checked = false;
moel@156
   700
      fahrenheitMenuItem.Checked = true;
moel@165
   701
      unitManager.TemperatureUnit = TemperatureUnit.Fahrenheit;
moel@122
   702
    }
moel@150
   703
moel@156
   704
    private void sumbitReportMenuItem_Click(object sender, EventArgs e) 
moel@150
   705
    {
moel@150
   706
      ReportForm form = new ReportForm();
moel@150
   707
      form.Report = computer.GetReport();
moel@150
   708
      form.ShowDialog();      
moel@150
   709
    }
moel@151
   710
moel@151
   711
    private void resetMinMaxMenuItem_Click(object sender, EventArgs e) {
moel@159
   712
      computer.Accept(new SensorVisitor(delegate(ISensor sensor) {
moel@159
   713
        sensor.ResetMin();
moel@159
   714
        sensor.ResetMax();
moel@159
   715
      }));
moel@151
   716
    }
moel@241
   717
moel@241
   718
    private void MainForm_MoveOrResize(object sender, EventArgs e) {
moel@241
   719
      if (WindowState != FormWindowState.Minimized) {
moel@241
   720
        settings.SetValue("mainForm.Location.X", Bounds.X);
moel@241
   721
        settings.SetValue("mainForm.Location.Y", Bounds.Y);
moel@241
   722
        settings.SetValue("mainForm.Width", Bounds.Width);
moel@241
   723
        settings.SetValue("mainForm.Height", Bounds.Height);
moel@241
   724
      }
moel@241
   725
    }
moel@262
   726
moel@262
   727
    private void resetClick(object sender, EventArgs e) {
moel@262
   728
      // disable the fallback MainIcon during reset, otherwise icon visibility
moel@262
   729
      // might be lost 
moel@262
   730
      systemTray.IsMainIconEnabled = false;
moel@262
   731
      computer.Close();
moel@262
   732
      computer.Open();
moel@262
   733
      // restore the MainIcon setting
moel@262
   734
      systemTray.IsMainIconEnabled = minimizeToTray.Value;
moel@262
   735
    }
moel@287
   736
moel@287
   737
    private void treeView_MouseMove(object sender, MouseEventArgs e) {
moel@288
   738
      selectionDragging = selectionDragging &
moel@288
   739
        (e.Button & (MouseButtons.Left | MouseButtons.Right)) > 0; 
moel@288
   740
moel@288
   741
      if (selectionDragging)
moel@288
   742
        treeView.SelectedNode = treeView.GetNodeAt(e.Location);     
moel@288
   743
    }
moel@288
   744
moel@288
   745
    private void treeView_MouseDown(object sender, MouseEventArgs e) {
moel@288
   746
      selectionDragging = true;
moel@288
   747
    }
moel@288
   748
moel@288
   749
    private void treeView_MouseUp(object sender, MouseEventArgs e) {
moel@288
   750
      selectionDragging = false;
moel@287
   751
    }
moel@1
   752
  }
moel@1
   753
}