using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace DockableUI
{
    public partial class ResizeSplitter : UserControl
    {
        public ResizeSplitter()
        {
            InitializeComponent();
            this.BackColor = SystemColors.ControlDark;
            base.Dock = DockStyle.Right;
        }

        #region Properties

        [DefaultValue(typeof(Color), "ControlDark")]
        public override Color BackColor
        {
            get { return base.BackColor; }
            set { base.BackColor = value; }
        }

        //private DockStyle dock = DockStyle.Right;
        [DefaultValue(typeof(DockStyle), "Right")]
        public new DockStyle Dock
        {
            get { return base.Dock; }
            set
            {
                if (value != base.Dock)
                {
                    //this.Parent.Resize += new EventHandler(Parent_Resize);
                    //base.Dock = value;
                    switch (value)
                    {
                        case DockStyle.Bottom:
                        case DockStyle.Top:
                            this.Cursor = Cursors.HSplit;
                            base.Dock = value;
                            base.Height = 4;
                            break;
                        case DockStyle.Left:
                        case DockStyle.Right:
                            this.Cursor = Cursors.VSplit;
                            base.Dock = value;
                            base.Width = 4;
                            break;
                        default:
                            throw new ArgumentOutOfRangeException("Dock cannot be Fill or None.");
                    }
                }
            }
        }

        #endregion

        #region Splitter moving

        private Point mousedownposition;
        private Size originalsize;

        private void ResizeSplitter_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                mousedownposition = Cursor.Position;
                originalsize = Parent.Size;
            }
        }

        private void ResizeSplitter_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left && Cursor.Position != mousedownposition)
            {
                switch (this.Dock)
                {
                    case DockStyle.Bottom:
                        Parent.Height = originalsize.Height + Cursor.Position.Y - mousedownposition.Y;
                        break;
                    case DockStyle.Top:
                        Parent.Height = originalsize.Height - Cursor.Position.Y + mousedownposition.Y;
                        break;
                    case DockStyle.Right:
                        Parent.Width = originalsize.Width + Cursor.Position.X - mousedownposition.X;
                        break;
                    case DockStyle.Left:
                        Parent.Width = originalsize.Width - Cursor.Position.X + mousedownposition.X;
                        break;
                    default:
                        break;
                }
            }
        }

        #endregion
    }
}
