From fb0285c27ff6e06ddde6c0b4343c1d2e8b4e0f5e Mon Sep 17 00:00:00 2001 From: Charles Dang Date: Tue, 1 Mar 2016 01:55:56 +1100 Subject: [PATCH] ttext_box: added support for defining a maximum input length --- data/gui/schema.cfg | 5 +++++ src/gui/auxiliary/window_builder/text_box.cpp | 6 +++++- src/gui/auxiliary/window_builder/text_box.hpp | 2 ++ src/gui/widgets/text_box.cpp | 8 ++++++++ src/gui/widgets/text_box.hpp | 8 ++++++++ 5 files changed, 28 insertions(+), 1 deletion(-) diff --git a/data/gui/schema.cfg b/data/gui/schema.cfg index 2171330cd403..9b1b7414ed84 100644 --- a/data/gui/schema.cfg +++ b/data/gui/schema.cfg @@ -1801,6 +1801,11 @@ type="string" default="" [/key] + [key] + name="max_input_length" + type="int" + default=0 + [/key] [key] name="label" type="t_string" diff --git a/src/gui/auxiliary/window_builder/text_box.cpp b/src/gui/auxiliary/window_builder/text_box.cpp index 1d74cb1a0b57..9ee37f01c838 100644 --- a/src/gui/auxiliary/window_builder/text_box.cpp +++ b/src/gui/auxiliary/window_builder/text_box.cpp @@ -27,7 +27,9 @@ namespace implementation { tbuilder_text_box::tbuilder_text_box(const config& cfg) - : tbuilder_control(cfg), history(cfg["history"]) + : tbuilder_control(cfg) + , history(cfg["history"]) + , max_input_length(cfg["max_input_length"]) { } @@ -44,6 +46,8 @@ twidget* tbuilder_text_box::build() const widget->set_history(history); } + widget->set_max_input_length(max_input_length); + DBG_GUI_G << "Window builder: placed text box '" << id << "' with definition '" << definition << "'.\n"; diff --git a/src/gui/auxiliary/window_builder/text_box.hpp b/src/gui/auxiliary/window_builder/text_box.hpp index e221aa4d286a..c8b2503e9f62 100644 --- a/src/gui/auxiliary/window_builder/text_box.hpp +++ b/src/gui/auxiliary/window_builder/text_box.hpp @@ -33,6 +33,8 @@ struct tbuilder_text_box : public tbuilder_control twidget* build() const; std::string history; + + size_t max_input_length; }; diff --git a/src/gui/widgets/text_box.cpp b/src/gui/widgets/text_box.cpp index d42588bcb1c6..ddf78fd4ecba 100644 --- a/src/gui/widgets/text_box.cpp +++ b/src/gui/widgets/text_box.cpp @@ -99,6 +99,7 @@ std::string ttext_history::get_value() const ttext_box::ttext_box() : ttext_() , history_() + , max_input_length_(0) , text_x_offset_(0) , text_y_offset_(0) , text_height_(0) @@ -124,6 +125,10 @@ void ttext_box::place(const tpoint& origin, const tpoint& size) set_maximum_width(get_text_maximum_width()); set_maximum_height(get_text_maximum_height(), false); + if(max_input_length_ != 0) { + set_maximum_length(max_input_length_); + } + update_offsets(); } @@ -135,6 +140,9 @@ void ttext_box::update_canvas() const unsigned start = get_selection_start(); const int length = get_selection_length(); + if(max_input_length_ != 0) { + set_maximum_length(max_input_length_); + } PangoEllipsizeMode ellipse_mode = PANGO_ELLIPSIZE_NONE; if(!can_wrap()) { diff --git a/src/gui/widgets/text_box.hpp b/src/gui/widgets/text_box.hpp index aa7ea95df9dd..9ba72c1d2602 100644 --- a/src/gui/widgets/text_box.hpp +++ b/src/gui/widgets/text_box.hpp @@ -131,6 +131,11 @@ class ttext_box : public ttext_ history_ = ttext_history::get_history(id, true); } + void set_max_input_length(const size_t length) + { + max_input_length_ = length; + } + void clear() { set_value(""); @@ -171,6 +176,9 @@ class ttext_box : public ttext_ /** The history text for this widget. */ ttext_history history_; + /** The maximum length of the text input. */ + size_t max_input_length_; + /** * The x offset in the widget where the text starts. *