Click here to Skip to main content
15,898,036 members
Articles / Desktop Programming / Windows Forms

Storm - the world's best IDE framework for .NET

Rate me:
Please Sign up or sign in to vote.
4.96/5 (82 votes)
4 Feb 2010LGPL311 min read 278.6K   6.5K   340  
Create fast, flexible, and extensible IDE applications easily with Storm - it takes nearly no code at all!
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;

using Storm;
using Storm.Docking;
using Storm.Docking.Controls;
using Storm.Docking.Visual;
using Storm.Docking.Visual.Drawing;

namespace Storm.Docking.Visual.Glyphs
{
    /// <summary>
    /// Represents a base class for all DockCaption glyphs to 
    /// inherit from.
    /// </summary>
    public class Glyph
        : Panel
    {
        #region Fields

        // Design members.
        private TransImage _image = null;

        // Basic members.
        private bool _isMouseOver = false;
        private bool _isMouseClicked = false;
        private Rectangle _glyphRect = Rectangle.Empty;
        private DockCaption _dockCaption = null;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets whether the cursor is over the Glyph.
        /// </summary>
        [Browsable(false)]
        [Description("Gets or sets whether the cursor is over the Glyph.")]
        public bool IsMouseOver
        {
            get { return this._isMouseOver; }
            set
            {
                this._isMouseOver = value;
                this.Invalidate();  // Call Invalidate to redraw.
            }
        }

        /// <summary>
        /// Gets or sets whether the mouse is clicked on the Glyph.
        /// </summary>
        [Browsable(false)]
        [Description("Gets or sets whether the mouse is clicked on the Glyph.")]
        public bool IsMouseClicked
        {
            get { return _isMouseClicked; }
            set
            {
                _isMouseClicked = value;
                this.Invalidate();  // Call Invalidate to redraw.
            }
        }

        /// <summary>
        /// Gets or sets the Rectangle Bounds of the Glyph.
        /// </summary>
        [Browsable(false)]
        [Description("Gets or sets the Rectangle Bounds of the Glyph.")]
        public new Rectangle Bounds
        {
            get { return this._glyphRect; }
            set { this._glyphRect = value; }
        }

        /// <summary>
        /// Gets or sets the DockCaption parent of the Glyph.
        /// </summary>
        [Browsable(true)]
        [Description("Gets or sets the DockCaption parent of the Glyph.")]
        public DockCaption DockCaption
        {
            get { return this._dockCaption; }
            set
            {
                this._dockCaption = value;
                this.UpdateGlyphDesign();
            }
        }

        /// <summary>
        /// Gets or sets the TransImage control of the Glyph.
        /// </summary>
        [Browsable(false)]
        [Description("Gets or sets the TransImage control of the Glyph.")]
        public TransImage Image
        {
            get { return _image; }
            set { _image = value; }
        }

        /// <summary>
        /// Gets the Glyph's hovering Bounds.
        /// </summary>
        [Browsable(false)]
        [Description("Gets the Glyph's hovering Bounds.")]
        public Rectangle GlyphRect
        { get { return _glyphRect; } }

        #endregion

        #region Methods

        #region Public

        /// <summary>
        /// Updates the design of the Glyph to match the DockCaption's design.
        /// </summary>
        public void UpdateGlyphDesign()
        {
            // Initialize the base settings.
            this.AutoSize = false;
            this.BackColor = Color.Transparent;
            this.Dock = DockStyle.Right;
            this.Visible = true;

            // Initialize our hovering Bounds..
            _glyphRect = new Rectangle(this.Location.X + 1,
                                       this.Location.Y + 2,
                                       14, 14);

            // Set the Bounds of the CloseGlyph.
            this.Width = this.GlyphRect.Width + 2;
            this.Height = this.GlyphRect.Height + 2;

            this.Image.Location = new Point(this.GlyphRect.Location.X,
                                             this.GlyphRect.Location.Y);
        }

        #endregion

        #region Private

        private void OnImageDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                this.IsMouseClicked = true;
        }

        private void OnImageUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                this.IsMouseClicked = false;
        }

        private void OnImageEnter(object sender, EventArgs e)
        { this.IsMouseOver = true; }

        private void OnImageLeave(object sender, EventArgs e)
        { this.IsMouseOver = false; }

        private void OnImageClick(object sender, EventArgs e)
        {
            this.DockCaption.Focus();
            this.OnClickAction();
        }

        #endregion

        #region Internal

        /// <summary>
        /// Draws the Glyph's border depending on the DockCapton's
        /// focus state and whether the mouse is hovering over 
        /// the Glyph.
        /// </summary>
        /// <param name="g">Graphics that the Glyph should use to draw itself.</param>
        internal void DrawGlyph(Graphics g)
        {
            if (this.IsMouseOver == true)
            {
                if (this.IsMouseClicked == true)
                    this.DockCaption.Renderer.GlyphDrawPressed(this, g);
                else
					this.DockCaption.Renderer.GlyphDrawHover(this, g);
            }
        }

        /// <summary>
        /// Made to be overridden by inheriters.
        /// </summary>
        internal virtual void OnClickAction()
        {
        }

        /// <summary>
        /// Made to be overridden by inheriters.
        /// </summary>
        internal virtual void OnParentRecieveFocus
            (object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Made to be overridden by inheriters.
        /// </summary>
        internal virtual void OnParentReleaseFocus
            (object sender, EventArgs e)
        {
        }

        #endregion

        #region Protected

        protected override void OnClick(EventArgs e)
        {
            this.DockCaption.Focus();
            this.OnClickAction();

            base.OnClick(e);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            this.IsMouseOver = true;
            base.OnMouseEnter(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            this.IsMouseOver = false;
            base.OnMouseLeave(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                this.IsMouseClicked = true;

            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
                this.IsMouseClicked = false;

            base.OnMouseUp(e);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            this.DrawGlyph(e.Graphics);
            base.OnPaint(e);
        }

        #endregion

        #endregion

        /// <summary>
        /// Initializes a new instance of Glyph.
        /// </summary>
        /// <param name="parent">DockCaption parent for the Glyph.</param>
        public Glyph(DockCaption parent)
        {
            // Prevent parent from being null.
            if (parent == null) throw new ArgumentNullException("parent");

            // Update our DockCaption parent to match what's
            // specified in the arguments.

            _dockCaption = parent;

            // Add the DockCaption's EventHandlers.
            this.DockCaption.RecieveFocus += OnParentRecieveFocus;
            this.DockCaption.ReleaseFocus += OnParentReleaseFocus;

            // Initialize the base TransImage.
            this.Image = new TransImage(null);
            this.Controls.Add(this.Image);

            // Update the Glyph's design.
            this.UpdateGlyphDesign();

            // EventHandlers.
            this.Image.MouseEnter += OnImageEnter;
            this.Image.MouseLeave += OnImageLeave;
            this.Image.MouseDown += OnImageDown;
            this.Image.MouseUp += OnImageUp;
            this.Image.Click += OnImageClick;

            // Set styles to prevent flickering.
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            // Should not be allowed to select this control.
            this.SetStyle(ControlStyles.Selectable, true);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)



Comments and Discussions