diff --git a/src/Forms/Main.cs b/src/Forms/Main.cs index ab1b6fad4..1bb1f2feb 100644 --- a/src/Forms/Main.cs +++ b/src/Forms/Main.cs @@ -82,6 +82,8 @@ namespace Nikse.SubtitleEdit.Forms WaveFormUnDocked _waveFormUnDocked = null; VideoControlsUndocked _videoControlsUnDocked = null; + bool _cancelWordSpellCheck = false; + private bool AutoRepeatContinueOn { get @@ -2767,7 +2769,7 @@ namespace Nikse.SubtitleEdit.Forms } private void SpellCheckViaWord() - { + { if (_subtitle == null | _subtitle.Paragraphs.Count == 0) return; @@ -2776,24 +2778,27 @@ namespace Nikse.SubtitleEdit.Forms try { wordSpellChecker = new WordSpellChecker(); + wordSpellChecker.NewDocument(); } catch { MessageBox.Show(_language.UnableToStartWord); - //Configuration.Settings.General.SpellChecker = "hunspell"; ??? return; } - string version = wordSpellChecker.Version; - int index = 1; + string version = wordSpellChecker.Version; + + int index = FirstSelectedIndex; + if (index < 0) + index = 0; + + _cancelWordSpellCheck = false; foreach (Paragraph p in _subtitle.Paragraphs) { int errorsBefore; int errorsAfter; - wordSpellChecker.NewDocument(); ShowStatus(string.Format(_language.SpellChekingViaWordXLineYOfX, version, index, _subtitle.Paragraphs.Count.ToString())); SubtitleListview1.SelectIndexAndEnsureVisible(index - 1); string newText = wordSpellChecker.CheckSpelling(p.Text, out errorsBefore, out errorsAfter); - wordSpellChecker.CloseDocument(); if (errorsAfter > 0) { wordSpellChecker.Quit(); @@ -2806,9 +2811,14 @@ namespace Nikse.SubtitleEdit.Forms } totalCorrections += (errorsBefore - errorsAfter); index++; + + if (_cancelWordSpellCheck) + break; } + wordSpellChecker.CloseDocument(); wordSpellChecker.Quit(); ShowStatus(string.Format(_language.SpellCheckCompletedXCorrections, totalCorrections)); + Cursor = Cursors.Default; } private void SpellCheck(bool autoDetect) @@ -5126,6 +5136,11 @@ namespace Nikse.SubtitleEdit.Forms InsertAfter(); e.SuppressKeyPress = true; } + else if (e.KeyCode == Keys.Escape) + { + _cancelWordSpellCheck = true; + } + //else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.D) //{ // InsertAfter(); diff --git a/src/Forms/Settings.cs b/src/Forms/Settings.cs index ad85457c4..b027b7845 100644 --- a/src/Forms/Settings.cs +++ b/src/Forms/Settings.cs @@ -181,7 +181,7 @@ namespace Nikse.SubtitleEdit.Forms labelSubtitleFontSize.Text = language.SubtitleFontSize; labelSubtitleFontColor.Text = language.SubtitleFontColor; labelSubtitleFontBackgroundColor.Text = language.SubtitleBackgroundColor; - labelSpellChecker.Text = language.SubtitleBackgroundColor; + labelSpellChecker.Text = language.SpellChecker; checkBoxSubtitleFontBold.Text = Configuration.Settings.Language.General.Bold; checkBoxRememberRecentFiles.Text = language.RememberRecentFiles; checkBoxReopenLastOpened.Text = language.StartWithLastFileLoaded; diff --git a/src/Logic/WordLateBound.cs b/src/Logic/WordSpellChecker.cs similarity index 76% rename from src/Logic/WordLateBound.cs rename to src/Logic/WordSpellChecker.cs index 0a9977912..471c491e0 100644 --- a/src/Logic/WordLateBound.cs +++ b/src/Logic/WordSpellChecker.cs @@ -18,8 +18,8 @@ namespace Nikse.SubtitleEdit.Logic { _wordApplicationType = System.Type.GetTypeFromProgID("Word.Application"); _wordApplication = Activator.CreateInstance(_wordApplicationType); - _wordApplicationType.InvokeMember("Top", BindingFlags.SetProperty, null, _wordApplication, new object[] { -1000 }); // hide window - it's a hack - _wordApplicationType.InvokeMember("Visible", BindingFlags.SetProperty, null, _wordApplication, new object[] { true }); // set visible to true - otherwise it will appear in the background + _wordApplicationType.InvokeMember("WindowState", BindingFlags.SetProperty, null, _wordApplication, new object[] { 0 }); // 0 == ? + _wordApplicationType.InvokeMember("Top", BindingFlags.SetProperty, null, _wordApplication, new object[] { -10000 }); // hide window - it's a hack } public void NewDocument() @@ -32,7 +32,7 @@ namespace Nikse.SubtitleEdit.Logic { object saveChanges = false; object p = Missing.Value; - _wordDocumentType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, _wordDocument, new object[] { saveChanges, p, p }); + _wordDocumentType.InvokeMember("Close", BindingFlags.InvokeMethod, null, _wordDocument, new object[] { saveChanges, p, p }); } public string Version @@ -49,7 +49,7 @@ namespace Nikse.SubtitleEdit.Logic object saveChanges = false; object originalFormat = Missing.Value; object routeDocument = Missing.Value; - _wordApplicationType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, _wordApplication, new object[] { saveChanges, originalFormat, routeDocument }); + _wordApplicationType.InvokeMember("Quit", BindingFlags.InvokeMethod, null, _wordApplication, new object[] { saveChanges, originalFormat, routeDocument }); try { System.Runtime.InteropServices.Marshal.ReleaseComObject(_wordDocument); @@ -74,12 +74,13 @@ namespace Nikse.SubtitleEdit.Logic object spellingErrorsCount = spellingErrors.GetType().InvokeMember("Count", BindingFlags.GetProperty, null, spellingErrors, null); errorsBefore = int.Parse(spellingErrorsCount.ToString()); System.Runtime.InteropServices.Marshal.ReleaseComObject(spellingErrors); - + // perform spell check object p = Missing.Value; + _wordApplicationType.InvokeMember("Top", BindingFlags.SetProperty, null, _wordApplication, new object[] { -10000 }); // hide window - it's a hack _wordApplicationType.InvokeMember("Visible", BindingFlags.SetProperty, null, _wordApplication, new object[] { true }); // set visible to true - otherwise it will appear in the background _wordDocumentType.InvokeMember("CheckSpelling", BindingFlags.InvokeMethod, null, _wordDocument, new Object[] { p, p, p, p, p, p, p, p, p, p, p, p }); // 12 parameters -// _wordApplicationType.InvokeMember("Top", BindingFlags.SetProperty, null, _wordApplication, new object[] { -1000 }); // hide window - it's a hack + // spell check error count spellingErrors = _wordDocumentType.InvokeMember("SpellingErrors", BindingFlags.GetProperty, null, _wordDocument, null); @@ -88,13 +89,12 @@ namespace Nikse.SubtitleEdit.Logic System.Runtime.InteropServices.Marshal.ReleaseComObject(spellingErrors); // Get spellcheck text - object first = 0; - object characters = _wordDocumentType.InvokeMember("Characters", BindingFlags.GetProperty, null, _wordDocument, null); - object charactersCount = characters.GetType().InvokeMember("Count", BindingFlags.GetProperty, null, characters, null); - object last = int.Parse(charactersCount.ToString()) - 1; - System.Runtime.InteropServices.Marshal.ReleaseComObject(characters); - object resultText = range.GetType().InvokeMember("Text", BindingFlags.GetProperty, null, range, null); + range.GetType().InvokeMember("Delete", BindingFlags.InvokeMethod, null, range, null); + + System.Runtime.InteropServices.Marshal.ReleaseComObject(words); + System.Runtime.InteropServices.Marshal.ReleaseComObject(range); + return resultText.ToString().TrimEnd(); // result needs a trimming at the end } diff --git a/src/SubtitleEdit.csproj b/src/SubtitleEdit.csproj index 32e9d47d2..6c96f6daf 100644 --- a/src/SubtitleEdit.csproj +++ b/src/SubtitleEdit.csproj @@ -561,7 +561,7 @@ - +