From d0c66e9fb8e4dfe81969162405ef2ddc7cc00f89 Mon Sep 17 00:00:00 2001 From: Bruno Van de Velde Date: Tue, 15 Aug 2017 14:49:47 +0200 Subject: [PATCH] Added read-only property to EditBox --- include/TGUI/Widgets/EditBox.hpp | 26 ++++++++++++++++++++++++++ src/TGUI/Widgets/EditBox.cpp | 30 ++++++++++++++++++++++++++++++ tests/Widgets/EditBox.cpp | 16 ++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/include/TGUI/Widgets/EditBox.hpp b/include/TGUI/Widgets/EditBox.hpp index 7ed89978..742e1c8e 100644 --- a/include/TGUI/Widgets/EditBox.hpp +++ b/include/TGUI/Widgets/EditBox.hpp @@ -320,6 +320,30 @@ namespace tgui bool isTextWidthLimited() const; + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Makes the edit box read-only or make it writable again + /// + /// @param readOnly Should the edit box be read-only? + /// + /// When the edit box is read-only, you can no longer delete characters and type text. + /// Selecting text, copying text and even calling the setText function will still work. + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void setReadOnly(bool readOnly = true); + + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Checks if the edit box read-only or writable + /// + /// @return Is the edit box read-only? + /// + /// When the edit box is read-only, you can no longer delete characters and type text. + /// Selecting text, copying text and even calling the setText function will still work. + /// + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + bool isReadOnly() const; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Sets the blinking caret to after a specific character /// @@ -493,6 +517,8 @@ namespace tgui // You can change the boolean with the limitTextWidth(bool) function. bool m_limitTextWidth = false; + bool m_readOnly = false; + // The text inside the edit box sf::String m_text; diff --git a/src/TGUI/Widgets/EditBox.cpp b/src/TGUI/Widgets/EditBox.cpp index 01b045b4..4be87a16 100644 --- a/src/TGUI/Widgets/EditBox.cpp +++ b/src/TGUI/Widgets/EditBox.cpp @@ -399,6 +399,20 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void EditBox::setReadOnly(bool readOnly) + { + m_readOnly = readOnly; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + bool EditBox::isReadOnly() const + { + return m_readOnly; + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void EditBox::setCaretPosition(std::size_t charactersBeforeCaret) { // The caret position has to stay inside the string @@ -697,6 +711,9 @@ namespace tgui } else if (event.code == sf::Keyboard::BackSpace) { + if (m_readOnly) + return; + // Make sure that we did not select any characters if (m_selChars == 0) { @@ -738,6 +755,9 @@ namespace tgui } else if (event.code == sf::Keyboard::Delete) { + if (m_readOnly) + return; + // Make sure that no text is selected if (m_selChars == 0) { @@ -787,6 +807,9 @@ namespace tgui } else if (event.code == sf::Keyboard::V) { + if (m_readOnly) + return; + const auto clipboardContents = Clipboard::get(); // Only continue pasting if you actually have to do something @@ -807,6 +830,10 @@ namespace tgui else if (event.code == sf::Keyboard::X) { Clipboard::set(m_textSelection.getString()); + + if (m_readOnly) + return; + deleteSelectedCharacters(); onTextChange->emit(this, m_text); @@ -823,6 +850,9 @@ namespace tgui void EditBox::textEntered(sf::Uint32 key) { + if (m_readOnly) + return; + // Only add the character when the regex matches if (m_regexString != ".*") { diff --git a/tests/Widgets/EditBox.cpp b/tests/Widgets/EditBox.cpp index bb3ce6d4..c224540f 100644 --- a/tests/Widgets/EditBox.cpp +++ b/tests/Widgets/EditBox.cpp @@ -145,6 +145,22 @@ TEST_CASE("[EditBox]") REQUIRE(editBox->getText() == "yet another text"); } + SECTION("ReadOnly") + { + REQUIRE(!editBox->isReadOnly()); + + editBox->setReadOnly(true); + REQUIRE(editBox->isReadOnly()); + + // The text can still be changed using the setText function + editBox->setText("Test"); + REQUIRE(editBox->getText() == "Test"); + REQUIRE(editBox->isReadOnly()); + + editBox->setReadOnly(false); + REQUIRE(!editBox->isReadOnly()); + } + SECTION("Input Validator") { editBox->setText(L"++Some123 Ê Text456--");