-
Notifications
You must be signed in to change notification settings - Fork 30
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
|
@@ -69,7 +70,6 @@ void TextBox::InsertText(const Gwk::String& strInsert) | |
if (!m_bEditable) | ||
return; | ||
|
||
// TODO: Make sure fits (implement maxlength) | ||
if (HasSelection()) | ||
EraseSelection(); | ||
|
||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
||
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(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
@@ -68,6 +81,20 @@ 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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->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) | ||
|
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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
.