Skip to content

Commit

Permalink
ttext_box: added support for defining a maximum input length
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Feb 29, 2016
1 parent 3baba92 commit fb0285c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions data/gui/schema.cfg
Expand Up @@ -1801,6 +1801,11 @@
type="string"
default=""
[/key]
[key]
name="max_input_length"
type="int"
default=0
[/key]
[key]
name="label"
type="t_string"
Expand Down
6 changes: 5 additions & 1 deletion src/gui/auxiliary/window_builder/text_box.cpp
Expand Up @@ -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"])
{
}

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

Expand Down
2 changes: 2 additions & 0 deletions src/gui/auxiliary/window_builder/text_box.hpp
Expand Up @@ -33,6 +33,8 @@ struct tbuilder_text_box : public tbuilder_control
twidget* build() const;

std::string history;

size_t max_input_length;
};


Expand Down
8 changes: 8 additions & 0 deletions src/gui/widgets/text_box.cpp
Expand Up @@ -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)
Expand All @@ -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();
}

Expand All @@ -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()) {
Expand Down
8 changes: 8 additions & 0 deletions src/gui/widgets/text_box.hpp
Expand Up @@ -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("");
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit fb0285c

Please sign in to comment.