Mana
Loading...
Searching...
No Matches
inttextfield.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "gui/sdlinput.h"
25
26#include "utils/stringutils.h"
27
29 TextField(toString(def)),
30 mDefault(def),
31 mValue(def)
32{
33}
34
35void IntTextField::keyPressed(gcn::KeyEvent &event)
36{
37 const gcn::Key &key = event.getKey();
38
39 if (key.getValue() == Key::BACKSPACE ||
40 key.getValue() == Key::DELETE_KEY)
41 {
42 setText(std::string());
43 event.consume();
44 }
45
46 if (!key.isNumber())
47 return;
48
50
51 std::istringstream s(getText());
52 int i;
53 s >> i;
54 setValue(i);
55}
56
57void IntTextField::setRange(int min, int max)
58{
59 mMin = min;
60 mMax = max;
61
62 if (mValue < mMin)
63 mValue = mMin;
64 else if (mValue > mMax)
65 mValue = mMax;
66
67 if (mDefault < mMin)
68 mDefault = mMin;
69 else if (mDefault > mMax)
70 mDefault = mMax;
71}
72
74{
75 return getText().empty() ? mMin : mValue;
76}
77
79{
80 if (i < mMin)
81 mValue = mMin;
82 else if (i > mMax)
83 mValue = mMax;
84 else
85 mValue = i;
86
87 const std::string valStr = toString(mValue);
88 setText(valStr);
89 setCaretPosition(valStr.length() + 1);
90}
91
93{
94 if (value < mMin)
95 mDefault = mMin;
96 else if (value > mMax)
97 mDefault = mMax;
98 else
99 mDefault = value;
100}
101
103{
105}
void keyPressed(gcn::KeyEvent &event) override
Responds to key presses.
int mDefault
Default value.
int mValue
Current value.
int mMax
Maximum value.
int mMin
Minimum value.
void reset()
Reset the field to the default value.
IntTextField(int def=0)
Constructor, sets default value.
void setRange(int minimum, int maximum)
Sets the minimum and maximum values of the text box.
int getValue()
Returns the value in the text box.
void setDefaultValue(int value)
Set the default value of the text box to the specified value.
void setValue(int value)
Set the value of the text box to the specified value.
A text field.
Definition textfield.h:72
void keyPressed(gcn::KeyEvent &keyEvent) override
Processes one keypress.
@ BACKSPACE
Definition sdlinput.h:98
@ DELETE_KEY
Definition sdlinput.h:93
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68