示例#1
0
 private int GetKeyOffset(TextPointer caretPosition)
 {
     try
     {
         var count = 0;
         if (caretPosition != null && caretPosition.Paragraph != null)
         {
             foreach (var inline in caretPosition.Paragraph.Inlines)
             {
                 if (inline != caretPosition.Parent)
                 {
                     count += ((Run)inline).Text.Length;
                 }
                 else
                 {
                     count += caretPosition.GetTextRunLength(LogicalDirection.Backward);
                     return(count);
                 }
             }
         }
     }
     catch (Exception)
     {
         //wtf?
     }
     return(0);
 }
        TextPointer GetTextPositionAtOffset(TextPointer position, int characterCount)
        {
            while (position != null)
            {
                if (position.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                {
                    int count = position.GetTextRunLength(LogicalDirection.Forward);
                    if (characterCount <= count)
                    {
                        return(position.GetPositionAtOffset(characterCount));
                    }

                    characterCount -= count;
                }

                TextPointer nextContextPosition = position.GetNextContextPosition(LogicalDirection.Forward);
                if (nextContextPosition == null)
                {
                    return(position);
                }

                position = nextContextPosition;
            }

            return(position);
        }
        public static bool IsAtRunBoundary(this TextPointer current, LogicalDirection direction)
        {
            int         boundaryDistance = current.GetTextRunLength(direction);
            int         boundaryOffset   = direction == LogicalDirection.Forward ? boundaryDistance : -boundaryDistance;
            TextPointer boundary         = current.GetPositionAtOffset(boundaryOffset);

            return(boundary != null && current.IsAt(boundary));
        }
示例#4
0
        private static void PlainConvertTextRun(StringBuilder textBuffer, ref TextPointer navigator, TextPointer endPosition, ref char[] charArray)
        {
            int textRunLength = navigator.GetTextRunLength(LogicalDirection.Forward);

            charArray     = EnsureCharArraySize(charArray, textRunLength);
            textRunLength = GetTextWithLimit(navigator, LogicalDirection.Forward, charArray, 0, textRunLength, endPosition);
            textBuffer.Append(charArray, 0, textRunLength);
            navigator = navigator.GetNextContextPosition(LogicalDirection.Forward);
        }
        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);
        }
示例#6
0
        public static TextPointer GetTextPointerAtOffset(this RichTextBox richTextBox, int offset)
        {
            TextPointer navigator = richTextBox.Document.ContentStart;
            int         count     = 0;

            while (navigator.CompareTo(richTextBox.Document.ContentEnd) < 0)
            {
                switch (navigator.GetPointerContext(LogicalDirection.Forward))
                {
                case TextPointerContext.ElementStart:
                    break;

                case TextPointerContext.ElementEnd:
                    if (navigator.GetAdjacentElement(LogicalDirection.Forward) is Paragraph)
                    {
                        count += 2;
                    }
                    break;

                case TextPointerContext.EmbeddedElement:
                    count++;
                    break;

                case TextPointerContext.Text:
                    int runLength = navigator.GetTextRunLength(LogicalDirection.Forward);

                    if (runLength > 0 && runLength + count < offset)
                    {
                        count    += runLength;
                        navigator = navigator.GetPositionAtOffset(runLength);
                        if (count > offset)
                        {
                            break;
                        }
                        continue;
                    }
                    count++;
                    break;
                }

                if (count > offset)
                {
                    break;
                }

                navigator = navigator.GetPositionAtOffset(1, LogicalDirection.Forward);
            }

            return(navigator);
        }
示例#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
        /// <summary>
        /// Gets the text pointer at the given character offset.
        /// Each line break will count as 2 chars.
        /// </summary>
        public TextPointer GetTextPointerAtOffset(int offset, TextPointer navigator)
        {
            int cnt = 0;

            while (navigator.CompareTo(Document.ContentEnd) < 0)
            {
                switch (navigator.GetPointerContext(LogicalDirection.Forward))
                {
                case TextPointerContext.ElementStart:
                    break;

                case TextPointerContext.ElementEnd:
                    if (navigator.GetAdjacentElement(LogicalDirection.Forward) is Paragraph)
                    {
                        cnt += 2;
                    }
                    break;

                case TextPointerContext.EmbeddedElement:
                    // TODO: Find out what to do here?
                    cnt++;
                    break;

                case TextPointerContext.Text:
                    int runLength = navigator.GetTextRunLength(LogicalDirection.Forward);

                    if (runLength > 0 && runLength + cnt < offset)
                    {
                        cnt      += runLength;
                        navigator = navigator.GetPositionAtOffset(runLength);
                        if (cnt > offset)
                        {
                            break;
                        }
                        continue;
                    }
                    cnt++;
                    break;
                }

                if (cnt > offset)
                {
                    break;
                }

                navigator = navigator.GetPositionAtOffset(1, LogicalDirection.Forward);
            } // End while.

            return(navigator);
        }
 private TextPointer GetTextPointerFromTextOffset(TextPointer start, int textCharacterOffset)
 {
     while (textCharacterOffset > 0)
     {
         int    charsInRun = start.GetTextRunLength(LogicalDirection.Forward);
         string run        = start.GetTextInRun(LogicalDirection.Forward);
         if (textCharacterOffset <= charsInRun)
         {
             return(start.GetPositionAtOffset(textCharacterOffset));
         }
         textCharacterOffset -= charsInRun;
         start = start.GetNextContextPosition(LogicalDirection.Forward);
     }
     return(start);
 }
示例#10
0
        public TextPointer GetTextPointer(int charOffset, bool reset = false)
        {
            if (reset)
            {
                ResetToStart();
            }

            while (CurrentPosition.CompareTo(EndOfDocument) < 0)
            {
                switch (CurrentPosition.GetPointerContext(LogicalDirection.Forward))
                {
                case TextPointerContext.ElementEnd: {
                    DependencyObject d = this.CurrentPosition.GetAdjacentElement(LogicalDirection.Forward);
                    if (d is Paragraph || d is LineBreak)
                    {
                        CurrentCharacterOffset += 2;
                    }
                    if (d is InlineUIContainer)
                    {
                        CurrentCharacterOffset += 1;
                    }
                    break;
                }

                case TextPointerContext.Text: {
                    int characterInCurrentRun = CurrentCharacterOffset + CurrentPosition.GetTextRunLength(LogicalDirection.Forward);

                    if (charOffset <= characterInCurrentRun)
                    {
                        int offset = charOffset - CurrentCharacterOffset;

                        return(CurrentPosition.GetPositionAtOffset(offset, LogicalDirection.Forward));
                    }

                    CurrentCharacterOffset = characterInCurrentRun;
                    break;
                }
                }
                CurrentPosition = CurrentPosition.GetNextContextPosition(LogicalDirection.Forward);
            }

            if (CurrentCharacterOffset != 0 && charOffset <= CurrentCharacterOffset)
            {
                return(EndOfDocument.GetNextInsertionPosition(LogicalDirection.Backward));
            }

            return(EndOfDocument);
        }
示例#11
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;
        }
示例#12
0
        private TextPointer _GetTextPositionAtOffset(TextPointer position, int characterCount)
        {
            // Console.WriteLine($"Character count {characterCount}");
            while (position != null)
            {
                var    type = position.GetPointerContext(LogicalDirection.Forward);
                string TypeName;
                //Console.WriteLine($"type:{type}");
                var AdjacentElementF = position.GetAdjacentElement(LogicalDirection.Forward);
                if (AdjacentElementF != null)
                {
                    TypeName = AdjacentElementF.GetType().Name;
                    //  Console.WriteLine($"element {TypeName}");
                }
                else
                {
                    TypeName = "";
                }

                if (type == TextPointerContext.Text)
                {
                    //Console.WriteLine("Text in run:" + position.GetTextInRun(LogicalDirection.Forward));
                    int count = position.GetTextRunLength(LogicalDirection.Forward);
                    if (characterCount <= count)
                    {
                        return(position.GetPositionAtOffset(characterCount));
                    }
                    characterCount -= count;
                }
                if (type == TextPointerContext.ElementEnd && (TypeName == "Paragraph" || TypeName == "LineBreak"))
                {
                    //Console.WriteLine("Enter Count -- ");
                    characterCount -= 2;
                }

                TextPointer nextContextPosition = position.GetNextContextPosition(LogicalDirection.Forward);
                if (nextContextPosition == null)
                {
                    return(position);
                }

                position = nextContextPosition;
            }

            return(position);
        }
示例#13
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;
     }
 }
示例#14
0
        public RunWord SplitAt(TextPointer pointer)
        {
            int  splitPos = pointer.GetTextRunLength(GoBackward);
            Word newWord  = Word.SplitAt(splitPos);

            Text = Word.OriginalText;
            UpdateBackground();
            //UpdateSegmentedBackground();

            TextPointer insertPos = pointer.GetNextContextPosition(GoForward);

            while (insertPos != null && insertPos.GetPointerContext(GoBackward) != TextPointerContext.ElementEnd)
            {
                insertPos = insertPos.GetNextContextPosition(GoForward);
            }
            return(new RunWord(newWord, insertPos));
        }
示例#15
0
        private void _CollectRangeProperties(TextRange TargetRange)
        {
            if (TargetRange != null)
            {
                _rangeProperty.Clear();
                var         start        = TargetRange.Start;
                var         end          = TargetRange.End;
                int         targetlength = TargetRange.Text.Length;
                int         totallength  = 0;
                TextPointer current      = start;
                int         length       = 0;

                while (current.CompareTo(end) < 0)
                {
                    if (current.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
                    {
                        length       = current.GetTextRunLength(LogicalDirection.Forward);
                        totallength += length;
                        if (totallength > targetlength)
                        {
                            length = targetlength - totallength + length;
                        }
                        var ad                 = current.GetAdjacentElement(LogicalDirection.Forward);
                        var tempRange          = new TextRange(current, current.GetPositionAtOffset(length, LogicalDirection.Forward));
                        var backgroundProperty = tempRange.GetPropertyValue(Run.BackgroundProperty);
                        _rangeProperty.Add(tempRange, backgroundProperty);
                    }
                    var nextPos = current.GetNextContextPosition(LogicalDirection.Forward);
                    if (nextPos.CompareTo(end) > 0)
                    {
                        break;
                    }
                    current = nextPos;
                }
            }
        }
示例#16
0
        protected override void OnClick()
        {
            base.OnClick();

            MainWindow mainWindow = (MainWindow)Application.Current.MainWindow;

            if (this.Parent != StackPanelRecentColor)
            {
                ColorButton colorButton = new ColorButton(CustomRichTextBox, GridProp, Brush, StackPanelRecentColor, DropButton, RectangleColor);

                if (mainWindow.RecentColorCollection.Count < 7)
                {
                    StackPanelRecentColor.Children.Add(colorButton);

                    mainWindow.RecentColorCollection.Add(colorButton);
                }
                else
                {
                    mainWindow.RecentColorCollection.RemoveAt(0);
                    mainWindow.RecentColorCollection.Add(colorButton);

                    StackPanelRecentColor.Children.RemoveAt(0);
                    StackPanelRecentColor.Children.Add(colorButton);
                }
            }

            RectangleColor.Fill = Brush;

            DropButton.IsOpen = false;

            if (GridProp == null)
            {
                TextPointer textPoint = CustomRichTextBox.CaretPosition;
                CustomRichTextBox.startTextPointer = null;
                CustomRichTextBox.endTextPointer   = null;

                if (CustomRichTextBox.Selection.IsEmpty)
                {
                    TextElement textElement = null;

                    if (CustomRichTextBox.CaretPosition.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None)
                    {
                        CustomRichTextBox.IsCreateRunNull = true;

                        CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                        return;
                    }
                    else if (CustomRichTextBox.CaretPosition.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementStart)
                    {
                        textElement = (TextElement)CustomRichTextBox.CaretPosition.GetAdjacentElement(LogicalDirection.Backward);

                        if (textElement is Paragraph)
                        {
                            CustomRichTextBox.IsCreateRunNull = true;

                            CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                            return;
                        }
                        else if (textElement is ListItem)
                        {
                            CustomRichTextBox.IsCreateRunNull = true;

                            CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                            return;
                        }
                    }
                    else if (CustomRichTextBox.CaretPosition.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.ElementEnd)
                    {
                        textElement = (TextElement)CustomRichTextBox.CaretPosition.GetAdjacentElement(LogicalDirection.Backward);

                        if (textElement is LineBreak)
                        {
                            CustomRichTextBox.IsCreateRunNull = true;

                            CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                            return;
                        }
                    }

                    if (textPoint.GetTextRunLength(LogicalDirection.Backward) > 0) // Если позади каретки в текстовом элементе есть текст
                    {
                        TextPointer textPointer = textPoint.GetNextContextPosition(LogicalDirection.Backward);

                        Run run = (Run)textPointer.GetAdjacentElement(LogicalDirection.Backward);

                        TextElement nextBackTextElement = (TextElement)run.ElementStart.GetAdjacentElement(LogicalDirection.Backward);

                        int i = CustomRichTextBox.CaretPosition.GetTextRunLength(LogicalDirection.Backward);

                        TextRange textRange = new TextRange(textPointer, CustomRichTextBox.CaretPosition);

                        int index = textRange.Text.LastIndexOfAny(new char[] { ' ', ',', '.', '?', '!', ':', ';' });

                        if (index != -1)
                        {
                            if (textRange.Text[i - 1] != ' ' && textRange.Text[i - 1] != ',' && textRange.Text[i - 1] != '.' && textRange.Text[i - 1] != '?' && textRange.Text[i - 1] != '!' && textRange.Text[i - 1] != ':' && textRange.Text[i - 1] != ';')
                            {
                                CustomRichTextBox.startTextPointer = CustomRichTextBox.CaretPosition.GetPositionAtOffset(index + 1 - i);
                            }
                            else
                            {
                                CustomRichTextBox.startTextPointer = null;
                            }
                        }
                        else
                        {
                            CustomRichTextBox.IsText = true;

                            CustomRichTextBox.StartTextPointer(nextBackTextElement);
                        }
                    }
                    else
                    {
                        CustomRichTextBox.IsTextNull = true;

                        TextElement nextBackTextElement = (TextElement)CustomRichTextBox.CaretPosition.GetAdjacentElement(LogicalDirection.Backward);

                        if (nextBackTextElement is Run)
                        {
                            nextBackTextElement = (TextElement)nextBackTextElement.ElementStart.GetAdjacentElement(LogicalDirection.Backward);
                        }

                        CustomRichTextBox.StartTextPointer(nextBackTextElement);
                    }

                    CustomRichTextBox.IsTextNull = false;

                    CustomRichTextBox.IsText = false;

                    if (textPoint.GetTextRunLength(LogicalDirection.Forward) > 0) // Если в текстовом элементе впереди каретки есть текст
                    {
                        TextPointer textPointer = textPoint.GetNextContextPosition(LogicalDirection.Forward);

                        Run run = (Run)textPointer.GetAdjacentElement(LogicalDirection.Forward);

                        TextElement nextForwardTextElement = (TextElement)run.ElementEnd.GetAdjacentElement(LogicalDirection.Forward);

                        int i = CustomRichTextBox.CaretPosition.GetTextRunLength(LogicalDirection.Forward);

                        TextRange textRange = new TextRange(CustomRichTextBox.CaretPosition, textPointer);

                        int index = textRange.Text.IndexOfAny(new char[] { ' ', ',', '.', '?', '!', ':', ';' });

                        if (index != -1)
                        {
                            if (textRange.Text[0] != ' ' && textRange.Text[0] != ',' && textRange.Text[0] != '.' && textRange.Text[0] != '?' && textRange.Text[0] != '!' && textRange.Text[0] != ':' && textRange.Text[0] != ';')
                            {
                                CustomRichTextBox.endTextPointer = CustomRichTextBox.CaretPosition.GetPositionAtOffset(index);
                            }
                            else
                            {
                                CustomRichTextBox.endTextPointer = null;
                            }
                        }
                        else
                        {
                            CustomRichTextBox.IsText = true;

                            CustomRichTextBox.EndTextPointer(nextForwardTextElement);
                        }
                    }
                    else
                    {
                        CustomRichTextBox.IsTextNull = true;

                        TextElement nextForwardTextElement = (TextElement)CustomRichTextBox.CaretPosition.GetAdjacentElement(LogicalDirection.Forward);

                        if (nextForwardTextElement is Run)
                        {
                            nextForwardTextElement = (TextElement)nextForwardTextElement.ElementEnd.GetAdjacentElement(LogicalDirection.Forward);
                        }

                        CustomRichTextBox.EndTextPointer(nextForwardTextElement);
                    }

                    CustomRichTextBox.IsText = false;

                    CustomRichTextBox.IsTextNull = false;

                    if (CustomRichTextBox.startTextPointer != null && CustomRichTextBox.endTextPointer != null)
                    {
                        TextRange textRange = new TextRange(CustomRichTextBox.startTextPointer, CustomRichTextBox.endTextPointer);

                        textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brush);
                    }
                    else
                    {
                        if (CustomRichTextBox.Run.Foreground is SolidColorBrush && Brush is SolidColorBrush)
                        {
                            SolidColorBrush runBrush = CustomRichTextBox.Run.Foreground as SolidColorBrush;
                            SolidColorBrush newBrush = Brush as SolidColorBrush;

                            if (runBrush.Color.A != newBrush.Color.A || runBrush.Color.B != newBrush.Color.B || runBrush.Color.G != newBrush.Color.G || runBrush.Color.R != newBrush.Color.R || runBrush.Color.ScA != newBrush.Color.ScA || runBrush.Color.ScB != newBrush.Color.ScB || runBrush.Color.ScG != newBrush.Color.ScG || runBrush.Color.ScR != newBrush.Color.ScR || runBrush.Opacity != newBrush.Opacity)
                            {
                                CustomRichTextBox.IsCreateRunColor = true;

                                CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;
                            }
                            else
                            {
                                CustomRichTextBox.IsCreateRunColor = false;
                            }
                        }
                        else if (CustomRichTextBox.Run.Foreground is LinearGradientBrush && Brush is LinearGradientBrush)
                        {
                            LinearGradientBrush runBrush = CustomRichTextBox.Run.Foreground as LinearGradientBrush;
                            LinearGradientBrush newBrush = Brush as LinearGradientBrush;

                            int count = 0;

                            if (runBrush.ColorInterpolationMode != newBrush.ColorInterpolationMode || runBrush.EndPoint != newBrush.EndPoint || runBrush.Opacity != newBrush.Opacity || runBrush.MappingMode != newBrush.MappingMode || runBrush.SpreadMethod != newBrush.SpreadMethod || runBrush.StartPoint != newBrush.StartPoint || runBrush.GradientStops.Count != newBrush.GradientStops.Count)
                            {
                                CustomRichTextBox.IsCreateRunColor = true;

                                CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;
                            }
                            else
                            {
                                GradientStop newGradientStop;

                                foreach (GradientStop runGradientStop in runBrush.GradientStops)
                                {
                                    newGradientStop = newBrush.GradientStops[count];

                                    if (runGradientStop.Color.A != newGradientStop.Color.A || runGradientStop.Color.B != newGradientStop.Color.B || runGradientStop.Color.G != newGradientStop.Color.G || runGradientStop.Color.R != newGradientStop.Color.R || runGradientStop.Color.ScA != newGradientStop.Color.ScA || runGradientStop.Color.ScB != newGradientStop.Color.ScB || runGradientStop.Color.ScG != newGradientStop.Color.ScG || runGradientStop.Color.ScR != newGradientStop.Color.ScR)
                                    {
                                        CustomRichTextBox.IsCreateRunColor = true;

                                        CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                                        return;
                                    }
                                    else
                                    {
                                        CustomRichTextBox.IsCreateRunColor = false;
                                    }

                                    count++;
                                }
                            }
                        }
                        else if (CustomRichTextBox.Run.Foreground is RadialGradientBrush && Brush is RadialGradientBrush)
                        {
                            RadialGradientBrush runBrush = CustomRichTextBox.Run.Foreground as RadialGradientBrush;
                            RadialGradientBrush newBrush = Brush as RadialGradientBrush;

                            int count = 0;

                            if (runBrush.Center != newBrush.Center || runBrush.ColorInterpolationMode != newBrush.ColorInterpolationMode || runBrush.GradientOrigin != newBrush.GradientOrigin || runBrush.MappingMode != newBrush.MappingMode || runBrush.Opacity != newBrush.Opacity || runBrush.RadiusX != newBrush.RadiusX || runBrush.RadiusY != newBrush.RadiusY || runBrush.SpreadMethod != newBrush.SpreadMethod || runBrush.GradientStops.Count != newBrush.GradientStops.Count)
                            {
                                CustomRichTextBox.IsCreateRunColor = true;

                                CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;
                            }
                            else
                            {
                                GradientStop newGradientStop;

                                foreach (GradientStop runGradientStop in runBrush.GradientStops)
                                {
                                    newGradientStop = newBrush.GradientStops[count];

                                    if (runGradientStop.Color.A != newGradientStop.Color.A || runGradientStop.Color.B != newGradientStop.Color.B || runGradientStop.Color.G != newGradientStop.Color.G || runGradientStop.Color.R != newGradientStop.Color.R || runGradientStop.Color.ScA != newGradientStop.Color.ScA || runGradientStop.Color.ScB != newGradientStop.Color.ScB || runGradientStop.Color.ScG != newGradientStop.Color.ScG || runGradientStop.Color.ScR != newGradientStop.Color.ScR)
                                    {
                                        CustomRichTextBox.IsCreateRunColor = true;

                                        CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;

                                        return;
                                    }
                                    else
                                    {
                                        CustomRichTextBox.IsCreateRunColor = false;
                                    }

                                    count++;
                                }
                            }
                        }
                        else
                        {
                            CustomRichTextBox.IsCreateRunColor = true;

                            CustomRichTextBox.CreateRunTextPointer = CustomRichTextBox.CaretPosition;
                        }
                    }
                }
                else
                {
                    CustomRichTextBox.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brush);
                }
            }
            else
            {
                GridProp.NewBrush = Brush;
            }
        }
        private void OnConsoleKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // get the current line of text after all their editing is finished and send it to the drone.
                var doc = ConsoleTextBox.Document;
                // since the user editable Run is unique, we can get just that text easily.

                string text = SetUserText("");
                if (text == "clear" || text == "cls")
                {
                    // handle this one inline.
                    doc.Blocks.Clear();
                    SendConsoleMessage("\n");
                    return;
                }
                if (pos < history.Count && history[pos] == text)
                {
                    // then we are replaying history, so rewrite history
                }
                else
                {
                    history.Add(text);
                    pos = history.Count;
                }
                text += "\n";

                e.Handled = true; // don't let newline go to RichTextBox because this will split our "user" text Run.
                SendConsoleMessage(text);
            }
            else if (e.Key == Key.Back)
            {
                // don't let RichTextBox handle this delete because it will remove the "user" tagged text run.
                TextPointer selection = ConsoleTextBox.Selection.Start;
                int         len       = selection.GetTextRunLength(LogicalDirection.Backward);
                if (len > 1)
                {
                    // remove the text
                    selection.GetPositionAtOffset(-1).DeleteTextInRun(1);
                }
                e.Handled = true;
            }
            else if (e.Key == Key.Up)
            {
                if (pos > 0 && pos - 1 < history.Count)
                {
                    pos--;
                    SetUserText(history[pos]);
                }
                e.Handled = true;
            }
            else if (e.Key == Key.Down)
            {
                if (pos + 1 < history.Count)
                {
                    pos++;
                    SetUserText(history[pos]);
                }
                else
                {
                    pos = history.Count;
                    SetUserText("");
                }
                e.Handled = true;
            }
            else if (e.Key == Key.Up)
            {
                if (pos + 1 < history.Count)
                {
                    pos++;
                    SetUserText(history[pos]);
                }
                else
                {
                    pos = history.Count;
                    SetUserText("");
                }
                e.Handled = true;
            }
        }