From c101b49cd5d8fad98e69f9155339a44d05cbe0c6 Mon Sep 17 00:00:00 2001 From: Bruno Van de Velde Date: Sat, 7 Oct 2017 15:56:40 +0200 Subject: [PATCH] :beetle: The caret of a TextBox should not move when adding a newline at the start of a line that only exists due to word-wrap --- src/TGUI/Widgets/TextBox.cpp | 8 ++++++-- tests/Widgets/TextBox.cpp | 9 +++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/TGUI/Widgets/TextBox.cpp b/src/TGUI/Widgets/TextBox.cpp index c1f4e00f..e070deb6 100644 --- a/src/TGUI/Widgets/TextBox.cpp +++ b/src/TGUI/Widgets/TextBox.cpp @@ -890,8 +890,12 @@ namespace tgui m_text.insert(caretPosition, key); m_lines[m_selEnd.y].insert(m_selEnd.x, key); - m_selStart.x++; - m_selEnd.x++; + // Increment the caret position, unless you type a newline at the start of a line while that line only existed due to word wrapping + if ((key != '\n') || (m_selEnd.x > 0) || (m_selEnd.y == 0) || m_lines[m_selEnd.y-1].isEmpty() || (m_text[caretPosition-1] == '\n')) + { + m_selStart.x++; + m_selEnd.x++; + } rearrangeText(true); }; diff --git a/tests/Widgets/TextBox.cpp b/tests/Widgets/TextBox.cpp index a59a5a14..8b4ce35b 100644 --- a/tests/Widgets/TextBox.cpp +++ b/tests/Widgets/TextBox.cpp @@ -301,6 +301,15 @@ TEST_CASE("[TextBox]") textBox->keyPressed({sf::Keyboard::Key::BackSpace, false, false, false, false}); REQUIRE(textBox->getText() == "AB\nFGHIJKLPQ\n\nTXYZ"); + + // The caret should not move when adding a newline at the start of a line that only exists due to word-wrap + textBox->setText(""); // Make sure the scrollbar is gone + textBox->setText("ABCDEFGHIJKLMONPQRSTUVWXYZ"); + for (unsigned int i = 0; i < 17; ++i) + textBox->keyPressed({sf::Keyboard::Key::Left, false, false, false, false}); + textBox->keyPressed({sf::Keyboard::Key::Return, false, false, false, false}); + textBox->keyPressed({sf::Keyboard::Key::Delete, false, false, false, false}); + REQUIRE(textBox->getText() == "ABCDEFGHIJ\nLMONPQRSTUVWXYZ"); } SECTION("Copy and Paste")