diff --git a/include/TGUI/Widgets/TextArea.hpp b/include/TGUI/Widgets/TextArea.hpp index 8b2430e10..1a2534f87 100644 --- a/include/TGUI/Widgets/TextArea.hpp +++ b/include/TGUI/Widgets/TextArea.hpp @@ -247,10 +247,12 @@ TGUI_MODULE_EXPORT namespace tgui /// This function will take word-wrap into account. So, if a caret is on a line that is /// currently wrapping, the caret will still register as being on the same line even if /// that line spans many lines in the TextArea. + /// + /// If the value is at the top, the value will be 1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD std::size_t getCaretLine() const; - - + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns which column the blinking cursor is currently located on /// @@ -259,6 +261,8 @@ TGUI_MODULE_EXPORT namespace tgui /// This function will take word-wrap into account. So, if a caret is on a line that is /// currently wrapping, the caret will still register as being on the same line even if /// that line spans many lines in the TextArea. + /// + /// If the caret is at the beginning of a line, the value will be 1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// TGUI_NODISCARD std::size_t getCaretColumn() const; diff --git a/src/String.cpp b/src/String.cpp index 650cdf1d6..fc141672f 100644 --- a/src/String.cpp +++ b/src/String.cpp @@ -2303,7 +2303,11 @@ namespace tgui std::size_t counter = 0; const std::size_t end = m_string.size(); for (std::size_t c = pos; c < end; ++c) - if (m_string[c] == ch) ++counter; + { + if (m_string[c] == ch) + ++counter; + } + return counter; } diff --git a/tests/CompareFiles.cpp b/tests/CompareFiles.cpp index 29a480f27..f99d756f4 100644 --- a/tests/CompareFiles.cpp +++ b/tests/CompareFiles.cpp @@ -62,7 +62,7 @@ void compareImageFiles(const tgui::String& filename1, const tgui::String& filena const double diffPercentage = (totalDiff * 100) / (imageSize1.x * imageSize1.y * 3); INFO("Filename: " + filename1.toStdString()); - REQUIRE(diffPercentage < 0.05); + REQUIRE(diffPercentage < 0.06); } // The compareFiles can't be used to compare empty files because it uses readFileToMemory which diff --git a/tests/Widgets/TextArea.cpp b/tests/Widgets/TextArea.cpp index d63f5a45a..3adc9b28d 100644 --- a/tests/Widgets/TextArea.cpp +++ b/tests/Widgets/TextArea.cpp @@ -53,7 +53,10 @@ TEST_CASE("[TextArea]") textArea->onTextChange([](){}); textArea->onTextChange([](const tgui::String&){}); + textArea->onCaretPositionChange([](){}); + REQUIRE_NOTHROW(tgui::Widget::Ptr(textArea)->getSignal("TextChanged").connect([]{})); + REQUIRE_NOTHROW(tgui::Widget::Ptr(textArea)->getSignal("CaretPositionChanged").connect([]{})); } SECTION("WidgetType") @@ -118,6 +121,16 @@ TEST_CASE("[TextArea]") REQUIRE(textArea->getSelectedText() == ""); REQUIRE(textArea->getSelectionStart() == 2); REQUIRE(textArea->getSelectionEnd() == 2); + + textArea->setSelectedText(6, 10); + REQUIRE(textArea->getSelectedText() == "xt"); + REQUIRE(textArea->getSelectionStart() == 6); + REQUIRE(textArea->getSelectionEnd() == 8); + + textArea->setSelectedText(10, 20); + REQUIRE(textArea->getSelectedText() == ""); + REQUIRE(textArea->getSelectionStart() == 8); + REQUIRE(textArea->getSelectionEnd() == 8); } SECTION("TextSize") @@ -224,6 +237,34 @@ TEST_CASE("[TextArea]") textArea->setCaretPosition(25); REQUIRE(textArea->getCaretPosition() == 9); + + textArea->setSize({50, 50}); + textArea->setText("A long text that wouldn't fit on a single line.\nFollowed by more\nand more..."); + + textArea->setCaretPosition(0); + REQUIRE(textArea->getCaretLine() == 1); + REQUIRE(textArea->getCaretColumn() == 1); + + textArea->setCaretPosition(10); + REQUIRE(textArea->getCaretLine() == 1); + REQUIRE(textArea->getCaretColumn() == 11); + + textArea->setCaretPosition(60); + REQUIRE(textArea->getCaretLine() == 2); + REQUIRE(textArea->getCaretColumn() == 13); + + textArea->setCaretPosition(70); + REQUIRE(textArea->getCaretLine() == 3); + REQUIRE(textArea->getCaretColumn() == 6); + + textArea->setCaretPosition(100); + REQUIRE(textArea->getCaretLine() == 3); + REQUIRE(textArea->getCaretColumn() == 12); + + textArea->setSelectedText(6, 2); + REQUIRE(textArea->getCaretPosition() == 2); + REQUIRE(textArea->getCaretLine() == 1); + REQUIRE(textArea->getCaretColumn() == 3); } SECTION("LinesCount")