Click here to Skip to main content
15,896,606 members
Articles / Desktop Programming / Win32

IlPad

Rate me:
Please Sign up or sign in to vote.
4.68/5 (13 votes)
21 Sep 2012CPOL2 min read 29.2K   772   15  
A program to compile C# code into MSIL code
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace IlPad
{
    [System.ComponentModel.DefaultProperty("ParentRichTextBox")]
    public class LineNumbers_For_RichTextBox : Control
    {
        private class LineNumberItem
        {
            private int LineNumber;
            private Rectangle _rectangle;

            public Rectangle GetRectangle
            {
                get { return _rectangle; }
            }

            public int SetRectangleHeight
            {
                set { _rectangle.Height = value; }
            }

            public int SetRectangleX
            {
                set { _rectangle.X = value; }
            }

            public int SetRectangleY
            {
                set { _rectangle.Y = value; }
            }

            public int GetLineNumber
            {
                get { return LineNumber; }
            }

            public LineNumberItem(int zLineNumber, Rectangle zRectangle)
            {
                LineNumber = zLineNumber;
                _rectangle = zRectangle;
            }
        }

        public enum LineNumberDockSide
        {
            None = 0,
            Left = 1,
            Right = 2,
            Height = 4
        }

        private RichTextBox zParent = null;
        private Timer zTimer = new Timer();

        private bool zAutoSizing = true;
        private Size zAutoSizing_Size = new Size(0, 0);
        private Rectangle zContentRectangle;
        private LineNumberDockSide zDockSide = LineNumberDockSide.Left;
        private bool zParentIsScrolling = false;
        private bool zSeeThroughMode = false;

        private bool zGradient_Show = true;
        private System.Drawing.Drawing2D.LinearGradientMode zGradient_Direction = System.Drawing.Drawing2D.LinearGradientMode.Horizontal;

        private Color zGradient_StartColor = Color.FromArgb(0, 0, 0, 0);
        private Color zGradient_EndColor = Color.LightSteelBlue;

        private bool zGridLines_Show = true;
        private Single zGridLines_Thickness = 1;
        private System.Drawing.Drawing2D.DashStyle zGridLines_Style = System.Drawing.Drawing2D.DashStyle.Dot;
        private Color zGridLines_Color = Color.SlateGray;

        private bool zBorderLines_Show = true;
        private Single zBorderLines_Thickness = 1;
        private System.Drawing.Drawing2D.DashStyle zBorderLines_Style = System.Drawing.Drawing2D.DashStyle.Dot;
        private Color zBorderLines_Color = Color.SlateGray;

        private bool zMarginLines_Show = true;
        private LineNumberDockSide zMarginLines_Side = LineNumberDockSide.Right;
        private Single zMarginLines_Thickness = 1;
        private System.Drawing.Drawing2D.DashStyle zMarginLines_Style = System.Drawing.Drawing2D.DashStyle.Dot;
        private Color zMarginLines_Color = Color.SlateGray;

        private bool zLineNumbers_Show = true;
        private bool zLineNumbers_ShowLeadingZeroes = true;
        private bool zLineNumbers_ShowAsHexadecimal = false;
        private bool zLineNumbers_ClipByItemRectangle = true;
        private Size zLineNumbers_Offset = new Size(0, 0);
        private string zLineNumbers_Format = "0";

        private System.Drawing.ContentAlignment zLineNumbers_Alignment = ContentAlignment.TopRight;
        private bool zLineNumbers_AntiAlias = true;

        private List<LineNumberItem> zLNIs = new List<LineNumberItem>();

        private Point zPointInParent = new Point(0, 0);
        private Point zPointInMe = new Point(0, 0);
        private int zParentInMe = 0;

        public LineNumbers_For_RichTextBox()
        {
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            Margin = new Padding(0);
            Padding = new Padding(0, 0, 2, 0);

            zTimer.Tick += new EventHandler(zTimer_Tick);
            zTimer.Enabled = true;
            zTimer.Interval = 200;
            zTimer.Stop();

            Update_SizeAndPosition();
            Invalidate();
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            AutoSize = false;
        }

        [System.ComponentModel.Browsable(false)]
        public override bool AutoSize
        {
            get
            {
                return base.AutoSize;
            }
            set
            {
                base.AutoSize = value;
                Invalidate();
            }
        }

        [System.ComponentModel.Description("Use this property to automatically resize the control (and reposition it if needed).")]
        [System.ComponentModel.Category("Additional Behavior")]
        public bool AutoSizing
        {
            get { return zAutoSizing; }
            set
            {
                zAutoSizing = value;
                this.Refresh();
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("Use this property to enable LineNumbers for the chosen RichTextBox.")]
        [System.ComponentModel.Category("Add LineNumbers to")]
        public RichTextBox ParentRichTextBox
        {
            get { return zParent; }
            set
            {
                zParent = value;
                if (zParent != null)
                {
                    this.Parent = zParent.Parent;
                    zParent.LocationChanged += new EventHandler(zParent_Changed);
                    zParent.Move += new EventHandler(zParent_Changed);
                    zParent.Resize += new EventHandler(zParent_Changed);
                    zParent.DockChanged += new EventHandler(zParent_Changed);
                    zParent.TextChanged += new EventHandler(zParent_Changed);
                    zParent.MultilineChanged += new EventHandler(zParent_Changed);

                    zParent.HScroll += new EventHandler(zParent_Scroll);
                    zParent.VScroll += new EventHandler(zParent_Scroll);

                    zParent.ContentsResized += new ContentsResizedEventHandler(zParent_ContentsResized);

                    zParent.Disposed += new EventHandler(zParent_Disposed);
                    zParent.Refresh();
                }
                this.Text = "";
                this.Refresh();
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("Use this property to dock the LineNumbers to a chosen side of the chosen RichTextBox.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public LineNumberDockSide DockSide
        {
            get { return zDockSide; }
            set
            {
                zDockSide = value;
                this.Refresh();
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("Use this property to enable the control to act as an overlay on top of the RichTextBox.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public bool _SeeThroughMode_
        {
            get { return zSeeThroughMode; }
            set
            {
                zSeeThroughMode = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("BorderLines are shown on all sides of the LineNumber control.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public bool Show_BorderLines
        {
            get { return zBorderLines_Show; }
            set
            {
                zBorderLines_Show = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public Color BorderLines_Color
        {
            get { return zBorderLines_Color; }
            set
            {
                zBorderLines_Color = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public Single BorderLines_Thickness
        {
            get { return zBorderLines_Thickness; }
            set
            {
                zBorderLines_Thickness = Math.Max(1, Math.Min(255, value));
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public System.Drawing.Drawing2D.DashStyle BorderLines_Style
        {
            get { return zBorderLines_Style; }
            set
            {
                if (value == System.Drawing.Drawing2D.DashStyle.Custom)
                    value = System.Drawing.Drawing2D.DashStyle.Solid;
                zBorderLines_Style = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("GridLines are the horizontal divider-lines shown above each LineNumber.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public bool Show_GridLines
        {
            get { return zGridLines_Show; }
            set
            {
                zGridLines_Show = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public Color GridLines_Color
        {
            get { return zGridLines_Color; }
            set
            {
                zGridLines_Color = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public Single GridLines_Thickness
        {
            get { return zGridLines_Thickness; }
            set
            {
                zGridLines_Thickness = Math.Max(1, Math.Min(255, value));
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public System.Drawing.Drawing2D.DashStyle GridLines_Style
        {
            get { return zGridLines_Style; }
            set
            {
                if (value == System.Drawing.Drawing2D.DashStyle.Custom)
                    value = System.Drawing.Drawing2D.DashStyle.Solid;
                zGridLines_Style = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("MarginLines are shown on the Left or Right (or both in Height-mode) of the LineNumber control.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public bool Show_MarginLines
        {
            get { return zMarginLines_Show; }
            set
            {
                zMarginLines_Show = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public LineNumberDockSide MarginLines_Side
        {
            get { return zMarginLines_Side; }
            set
            {
                zMarginLines_Side = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public Color MarginLines_Color
        {
            get { return zMarginLines_Color; }
            set
            {
                zMarginLines_Color = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public Single MarginLines_Thickness
        {
            get { return zMarginLines_Thickness; }
            set
            {
                zMarginLines_Thickness = Math.Max(1, Math.Min(255, value));
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public System.Drawing.Drawing2D.DashStyle MarginLines_Style
        {
            get { return zMarginLines_Style; }
            set
            {
                if (value == System.Drawing.Drawing2D.DashStyle.Custom)
                    value = System.Drawing.Drawing2D.DashStyle.Solid;
                zMarginLines_Style = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("The BackgroundGradient is a gradual blend of two colors, shown in the back of each LineNumber's item-area.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public bool Show_BackgroundGradient
        {
            get { return zGradient_Show; }
            set
            {
                zGradient_Show = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public Color BackgroundGradient_AlphaColor
        {
            get { return zGradient_StartColor; }
            set
            {
                zGradient_StartColor = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public Color BackgroundGradient_BetaColor
        {
            get { return zGradient_EndColor; }
            set
            {
                zGradient_EndColor = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Category("Additional Appearance")]
        public System.Drawing.Drawing2D.LinearGradientMode BackgroundGradient_Direction
        {
            get { return zGradient_Direction; }
            set
            {
                zGradient_Direction = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("Use this to set whether the LineNumbers are allowed to spill out of their item-area, or should be clipped by it.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public bool LineNrs_ClippedByItemRectangle
        {
            get { return zLineNumbers_ClipByItemRectangle; }
            set
            {
                zLineNumbers_ClipByItemRectangle = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("Use this to set whether the LineNumbers should have leading zeroes (based on the total amount of textlines).")]
        [System.ComponentModel.Category("Additional Behavior")]
        public bool LineNrs_LeadingZeroes
        {
            get { return zLineNumbers_ShowLeadingZeroes; }
            set
            {
                zLineNumbers_ShowLeadingZeroes = value;
                this.Refresh();
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("Use this to set whether the LineNumbers should be shown as hexadecimal values.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public bool LineNrs_AsHexadecimal
        {
            get { return zLineNumbers_ShowAsHexadecimal; }
            set
            {
                zLineNumbers_ShowAsHexadecimal = value;
                this.Refresh();
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("Use this property to manually reposition the LineNumbers, relative to their current location.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public Size LineNrs_Offset
        {
            get { return zLineNumbers_Offset; }
            set
            {
                zLineNumbers_Offset = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("Use this to align the LineNumbers to a chosen corner (or center) within their item-area.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public System.Drawing.ContentAlignment LineNrs_Alignment
        {
            get { return zLineNumbers_Alignment; }
            set
            {
                zLineNumbers_Alignment = value;
                this.Invalidate();
            }
        }

        [System.ComponentModel.Description("Use this to apply Anti-Aliasing to the LineNumbers (high quality). Some fonts will look better without it, though.")]
        [System.ComponentModel.Category("Additional Behavior")]
        public bool LineNrs_AntiAlias
        {
            get { return zLineNumbers_AntiAlias; }
            set
            {
                zLineNumbers_AntiAlias = value;
                this.Refresh();
                this.Invalidate();
            }
        }

        [System.ComponentModel.Browsable(true)]
        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                this.Refresh();
                this.Invalidate();
            }
        }

        [System.ComponentModel.DefaultValue("")]
        [System.ComponentModel.AmbientValue("")]
        [System.ComponentModel.Browsable(false)]
        public override string Text
        {
            get
            {
                return base.Text;
            }
            set
            {
                base.Text = "";
                this.Invalidate();
            }
        }

        protected override void OnSizeChanged(EventArgs e)
        {
            if (this.DesignMode)
                this.Refresh();
            base.OnSizeChanged(e);
            this.Invalidate();
        }

        protected override void OnLocationChanged(EventArgs e)
        {
            if (this.DesignMode)
                this.Refresh();
            base.OnLocationChanged(e);
            this.Invalidate();
        }

        public override void Refresh()
        {
            base.Refresh();
            this.Update_SizeAndPosition();
        }

        private void Update_SizeAndPosition()
        {
            if (this.AutoSize)
                return;

            if (this.Dock == DockStyle.Bottom || this.Dock == DockStyle.Fill || this.Dock == DockStyle.Top)
                return;

            Point zNewLocation = this.Location;
            Size zNewSize = this.Size;

            if (zAutoSizing)
            {
                if (zParent == null)
                {
                    if (zAutoSizing_Size.Width > 0)
                        zNewSize.Width = zAutoSizing_Size.Width;
                    if (zAutoSizing_Size.Height > 0)
                        zNewSize.Height = zAutoSizing_Size.Height;
                    this.Size = zNewSize;
                }
                else
                    if (this.Dock == DockStyle.Left || this.Dock == DockStyle.Right)
                    {
                        if (zAutoSizing_Size.Width > 0)
                            zNewSize.Width = zAutoSizing_Size.Width;
                        this.Width = zNewSize.Width;
                    }
                    else
                        if (zDockSide != LineNumberDockSide.None)
                        {
                            if (zAutoSizing_Size.Width > 0)
                                zNewSize.Width = zAutoSizing_Size.Width;
                            if (zDockSide == LineNumberDockSide.Left)
                                zNewLocation.X = zParent.Left - zNewSize.Width - 1;
                            if (zDockSide == LineNumberDockSide.Right)
                                zNewLocation.X = zParent.Right + 1;
                            zNewLocation.Y = zParent.Top;
                            this.Location = zNewLocation;
                            this.Size = zNewSize;
                        }
                        else
                            if (zDockSide == LineNumberDockSide.None)
                            {
                                if (zAutoSizing_Size.Width > 0)
                                    zNewSize.Width = zAutoSizing_Size.Width;
                                this.Size = zNewSize;
                            }
            }
            else
            {
                if (zParent == null)
                {
                    if (zAutoSizing_Size.Width > 0)
                        zNewSize.Width = zAutoSizing_Size.Width;
                    if (zAutoSizing_Size.Height > 0)
                        zNewSize.Height = zAutoSizing_Size.Height;
                    this.Size = zNewSize;
                }
                else
                    if (zDockSide != LineNumberDockSide.None)
                    {
                        zNewSize.Height = zParent.Height;
                        if (zDockSide == LineNumberDockSide.Left)
                            zNewLocation.X = zParent.Left - zNewSize.Width - 1;
                        if (zDockSide == LineNumberDockSide.Right)
                            zNewLocation.X = zParent.Right + 1;
                        zNewLocation.Y = zParent.Top;
                        this.Location = zNewLocation;
                        this.Size = zNewSize;
                    }
            }
        }

        private void Update_VisibleLineNumberItems()
        {
            zLNIs.Clear();
            zAutoSizing_Size = new Size(0, 0);
            zLineNumbers_Format = "0";

            if (zAutoSizing)
                zAutoSizing_Size =
                    new Size(
                        TextRenderer.MeasureText(zLineNumbers_Format.Replace('0', 'W'),
                                                 this.Font).Width, 0);
            if (zParent == null || zParent.Text.Equals(string.Empty))
                return;

            zPointInParent = zParent.PointToScreen(zParent.ClientRectangle.Location);
            zPointInMe = this.PointToScreen(new Point(0, 0));
            zParentInMe = zPointInParent.Y - zPointInMe.Y + 1;

            zPointInParent = zParent.PointToClient(zPointInMe);

            string[] zSplit = zParent.Text.Split("\r\n".ToCharArray());

            if (zSplit.Length < 2)
            {
                Point zPoint = zParent.GetPositionFromCharIndex(0);
                zLNIs.Add(new LineNumberItem(1,
                                             new Rectangle(new Point(0, zPoint.Y - 1 + zParentInMe),
                                                           new Size(this.Width, zContentRectangle.Height - zPoint.Y))));
            }
            else
            {
                TimeSpan zTimeSpan = new TimeSpan(DateTime.Now.Ticks);
                Point zPoint = new Point(0, 0);
                int zStartIndex = 0;
                int zSplitStartLine = 0;
                int zA = zParent.Text.Length - 1;
                zPointInParent.Y = this.FindStartIndex(ref zStartIndex, ref zA, zPointInParent.Y);

                zStartIndex = Math.Max(0,
                                       Math.Min(zParent.Text.Length - 1,
                                                zParent.Text.Substring(0, zStartIndex).LastIndexOf('\n') + 1));
                zSplitStartLine = Math.Max(0,
                                           zParent.Text.Substring(0, zStartIndex).Split("\r\n".ToCharArray()).Length - 1);

                for (zA = zSplitStartLine; zA <= zSplit.Length - 1; zA++)
                {
                    zPoint = zParent.GetPositionFromCharIndex(zStartIndex);
                    zStartIndex += Math.Max(1, zSplit[zA].Length + 1);
                    if (zPoint.Y + zParentInMe > this.Height)
                        break;
                    zLNIs.Add(new LineNumberItem(zA + 1, new Rectangle(0, zPoint.Y - 1 + zParentInMe, this.Width, 1)));
                    if (zParentIsScrolling && DateTime.Now.Ticks > zTimeSpan.Ticks + 500000)
                    {
                        if (zLNIs.Count == 1)
                            zLNIs[0].SetRectangleY = 0;
                        zParentIsScrolling = false;
                        zTimer.Start();
                        break;
                    }
                }

                if (zLNIs.Count == 0)
                    return;

                if (zA < zSplit.Length)
                {
                    zPoint = zParent.GetPositionFromCharIndex(Math.Min(zStartIndex, zParent.Text.Length - 1));
                    zLNIs.Add(new LineNumberItem(-1, new Rectangle(0, zPoint.Y - 1 + zParentInMe, 0, 0)));
                }
                else
                {
                    zLNIs.Add(new LineNumberItem(-1, new Rectangle(0, zContentRectangle.Bottom, 0, 0)));
                }
                for (zA = 0; zA <= zLNIs.Count - 2; zA++)
                {
                    zLNIs[zA].SetRectangleHeight = Math.Max(1, zLNIs[zA + 1].GetRectangle.Y - zLNIs[zA].GetRectangle.Y);
                }
                zLNIs.RemoveAt(zLNIs.Count - 1);
                if (zLineNumbers_ShowAsHexadecimal)
                    zLineNumbers_Format = "".PadRight(zSplit.Length.ToString("X").Length, '0');
                else
                    zLineNumbers_Format = "".PadRight(zSplit.Length.ToString().Length, '0');
            }
            if (zAutoSizing)
                zAutoSizing_Size =
                    new Size(TextRenderer.MeasureText(zLineNumbers_Format.Replace('0', 'W'), this.Font).Width, 0);
        }

        private int FindStartIndex(ref int zMin, ref int zMax, int zTarget)
        {
            int answer;

            if ((zMax == zMin + 1) || (zMin == (zMax + zMin) / 2))
                return zTarget;
            answer = (zParent.GetPositionFromCharIndex((zMax + zMin) / 2).Y);

            if (answer == zTarget)
                zMin = (zMax + zMin) / 2;
            if (answer > zTarget)
            {
                zMax = (zMax + zMin) / 2;
                zTarget = FindStartIndex(ref zMin, ref zMax, zTarget);
            }
            if (answer < 0)
            {
                zMin = (zMax + zMin) / 2;
                zTarget = FindStartIndex(ref zMin, ref zMax, zTarget);
            }

            return zTarget;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            this.Update_VisibleLineNumberItems();
            base.OnPaint(e);

            if (zLineNumbers_AntiAlias)
                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
            else
                e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;

            string zTextToShow = "";
            string zReminderToShow = "";
            StringFormat zSF = new StringFormat();
            SizeF zTextSize;
            Pen zPen = new Pen(this.ForeColor);
            SolidBrush zBrush = new SolidBrush(this.ForeColor);
            Point zPoint = new Point(0, 0);
            Rectangle zItemClipRectangle = new Rectangle(0, 0, 0, 0);

            System.Drawing.Drawing2D.GraphicsPath zGP_GridLines =
                new System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);
            System.Drawing.Drawing2D.GraphicsPath zGP_BorderLines =
                new System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);
            System.Drawing.Drawing2D.GraphicsPath zGP_MarginLines =
                new System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);
            System.Drawing.Drawing2D.GraphicsPath zGP_LineNumbers =
                new System.Drawing.Drawing2D.GraphicsPath(System.Drawing.Drawing2D.FillMode.Winding);
            Region zRegion = new Region(base.ClientRectangle);

            if (this.DesignMode)
            {
                if (zParent == null)
                {
                    zReminderToShow = "-!- Set ParentRichTextBox -!-";
                }
                else
                {
                    if (zLNIs.Count == 0)
                    {
                        zReminderToShow = "LineNrs ( " + zParent.Name + ")";
                    }
                }
                if (zReminderToShow.Length > 0)
                {
                    e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
                    e.Graphics.RotateTransform(-90f);
                    zSF.Alignment = StringAlignment.Center;
                    zSF.LineAlignment = StringAlignment.Center;
                    zTextSize = e.Graphics.MeasureString(zReminderToShow, this.Font, zPoint, zSF);
                    e.Graphics.DrawString(zReminderToShow, this.Font, Brushes.WhiteSmoke, 1, 1, zSF);
                    e.Graphics.DrawString(zReminderToShow, this.Font, Brushes.Firebrick, 0, 0, zSF);
                    e.Graphics.ResetTransform();

                    Rectangle zReminderRectangle = new Rectangle((int)(this.Width / 2 - zTextSize.Height / 2),
                                                                 (int)(this.Height / 2 - zTextSize.Width / 2), (int)zTextSize.Height,
                                                                 (int)zTextSize.Width);
                    zGP_LineNumbers.AddRectangle(zReminderRectangle);
                    zGP_LineNumbers.CloseFigure();

                    if (zAutoSizing)
                    {
                        zReminderRectangle.Inflate((int)(zTextSize.Height * 0.2f), (int)(zTextSize.Width * 0.1f));
                        zAutoSizing_Size = new Size(zReminderRectangle.Width, zReminderRectangle.Height);
                    }
                }
            }


            if (zLNIs.Count > 0)
            {
                System.Drawing.Drawing2D.LinearGradientBrush zLGB = null;
                zPen = new Pen(zGridLines_Color, zGridLines_Thickness);
                zPen.DashStyle = zGridLines_Style;
                zSF.Alignment = StringAlignment.Near;
                zSF.LineAlignment = StringAlignment.Near;
                zSF.FormatFlags = StringFormatFlags.FitBlackBox | StringFormatFlags.NoClip | StringFormatFlags.NoWrap;

                for (int zA = 0; zA <= zLNIs.Count - 1; zA++)
                {
                    if (zGradient_Show)
                    {
                        zLGB = new System.Drawing.Drawing2D.LinearGradientBrush(zLNIs[zA].GetRectangle,
                                                                                zGradient_StartColor, zGradient_EndColor,
                                                                                zGradient_Direction);
                        e.Graphics.FillRectangle(zLGB, zLNIs[zA].GetRectangle);
                    }

                    if (zGridLines_Show)
                    {
                        e.Graphics.DrawLine(zPen, new Point(0, zLNIs[zA].GetRectangle.Y),
                                            new Point(this.Width, zLNIs[zA].GetRectangle.Y));

                        zGP_GridLines.AddRectangle(new Rectangle((int)(-zGridLines_Thickness), zLNIs[zA].GetRectangle.Y,
                                                                 (int)(this.Width + zGridLines_Thickness * 2f),
                                                                 (int)(this.Height - zLNIs[0].GetRectangle.Y +
                                                                 zGridLines_Thickness)));
                        zGP_GridLines.CloseFigure();
                    }

                    if (zLineNumbers_Show)
                    {
                        if (zLineNumbers_ShowLeadingZeroes)
                            zTextToShow = zLineNumbers_ShowAsHexadecimal
                                              ? zLNIs[zA].GetLineNumber.ToString("X")
                                              : zLNIs[zA].GetLineNumber.ToString(zLineNumbers_Format);
                        else
                            zTextToShow = zLineNumbers_ShowAsHexadecimal
                                              ? zLNIs[zA].GetLineNumber.ToString("X")
                                              : zLNIs[zA].GetLineNumber.ToString();

                        zTextSize = e.Graphics.MeasureString(zTextToShow, this.Font, zPoint, zSF);

                        if (zLineNumbers_Alignment == ContentAlignment.TopLeft)
                        {
                            zPoint =
                                new Point(zLNIs[zA].GetRectangle.Left + this.Padding.Left + zLineNumbers_Offset.Width,
                                          zLNIs[zA].GetRectangle.Top + this.Padding.Top + zLineNumbers_Offset.Height);
                        }
                        else if (zLineNumbers_Alignment == ContentAlignment.MiddleLeft)
                        {
                            zPoint =
                               new Point(zLNIs[zA].GetRectangle.Left + this.Padding.Left + zLineNumbers_Offset.Width,
                                         (int)(zLNIs[zA].GetRectangle.Top + (zLNIs[zA].GetRectangle.Height / 2) + zLineNumbers_Offset.Height - zTextSize.Height / 2));
                        }
                        else if (zLineNumbers_Alignment == ContentAlignment.BottomLeft)
                        {
                            zPoint = new Point(zLNIs[zA].GetRectangle.Left + this.Padding.Left + zLineNumbers_Offset.Width,
                                               (int)(zLNIs[zA].GetRectangle.Bottom - this.Padding.Bottom + 1 +
                                               zLineNumbers_Offset.Height - zTextSize.Height));
                        }
                        else if (zLineNumbers_Alignment == ContentAlignment.TopCenter)
                        {
                            zPoint =
                                new Point(
                                    (int)(zLNIs[zA].GetRectangle.Width / 2 + zLineNumbers_Offset.Width - zTextSize.Width / 2),
                                    zLNIs[zA].GetRectangle.Top + this.Padding.Top + zLineNumbers_Offset.Height);
                        }
                        else if (zLineNumbers_Alignment == ContentAlignment.MiddleCenter)
                        {
                            zPoint =
                                new Point(
                                    (int)(zLNIs[zA].GetRectangle.Width / 2 + zLineNumbers_Offset.Width - zTextSize.Width / 2),
                                    (int)(zLNIs[zA].GetRectangle.Top + (zLNIs[zA].GetRectangle.Height / 2) +
                                    zLineNumbers_Offset.Height - zTextSize.Height / 2));
                        }
                        else if (zLineNumbers_Alignment == ContentAlignment.BottomCenter)
                        {
                            zPoint =
                                new Point(
                                    (int)
                                    (zLNIs[zA].GetRectangle.Width / 2 + zLineNumbers_Offset.Width - zTextSize.Width / 2),
                                    (int)
                                    (zLNIs[zA].GetRectangle.Bottom - this.Padding.Bottom + 1 +
                                     zLineNumbers_Offset.Height - zTextSize.Height));
                        }
                        else if (zLineNumbers_Alignment == ContentAlignment.TopRight)
                        {
                            zPoint =
                                new Point(
                                    (int)(zLNIs[zA].GetRectangle.Right - this.Padding.Right + zLineNumbers_Offset.Width -
                                    zTextSize.Width),
                                    zLNIs[zA].GetRectangle.Top + this.Padding.Top + zLineNumbers_Offset.Height);
                        }
                        else if (zLineNumbers_Alignment == ContentAlignment.MiddleRight)
                        {
                            zPoint =
                                new Point(
                                    (int)(zLNIs[zA].GetRectangle.Right - this.Padding.Right + zLineNumbers_Offset.Width -
                                    zTextSize.Width),
                                    (int)(zLNIs[zA].GetRectangle.Top + (zLNIs[zA].GetRectangle.Height / 2) +
                                    zLineNumbers_Offset.Height - zTextSize.Height / 2));
                        }
                        else if (zLineNumbers_Alignment == ContentAlignment.BottomRight)
                        {
                            zPoint =
                                new Point(
                                    (int)
                                    (zLNIs[zA].GetRectangle.Right - this.Padding.Right + zLineNumbers_Offset.Width -
                                     zTextSize.Width), (int)
                                    (zLNIs[zA].GetRectangle.Bottom - this.Padding.Bottom + 1 + zLineNumbers_Offset.Height -
                                    zTextSize.Height));
                        }
                        zItemClipRectangle = new Rectangle(zPoint, zTextSize.ToSize());
                        if (zLineNumbers_ClipByItemRectangle)
                        {
                            zItemClipRectangle.Intersect(zLNIs[zA].GetRectangle);
                            e.Graphics.SetClip(zItemClipRectangle);
                        }
                        e.Graphics.DrawString(zTextToShow, this.Font, zBrush, zPoint, zSF);
                        e.Graphics.ResetClip();

                        zGP_LineNumbers.AddRectangle(zItemClipRectangle);
                        zGP_LineNumbers.CloseFigure();
                    }
                }

                if (zGridLines_Show)
                {
                    zPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                    zGP_GridLines.Widen(zPen);
                }

                if (zLGB != null)
                    zLGB.Dispose();
            }

            Point zP_Left = new Point((int)(Math.Floor(zBorderLines_Thickness / 2)), (int)(Math.Floor(zBorderLines_Thickness / 2)));
            Point zP_Right = new Point((int)(this.Width - Math.Ceiling(zBorderLines_Thickness / 2)), (int)
                                       (this.Height - Math.Ceiling(zBorderLines_Thickness / 2)));
            Point[] zBorderLines_Points = new Point[] { new Point(zP_Left.X, zP_Left.Y), new Point(zP_Right.X, zP_Left.Y), new Point(zP_Right.X, zP_Right.Y), new Point(zP_Left.X, zP_Right.Y), new Point(zP_Left.X, zP_Left.Y) };

            if (zBorderLines_Show)
            {
                zPen = new Pen(zBorderLines_Color, zBorderLines_Thickness);
                zPen.DashStyle = zBorderLines_Style;
                e.Graphics.DrawLines(zPen, zBorderLines_Points);
                zGP_BorderLines.AddLines(zBorderLines_Points);
                zGP_BorderLines.CloseFigure();
                zPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                zGP_BorderLines.Widen(zPen);
            }

            if (zMarginLines_Show && zMarginLines_Side > LineNumberDockSide.None)
            {
                zP_Left = new Point((int)(-zMarginLines_Thickness), (int)(-zMarginLines_Thickness));
                zP_Right = new Point((int)(this.Width + zMarginLines_Thickness), (int)(this.Height + zMarginLines_Thickness));
                zPen = new Pen(zMarginLines_Color, zMarginLines_Thickness);
                zPen.DashStyle = zMarginLines_Style;
                if (zMarginLines_Side == LineNumberDockSide.Left || zMarginLines_Side == LineNumberDockSide.Height)
                {
                    e.Graphics.DrawLine(zPen, new Point((int)(Math.Floor(zMarginLines_Thickness / 2)), 0),
                                        new Point((int)Math.Floor(zMarginLines_Thickness / 2), this.Height - 1));
                    zP_Left = new Point((int)Math.Ceiling(zMarginLines_Thickness / 2), (int)-zMarginLines_Thickness);
                }
                if (zMarginLines_Side == LineNumberDockSide.Right || zMarginLines_Side == LineNumberDockSide.Height)
                {
                    e.Graphics.DrawLine(zPen, new Point((int)(this.Width - Math.Ceiling(zMarginLines_Thickness / 2)), 0), new Point((int)(this.Width - Math.Ceiling(zMarginLines_Thickness / 2)), this.Height - 1));
                    zP_Right = new
                    Point((int)(this.Width - Math.Ceiling(zMarginLines_Thickness / 2)), (int)(this.Height + zMarginLines_Thickness));
                }

                zGP_MarginLines.AddRectangle(new Rectangle(zP_Left,
                                                           new Size(zP_Right.X - zP_Left.X, zP_Right.Y - zP_Left.Y)));
                zPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                zGP_MarginLines.Widen(zPen);
            }

            if (zSeeThroughMode)
            {
                zRegion.MakeEmpty();
                zRegion.Union(zGP_BorderLines);
                zRegion.Union(zGP_MarginLines);
                zRegion.Union(zGP_GridLines);
                zRegion.Union(zGP_LineNumbers);
            }

            if (zRegion.GetBounds(e.Graphics).IsEmpty)
            {
                zGP_BorderLines.AddLines(zBorderLines_Points);
                zGP_BorderLines.CloseFigure();
                zPen = new Pen(zBorderLines_Color, 1);
                zPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
                zGP_BorderLines.Widen(zPen);

                zRegion = new Region(zGP_BorderLines);
            }
            this.Region = zRegion;

            if (zPen != null) zPen.Dispose();
            if (zBrush != null) zBrush.Dispose();
            if (zRegion != null) zRegion.Dispose();
            if (zGP_GridLines != null) zGP_GridLines.Dispose();
            if (zGP_BorderLines != null) zGP_BorderLines.Dispose();
            if (zGP_MarginLines != null) zGP_MarginLines.Dispose();
            if (zGP_LineNumbers != null) zGP_LineNumbers.Dispose();

        }

        private void zTimer_Tick(object sender, EventArgs e)
        {
            zParentIsScrolling = false;
            zTimer.Stop();
            this.Invalidate();
        }

        private void zParent_Changed(Object sender, System.EventArgs e)
        {
            this.Refresh();
            this.Invalidate();
        }

        private void zParent_Scroll(object sender, System.EventArgs e)
        {
            zParentIsScrolling = true;
            this.Invalidate();
        }

        private void zParent_ContentsResized(object sender, System.Windows.Forms.ContentsResizedEventArgs e)
        {
            zContentRectangle = e.NewRectangle;
            this.Refresh();
            this.Invalidate();
        }

        private void zParent_Disposed(object sender, System.EventArgs e)
        {
            this.ParentRichTextBox = null;
            this.Refresh();
            this.Invalidate();
        }
    }
}

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
Web Developer http://www.icemanind.com
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions