protected void ProcessButtonEx(FormButtons eFormButton)
        {
            if (String.IsNullOrEmpty(ecTextBoxFindWhat.Text))
            {
                throw new ApplicationException("Enter a regular expression in the 'Find what' box");
            }

            m_doc.Document.Application.System.Cursor = Microsoft.Office.Interop.Word.WdCursorType.wdCursorWait; // be sure to turn it off in backgroundWorker_RunWorkerCompleted

            // if there's no processor (e.g. initially, change of Find What or Replace With text)...
            if (m_aWordByWordProcessor == null)
            {
                AddToComboBox(ecTextBoxFindWhat, comboBoxFindWhat, Properties.Settings.Default.RecentFindWhat);

                // if the user clicked Find/Next, then don't give the Replace With text even if there is some
                m_aWordByWordProcessor = new FindWordProcessor(ecTextBoxFindWhat.Text, ecTextBoxReplaceWith.Text,
                                                               checkBoxMatchCase.Checked, ecTextBoxFindWhat.Font);
            }

            // update the button pressed information
            m_aWordByWordProcessor.FormButton = eFormButton;

            // if we're doing a replacement, then save the 'Replace with' string in our settings file
            if ((eFormButton == FormButtons.ReplaceOnce) || (eFormButton == FormButtons.ReplaceAll))
            {
                AddToComboBox(ecTextBoxReplaceWith, comboBoxReplaceWith, Properties.Settings.Default.RecentReplaceWith);

                // the user may have done 'Replace' when it was found, but then later clicked said ReplaceAll,
                //  so update the value
                if (eFormButton == FormButtons.ReplaceAll)
                {
                    m_aWordByWordProcessor.ReplaceAll        = true;
                    m_aWordByWordProcessor.NumOfReplacements = 0;
                }
            }

            // if the user manually repositioned the insertion point, then we have to start over.
            if (m_doc.HasPositionChanged)
            {
                // determine if this is a single selection or "the rest of the document" from the insertion point
                m_doc.DetermineRangeToSearch(out m_eSearchAreaType);
                System.Diagnostics.Debug.Assert(m_doc.theRangeToSearch != null);

                if (m_eSearchAreaType == SearchAreaType.eWholeDocument)
                {
                    progressBar.Maximum = m_doc.theRangeToSearch.End;
                }
            }
            else if (m_aWordByWordProcessor.IsFound && (eFormButton == FormButtons.Next))
            {
                m_doc.BumpPastLastFound();
            }

            m_doc.theSearchProcessor = m_aWordByWordProcessor;

            progressBar.Visible    = true; // be sure to turn it off during backgroundWorker_RunWorkerCompleted
            this.buttonCancel.Text = cstrStop;

            CallWorkerBee();
        }
        public void ProcessParagraphsFindAndReplace(FindWordProcessor aWordProcessor, Word.Range theRangeToSearch,
                                                    BackgroundWorker worker)
        {
            // get Range object we can manipulate for searching (i.e. don't muck with the original
            WordRange aRunRange = GetStartingRange(theRangeToSearch);

            if (aRunRange == null)
            {
                return;
            }

            // how many paragraphs ahead we have to look for a match
            int    nEndRangeToSearch = -1; // might change from loop to loop if we do replacements (so update inside loop)
            object nOffsetParagraph  = aWordProcessor.NumOfParagraphsToSearch - 1;

            do
            {
                SearchAreaStart = aRunRange.Start;

                // this updates the progress bar
                worker.ReportProgress(SearchAreaStart);

                // if the search string contains multiple '\r's, then we have to look ahead that many paragraphs
                int SearchAreaEnd;
                nEndRangeToSearch = theRangeToSearch.End;   // might change from loop to loop if we do replacements
                if (0 < (int)nOffsetParagraph)
                {
                    Word.Paragraph aEndParagraph = m_paraSearch.Next(ref nOffsetParagraph);
                    if (aEndParagraph == null)
                    {
                        return; // means we can't possibly find it now
                    }
                    SearchAreaEnd = Math.Min(aEndParagraph.Range.End, nEndRangeToSearch);
                    aRunRange.End = SearchAreaEnd;
                }
                else
                {
                    int nEndOfRun = aRunRange.End;
                    if (nEndOfRun > nEndRangeToSearch)
                    {
                        aRunRange.End = SearchAreaEnd = nEndRangeToSearch;
                    }
                    else
                    {
                        SearchAreaEnd = nEndOfRun;
                    }
                }

                // loop until the end of the paragraph and process it as a whole unit (we may have to do this multiple times)
                bool bGoToNext = true;
                while (SearchAreaStart < SearchAreaEnd)
                {
                    // keep track of the last index so we can tell whether anything was found or not
                    FindWordProcessor.FindResult res = aWordProcessor.FindReplaceCompare(aRunRange, ref SearchAreaStart, ref FoundAreaLength);

                    // if we found it and need to stop (or if the user had cancelled the search)...
                    bGoToNext = true;   // assume we will
                    if ((res == FindWordProcessor.FindResult.eFindFound) || worker.CancellationPending)
                    {
                        return;
                    }

                    // otherwise, it might have been a replacement and we have to update the end of the paragraph
                    //  value and the end of the range and do it again (e.g. find the next occurrence to replace)
                    else if (res == FindWordProcessor.FindResult.eReplaceFound)
                    {
                        // we might have actually removed the paragraph mark, in which case the next paragraph
                        //  is now part of the current paragraph. Unfortunately, the aParagraphRange isn't
                        //  updated.
                        // if the search string contained '\r's, then we have to look ahead all over again
                        if (aWordProcessor.IsAnyCRs)
                        {
                            bGoToNext = false;
                            break;
                        }
                        else
                        {
                            Word.Range aParagraphRange = m_paraSearch.Range;

                            SearchAreaEnd   = aParagraphRange.End;
                            aRunRange.End   = SearchAreaEnd;
                            aRunRange.Start = SearchAreaStart;
                        }
                    }

                    // otherwise, it means we didn't find it here and we're done with this paragraph
                    else
                    {
                        System.Diagnostics.Debug.Assert(!aWordProcessor.IsFound);
                        break;
                    }
                }

                // advance to the next paragraph
                if (bGoToNext)
                {
                    m_paraSearch = m_paraSearch.Next(ref offset1);
                }

                if (m_paraSearch != null)
                {
                    aRunRange = new WordRange(m_paraSearch.Range);

                    // in the case we replace some CRs, we re-start where we left off (after the replaced text)
                    if (!bGoToNext)
                    {
                        aRunRange.Start = SearchAreaStart;
                    }
                }
            } while ((m_paraSearch != null) && (aRunRange.Start < nEndRangeToSearch));
        }
 private void checkBoxMatchCase_CheckedChanged(object sender, EventArgs e)
 {
     m_aWordByWordProcessor = null;
 }
 private void ecTextBox_TextChanged(object sender, EventArgs e)
 {
     m_eSearchAreaType      = SearchAreaType.eUnknown;
     m_aWordByWordProcessor = null;
 }
예제 #5
0
		public void ProcessParagraphsFindAndReplace(FindWordProcessor aWordProcessor, Word.Range theRangeToSearch,
			BackgroundWorker worker)
		{
			// get Range object we can manipulate for searching (i.e. don't muck with the original
			WordRange aRunRange = GetStartingRange(theRangeToSearch);
			if (aRunRange == null)
				return;

			// how many paragraphs ahead we have to look for a match
			int nEndRangeToSearch = -1;   // might change from loop to loop if we do replacements (so update inside loop)
			object nOffsetParagraph = aWordProcessor.NumOfParagraphsToSearch - 1;
			do
			{
				SearchAreaStart = aRunRange.Start;

				// this updates the progress bar
				worker.ReportProgress(SearchAreaStart);

				// if the search string contains multiple '\r's, then we have to look ahead that many paragraphs
				int SearchAreaEnd;
				nEndRangeToSearch = theRangeToSearch.End;   // might change from loop to loop if we do replacements
				if (0 < (int)nOffsetParagraph)
				{
					Word.Paragraph aEndParagraph = m_paraSearch.Next(ref nOffsetParagraph);
					if (aEndParagraph == null)
						return; // means we can't possibly find it now
					SearchAreaEnd = Math.Min(aEndParagraph.Range.End, nEndRangeToSearch);
					aRunRange.End = SearchAreaEnd;
				}
				else
				{
					int nEndOfRun = aRunRange.End;
					if (nEndOfRun > nEndRangeToSearch)
					{
						aRunRange.End = SearchAreaEnd = nEndRangeToSearch;
					}
					else
						SearchAreaEnd = nEndOfRun;
				}

				// loop until the end of the paragraph and process it as a whole unit (we may have to do this multiple times)
				bool bGoToNext = true;
				while (SearchAreaStart < SearchAreaEnd)
				{
					// keep track of the last index so we can tell whether anything was found or not
					FindWordProcessor.FindResult res = aWordProcessor.FindReplaceCompare(aRunRange, ref SearchAreaStart, ref FoundAreaLength);

					// if we found it and need to stop (or if the user had cancelled the search)...
					bGoToNext = true;   // assume we will
					if ((res == FindWordProcessor.FindResult.eFindFound) || worker.CancellationPending)
						return;

					// otherwise, it might have been a replacement and we have to update the end of the paragraph
					//  value and the end of the range and do it again (e.g. find the next occurrence to replace)
					else if (res == FindWordProcessor.FindResult.eReplaceFound)
					{
						// we might have actually removed the paragraph mark, in which case the next paragraph
						//  is now part of the current paragraph. Unfortunately, the aParagraphRange isn't
						//  updated.
						// if the search string contained '\r's, then we have to look ahead all over again
						if (aWordProcessor.IsAnyCRs)
						{
							bGoToNext = false;
							break;
						}
						else
						{
							Word.Range aParagraphRange = m_paraSearch.Range;

							SearchAreaEnd = aParagraphRange.End;
							aRunRange.End = SearchAreaEnd;
							aRunRange.Start = SearchAreaStart;
						}
					}

					// otherwise, it means we didn't find it here and we're done with this paragraph
					else
					{
						System.Diagnostics.Debug.Assert(!aWordProcessor.IsFound);
						break;
					}
				}

				// advance to the next paragraph
				if (bGoToNext)
					m_paraSearch = m_paraSearch.Next(ref offset1);

				if (m_paraSearch != null)
				{
					aRunRange = new WordRange(m_paraSearch.Range);

					// in the case we replace some CRs, we re-start where we left off (after the replaced text)
					if (!bGoToNext)
						aRunRange.Start = SearchAreaStart;
				}

			} while ((m_paraSearch != null) && (aRunRange.Start < nEndRangeToSearch));
		}
예제 #6
0
		protected void ProcessButtonEx(FormButtons eFormButton)
		{
			if (String.IsNullOrEmpty(ecTextBoxFindWhat.Text))
				throw new ApplicationException("Enter a regular expression in the 'Find what' box");

			m_doc.Document.Application.System.Cursor = Microsoft.Office.Interop.Word.WdCursorType.wdCursorWait; // be sure to turn it off in backgroundWorker_RunWorkerCompleted

			// if there's no processor (e.g. initially, change of Find What or Replace With text)...
			if (m_aWordByWordProcessor == null)
			{
				AddToComboBox(ecTextBoxFindWhat, comboBoxFindWhat, Properties.Settings.Default.RecentFindWhat);

				// if the user clicked Find/Next, then don't give the Replace With text even if there is some
				m_aWordByWordProcessor = new FindWordProcessor(ecTextBoxFindWhat.Text, ecTextBoxReplaceWith.Text,
					checkBoxMatchCase.Checked, ecTextBoxFindWhat.Font);
			}

			// update the button pressed information
			m_aWordByWordProcessor.FormButton = eFormButton;

			// if we're doing a replacement, then save the 'Replace with' string in our settings file
			if ((eFormButton == FormButtons.ReplaceOnce) || (eFormButton == FormButtons.ReplaceAll))
			{
				AddToComboBox(ecTextBoxReplaceWith, comboBoxReplaceWith, Properties.Settings.Default.RecentReplaceWith);

				// the user may have done 'Replace' when it was found, but then later clicked said ReplaceAll,
				//  so update the value
				if (eFormButton == FormButtons.ReplaceAll)
				{
					m_aWordByWordProcessor.ReplaceAll = true;
					m_aWordByWordProcessor.NumOfReplacements = 0;
				}
			}

			// if the user manually repositioned the insertion point, then we have to start over.
			if (m_doc.HasPositionChanged)
			{
				// determine if this is a single selection or "the rest of the document" from the insertion point
				m_doc.DetermineRangeToSearch(out m_eSearchAreaType);
				System.Diagnostics.Debug.Assert(m_doc.theRangeToSearch != null);

				if (m_eSearchAreaType == SearchAreaType.eWholeDocument)
					progressBar.Maximum = m_doc.theRangeToSearch.End;
			}
			else if (m_aWordByWordProcessor.IsFound && (eFormButton == FormButtons.Next))
			{
				m_doc.BumpPastLastFound();
			}

			m_doc.theSearchProcessor = m_aWordByWordProcessor;

			progressBar.Visible = true; // be sure to turn it off during backgroundWorker_RunWorkerCompleted
			this.buttonCancel.Text = cstrStop;

			CallWorkerBee();
		}
예제 #7
0
		private void checkBoxMatchCase_CheckedChanged(object sender, EventArgs e)
		{
			m_aWordByWordProcessor = null;
		}
예제 #8
0
		private void ecTextBox_TextChanged(object sender, EventArgs e)
		{
			m_eSearchAreaType = SearchAreaType.eUnknown;
			m_aWordByWordProcessor = null;
		}