Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add text length limit on TextBox control #51

Merged
merged 3 commits into from
Jun 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions source/gwork/include/Gwork/Controls/TextBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ namespace Gwk
virtual void SetCursorPos(int i);
virtual void SetCursorEnd(int i);

void SetMaxTextLength(int maxLength) { m_maxTextLength = maxLength; }
int GetMaxTextLength() const { return m_maxTextLength; }

void OnMouseClickLeft(int x, int y, bool bDown) override;
void OnMouseMoved(int x, int y, int deltaX, int deltaY) override;

Expand All @@ -87,6 +90,8 @@ namespace Gwk
Event::Caller onTextChanged;
Event::Caller onReturnPressed;

static constexpr int NO_MAX_LENGTH = -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could just be static const int NO_MAX_LENGTH = -1;

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This project is C++11 so there is no reason not to use constexpr.


protected:

void OnTextChanged() override;
Expand All @@ -102,6 +107,8 @@ namespace Gwk
int m_cursorEnd;
int m_cursorLine;

int m_maxTextLength;

Gwk::Rect m_rectSelectionBounds;
Gwk::Rect m_rectCaretBounds;

Expand Down
15 changes: 12 additions & 3 deletions source/gwork/source/Controls/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ GWK_CONTROL_CONSTRUCTOR(TextBox)
m_cursorLine = 0;
m_bEditable = true;
m_bSelectAll = false;
m_maxTextLength = NO_MAX_LENGTH;
SetTextColor(Gwk::Color(50, 50, 50, 255)); // TODO: From Skin
SetTabable(true);
AddAccelerator("Ctrl + C", &TextBox::OnCopy);
Expand All @@ -69,7 +70,6 @@ void TextBox::InsertText(const Gwk::String& strInsert)
if (!m_bEditable)
return;

// TODO: Make sure fits (implement maxlength)
if (HasSelection())
EraseSelection();

Expand All @@ -79,10 +79,19 @@ void TextBox::InsertText(const Gwk::String& strInsert)
if (!IsTextAllowed(strInsert, m_cursorPos))
return;

int insertSize = static_cast<int>(strInsert.size());
if (m_maxTextLength > NO_MAX_LENGTH && TextLength() + insertSize > m_maxTextLength)
{
insertSize = m_maxTextLength - TextLength();

if (insertSize <= 0)
return;
}

String str = GetText();
str.insert(m_cursorPos, strInsert);
str.insert(m_cursorPos, strInsert, 0, insertSize);
SetText(str);
m_cursorPos += static_cast<int>(strInsert.size());
m_cursorPos += insertSize;
m_cursorEnd = m_cursorPos;
m_cursorLine = 0;
RefreshCursorBounds();
Expand Down
25 changes: 23 additions & 2 deletions source/test/source/api/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,31 @@ class TextBox : public TestUnit
label->SetTextColor(Gwk::Color(255, 0, 255, 255));
label->SetPos(10, 10+25*3);
}
{
Gwk::Controls::TextBox* label = new Gwk::Controls::TextBox(this);
label->SetText("Max Length 15");
label->SetMaxTextLength(15);
label->SetPos(10, 10+25*4);
}
{
Gwk::Controls::TextBoxNumeric* label = new Gwk::Controls::TextBoxNumeric(this);
label->SetText("2004");
label->SetTextColor(Gwk::Color(255, 0, 255, 255));
label->SetPos(10, 10+25*4);
label->SetPos(10, 10+25*5);
}
{
Gwk::Controls::TextBoxNumeric* label = new Gwk::Controls::TextBoxNumeric(this);
label->SetText("3.14159265");
label->SetTextColor(Gwk::Color(255, 0, 255, 255));
label->SetPos(10, 10+25*6);
label->SetMaxTextLength(10);
}
{
m_font.facename = "Impact";
m_font.size = 50;
Gwk::Controls::TextBox* label = new Gwk::Controls::TextBox(this);
label->SetText("Different Font");
label->SetPos(10, 10+25*5);
label->SetPos(10, 10+25*7);
label->SetFont(&m_font);
label->SetSize(200, 55);
}
Expand All @@ -68,6 +81,14 @@ class TextBox : public TestUnit
label->SetPos(220, 10);
label->SetSize(200, 180);
}
{
Gwk::Controls::TextBoxMultiline* label = new Gwk::Controls::TextBoxMultiline(this);
label->SetText("Max Length 20");
label->SetMaxTextLength(20);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After this, perhaps you could set the text to something longer than the maximum length to test whether this works as expected?

label->SetText("Max Length 20 -- Set text longer than max length");
label->SetPos(220, 195);
label->SetSize(200, 45);
}
}

void OnEdit(Gwk::Controls::Base* control)
Expand Down