Пример #1
0
        /// <summary>
        /// Insers the content of the document buffer into itself and sub containers.
        /// </summary>
        /// <param name="documentBuffer">The document buffer.</param>
        /// <param name="dimensionConstraint">The constraining dimensions.</param>
        public void Insert(IDocumentBuffer documentBuffer, IDimensionConstraint dimensionConstraint)
        {
            if (dimensionConstraint.MaxWidth == null)
            {
                throw new Exception("The formatted text object must be given a width constraint on insert.");
            }

            var textBeforeCursor   = Strings.strsubutf8(this.text, 0, this.cursorPos);
            var xBeforeCursor      = this.textScoper.GetWidth(this.flags.Font, this.flags.FontSize, textBeforeCursor);
            var availableDimension = new DimensionConstraint()
            {
                MaxWidth  = (double)dimensionConstraint.MaxWidth - xBeforeCursor,
                MaxHeight = dimensionConstraint.MaxHeight,
            };

            var addedText = documentBuffer.Take(availableDimension, this.GetCurrentFlags());

            if (addedText == String.Empty)
            {
                return;
            }

            var addedTextSize = Strings.strlenutf8(addedText);

            if (this.cursorPos < this.GetLength()) // The cursor is not and the end. The inserted text is thus put in at the cursor position
            {
                var textAfterCursor = Strings.strsubutf8(this.text, this.cursorPos);
                this.text = Strings.strsubutf8(this.text, 0, this.cursorPos);

                var bufferEmpty = documentBuffer.EndOfBuffer();

                documentBuffer.Append(textAfterCursor, this.GetCurrentFlags());

                if (bufferEmpty)
                {
                    availableDimension = new DimensionConstraint()
                    {
                        MaxWidth  = (double)dimensionConstraint.MaxWidth - this.textScoper.GetWidth(this.flags.Font, this.flags.FontSize, this.text + addedText),
                        MaxHeight = dimensionConstraint.MaxHeight,
                    };

                    addedText += documentBuffer.Take(availableDimension, this.GetCurrentFlags());
                }
            }

            if (this.cursor != null)
            {
                this.cursorPos += addedTextSize;
                this.CursorChanged();
            }

            this.text += addedText;
            this.TextChanged();
        }
Пример #2
0
 protected override IElement ProduceChild(IDocumentBuffer documentBuffer, IDimensionConstraint childConstraint)
 {
     return(documentBuffer.Take(childConstraint));
 }