Click here to Skip to main content
15,885,201 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 130.8K   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.Windows.Forms;
using System.Xml;
using System.Diagnostics;

namespace GFramework.Model.Text
{
    /// <summary>
    /// Represents a paragraph in the text.
    /// </summary>
    public class GParagraphElement : GTextElement
    {
        #region Constructor

        public GParagraphElement()
        {
        }

        #endregion

        #region Public Overrides

        public override object GetDefaultPropertyValue(int propertyKey)
        {
            switch (propertyKey)
            {
                case PaddingPropertyKey:
                    return Padding.Empty;
                case WrapPropertyKey:
                    return TextWrap.Word;
                case AlignPropertyKey:
                    return ParagraphAlign.Left;
            }

            return base.GetDefaultPropertyValue(propertyKey);
        }

        #endregion

        #region Protected Overrides

        protected override void ParseAttribute(XmlAttribute attribute)
        {
            switch (attribute.Name.ToLower())
            {
                case PaddingAttributeName:
                    string padding = attribute.Value.Trim();
                    string[] paddingValues = padding.Split(',');

                    Padding newPadding = Padding.Empty;

                    try
                    {
                        //if length is 1 then it indicates all values length
                        //otherwise it is in format (left, top, right, bottom)
                        if (paddingValues.Length == 1)
                        {
                            newPadding = new Padding(int.Parse(paddingValues[0]));
                        }
                        else if(paddingValues.Length == 4)
                        {
                            int left = int.Parse(paddingValues[0]);
                            int top = int.Parse(paddingValues[1]);
                            int right = int.Parse(paddingValues[2]);
                            int bottom = int.Parse(paddingValues[3]);

                            newPadding = new Padding(left, top, right, bottom);
                        }
                    }
                    catch
                    {
                        Debug.WriteLine("Failed to parse padding");
                    }

                    Padding = newPadding;
                    return;
                case WrapAttributeName:
                    try
                    {
                        Wrap = (TextWrap)Enum.Parse(typeof(TextWrap), attribute.Value, true);
                    }
                    catch
                    {
                        Debug.WriteLine("Failed to parse wrap");
                    }
                    return;
                case AlignAttributeName:
                    try
                    {
                        Align = (ParagraphAlign)Enum.Parse(typeof(ParagraphAlign), attribute.Value, true);
                    }
                    catch
                    {
                        Debug.WriteLine("Failed to parse align");
                    }
                    return;
            }

            base.ParseAttribute(attribute);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Name of the mark-up attribute.
        /// </summary>
        public override string TagName
        {
            get
            {
                return GTextDocument.ParagraphNodeName;
            }
        }
        public override bool IsContainer
        {
            get
            {
                return true;
            }
        }
        /// <summary>
        /// Gets or sets the internal padding of the paragraph.
        /// </summary>
        public Padding Padding
        {
            get
            {
                return (Padding)GetPropertyValue(PaddingPropertyKey);
            }
            set
            {
                if (Padding == value)
                {
                    return;
                }

                SetPropertyValue(PaddingPropertyKey, value);
            }
        }
        /// <summary>
        /// Gets or sets the text wrapping for the paragraph.
        /// </summary>
        public TextWrap Wrap
        {
            get
            {
                return (TextWrap)GetPropertyValue(WrapPropertyKey);
            }
            set
            {
                if (Wrap == value)
                {
                    return;
                }

                SetPropertyValue(WrapPropertyKey, value);
            }
        }
        /// <summary>
        /// Gets or sets the text align in this paragraph.
        /// </summary>
        public ParagraphAlign Align
        {
            get
            {
                return (ParagraphAlign)GetPropertyValue(AlignPropertyKey);
            }
            set
            {
                if (Align == value)
                {
                    return;
                }

                SetPropertyValue(AlignPropertyKey, value);
            }
        }

        #endregion

        #region Property Constants

        public const int PaddingPropertyKey = 1;
        public const int WrapPropertyKey = PaddingPropertyKey + 1;
        public const int AlignPropertyKey = WrapPropertyKey + 1;

        #endregion

        #region Static

        public const string PaddingAttributeName = "padding";
        public const string WrapAttributeName = "wrap";
        public const string AlignAttributeName = "align";

        #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