/// <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(); }
public void Insert(IDocumentBuffer documentBuffer, IDimensionConstraint dimensionConstraint) { double dimensionConsumed = 0; var child = this.FirstChild; while (this.CurrentCursorChild != null && child != this.CurrentCursorChild) { dimensionConsumed += this.GetDimension(child.Object); child = child.Next; } var objectConstraint = this.GetConstraint(dimensionConstraint, dimensionConsumed); while (child != null) { child.Object.Insert(documentBuffer, objectConstraint); if (documentBuffer.EndOfBuffer()) { this.SizeChanged(); return; } dimensionConsumed += this.GetDimension(child.Object); objectConstraint = this.GetConstraint(dimensionConstraint, dimensionConsumed); if (child.Next != null) { child = child.Next; if (this.CurrentCursorChild != null) { this.CurrentCursorChild.Object.ClearCursor(); this.CurrentCursorChild = child; this.CurrentCursorChild.Object.SetCursor(false, this.Cursor); } } else { break; } } var newElement = this.ProduceChild(documentBuffer, objectConstraint); while (newElement != null) { this.AppendChild(newElement); dimensionConsumed += this.GetDimension(newElement); objectConstraint = this.GetConstraint(dimensionConstraint, dimensionConsumed); if (documentBuffer.EndOfBuffer()) { break; } newElement = this.ProduceChild(documentBuffer, objectConstraint); } if (this.Cursor != null) { this.CurrentCursorChild?.Object.ClearCursor(); this.CurrentCursorChild = this.LastChild; this.CurrentCursorChild.Object.SetCursor(true, this.Cursor); } this.SizeChanged(); }