ttext_box: added support for defining a maximum input length

This commit is contained in:
Charles Dang 2016-03-01 01:55:56 +11:00
parent 3baba9235e
commit fb0285c27f
5 changed files with 28 additions and 1 deletions

View file

@ -1801,6 +1801,11 @@
type="string"
default=""
[/key]
[key]
name="max_input_length"
type="int"
default=0
[/key]
[key]
name="label"
type="t_string"

View file

@ -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";

View file

@ -33,6 +33,8 @@ public:
twidget* build() const;
std::string history;
size_t max_input_length;
};

View file

@ -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()) {

View file

@ -131,6 +131,11 @@ public:
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 @@ private:
/** 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.
*