// ALL RIGHTS RESERVED:
// ====================
// The contents of this file, and associated files in this directory, are 
// Copyright (C) SPT , all rights reserved, 2009.
// All software Source Code, Images, Database-Design and code, Graphics Design 
// and source files, and related content (collectively referred to as SOURCE) are 
// Copyright (c) SPT, 2009, All Rights Reserved.
// http://SerialPortTool.com

// PTZ Controller is a Registered Trademark of SerialPortTool.com.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace Spt.Share.Controls
{
    public partial class ctlSkinButtonPair : UserControl
    {
        private Image _normalImage;

        public Image NormalImage
        {
            get { return _normalImage; }
            set 
            { 
                _normalImage = value;

                this.BackgroundImage = value;

                if (value != null)
                {
                    this.Size = value.Size;
                }
            }
        }

        private Image _hoverLeftImage;

        public Image HoverLeftImage
        {
            get { return _hoverLeftImage; }
            set { _hoverLeftImage = value; }
        }

        private Image _hoverRightImage;

        public Image HoverRightImage
        {
            get { return _hoverRightImage; }
            set { _hoverRightImage = value; }
        }

        private Image _pressedLeftImage;

        public Image PressedLeftImage
        {
            get { return _pressedLeftImage; }
            set { _pressedLeftImage = value; }
        }

        private Image _pressedRightImage;

        public Image PressedRightImage
        {
            get { return _pressedRightImage; }
            set { _pressedRightImage = value; }
        }

        [Browsable(true)]
        public event EventHandler LeftButtonDown;

        [Browsable(true)]
        public event EventHandler RightButtonDown;

        [Browsable(true)]
        public event EventHandler ButtonUp;

        public ctlSkinButtonPair()
        {
            InitializeComponent();
        }

        private void ctlSkinButtonPair_MouseHover(object sender, EventArgs e)
        {
            CheckHover();
        }

        private void CheckHover()
        {
            Point pt = Cursor.Position;

            pt = this.PointToClient(pt);

            if (pt.X < this.ClientRectangle.Width / 2)
            {
                if (this.BackgroundImage != _hoverLeftImage)
                {
                    this.BackgroundImage = _hoverLeftImage;
                }
            }
            else
            {
                if (this.BackgroundImage != _hoverRightImage)
                {
                    this.BackgroundImage = _hoverRightImage;
                }
            }
        }

        private void CheckPressed()
        {
            Point pt = Cursor.Position;

            pt = this.PointToClient(pt);

            if (pt.X < this.ClientRectangle.Width / 2)
            {
                if (this.BackgroundImage != _pressedLeftImage)
                {
                    this.BackgroundImage = _pressedLeftImage;

                    if (LeftButtonDown != null)
                    {
                        LeftButtonDown(this, new EventArgs());
                    }
                }
            }
            else
            {
                if (this.BackgroundImage != _pressedRightImage)
                {
                    this.BackgroundImage = _pressedRightImage;

                    if (RightButtonDown != null)
                    {
                        RightButtonDown(this, new EventArgs());
                    }
                }
            }
        }

        private void ctlSkinButtonPair_MouseLeave(object sender, EventArgs e)
        {
            this.BackgroundImage = _normalImage;
        }

        private void ctlSkinButtonPair_MouseDown(object sender, MouseEventArgs e)
        {
            CheckPressed();
        }

        private void ctlSkinButtonPair_MouseUp(object sender, MouseEventArgs e)
        {
            CheckHover();

            if (ButtonUp != null)
            {
                ButtonUp(this, new EventArgs());
            }
        }

        private void ctlSkinButtonPair_MouseEnter(object sender, EventArgs e)
        {
            CheckHover();
        }

        private void ctlSkinButtonPair_MouseMove(object sender, MouseEventArgs e)
        {
            CheckHover();
        }
    }
}
