Added read-only property to EditBox

0.8
Bruno Van de Velde 2017-08-15 14:49:47 +02:00
parent 7880b9e00f
commit d0c66e9fb8
3 changed files with 72 additions and 0 deletions

View File

@ -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;

View File

@ -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 != ".*")
{

View File

@ -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--");