Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TextBoxLine"/> class.
 /// </summary>
 /// <param name="owner">The <see cref="TextBoxLines"/> this line is part of.</param>
 /// <param name="wasAutoBroken">if this <see cref="TextBoxLine"/> was created as the result of automatic line
 /// breaking due to word wrapping.</param>
 public TextBoxLine(TextBoxLines owner, bool wasAutoBroken = false)
 {
     TextBoxLines = owner;
     _wasAutoBroken = wasAutoBroken;
     _lineText = string.Empty;
     _texts = new List<StyledText>(4);
 }
Esempio n. 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="TextBoxLine"/> class.
 /// </summary>
 /// <param name="owner">The <see cref="TextBoxLines"/> this line is part of.</param>
 /// <param name="wasAutoBroken">if this <see cref="TextBoxLine"/> was created as the result of automatic line
 /// breaking due to word wrapping.</param>
 public TextBoxLine(TextBoxLines owner, bool wasAutoBroken = false)
 {
     TextBoxLines   = owner;
     _wasAutoBroken = wasAutoBroken;
     _lineText      = string.Empty;
     _texts         = new List <StyledText>(4);
 }
Esempio n. 3
0
        /// <summary>
        /// Appends styled text to the line.
        /// </summary>
        /// <param name="text">The text to append.</param>
        public void Append(StyledText text)
        {
            InternalAppend(text);

            if (TextBoxLines != null)
            {
                TextBoxLines.NotifyTextAdded(this);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Appends styled text to the line.
        /// </summary>
        /// <param name="texts">The texts to append.</param>
        public void Append(IEnumerable <StyledText> texts)
        {
            foreach (var text in texts)
            {
                InternalAppend(text);
            }

            if (TextBoxLines != null)
            {
                TextBoxLines.NotifyTextAdded(this);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Appends another <see cref="TextBoxLine"/> to this <see cref="TextBoxLine"/>.
        /// </summary>
        /// <param name="textBoxLine">The other <see cref="TextBoxLine"/> to append to this.</param>
        public void Append(TextBoxLine textBoxLine)
        {
            for (var i = 0; i < textBoxLine._texts.Count; i++)
            {
                InternalAppend(textBoxLine._texts[i]);
            }

            if (TextBoxLines != null)
            {
                TextBoxLines.NotifyTextAdded(this);
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Inserts the <see cref="text"/> into the line at the specified position.
        /// </summary>
        /// <param name="position">The 0-based index to insert the <see cref="text"/>. The text
        /// will be inserted before the character in the line whos index is equal to the position. For example,
        /// inserting at position 3 will insert between the 3rd and 4th character of the string. As such,
        /// position 0 will insert at the start of the string, and the length of the string will insert
        /// at the end. If the position is invalid, this method will always return false.</param>
        /// <param name="text">The character to insert.</param>
        /// <returns>True if the <paramref name="text"/> was inserted successfully; otherwise false.</returns>
        public bool Insert(StyledText text, int position)
        {
            if (position < 0 || position > _lineText.Length)
            {
                return(false);
            }

            if (position == 0)
            {
                // Insert at the start
                var first = _texts.Count > 0 ? _texts[0] : null;
                if (first != null)
                {
                    // Try to combine the texts
                    if (text.HasSameStyle(first))
                    {
                        _texts[0] = text + first.Text;
                    }
                    else
                    {
                        _texts.Insert(0, text);
                    }
                }
                else
                {
                    _texts.Add(text);
                }

                _lineText = text.Text + _lineText;
            }
            else if (position == _lineText.Length)
            {
                // Insert at the end
                var last = _texts.Count > 0 ? _texts[_texts.Count - 1] : null;
                if (last != null)
                {
                    // Try to combine the texts
                    if (text.HasSameStyle(last))
                    {
                        _texts[_texts.Count - 1] = last + text.Text;
                    }
                    else
                    {
                        _texts.Add(text);
                    }
                }
                else
                {
                    _texts.Add(text);
                }

                _lineText += text.Text;
            }
            else
            {
                // Somewhere in the middle

                // Get the StyledText containing the character to insert before
                StyledText subText;
                int        subTextIndex;
                int        listIndex;
                try
                {
                    FindLineCharacter(position, out subText, out subTextIndex, out listIndex);
                }
                catch (ArgumentOutOfRangeException)
                {
                    return(false);
                }

                // Try to combine the texts
                if (text.HasSameStyle(subText))
                {
                    var combined = string.Empty;
                    if (subTextIndex > 0)
                    {
                        combined += subText.Text.Substring(0, subTextIndex);
                    }
                    combined         += text.Text;
                    combined         += subText.Text.Substring(subTextIndex);
                    _texts[listIndex] = new StyledText(combined, text);
                }
                else
                {
                    if (subTextIndex == 0)
                    {
                        // Don't need to split up the existing StyledText (inserts whole thing before)
                        _texts.Insert(listIndex, text);
                    }
                    else
                    {
                        // Have to split apart the existing StyledText and insert the new one in between
                        // the split up parts
                        var firstPart  = subText.Substring(0, subTextIndex);
                        var secondPart = subText.Substring(subTextIndex);
                        _texts[listIndex] = secondPart;
                        _texts.Insert(listIndex, text);
                        _texts.Insert(listIndex, firstPart);
                    }
                }

                _lineText = _lineText.Substring(0, position) + text.Text + _lineText.Substring(position);
            }

            EnsureCacheMatchesActualText();

            if (TextBoxLines != null)
            {
                TextBoxLines.NotifyTextAdded(this);
            }

            return(true);
        }