Exemplo n.º 1
0
        private static bool HighlightPlainTextDiff(FlowDocument document, char[] s, RichTextBox rtb)
        {
            //Console.WriteLine("-----------------------");
            int         s_i         = 0;
            TextPointer pointer     = document.ContentStart;
            bool        IsBlankLine = false;

            while (pointer != null)
            {
                //Console.WriteLine(pointer.GetPointerContext(LogicalDirection.Forward));
                TextPointerContext context = pointer.GetPointerContext(LogicalDirection.Forward);
                if (context == TextPointerContext.Text)
                {
                    IsBlankLine = false;
                    char[] textRun = pointer.GetTextInRun(LogicalDirection.Forward).ToCharArray();
                    int    i       = 0;
                    for (; i < textRun.Length && s_i < s.Length; ++i, ++s_i)
                    {
                        if (textRun[i] != s[s_i])
                        {
                            int unmatching_word = SearchUnmatchingWord(textRun, i);
                            rtb.Selection.Select(pointer.GetPositionAtOffset(i),
                                                 pointer.GetPositionAtOffset(unmatching_word));
                            rtb.Focus();
                            return(true);
                        }
                    }
                }
                else if (context == TextPointerContext.ElementStart)
                {
                    IsBlankLine = true;
                }
                else if (context == TextPointerContext.ElementEnd && IsBlankLine)
                {
                    pointer.InsertTextInRun("     ");
                    HighlightRange = new TextRange(pointer.Paragraph.ElementStart,
                                                   pointer.Paragraph.ElementEnd);
                    HighlightRange.ApplyPropertyValue(TextElement.BackgroundProperty,
                                                      Brushes.Gray); //new SolidColorBrush(SysColor));
                    //rtb.Selection.Select(HighlightRange.Start, HighlightRange.End);
                    //rtb.Focus();
                    return(true);
                }

                pointer = pointer.GetNextContextPosition(LogicalDirection.Forward);
            }

            if (s_i < s.Length)
            {
                HighlightRange = new TextRange(document.ContentEnd,
                                               document.ContentEnd);
                HighlightRange.Text = "     ";
                HighlightRange.ApplyPropertyValue(TextElement.BackgroundProperty,
                                                  Brushes.Gray);//new SolidColorBrush(SysColor));
                //rtb.Selection.Select(HighlightRange.Start, HighlightRange.End);
                //rtb.Focus();
                return(true);
            }
            return(false);
        }
        // Token: 0x060039C1 RID: 14785 RVA: 0x001064C0 File Offset: 0x001046C0
        private static bool IsAdjacentToFormatElement(ITextPointer pointer, LogicalDirection direction)
        {
            bool result = false;

            if (direction == LogicalDirection.Forward)
            {
                TextPointerContext pointerContext = pointer.GetPointerContext(LogicalDirection.Forward);
                if (pointerContext == TextPointerContext.ElementStart && TextSchema.IsFormattingType(pointer.GetElementType(LogicalDirection.Forward)))
                {
                    result = true;
                }
                else if (pointerContext == TextPointerContext.ElementEnd && TextSchema.IsFormattingType(pointer.ParentType))
                {
                    result = true;
                }
            }
            else
            {
                TextPointerContext pointerContext = pointer.GetPointerContext(LogicalDirection.Backward);
                if (pointerContext == TextPointerContext.ElementEnd && TextSchema.IsFormattingType(pointer.GetElementType(LogicalDirection.Backward)))
                {
                    result = true;
                }
                else if (pointerContext == TextPointerContext.ElementStart && TextSchema.IsFormattingType(pointer.ParentType))
                {
                    result = true;
                }
            }
            return(result);
        }
Exemplo n.º 3
0
        private void TextInput_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textInput.Document == null)
            {
                return;
            }

            TextRange documentRange = new TextRange(textInput.Document.ContentStart, textInput.Document.ContentEnd);

            documentRange.ClearAllProperties();

            TextPointer navigator = textInput.Document.ContentStart;

            while (navigator.CompareTo(textInput.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    try
                    {
                        CheckWordsInRun((Run)navigator.Parent);
                    }
                    catch { }
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }
            Format();
        }
Exemplo n.º 4
0
        // Token: 0x06002C19 RID: 11289 RVA: 0x000C8360 File Offset: 0x000C6560
        private static DocumentSequenceTextPointer xGetClingDSTP(DocumentSequenceTextPointer thisTp, LogicalDirection direction)
        {
            TextPointerContext pointerContext = thisTp.ChildPointer.GetPointerContext(direction);

            if (pointerContext != TextPointerContext.None)
            {
                return(thisTp);
            }
            ChildDocumentBlock childDocumentBlock = thisTp.ChildBlock;
            ITextPointer       textPointer        = thisTp.ChildPointer;

            if (direction == LogicalDirection.Forward)
            {
                while (pointerContext == TextPointerContext.None)
                {
                    if (childDocumentBlock.IsTail)
                    {
                        break;
                    }
                    childDocumentBlock = childDocumentBlock.NextBlock;
                    textPointer        = childDocumentBlock.ChildContainer.Start;
                    pointerContext     = textPointer.GetPointerContext(direction);
                }
            }
            else
            {
                while (pointerContext == TextPointerContext.None && !childDocumentBlock.IsHead)
                {
                    childDocumentBlock = childDocumentBlock.PreviousBlock;
                    textPointer        = childDocumentBlock.ChildContainer.End;
                    pointerContext     = textPointer.GetPointerContext(direction);
                }
            }
            return(new DocumentSequenceTextPointer(childDocumentBlock, textPointer));
        }
        /// <summary>
        /// Gets an enumerator that will enumerate the Runs in the element being read.
        /// </summary>
        public IEnumerator <Run> GetEnumerator()
        {
            TextPointer t = doc.ContentStart;

            // Keep a TextPointer for FlowDocument.ContentEnd handy,
            // so we know when we're done.
            TextPointer e = doc.ContentEnd;


            // Keep going until the TextPointer is equal to or greater than ContentEnd.
            while ((t != null) && (t.CompareTo(e) < 0))
            {
                // Find the TextPointerContext that identifies the purpose
                // for this ContentElement.
                TextPointerContext ctx = t.GetPointerContext(LogicalDirection.Forward);


                if (ctx == TextPointerContext.Text && t.Parent is Run)
                {
                    yield return(t.Parent as Run);
                }


                // Advance to the next ContentElement in the FlowDocument.
                t = t.GetNextContextPosition(LogicalDirection.Forward);
            }
        }
        // Token: 0x06003C04 RID: 15364 RVA: 0x00114F28 File Offset: 0x00113128
        private static bool HasIllegalHyperlinkDescendant(TextElement element, bool throwIfIllegalDescendent)
        {
            TextPointer textPointer = element.ElementStart;
            TextPointer elementEnd  = element.ElementEnd;

            while (textPointer.CompareTo(elementEnd) < 0)
            {
                TextPointerContext pointerContext = textPointer.GetPointerContext(LogicalDirection.Forward);
                if (pointerContext == TextPointerContext.ElementStart)
                {
                    TextElement textElement = (TextElement)textPointer.GetAdjacentElement(LogicalDirection.Forward);
                    if (textElement is Hyperlink || textElement is AnchoredBlock)
                    {
                        if (throwIfIllegalDescendent)
                        {
                            throw new InvalidOperationException(SR.Get("TextSchema_IllegalHyperlinkChild", new object[]
                            {
                                textElement.GetType()
                            }));
                        }
                        return(true);
                    }
                }
                textPointer = textPointer.GetNextContextPosition(LogicalDirection.Forward);
            }
            return(false);
        }
Exemplo n.º 7
0
        // Returns true if passed textelement has any Hyperlink or AnchoredBlock descendant.
        // It this context, the element or one of its ancestors is a Hyperlink.
        private static bool HasIllegalHyperlinkDescendant(TextElement element, bool throwIfIllegalDescendent)
        {
            TextPointer start = element.ElementStart;
            TextPointer end   = element.ElementEnd;

            while (start.CompareTo(end) < 0)
            {
                TextPointerContext forwardContext = start.GetPointerContext(LogicalDirection.Forward);
                if (forwardContext == TextPointerContext.ElementStart)
                {
                    TextElement nextElement = (TextElement)start.GetAdjacentElement(LogicalDirection.Forward);

                    if (nextElement is Hyperlink ||
                        nextElement is AnchoredBlock)
                    {
                        if (throwIfIllegalDescendent)
                        {
                            throw new InvalidOperationException(SR.Get(SRID.TextSchema_IllegalHyperlinkChild, nextElement.GetType()));
                        }
                        return(true);
                    }
                }

                start = start.GetNextContextPosition(LogicalDirection.Forward);
            }
            return(false);
        }
        //get the text, separate each word, check for first char if its @ or #, if it's, match against rules, then format else do nothing
        private void TextChangedEventHandler(object sender, TextChangedEventArgs e)
        {
            if (Text_Input.Document == null)
            {
                return;
            }

            TextRange documentRange = new TextRange(Text_Input.Document.ContentStart, Text_Input.Document.ContentEnd);

            documentRange.ClearAllProperties();


            TextPointer navigator = Text_Input.Document.ContentStart; //get the start of the input

            //inplace to remove invisible tags and match char position with text position, stackoverflow.com/questions/2565783/wpf-flowdocument-absolute-character-position
            while (navigator.CompareTo(Text_Input.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    checkWords((Run)navigator.Parent);
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }
            Format(words);
        }
Exemplo n.º 9
0
        public void Find(string _find)
        {
            TextRange documentRange = new TextRange(this.Document.ContentStart, this.Document.ContentEnd);

            documentRange.ClearAllProperties();
            _trs.Clear();
            TextPointer navigator = this.Document.ContentStart;

            while (navigator.CompareTo(this.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    Run    r    = ((Run)navigator.Parent);
                    string text = r.Text;

                    Match m = Regex.Match(text, _find);
                    if (m.Success)
                    {
                        TextPointer tp    = r.ElementStart;
                        TextRange   range = new TextRange(tp.GetPositionAtOffset(m.Index + 1), tp.GetPositionAtOffset(m.Index + m.Length + 1));
                        _trs.Add(range);
                    }
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }
            foreach (TextRange range in _trs)
            {
                range.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(Colors.Yellow));
            }
            FindNext();
        }
Exemplo n.º 10
0
        private static void PlainConvertParagraphEnd(StringBuilder textBuffer, ref TextPointer navigator)
        {
            //	navigator = navigator.GetInsertionPosition(LogicalDirection.Backward);
            bool flag = navigator.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart;

            navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            //	navigator = navigator.GetInsertionPosition(LogicalDirection.Forward);
            TextPointerContext pointerContext = navigator.GetPointerContext(LogicalDirection.Forward);

            if ((flag && (pointerContext == TextPointerContext.ElementEnd)) &&
                typeof(TableCell).IsAssignableFrom(navigator.Parent.GetType()))
            {
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);

                if (navigator.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementStart)
                {
                    textBuffer.Append('\t');
                }
                else
                {
                    textBuffer.Append(Environment.NewLine);
                }
            }
            else
            {
                textBuffer.Append(Environment.NewLine);
            }
        }
        /// <summary>
        /// Test if the text pointer is in a hyperlink
        /// </summary>
        /// <param name="pos"></param>
        /// <returns></returns>
        private bool searchHyperlink(TextPointer pos)
        {
            if (null == pos)
            {
                return(false);
            }

            TextPointerContext context = pos.GetPointerContext(LogicalDirection.Backward);

            if (TextPointerContext.ElementStart == context)
            {
                object elem = pos.GetAdjacentElement(LogicalDirection.Backward);
                if (elem is Hyperlink)
                {
                    return(true);
                }
            }
            else if (TextPointerContext.ElementEnd == context)
            {
                object elem = pos.GetAdjacentElement(LogicalDirection.Backward);
                if (elem is Hyperlink)
                {
                    return(false);
                }
            }

            // go backwards...
            TextPointer back = pos.GetNextContextPosition(LogicalDirection.Backward);

            return(searchHyperlink(back));
        }
Exemplo n.º 12
0
        private void txtStatus_TextChanged(object sender, TextChangedEventArgs e)
        {
            string desktopPath  = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            string settingsPath = desktopPath + "\\CodeVoidProject\\CodeVoid\\CodeVoidWPF\\bin\\Debug\\Data\\DarkModeFix.txt";

            using (StreamReader sr = new StreamReader(settingsPath))
            {
                string line;
                line = sr.ReadLine();
                if (line.Contains("DarkMode:True"))
                {
                    txtStatus.Background = Brushes.DarkGray;
                }
                else
                {
                    //do something..
                }
            }

            if (txtStatus.Document == null)
            {
                return;
            }
            txtStatus.TextChanged -= txtStatus_TextChanged;

            m_tags.Clear();

            TextPointer navigator = txtStatus.Document.ContentStart;

            while (navigator.CompareTo(txtStatus.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    text = ((Run)navigator.Parent).Text; //fix 2
                    if (text != "")
                    {
                        CheckWordsInRun((Run)navigator.Parent);
                    }
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }

            for (int i = 0; i < m_tags.Count; i++)
            {
                try
                {
                    TextRange range = new TextRange(m_tags[i].StartPosition, m_tags[i].EndPosition);
                    range.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(Colors.Blue));
                    range.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);
                }
                catch { }
            }
            txtStatus.TextChanged += txtStatus_TextChanged;
        }
Exemplo n.º 13
0
        // Token: 0x060068B0 RID: 26800 RVA: 0x001D8BE4 File Offset: 0x001D6DE4
        protected override BaseParagraph GetParagraph(ITextPointer textPointer, bool fEmptyOk)
        {
            Invariant.Assert(textPointer is TextPointer);
            BaseParagraph baseParagraph = null;

            while (baseParagraph == null)
            {
                TextPointerContext pointerContext = textPointer.GetPointerContext(LogicalDirection.Forward);
                if (pointerContext == TextPointerContext.ElementStart)
                {
                    TextElement adjacentElementFromOuterPosition = ((TextPointer)textPointer).GetAdjacentElementFromOuterPosition(LogicalDirection.Forward);
                    if (adjacentElementFromOuterPosition is ListItem)
                    {
                        baseParagraph = new ListItemParagraph(adjacentElementFromOuterPosition, base.StructuralCache);
                        break;
                    }
                    if (adjacentElementFromOuterPosition is List)
                    {
                        baseParagraph = new ListParagraph(adjacentElementFromOuterPosition, base.StructuralCache);
                        break;
                    }
                    if (((TextPointer)textPointer).IsFrozen)
                    {
                        textPointer = textPointer.CreatePointer();
                    }
                    textPointer.MoveToPosition(adjacentElementFromOuterPosition.ElementEnd);
                }
                else if (pointerContext == TextPointerContext.ElementEnd)
                {
                    if (base.Element == ((TextPointer)textPointer).Parent)
                    {
                        break;
                    }
                    if (((TextPointer)textPointer).IsFrozen)
                    {
                        textPointer = textPointer.CreatePointer();
                    }
                    textPointer.MoveToNextContextPosition(LogicalDirection.Forward);
                }
                else
                {
                    if (((TextPointer)textPointer).IsFrozen)
                    {
                        textPointer = textPointer.CreatePointer();
                    }
                    textPointer.MoveToNextContextPosition(LogicalDirection.Forward);
                }
            }
            if (baseParagraph != null)
            {
                base.StructuralCache.CurrentFormatContext.DependentMax = (TextPointer)textPointer;
            }
            return(baseParagraph);
        }
Exemplo n.º 14
0
        /// <summary>
        /// <see cref="ITextPointer.GetAdjacentElement"/>
        /// </summary>
        /// <remarks>Return null if the embedded object does not exist</remarks>
        object ITextPointer.GetAdjacentElement(LogicalDirection direction)
        {
            ValidationHelper.VerifyDirection(direction, "direction");
            TextPointerContext tpc = _flowPosition.GetPointerContext(direction);

            if (!(tpc == TextPointerContext.EmbeddedElement || tpc == TextPointerContext.ElementStart || tpc == TextPointerContext.ElementEnd))
            {
                return(null);
            }
            return(_flowPosition.GetAdjacentElement(direction));
        }
        // Token: 0x06002EB3 RID: 11955 RVA: 0x000D300C File Offset: 0x000D120C
        object ITextPointer.GetAdjacentElement(LogicalDirection direction)
        {
            ValidationHelper.VerifyDirection(direction, "direction");
            TextPointerContext pointerContext = this._flowPosition.GetPointerContext(direction);

            if (pointerContext != TextPointerContext.EmbeddedElement && pointerContext != TextPointerContext.ElementStart && pointerContext != TextPointerContext.ElementEnd)
            {
                return(null);
            }
            return(this._flowPosition.GetAdjacentElement(direction));
        }
Exemplo n.º 16
0
        // Token: 0x06007CED RID: 31981 RVA: 0x0023255C File Offset: 0x0023075C
        private static FlowDirection GetTextFlowDirection(ITextPointer pointer)
        {
            Invariant.Assert(pointer != null, "Null pointer passed.");
            Invariant.Assert(pointer.IsAtInsertionPosition, "Pointer is not an insertion position");
            int num = 0;
            LogicalDirection   logicalDirection = pointer.LogicalDirection;
            TextPointerContext pointerContext   = pointer.GetPointerContext(logicalDirection);
            FlowDirection      result;

            if ((pointerContext == TextPointerContext.ElementEnd || pointerContext == TextPointerContext.ElementStart) && !TextSchema.IsFormattingType(pointer.ParentType))
            {
                result = (FlowDirection)pointer.GetValue(FrameworkElement.FlowDirectionProperty);
            }
            else
            {
                Rect         anchorRectangle = TextSelectionHelper.GetAnchorRectangle(pointer);
                ITextPointer textPointer     = pointer.GetNextInsertionPosition(logicalDirection);
                if (textPointer != null)
                {
                    textPointer = textPointer.CreatePointer((logicalDirection == LogicalDirection.Backward) ? LogicalDirection.Forward : LogicalDirection.Backward);
                    if (logicalDirection == LogicalDirection.Forward)
                    {
                        if (pointerContext == TextPointerContext.ElementEnd && textPointer.GetPointerContext(textPointer.LogicalDirection) == TextPointerContext.ElementStart)
                        {
                            return((FlowDirection)pointer.GetValue(FrameworkElement.FlowDirectionProperty));
                        }
                    }
                    else if (pointerContext == TextPointerContext.ElementStart && textPointer.GetPointerContext(textPointer.LogicalDirection) == TextPointerContext.ElementEnd)
                    {
                        return((FlowDirection)pointer.GetValue(FrameworkElement.FlowDirectionProperty));
                    }
                    Rect anchorRectangle2 = TextSelectionHelper.GetAnchorRectangle(textPointer);
                    if (anchorRectangle2 != Rect.Empty && anchorRectangle != Rect.Empty)
                    {
                        num = Math.Sign(anchorRectangle2.Left - anchorRectangle.Left);
                        if (logicalDirection == LogicalDirection.Backward)
                        {
                            num = -num;
                        }
                    }
                }
                if (num == 0)
                {
                    result = (FlowDirection)pointer.GetValue(FrameworkElement.FlowDirectionProperty);
                }
                else
                {
                    result = ((num > 0) ? FlowDirection.LeftToRight : FlowDirection.RightToLeft);
                }
            }
            return(result);
        }
Exemplo n.º 17
0
        //--------------------------------------------------------------------
        // FlowPosition --> FixedPosition
        //---------------------------------------------------------------------

        // Helper function to overcome the limitation in FixedTextBuilder.GetFixedPosition
        // Making sure we are asking a position that is either a Run or an EmbeddedElement.
        private bool _GetFixedPosition(FixedTextPointer ftp, out FixedPosition fixedp)
        {
            LogicalDirection   textdir    = ftp.LogicalDirection;
            TextPointerContext symbolType = ((ITextPointer)ftp).GetPointerContext(textdir);

            if (ftp.FlowPosition.IsBoundary || symbolType == TextPointerContext.None)
            {
                return(_GetFirstFixedPosition(ftp, out fixedp));
            }

            if (symbolType == TextPointerContext.ElementStart || symbolType == TextPointerContext.ElementEnd)
            {
                //Try to find the first valid insertion position if exists
                switch (symbolType)
                {
                case TextPointerContext.ElementStart:
                    textdir = LogicalDirection.Forward;
                    break;

                case TextPointerContext.ElementEnd:
                    textdir = LogicalDirection.Backward;
                    break;
                }

                FixedTextPointer nav = new FixedTextPointer(true, textdir, (FlowPosition)ftp.FlowPosition.Clone());

                _SkipFormattingTags(nav);

                symbolType = ((ITextPointer)nav).GetPointerContext(textdir);
                if (symbolType != TextPointerContext.Text && symbolType != TextPointerContext.EmbeddedElement)
                {
                    //Try moving to the next insertion position
                    if (((ITextPointer)nav).MoveToNextInsertionPosition(textdir) &&
                        this.Container.GetPageNumber(nav) == this.PageIndex)
                    {
                        return(Container.FixedTextBuilder.GetFixedPosition(nav.FlowPosition, textdir, out fixedp));
                    }
                    else
                    {
                        fixedp = new FixedPosition(this.Container.FixedTextBuilder.FixedFlowMap.FixedStartEdge, 0);
                        return(false);
                    }
                }
                else
                {
                    ftp = nav;
                }
            }

            Debug.Assert(symbolType == TextPointerContext.Text || symbolType == TextPointerContext.EmbeddedElement);
            return(Container.FixedTextBuilder.GetFixedPosition(ftp.FlowPosition, textdir, out fixedp));
        }
        private void ChangeFontSize(TextPointer currentPoint, double size, TextPointer endingPoint, bool increase)
        {
            // currentPoint can split text, but size is the text's size based on the enclosing tag

            // find the next position
            TextPointer next = currentPoint.GetNextContextPosition(LogicalDirection.Forward);
            //TextPointerContext nextCtx = currentPoint.GetPointerContext(LogicalDirection.Forward);

            int         currentOffset = next.GetOffsetToPosition(endingPoint);
            TextPointer endOfApply    = (currentOffset <= 0) ? endingPoint : next;

            // apply the style for this portion
            TextRange range = new TextRange(currentPoint, endOfApply);

            try
            {
                string text    = currentPoint.GetTextInRun(LogicalDirection.Forward);
                double newSize = increase ? GetNextFontSize(size) : GetPreviousFontSize(size);
                range.ApplyPropertyValue(TextElement.FontSizeProperty, newSize);
            }
            catch (Exception)
            {
                // ignore
                return;
            }

            if (currentOffset <= 0)
            {
                return; // done
            }
            // what's next?
            object             element = endOfApply.GetAdjacentElement(LogicalDirection.Forward);
            TextPointerContext ctx     = endOfApply.GetPointerContext(LogicalDirection.Forward);

            if (element is UIElement)
            {
                UIElement uiElement = element as UIElement;
            }
            else if (element is TextElement)
            {
                TextElement textElement = element as TextElement;
                size = textElement.FontSize;
                ChangeFontSize(endOfApply, size, endingPoint, increase);
            }
            else
            {
                ChangeFontSize(endOfApply, size, endingPoint, increase);
            }

            return; // don't know what to do = done
        }
Exemplo n.º 19
0
    public static IEnumerable <Stack <DependencyObject> > WalkTextHierarchy(this FlowDocument doc)
    {
        if (doc == null)
        {
            throw new ArgumentNullException();
        }
        var stack = new Stack <DependencyObject>();
        // Keep a TextPointer for FlowDocument.ContentEnd handy, so we know when we're done.
        TextPointer docEnd = doc.ContentEnd;

        // Keep going until the TextPointer is equal to or greater than ContentEnd.
        for (var iterator = doc.ContentStart;
             (iterator != null) && (iterator.CompareTo(docEnd) < 0);
             iterator = iterator.GetNextContextPosition(LogicalDirection.Forward))
        {
            var parent = iterator.Parent;
            // Identify the type of content immediately adjacent to the text pointer.
            TextPointerContext context = iterator.GetPointerContext(LogicalDirection.Forward);
            switch (context)
            {
            case TextPointerContext.ElementStart:
            case TextPointerContext.EmbeddedElement:
            case TextPointerContext.Text:
                PushElement(stack, parent);
                yield return(stack);

                break;

            case TextPointerContext.ElementEnd:
                break;

            default:
                throw new System.Exception("Unhandled TextPointerContext " + context.ToString());
            }
            switch (context)
            {
            case TextPointerContext.ElementEnd:
            case TextPointerContext.EmbeddedElement:
            case TextPointerContext.Text:
                PopToElement(stack, parent);
                break;

            case TextPointerContext.ElementStart:
                break;

            default:
                throw new System.Exception("Unhandled TextPointerContext " + context.ToString());
            }
        }
    }
Exemplo n.º 20
0
        /// <summary>
        /// <see cref="ITextPointer.GetElementType"/>
        /// </summary>
        /// <remarks>Return null if no TextElement in the direction</remarks>
        Type ITextPointer.GetElementType(LogicalDirection direction)
        {
            ValidationHelper.VerifyDirection(direction, "direction");

            TextPointerContext tt = _flowPosition.GetPointerContext(direction);

            if (tt == TextPointerContext.ElementStart || tt == TextPointerContext.ElementEnd)
            {
                FixedElement e = _flowPosition.GetElement(direction);
                return(e.IsTextElement ? e.Type : null);
            }

            return(null);
        }
Exemplo n.º 21
0
        private void Run_MouseMove(object sender, MouseEventArgs e)
        {
            ARun run = sender as ARun;

            switch (CurrentState)
            {
            case State.Stop:
                run.Cursor = Cursors.Hand;
                break;

            case State.Play:
                run.Cursor = Cursors.Hand;
                break;

            case State.Segment:
                if (run is RunImage)
                {
                    run.Cursor = Cursors.Hand;
                    break;
                }
                RichTextBox rtb = run.IsImage ? ImageCaptionRTB : BookContentRTB;
                rtb.Focus();
                TextPointer pointer = rtb.GetPositionFromPoint(e.GetPosition(run), false);
                rtb.CaretPosition = pointer;
                TextPointerContext contextPrev = pointer.GetPointerContext(GoBackward);
                TextPointerContext contextNext = pointer.GetPointerContext(GoForward);
                if (contextNext == TextPointerContext.ElementEnd || contextPrev == TextPointerContext.ElementStart)
                {
                    run.Cursor = Cursors.No;
                }
                else if (contextNext == contextPrev && contextNext == TextPointerContext.Text)
                {
                    run.Cursor = Cursors.UpArrow;
                }
                else
                {
                    run.Cursor = Cursors.Arrow;
                }
                break;

            case State.Edit:
                run.Cursor = Cursors.Hand;
                break;

            case State.Caption:
                break;
            }
        }
Exemplo n.º 22
0
        // Token: 0x06003282 RID: 12930 RVA: 0x000DD010 File Offset: 0x000DB210
        internal static bool HasNoTextContent(Paragraph paragraph)
        {
            ITextPointer textPointer = paragraph.ContentStart.CreatePointer();
            ITextPointer contentEnd  = paragraph.ContentEnd;

            while (textPointer.CompareTo(contentEnd) < 0)
            {
                TextPointerContext pointerContext = textPointer.GetPointerContext(LogicalDirection.Forward);
                if (pointerContext == TextPointerContext.Text || pointerContext == TextPointerContext.EmbeddedElement || typeof(LineBreak).IsAssignableFrom(textPointer.ParentType) || typeof(AnchoredBlock).IsAssignableFrom(textPointer.ParentType))
                {
                    return(false);
                }
                textPointer.MoveToNextContextPosition(LogicalDirection.Forward);
            }
            return(true);
        }
Exemplo n.º 23
0
        // </Snippet_TextPointer_GetOffsetToPosition>

        // <Snippet_TextPointer_GetOffsetToPosition2>
        // Calculate and return the relative balance of opening and closing element tags
        // between two specified TextPointers.
        int GetElementTagBalance(TextPointer start, TextPointer end)
        {
            int balance = 0;
         
            while (start != null && start.CompareTo(end) < 0)
            {
                TextPointerContext forwardContext = start.GetPointerContext(LogicalDirection.Forward);
         
                if (forwardContext == TextPointerContext.ElementStart)     balance++;
                else if (forwardContext == TextPointerContext.ElementEnd)  balance--;
                     
                start = start.GetNextContextPosition(LogicalDirection.Forward);
            } // End while.
         
            return balance;
        } // End GetElementTagBalance
            // Token: 0x06008E5B RID: 36443 RVA: 0x0025BE9C File Offset: 0x0025A09C
            private void GetContent()
            {
                this._contentSegments.Clear();
                ITextPointer textPointer  = this._segment.Start.CreatePointer();
                ITextPointer textPointer2 = null;

                while (textPointer.CompareTo(this._segment.End) < 0)
                {
                    TextPointerContext pointerContext = textPointer.GetPointerContext(LogicalDirection.Forward);
                    if (pointerContext == TextPointerContext.ElementStart)
                    {
                        Type elementType = textPointer.GetElementType(LogicalDirection.Forward);
                        if (typeof(Run).IsAssignableFrom(elementType) || typeof(BlockUIContainer).IsAssignableFrom(elementType))
                        {
                            this.OpenSegment(ref textPointer2, textPointer);
                        }
                        else if (typeof(Table).IsAssignableFrom(elementType) || typeof(Floater).IsAssignableFrom(elementType) || typeof(Figure).IsAssignableFrom(elementType))
                        {
                            this.CloseSegment(ref textPointer2, textPointer, this._segment.End);
                        }
                        textPointer.MoveToNextContextPosition(LogicalDirection.Forward);
                        if (typeof(Run).IsAssignableFrom(elementType) || typeof(BlockUIContainer).IsAssignableFrom(elementType))
                        {
                            textPointer.MoveToElementEdge(ElementEdge.AfterEnd);
                        }
                    }
                    else if (pointerContext == TextPointerContext.ElementEnd)
                    {
                        Type parentType = textPointer.ParentType;
                        if (typeof(TableCell).IsAssignableFrom(parentType) || typeof(Floater).IsAssignableFrom(parentType) || typeof(Figure).IsAssignableFrom(parentType))
                        {
                            this.CloseSegment(ref textPointer2, textPointer, this._segment.End);
                        }
                        textPointer.MoveToNextContextPosition(LogicalDirection.Forward);
                    }
                    else if (pointerContext == TextPointerContext.Text || pointerContext == TextPointerContext.EmbeddedElement)
                    {
                        this.OpenSegment(ref textPointer2, textPointer);
                        textPointer.MoveToNextContextPosition(LogicalDirection.Forward);
                    }
                    else
                    {
                        Invariant.Assert(false, "unexpected TextPointerContext");
                    }
                }
                this.CloseSegment(ref textPointer2, textPointer, this._segment.End);
            }
        // Token: 0x06006855 RID: 26709 RVA: 0x001D677C File Offset: 0x001D497C
        private TextPointer FindElementPosition(IInputElement e, bool isLimitedToTextView)
        {
            TextPointer textPointer = null;

            if (e is TextElement)
            {
                if ((e as TextElement).TextContainer == this._structuralCache.TextContainer)
                {
                    textPointer = new TextPointer((e as TextElement).ElementStart);
                }
            }
            else
            {
                if (this._structuralCache.TextContainer.Start == null || this._structuralCache.TextContainer.End == null)
                {
                    return(null);
                }
                TextPointer textPointer2 = new TextPointer(this._structuralCache.TextContainer.Start);
                while (textPointer == null && ((ITextPointer)textPointer2).CompareTo(this._structuralCache.TextContainer.End) < 0)
                {
                    TextPointerContext pointerContext = textPointer2.GetPointerContext(LogicalDirection.Forward);
                    if (pointerContext == TextPointerContext.EmbeddedElement)
                    {
                        DependencyObject adjacentElement = textPointer2.GetAdjacentElement(LogicalDirection.Forward);
                        if ((adjacentElement is ContentElement || adjacentElement is UIElement) && (adjacentElement == e as ContentElement || adjacentElement == e as UIElement))
                        {
                            textPointer = new TextPointer(textPointer2);
                        }
                    }
                    textPointer2.MoveToNextContextPosition(LogicalDirection.Forward);
                }
            }
            if (textPointer != null && isLimitedToTextView)
            {
                this._textView = this.GetTextView();
                Invariant.Assert(this._textView != null);
                for (int i = 0; i < ((ITextView)this._textView).TextSegments.Count; i++)
                {
                    if (((ITextPointer)textPointer).CompareTo(((ITextView)this._textView).TextSegments[i].Start) >= 0 && ((ITextPointer)textPointer).CompareTo(((ITextView)this._textView).TextSegments[i].End) < 0)
                    {
                        return(textPointer);
                    }
                }
                textPointer = null;
            }
            return(textPointer);
        }
Exemplo n.º 26
0
        private void CopyTextCommand(object sender, ExecutedRoutedEventArgs ea)
        {
            const LogicalDirection forward   = LogicalDirection.Forward;
            TextSelection          selection = TbLog.Selection;
            TextPointer            navigator = selection.Start.GetPositionAtOffset(0, forward);
            TextPointer            end       = selection.End;
            var buffer = new StringBuilder();

            int offsetToEnd;

            do
            {
                offsetToEnd = navigator.GetOffsetToPosition(end);
                TextPointerContext context = navigator.GetPointerContext(forward);
                if (context == TextPointerContext.Text)
                {
                    string blockText  = navigator.GetTextInRun(forward);
                    int    croppedLen = Math.Min(offsetToEnd, navigator.GetTextRunLength(forward));
                    _ = buffer.Append(blockText, 0, croppedLen);
                }
                else if (
                    context == TextPointerContext.ElementEnd &&
                    navigator.Parent is Paragraph
                    )
                {
                    _ = buffer.AppendLine();
                }
                else if (
                    navigator.Parent is BlockUIContainer block &&
                    block.Child is WorkProgress progress &&
                    progress.DataContext is Jp2KrTranslationVM work
                    )
                {
                    foreach (Exception e in work.Exceptions)
                    {
                        _ = buffer.AppendLine(e.ToString());
                        _ = buffer.AppendLine();
                    }
                }
                navigator = navigator.GetNextContextPosition(forward);
            }while (offsetToEnd > 0);

            string txt = buffer.ToString();

            Clipboard.SetText(txt);
            ea.Handled = true;
        }
        private TextPointer SeekEnclosingStartTag(TextPointer point)
        {
            if (null == point)
            {
                return(null);
            }

            TextPointerContext ctx = point.GetPointerContext(LogicalDirection.Backward);

            if (TextPointerContext.ElementStart == ctx)
            {
                return(point);
            }

            // seek backward...
            return(SeekEnclosingStartTag(point.GetNextContextPosition(LogicalDirection.Backward)));
        }
Exemplo n.º 28
0
 private TextPointer GetPointer(TextPointer docEnd, TextPointer startPointer, int offset)
 {
     try
     {
         int findEnd = offset, textLength = 0;
         while (startPointer != null && docEnd.CompareTo(startPointer) > -1)
         {
             while (startPointer != null && startPointer.GetPointerContext(LogicalDirection.Forward) != TextPointerContext.Text)
             {
                 TextPointerContext context = startPointer.GetPointerContext(LogicalDirection.Forward);
                 if (context == TextPointerContext.ElementEnd)
                 {
                     var next = startPointer.GetNextContextPosition(LogicalDirection.Forward);
                     if (next != null && next.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.ElementEnd)
                     {
                         textLength  += 2;
                         offset      -= 2;
                         startPointer = next;
                     }
                 }
                 startPointer = startPointer.GetNextContextPosition(LogicalDirection.Forward);
             }
             if (startPointer == null)
             {
                 return(docEnd);
             }
             var len = startPointer.GetTextRunLength(LogicalDirection.Forward);
             if (textLength + len < findEnd)
             {
                 textLength  += len;
                 offset      -= len;
                 startPointer = startPointer.GetNextContextPosition(LogicalDirection.Forward);
             }
             else
             {
                 break;
             }
         }
         var tp = startPointer.GetPositionAtOffset(offset, LogicalDirection.Forward);
         return(tp);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 29
0
        public void Highlight(string regexStr)
        {
            Text = Text; //clean

            var         regex     = CreateRegex(regexStr);
            TextPointer navigator = tbxRich.Document.ContentStart;

            while (navigator.CompareTo(tbxRich.Document.ContentEnd) < 0)
            {
                TextPointerContext context = navigator.GetPointerContext(LogicalDirection.Backward);
                if (context == TextPointerContext.ElementStart && navigator.Parent is Run)
                {
                    HighlightRun((Run)navigator.Parent, regex);
                }
                navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
            }
        }
        // Token: 0x06002EB4 RID: 11956 RVA: 0x000D304C File Offset: 0x000D124C
        Type ITextPointer.GetElementType(LogicalDirection direction)
        {
            ValidationHelper.VerifyDirection(direction, "direction");
            TextPointerContext pointerContext = this._flowPosition.GetPointerContext(direction);

            if (pointerContext != TextPointerContext.ElementStart && pointerContext != TextPointerContext.ElementEnd)
            {
                return(null);
            }
            FixedElement element = this._flowPosition.GetElement(direction);

            if (!element.IsTextElement)
            {
                return(null);
            }
            return(element.Type);
        }
Exemplo n.º 31
0
        private void RectFromDcpCompositeLines( 
            int dcp,
            int originalDcp,
            LogicalDirection orientation,
            TextPointerContext context, 
            ref PTS.FSTEXTDETAILSFULL textDetails,
            ref Rect rect, 
            ref int vrBaseline) 
        {
            ErrorHandler.Assert(!PTS.ToBoolean(textDetails.fDropCapPresent), ErrorHandler.NotSupportedDropCap); 

            // Get list of lines
            PTS.FSLINEDESCRIPTIONCOMPOSITE [] arrayLineDesc;
            PtsHelper.LineListCompositeFromTextPara(PtsContext, _paraHandle.Value, ref textDetails, out arrayLineDesc); 

            // First iterate through lines 
            for (int index = 0; index < arrayLineDesc.Length; index++) 
            {
                PTS.FSLINEDESCRIPTIONCOMPOSITE lineDesc = arrayLineDesc[index]; 
                if (lineDesc.cElements == 0) { continue; }

                // Get list of line elements.
                PTS.FSLINEELEMENT [] arrayLineElement; 
                PtsHelper.LineElementListFromCompositeLine(PtsContext, ref lineDesc, out arrayLineElement);
 
                for (int elIndex = 0; elIndex < arrayLineElement.Length; elIndex++) 
                {
                    PTS.FSLINEELEMENT element = arrayLineElement[elIndex]; 

                    // 'dcp' needs to be within line range. If position points to dcpLim,
                    // it means that the next line starts from such position, hence go to the next line.
                    // But if this is the last line (EOP character), get rectangle form the last 
                    // character of the line.
                    if (   ((element.dcpFirst <= dcp) && (element.dcpLim > dcp)) 
                        || ((element.dcpLim == dcp) && (elIndex == arrayLineElement.Length - 1) && (index == arrayLineDesc.Length - 1))) 
                    {
                        // Create and format line 
                        Line line = new Line(Paragraph.StructuralCache.TextFormatterHost, this, Paragraph.ParagraphStartCharacterPosition);
                        Line.FormattingContext ctx = new Line.FormattingContext(false, PTS.ToBoolean(element.fClearOnLeft), PTS.ToBoolean(element.fClearOnRight), TextParagraph.TextRunCache);

                        if(IsOptimalParagraph) 
                        {
                            ctx.LineFormatLengthTarget = element.dcpLim - element.dcpFirst; 
                        } 

                        TextParagraph.FormatLineCore(line, element.pfsbreakreclineclient, ctx, element.dcpFirst, element.dur, PTS.ToBoolean(lineDesc.fTreatedAsFirst), element.dcpFirst); 

                        // Assert that number of characters in Text line is the same as our expected length
                        Invariant.Assert(line.SafeLength == element.dcpLim - element.dcpFirst, "Line length is out of [....]");
 
                        // Get rect from cp
                        FlowDirection flowDirection; 
                        rect = line.GetBoundsFromTextPosition(dcp, out flowDirection); 
                        rect.X += TextDpi.FromTextDpi(element.urStart);
                        rect.Y += TextDpi.FromTextDpi(lineDesc.vrStart); 

                        // Return only TopLeft and Height.
                        // Adjust rect.Left by taking into account flow direction of the
                        // content and orientation of input position. 
                        if (ThisFlowDirection != flowDirection)
                        { 
                            if (orientation == LogicalDirection.Forward) 
                            {
                                rect.X = rect.Right; 
                            }
                        }
                        else
                        { 
                            // NOTE: check for 'originalCharacterIndex > 0' is only required for position at the beginning
                            //       content with Backward orientation. This should not be a valid position. 
                            //       Remove it later 
                            if (orientation == LogicalDirection.Backward && originalDcp > 0 && (context == TextPointerContext.Text || context == TextPointerContext.EmbeddedElement))
                            { 
                                rect.X = rect.Right;
                            }
                        }
                        rect.Width = 0; 

                        vrBaseline = line.Baseline + lineDesc.vrStart; 
 
                        // Dispose the line
                        line.Dispose(); 
                        break;
                    }
                }
            } 
        }
Exemplo n.º 32
0
 /// <summary>
 /// Checks whether symbol type refers to element boundary.
 /// </summary>
 private bool IsElementBoundary(TextPointerContext symbolType)
 {
     return ((symbolType == TextPointerContext.ElementStart) || (symbolType == TextPointerContext.ElementEnd));
 }