Click here to Skip to main content
15,898,134 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.6K   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.ComponentModel;
using System.Collections;
using System.Runtime.Serialization;

namespace GFramework
{
    public abstract class GObject
    {
        #region Constructor

        public GObject()
        {
            m_PropertyStorage = new GPropertyStorage();
            m_BitState = new GBitVector64();
            m_BitState[StateCanRaiseEvents] = true;
        }

        #endregion

        #region Public Events

        public event GEventHandler PropertyChanging
        {
            add
            {
                Events.AddHandler(PropertyChangingEventKey, value);
            }
            remove
            {
                Events.RemoveHandler(PropertyChangingEventKey, value);
            }
        }
        public event GEventHandler PropertyChanged
        {
            add
            {
                Events.AddHandler(PropertyChangedEventKey, value);
            }
            remove
            {
                Events.RemoveHandler(PropertyChangedEventKey, value);
            }
        }

        #endregion

        #region Public Methods

        public object GetPropertyValue(int propertyKey)
        {
            bool found;
            object value = m_PropertyStorage.GetEntry(propertyKey, out found);

            if (found)
            {
                return value;
            }

            return GetDefaultPropertyValue(propertyKey);
        }
        public object GetLocalPropertyValue(int propertyKey)
        {
            bool found;
            object value = m_PropertyStorage.GetEntry(propertyKey, out found);

            if (found)
            {
                return value;
            }

            return null;
        }
        public bool ContainsLocalProperty(int propertyKey)
        {
            return GetLocalPropertyValue(propertyKey) != null;
        }
        public void ResetPropertyValue(int propertyKey)
        {
            if (ContainsLocalProperty(propertyKey) == false)
            {
                return;
            }

            SetPropertyValue(propertyKey, null);
        }
        public void SuspendEvents()
        {
            m_EventLock++;
        }
        public void ResumeEvents()
        {
            m_EventLock--;
            m_EventLock = Math.Max(0, m_EventLock);
        }

        #endregion

        #region Public Overridables

        public virtual bool GetCanRaiseEvents()
        {
            if (m_BitState[StateCanRaiseEvents] == false)
            {
                return false;
            }

            if (m_EventLock > 0)
            {
                return false;
            }

            return true;
        }
        public virtual object GetDefaultPropertyValue(int propertyKey)
        {
            return null;
        }

        #endregion

        #region Protected Overridables

        [OnDeserialized()]
        protected virtual void OnDeserialized()
        {
            m_PropertyStorage = new GPropertyStorage();
            m_BitState[StateCanRaiseEvents] = true;
        }
        protected virtual void RaiseEvent(GEventArgs args)
        {
            if (m_BitState[StateCanRaiseEvents] == false || m_EventLock > 0)
            {
                return;
            }

            GEventHandler handler = Events[args.m_Key] as GEventHandler;
            if (handler != null)
            {
                handler(args);
            }
        }
        protected virtual EventResult OnPropertyValueChanging(int propertyKey, object newValue)
        {
            if (GetCanRaiseEvents() == false)
            {
                return EventResult.Unspecified;
            }

            GPropertyChangingEventData data = new GPropertyChangingEventData(propertyKey, newValue);
            GEventArgs e = new GEventArgs(this, data, PropertyChangingEventKey, EventPropagation.Bubble);

            RaiseEvent(e);

            return e.m_Result;
        }
        protected virtual void OnPropertyValueChanged(int propertyKey)
        {
            if (GetCanRaiseEvents() == false)
            {
                return;
            }

            GPropertyEventData data = new GPropertyEventData(propertyKey);
            GEventArgs e = new GEventArgs(this, data, PropertyChangedEventKey, EventPropagation.Both);

            RaiseEvent(e);
        }
        protected virtual void SetPropertyValue(int propertyKey, object value)
        {
            EventResult result = OnPropertyValueChanging(propertyKey, value);
            if ((result & EventResult.Cancel) == EventResult.Cancel)
            {
                return;
            }

            if (value == null)
            {
                if (m_PropertyStorage.ContainsEntry(propertyKey))
                {
                    m_PropertyStorage.RemoveEntry(propertyKey);
                }
            }
            else
            {
                m_PropertyStorage.SetEntry(propertyKey, value);
            }

            OnPropertyValueChanged(propertyKey);
        }

        #endregion

        #region Protected Properties

        protected GEventStorage Events
        {
            get
            {
                if (m_Events == null)
                {
                    m_Events = new GEventStorage();
                }

                return m_Events;
            }
        }
        protected GPropertyStorage PropertyStorage
        {
            get
            {
                return m_PropertyStorage;
            }
        }

        #endregion

        #region Properties

        /// <summary>
        /// Determines whether the object is allowed to raise events.
        /// </summary>
        [DefaultValue(true)]
        public bool CanRaiseEvents
        {
            get
            {
                return m_BitState[StateCanRaiseEvents];
            }
            set
            {
                m_BitState[StateCanRaiseEvents] = value;
            }
        }
        /// <summary>
        /// Gets or sets the additional data associated with the object.
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object Tag
        {
            get
            {
                return m_Tag;
            }
            set
            {
                m_Tag = value;
            }
        }

        #endregion

        #region Fields

        internal GPropertyStorage m_PropertyStorage;

        [NonSerialized]
        internal object m_Tag;
        [NonSerialized]
        internal int m_EventLock;
        [NonSerialized]
        internal GBitVector64 m_BitState;
        [NonSerialized]
        internal GEventStorage m_Events;

        #endregion

        #region BitState Constants

        internal const ulong StateCanRaiseEvents = 1;

        #endregion

        #region Event Constants

        public const int PropertyChangingEventKey = 1;
        public const int PropertyChangedEventKey = PropertyChangingEventKey + 1;

        internal const int DefaultEventRange = 100;
        internal const int GObjectLastEventKey = 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