// 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.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace Spt.Share.Controls
{
    public class ctlSkinButton : PictureBox
    {
        public ctlSkinButton()
        {
            this.MouseEnter += new EventHandler(ImageButton_MouseEnter);
            this.MouseLeave += new EventHandler(ImageButton_MouseLeave);
            this.MouseDown += new MouseEventHandler(ImageButton_MouseDown);
            this.MouseUp += new MouseEventHandler(ImageButton_MouseUp);
            this.MouseClick += new MouseEventHandler(ImageButton_MouseClick);
            this.Paint += new PaintEventHandler(ImageButton_Paint);
            this.EnabledChanged += new System.EventHandler(ImageButton_EnabledChanged);

            if (m_normal != null)
            {
                this.Width = m_normal.Width;
                this.Height = m_normal.Height;
            }
        }

        void ImageButton_MouseClick(object sender, MouseEventArgs e)
        {
            if (Enabled)
            {
                if (PressButton && !PressedFlag)
                {
                    PressedFlag = !PressedFlag;
                    this.Image = (PressedFlag) ? m_pressed : m_normal;
                }                
            }
        }

        void ImageButton_Paint(object sender, PaintEventArgs e)
        {
            if (m_textFont != null)
            {
                SolidBrush b = new SolidBrush(m_textColor);
                StringFormat format = new StringFormat();

                format.Alignment = StringAlignment.Center;
                format.LineAlignment = StringAlignment.Center;

                e.Graphics.DrawString(m_caption, m_textFont, b, ClientRectangle, format);
            }
        }

        void ImageButton_MouseUp(object sender, MouseEventArgs e)
        {
            DoMouseUp();
        }

        public void DoMouseUp()
        {
            if (Enabled)
            {
                if (!PressButton)
                {
                    this.Image = m_hover;
                }
            }
        }

        void ImageButton_MouseDown(object sender, MouseEventArgs e)
        {
            DoMouseDown();
        }

        public void DoMouseDown()
        {
            if (Enabled)
            {
                if (!PressButton)
                {
                    this.Image = m_pressed;
                }
            }
        }

        void ImageButton_MouseLeave(object sender, EventArgs e)
        {
            DoMouseLeave();
        }

        public void DoMouseLeave()
        {
            if (Enabled)
            {
                if (!PressButton)
                {
                    this.Image = m_normal;
                }
            }
        }

        void ImageButton_MouseEnter(object sender, EventArgs e)
        {
            DoMouseEnter();
        }

        public void DoMouseEnter()
        {
            if (Enabled)
            {
                if (!PressButton)
                {
                    this.Image = m_hover;
                }
            }
        }

        private void ImageButton_EnabledChanged(object sender, EventArgs e)
        {
            if (Enabled)
            {
                if (!PressButton)
                {
                    Image = m_normal;
                }
                else
                {
                    Image = PressedFlag ? m_pressed : m_normal;
                }
            }
            else
            {
                Image = m_disable;
            }
        }

        private Image m_normal;

        public Image Normal
        {
            get
            {
                return m_normal; 
            }
            set 
            { 
                m_normal = value;

                if (Enabled)
                {
                    this.Image = value;

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

        private Image m_pressed;

        public Image Pressed
        {
            get { return m_pressed; }
            set { m_pressed = value; }
        }

        private Image m_hover;

        public Image Hover
        {
            get { return m_hover; }
            set { m_hover = value; }
        }

        private Image m_disable;

        public Image Disable
        {
            get { return m_disable; }
            set 
            { 
                m_disable = value; 

                if (!Enabled)
                {
                    Image = value;
                }
            }
        }
        
        private string m_caption;

        public string Caption
        {
            get { return m_caption; }
            set { m_caption = value; }
        }

        private Font m_textFont = null;

        public Font TextFont
        {
            get { return m_textFont; }
            set { m_textFont = value; }
        }

        private Color m_textColor = Color.Black;

        public Color TextColor
        {
            get { return m_textColor; }
            set { m_textColor = value; }
        }

        /// <summary>
        /// A flag indicating if the button can save its pressed state
        /// </summary>
        private bool m_pressButton = false;

        public bool PressButton
        {
            get { return m_pressButton; }
            set { m_pressButton = value; }
        }

        private bool m_pressedFlag = false;

        public bool PressedFlag
        {
            get { return m_pressedFlag; }
            set 
            {
                m_pressedFlag = value;

                if (Enabled)
                {
                    this.Image = (PressedFlag) ? m_pressed : m_normal;
                }
            }
        }

        private void InitializeComponent()
        {
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // ImageButton
            // 
            this.BackColor = System.Drawing.Color.Transparent;
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);

        }
    }
}
