protected void InitSearchList(ref StorySearchIndex alstBoxesToSearch, out int nStoryIndex, out int nCtxBoxIndex, out int nCharIndex) { StringTransferSearchIndex ssidx = null; // if the index is empty, then load it... if (alstBoxesToSearch.Count == 0) { if (checkBoxAllStories.Checked) { TheSE.TheCurrentStoriesSet.IndexSearch(FindProperties, ref alstBoxesToSearch); } else { ssidx = alstBoxesToSearch.GetNewStorySearchIndex(TheSE.theCurrentStory.Name); TheSE.theCurrentStory.IndexSearch(FindProperties, ref ssidx); } } // this should be 'else' because if we requery the index, then this // index is definitely bad else if ((LastStoryIndex != -1) && (LastCtxBoxIndex != -1) && (LastCharIndex != -1)) { nStoryIndex = LastStoryIndex; nCtxBoxIndex = LastCtxBoxIndex; nCharIndex = LastCharIndex; return; } ssidx = ssidx ?? BoxesToSearch[TheSE.theCurrentStory.Name]; // check to see if we have a starting place... if (CtrlTextBox._inTextBox != null) { for (int i = 0; i < ssidx.Count; i++) { // start with the last text box selected... find it StringTransfer boxToSearch = ssidx[i].StringTransfer; if (boxToSearch.TextBox == CtrlTextBox._inTextBox) { nStoryIndex = LastStoryIndex = BoxesToSearch.IndexOf(ssidx); nCtxBoxIndex = LastCtxBoxIndex = i; nCharIndex = LastCharIndex = CaptureNextStartingCharIndex(boxToSearch.TextBox); return; } } } // otherwise, just start at the 0th verse of *this* story nStoryIndex = LastStoryIndex = BoxesToSearch.IndexOf(ssidx); nCtxBoxIndex = LastCtxBoxIndex = nCharIndex = LastCharIndex = 0; }
public void DoFindNext() { string strToSearchFor = UpdateComboBox(comboBoxFindWhat); if (String.IsNullOrEmpty(strToSearchFor)) { MessageBox.Show(Properties.Resources.IDS_NoSearchString, Properties.Resources.IDS_Caption); return; } if (FindProperties.UseRegex) { regex = GetRegex(strToSearchFor); } else { regex = null; } int nLastStoryIndex, nLastCtxBoxIndex, nLastCharIndex; InitSearchList(ref BoxesToSearch, out nLastStoryIndex, out nLastCtxBoxIndex, out nLastCharIndex); CtrlTextBox ctbStopWhereWeStarted = null; int nStoryIndex = nLastStoryIndex; int nCtxBoxIndex = nLastCtxBoxIndex; while (nStoryIndex < BoxesToSearch.Count) { StringTransferSearchIndex stsi = BoxesToSearch[nStoryIndex]; for (; nCtxBoxIndex < stsi.Count; nCtxBoxIndex++) { StringTransfer stringTransfer = stsi[nCtxBoxIndex].StringTransfer; if (!stringTransfer.HasData) { continue; } string strValue = stringTransfer.ToString(); // check to see if we're wrapped around and are starting // back at the beginning // note: that it's possible that this stringTransfer // does not have a TextBox (e.g. if it isn't visible) OR // if its in the ConNotes panes (which are HTML thingys) CtrlTextBox ctrlTextBox = stringTransfer.TextBox; // get the index *after* the selected text (our starting index // of the search) int nStartIndex = 0; if (ctrlTextBox != null) { // if the length of the text in the text box is not the same // length as the text in the StringTransfer, then this // assumption is probably no good System.Diagnostics.Debug.Assert(ctrlTextBox.TextLength == strValue.Length); if (ctbStopWhereWeStarted == ctrlTextBox) { ShowNotFound(); return; } nStartIndex = nLastCharIndex; if (nLastCharIndex != 0) { nLastCharIndex = 0; // only do that once ctrlTextBox.Select(0, 0); // don't leave it selected } } int nFoundIndex = -1, nLengthToSelect = 0; if (regex != null) { Match match = regex.Match(strValue, nStartIndex); if (match.Success) { nFoundIndex = match.Index; nLengthToSelect = match.Length; } } else { nFoundIndex = strValue.IndexOf(strToSearchFor, nStartIndex); nLengthToSelect = strToSearchFor.Length; } if (nFoundIndex != -1) { // found a match! VerseString vs = stsi[nCtxBoxIndex]; System.Diagnostics.Debug.Assert(vs.StringTransfer == stringTransfer); TheSE.NavigateTo(stsi.StoryName, vs.ViewToInsureIsOn, false, stringTransfer.TextBox); // The navigation process should make it visible as well. if (stringTransfer.TextBox != null) { stringTransfer.TextBox.Select(nFoundIndex, nLengthToSelect); LastCharIndex = CaptureNextStartingCharIndex(stringTransfer.TextBox); } else if (stringTransfer.HtmlConNoteCtrl != null) { stringTransfer.HtmlConNoteCtrl.SelectFoundText(stringTransfer.HtmlElementId, nFoundIndex, nLengthToSelect); LastCharIndex = nFoundIndex + nLengthToSelect; } LastStoryIndex = nStoryIndex; LastCtxBoxIndex = nCtxBoxIndex; buttonReplace.Enabled = true; return; } } // if we've reached the end of the verses in *this* story... if (!FindProperties.SearchAll) { // if we were already searching from the beginning... if (nLastCtxBoxIndex == 0) { // ... then we couldn't find it ShowNotFound(); return; } // otherwise, see if the user wants to start over from 0 if (MessageBox.Show(Properties.Resources.IDS_StartFromBeginning, Properties.Resources.IDS_Caption, MessageBoxButtons.YesNoCancel) == DialogResult.Yes) { nCtxBoxIndex = 0; // have it stop where we started ctbStopWhereWeStarted = CtrlTextBox._inTextBox ?? stsi[0].StringTransfer.TextBox; } else { return; // user said *don't* start over } } // otherwise, we're supposed to search the next story as well. else { nStoryIndex++; nCtxBoxIndex = 0; } } // if we reach here, it's because we were searching all the stories // and we reached the end and couldn't find it // if we were already searching from the beginning... if ((nLastStoryIndex == 0) && (nLastCtxBoxIndex == 0)) { // ... then we couldn't find it ShowNotFound(); return; } // otherwise, see if the user wants to start over from 0.0 if (MessageBox.Show(Properties.Resources.IDS_StartFromBeginning, Properties.Resources.IDS_Caption, MessageBoxButtons.YesNoCancel) == DialogResult.Yes) { LastStoryIndex = LastCtxBoxIndex = LastCharIndex = 0; DoFindNext(); } }