GUI/SplitContainerAdv.cs
author moel.mich
Sat, 25 Jun 2011 14:46:28 +0000
changeset 304 16a86362c2ca
child 344 3145aadca3d2
permissions -rw-r--r--
Changed the maximum buffer size for double buffering of controls that isn't disposed after each draw call to the size of the screen. This should reduce the memory allocation and disposing on each sensor update. Also page faults are no longer increasing with this change.
moel@158
     1
/*
moel@158
     2
  
moel@158
     3
  Version: MPL 1.1/GPL 2.0/LGPL 2.1
moel@158
     4
moel@158
     5
  The contents of this file are subject to the Mozilla Public License Version
moel@158
     6
  1.1 (the "License"); you may not use this file except in compliance with
moel@158
     7
  the License. You may obtain a copy of the License at
moel@158
     8
 
moel@158
     9
  http://www.mozilla.org/MPL/
moel@158
    10
moel@158
    11
  Software distributed under the License is distributed on an "AS IS" basis,
moel@158
    12
  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
moel@158
    13
  for the specific language governing rights and limitations under the License.
moel@158
    14
moel@158
    15
  The Original Code is the Open Hardware Monitor code.
moel@158
    16
moel@158
    17
  The Initial Developer of the Original Code is 
moel@158
    18
  Michael Möller <m.moeller@gmx.ch>.
moel@158
    19
  Portions created by the Initial Developer are Copyright (C) 2009-2010
moel@158
    20
  the Initial Developer. All Rights Reserved.
moel@158
    21
moel@158
    22
  Contributor(s):
moel@158
    23
moel@158
    24
  Alternatively, the contents of this file may be used under the terms of
moel@158
    25
  either the GNU General Public License Version 2 or later (the "GPL"), or
moel@158
    26
  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
moel@158
    27
  in which case the provisions of the GPL or the LGPL are applicable instead
moel@158
    28
  of those above. If you wish to allow use of your version of this file only
moel@158
    29
  under the terms of either the GPL or the LGPL, and not to allow others to
moel@158
    30
  use your version of this file under the terms of the MPL, indicate your
moel@158
    31
  decision by deleting the provisions above and replace them with the notice
moel@158
    32
  and other provisions required by the GPL or the LGPL. If you do not delete
moel@158
    33
  the provisions above, a recipient may use your version of this file under
moel@158
    34
  the terms of any one of the MPL, the GPL or the LGPL.
moel@158
    35
 
moel@158
    36
*/
moel@158
    37
moel@158
    38
using System;
moel@158
    39
using System.Collections.Generic;
moel@158
    40
using System.Drawing;
moel@158
    41
using System.Windows.Forms;
moel@158
    42
moel@158
    43
namespace OpenHardwareMonitor.GUI {
moel@158
    44
  public class SplitContainerAdv : SplitContainer {
moel@158
    45
moel@158
    46
    private int delta = 0;
moel@158
    47
    private Border3DStyle border3DStyle = Border3DStyle.Raised;
moel@158
    48
    private Color color = SystemColors.Control;
moel@158
    49
moel@158
    50
    public SplitContainerAdv()
moel@158
    51
      : base() {
moel@158
    52
      SetStyle(ControlStyles.ResizeRedraw, true);
moel@158
    53
      SetStyle(ControlStyles.AllPaintingInWmPaint, true);
moel@158
    54
      SetStyle(ControlStyles.UserPaint, true);
moel@158
    55
      SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
moel@158
    56
      SetStyle(ControlStyles.ContainerControl, true);
moel@158
    57
      UpdateStyles();
moel@158
    58
    }
moel@158
    59
moel@158
    60
    protected override void OnPaint(PaintEventArgs e) {
moel@158
    61
      base.OnPaint(e);
moel@158
    62
moel@158
    63
      Graphics g = e.Graphics;
moel@158
    64
      Rectangle r = SplitterRectangle;
moel@158
    65
      using (SolidBrush brush = new SolidBrush(color))
moel@158
    66
        g.FillRectangle(brush, r);
moel@158
    67
      ControlPaint.DrawBorder3D(g, r, border3DStyle);
moel@158
    68
    }
moel@158
    69
moel@158
    70
    protected override void OnKeyDown(KeyEventArgs e) {
moel@158
    71
      if (!base.IsSplitterFixed) {
moel@158
    72
        if (e.KeyData == Keys.Right || e.KeyData == Keys.Down) {
moel@158
    73
          SplitterDistance += SplitterIncrement;
moel@158
    74
        } else if (e.KeyData == Keys.Left || e.KeyData == Keys.Up) {
moel@158
    75
          SplitterDistance -= SplitterIncrement;
moel@158
    76
        }
moel@158
    77
        Invalidate();
moel@158
    78
      }
moel@158
    79
    }
moel@158
    80
moel@158
    81
    protected override void OnMouseDown(MouseEventArgs e) {
moel@158
    82
      if (Orientation == Orientation.Vertical) {
moel@158
    83
        delta = this.SplitterDistance - e.X;
moel@158
    84
        Cursor.Current = Cursors.VSplit;
moel@158
    85
      } else {
moel@158
    86
        delta = this.SplitterDistance - e.Y;
moel@158
    87
        Cursor.Current = Cursors.HSplit;
moel@158
    88
      }
moel@158
    89
      base.IsSplitterFixed = true;
moel@158
    90
    }
moel@158
    91
moel@158
    92
    protected override void OnMouseMove(MouseEventArgs e) {
moel@158
    93
      if (base.IsSplitterFixed) {
moel@158
    94
        if (e.Button == MouseButtons.Left) {
moel@158
    95
          if (Orientation == Orientation.Vertical) {
moel@158
    96
            if (e.X > 0 && e.X < Width) {
moel@158
    97
              SplitterDistance = e.X + delta < 0 ? 0 : e.X + delta;
moel@158
    98
            }
moel@158
    99
          } else {
moel@158
   100
            if (e.Y > 0 && e.Y < Height) {
moel@158
   101
              SplitterDistance = e.Y + delta < 0 ? 0 : e.Y + delta;
moel@158
   102
            }
moel@158
   103
          }
moel@158
   104
        } else {
moel@158
   105
          base.IsSplitterFixed = false;
moel@158
   106
        }
moel@158
   107
        Invalidate();
moel@158
   108
      } else {
moel@158
   109
        if (SplitterRectangle.Contains(e.Location)) {
moel@158
   110
          Cursor = Orientation == Orientation.Vertical ?
moel@158
   111
            Cursors.VSplit : Cursors.HSplit;
moel@158
   112
        }
moel@158
   113
      }
moel@158
   114
    }
moel@158
   115
moel@158
   116
    protected override void OnMouseLeave(EventArgs e) {
moel@158
   117
      base.OnMouseLeave(e);
moel@158
   118
      Cursor = Cursors.Default;
moel@158
   119
    }
moel@158
   120
moel@158
   121
    protected override void OnMouseUp(MouseEventArgs e) {
moel@158
   122
      delta = 0;
moel@158
   123
      base.IsSplitterFixed = false;
moel@158
   124
      Cursor.Current = Cursors.Default;
moel@158
   125
    }
moel@158
   126
moel@158
   127
    public Border3DStyle Border3DStyle {
moel@158
   128
      get { return border3DStyle; }
moel@158
   129
      set {
moel@158
   130
        border3DStyle = value;
moel@158
   131
        Invalidate(false);
moel@158
   132
      }
moel@158
   133
    }
moel@158
   134
moel@158
   135
    public Color Color {
moel@158
   136
      get { return color; }
moel@158
   137
      set {
moel@158
   138
        color = value;
moel@158
   139
        Invalidate(false);
moel@158
   140
      }
moel@158
   141
    }
moel@158
   142
moel@158
   143
    public new bool IsSplitterFixed {
moel@158
   144
      get {
moel@158
   145
        return false;
moel@158
   146
      }
moel@158
   147
    }
moel@158
   148
moel@158
   149
  }
moel@158
   150
}