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

35
    if (mValue < mMin)
108
        mValue = mMin;
109

35
    else if (mValue > mMax)
110
2
        mValue = mMax;
111
112

35
    if (mDefault < mMin)
113
        mDefault = mMin;
114

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