/// <summary>Deletes the selection that was made.</summary> /// <param name="textBox">The text box.</param> /// <exception cref="System.ArgumentNullException">TextBox was null.</exception> public static void DeleteSelection(TextBox textBox) { #if DEBUG if (textBox == null) { throw new ArgumentNullException("textBox"); } #endif var selectionBox = textBox.TextBoxSelection; var cursor = textBox.TextBoxCursor; var textToModify = textBox.Text; textToModify = textToModify.Remove(selectionBox.ConfigBegin, selectionBox.ConfigGetCharacterSelectionLength()); selectionBox.ConfigBegin = selectionBox.ConfigBegin; selectionBox.ConfigEnd = selectionBox.ConfigEnd; selectionBox.ConfigActive = false; cursor.ConfigIndex = selectionBox.ConfigBegin; cursor.ConfigShowCursor = true; cursor.ConfigBlinking = true; textBox.Text = textToModify; }
/// <summary> /// Select all text in the text box. /// </summary> /// <param name="textBox">The text box.</param> public static void DoSelectAll(TextBox textBox) { #if DEBUG if (textBox == null) { throw new ArgumentNullException("textBox"); } #endif var selectionBox = textBox.TextBoxSelection; var cursor = textBox.TextBoxCursor; selectionBox.ConfigBegin = 0; selectionBox.ConfigEnd = textBox.Text.Length; selectionBox.ConfigActive = true; cursor.ConfigIndex = textBox.Text.Length; cursor.ConfigShowCursor = true; cursor.ConfigBlinking = true; }
/// <summary> /// Set the cursor to given location. /// </summary> /// <param name="textBox">The text box.</param> /// <param name="cursorIndex">Index of the cursor.</param> /// <exception cref="System.ArgumentNullException">TextBox was null.</exception> public void DoCursorLocation(TextBox textBox, int cursorIndex) { #if DEBUG if (textBox == null) { throw new ArgumentNullException("textBox"); } #endif var selectionBox = textBox.TextBoxSelection; var cursor = textBox.TextBoxCursor; if (cursorIndex < 0) { cursorIndex = 0; } var text = textBox.Text; if (string.IsNullOrEmpty(text)) { cursorIndex = 0; } else if (cursorIndex > textBox.Text.Length) { cursorIndex = textBox.Text.Length; } cursor.ConfigIndex = cursorIndex; selectionBox.ConfigActive = false; this.ValidateAndRepair(textBox); }
/// <summary> /// Checks if all text is selected. /// </summary> /// <param name="textBox">The text box.</param> /// <returns>True if all text is selected</returns> /// <exception cref="System.ArgumentNullException">TextBox can not be null.</exception> public static bool CheckIfAllTextIsSelected(TextBox textBox) { #if DEBUG if (textBox == null) { throw new ArgumentNullException("textBox"); } #endif var selectionControl = textBox.TextBoxSelection; if (string.IsNullOrEmpty(textBox.Text) || selectionControl.ConfigBegin != 0 || selectionControl.ConfigBegin != textBox.Text.Length || selectionControl.ConfigActive == false) { return false; } return true; }
private void MakeVisualDataFromConfig(TextBox textBox) { var textControl = textBox.Label; var text = textBox.Label; var cursor = textBox.TextBoxCursor; // populate visual text-data text.StateMaximumSize = new DVector2(textControl.StateCalculatedWidth, textControl.StateCalculatedHeight); text.State.DrawPosition = textControl.State.DrawPosition; text.State.Offset = new DVector2(textControl.StateCalculatedOffsetX, textControl.StateCalculatedOffsetY); var firstCharacterShown = text.StateFirstCharacterIndexShown; var shownCharacterLength = text.StateShownCharacterCount; text.StateTextShown = textBox.Text.Substring(firstCharacterShown, shownCharacterLength); // populate visual cursor data var partBetweenFirstCharAndCursorLength = cursor.ConfigIndex - firstCharacterShown; var partBetweenFirstCharAndCursorText = text.StateTextShown.Substring(0, partBetweenFirstCharAndCursorLength); var fontName = textBox.Theme.FontName; var sizeOfFirstPart = textControl.Manager.ImageCompositor.ReadSizeString(fontName, partBetweenFirstCharAndCursorText); var sizeOfAGiantLetter = textControl.Manager.ImageCompositor.ReadSizeString(fontName, "M"); cursor.State.DrawPosition = textBox.State.DrawPosition; cursor.State.Offset = new DVector2(sizeOfFirstPart.X, 0); cursor.State.Width = 1; cursor.State.Height = sizeOfAGiantLetter.Y; }
/// <summary> /// Fixes everything in their boundaries. /// </summary> /// <param name="textBox">The text box.</param> private void FixEveryConfigInTheirBoundaries(TextBox textBox) { var label = textBox.Label; var cursor = textBox.TextBoxCursor; var selectionBox = textBox.TextBoxSelection; // place last-char inside text var lastShownCharIsAt = WhereIs(0, label.StateLastCharacterIndexShown, textBox.Text.Length); switch (lastShownCharIsAt) { case Side.Left: label.StateLastCharacterIndexShown = 0; break; case Side.Right: label.StateLastCharacterIndexShown = textBox.Text.Length; break; } // place first-char inside text var firstShownCharIsAt = WhereIs(0, label.StateFirstCharacterIndexShown, textBox.Text.Length); switch (firstShownCharIsAt) { case Side.Left: label.StateFirstCharacterIndexShown = 0; break; case Side.Right: label.StateFirstCharacterIndexShown = textBox.Text.Length; break; } // place cursor index inside text var cursorIsAt = WhereIs(label.StateFirstCharacterIndexShown, cursor.ConfigIndex, label.StateLastCharacterIndexShown); switch (cursorIsAt) { case Side.Left: cursor.ConfigIndex = label.StateFirstCharacterIndexShown; break; case Side.Right: cursor.ConfigIndex = label.StateFirstCharacterIndexShown; break; } // make sure selection box is inside the boundaries of text if (selectionBox.ConfigBegin < 0) { selectionBox.ConfigBegin = 0; } if (selectionBox.ConfigEnd > textBox.Text.Length) { selectionBox.ConfigEnd = selectionBox.ConfigGetCharacterSelectionLength(); } if (this.Debug) { System.Diagnostics.Debug.WriteLine(label.ToString()); System.Diagnostics.Debug.WriteLine(cursor.ToString()); System.Diagnostics.Debug.WriteLine(selectionBox.ToString()); } }
/// <summary> /// Validates the data in the text-box. /// </summary> /// <param name="textBox">The text box.</param> public void ValidateAndRepair(TextBox textBox) { this.FixEveryConfigInTheirBoundaries(textBox); this.MakeVisualDataFromConfig(textBox); this.FixConfigByCheckingVisual(); }
/// <summary> /// Does the key delete action. /// </summary> /// <param name="textBox">The text box.</param> /// <exception cref="System.ArgumentNullException">TextBox can not be null.</exception> public void DoKeyDelete(TextBox textBox) { #if DEBUG if (textBox == null) { throw new ArgumentNullException("textBox"); } #endif var cursor = textBox.TextBoxCursor; var changed = false; // if there is no text to look at. then we have nothing to do var textboxLength = textBox.Text.Length; if (textboxLength <= 0) { return; } // remove text that was selected var allSelected = CheckIfAllTextIsSelected(textBox); if (allSelected) { DeleteSelection(textBox); changed = true; } else if (cursor.ConfigIndex < textBox.Text.Length) { // or else remove a character at the right of cursor position // if the cursor is already at the end , we can't delete // left part is on the left of the cursor var leftPart = textBox.Text.Substring(0, cursor.ConfigIndex); // the right part is on the right of the cursor , but without the first character var rightPartMinusACharacter = textBox.Text.Substring(cursor.ConfigIndex + 1, textBox.Text.Length - (cursor.ConfigIndex + 1)); // join left and right , resulting in a Delete-action textBox.Text = leftPart + rightPartMinusACharacter; changed = true; } if (changed) { this.ValidateAndRepair(textBox); } }
/// <summary> /// Does the key backspace action. /// </summary> /// <param name="textBox">The text box.</param> /// <exception cref="System.ArgumentNullException">TextBox can not be null.</exception> public void DoKeyBackspace(TextBox textBox) { #if DEBUG if (textBox == null) { throw new ArgumentNullException("textBox"); } #endif var selectionBox = textBox.TextBoxSelection; var cursor = textBox.TextBoxCursor; // if there is no text , then do nothing if (textBox.Text.Length > 0 == false) { return; } // if there is no selection and the cursor is not already at 0 if (selectionBox.ConfigActive == false && cursor.ConfigIndex > 0) { var cursorLocation = cursor.ConfigIndex; var textToModify = textBox.Text; textToModify = textToModify.Substring(0, cursorLocation - 1) + textToModify.Substring(cursorLocation, textToModify.Length - cursorLocation); cursor.ConfigIndex = cursorLocation - 1; textBox.Text = textToModify; } else { DeleteSelection(textBox); } this.ValidateAndRepair(textBox); }
/// <summary> /// Does the cursor right action. /// </summary> /// <param name="textBox">The text box.</param> /// <exception cref="System.ArgumentNullException">TextBox can not be null.</exception> public void DoCursorRight(TextBox textBox) { #if DEBUG if (textBox == null) { throw new ArgumentNullException("textBox"); } #endif var selection = textBox.TextBoxSelection; var cursor = textBox.TextBoxCursor; if (cursor.ConfigIndex >= textBox.Text.Length) { return; } cursor.ConfigIndex = cursor.ConfigIndex + 1; selection.ConfigActive = false; this.ValidateAndRepair(textBox); }
/// <summary> /// Does the insert text action. /// </summary> /// <param name="textBox">The text box.</param> /// <param name="textToInsert">The text to insert.</param> /// <exception cref="System.ArgumentNullException">TextBox can not be null.</exception> /// <exception cref="System.InvalidOperationException">The cursor for given text-box is null</exception> public void DoInsertText(TextBox textBox, string textToInsert) { if (string.IsNullOrEmpty(textToInsert)) { // nothing to do.. return. return; } #if DEBUG if (textBox == null) { throw new ArgumentNullException("textBox"); } #endif var cursor = textBox.TextBoxCursor; #if DEBUG if (cursor == null) { throw new InvalidOperationException("The cursor for given text-box is null"); } #endif //// var changed = false; // remove text that was selected var allSelected = CheckIfAllTextIsSelected(textBox); if (allSelected) { DeleteSelection(textBox); //// changed = true; } // if there is already text, then insert it at the cursor-position var cursorIndex = cursor.ConfigIndex; var textToModify = textBox.Text; if (!string.IsNullOrEmpty(textToModify)) { textToModify = textToModify.Insert(cursorIndex, textToInsert); //// changed = true; } else { textToModify = textToInsert; //// changed = true; } textBox.Text = textToModify; // we must move the cursor because we just inserted some text cursor.ConfigIndex = cursor.ConfigIndex + textToInsert.Length; this.ValidateAndRepair(textBox); }
/// <summary>This will result that we show a extra character to the right side.</summary> /// <param name="internalTextData">The internal Text Data.</param> /// <param name="visualTextData">The visual Text Data.</param> /// <returns>true when succeeded , otherwise false</returns> public static bool ShiftLastCharShownRight(TextBox internalTextData, Label visualTextData) { #if DEBUG if (internalTextData == null) { throw new ArgumentNullException("internalTextData"); } if (visualTextData == null) { throw new ArgumentNullException("visualTextData"); } #endif if (visualTextData.StateLastCharacterIndexShown < internalTextData.Text.Length) { visualTextData.StateLastCharacterIndexShown++; return true; } return false; }
/// <summary>Shifts the shown text to the right.</summary> /// <param name="internalTextData">The internal text data.</param> /// <param name="visualTextData">The visual text data.</param> /// <returns>True when successful , otherworldly false.</returns> /// <exception cref="System.ArgumentNullException">InternalTextData or visualTextData</exception> public static bool ShiftShownTextToTheRight(TextBox internalTextData, Label visualTextData) { #if DEBUG if (internalTextData == null) { throw new ArgumentNullException("internalTextData"); } if (visualTextData == null) { throw new ArgumentNullException("visualTextData"); } #endif if (ShiftFirstCharShownToRight(internalTextData, visualTextData) == false) { return false; } if (ShiftLastCharShownRight(internalTextData, visualTextData) == false) { return false; } return true; }
public override void LoadContent() { base.LoadContent(); // label var y = (float)Theme.ControlLargeSpacing + Theme.ControlHeight; this.Label = new Label("MyLabel") { Config = { PositionX = Theme.ControlLargeSpacing, PositionY = y, }, ConfigText = "Test label ff", ConfigHorizontalAlignment = HorizontalAlignment.Left, ConfigVerticalAlignment = VerticalAlignment.Center }; this.AddControl(this.Label); // text-box y = y + this.Label.Config.Height + Theme.ControlSmallSpacing; this.TextBox = new TextBox("MyTextBox") { Config = { PositionX = Theme.ControlLargeSpacing, PositionY = y, }, Text = "My text control", }; this.AddControl(this.TextBox); // multi-line text-box y = y + this.TextBox.Config.Height + Theme.ControlSmallSpacing; var multilineTextBox = new MultilineTextBox("MyMultiLineTextBox") { Config = { PositionX = Theme.ControlLargeSpacing, PositionY = y, }, ConfigText = "This is test text.\nTo see if this is working.\nIf you see this.\nAll is working.\n" }; this.AddControl(multilineTextBox); Config.Width = this.Label.Config.Width + (Theme.ControlLargeSpacing * 4); Config.Height = multilineTextBox.Config.PositionY + (multilineTextBox.Config.Height * 4) + Theme.ControlLargeSpacing; }