Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Fast Colored TextBox for Syntax Highlighting

, 12 Jul 2014
Custom text editor with syntax highlighting
example-noexe.zip
example.zip
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_Help.zip
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.Collections.Generic;
using System;
using System.Text;
using System.Drawing;

namespace FastColoredTextBoxNS
{
    /// <summary>
    /// Line of text
    /// </summary>
    public class Line : IList<Char>
    {
        protected List<Char> chars;

        public string FoldingStartMarker { get; set; }
        public string FoldingEndMarker { get; set; }
        /// <summary>
        /// Text of line was changed
        /// </summary>
        public bool IsChanged { get; set; }
        /// <summary>
        /// Time of last visit of caret in this line
        /// </summary>
        /// <remarks>This property can be used for forward/backward navigating</remarks>
        public DateTime LastVisit { get; set; }
        /// <summary>
        /// Background brush.
        /// </summary>
        public Brush BackgroundBrush { get; set;}
        /// <summary>
        /// Unique ID
        /// </summary>
        public int UniqueId { get; private set; }
        /// <summary>
        /// Count of needed start spaces for AutoIndent
        /// </summary>
        public int AutoIndentSpacesNeededCount
        {
            get;
            internal set;
        }

        internal Line(int uid)
        {
            this.UniqueId = uid;
            chars = new List<Char>();
        }


        /// <summary>
        /// Clears style of chars, delete folding markers
        /// </summary>
        public void ClearStyle(StyleIndex styleIndex)
        {
            FoldingStartMarker = null;
            FoldingEndMarker = null;
            for (int i = 0; i < Count; i++)
            {
                Char c = this[i];
                c.style &= ~styleIndex;
                this[i] = c;
            }
        }

        /// <summary>
        /// Text of the line
        /// </summary>
        public virtual string Text
        {
            get{
                StringBuilder sb = new StringBuilder(Count);
                foreach(Char c in this)
                    sb.Append(c.c);
                return sb.ToString();
            }
        }

        /// <summary>
        /// Clears folding markers
        /// </summary>
        public void ClearFoldingMarkers()
        {
            FoldingStartMarker = null;
            FoldingEndMarker = null;
        }

        /// <summary>
        /// Count of start spaces
        /// </summary>
        public int StartSpacesCount
        {
            get
            {
                int spacesCount = 0;
                for (int i = 0; i < Count; i++)
                    if (this[i].c == ' ')
                        spacesCount++;
                    else
                        break;
                return spacesCount;
            }
        }

        public int IndexOf(Char item)
        {
            return chars.IndexOf(item);
        }

        public void Insert(int index, Char item)
        {
            chars.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            chars.RemoveAt(index);
        }

        public Char this[int index]
        {
            get
            {
                return chars[index];
            }
            set
            {
                chars[index] = value;
            }
        }

        public void Add(Char item)
        {
            chars.Add(item);
        }

        public void Clear()
        {
            chars.Clear();
        }

        public bool Contains(Char item)
        {
            return chars.Contains(item);
        }

        public void CopyTo(Char[] array, int arrayIndex)
        {
            chars.CopyTo(array, arrayIndex);
        }

        /// <summary>
        /// Chars count
        /// </summary>
        public int Count
        {
            get { return chars.Count; }
        }

        public bool IsReadOnly
        {
            get {  return false; }
        }

        public bool Remove(Char item)
        {
            return chars.Remove(item);
        }

        public IEnumerator<Char> GetEnumerator()
        {
            return chars.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return chars.GetEnumerator() as System.Collections.IEnumerator;
        }

        public virtual void RemoveRange(int index, int count)
        {
            if (index >= Count)
                return;
            chars.RemoveRange(index, Math.Min(Count - index, count));
        }

        public virtual void TrimExcess()
        {
            chars.TrimExcess();
        }

        public virtual void AddRange(IEnumerable<Char> collection)
        {
            chars.AddRange(collection);
        }
    }

    public struct LineInfo
    {
        List<int> cutOffPositions;
        //Y coordinate of line on screen
        internal int startY;// = -1;
        internal int bottomPadding;
        /// <summary>
        /// Visible state
        /// </summary>
        public VisibleState VisibleState;

        public LineInfo(int startY)
        {
            cutOffPositions = null;
            VisibleState = FastColoredTextBoxNS.VisibleState.Visible;
            this.startY = startY;
            bottomPadding = 0;
        }
        /// <summary>
        /// Positions for wordwrap cutoffs
        /// </summary>
        public List<int> CutOffPositions
        {
            get
            {
                if (cutOffPositions == null)
                    cutOffPositions = new List<int>();
                return cutOffPositions;
            }
        }

        /// <summary>
        /// Count of wordwrap string count for this line
        /// </summary>
        public int WordWrapStringsCount
        {
            get
            {
                switch (VisibleState)
                {
                    case VisibleState.Visible:
                         if (cutOffPositions == null)
                            return 1;
                         else
                            return cutOffPositions.Count + 1;
                    case VisibleState.Hidden: return 0;
                    case VisibleState.StartOfHiddenBlock: return 1;
                }

                return 0;
            }
        }

        internal int GetWordWrapStringStartPosition(int iWordWrapLine)
        {
            return iWordWrapLine == 0 ? 0 : CutOffPositions[iWordWrapLine - 1];
        }

        internal int GetWordWrapStringFinishPosition(int iWordWrapLine, Line line)
        {
            if (WordWrapStringsCount <= 0)
                return 0;
            return iWordWrapLine == WordWrapStringsCount - 1 ? line.Count - 1 : CutOffPositions[iWordWrapLine] - 1;
        }

        /// <summary>
        /// Gets index of wordwrap string for given char position
        /// </summary>
        public int GetWordWrapStringIndex(int iChar)
        {
            if (cutOffPositions == null || cutOffPositions.Count == 0) return 0;
            for (int i = 0; i < cutOffPositions.Count; i++)
                if (cutOffPositions[i] >/*>=*/ iChar)
                    return i;
            return cutOffPositions.Count;
        }

        /// <summary>
        /// Calculates wordwrap cutoffs
        /// </summary>
        internal void CalcCutOffs(int maxCharsPerLine, bool allowIME, bool charWrap, Line line)
        {
            int segmentLength = 0;
            int cutOff = 0;
            CutOffPositions.Clear();

            for (int i = 0; i < line.Count; i++)
            {
                char c = line[i].c;
                if (charWrap)
                {
                    //char wrapping
                    cutOff = Math.Min(i + 1, line.Count - 1);
                }
                else
                {
                    //word wrapping
                    if (allowIME && isCJKLetter(c))//in CJK languages cutoff can be in any letter
                    {
                        cutOff = i;
                    }
                    else
                        if (!char.IsLetterOrDigit(c) && c != '_' && c != '\'')
                            cutOff = Math.Min(i + 1, line.Count - 1);
                }

                segmentLength++;

                if (segmentLength == maxCharsPerLine)
                {
                    if (cutOff == 0 || (cutOffPositions.Count > 0 && cutOff == cutOffPositions[cutOffPositions.Count - 1]))
                        cutOff = i + 1;
                    CutOffPositions.Add(cutOff);
                    segmentLength = 1 + i - cutOff;
                }
            }
        }

        private bool isCJKLetter(char c)
        {
            int code = Convert.ToInt32(c);
            return
            (code >= 0x3300 && code <= 0x33FF) ||
            (code >= 0xFE30 && code <= 0xFE4F) ||
            (code >= 0xF900 && code <= 0xFAFF) ||
            (code >= 0x2E80 && code <= 0x2EFF) ||
            (code >= 0x31C0 && code <= 0x31EF) ||
            (code >= 0x4E00 && code <= 0x9FFF) ||
            (code >= 0x3400 && code <= 0x4DBF) ||
            (code >= 0x3200 && code <= 0x32FF) ||
            (code >= 0x2460 && code <= 0x24FF) ||
            (code >= 0x3040 && code <= 0x309F) ||
            (code >= 0x2F00 && code <= 0x2FDF) ||
            (code >= 0x31A0 && code <= 0x31BF) ||
            (code >= 0x4DC0 && code <= 0x4DFF) ||
            (code >= 0x3100 && code <= 0x312F) ||
            (code >= 0x30A0 && code <= 0x30FF) ||
            (code >= 0x31F0 && code <= 0x31FF) ||
            (code >= 0x2FF0 && code <= 0x2FFF) ||
            (code >= 0x1100 && code <= 0x11FF) ||
            (code >= 0xA960 && code <= 0xA97F) ||
            (code >= 0xD7B0 && code <= 0xD7FF) ||
            (code >= 0x3130 && code <= 0x318F) ||
            (code >= 0xAC00 && code <= 0xD7AF);

        }
    }

    public enum VisibleState: byte
    {
        Visible, StartOfHiddenBlock, Hidden
    }

    public enum IndentMarker
    {
        None,
        Increased,
        Decreased
    }
}

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 12 Jul 2014
Article Copyright 2011 by Pavel Torgashov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid