// 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 ctlSkinSlider : UserControl
    {
        private bool _mouseDown = false;
        private Point _downPoint = new Point();

        private int _minValue = 0;

        public int MinValue
        {
            get { return _minValue; }
            set { _minValue = value; }
        }

        private int _maxValue = 100;

        public int MaxValue
        {
            get { return _maxValue; }
            set { _maxValue = value; }
        }

        private int _value;

        public int Value
        {
            get { return _value; }
            set 
            {
                if (_value != value)
                {
                    _value = value;

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

                int x = (this.ClientSize.Width - _slider.Width) *
                    (value - MinValue) / (MaxValue - MinValue);

                _slider.Left = x;
            }
        }
	

        public Image SliderImage
        {
            get
            {
                return _slider.Image;
            }
            set
            {
                _slider.Image = value;

                if (value != null)
                {
                    this.Height = value.Height + 2;
                    _slider.Top = 0;
                    _slider.Height = value.Height;
                    _slider.Width = value.Width;
                }
            }
        }

        private EventHandler _valueChanged = null;

        public EventHandler ValueChanged
        {
            get { return _valueChanged; }
            set { _valueChanged = value; }
        }
	

        public ctlSkinSlider()
        {
            InitializeComponent();
        }

        private void _slider_MouseDown(object sender, MouseEventArgs e)
        {
            if (Enabled)
            {
                _mouseDown = true;

                Point lt = PointToScreen(new Point(0, 0));
                Point pt = Cursor.Position;

                _downPoint.X = pt.X - lt.X;
            }
        }

        private void _slider_MouseMove(object sender, MouseEventArgs e)
        {
            if (_mouseDown)
            {
                Point pt = Cursor.Position;
                pt = ChangePosition(pt);
            }
        }

        private Point ChangePosition(Point pt)
        {
            Point lt = PointToScreen(new Point(0, 0));

            pt.X -= _slider.Width / 2;

            int x = pt.X - lt.X;

            if (x >= 0 && x <= this.ClientSize.Width - _slider.Width)
            {
                _slider.Left = x;

                int v = MinValue + x * (MaxValue - MinValue) / (this.ClientSize.Width - _slider.Width);

                if (v != Value)
                {
                    Value = v;

                    if (_valueChanged != null)
                    {
                        ValueChanged(this, new EventArgs());
                    }
                }
            }
            return pt;
        }

        private void _slider_MouseUp(object sender, MouseEventArgs e)
        {
            if (Enabled)
            {
                _mouseDown = false;
            }
        }

        private void ctlSkinSlider_MouseLeave(object sender, EventArgs e)
        {
            if (Enabled)
            {
                _mouseDown = false;
            }
        }

        private void ctlSkinSlider_MouseDown(object sender, MouseEventArgs e)
        {
            Point pt = Cursor.Position;
            ChangePosition(pt);
        }
    }
}
