From 425ff12bee8f791faf9ee1b4c269e629d46f423d Mon Sep 17 00:00:00 2001 From: Hayden Curran Date: Thu, 1 Jun 2017 23:43:24 +1200 Subject: [PATCH 1/3] Add a length limit to textboxes. Also basic testing controls. --- source/gwork/include/Gwork/Controls/TextBox.h | 5 ++++ source/gwork/source/Controls/TextBox.cpp | 15 +++++++++--- source/test/source/api/TextBox.cpp | 24 +++++++++++++++++-- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/source/gwork/include/Gwork/Controls/TextBox.h b/source/gwork/include/Gwork/Controls/TextBox.h index 869a3f48..9d381a54 100644 --- a/source/gwork/include/Gwork/Controls/TextBox.h +++ b/source/gwork/include/Gwork/Controls/TextBox.h @@ -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; } + void OnMouseClickLeft(int x, int y, bool bDown) override; void OnMouseMoved(int x, int y, int deltaX, int deltaY) override; @@ -102,6 +105,8 @@ namespace Gwk int m_cursorEnd; int m_cursorLine; + int m_maxTextLength; + Gwk::Rect m_rectSelectionBounds; Gwk::Rect m_rectCaretBounds; diff --git a/source/gwork/source/Controls/TextBox.cpp b/source/gwork/source/Controls/TextBox.cpp index ed93a09e..8daa802f 100644 --- a/source/gwork/source/Controls/TextBox.cpp +++ b/source/gwork/source/Controls/TextBox.cpp @@ -46,6 +46,7 @@ GWK_CONTROL_CONSTRUCTOR(TextBox) m_cursorLine = 0; m_bEditable = true; m_bSelectAll = false; + m_maxTextLength = -1; 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(strInsert.size()); + if (m_maxTextLength > -1 && 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(strInsert.size()); + m_cursorPos += insertSize; m_cursorEnd = m_cursorPos; m_cursorLine = 0; RefreshCursorBounds(); diff --git a/source/test/source/api/TextBox.cpp b/source/test/source/api/TextBox.cpp index 66b36eb0..9217722a 100644 --- a/source/test/source/api/TextBox.cpp +++ b/source/test/source/api/TextBox.cpp @@ -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,13 @@ 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->SetPos(220, 195); + label->SetSize(200, 45); + } } void OnEdit(Gwk::Controls::Base* control) From 2bfa998923b59dc7b9b3017a9f0603be31c69ab5 Mon Sep 17 00:00:00 2001 From: Hayden Curran Date: Fri, 2 Jun 2017 23:40:35 +1200 Subject: [PATCH 2/3] Suggested changes: Changed return type to int. Added a constant to replace magic number. Added a test covering longer than max length. --- source/gwork/include/Gwork/Controls/TextBox.h | 4 +++- source/gwork/source/Controls/TextBox.cpp | 4 ++-- source/test/source/api/TextBox.cpp | 7 +++++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/source/gwork/include/Gwork/Controls/TextBox.h b/source/gwork/include/Gwork/Controls/TextBox.h index 9d381a54..8996f79a 100644 --- a/source/gwork/include/Gwork/Controls/TextBox.h +++ b/source/gwork/include/Gwork/Controls/TextBox.h @@ -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; @@ -90,6 +90,8 @@ namespace Gwk Event::Caller onTextChanged; Event::Caller onReturnPressed; + static constexpr int NO_MAX_LENGTH = -1; + protected: void OnTextChanged() override; diff --git a/source/gwork/source/Controls/TextBox.cpp b/source/gwork/source/Controls/TextBox.cpp index 8daa802f..335e6043 100644 --- a/source/gwork/source/Controls/TextBox.cpp +++ b/source/gwork/source/Controls/TextBox.cpp @@ -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); @@ -80,7 +80,7 @@ void TextBox::InsertText(const Gwk::String& strInsert) return; int insertSize = static_cast(strInsert.size()); - if (m_maxTextLength > -1 && TextLength() + insertSize > m_maxTextLength) + if (m_maxTextLength > NO_MAX_LENGTH && TextLength() + insertSize > m_maxTextLength) { insertSize = m_maxTextLength - TextLength(); diff --git a/source/test/source/api/TextBox.cpp b/source/test/source/api/TextBox.cpp index 9217722a..bc44dd48 100644 --- a/source/test/source/api/TextBox.cpp +++ b/source/test/source/api/TextBox.cpp @@ -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) From ae128c1dcf8189e8f180e9a686b9303033105c7e Mon Sep 17 00:00:00 2001 From: Hayden Curran Date: Sat, 3 Jun 2017 11:02:32 +1200 Subject: [PATCH 3/3] Remove extra TextBoxMultiline test, add SetText past length limit to previous test --- source/test/source/api/TextBox.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/source/test/source/api/TextBox.cpp b/source/test/source/api/TextBox.cpp index bc44dd48..6f3824b0 100644 --- a/source/test/source/api/TextBox.cpp +++ b/source/test/source/api/TextBox.cpp @@ -85,14 +85,8 @@ class TextBox : public TestUnit Gwk::Controls::TextBoxMultiline* label = new Gwk::Controls::TextBoxMultiline(this); label->SetText("Max Length 20"); label->SetMaxTextLength(20); - 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->SetPos(220, 195); label->SetSize(200, 45); } }