示例#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);
        }
示例#2
0
        /// <summary>
        /// Pad the line of the text pointer to the length specified.
        /// </summary>
        /// <param name="Doc"></param>
        /// <param name="Pointer"></param>
        /// <param name="Length"></param>
        public static void PadLineToLength(
            this TextPointer Pointer, FlowDocument Doc, int Length, char PadChar = ' ')
        {
            TextPointer tp1 = Pointer.GetLineStartPosition(0);
            TextPointer tp2 = Pointer.GetLineStartPosition(1);

            if (tp2 != null)
            {
                tp2 = tp2.GetNextInsertionPosition(LogicalDirection.Backward);
            }
            else
            {
                tp2 = Doc.ContentEnd.GetNextInsertionPosition(LogicalDirection.Backward);
            }

            // current line text.
            TextRange r1       = new TextRange(tp1, tp2);
            string    lineText = r1.Text;

            // pad length
            string padText = null;
            int    padLx   = Length - lineText.Length;

            if (padLx > 0)
            {
                padText = new StringBuilder().AppendRepeat(PadChar, padLx).ToString();
                tp2.InsertTextInRun(padText);
            }
        }
        private void ReplaceText(string text, string replaceText)
        {
            TextBox textBox = textBoxBase as TextBox;

            if (textBox != null)
            {
                StringBuilder sb          = new StringBuilder(text);
                int           removeStart = textBox.CaretIndex - text.Length;
                sb.Remove(removeStart, text.Length);
                sb.Insert(removeStart, replaceText);
                textBox.Text       = sb.ToString();
                textBox.CaretIndex = removeStart + replaceText.Length;
            }
            else if (textBoxBase is RichTextBox)
            {
                RichTextBox richTextBox = textBoxBase as RichTextBox;
                if (richTextBox.CaretPosition.LogicalDirection == LogicalDirection.Backward)
                {
                    richTextBox.CaretPosition = richTextBox.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);
                }
                TextPointer removePointer = richTextBox.CaretPosition.GetPositionAtOffset(-text.Length);
                removePointer.DeleteTextInRun(text.Length);
                removePointer.InsertTextInRun(replaceText);
                richTextBox.CaretPosition = removePointer;
            }
            return;
        }
示例#4
0
        public static void ReplaceWordAtPointer(TextPointer textPointer, string replacementWord)
        {
            textPointer.DeleteTextInRun(-GetWordCharactersBefore(textPointer).Count());
            textPointer.DeleteTextInRun(GetWordCharactersAfter(textPointer).Count());

            textPointer.InsertTextInRun(replacementWord);
        }
示例#5
0
        } // End IsEmptyElement method.
        // </Snippet_TextPointer_GetInsertionPosition>

        void GetXAML(object sender, RoutedEventArgs args) 
        {
            tb.Text = GetXaml(fdsv.Document.Blocks.FirstBlock);

            TextPointer tp = bold.ContentStart;

            tp.InsertTextInRun("Inserted text 1...");
            tp.InsertTextInRun("Inserted text 2...");

            tb.Text += GetXaml(fdsv.Document.Blocks.FirstBlock);
            tb.Text += GetXaml(fdsv.Document.Blocks.FirstBlock.NextBlock);
            
            buic.ContentStart.InsertParagraphBreak();

            tb.Text += GetXaml(fdsv.Document.Blocks.FirstBlock);
            tb.Text += GetXaml(fdsv.Document.Blocks.FirstBlock.NextBlock);
            tb.Text += GetXaml(fdsv.Document.Blocks.FirstBlock.NextBlock.NextBlock);
        }
        string SetUserText(string newText)
        {
            TextPointer selection = ConsoleTextBox.Selection.Start;
            int         len       = selection.GetTextRunLength(LogicalDirection.Backward);
            string      text      = selection.GetTextInRun(LogicalDirection.Backward).Trim();

            // delete the user input (so console can echo it back in green).
            len--; // not including the leading space which we keep to stop the runs from getting merged.
            selection.GetPositionAtOffset(-len).DeleteTextInRun(len);
            selection.InsertTextInRun(newText);
            return(text);
        }
示例#7
0
        void PasteCommand(object sender, ExecutedRoutedEventArgs e)
        {
            if (Clipboard.ContainsText())
            {
                string      text = Clipboard.GetText();
                TextPointer p    = this.CaretPosition;
                p.InsertTextInRun(text);
                p             = this.CaretPosition;
                CaretPosition = p.GetPositionAtOffset(p.GetTextRunLength(LogicalDirection.Forward));

                LinksCheck(p);
                return;
            }
        }
示例#8
0
        private void TextEntered(string text)
        {
            Debug.WriteLine(String.Format("Text entered: '{0}'", text));

            // Удалить то количество символов, которое указано в text из ввода.
            if (__ImeJustPrecessed)
            {
                TextPointer remove_start = Selection.Start;
                TextPointer remove_end   = remove_start.GetPositionAtOffset(-text.Length);
                TextRange   remove_range = new TextRange(remove_start, remove_end);
                remove_range.Text = "";
            }

            if (!Selection.IsEmpty)
            {
                PushUndoAction(new FormatUndo(Document, Selection, this), true);

                TextPointer insertPoint = Selection.Start;

                Selection.Start.InsertTextInRun(text);
                TextPointer newPointer = insertPoint.GetPositionAtOffset(text.Length);
                Selection.Select(newPointer, Selection.End);
                Selection.Text = "";
                CaretPosition  = newPointer;
                Selection.Select(CaretPosition, CaretPosition);
                return;
            }

            TextPointer beforeInsert = CaretPosition.GetPositionAtOffset(0, LogicalDirection.Backward);
            TextPointer insert       = CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);

            insert.InsertTextInRun(text);
            CaretPosition = insert.GetPositionAtOffset(0, LogicalDirection.Backward);

            TextRange range = new TextRange(beforeInsert, CaretPosition);

            range.ApplyPropertyValue(RichTextBox.FontWeightProperty, Selection.GetPropertyValue(RichTextBox.FontWeightProperty));
            range.ApplyPropertyValue(RichTextBox.FontFamilyProperty, Selection.GetPropertyValue(RichTextBox.FontFamilyProperty));
            range.ApplyPropertyValue(RichTextBox.FontSizeProperty, Selection.GetPropertyValue(RichTextBox.FontSizeProperty));
            range.ApplyPropertyValue(RichTextBox.FontStyleProperty, Selection.GetPropertyValue(RichTextBox.FontStyleProperty));
            range.ApplyPropertyValue(TextBlock.TextDecorationsProperty, Selection.GetPropertyValue(TextBlock.TextDecorationsProperty));
            PushUndoAction(new UndoTextEnter(this, range, text), true);

            TextPointer endPointer = CaretPosition.GetPositionAtOffset(0, LogicalDirection.Backward);

            Selection.Select(CaretPosition, endPointer);

            LinksCheck(CaretPosition);
        }
        private void btnSeparator_click(object sender, RoutedEventArgs e)
        {
            TextPointer position = rtbMainText.Selection.Start;

            if (position.IsAtLineStartPosition)
            {
                position.InsertTextInRun("- - -");
                position.InsertLineBreak();
            }
            else
            {
                position = position.InsertLineBreak();
                position.GetPositionAtOffset(0, LogicalDirection.Forward).InsertTextInRun("- - -");
                position.GetPositionAtOffset(5, LogicalDirection.Forward).InsertLineBreak();
            }
        }
示例#10
0
 private void PreviewKeyDownHandler(object sender, KeyEventArgs e)
 {
     // Are we seeing a key press before preceding character is processed?
     if (DelayActionFacility && changingText)
     {
         if (delayAction == null)
         {
             // Not done processing previous character --
             // -- set to redo in TextChanged event.
             delayAction = new Action(() => { PreviewKeyDownHandler(this, e); });
         }
         return;
     }
     // See if we're at the end of a word
     if (delimiterKeys.Contains(e.Key))
     {
         string word = GetPrecedingText();
         // Is the word an abbreviation?
         if (abbreviations.ContainsKey(word.ToUpper()))
         {
             TextPointer pointer = textbox1.CaretPosition.GetPositionAtOffset(-word.Length);
             if (pointer.LogicalDirection == LogicalDirection.Backward) // change direction, if needed
             {
                 pointer = pointer.GetPositionAtOffset(0, LogicalDirection.Forward);
                 textbox1.CaretPosition = pointer;
             }
             // replace abbreviation w/expansion
             pointer.DeleteTextInRun(word.Length);
             pointer.InsertTextInRun(abbreviations[word.ToUpper()]);
         }
         // Are we at the end of a line, and don't have a period?
         if (e.Key == Key.Enter && !word.EndsWith("."))
         {
             TextPointer pointer = textbox1.CaretPosition;
             if (pointer.LogicalDirection == LogicalDirection.Backward) // change direction, if needed
             {
                 pointer = pointer.GetPositionAtOffset(0, LogicalDirection.Forward);
                 textbox1.CaretPosition = pointer;
             }
             pointer.InsertTextInRun(".");
         }
     }
 }
示例#11
0
        private void InsertText(string text)
        {
            TextBox textBox = textBoxBase as TextBox;

            if (textBox != null)
            {
                textBox.Text      += text;
                textBox.CaretIndex = textBox.Text.Length;
            }
            else if (textBoxBase is RichTextBox)
            {
                RichTextBox richTextBox = textBoxBase as RichTextBox;
                if (richTextBox.CaretPosition.LogicalDirection == LogicalDirection.Backward)
                {
                    richTextBox.CaretPosition = richTextBox.CaretPosition.GetPositionAtOffset(0, LogicalDirection.Forward);
                }
                TextPointer pointer = richTextBox.CaretPosition;
                pointer.InsertTextInRun(text);
            }
            return;
        }
示例#12
0
        public void AppendText(string text)
        {
            if (OutputTextView.Document.Blocks.Count == 0)
            {
                OutputTextView.Document.Blocks.Add(new Paragraph());
                TextPointer pos = OutputTextView.Document.ContentEnd;
                OutputTextView.Selection.Select(pos, pos);
            }

            TextPointer ptr   = OutputTextView.Document.ContentEnd;
            int         delta = OutputTextView.Selection.End.GetOffsetToPosition(OutputTextView.Document.ContentEnd);

            ptr.InsertTextInRun(text);
            ptr = OutputTextView.Document.ContentEnd;
            ptr.InsertLineBreak();
            ptr = OutputTextView.Document.ContentEnd;

            if (delta < 10)
            {
                OutputTextView.Selection.Select(ptr, ptr);
                OutputTextView.ScrollToEnd();
            }
        }
示例#13
0
        /// <summary>
        /// Reads the document from the reader into the specified position.
        /// </summary>
        private void ReadDocument(XmlReader reader, TextPointer position)
        {
            // Initialize all fields.
            _cursor   = position;
            _dir      = LogicalDirection.Forward;
            _otherDir = LogicalDirection.Backward;
            _reader   = reader;

            // Set the gravity of the cursor to always look forward.
            _cursor = _cursor.GetPositionAtOffset(0, _dir);

            while (_reader.Read())
            {
                switch (_reader.NodeType)
                {
                case XmlNodeType.Attribute:
                    System.Diagnostics.Debug.Assert(false,
                                                    "Attributes should never be processed by top-level convertion loop.");
                    break;

                case XmlNodeType.EndElement:
                    System.Diagnostics.Trace.WriteLine("WordXmlReader.ReadDocument - EndElement [" +
                                                       _reader.Name + "]");
                    switch (_reader.Name)
                    {
                    case WordXmlSerializer.WordParagraphTag:
                        MoveOutOfElement(typeof(Paragraph));
                        _inParagraph = false;
                        break;

                    case WordXmlSerializer.WordRangeTag:
                        MoveOutOfElement(typeof(Run));
                        _inRange = false;
                        break;

                    case WordXmlSerializer.WordStyleTag:
                        _currentStyle = null;
                        break;

                    case WordXmlSerializer.WordTextTag:
                        _inText = false;
                        break;
                    }
                    break;

                case XmlNodeType.Element:
                    System.Diagnostics.Trace.WriteLine("WordXmlReader.ReadDocument - Element [" +
                                                       _reader.Name + "]");
                    switch (_reader.Name)
                    {
                    case WordXmlSerializer.WordParagraphTag:
                        if (_inParagraph)
                        {
                            throw CreateUnexpectedNodeException(_reader);
                        }
                        SurroundWithElement(new Paragraph(new Run()));
                        ApplyStyleToParagraph();
                        _inParagraph = true;
                        if (_reader.IsEmptyElement)
                        {
                            MoveOutOfElement(typeof(Paragraph));
                            _inParagraph = false;
                        }
                        break;

                    case WordXmlSerializer.WordRangeTag:
                        SurroundWithElement(new Run());
                        _inRange = true;
                        break;

                    case WordXmlSerializer.WordNameTag:
                        if (!IsPopulatingStyle)
                        {
                            throw new ArgumentException("w:name only supported on styles.");
                        }
                        break;

                    case WordXmlSerializer.WordStyleTag:
                        _currentStyle = new Style();
                        SetupCurrentStyle();
                        break;

                    case WordXmlSerializer.WordTextTag:
                        _inText = true;
                        break;

                    case WordXmlSerializer.WordBreakTag:
                        if (_inRange)
                        {
                            MoveOutOfElement(typeof(Run));
                            new LineBreak(_cursor);
                            SurroundWithElement(new Run());
                        }
                        else
                        {
                            new LineBreak(_cursor);
                        }
                        break;

                    default:
                        SetSimpleProperty();
                        break;
                    }
                    break;

                case XmlNodeType.SignificantWhitespace:
                case XmlNodeType.CDATA:
                case XmlNodeType.Text:
                    if (_inText)
                    {
                        _cursor.InsertTextInRun(_reader.Value);
                    }
                    break;
                }
            }
        }
        void AddUserText(string newText)
        {
            TextPointer selection = ConsoleTextBox.Selection.Start;

            selection.InsertTextInRun(newText);
        }
示例#15
0
        /// <summary>
        /// UpdateAccessKey - Scans forward in the tree looking for the access key marker, replacing it with access key element. We only support one find.
        /// </summary>
        private void UpdateAccessKey()
        {
            TextPointer navigator = new TextPointer(TextContainer.Start);

            while (!_accessKeyLocated && navigator.CompareTo(TextContainer.End) < 0)
            {
                TextPointerContext symbolType = navigator.GetPointerContext(LogicalDirection.Forward);
                switch (symbolType)
                {
                case TextPointerContext.Text:
                    string text  = navigator.GetTextInRun(LogicalDirection.Forward);
                    int    index = FindAccessKeyMarker(text);
                    if (index != -1 && index < text.Length - 1)
                    {
                        string      keyText = StringInfo.GetNextTextElement(text, index + 1);
                        TextPointer keyEnd  = navigator.GetPositionAtOffset(index + 1 + keyText.Length);

                        _accessKey       = new Run(keyText);
                        _accessKey.Style = AccessKeyStyle;

                        RegisterAccessKey();

                        HasCustomSerializationStorage.SetValue(_accessKey, true);
                        _accessKeyLocated = true;

                        UninitializeTextContainerListener();

                        TextContainer.BeginChange();
                        try
                        {
                            TextPointer underlineStart = new TextPointer(navigator, index);
                            TextRangeEdit.DeleteInlineContent(underlineStart, keyEnd);
                            _accessKey.RepositionWithContent(underlineStart);
                        }
                        finally
                        {
                            TextContainer.EndChange();
                            InitializeTextContainerListener();
                        }
                    }

                    break;
                }
                navigator.MoveToNextContextPosition(LogicalDirection.Forward);
            }

            // Convert double _ to single _
            navigator = new TextPointer(TextContainer.Start);
            string accessKeyMarker       = AccessKeyMarker.ToString();
            string doubleAccessKeyMarker = accessKeyMarker + accessKeyMarker;

            while (navigator.CompareTo(TextContainer.End) < 0)
            {
                TextPointerContext symbolType = navigator.GetPointerContext(LogicalDirection.Forward);
                switch (symbolType)
                {
                case TextPointerContext.Text:
                    string text    = navigator.GetTextInRun(LogicalDirection.Forward);
                    string nexText = text.Replace(doubleAccessKeyMarker, accessKeyMarker);
                    if (text != nexText)
                    {
                        TextPointer keyStart = new TextPointer(navigator, 0);
                        TextPointer keyEnd   = new TextPointer(navigator, text.Length);

                        UninitializeTextContainerListener();
                        TextContainer.BeginChange();
                        try
                        {
                            keyEnd.InsertTextInRun(nexText);
                            TextRangeEdit.DeleteInlineContent(keyStart, keyEnd);
                        }
                        finally
                        {
                            TextContainer.EndChange();
                            InitializeTextContainerListener();
                        }
                    }

                    break;
                }
                navigator.MoveToNextContextPosition(LogicalDirection.Forward);
            }
        }
示例#16
0
        // Token: 0x06004211 RID: 16913 RVA: 0x0012E228 File Offset: 0x0012C428
        private void UpdateAccessKey()
        {
            TextPointer textPointer = new TextPointer(this.TextContainer.Start);

            while (!this._accessKeyLocated && textPointer.CompareTo(this.TextContainer.End) < 0)
            {
                TextPointerContext pointerContext = textPointer.GetPointerContext(LogicalDirection.Forward);
                if (pointerContext == TextPointerContext.Text)
                {
                    string textInRun = textPointer.GetTextInRun(LogicalDirection.Forward);
                    int    num       = AccessText.FindAccessKeyMarker(textInRun);
                    if (num != -1 && num < textInRun.Length - 1)
                    {
                        string      nextTextElement  = StringInfo.GetNextTextElement(textInRun, num + 1);
                        TextPointer positionAtOffset = textPointer.GetPositionAtOffset(num + 1 + nextTextElement.Length);
                        this._accessKey       = new Run(nextTextElement);
                        this._accessKey.Style = AccessText.AccessKeyStyle;
                        this.RegisterAccessKey();
                        AccessText.HasCustomSerializationStorage.SetValue(this._accessKey, true);
                        this._accessKeyLocated = true;
                        this.UninitializeTextContainerListener();
                        this.TextContainer.BeginChange();
                        try
                        {
                            TextPointer textPointer2 = new TextPointer(textPointer, num);
                            TextRangeEdit.DeleteInlineContent(textPointer2, positionAtOffset);
                            this._accessKey.RepositionWithContent(textPointer2);
                        }
                        finally
                        {
                            this.TextContainer.EndChange();
                            this.InitializeTextContainerListener();
                        }
                    }
                }
                textPointer.MoveToNextContextPosition(LogicalDirection.Forward);
            }
            textPointer = new TextPointer(this.TextContainer.Start);
            string text     = AccessText.AccessKeyMarker.ToString();
            string oldValue = text + text;

            while (textPointer.CompareTo(this.TextContainer.End) < 0)
            {
                TextPointerContext pointerContext2 = textPointer.GetPointerContext(LogicalDirection.Forward);
                if (pointerContext2 == TextPointerContext.Text)
                {
                    string textInRun2 = textPointer.GetTextInRun(LogicalDirection.Forward);
                    string text2      = textInRun2.Replace(oldValue, text);
                    if (textInRun2 != text2)
                    {
                        TextPointer start        = new TextPointer(textPointer, 0);
                        TextPointer textPointer3 = new TextPointer(textPointer, textInRun2.Length);
                        this.UninitializeTextContainerListener();
                        this.TextContainer.BeginChange();
                        try
                        {
                            textPointer3.InsertTextInRun(text2);
                            TextRangeEdit.DeleteInlineContent(start, textPointer3);
                        }
                        finally
                        {
                            this.TextContainer.EndChange();
                            this.InitializeTextContainerListener();
                        }
                    }
                }
                textPointer.MoveToNextContextPosition(LogicalDirection.Forward);
            }
        }