Example #1
0
        protected override void OnTextChanged(EventArgs e)
        {
            Form1 form = (Form1)FindForm();

            if (form == null)
            {
                base.OnTextChanged(e);
                return;
            }

            if (!IsUndoingRedoing && RichTextBoxUtil.ContainsUnderlineText(this))
            {
                if (IsUnderlining)
                {
                    IsUnderlining = false;
                }
                else
                {
                    DictionaryManager.ClearTextCorrectness(this);
                }
            }
            if (!IsUndoingRedoing && RichTextBoxUtil.ContainsHighlightText(this))
            {
                if (IsHighlighting)
                {
                    IsHighlighting = false;
                }
                else
                {
                    StringUtil.ClearHighlightsResults(this);
                }
            }

            if (IsUndoingRedoing)
            {
                IsUndoingRedoing = false;
            }

            TabManager.TabTextChange(form);
            TextManager.RefreshUndoRedoExternal(form);
            base.OnTextChanged(e);
        }
Example #2
0
        internal static void CheckTextCorrectness(Form1 form, String languageSign)
        {
            if (!ExistsDictionary(languageSign))
            {
                WindowManager.ShowInfoBox(form, LanguageUtil.GetCurrentLanguageString("NoDictionary", className) + ConstantUtil.dtPadURL + "wikipage?title=Dictionaries");
                return;
            }

            CustomXtraTabControl pagesTabControl = form.pagesTabControl;
            CustomRichTextBox    pageTextBox     = ProgramUtil.GetPageTextBox(pagesTabControl.SelectedTabPage);

            if (RichTextBoxUtil.ContainsUnderlineText(pageTextBox))
            {
                ClearTextCorrectness(pageTextBox);
            }

            bool        textCorrectness = true;
            RichTextBox tempRichTextBox = new RichTextBox {
                BackColor = pageTextBox.BackColor
            };                                                                                   //Temporary RichTextBox to avoid too much undo/redo into buffer

            pageTextBox.SuspendPainting();
            tempRichTextBox.Rtf = pageTextBox.Rtf;

            //Method 1: char parse (more accurate, slower)
            using (Hunspell hunspell = new Hunspell(ConstantUtil.ApplicationExecutionPath() + "\\Languages\\" + languageSign + ".aff", ConstantUtil.ApplicationExecutionPath() + "\\Languages\\" + languageSign + ".dic"))
            {
                int           wordStart = 0;
                StringBuilder word      = new StringBuilder(String.Empty);
                StringBuilder text      = new StringBuilder(pageTextBox.Text);

                for (int i = 0; i < text.Length; i++)
                {
                    if (text[i] == '\'' || (!Char.IsWhiteSpace(text[i]) && !Char.IsPunctuation(text[i]) && !Char.IsSymbol(text[i]) && !Char.IsSeparator(text[i])))
                    {
                        word.Append(text[i]);

                        if (i < text.Length - 1)
                        {
                            continue;
                        }
                    }

                    if (!hunspell.Spell(word.ToString()) && !String.IsNullOrEmpty(word.ToString().Trim()))
                    {
                        tempRichTextBox.Select(wordStart, word.Length);
                        tempRichTextBox.SelectionFont  = new Font(pageTextBox.Font, FontStyle.Underline);
                        tempRichTextBox.SelectionColor = (pageTextBox.BackColor == Color.Red) ? Color.Yellow : Color.Red;
                        textCorrectness = false;
                    }

                    word      = new StringBuilder(String.Empty);
                    wordStart = i + 1;
                }
            }

            //Method 2: word parse (less accurate, faster)
            //String[] wordsList = pageTextBox.Text.Split(new[] { ' ', Convert.ToChar(ConstantUtil.newLine), '\t', '/', '\\', '.', '?', ';', ',', ':', '(', ')', '[', ']', '{', '}', '\\', '|', '/', '!', '"', '\'', '=' }, StringSplitOptions.RemoveEmptyEntries);
            //List<String> wordsAlreadySeen = new List<String>();

            //using (Hunspell hunspell = new Hunspell(ConstantUtil.ApplicationExecutionPath() + "\\Languages\\" + languageSign + ".aff", ConstantUtil.ApplicationExecutionPath() + "\\Languages\\" + languageSign + ".dic"))
            //{
            //    foreach (String word in wordsList)
            //    {
            //        if (wordsAlreadySeen.Contains(word))
            //        {
            //            continue;
            //        }
            //        wordsAlreadySeen.Add(word);

            //        if (hunspell.Spell(word))
            //        {
            //            continue;
            //        }

            //        int i = 0;
            //        while (i != -1 && (i = pageTextBox.Text.IndexOf(word, i)) != -1)
            //        {
            //            tempRichTextBox.Select(i, word.Length);
            //            tempRichTextBox.SelectionFont = new Font(pageTextBox.Font, FontStyle.Underline);
            //            tempRichTextBox.SelectionColor = (pageTextBox.BackColor == Color.Red) ? Color.Yellow : Color.Red;

            //            i += word.Length;
            //        }
            //    }
            //}

            pageTextBox.IsUnderlining = true;
            RichTextBoxUtil.ReplaceRTFContent(pageTextBox, tempRichTextBox);

            pageTextBox.ResumePainting();
            tempRichTextBox.Dispose();
            TextManager.RefreshUndoRedoExternal(form);

            if (textCorrectness)
            {
                WindowManager.ShowInfoBox(form, LanguageUtil.GetCurrentLanguageString("TextCorrect", className));
            }
        }