namespace InstrumentationControls
{
	using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;

	[ToolboxBitmap(typeof(MultimediaButton))]
    public class MultimediaButton : MultimediaButtonBase
    {
        private Color faceColor = Color.White;
        private FlatStyle3D flatStyle = FlatStyle3D.Normal;
        private Color highlightColor = Color.Lavender;
        private int pushDepth = 2;
        private ShapeKinds shape = ShapeKinds.Ellipse;

        [Category("Property Changed"), Description("Occurs when the FaceColor property changed")]
        public event EventHandler FaceColorChanged;

        [Category("Property Changed"), Description("Occurs when the FlatStyle property changed")]
        public event EventHandler FlatStyleChanged;

        [Description("Occurs when the HighlightColor property changed"), Category("Property Changed")]
        public event EventHandler HighlightColorChanged;

        [Category("Property Changed"), Description("Occurs when the PushDepth property changed")]
        public event EventHandler PushDepthChanged;

        [Category("Property Changed"), Description("Occurs when the Shape property changed")]
        public event EventHandler ShapeChanged;

        public MultimediaButton()
        {
            base.SetStyle((System.Windows.Forms.ControlStyles)0x200, true);
        }

        protected override void Draw(Graphics g, Rectangle r)
        {
            base.Draw(g, r);
            Point center = this.Center;
            this.DrawFace(g, center);
        }

        private void DrawFace(Graphics g, Point p)
        {
            Color highlightColor;
            Rectangle workRectangle = this.WorkRectangle;
            workRectangle.Width--;
            workRectangle.Height--;
            GraphicsContainer container = g.BeginContainer();
            g.SmoothingMode = (SmoothingMode)4;
            Rectangle rect = workRectangle;
            rect.Inflate(-4, -4);
            if (base.Highlighted)
            {
                highlightColor = this.highlightColor;
            }
            else
            {
							highlightColor = !Grayed ? this.faceColor : Color.Gray;
            }
            if (((this.FlatStyle == FlatStyle3D.Normal) || base.Pressed) || base.Highlighted)
            {
                if (!base.Pressed)
                {
                    GraphicsUtils.FillShape3D(g, workRectangle, this.shape, highlightColor, false);
                    GraphicsUtils.FillShape(g, rect, this.shape, highlightColor);
                }
                else
                {
                    GraphicsUtils.FillShape(g, workRectangle, this.shape, Color.DimGray);
                    GraphicsPath path = ShapeGenerator.GenerateShape(this.shape, workRectangle);
                    g.SetClip(path);
                    workRectangle.X += this.pushDepth;
                    workRectangle.Y += this.pushDepth;
                    rect.X += this.pushDepth;
                    rect.Y += this.pushDepth;
                    GraphicsUtils.FillShape3D(g, workRectangle, this.shape, highlightColor, false);
                    GraphicsUtils.FillShape(g, rect, this.shape, highlightColor);
                }
            }
            g.EndContainer(container);
            base.DrawCaption(g, workRectangle);
            base.DrawSelect(g);
        }

        protected override void OnBackColorChanged(EventArgs e)
        {
            base.Invalidate();
            base.OnBackColorChanged(e);
        }

        protected override void OnBackgroundImageChanged(EventArgs e)
        {
            base.Invalidate();
            base.OnBackgroundImageChanged(e);
        }

        protected virtual void OnFaceColorChanged(EventArgs e)
        {
            if (this.FaceColorChanged != null)
            {
                this.FaceColorChanged(this, e);
            }
        }

        protected virtual void OnFlatStyleChanged(EventArgs e)
        {
            if (this.FlatStyleChanged != null)
            {
                this.FlatStyleChanged(this, e);
            }
        }

        protected override void OnForeColorChanged(EventArgs e)
        {
            base.Invalidate();
            base.OnForeColorChanged(e);
        }

        protected virtual void OnHighlightColorChanged(EventArgs e)
        {
            if (this.HighlightColorChanged != null)
            {
                this.HighlightColorChanged(this, e);
            }
        }

        protected virtual void OnPushDepthChanged(EventArgs e)
        {
            if (this.PushDepthChanged != null)
            {
                this.PushDepthChanged(this, e);
            }
        }

        protected virtual void OnShapeChanged(EventArgs e)
        {
            if (this.ShapeChanged != null)
            {
                this.ShapeChanged(this, e);
            }
        }

        private Point Center
        {
            get
            {
                Rectangle rectangle = this.DisplayRectangle;
                return new Point((rectangle.Left + rectangle.Right) / 2, (rectangle.Top + rectangle.Bottom) / 2);
            }
        }

        protected override Size DefaultSize
        {
            get
            {
                return new Size(0x24, 0x18);
            }
        }

        [DefaultValue(typeof(Color), "White"), Category("Appearance"), Description("Face button color")]
        public Color FaceColor
        {
            get
            {
                return this.faceColor;
            }
            set
            {
                if (this.faceColor != value)
                {
                    this.faceColor = value;
                    base.Invalidate();
                    this.OnFaceColorChanged(new EventArgs());
                }
            }
        }

        [DefaultValue(0), Category("Appearance"), Description("Width of button's border in pressed state")]
        public FlatStyle3D FlatStyle
        {
            get
            {
                return this.flatStyle;
            }
            set
            {
                if (this.flatStyle != value)
                {
                    this.flatStyle = value;
                    base.Invalidate();
                    this.OnFlatStyleChanged(new EventArgs());
                }
            }
        }

        [DefaultValue(typeof(Color), "Lavender"), Description("Highlight button color"), Category("Appearance")]
        public Color HighlightColor
        {
            get
            {
                return this.highlightColor;
            }
            set
            {
                if (this.highlightColor != value)
                {
                    this.highlightColor = value;
                    base.Invalidate();
                    this.OnHighlightColorChanged(new EventArgs());
                }
            }
        }

        [Category("Appearance"), Description("Width of button's border in pressed state"), DefaultValue(2)]
        public int PushDepth
        {
            get
            {
                return this.pushDepth;
            }
            set
            {
                if (this.pushDepth != value)
                {
                    this.pushDepth = value;
                    base.Invalidate();
                    this.OnPushDepthChanged(new EventArgs());
                }
            }
        }

        [Category("Appearance"), Description("Button shape"), DefaultValue(2)]
        public ShapeKinds Shape
        {
            get
            {
                return this.shape;
            }
            set
            {
                if (this.shape != value)
                {
                    this.shape = value;
                    base.Invalidate();
                    this.OnShapeChanged(new EventArgs());
                }
            }
        }
    }
}
