From fef92953c413e41faa8700748f27fcc580b08bc7 Mon Sep 17 00:00:00 2001 From: Bruno Van de Velde Date: Sat, 11 May 2019 18:26:01 +0200 Subject: [PATCH] Added SelectionChanged signal to TextBox (#97) --- changelog.txt | 1 + include/TGUI/Widgets/TextBox.hpp | 4 +++- src/TGUI/Widgets/TextBox.cpp | 13 +++++++++++++ tests/Widgets/TextBox.cpp | 2 ++ 4 files changed, 19 insertions(+), 1 deletion(-) diff --git a/changelog.txt b/changelog.txt index 66cd5c43..044de9aa 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,6 +1,7 @@ TGUI 0.8.6 (TBD) ----------------- +- Added SelectionChanged signal to TextBox - Added getSelectionStart and getSelectionEnd functions to TextBox - Added mousePressed and mouseReleased to respond to different mouse buttons in custom widgets diff --git a/include/TGUI/Widgets/TextBox.hpp b/include/TGUI/Widgets/TextBox.hpp index e6105b6a..67c0123a 100644 --- a/include/TGUI/Widgets/TextBox.hpp +++ b/include/TGUI/Widgets/TextBox.hpp @@ -568,7 +568,8 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public: - SignalString onTextChange = {"TextChanged"}; ///< The text was changed. Optional parameter: new text + SignalString onTextChange = {"TextChanged"}; ///< The text was changed. Optional parameter: new text + Signal onSelectionChange = {"SelectionChanged"}; ///< Selected text changed ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -593,6 +594,7 @@ namespace tgui // Information about the selection sf::Vector2 m_selStart; sf::Vector2 m_selEnd; + std::pair, sf::Vector2> m_lastSelection; // Information about the caret Vector2f m_caretPosition; diff --git a/src/TGUI/Widgets/TextBox.cpp b/src/TGUI/Widgets/TextBox.cpp index d59bf40f..6929574f 100644 --- a/src/TGUI/Widgets/TextBox.cpp +++ b/src/TGUI/Widgets/TextBox.cpp @@ -1546,6 +1546,17 @@ namespace tgui } recalculatePositions(); + + // Send an event when the selection changed + if ((m_selStart != m_lastSelection.first) || (m_selEnd != m_lastSelection.second)) + { + // Only send the event when there is an actual change, not when the caret position moved + if ((m_selStart != m_selEnd) || (m_lastSelection.first != m_lastSelection.second)) + onSelectionChange.emit(this); + + m_lastSelection.first = m_selStart; + m_lastSelection.second = m_selEnd; + } } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -1710,6 +1721,8 @@ namespace tgui { if (signalName == toLower(onTextChange.getName())) return onTextChange; + else if (signalName == toLower(onSelectionChange.getName())) + return onSelectionChange; else return Widget::getSignal(std::move(signalName)); } diff --git a/tests/Widgets/TextBox.cpp b/tests/Widgets/TextBox.cpp index 033bc997..fdaa51b5 100644 --- a/tests/Widgets/TextBox.cpp +++ b/tests/Widgets/TextBox.cpp @@ -441,6 +441,8 @@ TEST_CASE("[TextBox]") textBox->textEntered('C'); REQUIRE(textBox->getText() == "ABCDEFGHIJKLMNOPQRSTUVWXYZABC"); } + + // TODO: TextChanged and SelectionChanged events } testWidgetRenderer(textBox->getRenderer());