Click here to Skip to main content
11,413,961 members (77,272 online)
Click here to Skip to main content
Add your own
alternative version

Fast Colored TextBox for Syntax Highlighting

, 25 Oct 2014 LGPL3
Custom text editor with syntax highlighting
example-noexe.zip
example.zip
FastColoredTextBox.dll
WindowsFormsApplication8.exe
FastColoredTextBox.zip
FastColoredTextBox
FastColoredTextBox.suo
FastColoredTextBoxVS2008.suo
FastColoredTextBox
SyntaxHighlighter.cs.old
bin
Debug
FastColoredTextBox.dll
obj
Debug
FastColoredTextBox.dll
FastColoredTextBoxNS.FastColoredTextBox.resources
FastColoredTextBoxNS.FindForm.resources
FastColoredTextBoxNS.GoToForm.resources
FastColoredTextBoxNS.ReplaceForm.resources
Release
FastColoredTextBox.dll
FastColoredTextBoxNS.FastColoredTextBox.resources
FastColoredTextBoxNS.FindForm.resources
FastColoredTextBoxNS.ReplaceForm.resources
GenerateResource-ResGen.read.1.tlog
GenerateResource-ResGen.write.1.tlog
Properties
Tester
Tester.csproj.user
bin
Debug
FastColoredTextBox.dll
TabStrip.dll
Tester.exe
Tester.vshost.exe
obj
x86
Debug
Tester.AutocompleteSample.resources
Tester.AutocompleteSample2.resources
Tester.AutoIndentSample.resources
Tester.BilingualHighlighterSample.resources
Tester.BookmarksSample.resources
Tester.ConsoleSample.resources
Tester.CustomFoldingSample.resources
Tester.CustomHint.resources
Tester.CustomStyleSample.resources
Tester.CustomTextSourceSample.resources
Tester.DynamicSyntaxHighlighting.resources
Tester.exe
Tester.GifImageDrawingSample.resources
Tester.HintSample.resources
Tester.HyperlinkSample.resources
Tester.IMEsample.resources
Tester.JokeSample.resources
Tester.LazyLoadingSample.resources
Tester.LoggerSample.resources
Tester.MainForm.resources
Tester.MarkerToolSample.resources
Tester.PowerfulCSharpEditor.resources
Tester.PowerfulSample.resources
Tester.PredefinedStylesSample.resources
Tester.Properties.Resources.resources
Tester.ReadOnlyBlocksSample.resources
Tester.SimplestCodeFoldingSample.resources
Tester.SimplestSyntaxHighlightingSample.resources
Tester.SplitSample.resources
Tester.SyntaxHighlightingByXmlDescription.resources
Tester.TooltipSample.resources
Tester.VisibleRangeChangedDelayedSample.resources
TempPE
Properties.Resources.Designer.cs.dll
Release
GenerateResource-ResGen.read.1.tlog
GenerateResource-ResGen.write.1.tlog
Tester.AutocompleteSample.resources
Tester.AutocompleteSample2.resources
Tester.AutoIndentSample.resources
Tester.BookmarksSample.resources
Tester.CustomStyleSample.resources
Tester.DynamicSyntaxHighlighting.resources
Tester.exe
Tester.GifImageDrawingSample.resources
Tester.IMEsample.resources
Tester.JokeSample.resources
Tester.LazyLoadingSample.resources
Tester.LoggerSample.resources
Tester.MainForm.resources
Tester.MarkerToolSample.resources
Tester.PowerfulCSharpEditor.resources
Tester.PowerfulSample.resources
Tester.Properties.Resources.resources
Tester.SimplestCodeFoldingSample.resources
Tester.SimplestSyntaxHighlightingSample.resources
Tester.SplitSample.resources
Tester.SyntaxHighlightingByXmlDescription.resources
Tester.TooltipSample.resources
Tester.VisibleRangeChangedDelayedSample.resources
TempPE
Properties.Resources.Designer.cs.dll
Properties
Settings.settings
Resources
backward0_16x16.png
bookmark--plus.png
box.png
bye.gif
class_libraries.png
edit-padding-top.png
forward_16x16.png
layer--minus.png
layer--plus.png
lightning.png
lol.gif
property.png
redo_16x16.png
rolleyes.gif
sad_16x16.png
smile_16x16.png
undo_16x16.png
unsure.gif
TesterVB
TesterVB.vbproj.user
bin
Debug
FastColoredTextBox.dll
TabStrip.dll
TesterVB.exe
TesterVB.vshost.exe
TesterVB.vshost.exe.manifest
My Project
Application.myapp
Settings.settings
obj
x86
Debug
TesterVB.AutocompleteSample.resources
TesterVB.AutocompleteSample2.resources
TesterVB.AutoIndentSample.resources
TesterVB.BookmarksSample.resources
TesterVB.exe
TesterVB.PowerfulSample.resources
TesterVB.Resources.resources
TesterVB.TesterVB.ConsoleSample.resources
TesterVB.TesterVB.CustomStyleSample.resources
TesterVB.TesterVB.JokeSample.resources
TesterVB.TesterVB.LazyLoadingSample.resources
TesterVB.TesterVB.MainForm.resources
TesterVB.TesterVB.PowerfulCSharpEditor.resources
TesterVB.TesterVB.TooltipSample.resources
TempPE
My Project.Resources.Designer.vb.dll
Resources
99.jpeg
backward0_16x16.png
bookmark--plus.png
box.png
bye.gif
class_libraries.png
edit-padding-top.png
forward_16x16.png
layer--minus.png
layer--plus.png
lightning.png
lol.gif
property.png
redo_16x16.png
rolleyes.gif
sad_16x16.png
smile_16x16.png
undo_16x16.png
unsure.gif
FastColoredTextBoxCF.zip
FastColoredTextBoxCF
FastColoredTextBox
FastColoredTextBox.csproj.user
bin
Release
FastColoredTextBox.dll
FastColoredTextBox.pdb
Properties
Tester
Tester.csproj.user
bin
Release
FastColoredTextBox.dll
FastColoredTextBox.pdb
Tester.exe
Tester.pdb
Properties
FastColoredTextBoxDemo-noexe.zip
FastColoredTextBoxDemo
FastColoredTextBoxDemo.zip
FastColoredTextBox.dll
TabStrip.dll
Tester.exe
fastcoloredtextboxime-noexe.zip
fastcoloredtextboxime.zip
FastColoredTextBox.dll
WindowsFormsApplication83.exe
FastColoredTextBox_Help.zip
FastColoredTextBox_Help.chm
IronyFCTB-noexe.zip
IronyFCTB
IronyFCTB.suo
ExternalDlls
IronyFCTB
bin
Debug
obj
Debug
Properties
Tester
bin
Debug
obj
x86
Debug
Tester.MainForm.resources
Tester.Properties.Resources.resources
Tester.SimplestSample.resources
Properties
Settings.settings
IronyFCTB.zip
IronyFCTB.suo
FastColoredTextBox.dll
Irony.dll
Irony.Interpreter.dll
Irony.Samples.dll
FastColoredTextBox.dll
Irony.dll
IronyFCTB.dll
IronyFCTB.pdb
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
IronyFCTB.csprojResolveAssemblyReference.cache
IronyFCTB.dll
IronyFCTB.pdb
FastColoredTextBox.dll
Irony.dll
Irony.Interpreter.dll
Irony.Samples.dll
IronyFCTB.dll
IronyFCTB.pdb
Tester.exe
Tester.pdb
Tester.vshost.exe
DesignTimeResolveAssemblyReferences.cache
DesignTimeResolveAssemblyReferencesInput.cache
Tester.csproj.GenerateResource.Cache
Tester.csprojResolveAssemblyReference.cache
Tester.exe
Tester.MainForm.resources
Tester.pdb
Tester.Properties.Resources.resources
Tester.SimplestSample.resources
Settings.settings
using System.Drawing;
using System;
using System.Drawing.Drawing2D;
using System.Collections.Generic;

namespace FastColoredTextBoxNS
{
    /// <summary>
    /// Style of chars
    /// </summary>
    /// <remarks>This is base class for all text and design renderers</remarks>
    public abstract class Style : IDisposable
    {
        /// <summary>
        /// This style is exported to outer formats (HTML for example)
        /// </summary>
        public virtual bool IsExportable { get; set; }
        /// <summary>
        /// Occurs when user click on StyleVisualMarker joined to this style 
        /// </summary>
        public event EventHandler<VisualMarkerEventArgs> VisualMarkerClick;

        /// <summary>
        /// Constructor
        /// </summary>
        public Style()
        {
            IsExportable = true;
        }

        /// <summary>
        /// Renders given range of text
        /// </summary>
        /// <param name="gr">Graphics object</param>
        /// <param name="position">Position of the range in absolute control coordinates</param>
        /// <param name="range">Rendering range of text</param>
        public abstract void Draw(Graphics gr, Point position, Range range);

        /// <summary>
        /// Occurs when user click on StyleVisualMarker joined to this style 
        /// </summary>
        public virtual void OnVisualMarkerClick(FastColoredTextBox tb, VisualMarkerEventArgs args)
        {
            if (VisualMarkerClick != null)
                VisualMarkerClick(tb, args);
        }

        /// <summary>
        /// Shows VisualMarker
        /// Call this method in Draw method, when you need to show VisualMarker for your style
        /// </summary>
        protected virtual void AddVisualMarker(FastColoredTextBox tb, StyleVisualMarker marker)
        {
            tb.AddVisualMarker(marker);
        }

        public static Size GetSizeOfRange(Range range)
        {
            return new Size((range.End.iChar - range.Start.iChar) * range.tb.CharWidth, range.tb.CharHeight);
        }

        public static GraphicsPath GetRoundedRectangle(Rectangle rect, int d)
        {
            GraphicsPath gp = new GraphicsPath();

            gp.AddArc(rect.X, rect.Y, d, d, 180, 90);
            gp.AddArc(rect.X + rect.Width - d, rect.Y, d, d, 270, 90);
            gp.AddArc(rect.X + rect.Width - d, rect.Y + rect.Height - d, d, d, 0, 90);
            gp.AddArc(rect.X, rect.Y + rect.Height - d, d, d, 90, 90);
            gp.AddLine(rect.X, rect.Y + rect.Height - d, rect.X, rect.Y + d / 2);

            return gp;
        }

        public virtual void Dispose()
        {
            ;
        }

        /// <summary>
        /// Returns CSS for export to HTML
        /// </summary>
        /// <returns></returns>
        public virtual string GetCSS()
        {
            return "";
        }
    }

    /// <summary>
    /// Style for chars rendering
    /// This renderer can draws chars, with defined fore and back colors
    /// </summary>
    public class TextStyle : Style
    {
        public Brush ForeBrush { get; set; }
        public Brush BackgroundBrush { get; set; }
        public FontStyle FontStyle { get; set; }
        //public readonly Font Font;
        public StringFormat stringFormat;

        public TextStyle(Brush foreBrush, Brush backgroundBrush, FontStyle fontStyle)
        {
            this.ForeBrush = foreBrush;
            this.BackgroundBrush = backgroundBrush;
            this.FontStyle = fontStyle;
            stringFormat = new StringFormat(StringFormatFlags.MeasureTrailingSpaces);
        }

        public override void Draw(Graphics gr, Point position, Range range)
        {
            //draw background
            if (BackgroundBrush != null)
                gr.FillRectangle(BackgroundBrush, position.X, position.Y, (range.End.iChar - range.Start.iChar) * range.tb.CharWidth, range.tb.CharHeight);
            //draw chars
            Font f = new Font(range.tb.Font, FontStyle);
            //Font fHalfSize = new Font(range.tb.Font.FontFamily, f.SizeInPoints/2, FontStyle);
            Line line = range.tb[range.Start.iLine];
            float dx = range.tb.CharWidth;
            float y = position.Y + range.tb.LineInterval / 2;
            float x = position.X - range.tb.CharWidth/3;

            if(ForeBrush == null)
                ForeBrush = new SolidBrush(range.tb.ForeColor);

            //IME mode
            if (range.tb.ImeAllowed)
            for (int i = range.Start.iChar; i < range.End.iChar; i++)
            {
                SizeF size = FastColoredTextBox.GetCharSize(f, line[i].c);
                
                var gs = gr.Save();
                float k = size.Width>range.tb.CharWidth + 1?range.tb.CharWidth / size.Width:1;
                gr.TranslateTransform(x, y+(1-k)*range.tb.CharHeight/2);
                gr.ScaleTransform(k, (float)Math.Sqrt(k));
                gr.DrawString(line[i].c.ToString(), f, ForeBrush, 0, 0, stringFormat);
                gr.Restore(gs);
                /*
                if(size.Width>range.tb.CharWidth*1.5f)
                    gr.DrawString(line[i].c.ToString(), fHalfSize, foreBrush, x, y+range.tb.CharHeight/4, stringFormat);
                else
                    gr.DrawString(line[i].c.ToString(), f, foreBrush, x, y, stringFormat);
                 * */
                x += dx;
            }
            else
            //classic mode 
            for (int i = range.Start.iChar; i < range.End.iChar; i++)
            {
                //draw char
                gr.DrawString(line[i].c.ToString(), f, ForeBrush, x, y, stringFormat);
                x += dx;
            }
            //
            f.Dispose();
        }

        public override void Dispose()
        {
            base.Dispose();
            
            if(ForeBrush!=null)
                ForeBrush.Dispose();
            if(BackgroundBrush!=null)
                BackgroundBrush.Dispose();
        }

        public override string GetCSS()
        {
            string result = "";

            if (BackgroundBrush is SolidBrush)
            {
                var s =  ExportToHTML.GetColorAsString((BackgroundBrush as SolidBrush).Color);
                if (s != "")
                    result += "background-color:" + s + ";";
            }
            if (ForeBrush is SolidBrush)
            {
                var s = ExportToHTML.GetColorAsString((ForeBrush as SolidBrush).Color);
                if (s != "")
                    result += "color:" + s + ";";
            }
            if ((FontStyle & FontStyle.Bold) != 0)
                result += "font-weight:bold;";
            if ((FontStyle & FontStyle.Italic) != 0)
                result += "font-style:oblique;";
            if ((FontStyle & FontStyle.Strikeout) != 0)
                result += "text-decoration:line-through;";
            if ((FontStyle & FontStyle.Underline) != 0)
                result += "text-decoration:underline;";

            return result;
        }
    }

    /// <summary>
    /// Renderer for folded block
    /// </summary>
    public class FoldedBlockStyle : TextStyle
    {
        public FoldedBlockStyle(Brush foreBrush, Brush backgroundBrush, FontStyle fontStyle):
            base(foreBrush, backgroundBrush, fontStyle)
        {
        }

        public override void Draw(Graphics gr, Point position, Range range)
        {
            if (range.End.iChar > range.Start.iChar)
            {
                base.Draw(gr, position, range);

                int firstNonSpaceSymbolX = position.X;
                
                //find first non space symbol
                for (int i = range.Start.iChar; i < range.End.iChar; i++)
                    if (range.tb[range.Start.iLine][i].c != ' ')
                        break;
                    else
                        firstNonSpaceSymbolX += range.tb.CharWidth;

                //create marker
                range.tb.AddVisualMarker(new FoldedAreaMarker(range.Start.iLine, new Rectangle(firstNonSpaceSymbolX, position.Y, position.X + (range.End.iChar - range.Start.iChar) * range.tb.CharWidth - firstNonSpaceSymbolX, range.tb.CharHeight)));
            }
            else
            {
                //draw '...'
                using(Font f = new Font(range.tb.Font, FontStyle))
                    gr.DrawString("...", f, ForeBrush, range.tb.LeftIndent, position.Y - 2);
                //create marker
                range.tb.AddVisualMarker(new FoldedAreaMarker(range.Start.iLine, new Rectangle(range.tb.LeftIndent + 2, position.Y, 2 * range.tb.CharHeight, range.tb.CharHeight)));
            }
        }
    }

    /// <summary>
    /// Renderer for selected area
    /// </summary>
    public class SelectionStyle : Style
    {
        public Brush BackgroundBrush{get;set;}

        public override bool IsExportable
        {
            get{return false;}  set{}
        }

        public SelectionStyle(Brush backgroundBrush)
        {
            this.BackgroundBrush = backgroundBrush;
        }

        public override void Draw(Graphics gr, Point position, Range range)
        {
            //draw background
            if (BackgroundBrush != null)
            {
                Rectangle rect = new Rectangle(position.X, position.Y, (range.End.iChar - range.Start.iChar) * range.tb.CharWidth, range.tb.CharHeight);
                if (rect.Width == 0)
                    return;
                gr.FillRectangle(BackgroundBrush, rect);
            }
        }

        public override void Dispose()
        {
            base.Dispose();

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

    /// <summary>
    /// Marker style
    /// Draws background color for text
    /// </summary>
    public class MarkerStyle : Style
    {
        public Brush BackgroundBrush{get;set;}

        public MarkerStyle(Brush backgroundBrush)
        {
            this.BackgroundBrush = backgroundBrush;
            IsExportable = true;
        }

        public override void Draw(Graphics gr, Point position, Range range)
        {
            //draw background
            if (BackgroundBrush != null)
            {
                Rectangle rect = new Rectangle(position.X, position.Y, (range.End.iChar - range.Start.iChar) * range.tb.CharWidth, range.tb.CharHeight);
                if (rect.Width == 0)
                    return;
                gr.FillRectangle(BackgroundBrush, rect);
            }
        }

        public override void Dispose()
        {
            base.Dispose();

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

        public override string GetCSS()
        {
            string result = "";

            if (BackgroundBrush is SolidBrush)
            {
                var s = ExportToHTML.GetColorAsString((BackgroundBrush as SolidBrush).Color);
                if (s != "")
                    result += "background-color:" + s + ";";
            }

            return result;
        }
    }

    /// <summary>
    /// Draws small rectangle for popup menu
    /// </summary>
    public class ShortcutStyle : Style
    {
        public Pen borderPen;

        public ShortcutStyle(Pen borderPen)
        {
            this.borderPen = borderPen;
        }

        public override void Draw(Graphics gr, Point position, Range range)
        {
            //get last char coordinates
            Point p = range.tb.PlaceToPoint(range.End);
            //draw small square under char
            Rectangle rect = new Rectangle(p.X - 5, p.Y + range.tb.CharHeight - 2, 4, 3);
            gr.FillPath(Brushes.White, GetRoundedRectangle(rect, 1));
            gr.DrawPath(borderPen, GetRoundedRectangle(rect, 1));
            //add visual marker for handle mouse events
            AddVisualMarker(range.tb, new StyleVisualMarker(new Rectangle(p.X-range.tb.CharWidth, p.Y, range.tb.CharWidth, range.tb.CharHeight), this));
        }

        public override void Dispose()
        {
            base.Dispose();

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

    /// <summary>
    /// This style draws a wavy line below a given text range.
    /// </summary>
    /// <remarks>Thanks for Yallie</remarks>
    public class WavyLineStyle : Style
    {
        private Pen Pen { get; set; }

        public WavyLineStyle(int alpha, Color color)
        {
            Pen = new Pen(Color.FromArgb(alpha, color));
        }

        public override void Draw(Graphics gr, Point pos, Range range)
        {
            var size = GetSizeOfRange(range);
            var start = new Point(pos.X, pos.Y + size.Height - 1);
            var end = new Point(pos.X + size.Width, pos.Y + size.Height - 1);
            DrawWavyLine(gr, start, end);
        }

        private void DrawWavyLine(Graphics graphics, Point start, Point end)
        {
            if (end.X - start.X < 2)
            {
                graphics.DrawLine(Pen, start, end);
                return;
            }

            var offset = -1;
            var points = new List<Point>();

            for (int i = start.X; i <= end.X; i += 2)
            {
                points.Add(new Point(i, start.Y + offset));
                offset = -offset;
            }

            graphics.DrawLines(Pen, points.ToArray());
        }

        public override void Dispose()
        {
            base.Dispose();
            if (Pen != null)
                Pen.Dispose();
        }
    }

    /// <summary>
    /// This style is used to mark range of text as ReadOnly block
    /// </summary>
    /// <remarks>You can inherite this style to add visual effects of readonly text</remarks>
    public class ReadOnlyStyle : Style
    {
        public ReadOnlyStyle()
        {
            IsExportable = false;
        }

        public override void Draw(Graphics gr, Point position, Range range)
        {
            //
        }
    }
}

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)

Share

About the Author

Pavel Torgashov
Software Developer Freelancer
Ukraine Ukraine
I am Pavеl Tоrgаshоv, and I live in Kyiv, Ukraine.
I've been developing software since 1998.
Main activities: processing of large volumes of data, statistics, computer vision and graphics.

My contact email is p_torgashov[at]ukr.net
Follow on   LinkedIn

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150427.2 | Last Updated 25 Oct 2014
Article Copyright 2011 by Pavel Torgashov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid