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

, 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
//#define debug

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace FastColoredTextBoxNS
{
    /// <summary>
    /// This class contains the source text (chars and styles).
    /// It stores a text lines, the manager of commands, undo/redo stack, styles.
    /// </summary>
    public class FileTextSource : TextSource, IDisposable
    {
        List<int> sourceFileLinePositions = new List<int>();
        FileStream fs;
        Encoding fileEncoding;
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        /// <summary>
        /// Occurs when need to display line in the textbox
        /// </summary>
        public event EventHandler<LineNeededEventArgs> LineNeeded;

        /// <summary>
        /// Occurs when need to save line in the file
        /// </summary>
        public event EventHandler<LinePushedEventArgs> LinePushed;

        public FileTextSource(FastColoredTextBox currentTB)
            : base(currentTB)
        {
            timer.Interval = 10000;
            timer.Tick += new EventHandler(timer_Tick);
            timer.Enabled = true;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            timer.Enabled = false;
            try
            {
                UnloadUnusedLines();
            }
            finally
            {
                timer.Enabled = true;
            }
        }

        private void UnloadUnusedLines()
        {
            const int margin = 2000;
            var iStartVisibleLine = CurrentTB.VisibleRange.Start.iLine;
            var iFinishVisibleLine = CurrentTB.VisibleRange.End.iLine;

            int count = 0;
            for (int i = 0; i < Count; i++)
                if (base.lines[i] != null && !base.lines[i].IsChanged && Math.Abs(i - iFinishVisibleLine) > margin)
                {
                    base.lines[i] = null;
                    count++;
                }
            #if debug
            Console.WriteLine("UnloadUnusedLines: " + count);
            #endif
        }

        public void OpenFile(string fileName, Encoding enc)
        {
            Clear();

            if (fs != null)
                fs.Dispose();

            //read lines of file
            fs = new FileStream(fileName, FileMode.Open);
            var length = fs.Length;
            //read signature
            enc = DefineEncoding(enc, fs);
            int shift = DefineShift(enc);
            //first line
            sourceFileLinePositions.Add((int)fs.Position);
            base.lines.Add(null);
            //other lines
            while(fs.Position < length)
            {
                var b = fs.ReadByte();
                if (b == 10)// char \n
                {
                    sourceFileLinePositions.Add((int)(fs.Position) + shift);
                    base.lines.Add(null);
                }
            }

            Line[] temp = new Line[100];
            var c = base.lines.Count;
            base.lines.AddRange(temp);
            base.lines.TrimExcess();
            base.lines.RemoveRange(c, temp.Length);


            int[] temp2 = new int[100];
            c = base.lines.Count;
            sourceFileLinePositions.AddRange(temp2);
            sourceFileLinePositions.TrimExcess();
            sourceFileLinePositions.RemoveRange(c, temp.Length);
            

            fileEncoding = enc;

            OnLineInserted(0, Count);
            //load first lines for calc width of the text
            var linesCount = Math.Min(lines.Count, CurrentTB.Height/CurrentTB.CharHeight);
            for (int i = 0; i < linesCount; i++)
                LoadLineFromSourceFile(i);
            //
            NeedRecalc(new TextChangedEventArgs(0, 1));
        }

        private int DefineShift(Encoding enc)
        {
            if (enc.IsSingleByte)
                return 0;

            if (enc.HeaderName == "unicodeFFFE")
                return 0;//UTF16 BE

            if (enc.HeaderName == "utf-16")
                return 1;//UTF16 LE

            if (enc.HeaderName == "utf-32BE")
                return 0;//UTF32 BE

            if (enc.HeaderName == "utf-32")
                return 3;//UTF32 LE

            return 0;
        }

        private static Encoding DefineEncoding(Encoding enc, FileStream fs)
        {
            int bytesPerSignature = 0;
            byte[] signature = new byte[4];
            int c = fs.Read(signature, 0, 4);
            if (signature[0] == 0xFF && signature[1] == 0xFE && signature[2] == 0x00 && signature[3] == 0x00 && c >= 4)
            {
                enc = Encoding.UTF32;//UTF32 LE
                bytesPerSignature = 4;
            }
            else
            if (signature[0] == 0x00 && signature[1] == 0x00 && signature[2] == 0xFE && signature[3] == 0xFF)
            {
                enc = new UTF32Encoding(true, true);//UTF32 BE
                bytesPerSignature = 4;
            }
            else
            if (signature[0] == 0xEF && signature[1] == 0xBB && signature[2] == 0xBF)
            {
                enc = Encoding.UTF8;//UTF8
                bytesPerSignature = 3;
            }
            else
            if (signature[0] == 0xFE && signature[1] == 0xFF)
            {
                enc = Encoding.BigEndianUnicode;//UTF16 BE
                bytesPerSignature = 2;
            }
            else
            if (signature[0] == 0xFF && signature[1] == 0xFE)
            {
                enc = Encoding.Unicode;//UTF16 LE
                bytesPerSignature = 2;
            }

            fs.Seek(bytesPerSignature, SeekOrigin.Begin);

            return enc;
        }

        public void CloseFile()
        {
            if(fs!=null)
                fs.Dispose();
            fs = null;
        }

        public override void SaveToFile(string fileName, Encoding enc)
        {
            //
            var newLinePos = new List<int>(Count);
            //create temp file
            var dir = Path.GetDirectoryName(fileName);
            var tempFileName = Path.Combine(dir, Path.GetFileNameWithoutExtension(fileName) + ".tmp");

            StreamReader sr = new StreamReader(fs, fileEncoding);
            using (FileStream tempFs = new FileStream(tempFileName, FileMode.Create))
            using (StreamWriter sw = new StreamWriter(tempFs, enc))
            {
                sw.Flush();

                for (int i = 0; i < Count; i++)
                {
                    newLinePos.Add((int)tempFs.Length);

                    var sourceLine = ReadLine(sr, i);//read line from source file
                    string line;

                    bool lineIsChanged = lines[i] != null && lines[i].IsChanged;

                    if (lineIsChanged)
                        line = lines[i].Text;
                    else
                        line = sourceLine;

                    //call event handler
                    if (LinePushed != null)
                    {
                        var args = new LinePushedEventArgs(sourceLine, i, lineIsChanged ? line : null);
                        LinePushed(this, args);

                        if(args.SavedText != null)
                            line = args.SavedText;
                    }

                    //save line to file
                    if (i == Count - 1)
                        sw.Write(line);
                    else
                        sw.WriteLine(line);

                    sw.Flush();
                }
            }

            //clear lines buffer
            for (int i = 0; i < Count; i++)
                lines[i] = null;
            //deattach from source file
            sr.Dispose();
            fs.Dispose();
            //delete target file
            if (File.Exists(fileName))
                File.Delete(fileName);
            //rename temp file
            File.Move(tempFileName, fileName);

            //binding to new file
            sourceFileLinePositions = newLinePos;
            fs = new FileStream(fileName, FileMode.Open);
            this.fileEncoding = enc;
        }

        private string ReadLine(StreamReader sr, int i)
        {
            string line;
            var filePos = sourceFileLinePositions[i];
            if (filePos < 0)
                return "";
            fs.Seek(filePos, SeekOrigin.Begin);
            sr.DiscardBufferedData();
            line = sr.ReadLine();
            return line;
        }

        public override void ClearIsChanged()
        {
            foreach (var line in lines)
                if(line!=null)
                    line.IsChanged = false;
        }

        public override Line this[int i]
        {
            get 
            {
                if (base.lines[i] != null)
                    return lines[i];
                else
                    LoadLineFromSourceFile(i);

                return lines[i];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        private void LoadLineFromSourceFile(int i)
        {
            var line = CreateLine();
            fs.Seek(sourceFileLinePositions[i], SeekOrigin.Begin);
            StreamReader sr = new StreamReader(fs, fileEncoding);

            var s = sr.ReadLine();
            if (s == null)
                s = "";

            //call event handler
            if(LineNeeded!=null)
            {
                var args = new LineNeededEventArgs(s, i);
                LineNeeded(this, args);
                s = args.DisplayedLineText;
                if (s == null)
                    return;
            }

            foreach (var c in s)
                line.Add(new Char(c));
            base.lines[i] = line;
        }

        public override void InsertLine(int index, Line line)
        {
            sourceFileLinePositions.Insert(index, -1);
            base.InsertLine(index, line);
        }

        public override void RemoveLine(int index, int count)
        {
            sourceFileLinePositions.RemoveRange(index, count);
            base.RemoveLine(index, count);
        }

        public override void Clear()
        {
            base.Clear();
        }

        public override int GetLineLength(int i)
        {
            if (base.lines[i] == null)
                return 0;
            else
                return base.lines[i].Count;
        }

        public override bool LineHasFoldingStartMarker(int iLine)
        {
            if (lines[iLine] == null)
                return false;
            else
                return !string.IsNullOrEmpty(lines[iLine].FoldingStartMarker);
        }

        public override bool LineHasFoldingEndMarker(int iLine)
        {
            if (lines[iLine] == null)
                return false;
            else
                return !string.IsNullOrEmpty(lines[iLine].FoldingEndMarker);
        }

        public override void Dispose()
        {
            if (fs != null)
                fs.Dispose();

            timer.Dispose();
        }

        internal void UnloadLine(int iLine)
        {
            if (lines[iLine] != null && !lines[iLine].IsChanged)
                lines[iLine] = null;
        }
    }

    public class LineNeededEventArgs : EventArgs
    {
        public string SourceLineText { get; private set; }
        public int DisplayedLineIndex { get; private set; }
        /// <summary>
        /// This text will be displayed in textbox
        /// </summary>
        public string DisplayedLineText { get; set; }

        public LineNeededEventArgs(string sourceLineText, int displayedLineIndex)
        {
            this.SourceLineText = sourceLineText;
            this.DisplayedLineIndex = displayedLineIndex;
            this.DisplayedLineText = sourceLineText;
        }
    }

    public class LinePushedEventArgs : EventArgs
    {
        public string SourceLineText { get; private set; }
        public int DisplayedLineIndex { get; private set; }
        /// <summary>
        /// This property contains only changed text.
        /// If text of line is not changed, this property contains null.
        /// </summary>
        public string DisplayedLineText { get; private set; }
        /// <summary>
        /// This text will be saved in the file
        /// </summary>
        public string SavedText { get; set; }

        public LinePushedEventArgs(string sourceLineText, int displayedLineIndex, string displayedLineText)
        {
            this.SourceLineText = sourceLineText;
            this.DisplayedLineIndex = displayedLineIndex;
            this.DisplayedLineText = displayedLineText;
            this.SavedText = displayedLineText;
        }
    }
}

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
My blog.

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