diff --git a/src/ui/Forms/FindDialog.Designer.cs b/src/ui/Forms/FindDialog.Designer.cs index d0ee2c2ff..81e76ffb9 100644 --- a/src/ui/Forms/FindDialog.Designer.cs +++ b/src/ui/Forms/FindDialog.Designer.cs @@ -30,7 +30,6 @@ { this.textBoxFind = new System.Windows.Forms.TextBox(); this.buttonFind = new System.Windows.Forms.Button(); - this.buttonCancel = new System.Windows.Forms.Button(); this.radioButtonNormal = new System.Windows.Forms.RadioButton(); this.radioButtonCaseSensitive = new System.Windows.Forms.RadioButton(); this.radioButtonRegEx = new System.Windows.Forms.RadioButton(); @@ -38,42 +37,34 @@ this.buttonCount = new System.Windows.Forms.Button(); this.labelCount = new System.Windows.Forms.Label(); this.checkBoxWholeWord = new System.Windows.Forms.CheckBox(); + this.buttonFindPrev = new System.Windows.Forms.Button(); + this.labelFindWhat = new System.Windows.Forms.Label(); this.SuspendLayout(); // // textBoxFind // - this.textBoxFind.Location = new System.Drawing.Point(12, 12); + this.textBoxFind.Location = new System.Drawing.Point(12, 28); this.textBoxFind.Name = "textBoxFind"; - this.textBoxFind.Size = new System.Drawing.Size(189, 21); + this.textBoxFind.Size = new System.Drawing.Size(232, 21); this.textBoxFind.TabIndex = 0; this.textBoxFind.TextChanged += new System.EventHandler(this.textBoxFind_TextChanged); this.textBoxFind.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBoxFind_KeyDown); // // buttonFind // - this.buttonFind.Location = new System.Drawing.Point(207, 11); + this.buttonFind.Location = new System.Drawing.Point(260, 28); this.buttonFind.Name = "buttonFind"; - this.buttonFind.Size = new System.Drawing.Size(89, 23); + this.buttonFind.Size = new System.Drawing.Size(122, 23); this.buttonFind.TabIndex = 1; - this.buttonFind.Text = "Find"; + this.buttonFind.Text = "Find next"; this.buttonFind.UseVisualStyleBackColor = true; this.buttonFind.Click += new System.EventHandler(this.ButtonFind_Click); // - // buttonCancel - // - this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.buttonCancel.Location = new System.Drawing.Point(207, 69); - this.buttonCancel.Name = "buttonCancel"; - this.buttonCancel.Size = new System.Drawing.Size(89, 23); - this.buttonCancel.TabIndex = 3; - this.buttonCancel.Text = "C&ancel"; - this.buttonCancel.UseVisualStyleBackColor = true; - // // radioButtonNormal // this.radioButtonNormal.AutoSize = true; this.radioButtonNormal.Checked = true; - this.radioButtonNormal.Location = new System.Drawing.Point(12, 63); + this.radioButtonNormal.Location = new System.Drawing.Point(12, 89); this.radioButtonNormal.Name = "radioButtonNormal"; this.radioButtonNormal.Size = new System.Drawing.Size(58, 17); this.radioButtonNormal.TabIndex = 5; @@ -85,7 +76,7 @@ // radioButtonCaseSensitive // this.radioButtonCaseSensitive.AutoSize = true; - this.radioButtonCaseSensitive.Location = new System.Drawing.Point(12, 86); + this.radioButtonCaseSensitive.Location = new System.Drawing.Point(12, 112); this.radioButtonCaseSensitive.Name = "radioButtonCaseSensitive"; this.radioButtonCaseSensitive.Size = new System.Drawing.Size(94, 17); this.radioButtonCaseSensitive.TabIndex = 7; @@ -96,7 +87,7 @@ // radioButtonRegEx // this.radioButtonRegEx.AutoSize = true; - this.radioButtonRegEx.Location = new System.Drawing.Point(12, 109); + this.radioButtonRegEx.Location = new System.Drawing.Point(12, 135); this.radioButtonRegEx.Name = "radioButtonRegEx"; this.radioButtonRegEx.Size = new System.Drawing.Size(56, 17); this.radioButtonRegEx.TabIndex = 9; @@ -107,19 +98,19 @@ // comboBoxFind // this.comboBoxFind.FormattingEnabled = true; - this.comboBoxFind.Location = new System.Drawing.Point(12, 13); + this.comboBoxFind.Location = new System.Drawing.Point(12, 28); this.comboBoxFind.Name = "comboBoxFind"; - this.comboBoxFind.Size = new System.Drawing.Size(189, 21); + this.comboBoxFind.Size = new System.Drawing.Size(232, 21); this.comboBoxFind.TabIndex = 0; this.comboBoxFind.TextChanged += new System.EventHandler(this.comboBoxFind_TextChanged); this.comboBoxFind.KeyDown += new System.Windows.Forms.KeyEventHandler(this.ComboBoxFind_KeyDown); // // buttonCount // - this.buttonCount.Location = new System.Drawing.Point(207, 40); + this.buttonCount.Location = new System.Drawing.Point(260, 86); this.buttonCount.Name = "buttonCount"; - this.buttonCount.Size = new System.Drawing.Size(89, 23); - this.buttonCount.TabIndex = 2; + this.buttonCount.Size = new System.Drawing.Size(122, 23); + this.buttonCount.TabIndex = 3; this.buttonCount.Text = "Count"; this.buttonCount.UseVisualStyleBackColor = true; this.buttonCount.Click += new System.EventHandler(this.buttonCount_Click); @@ -127,7 +118,7 @@ // labelCount // this.labelCount.AutoSize = true; - this.labelCount.Location = new System.Drawing.Point(204, 113); + this.labelCount.Location = new System.Drawing.Point(259, 112); this.labelCount.Name = "labelCount"; this.labelCount.Size = new System.Drawing.Size(40, 13); this.labelCount.TabIndex = 11; @@ -136,7 +127,7 @@ // checkBoxWholeWord // this.checkBoxWholeWord.AutoSize = true; - this.checkBoxWholeWord.Location = new System.Drawing.Point(12, 40); + this.checkBoxWholeWord.Location = new System.Drawing.Point(12, 61); this.checkBoxWholeWord.Name = "checkBoxWholeWord"; this.checkBoxWholeWord.Size = new System.Drawing.Size(83, 17); this.checkBoxWholeWord.TabIndex = 4; @@ -144,11 +135,32 @@ this.checkBoxWholeWord.UseVisualStyleBackColor = true; this.checkBoxWholeWord.CheckedChanged += new System.EventHandler(this.checkBoxWholeWord_CheckedChanged); // + // buttonFindPrev + // + this.buttonFindPrev.Location = new System.Drawing.Point(260, 57); + this.buttonFindPrev.Name = "buttonFindPrev"; + this.buttonFindPrev.Size = new System.Drawing.Size(122, 23); + this.buttonFindPrev.TabIndex = 2; + this.buttonFindPrev.Text = "Find prevoius"; + this.buttonFindPrev.UseVisualStyleBackColor = true; + this.buttonFindPrev.Click += new System.EventHandler(this.buttonFindPrev_Click); + // + // labelFindWhat + // + this.labelFindWhat.AutoSize = true; + this.labelFindWhat.Location = new System.Drawing.Point(12, 12); + this.labelFindWhat.Name = "labelFindWhat"; + this.labelFindWhat.Size = new System.Drawing.Size(58, 13); + this.labelFindWhat.TabIndex = 12; + this.labelFindWhat.Text = "Find what:"; + // // FindDialog // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(307, 135); + this.ClientSize = new System.Drawing.Size(394, 172); + this.Controls.Add(this.labelFindWhat); + this.Controls.Add(this.buttonFindPrev); this.Controls.Add(this.checkBoxWholeWord); this.Controls.Add(this.labelCount); this.Controls.Add(this.buttonCount); @@ -156,7 +168,6 @@ this.Controls.Add(this.radioButtonRegEx); this.Controls.Add(this.radioButtonCaseSensitive); this.Controls.Add(this.radioButtonNormal); - this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonFind); this.Controls.Add(this.textBoxFind); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); @@ -168,6 +179,7 @@ this.ShowInTaskbar = false; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent; this.Text = "Find"; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FindDialog_FormClosing); this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormFindDialog_KeyDown); this.ResumeLayout(false); this.PerformLayout(); @@ -178,7 +190,6 @@ private System.Windows.Forms.TextBox textBoxFind; private System.Windows.Forms.Button buttonFind; - private System.Windows.Forms.Button buttonCancel; private System.Windows.Forms.RadioButton radioButtonNormal; private System.Windows.Forms.RadioButton radioButtonCaseSensitive; private System.Windows.Forms.RadioButton radioButtonRegEx; @@ -186,5 +197,7 @@ private System.Windows.Forms.Button buttonCount; private System.Windows.Forms.Label labelCount; private System.Windows.Forms.CheckBox checkBoxWholeWord; + private System.Windows.Forms.Button buttonFindPrev; + private System.Windows.Forms.Label labelFindWhat; } } diff --git a/src/ui/Forms/FindDialog.cs b/src/ui/Forms/FindDialog.cs index 86cbcf158..29b9d4298 100644 --- a/src/ui/Forms/FindDialog.cs +++ b/src/ui/Forms/FindDialog.cs @@ -10,31 +10,36 @@ namespace Nikse.SubtitleEdit.Forms { public sealed partial class FindDialog : PositionAndSizeForm { + private readonly IFindAndReplace _findAndReplaceMethods; + private Regex _regEx; private readonly Subtitle _subtitle; - public FindDialog(Subtitle subtitle) + + public FindDialog(Subtitle subtitle, IFindAndReplace findAndReplaceMethods) { UiUtil.PreInitialize(this); InitializeComponent(); UiUtil.FixFonts(this); Text = LanguageSettings.Current.FindDialog.Title; - buttonFind.Text = LanguageSettings.Current.FindDialog.Find; + labelFindWhat.Text = LanguageSettings.Current.ReplaceDialog.FindWhat; + buttonFind.Text = LanguageSettings.Current.FindDialog.FindNext; + buttonFindPrev.Text = LanguageSettings.Current.FindDialog.FindPrevious; radioButtonNormal.Text = LanguageSettings.Current.FindDialog.Normal; radioButtonCaseSensitive.Text = LanguageSettings.Current.FindDialog.CaseSensitive; radioButtonRegEx.Text = LanguageSettings.Current.FindDialog.RegularExpression; - buttonCancel.Text = LanguageSettings.Current.General.Cancel; checkBoxWholeWord.Text = LanguageSettings.Current.FindDialog.WholeWord; buttonCount.Text = LanguageSettings.Current.FindDialog.Count; labelCount.Text = string.Empty; _subtitle = subtitle; + _findAndReplaceMethods = findAndReplaceMethods; if (Width < radioButtonRegEx.Right + 5) { Width = radioButtonRegEx.Right + 5; } - UiUtil.FixLargeFonts(this, buttonCancel); + UiUtil.FixLargeFonts(this, buttonFind); } private ReplaceType FindReplaceType @@ -98,12 +103,13 @@ namespace Nikse.SubtitleEdit.Forms if (e.KeyCode == Keys.Escape) { DialogResult = DialogResult.Cancel; + Close(); } } private void ButtonFind_Click(object sender, EventArgs e) { - string searchText = FindText; + var searchText = FindText; textBoxFind.Text = searchText; if (searchText.Length == 0) @@ -113,10 +119,12 @@ namespace Nikse.SubtitleEdit.Forms else if (radioButtonNormal.Checked) { DialogResult = DialogResult.OK; + _findAndReplaceMethods.FindDialogFind(FindText); } else if (radioButtonCaseSensitive.Checked) { DialogResult = DialogResult.OK; + _findAndReplaceMethods.FindDialogFind(FindText); } else if (radioButtonRegEx.Checked) { @@ -124,6 +132,7 @@ namespace Nikse.SubtitleEdit.Forms { _regEx = new Regex(RegexUtils.FixNewLine(searchText), RegexOptions.Compiled, TimeSpan.FromSeconds(5)); DialogResult = DialogResult.OK; + _findAndReplaceMethods.FindDialogFind(FindText); } catch (Exception exception) { @@ -239,5 +248,49 @@ namespace Nikse.SubtitleEdit.Forms labelCount.Text = string.Empty; } + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + this.Close(); + } + + private void FindDialog_FormClosing(object sender, FormClosingEventArgs e) + { + _findAndReplaceMethods.FindDialogClose(); + } + + private void buttonFindPrev_Click(object sender, EventArgs e) + { + var searchText = FindText; + textBoxFind.Text = searchText; + + if (searchText.Length == 0) + { + DialogResult = DialogResult.Cancel; + } + else if (radioButtonNormal.Checked) + { + DialogResult = DialogResult.OK; + _findAndReplaceMethods.FindDialogFindPrevious(); + } + else if (radioButtonCaseSensitive.Checked) + { + DialogResult = DialogResult.OK; + _findAndReplaceMethods.FindDialogFindPrevious(); + } + else if (radioButtonRegEx.Checked) + { + try + { + _regEx = new Regex(RegexUtils.FixNewLine(searchText), RegexOptions.Compiled, TimeSpan.FromSeconds(5)); + DialogResult = DialogResult.OK; + _findAndReplaceMethods.FindDialogFindPrevious(); + } + catch (Exception exception) + { + MessageBox.Show(exception.Message); + } + } + } } } diff --git a/src/ui/Forms/Main.cs b/src/ui/Forms/Main.cs index 574cc8c49..2e9bf6a8d 100644 --- a/src/ui/Forms/Main.cs +++ b/src/ui/Forms/Main.cs @@ -47,7 +47,7 @@ using Nikse.SubtitleEdit.Forms.AudioToText; namespace Nikse.SubtitleEdit.Forms { - public sealed partial class Main : Form, IReloadSubtitle + public sealed partial class Main : Form, IReloadSubtitle, IFindAndReplace { private class ComboBoxZoomItem { @@ -109,6 +109,8 @@ namespace Nikse.SubtitleEdit.Forms private DateTime _fileDateTime; private string _title; private FindReplaceDialogHelper _findHelper; + private FindDialog _findDialog; + private ReplaceDialog _replaceDialog; private int _replaceStartLineIndex; private bool _sourceViewChange; private int _changeSubtitleHash = -1; @@ -6228,82 +6230,34 @@ namespace Nikse.SubtitleEdit.Forms } } - using (var findDialog = new FindDialog(_subtitle)) - { - findDialog.SetIcon(toolStripButtonFind.Image as Bitmap); - findDialog.Initialize(selectedText, _findHelper); - if (findDialog.ShowDialog(this) != DialogResult.OK) - { - if (_findHelper != null) - { - _findHelper.InProgress = false; - } - - return; - } - - _findHelper = findDialog.GetFindDialogHelper(_subtitleListViewIndex); - _findHelper.InProgress = true; - if (!string.IsNullOrWhiteSpace(_findHelper.FindText)) - { - if (Configuration.Settings.Tools.FindHistory.Count == 0 || Configuration.Settings.Tools.FindHistory[0] != _findHelper.FindText) - { - Configuration.Settings.Tools.FindHistory.Insert(0, _findHelper.FindText); - } - } - - ShowStatus(string.Format(_language.SearchingForXFromLineY, _findHelper.FindText, _subtitleListViewIndex + 1)); - if (InListView) - { - var tb = GetFindReplaceTextBox(); - int startPos = tb.SelectedText.Length > 0 ? tb.SelectionStart + 1 : tb.SelectionStart; - bool found = _findHelper.Find(_subtitle, _subtitleOriginal, _subtitleListViewIndex, startPos); - tb = GetFindReplaceTextBox(); - // if we fail to find the text, we might want to start searching from the top of the file. - if (!found && _findHelper.StartLineIndex >= 1) - { - if (MessageBox.Show(_language.FindContinue, _language.FindContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes) - { - found = _findHelper.Find(_subtitle, _subtitleOriginal, -1); - } - } - - if (found) - { - SelectListViewIndexAndEnsureVisible(_findHelper.SelectedLineIndex); - tb.Focus(); - tb.SelectionStart = _findHelper.SelectedPosition; - tb.SelectionLength = _findHelper.FindTextLength; - ShowStatus(string.Format(_language.XFoundAtLineNumberY, _findHelper.FindText, _findHelper.SelectedLineIndex + 1)); - _findHelper.SelectedPosition++; - } - else - { - ShowStatus(string.Format(_language.XNotFound, _findHelper.FindText)); - } - } - else if (InSourceView) - { - if (_findHelper.Find(textBoxSource, textBoxSource.SelectionStart)) - { - textBoxSource.SelectionStart = _findHelper.SelectedLineIndex; - textBoxSource.SelectionLength = _findHelper.FindTextLength; - textBoxSource.ScrollToCaret(); - ShowStatus(string.Format(_language.XFoundAtLineNumberY, _findHelper.FindText, textBoxSource.GetLineFromCharIndex(textBoxSource.SelectionStart))); - } - else - { - ShowStatus(string.Format(_language.XNotFound, _findHelper.FindText)); - } - } - } + _findDialog?.Dispose(); + _findDialog = new FindDialog(_subtitle, this); + _findDialog.SetIcon(toolStripButtonFind.Image as Bitmap); + _findDialog.Initialize(selectedText, _findHelper); + _findDialog.Show(this); + } + public void FindDialogClose() + { if (_findHelper != null) { _findHelper.InProgress = false; } } + public void FindDialogFindPrevious() + { + FindPrevious(); + } + + public void FindDialogFind(string findText) + { + _findHelper = _findHelper ?? _findDialog.GetFindDialogHelper(_subtitleListViewIndex); + _findHelper.FindText = findText; + _findHelper.FindTextLength = findText.Length; + DialogFind(_findHelper); + } + private void FindNextToolStripMenuItemClick(object sender, EventArgs e) { ReloadFromSourceView(); @@ -6477,110 +6431,353 @@ namespace Nikse.SubtitleEdit.Forms private void ToolStripButtonReplaceClick(object sender, EventArgs e) { ReloadFromSourceView(); - Replace(null); + Replace(); } private void ReplaceToolStripMenuItemClick(object sender, EventArgs e) { ReloadFromSourceView(); - Replace(null); + Replace(); } - private void ReplaceSourceView(ReplaceDialog replaceDialog) + private void ReplaceSourceViewStart() { - bool isFirst = true; string selectedText = textBoxSource.SelectedText; if (selectedText.Length == 0 && _findHelper != null) { selectedText = _findHelper.FindText; } - if (replaceDialog == null) + if (_replaceDialog == null || _replaceDialog.IsDisposed) { - replaceDialog = new ReplaceDialog(); - replaceDialog.SetIcon(toolStripButtonReplace.Image as Bitmap); - _findHelper = _findHelper ?? replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); - } - else - { - isFirst = false; + _replaceDialog = new ReplaceDialog(this); + _replaceDialog.SetIcon(toolStripButtonReplace.Image as Bitmap); + _findHelper = _findHelper ?? _replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); } - replaceDialog.Initialize(selectedText, _findHelper); - if (replaceDialog.ShowDialog(this) == DialogResult.OK) + _replaceDialog.Initialize(selectedText, _findHelper); + if (!_replaceDialog.Visible) { - _findHelper = replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); - ShowStatus(string.Format(_language.SearchingForXFromLineY, _findHelper.FindText, _subtitleListViewIndex + 1)); - if (replaceDialog.ReplaceAll) + _replaceDialog.Show(this); + } + } + + public void ReplaceDialogFind() + { + DialogFind(_replaceDialog.GetFindDialogHelper(_subtitleListViewIndex)); + } + + public void DialogFind(FindReplaceDialogHelper findHelper) + { + _findHelper = findHelper; + _findHelper.InProgress = true; + if (!string.IsNullOrWhiteSpace(_findHelper.FindText)) + { + if (Configuration.Settings.Tools.FindHistory.Count == 0 || Configuration.Settings.Tools.FindHistory[0] != _findHelper.FindText) { - SourceListReplaceAll(replaceDialog, _findHelper); - return; + Configuration.Settings.Tools.FindHistory.Insert(0, _findHelper.FindText); + } + } + + ShowStatus(string.Format(_language.SearchingForXFromLineY, _findHelper.FindText, _subtitleListViewIndex + 1)); + if (InListView) + { + var tb = GetFindReplaceTextBox(); + int startPos = tb.SelectedText.Length > 0 ? tb.SelectionStart + 1 : tb.SelectionStart; + bool found = _findHelper.Find(_subtitle, _subtitleOriginal, _subtitleListViewIndex, startPos); + tb = GetFindReplaceTextBox(); + // if we fail to find the text, we might want to start searching from the top of the file. + if (!found && _findHelper.StartLineIndex >= 1) + { + if (MessageBox.Show(_language.FindContinue, _language.FindContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes) + { + found = _findHelper.Find(_subtitle, _subtitleOriginal, -1); + } } - int replaceCount = 0; - var searchStringFound = false; - int start = textBoxSource.SelectionStart; - if (isFirst) + if (found) { - MakeHistoryForUndo(string.Format(_language.BeforeReplace, _findHelper.FindText)); - _makeHistoryPaused = true; - if (start >= 0) - { - start--; - } + SelectListViewIndexAndEnsureVisible(_findHelper.SelectedLineIndex); + tb.Focus(); + tb.SelectionStart = _findHelper.SelectedPosition; + tb.SelectionLength = _findHelper.FindTextLength; + ShowStatus(string.Format(_language.XFoundAtLineNumberY, _findHelper.FindText, _findHelper.SelectedLineIndex + 1)); + _findHelper.SelectedPosition++; } else { - if (textBoxSource.SelectionLength > 0 && start > 0 && !replaceDialog.FindOnly) - { - start--; - } + ShowStatus(string.Format(_language.XNotFound, _findHelper.FindText)); } - - if (_findHelper.FindNext(textBoxSource.Text, start)) + } + else if (InSourceView) + { + if (_findHelper.Find(textBoxSource, textBoxSource.SelectionStart)) { textBoxSource.SelectionStart = _findHelper.SelectedLineIndex; textBoxSource.SelectionLength = _findHelper.FindTextLength; - if (!replaceDialog.FindOnly) - { - textBoxSource.SelectedText = _findHelper.ReplaceText; - } - textBoxSource.ScrollToCaret(); - - replaceCount++; - searchStringFound = true; - - if (!replaceDialog.FindOnly) - { - if (_findHelper.FindNext(textBoxSource.Text, start)) - { - textBoxSource.SelectionStart = _findHelper.SelectedLineIndex; - textBoxSource.SelectionLength = _findHelper.FindTextLength; - textBoxSource.ScrollToCaret(); - } - - Replace(replaceDialog); - return; - } + ShowStatus(string.Format(_language.XFoundAtLineNumberY, _findHelper.FindText, textBoxSource.GetLineFromCharIndex(textBoxSource.SelectionStart))); } - - if (replaceDialog.FindOnly) + else { - if (searchStringFound) + ShowStatus(string.Format(_language.XNotFound, _findHelper.FindText)); + } + } + } + + public void ReplaceDialogReplace() + { + if (InListView) + { + ReplaceDialogReplaceListView(); + } + else + { + ReplaceDialogReplaceSourceView(); + } + } + + public void ReplaceDialogReplaceListView() + { + _findHelper.InProgress = true; + var line = _findHelper.SelectedLineIndex; + var pos = _findHelper.ReplaceFromPosition; + var success = _findHelper.Success; + var matchInOriginal = _findHelper.MatchInOriginal; + _findHelper = _replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); + _findHelper.SelectedLineIndex = line; + _findHelper.SelectedPosition = pos; + _findHelper.Success = success; + _findHelper.MatchInOriginal = matchInOriginal; + _findHelper.InProgress = true; + + ShowStatus(string.Format(_language.SearchingForXFromLineY, _findHelper.FindText, _subtitleListViewIndex + 1)); + var tb = GetFindReplaceTextBox(); + string msg = string.Empty; + if (_findHelper.FindReplaceType.FindType == FindType.RegEx) + { + if (_findHelper.Success) + { + if (_findHelper.FindReplaceType.FindType == FindType.RegEx) { - ShowStatus(string.Format(_language.MatchFoundX, _findHelper.FindText)); + ReplaceViaRegularExpression(tb, _replaceDialog.ReplaceAll); } else { - ShowStatus(string.Format(_language.NoMatchFoundX, _findHelper.FindText)); + tb.SelectedText = _findHelper.ReplaceText; } - Replace(replaceDialog); - return; + msg = _language.OneReplacementMade + " "; + } + } + else if (tb.SelectionLength == _findHelper.FindTextLength) + { + tb.SelectedText = _findHelper.ReplaceText; + msg = _language.OneReplacementMade + " "; + _findHelper.SelectedPosition += _findHelper.ReplaceText.Length; + } + + if (_findHelper.FindNext(_subtitle, _subtitleOriginal, _findHelper.SelectedLineIndex, _findHelper.SelectedPosition, Configuration.Settings.General.AllowEditOfOriginalSubtitle)) + { + SelectListViewIndexAndEnsureVisible(_findHelper.SelectedLineIndex); + tb = GetFindReplaceTextBox(); + tb.Focus(); + tb.SelectionStart = _findHelper.SelectedPosition; + tb.SelectionLength = _findHelper.FindTextLength; + if (_findHelper.FindReplaceType.FindType != FindType.RegEx) + { + _findHelper.SelectedPosition += _findHelper.ReplaceText.Length; } - ReloadFromSourceView(); + ShowStatus(string.Format(msg + _language.XFoundAtLineNumberY, _findHelper.FindText, _findHelper.SelectedLineIndex + 1)); + } + else + { + ShowStatus(msg + string.Format(_language.XNotFound, _findHelper.FindText)); + + // Prompt for start over + if (_replaceStartLineIndex >= 1) + { + _replaceStartLineIndex = 0; + if (MessageBox.Show(_language.FindContinue, _language.FindContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes) + { + SelectListViewIndexAndEnsureVisible(0); + _findHelper.StartLineIndex = 0; + _findHelper.SelectedLineIndex = 0; + _findHelper.SelectedPosition = 0; + _findHelper.ReplaceFromPosition = 0; + if (_findHelper.FindNext(_subtitle, _subtitleOriginal, _findHelper.SelectedLineIndex, _findHelper.SelectedPosition, Configuration.Settings.General.AllowEditOfOriginalSubtitle)) + { + SelectListViewIndexAndEnsureVisible(_findHelper.SelectedLineIndex); + tb = GetFindReplaceTextBox(); + tb.Focus(); + tb.SelectionStart = _findHelper.SelectedPosition; + tb.SelectionLength = _findHelper.FindTextLength; + _findHelper.SelectedPosition += _findHelper.ReplaceText.Length; + ShowStatus(string.Format(msg + _language.XFoundAtLineNumberY, _findHelper.FindText, _findHelper.SelectedLineIndex + 1)); + } + } + } + } + + _findHelper.InProgress = false; + } + + public void ReplaceDialogReplaceSourceView() + { + _findHelper = _replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); + ShowStatus(string.Format(_language.SearchingForXFromLineY, _findHelper.FindText, _subtitleListViewIndex + 1)); + + int replaceCount = 0; + var searchStringFound = false; + int start = textBoxSource.SelectionStart; + + MakeHistoryForUndo(string.Format(_language.BeforeReplace, _findHelper.FindText)); + _makeHistoryPaused = true; + if (start >= 0) + { + start--; + } + + if (_findHelper.FindNext(textBoxSource.Text, start)) + { + textBoxSource.SelectionStart = _findHelper.SelectedLineIndex; + textBoxSource.SelectionLength = _findHelper.FindTextLength; + if (!_replaceDialog.FindOnly) + { + textBoxSource.SelectedText = _findHelper.ReplaceText; + } + + textBoxSource.ScrollToCaret(); + + replaceCount++; + searchStringFound = true; + + if (!_replaceDialog.FindOnly) + { + if (_findHelper.FindNext(textBoxSource.Text, start)) + { + textBoxSource.SelectionStart = _findHelper.SelectedLineIndex; + textBoxSource.SelectionLength = _findHelper.FindTextLength; + textBoxSource.ScrollToCaret(); + } + + Replace(); + return; + } + } + + if (_replaceDialog.FindOnly) + { + if (searchStringFound) + { + ShowStatus(string.Format(_language.MatchFoundX, _findHelper.FindText)); + } + else + { + ShowStatus(string.Format(_language.NoMatchFoundX, _findHelper.FindText)); + } + + Replace(); + return; + } + + ReloadFromSourceView(); + if (replaceCount == 0) + { + ShowStatus(_language.FoundNothingToReplace); + } + else + { + ShowStatus(string.Format(_language.ReplaceCountX, replaceCount)); + } + } + + public void ReplaceDialogReplaceAll() + { + _findHelper = _replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); + ShowStatus(string.Format(_language.SearchingForXFromLineY, _findHelper.FindText, _subtitleListViewIndex + 1)); + + if (InListView) + { + ListViewReplaceAll(_replaceDialog, _findHelper); + } + else + { + SourceListReplaceAll(_replaceDialog, _findHelper); + } + } + + public void ReplaceDialogClose() + { + if (_makeHistoryPaused) + { + RestartHistory(); + } + } + + public void ListViewReplaceAll(ReplaceDialog replaceDialog, FindReplaceDialogHelper findHelper) + { + _makeHistoryPaused = true; + if (_findHelper == null) + { + _findHelper = replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); + _findHelper.InProgress = true; + } + else + { + var line = _findHelper.SelectedLineIndex; + var pos = _findHelper.ReplaceFromPosition; + var success = _findHelper.Success; + var matchInOriginal = _findHelper.MatchInOriginal; + _findHelper = replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); + _findHelper.SelectedLineIndex = line; + _findHelper.SelectedPosition = pos; + _findHelper.Success = success; + _findHelper.MatchInOriginal = matchInOriginal; + _findHelper.InProgress = true; + } + + var isFirst = true; + var replaceCount = 0; + var searchStringFound = true; + var stopAtIndex = int.MaxValue; + var firstIndex = FirstSelectedIndex; + var searchedFromTop = firstIndex == 0 && _findHelper.ReplaceFromPosition == 0; + while (searchStringFound) + { + searchStringFound = false; + if (isFirst) + { + MakeHistoryForUndo(string.Format(_language.BeforeReplace, _findHelper.FindText)); + isFirst = false; + _makeHistoryPaused = true; + } + + if (replaceDialog.ReplaceAll) + { + replaceCount = ReplaceAllHelper.ReplaceAll(_findHelper, _subtitle, _subtitleOriginal, Configuration.Settings.General.AllowEditOfOriginalSubtitle, stopAtIndex); + SubtitleListview1.Fill(_subtitle, _subtitleOriginal); + RestoreSubtitleListviewIndices(); + + _replaceStartLineIndex = 0; + string msgText = _language.ReplaceContinueNotFound; + if (replaceCount > 0) + { + msgText = string.Format(_language.ReplaceXContinue, replaceCount); + } + + if (!searchedFromTop && MessageBox.Show(msgText, _language.ReplaceContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes) + { + stopAtIndex = firstIndex; + _findHelper.StartLineIndex = 0; + _findHelper.SelectedLineIndex = 0; + replaceCount = ReplaceAllHelper.ReplaceAll(_findHelper, _subtitle, _subtitleOriginal, Configuration.Settings.General.AllowEditOfOriginalSubtitle, stopAtIndex); + SubtitleListview1.Fill(_subtitle, _subtitleOriginal); + } + } + + UpdateSourceView(); if (replaceCount == 0) { ShowStatus(_language.FoundNothingToReplace); @@ -6588,30 +6785,35 @@ namespace Nikse.SubtitleEdit.Forms else { ShowStatus(string.Format(_language.ReplaceCountX, replaceCount)); + SubtitleListview1.SyntaxColorAllLines(_subtitle); } } + RestoreSubtitleListviewIndices(); if (_makeHistoryPaused) { RestartHistory(); } - replaceDialog.Dispose(); + _findHelper.InProgress = false; } private void SourceListReplaceAll(ReplaceDialog replaceDialog, FindReplaceDialogHelper findHelper) { + _makeHistoryPaused = true; + if (_findHelper.FindReplaceType.FindType == FindType.RegEx) { SourceListReplaceAllRegEx(replaceDialog); return; } - int replaceCount = 0; - bool searchStringFound = true; - int start = textBoxSource.SelectionStart; - bool isFirst = true; - string text = textBoxSource.Text; + var replaceCount = 0; + var searchStringFound = true; + var start = textBoxSource.SelectionStart; + var originalSelectionStart = textBoxSource.SelectionStart; + var isFirst = true; + var text = textBoxSource.Text; while (searchStringFound) { searchStringFound = false; @@ -6641,6 +6843,13 @@ namespace Nikse.SubtitleEdit.Forms textBoxSource.Text = text; ReloadFromSourceView(); + + if (originalSelectionStart < text.Length) + { + textBoxSource.SelectionStart = originalSelectionStart; + } + textBoxSource.SelectionLength = 0; + if (replaceCount == 0) { ShowStatus(_language.FoundNothingToReplace); @@ -6708,11 +6917,9 @@ namespace Nikse.SubtitleEdit.Forms ReloadFromSourceView(); } - private void ReplaceListView(ReplaceDialog replaceDialog) + private void ReplaceListViewStart() { SaveSubtitleListviewIndices(); - int firstIndex = FirstSelectedIndex; - bool isFirst = true; string selectedText; if (textBoxListViewTextOriginal.Focused) { @@ -6728,11 +6935,11 @@ namespace Nikse.SubtitleEdit.Forms selectedText = _findHelper.FindText; } - if (replaceDialog == null) + if (_replaceDialog == null || _replaceDialog.IsDisposed) { - replaceDialog = new ReplaceDialog(); - replaceDialog.SetIcon(toolStripButtonReplace.Image as Bitmap); - _findHelper = _findHelper ?? replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); + _replaceDialog = new ReplaceDialog(this); + _replaceDialog.SetIcon(toolStripButtonReplace.Image as Bitmap); + _findHelper = _findHelper ?? _replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); _findHelper.InProgress = true; int index = 0; @@ -6757,7 +6964,6 @@ namespace Nikse.SubtitleEdit.Forms } else { - isFirst = false; if (_findHelper != null) { selectedText = _findHelper.FindText; @@ -6765,261 +6971,11 @@ namespace Nikse.SubtitleEdit.Forms } } - replaceDialog.Initialize(selectedText, _findHelper); - if (replaceDialog.ShowDialog(this) == DialogResult.OK) + _replaceDialog.Initialize(selectedText, _findHelper); + if (!_replaceDialog.Visible) { - if (_findHelper == null) - { - _findHelper = replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); - _findHelper.InProgress = true; - } - else - { - int line = _findHelper.SelectedLineIndex; - int pos = _findHelper.ReplaceFromPosition; - bool success = _findHelper.Success; - var matchInOriginal = _findHelper.MatchInOriginal; - _findHelper = replaceDialog.GetFindDialogHelper(_subtitleListViewIndex); - _findHelper.SelectedLineIndex = line; - _findHelper.SelectedPosition = pos; - _findHelper.Success = success; - _findHelper.MatchInOriginal = matchInOriginal; - _findHelper.InProgress = true; - } - - ShowStatus(string.Format(_language.SearchingForXFromLineY, _findHelper.FindText, _subtitleListViewIndex + 1)); - int replaceCount = 0; - bool searchStringFound = true; - int stopAtIndex = int.MaxValue; - while (searchStringFound) - { - searchStringFound = false; - if (isFirst) - { - MakeHistoryForUndo(string.Format(_language.BeforeReplace, _findHelper.FindText)); - isFirst = false; - _makeHistoryPaused = true; - } - - if (replaceDialog.ReplaceAll) - { - replaceCount = ReplaceAllHelper.ReplaceAll(_findHelper, _subtitle, _subtitleOriginal, Configuration.Settings.General.AllowEditOfOriginalSubtitle, stopAtIndex); - SubtitleListview1.Fill(_subtitle, _subtitleOriginal); - RestoreSubtitleListviewIndices(); - - if (_replaceStartLineIndex >= 1) // Prompt for start over - { - _replaceStartLineIndex = 0; - string msgText = _language.ReplaceContinueNotFound; - if (replaceCount > 0) - { - msgText = string.Format(_language.ReplaceXContinue, replaceCount); - } - - if (MessageBox.Show(msgText, _language.ReplaceContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes) - { - stopAtIndex = firstIndex; - _findHelper.StartLineIndex = 0; - _findHelper.SelectedLineIndex = 0; - replaceCount = ReplaceAllHelper.ReplaceAll(_findHelper, _subtitle, _subtitleOriginal, Configuration.Settings.General.AllowEditOfOriginalSubtitle, stopAtIndex); - SubtitleListview1.Fill(_subtitle, _subtitleOriginal); - } - } - - break; - } - else if (replaceDialog.FindOnly) - { - if (_findHelper.FindNext(_subtitle, _subtitleOriginal, _findHelper.SelectedLineIndex, _findHelper.SelectedPosition, Configuration.Settings.General.AllowEditOfOriginalSubtitle)) - { - var tb = GetFindReplaceTextBox(); - SelectListViewIndexAndEnsureVisible(_findHelper.SelectedLineIndex); - tb.Focus(); - tb.SelectionStart = _findHelper.SelectedPosition; - tb.SelectionLength = _findHelper.FindTextLength; - _findHelper.ReplaceFromPosition = _findHelper.SelectedPosition; - _findHelper.SelectedPosition += _findHelper.FindTextLength; - ShowStatus(string.Format(_language.NoXFoundAtLineY, _findHelper.SelectedLineIndex + 1, _findHelper.FindText)); - Replace(replaceDialog); - if (replaceDialog != null && !replaceDialog.IsDisposed) - { - replaceDialog.Dispose(); - } - - _findHelper.InProgress = false; - return; - } - - if (_replaceStartLineIndex >= 1) // Prompt for start over - { - _replaceStartLineIndex = 0; - if (MessageBox.Show(_language.FindContinue, _language.FindContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes) - { - SelectListViewIndexAndEnsureVisible(0); - _findHelper.StartLineIndex = 0; - _findHelper.SelectedLineIndex = 0; - _findHelper.SelectedPosition = 0; - _findHelper.ReplaceFromPosition = 0; - if (_findHelper.FindNext(_subtitle, _subtitleOriginal, _findHelper.SelectedLineIndex, _findHelper.SelectedPosition, Configuration.Settings.General.AllowEditOfOriginalSubtitle)) - { - var tb = GetFindReplaceTextBox(); - SelectListViewIndexAndEnsureVisible(_findHelper.SelectedLineIndex); - tb.Focus(); - tb.SelectionStart = _findHelper.SelectedPosition; - tb.SelectionLength = _findHelper.FindTextLength; - _findHelper.ReplaceFromPosition = _findHelper.SelectedPosition; - _findHelper.SelectedPosition += _findHelper.FindTextLength; - ShowStatus(string.Format(_language.NoXFoundAtLineY, _findHelper.SelectedLineIndex + 1, _findHelper.FindText)); - Replace(replaceDialog); - if (replaceDialog != null) - { - replaceDialog.Dispose(); - } - - _findHelper.InProgress = false; - return; - } - } - else - { - if (replaceDialog != null && !replaceDialog.IsDisposed) - { - replaceDialog.Dispose(); - } - - _findHelper.InProgress = false; - return; - } - } - - ShowStatus(string.Format(_language.NoMatchFoundX, _findHelper.FindText)); - } - else if (!replaceDialog.FindOnly) // replace once only - { - var tb = GetFindReplaceTextBox(); - string msg = string.Empty; - if (_findHelper.FindReplaceType.FindType == FindType.RegEx) - { - if (_findHelper.Success) - { - if (_findHelper.FindReplaceType.FindType == FindType.RegEx) - { - ReplaceViaRegularExpression(tb, replaceDialog.ReplaceAll); - } - else - { - tb.SelectedText = _findHelper.ReplaceText; - } - - msg = _language.OneReplacementMade + " "; - } - } - else if (tb.SelectionLength == _findHelper.FindTextLength) - { - tb.SelectedText = _findHelper.ReplaceText; - msg = _language.OneReplacementMade + " "; - _findHelper.SelectedPosition += _findHelper.ReplaceText.Length; - } - - if (_findHelper.FindNext(_subtitle, _subtitleOriginal, _findHelper.SelectedLineIndex, _findHelper.SelectedPosition, Configuration.Settings.General.AllowEditOfOriginalSubtitle)) - { - SelectListViewIndexAndEnsureVisible(_findHelper.SelectedLineIndex); - tb = GetFindReplaceTextBox(); - tb.Focus(); - tb.SelectionStart = _findHelper.SelectedPosition; - tb.SelectionLength = _findHelper.FindTextLength; - if (_findHelper.FindReplaceType.FindType != FindType.RegEx) - { - _findHelper.SelectedPosition += _findHelper.ReplaceText.Length; - } - - ShowStatus(string.Format(msg + _language.XFoundAtLineNumberY, _findHelper.FindText, _findHelper.SelectedLineIndex + 1)); - } - else - { - ShowStatus(msg + string.Format(_language.XNotFound, _findHelper.FindText)); - - // Prompt for start over - if (_replaceStartLineIndex >= 1) - { - _replaceStartLineIndex = 0; - if (MessageBox.Show(_language.FindContinue, _language.FindContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes) - { - SelectListViewIndexAndEnsureVisible(0); - _findHelper.StartLineIndex = 0; - _findHelper.SelectedLineIndex = 0; - _findHelper.SelectedPosition = 0; - _findHelper.ReplaceFromPosition = 0; - if (_findHelper.FindNext(_subtitle, _subtitleOriginal, _findHelper.SelectedLineIndex, _findHelper.SelectedPosition, Configuration.Settings.General.AllowEditOfOriginalSubtitle)) - { - SelectListViewIndexAndEnsureVisible(_findHelper.SelectedLineIndex); - tb = GetFindReplaceTextBox(); - tb.Focus(); - tb.SelectionStart = _findHelper.SelectedPosition; - tb.SelectionLength = _findHelper.FindTextLength; - _findHelper.SelectedPosition += _findHelper.ReplaceText.Length; - ShowStatus(string.Format(msg + _language.XFoundAtLineNumberY, _findHelper.FindText, _findHelper.SelectedLineIndex + 1)); - } - } - else - { - if (replaceDialog != null && !replaceDialog.IsDisposed) - { - replaceDialog.Dispose(); - } - - _findHelper.InProgress = false; - return; - } - } - else - { - if (replaceDialog != null && !replaceDialog.IsDisposed) - { - replaceDialog.Dispose(); - } - - _findHelper.InProgress = false; - return; - } - } - - Replace(replaceDialog); - if (replaceDialog != null && !replaceDialog.IsDisposed) - { - replaceDialog.Dispose(); - } - - _findHelper.InProgress = false; - return; - } - - if (_findHelper.SelectedLineIndex > stopAtIndex) - { - break; - } - } - - UpdateSourceView(); - if (replaceCount == 0) - { - ShowStatus(_language.FoundNothingToReplace); - } - else - { - ShowStatus(string.Format(_language.ReplaceCountX, replaceCount)); - SubtitleListview1.SyntaxColorAllLines(_subtitle); - } + _replaceDialog.Show(this); } - - RestoreSubtitleListviewIndices(); - if (_makeHistoryPaused) - { - RestartHistory(); - } - - replaceDialog.Dispose(); - _findHelper.InProgress = false; } private void ReplaceViaRegularExpression(SETextBox tb, bool replaceAll) @@ -7059,15 +7015,15 @@ namespace Nikse.SubtitleEdit.Forms } } - private void Replace(ReplaceDialog replaceDialog) + private void Replace() { if (InSourceView) { - ReplaceSourceView(replaceDialog); + ReplaceSourceViewStart(); } else { - ReplaceListView(replaceDialog); + ReplaceListViewStart(); } } diff --git a/src/ui/Forms/Ocr/VobSubOcr.cs b/src/ui/Forms/Ocr/VobSubOcr.cs index 75fbbe06d..c9dc62564 100644 --- a/src/ui/Forms/Ocr/VobSubOcr.cs +++ b/src/ui/Forms/Ocr/VobSubOcr.cs @@ -31,7 +31,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr { using LogItem = OcrFixEngine.LogItem; - public sealed partial class VobSubOcr : PositionAndSizeForm, IBinaryParagraphList + public sealed partial class VobSubOcr : PositionAndSizeForm, IBinaryParagraphList, IFindAndReplace { private static readonly Color _listViewGreen = Configuration.Settings.General.UseDarkTheme ? Color.Green : Color.LightGreen; private static readonly Color _listViewYellow = Configuration.Settings.General.UseDarkTheme ? Color.FromArgb(218, 135, 32) : Color.Yellow; @@ -376,6 +376,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr private readonly int _ocrMethodCloudVision = -1; private FindReplaceDialogHelper _findHelper; + private FindDialog _findDialog; public static void SetDoubleBuffered(Control c) { @@ -6943,29 +6944,10 @@ namespace Nikse.SubtitleEdit.Forms.Ocr private void Find() { - using (var findDialog = new FindDialog(_subtitle)) - { - var idx = _selectedIndex; - - findDialog.Initialize(string.Empty, _findHelper); - if (findDialog.ShowDialog(this) == DialogResult.OK) - { - _findHelper = findDialog.GetFindDialogHelper(idx); - if (_findHelper.Find(_subtitle, null, idx, textBoxCurrentText.SelectionStart)) - { - subtitleListView1.SelectIndexAndEnsureVisible(_findHelper.SelectedLineIndex, true); - textBoxCurrentText.SelectionStart = _findHelper.SelectedPosition; - textBoxCurrentText.SelectionLength = _findHelper.FindTextLength; - _findNextLastLineIndex = idx; - _findNextLastTextPosition = _findHelper.SelectedPosition; - } - - if (!string.IsNullOrWhiteSpace(_findHelper.FindText) && (Configuration.Settings.Tools.FindHistory.Count == 0 || Configuration.Settings.Tools.FindHistory[0] != _findHelper.FindText)) - { - Configuration.Settings.Tools.FindHistory.Insert(0, _findHelper.FindText); - } - } - } + _findDialog = new FindDialog(_subtitle, this); + var idx = _selectedIndex; + _findDialog.Initialize(string.Empty, _findHelper); + _findDialog.Show(this); } private int _findNextLastLineIndex = -1; @@ -9800,5 +9782,115 @@ namespace Nikse.SubtitleEdit.Forms.Ocr { Configuration.Settings.Tools.OcrGoogleCloudVisionSeHandlesTextMerge = checkBoxSeHandlesTextMerge.Checked; } + + public void FindDialogFind(string findText) + { + _findHelper = _findHelper ?? _findDialog.GetFindDialogHelper(_selectedIndex); + _findHelper.FindText = findText; + _findHelper.FindTextLength = findText.Length; + _findHelper.InProgress = true; + if (!string.IsNullOrWhiteSpace(_findHelper.FindText)) + { + if (Configuration.Settings.Tools.FindHistory.Count == 0 || Configuration.Settings.Tools.FindHistory[0] != _findHelper.FindText) + { + Configuration.Settings.Tools.FindHistory.Insert(0, _findHelper.FindText); + } + } + + ShowStatus(string.Format(LanguageSettings.Current.Main.SearchingForXFromLineY, _findHelper.FindText, _selectedIndex + 1)); + + var tb = textBoxCurrentText; + int startPos = tb.SelectedText.Length > 0 ? tb.SelectionStart + 1 : tb.SelectionStart; + bool found = _findHelper.Find(_subtitle, null, _selectedIndex, startPos); + + // if we fail to find the text, we might want to start searching from the top of the file. + if (!found && _findHelper.StartLineIndex >= 1) + { + if (MessageBox.Show(LanguageSettings.Current.Main.FindContinue, LanguageSettings.Current.Main.FindContinueTitle, MessageBoxButtons.YesNoCancel) == DialogResult.Yes) + { + found = _findHelper.Find(_subtitle, null, -1); + } + } + + if (found) + { + subtitleListView1.SelectIndexAndEnsureVisible(_findHelper.SelectedLineIndex, true); + tb.Focus(); + tb.SelectionStart = _findHelper.SelectedPosition; + tb.SelectionLength = _findHelper.FindTextLength; + ShowStatus(string.Format(LanguageSettings.Current.Main.XFoundAtLineNumberY, _findHelper.FindText, _findHelper.SelectedLineIndex + 1)); + _findHelper.SelectedPosition++; + } + else + { + ShowStatus(string.Format(LanguageSettings.Current.Main.XNotFound, _findHelper.FindText)); + } + } + + public void FindDialogFindPrevious() + { + if (_findHelper == null) + { + return; + } + + _findHelper.InProgress = true; + var tb = textBoxCurrentText; + + var selectedIndex = -1; + if (subtitleListView1.SelectedItems.Count > 0) + { + selectedIndex = subtitleListView1.SelectedItems[0].Index; + } + + var textBoxStart = tb.SelectionStart; + if (_findHelper.SelectedPosition - 1 == tb.SelectionStart && tb.SelectionLength > 0 || + _findHelper.FindText.Equals(tb.SelectedText, StringComparison.OrdinalIgnoreCase)) + { + textBoxStart = tb.SelectionStart - 1; + } + + if (_findHelper.FindPrevious(_subtitle, null, selectedIndex, textBoxStart, Configuration.Settings.General.AllowEditOfOriginalSubtitle)) + { + tb = textBoxCurrentText; + subtitleListView1.SelectIndexAndEnsureVisible(_findHelper.SelectedLineIndex, true); + ShowStatus(string.Format(LanguageSettings.Current.Main.XFoundAtLineNumberY, _findHelper.FindText, _findHelper.SelectedLineIndex + 1)); + tb.Focus(); + tb.SelectionStart = _findHelper.SelectedPosition; + tb.SelectionLength = _findHelper.FindTextLength; + _findHelper.SelectedPosition--; + } + else + { + ShowStatus(string.Format(LanguageSettings.Current.Main.XNotFound, _findHelper.FindText)); + } + + _findHelper.InProgress = false; + } + + public void FindDialogClose() + { + _findHelper.InProgress = false; + } + + public void ReplaceDialogFind() + { + throw new NotImplementedException(); + } + + public void ReplaceDialogReplace() + { + throw new NotImplementedException(); + } + + public void ReplaceDialogReplaceAll() + { + throw new NotImplementedException(); + } + + public void ReplaceDialogClose() + { + throw new NotImplementedException(); + } } } diff --git a/src/ui/Forms/ReplaceDialog.Designer.cs b/src/ui/Forms/ReplaceDialog.Designer.cs index fca8430e1..a442e098e 100644 --- a/src/ui/Forms/ReplaceDialog.Designer.cs +++ b/src/ui/Forms/ReplaceDialog.Designer.cs @@ -43,9 +43,8 @@ // // radioButtonRegEx // - this.radioButtonRegEx.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.radioButtonRegEx.AutoSize = true; - this.radioButtonRegEx.Location = new System.Drawing.Point(15, 182); + this.radioButtonRegEx.Location = new System.Drawing.Point(15, 190); this.radioButtonRegEx.Name = "radioButtonRegEx"; this.radioButtonRegEx.Size = new System.Drawing.Size(56, 17); this.radioButtonRegEx.TabIndex = 5; @@ -55,9 +54,8 @@ // // radioButtonCaseSensitive // - this.radioButtonCaseSensitive.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.radioButtonCaseSensitive.AutoSize = true; - this.radioButtonCaseSensitive.Location = new System.Drawing.Point(15, 159); + this.radioButtonCaseSensitive.Location = new System.Drawing.Point(15, 167); this.radioButtonCaseSensitive.Name = "radioButtonCaseSensitive"; this.radioButtonCaseSensitive.Size = new System.Drawing.Size(94, 17); this.radioButtonCaseSensitive.TabIndex = 4; @@ -67,10 +65,9 @@ // // radioButtonNormal // - this.radioButtonNormal.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.radioButtonNormal.AutoSize = true; this.radioButtonNormal.Checked = true; - this.radioButtonNormal.Location = new System.Drawing.Point(15, 136); + this.radioButtonNormal.Location = new System.Drawing.Point(15, 144); this.radioButtonNormal.Name = "radioButtonNormal"; this.radioButtonNormal.Size = new System.Drawing.Size(58, 17); this.radioButtonNormal.TabIndex = 3; @@ -82,9 +79,9 @@ // buttonReplace // this.buttonReplace.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonReplace.Location = new System.Drawing.Point(93, 206); + this.buttonReplace.Location = new System.Drawing.Point(266, 54); this.buttonReplace.Name = "buttonReplace"; - this.buttonReplace.Size = new System.Drawing.Size(75, 23); + this.buttonReplace.Size = new System.Drawing.Size(119, 23); this.buttonReplace.TabIndex = 7; this.buttonReplace.Text = "Replace"; this.buttonReplace.UseVisualStyleBackColor = true; @@ -94,7 +91,7 @@ // this.textBoxFind.Location = new System.Drawing.Point(15, 25); this.textBoxFind.Name = "textBoxFind"; - this.textBoxFind.Size = new System.Drawing.Size(265, 21); + this.textBoxFind.Size = new System.Drawing.Size(232, 21); this.textBoxFind.TabIndex = 0; this.textBoxFind.TextChanged += new System.EventHandler(this.textBoxFind_TextChanged); this.textBoxFind.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBoxFindKeyDown); @@ -102,9 +99,9 @@ // buttonReplaceAll // this.buttonReplaceAll.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonReplaceAll.Location = new System.Drawing.Point(174, 206); + this.buttonReplaceAll.Location = new System.Drawing.Point(266, 83); this.buttonReplaceAll.Name = "buttonReplaceAll"; - this.buttonReplaceAll.Size = new System.Drawing.Size(103, 23); + this.buttonReplaceAll.Size = new System.Drawing.Size(119, 23); this.buttonReplaceAll.TabIndex = 8; this.buttonReplaceAll.Text = "Replace all"; this.buttonReplaceAll.UseVisualStyleBackColor = true; @@ -114,7 +111,7 @@ // this.textBoxReplace.Location = new System.Drawing.Point(15, 71); this.textBoxReplace.Name = "textBoxReplace"; - this.textBoxReplace.Size = new System.Drawing.Size(265, 21); + this.textBoxReplace.Size = new System.Drawing.Size(232, 21); this.textBoxReplace.TabIndex = 1; this.textBoxReplace.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBoxFindKeyDown); // @@ -139,9 +136,9 @@ // buttonFind // this.buttonFind.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.buttonFind.Location = new System.Drawing.Point(12, 206); + this.buttonFind.Location = new System.Drawing.Point(266, 25); this.buttonFind.Name = "buttonFind"; - this.buttonFind.Size = new System.Drawing.Size(75, 23); + this.buttonFind.Size = new System.Drawing.Size(119, 23); this.buttonFind.TabIndex = 6; this.buttonFind.Text = "Find"; this.buttonFind.UseVisualStyleBackColor = true; @@ -161,7 +158,7 @@ // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(330, 241); + this.ClientSize = new System.Drawing.Size(397, 221); this.Controls.Add(this.checkBoxWholeWord); this.Controls.Add(this.buttonFind); this.Controls.Add(this.labelFindWhat); diff --git a/src/ui/Forms/ReplaceDialog.cs b/src/ui/Forms/ReplaceDialog.cs index d2b0d2571..cea3887f2 100644 --- a/src/ui/Forms/ReplaceDialog.cs +++ b/src/ui/Forms/ReplaceDialog.cs @@ -10,17 +10,21 @@ namespace Nikse.SubtitleEdit.Forms { public sealed partial class ReplaceDialog : PositionAndSizeForm { + private readonly IFindAndReplace _findAndReplaceMethods; + private Regex _regEx; private bool _userAction; private bool _findNext; private FindReplaceDialogHelper _findHelper; - public ReplaceDialog() + public ReplaceDialog(IFindAndReplace findAndReplaceMethods) { UiUtil.PreInitialize(this); InitializeComponent(); UiUtil.FixFonts(this); + _findAndReplaceMethods = findAndReplaceMethods; + Text = LanguageSettings.Current.ReplaceDialog.Title; labelFindWhat.Text = LanguageSettings.Current.ReplaceDialog.FindWhat; radioButtonNormal.Text = LanguageSettings.Current.ReplaceDialog.Normal; @@ -73,6 +77,7 @@ namespace Nikse.SubtitleEdit.Forms if (e.KeyCode == Keys.Escape) { DialogResult = DialogResult.Cancel; + Close(); } } @@ -116,6 +121,10 @@ namespace Nikse.SubtitleEdit.Forms FindOnly = false; Validate(textBoxFind.Text); + if (DialogResult == DialogResult.OK) + { + _findAndReplaceMethods.ReplaceDialogReplace(); + } } private void ButtonReplaceAllClick(object sender, EventArgs e) @@ -124,6 +133,10 @@ namespace Nikse.SubtitleEdit.Forms FindOnly = false; Validate(textBoxFind.Text); + if (DialogResult == DialogResult.OK) + { + _findAndReplaceMethods.ReplaceDialogReplaceAll(); + } } private void Validate(string searchText) @@ -168,6 +181,10 @@ namespace Nikse.SubtitleEdit.Forms FindOnly = true; Validate(textBoxFind.Text); + if (DialogResult == DialogResult.OK) + { + _findAndReplaceMethods.ReplaceDialogFind(); + } } private void RadioButtonCheckedChanged(object sender, EventArgs e) @@ -202,6 +219,8 @@ namespace Nikse.SubtitleEdit.Forms private void ReplaceDialog_FormClosing(object sender, FormClosingEventArgs e) { + _findAndReplaceMethods.ReplaceDialogClose(); + if (!_userAction) { DialogResult = DialogResult.Cancel; diff --git a/src/ui/Logic/FindReplaceDialogHelper.cs b/src/ui/Logic/FindReplaceDialogHelper.cs index 4d2cda583..b6d236fe7 100644 --- a/src/ui/Logic/FindReplaceDialogHelper.cs +++ b/src/ui/Logic/FindReplaceDialogHelper.cs @@ -20,9 +20,9 @@ namespace Nikse.SubtitleEdit.Logic public bool MatchInOriginal { get; set; } public bool InProgress { get; set; } - public int FindTextLength { get; private set; } + public int FindTextLength { get; set; } - public string FindText { get; } + public string FindText { get; set; } public string ReplaceText { get; } @@ -181,18 +181,21 @@ namespace Nikse.SubtitleEdit.Logic public bool FindPrevious(Subtitle subtitle, Subtitle originalSubtitle, int startIndex, int position, bool allowEditOfOriginalSubtitle) { + //TODO: us whole word + + Success = false; - int index = startIndex; - bool first = true; + var index = startIndex; + var first = true; for (var i = startIndex; i >= 0; i--) { - Paragraph p = subtitle.Paragraphs[i]; + var p = subtitle.Paragraphs[i]; if (originalSubtitle != null && allowEditOfOriginalSubtitle) { if (!first || MatchInOriginal) { - Paragraph o = Utilities.GetOriginalParagraph(index, p, originalSubtitle.Paragraphs); + var o = Utilities.GetOriginalParagraph(index, p, originalSubtitle.Paragraphs); if (o != null) { if (!first) @@ -234,8 +237,16 @@ namespace Nikse.SubtitleEdit.Logic if (position - j >= 0 && position < p.Text.Length) { var t = p.Text.Substring(position - j, j + 1); - int pos = FindPositionInText(t, 0); - if (pos >= 0) + var pos = FindPositionInText(t, 0); + var startWholeWord = position - j < 1; + if (!startWholeWord && position - j - 1 > 0) + { + startWholeWord = SeparatorChars.Contains(p.Text[position - j - 1]); + } + + var startWholeWorkOkay = !FindReplaceType.WholeWord || startWholeWord; + + if (pos >= 0 && startWholeWorkOkay) { pos += position - j; MatchInOriginal = false; @@ -251,6 +262,7 @@ namespace Nikse.SubtitleEdit.Logic first = false; index--; } + return false; } @@ -363,10 +375,11 @@ namespace Nikse.SubtitleEdit.Logic } Success = true; } + return Success; } - string searchText = text.Substring(0, startIndex); - int pos = -1; + var searchText = text.Substring(0, startIndex); + var pos = -1; var comparison = GetComparison(); var idx = searchText.LastIndexOf(FindText, startIndex, comparison); while (idx >= 0) @@ -389,12 +402,14 @@ namespace Nikse.SubtitleEdit.Logic searchText = text.Substring(0, idx); idx = searchText.LastIndexOf(FindText, comparison); } + if (pos >= 0) { SelectedLineIndex = pos; return true; } } + return false; } diff --git a/src/ui/Logic/IFindAndReplace.cs b/src/ui/Logic/IFindAndReplace.cs new file mode 100644 index 000000000..d1877151e --- /dev/null +++ b/src/ui/Logic/IFindAndReplace.cs @@ -0,0 +1,14 @@ +namespace Nikse.SubtitleEdit.Logic +{ + public interface IFindAndReplace + { + void FindDialogFind(string findText); + void FindDialogFindPrevious(); + void FindDialogClose(); + + void ReplaceDialogFind(); + void ReplaceDialogReplace(); + void ReplaceDialogReplaceAll(); + void ReplaceDialogClose(); + } +} diff --git a/src/ui/Logic/Language.cs b/src/ui/Logic/Language.cs index 2eaeda2d3..3b1d7aef6 100644 --- a/src/ui/Logic/Language.cs +++ b/src/ui/Logic/Language.cs @@ -945,7 +945,8 @@ namespace Nikse.SubtitleEdit.Logic FindDialog = new LanguageStructure.FindDialog { Title = "Find", - Find = "&Find", + FindNext = "&Find next", + FindPrevious = "Find &previous", Normal = "&Normal", CaseSensitive = "&Case sensitive", RegularExpression = "Regular e&xpression", diff --git a/src/ui/Logic/LanguageDeserializer.cs b/src/ui/Logic/LanguageDeserializer.cs index 40e450600..5110f0415 100644 --- a/src/ui/Logic/LanguageDeserializer.cs +++ b/src/ui/Logic/LanguageDeserializer.cs @@ -1894,9 +1894,6 @@ namespace Nikse.SubtitleEdit.Logic case "FindDialog/Title": language.FindDialog.Title = reader.Value; break; - case "FindDialog/Find": - language.FindDialog.Find = reader.Value; - break; case "FindDialog/Normal": language.FindDialog.Normal = reader.Value; break; diff --git a/src/ui/Logic/LanguageStructure.cs b/src/ui/Logic/LanguageStructure.cs index 78882f68c..898f1fb82 100644 --- a/src/ui/Logic/LanguageStructure.cs +++ b/src/ui/Logic/LanguageStructure.cs @@ -804,7 +804,8 @@ namespace Nikse.SubtitleEdit.Logic public class FindDialog { public string Title { get; set; } - public string Find { get; set; } + public string FindNext { get; set; } + public string FindPrevious { get; set; } public string Normal { get; set; } public string CaseSensitive { get; set; } public string RegularExpression { get; set; } diff --git a/src/ui/SubtitleEdit.csproj b/src/ui/SubtitleEdit.csproj index 9565a7d0a..ee0d4417b 100644 --- a/src/ui/SubtitleEdit.csproj +++ b/src/ui/SubtitleEdit.csproj @@ -1416,6 +1416,7 @@ +