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 1 commit
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
4 changes: 3 additions & 1 deletion source/gwork/include/Gwork/Controls/TextBox.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ namespace Gwk
virtual void SetCursorEnd(int i);

void SetMaxTextLength(int maxLength) { m_maxTextLength = maxLength; }
const int& GetMaxTextLength() const { return m_maxTextLength; }
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 @@ -90,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 Down
4 changes: 2 additions & 2 deletions source/gwork/source/Controls/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ GWK_CONTROL_CONSTRUCTOR(TextBox)
m_cursorLine = 0;
m_bEditable = true;
m_bSelectAll = false;
m_maxTextLength = -1;
m_maxTextLength = NO_MAX_LENGTH;
SetTextColor(Gwk::Color(50, 50, 50, 255)); // TODO: From Skin
SetTabable(true);
AddAccelerator("Ctrl + C", &TextBox::OnCopy);
Expand Down Expand Up @@ -80,7 +80,7 @@ void TextBox::InsertText(const Gwk::String& strInsert)
return;

int insertSize = static_cast<int>(strInsert.size());
if (m_maxTextLength > -1 && TextLength() + insertSize > m_maxTextLength)
if (m_maxTextLength > NO_MAX_LENGTH && TextLength() + insertSize > m_maxTextLength)
Copy link
Owner

Choose a reason for hiding this comment

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

This should be != NO_MAX_LENGTH as it is a magic number out of the usual range.

{
insertSize = m_maxTextLength - TextLength();

Expand Down
7 changes: 7 additions & 0 deletions source/test/source/api/TextBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ class TextBox : public TestUnit
label->SetPos(220, 195);
label->SetSize(200, 45);
}
{
Gwk::Controls::TextBoxMultiline* label = new Gwk::Controls::TextBoxMultiline(this);
label->SetMaxTextLength(20);
label->SetText("Max Length 20 -- Set text longer than max length");
label->SetPos(220, 245);
label->SetSize(200, 45);
}
}

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