Click here to Skip to main content
15,892,746 members
Articles / Desktop Programming / Windows Forms

GMarkupLabel - A C# Windows Forms control to display XML-formatted text

Rate me:
Please Sign up or sign in to vote.
4.95/5 (77 votes)
25 Nov 2008CPOL9 min read 132.1K   1.1K   166  
A framework, and a WinForms control which enables .NET 2.0 users to visualize XML-formatted rich text.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace GFramework.View
{
    /// <summary>
    /// Represents an abstraction for a visual representation of an element.
    /// Encapsulates common paint functionality.
    /// </summary>
    public abstract class GVisualElement : GElement
    {
        #region Constructor

        public GVisualElement()
        {
            m_Bounds = Rectangle.Empty;
        }

        #endregion

        #region Events

        public event GEventHandler Invalidated
        {
            add
            {
                Events.AddHandler(InvalidatedEventKey, value);
            }
            remove
            {
                Events.RemoveHandler(InvalidatedEventKey, value);
            }
        }

        #endregion

        #region Public Methods

        public void SuspendInvalidate()
        {
            m_InvalidateLock++;
        }
        public void ResumeInvalidate()
        {
            m_InvalidateLock--;
            m_InvalidateLock = Math.Max(0, m_InvalidateLock);
        }

        public void Invalidate()
        {
            InvalidateCore(new RectangleF[] { m_Bounds });
        }
        public void Invalidate(RectangleF[] rects)
        {
            InvalidateCore(rects);
        }
        public void SetBounds(RectangleF bounds)
        {
            SetBounds(bounds, false);
        }
        public void SetBounds(RectangleF bounds, bool invalidate)
        {
            bounds = GLayoutHelper.NormalizeRect(bounds);
            if (m_Bounds == bounds)
            {
                return;
            }

            RectangleF prevBounds = m_Bounds;
            SetBoundsCore(bounds);

            if (invalidate)
            {
                InvalidateCore(new RectangleF[] { prevBounds, bounds });
            }
        }

        #endregion

        #region Public overridables

        public virtual bool HitTest(PointF point)
        {
            return m_Bounds.Contains(point);
        }
        public virtual bool HitTest(RectangleF rect)
        {
            return m_Bounds.Contains(rect);
        }

        public virtual void Paint(GPaintContext context)
        {
            //we are not visible, do not proceed
            if (Visible == false)
            {
                return;
            }

            PrePaint(context);
            PaintContent(context);
            PostPaint(context);
        }

        #endregion

        #region Protected Overridables

        protected virtual void PrePaint(GPaintContext context)
        {
        }
        protected virtual void PaintContent(GPaintContext context)
        {
            PaintChildren(context);
        }
        protected virtual void PostPaint(GPaintContext context)
        {
        }
        protected virtual void PaintChildren(GPaintContext context)
        {
            //ask all children to paint themselves
            int count = m_Children.Count;
            GVisualElement child;

            for (int i = 0; i < count; i++)
            {
                child = m_Children[i] as GVisualElement;
                if (child != null)
                {
                    child.Paint(context);
                }
            }
        }

        protected virtual void SetBoundsCore(RectangleF bounds)
        {
            m_Bounds = bounds;
        }
        protected virtual void InvalidateCore(RectangleF[] rects)
        {
            if (m_InvalidateLock > 0)
            {
                return;
            }

            bool allEmpty = true;
            int length = rects.Length;
            for (int i = 0; i < length; i++)
            {
                if (rects[i].IsEmpty == false)
                {
                    allEmpty = false;
                    break;
                }
            }

            //all rects are empty, no need to proceed
            if (allEmpty)
            {
                return;
            }

            GInvalidatedEventData data = new GInvalidatedEventData(rects);
            GEventArgs args = new GEventArgs(this, data, InvalidatedEventKey, EventPropagation.None);

            RaiseEvent(args);
        }

        #endregion

        #region Public Overrides

        public override object GetDefaultPropertyValue(int propertyKey)
        {
            switch (propertyKey)
            {
                case VisiblePropertyKey:
                    return true;
            }

            return base.GetDefaultPropertyValue(propertyKey);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Determines whether the element is visible (may be visualized on a device context).
        /// </summary>
        public virtual bool Visible
        {
            get
            {
                //we are parented by a visual element, which is not visible
                GVisualElement parent = (GVisualElement)m_Parent;
                if (parent != null && parent.Visible == false)
                {
                    return false;
                }

                return (bool)GetPropertyValue(VisiblePropertyKey);
            }
            set
            {
                if (Visible == value)
                {
                    return;
                }

                SetPropertyValue(VisiblePropertyKey, value);
            }
        }
        /// <summary>
        /// Gets the smallest rectangle that encapsulates this visual.
        /// </summary>
        public RectangleF Bounds
        {
            get
            {
                return m_Bounds;
            }
        }

        #endregion

        #region Fields

        internal RectangleF m_Bounds;
        internal int m_InvalidateLock;

        #endregion

        #region Property Constants

        public const int VisiblePropertyKey = 1;

        #endregion

        #region Event Constants

        public const int InvalidatedEventKey = GElement.GElementLastEventKey + 1;

        internal const int GVisualElementLastEventKey = InvalidatedEventKey + DefaultEventRange;

        #endregion
    }
}

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 Code Project Open License (CPOL)


Written By
Team Leader Telerik Corp.
Bulgaria Bulgaria
.NET & C# addicted. Win8 & WinRT enthusiast and researcher @Telerik.

Comments and Discussions