/// <summary>
        /// Creates a new SQL (default) Scintilla editor with highlighting
        /// </summary>
        /// <param name="commandFactory">Unless your control is going to be 100% ReadOnly then you should supply an <see cref="ICombineableFactory"/> to allow dragging and
        /// dropping components into the window.  The <see cref="ICombineableFactory"/> will decide whether the given object can be translated into an <see cref="ICombineToMakeCommand"/> and hence into a unique bit of SQL
        /// to add to the editor</param>
        /// <param name="language">Determines highlighting, options include mssql,csharp or null</param>
        /// <param name="syntaxHelper"></param>
        /// <param name="spellCheck"></param>
        /// <param name="lineNumbers"></param>
        /// <param name="currentDirectory"></param>
        /// <returns></returns>
        public Scintilla Create(ICombineableFactory commandFactory = null, SyntaxLanguage language = SyntaxLanguage.SQL, IQuerySyntaxHelper syntaxHelper = null, bool spellCheck = false, bool lineNumbers = true, string currentDirectory = null)
        {
            var toReturn = new Scintilla();

            toReturn.Dock       = DockStyle.Fill;
            toReturn.HScrollBar = true;
            toReturn.VScrollBar = true;

            if (lineNumbers)
            {
                toReturn.Margins[0].Width = 40; //allows display of line numbers
            }
            else
            {
                foreach (var margin in toReturn.Margins)
                {
                    margin.Width = 0;
                }
            }

            toReturn.ClearCmdKey(Keys.Control | Keys.S); //prevent Ctrl+S displaying ascii code
            toReturn.ClearCmdKey(Keys.Control | Keys.R); //prevent Ctrl+R displaying ascii code
            toReturn.ClearCmdKey(Keys.Control | Keys.W); //prevent Ctrl+W displaying ascii code

            switch (language)
            {
            case SyntaxLanguage.SQL:
                SetSQLHighlighting(toReturn, syntaxHelper);
                break;

            case SyntaxLanguage.CSharp:
                SetCSharpHighlighting(toReturn);
                break;

            case SyntaxLanguage.XML:
                SetLexerEnumHighlighting(toReturn, Lexer.Xml);
                break;

            case SyntaxLanguage.LogFile:
                SetLexerEnumHighlighting(toReturn, Lexer.Verilog);
                break;
            }

            if (commandFactory != null)
            {
                toReturn.AllowDrop  = true;
                toReturn.DragEnter += (s, e) => OnDragEnter(s, e, commandFactory);
                toReturn.DragDrop  += (s, e) => OnDragDrop(s, e, commandFactory);
            }

            toReturn.WrapMode = (WrapMode)UserSettings.WrapMode;
            var scintillaMenu = new ScintillaMenu(toReturn, spellCheck);

            toReturn.ContextMenuStrip = scintillaMenu;

            try
            {
                if (spellCheck)
                {
                    string aff;
                    string dic;

                    if (currentDirectory == null)
                    {
                        aff = "en_us.aff";
                        dic = "en_us.dic";
                    }
                    else
                    {
                        aff = Path.Combine(currentDirectory, "en_us.aff");
                        dic = Path.Combine(currentDirectory, "en_us.dic");
                    }

                    var hunspell = new Hunspell(aff, dic);

                    DateTime lastCheckedSpelling = DateTime.MinValue;

                    toReturn.KeyPress += (s, e) =>
                    {
                        if (DateTime.Now.Subtract(lastCheckedSpelling) > TimeSpan.FromSeconds(10))
                        {
                            lastCheckedSpelling = DateTime.Now;
                            CheckSpelling((Scintilla)s, hunspell);
                        }
                    };

                    toReturn.Leave        += (s, e) => CheckSpelling((Scintilla)s, hunspell);
                    toReturn.Disposed     += (s, e) => scintilla_Disposed(s, e, hunspell);
                    scintillaMenu.Hunspell = hunspell;
                }
            }
            catch (Exception e)
            {
                if (!DictionaryExceptionShown)
                {
                    ExceptionViewer.Show("Could not load dictionary", e);
                    DictionaryExceptionShown = true;
                }
            }

            return(toReturn);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a new SQL (default) Scintilla editor with highlighting
        /// </summary>
        /// <param name="commandFactory">Unless your control is going to be 100% ReadOnly then you should supply an <see cref="ICombineableFactory"/> to allow dragging and
        /// dropping components into the window.  The <see cref="ICombineableFactory"/> will decide whether the given object can be translated into an <see cref="ICombineToMakeCommand"/> and hence into a unique bit of SQL
        /// to add to the editor</param>
        /// <param name="language">Determines highlighting, options include mssql,csharp or null</param>
        /// <param name="syntaxHelper"></param>
        /// <param name="spellCheck"></param>
        /// <param name="lineNumbers"></param>
        /// <param name="currentDirectory"></param>
        /// <returns></returns>
        public Scintilla Create(ICombineableFactory commandFactory = null, string language = "mssql", IQuerySyntaxHelper syntaxHelper = null, bool spellCheck = false, bool lineNumbers = true, string currentDirectory = null)
        {
            var toReturn = new Scintilla();

            toReturn.Dock       = DockStyle.Fill;
            toReturn.HScrollBar = true;
            toReturn.VScrollBar = true;

            if (lineNumbers)
            {
                toReturn.Margins[0].Width = 40; //allows display of line numbers
            }
            else
            {
                foreach (var margin in toReturn.Margins)
                {
                    margin.Width = 0;
                }
            }

            toReturn.ClearCmdKey(Keys.Control | Keys.S); //prevent Ctrl+S displaying ascii code
            toReturn.ClearCmdKey(Keys.Control | Keys.R); //prevent Ctrl+R displaying ascii code
            toReturn.ClearCmdKey(Keys.Control | Keys.W); //prevent Ctrl+W displaying ascii code

            if (language == "mssql")
            {
                SetSQLHighlighting(toReturn, syntaxHelper);
            }

            if (language == "csharp")
            {
                SetCSharpHighlighting(toReturn);
            }

            if (language == "xml")
            {
                SetLexerEnumHighlighting(toReturn, Lexer.Xml);
            }

            if (commandFactory != null)
            {
                toReturn.AllowDrop  = true;
                toReturn.DragEnter += (s, e) => OnDragEnter(s, e, commandFactory);
                toReturn.DragDrop  += (s, e) => OnDragDrop(s, e, commandFactory);
            }

            toReturn.WrapMode = (WrapMode)UserSettings.WrapMode;
            var scintillaMenu = new ScintillaMenu(toReturn);

            toReturn.ContextMenuStrip = scintillaMenu;

            try
            {
                if (spellCheck)
                {
                    string aff;
                    string dic;

                    if (currentDirectory == null)
                    {
                        aff = "en_us.aff";
                        dic = "en_us.dic";
                    }
                    else
                    {
                        aff = Path.Combine(currentDirectory, "en_us.aff");
                        dic = Path.Combine(currentDirectory, "en_us.dic");
                    }

                    var hunspell = new Hunspell(aff, dic);
                    toReturn.TextChanged  += (s, e) => scintilla_TextChanged(s, e, hunspell);
                    toReturn.Disposed     += (s, e) => scintilla_Disposed(s, e, hunspell);
                    scintillaMenu.Hunspell = hunspell;
                }
            }
            catch (Exception e)
            {
                if (!DictionaryExceptionShown)
                {
                    ExceptionViewer.Show("Could not load dictionary", e);
                    DictionaryExceptionShown = true;
                }
            }

            return(toReturn);
        }