Ejemplo n.º 1
0
        //-------------------------------------------------------------------
        //
        //  Private Methods
        //
        //-------------------------------------------------------------------

        #region Private Methods

        /// <summary>
        /// Given a ContentElement within FlowDocument searches for associated IContentHost.
        /// </summary>
        /// <param name="contentElement">Content element</param>
        /// <param name="flowDocument">FlowDocument hosting ContentElement.</param>
        /// <returns>Associated IContentHost with ContentElement.</returns>
        private static IContentHost GetICHFromFlowDocument(TextElement contentElement, FlowDocument flowDocument)
        {
            IContentHost            ich = null;
            List <DocumentPageView> pageViews;
            ITextView textView = flowDocument.StructuralCache.TextContainer.TextView;

            if (textView != null)
            {
                // If FlowDocument is hosted by FlowDocumentScrollViewer, the RenderScope
                // is FlowDocumentView object which hosts PageVisual representing the content.
                // This PageVisual is also IContentHost for the entire content of DocumentPage.
                if (textView.RenderScope is FlowDocumentView) // FlowDocumentScrollViewer
                {
                    if (VisualTreeHelper.GetChildrenCount(textView.RenderScope) > 0)
                    {
                        ich = VisualTreeHelper.GetChild(textView.RenderScope, 0) as IContentHost;
                    }
                }
                // Our best guess is that FlowDocument is hosted by DocumentViewerBase.
                // In this case search the style for all DocumentPageViews.
                // Having collection of DocumentPageViews, find for the one which hosts TextElement.
                else if (textView.RenderScope is FrameworkElement)
                {
                    pageViews = new List <DocumentPageView>();
                    FindDocumentPageViews(textView.RenderScope, pageViews);
                    for (int i = 0; i < pageViews.Count; i++)
                    {
                        if (pageViews[i].DocumentPage is FlowDocumentPage)
                        {
                            textView = (ITextView)((IServiceProvider)pageViews[i].DocumentPage).GetService(typeof(ITextView));
                            if (textView != null && textView.IsValid)
                            {
                                // Check if the page contains ContentElement. Check Start and End
                                // position, which will give desired results in most of the cases.
                                // Having hyperlink spanning more than 2 pages is not very common,
                                // and this code will not work with it correctly.
                                if (textView.Contains(contentElement.ContentStart) ||
                                    textView.Contains(contentElement.ContentEnd))
                                {
                                    ich = pageViews[i].DocumentPage.Visual as IContentHost;
                                }
                            }
                        }
                    }
                }
            }

            return(ich);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// <see cref="AutomationPeer.IsOffscreenCore"/>
        /// </summary>
        /// <returns></returns>
        protected override bool IsOffscreenCore()
        {
            IsOffscreenBehavior behavior = AutomationProperties.GetIsOffscreenBehavior(Owner);

            switch (behavior)
            {
            case IsOffscreenBehavior.Onscreen:
                return(false);

            case IsOffscreenBehavior.Offscreen:
                return(true);

            default:
            {
                TextElement textElement = (TextElement)Owner;
                ITextView   textView    = textElement.TextContainer.TextView;
                if (textView == null || !textView.IsValid || (!textView.Contains(textElement.ContentStart) && !textView.Contains(textElement.ContentEnd)))
                {
                    return(true);
                }

                if (CalculateVisibleRect(textView, textElement, textElement.ContentStart, textElement.ContentEnd) == Rect.Empty)
                {
                    return(true);
                }

                return(false);
            }
            }
        }
        // Token: 0x06006F07 RID: 28423 RVA: 0x001FE3FC File Offset: 0x001FC5FC
        private static IContentHost GetICHFromFlowDocument(TextElement contentElement, FlowDocument flowDocument)
        {
            IContentHost result   = null;
            ITextView    textView = flowDocument.StructuralCache.TextContainer.TextView;

            if (textView != null)
            {
                if (textView.RenderScope is FlowDocumentView)
                {
                    if (VisualTreeHelper.GetChildrenCount(textView.RenderScope) > 0)
                    {
                        result = (VisualTreeHelper.GetChild(textView.RenderScope, 0) as IContentHost);
                    }
                }
                else if (textView.RenderScope is FrameworkElement)
                {
                    List <DocumentPageView> list = new List <DocumentPageView>();
                    ContentHostHelper.FindDocumentPageViews(textView.RenderScope, list);
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (list[i].DocumentPage is FlowDocumentPage)
                        {
                            textView = (ITextView)((IServiceProvider)list[i].DocumentPage).GetService(typeof(ITextView));
                            if (textView != null && textView.IsValid && (textView.Contains(contentElement.ContentStart) || textView.Contains(contentElement.ContentEnd)))
                            {
                                result = (list[i].DocumentPage.Visual as IContentHost);
                            }
                        }
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 4
0
        /// <summary>Gets a value that indicates whether <see cref="T:System.Windows.Documents.TextElement" /> that is associated with this <see cref="T:System.Windows.Automation.Peers.TextElementAutomationPeer" /> is off of the screen. Called by <see cref="M:System.Windows.Automation.Peers.AutomationPeer.IsOffscreen" />.</summary>
        /// <returns>
        ///     <see langword="true" /> if the element is not on the screen; otherwise, <see langword="false" />.</returns>
        // Token: 0x0600284D RID: 10317 RVA: 0x000BC0FC File Offset: 0x000BA2FC
        protected override bool IsOffscreenCore()
        {
            IsOffscreenBehavior isOffscreenBehavior = AutomationProperties.GetIsOffscreenBehavior(base.Owner);

            if (isOffscreenBehavior == IsOffscreenBehavior.Onscreen)
            {
                return(false);
            }
            if (isOffscreenBehavior != IsOffscreenBehavior.Offscreen)
            {
                TextElement textElement = (TextElement)base.Owner;
                ITextView   textView    = textElement.TextContainer.TextView;
                return(textView == null || !textView.IsValid || (!textView.Contains(textElement.ContentStart) && !textView.Contains(textElement.ContentEnd)) || this.CalculateVisibleRect(textView, textElement, textElement.ContentStart, textElement.ContentEnd) == Rect.Empty);
            }
            return(true);
        }
Ejemplo n.º 5
0
        protected override Point GetClickablePointCore()
        {
            Point pt = new Point();

            TextElement textElement = (TextElement)Owner;
            ITextView   textView    = textElement.TextContainer.TextView;

            if (textView == null || !textView.IsValid || (!textView.Contains(textElement.ContentStart) && !textView.Contains(textElement.ContentEnd)))
            {
                return(pt);
            }

            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(textView.RenderScope);

            if (presentationSource == null)
            {
                return(pt);
            }

            HwndSource hwndSource = presentationSource as HwndSource;

            // If the source isn't an HwnSource, there's not much we can do, return empty rect
            if (hwndSource == null)
            {
                return(pt);
            }

            TextPointer endPosition = textElement.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);

            if (endPosition == null || endPosition.CompareTo(textElement.ContentEnd) > 0)
            {
                endPosition = textElement.ContentEnd;
            }

            Rect rectElement = CalculateVisibleRect(textView, textElement, textElement.ContentStart, endPosition);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, textView.RenderScope, presentationSource);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
            Rect rectScreen  = PointUtil.ClientToScreen(rectClient, hwndSource);

            pt = new Point(rectScreen.Left + rectScreen.Width * 0.5, rectScreen.Top + rectScreen.Height * 0.5);

            return(pt);
        }
Ejemplo n.º 6
0
        protected override Point GetClickablePointCore()
        {
            Point       result      = default(Point);
            TextElement textElement = (TextElement)base.Owner;
            ITextView   textView    = textElement.TextContainer.TextView;

            if (textView == null || !textView.IsValid || (!textView.Contains(textElement.ContentStart) && !textView.Contains(textElement.ContentEnd)))
            {
                return(result);
            }
            PresentationSource presentationSource = PresentationSource.CriticalFromVisual(textView.RenderScope);

            if (presentationSource == null)
            {
                return(result);
            }
            HwndSource hwndSource = presentationSource as HwndSource;

            if (hwndSource == null)
            {
                return(result);
            }
            TextPointer textPointer = textElement.ContentStart.GetNextInsertionPosition(LogicalDirection.Forward);

            if (textPointer == null || textPointer.CompareTo(textElement.ContentEnd) > 0)
            {
                textPointer = textElement.ContentEnd;
            }
            Rect rectElement = this.CalculateVisibleRect(textView, textElement, textElement.ContentStart, textPointer);
            Rect rectRoot    = PointUtil.ElementToRoot(rectElement, textView.RenderScope, presentationSource);
            Rect rectClient  = PointUtil.RootToClient(rectRoot, presentationSource);
            Rect rect        = PointUtil.ClientToScreen(rectClient, hwndSource);

            result = new Point(rect.Left + rect.Width * 0.5, rect.Top + rect.Height * 0.5);
            return(result);
        }
Ejemplo n.º 7
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;
        }
Ejemplo n.º 8
0
        //-------------------------------------------------------------------
        //
        //  Internal Methods
        //
        //-------------------------------------------------------------------

        #region Internal Methods

        /// <summary>
        /// Retrieves the bounding rectangles for the text lines of a given range.
        /// </summary>
        /// <param name="start">Start of range to measure</param>
        /// <param name="end">End of range to measure</param>
        /// <param name="clipToView">Specifies whether the caller wants the full bounds (false) or the bounds of visible portions
        /// of the viewable line only ('true')</param>
        /// <param name="transformToScreen">Requests the results in screen coordinates</param>
        /// <returns>An array of bounding rectangles for each line or portion of a line within the client area of the text provider.
        /// No bounding rectangles will be returned for lines that are empty or scrolled out of view.  Note that even though a
        /// bounding rectangle is returned the corresponding text may not be visible due to overlapping windows.
        /// This will not return null, but may return an empty array.</returns>
        internal Rect[] GetBoundingRectangles(ITextPointer start, ITextPointer end, bool clipToView, bool transformToScreen)
        {
            ITextView textView = GetUpdatedTextView();

            if (textView == null)
            {
                return(new Rect[0]);
            }

            // If start/end positions are not in the visible range, move them to the first/last visible positions.
            ReadOnlyCollection <TextSegment> textSegments = textView.TextSegments;

            if (textSegments.Count > 0)
            {
                if (!textView.Contains(start) && start.CompareTo(textSegments[0].Start) < 0)
                {
                    start = textSegments[0].Start.CreatePointer();;
                }
                if (!textView.Contains(end) && end.CompareTo(textSegments[textSegments.Count - 1].End) > 0)
                {
                    end = textSegments[textSegments.Count - 1].End.CreatePointer();
                }
            }
            if (!textView.Contains(start) || !textView.Contains(end))
            {
                return(new Rect[0]);
            }

            TextRangeAdaptor.MoveToInsertionPosition(start, LogicalDirection.Forward);
            TextRangeAdaptor.MoveToInsertionPosition(end, LogicalDirection.Backward);

            Rect visibleRect = Rect.Empty;

            if (clipToView)
            {
                visibleRect = GetVisibleRectangle(textView);
                // If clipping into view and visible rect is empty, return.
                if (visibleRect.IsEmpty)
                {
                    return(new Rect[0]);
                }
            }

            List <Rect>  rectangles = new List <Rect>();
            ITextPointer position   = start.CreatePointer();

            while (position.CompareTo(end) < 0)
            {
                TextSegment lineRange = textView.GetLineRange(position);
                if (!lineRange.IsNull)
                {
                    // Since range is limited to just one line, GetTightBoundingGeometry will return tight bounding
                    // rectangle for given range. It will also work correctly with bidi text.
                    ITextPointer first    = (lineRange.Start.CompareTo(start) <= 0) ? start : lineRange.Start;
                    ITextPointer last     = (lineRange.End.CompareTo(end) >= 0) ? end : lineRange.End;
                    Rect         lineRect = Rect.Empty;
                    Geometry     geometry = textView.GetTightBoundingGeometryFromTextPositions(first, last);
                    if (geometry != null)
                    {
                        lineRect = geometry.Bounds;
                        if (clipToView)
                        {
                            lineRect.Intersect(visibleRect);
                        }
                        if (!lineRect.IsEmpty)
                        {
                            if (transformToScreen)
                            {
                                lineRect = new Rect(ClientToScreen(lineRect.TopLeft, textView.RenderScope), ClientToScreen(lineRect.BottomRight, textView.RenderScope));
                            }
                            rectangles.Add(lineRect);
                        }
                    }
                }
                if (position.MoveToLineBoundary(1) == 0)
                {
                    position = end;
                }
            }
            return(rectangles.ToArray());
        }
Ejemplo n.º 9
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);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Called when ContextMenuOpening is raised on FlowDocument viewer element.
        /// </summary>
        internal static void OnContextMenuOpening(FlowDocument document, Control viewer, ContextMenuEventArgs e)
        {
            // Get ContextMenu from TargetElement, if exests. Otherwise get ContextMenu from the viewer.
            ContextMenu cm = null;

            if (e.TargetElement != null)
            {
                cm = e.TargetElement.GetValue(FrameworkElement.ContextMenuProperty) as ContextMenu;
            }
            if (cm == null)
            {
                cm = viewer.ContextMenu;
            }

            // Add special handling for ContextMenu, if invoked through a hotkey.
            if (cm != null)
            {
                if (document != null)
                {
                    // A negative offset for e.CursorLeft means the user invoked
                    // the menu with a hotkey (shift-F10).
                    // For this case place the menu relative to Selection.Start,
                    // otherwise do not modify it.
                    if (DoubleUtil.LessThan(e.CursorLeft, 0))
                    {
                        // Retrieve desired ContextMenu position. If the TextSelection is not empty and visible,
                        // use selection start position. Otherwise prefer TargetElements's start, if provided.
                        ITextContainer textContainer       = (ITextContainer)((IServiceProvider)document).GetService(typeof(ITextContainer));
                        ITextPointer   contextMenuPosition = null;
                        if (textContainer.TextSelection != null)
                        {
                            if ((textContainer.TextSelection.IsEmpty || !textContainer.TextSelection.TextEditor.UiScope.IsFocused) &&
                                e.TargetElement is TextElement)
                            {
                                contextMenuPosition = ((TextElement)e.TargetElement).ContentStart;
                            }
                            else
                            {
                                // Selection start is always normalized to have backward LogicalDirection. However, if selection starts at the beginning
                                // of a line this will cause the text view to return rectangle on the previous line. So we need to switch  logical direction.
                                contextMenuPosition = textContainer.TextSelection.Start.CreatePointer(LogicalDirection.Forward);
                            }
                        }
                        else if (e.TargetElement is TextElement)
                        {
                            contextMenuPosition = ((TextElement)e.TargetElement).ContentStart;
                        }

                        // If ContextMenu position has been found and it is visible, show ContextMenu there.
                        // Otherwise let default ContextMenu handling logic handle this event.
                        ITextView textView = textContainer.TextView;
                        if (contextMenuPosition != null && textView != null && textView.IsValid && textView.Contains(contextMenuPosition))
                        {
                            Rect positionRect = textView.GetRectangleFromTextPosition(contextMenuPosition);
                            if (positionRect != Rect.Empty)
                            {
                                positionRect = DocumentViewerHelper.CalculateVisibleRect(positionRect, textView.RenderScope);
                                if (positionRect != Rect.Empty)
                                {
                                    GeneralTransform transform         = textView.RenderScope.TransformToAncestor(viewer);
                                    Point            contextMenuOffset = transform.Transform(positionRect.BottomLeft);
                                    cm.Placement        = PlacementMode.Relative;
                                    cm.PlacementTarget  = viewer;
                                    cm.HorizontalOffset = contextMenuOffset.X;
                                    cm.VerticalOffset   = contextMenuOffset.Y;
                                    cm.IsOpen           = true;
                                    e.Handled           = true;
                                }
                            }
                        }
                    }
                }
                if (!e.Handled)
                {
                    // Since we are not handling ContextMenu, clear all the values that
                    // could be set through explicit handling.
                    cm.ClearValue(ContextMenu.PlacementProperty);
                    cm.ClearValue(ContextMenu.PlacementTargetProperty);
                    cm.ClearValue(ContextMenu.HorizontalOffsetProperty);
                    cm.ClearValue(ContextMenu.VerticalOffsetProperty);
                }
            }
        }
        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);
        }
        // Token: 0x06006FD7 RID: 28631 RVA: 0x00202514 File Offset: 0x00200714
        internal static void OnContextMenuOpening(FlowDocument document, Control viewer, ContextMenuEventArgs e)
        {
            ContextMenu contextMenu = null;

            if (e.TargetElement != null)
            {
                contextMenu = (e.TargetElement.GetValue(FrameworkElement.ContextMenuProperty) as ContextMenu);
            }
            if (contextMenu == null)
            {
                contextMenu = viewer.ContextMenu;
            }
            if (contextMenu != null)
            {
                if (document != null && DoubleUtil.LessThan(e.CursorLeft, 0.0))
                {
                    ITextContainer textContainer = (ITextContainer)((IServiceProvider)document).GetService(typeof(ITextContainer));
                    ITextPointer   textPointer   = null;
                    if (textContainer.TextSelection != null)
                    {
                        if ((textContainer.TextSelection.IsEmpty || !textContainer.TextSelection.TextEditor.UiScope.IsFocused) && e.TargetElement is TextElement)
                        {
                            textPointer = ((TextElement)e.TargetElement).ContentStart;
                        }
                        else
                        {
                            textPointer = textContainer.TextSelection.Start.CreatePointer(LogicalDirection.Forward);
                        }
                    }
                    else if (e.TargetElement is TextElement)
                    {
                        textPointer = ((TextElement)e.TargetElement).ContentStart;
                    }
                    ITextView textView = textContainer.TextView;
                    if (textPointer != null && textView != null && textView.IsValid && textView.Contains(textPointer))
                    {
                        Rect rect = textView.GetRectangleFromTextPosition(textPointer);
                        if (rect != Rect.Empty)
                        {
                            rect = DocumentViewerHelper.CalculateVisibleRect(rect, textView.RenderScope);
                            if (rect != Rect.Empty)
                            {
                                GeneralTransform generalTransform = textView.RenderScope.TransformToAncestor(viewer);
                                Point            point            = generalTransform.Transform(rect.BottomLeft);
                                contextMenu.Placement        = PlacementMode.Relative;
                                contextMenu.PlacementTarget  = viewer;
                                contextMenu.HorizontalOffset = point.X;
                                contextMenu.VerticalOffset   = point.Y;
                                contextMenu.IsOpen           = true;
                                e.Handled = true;
                            }
                        }
                    }
                }
                if (!e.Handled)
                {
                    contextMenu.ClearValue(ContextMenu.PlacementProperty);
                    contextMenu.ClearValue(ContextMenu.PlacementTargetProperty);
                    contextMenu.ClearValue(ContextMenu.HorizontalOffsetProperty);
                    contextMenu.ClearValue(ContextMenu.VerticalOffsetProperty);
                }
            }
        }
Ejemplo n.º 13
0
        // Token: 0x060065C4 RID: 26052 RVA: 0x001C8254 File Offset: 0x001C6454
        internal Rect[] GetBoundingRectangles(ITextPointer start, ITextPointer end, bool clipToView, bool transformToScreen)
        {
            ITextView updatedTextView = this.GetUpdatedTextView();

            if (updatedTextView == null)
            {
                return(new Rect[0]);
            }
            ReadOnlyCollection <TextSegment> textSegments = updatedTextView.TextSegments;

            if (textSegments.Count > 0)
            {
                if (!updatedTextView.Contains(start) && start.CompareTo(textSegments[0].Start) < 0)
                {
                    start = textSegments[0].Start.CreatePointer();
                }
                if (!updatedTextView.Contains(end) && end.CompareTo(textSegments[textSegments.Count - 1].End) > 0)
                {
                    end = textSegments[textSegments.Count - 1].End.CreatePointer();
                }
            }
            if (!updatedTextView.Contains(start) || !updatedTextView.Contains(end))
            {
                return(new Rect[0]);
            }
            TextRangeAdaptor.MoveToInsertionPosition(start, LogicalDirection.Forward);
            TextRangeAdaptor.MoveToInsertionPosition(end, LogicalDirection.Backward);
            Rect rect = Rect.Empty;

            if (clipToView)
            {
                rect = this.GetVisibleRectangle(updatedTextView);
                if (rect.IsEmpty)
                {
                    return(new Rect[0]);
                }
            }
            List <Rect>  list        = new List <Rect>();
            ITextPointer textPointer = start.CreatePointer();

            while (textPointer.CompareTo(end) < 0)
            {
                TextSegment lineRange = updatedTextView.GetLineRange(textPointer);
                if (!lineRange.IsNull)
                {
                    ITextPointer startPosition = (lineRange.Start.CompareTo(start) <= 0) ? start : lineRange.Start;
                    ITextPointer endPosition   = (lineRange.End.CompareTo(end) >= 0) ? end : lineRange.End;
                    Rect         item          = Rect.Empty;
                    Geometry     tightBoundingGeometryFromTextPositions = updatedTextView.GetTightBoundingGeometryFromTextPositions(startPosition, endPosition);
                    if (tightBoundingGeometryFromTextPositions != null)
                    {
                        item = tightBoundingGeometryFromTextPositions.Bounds;
                        if (clipToView)
                        {
                            item.Intersect(rect);
                        }
                        if (!item.IsEmpty)
                        {
                            if (transformToScreen)
                            {
                                item = new Rect(this.ClientToScreen(item.TopLeft, updatedTextView.RenderScope), this.ClientToScreen(item.BottomRight, updatedTextView.RenderScope));
                            }
                            list.Add(item);
                        }
                    }
                }
                if (textPointer.MoveToLineBoundary(1) == 0)
                {
                    textPointer = end;
                }
            }
            return(list.ToArray());
        }