Пример #1
0
        public void TextPointersUpdatePosition_clear()
        {
            TextSelection ts = rtb.Selection;

            ts.Text = "Moonlight";

            rtb.SelectAll();

            TextPointer start  = ts.Start;
            TextPointer insert = ts.Start;

            start  = start.GetPositionAtOffset(4, LogicalDirection.Backward);
            insert = insert.GetPositionAtOffset(3, LogicalDirection.Backward);

            ts.Select(insert, insert.GetPositionAtOffset(1, LogicalDirection.Forward));

            ts.Text = "";

            ts.Select(start, start);

            ts.Text = "2";

            rtb.SelectAll();

            Assert.AreEqual("Moo2light", ts.Text, "#0");
        }
Пример #2
0
        public void TextPointersUpdatePosition_clear3()
        {
            Paragraph p = new Paragraph();
            Run       r = new Run();

            r.Text = "Moonlight";

            p.Inlines.Add(r);

            rtb.Blocks.Add(p);

            TextSelection ts = rtb.Selection;

            rtb.SelectAll();

            TextPointer pos = ts.Start.GetPositionAtOffset(4, LogicalDirection.Backward);

            ts.Select(r.ContentStart, r.ContentEnd);

            Assert.AreEqual(@"&lt;Section xml:space=""preserve"" HasTrailingParagraphBreakOnPaste=""False"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">&lt;Paragraph FontSize=""11"" FontFamily=""Portable User Interface"" Foreground=""#FF000000"" FontWeight=""Normal"" FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left"">&lt;Run Text=""Moonlight"" />&lt;/Paragraph>&lt;/Section>", rtb.Xaml.Replace("<", "&lt;"), "#0");

            ts.Text = "";

            Assert.AreEqual(@"", rtb.Xaml, "#1");

            // message on the exception is
            // TextElement_OperationNotSupportedOutsideRTB,
            // which should only happen if the
            // textpointer's parent is not in the document
            // model for the RTB anymore (this is also
            // indicated in the Xaml being "" above.)
            Assert.Throws <NotSupportedException> (() => pos.CompareTo(r.ContentEnd), "#2");
        }
        public override bool MakeLowercase()
        {
            // NOTE: We store this as a *Span* because we don't want it to track
            Span selectionSpan = TextRange.AdvancedTextRange;
            bool isReversed    = IsReversed;

            if (!TextRange.MakeLowercase())
            {
                return(false);
            }
            TextSelection.Select(new SnapshotSpan(_textView.AdvancedTextView.TextSnapshot, selectionSpan), isReversed);
            return(true);
        }
        public override bool ReplaceText(string newText)
        {
            // We use *int* and *Span* here because we don't want them to track
            int  startPosition    = TextRange.GetDisplayStartPoint().CurrentPosition;
            Span newSelectionSpan = new Span(startPosition, newText.Length);
            bool isReversed       = IsReversed;

            if (!TextRange.ReplaceText(newText))
            {
                return(false);
            }
            TextSelection.Select(new SnapshotSpan(_textView.AdvancedTextView.TextSnapshot, newSelectionSpan), isReversed);
            return(true);
        }
Пример #5
0
        public override void Perform()
        {
            parent.Focus();
            BindingFlags  STATIC_INSTANCE_BINDING = BindingFlags.Static | BindingFlags.NonPublic;
            Type          textEditorType          = typeof(FrameworkElement).Assembly.GetType("System.Windows.Documents.TextEditor");
            MethodInfo    mi       = textEditorType.GetMethod("GetTextSelection", STATIC_INSTANCE_BINDING);
            TextSelection ts       = mi.Invoke(null, new object[] { parent }) as TextSelection;
            TextRange     subRange = GetRandomSubRange(FlowDocument.ContentStart, FlowDocument.ContentEnd);

            if (ts != null)
            {
                ts.Select(subRange.Start, subRange.End);
            }
        }
Пример #6
0
        public void TextPointersAreSnapshots()
        {
            TextSelection ts = rtb.Selection;

            ts.Text = "Moon";

            rtb.SelectAll();

            TextPointer start = ts.Start;
            TextPointer end   = ts.End;

            ts.Select(ts.Start.GetPositionAtOffset(1, LogicalDirection.Backward), ts.End.GetPositionAtOffset(-1, LogicalDirection.Forward));
            Assert.AreEqual(-1, start.CompareTo(ts.Start), "#1");
            Assert.AreEqual(1, end.CompareTo(ts.End), "#2");
        }
        private void SelectRange(int selectionStart, int selectionEnd)
        {
            SnapshotPoint startPoint = new SnapshotPoint(TextView.AdvancedTextView.TextSnapshot, selectionStart);
            SnapshotPoint endPoint   = new SnapshotPoint(TextView.AdvancedTextView.TextSnapshot, selectionEnd);

            TextSelection.Select(new VirtualSnapshotPoint(startPoint), new VirtualSnapshotPoint(endPoint));

            ITextViewLine    textViewLine = TextView.AdvancedTextView.GetTextViewLineContainingBufferPosition(endPoint);
            PositionAffinity affinity     = (textViewLine.IsLastTextViewLineForSnapshotLine || (endPoint != textViewLine.End)) ? PositionAffinity.Successor : PositionAffinity.Predecessor;

            Caret.MoveTo(endPoint, affinity);
            TextView.AdvancedTextView.ViewScroller.EnsureSpanVisible(TextSelection.StreamSelectionSpan.SnapshotSpan,
                                                                     (selectionStart <= selectionEnd)
                                                                     ? EnsureSpanVisibleOptions.MinimumScroll
                                                                     : (EnsureSpanVisibleOptions.MinimumScroll | EnsureSpanVisibleOptions.ShowStart));
        }
Пример #8
0
 public void setColor(RichTextBox txt, int startIndex, int endIndex, Color color)
 {
     try
     {
         TextRange tr = new TextRange(txt.Document.ContentStart, txt.Document.ContentEnd);
         tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.White);
         TextSelection textRange = txt.Selection;
         TextPointer   start     = txt.Document.ContentStart;
         TextPointer   startPos  = GetTextPointAt(start, startIndex);
         TextPointer   endPos    = GetTextPointAt(start, endIndex);
         textRange.Select(startPos, endPos);
         textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));
     }
     catch (Exception)
     {
     }
 }
        public override bool ToggleCase()
        {
            // NOTE: We store this as a *Span* because we don't want it to track
            Span selectionSpan = TextRange.AdvancedTextRange;
            bool isReversed    = IsReversed;
            bool isEmpty       = IsEmpty;

            if (!TextRange.ToggleCase())
            {
                return(false);
            }

            if (!isEmpty)
            {
                TextSelection.Select(new SnapshotSpan(_textView.AdvancedTextView.TextSnapshot, selectionSpan), isReversed);
            }

            return(true);
        }
        private void setErrorTextColor(int starting, int ending, RichTextBox rtb)
        {
            //Select the entire box
            rtb.SelectAll();
            //Create a selection
            TextSelection currentSelection = rtb.Selection;
            //Set the starter pointer to the start of the content
            TextPointer start    = rtb.Document.ContentStart;
            TypingModel newModel = new TypingModel();
            //Indicate the start and end pos
            TextPointer startPos = GetTextPointAt(start, starting);
            TextPointer endPos   = GetTextPointAt(start, ending);

            //Choose the area in which to change the color
            currentSelection.Select(startPos, endPos);
            Color color = (Color)ColorConverter.ConvertFromString("#Ab3334");

            currentSelection.ApplyPropertyValue(TextElement.ForegroundProperty, new SolidColorBrush(color));
            currentSelection.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
        }
Пример #11
0
        public static string Select(this RichTextBox rtb, int offset, int length, Color color)
        {
            // Get text selection:
            TextSelection textRange = rtb.Selection;

            // Get text starting point:
            TextPointer start = rtb.Document.ContentStart;

            // Get begin and end requested:
            TextPointer startPos = GetTextPointAt(start, offset);
            TextPointer endPos   = GetTextPointAt(start, offset + length);

            // New selection of text:
            textRange.Select(startPos, endPos);

            // Apply property to the selection:
            textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));

            // Return selection text:
            return(rtb.Selection.Text);
        }
Пример #12
0
        public void TextPointersUpdatePosition_clear2()
        {
            TextSelection ts = rtb.Selection;

            ts.Text = "Moonlight";

            rtb.SelectAll();

            TextPointer pos = ts.Start.GetPositionAtOffset(4, LogicalDirection.Backward);

            Run r = (Run)pos.Parent;

            ts.Select(r.ContentStart, r.ContentEnd);

            Assert.AreEqual(@"&lt;Section xml:space=""preserve"" HasTrailingParagraphBreakOnPaste=""False"" xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">&lt;Paragraph FontSize=""11"" FontFamily=""Portable User Interface"" Foreground=""#FF000000"" FontWeight=""Normal"" FontStyle=""Normal"" FontStretch=""Normal"" TextAlignment=""Left"">&lt;Run Text=""Moonlight"" />&lt;/Paragraph>&lt;/Section>", rtb.Xaml.Replace("<", "&lt;"), "#0");

            ts.Text = "";

            Assert.AreEqual(@"", rtb.Xaml.Replace("<", "&lt;"), "#1");

            // message on the exception is TextElement_OperationNotSupportedOutsideRTB
            Assert.Throws <NotSupportedException> (() => pos.CompareTo(r.ContentEnd), "#2");
        }
        public override bool Indent()
        {
            bool singleLineSelection = (GetStartPoint().LineNumber == GetEndPoint().LineNumber);
            bool entireLastLineSelected
                = (GetStartPoint().CurrentPosition != GetEndPoint().CurrentPosition&&
                   GetStartPoint().CurrentPosition == TextBuffer.GetEndPoint().StartOfLine&&
                   GetEndPoint().CurrentPosition == TextBuffer.GetEndPoint().EndOfLine);

            if (singleLineSelection && !entireLastLineSelected)
            {
                TextPoint endPoint = GetEndPoint();
                if (!Delete())
                {
                    return(false);
                }
                if (!endPoint.InsertIndent())
                {
                    return(false);
                }
                TextView.AdvancedTextView.Caret.MoveTo(endPoint.AdvancedTextPoint);
            }
            else // indent the selected lines
            {
                VirtualSnapshotPoint oldStartPoint = TextSelection.Start;
                VirtualSnapshotPoint oldEndPoint   = TextSelection.End;
                bool isReversed = TextSelection.IsReversed;

                ITextSnapshotLine startLine = AdvancedTextRange.Snapshot.GetLineFromPosition(oldStartPoint.Position);
                ITextSnapshotLine endLine   = AdvancedTextRange.Snapshot.GetLineFromPosition(oldEndPoint.Position);

                // If the selection span initially starts at the whitespace at the beginning of the line in the startLine or
                // ends at the whitespace at the beginning of the line in the endLine, restore selection and caret position,
                // *unless* the selection was in box mode.
                bool startAtStartLineWhitespace = oldStartPoint.Position <= _textView.GetTextPoint(startLine.Start).GetFirstNonWhiteSpaceCharacterOnLine().CurrentPosition;
                bool endAtEndLineWhitespace     = oldEndPoint.Position < _textView.GetTextPoint(endLine.Start).GetFirstNonWhiteSpaceCharacterOnLine().CurrentPosition;
                bool isBoxSelection             = AdvancedSelection.Mode == TextSelectionMode.Box;

                if (isBoxSelection)
                {
                    if (!this.BoxIndent())
                    {
                        return(false);
                    }
                }
                else
                {
                    if (!TextRange.Indent())
                    {
                        return(false);
                    }
                }

                // Computing the new selection and caret position
                VirtualSnapshotPoint newStartPoint = TextSelection.Start;
                VirtualSnapshotPoint newEndPoint   = TextSelection.End;

                if (!isBoxSelection && (startAtStartLineWhitespace || endAtEndLineWhitespace))
                {
                    // After indent selection span should start at the start of startLine and end at the start of endLine
                    if (startAtStartLineWhitespace)
                    {
                        newStartPoint = new VirtualSnapshotPoint(AdvancedTextRange.Snapshot, oldStartPoint.Position.Position);
                    }

                    if (endAtEndLineWhitespace && oldEndPoint.Position.Position != endLine.Start && endLine.Length != 0)
                    {
                        int insertedTextSize = _editorOptions.IsConvertTabsToSpacesEnabled() ? _editorOptions.GetTabSize() : 1;
                        newEndPoint = new VirtualSnapshotPoint(AdvancedTextRange.Snapshot, newEndPoint.Position.Position - insertedTextSize);
                    }

                    if (!isReversed)
                    {
                        TextSelection.Select(newStartPoint, newEndPoint);
                    }
                    else
                    {
                        TextSelection.Select(newEndPoint, newStartPoint);
                    }

                    TextView.AdvancedTextView.Caret.MoveTo(TextSelection.ActivePoint, PositionAffinity.Successor);
                }
            }
            TextView.AdvancedTextView.Caret.EnsureVisible();
            return(true);
        }