GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/widgets/inttextfield.cpp Lines: 28 58 48.3 %
Date: 2018-11-12 Branches: 21 56 37.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2018  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "gui/widgets/inttextfield.h"
24
25
#ifdef USE_SDL2
26
#include "enums/input/keyvalue.h"
27
#endif  // USE_SDL2
28
29
#include "utils/stringutils.h"
30
31
#include <sstream>
32
33
#include "debug.h"
34
35
34
IntTextField::IntTextField(const Widget2 *const widget,
36
                           const int def,
37
                           const int min,
38
                           const int max,
39
                           const Enable enabled,
40
34
                           const int width) :
41
68
    TextField(widget, toString(def),
42
68
        LoseFocusOnTab_true, nullptr, std::string(), false),
43
    mMin(0),
44
    mMax(0),
45
    mDefault(def),
46
68
    mValue(def)
47
{
48
34
    if (min != 0 || max != 0)
49
        setRange(min, max);
50
51
68
    setEnabled(enabled == Enable_true);
52
34
    if (width != 0)
53
22
        setWidth(width);
54
34
}
55
56
void IntTextField::keyPressed(KeyEvent &event)
57
{
58
    const InputActionT action = event.getActionId();
59
60
    if (action == InputAction::GUI_DELETE
61
        || action == InputAction::GUI_BACKSPACE)
62
    {
63
        setText(std::string());
64
        if (mSendAlwaysEvents)
65
            distributeActionEvent();
66
67
        event.consume();
68
    }
69
70
#ifdef USE_SDL2
71
    const int val = event.getKey().getValue();
72
    if (val != KeyValue::TEXTINPUT)
73
        return;
74
75
    const std::string str = event.getText();
76
    if (str.empty())
77
        return;
78
    const size_t sz = str.size();
79
    for (size_t f = 0; f < sz; f ++)
80
    {
81
        const char chr = str[f];
82
        if (chr < '0' || chr > '9')
83
            return;
84
    }
85
#else  // USE_SDL2
86
87
    if (!event.getKey().isNumber())
88
        return;
89
#endif  // USE_SDL2
90
91
    TextField::keyPressed(event);
92
93
    std::istringstream s(getText());
94
    int i;
95
    s >> i;
96
    setValue(i);
97
    if (mSendAlwaysEvents)
98
        distributeActionEvent();
99
}
100
101
4
void IntTextField::setRange(const int min, const int max)
102
{
103
33
    mMin = min;
104
33
    mMax = max;
105
106

33
    if (mValue < mMin)
107
        mValue = mMin;
108

33
    else if (mValue > mMax)
109
2
        mValue = mMax;
110
111

33
    if (mDefault < mMin)
112
        mDefault = mMin;
113

33
    else if (mDefault > mMax)
114
2
        mDefault = mMax;
115
4
}
116
117
int IntTextField::getValue() const
118
{
119
    return getText().empty() ? mMin : mValue;
120
}
121
122
5
void IntTextField::setValue(const int i)
123
{
124
5
    if (i < mMin)
125
        mValue = mMin;
126
5
    else if (i > mMax)
127
        mValue = mMax;
128
    else
129
5
        mValue = i;
130
131
10
    const std::string valStr = toString(mValue);
132
5
    setText(valStr);
133
5
    setCaretPosition(CAST_U32(valStr.length()) + 1);
134
5
}
135
136
void IntTextField::setDefaultValue(const int value)
137
{
138
    if (value < mMin)
139
        mDefault = mMin;
140
    else if (value > mMax)
141
        mDefault = mMax;
142
    else
143
        mDefault = value;
144
}
145
146
void IntTextField::reset()
147
{
148
    setValue(mDefault);
149
}