Exemplo n.º 1
0
        internal static ITextRange Find(FindToolBar findToolBar, TextEditor textEditor, ITextView textView, ITextView masterPageTextView)
        {
            string         searchText;
            FindFlags      findFlags;
            ITextContainer textContainer;
            ITextRange     textSelection;
            ITextPointer   contentStart;
            ITextPointer   contentEnd;
            ITextPointer   startPointer = null;
            ITextRange     findResult   = null;

            Invariant.Assert(findToolBar != null);
            Invariant.Assert(textEditor != null);

            // Set up our FindOptions from the options in the Find Toolbar.
            findFlags  = FindFlags.None;
            findFlags |= (findToolBar.SearchUp ? FindFlags.FindInReverse : FindFlags.None);
            findFlags |= (findToolBar.MatchCase ? FindFlags.MatchCase : FindFlags.None);
            findFlags |= (findToolBar.MatchWholeWord ? FindFlags.FindWholeWordsOnly : FindFlags.None);
            findFlags |= (findToolBar.MatchDiacritic ? FindFlags.MatchDiacritics : FindFlags.None);
            findFlags |= (findToolBar.MatchKashida ? FindFlags.MatchKashida : FindFlags.None);
            findFlags |= (findToolBar.MatchAlefHamza ? FindFlags.MatchAlefHamza : FindFlags.None);

            // Get the text container for our content.
            textContainer = textEditor.TextContainer;
            textSelection = textEditor.Selection;

            // Initialize other Find parameters
            searchText = findToolBar.SearchText;
            CultureInfo cultureInfo = GetDocumentCultureInfo(textContainer);

            // The find behavior below is defined in section 2.2.3 of this spec:
            // http://d2/DRX/Development%20Documents/02.01.00%20-%20UI%20Design.DocumentViewer.mht

            // Determine if we have a starting selection
            if (textSelection.IsEmpty)
            {
                if (textView != null && !textView.IsValid)
                {
                    textView = null;
                }

                // Determine if the IP/Selection is in view.
                if (textView != null && textView.Contains(textSelection.Start))
                {
                    // Case 1: Selection is empty and IP is currently visible.
                    // Search from this IP to the start/end of the document.

                    //We treat the start of the selection as the IP.
                    contentStart = findToolBar.SearchUp ? textContainer.Start : textSelection.Start;
                    contentEnd   = findToolBar.SearchUp ? textSelection.Start : textContainer.End;
                }
                else
                {
                    // Case 4: Selection is empty and IP is not currently visible.
                    // Search from the top of the current TextView to the end of the document,
                    // if searching down. If searchind up, search from the start of the document
                    // to the end position of the current TextView.
                    if (masterPageTextView != null && masterPageTextView.IsValid)
                    {
                        foreach (TextSegment textSegment in masterPageTextView.TextSegments)
                        {
                            if (textSegment.IsNull)
                            {
                                continue;
                            }

                            if (startPointer == null)
                            {
                                // Set initial masterPointer value.
                                startPointer = !findToolBar.SearchUp ? textSegment.Start : textSegment.End;
                            }
                            else
                            {
                                if (!findToolBar.SearchUp)
                                {
                                    if (textSegment.Start.CompareTo(startPointer) < 0)
                                    {
                                        // Start is before the current masterPointer
                                        startPointer = textSegment.Start;
                                    }
                                }
                                else
                                {
                                    // end is after than the current masterPointer
                                    if (textSegment.End.CompareTo(startPointer) > 0)
                                    {
                                        startPointer = textSegment.End;
                                    }
                                }
                            }
                        }
                    }

                    if (startPointer != null)
                    {
                        // Now build the content range from that pointer to the start/end of the document.
                        // Set content start/end pointer to the content of the find document
                        contentStart = findToolBar.SearchUp ? textContainer.Start : startPointer;
                        contentEnd   = findToolBar.SearchUp ? startPointer : textContainer.End;
                    }
                    else
                    {
                        // We were unable to determine the viewing area (form TextView),
                        // just use the entire TextContainer.
                        contentStart = textContainer.Start;
                        contentEnd   = textContainer.End;
                    }
                }
            }
            else
            {
                // Determine if the search text is already selected in the document.
                findResult = TextFindEngine.Find(textSelection.Start, textSelection.End, searchText, findFlags, cultureInfo);

                // To see if our Text ranges are the same, we will verify that
                // their start and end points are the same.
                if ((findResult != null) &&
                    (findResult.Start != null) &&
                    (findResult.Start.CompareTo(textSelection.Start) == 0) &&
                    (findResult.End.CompareTo(textSelection.End) == 0))
                {
                    // Case 2: Selection exists and it matches the search text.
                    // Search from the end of the given selection.

                    contentStart = findToolBar.SearchUp ? textSelection.Start : textSelection.End;
                    contentEnd   = findToolBar.SearchUp ? textContainer.Start : textContainer.End;
                }
                else
                {
                    // Case 3: Selection exists and it does not match the search text.
                    // Search from the beginning of the given selection to the end of the document.

                    contentStart = findToolBar.SearchUp ? textSelection.End : textSelection.Start;
                    contentEnd   = findToolBar.SearchUp ? textContainer.Start : textContainer.End;
                }
            }

            // We should have content. Try to find something.
            findResult = null;
            if (contentStart != null && contentEnd != null && contentStart.CompareTo(contentEnd) != 0)
            {
                // We might legimately have crossed start/end given our logic above.
                // It's easier to untangle the range here.
                if (contentStart.CompareTo(contentEnd) > 0)
                {
                    ITextPointer temp = contentStart;
                    contentStart = contentEnd;
                    contentEnd   = temp;
                }

                findResult = TextFindEngine.Find(contentStart, contentEnd, searchText, findFlags, cultureInfo);
                if ((findResult != null) && (!findResult.IsEmpty))
                {
                    textSelection.Select(findResult.Start, findResult.End);
                }
            }

            return(findResult);
        }
        internal static ITextRange Find(FindToolBar findToolBar, TextEditor textEditor, ITextView textView, ITextView masterPageTextView)
        {
            ITextPointer textPointer = null;

            Invariant.Assert(findToolBar != null);
            Invariant.Assert(textEditor != null);
            FindFlags findFlags = FindFlags.None;

            findFlags |= (findToolBar.SearchUp ? FindFlags.FindInReverse : FindFlags.None);
            findFlags |= (findToolBar.MatchCase ? FindFlags.MatchCase : FindFlags.None);
            findFlags |= (findToolBar.MatchWholeWord ? FindFlags.FindWholeWordsOnly : FindFlags.None);
            findFlags |= (findToolBar.MatchDiacritic ? FindFlags.MatchDiacritics : FindFlags.None);
            findFlags |= (findToolBar.MatchKashida ? FindFlags.MatchKashida : FindFlags.None);
            findFlags |= (findToolBar.MatchAlefHamza ? FindFlags.MatchAlefHamza : FindFlags.None);
            ITextContainer textContainer       = textEditor.TextContainer;
            ITextRange     selection           = textEditor.Selection;
            string         searchText          = findToolBar.SearchText;
            CultureInfo    documentCultureInfo = DocumentViewerHelper.GetDocumentCultureInfo(textContainer);
            ITextPointer   textPointer2;
            ITextPointer   textPointer3;
            ITextRange     textRange;

            if (selection.IsEmpty)
            {
                if (textView != null && !textView.IsValid)
                {
                    textView = null;
                }
                if (textView != null && textView.Contains(selection.Start))
                {
                    textPointer2 = (findToolBar.SearchUp ? textContainer.Start : selection.Start);
                    textPointer3 = (findToolBar.SearchUp ? selection.Start : textContainer.End);
                }
                else
                {
                    if (masterPageTextView != null && masterPageTextView.IsValid)
                    {
                        foreach (TextSegment textSegment in masterPageTextView.TextSegments)
                        {
                            if (!textSegment.IsNull)
                            {
                                if (textPointer == null)
                                {
                                    textPointer = ((!findToolBar.SearchUp) ? textSegment.Start : textSegment.End);
                                }
                                else if (!findToolBar.SearchUp)
                                {
                                    if (textSegment.Start.CompareTo(textPointer) < 0)
                                    {
                                        textPointer = textSegment.Start;
                                    }
                                }
                                else if (textSegment.End.CompareTo(textPointer) > 0)
                                {
                                    textPointer = textSegment.End;
                                }
                            }
                        }
                    }
                    if (textPointer != null)
                    {
                        textPointer2 = (findToolBar.SearchUp ? textContainer.Start : textPointer);
                        textPointer3 = (findToolBar.SearchUp ? textPointer : textContainer.End);
                    }
                    else
                    {
                        textPointer2 = textContainer.Start;
                        textPointer3 = textContainer.End;
                    }
                }
            }
            else
            {
                textRange = TextFindEngine.Find(selection.Start, selection.End, searchText, findFlags, documentCultureInfo);
                if (textRange != null && textRange.Start != null && textRange.Start.CompareTo(selection.Start) == 0 && textRange.End.CompareTo(selection.End) == 0)
                {
                    textPointer2 = (findToolBar.SearchUp ? selection.Start : selection.End);
                    textPointer3 = (findToolBar.SearchUp ? textContainer.Start : textContainer.End);
                }
                else
                {
                    textPointer2 = (findToolBar.SearchUp ? selection.End : selection.Start);
                    textPointer3 = (findToolBar.SearchUp ? textContainer.Start : textContainer.End);
                }
            }
            textRange = null;
            if (textPointer2 != null && textPointer3 != null && textPointer2.CompareTo(textPointer3) != 0)
            {
                if (textPointer2.CompareTo(textPointer3) > 0)
                {
                    ITextPointer textPointer4 = textPointer2;
                    textPointer2 = textPointer3;
                    textPointer3 = textPointer4;
                }
                textRange = TextFindEngine.Find(textPointer2, textPointer3, searchText, findFlags, documentCultureInfo);
                if (textRange != null && !textRange.IsEmpty)
                {
                    selection.Select(textRange.Start, textRange.End);
                }
            }
            return(textRange);
        }