예제 #1
0
        public string GetTextRange(TextPosition startPosition, TextPosition endPosition)
        {
            if (startPosition == (TextPosition)null)
            {
                throw new ArgumentNullException(nameof(startPosition));
            }
            if (endPosition == (TextPosition)null)
            {
                throw new ArgumentNullException(nameof(endPosition));
            }
            TextPosition.Swap(ref startPosition, ref endPosition);
            StringBuilder stringBuilder = new StringBuilder();
            ITextBlock    textBlock1    = startPosition.TextBlock;
            ITextBlock    textBlock2    = endPosition.TextBlock;

            if (object.Equals((object)textBlock1, (object)textBlock2))
            {
                return(this.GetBlockText(textBlock1, startPosition.CharPosition, endPosition.CharPosition - startPosition.CharPosition));
            }
            stringBuilder.Append(this.GetBlockText(textBlock1, startPosition.CharPosition, textBlock1.Length - startPosition.CharPosition));
            for (int index = textBlock1.Index + 1; index < textBlock2.Index && index < this.Children.Count; ++index)
            {
                ITextBlock child = this.Children[index] as ITextBlock;
                if (child != null)
                {
                    stringBuilder.Append(this.GetBlockText(child, 0, child.Length));
                }
            }
            stringBuilder.Append(this.GetBlockText(textBlock2, 0, endPosition.CharPosition));
            return(stringBuilder.ToString());
        }
        public virtual void Invalidate(
            TextPosition selectionStart,
            TextPosition selectionEnd,
            bool repaint)
        {
            this.selectionPolygons = (RectangleF[])null;
            TextBoxViewElement viewElement = this.textBoxElement.ViewElement;

            if (object.Equals((object)selectionStart, (object)selectionEnd) || selectionEnd == (TextPosition)null)
            {
                viewElement.Invalidate();
            }
            else
            {
                TextPosition.Swap(ref selectionStart, ref selectionEnd);
                LineInfo line1 = selectionStart.Line;
                LineInfo line2 = selectionEnd.Line;
                ReadOnlyCollection <LineInfo> lines = (ReadOnlyCollection <LineInfo>)viewElement.Lines;
                int index       = lines.IndexOf(line1);
                int lineSpacing = viewElement.LineSpacing;
                List <RectangleF> rectangleFList = new List <RectangleF>();
                for (; index < lines.Count; ++index)
                {
                    LineInfo   currentLine = lines[index];
                    RectangleF rectangle   = this.GetRectangle(currentLine);
                    if (currentLine == line1)
                    {
                        PointF location = viewElement.GetLocation(selectionStart);
                        rectangle.Width += rectangle.X - location.X;
                        rectangle.X      = location.X;
                    }
                    if (currentLine == line2)
                    {
                        PointF location = viewElement.GetLocation(selectionEnd);
                        rectangle.Width = location.X - rectangle.X;
                    }
                    if (currentLine != line1)
                    {
                        rectangle.Y      -= (float)lineSpacing;
                        rectangle.Height += (float)lineSpacing;
                    }
                    rectangleFList.Add(rectangle);
                    if (currentLine == line2)
                    {
                        break;
                    }
                }
                this.selectionPolygons = rectangleFList.ToArray();
                if (!repaint)
                {
                    return;
                }
                viewElement.Invalidate();
            }
        }
예제 #3
0
        private void ClampSelection()
        {
            ITextBoxNavigator navigator     = this.textBoxElement.Navigator;
            TextPosition      startPosition = navigator.SelectionStart;
            TextPosition      endPosition   = navigator.SelectionEnd;

            if (startPosition == (TextPosition)null || endPosition == (TextPosition)null)
            {
                return;
            }
            bool flag = false;

            TextPosition.Swap(ref startPosition, ref endPosition);
            if (TextBoxWrapPanel.IsLineFeed(startPosition.TextBlock.Text))
            {
                startPosition = navigator.GetNextPosition(startPosition);
                startPosition.CharPosition = 0;
                flag = true;
            }
            if (TextBoxWrapPanel.IsCarriageReturn(endPosition.TextBlock.Text))
            {
                endPosition = navigator.GetPreviousPosition(endPosition);
                endPosition.CharPosition = endPosition.TextBlock.Length;
                flag = true;
            }
            if (!flag)
            {
                return;
            }
            if (navigator.SelectionStart > navigator.SelectionEnd)
            {
                TextPosition textPosition = startPosition;
                startPosition = endPosition;
                endPosition   = textPosition;
            }
            navigator.SuspendNotifications();
            navigator.Select(startPosition, endPosition);
            navigator.ResumeNotifications();
        }
예제 #4
0
        public bool Replace(TextPosition startPosition, TextPosition endPosition, string text)
        {
            TextPosition.Swap(ref startPosition, ref endPosition);
            if (endPosition == (TextPosition)null && string.IsNullOrEmpty(text))
            {
                return(false);
            }
            TextBoxChangeAction action = TextBoxChangeAction.TextEdit;
            int    length    = TextPosition.GetLength(startPosition, endPosition);
            string textRange = this.GetTextRange(startPosition, endPosition);

            if (textRange == text || !this.NotifyTextChanging((int)startPosition, length, textRange, text, action))
            {
                return(false);
            }
            int caretPosition = (int)startPosition + (string.IsNullOrEmpty(text) ? 0 : text.Length);

            this.BeginEditUpdate();
            this.ReplaceOverride(startPosition, endPosition, text);
            this.InvalidateLayout();
            this.Invalidate();
            this.EndEditUpdate(true, text, caretPosition, action);
            return(true);
        }