Skip to content

Commit

Permalink
Merge pull request #51 from okonk/textbox-length-limit
Browse files Browse the repository at this point in the history
Add text length limit on TextBox control
  • Loading branch information
billyquith authored Jun 3, 2017
2 parents 7493ff4 + f378ce9 commit 0ffcb2b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
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;

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

0 comments on commit 0ffcb2b

Please sign in to comment.