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

Conversation

okonk
Copy link
Contributor

@okonk okonk commented Jun 1, 2017

As per the discussion, went with an approach that once the limit is reached no more text can be entered. This seems to be the standard behaviour used by other applications and makes sense.

Can adjust naming/stuff if required.

Copy link
Contributor

@Grant1219 Grant1219 left a comment

Choose a reason for hiding this comment

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

I would prefer to use size_t when dealing with string lengths, but I realize int is being used here for the special case where there is no text limit and m_maxTextLength is set to -1.

@billyquith
Copy link
Owner

billyquith commented Jun 1, 2017

I would prefer to use size_t when dealing with string length

Fair comment, but int is used throughout the code. I have a ticket #45 to work on this.

Copy link
Owner

@billyquith billyquith left a comment

Choose a reason for hiding this comment

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

Could you please make the changes suggested. I'm more keen on the demo that the change works, and return type.

@@ -63,6 +63,9 @@ namespace Gwk
virtual void SetCursorPos(int i);
virtual void SetCursorEnd(int i);

void SetMaxTextLength(int maxLength) { m_maxTextLength = maxLength; }
const int& GetMaxTextLength() const { return 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.

We can return int here

@@ -46,6 +46,7 @@ GWK_CONTROL_CONSTRUCTOR(TextBox)
m_cursorLine = 0;
m_bEditable = true;
m_bSelectAll = false;
m_maxTextLength = -1;
Copy link
Owner

Choose a reason for hiding this comment

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

Be nice if -1 wasn't a magic number. Perhaps static constexpr NO_MAX_LENGTH = -1; ?

{
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?

@okonk
Copy link
Contributor Author

okonk commented Jun 2, 2017

What happens is the text will be larger (since we're only checking the limit on insert), but nothing bad happens, the user just has to take the text below the limit before they can edit it. That seems alright to me. I'll add a test for it anyway

Changed return type to int.
Added a constant to replace magic number.
Added a test covering longer than max length.
@billyquith
Copy link
Owner

Yep, but I think you added another control, so just want to distinguish what the control is demonstrating. E.g. setText(...), setMaxTextLen(6), setText("Maximum text length"), so we can see that the length has been clamped/truncated, and that any editing maintains this. - possibly you could add a counter next to it showing the text length, but that is entirely optional.

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

@okonk
Copy link
Contributor Author

okonk commented Jun 2, 2017

Alright, changed it to add an extra SetText after SetMaxTextLength

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

Copy link
Owner

@billyquith billyquith left a comment

Choose a reason for hiding this comment

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

Thanks for making the changes. I haven't tested them but look good.

@billyquith billyquith merged commit a392d74 into billyquith:gwork Jun 3, 2017
@okonk okonk deleted the textbox-length-limit branch June 4, 2017 04:00
billyquith added a commit that referenced this pull request Aug 1, 2017
Add text length limit on TextBox control
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants