Completed
Last Updated: 23 Apr 2014 12:56 by ADMIN
ADMIN
Plamen
Created on: 04 Jan 2013 07:48
Category: Editors
Type: Feature Request
2
IMPROVE. RadTextBoxControl - add spell checking feature for the RadTextBoxControl
[C#] [Export(typeof(IControlSpellChecker))] class MyTextBoxControlSpellChecker : ControlSpellCheckerBase { #region Constants private static readonly char[] WordPartSeparators = new char[] { '-', '–', '’', '\\', '/', '|', '=', '*', '(', ')' }; #endregion #region Fields private RadTextBoxControl textBox; #endregion #region Constructors public MyTextBoxControlSpellChecker() { } #endregion #region Properties public override Control CurrentControl { get { return this.textBox; } set { this.textBox = (RadTextBoxControl)value; } } #endregion #region Methods public override void ChangeCurrentWord(string suggestion) { int selectionStartPosition = textBox.SelectionStart; int selectionLength = textBox.SelectionLength; StringBuilder textBoxText = new StringBuilder(this.textBox.Text); textBoxText.Remove(selectionStartPosition, selectionLength); textBoxText.Insert(selectionStartPosition, suggestion); textBox.Text = textBoxText.ToString(); } public override IWordInfo MoveToNextError() { foreach (TextBoxWordInfo wordInfo in this.GetListOfWords()) { if (!this.CheckWordIsCorrect(wordInfo.Word)) { this.textBox.SelectionStart = wordInfo.StartPosition; this.textBox.SelectionLength = wordInfo.Word.Length; this.textBox.Focus(); return wordInfo; } } return null; } private IEnumerable<IWordInfo> GetListOfWords() { foreach (var wordItem in this.GetWordsFromText(this.textBox.Text)) { if (wordItem.Word == " " || wordItem.Word == Environment.NewLine || wordItem.Word == "\r" || wordItem.IsUppercase || wordItem.ContainsDigits) { continue; } int partIndex = 0; int startIndex = wordItem.IndexInText; foreach (string word in wordItem.Word.Split(WordPartSeparators, StringSplitOptions.None)) { string trimmedWord = TrimWordEnd(word); startIndex += TrimWordStart(ref trimmedWord); var wordInfo = new TextBoxWordInfo(trimmedWord, startIndex); if (trimmedWord.Length > 1) { yield return wordInfo; } startIndex += word.Length; startIndex++;//+1 for the separator partIndex++; } } } private IEnumerable<WordItem> GetWordsFromText(string text) { bool wordContainsDigits = false; List<WordItem> words = new List<WordItem>(text.Length / 7); StringBuilder currentWord = new StringBuilder(); bool onlyUpperLetters = true; int i = 0; for (i = 0; i < text.Length; i++) { if (text[i] == Environment.NewLine[0]) { if (currentWord.Length > 0) { words.Add(new WordItem(currentWord.ToString(), i - currentWord.Length, wordContainsDigits, onlyUpperLetters)); currentWord.Clear(); } words.Add(new WordItem(Environment.NewLine, i - currentWord.Length, wordContainsDigits, onlyUpperLetters)); if (i + 1 < text.Length && text[i + 1] == Environment.NewLine[1]) { i++; } wordContainsDigits = false; onlyUpperLetters = true; } else if (text[i] != '\'' && (text[i].ToString() == " " || text[i].ToString() == "\t" || Char.IsPunctuation(text[i]))) { if (currentWord.Length > 0) { words.Add(new WordItem(currentWord.ToString(), i - currentWord.Length, wordContainsDigits, onlyUpperLetters)); currentWord.Clear(); } words.Add(new WordItem(text[i].ToString(), i - currentWord.Length, wordContainsDigits, onlyUpperLetters)); wordContainsDigits = false; onlyUpperLetters = true; } else { char nextChar = text[i]; if (nextChar > '0' && nextChar < '9') { wordContainsDigits = true; } onlyUpperLetters = onlyUpperLetters && !char.IsLower(nextChar); currentWord.Append(text[i]); } } if (currentWord.Length > 0) { words.Add(new WordItem(currentWord.ToString(), i - currentWord.Length, wordContainsDigits, onlyUpperLetters)); } return words; } private int TrimWordStart(ref string word) { int index = 0; while (index < word.Length && !char.IsLetter(word[index])) { index++; } if (index == 0) { return index; } if (index < word.Length) { word = word.Substring(index); } else { word = string.Empty; } return index; } private string TrimWordEnd(string word) { int index = word.Length - 1; while (index >= 0 && !char.IsLetter(word[index])) { index--; } if (index >= 0) { return word.Substring(0, index + 1); } return string.Empty; } private bool CheckWordIsCorrect(string word) { //TODO: add built-in dictionary, to allow suggestions for these words if (string.CompareOrdinal(word, "Telerik") == 0 || string.CompareOrdinal(word, "Microsoft") == 0 || string.CompareOrdinal(word, "Silverlight") == 0 || string.CompareOrdinal(word, "RadRichTextBox") == 0 || string.CompareOrdinal(word, "RadControls") == 0) { return true; } if (this.IgnoredWords != null && this.IgnoredWords.ContainsWord(word)) { return true; } return base.SpellChecker.CheckWordIsCorrect(word); } public override RadDocument GetContentAsDocument() { TxtFormatProvider txtFormatProvider = new TxtFormatProvider(); RadDocument document = txtFormatProvider.Import(this.textBox.Text); return document; } public override void SetContentFromDocument(RadDocument document) { TxtFormatProvider txtFormatProvider = new TxtFormatProvider(); this.textBox.Text = txtFormatProvider.Export(document); } #endregion } internal struct WordItem { public string Word; public int IndexInText; public bool ContainsDigits; public bool IsUppercase; public WordItem(string word, int indexInText, bool containsDigits, bool isUppercase) { this.Word = word; this.IndexInText = indexInText; this.ContainsDigits = containsDigits; this.IsUppercase = isUppercase; } }
0 comments