protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { int currentPosition = base.SelectionStart; // get indexs int previousWordIndex = _SpellChecker.GetWordIndexFromTextIndex(currentPosition - 1); CurrentWordIndex = _SpellChecker.GetWordIndexFromTextIndex(currentPosition); // set current word to spell check _SpellChecker.WordIndex = previousWordIndex; if (_SpellChecker.CurrentWord.Length > 0) { Point start = base.GetPositionFromCharIndex(_SpellChecker.TextIndex); Point end = base.GetPositionFromCharIndex(_SpellChecker.TextIndex + _SpellChecker.CurrentWord.Length); this.DrawUnderline(e.Graphics, start, end); } base.OnPaint(e); }
protected override void OnTextChanged(EventArgs e) { // get change size int changeSize = this.Text.Length - PreviousTextLength; PreviousTextLength = this.Text.Length; // sync spell checker text with text box text _SpellChecker.Text = base.Text; int currentPosition = base.SelectionStart; // get indexs int previousWordIndex = _SpellChecker.GetWordIndexFromTextIndex(currentPosition - changeSize); CurrentWordIndex = _SpellChecker.GetWordIndexFromTextIndex(currentPosition); // set current word to spell check _SpellChecker.WordIndex = previousWordIndex; // get the end index of previous word with out white space int wordEndIndex = _SpellChecker.TextIndex + _SpellChecker.CurrentWord.Length; TraceWriter.TraceVerbose("ChangeSize:{0}; PreviousWord:{1}; CurrentWord:{2}; Position:{3}; WordEnd:{4};", changeSize, previousWordIndex, CurrentWordIndex, currentPosition, wordEndIndex); if (previousWordIndex != CurrentWordIndex || wordEndIndex < currentPosition) { // if word indexs not equal, spell check all words from previousWordIndex to CurrentWordIndex // or if word indexs equal, spell check if caret in white space this.MarkMisspelledWords(previousWordIndex, CurrentWordIndex); UncheckedWordIndex = -1; } else { UncheckedWordIndex = previousWordIndex; } base.OnTextChanged(e); }
public void GetWordIndexFromTextIndex() { Spelling _SpellChecker = NewSpellChecker(); _SpellChecker.Text = "This is a test "; Assert.AreEqual(0, _SpellChecker.GetWordIndexFromTextIndex(1)); Assert.AreEqual(0, _SpellChecker.GetWordIndexFromTextIndex(4)); Assert.AreEqual(1, _SpellChecker.GetWordIndexFromTextIndex(5)); Assert.AreEqual(2, _SpellChecker.GetWordIndexFromTextIndex(9)); Assert.AreEqual(3, _SpellChecker.GetWordIndexFromTextIndex(12)); Assert.AreEqual(3, _SpellChecker.GetWordIndexFromTextIndex(15)); Assert.AreEqual(3, _SpellChecker.GetWordIndexFromTextIndex(20)); }
public void GetWordIndexFromTextIndex() { Spelling _SpellChecker = NewSpellChecker(); _SpellChecker.Text = "This is a test "; Assert.True(0 == _SpellChecker.GetWordIndexFromTextIndex(1)); Assert.True(0 == _SpellChecker.GetWordIndexFromTextIndex(4)); Assert.True(1 == _SpellChecker.GetWordIndexFromTextIndex(5)); Assert.True(2 == _SpellChecker.GetWordIndexFromTextIndex(9)); Assert.True(3 == _SpellChecker.GetWordIndexFromTextIndex(12)); Assert.True(3 == _SpellChecker.GetWordIndexFromTextIndex(15)); Assert.True(3 == _SpellChecker.GetWordIndexFromTextIndex(20)); }
private void SpellCheckContextMenuOpening(object sender, CancelEventArgs e) { SpellCheckContextMenu.Items.Clear(); try { var pos = TextBox.GetCharIndexFromPosition(TextBox.PointToClient(MousePosition)); if (pos <= 0) { e.Cancel = true; return; } _spelling.Text = TextBox.Text; _spelling.WordIndex = _spelling.GetWordIndexFromTextIndex(pos); _spelling.ShowDialog = false; _spelling.MaxSuggestions = 10; //generate suggestions _spelling.Suggest(); var addToDictionary = SpellCheckContextMenu.Items.Add(addToDictionaryText.Text); addToDictionary.Click += AddToDictionaryClick; var ignoreWord = SpellCheckContextMenu.Items.Add(ignoreWordText.Text); ignoreWord.Click += IgnoreWordClick; var removeWord = SpellCheckContextMenu.Items.Add(removeWordText.Text); removeWord.Click += RemoveWordClick; SpellCheckContextMenu.Items.Add(new ToolStripSeparator()); bool suggestionsFound = false; foreach (var suggestion in (string[])_spelling.Suggestions.ToArray(typeof(string))) { var suggestionToolStripItem = SpellCheckContextMenu.Items.Add(suggestion); suggestionToolStripItem.Click += SuggestionToolStripItemClick; suggestionsFound = true; } if (suggestionsFound) { SpellCheckContextMenu.Items.Add(new ToolStripSeparator()); } } catch (Exception ex) { Trace.WriteLine(ex); } if (!string.IsNullOrEmpty(_spelling.CurrentWord)) { var translate = new ToolStripMenuItem(string.Format(translateCurrentWord.Text, _spelling.CurrentWord, CultureCodeToString(Settings.Dictionary))); translate.Click += translate_Click; SpellCheckContextMenu.Items.Add(translate); } var translateText = new ToolStripMenuItem(string.Format(translateEntireText.Text, CultureCodeToString(Settings.Dictionary))); translateText.Click += translateText_Click; SpellCheckContextMenu.Items.Add(translateText); SpellCheckContextMenu.Items.Add(new ToolStripSeparator()); try { var dictionaryToolStripMenuItem = new ToolStripMenuItem(dictionaryText.Text); SpellCheckContextMenu.Items.Add(dictionaryToolStripMenuItem); var toolStripDropDown = new ContextMenuStrip(); var noDicToolStripMenuItem = new ToolStripMenuItem("None"); noDicToolStripMenuItem.Click += DicToolStripMenuItemClick; if (Settings.Dictionary == "None") { noDicToolStripMenuItem.Checked = true; } toolStripDropDown.Items.Add(noDicToolStripMenuItem); foreach ( var fileName in Directory.GetFiles(Settings.GetDictionaryDir(), "*.dic", SearchOption.TopDirectoryOnly)) { var file = new FileInfo(fileName); var dic = file.Name.Replace(".dic", ""); var dicToolStripMenuItem = new ToolStripMenuItem(dic); dicToolStripMenuItem.Click += DicToolStripMenuItemClick; if (Settings.Dictionary == dic) { dicToolStripMenuItem.Checked = true; } toolStripDropDown.Items.Add(dicToolStripMenuItem); } dictionaryToolStripMenuItem.DropDown = toolStripDropDown; } catch (Exception ex) { Trace.WriteLine(ex); } SpellCheckContextMenu.Items.Add(new ToolStripSeparator()); var mi = new ToolStripMenuItem(markIllFormedLinesText.Text) { Checked = Settings.MarkIllFormedLinesInCommitMsg }; mi.Click += MarkIllFormedLinesInCommitMsgClick; SpellCheckContextMenu.Items.Add(mi); }
private void SpellCheckContextMenuOpening(object sender, CancelEventArgs e) { TextBox.Focus(); SpellCheckContextMenu.Items.Clear(); try { var pos = TextBox.GetCharIndexFromPosition(TextBox.PointToClient(MousePosition)); if (pos < 0) { e.Cancel = true; return; } _spelling.Text = TextBox.Text; _spelling.WordIndex = _spelling.GetWordIndexFromTextIndex(pos); if (_spelling.CurrentWord.Length != 0 && !_spelling.TestWord()) { _spelling.ShowDialog = false; _spelling.MaxSuggestions = 5; //generate suggestions _spelling.Suggest(); foreach (var suggestion in _spelling.Suggestions) { var si = AddContextMenuItem(suggestion, SuggestionToolStripItemClick); si.Font = new System.Drawing.Font(si.Font, FontStyle.Bold); } AddContextMenuItem(addToDictionaryText.Text, AddToDictionaryClick) .Enabled = (_spelling.CurrentWord.Length > 0); AddContextMenuItem(ignoreWordText.Text, IgnoreWordClick) .Enabled = (_spelling.CurrentWord.Length > 0); AddContextMenuItem(removeWordText.Text, RemoveWordClick) .Enabled = (_spelling.CurrentWord.Length > 0); if (_spelling.Suggestions.Count > 0) { AddContextMenuSeparator(); } } } catch (Exception ex) { Trace.WriteLine(ex); } AddContextMenuItem(cutMenuItemText.Text, CutMenuItemClick) .Enabled = (TextBox.SelectedText.Length > 0); AddContextMenuItem(copyMenuItemText.Text, CopyMenuItemdClick) .Enabled = (TextBox.SelectedText.Length > 0); AddContextMenuItem(pasteMenuItemText.Text, PasteMenuItemClick) .Enabled = Clipboard.ContainsText(); AddContextMenuItem(deleteMenuItemText.Text, DeleteMenuItemClick) .Enabled = (TextBox.SelectedText.Length > 0); AddContextMenuItem(selectAllMenuItemText.Text, SelectAllMenuItemClick); /*AddContextMenuSeparator(); * * if (!string.IsNullOrEmpty(_spelling.CurrentWord)) * { * string text = string.Format(translateCurrentWord.Text, _spelling.CurrentWord, CultureCodeToString(Settings.Dictionary)); * AddContextMenuItem(text, translate_Click); * } * * string entireText = string.Format(translateEntireText.Text, CultureCodeToString(Settings.Dictionary)); * AddContextMenuItem(entireText, translateText_Click);*/ AddContextMenuSeparator(); try { var dictionaryToolStripMenuItem = new ToolStripMenuItem(dictionaryText.Text); SpellCheckContextMenu.Items.Add(dictionaryToolStripMenuItem); var toolStripDropDown = new ContextMenuStrip(); var noDicToolStripMenuItem = new ToolStripMenuItem("None"); noDicToolStripMenuItem.Click += DicToolStripMenuItemClick; if (Settings.Dictionary == "None") { noDicToolStripMenuItem.Checked = true; } toolStripDropDown.Items.Add(noDicToolStripMenuItem); foreach ( var fileName in Directory.GetFiles(AppSettings.GetDictionaryDir(), "*.dic", SearchOption.TopDirectoryOnly)) { var file = new FileInfo(fileName); var dic = file.Name.Replace(".dic", ""); var dicToolStripMenuItem = new ToolStripMenuItem(dic); dicToolStripMenuItem.Click += DicToolStripMenuItemClick; if (Settings.Dictionary == dic) { dicToolStripMenuItem.Checked = true; } toolStripDropDown.Items.Add(dicToolStripMenuItem); } dictionaryToolStripMenuItem.DropDown = toolStripDropDown; } catch (Exception ex) { Trace.WriteLine(ex); } AddContextMenuSeparator(); var mi = new ToolStripMenuItem(markIllFormedLinesText.Text) { Checked = AppSettings.MarkIllFormedLinesInCommitMsg }; mi.Click += MarkIllFormedLinesInCommitMsgClick; SpellCheckContextMenu.Items.Add(mi); }