moel@158: /*
moel@158:  
moel@344:   This Source Code Form is subject to the terms of the Mozilla Public
moel@344:   License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344:   file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@158:  
moel@344:   Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344: 	
moel@158: */
moel@158: 
moel@158: using System;
moel@158: using System.Collections.Generic;
moel@158: using System.Drawing;
moel@158: using System.Windows.Forms;
moel@158: 
moel@158: namespace OpenHardwareMonitor.GUI {
moel@158:   public class SplitContainerAdv : SplitContainer {
moel@158: 
moel@158:     private int delta = 0;
moel@158:     private Border3DStyle border3DStyle = Border3DStyle.Raised;
moel@158:     private Color color = SystemColors.Control;
moel@158: 
moel@158:     public SplitContainerAdv()
moel@158:       : base() {
moel@158:       SetStyle(ControlStyles.ResizeRedraw, true);
moel@158:       SetStyle(ControlStyles.AllPaintingInWmPaint, true);
moel@158:       SetStyle(ControlStyles.UserPaint, true);
moel@158:       SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
moel@158:       SetStyle(ControlStyles.ContainerControl, true);
moel@158:       UpdateStyles();
moel@158:     }
moel@158: 
moel@158:     protected override void OnPaint(PaintEventArgs e) {
moel@158:       base.OnPaint(e);
moel@158: 
moel@158:       Graphics g = e.Graphics;
moel@158:       Rectangle r = SplitterRectangle;
moel@158:       using (SolidBrush brush = new SolidBrush(color))
moel@158:         g.FillRectangle(brush, r);
moel@158:       ControlPaint.DrawBorder3D(g, r, border3DStyle);
moel@158:     }
moel@158: 
moel@158:     protected override void OnKeyDown(KeyEventArgs e) {
moel@158:       if (!base.IsSplitterFixed) {
moel@158:         if (e.KeyData == Keys.Right || e.KeyData == Keys.Down) {
moel@158:           SplitterDistance += SplitterIncrement;
moel@158:         } else if (e.KeyData == Keys.Left || e.KeyData == Keys.Up) {
moel@158:           SplitterDistance -= SplitterIncrement;
moel@158:         }
moel@158:         Invalidate();
moel@158:       }
moel@158:     }
moel@158: 
moel@158:     protected override void OnMouseDown(MouseEventArgs e) {
moel@158:       if (Orientation == Orientation.Vertical) {
moel@158:         delta = this.SplitterDistance - e.X;
moel@158:         Cursor.Current = Cursors.VSplit;
moel@158:       } else {
moel@158:         delta = this.SplitterDistance - e.Y;
moel@158:         Cursor.Current = Cursors.HSplit;
moel@158:       }
moel@158:       base.IsSplitterFixed = true;
moel@158:     }
moel@158: 
moel@158:     protected override void OnMouseMove(MouseEventArgs e) {
moel@158:       if (base.IsSplitterFixed) {
moel@158:         if (e.Button == MouseButtons.Left) {
moel@158:           if (Orientation == Orientation.Vertical) {
moel@158:             if (e.X > 0 && e.X < Width) {
moel@158:               SplitterDistance = e.X + delta < 0 ? 0 : e.X + delta;
moel@158:             }
moel@158:           } else {
moel@158:             if (e.Y > 0 && e.Y < Height) {
moel@158:               SplitterDistance = e.Y + delta < 0 ? 0 : e.Y + delta;
moel@158:             }
moel@158:           }
moel@158:         } else {
moel@158:           base.IsSplitterFixed = false;
moel@158:         }
moel@158:         Invalidate();
moel@158:       } else {
moel@158:         if (SplitterRectangle.Contains(e.Location)) {
moel@158:           Cursor = Orientation == Orientation.Vertical ?
moel@158:             Cursors.VSplit : Cursors.HSplit;
moel@158:         }
moel@158:       }
moel@158:     }
moel@158: 
moel@158:     protected override void OnMouseLeave(EventArgs e) {
moel@158:       base.OnMouseLeave(e);
moel@158:       Cursor = Cursors.Default;
moel@158:     }
moel@158: 
moel@158:     protected override void OnMouseUp(MouseEventArgs e) {
moel@158:       delta = 0;
moel@158:       base.IsSplitterFixed = false;
moel@158:       Cursor.Current = Cursors.Default;
moel@158:     }
moel@158: 
moel@158:     public Border3DStyle Border3DStyle {
moel@158:       get { return border3DStyle; }
moel@158:       set {
moel@158:         border3DStyle = value;
moel@158:         Invalidate(false);
moel@158:       }
moel@158:     }
moel@158: 
moel@158:     public Color Color {
moel@158:       get { return color; }
moel@158:       set {
moel@158:         color = value;
moel@158:         Invalidate(false);
moel@158:       }
moel@158:     }
moel@158: 
moel@158:     public new bool IsSplitterFixed {
moel@158:       get {
moel@158:         return false;
moel@158:       }
moel@158:     }
moel@158: 
moel@158:   }
moel@158: }